1 # include <errno.h> 2 # include "sendmail.h" 3 4 static char SccsId[] = "@(#)collect.c 3.28 10/31/81"; 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 long MsgSize; /* size of message in bytes */ 34 35 collect(sayok) 36 bool sayok; 37 { 38 register FILE *tf; 39 char buf[MAXFIELD+1]; 40 register char *p; 41 char *xfrom; 42 extern char *hvalue(); 43 extern char *mktemp(); 44 static char tempfname[40]; 45 extern char *QueueDir; 46 47 /* 48 ** Create the temp file name and create the file. 49 */ 50 51 strcpy(tempfname, QueueDir); 52 strcat(tempfname, "/dfaXXXXXX"); 53 (void) mktemp(tempfname); 54 (void) close(creat(tempfname, 0600)); 55 if ((tf = fopen(tempfname, "w")) == NULL) 56 { 57 syserr("Cannot create %s", tempfname); 58 return; 59 } 60 InFileName = tempfname; 61 62 /* 63 ** Tell ARPANET to go ahead. 64 */ 65 66 if (sayok) 67 message("354", "Enter mail, end with \".\" on a line by itself"); 68 69 /* 70 ** Try to read a UNIX-style From line 71 */ 72 73 if (fgets(buf, sizeof buf, stdin) == NULL) 74 return; 75 fixcrlf(buf, FALSE); 76 # ifndef NOTUNIX 77 if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 78 { 79 eatfrom(buf); 80 (void) fgets(buf, sizeof buf, stdin); 81 fixcrlf(buf, FALSE); 82 } 83 # endif NOTUNIX 84 85 /* 86 ** Copy stdin to temp file & do message editting. 87 ** To keep certain mailers from getting confused, 88 ** and to keep the output clean, lines that look 89 ** like UNIX "From" lines are deleted in the header, 90 ** and prepended with ">" in the body. 91 */ 92 93 for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 94 { 95 register char c; 96 extern bool isheader(); 97 98 fixcrlf(buf, FALSE); 99 100 /* see if the header is over */ 101 if (!isheader(buf)) 102 break; 103 104 /* get the rest of this field */ 105 while ((c = getc(stdin)) == ' ' || c == '\t') 106 { 107 p = &buf[strlen(buf)]; 108 *p++ = c; 109 if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 110 break; 111 fixcrlf(p, FALSE); 112 } 113 if (!feof(stdin)) 114 (void) ungetc(c, stdin); 115 116 MsgSize += strlen(buf); 117 118 /* 119 ** Snarf header away. 120 */ 121 122 if (bitset(H_EOH, chompheader(buf, FALSE))) 123 break; 124 } 125 126 # ifdef DEBUG 127 if (Debug) 128 printf("EOH\n"); 129 # endif DEBUG 130 131 /* throw away a blank line */ 132 if (buf[0] == '\n') 133 { 134 (void) fgets(buf, sizeof buf, stdin); 135 fixcrlf(buf, FALSE); 136 } 137 138 /* 139 ** Collect the body of the message. 140 */ 141 142 for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 143 { 144 register int i; 145 register char *bp = buf; 146 147 fixcrlf(buf, FALSE); 148 149 /* check for end-of-message */ 150 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 151 break; 152 153 /* check for transparent dot */ 154 if (Smtp && *bp == '.') 155 bp++; 156 157 # ifndef NOTUNIX 158 /* Hide UNIX-like From lines */ 159 if (strncmp(bp, "From ", 5) == 0) 160 { 161 fputs(">", tf); 162 MsgSize++; 163 } 164 # endif NOTUNIX 165 166 /* 167 ** Figure message length, output the line to the temp 168 ** file, and insert a newline if missing. 169 */ 170 171 i = strlen(bp); 172 MsgSize += i; 173 fputs(bp, tf); 174 if (bp[i - 1] != '\n') 175 fputs("\n", tf); 176 if (ferror(tf)) 177 { 178 if (errno == ENOSPC) 179 { 180 (void) freopen(InFileName, "w", tf); 181 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 182 usrerr("452 Out of disk space for temp file"); 183 } 184 else 185 syserr("collect: Cannot write %s", InFileName); 186 (void) freopen("/dev/null", "w", tf); 187 } 188 } 189 (void) fclose(tf); 190 191 /* 192 ** Find out some information from the headers. 193 ** Examples are who is the from person & the date. 194 */ 195 196 /* message priority */ 197 p = hvalue("priority"); 198 if (p != NULL) 199 MsgPriority = priencode(p); 200 201 /* from person */ 202 xfrom = hvalue("sender"); 203 if (xfrom == NULL) 204 xfrom = OrigFrom; 205 if (ArpaMode) 206 setfrom(xfrom, (char *) NULL); 207 208 /* full name of from person */ 209 p = hvalue("full-name"); 210 if (p != NULL) 211 define('x', p); 212 else 213 { 214 register char *q; 215 216 /* 217 ** Try to extract the full name from a general From: 218 ** field. We take anything which is a comment as a 219 ** first choice. Failing in that, we see if there is 220 ** a "machine readable" name (in <angle brackets>); if 221 ** so we take anything preceeding that clause. 222 ** 223 ** If we blow it here it's not all that serious. 224 */ 225 226 p = hvalue("original-from"); 227 if (p == NULL) 228 p = OrigFrom; 229 q = index(p, '('); 230 if (q != NULL) 231 { 232 int parenlev = 0; 233 234 for (p = q; *p != '\0'; p++) 235 { 236 if (*p == '(') 237 parenlev++; 238 else if (*p == ')' && --parenlev <= 0) 239 break; 240 } 241 if (*p == ')') 242 { 243 *p = '\0'; 244 if (*++q != '\0') 245 define('x', newstr(q)); 246 *p = ')'; 247 } 248 } 249 else if ((q = index(p, '<')) != NULL) 250 { 251 char savec; 252 253 while (*--q == ' ') 254 continue; 255 while (isspace(*p)) 256 p++; 257 savec = *++q; 258 *q = '\0'; 259 if (*p != '\0') 260 define('x', newstr(p)); 261 *q = savec; 262 } 263 } 264 265 /* date message originated */ 266 p = hvalue("posted-date"); 267 if (p == NULL) 268 p = hvalue("date"); 269 if (p != NULL) 270 { 271 define('a', p); 272 /* we don't have a good way to do canonical conversion .... 273 define('d', newstr(arpatounix(p))); 274 .... so we will ignore the problem for the time being */ 275 } 276 277 if ((TempFile = fopen(InFileName, "r")) == NULL) 278 syserr("Cannot reopen %s", InFileName); 279 280 # ifdef DEBUG 281 if (Debug) 282 { 283 HDR *h; 284 extern char *capitalize(); 285 286 printf("----- collected header -----\n"); 287 for (h = Header; h != NULL; h = h->h_link) 288 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 289 printf("----------------------------\n"); 290 } 291 # endif DEBUG 292 return; 293 } 294 /* 295 ** EATFROM -- chew up a UNIX style from line and process 296 ** 297 ** This does indeed make some assumptions about the format 298 ** of UNIX messages. 299 ** 300 ** Parameters: 301 ** fm -- the from line. 302 ** 303 ** Returns: 304 ** none. 305 ** 306 ** Side Effects: 307 ** extracts what information it can from the header, 308 ** such as the date. 309 */ 310 311 # ifndef NOTUNIX 312 313 char *DowList[] = 314 { 315 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 316 }; 317 318 char *MonthList[] = 319 { 320 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 321 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 322 NULL 323 }; 324 325 eatfrom(fm) 326 char *fm; 327 { 328 register char *p; 329 register char **dt; 330 331 # ifdef DEBUG 332 if (Debug > 1) 333 printf("eatfrom(%s)\n", fm); 334 # endif DEBUG 335 336 /* find the date part */ 337 p = fm; 338 while (*p != '\0') 339 { 340 /* skip a word */ 341 while (*p != '\0' && *p != ' ') 342 *p++; 343 while (*p == ' ') 344 *p++; 345 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 346 continue; 347 348 /* we have a possible date */ 349 for (dt = DowList; *dt != NULL; dt++) 350 if (strncmp(*dt, p, 3) == 0) 351 break; 352 if (*dt == NULL) 353 continue; 354 355 for (dt = MonthList; *dt != NULL; dt++) 356 if (strncmp(*dt, &p[4], 3) == 0) 357 break; 358 if (*dt != NULL) 359 break; 360 } 361 362 if (*p != NULL) 363 { 364 char *q; 365 366 /* we have found a date */ 367 q = xalloc(25); 368 strncpy(q, p, 25); 369 q[24] = '\0'; 370 define('d', q); 371 } 372 } 373 374 # endif NOTUNIX 375 /* 376 ** PRIENCODE -- encode external priority names into internal values. 377 ** 378 ** Parameters: 379 ** p -- priority in ascii. 380 ** 381 ** Returns: 382 ** priority as a numeric level. 383 ** 384 ** Side Effects: 385 ** none. 386 */ 387 388 struct prio 389 { 390 char *pri_name; /* external name of priority */ 391 int pri_val; /* internal value for same */ 392 }; 393 394 static struct prio Prio[] = 395 { 396 "alert", PRI_ALERT, 397 "quick", PRI_QUICK, 398 "first-class", PRI_FIRSTCL, 399 "normal", PRI_NORMAL, 400 "second-class", PRI_SECONDCL, 401 "third-class", PRI_THIRDCL, 402 NULL, PRI_NORMAL, 403 }; 404 405 priencode(p) 406 char *p; 407 { 408 register struct prio *pl; 409 extern bool sameword(); 410 411 for (pl = Prio; pl->pri_name != NULL; pl++) 412 { 413 if (sameword(p, pl->pri_name)) 414 break; 415 } 416 return (pl->pri_val); 417 } 418