1 # include <stdio.h> 2 # include <ctype.h> 3 # include <errno.h> 4 # include "postbox.h" 5 6 static char SccsId[] = "@(#)collect.c 3.2 03/07/81"; 7 8 /* 9 ** COLLECT -- read & parse message header & make temp file. 10 ** 11 ** Creates a temporary file name and copies the standard 12 ** input to that file. While it is doing it, it looks for 13 ** "From:" and "Sender:" fields to use as the from-person 14 ** (but only if the -a flag is specified). It prefers to 15 ** to use the "Sender:" field. 16 ** 17 ** MIT seems to like to produce "Sent-By:" fields instead 18 ** of "Sender:" fields. We used to catch this, but it turns 19 ** out that the "Sent-By:" field doesn't always correspond 20 ** to someone real ("___057", for instance), as required by 21 ** the protocol. So we limp by..... 22 ** 23 ** Parameters: 24 ** none 25 ** 26 ** Returns: 27 ** Name of temp file. 28 ** 29 ** Side Effects: 30 ** Temp file is created and filled. 31 ** 32 ** Called By: 33 ** main 34 ** 35 ** Notes: 36 ** This is broken off from main largely so that the 37 ** temp buffer can be deallocated. 38 */ 39 40 char *MsgId; /* message-id, determined or created */ 41 long MsgSize; /* size of message in bytes */ 42 char *Date; /* UNIX-style origination date */ 43 44 char * 45 collect() 46 { 47 register FILE *tf; 48 char buf[MAXFIELD+1]; 49 register char *p; 50 char c; 51 extern int errno; 52 register HDR *h; 53 HDR **hp; 54 extern bool isheader(); 55 extern char *newstr(); 56 extern char *xalloc(); 57 char *fname; 58 char *fvalue; 59 extern char *index(), *rindex(); 60 char *xfrom; 61 extern char *hvalue(); 62 extern char *makemsgid(); 63 struct hdrinfo *hi; 64 65 /* 66 ** Create the temp file name and create the file. 67 */ 68 69 mktemp(InFileName); 70 close(creat(InFileName, 0600)); 71 if ((tf = fopen(InFileName, "w")) == NULL) 72 { 73 syserr("Cannot create %s", InFileName); 74 return (NULL); 75 } 76 77 /* try to read a UNIX-style From line */ 78 if (fgets(buf, sizeof buf, stdin) == NULL) 79 return (NULL); 80 if (strncmp(buf, "From ", 5) == 0) 81 { 82 eatfrom(buf); 83 fgets(buf, sizeof buf, stdin); 84 } 85 86 /* 87 ** Copy stdin to temp file & do message editting. 88 ** To keep certain mailers from getting confused, 89 ** and to keep the output clean, lines that look 90 ** like UNIX "From" lines are deleted in the header, 91 ** and prepended with ">" in the body. 92 */ 93 94 for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin)) 95 { 96 /* see if the header is over */ 97 if (!isheader(buf)) 98 break; 99 100 /* get the rest of this field */ 101 while ((c = getc(stdin)) == ' ' || c == '\t') 102 { 103 p = &buf[strlen(buf)]; 104 *p++ = c; 105 if (fgets(p, sizeof buf - (p - buf), stdin) == NULL) 106 break; 107 } 108 if (c != EOF) 109 ungetc(c, stdin); 110 111 MsgSize += strlen(buf); 112 113 /* 114 ** Snarf header away. 115 */ 116 117 /* strip off trailing newline */ 118 p = rindex(buf, '\n'); 119 if (p != NULL) 120 *p = '\0'; 121 122 /* find canonical name */ 123 fname = buf; 124 p = index(buf, ':'); 125 fvalue = &p[1]; 126 while (isspace(*--p)) 127 continue; 128 *++p = '\0'; 129 makelower(fname); 130 131 /* strip field value on front */ 132 if (*fvalue == ' ') 133 fvalue++; 134 135 /* search header list for this header */ 136 for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link) 137 { 138 if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags)) 139 break; 140 } 141 if (h == NULL) 142 { 143 /* create a new node */ 144 *hp = h = (HDR *) xalloc(sizeof *h); 145 h->h_field = newstr(fname); 146 h->h_value = NULL; 147 h->h_link = NULL; 148 h->h_flags = 0; 149 150 /* see if it is a known type */ 151 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 152 { 153 if (strcmp(hi->hi_field, h->h_field) == 0) 154 { 155 h->h_flags = hi->hi_flags; 156 break; 157 } 158 } 159 } 160 else if (bitset(H_DEFAULT, h->h_flags)) 161 { 162 /* overriding default, throw out old value */ 163 free(h->h_value); 164 h->h_value = NULL; 165 } 166 167 /* do something with the value */ 168 if (h->h_value == NULL) 169 { 170 h->h_value = newstr(fvalue); 171 } 172 else 173 { 174 register int len; 175 176 /* concatenate the two values */ 177 len = strlen(h->h_value) + strlen(fvalue) + 2; 178 p = xalloc(len); 179 strcpy(p, h->h_value); 180 strcat(p, ","); 181 strcat(p, fvalue); 182 free(h->h_value); 183 h->h_value = p; 184 } 185 } 186 187 # ifdef DEBUG 188 if (Debug) 189 printf("EOH\n"); 190 # endif DEBUG 191 192 /* throw away a blank line */ 193 if (buf[0] == '\n') 194 fgets(buf, sizeof buf, stdin); 195 196 /* 197 ** Collect the body of the message. 198 */ 199 200 for (; !feof(stdin); !feof(stdin) && fgets(buf, sizeof buf, stdin) != NULL) 201 { 202 /* check for end-of-message */ 203 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 204 break; 205 206 /* Hide UNIX-like From lines */ 207 if (strncmp(buf, "From ", 5) == 0) 208 { 209 fputs(">", tf); 210 MsgSize++; 211 } 212 MsgSize += strlen(buf); 213 fputs(buf, tf); 214 if (ferror(tf)) 215 { 216 if (errno == ENOSPC) 217 { 218 freopen(InFileName, "w", tf); 219 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 220 syserr("Out of disk space for temp file"); 221 } 222 else 223 syserr("Cannot write %s", InFileName); 224 freopen("/dev/null", "w", tf); 225 } 226 } 227 fclose(tf); 228 229 /* 230 ** Find out some information from the headers. 231 ** Examples are who is the from person, the date, the 232 ** message-id, etc. 233 */ 234 235 /* from person */ 236 xfrom = hvalue("sender"); 237 if (xfrom == NULL) 238 xfrom = hvalue("from"); 239 240 /* date message originated */ 241 /* we don't seem to have a good way to do canonical conversion .... 242 p = hvalue("date"); 243 if (p != NULL) 244 Date = newstr(arpatounix(p)); 245 .... so we will ignore the problem for the time being */ 246 if (Date == NULL) 247 { 248 auto long t; 249 extern char *ctime(); 250 251 time(&t); 252 Date = newstr(ctime(&t)); 253 } 254 255 /* message id */ 256 MsgId = hvalue("message-id"); 257 if (MsgId == NULL) 258 MsgId = makemsgid(); 259 260 if (freopen(InFileName, "r", stdin) == NULL) 261 syserr("Cannot reopen %s", InFileName); 262 263 # ifdef DEBUG 264 if (Debug) 265 { 266 printf("----- collected header -----\n"); 267 for (h = Header; h != NULL; h = h->h_link) 268 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 269 printf("----------------------------\n"); 270 } 271 # endif DEBUG 272 return (ArpaFmt ? xfrom : NULL); 273 } 274 /* 275 ** EATFROM -- chew up a UNIX style from line and process 276 ** 277 ** This does indeed make some assumptions about the format 278 ** of UNIX messages. 279 ** 280 ** Parameters: 281 ** fm -- the from line. 282 ** 283 ** Returns: 284 ** none. 285 ** 286 ** Side Effects: 287 ** extracts what information it can from the header, 288 ** such as the Date. 289 */ 290 291 char *MonthList[] = 292 { 293 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 294 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 295 NULL 296 }; 297 298 eatfrom(fm) 299 char *fm; 300 { 301 register char *p; 302 register char **dt; 303 304 /* find the date part */ 305 p = fm; 306 while (*p != '\0') 307 { 308 /* skip a word */ 309 while (*p != '\0' && *p != ' ') 310 *p++; 311 while (*p == ' ') 312 *p++; 313 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 314 continue; 315 316 /* we have a possible date */ 317 for (dt = MonthList; *dt != NULL; dt++) 318 if (strncmp(*dt, p, 3) == 0) 319 break; 320 321 if (*dt != NULL) 322 break; 323 } 324 325 if (*p != NULL) 326 { 327 /* we have found a date */ 328 Date = xalloc(25); 329 strncpy(Date, p, 25); 330 Date[24] = '\0'; 331 } 332 } 333 /* 334 ** HVALUE -- return value of a header. 335 ** 336 ** Parameters: 337 ** field -- the field name. 338 ** 339 ** Returns: 340 ** pointer to the value part. 341 ** NULL if not found. 342 ** 343 ** Side Effects: 344 ** sets the H_USED bit in the header if found. 345 */ 346 347 char * 348 hvalue(field) 349 char *field; 350 { 351 register HDR *h; 352 353 for (h = Header; h != NULL; h = h->h_link) 354 { 355 if (strcmp(h->h_field, field) == 0) 356 { 357 h->h_flags |= H_USED; 358 return (h->h_value); 359 } 360 } 361 return (NULL); 362 } 363 /* 364 ** MAKEMSGID -- Compute a message id for this process. 365 ** 366 ** This routine creates a message id for a message if 367 ** it did not have one already. If the MESSAGEID compile 368 ** flag is set, the messageid will be added to any message 369 ** that does not already have one. Currently it is more 370 ** of an artifact, but I suggest that if you are hacking, 371 ** you leave it in -- I may want to use it someday if 372 ** duplicate messages turn out to be a problem. 373 ** 374 ** Parameters: 375 ** none. 376 ** 377 ** Returns: 378 ** a message id. 379 ** 380 ** Side Effects: 381 ** none. 382 */ 383 384 char * 385 makemsgid() 386 { 387 auto long t; 388 extern char *MyLocName; 389 extern char *ArpaHost; 390 static char buf[50]; 391 392 time(&t); 393 sprintf(buf, "<%ld.%d.%s@%s>", t, getpid(), MyLocName, ArpaHost); 394 return (buf); 395 } 396 /* 397 ** ISHEADER -- predicate telling if argument is a header. 398 ** 399 ** Parameters: 400 ** s -- string to check for possible headerness. 401 ** 402 ** Returns: 403 ** TRUE if s is a header. 404 ** FALSE otherwise. 405 ** 406 ** Side Effects: 407 ** none. 408 */ 409 410 bool 411 isheader(s) 412 register char *s; 413 { 414 if (!isalnum(*s)) 415 return (FALSE); 416 while (!isspace(*s) && *s != ':') 417 s++; 418 while (isspace(*s)) 419 s++; 420 return (*s == ':'); 421 } 422