Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <thread.h>
5 #include <9pclient.h>
6 #include <plumb.h>
7 #include <ctype.h>
8 #include "dat.h"
10 char *maildir = "Mail/"; /* mountpoint of mail file system */
11 char *mboxname = "mbox"; /* mailboxdir/mboxname is mail spool file */
12 char *mailboxdir = nil; /* nil == /mail/box/$user */
13 char *fsname; /* filesystem for mailboxdir/mboxname is at maildir/fsname */
14 char *user;
15 char *outgoing;
16 char *srvname;
18 Window *wbox;
19 Message mbox;
20 Message replies;
21 char *home;
22 CFid *plumbsendfd;
23 CFid *plumbseemailfd;
24 CFid *plumbshowmailfd;
25 CFid *plumbsendmailfd;
26 Channel *cplumb;
27 Channel *cplumbshow;
28 Channel *cplumbsend;
29 int wctlfd;
30 void mainctl(void*);
31 void plumbproc(void*);
32 void plumbshowproc(void*);
33 void plumbsendproc(void*);
34 void plumbthread(void);
35 void plumbshowthread(void*);
36 void plumbsendthread(void*);
38 int shortmenu;
40 CFsys *mailfs;
41 CFsys *acmefs;
43 void
44 usage(void)
45 {
46 fprint(2, "usage: Mail [-sS] [-n srvname] [-o outgoing] [mailboxname [directoryname]]\n");
47 threadexitsall("usage");
48 }
50 void
51 removeupasfs(void)
52 {
53 char buf[256];
55 if(strcmp(mboxname, "mbox") == 0)
56 return;
57 snprint(buf, sizeof buf, "close %s", mboxname);
58 fswrite(mbox.ctlfd, buf, strlen(buf));
59 }
61 int
62 ismaildir(char *s)
63 {
64 Dir *d;
65 int ret;
67 d = fsdirstat(mailfs, s);
68 if(d == nil)
69 return 0;
70 ret = d->qid.type & QTDIR;
71 free(d);
72 return ret;
73 }
75 void
76 threadmain(int argc, char *argv[])
77 {
78 char *s, *name;
79 char err[ERRMAX], *cmd;
80 int i, newdir;
81 Fmt fmt;
83 doquote = needsrcquote;
84 quotefmtinstall();
86 /* open these early so we won't miss notification of new mail messages while we read mbox */
87 plumbsendfd = plumbopenfid("send", OWRITE|OCEXEC);
88 plumbseemailfd = plumbopenfid("seemail", OREAD|OCEXEC);
89 plumbshowmailfd = plumbopenfid("showmail", OREAD|OCEXEC);
91 shortmenu = 0;
92 srvname = "mail";
93 ARGBEGIN{
94 case 's':
95 shortmenu = 1;
96 break;
97 case 'S':
98 shortmenu = 2;
99 break;
100 case 'o':
101 outgoing = EARGF(usage());
102 break;
103 case 'm':
104 smprint(maildir, "%s/", EARGF(usage()));
105 break;
106 case 'n':
107 srvname = EARGF(usage());
108 break;
109 default:
110 usage();
111 }ARGEND
113 acmefs = nsmount("acme",nil);
114 if(acmefs == nil)
115 error("cannot mount acme: %r");
116 mailfs = nsmount(srvname, nil);
117 if(mailfs == nil)
118 error("cannot mount %s: %r", srvname);
120 name = "mbox";
122 newdir = 1;
123 if(argc > 0){
124 i = strlen(argv[0]);
125 if(argc>2 || i==0)
126 usage();
127 /* see if the name is that of an existing /mail/fs directory */
128 if(argc==1 && argv[0][0] != '/' && ismaildir(argv[0])){
129 name = argv[0];
130 mboxname = estrdup(name);
131 newdir = 0;
132 }else{
133 if(argv[0][i-1] == '/')
134 argv[0][i-1] = '\0';
135 s = strrchr(argv[0], '/');
136 if(s == nil)
137 mboxname = estrdup(argv[0]);
138 else{
139 *s++ = '\0';
140 if(*s == '\0')
141 usage();
142 mailboxdir = argv[0];
143 mboxname = estrdup(s);
145 if(argc > 1)
146 name = argv[1];
147 else
148 name = mboxname;
152 user = getenv("user");
153 if(user == nil)
154 user = "none";
155 home = getenv("home");
156 if(home == nil)
157 home = getenv("HOME");
158 if(home == nil)
159 error("can't find $home");
160 if(mailboxdir == nil)
161 mailboxdir = estrstrdup(home, "/mail");
162 if(outgoing == nil)
163 outgoing = estrstrdup(mailboxdir, "/outgoing");
165 mbox.ctlfd = fsopen(mailfs, estrstrdup(mboxname, "/ctl"), OWRITE);
166 if(mbox.ctlfd == nil)
167 error("can't open %s: %r", estrstrdup(mboxname, "/ctl"));
169 fsname = estrdup(name);
170 if(newdir && argc > 0){
171 s = emalloc(5+strlen(mailboxdir)+strlen(mboxname)+strlen(name)+10+1);
172 for(i=0; i<10; i++){
173 sprint(s, "open %s/%s %s", mailboxdir, mboxname, fsname);
174 if(fswrite(mbox.ctlfd, s, strlen(s)) >= 0)
175 break;
176 err[0] = '\0';
177 errstr(err, sizeof err);
178 if(strstr(err, "mbox name in use") == nil)
179 error("can't create directory %s for mail: %s", name, err);
180 free(fsname);
181 fsname = emalloc(strlen(name)+10);
182 sprint(fsname, "%s-%d", name, i);
184 if(i == 10)
185 error("can't open %s/%s: %r", mailboxdir, mboxname);
186 free(s);
189 s = estrstrdup(fsname, "/");
190 mbox.name = estrstrdup(maildir, s);
191 mbox.level= 0;
192 readmbox(&mbox, maildir, s);
193 home = getenv("home");
194 if(home == nil)
195 home = "/";
197 wbox = newwindow();
198 winname(wbox, mbox.name);
199 wintagwrite(wbox, "Put Mail Delmesg ", 3+1+4+1+7+1);
200 threadcreate(mainctl, wbox, STACK);
202 fmtstrinit(&fmt);
203 fmtprint(&fmt, "Mail");
204 if(shortmenu)
205 fmtprint(&fmt, " -%c", "sS"[shortmenu-1]);
206 if(outgoing)
207 fmtprint(&fmt, " -o %s", outgoing);
208 fmtprint(&fmt, " %s", name);
209 cmd = fmtstrflush(&fmt);
210 if(cmd == nil)
211 sysfatal("out of memory");
212 winsetdump(wbox, "/acme/mail", cmd);
213 mbox.w = wbox;
215 mesgmenu(wbox, &mbox);
216 winclean(wbox);
218 /* wctlfd = open("/dev/wctl", OWRITE|OCEXEC); /* for acme window */
219 wctlfd = -1;
220 cplumb = chancreate(sizeof(Plumbmsg*), 0);
221 cplumbshow = chancreate(sizeof(Plumbmsg*), 0);
222 if(strcmp(name, "mbox") == 0){
223 /*
224 * Avoid creating multiple windows to send mail by only accepting
225 * sendmail plumb messages if we're reading the main mailbox.
226 */
227 plumbsendmailfd = plumbopenfid("sendmail", OREAD|OCEXEC);
228 cplumbsend = chancreate(sizeof(Plumbmsg*), 0);
229 proccreate(plumbsendproc, nil, STACK);
230 threadcreate(plumbsendthread, nil, STACK);
232 /* start plumb reader as separate proc ... */
233 proccreate(plumbproc, nil, STACK);
234 proccreate(plumbshowproc, nil, STACK);
235 threadcreate(plumbshowthread, nil, STACK);
236 fswrite(mbox.ctlfd, "refresh", 7);
237 /* ... and use this thread to read the messages */
238 plumbthread();
241 void
242 plumbproc(void* v)
244 Plumbmsg *m;
246 threadsetname("plumbproc");
247 for(;;){
248 m = plumbrecvfid(plumbseemailfd);
249 sendp(cplumb, m);
250 if(m == nil)
251 threadexits(nil);
255 void
256 plumbshowproc(void* v)
258 Plumbmsg *m;
260 threadsetname("plumbshowproc");
261 for(;;){
262 m = plumbrecvfid(plumbshowmailfd);
263 sendp(cplumbshow, m);
264 if(m == nil)
265 threadexits(nil);
269 void
270 plumbsendproc(void* v)
272 Plumbmsg *m;
274 threadsetname("plumbsendproc");
275 for(;;){
276 m = plumbrecvfid(plumbsendmailfd);
277 sendp(cplumbsend, m);
278 if(m == nil)
279 threadexits(nil);
283 void
284 newmesg(char *name, char *digest)
286 Dir *d;
288 if(strncmp(name, mbox.name, strlen(mbox.name)) != 0)
289 return; /* message is about another mailbox */
290 if(mesglookupfile(&mbox, name, digest) != nil)
291 return;
292 if(strncmp(name, "Mail/", 5) == 0)
293 name += 5;
294 d = fsdirstat(mailfs, name);
295 if(d == nil)
296 return;
297 if(mesgadd(&mbox, mbox.name, d, digest))
298 mesgmenunew(wbox, &mbox);
299 free(d);
302 void
303 showmesg(char *name, char *digest)
305 char *n;
306 char *mb;
308 mb = mbox.name;
309 if(strncmp(name, mb, strlen(mb)) != 0)
310 return; /* message is about another mailbox */
311 n = estrdup(name+strlen(mb));
312 if(n[strlen(n)-1] != '/')
313 n = egrow(n, "/", nil);
314 mesgopen(&mbox, mbox.name, name+strlen(mb), nil, 1, digest);
315 free(n);
318 void
319 delmesg(char *name, char *digest, int dodel, char *save)
321 Message *m;
323 m = mesglookupfile(&mbox, name, digest);
324 if(m != nil){
325 if(save)
326 mesgcommand(m, estrstrdup("Save ", save));
327 if(dodel)
328 mesgmenumarkdel(wbox, &mbox, m, 1);
329 else{
330 /* notification came from plumber - message is gone */
331 mesgmenudel(wbox, &mbox, m);
332 if(!m->opened)
333 mesgdel(&mbox, m);
338 void
339 plumbthread(void)
341 Plumbmsg *m;
342 Plumbattr *a;
343 char *type, *digest;
345 threadsetname("plumbthread");
346 while((m = recvp(cplumb)) != nil){
347 a = m->attr;
348 digest = plumblookup(a, "digest");
349 type = plumblookup(a, "mailtype");
350 if(type == nil)
351 fprint(2, "Mail: plumb message with no mailtype attribute\n");
352 else if(strcmp(type, "new") == 0)
353 newmesg(m->data, digest);
354 else if(strcmp(type, "delete") == 0)
355 delmesg(m->data, digest, 0, nil);
356 else
357 fprint(2, "Mail: unknown plumb attribute %s\n", type);
358 plumbfree(m);
360 threadexits(nil);
363 void
364 plumbshowthread(void *v)
366 Plumbmsg *m;
368 USED(v);
369 threadsetname("plumbshowthread");
370 while((m = recvp(cplumbshow)) != nil){
371 showmesg(m->data, plumblookup(m->attr, "digest"));
372 plumbfree(m);
374 threadexits(nil);
377 void
378 plumbsendthread(void *v)
380 Plumbmsg *m;
382 USED(v);
383 threadsetname("plumbsendthread");
384 while((m = recvp(cplumbsend)) != nil){
385 mkreply(nil, "Mail", m->data, m->attr, nil);
386 plumbfree(m);
388 threadexits(nil);
391 int
392 mboxcommand(Window *w, char *s)
394 char *args[10], **targs, *save;
395 Message *m, *next;
396 int ok, nargs, i, j;
397 char buf[128];
399 nargs = tokenize(s, args, nelem(args));
400 if(nargs == 0)
401 return 0;
402 if(strcmp(args[0], "Mail") == 0){
403 if(nargs == 1)
404 mkreply(nil, "Mail", "", nil, nil);
405 else
406 mkreply(nil, "Mail", args[1], nil, nil);
407 return 1;
409 if(strcmp(s, "Del") == 0){
410 if(mbox.dirty){
411 mbox.dirty = 0;
412 fprint(2, "mail: mailbox not written\n");
413 return 1;
415 ok = 1;
416 for(m=mbox.head; m!=nil; m=next){
417 next = m->next;
418 if(m->w){
419 if(windel(m->w, 0))
420 m->w = nil;
421 else
422 ok = 0;
425 for(m=replies.head; m!=nil; m=next){
426 next = m->next;
427 if(m->w){
428 if(windel(m->w, 0))
429 m->w = nil;
430 else
431 ok = 0;
434 if(ok){
435 windel(w, 1);
436 removeupasfs();
437 threadexitsall(nil);
439 return 1;
441 if(strcmp(s, "Put") == 0){
442 rewritembox(wbox, &mbox);
443 return 1;
445 if(strcmp(s, "Get") == 0){
446 fswrite(mbox.ctlfd, "refresh", 7);
447 return 1;
449 if(strcmp(s, "Delmesg") == 0){
450 save = nil;
451 if(nargs > 1)
452 save = args[1];
453 s = winselection(w);
454 if(s == nil)
455 return 1;
456 nargs = 1;
457 for(i=0; s[i]; i++)
458 if(s[i] == '\n')
459 nargs++;
460 targs = emalloc(nargs*sizeof(char*)); /* could be too many for a local array */
461 nargs = getfields(s, targs, nargs, 1, "\n");
462 for(i=0; i<nargs; i++){
463 if(!isdigit(targs[i][0]))
464 continue;
465 j = atoi(targs[i]); /* easy way to parse the number! */
466 if(j == 0)
467 continue;
468 snprint(buf, sizeof buf, "%s%d", mbox.name, j);
469 delmesg(buf, nil, 1, save);
471 free(s);
472 free(targs);
473 return 1;
475 return 0;
478 void
479 mainctl(void *v)
481 Window *w;
482 Event *e, *e2, *eq, *ea;
483 int na, nopen;
484 char *s, *t, *buf;
486 w = v;
487 winincref(w);
488 proccreate(wineventproc, w, STACK);
490 for(;;){
491 e = recvp(w->cevent);
492 switch(e->c1){
493 default:
494 Unknown:
495 print("unknown message %c%c\n", e->c1, e->c2);
496 break;
498 case 'E': /* write to body; can't affect us */
499 break;
501 case 'F': /* generated by our actions; ignore */
502 break;
504 case 'K': /* type away; we don't care */
505 break;
507 case 'M':
508 switch(e->c2){
509 case 'x':
510 case 'X':
511 ea = nil;
512 e2 = nil;
513 if(e->flag & 2)
514 e2 = recvp(w->cevent);
515 if(e->flag & 8){
516 ea = recvp(w->cevent);
517 na = ea->nb;
518 recvp(w->cevent);
519 }else
520 na = 0;
521 s = e->b;
522 /* if it's a known command, do it */
523 if((e->flag&2) && e->nb==0)
524 s = e2->b;
525 if(na){
526 t = emalloc(strlen(s)+1+na+1);
527 sprint(t, "%s %s", s, ea->b);
528 s = t;
530 /* if it's a long message, it can't be for us anyway */
531 if(!mboxcommand(w, s)) /* send it back */
532 winwriteevent(w, e);
533 if(na)
534 free(s);
535 break;
537 case 'l':
538 case 'L':
539 buf = nil;
540 eq = e;
541 if(e->flag & 2){
542 e2 = recvp(w->cevent);
543 eq = e2;
545 s = eq->b;
546 if(eq->q1>eq->q0 && eq->nb==0){
547 buf = emalloc((eq->q1-eq->q0)*UTFmax+1);
548 winread(w, eq->q0, eq->q1, buf);
549 s = buf;
551 nopen = 0;
552 do{
553 /* skip 'deleted' string if present' */
554 if(strncmp(s, deleted, strlen(deleted)) == 0)
555 s += strlen(deleted);
556 /* skip mail box name if present */
557 if(strncmp(s, mbox.name, strlen(mbox.name)) == 0)
558 s += strlen(mbox.name);
559 nopen += mesgopen(&mbox, mbox.name, s, nil, 0, nil);
560 while(*s!='\0' && *s++!='\n')
562 }while(*s);
563 if(nopen == 0) /* send it back */
564 winwriteevent(w, e);
565 free(buf);
566 break;
568 case 'I': /* modify away; we don't care */
569 case 'D':
570 case 'd':
571 case 'i':
572 break;
574 default:
575 goto Unknown;