-
Notifications
You must be signed in to change notification settings - Fork 12
/
collect.c
286 lines (274 loc) · 6.38 KB
/
collect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* Collect mail from a mail box and put it into a message directory */
/* For now, it does no locking on the mail box, unless you have the
exclusive-access mod to the Unix kernel */
/* Mods for UW - Fixed spelling of "separate." If no "From:" field is
found, use the "From " field instead. If no "Date:" field is found,
use the date in the "From " field. */
/*
* Fixed to use lock() routine from ucb mail program.
* SWT Sat Apr 9 1983
* $Log: collect.c,v $
* Revision 1.2 93/01/11 13:24:59 mogul
* Bug fixes for MIPS, Alpha
*
* Revision 1.1 86/04/16 13:52:32 mcdaniel
* Initial revision
*
* Revision 1.3 83/04/11 13:19:07 thomas
* Add - flag for stdout output (incremental mail reading).
*
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sgtty.h>
#include <sys/ioctl.h>
#include "config.h"
#include <pwd.h>
char date[BUFSIZ],
subj[BUFSIZ],
to[BUFSIZ],
me[BUFSIZ],
from[BUFSIZ],
msgfn[20],
Fromline[BUFSIZ];
char *mbdir;
extern errno;
char mbdbuf[BUFSIZ],
mbfbuf[BUFSIZ];
long now;
int seq = 0;
int usestdout = 0;
FILE *out,
*dir;
int HaveNew = 0;
struct passwd *pw,
*getpwuid();
main (argc, argv)
char **argv;
{
struct stat st;
pw = getpwuid (getuid ());
now = time (0);
{
register char *p1 = me,
*p2 = pw ? MailOriginator : "XXXX";
while (*p2)
if (*p2 != ' ')
*p1++ = *p2++;
else {
if (p1 != me && *(p1 - 1) != '.')
*p1++ = '.';
p2++;
}
*p1++ = '\0';
}
umask (077);
if (argc > 1 && strcmp(argv[1], "-") == 0)
{
usestdout=1;
argc--;
argv++;
}
if (argc > 1)
mbdir = argv[1];
else {
sprintf (mbdbuf, "%s/Messages", getenv ("HOME"));
mbdir = mbdbuf;
}
{
register char *p;
for (p = mbdir; *p; p++);
while (*--p == '/')
*p = 0;
}
if (stat (mbdir, &st) < 0) {
int st;
if (vfork()==0){
execlp ("mkdir", "mkdir", mbdir, 0);
exit (1);
}
wait(&st);
if (st) {
printf ("Can't create mailbox directory %s\n", mbdir);
exit (1);
}
chmod (mbdir, 0700);
}
if (! usestdout)
{
sprintf (subj, "%s/Directory", mbdir);
dir = fopen (subj, "a");
if (dir == 0) {
printf ("Can't append to directory\n");
exit (1);
}
}
else
dir = stdout;
if (argc > 2)
do {
ScanBox (argv[2]);
argv++;
} while (--argc > 2);
else {
sprintf (mbfbuf, "/usr/spool/mail/%s", getenv ("USER"));
ScanBox (mbfbuf);
}
fclose (dir);
}
ScanBox (mbf)
char *mbf; {
int inheader = 0;
int RationalSeparators = 0;/* true iff entries in this mbox are
rationally separated */
register FILE * in;
struct stat st;
out = 0;
inheader = 0;
if (mbf==0) return;
if (stat (mbf, &st) < 0 || st.st_size <= 0)
return;
#ifndef FXMUPD
if ((in = fopen (mbf, "r")) == 0)
return;
lock(mbf); /* lock the mail file */
#else
{ static mode = FXMUPD; int lock;int infd;
while ((infd = open (mbf, 2))<0 && errno==EBUSY
|| (lock = ioctl (infd, FIOCXMOD, &mode))<0 && errno==EBUSY) {
if (infd>=0) close(infd);
sleep (5);
}
if (lock<0 || infd<0) perror("collectmail");
in = fdopen (infd, "r");
}
#endif
if (in==0) return;
while (1) {
char line[BUFSIZ];
if (fgets (line, sizeof line, in) == 0)
break;
if (line[0] == '\n') {
if (out == 0)
continue;
inheader = 0;
fputs (line, out);
continue;
}
if (!inheader && !RationalSeparators &&
(strncmp (line, "From ", 5) == 0
|| strncmp (line, "Date: ", 6) == 0)
|| out == 0
|| strncmp (line, "\003\n", 2) == 0) {
CloseMessage ();
inheader++;
/***/ if (strncmp(line, "From ", 5) == 0)
strcpy(Fromline, line);
else
Fromline[0] = '\0';
do {
sprintf (msgfn, "%011o%03o", now, seq++);
sprintf (subj, "%s/%s", mbdir, msgfn);
} while (stat (subj, &st) >= 0);
out = fopen (subj, "w");
subj[0] = to[0] = date[0] = from[0] = 0;
if (line[0] == 3) {
RationalSeparators++;
continue;
}
}
if (inheader) {
extractfield (line, "from", from, sizeof from) ;
extractfield (line, "subject", subj, sizeof subj) ;
extractfield (line, "subj", subj, sizeof subj) ;
extractfield (line, "to", to, sizeof to) ;
extractfield (line, "date", date, sizeof date) ;
}
if (out)
fputs (line, out);
}
CloseMessage ();
fflush (dir);
fclose (in); /* There's a narrow window in the locking
here, but I can't truncate the file
without closing it and losing the lock */
creat (mbf, 0600);
#ifndef FXMUPD
unlock(); /* unlock the mail file */
#endif
}
extractfield (line, field, buf, bufsz)
register char *field, *line, *buf; {
while (1) {
while (isspace(*field)) field++;
while (isspace(*line)) line++;
if (*field==0) break;
if (*field++ != (isupper (*line) ? tolower (*line++) : *line++)) {
return 0;
}
};
if (*line++ != ':') return 0;
while (isspace (*line)) line++;
while (--bufsz > 0 && *line != '\n' && (*buf++ = *line++));
*buf++ = 0;
return 1;
}
char *trim (str, limit)
register char *str;
{
register char *p;
while (isspace(*str))
str++;
for (p = str; *p; p++);
if (limit) {
register char *at = (char *) sindex (str, " at ");
if (at) *at = '\0', p = at;
}
while (isspace (*--p) && p >= str)
*p = 0;
if (limit && p - str > limit)
for (p = str + limit; p > str; p--)
if (*p == ' ') {
*p = '\0';
break;
}
return * str ? str : "[empty]";
}
CloseMessage () {
int i;
char *f, *d;
/***/ if (from[0] == '\0' && *Fromline) {
for (i = 5; Fromline[i] == ' '; i++)
;
f = from;
while (Fromline[i] != ' ' && Fromline[i] != '\0')
*f++ = Fromline[i++];
*f = '\0';
}
/***/ if (date[0] == '\0' && *Fromline) {
for (i = 5; Fromline[i] == ' '; i++) /* Skip to fromname */
;
while (Fromline[i] != ' ' && Fromline[i] != '\0')
i++;
while (Fromline[i] == ' ') /* Skip to date */
i++;
d = date;
while (Fromline[i] != '\n' && Fromline[i] != '\0')
*d++ = Fromline[i++];
*d = '\0';
}
if (out == 0)
return;
if (sindex (from, me))
fprintf (dir, " B %14s %-12.12s =>%-13.13s %.30s\n",
msgfn, trim (date, 12), trim (to, 13), trim (subj, 0));
else {
fprintf (dir, " N %14s %-12.12s %-15.15s %.30s\n",
msgfn, trim (date, 12), trim (from, 15), trim (subj, 0));
HaveNew++;
}
fclose (out);
}