#include #include #include #include #include #include #include "faces.h" static int showfd = -1; static int seefd = -1; static int logfd = -1; static char *user; static char *logtag; char **maildirs; int nmaildirs; Face *pathface(char*, Face*); void initplumb(void) { showfd = plumbopen("send", OWRITE); seefd = plumbopen("seemail", OREAD); if(seefd < 0){ logfd = open("/sys/log/mail", OREAD); seek(logfd, 0LL, 2); user = getenv("user"); if(user == nil){ fprint(2, "faces: can't find user name: %r\n"); exits("$user"); } logtag = emalloc(32+strlen(user)+1); sprint(logtag, " delivered %s From ", user); } } void addmaildir(char *dir) { maildirs = erealloc(maildirs, (nmaildirs+1)*sizeof(char*)); maildirs[nmaildirs++] = dir; } char* attr(Face *f) { static char buf[128]; if(f->str[Sdigest]){ snprint(buf, sizeof buf, "digest=%s", f->str[Sdigest]); return buf; } return nil; } void showmail(Face *f) { char *s; int n; if(showfd<0 || f->str[Sshow]==nil || f->str[Sshow][0]=='\0') return; s = emalloc(128+strlen(f->str[Sshow])+1); n = sprint(s, "faces\nshowmail\n/mail/fs/\ntext\n%s\n%ld\n%s", attr(f), strlen(f->str[Sshow]), f->str[Sshow]); write(showfd, s, n); free(s); } char* value(Plumbattr *attr, char *key, char *def) { char *v; v = plumblookup(attr, key); if(v) return v; return def; } void setname(Face *f, char *sender) { char *at, *bang; char *p; /* works with UTF-8, although it's written as ASCII */ for(p=sender; *p!='\0'; p++) *p = tolower(*p); f->str[Suser] = sender; at = strchr(sender, '@'); if(at){ *at++ = '\0'; f->str[Sdomain] = estrdup(at); return; } bang = strchr(sender, '!'); if(bang){ *bang++ = '\0'; f->str[Suser] = estrdup(bang); f->str[Sdomain] = sender; return; } } int getc(void) { static uchar buf[512]; static int nbuf = 0; static int i = 0; while(i == nbuf){ i = 0; nbuf = read(logfd, buf, sizeof buf); if(nbuf == 0){ sleep(15000); continue; } if(nbuf < 0) return -1; } return buf[i++]; } char* getline(char *buf, int n) { int i, c; for(i=0; i= 0){ m = plumbrecv(seefd); if(m == nil) killall("error on seemail plumb port"); t = value(m->attr, "mailtype", ""); if(strcmp(t, "delete") == 0) delete(m->data, value(m->attr, "digest", nil)); else if(strcmp(t, "new") != 0) fprint(2, "faces: unknown plumb message type %s\n", t); else for(i=0; idata, maildirs[i], strlen(maildirs[i])) == 0) goto Found; plumbfree(m); continue; Found: xtime = parsedate(value(m->attr, "date", date)); digestp = value(m->attr, "digest", nil); if(alreadyseen(digestp)){ /* duplicate upas/fs can send duplicate messages */ plumbfree(m); continue; } senderp = estrdup(value(m->attr, "sender", "???")); showmailp = estrdup(m->data); if(digestp) digestp = estrdup(digestp); plumbfree(m); }else{ if(logrecv(&senderp, &xtime) <= 0) killall("error reading log file"); showmailp = estrdup(""); digestp = nil; } setname(f, senderp); f->time = xtime; f->tm = *localtime(xtime); f->str[Sshow] = showmailp; f->str[Sdigest] = digestp; f = pathface(showmailp, f); return f; } } char* iline(char *data, char **pp) { char *p; for(p=data; *p!='\0' && *p!='\n'; p++) ; if(*p == '\n') *p++ = '\0'; *pp = p; return data; } Face* dirface0(char *dir, char *num, Face *f) { char *from, *date; char buf[1024], pwd[1024], *info, *p, *digest; int n, fd; ulong len; /* * loadmbox leaves us in maildir, so we needn't * walk /mail/fs/mbox for each face; this makes startup * a fair bit quicker. */ if(getwd(pwd, sizeof pwd) != nil && strcmp(pwd, dir) == 0) sprint(buf, "%s/info", num); else sprint(buf, "%s/%s/info", dir, num); len = dirlen(buf); if(len <= 0) return nil; fd = open(buf, OREAD); if(fd < 0) return nil; info = emalloc(len+1); n = readn(fd, info, len); close(fd); if(n < 0){ free(info); return nil; } info[n] = '\0'; if(f == nil) f = emalloc(sizeof(Face)); from = iline(info, &p); /* from */ iline(p, &p); /* to */ iline(p, &p); /* cc */ iline(p, &p); /* replyto */ date = iline(p, &p); /* date */ if(f->str[Suser] == nil) setname(f, estrdup(from)); if(f->time == 0){ f->time = parsedate(date); f->tm = *localtime(f->time); } sprint(buf, "%s/%s", dir, num); if(f->str[Sshow] == nil) f->str[Sshow] = estrdup(buf); if(f->str[Ssubject] == nil) f->str[Ssubject] = estrdup(iline(p, &p)); /* subject */ iline(p, &p); /* mime content type */ iline(p, &p); /* mime disposition */ iline(p, &p); /* filename */ digest = iline(p, &p); /* digest */ if(f->str[Sdigest] == nil) f->str[Sdigest] = estrdup(digest); free(info); return f; } Face* dirface(char *dir, char *num) { return dirface0(dir, num, 0); } Face* pathface(char *path, Face *f) { char buf[1024], *t; strncpy(buf, path, 1024); if((t = strrchr(buf, '/')) == 0) return f; *t = 0; dirface0(buf, t+1, f); return f; }