This commit is contained in:
rsc 2006-02-08 22:39:54 +00:00
parent 5a1e9de7b1
commit 605c0ea102
11 changed files with 132 additions and 386 deletions

View file

@ -45,7 +45,7 @@ extern char date[];
extern char *maildir;
extern char **maildirs;
extern int nmaildirs;
extern CFsys *upasfs;
extern CFsys *mailfs;
Face* nextface(void);
void findbit(Face*);

View file

@ -3,7 +3,6 @@
#include <draw.h>
#include <plumb.h>
#include <regexp.h>
//jpc #include <event.h> /* for support routines only */
#include <bio.h>
#include <thread.h>
#include <mouse.h>
@ -20,6 +19,8 @@ enum
Infolines = 9,
HhmmTime = 18*60*60, /* max age of face to display hh:mm time */
STACK = 32768
};
enum
@ -27,6 +28,7 @@ enum
Mainp,
Timep,
Mousep,
Resizep,
NPROC
};
@ -34,7 +36,8 @@ int pids[NPROC];
char *procnames[] = {
"main",
"time",
"mouse"
"mouse",
"resize",
};
Rectangle leftright = {0, 0, 20, 15};
@ -57,7 +60,7 @@ uchar rightdata[] = {
0x18, 0x00, 0x00, 0x10, 0x00
};
CFsys *upasfs;
CFsys *mailfs;
Mousectl *mousectl;
Image *blue; /* full arrow */
Image *bgrnd; /* pale blue background color */
@ -74,7 +77,7 @@ int ndown;
char date[64];
Face **faces;
char *maildir = "/mail/fs/mbox";
char *maildir = "INBOX";
ulong now;
Point datep = { 8, 6 };
@ -83,6 +86,7 @@ Point enddate; /* where date ends on display; used to place arrows */
Rectangle leftr; /* location of left arrow on display */
Rectangle rightr; /* location of right arrow on display */
void updatetimes(void);
void eresized(int);
void
setdate(void)
@ -95,15 +99,12 @@ setdate(void)
void
init(void)
{
#if 0
mousefd = open("/dev/mouse", OREAD);
if(mousefd < 0){
fprint(2, "faces: can't open mouse: %r\n");
threadexitsall("mouse");
}
#endif
upasfs = nsmount("upasfs",nil);
mousectl = initmouse(nil,screen);
mailfs = nsmount("mail", nil);
if(mailfs == nil)
sysfatal("mount mail: %r");
mousectl = initmouse(nil, screen);
if(mousectl == nil)
sysfatal("initmouse: %r");
initplumb();
/* make background color */
@ -332,7 +333,7 @@ addface(Face *f) /* always adds at 0 */
lockdisplay(display);
if(first != 0){
first = 0;
resized();
eresized(0);
}
findbit(f);
@ -366,27 +367,6 @@ addface(Face *f) /* always adds at 0 */
unlockdisplay(display);
}
#if 0
void
loadmboxfaces(char *maildir)
{
int dirfd;
Dir *d;
int i, n;
dirfd = open(maildir, OREAD);
if(dirfd >= 0){
chdir(maildir);
while((n = dirread(dirfd, &d)) > 0){
for(i=0; i<n; i++)
addface(dirface(maildir, d[i].name));
free(d);
}
close(dirfd);
}
}
#endif
void
loadmboxfaces(char *maildir)
{
@ -394,20 +374,16 @@ loadmboxfaces(char *maildir)
Dir *d;
int i, n;
dirfd = fsopen(upasfs,maildir, OREAD);
dirfd = fsopen(mailfs, maildir, OREAD);
if(dirfd != nil){
//jpc chdir(maildir);
while((n = fsdirread(dirfd, &d)) > 0){
for(i=0; i<n; i++) {
for(i=0; i<n; i++)
addface(dirface(maildir, d[i].name));
}
free(d);
}
fsclose(dirfd);
}
else {
error("cannot open %s: %r",maildir);
}
}else
sysfatal("open %s: %r", maildir);
}
void
@ -542,30 +518,15 @@ eresized(int new)
unlockdisplay(display);
}
#if 0
int
getmouse(Mouse *m)
void
resizeproc(void *v)
{
int n;
static int eof;
char buf[128];
USED(v);
if(eof)
return 0;
for(;;){
n = read(mousefd, buf, sizeof(buf));
if(n <= 0){
/* so callers needn't check return value every time */
eof = 1;
m->buttons = 0;
return 0;
}
//jpc n = eatomouse(m, buf, n);
if(n > 0)
return 1;
}
while(recv(mousectl->resizec, 0) == 1)
eresized(1);
}
#endif
int
getmouse(Mouse *m)
{
@ -573,19 +534,13 @@ getmouse(Mouse *m)
if(eof)
return 0;
if( readmouse(mousectl) < 0 ) {
if(readmouse(mousectl) < 0){
eof = 1;
m->buttons = 0;
return 0;
}
else {
*m = mousectl->m;
/* m->buttons = mousectl->m.buttons;
m->xy.x = mousectl->m.xy.x;
m->xy.y = mousectl->m.xy.y;
m->msec = mousectl->m.msec; */
return 1;
}
*m = mousectl->m;
return 1;
}
enum
@ -680,9 +635,10 @@ click(int button, Mouse *m)
}
void
mouseproc(void *dummy)
mouseproc(void *v)
{
Mouse mouse;
USED(v);
while(getmouse(&mouse)){
if(mouse.buttons == 1)
@ -700,35 +656,9 @@ mouseproc(void *dummy)
void
killall(char *s)
{
int i, pid;
pid = getpid();
for(i=0; i<NPROC; i++)
if(pids[i] && pids[i]!=pid)
postnote(PNPROC, pids[i], "kill");
threadexitsall(s);
}
void
startproc(void (*f)(void), int index)
{
int pid;
switch(pid = rfork(RFPROC|RFNOWAIT)){ //jpc removed |RFMEM
case -1:
fprint(2, "faces: fork failed: %r\n");
killall("fork failed");
case 0:
f();
fprint(2, "faces: %s process exits\n", procnames[index]);
if(index >= 0)
killall("process died");
threadexitsall(nil);
}
if(index >= 0)
pids[index] = pid;
}
void
usage(void)
{
@ -741,6 +671,8 @@ threadmain(int argc, char *argv[])
{
int i;
rfork(RFNOTEG);
ARGBEGIN{
case 'h':
history++;
@ -772,8 +704,9 @@ threadmain(int argc, char *argv[])
eresized(0);
pids[Mainp] = getpid();
pids[Timep] = proccreate(timeproc, nil, 16000);
pids[Mousep] = proccreate(mouseproc, nil, 16000);
pids[Timep] = proccreate(timeproc, nil, STACK);
pids[Mousep] = proccreate(mouseproc, nil, STACK);
pids[Resizep] = proccreate(resizeproc, nil, STACK);
if(initload)
for(i = 0; i < nmaildirs; i++)
loadmboxfaces(maildirs[i]);

View file

@ -1,17 +1,15 @@
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <plumb.h>
#include <regexp.h>
#include <bio.h>
#include <9pclient.h>
#include <plumb.h>
#include "faces.h"
static int showfd = -1;
static int seefd = -1;
static int logfd = -1;
static char *user;
static char *logtag;
static CFid* showfd;
static CFid* seefd;
char **maildirs;
int nmaildirs;
@ -19,20 +17,10 @@ int nmaildirs;
void
initplumb(void)
{
showfd = plumbopen("send", OWRITE);
seefd = plumbopen("seemail", OREAD);
if(seefd < 0){
logfd = open(unsharp("#9/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);
}
showfd = plumbopenfid("send", OWRITE);
seefd = plumbopenfid("seemail", OREAD);
if(showfd == nil || seefd == nil)
sysfatal("plumbopen: %r");
}
void
@ -75,7 +63,7 @@ showmail(Face *f)
pm.attr = &a;
pm.ndata = strlen(s);
pm.data = s;
plumbsend(showfd,&pm);
plumbsendtofid(showfd, &pm);
}
char*
@ -114,43 +102,6 @@ setname(Face *f, char *sender)
}
}
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<n-1; i++){
c = getc();
if(c <= 0)
return nil;
if(c == '\n')
break;
buf[i] = c;
}
buf[i] = '\0';
return buf;
}
static char* months[] = {
"jan", "feb", "mar", "apr",
"may", "jun", "jul", "aug",
@ -216,42 +167,6 @@ parsedate(char *s)
return parsedatev(f);
}
/* achille Jul 23 14:05:15 delivered jmk From ms.com!bub Fri Jul 23 14:05:14 EDT 1999 (plan9.bell-labs.com!jmk) 1352 */
/* achille Oct 26 13:45:42 remote local!rsc From rsc Sat Oct 26 13:45:41 EDT 2002 (rsc) 170 */
int
parselog(char *s, char **sender, ulong *xtime)
{
char *f[20];
int nf;
nf = getfields(s, f, nelem(f), 1, " ");
if(nf < 14)
return 0;
if(strcmp(f[4], "delivered") == 0 && strcmp(f[5], user) == 0)
goto Found;
if(strcmp(f[4], "remote") == 0 && strncmp(f[5], "local!", 6) == 0 && strcmp(f[5]+6, user) == 0)
goto Found;
return 0;
Found:
*sender = estrdup(f[7]);
*xtime = parsedatev(&f[8]);
return 1;
}
int
logrecv(char **sender, ulong *xtime)
{
char buf[4096];
for(;;){
if(getline(buf, sizeof buf) == nil)
return 0;
if(parselog(buf, sender, xtime))
return 1;
}
return -1;
}
char*
tweakdate(char *d)
@ -280,49 +195,36 @@ nextface(void)
f = emalloc(sizeof(Face));
for(;;){
if(seefd >= 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; i<nmaildirs; i++) {
if(strncmp(m->data,"/mail/fs/",strlen("/mail/fs/")) == 0)
m->data += strlen("/mail/fs/");
if(strncmp(m->data, maildirs[i], strlen(maildirs[i])) == 0)
goto Found;
}
m = plumbrecvfid(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; i<nmaildirs; i++) { /* XXX */
if(strncmp(m->data,"/mail/fs/",strlen("/mail/fs/")) == 0)
m->data += strlen("/mail/fs/");
if(strncmp(m->data, 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;
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;
return f;
senderp = estrdup(value(m->attr, "sender", "???"));
showmailp = estrdup(m->data);
if(digestp)
digestp = estrdup(digestp);
plumbfree(m);
}
return nil;
}
@ -332,6 +234,8 @@ iline(char *data, char **pp)
{
char *p;
if(*data == 0)
return nil;
for(p=data; *p!='\0' && *p!='\n'; p++)
;
if(*p == '\n')
@ -344,28 +248,16 @@ Face*
dirface(char *dir, char *num)
{
Face *f;
char *from, *date;
char buf[1024], *info, *p, *digest;
char buf[1024], *fld[3], *info, *p, *t, *s;
int n;
ulong len;
CFid *fid;
#if 0
/*
* 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);
#endif
sprint(buf, "%s/%s/info", dir, num);
len = fsdirlen(upasfs, buf);
len = fsdirlen(mailfs, buf);
if(len <= 0)
return nil;
fid = fsopen(upasfs,buf, OREAD);
fid = fsopen(mailfs, buf, OREAD);
if(fid == nil)
return nil;
info = emalloc(len+1);
@ -377,22 +269,22 @@ dirface(char *dir, char *num)
}
info[n] = '\0';
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 */
setname(f, estrdup(from));
f->time = parsedate(date);
f->tm = *localtime(f->time);
for(p=info; (s=iline(p, &p)) != nil; ){
t = strchr(s, ' ');
if(t == nil)
continue;
*t++ = 0;
if(strcmp(s, "unixdate") == 0){
f->time = atoi(t);
f->tm = *localtime(f->time);
}
else if(strcmp(s, "from") == 0 && tokenize(t, fld, 3) >= 2)
setname(f, estrdup(fld[1]));
else if(strcmp(s, "digest") == 0)
f->str[Sdigest] = estrdup(t);
}
sprint(buf, "%s/%s", dir, num);
f->str[Sshow] = estrdup(buf);
iline(p, &p); /* subject */
iline(p, &p); /* mime content type */
iline(p, &p); /* mime disposition */
iline(p, &p); /* filename */
digest = iline(p, &p); /* digest */
f->str[Sdigest] = estrdup(digest);
free(info);
return f;
}