]> diplodocus.org Git - nmh/blob - docs/historical/2.9BSD/cmds/inc.c
Added test of encoded Resent-To: and Resent-cc:.
[nmh] / docs / historical / 2.9BSD / cmds / inc.c
1 #include "mh.h"
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <strings.h>
7 #include <signal.h>
8
9 char *anoyes[]; /* Std no/yes gans array */
10
11 extern int errno;
12 char scanl[];
13 struct msgs *mp;
14 FILE *in, *aud;
15 struct stat stbuf;
16 char *locknode;
17 int lockwait; /* Secs to wait for lock-Def in strings/lockdir.c */
18 #define LOCKWAIT (lockwait*5) /* If lock is this old, simply ignore it! */
19
20 struct swit switches[] = {
21 "audit audit-file", 0, /* 0 */
22 "ms ms-folder", 0, /* 1 */
23 "help", 4, /* 2 */
24 "file mail-file", 0, /* 3 */
25 0, 0
26 };
27
28 #ifdef UNIXCOMP
29 char unixtmp[] = "/tmp/mhXXXXXX";
30 #endif
31
32 main(argc, argv)
33 char *argv[];
34 {
35
36 char *newmail, maildir[128], *folder, *from, *audfile, *srcfile;
37 char mailspace[128];
38 char *myname, *savemail;
39 register char *cp;
40 register int i, msgnum;
41 long now;
42 char **ap;
43 char *arguments[50], **argp;
44 int done();
45 long time();
46 int pid, status, fildes;
47 extern char _sobuf[];
48
49 setbuf(stdout, _sobuf);
50 #ifdef NEWS
51 m_news();
52 #endif
53
54 from = 0; folder = 0; audfile = 0; srcfile = 0;
55 cp = r1bindex(argv[0], '/');
56 if((cp = m_find(cp)) != NULL) {
57 ap = brkstring(cp = getcpy(cp), " ", "\n");
58 ap = copyip(ap, arguments);
59 } else
60 ap = arguments;
61 copyip(argv+1, ap);
62 argp = arguments;
63 while(cp = *argp++) {
64 if(*cp == '-')
65 switch(smatch(++cp, switches)) {
66 case -2:ambigsw(cp, switches); /* ambiguous */
67 goto leave;
68 /* unknown */
69 case -1:fprintf(stderr, "inc: -%s unknown\n", cp);
70 goto leave;
71 case 0: if(!(audfile = *argp++)) { /* -audit */
72 missing: fprintf(stderr, "inc: Missing argument for %s switch\n", argp[-2]);
73 goto leave;
74 }
75 continue;
76 case 1: if(!(from = *argp++)) /* -ms */
77 goto missing;
78 continue;
79 case 2: /* -help */
80 help("inc [+folder] [switches]", switches);
81 goto leave;
82 case 3: /* -file */
83 if (!(srcfile = *argp++))
84 goto missing;
85 continue;
86 }
87 if(*cp == '+') {
88 if(folder) {
89 fprintf(stderr, "Only one folder at a time.\n");
90 goto leave;
91 } else
92 folder = cp + 1;
93 } else {
94 fprintf(stderr, "Bad arg: %s\n", argp[-1]);
95 fprintf(stderr, "Usage: inc [+folder] [-ms ms-folder] [-audit audit-file]\n");
96 goto leave;
97 }
98 }
99 if (from && srcfile) {
100 fprintf(stderr, "Only one of \"-ms\" and \"-file\" allowed\n");
101 goto leave;
102 }
103 if(from)
104 newmail = from;
105 else {
106 if((myname = getenv("USER")) == 0) {
107 fprintf(stderr,
108 "Environment Variable \"USER\" Must be set to your login name!\n");
109 done(1);
110 }
111 if (srcfile) {
112 newmail = srcfile;
113 goto statit;
114 }
115 newmail = concat(mailboxes, myname, 0);
116 if (stat(newmail, &stbuf) >= 0
117 && (stbuf.st_mode & S_IFMT) == S_IFDIR) {
118 strcpy(mailspace, newmail);
119 newmail = mailspace;
120 strcat(newmail, "/");
121 strcat(newmail, myname);
122 }
123 statit:
124 if(stat(newmail, &stbuf) < 0 ||
125 stbuf.st_size == 0) {
126 fprintf(stderr, "No Mail to incorporate.\n");
127 goto leave;
128 }
129 }
130 fildes = -1;
131 if (srcfile)
132 if ((fildes = open(srcfile, 0)) < 0) {
133 perror(srcfile);
134 goto leave;
135 }
136 if(!folder) {
137 folder = defalt;
138 if(from && strcmp(from, "inbox") == 0) {
139 cp = concat("Do you really want to convert from ",
140 from, " into ", folder, "?? ", 0);
141 if(!gans(cp, anoyes))
142 goto leave;
143 cndfree(cp);
144 }
145 }
146 copy(m_maildir(folder), maildir);
147 if(stat(maildir, &stbuf) < 0) {
148 if(errno != ENOENT) {
149 fprintf(stderr, "Error on folder ");
150 perror(maildir);
151 goto leave;
152 }
153 cp = concat("Create folder \"", maildir, "\"? ", 0);
154 if(!gans(cp, anoyes))
155 goto leave;
156 if(!makedir(maildir)) {
157 fprintf(stderr, "Can't create folder \"%s\"\n", maildir);
158 goto leave;
159 }
160 }
161 if(chdir(maildir) < 0) {
162 fprintf(stderr, "Can't chdir to: ");
163 perror(maildir);
164 goto leave;
165 }
166 if(!(mp = m_gmsg(folder))) {
167 fprintf(stderr, "Can't read folder!?\n");
168 goto leave;
169 }
170 /* Lock the mail file */
171 if(!from && !srcfile) {
172 signal(SIGINT, done);
173 cp = concat(lockdir, myname, 0);
174 for(i = 0; i < lockwait; i += 2) {
175 if(link(newmail, cp) == -1) {
176 fprintf(stderr, "Mailbox busy...\n");
177 if(i == 0 && stat(newmail, &stbuf) >= 0)
178 if(stbuf.st_ctime + LOCKWAIT < time((long *)0)) {
179 unlink(cp);
180 fprintf(stderr, "Removing lock.\n");
181 continue;
182 }
183 sleep(2);
184 } else {
185 locknode = cp; /* We own the lock now! */
186 break;
187 }
188 }
189 if(i >= lockwait) {
190 fprintf(stderr, "Try again.\n");
191 done(1);
192 }
193 }
194
195 #ifdef UNIXCOMP
196 /*
197 * If trying to be compatible with standard
198 * UNIX mailing scheme, call the program unixtomh
199 * to convert the mailbox to some temporary name.
200 */
201
202 mktemp(unixtmp);
203 switch (pid = fork()) {
204 case 0:
205 if (srcfile) {
206 int nout;
207
208 if ((nout = creat(unixtmp, 0600)) < 0) {
209 perror(unixtmp);
210 _exit(1);
211 }
212 close(0);
213 dup(fildes);
214 close(fildes);
215 close(1);
216 dup(nout);
217 close(nout);
218 execl(unixtomh, "unixtomh", 0);
219 perror(unixtomh);
220 _exit(1);
221 }
222 execl(unixtomh, "unixtomh", newmail, unixtmp, 0);
223 perror(unixtomh);
224 _exit(1);
225 break;
226
227 case -1:
228 perror("fork");
229 goto leave;
230
231 default:
232 while (wait(&status) != pid)
233 ;
234 if (status != 0) {
235 fprintf(stderr, "unixtomh failed!?\n");
236 goto leave;
237 }
238 }
239 if (fildes >= 0)
240 close(fildes);
241 savemail = newmail;
242 newmail = unixtmp;
243 if((in = fopen(newmail, "r")) == NULL) {
244 fprintf(stderr, "Can't open "); perror(newmail);
245 goto leave;
246 }
247 #else
248 if (srcfile)
249 in = fdopen(fildes, "r");
250 else
251 if((in = fopen(newmail, "r")) == NULL) {
252 fprintf(stderr, "Can't open "); perror(newmail);
253 goto leave;
254 }
255 #endif
256
257 if(audfile) {
258 cp = m_maildir(audfile);
259 if((i = stat(cp, &stbuf)) < 0)
260 fprintf(stderr, "Creating Receive-Audit: %s\n", cp);
261 if((aud = fopen(cp, "a")) == NULL) {
262 fprintf(stderr, "Can't append to ");
263 perror(cp);
264 goto leave;
265 } else if(i < 0)
266 chmod(cp, 0600);
267 time(&now);
268 fputs("<<inc>> ", aud);
269 cp = cdate(&now);
270 cp[9] = ' ';
271 fputs(cp, aud);
272 if(from) {
273 fputs(" -ms ", aud);
274 fputs(from, aud);
275 }
276 putc('\n', aud);
277 }
278 printf("Incorporating new mail into %s...\n\n", folder);
279 fflush(stdout);
280 msgnum = mp->hghmsg;
281
282 while((i = scan(in, msgnum+1, msgnum+1, msgnum == mp->hghmsg))) {
283 if(i == -1) {
284 fprintf(stderr, "inc aborted!\n");
285 if(aud)
286 fputs("inc aborted!\n", aud);
287 goto leave;
288 }
289 if(aud)
290 fputs(scanl, aud);
291 fflush(stdout);
292 msgnum++;
293 }
294
295 fclose(in);
296 if(aud)
297 fclose(aud);
298
299 #ifdef UNIXCOMP
300 unlink(newmail);
301 newmail = savemail;
302 #endif
303
304 if(!from && !srcfile) {
305 if (unlink(newmail) < 0) {
306 if((i = creat(newmail, 0600)) >= 0)
307 close(i);
308 else
309 fprintf(stderr, "Error zeroing %s\n", newmail);
310 }
311 }
312
313 i = msgnum - mp->hghmsg;
314 if(!i)
315 fprintf(stderr, "[No messages incorporated.]\n");
316 else {
317 m_replace(pfolder, folder);
318 m_setcur(mp->hghmsg + 1);
319 }
320 leave:
321 m_update();
322 done(0);
323 }
324
325
326 done(status)
327 {
328 if(locknode);
329 unlink(locknode);
330 exit(status);
331 }