1 # include <errno.h> 2 # include "sendmail.h" 3 4 static char SccsId[] = "@(#)collect.c 3.21 09/07/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 = hvalue("original-from"); 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 q = index(p, '('); 206 if (q != NULL) 207 { 208 int parenlev = 0; 209 210 for (p = q; *p != '\0'; p++) 211 { 212 if (*p == '(') 213 parenlev++; 214 else if (*p == ')' && --parenlev <= 0) 215 break; 216 } 217 if (*p == ')') 218 { 219 *p = '\0'; 220 if (*++q != '\0') 221 define('x', newstr(q)); 222 *p = ')'; 223 } 224 } 225 else if ((q = index(p, '<')) != NULL) 226 { 227 char savec; 228 229 while (*--q == ' ') 230 continue; 231 while (isspace(*p)) 232 p++; 233 savec = *++q; 234 *q = '\0'; 235 if (*p != '\0') 236 define('x', newstr(p)); 237 *q = savec; 238 } 239 } 240 241 /* date message originated */ 242 p = hvalue("posted-date"); 243 if (p == NULL) 244 p = hvalue("date"); 245 if (p != NULL) 246 { 247 define('a', p); 248 /* we don't have a good way to do canonical conversion .... 249 define('d', newstr(arpatounix(p))); 250 .... so we will ignore the problem for the time being */ 251 } 252 253 if ((TempFile = fopen(InFileName, "r")) == NULL) 254 syserr("Cannot reopen %s", InFileName); 255 256 # ifdef DEBUG 257 if (Debug) 258 { 259 HDR *h; 260 extern char *capitalize(); 261 262 printf("----- collected header -----\n"); 263 for (h = Header; h != NULL; h = h->h_link) 264 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 265 printf("----------------------------\n"); 266 } 267 # endif DEBUG 268 return; 269 } 270 /* 271 ** EATFROM -- chew up a UNIX style from line and process 272 ** 273 ** This does indeed make some assumptions about the format 274 ** of UNIX messages. 275 ** 276 ** Parameters: 277 ** fm -- the from line. 278 ** 279 ** Returns: 280 ** none. 281 ** 282 ** Side Effects: 283 ** extracts what information it can from the header, 284 ** such as the date. 285 */ 286 287 # ifndef NOTUNIX 288 289 char *DowList[] = 290 { 291 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 292 }; 293 294 char *MonthList[] = 295 { 296 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 297 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 298 NULL 299 }; 300 301 eatfrom(fm) 302 char *fm; 303 { 304 register char *p; 305 register char **dt; 306 307 # ifdef DEBUG 308 if (Debug > 1) 309 printf("eatfrom(%s)\n", fm); 310 # endif DEBUG 311 312 /* find the date part */ 313 p = fm; 314 while (*p != '\0') 315 { 316 /* skip a word */ 317 while (*p != '\0' && *p != ' ') 318 *p++; 319 while (*p == ' ') 320 *p++; 321 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 322 continue; 323 324 /* we have a possible date */ 325 for (dt = DowList; *dt != NULL; dt++) 326 if (strncmp(*dt, p, 3) == 0) 327 break; 328 if (*dt == NULL) 329 continue; 330 331 for (dt = MonthList; *dt != NULL; dt++) 332 if (strncmp(*dt, &p[4], 3) == 0) 333 break; 334 if (*dt != NULL) 335 break; 336 } 337 338 if (*p != NULL) 339 { 340 char *q; 341 342 /* we have found a date */ 343 q = xalloc(25); 344 strncpy(q, p, 25); 345 q[24] = '\0'; 346 define('d', q); 347 } 348 } 349 350 # endif NOTUNIX 351