xref: /csrg-svn/usr.bin/xsend/xget/xget.c (revision 37916)
111788Ssam #ifndef lint
2*37916Sbostic static char sccsid[] = "@(#)xget.c	4.5 05/11/89";
311788Ssam #endif
411788Ssam 
513481Ssam #include <sys/types.h>
613481Ssam #include <sys/dir.h>
7*37916Sbostic #include <sys/stat.h>
813481Ssam #include <ctype.h>
913481Ssam #include <pwd.h>
10*37916Sbostic #include "xmail.h"
11*37916Sbostic #include "pathnames.h"
12*37916Sbostic 
1311788Ssam char *myname;
1411788Ssam int uid;
1512387Smckusick struct direct *dbuf;
16*37916Sbostic char *maildir = _PATH_SECRETMAIL;
1712387Smckusick FILE *kf, *mf;
1812387Smckusick DIR *df;
1911788Ssam MINT *x, *b, *one, *t45, *z, *q, *r;
2011788Ssam MINT *two, *t15, *mbuf;
2111788Ssam char buf[256], line[128];
2211788Ssam #define MXF 100
2311788Ssam int fnum[MXF], fcnt;
2411788Ssam struct stat stbuf;
2511788Ssam main()
2611788Ssam {	int i;
2711788Ssam 	char *p;
2812387Smckusick 
2911788Ssam 	uid = getuid();
3012387Smckusick 	myname = (char *)getlogin();
3111788Ssam 	if(myname == NULL)
3211788Ssam 		myname = getpwuid(uid)->pw_name;
3311788Ssam 	comminit();
3411788Ssam 	mbuf = itom(0);
3511788Ssam 	files();
3611788Ssam 	setup(getpass("Key: "));
3711788Ssam 	mkb();
3811788Ssam 	mkx();
3911788Ssam #ifndef debug
4011788Ssam 	invert(x, b, x);
4111788Ssam #else
4211788Ssam 	invert(x, b, z);
4311788Ssam 	mult(x, z, z);
4411788Ssam 	mdiv(z, b, q, z);
4511788Ssam 	omout(z);
4611788Ssam 	invert(x, b, x);
4711788Ssam #endif
4811788Ssam 	for(i=0; i<fcnt; i++)
4911788Ssam 	{	sprintf(line, "%s%s.%d", maildir, myname, fnum[i]);
5011788Ssam 		if(stat(line, &stbuf)<0)
5111788Ssam 		{	perror(line);
5211788Ssam 			continue;
5311788Ssam 		}
5411788Ssam 		if(stbuf.st_size == 0)
5511788Ssam 		{	printf("zero length mail file\n");
5611788Ssam 			unlink(line);
5711788Ssam 			continue;
5811788Ssam 		}
5911788Ssam 		if((mf = fopen(line, "r"))==NULL)
6011788Ssam 		{	perror(line);
6111788Ssam 			continue;
6211788Ssam 		}
6311788Ssam 		decipher(mf, stdout);
6411788Ssam 	cmnd:
6511788Ssam 		printf("? ");
6611788Ssam 		fgets(buf, sizeof(buf), stdin);
6711788Ssam 		if(feof(stdin)) exit(0);
6811788Ssam 		switch(buf[0])
6911788Ssam 		{
7011788Ssam 		case 'q':
7111788Ssam 			exit(0);
7211788Ssam 		case 'n':
7311788Ssam 		case 'd':
7411788Ssam 		case '\n':
7512565Sralph 			fclose(mf);
7611788Ssam 			unlink(line);
7711788Ssam 			break;
7811788Ssam 		case '!':
7911788Ssam 			system(buf+1);
8011788Ssam 			printf("!\n");
8111788Ssam 			goto cmnd;
8211788Ssam 		case 's':
8311788Ssam 		case 'w':
8411788Ssam 			rewind(mf);
8511788Ssam 			if(buf[1] == '\n' || buf[1] == '\0')
8611788Ssam 				strcpy(buf, "s mbox\n");
8712565Sralph 			for(p = buf+1; isspace(*p); p++);
8811788Ssam 			p[strlen(p)-1] = 0;
8911788Ssam 			kf = fopen(p, "a");
9011788Ssam 			if(kf == NULL)
9111788Ssam 			{	perror(p);
9212565Sralph 				goto cmnd;
9311788Ssam 			}
9411788Ssam 			decipher(mf, kf);
9511788Ssam 			fclose(mf);
9611788Ssam 			fclose(kf);
9711788Ssam 			unlink(line);
9811788Ssam 			break;
9912565Sralph 		default:
10012565Sralph 			printf("Commands are:\n");
10112565Sralph 			printf("q	quit, leaving unread messages\n");
10212565Sralph 			printf("n	delete current message and goto next\n");
10312565Sralph 			printf("d	same as above\n");
10412565Sralph 			printf("\\n	same as above\n");
10512565Sralph 			printf("!	execute shell command\n");
10612565Sralph 			printf("s	save message in the named file or mbox\n");
10712565Sralph 			printf("w	same as above\n");
10812565Sralph 			printf("?	prints this list\n");
10912565Sralph 			goto cmnd;
11011788Ssam 		}
11111788Ssam 	}
11211788Ssam 	exit(0);
11311788Ssam }
11411788Ssam icmp(a, b) int *a, *b;
11511788Ssam {
11611788Ssam 	return(*a - *b);
11711788Ssam }
11811788Ssam files()
11911788Ssam {	int i;
12012387Smckusick 	if((df = opendir(maildir)) == NULL)
12111788Ssam 	{	perror(maildir);
12211788Ssam 		exit(1);
12311788Ssam 	}
12411788Ssam 	strcpy(line, myname);
12511788Ssam 	strcat(line, ".%d");
12612387Smckusick 	while ((dbuf = readdir(df)) != NULL)
12712565Sralph 	{
12812565Sralph 		if(sscanf(dbuf->d_name, line, &i) != 1)
12911788Ssam 			continue;
13011788Ssam 		if(fcnt >= MXF)
13111788Ssam 			break;
13211788Ssam 		fnum[fcnt++] = i;
13311788Ssam 	}
13412387Smckusick 	closedir(df);
13511788Ssam 	if(fcnt == 0)
13611788Ssam 	{	printf("no secret mail\n");
13711788Ssam 		exit(0);
13811788Ssam 	}
13911788Ssam 	qsort(fnum, fcnt, sizeof(int), icmp);
14011788Ssam }
14111788Ssam decipher(u, w) FILE *u, *w;
14211788Ssam {	int i;
14311788Ssam 	short a;
14411788Ssam 	for(;;)
14511788Ssam 	{	nin(mbuf, u);
14611788Ssam 		if(feof(u)) break;
14711788Ssam 		mult(mbuf, x, mbuf);
14811788Ssam 		mdiv(mbuf, b, q, mbuf);
14911788Ssam 		for(i=1; i<=3; i++)
15011788Ssam 		{	a = mbuf->val[i];
15111788Ssam 			putc(a&0177, w);
15211788Ssam 			a >>= 8;
15311788Ssam 			putc(a&0177, w);
15411788Ssam 		}
15511788Ssam 	}
15611788Ssam }
157