|
|
1.1 root 1: #include <stdio.h>
2: #include <ctype.h>
3: #include "mail.h"
4: #include "string.h"
5: #include "header.h"
6: #include "aux.h"
7:
8: /*
9: * Convert the rfc822 message on standard input into `UNIX' format
10: * and write it onto the passed FILE.
11: */
12:
13: /* header tags */
14: header hdrs[] = {
15: HEADER("Date:"),
16: HEADER("From:"),
17: HEADER("Sender:"),
18: HEADER("UnixFrom:"),
19: HEADER("UnixDate:"),
20: HEADER("Reply-To:"),
21: HEADER("")
22: };
23: #define datehdr hdrs[0]
24: #define fromhdr hdrs[1]
25: #define senderhdr hdrs[2]
26: #define unixfromhdr hdrs[3]
27: #define unixdatehdr hdrs[4]
28: #define replyhdr hdrs[5]
29:
30: /* imported */
31: extern int getheader();
32: extern string *getaddr();
33: extern int printheaders();
34: extern void printbodies();
35: extern char *lowercase();
36: extern char *convertaddr();
37:
38:
39: /* predeclared */
40: static char *getfrom();
41: static char *getdate();
42:
43: /* exported */
44: int extrafrom = 0;
45:
46: /*
47: * network name gets tacked onto the return addresses if addnet is set or if
48: * rfc822 routing is specified.
49: */
50: from822(net, fgetsp, fi, fo, defsender, helohost)
51: char *net;
52: char *(*fgetsp)();
53: FILE *fi; /* input file */
54: FILE *fo; /* output file */
55: char *defsender;
56: char *helohost;
57: {
58: char *from=NULL;
59: char *date;
60: char *basic;
61: char buf[4096];
62: int n;
63: extern int fputs();
64:
65: getheader(fgetsp, fi);
66:
67: /* Get sender's address.
68: */
69: if (unixfromhdr.line != NULL) {
70: from = basic = s_to_c(unixfromhdr.line);
71: } else if (defsender!=NULL && *defsender != '\0' && strcmp(defsender, "postmaster")!=0) {
72: from = getfrom(basic=defsender, net);
73: } else if (senderhdr.line != NULL) {
74: from = getfrom(basic=HCONTENT(senderhdr), net);
75: } else if (fromhdr.line != NULL) {
76: from = getfrom(basic=HCONTENT(fromhdr), net);
77: } else if (replyhdr.line != NULL) {
78: from = getfrom(basic=HCONTENT(replyhdr), net);
79: } else {
80: from = basic = "unknown";
81: }
82:
83:
84: /* Get date line, or make one up */
85: if(datehdr.line != NULL)
86: date = getdate(HCONTENT(datehdr));
87: else if(unixdatehdr.line != NULL)
88: date = s_to_c(unixdatehdr.line);
89: else
90: date = getdate((char *)0);
91:
92: /* output UNIX header */
93: if (from != NULL && *from != '\0' && date != '\0')
94: print_remote_header(fo, from, date, "");
95:
96: /* throw in a received line */
97: if(unixfromhdr.line == NULL)
98: printrcved(fo, helohost);
99:
100: /* output the rest */
101: if(printheaders(fputs, fo, 0))
102: fputs("\n", fo);
103: printbodies(fputs, fo);
104: while ((*fgetsp)(buf, sizeof(buf), fi)!=NULL)
105: fputs(buf, fo);
106: }
107:
108: /*
109: * Print out a received line
110: */
111: printrcved(fo, by)
112: FILE *fo;
113: char *by;
114: {
115: fprintf(fo, "Received: by %s; %s\n", by, getdate((char *)0));
116: }
117:
118: /*
119: * Return true if the two lines are the same modulo <>, whitespace,
120: * and newline.
121: */
122: sameaddr(line, addr)
123: char *line;
124: char *addr;
125: {
126: if (line==NULL)
127: return(1);
128: while (*line==' ' || *line=='\t' || *line=='<')
129: line++;
130: while (*addr==' ' || *addr=='\t' || *addr=='<')
131: addr++;
132: while(*addr && *addr!='>' && *addr!=' ' && *addr!='\t'){
133: if(*addr != *line)
134: break;
135: addr++;
136: line++;
137: }
138: while (*line=='>' || *line==' ' || *line=='\t')
139: line++;
140: while (*addr=='>' || *addr==' ' || *addr=='\t')
141: addr++;
142: while(*line)
143: if(*addr++ != *line++)
144: return(0);
145: return(1);
146: }
147:
148: /*
149: * The sender is either the next first whitespace delimited token or
150: * the first thing enclosed in "<" ">".
151: * Sets extrafrom > 0 if a from line with other cruft in it.
152: * Returns pointer to static area containing address converted to bang format.
153: */
154: static char *
155: getfrom(line, net)
156: char *line;
157: char *net;
158: {
159: register char *lp;
160: string *sender;
161:
162: sender = getaddr(line);
163: lp = convertaddr(s_to_c(sender));
164: if(net!=NULL){
165: s_reset(sender);
166: s_append(sender, net);
167: s_append(sender, "!");
168: s_append(sender, lp);
169: lp = s_to_c(sender);
170: }
171: return lp;
172: }
173:
174: /*
175: * Get a date line. Convert to `UNIX' format.
176: * use the current time instead if misparse, or if day omitted
177: * 822 standard: [Mon, ] 19 Oct 87 19:47:25 EDT
178: * (sometimes the , is omitted; sometimes (Mon) at end)
179: * Unix: Mon Oct 19 19:47:25 EDT 1987
180: *
181: */
182: static char *
183: getdate(line)
184: char *line;
185: {
186: register char *sp;
187: int i;
188: long t, time();
189: char *p, *nl, *ctime();
190: char *field[6];
191: static string *date=0;
192: static string *olddate=0;
193:
194: date = s_reset(date);
195: olddate = s_reset(olddate);
196: if (line) {
197: s_append(olddate, line);
198: sp = s_to_c(olddate);
199: for (i = 0; *sp && i < 6; i++) {
200: while (isspace(*sp) || *sp == ',' || *sp == '-')
201: *sp++ = '\0';
202: field[i] = sp;
203: while (!isspace(*sp) && *sp != ',' && *sp != '-'
204: && *sp != '\0')
205: sp++;
206: }
207: *sp = '\0';
208: if (i==6 && isalpha(*field[0])) {
209: s_append(date, field[0]);
210: s_putc(date, ' ');
211: s_append(date, field[2]);
212: s_putc(date, ' ');
213: if(strlen(field[1])==1)
214: s_putc(date, ' ');
215: s_append(date, field[1]);
216: s_putc(date, ' ');
217: s_append(date, field[4]);
218: s_putc(date, ' ');
219: for (p = field[5]; *p; p++)
220: if(islower(*p))
221: *p = toupper(*p);
222: s_append(date, field[5]);
223: s_putc(date, ' ');
224: if (field[3][2]=='\0')
225: s_append(date, "19");
226: s_append(date, field[3]);
227: }
228: }
229: s_terminate(date);
230: if (*s_to_c(date) == '\0') {
231: s_append(date, thedate());
232: }
233: return s_to_c(date);
234: }
235:
236: /* Return 1 if addr has only one bang in it */
237: int
238: onehop(addr)
239: char *addr;
240: {
241: register char *p;
242: register int cnt=0;
243:
244: for (p=addr; *p; )
245: if (*p++ == '!')
246: if (cnt++>0)
247: return 0;
248: return 1;
249: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.