11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*4322Seric static char SccsId[] = "@(#)collect.c 3.21 09/07/81"; 51392Seric 61392Seric /* 72969Seric ** COLLECT -- read & parse message header & make temp file. 81392Seric ** 91392Seric ** Creates a temporary file name and copies the standard 101392Seric ** input to that file. While it is doing it, it looks for 111392Seric ** "From:" and "Sender:" fields to use as the from-person 121392Seric ** (but only if the -a flag is specified). It prefers to 131392Seric ** to use the "Sender:" field. 141392Seric ** 151392Seric ** MIT seems to like to produce "Sent-By:" fields instead 161392Seric ** of "Sender:" fields. We used to catch this, but it turns 171392Seric ** out that the "Sent-By:" field doesn't always correspond 181392Seric ** to someone real ("___057", for instance), as required by 191392Seric ** the protocol. So we limp by..... 201392Seric ** 211392Seric ** Parameters: 221875Seric ** none 231392Seric ** 241392Seric ** Returns: 254162Seric ** none. 261392Seric ** 271392Seric ** Side Effects: 281392Seric ** Temp file is created and filled. 294162Seric ** The from person may be set. 301392Seric */ 311392Seric 321624Seric long MsgSize; /* size of message in bytes */ 334182Seric FILE *TempFile; /* the tempfile (after creation) */ 341397Seric 352969Seric collect() 361392Seric { 371392Seric register FILE *tf; 381392Seric char buf[MAXFIELD+1]; 391392Seric register char *p; 402900Seric char *xfrom; 412900Seric extern char *hvalue(); 424083Seric extern char *mktemp(); 431392Seric 441392Seric /* 451392Seric ** Create the temp file name and create the file. 461392Seric */ 471392Seric 484083Seric (void) mktemp(InFileName); 494083Seric (void) close(creat(InFileName, 0600)); 501392Seric if ((tf = fopen(InFileName, "w")) == NULL) 511392Seric { 521392Seric syserr("Cannot create %s", InFileName); 534162Seric return; 541392Seric } 551392Seric 564316Seric /* 57*4322Seric ** Tell ARPANET to go ahead. 58*4322Seric */ 59*4322Seric 60*4322Seric if (ArpaMode == ARPA_MAIL) 61*4322Seric { 62*4322Seric extern char Arpa_Enter[]; 63*4322Seric 64*4322Seric message(Arpa_Enter, "Enter mail, end with \".\" on a line by itself"); 65*4322Seric } 66*4322Seric 67*4322Seric /* 684316Seric ** Try to read a UNIX-style From line 694316Seric */ 704316Seric 712900Seric if (fgets(buf, sizeof buf, stdin) == NULL) 724162Seric return; 734321Seric # ifndef NOTUNIX 74*4322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 752900Seric { 762900Seric eatfrom(buf); 774083Seric (void) fgets(buf, sizeof buf, stdin); 782900Seric } 794321Seric # endif NOTUNIX 802900Seric 811392Seric /* 821392Seric ** Copy stdin to temp file & do message editting. 831392Seric ** To keep certain mailers from getting confused, 841392Seric ** and to keep the output clean, lines that look 851392Seric ** like UNIX "From" lines are deleted in the header, 861392Seric ** and prepended with ">" in the body. 871392Seric */ 881392Seric 892900Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin)) 901392Seric { 914316Seric register char c; 924316Seric extern bool isheader(); 934316Seric 942900Seric /* see if the header is over */ 952900Seric if (!isheader(buf)) 962900Seric break; 972900Seric 982900Seric /* get the rest of this field */ 992900Seric while ((c = getc(stdin)) == ' ' || c == '\t') 1001392Seric { 1012900Seric p = &buf[strlen(buf)]; 1022900Seric *p++ = c; 1032900Seric if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 1042900Seric break; 1051392Seric } 1064083Seric if (!feof(stdin)) 1074083Seric (void) ungetc(c, stdin); 1081392Seric 1092900Seric MsgSize += strlen(buf); 1101392Seric 1112900Seric /* 1122900Seric ** Snarf header away. 1132900Seric */ 1142900Seric 1153391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1163058Seric break; 1172900Seric } 1181392Seric 1192900Seric # ifdef DEBUG 1202900Seric if (Debug) 1212900Seric printf("EOH\n"); 1222900Seric # endif DEBUG 1232900Seric 1242900Seric /* throw away a blank line */ 1252900Seric if (buf[0] == '\n') 1264083Seric (void) fgets(buf, sizeof buf, stdin); 1272900Seric 1282900Seric /* 1292900Seric ** Collect the body of the message. 1302900Seric */ 1312900Seric 1322900Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 1332900Seric { 1344156Seric register int i; 1354156Seric 1362900Seric /* check for end-of-message */ 1372900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1382900Seric break; 1392900Seric 1404321Seric # ifndef NOTUNIX 1412900Seric /* Hide UNIX-like From lines */ 1422900Seric if (strncmp(buf, "From ", 5) == 0) 1431392Seric { 1442900Seric fputs(">", tf); 1452900Seric MsgSize++; 1461392Seric } 1474321Seric # endif NOTUNIX 1484156Seric 1494156Seric /* 1504156Seric ** Figure message length, output the line to the temp 1514156Seric ** file, and insert a newline if missing. 1524156Seric */ 1534156Seric 1544156Seric i = strlen(buf); 1554156Seric MsgSize += i; 1561392Seric fputs(buf, tf); 1574156Seric if (buf[i - 1] != '\n') 1584156Seric fputs("\n", tf); 1591392Seric if (ferror(tf)) 1601392Seric { 1611439Seric if (errno == ENOSPC) 1621439Seric { 1634083Seric (void) freopen(InFileName, "w", tf); 1641439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1651439Seric syserr("Out of disk space for temp file"); 1661439Seric } 1671439Seric else 1681439Seric syserr("Cannot write %s", InFileName); 1694083Seric (void) freopen("/dev/null", "w", tf); 1701392Seric } 1711392Seric } 1724083Seric (void) fclose(tf); 1732900Seric 1742900Seric /* 1752900Seric ** Find out some information from the headers. 1763386Seric ** Examples are who is the from person & the date. 1772900Seric */ 1782900Seric 1792900Seric /* from person */ 1802900Seric xfrom = hvalue("sender"); 1812900Seric if (xfrom == NULL) 1824210Seric xfrom = hvalue("original-from"); 1834162Seric if (ArpaMode != ARPA_NONE) 1844316Seric setfrom(xfrom, (char *) NULL); 1852900Seric 1863390Seric /* full name of from person */ 1873390Seric p = hvalue("full-name"); 1883390Seric if (p != NULL) 1893390Seric define('x', p); 1904210Seric else 1914210Seric { 1924210Seric register char *q; 1933390Seric 1944210Seric /* 1954210Seric ** Try to extract the full name from a general From: 1964210Seric ** field. We take anything which is a comment as a 1974210Seric ** first choice. Failing in that, we see if there is 1984210Seric ** a "machine readable" name (in <angle brackets>); if 1994210Seric ** so we take anything preceeding that clause. 2004210Seric ** 2014210Seric ** If we blow it here it's not all that serious. 2024210Seric */ 2034210Seric 2044210Seric p = hvalue("original-from"); 2054210Seric q = index(p, '('); 2064210Seric if (q != NULL) 2074210Seric { 2084210Seric int parenlev = 0; 2094210Seric 2104210Seric for (p = q; *p != '\0'; p++) 2114210Seric { 2124210Seric if (*p == '(') 2134210Seric parenlev++; 2144210Seric else if (*p == ')' && --parenlev <= 0) 2154210Seric break; 2164210Seric } 2174210Seric if (*p == ')') 2184210Seric { 2194210Seric *p = '\0'; 2204210Seric if (*++q != '\0') 2214210Seric define('x', newstr(q)); 2224210Seric *p = ')'; 2234210Seric } 2244210Seric } 2254210Seric else if ((q = index(p, '<')) != NULL) 2264210Seric { 2274210Seric char savec; 2284210Seric 2294210Seric while (*--q == ' ') 2304210Seric continue; 2314210Seric while (isspace(*p)) 2324210Seric p++; 2334210Seric savec = *++q; 2344210Seric *q = '\0'; 2354210Seric if (*p != '\0') 2364210Seric define('x', newstr(p)); 2374210Seric *q = savec; 2384210Seric } 2394210Seric } 2404210Seric 2412900Seric /* date message originated */ 2424149Seric p = hvalue("posted-date"); 2434149Seric if (p == NULL) 2444149Seric p = hvalue("date"); 2452900Seric if (p != NULL) 2462900Seric { 2473386Seric define('a', p); 2483386Seric /* we don't have a good way to do canonical conversion .... 2493386Seric define('d', newstr(arpatounix(p))); 2503386Seric .... so we will ignore the problem for the time being */ 2512900Seric } 2522900Seric 2534182Seric if ((TempFile = fopen(InFileName, "r")) == NULL) 2541392Seric syserr("Cannot reopen %s", InFileName); 2552900Seric 2562900Seric # ifdef DEBUG 2572900Seric if (Debug) 2582900Seric { 2594316Seric HDR *h; 2604316Seric extern char *capitalize(); 2614316Seric 2622900Seric printf("----- collected header -----\n"); 2632900Seric for (h = Header; h != NULL; h = h->h_link) 2642900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 2652900Seric printf("----------------------------\n"); 2662900Seric } 2672900Seric # endif DEBUG 2684162Seric return; 2691392Seric } 2701392Seric /* 2712900Seric ** EATFROM -- chew up a UNIX style from line and process 2722900Seric ** 2732900Seric ** This does indeed make some assumptions about the format 2742900Seric ** of UNIX messages. 2752900Seric ** 2762900Seric ** Parameters: 2772900Seric ** fm -- the from line. 2782900Seric ** 2792900Seric ** Returns: 2802900Seric ** none. 2812900Seric ** 2822900Seric ** Side Effects: 2832900Seric ** extracts what information it can from the header, 2843386Seric ** such as the date. 2852900Seric */ 2862900Seric 2874321Seric # ifndef NOTUNIX 2884321Seric 2894203Seric char *DowList[] = 2904203Seric { 2914203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 2924203Seric }; 2934203Seric 2942900Seric char *MonthList[] = 2952900Seric { 2962900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2972900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 2982900Seric NULL 2992900Seric }; 3002900Seric 3012900Seric eatfrom(fm) 3022900Seric char *fm; 3032900Seric { 3042900Seric register char *p; 3052900Seric register char **dt; 3062900Seric 3074203Seric # ifdef DEBUG 3084203Seric if (Debug > 1) 3094203Seric printf("eatfrom(%s)\n", fm); 3104203Seric # endif DEBUG 3114203Seric 3122900Seric /* find the date part */ 3132900Seric p = fm; 3142900Seric while (*p != '\0') 3152900Seric { 3162900Seric /* skip a word */ 3172900Seric while (*p != '\0' && *p != ' ') 3182900Seric *p++; 3192900Seric while (*p == ' ') 3202900Seric *p++; 3212900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3222900Seric continue; 3232900Seric 3242900Seric /* we have a possible date */ 3254203Seric for (dt = DowList; *dt != NULL; dt++) 3262900Seric if (strncmp(*dt, p, 3) == 0) 3272900Seric break; 3284203Seric if (*dt == NULL) 3294203Seric continue; 3302900Seric 3314203Seric for (dt = MonthList; *dt != NULL; dt++) 3324203Seric if (strncmp(*dt, &p[4], 3) == 0) 3334203Seric break; 3342900Seric if (*dt != NULL) 3352900Seric break; 3362900Seric } 3372900Seric 3382900Seric if (*p != NULL) 3392900Seric { 3403386Seric char *q; 3413386Seric 3422900Seric /* we have found a date */ 3433386Seric q = xalloc(25); 3443386Seric strncpy(q, p, 25); 3453386Seric q[24] = '\0'; 3463386Seric define('d', q); 3472900Seric } 3482900Seric } 3494321Seric 3504321Seric # endif NOTUNIX 351