11439Seric # include <errno.h> 23309Seric # include "sendmail.h" 31392Seric 4*6909Seric SCCSID(@(#)collect.c 3.38 05/22/82); 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 334710Seric collect(sayok) 344710Seric bool sayok; 351392Seric { 361392Seric register FILE *tf; 371392Seric char buf[MAXFIELD+1]; 381392Seric register char *p; 392900Seric char *xfrom; 402900Seric extern char *hvalue(); 414083Seric extern char *mktemp(); 424622Seric static char tempfname[40]; 435192Seric extern char *macvalue(); 441392Seric 451392Seric /* 461392Seric ** Create the temp file name and create the file. 471392Seric */ 481392Seric 494622Seric strcpy(tempfname, QueueDir); 504622Seric strcat(tempfname, "/dfaXXXXXX"); 514622Seric (void) mktemp(tempfname); 526888Seric if ((tf = dfopen(tempfname, "w")) == NULL) 531392Seric { 544622Seric syserr("Cannot create %s", tempfname); 555366Seric NoReturn = TRUE; 565366Seric finis(); 571392Seric } 586888Seric chmod(tempfname, 0600); 594622Seric InFileName = tempfname; 601392Seric 614316Seric /* 625185Seric ** Create the Mail-From line if we want to. 635185Seric */ 645185Seric 655366Seric if (Smtp && macvalue('s') != NULL) 665185Seric { 675185Seric char xbuf[50]; 685185Seric 695192Seric (void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b", 705185Seric macvalue('r') == NULL ? "" : "$r host "); 715185Seric (void) expand(xbuf, buf, &buf[sizeof buf - 1]); 725192Seric (void) chompheader(buf, FALSE); 735185Seric } 745185Seric 755185Seric /* 764322Seric ** Tell ARPANET to go ahead. 774322Seric */ 784322Seric 794710Seric if (sayok) 804710Seric message("354", "Enter mail, end with \".\" on a line by itself"); 814322Seric 824322Seric /* 834316Seric ** Try to read a UNIX-style From line 844316Seric */ 854316Seric 865975Seric if (fgets(buf, sizeof buf, InChannel) == NULL) 874162Seric return; 884557Seric fixcrlf(buf, FALSE); 894321Seric # ifndef NOTUNIX 904322Seric if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 912900Seric { 922900Seric eatfrom(buf); 935975Seric (void) fgets(buf, sizeof buf, InChannel); 944557Seric fixcrlf(buf, FALSE); 952900Seric } 964321Seric # endif NOTUNIX 972900Seric 981392Seric /* 995975Seric ** Copy InChannel to temp file & do message editing. 1001392Seric ** To keep certain mailers from getting confused, 1011392Seric ** and to keep the output clean, lines that look 1021392Seric ** like UNIX "From" lines are deleted in the header, 1031392Seric ** and prepended with ">" in the body. 1041392Seric */ 1051392Seric 1065975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1071392Seric { 1084316Seric register char c; 1094316Seric extern bool isheader(); 1104316Seric 1114557Seric fixcrlf(buf, FALSE); 1124557Seric 1132900Seric /* see if the header is over */ 1142900Seric if (!isheader(buf)) 1152900Seric break; 1162900Seric 1172900Seric /* get the rest of this field */ 1185975Seric while ((c = getc(InChannel)) == ' ' || c == '\t') 1191392Seric { 1202900Seric p = &buf[strlen(buf)]; 1212900Seric *p++ = c; 1225975Seric if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL) 1232900Seric break; 1244557Seric fixcrlf(p, FALSE); 1251392Seric } 1265975Seric if (!feof(InChannel)) 1275975Seric (void) ungetc(c, InChannel); 1281392Seric 1296901Seric CurEnv->e_msgsize += strlen(buf); 1301392Seric 1312900Seric /* 1322900Seric ** Snarf header away. 1332900Seric */ 1342900Seric 1353391Seric if (bitset(H_EOH, chompheader(buf, FALSE))) 1363058Seric break; 1372900Seric } 1381392Seric 1392900Seric # ifdef DEBUG 1402900Seric if (Debug) 1412900Seric printf("EOH\n"); 1422900Seric # endif DEBUG 1432900Seric 1442900Seric /* throw away a blank line */ 1452900Seric if (buf[0] == '\n') 1464557Seric { 1475975Seric (void) fgets(buf, sizeof buf, InChannel); 1484557Seric fixcrlf(buf, FALSE); 1494557Seric } 1502900Seric 1512900Seric /* 1522900Seric ** Collect the body of the message. 1532900Seric */ 1542900Seric 1555975Seric for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 1562900Seric { 1574156Seric register int i; 1584551Seric register char *bp = buf; 1594156Seric 1604557Seric fixcrlf(buf, FALSE); 1614557Seric 1622900Seric /* check for end-of-message */ 1632900Seric if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 1642900Seric break; 1652900Seric 1664551Seric /* check for transparent dot */ 1674551Seric if (Smtp && *bp == '.') 1684551Seric bp++; 1694551Seric 1704321Seric # ifndef NOTUNIX 1712900Seric /* Hide UNIX-like From lines */ 1724551Seric if (strncmp(bp, "From ", 5) == 0) 1731392Seric { 1742900Seric fputs(">", tf); 1756901Seric CurEnv->e_msgsize++; 1761392Seric } 1774321Seric # endif NOTUNIX 1784156Seric 1794156Seric /* 1804156Seric ** Figure message length, output the line to the temp 1814156Seric ** file, and insert a newline if missing. 1824156Seric */ 1834156Seric 1844551Seric i = strlen(bp); 1856901Seric CurEnv->e_msgsize += i; 1864551Seric fputs(bp, tf); 1874551Seric if (bp[i - 1] != '\n') 1884156Seric fputs("\n", tf); 1891392Seric if (ferror(tf)) 1901392Seric { 1911439Seric if (errno == ENOSPC) 1921439Seric { 1934083Seric (void) freopen(InFileName, "w", tf); 1941439Seric fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 1954557Seric usrerr("452 Out of disk space for temp file"); 1961439Seric } 1971439Seric else 1984453Seric syserr("collect: Cannot write %s", InFileName); 1994083Seric (void) freopen("/dev/null", "w", tf); 2001392Seric } 2011392Seric } 2024083Seric (void) fclose(tf); 2032900Seric 2042900Seric /* 2052900Seric ** Find out some information from the headers. 2063386Seric ** Examples are who is the from person & the date. 2072900Seric */ 2082900Seric 2095982Seric /* message priority */ 2105033Seric if (!QueueRun) 2115033Seric { 2125033Seric /* adjust total priority by message priority */ 2136901Seric CurEnv->e_msgpriority = CurEnv->e_msgsize; 2145033Seric p = hvalue("priority"); 2155033Seric if (p != NULL) 216*6909Seric CurEnv->e_class = priencode(p); 217*6909Seric else 218*6909Seric CurEnv->e_class = PRI_NORMAL; 219*6909Seric CurEnv->e_msgpriority -= CurEnv->e_class * WKPRIFACT; 2205033Seric } 2214622Seric 2225982Seric /* special handling */ 2235982Seric p = hvalue("special-handling"); 2245982Seric if (p != NULL) 2255982Seric spechandling(p); 2265982Seric 2272900Seric /* from person */ 2282900Seric xfrom = hvalue("sender"); 2292900Seric if (xfrom == NULL) 2306901Seric xfrom = CurEnv->e_origfrom; 2314710Seric if (ArpaMode) 2324316Seric setfrom(xfrom, (char *) NULL); 2332900Seric 2343390Seric /* full name of from person */ 2353390Seric p = hvalue("full-name"); 2363390Seric if (p != NULL) 2373390Seric define('x', p); 2384210Seric else 2394210Seric { 2405917Seric extern char *getxpart(); 2413390Seric 2424210Seric /* 2434210Seric ** Try to extract the full name from a general From: 2444210Seric ** field. We take anything which is a comment as a 2454210Seric ** first choice. Failing in that, we see if there is 2464210Seric ** a "machine readable" name (in <angle brackets>); if 2474210Seric ** so we take anything preceeding that clause. 2484210Seric ** 2494210Seric ** If we blow it here it's not all that serious. 2504210Seric */ 2514210Seric 2524210Seric p = hvalue("original-from"); 2534371Seric if (p == NULL) 2546901Seric p = CurEnv->e_origfrom; 2555917Seric p = getxpart(p); 2565917Seric if (p != NULL) 2575917Seric define('x', newstr(p)); 2584210Seric } 2594210Seric 2602900Seric /* date message originated */ 2614149Seric p = hvalue("posted-date"); 2624149Seric if (p == NULL) 2634149Seric p = hvalue("date"); 2642900Seric if (p != NULL) 2652900Seric { 2663386Seric define('a', p); 2673386Seric /* we don't have a good way to do canonical conversion .... 2683386Seric define('d', newstr(arpatounix(p))); 2693386Seric .... so we will ignore the problem for the time being */ 2702900Seric } 2712900Seric 2724182Seric if ((TempFile = fopen(InFileName, "r")) == NULL) 2731392Seric syserr("Cannot reopen %s", InFileName); 2742900Seric 2752900Seric # ifdef DEBUG 2762900Seric if (Debug) 2772900Seric { 2784316Seric HDR *h; 2794316Seric extern char *capitalize(); 2804316Seric 2812900Seric printf("----- collected header -----\n"); 2826901Seric for (h = CurEnv->e_header; h != NULL; h = h->h_link) 2832900Seric printf("%s: %s\n", capitalize(h->h_field), h->h_value); 2842900Seric printf("----------------------------\n"); 2852900Seric } 2862900Seric # endif DEBUG 2874162Seric return; 2881392Seric } 2891392Seric /* 2902900Seric ** EATFROM -- chew up a UNIX style from line and process 2912900Seric ** 2922900Seric ** This does indeed make some assumptions about the format 2932900Seric ** of UNIX messages. 2942900Seric ** 2952900Seric ** Parameters: 2962900Seric ** fm -- the from line. 2972900Seric ** 2982900Seric ** Returns: 2992900Seric ** none. 3002900Seric ** 3012900Seric ** Side Effects: 3022900Seric ** extracts what information it can from the header, 3033386Seric ** such as the date. 3042900Seric */ 3052900Seric 3064321Seric # ifndef NOTUNIX 3074321Seric 3084203Seric char *DowList[] = 3094203Seric { 3104203Seric "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 3114203Seric }; 3124203Seric 3132900Seric char *MonthList[] = 3142900Seric { 3152900Seric "Jan", "Feb", "Mar", "Apr", "May", "Jun", 3162900Seric "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 3172900Seric NULL 3182900Seric }; 3192900Seric 3202900Seric eatfrom(fm) 3212900Seric char *fm; 3222900Seric { 3232900Seric register char *p; 3242900Seric register char **dt; 3252900Seric 3264203Seric # ifdef DEBUG 3274203Seric if (Debug > 1) 3284203Seric printf("eatfrom(%s)\n", fm); 3294203Seric # endif DEBUG 3304203Seric 3312900Seric /* find the date part */ 3322900Seric p = fm; 3332900Seric while (*p != '\0') 3342900Seric { 3352900Seric /* skip a word */ 3362900Seric while (*p != '\0' && *p != ' ') 3372900Seric *p++; 3382900Seric while (*p == ' ') 3392900Seric *p++; 3402900Seric if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 3412900Seric continue; 3422900Seric 3432900Seric /* we have a possible date */ 3444203Seric for (dt = DowList; *dt != NULL; dt++) 3452900Seric if (strncmp(*dt, p, 3) == 0) 3462900Seric break; 3474203Seric if (*dt == NULL) 3484203Seric continue; 3492900Seric 3504203Seric for (dt = MonthList; *dt != NULL; dt++) 3514203Seric if (strncmp(*dt, &p[4], 3) == 0) 3524203Seric break; 3532900Seric if (*dt != NULL) 3542900Seric break; 3552900Seric } 3562900Seric 3572900Seric if (*p != NULL) 3582900Seric { 3593386Seric char *q; 3605366Seric extern char *arpadate(); 3613386Seric 3622900Seric /* we have found a date */ 3633386Seric q = xalloc(25); 3643386Seric strncpy(q, p, 25); 3653386Seric q[24] = '\0'; 3663386Seric define('d', q); 3675366Seric q = arpadate(q); 3685366Seric define('a', newstr(q)); 3692900Seric } 3702900Seric } 3714321Seric 3724321Seric # endif NOTUNIX 3734622Seric /* 3744622Seric ** PRIENCODE -- encode external priority names into internal values. 3754622Seric ** 3764622Seric ** Parameters: 3774622Seric ** p -- priority in ascii. 3784622Seric ** 3794622Seric ** Returns: 3804622Seric ** priority as a numeric level. 3814622Seric ** 3824622Seric ** Side Effects: 3834622Seric ** none. 3844622Seric */ 3854622Seric 3864622Seric struct prio 3874622Seric { 3884622Seric char *pri_name; /* external name of priority */ 3894622Seric int pri_val; /* internal value for same */ 3904622Seric }; 3914622Seric 3924622Seric static struct prio Prio[] = 3934622Seric { 3944633Seric "alert", PRI_ALERT, 3954633Seric "quick", PRI_QUICK, 3964633Seric "first-class", PRI_FIRSTCL, 3974622Seric "normal", PRI_NORMAL, 3984622Seric "second-class", PRI_SECONDCL, 3994622Seric "third-class", PRI_THIRDCL, 400*6909Seric "junk", PRI_JUNK, 4014622Seric NULL, PRI_NORMAL, 4024622Seric }; 4034622Seric 4044622Seric priencode(p) 4054622Seric char *p; 4064622Seric { 4074622Seric register struct prio *pl; 4084633Seric extern bool sameword(); 4094622Seric 4104622Seric for (pl = Prio; pl->pri_name != NULL; pl++) 4114622Seric { 4124633Seric if (sameword(p, pl->pri_name)) 4134622Seric break; 4144622Seric } 4154622Seric return (pl->pri_val); 4164622Seric } 4175982Seric /* 4185982Seric ** SPECHANDLE -- do special handling 4195982Seric ** 4205982Seric ** Parameters: 4215982Seric ** p -- pointer to list of special handling words. 4225982Seric ** 4235982Seric ** Returns: 4245982Seric ** none. 4255982Seric ** 4265982Seric ** Side Effects: 4275982Seric ** Sets flags as indicated by p. 4285982Seric */ 4295982Seric 4305982Seric struct handling 4315982Seric { 4325982Seric char *han_name; /* word to get this magic */ 4335982Seric int han_what; /* what to do, see below */ 4345982Seric }; 4355982Seric 4365982Seric /* modes for han_what */ 4375982Seric # define HAN_NONE 0 /* nothing special */ 4385982Seric # define HAN_RRECEIPT 1 /* give return receipt */ 4395982Seric 4405982Seric struct handling Handling[] = 4415982Seric { 4425982Seric "return-receipt-requested", HAN_RRECEIPT, 4435982Seric NULL, HAN_NONE 4445982Seric }; 4455982Seric 4465982Seric spechandling(p) 4475982Seric register char *p; 4485982Seric { 4495982Seric register char *w; 4505982Seric register struct handling *h; 4515982Seric extern bool sameword(); 4525982Seric 4535982Seric while (*p != '\0') 4545982Seric { 4555982Seric /* collect a word to compare to */ 4565982Seric while (*p != '\0' && (*p == ',' || isspace(*p))) 4575982Seric p++; 4585982Seric if (*p == '\0') 4595982Seric break; 4605982Seric w = p; 4615982Seric while (*p != '\0' && *p != ',' && !isspace(*p)) 4625982Seric p++; 4635982Seric if (*p != '\0') 4645982Seric *p++ = '\0'; 4655982Seric 4665982Seric /* scan the special handling table */ 4675982Seric for (h = Handling; h->han_name != NULL; h++) 4685982Seric if (sameword(h->han_name, w)) 4695982Seric break; 4705982Seric 4715982Seric /* see if we can do anything interesting */ 4725982Seric switch (h->han_what) 4735982Seric { 4745982Seric case HAN_NONE: /* nothing to be done */ 4755982Seric break; 4765982Seric 4775982Seric case HAN_RRECEIPT: /* give return receipt */ 4786901Seric CurEnv->e_retreceipt = TRUE; 4795982Seric # ifdef DEBUG 4805982Seric if (Debug > 2) 4815982Seric printf(">>> Return receipt requested\n"); 4825982Seric # endif DEBUG 4835982Seric break; 4845982Seric 4855982Seric default: 4865982Seric syserr("spechandling: handling %d (%s)", h->han_what, w); 4875982Seric } 4885982Seric } 4895982Seric } 490