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 */
24 CFid *plumbshowmailfd;
25 CFid *plumbsendmailfd;
31 void plumbproc(void*);
32 void plumbshowproc(void*);
33 void plumbsendproc(void*);
34 void plumbthread(void);
35 void plumbshowthread(void*);
36 void plumbsendthread(void*);
46 fprint(2, "usage: Mail [-sS] [-n srvname] [-o outgoing] [mailboxname [directoryname]]\n");
47 threadexitsall("usage");
55 if(strcmp(mboxname, "mbox") == 0)
57 snprint(buf, sizeof buf, "close %s", mboxname);
58 fswrite(mbox.ctlfd, buf, strlen(buf));
67 d = fsdirstat(mailfs, s);
70 ret = d->qid.type & QTDIR;
76 threadmain(int argc, char *argv[])
79 char err[ERRMAX], *cmd;
83 doquote = needsrcquote;
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);
101 outgoing = EARGF(usage());
104 smprint(maildir, "%s/", EARGF(usage()));
107 srvname = EARGF(usage());
113 acmefs = nsmount("acme",nil);
115 error("cannot mount acme: %r");
116 mailfs = nsmount(srvname, nil);
118 error("cannot mount %s: %r", srvname);
127 /* see if the name is that of an existing /mail/fs directory */
128 if(argc==1 && argv[0][0] != '/' && ismaildir(argv[0])){
130 mboxname = estrdup(name);
133 if(argv[0][i-1] == '/')
135 s = strrchr(argv[0], '/');
137 mboxname = estrdup(argv[0]);
142 mailboxdir = argv[0];
143 mboxname = estrdup(s);
152 user = getenv("user");
155 home = getenv("home");
157 home = getenv("HOME");
159 error("can't find $home");
160 if(mailboxdir == nil)
161 mailboxdir = estrstrdup(home, "/mail");
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);
173 sprint(s, "open %s/%s %s", mailboxdir, mboxname, fsname);
174 if(fswrite(mbox.ctlfd, s, strlen(s)) >= 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);
181 fsname = emalloc(strlen(name)+10);
182 sprint(fsname, "%s-%d", name, i);
185 error("can't open %s/%s: %r", mailboxdir, mboxname);
189 s = estrstrdup(fsname, "/");
190 mbox.name = estrstrdup(maildir, s);
192 readmbox(&mbox, maildir, s);
193 home = getenv("home");
198 winname(wbox, mbox.name);
199 wintagwrite(wbox, "Put Mail Delmesg ", 3+1+4+1+7+1);
200 threadcreate(mainctl, wbox, STACK);
203 fmtprint(&fmt, "Mail");
205 fmtprint(&fmt, " -%c", "sS"[shortmenu-1]);
207 fmtprint(&fmt, " -o %s", outgoing);
208 fmtprint(&fmt, " %s", name);
209 cmd = fmtstrflush(&fmt);
211 sysfatal("out of memory");
212 winsetdump(wbox, "/acme/mail", cmd);
215 mesgmenu(wbox, &mbox);
218 /* wctlfd = open("/dev/wctl", OWRITE|OCEXEC); /* for acme window */
220 cplumb = chancreate(sizeof(Plumbmsg*), 0);
221 cplumbshow = chancreate(sizeof(Plumbmsg*), 0);
222 if(strcmp(name, "mbox") == 0){
224 * Avoid creating multiple windows to send mail by only accepting
225 * sendmail plumb messages if we're reading the main mailbox.
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 */
246 threadsetname("plumbproc");
248 m = plumbrecvfid(plumbseemailfd);
256 plumbshowproc(void* v)
260 threadsetname("plumbshowproc");
262 m = plumbrecvfid(plumbshowmailfd);
263 sendp(cplumbshow, m);
270 plumbsendproc(void* v)
274 threadsetname("plumbsendproc");
276 m = plumbrecvfid(plumbsendmailfd);
277 sendp(cplumbsend, m);
284 newmesg(char *name, char *digest)
288 if(strncmp(name, mbox.name, strlen(mbox.name)) != 0)
289 return; /* message is about another mailbox */
290 if(mesglookupfile(&mbox, name, digest) != nil)
292 if(strncmp(name, "Mail/", 5) == 0)
294 d = fsdirstat(mailfs, name);
297 if(mesgadd(&mbox, mbox.name, d, digest))
298 mesgmenunew(wbox, &mbox);
303 showmesg(char *name, char *digest)
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);
319 delmesg(char *name, char *digest, int dodel, char *save)
323 m = mesglookupfile(&mbox, name, digest);
326 mesgcommand(m, estrstrdup("Save ", save));
328 mesgmenumarkdel(wbox, &mbox, m, 1);
330 /* notification came from plumber - message is gone */
331 mesgmenudel(wbox, &mbox, m);
345 threadsetname("plumbthread");
346 while((m = recvp(cplumb)) != nil){
348 digest = plumblookup(a, "digest");
349 type = plumblookup(a, "mailtype");
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);
357 fprint(2, "Mail: unknown plumb attribute %s\n", type);
364 plumbshowthread(void *v)
369 threadsetname("plumbshowthread");
370 while((m = recvp(cplumbshow)) != nil){
371 showmesg(m->data, plumblookup(m->attr, "digest"));
378 plumbsendthread(void *v)
383 threadsetname("plumbsendthread");
384 while((m = recvp(cplumbsend)) != nil){
385 mkreply(nil, "Mail", m->data, m->attr, nil);
392 mboxcommand(Window *w, char *s)
394 char *args[10], **targs, *save;
399 nargs = tokenize(s, args, nelem(args));
402 if(strcmp(args[0], "Mail") == 0){
404 mkreply(nil, "Mail", "", nil, nil);
406 mkreply(nil, "Mail", args[1], nil, nil);
409 if(strcmp(s, "Del") == 0){
412 fprint(2, "mail: mailbox not written\n");
416 for(m=mbox.head; m!=nil; m=next){
425 for(m=replies.head; m!=nil; m=next){
441 if(strcmp(s, "Put") == 0){
442 rewritembox(wbox, &mbox);
445 if(strcmp(s, "Get") == 0){
446 fswrite(mbox.ctlfd, "refresh", 7);
449 if(strcmp(s, "Delmesg") == 0){
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]))
465 j = atoi(targs[i]); /* easy way to parse the number! */
468 snprint(buf, sizeof buf, "%s%d", mbox.name, j);
469 delmesg(buf, nil, 1, save);
482 Event *e, *e2, *eq, *ea;
488 proccreate(wineventproc, w, STACK);
491 e = recvp(w->cevent);
495 print("unknown message %c%c\n", e->c1, e->c2);
498 case 'E': /* write to body; can't affect us */
501 case 'F': /* generated by our actions; ignore */
504 case 'K': /* type away; we don't care */
514 e2 = recvp(w->cevent);
516 ea = recvp(w->cevent);
522 /* if it's a known command, do it */
523 if((e->flag&2) && e->nb==0)
526 t = emalloc(strlen(s)+1+na+1);
527 sprint(t, "%s %s", s, ea->b);
530 /* if it's a long message, it can't be for us anyway */
531 if(!mboxcommand(w, s)) /* send it back */
542 e2 = recvp(w->cevent);
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);
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')
563 if(nopen == 0) /* send it back */
568 case 'I': /* modify away; we don't care */