Blame


1 9f1fdc12 2005-10-29 devnull #include <u.h>
2 9f1fdc12 2005-10-29 devnull #include <libc.h>
3 9f1fdc12 2005-10-29 devnull #include <bio.h>
4 9f1fdc12 2005-10-29 devnull #include <thread.h>
5 9f1fdc12 2005-10-29 devnull #include <ctype.h>
6 9f1fdc12 2005-10-29 devnull #include <plumb.h>
7 9f1fdc12 2005-10-29 devnull #include <9pclient.h>
8 9f1fdc12 2005-10-29 devnull #include "dat.h"
9 9f1fdc12 2005-10-29 devnull
10 9f1fdc12 2005-10-29 devnull static int replyid;
11 9f1fdc12 2005-10-29 devnull
12 9f1fdc12 2005-10-29 devnull int
13 9f1fdc12 2005-10-29 devnull quote(Message *m, CFid *fid, char *dir, char *quotetext)
14 9f1fdc12 2005-10-29 devnull {
15 9f1fdc12 2005-10-29 devnull char *body, *type;
16 9f1fdc12 2005-10-29 devnull int i, n, nlines;
17 9f1fdc12 2005-10-29 devnull char **lines;
18 9f1fdc12 2005-10-29 devnull
19 9f1fdc12 2005-10-29 devnull if(quotetext){
20 9f1fdc12 2005-10-29 devnull body = quotetext;
21 9f1fdc12 2005-10-29 devnull n = strlen(body);
22 9f1fdc12 2005-10-29 devnull type = nil;
23 9f1fdc12 2005-10-29 devnull }else{
24 9f1fdc12 2005-10-29 devnull /* look for first textual component to quote */
25 9f1fdc12 2005-10-29 devnull type = readfile(dir, "type", &n);
26 9f1fdc12 2005-10-29 devnull if(type == nil){
27 9f1fdc12 2005-10-29 devnull print("no type in %s\n", dir);
28 9f1fdc12 2005-10-29 devnull return 0;
29 9f1fdc12 2005-10-29 devnull }
30 9f1fdc12 2005-10-29 devnull if(strncmp(type, "multipart/", 10)==0 || strncmp(type, "message/", 8)==0){
31 9f1fdc12 2005-10-29 devnull dir = estrstrdup(dir, "1/");
32 9f1fdc12 2005-10-29 devnull if(quote(m, fid, dir, nil)){
33 9f1fdc12 2005-10-29 devnull free(type);
34 9f1fdc12 2005-10-29 devnull free(dir);
35 9f1fdc12 2005-10-29 devnull return 1;
36 9f1fdc12 2005-10-29 devnull }
37 9f1fdc12 2005-10-29 devnull free(dir);
38 9f1fdc12 2005-10-29 devnull }
39 9f1fdc12 2005-10-29 devnull if(strncmp(type, "text", 4) != 0){
40 9f1fdc12 2005-10-29 devnull free(type);
41 9f1fdc12 2005-10-29 devnull return 0;
42 9f1fdc12 2005-10-29 devnull }
43 9f1fdc12 2005-10-29 devnull body = readbody(m->type, dir, &n);
44 9f1fdc12 2005-10-29 devnull if(body == nil)
45 9f1fdc12 2005-10-29 devnull return 0;
46 9f1fdc12 2005-10-29 devnull }
47 9f1fdc12 2005-10-29 devnull nlines = 0;
48 9f1fdc12 2005-10-29 devnull for(i=0; i<n; i++)
49 9f1fdc12 2005-10-29 devnull if(body[i] == '\n')
50 9f1fdc12 2005-10-29 devnull nlines++;
51 9f1fdc12 2005-10-29 devnull nlines++;
52 9f1fdc12 2005-10-29 devnull lines = emalloc(nlines*sizeof(char*));
53 9f1fdc12 2005-10-29 devnull nlines = getfields(body, lines, nlines, 0, "\n");
54 9f1fdc12 2005-10-29 devnull /* delete leading and trailing blank lines */
55 9f1fdc12 2005-10-29 devnull i = 0;
56 9f1fdc12 2005-10-29 devnull while(i<nlines && lines[i][0]=='\0')
57 9f1fdc12 2005-10-29 devnull i++;
58 9f1fdc12 2005-10-29 devnull while(i<nlines && lines[nlines-1][0]=='\0')
59 9f1fdc12 2005-10-29 devnull nlines--;
60 9f1fdc12 2005-10-29 devnull while(i < nlines){
61 9f1fdc12 2005-10-29 devnull fsprint(fid, ">%s%s\n", lines[i][0]=='>'? "" : " ", lines[i]);
62 9f1fdc12 2005-10-29 devnull i++;
63 9f1fdc12 2005-10-29 devnull }
64 9f1fdc12 2005-10-29 devnull free(lines);
65 9f1fdc12 2005-10-29 devnull free(body); /* will free quotetext if non-nil */
66 9f1fdc12 2005-10-29 devnull free(type);
67 9f1fdc12 2005-10-29 devnull return 1;
68 9f1fdc12 2005-10-29 devnull }
69 9f1fdc12 2005-10-29 devnull
70 9f1fdc12 2005-10-29 devnull void
71 9f1fdc12 2005-10-29 devnull mkreply(Message *m, char *label, char *to, Plumbattr *attr, char *quotetext)
72 9f1fdc12 2005-10-29 devnull {
73 9f1fdc12 2005-10-29 devnull Message *r;
74 9f1fdc12 2005-10-29 devnull char *dir, *t;
75 9f1fdc12 2005-10-29 devnull int quotereply;
76 9f1fdc12 2005-10-29 devnull Plumbattr *a;
77 9f1fdc12 2005-10-29 devnull
78 9f1fdc12 2005-10-29 devnull quotereply = (label[0] == 'Q');
79 9f1fdc12 2005-10-29 devnull r = emalloc(sizeof(Message));
80 9f1fdc12 2005-10-29 devnull r->isreply = 1;
81 9f1fdc12 2005-10-29 devnull if(m != nil)
82 9f1fdc12 2005-10-29 devnull r->replyname = estrdup(m->name);
83 9f1fdc12 2005-10-29 devnull r->next = replies.head;
84 9f1fdc12 2005-10-29 devnull r->prev = nil;
85 9f1fdc12 2005-10-29 devnull if(replies.head != nil)
86 9f1fdc12 2005-10-29 devnull replies.head->prev = r;
87 9f1fdc12 2005-10-29 devnull replies.head = r;
88 9f1fdc12 2005-10-29 devnull if(replies.tail == nil)
89 9f1fdc12 2005-10-29 devnull replies.tail = r;
90 9f1fdc12 2005-10-29 devnull r->name = emalloc(strlen(mbox.name)+strlen(label)+10);
91 9f1fdc12 2005-10-29 devnull sprint(r->name, "%s%s%d", mbox.name, label, ++replyid);
92 9f1fdc12 2005-10-29 devnull r->w = newwindow();
93 9f1fdc12 2005-10-29 devnull winname(r->w, r->name);
94 9f1fdc12 2005-10-29 devnull ctlprint(r->w->ctl, "cleartag");
95 9f1fdc12 2005-10-29 devnull wintagwrite(r->w, "fmt Look Post Undo", 4+5+5+4);
96 9f1fdc12 2005-10-29 devnull r->tagposted = 1;
97 9f1fdc12 2005-10-29 devnull threadcreate(mesgctl, r, STACK);
98 9f1fdc12 2005-10-29 devnull winopenbody(r->w, OWRITE);
99 3f8c36d6 2006-02-09 devnull if(to!=nil && to[0]!='\0')
100 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "%s\n", to);
101 3f8c36d6 2006-02-09 devnull for(a=attr; a; a=a->next)
102 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "%s: %s\n", a->name, a->value);
103 9f1fdc12 2005-10-29 devnull dir = nil;
104 9f1fdc12 2005-10-29 devnull if(m != nil){
105 9f1fdc12 2005-10-29 devnull dir = estrstrdup(mbox.name, m->name);
106 9f1fdc12 2005-10-29 devnull if(to == nil && attr == nil){
107 9f1fdc12 2005-10-29 devnull /* Reply goes to replyto; Reply all goes to From and To and CC */
108 3f8c36d6 2006-02-09 devnull if(strstr(label, "all") == nil)
109 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "To: %s\n", m->replyto);
110 9f1fdc12 2005-10-29 devnull else{ /* Replyall */
111 3f8c36d6 2006-02-09 devnull if(strlen(m->from) > 0)
112 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "To: %s\n", m->from);
113 3f8c36d6 2006-02-09 devnull if(strlen(m->to) > 0)
114 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "To: %s\n", m->to);
115 3f8c36d6 2006-02-09 devnull if(strlen(m->cc) > 0)
116 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "CC: %s\n", m->cc);
117 9f1fdc12 2005-10-29 devnull }
118 9f1fdc12 2005-10-29 devnull }
119 9f1fdc12 2005-10-29 devnull if(strlen(m->subject) > 0){
120 9f1fdc12 2005-10-29 devnull t = "Subject: Re: ";
121 9f1fdc12 2005-10-29 devnull if(strlen(m->subject) >= 3)
122 9f1fdc12 2005-10-29 devnull if(tolower(m->subject[0])=='r' && tolower(m->subject[1])=='e' && m->subject[2]==':')
123 9f1fdc12 2005-10-29 devnull t = "Subject: ";
124 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "%s%s\n", t, m->subject);
125 9f1fdc12 2005-10-29 devnull }
126 9f1fdc12 2005-10-29 devnull if(!quotereply){
127 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "Include: %sraw\n", dir);
128 9f1fdc12 2005-10-29 devnull free(dir);
129 9f1fdc12 2005-10-29 devnull }
130 9f1fdc12 2005-10-29 devnull }
131 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "\n");
132 3f8c36d6 2006-02-09 devnull if(m == nil)
133 9f1fdc12 2005-10-29 devnull fsprint(r->w->body, "\n");
134 9f1fdc12 2005-10-29 devnull else if(quotereply){
135 9f1fdc12 2005-10-29 devnull quote(m, r->w->body, dir, quotetext);
136 9f1fdc12 2005-10-29 devnull free(dir);
137 9f1fdc12 2005-10-29 devnull }
138 9f1fdc12 2005-10-29 devnull winclosebody(r->w);
139 9f1fdc12 2005-10-29 devnull if(m==nil && (to==nil || to[0]=='\0'))
140 9f1fdc12 2005-10-29 devnull winselect(r->w, "0", 0);
141 9f1fdc12 2005-10-29 devnull else
142 9f1fdc12 2005-10-29 devnull winselect(r->w, "$", 0);
143 9f1fdc12 2005-10-29 devnull winclean(r->w);
144 9f1fdc12 2005-10-29 devnull windormant(r->w);
145 9f1fdc12 2005-10-29 devnull }
146 9f1fdc12 2005-10-29 devnull
147 9f1fdc12 2005-10-29 devnull void
148 9f1fdc12 2005-10-29 devnull delreply(Message *m)
149 9f1fdc12 2005-10-29 devnull {
150 9f1fdc12 2005-10-29 devnull if(m->next == nil)
151 9f1fdc12 2005-10-29 devnull replies.tail = m->prev;
152 9f1fdc12 2005-10-29 devnull else
153 9f1fdc12 2005-10-29 devnull m->next->prev = m->prev;
154 9f1fdc12 2005-10-29 devnull if(m->prev == nil)
155 9f1fdc12 2005-10-29 devnull replies.head = m->next;
156 9f1fdc12 2005-10-29 devnull else
157 9f1fdc12 2005-10-29 devnull m->prev->next = m->next;
158 9f1fdc12 2005-10-29 devnull mesgfreeparts(m);
159 9f1fdc12 2005-10-29 devnull free(m);
160 9f1fdc12 2005-10-29 devnull }
161 9f1fdc12 2005-10-29 devnull
162 9f1fdc12 2005-10-29 devnull /* copy argv to stack and free the incoming strings, so we don't leak argument vectors */
163 9f1fdc12 2005-10-29 devnull void
164 9f1fdc12 2005-10-29 devnull buildargv(char **inargv, char *argv[NARGS+1], char args[NARGCHAR])
165 9f1fdc12 2005-10-29 devnull {
166 9f1fdc12 2005-10-29 devnull int i, n;
167 9f1fdc12 2005-10-29 devnull char *s, *a;
168 9f1fdc12 2005-10-29 devnull
169 9f1fdc12 2005-10-29 devnull s = args;
170 9f1fdc12 2005-10-29 devnull for(i=0; i<NARGS; i++){
171 9f1fdc12 2005-10-29 devnull a = inargv[i];
172 9f1fdc12 2005-10-29 devnull if(a == nil)
173 9f1fdc12 2005-10-29 devnull break;
174 9f1fdc12 2005-10-29 devnull n = strlen(a)+1;
175 9f1fdc12 2005-10-29 devnull if((s-args)+n >= NARGCHAR) /* too many characters */
176 9f1fdc12 2005-10-29 devnull break;
177 9f1fdc12 2005-10-29 devnull argv[i] = s;
178 9f1fdc12 2005-10-29 devnull memmove(s, a, n);
179 9f1fdc12 2005-10-29 devnull s += n;
180 9f1fdc12 2005-10-29 devnull free(a);
181 9f1fdc12 2005-10-29 devnull }
182 9f1fdc12 2005-10-29 devnull argv[i] = nil;
183 9f1fdc12 2005-10-29 devnull }
184 9f1fdc12 2005-10-29 devnull
185 9f1fdc12 2005-10-29 devnull void
186 9f1fdc12 2005-10-29 devnull execproc(void *v)
187 9f1fdc12 2005-10-29 devnull {
188 9f1fdc12 2005-10-29 devnull struct Exec *e;
189 9f1fdc12 2005-10-29 devnull int p[2], q[2];
190 9f1fdc12 2005-10-29 devnull char *prog;
191 9f1fdc12 2005-10-29 devnull char *argv[NARGS+1], args[NARGCHAR];
192 9f1fdc12 2005-10-29 devnull int fd[3];
193 9f1fdc12 2005-10-29 devnull
194 9f1fdc12 2005-10-29 devnull e = v;
195 9f1fdc12 2005-10-29 devnull p[0] = e->p[0];
196 9f1fdc12 2005-10-29 devnull p[1] = e->p[1];
197 9f1fdc12 2005-10-29 devnull q[0] = e->q[0];
198 9f1fdc12 2005-10-29 devnull q[1] = e->q[1];
199 9f1fdc12 2005-10-29 devnull prog = e->prog; /* known not to be malloc'ed */
200 3f8c36d6 2006-02-09 devnull
201 3f8c36d6 2006-02-09 devnull fd[0] = dup(p[0], -1);
202 3f8c36d6 2006-02-09 devnull if(q[0])
203 3f8c36d6 2006-02-09 devnull fd[1] = dup(q[1], -1);
204 3f8c36d6 2006-02-09 devnull else
205 3f8c36d6 2006-02-09 devnull fd[1] = dup(1, -1);
206 3f8c36d6 2006-02-09 devnull fd[2] = dup(2, -2);
207 9f1fdc12 2005-10-29 devnull sendul(e->sync, 1);
208 9f1fdc12 2005-10-29 devnull buildargv(e->argv, argv, args);
209 9f1fdc12 2005-10-29 devnull free(e->argv);
210 9f1fdc12 2005-10-29 devnull chanfree(e->sync);
211 9f1fdc12 2005-10-29 devnull free(e);
212 3f8c36d6 2006-02-09 devnull
213 9f1fdc12 2005-10-29 devnull threadexec(nil, fd, prog, argv);
214 9f1fdc12 2005-10-29 devnull close(fd[0]);
215 9f1fdc12 2005-10-29 devnull close(fd[1]);
216 3f8c36d6 2006-02-09 devnull close(fd[2]);
217 3f8c36d6 2006-02-09 devnull
218 9f1fdc12 2005-10-29 devnull fprint(2, "Mail: can't exec %s: %r\n", prog);
219 9f1fdc12 2005-10-29 devnull threadexits("can't exec");
220 9f1fdc12 2005-10-29 devnull }
221 9f1fdc12 2005-10-29 devnull
222 9f1fdc12 2005-10-29 devnull enum{
223 9f1fdc12 2005-10-29 devnull ATTACH,
224 9f1fdc12 2005-10-29 devnull BCC,
225 9f1fdc12 2005-10-29 devnull CC,
226 9f1fdc12 2005-10-29 devnull FROM,
227 9f1fdc12 2005-10-29 devnull INCLUDE,
228 9f1fdc12 2005-10-29 devnull TO,
229 9f1fdc12 2005-10-29 devnull };
230 9f1fdc12 2005-10-29 devnull
231 9f1fdc12 2005-10-29 devnull char *headers[] = {
232 9f1fdc12 2005-10-29 devnull "attach:",
233 9f1fdc12 2005-10-29 devnull "bcc:",
234 9f1fdc12 2005-10-29 devnull "cc:",
235 9f1fdc12 2005-10-29 devnull "from:",
236 9f1fdc12 2005-10-29 devnull "include:",
237 9f1fdc12 2005-10-29 devnull "to:",
238 9f1fdc12 2005-10-29 devnull nil,
239 9f1fdc12 2005-10-29 devnull };
240 9f1fdc12 2005-10-29 devnull
241 9f1fdc12 2005-10-29 devnull int
242 9f1fdc12 2005-10-29 devnull whichheader(char *h)
243 9f1fdc12 2005-10-29 devnull {
244 9f1fdc12 2005-10-29 devnull int i;
245 9f1fdc12 2005-10-29 devnull
246 9f1fdc12 2005-10-29 devnull for(i=0; headers[i]!=nil; i++)
247 9f1fdc12 2005-10-29 devnull if(cistrcmp(h, headers[i]) == 0)
248 9f1fdc12 2005-10-29 devnull return i;
249 9f1fdc12 2005-10-29 devnull return -1;
250 9f1fdc12 2005-10-29 devnull }
251 9f1fdc12 2005-10-29 devnull
252 9f1fdc12 2005-10-29 devnull char *tolist[200];
253 9f1fdc12 2005-10-29 devnull char *cclist[200];
254 9f1fdc12 2005-10-29 devnull char *bcclist[200];
255 9f1fdc12 2005-10-29 devnull int ncc, nbcc, nto;
256 9f1fdc12 2005-10-29 devnull char *attlist[200];
257 9f1fdc12 2005-10-29 devnull char included[200];
258 9f1fdc12 2005-10-29 devnull
259 9f1fdc12 2005-10-29 devnull int
260 9f1fdc12 2005-10-29 devnull addressed(char *name)
261 9f1fdc12 2005-10-29 devnull {
262 9f1fdc12 2005-10-29 devnull int i;
263 9f1fdc12 2005-10-29 devnull
264 9f1fdc12 2005-10-29 devnull for(i=0; i<nto; i++)
265 9f1fdc12 2005-10-29 devnull if(strcmp(name, tolist[i]) == 0)
266 9f1fdc12 2005-10-29 devnull return 1;
267 9f1fdc12 2005-10-29 devnull for(i=0; i<ncc; i++)
268 9f1fdc12 2005-10-29 devnull if(strcmp(name, cclist[i]) == 0)
269 9f1fdc12 2005-10-29 devnull return 1;
270 9f1fdc12 2005-10-29 devnull for(i=0; i<nbcc; i++)
271 9f1fdc12 2005-10-29 devnull if(strcmp(name, bcclist[i]) == 0)
272 9f1fdc12 2005-10-29 devnull return 1;
273 9f1fdc12 2005-10-29 devnull return 0;
274 9f1fdc12 2005-10-29 devnull }
275 9f1fdc12 2005-10-29 devnull
276 9f1fdc12 2005-10-29 devnull char*
277 9f1fdc12 2005-10-29 devnull skipbl(char *s, char *e)
278 9f1fdc12 2005-10-29 devnull {
279 9f1fdc12 2005-10-29 devnull while(s < e){
280 9f1fdc12 2005-10-29 devnull if(*s!=' ' && *s!='\t' && *s!=',')
281 9f1fdc12 2005-10-29 devnull break;
282 9f1fdc12 2005-10-29 devnull s++;
283 9f1fdc12 2005-10-29 devnull }
284 9f1fdc12 2005-10-29 devnull return s;
285 9f1fdc12 2005-10-29 devnull }
286 9f1fdc12 2005-10-29 devnull
287 9f1fdc12 2005-10-29 devnull char*
288 9f1fdc12 2005-10-29 devnull findbl(char *s, char *e)
289 9f1fdc12 2005-10-29 devnull {
290 9f1fdc12 2005-10-29 devnull while(s < e){
291 9f1fdc12 2005-10-29 devnull if(*s==' ' || *s=='\t' || *s==',')
292 9f1fdc12 2005-10-29 devnull break;
293 9f1fdc12 2005-10-29 devnull s++;
294 9f1fdc12 2005-10-29 devnull }
295 9f1fdc12 2005-10-29 devnull return s;
296 9f1fdc12 2005-10-29 devnull }
297 9f1fdc12 2005-10-29 devnull
298 9f1fdc12 2005-10-29 devnull /*
299 9f1fdc12 2005-10-29 devnull * comma-separate possibly blank-separated strings in line; e points before newline
300 9f1fdc12 2005-10-29 devnull */
301 9f1fdc12 2005-10-29 devnull void
302 9f1fdc12 2005-10-29 devnull commas(char *s, char *e)
303 9f1fdc12 2005-10-29 devnull {
304 9f1fdc12 2005-10-29 devnull char *t;
305 9f1fdc12 2005-10-29 devnull
306 9f1fdc12 2005-10-29 devnull /* may have initial blanks */
307 9f1fdc12 2005-10-29 devnull s = skipbl(s, e);
308 9f1fdc12 2005-10-29 devnull while(s < e){
309 9f1fdc12 2005-10-29 devnull s = findbl(s, e);
310 9f1fdc12 2005-10-29 devnull if(s == e)
311 9f1fdc12 2005-10-29 devnull break;
312 9f1fdc12 2005-10-29 devnull t = skipbl(s, e);
313 9f1fdc12 2005-10-29 devnull if(t == e) /* no more words */
314 9f1fdc12 2005-10-29 devnull break;
315 9f1fdc12 2005-10-29 devnull /* patch comma */
316 9f1fdc12 2005-10-29 devnull *s++ = ',';
317 9f1fdc12 2005-10-29 devnull while(s < t)
318 9f1fdc12 2005-10-29 devnull *s++ = ' ';
319 9f1fdc12 2005-10-29 devnull }
320 9f1fdc12 2005-10-29 devnull }
321 9f1fdc12 2005-10-29 devnull
322 9f1fdc12 2005-10-29 devnull int
323 9f1fdc12 2005-10-29 devnull print2(int fd, int ofd, char *fmt, ...)
324 9f1fdc12 2005-10-29 devnull {
325 9f1fdc12 2005-10-29 devnull int m, n;
326 9f1fdc12 2005-10-29 devnull char *s;
327 9f1fdc12 2005-10-29 devnull va_list arg;
328 9f1fdc12 2005-10-29 devnull
329 9f1fdc12 2005-10-29 devnull va_start(arg, fmt);
330 9f1fdc12 2005-10-29 devnull s = vsmprint(fmt, arg);
331 9f1fdc12 2005-10-29 devnull va_end(arg);
332 9f1fdc12 2005-10-29 devnull if(s == nil)
333 9f1fdc12 2005-10-29 devnull return -1;
334 9f1fdc12 2005-10-29 devnull m = strlen(s);
335 9f1fdc12 2005-10-29 devnull n = write(fd, s, m);
336 9f1fdc12 2005-10-29 devnull if(ofd > 0)
337 9f1fdc12 2005-10-29 devnull write(ofd, s, m);
338 9f1fdc12 2005-10-29 devnull return n;
339 9f1fdc12 2005-10-29 devnull }
340 9f1fdc12 2005-10-29 devnull
341 9f1fdc12 2005-10-29 devnull void
342 9f1fdc12 2005-10-29 devnull write2(int fd, int ofd, char *buf, int n, int nofrom)
343 9f1fdc12 2005-10-29 devnull {
344 9f1fdc12 2005-10-29 devnull char *from, *p;
345 9f1fdc12 2005-10-29 devnull int m;
346 9f1fdc12 2005-10-29 devnull
347 9f1fdc12 2005-10-29 devnull write(fd, buf, n);
348 9f1fdc12 2005-10-29 devnull
349 9f1fdc12 2005-10-29 devnull if(ofd <= 0)
350 9f1fdc12 2005-10-29 devnull return;
351 9f1fdc12 2005-10-29 devnull
352 9f1fdc12 2005-10-29 devnull if(nofrom == 0){
353 9f1fdc12 2005-10-29 devnull write(ofd, buf, n);
354 9f1fdc12 2005-10-29 devnull return;
355 9f1fdc12 2005-10-29 devnull }
356 9f1fdc12 2005-10-29 devnull
357 9f1fdc12 2005-10-29 devnull /* need to escape leading From lines to avoid corrupting 'outgoing' mailbox */
358 9f1fdc12 2005-10-29 devnull for(p=buf; *p; p+=m){
359 9f1fdc12 2005-10-29 devnull from = cistrstr(p, "from");
360 9f1fdc12 2005-10-29 devnull if(from == nil)
361 9f1fdc12 2005-10-29 devnull m = n;
362 9f1fdc12 2005-10-29 devnull else
363 9f1fdc12 2005-10-29 devnull m = from - p;
364 9f1fdc12 2005-10-29 devnull if(m > 0)
365 9f1fdc12 2005-10-29 devnull write(ofd, p, m);
366 9f1fdc12 2005-10-29 devnull if(from){
367 9f1fdc12 2005-10-29 devnull if(p==buf || from[-1]=='\n')
368 9f1fdc12 2005-10-29 devnull write(ofd, " ", 1); /* escape with space if From is at start of line */
369 9f1fdc12 2005-10-29 devnull write(ofd, from, 4);
370 9f1fdc12 2005-10-29 devnull m += 4;
371 9f1fdc12 2005-10-29 devnull }
372 9f1fdc12 2005-10-29 devnull n -= m;
373 9f1fdc12 2005-10-29 devnull }
374 9f1fdc12 2005-10-29 devnull }
375 9f1fdc12 2005-10-29 devnull
376 9f1fdc12 2005-10-29 devnull void
377 9f1fdc12 2005-10-29 devnull mesgsend(Message *m)
378 9f1fdc12 2005-10-29 devnull {
379 9f1fdc12 2005-10-29 devnull char *s, *body, *to;
380 9f1fdc12 2005-10-29 devnull int i, j, h, n, natt, p[2];
381 9f1fdc12 2005-10-29 devnull struct Exec *e;
382 9f1fdc12 2005-10-29 devnull Channel *sync;
383 9f1fdc12 2005-10-29 devnull int first, nfld, delit, ofd;
384 9f1fdc12 2005-10-29 devnull char *copy, *fld[100], *now;
385 9f1fdc12 2005-10-29 devnull
386 9f1fdc12 2005-10-29 devnull body = winreadbody(m->w, &n);
387 9f1fdc12 2005-10-29 devnull /* assemble to: list from first line, to: line, and cc: line */
388 9f1fdc12 2005-10-29 devnull nto = 0;
389 9f1fdc12 2005-10-29 devnull natt = 0;
390 9f1fdc12 2005-10-29 devnull ncc = 0;
391 9f1fdc12 2005-10-29 devnull nbcc = 0;
392 9f1fdc12 2005-10-29 devnull first = 1;
393 9f1fdc12 2005-10-29 devnull to = body;
394 9f1fdc12 2005-10-29 devnull for(;;){
395 9f1fdc12 2005-10-29 devnull for(s=to; *s!='\n'; s++)
396 9f1fdc12 2005-10-29 devnull if(*s == '\0'){
397 9f1fdc12 2005-10-29 devnull free(body);
398 9f1fdc12 2005-10-29 devnull return;
399 9f1fdc12 2005-10-29 devnull }
400 9f1fdc12 2005-10-29 devnull if(s++ == to) /* blank line */
401 9f1fdc12 2005-10-29 devnull break;
402 9f1fdc12 2005-10-29 devnull /* make copy of line to tokenize */
403 9f1fdc12 2005-10-29 devnull copy = emalloc(s-to);
404 9f1fdc12 2005-10-29 devnull memmove(copy, to, s-to);
405 9f1fdc12 2005-10-29 devnull copy[s-to-1] = '\0';
406 9f1fdc12 2005-10-29 devnull nfld = tokenizec(copy, fld, nelem(fld), ", \t");
407 9f1fdc12 2005-10-29 devnull if(nfld == 0){
408 9f1fdc12 2005-10-29 devnull free(copy);
409 9f1fdc12 2005-10-29 devnull break;
410 9f1fdc12 2005-10-29 devnull }
411 9f1fdc12 2005-10-29 devnull n -= s-to;
412 9f1fdc12 2005-10-29 devnull switch(h = whichheader(fld[0])){
413 9f1fdc12 2005-10-29 devnull case TO:
414 9f1fdc12 2005-10-29 devnull case FROM:
415 9f1fdc12 2005-10-29 devnull delit = 1;
416 9f1fdc12 2005-10-29 devnull commas(to+strlen(fld[0]), s-1);
417 9f1fdc12 2005-10-29 devnull for(i=1; i<nfld && nto<nelem(tolist); i++)
418 9f1fdc12 2005-10-29 devnull if(!addressed(fld[i]))
419 9f1fdc12 2005-10-29 devnull tolist[nto++] = estrdup(fld[i]);
420 9f1fdc12 2005-10-29 devnull break;
421 9f1fdc12 2005-10-29 devnull case BCC:
422 9f1fdc12 2005-10-29 devnull delit = 1;
423 9f1fdc12 2005-10-29 devnull commas(to+strlen(fld[0]), s-1);
424 9f1fdc12 2005-10-29 devnull for(i=1; i<nfld && nbcc<nelem(bcclist); i++)
425 9f1fdc12 2005-10-29 devnull if(!addressed(fld[i]))
426 9f1fdc12 2005-10-29 devnull bcclist[nbcc++] = estrdup(fld[i]);
427 9f1fdc12 2005-10-29 devnull break;
428 9f1fdc12 2005-10-29 devnull case CC:
429 9f1fdc12 2005-10-29 devnull delit = 1;
430 9f1fdc12 2005-10-29 devnull commas(to+strlen(fld[0]), s-1);
431 9f1fdc12 2005-10-29 devnull for(i=1; i<nfld && ncc<nelem(cclist); i++)
432 9f1fdc12 2005-10-29 devnull if(!addressed(fld[i]))
433 9f1fdc12 2005-10-29 devnull cclist[ncc++] = estrdup(fld[i]);
434 9f1fdc12 2005-10-29 devnull break;
435 9f1fdc12 2005-10-29 devnull case ATTACH:
436 9f1fdc12 2005-10-29 devnull case INCLUDE:
437 9f1fdc12 2005-10-29 devnull delit = 1;
438 9f1fdc12 2005-10-29 devnull for(i=1; i<nfld && natt<nelem(attlist); i++){
439 9f1fdc12 2005-10-29 devnull attlist[natt] = estrdup(fld[i]);
440 9f1fdc12 2005-10-29 devnull included[natt++] = (h == INCLUDE);
441 9f1fdc12 2005-10-29 devnull }
442 9f1fdc12 2005-10-29 devnull break;
443 9f1fdc12 2005-10-29 devnull default:
444 9f1fdc12 2005-10-29 devnull if(first){
445 9f1fdc12 2005-10-29 devnull delit = 1;
446 9f1fdc12 2005-10-29 devnull for(i=0; i<nfld && nto<nelem(tolist); i++)
447 9f1fdc12 2005-10-29 devnull tolist[nto++] = estrdup(fld[i]);
448 9f1fdc12 2005-10-29 devnull }else /* ignore it */
449 9f1fdc12 2005-10-29 devnull delit = 0;
450 9f1fdc12 2005-10-29 devnull break;
451 9f1fdc12 2005-10-29 devnull }
452 9f1fdc12 2005-10-29 devnull if(delit){
453 9f1fdc12 2005-10-29 devnull /* delete line from body */
454 9f1fdc12 2005-10-29 devnull memmove(to, s, n+1);
455 9f1fdc12 2005-10-29 devnull }else
456 9f1fdc12 2005-10-29 devnull to = s;
457 9f1fdc12 2005-10-29 devnull free(copy);
458 9f1fdc12 2005-10-29 devnull first = 0;
459 9f1fdc12 2005-10-29 devnull }
460 9f1fdc12 2005-10-29 devnull
461 9f1fdc12 2005-10-29 devnull ofd = open(outgoing, OWRITE|OCEXEC); /* no error check necessary */
462 9f1fdc12 2005-10-29 devnull if(ofd > 0){
463 9f1fdc12 2005-10-29 devnull /* From dhog Fri Aug 24 22:13:00 EDT 2001 */
464 9f1fdc12 2005-10-29 devnull now = ctime(time(0));
465 9f1fdc12 2005-10-29 devnull fprint(ofd, "From %s %s", user, now);
466 9f1fdc12 2005-10-29 devnull fprint(ofd, "From: %s\n", user);
467 9f1fdc12 2005-10-29 devnull fprint(ofd, "Date: %s", now);
468 9f1fdc12 2005-10-29 devnull for(i=0; i<natt; i++)
469 9f1fdc12 2005-10-29 devnull if(included[i])
470 9f1fdc12 2005-10-29 devnull fprint(ofd, "Include: %s\n", attlist[i]);
471 9f1fdc12 2005-10-29 devnull else
472 9f1fdc12 2005-10-29 devnull fprint(ofd, "Attach: %s\n", attlist[i]);
473 9f1fdc12 2005-10-29 devnull /* needed because mail is by default Latin-1 */
474 9f1fdc12 2005-10-29 devnull fprint(ofd, "Content-Type: text/plain; charset=\"UTF-8\"\n");
475 9f1fdc12 2005-10-29 devnull fprint(ofd, "Content-Transfer-Encoding: 8bit\n");
476 9f1fdc12 2005-10-29 devnull }
477 9f1fdc12 2005-10-29 devnull
478 9f1fdc12 2005-10-29 devnull e = emalloc(sizeof(struct Exec));
479 9f1fdc12 2005-10-29 devnull if(pipe(p) < 0)
480 9f1fdc12 2005-10-29 devnull error("can't create pipe: %r");
481 9f1fdc12 2005-10-29 devnull e->p[0] = p[0];
482 9f1fdc12 2005-10-29 devnull e->p[1] = p[1];
483 9f1fdc12 2005-10-29 devnull e->prog = unsharp("#9/bin/upas/marshal");
484 9f1fdc12 2005-10-29 devnull e->argv = emalloc((1+1+2+4*natt+1)*sizeof(char*));
485 9f1fdc12 2005-10-29 devnull e->argv[0] = estrdup("marshal");
486 9f1fdc12 2005-10-29 devnull e->argv[1] = estrdup("-8");
487 9f1fdc12 2005-10-29 devnull j = 2;
488 9f1fdc12 2005-10-29 devnull if(m->replyname){
489 9f1fdc12 2005-10-29 devnull e->argv[j++] = estrdup("-R");
490 9f1fdc12 2005-10-29 devnull e->argv[j++] = estrstrdup(mbox.name, m->replyname);
491 9f1fdc12 2005-10-29 devnull }
492 9f1fdc12 2005-10-29 devnull for(i=0; i<natt; i++){
493 9f1fdc12 2005-10-29 devnull if(included[i])
494 9f1fdc12 2005-10-29 devnull e->argv[j++] = estrdup("-A");
495 9f1fdc12 2005-10-29 devnull else
496 9f1fdc12 2005-10-29 devnull e->argv[j++] = estrdup("-a");
497 9f1fdc12 2005-10-29 devnull e->argv[j++] = estrdup(attlist[i]);
498 9f1fdc12 2005-10-29 devnull }
499 9f1fdc12 2005-10-29 devnull sync = chancreate(sizeof(int), 0);
500 9f1fdc12 2005-10-29 devnull e->sync = sync;
501 9f1fdc12 2005-10-29 devnull proccreate(execproc, e, EXECSTACK);
502 9f1fdc12 2005-10-29 devnull recvul(sync);
503 9f1fdc12 2005-10-29 devnull // close(p[0]);
504 9f1fdc12 2005-10-29 devnull
505 9f1fdc12 2005-10-29 devnull /* using marshal -8, so generate rfc822 headers */
506 9f1fdc12 2005-10-29 devnull if(nto > 0){
507 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "To: ");
508 9f1fdc12 2005-10-29 devnull for(i=0; i<nto-1; i++)
509 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "%s, ", tolist[i]);
510 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "%s\n", tolist[i]);
511 9f1fdc12 2005-10-29 devnull }
512 9f1fdc12 2005-10-29 devnull if(ncc > 0){
513 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "CC: ");
514 9f1fdc12 2005-10-29 devnull for(i=0; i<ncc-1; i++)
515 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "%s, ", cclist[i]);
516 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "%s\n", cclist[i]);
517 9f1fdc12 2005-10-29 devnull }
518 9f1fdc12 2005-10-29 devnull if(nbcc > 0){
519 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "BCC: ");
520 9f1fdc12 2005-10-29 devnull for(i=0; i<nbcc-1; i++)
521 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "%s, ", bcclist[i]);
522 9f1fdc12 2005-10-29 devnull print2(p[1], ofd, "%s\n", bcclist[i]);
523 9f1fdc12 2005-10-29 devnull }
524 9f1fdc12 2005-10-29 devnull
525 9f1fdc12 2005-10-29 devnull i = strlen(body);
526 9f1fdc12 2005-10-29 devnull if(i > 0)
527 9f1fdc12 2005-10-29 devnull write2(p[1], ofd, body, i, 1);
528 9f1fdc12 2005-10-29 devnull
529 9f1fdc12 2005-10-29 devnull /* guarantee a blank line, to ensure attachments are separated from body */
530 9f1fdc12 2005-10-29 devnull if(i==0 || body[i-1]!='\n')
531 9f1fdc12 2005-10-29 devnull write2(p[1], ofd, "\n\n", 2, 0);
532 9f1fdc12 2005-10-29 devnull else if(i>1 && body[i-2]!='\n')
533 9f1fdc12 2005-10-29 devnull write2(p[1], ofd, "\n", 1, 0);
534 9f1fdc12 2005-10-29 devnull
535 9f1fdc12 2005-10-29 devnull /* these look like pseudo-attachments in the "outgoing" box */
536 9f1fdc12 2005-10-29 devnull if(ofd>0 && natt>0){
537 9f1fdc12 2005-10-29 devnull for(i=0; i<natt; i++)
538 9f1fdc12 2005-10-29 devnull if(included[i])
539 9f1fdc12 2005-10-29 devnull fprint(ofd, "=====> Include: %s\n", attlist[i]);
540 9f1fdc12 2005-10-29 devnull else
541 9f1fdc12 2005-10-29 devnull fprint(ofd, "=====> Attach: %s\n", attlist[i]);
542 9f1fdc12 2005-10-29 devnull }
543 9f1fdc12 2005-10-29 devnull if(ofd > 0)
544 9f1fdc12 2005-10-29 devnull write(ofd, "\n", 1);
545 9f1fdc12 2005-10-29 devnull
546 9f1fdc12 2005-10-29 devnull for(i=0; i<natt; i++)
547 9f1fdc12 2005-10-29 devnull free(attlist[i]);
548 9f1fdc12 2005-10-29 devnull close(ofd);
549 9f1fdc12 2005-10-29 devnull close(p[1]);
550 9f1fdc12 2005-10-29 devnull free(body);
551 9f1fdc12 2005-10-29 devnull
552 9f1fdc12 2005-10-29 devnull if(m->replyname != nil)
553 9f1fdc12 2005-10-29 devnull mesgmenumark(mbox.w, m->replyname, "\t[replied]");
554 9f1fdc12 2005-10-29 devnull if(m->name[0] == '/')
555 9f1fdc12 2005-10-29 devnull s = estrdup(m->name);
556 9f1fdc12 2005-10-29 devnull else
557 9f1fdc12 2005-10-29 devnull s = estrstrdup(mbox.name, m->name);
558 9f1fdc12 2005-10-29 devnull s = egrow(s, "-R", nil);
559 9f1fdc12 2005-10-29 devnull winname(m->w, s);
560 9f1fdc12 2005-10-29 devnull free(s);
561 9f1fdc12 2005-10-29 devnull winclean(m->w);
562 9f1fdc12 2005-10-29 devnull /* mark message unopened because it's no longer the original message */
563 9f1fdc12 2005-10-29 devnull m->opened = 0;
564 9f1fdc12 2005-10-29 devnull }