#include <u.h>
#include <libc.h>
#include <draw.h>
#include <plumb.h>
#include <regexp.h>
#include <bio.h>
#include "faces.h"
static int showfd = -1;
static int logfd = -1;
static char *user;
static char *logtag;
void
initplumb(void)
{
showfd = plumbopen("send", OWRITE);
}
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<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",
"sep", "oct", "nov", "dec"
};
static int
getmon(char *s)
{
int i;
for(i=0; i<nelem(months); i++)
if(cistrcmp(months[i], s) == 0)
return i;
return -1;
}
/* Fri Jul 23 14:05:14 EDT 1999 */
ulong
parsedatev(char **a)
{
char *p;
Tm tm;
memset(&tm, 0, sizeof tm);
if((tm.mon=getmon(a[1])) == -1)
goto Err;
tm.mday = strtol(a[2], &p, 10);
if(*p != '\0')
goto Err;
tm.hour = strtol(a[3], &p, 10);
if(*p != ':')
goto Err;
tm.min = strtol(p+1, &p, 10);
if(*p != ':')
goto Err;
tm.sec = strtol(p+1, &p, 10);
if(*p != '\0')
goto Err;
if(strlen(a[4]) != 3)
goto Err;
strcpy(tm.zone, a[4]);
if(strlen(a[5]) != 4)
goto Err;
tm.year = strtol(a[5], &p, 10);
if(*p != '\0')
goto Err;
tm.year -= 1900;
return tm2sec(&tm);
Err:
return time(0);
}
ulong
parsedate(char *s)
{
char *f[10];
int nf;
nf = getfields(s, f, nelem(f), 1, " ");
if(nf < 6)
return time(0);
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;
}
}
Face *
dirface(char *name, ulong mtime)
{
Face *f;
f = emalloc(sizeof(Face));
setname(f, estrdup(name));
f->time = mtime;
f->tm = *localtime(f->time);
f->str[Sshow] = estrdup(name);
return f;
}
|