Commit | Line | Data |
---|---|---|
3184019b KS |
1 | # |
2 | ||
3 | #include "rcv.h" | |
4 | #ifdef VMUNIX | |
5 | #include <wait.h> | |
6 | #endif | |
a6b3a6c5 | 7 | #include <ctype.h> |
a1f76dd3 | 8 | #include <sys/stat.h> |
3184019b KS |
9 | |
10 | /* | |
11 | * Mail -- a mail program | |
12 | * | |
13 | * Mail to others. | |
14 | */ | |
15 | ||
85e62533 | 16 | static char *SccsId = "@(#)send.c 2.11 %G%"; |
3184019b KS |
17 | |
18 | /* | |
19 | * Send message described by the passed pointer to the | |
20 | * passed output buffer. Return -1 on error, but normally | |
57079810 | 21 | * the number of lines written. Adjust the status: field |
b870137d | 22 | * if need be. If doign is set, suppress ignored header fields. |
3184019b | 23 | */ |
b870137d | 24 | send(mailp, obuf, doign) |
3184019b KS |
25 | struct message *mailp; |
26 | FILE *obuf; | |
27 | { | |
28 | register struct message *mp; | |
29 | register int t; | |
0154300b | 30 | long c; |
3184019b | 31 | FILE *ibuf; |
b870137d KS |
32 | char line[LINESIZE], field[BUFSIZ]; |
33 | int lc, ishead, infld, fline, dostat; | |
34 | char *cp, *cp2; | |
3184019b KS |
35 | |
36 | mp = mailp; | |
37 | ibuf = setinput(mp); | |
0154300b | 38 | c = mp->m_size; |
b870137d KS |
39 | ishead = 1; |
40 | dostat = 1; | |
a6b3a6c5 KS |
41 | infld = 0; |
42 | fline = 1; | |
3184019b | 43 | lc = 0; |
0154300b | 44 | while (c > 0L) { |
57079810 | 45 | fgets(line, LINESIZE, ibuf); |
0154300b | 46 | c -= (long) strlen(line); |
57079810 KS |
47 | lc++; |
48 | if (ishead) { | |
b870137d KS |
49 | /* |
50 | * First line is the From line, so no headers | |
51 | * there to worry about | |
52 | */ | |
a6b3a6c5 KS |
53 | if (fline) { |
54 | fline = 0; | |
55 | goto writeit; | |
56 | } | |
b870137d KS |
57 | /* |
58 | * If line is blank, we've reached end of | |
59 | * headers, so force out status: field | |
60 | * and note that we are no longer in header | |
61 | * fields | |
62 | */ | |
57079810 | 63 | if (line[0] == '\n') { |
b870137d KS |
64 | if (dostat) { |
65 | statusput(mailp, obuf, doign); | |
66 | dostat = 0; | |
67 | } | |
57079810 KS |
68 | ishead = 0; |
69 | goto writeit; | |
70 | } | |
b870137d | 71 | /* |
df5134c8 BB |
72 | * If this line is a continuation (via space or tab) |
73 | * of a previous header field, just echo it | |
74 | * (unless the field should be ignored). | |
b870137d | 75 | */ |
df5134c8 BB |
76 | if (infld && (isspace(line[0]) || line[0] == '\t')) { |
77 | if (doign && isign(field)) continue; | |
a6b3a6c5 | 78 | goto writeit; |
df5134c8 | 79 | } |
a6b3a6c5 | 80 | infld = 0; |
b870137d KS |
81 | /* |
82 | * If we are no longer looking at real | |
83 | * header lines, force out status: | |
84 | * This happens in uucp style mail where | |
85 | * there are no headers at all. | |
86 | */ | |
a6b3a6c5 | 87 | if (!headerp(line)) { |
b870137d KS |
88 | if (dostat) { |
89 | statusput(mailp, obuf, doign); | |
90 | dostat = 0; | |
91 | } | |
a6b3a6c5 KS |
92 | putc('\n', obuf); |
93 | ishead = 0; | |
57079810 | 94 | goto writeit; |
a6b3a6c5 KS |
95 | } |
96 | infld++; | |
b870137d KS |
97 | /* |
98 | * Pick up the header field. | |
99 | * If it is an ignored field and | |
100 | * we care about such things, skip it. | |
101 | */ | |
102 | cp = line; | |
103 | cp2 = field; | |
104 | while (*cp && *cp != ':' && !isspace(*cp)) | |
105 | *cp2++ = *cp++; | |
106 | *cp2 = 0; | |
107 | if (doign && isign(field)) | |
108 | continue; | |
2d7a1b5d KS |
109 | /* |
110 | * If the field is "status," go compute and print the | |
111 | * real Status: field | |
112 | */ | |
113 | if (icequal(field, "status")) { | |
114 | if (dostat) { | |
115 | statusput(mailp, obuf, doign); | |
116 | dostat = 0; | |
117 | } | |
118 | continue; | |
119 | } | |
57079810 KS |
120 | } |
121 | writeit: | |
122 | fputs(line, obuf); | |
3184019b KS |
123 | if (ferror(obuf)) |
124 | return(-1); | |
125 | } | |
57079810 KS |
126 | if (ferror(obuf)) |
127 | return(-1); | |
128 | if (ishead && (mailp->m_flag & MSTATUS)) | |
129 | printf("failed to fix up status field\n"); | |
3184019b KS |
130 | return(lc); |
131 | } | |
132 | ||
a6b3a6c5 KS |
133 | /* |
134 | * Test if the passed line is a header line, RFC 733 style. | |
135 | */ | |
136 | headerp(line) | |
137 | register char *line; | |
138 | { | |
139 | register char *cp = line; | |
140 | ||
141 | while (*cp && !isspace(*cp) && *cp != ':') | |
142 | cp++; | |
143 | while (*cp && isspace(*cp)) | |
144 | cp++; | |
145 | return(*cp == ':'); | |
146 | } | |
147 | ||
57079810 KS |
148 | /* |
149 | * Output a reasonable looking status field. | |
b870137d | 150 | * But if "status" is ignored and doign, forget it. |
57079810 | 151 | */ |
b870137d | 152 | statusput(mp, obuf, doign) |
57079810 KS |
153 | register struct message *mp; |
154 | register FILE *obuf; | |
155 | { | |
156 | char statout[3]; | |
157 | ||
b870137d KS |
158 | if (doign && isign("status")) |
159 | return; | |
57079810 KS |
160 | if ((mp->m_flag & (MNEW|MREAD)) == MNEW) |
161 | return; | |
162 | if (mp->m_flag & MREAD) | |
163 | strcpy(statout, "R"); | |
164 | else | |
165 | strcpy(statout, ""); | |
166 | if ((mp->m_flag & MNEW) == 0) | |
167 | strcat(statout, "O"); | |
168 | fprintf(obuf, "Status: %s\n", statout); | |
169 | } | |
170 | ||
171 | ||
3184019b KS |
172 | /* |
173 | * Interface between the argument list and the mail1 routine | |
174 | * which does all the dirty work. | |
175 | */ | |
176 | ||
177 | mail(people) | |
178 | char **people; | |
179 | { | |
180 | register char *cp2; | |
181 | register int s; | |
182 | char *buf, **ap; | |
183 | struct header head; | |
184 | ||
185 | for (s = 0, ap = people; *ap != (char *) -1; ap++) | |
186 | s += strlen(*ap) + 1; | |
187 | buf = salloc(s+1); | |
188 | cp2 = buf; | |
189 | for (ap = people; *ap != (char *) -1; ap++) { | |
190 | cp2 = copy(*ap, cp2); | |
191 | *cp2++ = ' '; | |
192 | } | |
193 | if (cp2 != buf) | |
194 | cp2--; | |
195 | *cp2 = '\0'; | |
196 | head.h_to = buf; | |
197 | head.h_subject = NOSTR; | |
198 | head.h_cc = NOSTR; | |
199 | head.h_bcc = NOSTR; | |
200 | head.h_seq = 0; | |
201 | mail1(&head); | |
202 | return(0); | |
203 | } | |
204 | ||
205 | ||
206 | /* | |
207 | * Send mail to a bunch of user names. The interface is through | |
208 | * the mail routine below. | |
209 | */ | |
210 | ||
211 | sendmail(str) | |
212 | char *str; | |
213 | { | |
214 | register char **ap; | |
215 | char *bufp; | |
216 | register int t; | |
217 | struct header head; | |
218 | ||
219 | if (blankline(str)) | |
220 | head.h_to = NOSTR; | |
221 | else | |
222 | head.h_to = str; | |
223 | head.h_subject = NOSTR; | |
224 | head.h_cc = NOSTR; | |
225 | head.h_bcc = NOSTR; | |
226 | head.h_seq = 0; | |
227 | mail1(&head); | |
228 | return(0); | |
229 | } | |
230 | ||
231 | /* | |
232 | * Mail a message on standard input to the people indicated | |
233 | * in the passed header. (Internal interface). | |
234 | */ | |
235 | ||
236 | mail1(hp) | |
237 | struct header *hp; | |
238 | { | |
239 | register char *cp; | |
240 | int pid, i, s, p, gotcha; | |
6fa89d43 | 241 | char **namelist, *deliver; |
3184019b | 242 | struct name *to, *np; |
a1f76dd3 | 243 | struct stat sbuf; |
3184019b KS |
244 | FILE *mtf, *postage; |
245 | int remote = rflag != NOSTR || rmail; | |
246 | char **t; | |
247 | ||
248 | /* | |
249 | * Collect user's mail from standard input. | |
250 | * Get the result as mtf. | |
251 | */ | |
252 | ||
253 | pid = -1; | |
254 | if ((mtf = collect(hp)) == NULL) | |
255 | return(-1); | |
256 | hp->h_seq = 1; | |
257 | if (hp->h_subject == NOSTR) | |
258 | hp->h_subject = sflag; | |
259 | if (fsize(mtf) == 0 && hp->h_subject == NOSTR) { | |
260 | printf("No message !?!\n"); | |
261 | goto out; | |
262 | } | |
263 | if (intty && value("askcc") != NOSTR) | |
264 | grabh(hp, GCC); | |
265 | else if (intty) { | |
266 | printf("EOT\n"); | |
267 | flush(); | |
268 | } | |
269 | ||
270 | /* | |
271 | * Now, take the user names from the combined | |
272 | * to and cc lists and do all the alias | |
273 | * processing. | |
274 | */ | |
275 | ||
276 | senderr = 0; | |
277 | to = usermap(cat(extract(hp->h_bcc, GBCC), | |
278 | cat(extract(hp->h_to, GTO), extract(hp->h_cc, GCC)))); | |
279 | if (to == NIL) { | |
280 | printf("No recipients specified\n"); | |
281 | goto topdog; | |
282 | } | |
283 | ||
284 | /* | |
285 | * Look through the recipient list for names with /'s | |
286 | * in them which we write to as files directly. | |
287 | */ | |
288 | ||
289 | to = outof(to, mtf, hp); | |
290 | rewind(mtf); | |
291 | to = verify(to); | |
292 | if (senderr && !remote) { | |
293 | topdog: | |
294 | ||
295 | if (fsize(mtf) != 0) { | |
296 | remove(deadletter); | |
297 | exwrite(deadletter, mtf, 1); | |
298 | rewind(mtf); | |
299 | } | |
300 | } | |
301 | for (gotcha = 0, np = to; np != NIL; np = np->n_flink) | |
302 | if ((np->n_type & GDEL) == 0) { | |
303 | gotcha++; | |
304 | break; | |
305 | } | |
306 | if (!gotcha) | |
307 | goto out; | |
308 | to = elide(to); | |
309 | mechk(to); | |
310 | if (count(to) > 1) | |
311 | hp->h_seq++; | |
312 | if (hp->h_seq > 0 && !remote) { | |
313 | fixhead(hp, to); | |
314 | if (fsize(mtf) == 0) | |
315 | printf("Null message body; hope that's ok\n"); | |
316 | if ((mtf = infix(hp, mtf)) == NULL) { | |
317 | fprintf(stderr, ". . . message lost, sorry.\n"); | |
318 | return(-1); | |
319 | } | |
320 | } | |
321 | namelist = unpack(to); | |
322 | if (debug) { | |
323 | printf("Recipients of message:\n"); | |
324 | for (t = namelist; *t != NOSTR; t++) | |
325 | printf(" \"%s\"", *t); | |
326 | printf("\n"); | |
327 | fflush(stdout); | |
328 | return; | |
329 | } | |
330 | if ((cp = value("record")) != NOSTR) | |
331 | savemail(expand(cp), hp, mtf); | |
332 | ||
333 | /* | |
334 | * Wait, to absorb a potential zombie, then | |
335 | * fork, set up the temporary mail file as standard | |
336 | * input for "mail" and exec with the user list we generated | |
337 | * far above. Return the process id to caller in case he | |
338 | * wants to await the completion of mail. | |
339 | */ | |
340 | ||
341 | #ifdef VMUNIX | |
69a45597 CS |
342 | #ifdef pdp11 |
343 | while (wait2(&s, WNOHANG) > 0) | |
344 | #endif | |
338c4a5d | 345 | #if defined(vax) || defined(sun) |
3184019b | 346 | while (wait3(&s, WNOHANG, 0) > 0) |
69a45597 | 347 | #endif |
3184019b KS |
348 | ; |
349 | #else | |
350 | wait(&s); | |
351 | #endif | |
352 | rewind(mtf); | |
353 | pid = fork(); | |
354 | if (pid == -1) { | |
355 | perror("fork"); | |
356 | remove(deadletter); | |
357 | exwrite(deadletter, mtf, 1); | |
358 | goto out; | |
359 | } | |
360 | if (pid == 0) { | |
a302be8d | 361 | sigchild(); |
3184019b KS |
362 | #ifdef SIGTSTP |
363 | if (remote == 0) { | |
67f02f55 KS |
364 | sigset(SIGTSTP, SIG_IGN); |
365 | sigset(SIGTTIN, SIG_IGN); | |
366 | sigset(SIGTTOU, SIG_IGN); | |
3184019b KS |
367 | } |
368 | #endif | |
369 | for (i = SIGHUP; i <= SIGQUIT; i++) | |
67f02f55 | 370 | sigset(i, SIG_IGN); |
a1f76dd3 CL |
371 | if (!stat(POSTAGE, &sbuf)) |
372 | if ((postage = fopen(POSTAGE, "a")) != NULL) { | |
373 | fprintf(postage, "%s %d %d\n", myname, | |
374 | count(to), fsize(mtf)); | |
375 | fclose(postage); | |
376 | } | |
3184019b KS |
377 | s = fileno(mtf); |
378 | for (i = 3; i < 15; i++) | |
379 | if (i != s) | |
380 | close(i); | |
381 | close(0); | |
382 | dup(s); | |
383 | close(s); | |
384 | #ifdef CC | |
385 | submit(getpid()); | |
386 | #endif CC | |
ac57be53 | 387 | #ifdef SENDMAIL |
6fa89d43 | 388 | if ((deliver = value("sendmail")) == NOSTR) |
ac57be53 | 389 | deliver = SENDMAIL; |
6fa89d43 | 390 | execv(deliver, namelist); |
ac57be53 | 391 | #endif SENDMAIL |
3184019b KS |
392 | execv(MAIL, namelist); |
393 | perror(MAIL); | |
394 | exit(1); | |
395 | } | |
396 | ||
397 | out: | |
a1f76dd3 | 398 | if (remote || (value("verbose") != NOSTR)) { |
3184019b KS |
399 | while ((p = wait(&s)) != pid && p != -1) |
400 | ; | |
401 | if (s != 0) | |
402 | senderr++; | |
403 | pid = 0; | |
404 | } | |
405 | fclose(mtf); | |
406 | return(pid); | |
407 | } | |
408 | ||
409 | /* | |
410 | * Fix the header by glopping all of the expanded names from | |
411 | * the distribution list into the appropriate fields. | |
412 | * If there are any ARPA net recipients in the message, | |
413 | * we must insert commas, alas. | |
414 | */ | |
415 | ||
416 | fixhead(hp, tolist) | |
417 | struct header *hp; | |
418 | struct name *tolist; | |
419 | { | |
420 | register struct name *nlist; | |
421 | register int f; | |
422 | register struct name *np; | |
423 | ||
424 | for (f = 0, np = tolist; np != NIL; np = np->n_flink) | |
425 | if (any('@', np->n_name)) { | |
426 | f |= GCOMMA; | |
427 | break; | |
428 | } | |
429 | ||
430 | if (debug && f & GCOMMA) | |
431 | fprintf(stderr, "Should be inserting commas in recip lists\n"); | |
432 | hp->h_to = detract(tolist, GTO|f); | |
433 | hp->h_cc = detract(tolist, GCC|f); | |
434 | } | |
435 | ||
436 | /* | |
437 | * Prepend a header in front of the collected stuff | |
438 | * and return the new file. | |
439 | */ | |
440 | ||
441 | FILE * | |
442 | infix(hp, fi) | |
443 | struct header *hp; | |
444 | FILE *fi; | |
445 | { | |
446 | extern char tempMail[]; | |
447 | register FILE *nfo, *nfi; | |
448 | register int c; | |
449 | ||
1c5ad8c6 | 450 | rewind(fi); |
3184019b KS |
451 | if ((nfo = fopen(tempMail, "w")) == NULL) { |
452 | perror(tempMail); | |
453 | return(fi); | |
454 | } | |
455 | if ((nfi = fopen(tempMail, "r")) == NULL) { | |
456 | perror(tempMail); | |
457 | fclose(nfo); | |
458 | return(fi); | |
459 | } | |
460 | remove(tempMail); | |
461 | puthead(hp, nfo, GTO|GSUBJECT|GCC|GNL); | |
3184019b KS |
462 | c = getc(fi); |
463 | while (c != EOF) { | |
464 | putc(c, nfo); | |
465 | c = getc(fi); | |
466 | } | |
467 | if (ferror(fi)) { | |
468 | perror("read"); | |
3184019b KS |
469 | return(fi); |
470 | } | |
471 | fflush(nfo); | |
472 | if (ferror(nfo)) { | |
473 | perror(tempMail); | |
474 | fclose(nfo); | |
475 | fclose(nfi); | |
476 | return(fi); | |
477 | } | |
478 | fclose(nfo); | |
479 | fclose(fi); | |
480 | rewind(nfi); | |
481 | return(nfi); | |
482 | } | |
483 | ||
484 | /* | |
485 | * Dump the to, subject, cc header on the | |
486 | * passed file buffer. | |
487 | */ | |
488 | ||
489 | puthead(hp, fo, w) | |
490 | struct header *hp; | |
491 | FILE *fo; | |
492 | { | |
493 | register int gotcha; | |
494 | ||
495 | gotcha = 0; | |
496 | if (hp->h_to != NOSTR && w & GTO) | |
a1f76dd3 | 497 | fmt("To: ", hp->h_to, fo), gotcha++; |
3184019b KS |
498 | if (hp->h_subject != NOSTR && w & GSUBJECT) |
499 | fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++; | |
500 | if (hp->h_cc != NOSTR && w & GCC) | |
a1f76dd3 | 501 | fmt("Cc: ", hp->h_cc, fo), gotcha++; |
3184019b | 502 | if (hp->h_bcc != NOSTR && w & GBCC) |
a1f76dd3 | 503 | fmt("Bcc: ", hp->h_bcc, fo), gotcha++; |
3184019b KS |
504 | if (gotcha && w & GNL) |
505 | putc('\n', fo); | |
506 | return(0); | |
507 | } | |
508 | ||
509 | /* | |
510 | * Format the given text to not exceed 72 characters. | |
511 | */ | |
512 | ||
a1f76dd3 CL |
513 | fmt(str, txt, fo) |
514 | register char *str, *txt; | |
3184019b KS |
515 | register FILE *fo; |
516 | { | |
517 | register int col; | |
a1f76dd3 CL |
518 | register char *bg, *bl, *pt, ch; |
519 | ||
520 | col = strlen(str); | |
521 | if (col) | |
522 | fprintf(fo, "%s", str); | |
523 | pt = bg = txt; | |
524 | bl = 0; | |
525 | while (*bg) { | |
526 | pt++; | |
527 | if (++col >72) { | |
528 | if (!bl) { | |
529 | bl = bg; | |
530 | while (*bl && !isspace(*bl)) | |
531 | bl++; | |
532 | } | |
533 | if (!*bl) | |
534 | goto finish; | |
535 | ch = *bl; | |
536 | *bl = '\0'; | |
537 | fprintf(fo, "%s\n ", bg); | |
3184019b | 538 | col = 4; |
a1f76dd3 CL |
539 | *bl = ch; |
540 | pt = bg = ++bl; | |
541 | bl = 0; | |
542 | } | |
543 | if (!*pt) { | |
544 | finish: | |
545 | fprintf(fo, "%s\n", bg); | |
546 | return; | |
3184019b | 547 | } |
a1f76dd3 CL |
548 | if (isspace(*pt)) |
549 | bl = pt; | |
3184019b | 550 | } |
3184019b KS |
551 | } |
552 | ||
553 | /* | |
554 | * Save the outgoing mail on the passed file. | |
555 | */ | |
556 | ||
557 | savemail(name, hp, fi) | |
558 | char name[]; | |
559 | struct header *hp; | |
560 | FILE *fi; | |
561 | { | |
562 | register FILE *fo; | |
563 | register int c; | |
564 | long now; | |
565 | char *n; | |
566 | ||
567 | if ((fo = fopen(name, "a")) == NULL) { | |
568 | perror(name); | |
569 | return(-1); | |
570 | } | |
571 | time(&now); | |
572 | n = rflag; | |
573 | if (n == NOSTR) | |
574 | n = myname; | |
575 | fprintf(fo, "From %s %s", n, ctime(&now)); | |
576 | rewind(fi); | |
577 | for (c = getc(fi); c != EOF; c = getc(fi)) | |
578 | putc(c, fo); | |
579 | fprintf(fo, "\n"); | |
580 | fflush(fo); | |
581 | if (ferror(fo)) | |
582 | perror(name); | |
583 | fclose(fo); | |
584 | return(0); | |
585 | } |