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