1 # include <errno.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)collect.c 3.37 05/22/82); 5 6 /* 7 ** COLLECT -- read & parse message header & make temp file. 8 ** 9 ** Creates a temporary file name and copies the standard 10 ** input to that file. While it is doing it, it looks for 11 ** "From:" and "Sender:" fields to use as the from-person 12 ** (but only if the -a flag is specified). It prefers to 13 ** to use the "Sender:" field. 14 ** 15 ** MIT seems to like to produce "Sent-By:" fields instead 16 ** of "Sender:" fields. We used to catch this, but it turns 17 ** out that the "Sent-By:" field doesn't always correspond 18 ** to someone real ("___057", for instance), as required by 19 ** the protocol. So we limp by..... 20 ** 21 ** Parameters: 22 ** sayok -- if set, give an ARPANET style message 23 ** to say we are ready to collect input. 24 ** 25 ** Returns: 26 ** none. 27 ** 28 ** Side Effects: 29 ** Temp file is created and filled. 30 ** The from person may be set. 31 */ 32 33 collect(sayok) 34 bool sayok; 35 { 36 register FILE *tf; 37 char buf[MAXFIELD+1]; 38 register char *p; 39 char *xfrom; 40 extern char *hvalue(); 41 extern char *mktemp(); 42 static char tempfname[40]; 43 extern char *macvalue(); 44 45 /* 46 ** Create the temp file name and create the file. 47 */ 48 49 strcpy(tempfname, QueueDir); 50 strcat(tempfname, "/dfaXXXXXX"); 51 (void) mktemp(tempfname); 52 if ((tf = dfopen(tempfname, "w")) == NULL) 53 { 54 syserr("Cannot create %s", tempfname); 55 NoReturn = TRUE; 56 finis(); 57 } 58 chmod(tempfname, 0600); 59 InFileName = tempfname; 60 61 /* 62 ** Create the Mail-From line if we want to. 63 */ 64 65 if (Smtp && macvalue('s') != NULL) 66 { 67 char xbuf[50]; 68 69 (void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b", 70 macvalue('r') == NULL ? "" : "$r host "); 71 (void) expand(xbuf, buf, &buf[sizeof buf - 1]); 72 (void) chompheader(buf, FALSE); 73 } 74 75 /* 76 ** Tell ARPANET to go ahead. 77 */ 78 79 if (sayok) 80 message("354", "Enter mail, end with \".\" on a line by itself"); 81 82 /* 83 ** Try to read a UNIX-style From line 84 */ 85 86 if (fgets(buf, sizeof buf, InChannel) == NULL) 87 return; 88 fixcrlf(buf, FALSE); 89 # ifndef NOTUNIX 90 if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 91 { 92 eatfrom(buf); 93 (void) fgets(buf, sizeof buf, InChannel); 94 fixcrlf(buf, FALSE); 95 } 96 # endif NOTUNIX 97 98 /* 99 ** Copy InChannel to temp file & do message editing. 100 ** To keep certain mailers from getting confused, 101 ** and to keep the output clean, lines that look 102 ** like UNIX "From" lines are deleted in the header, 103 ** and prepended with ">" in the body. 104 */ 105 106 for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 107 { 108 register char c; 109 extern bool isheader(); 110 111 fixcrlf(buf, FALSE); 112 113 /* see if the header is over */ 114 if (!isheader(buf)) 115 break; 116 117 /* get the rest of this field */ 118 while ((c = getc(InChannel)) == ' ' || c == '\t') 119 { 120 p = &buf[strlen(buf)]; 121 *p++ = c; 122 if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL) 123 break; 124 fixcrlf(p, FALSE); 125 } 126 if (!feof(InChannel)) 127 (void) ungetc(c, InChannel); 128 129 CurEnv->e_msgsize += strlen(buf); 130 131 /* 132 ** Snarf header away. 133 */ 134 135 if (bitset(H_EOH, chompheader(buf, FALSE))) 136 break; 137 } 138 139 # ifdef DEBUG 140 if (Debug) 141 printf("EOH\n"); 142 # endif DEBUG 143 144 /* throw away a blank line */ 145 if (buf[0] == '\n') 146 { 147 (void) fgets(buf, sizeof buf, InChannel); 148 fixcrlf(buf, FALSE); 149 } 150 151 /* 152 ** Collect the body of the message. 153 */ 154 155 for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 156 { 157 register int i; 158 register char *bp = buf; 159 160 fixcrlf(buf, FALSE); 161 162 /* check for end-of-message */ 163 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 164 break; 165 166 /* check for transparent dot */ 167 if (Smtp && *bp == '.') 168 bp++; 169 170 # ifndef NOTUNIX 171 /* Hide UNIX-like From lines */ 172 if (strncmp(bp, "From ", 5) == 0) 173 { 174 fputs(">", tf); 175 CurEnv->e_msgsize++; 176 } 177 # endif NOTUNIX 178 179 /* 180 ** Figure message length, output the line to the temp 181 ** file, and insert a newline if missing. 182 */ 183 184 i = strlen(bp); 185 CurEnv->e_msgsize += i; 186 fputs(bp, tf); 187 if (bp[i - 1] != '\n') 188 fputs("\n", tf); 189 if (ferror(tf)) 190 { 191 if (errno == ENOSPC) 192 { 193 (void) freopen(InFileName, "w", tf); 194 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 195 usrerr("452 Out of disk space for temp file"); 196 } 197 else 198 syserr("collect: Cannot write %s", InFileName); 199 (void) freopen("/dev/null", "w", tf); 200 } 201 } 202 (void) fclose(tf); 203 204 /* 205 ** Find out some information from the headers. 206 ** Examples are who is the from person & the date. 207 */ 208 209 /* message priority */ 210 if (!QueueRun) 211 { 212 /* adjust total priority by message priority */ 213 CurEnv->e_msgpriority = CurEnv->e_msgsize; 214 p = hvalue("priority"); 215 if (p != NULL) 216 CurEnv->e_msgpriority -= priencode(p) * WKPRIFACT; 217 } 218 219 /* special handling */ 220 p = hvalue("special-handling"); 221 if (p != NULL) 222 spechandling(p); 223 224 /* from person */ 225 xfrom = hvalue("sender"); 226 if (xfrom == NULL) 227 xfrom = CurEnv->e_origfrom; 228 if (ArpaMode) 229 setfrom(xfrom, (char *) NULL); 230 231 /* full name of from person */ 232 p = hvalue("full-name"); 233 if (p != NULL) 234 define('x', p); 235 else 236 { 237 extern char *getxpart(); 238 239 /* 240 ** Try to extract the full name from a general From: 241 ** field. We take anything which is a comment as a 242 ** first choice. Failing in that, we see if there is 243 ** a "machine readable" name (in <angle brackets>); if 244 ** so we take anything preceeding that clause. 245 ** 246 ** If we blow it here it's not all that serious. 247 */ 248 249 p = hvalue("original-from"); 250 if (p == NULL) 251 p = CurEnv->e_origfrom; 252 p = getxpart(p); 253 if (p != NULL) 254 define('x', newstr(p)); 255 } 256 257 /* date message originated */ 258 p = hvalue("posted-date"); 259 if (p == NULL) 260 p = hvalue("date"); 261 if (p != NULL) 262 { 263 define('a', p); 264 /* we don't have a good way to do canonical conversion .... 265 define('d', newstr(arpatounix(p))); 266 .... so we will ignore the problem for the time being */ 267 } 268 269 if ((TempFile = fopen(InFileName, "r")) == NULL) 270 syserr("Cannot reopen %s", InFileName); 271 272 # ifdef DEBUG 273 if (Debug) 274 { 275 HDR *h; 276 extern char *capitalize(); 277 278 printf("----- collected header -----\n"); 279 for (h = CurEnv->e_header; h != NULL; h = h->h_link) 280 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 281 printf("----------------------------\n"); 282 } 283 # endif DEBUG 284 return; 285 } 286 /* 287 ** EATFROM -- chew up a UNIX style from line and process 288 ** 289 ** This does indeed make some assumptions about the format 290 ** of UNIX messages. 291 ** 292 ** Parameters: 293 ** fm -- the from line. 294 ** 295 ** Returns: 296 ** none. 297 ** 298 ** Side Effects: 299 ** extracts what information it can from the header, 300 ** such as the date. 301 */ 302 303 # ifndef NOTUNIX 304 305 char *DowList[] = 306 { 307 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 308 }; 309 310 char *MonthList[] = 311 { 312 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 313 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 314 NULL 315 }; 316 317 eatfrom(fm) 318 char *fm; 319 { 320 register char *p; 321 register char **dt; 322 323 # ifdef DEBUG 324 if (Debug > 1) 325 printf("eatfrom(%s)\n", fm); 326 # endif DEBUG 327 328 /* find the date part */ 329 p = fm; 330 while (*p != '\0') 331 { 332 /* skip a word */ 333 while (*p != '\0' && *p != ' ') 334 *p++; 335 while (*p == ' ') 336 *p++; 337 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 338 continue; 339 340 /* we have a possible date */ 341 for (dt = DowList; *dt != NULL; dt++) 342 if (strncmp(*dt, p, 3) == 0) 343 break; 344 if (*dt == NULL) 345 continue; 346 347 for (dt = MonthList; *dt != NULL; dt++) 348 if (strncmp(*dt, &p[4], 3) == 0) 349 break; 350 if (*dt != NULL) 351 break; 352 } 353 354 if (*p != NULL) 355 { 356 char *q; 357 extern char *arpadate(); 358 359 /* we have found a date */ 360 q = xalloc(25); 361 strncpy(q, p, 25); 362 q[24] = '\0'; 363 define('d', q); 364 q = arpadate(q); 365 define('a', newstr(q)); 366 } 367 } 368 369 # endif NOTUNIX 370 /* 371 ** PRIENCODE -- encode external priority names into internal values. 372 ** 373 ** Parameters: 374 ** p -- priority in ascii. 375 ** 376 ** Returns: 377 ** priority as a numeric level. 378 ** 379 ** Side Effects: 380 ** none. 381 */ 382 383 struct prio 384 { 385 char *pri_name; /* external name of priority */ 386 int pri_val; /* internal value for same */ 387 }; 388 389 static struct prio Prio[] = 390 { 391 "alert", PRI_ALERT, 392 "quick", PRI_QUICK, 393 "first-class", PRI_FIRSTCL, 394 "normal", PRI_NORMAL, 395 "second-class", PRI_SECONDCL, 396 "third-class", PRI_THIRDCL, 397 NULL, PRI_NORMAL, 398 }; 399 400 priencode(p) 401 char *p; 402 { 403 register struct prio *pl; 404 extern bool sameword(); 405 406 for (pl = Prio; pl->pri_name != NULL; pl++) 407 { 408 if (sameword(p, pl->pri_name)) 409 break; 410 } 411 return (pl->pri_val); 412 } 413 /* 414 ** SPECHANDLE -- do special handling 415 ** 416 ** Parameters: 417 ** p -- pointer to list of special handling words. 418 ** 419 ** Returns: 420 ** none. 421 ** 422 ** Side Effects: 423 ** Sets flags as indicated by p. 424 */ 425 426 struct handling 427 { 428 char *han_name; /* word to get this magic */ 429 int han_what; /* what to do, see below */ 430 }; 431 432 /* modes for han_what */ 433 # define HAN_NONE 0 /* nothing special */ 434 # define HAN_RRECEIPT 1 /* give return receipt */ 435 436 struct handling Handling[] = 437 { 438 "return-receipt-requested", HAN_RRECEIPT, 439 NULL, HAN_NONE 440 }; 441 442 spechandling(p) 443 register char *p; 444 { 445 register char *w; 446 register struct handling *h; 447 extern bool sameword(); 448 449 while (*p != '\0') 450 { 451 /* collect a word to compare to */ 452 while (*p != '\0' && (*p == ',' || isspace(*p))) 453 p++; 454 if (*p == '\0') 455 break; 456 w = p; 457 while (*p != '\0' && *p != ',' && !isspace(*p)) 458 p++; 459 if (*p != '\0') 460 *p++ = '\0'; 461 462 /* scan the special handling table */ 463 for (h = Handling; h->han_name != NULL; h++) 464 if (sameword(h->han_name, w)) 465 break; 466 467 /* see if we can do anything interesting */ 468 switch (h->han_what) 469 { 470 case HAN_NONE: /* nothing to be done */ 471 break; 472 473 case HAN_RRECEIPT: /* give return receipt */ 474 CurEnv->e_retreceipt = TRUE; 475 # ifdef DEBUG 476 if (Debug > 2) 477 printf(">>> Return receipt requested\n"); 478 # endif DEBUG 479 break; 480 481 default: 482 syserr("spechandling: handling %d (%s)", h->han_what, w); 483 } 484 } 485 } 486