xref: /original-bsd/usr.bin/xsend/xget/xget.c (revision a1c2194a)
1 /*-
2  * %sccs.include.proprietary.c%
3  */
4 
5 #ifndef lint
6 static char sccsid[] = "@(#)xget.c	4.6 (Berkeley) 04/18/91";
7 #endif /* not lint */
8 
9 #include <sys/types.h>
10 #include <sys/dir.h>
11 #include <sys/stat.h>
12 #include <ctype.h>
13 #include <pwd.h>
14 #include "xmail.h"
15 #include "pathnames.h"
16 
17 char *myname;
18 int uid;
19 struct direct *dbuf;
20 char *maildir = _PATH_SECRETMAIL;
21 FILE *kf, *mf;
22 DIR *df;
23 MINT *x, *b, *one, *t45, *z, *q, *r;
24 MINT *two, *t15, *mbuf;
25 char buf[256], line[128];
26 #define MXF 100
27 int fnum[MXF], fcnt;
28 struct stat stbuf;
29 main()
30 {	int i;
31 	char *p;
32 
33 	uid = getuid();
34 	myname = (char *)getlogin();
35 	if(myname == NULL)
36 		myname = getpwuid(uid)->pw_name;
37 	comminit();
38 	mbuf = itom(0);
39 	files();
40 	setup(getpass("Key: "));
41 	mkb();
42 	mkx();
43 #ifndef debug
44 	invert(x, b, x);
45 #else
46 	invert(x, b, z);
47 	mult(x, z, z);
48 	mdiv(z, b, q, z);
49 	omout(z);
50 	invert(x, b, x);
51 #endif
52 	for(i=0; i<fcnt; i++)
53 	{	sprintf(line, "%s%s.%d", maildir, myname, fnum[i]);
54 		if(stat(line, &stbuf)<0)
55 		{	perror(line);
56 			continue;
57 		}
58 		if(stbuf.st_size == 0)
59 		{	printf("zero length mail file\n");
60 			unlink(line);
61 			continue;
62 		}
63 		if((mf = fopen(line, "r"))==NULL)
64 		{	perror(line);
65 			continue;
66 		}
67 		decipher(mf, stdout);
68 	cmnd:
69 		printf("? ");
70 		fgets(buf, sizeof(buf), stdin);
71 		if(feof(stdin)) exit(0);
72 		switch(buf[0])
73 		{
74 		case 'q':
75 			exit(0);
76 		case 'n':
77 		case 'd':
78 		case '\n':
79 			fclose(mf);
80 			unlink(line);
81 			break;
82 		case '!':
83 			system(buf+1);
84 			printf("!\n");
85 			goto cmnd;
86 		case 's':
87 		case 'w':
88 			rewind(mf);
89 			if(buf[1] == '\n' || buf[1] == '\0')
90 				strcpy(buf, "s mbox\n");
91 			for(p = buf+1; isspace(*p); p++);
92 			p[strlen(p)-1] = 0;
93 			kf = fopen(p, "a");
94 			if(kf == NULL)
95 			{	perror(p);
96 				goto cmnd;
97 			}
98 			decipher(mf, kf);
99 			fclose(mf);
100 			fclose(kf);
101 			unlink(line);
102 			break;
103 		default:
104 			printf("Commands are:\n");
105 			printf("q	quit, leaving unread messages\n");
106 			printf("n	delete current message and goto next\n");
107 			printf("d	same as above\n");
108 			printf("\\n	same as above\n");
109 			printf("!	execute shell command\n");
110 			printf("s	save message in the named file or mbox\n");
111 			printf("w	same as above\n");
112 			printf("?	prints this list\n");
113 			goto cmnd;
114 		}
115 	}
116 	exit(0);
117 }
118 icmp(a, b) int *a, *b;
119 {
120 	return(*a - *b);
121 }
122 files()
123 {	int i;
124 	if((df = opendir(maildir)) == NULL)
125 	{	perror(maildir);
126 		exit(1);
127 	}
128 	strcpy(line, myname);
129 	strcat(line, ".%d");
130 	while ((dbuf = readdir(df)) != NULL)
131 	{
132 		if(sscanf(dbuf->d_name, line, &i) != 1)
133 			continue;
134 		if(fcnt >= MXF)
135 			break;
136 		fnum[fcnt++] = i;
137 	}
138 	closedir(df);
139 	if(fcnt == 0)
140 	{	printf("no secret mail\n");
141 		exit(0);
142 	}
143 	qsort(fnum, fcnt, sizeof(int), icmp);
144 }
145 decipher(u, w) FILE *u, *w;
146 {	int i;
147 	short a;
148 	for(;;)
149 	{	nin(mbuf, u);
150 		if(feof(u)) break;
151 		mult(mbuf, x, mbuf);
152 		mdiv(mbuf, b, q, mbuf);
153 		for(i=1; i<=3; i++)
154 		{	a = mbuf->val[i];
155 			putc(a&0177, w);
156 			a >>= 8;
157 			putc(a&0177, w);
158 		}
159 	}
160 }
161