11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*9539Seric SCCSID(@(#)collect.c 3.56 12/05/82); 51392Seric 61392Seric /* 72969Seric ** COLLECT -- read & parse message header & make temp file. 81392Seric ** 91392Seric ** Creates a temporary file name and copies the standard 109371Seric ** input to that file. Leading UNIX-style "From" lines are 119371Seric ** stripped off (after important information is extracted). 121392Seric ** 131392Seric ** Parameters: 144710Seric ** sayok -- if set, give an ARPANET style message 154710Seric ** to say we are ready to collect input. 161392Seric ** 171392Seric ** Returns: 184162Seric ** none. 191392Seric ** 201392Seric ** Side Effects: 211392Seric ** Temp file is created and filled. 224162Seric ** The from person may be set. 231392Seric */ 241392Seric 254710Seric collect(sayok) 264710Seric bool sayok; 271392Seric { 281392Seric register FILE *tf; 297852Seric char buf[MAXFIELD+2]; 301392Seric register char *p; 312900Seric extern char *hvalue(); 321392Seric 331392Seric /* 341392Seric ** Create the temp file name and create the file. 351392Seric */ 361392Seric 377809Seric CurEnv->e_df = newstr(queuename(CurEnv, 'd')); 387809Seric if ((tf = dfopen(CurEnv->e_df, "w")) == NULL) 391392Seric { 407809Seric syserr("Cannot create %s", CurEnv->e_df); 415366Seric NoReturn = TRUE; 425366Seric finis(); 431392Seric } 449047Seric (void) chmod(CurEnv->e_df, FileMode); 451392Seric 464316Seric /* 474322Seric ** Tell ARPANET to go ahead. 484322Seric */ 494322Seric 504710Seric if (sayok) 514710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 524322Seric 534322Seric /* 544316Seric ** Try to read a UNIX-style From line 554316Seric */ 564316Seric 575975Seric if (fgets(buf, sizeof buf, InChannel) == NULL) 584162Seric return; 594557Seric fixcrlf(buf, FALSE); 604321Seric # ifndef NOTUNIX 614322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 622900Seric { 632900Seric eatfrom(buf); 645975Seric (void) fgets(buf, sizeof buf, InChannel); 654557Seric fixcrlf(buf, FALSE); 662900Seric } 674321Seric # endif NOTUNIX 682900Seric 691392Seric /* 705975Seric ** Copy InChannel to temp file & do message editing. 711392Seric ** To keep certain mailers from getting confused, 721392Seric ** and to keep the output clean, lines that look 731392Seric ** like UNIX "From" lines are deleted in the header, 741392Seric ** and prepended with ">" in the body. 751392Seric */ 761392Seric 779371Seric for (; !feof(InChannel); !feof(InChannel) && 789371Seric fgets(buf, MAXFIELD, InChannel) != NULL) 791392Seric { 804316Seric register char c; 814316Seric extern bool isheader(); 824316Seric 837681Seric /* if the line is too long, throw the rest away */ 847681Seric if (index(buf, '\n') == NULL) 857681Seric { 867681Seric while ((c = getc(InChannel)) != '\n') 877681Seric continue; 887681Seric /* give an error? */ 897681Seric } 907681Seric 917852Seric fixcrlf(buf, TRUE); 924557Seric 932900Seric /* see if the header is over */ 942900Seric if (!isheader(buf)) 952900Seric break; 962900Seric 972900Seric /* get the rest of this field */ 985975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 991392Seric { 1002900Seric p = &buf[strlen(buf)]; 1017852Seric *p++ = '\n'; 1022900Seric *p++ = c; 1037852Seric if (fgets(p, MAXFIELD - (p - buf), InChannel) == NULL) 1042900Seric break; 1057852Seric fixcrlf(p, TRUE); 1061392Seric } 1075975Seric if (!feof(InChannel)) 1085975Seric (void) ungetc(c, InChannel); 1091392Seric 1106901Seric CurEnv->e_msgsize += strlen(buf); 1111392Seric 1122900Seric /* 1132900Seric ** Snarf header away. 1142900Seric */ 1152900Seric 1163391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1173058Seric break; 1182900Seric } 1191392Seric 1202900Seric # ifdef DEBUG 1217673Seric if (tTd(30, 1)) 1222900Seric printf("EOH\n"); 1232900Seric # endif DEBUG 1242900Seric 1252900Seric /* throw away a blank line */ 1267852Seric if (buf[0] == '\0') 1274557Seric { 1287852Seric (void) fgets(buf, MAXFIELD, InChannel); 1297852Seric fixcrlf(buf, TRUE); 1304557Seric } 1312900Seric 1322900Seric /* 1332900Seric ** Collect the body of the message. 1342900Seric */ 1352900Seric 1369371Seric for (; !feof(InChannel); !feof(InChannel) && 1379371Seric fgets(buf, sizeof buf, InChannel) != NULL) 1382900Seric { 1394551Seric register char *bp = buf; 1404156Seric 1417852Seric fixcrlf(buf, TRUE); 1424557Seric 1432900Seric /* check for end-of-message */ 1442900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1452900Seric break; 1462900Seric 1474551Seric /* check for transparent dot */ 1489371Seric if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.') 1494551Seric bp++; 1504551Seric 1514321Seric # ifndef NOTUNIX 1522900Seric /* Hide UNIX-like From lines */ 1534551Seric if (strncmp(bp, "From ", 5) == 0) 1541392Seric { 1552900Seric fputs(">", tf); 1566901Seric CurEnv->e_msgsize++; 1571392Seric } 1584321Seric # endif NOTUNIX 1594156Seric 1604156Seric /* 1614156Seric ** Figure message length, output the line to the temp 1624156Seric ** file, and insert a newline if missing. 1634156Seric */ 1644156Seric 1659371Seric CurEnv->e_msgsize += strlen(bp) + 1; 1664551Seric fputs(bp, tf); 1677852Seric fputs("\n", tf); 1681392Seric if (ferror(tf)) 1691392Seric { 1701439Seric if (errno == ENOSPC) 1711439Seric { 1726986Seric (void) freopen(CurEnv->e_df, "w", tf); 1731439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1744557Seric usrerr("452 Out of disk space for temp file"); 1751439Seric } 1761439Seric else 1776986Seric syserr("collect: Cannot write %s", CurEnv->e_df); 1784083Seric (void) freopen("/dev/null", "w", tf); 1791392Seric } 1801392Seric } 1814083Seric (void) fclose(tf); 1822900Seric 1832900Seric /* 1842900Seric ** Find out some information from the headers. 1853386Seric ** Examples are who is the from person & the date. 1862900Seric */ 1872900Seric 1889371Seric eatheader(CurEnv); 1897673Seric 1907782Seric /* 1917782Seric ** Add an Apparently-To: line if we have no recipient lines. 1927782Seric */ 1934622Seric 1947367Seric if (hvalue("to") == NULL && hvalue("cc") == NULL && 1957367Seric hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 1967367Seric { 1977367Seric register ADDRESS *q; 1987367Seric 1997367Seric /* create an Apparently-To: field */ 2007367Seric /* that or reject the message.... */ 2017367Seric for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 2027367Seric { 2037389Seric if (q->q_alias != NULL) 2047389Seric continue; 2057367Seric # ifdef DEBUG 2067673Seric if (tTd(30, 3)) 2077367Seric printf("Adding Apparently-To: %s\n", q->q_paddr); 2087367Seric # endif DEBUG 2097367Seric addheader("apparently-to", q->q_paddr, CurEnv); 2107367Seric } 2117367Seric } 2127367Seric 213*9539Seric if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL) 2146986Seric syserr("Cannot reopen %s", CurEnv->e_df); 2152900Seric 2167673Seric /* 2177673Seric ** Log collection information. 2187673Seric */ 2197673Seric 2207673Seric # ifdef LOG 2217673Seric if (LogLevel > 1) 2227809Seric syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n", 2237809Seric CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 2247758Seric CurEnv->e_class); 2257673Seric # endif LOG 2264162Seric return; 2271392Seric } 2281392Seric /* 2292900Seric ** EATFROM -- chew up a UNIX style from line and process 2302900Seric ** 2312900Seric ** This does indeed make some assumptions about the format 2322900Seric ** of UNIX messages. 2332900Seric ** 2342900Seric ** Parameters: 2352900Seric ** fm -- the from line. 2362900Seric ** 2372900Seric ** Returns: 2382900Seric ** none. 2392900Seric ** 2402900Seric ** Side Effects: 2412900Seric ** extracts what information it can from the header, 2423386Seric ** such as the date. 2432900Seric */ 2442900Seric 2454321Seric # ifndef NOTUNIX 2464321Seric 2474203Seric char *DowList[] = 2484203Seric { 2494203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 2504203Seric }; 2514203Seric 2522900Seric char *MonthList[] = 2532900Seric { 2542900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 2552900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 2562900Seric NULL 2572900Seric }; 2582900Seric 2592900Seric eatfrom(fm) 2602900Seric char *fm; 2612900Seric { 2622900Seric register char *p; 2632900Seric register char **dt; 2642900Seric 2654203Seric # ifdef DEBUG 2667673Seric if (tTd(30, 2)) 2674203Seric printf("eatfrom(%s)\n", fm); 2684203Seric # endif DEBUG 2694203Seric 2702900Seric /* find the date part */ 2712900Seric p = fm; 2722900Seric while (*p != '\0') 2732900Seric { 2742900Seric /* skip a word */ 2752900Seric while (*p != '\0' && *p != ' ') 2762900Seric *p++; 2772900Seric while (*p == ' ') 2782900Seric *p++; 2792900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 2802900Seric continue; 2812900Seric 2822900Seric /* we have a possible date */ 2834203Seric for (dt = DowList; *dt != NULL; dt++) 2842900Seric if (strncmp(*dt, p, 3) == 0) 2852900Seric break; 2864203Seric if (*dt == NULL) 2874203Seric continue; 2882900Seric 2894203Seric for (dt = MonthList; *dt != NULL; dt++) 2904203Seric if (strncmp(*dt, &p[4], 3) == 0) 2914203Seric break; 2922900Seric if (*dt != NULL) 2932900Seric break; 2942900Seric } 2952900Seric 2962900Seric if (*p != NULL) 2972900Seric { 2983386Seric char *q; 2995366Seric extern char *arpadate(); 3003386Seric 3012900Seric /* we have found a date */ 3023386Seric q = xalloc(25); 3033386Seric strncpy(q, p, 25); 3043386Seric q[24] = '\0'; 3059371Seric define('d', q, CurEnv); 3065366Seric q = arpadate(q); 3079371Seric define('a', newstr(q), CurEnv); 3082900Seric } 3092900Seric } 3104321Seric 3114321Seric # endif NOTUNIX 312