11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*4453Seric static char SccsId[] = "@(#)collect.c 3.23 10/02/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 /* 574322Seric ** Tell ARPANET to go ahead. 584322Seric */ 594322Seric 604322Seric if (ArpaMode == ARPA_MAIL) 614322Seric { 624322Seric extern char Arpa_Enter[]; 634322Seric 644322Seric message(Arpa_Enter, "Enter mail, end with \".\" on a line by itself"); 654322Seric } 664322Seric 674322Seric /* 684316Seric ** Try to read a UNIX-style From line 694316Seric */ 704316Seric 712900Seric if (fgets(buf, sizeof buf, stdin) == NULL) 724162Seric return; 734321Seric # ifndef NOTUNIX 744322Seric 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); 165*4453Seric syserr("collect: Out of disk space for temp file"); 1661439Seric } 1671439Seric else 168*4453Seric syserr("collect: 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) 1824371Seric xfrom = OrigFrom; 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"); 2054371Seric if (p == NULL) 2064371Seric p = OrigFrom; 2074210Seric q = index(p, '('); 2084210Seric if (q != NULL) 2094210Seric { 2104210Seric int parenlev = 0; 2114210Seric 2124210Seric for (p = q; *p != '\0'; p++) 2134210Seric { 2144210Seric if (*p == '(') 2154210Seric parenlev++; 2164210Seric else if (*p == ')' && --parenlev <= 0) 2174210Seric break; 2184210Seric } 2194210Seric if (*p == ')') 2204210Seric { 2214210Seric *p = '\0'; 2224210Seric if (*++q != '\0') 2234210Seric define('x', newstr(q)); 2244210Seric *p = ')'; 2254210Seric } 2264210Seric } 2274210Seric else if ((q = index(p, '<')) != NULL) 2284210Seric { 2294210Seric char savec; 2304210Seric 2314210Seric while (*--q == ' ') 2324210Seric continue; 2334210Seric while (isspace(*p)) 2344210Seric p++; 2354210Seric savec = *++q; 2364210Seric *q = '\0'; 2374210Seric if (*p != '\0') 2384210Seric define('x', newstr(p)); 2394210Seric *q = savec; 2404210Seric } 2414210Seric } 2424210Seric 2432900Seric /* date message originated */ 2444149Seric p = hvalue("posted-date"); 2454149Seric if (p == NULL) 2464149Seric p = hvalue("date"); 2472900Seric if (p != NULL) 2482900Seric { 2493386Seric define('a', p); 2503386Seric /* we don't have a good way to do canonical conversion .... 2513386Seric define('d', newstr(arpatounix(p))); 2523386Seric .... so we will ignore the problem for the time being */ 2532900Seric } 2542900Seric 2554182Seric if ((TempFile = fopen(InFileName, "r")) == NULL) 2561392Seric syserr("Cannot reopen %s", InFileName); 2572900Seric 2582900Seric # ifdef DEBUG 2592900Seric if (Debug) 2602900Seric { 2614316Seric HDR *h; 2624316Seric extern char *capitalize(); 2634316Seric 2642900Seric printf("----- collected header -----\n"); 2652900Seric for (h = Header; h != NULL; h = h->h_link) 2662900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 2672900Seric printf("----------------------------\n"); 2682900Seric } 2692900Seric # endif DEBUG 2704162Seric return; 2711392Seric } 2721392Seric /* 2732900Seric ** EATFROM -- chew up a UNIX style from line and process 2742900Seric ** 2752900Seric ** This does indeed make some assumptions about the format 2762900Seric ** of UNIX messages. 2772900Seric ** 2782900Seric ** Parameters: 2792900Seric ** fm -- the from line. 2802900Seric ** 2812900Seric ** Returns: 2822900Seric ** none. 2832900Seric ** 2842900Seric ** Side Effects: 2852900Seric ** extracts what information it can from the header, 2863386Seric ** such as the date. 2872900Seric */ 2882900Seric 2894321Seric # ifndef NOTUNIX 2904321Seric 2914203Seric char *DowList[] = 2924203Seric { 2934203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 2944203Seric }; 2954203Seric 2962900Seric char *MonthList[] = 2972900Seric { 2982900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2992900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3002900Seric NULL 3012900Seric }; 3022900Seric 3032900Seric eatfrom(fm) 3042900Seric char *fm; 3052900Seric { 3062900Seric register char *p; 3072900Seric register char **dt; 3082900Seric 3094203Seric # ifdef DEBUG 3104203Seric if (Debug > 1) 3114203Seric printf("eatfrom(%s)\n", fm); 3124203Seric # endif DEBUG 3134203Seric 3142900Seric /* find the date part */ 3152900Seric p = fm; 3162900Seric while (*p != '\0') 3172900Seric { 3182900Seric /* skip a word */ 3192900Seric while (*p != '\0' && *p != ' ') 3202900Seric *p++; 3212900Seric while (*p == ' ') 3222900Seric *p++; 3232900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3242900Seric continue; 3252900Seric 3262900Seric /* we have a possible date */ 3274203Seric for (dt = DowList; *dt != NULL; dt++) 3282900Seric if (strncmp(*dt, p, 3) == 0) 3292900Seric break; 3304203Seric if (*dt == NULL) 3314203Seric continue; 3322900Seric 3334203Seric for (dt = MonthList; *dt != NULL; dt++) 3344203Seric if (strncmp(*dt, &p[4], 3) == 0) 3354203Seric break; 3362900Seric if (*dt != NULL) 3372900Seric break; 3382900Seric } 3392900Seric 3402900Seric if (*p != NULL) 3412900Seric { 3423386Seric char *q; 3433386Seric 3442900Seric /* we have found a date */ 3453386Seric q = xalloc(25); 3463386Seric strncpy(q, p, 25); 3473386Seric q[24] = '\0'; 3483386Seric define('d', q); 3492900Seric } 3502900Seric } 3514321Seric 3524321Seric # endif NOTUNIX 353