11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*5033Seric static char SccsId[] = "@(#)collect.c 3.29 11/22/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: 224710Seric ** sayok -- if set, give an ARPANET style message 234710Seric ** to say we are ready to collect input. 241392Seric ** 251392Seric ** Returns: 264162Seric ** none. 271392Seric ** 281392Seric ** Side Effects: 291392Seric ** Temp file is created and filled. 304162Seric ** The from person may be set. 311392Seric */ 321392Seric 331624Seric long MsgSize; /* size of message in bytes */ 341397Seric 354710Seric collect(sayok) 364710Seric bool sayok; 371392Seric { 381392Seric register FILE *tf; 391392Seric char buf[MAXFIELD+1]; 401392Seric register char *p; 412900Seric char *xfrom; 422900Seric extern char *hvalue(); 434083Seric extern char *mktemp(); 444622Seric static char tempfname[40]; 454622Seric extern char *QueueDir; 461392Seric 471392Seric /* 481392Seric ** Create the temp file name and create the file. 491392Seric */ 501392Seric 514622Seric strcpy(tempfname, QueueDir); 524622Seric strcat(tempfname, "/dfaXXXXXX"); 534622Seric (void) mktemp(tempfname); 544622Seric (void) close(creat(tempfname, 0600)); 554622Seric if ((tf = fopen(tempfname, "w")) == NULL) 561392Seric { 574622Seric syserr("Cannot create %s", tempfname); 584162Seric return; 591392Seric } 604622Seric InFileName = tempfname; 611392Seric 624316Seric /* 634322Seric ** Tell ARPANET to go ahead. 644322Seric */ 654322Seric 664710Seric if (sayok) 674710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 684322Seric 694322Seric /* 704316Seric ** Try to read a UNIX-style From line 714316Seric */ 724316Seric 732900Seric if (fgets(buf, sizeof buf, stdin) == NULL) 744162Seric return; 754557Seric fixcrlf(buf, FALSE); 764321Seric # ifndef NOTUNIX 774322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 782900Seric { 792900Seric eatfrom(buf); 804083Seric (void) fgets(buf, sizeof buf, stdin); 814557Seric fixcrlf(buf, FALSE); 822900Seric } 834321Seric # endif NOTUNIX 842900Seric 851392Seric /* 861392Seric ** Copy stdin to temp file & do message editting. 871392Seric ** To keep certain mailers from getting confused, 881392Seric ** and to keep the output clean, lines that look 891392Seric ** like UNIX "From" lines are deleted in the header, 901392Seric ** and prepended with ">" in the body. 911392Seric */ 921392Seric 934622Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 941392Seric { 954316Seric register char c; 964316Seric extern bool isheader(); 974316Seric 984557Seric fixcrlf(buf, FALSE); 994557Seric 1002900Seric /* see if the header is over */ 1012900Seric if (!isheader(buf)) 1022900Seric break; 1032900Seric 1042900Seric /* get the rest of this field */ 1052900Seric while ((c = getc(stdin)) == ' ' || c == '\t') 1061392Seric { 1072900Seric p = &buf[strlen(buf)]; 1082900Seric *p++ = c; 1092900Seric if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 1102900Seric break; 1114557Seric fixcrlf(p, FALSE); 1121392Seric } 1134083Seric if (!feof(stdin)) 1144083Seric (void) ungetc(c, stdin); 1151392Seric 1162900Seric MsgSize += strlen(buf); 1171392Seric 1182900Seric /* 1192900Seric ** Snarf header away. 1202900Seric */ 1212900Seric 1223391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1233058Seric break; 1242900Seric } 1251392Seric 1262900Seric # ifdef DEBUG 1272900Seric if (Debug) 1282900Seric printf("EOH\n"); 1292900Seric # endif DEBUG 1302900Seric 1312900Seric /* throw away a blank line */ 1322900Seric if (buf[0] == '\n') 1334557Seric { 1344083Seric (void) fgets(buf, sizeof buf, stdin); 1354557Seric fixcrlf(buf, FALSE); 1364557Seric } 1372900Seric 1382900Seric /* 1392900Seric ** Collect the body of the message. 1402900Seric */ 1412900Seric 1422900Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 1432900Seric { 1444156Seric register int i; 1454551Seric register char *bp = buf; 1464156Seric 1474557Seric fixcrlf(buf, FALSE); 1484557Seric 1492900Seric /* check for end-of-message */ 1502900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1512900Seric break; 1522900Seric 1534551Seric /* check for transparent dot */ 1544551Seric if (Smtp && *bp == '.') 1554551Seric bp++; 1564551Seric 1574321Seric # ifndef NOTUNIX 1582900Seric /* Hide UNIX-like From lines */ 1594551Seric if (strncmp(bp, "From ", 5) == 0) 1601392Seric { 1612900Seric fputs(">", tf); 1622900Seric MsgSize++; 1631392Seric } 1644321Seric # endif NOTUNIX 1654156Seric 1664156Seric /* 1674156Seric ** Figure message length, output the line to the temp 1684156Seric ** file, and insert a newline if missing. 1694156Seric */ 1704156Seric 1714551Seric i = strlen(bp); 1724156Seric MsgSize += i; 1734551Seric fputs(bp, tf); 1744551Seric if (bp[i - 1] != '\n') 1754156Seric fputs("\n", tf); 1761392Seric if (ferror(tf)) 1771392Seric { 1781439Seric if (errno == ENOSPC) 1791439Seric { 1804083Seric (void) freopen(InFileName, "w", tf); 1811439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1824557Seric usrerr("452 Out of disk space for temp file"); 1831439Seric } 1841439Seric else 1854453Seric syserr("collect: Cannot write %s", InFileName); 1864083Seric (void) freopen("/dev/null", "w", tf); 1871392Seric } 1881392Seric } 1894083Seric (void) fclose(tf); 1902900Seric 1912900Seric /* 1922900Seric ** Find out some information from the headers. 1933386Seric ** Examples are who is the from person & the date. 1942900Seric */ 1952900Seric 196*5033Seric if (!QueueRun) 197*5033Seric { 198*5033Seric /* adjust total priority by message priority */ 199*5033Seric MsgPriority = MsgSize; 200*5033Seric p = hvalue("priority"); 201*5033Seric if (p != NULL) 202*5033Seric MsgPriority -= priencode(p) * WKPRIFACT; 203*5033Seric } 2044622Seric 2052900Seric /* from person */ 2062900Seric xfrom = hvalue("sender"); 2072900Seric if (xfrom == NULL) 2084371Seric xfrom = OrigFrom; 2094710Seric if (ArpaMode) 2104316Seric setfrom(xfrom, (char *) NULL); 2112900Seric 2123390Seric /* full name of from person */ 2133390Seric p = hvalue("full-name"); 2143390Seric if (p != NULL) 2153390Seric define('x', p); 2164210Seric else 2174210Seric { 2184210Seric register char *q; 2193390Seric 2204210Seric /* 2214210Seric ** Try to extract the full name from a general From: 2224210Seric ** field. We take anything which is a comment as a 2234210Seric ** first choice. Failing in that, we see if there is 2244210Seric ** a "machine readable" name (in <angle brackets>); if 2254210Seric ** so we take anything preceeding that clause. 2264210Seric ** 2274210Seric ** If we blow it here it's not all that serious. 2284210Seric */ 2294210Seric 2304210Seric p = hvalue("original-from"); 2314371Seric if (p == NULL) 2324371Seric p = OrigFrom; 2334210Seric q = index(p, '('); 2344210Seric if (q != NULL) 2354210Seric { 2364210Seric int parenlev = 0; 2374210Seric 2384210Seric for (p = q; *p != '\0'; p++) 2394210Seric { 2404210Seric if (*p == '(') 2414210Seric parenlev++; 2424210Seric else if (*p == ')' && --parenlev <= 0) 2434210Seric break; 2444210Seric } 2454210Seric if (*p == ')') 2464210Seric { 2474210Seric *p = '\0'; 2484210Seric if (*++q != '\0') 2494210Seric define('x', newstr(q)); 2504210Seric *p = ')'; 2514210Seric } 2524210Seric } 2534210Seric else if ((q = index(p, '<')) != NULL) 2544210Seric { 2554210Seric char savec; 2564210Seric 2574210Seric while (*--q == ' ') 2584210Seric continue; 2594210Seric while (isspace(*p)) 2604210Seric p++; 2614210Seric savec = *++q; 2624210Seric *q = '\0'; 2634210Seric if (*p != '\0') 2644210Seric define('x', newstr(p)); 2654210Seric *q = savec; 2664210Seric } 2674210Seric } 2684210Seric 2692900Seric /* date message originated */ 2704149Seric p = hvalue("posted-date"); 2714149Seric if (p == NULL) 2724149Seric p = hvalue("date"); 2732900Seric if (p != NULL) 2742900Seric { 2753386Seric define('a', p); 2763386Seric /* we don't have a good way to do canonical conversion .... 2773386Seric define('d', newstr(arpatounix(p))); 2783386Seric .... so we will ignore the problem for the time being */ 2792900Seric } 2802900Seric 2814182Seric if ((TempFile = fopen(InFileName, "r")) == NULL) 2821392Seric syserr("Cannot reopen %s", InFileName); 2832900Seric 2842900Seric # ifdef DEBUG 2852900Seric if (Debug) 2862900Seric { 2874316Seric HDR *h; 2884316Seric extern char *capitalize(); 2894316Seric 2902900Seric printf("----- collected header -----\n"); 2912900Seric for (h = Header; h != NULL; h = h->h_link) 2922900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 2932900Seric printf("----------------------------\n"); 2942900Seric } 2952900Seric # endif DEBUG 2964162Seric return; 2971392Seric } 2981392Seric /* 2992900Seric ** EATFROM -- chew up a UNIX style from line and process 3002900Seric ** 3012900Seric ** This does indeed make some assumptions about the format 3022900Seric ** of UNIX messages. 3032900Seric ** 3042900Seric ** Parameters: 3052900Seric ** fm -- the from line. 3062900Seric ** 3072900Seric ** Returns: 3082900Seric ** none. 3092900Seric ** 3102900Seric ** Side Effects: 3112900Seric ** extracts what information it can from the header, 3123386Seric ** such as the date. 3132900Seric */ 3142900Seric 3154321Seric # ifndef NOTUNIX 3164321Seric 3174203Seric char *DowList[] = 3184203Seric { 3194203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3204203Seric }; 3214203Seric 3222900Seric char *MonthList[] = 3232900Seric { 3242900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3252900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3262900Seric NULL 3272900Seric }; 3282900Seric 3292900Seric eatfrom(fm) 3302900Seric char *fm; 3312900Seric { 3322900Seric register char *p; 3332900Seric register char **dt; 3342900Seric 3354203Seric # ifdef DEBUG 3364203Seric if (Debug > 1) 3374203Seric printf("eatfrom(%s)\n", fm); 3384203Seric # endif DEBUG 3394203Seric 3402900Seric /* find the date part */ 3412900Seric p = fm; 3422900Seric while (*p != '\0') 3432900Seric { 3442900Seric /* skip a word */ 3452900Seric while (*p != '\0' && *p != ' ') 3462900Seric *p++; 3472900Seric while (*p == ' ') 3482900Seric *p++; 3492900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3502900Seric continue; 3512900Seric 3522900Seric /* we have a possible date */ 3534203Seric for (dt = DowList; *dt != NULL; dt++) 3542900Seric if (strncmp(*dt, p, 3) == 0) 3552900Seric break; 3564203Seric if (*dt == NULL) 3574203Seric continue; 3582900Seric 3594203Seric for (dt = MonthList; *dt != NULL; dt++) 3604203Seric if (strncmp(*dt, &p[4], 3) == 0) 3614203Seric break; 3622900Seric if (*dt != NULL) 3632900Seric break; 3642900Seric } 3652900Seric 3662900Seric if (*p != NULL) 3672900Seric { 3683386Seric char *q; 3693386Seric 3702900Seric /* we have found a date */ 3713386Seric q = xalloc(25); 3723386Seric strncpy(q, p, 25); 3733386Seric q[24] = '\0'; 3743386Seric define('d', q); 3752900Seric } 3762900Seric } 3774321Seric 3784321Seric # endif NOTUNIX 3794622Seric /* 3804622Seric ** PRIENCODE -- encode external priority names into internal values. 3814622Seric ** 3824622Seric ** Parameters: 3834622Seric ** p -- priority in ascii. 3844622Seric ** 3854622Seric ** Returns: 3864622Seric ** priority as a numeric level. 3874622Seric ** 3884622Seric ** Side Effects: 3894622Seric ** none. 3904622Seric */ 3914622Seric 3924622Seric struct prio 3934622Seric { 3944622Seric char *pri_name; /* external name of priority */ 3954622Seric int pri_val; /* internal value for same */ 3964622Seric }; 3974622Seric 3984622Seric static struct prio Prio[] = 3994622Seric { 4004633Seric "alert", PRI_ALERT, 4014633Seric "quick", PRI_QUICK, 4024633Seric "first-class", PRI_FIRSTCL, 4034622Seric "normal", PRI_NORMAL, 4044622Seric "second-class", PRI_SECONDCL, 4054622Seric "third-class", PRI_THIRDCL, 4064622Seric NULL, PRI_NORMAL, 4074622Seric }; 4084622Seric 4094622Seric priencode(p) 4104622Seric char *p; 4114622Seric { 4124622Seric register struct prio *pl; 4134633Seric extern bool sameword(); 4144622Seric 4154622Seric for (pl = Prio; pl->pri_name != NULL; pl++) 4164622Seric { 4174633Seric if (sameword(p, pl->pri_name)) 4184622Seric break; 4194622Seric } 4204622Seric return (pl->pri_val); 4214622Seric } 422