11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*5192Seric SCCSID(@(#)collect.c 3.31 12/06/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; 46*5192Seric extern char *macvalue(); 471392Seric 481392Seric /* 491392Seric ** Create the temp file name and create the file. 501392Seric */ 511392Seric 524622Seric strcpy(tempfname, QueueDir); 534622Seric strcat(tempfname, "/dfaXXXXXX"); 544622Seric (void) mktemp(tempfname); 554622Seric (void) close(creat(tempfname, 0600)); 564622Seric if ((tf = fopen(tempfname, "w")) == NULL) 571392Seric { 584622Seric syserr("Cannot create %s", tempfname); 594162Seric return; 601392Seric } 614622Seric InFileName = tempfname; 621392Seric 634316Seric /* 645185Seric ** Create the Mail-From line if we want to. 655185Seric */ 665185Seric 675185Seric if (macvalue('s') != NULL) 685185Seric { 695185Seric char xbuf[50]; 705185Seric 71*5192Seric (void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b", 725185Seric macvalue('r') == NULL ? "" : "$r host "); 735185Seric (void) expand(xbuf, buf, &buf[sizeof buf - 1]); 74*5192Seric (void) chompheader(buf, FALSE); 755185Seric } 765185Seric 775185Seric /* 784322Seric ** Tell ARPANET to go ahead. 794322Seric */ 804322Seric 814710Seric if (sayok) 824710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 834322Seric 844322Seric /* 854316Seric ** Try to read a UNIX-style From line 864316Seric */ 874316Seric 882900Seric if (fgets(buf, sizeof buf, stdin) == NULL) 894162Seric return; 904557Seric fixcrlf(buf, FALSE); 914321Seric # ifndef NOTUNIX 924322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 932900Seric { 942900Seric eatfrom(buf); 954083Seric (void) fgets(buf, sizeof buf, stdin); 964557Seric fixcrlf(buf, FALSE); 972900Seric } 984321Seric # endif NOTUNIX 992900Seric 1001392Seric /* 1015185Seric ** Copy stdin to temp file & do message editing. 1021392Seric ** To keep certain mailers from getting confused, 1031392Seric ** and to keep the output clean, lines that look 1041392Seric ** like UNIX "From" lines are deleted in the header, 1051392Seric ** and prepended with ">" in the body. 1061392Seric */ 1071392Seric 1084622Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 1091392Seric { 1104316Seric register char c; 1114316Seric extern bool isheader(); 1124316Seric 1134557Seric fixcrlf(buf, FALSE); 1144557Seric 1152900Seric /* see if the header is over */ 1162900Seric if (!isheader(buf)) 1172900Seric break; 1182900Seric 1192900Seric /* get the rest of this field */ 1202900Seric while ((c = getc(stdin)) == ' ' || c == '\t') 1211392Seric { 1222900Seric p = &buf[strlen(buf)]; 1232900Seric *p++ = c; 1242900Seric if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 1252900Seric break; 1264557Seric fixcrlf(p, FALSE); 1271392Seric } 1284083Seric if (!feof(stdin)) 1294083Seric (void) ungetc(c, stdin); 1301392Seric 1312900Seric MsgSize += strlen(buf); 1321392Seric 1332900Seric /* 1342900Seric ** Snarf header away. 1352900Seric */ 1362900Seric 1373391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1383058Seric break; 1392900Seric } 1401392Seric 1412900Seric # ifdef DEBUG 1422900Seric if (Debug) 1432900Seric printf("EOH\n"); 1442900Seric # endif DEBUG 1452900Seric 1462900Seric /* throw away a blank line */ 1472900Seric if (buf[0] == '\n') 1484557Seric { 1494083Seric (void) fgets(buf, sizeof buf, stdin); 1504557Seric fixcrlf(buf, FALSE); 1514557Seric } 1522900Seric 1532900Seric /* 1542900Seric ** Collect the body of the message. 1552900Seric */ 1562900Seric 1572900Seric for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 1582900Seric { 1594156Seric register int i; 1604551Seric register char *bp = buf; 1614156Seric 1624557Seric fixcrlf(buf, FALSE); 1634557Seric 1642900Seric /* check for end-of-message */ 1652900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1662900Seric break; 1672900Seric 1684551Seric /* check for transparent dot */ 1694551Seric if (Smtp && *bp == '.') 1704551Seric bp++; 1714551Seric 1724321Seric # ifndef NOTUNIX 1732900Seric /* Hide UNIX-like From lines */ 1744551Seric if (strncmp(bp, "From ", 5) == 0) 1751392Seric { 1762900Seric fputs(">", tf); 1772900Seric MsgSize++; 1781392Seric } 1794321Seric # endif NOTUNIX 1804156Seric 1814156Seric /* 1824156Seric ** Figure message length, output the line to the temp 1834156Seric ** file, and insert a newline if missing. 1844156Seric */ 1854156Seric 1864551Seric i = strlen(bp); 1874156Seric MsgSize += i; 1884551Seric fputs(bp, tf); 1894551Seric if (bp[i - 1] != '\n') 1904156Seric fputs("\n", tf); 1911392Seric if (ferror(tf)) 1921392Seric { 1931439Seric if (errno == ENOSPC) 1941439Seric { 1954083Seric (void) freopen(InFileName, "w", tf); 1961439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1974557Seric usrerr("452 Out of disk space for temp file"); 1981439Seric } 1991439Seric else 2004453Seric syserr("collect: Cannot write %s", InFileName); 2014083Seric (void) freopen("/dev/null", "w", tf); 2021392Seric } 2031392Seric } 2044083Seric (void) fclose(tf); 2052900Seric 2062900Seric /* 2072900Seric ** Find out some information from the headers. 2083386Seric ** Examples are who is the from person & the date. 2092900Seric */ 2102900Seric 2115033Seric if (!QueueRun) 2125033Seric { 2135033Seric /* adjust total priority by message priority */ 2145033Seric MsgPriority = MsgSize; 2155033Seric p = hvalue("priority"); 2165033Seric if (p != NULL) 2175033Seric MsgPriority -= priencode(p) * WKPRIFACT; 2185033Seric } 2194622Seric 2202900Seric /* from person */ 2212900Seric xfrom = hvalue("sender"); 2222900Seric if (xfrom == NULL) 2234371Seric xfrom = OrigFrom; 2244710Seric if (ArpaMode) 2254316Seric setfrom(xfrom, (char *) NULL); 2262900Seric 2273390Seric /* full name of from person */ 2283390Seric p = hvalue("full-name"); 2293390Seric if (p != NULL) 2303390Seric define('x', p); 2314210Seric else 2324210Seric { 2334210Seric register char *q; 2343390Seric 2354210Seric /* 2364210Seric ** Try to extract the full name from a general From: 2374210Seric ** field. We take anything which is a comment as a 2384210Seric ** first choice. Failing in that, we see if there is 2394210Seric ** a "machine readable" name (in <angle brackets>); if 2404210Seric ** so we take anything preceeding that clause. 2414210Seric ** 2424210Seric ** If we blow it here it's not all that serious. 2434210Seric */ 2444210Seric 2454210Seric p = hvalue("original-from"); 2464371Seric if (p == NULL) 2474371Seric p = OrigFrom; 2484210Seric q = index(p, '('); 2494210Seric if (q != NULL) 2504210Seric { 2514210Seric int parenlev = 0; 2524210Seric 2534210Seric for (p = q; *p != '\0'; p++) 2544210Seric { 2554210Seric if (*p == '(') 2564210Seric parenlev++; 2574210Seric else if (*p == ')' && --parenlev <= 0) 2584210Seric break; 2594210Seric } 2604210Seric if (*p == ')') 2614210Seric { 2624210Seric *p = '\0'; 2634210Seric if (*++q != '\0') 2644210Seric define('x', newstr(q)); 2654210Seric *p = ')'; 2664210Seric } 2674210Seric } 2684210Seric else if ((q = index(p, '<')) != NULL) 2694210Seric { 2704210Seric char savec; 2714210Seric 2724210Seric while (*--q == ' ') 2734210Seric continue; 2744210Seric while (isspace(*p)) 2754210Seric p++; 2764210Seric savec = *++q; 2774210Seric *q = '\0'; 2784210Seric if (*p != '\0') 2794210Seric define('x', newstr(p)); 2804210Seric *q = savec; 2814210Seric } 2824210Seric } 2834210Seric 2842900Seric /* date message originated */ 2854149Seric p = hvalue("posted-date"); 2864149Seric if (p == NULL) 2874149Seric p = hvalue("date"); 2882900Seric if (p != NULL) 2892900Seric { 2903386Seric define('a', p); 2913386Seric /* we don't have a good way to do canonical conversion .... 2923386Seric define('d', newstr(arpatounix(p))); 2933386Seric .... so we will ignore the problem for the time being */ 2942900Seric } 2952900Seric 2964182Seric if ((TempFile = fopen(InFileName, "r")) == NULL) 2971392Seric syserr("Cannot reopen %s", InFileName); 2982900Seric 2992900Seric # ifdef DEBUG 3002900Seric if (Debug) 3012900Seric { 3024316Seric HDR *h; 3034316Seric extern char *capitalize(); 3044316Seric 3052900Seric printf("----- collected header -----\n"); 3062900Seric for (h = Header; h != NULL; h = h->h_link) 3072900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 3082900Seric printf("----------------------------\n"); 3092900Seric } 3102900Seric # endif DEBUG 3114162Seric return; 3121392Seric } 3131392Seric /* 3142900Seric ** EATFROM -- chew up a UNIX style from line and process 3152900Seric ** 3162900Seric ** This does indeed make some assumptions about the format 3172900Seric ** of UNIX messages. 3182900Seric ** 3192900Seric ** Parameters: 3202900Seric ** fm -- the from line. 3212900Seric ** 3222900Seric ** Returns: 3232900Seric ** none. 3242900Seric ** 3252900Seric ** Side Effects: 3262900Seric ** extracts what information it can from the header, 3273386Seric ** such as the date. 3282900Seric */ 3292900Seric 3304321Seric # ifndef NOTUNIX 3314321Seric 3324203Seric char *DowList[] = 3334203Seric { 3344203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3354203Seric }; 3364203Seric 3372900Seric char *MonthList[] = 3382900Seric { 3392900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3402900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3412900Seric NULL 3422900Seric }; 3432900Seric 3442900Seric eatfrom(fm) 3452900Seric char *fm; 3462900Seric { 3472900Seric register char *p; 3482900Seric register char **dt; 3492900Seric 3504203Seric # ifdef DEBUG 3514203Seric if (Debug > 1) 3524203Seric printf("eatfrom(%s)\n", fm); 3534203Seric # endif DEBUG 3544203Seric 3552900Seric /* find the date part */ 3562900Seric p = fm; 3572900Seric while (*p != '\0') 3582900Seric { 3592900Seric /* skip a word */ 3602900Seric while (*p != '\0' && *p != ' ') 3612900Seric *p++; 3622900Seric while (*p == ' ') 3632900Seric *p++; 3642900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3652900Seric continue; 3662900Seric 3672900Seric /* we have a possible date */ 3684203Seric for (dt = DowList; *dt != NULL; dt++) 3692900Seric if (strncmp(*dt, p, 3) == 0) 3702900Seric break; 3714203Seric if (*dt == NULL) 3724203Seric continue; 3732900Seric 3744203Seric for (dt = MonthList; *dt != NULL; dt++) 3754203Seric if (strncmp(*dt, &p[4], 3) == 0) 3764203Seric break; 3772900Seric if (*dt != NULL) 3782900Seric break; 3792900Seric } 3802900Seric 3812900Seric if (*p != NULL) 3822900Seric { 3833386Seric char *q; 3843386Seric 3852900Seric /* we have found a date */ 3863386Seric q = xalloc(25); 3873386Seric strncpy(q, p, 25); 3883386Seric q[24] = '\0'; 3893386Seric define('d', q); 3902900Seric } 3912900Seric } 3924321Seric 3934321Seric # endif NOTUNIX 3944622Seric /* 3954622Seric ** PRIENCODE -- encode external priority names into internal values. 3964622Seric ** 3974622Seric ** Parameters: 3984622Seric ** p -- priority in ascii. 3994622Seric ** 4004622Seric ** Returns: 4014622Seric ** priority as a numeric level. 4024622Seric ** 4034622Seric ** Side Effects: 4044622Seric ** none. 4054622Seric */ 4064622Seric 4074622Seric struct prio 4084622Seric { 4094622Seric char *pri_name; /* external name of priority */ 4104622Seric int pri_val; /* internal value for same */ 4114622Seric }; 4124622Seric 4134622Seric static struct prio Prio[] = 4144622Seric { 4154633Seric "alert", PRI_ALERT, 4164633Seric "quick", PRI_QUICK, 4174633Seric "first-class", PRI_FIRSTCL, 4184622Seric "normal", PRI_NORMAL, 4194622Seric "second-class", PRI_SECONDCL, 4204622Seric "third-class", PRI_THIRDCL, 4214622Seric NULL, PRI_NORMAL, 4224622Seric }; 4234622Seric 4244622Seric priencode(p) 4254622Seric char *p; 4264622Seric { 4274622Seric register struct prio *pl; 4284633Seric extern bool sameword(); 4294622Seric 4304622Seric for (pl = Prio; pl->pri_name != NULL; pl++) 4314622Seric { 4324633Seric if (sameword(p, pl->pri_name)) 4334622Seric break; 4344622Seric } 4354622Seric return (pl->pri_val); 4364622Seric } 437