Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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