xref: /csrg-svn/usr.bin/xsend/xget/xget.c (revision 62468)
148338Sbostic /*-
248338Sbostic  * %sccs.include.proprietary.c%
348338Sbostic  */
448338Sbostic 
511788Ssam #ifndef lint
6*62468Sbostic static char sccsid[] = "@(#)xget.c	8.1 (Berkeley) 06/06/93";
748338Sbostic #endif /* not lint */
811788Ssam 
913481Ssam #include <sys/types.h>
1013481Ssam #include <sys/dir.h>
1137916Sbostic #include <sys/stat.h>
1213481Ssam #include <ctype.h>
1313481Ssam #include <pwd.h>
1437916Sbostic #include "xmail.h"
1537916Sbostic #include "pathnames.h"
1637916Sbostic 
1711788Ssam char *myname;
1811788Ssam int uid;
1912387Smckusick struct direct *dbuf;
2037916Sbostic char *maildir = _PATH_SECRETMAIL;
2112387Smckusick FILE *kf, *mf;
2212387Smckusick DIR *df;
2311788Ssam MINT *x, *b, *one, *t45, *z, *q, *r;
2411788Ssam MINT *two, *t15, *mbuf;
2511788Ssam char buf[256], line[128];
2611788Ssam #define MXF 100
2711788Ssam int fnum[MXF], fcnt;
2811788Ssam struct stat stbuf;
main()2911788Ssam main()
3011788Ssam {	int i;
3111788Ssam 	char *p;
3212387Smckusick 
3311788Ssam 	uid = getuid();
3412387Smckusick 	myname = (char *)getlogin();
3511788Ssam 	if(myname == NULL)
3611788Ssam 		myname = getpwuid(uid)->pw_name;
3711788Ssam 	comminit();
3811788Ssam 	mbuf = itom(0);
3911788Ssam 	files();
4011788Ssam 	setup(getpass("Key: "));
4111788Ssam 	mkb();
4211788Ssam 	mkx();
4311788Ssam #ifndef debug
4411788Ssam 	invert(x, b, x);
4511788Ssam #else
4611788Ssam 	invert(x, b, z);
4711788Ssam 	mult(x, z, z);
4811788Ssam 	mdiv(z, b, q, z);
4911788Ssam 	omout(z);
5011788Ssam 	invert(x, b, x);
5111788Ssam #endif
5211788Ssam 	for(i=0; i<fcnt; i++)
5311788Ssam 	{	sprintf(line, "%s%s.%d", maildir, myname, fnum[i]);
5411788Ssam 		if(stat(line, &stbuf)<0)
5511788Ssam 		{	perror(line);
5611788Ssam 			continue;
5711788Ssam 		}
5811788Ssam 		if(stbuf.st_size == 0)
5911788Ssam 		{	printf("zero length mail file\n");
6011788Ssam 			unlink(line);
6111788Ssam 			continue;
6211788Ssam 		}
6311788Ssam 		if((mf = fopen(line, "r"))==NULL)
6411788Ssam 		{	perror(line);
6511788Ssam 			continue;
6611788Ssam 		}
6711788Ssam 		decipher(mf, stdout);
6811788Ssam 	cmnd:
6911788Ssam 		printf("? ");
7011788Ssam 		fgets(buf, sizeof(buf), stdin);
7111788Ssam 		if(feof(stdin)) exit(0);
7211788Ssam 		switch(buf[0])
7311788Ssam 		{
7411788Ssam 		case 'q':
7511788Ssam 			exit(0);
7611788Ssam 		case 'n':
7711788Ssam 		case 'd':
7811788Ssam 		case '\n':
7912565Sralph 			fclose(mf);
8011788Ssam 			unlink(line);
8111788Ssam 			break;
8211788Ssam 		case '!':
8311788Ssam 			system(buf+1);
8411788Ssam 			printf("!\n");
8511788Ssam 			goto cmnd;
8611788Ssam 		case 's':
8711788Ssam 		case 'w':
8811788Ssam 			rewind(mf);
8911788Ssam 			if(buf[1] == '\n' || buf[1] == '\0')
9011788Ssam 				strcpy(buf, "s mbox\n");
9112565Sralph 			for(p = buf+1; isspace(*p); p++);
9211788Ssam 			p[strlen(p)-1] = 0;
9311788Ssam 			kf = fopen(p, "a");
9411788Ssam 			if(kf == NULL)
9511788Ssam 			{	perror(p);
9612565Sralph 				goto cmnd;
9711788Ssam 			}
9811788Ssam 			decipher(mf, kf);
9911788Ssam 			fclose(mf);
10011788Ssam 			fclose(kf);
10111788Ssam 			unlink(line);
10211788Ssam 			break;
10312565Sralph 		default:
10412565Sralph 			printf("Commands are:\n");
10512565Sralph 			printf("q	quit, leaving unread messages\n");
10612565Sralph 			printf("n	delete current message and goto next\n");
10712565Sralph 			printf("d	same as above\n");
10812565Sralph 			printf("\\n	same as above\n");
10912565Sralph 			printf("!	execute shell command\n");
11012565Sralph 			printf("s	save message in the named file or mbox\n");
11112565Sralph 			printf("w	same as above\n");
11212565Sralph 			printf("?	prints this list\n");
11312565Sralph 			goto cmnd;
11411788Ssam 		}
11511788Ssam 	}
11611788Ssam 	exit(0);
11711788Ssam }
icmp(a,b)11811788Ssam icmp(a, b) int *a, *b;
11911788Ssam {
12011788Ssam 	return(*a - *b);
12111788Ssam }
files()12211788Ssam files()
12311788Ssam {	int i;
12412387Smckusick 	if((df = opendir(maildir)) == NULL)
12511788Ssam 	{	perror(maildir);
12611788Ssam 		exit(1);
12711788Ssam 	}
12811788Ssam 	strcpy(line, myname);
12911788Ssam 	strcat(line, ".%d");
13012387Smckusick 	while ((dbuf = readdir(df)) != NULL)
13112565Sralph 	{
13212565Sralph 		if(sscanf(dbuf->d_name, line, &i) != 1)
13311788Ssam 			continue;
13411788Ssam 		if(fcnt >= MXF)
13511788Ssam 			break;
13611788Ssam 		fnum[fcnt++] = i;
13711788Ssam 	}
13812387Smckusick 	closedir(df);
13911788Ssam 	if(fcnt == 0)
14011788Ssam 	{	printf("no secret mail\n");
14111788Ssam 		exit(0);
14211788Ssam 	}
14311788Ssam 	qsort(fnum, fcnt, sizeof(int), icmp);
14411788Ssam }
decipher(u,w)14511788Ssam decipher(u, w) FILE *u, *w;
14611788Ssam {	int i;
14711788Ssam 	short a;
14811788Ssam 	for(;;)
14911788Ssam 	{	nin(mbuf, u);
15011788Ssam 		if(feof(u)) break;
15111788Ssam 		mult(mbuf, x, mbuf);
15211788Ssam 		mdiv(mbuf, b, q, mbuf);
15311788Ssam 		for(i=1; i<=3; i++)
15411788Ssam 		{	a = mbuf->val[i];
15511788Ssam 			putc(a&0177, w);
15611788Ssam 			a >>= 8;
15711788Ssam 			putc(a&0177, w);
15811788Ssam 		}
15911788Ssam 	}
16011788Ssam }
161