1 /* uumm.c - routines to filter UUCP to MMDF mailboxes */
4 #include "../tws/tws.h"
6 #include "../mts/mts.h"
13 static struct header {
24 "Resent-Sender", HADDR,
25 "Resent-Reply-To", HADDR,
34 static char buffer[BUFSIZ],
44 * uucp2mmdf() - given a file descriptor to a uucp mailbox, filter
45 * its contents to the file descriptor for a mmdf mailbox. Returns
46 * non-zero on error (see mf.h for values)
48 * It is assumed that the caller will have made sure that the necessary
49 * locking has been performed on the output fd.
52 int uucp2mmdf (infd, outfd, nodelim)
62 if (fstat (infd, &st) == NOTOK || fstat (outfd, &st) == NOTOK)
64 if ((in = fdopen (infd, "r")) == NULL
65 || (out = fdopen (outfd, "w")) == NULL)
68 result = uumm (in, out, nodelim);
70 /* for STDIO - free up some fp:s */
71 fd = dup (fileno (in));
76 fd = dup (fileno (out));
86 int uumm (in, out, nodelim)
97 for (tmp_fd = NOTOK;;) {
98 if ((i = uucp_file (&tmp_fd, in, &tmp, nodelim)) == DONE)
103 if ((i = uucp_from (from, date, tmp)) != OK)
104 return uucp_die (i, tmp, in, out, nodelim);
105 if ((i = uucp_headers (from, date, tmp, out, nodelim)) != OK)
106 return uucp_die (i, tmp, in, out, nodelim);
107 if ((i = uucp_text (tmp, out, nodelim)) != OK)
108 return uucp_die (i, tmp, in, out, nodelim);
113 return (ferror (in) || ferror (out) ? MFERR : MFOK);
118 static int uucp_file (tmp_fd, in, tmp, nodelim)
125 char tmpfil[LINESIZ];
129 if (*tmp_fd != NOTOK)
132 if ((*tmp_fd = dup (fileno (in))) == NOTOK)
135 if ((*tmp = fdopen (*tmp_fd, "r")) == NULL) {
142 if (*tmp_fd == NOTOK && fgets (tmpbuf, sizeof tmpbuf, in) == NULL)
148 strcpy (tmpfil, "/tmp/uummXXXXXX");
149 unlink (mktemp (tmpfil));
150 if ((fd = creat (tmpfil, TMPMODE)) == NOTOK)
154 if ((fd = open (tmpfil, 2)) == NOTOK)
156 if ((out = fdopen (fd, "w")) == NULL) {
162 if ((*tmp_fd = dup (fd)) == NOTOK) {
166 if ((*tmp = fdopen (*tmp_fd, "r")) == NULL) {
174 for (done = FALSE;;) {
180 done = tmpbuf[0] == '\n';
183 if (fgets (tmpbuf, sizeof tmpbuf, in) == NULL)
194 /* We might want to attempt recovery here. Forget it. */
196 static int uucp_from (from, date, in)
206 if (fgets (buffer, sizeof buffer, in) == NULL || !isfrom (buffer))
208 if (buffer[strlen (buffer) - 1] == '\n')
209 buffer[strlen (buffer) - 1] = NULL;
210 if ((cp = index (buffer, ' ')) == NULL)
213 if ((cp = index (cp, ' ')) == NULL)
216 strcpy (fromwhom, pp);
217 while (isspace (*cp))
219 sprintf (date, "%.24s", cp);
222 if ((cp = index (cp + 1, 'r')) == NULL) {
223 if (index (fromwhom, '!') || index (fromwhom, '@'))
224 strcpy (from, fromwhom);
226 sprintf (from, "%s!%s", SystemName (), fromwhom);
229 if (strncmp (cp, "remote from ", 12) == 0) {
231 sprintf (from, "%s!%s", cp + 12, fromwhom);
236 if ((adrxp = seekadrx (from)) == NULL)
238 addr_convert (adrxp, from, TRUE);
239 while (seekadrx (NULL))
243 date_convert (date, date);
244 return (date[0] != NULL ? OK : MFROM);
249 static int uucp_headers (from, date, in, out, nodelim)
260 seen_from = seen_sender = seen_date = 0;
264 fprintf (out, "Munged: from %s to %s; %s\n",
265 SystemName (), LocalName (), dtimenow ());
268 switch (do_header (&seen_from, &seen_sender, &seen_date, in, out)) {
280 /* extra newline separates headers and body */
281 fprintf (out, "%sDate: %s\n%s: %s\n\n",
282 seen_date ? "UUCP-" : NULL, date,
283 seen_from ? (seen_sender ? "UUCP-Sender" : "Sender") : "From",
291 static int uucp_text (in, out, nodelim)
295 if (feof (in)) /* probably no body */
298 while (fgets (buffer, sizeof buffer, in) != NULL) {
299 if (!nodelim && isdlm2 (buffer))
313 static int do_header (seen_from, seen_sender, seen_date, in, out)
331 if ((i = mfgets (in, &bp)) != OK)
334 if ((cp = index (bp, ':')) == NULL) {
335 fprintf (out, "Illegal-Field: %s\n", bp);
340 for (hl = &headers[0]; hl -> h_name; hl++)
341 if (lequal (hl -> h_name, bp))
346 switch (hl -> h_type) {
348 if (lequal (hl -> h_name, "Date"))
350 for (pp = cp + 1; isspace (*pp); pp++)
352 date_convert (pp, line);
354 fprintf (out, "Illegal-Object: %s: %s -- %s\n",
355 hl -> h_name, pp, "illegal date construct");
357 fprintf (out, "%s: %s\n", hl -> h_name, line);
362 fprintf (out, "%s\n", bp);
367 if (hl -> h_type == HFROM)
375 margin = pos = strlen (hl -> h_name) + 2;
376 while (adrxp = seekadrx (pp)) {
377 addr_convert (adrxp, line, FALSE);
378 if (line[0] != NULL) {
380 fprintf (out, "%s: ", hl -> h_name);
382 fputs (", ", out), pos += 2;
383 if (pos + strlen (line) >= OWIDTH) {
384 fprintf (out, "\n%*s", margin, " ");
388 pos += strlen (line);
393 fprintf (out, "Illegal-Object: %s: %s -- %s\n",
394 hl -> h_name, adrxp -> text, adrxp -> err);
413 addr_convert (adrxp, to, notice)
425 static char path[LINESIZ] = "";
427 if (adrxp -> err || !adrxp -> mbox) {
432 if ((cp = rindex (adrxp -> mbox, '!')) != NULL)
433 sprintf (path, "%.*s!", cp - adrxp -> mbox, adrxp -> mbox);
437 sprintf (addr, "%s%s", path, adrxp -> mbox);
438 sprintf (uucp, "%s!", SystemName ());
439 uucplen = strlen (uucp);
440 if ((addrlen = strlen (addr) - uucplen - 1) >= 0)
441 for (cp = addr + addrlen; cp >= addr; cp--)
442 if (strncmp (cp, uucp, uucplen) == NULL) {
443 if (cp != addr && *(cp - 1) != '!')
445 strcpy (addr, cp + uucplen);
451 if (adrxp -> host == NULL) {
454 sprintf (uucp, "%s%%%s@%s", cp, UucpChan (), LocalName ());
456 if (wp = index (adrxp -> mbox, '!'))
457 sprintf (uucp, "%s@%.*s.%s",
458 wp + 1, wp - adrxp -> mbox, adrxp -> mbox, UucpChan ());
460 sprintf (uucp, "%s@%s.%s",
461 adrxp -> mbox, SystemName (), UucpChan ());
463 if (strcmp (adrxp -> mbox, cp))
464 sprintf (tmp, "\"%s\" <%s>", adrxp -> mbox, uucp);
469 if ((wp = rindex (adrxp -> mbox, '!')) == NULL)
470 sprintf (tmp, "%s@%s", adrxp -> mbox, adrxp -> host);
472 sprintf (uucp, "%%%s", UucpChan ());
473 uucplen = strlen (uucp);
474 cp = (lequal (LocalName (), adrxp -> host)
475 && (addrlen = strlen (addr) - uucplen) > 0)
476 ? addr + addrlen : NULL;
477 if (lequal (uucp, cp))
478 sprintf (tmp, "%s@%s", shrink (addr), adrxp -> host);
480 if (lequal (adrxp -> mbox, ++wp))
481 sprintf (tmp, "%s@%s", wp, adrxp -> host);
483 sprintf (tmp, "\"%s\" <%s@%s>", adrxp -> mbox,
493 static char *shrink (addr)
502 static char r1[BUFSIZ],
505 sprintf (r2, "%s!", SystemName ());
507 if ((j = strlen (addr) - i - 1) >= 0)
508 for (cp = &addr[j]; cp >= addr; cp--)
509 if (strncmp (cp, r2, i) == NULL) {
510 if (cp != addr && *(cp - 1) != '!')
512 strcpy (addr, cp + i);
516 if ((cp = rindex (addr, '!')) == NULL) {
517 sprintf (r1, "%s%s", r2, addr);
522 if ((pp = rindex (addr, '!')) == NULL) {
529 while ((pp = rindex (addr, '!')) != NULL) {
530 for (pp++, xp = addr; (wp = index (xp, '!')) != NULL;) {
532 if (strcmp (pp, xp)) {
541 sprintf (r2, "%s!%s", pp, r1);
549 if ((wp = index (r1, '!')) != NULL) {
553 if (strcmp (addr, r2)) {
554 sprintf (r2, "%s!%s", addr, r1);
564 date_convert (from, to)
568 static int zone = -1,
573 if (dparsetime (from)) /* might be OK as is */
576 if (isdigit (from[20])) {
578 if (tw = dtwstime ()) {
579 zone = tw -> tw_zone;
580 flags = tw -> tw_flags;
583 zone = 0, flags = TW_NULL;
585 sprintf (date, "%.3s, %.2s %.3s %.2s %.2s:%.2s:%.2s %s",
586 from + 0, from + 8, from + 4, from + 22, from + 11,
587 from + 14, from + 17, dtimezone (zone, flags));
590 sprintf (date, "%.3s, %.2s %.3s %.2s %.2s:%.2s:%.2s %s",
591 from + 0, from + 8, from + 4, from + 26, from + 11,
592 from + 14, from + 17, from + 20);
599 static int uucp_die (error, in1, in2, out, nodelim)
602 FILE * in1, *in2, *out;
616 fprintf (out, "\n%s", mmdlm2);
621 sprintf (date, "%.*s", sizeof date - 1, dtime (&clock));
622 fprintf (out, "%sFrom: %s <%s@%s>\nDate: %s\nSubject: %s %s\n\n",
623 mmdlm1, "UUCP to MMDF filter", getusr (), LocalName (), date,
624 "Bad UUCP mailbox - error in",
625 error == MFHDR ? "Header" : error == MFTXT ? "Body" : "Mailbox");
627 fprintf (out, "%s: %s\n%s\n--------\n",
628 "Error detected at line", buffer, "Message being processed");
630 while (fgets (buffer, sizeof buffer, in1) != NULL) {
638 fprintf (out, "--------\n%s\n--------\n%s",
639 "Remainder of unfiltered mailbox follows", tmpbuf);
640 while (fgets (buffer, sizeof buffer, in2) != NULL) {
647 fprintf (out, "--------\n%s", mmdlm2);