2 * Locking here is not quite right.
3 * Calling qlock(&z->lk) can block the proc,
4 * and when it comes back, boxes and msgs might have been freed
5 * (if the refresh proc was holding the lock and in the middle of a
6 * redial). I've tried to be careful about not assuming boxes continue
7 * to exist across imap commands, but maybe this isn't really tenable.
8 * Maybe instead we should ref count the boxes and messages.
18 int ticks; /* until boom! */
44 "NonJunk", FlagNonJunk,
45 "\\Answered", FlagReplied,
46 "\\Flagged", FlagFlagged,
47 "\\Deleted", FlagDeleted,
49 "\\Recent", FlagRecent,
51 "\\NoInferiors", FlagNoInferiors,
52 "\\NoSelect", FlagNoSelect,
53 "\\Marked", FlagMarked,
54 "\\UnMarked", FlagUnMarked
59 static char *tag = "#";
61 static void checkbox(Imap*, Box*);
62 static char* copyaddrs(Sx*);
63 static void freeup(UserPasswd*);
64 static int getbox(Imap*, Box*);
65 static int getboxes(Imap*);
66 static char* gsub(char*, char*, char*);
67 static int imapcmd(Imap*, Box*, char*, ...);
68 static Sx* imapcmdsx(Imap*, Box*, char*, ...);
69 static Sx* imapcmdsx0(Imap*, char*, ...);
70 static Sx* imapvcmdsx(Imap*, Box*, char*, va_list);
71 static Sx* imapvcmdsx0(Imap*, char*, va_list);
72 static int imapdial(char*, int);
73 static int imaplogin(Imap*);
74 static int imapquote(Fmt*);
75 static int imapreconnect(Imap*);
76 static void imaprefreshthread(void*);
77 static void imaptimerproc(void*);
78 static Sx* imapwaitsx(Imap*);
79 static int isatom(Sx *v, char *name);
80 static int islist(Sx *v);
81 static int isnil(Sx *v);
82 static int isnumber(Sx *sx);
83 static int isstring(Sx *sx);
84 static int ioimapdial(Ioproc*, char*, int);
85 static char* nstring(Sx*);
86 static void unexpected(Imap*, Sx*);
87 static Sx* zBrdsx(Imap*);
90 * Imap connection maintenance and login.
94 imapconnect(char *server, int mode, char *root)
98 fmtinstall('H', encodefmt);
99 fmtinstall('Z', imapquote);
101 z = emalloc(sizeof *z);
102 z->server = estrdup(server);
105 if(root[0] != 0 && root[strlen(root)-1] != '/')
106 z->root = smprint("%s/", root);
112 z->autoreconnect = 0;
116 if(imapreconnect(z) < 0){
122 z->autoreconnect = 1;
125 proccreate(imaptimerproc, z, STACK);
126 mailthread(imaprefreshthread, z);
132 imaphangup(Imap *z, int ticks)
142 imapreconnect(Imap *z)
146 z->autoreconnect = 0;
156 fprint(2, "dial %s...\n", z->server);
157 if((z->fd = ioimapdial(z->io, z->server, z->mode)) < 0)
160 Binit(&z->b, z->fd, OREAD);
161 if((sx = zBrdsx(z)) == nil){
162 werrstr("no greeting");
166 fprint(2, "<I %#$\n", sx);
167 if(sx->nsx >= 2 && isatom(sx->sx[0], "*") && isatom(sx->sx[1], "PREAUTH")){
172 werrstr("bad greeting - %#$", sx);
180 if(getboxes(z) < 0 || getbox(z, z->inbox) < 0)
182 z->autoreconnect = 1;
192 z->autoreconnect = 1;
203 if((up = auth_getuserpasswd(auth_getkey, "proto=pass role=client service=imap server=%q", z->server)) == nil){
204 werrstr("getuserpasswd - %r");
208 sx = imapcmdsx(z, nil, "LOGIN %Z %Z", up->user, up->passwd);
214 werrstr("login rejected - %#$", sx);
226 for(i=0; i<nboxes; i++){
228 boxes[i]->exists = 0;
229 boxes[i]->maxseen = 0;
231 if(imapcmd(z, nil, "LIST %Z *", z->root) < 0)
233 if(z->root != nil && imapcmd(z, nil, "LIST %Z INBOX", "") < 0)
235 if(z->nextbox && z->nextbox->mark)
237 for(r=boxes, w=boxes, e=boxes+nboxes; r<e; r++){
239 {fprint(2, "*** free box %s %s\n", (*r)->name, (*r)->imapname);
250 getbox(Imap *z, Box *b)
258 for(i=0; i<b->nmsg; i++)
259 b->msg[i]->imapid = 0;
260 if(imapcmd(z, b, "UID FETCH 1:* FLAGS") < 0)
262 for(r=b->msg, w=b->msg, e=b->msg+b->nmsg; r<e; r++){
263 if((*r)->imapid == 0)
270 b->nmsg = w - b->msg;
277 freeup(UserPasswd *up)
279 memset(up->user, 0, strlen(up->user));
280 memset(up->passwd, 0, strlen(up->passwd));
285 imaptimerproc(void *v)
299 checkbox(Imap *z, Box *b)
301 if(imapcmd(z, b, "NOOP") >= 0){
306 if(b==z->box && b->exists > b->maxseen){
307 imapcmd(z, b, "UID FETCH %d:* FULL",
314 imaprefreshthread(void *v)
326 checkbox(z, z->inbox);
332 * Run a single command and return the Sx. Does NOT redial.
335 imapvcmdsx0(Imap *z, char *fmt, va_list arg)
345 if(z->fd < 0 || !z->connected)
348 prefix = strlen(tag)+1;
350 fmtprint(&f, "%s ", tag);
351 fmtvprint(&f, fmt, arg);
352 fmtprint(&f, "\r\n");
357 fprint(2, "I> %s\n", s);
359 if(iowrite(z->io, z->fd, s, len) < 0){
370 imapcmdsx0(Imap *z, char *fmt, ...)
376 sx = imapvcmdsx0(z, fmt, arg);
382 * Run a single command on box b. Does redial.
385 imapvcmdsx(Imap *z, Box *b, char *fmt, va_list arg)
393 if(z->fd < 0 || !z->connected){
395 if(!z->autoreconnect)
397 if(imapreconnect(z) < 0)
399 if(b && z->nextbox == nil) /* box disappeared on reconnect */
403 if(b && b != z->box){
405 z->box->imapinit = 0;
407 if((sx=imapcmdsx0(z, "SELECT %Z", b->imapname)) == nil){
409 if(tries++ == 0 && (z->fd < 0 || !z->connected))
416 if((sx=imapvcmdsx0(z, fmt, arg)) == nil){
417 if(tries++ == 0 && (z->fd < 0 || !z->connected))
425 imapcmd(Imap *z, Box *b, char *fmt, ...)
431 sx = imapvcmdsx(z, b, fmt, arg);
435 if(sx->nsx < 2 || !isatom(sx->sx[1], "OK")){
445 imapcmdsx(Imap *z, Box *b, char *fmt, ...)
451 sx = imapvcmdsx(z, b, fmt, arg);
461 while((sx = zBrdsx(z)) != nil){
463 fprint(2, "<| %#$\n", sx);
464 if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && cistrcmp(sx->sx[0]->data, tag) == 0)
466 if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && strcmp(sx->sx[0]->data, "*") == 0)
468 if(sx->type == SxList && sx->nsx == 0){
479 * Imap interface to mail file system.
483 _bodyname(char *buf, char *ebuf, Part *p, char *extra)
486 fprint(2, "***** BUFFER TOO SMALL\n");
491 _bodyname(buf, ebuf, p->parent, "");
493 seprint(buf, ebuf, ".%d", p->pix+1);
496 seprint(buf, ebuf, "%s", extra);
500 bodyname(Part *p, char *extra)
502 static char buf[256];
503 memset(buf, 0, sizeof buf); /* can't see why this is necessary, but it is */
504 _bodyname(buf, buf+sizeof buf, p, extra);
505 return buf+1; /* buf[0] == '.' */
509 fetch1(Imap *z, Part *p, char *s)
512 imapcmd(z, p->msg->box, "UID FETCH %d BODY[%s]",
513 p->msg->imapuid, bodyname(p, s));
518 imapfetchrawheader(Imap *z, Part *p)
520 fetch1(z, p, ".HEADER");
524 imapfetchrawmime(Imap *z, Part *p)
526 fetch1(z, p, ".MIME");
530 imapfetchrawbody(Imap *z, Part *p)
532 fetch1(z, p, ".TEXT");
536 imapfetchraw(Imap *z, Part *p)
542 imaplistcmd(Imap *z, Box *box, char *before, Msg **m, uint nm, char *after)
552 fmtprint(&fmt, "%s ", before);
556 fmtprint(&fmt, "%ud", m[i]->imapuid);
558 fmtprint(&fmt, " %s", after);
559 cmd = fmtstrflush(&fmt);
562 if(imapcmd(z, box, "%s", cmd) < 0)
569 imapcopylist(Imap *z, char *nbox, Msg **m, uint nm)
578 if(strcmp(nbox, "mbox") == 0)
579 name = estrdup("INBOX");
581 p = esmprint("%s%s", z->root, nbox);
582 name = esmprint("%Z", p);
585 rv = imaplistcmd(z, m[0]->box, "UID COPY", m, nm, name);
592 imapremovelist(Imap *z, Msg **m, uint nm)
600 rv = imaplistcmd(z, m[0]->box, "UID STORE", m, nm, "+FLAGS.SILENT (\\Deleted)");
601 /* careful - box might be gone; use z->box instead */
602 if(rv == 0 && z->box)
603 rv = imapcmd(z, z->box, "EXPUNGE");
609 imapflaglist(Imap *z, int op, int flag, Msg **m, uint nm)
623 fmtprint(&fmt, "%sFLAGS (", mod);
625 for(i=0; i<nelem(flagstab); i++){
626 if(flagstab[i].flag & flag){
627 fmtprint(&fmt, "%s%s", sep, flagstab[i].name);
632 s = fmtstrflush(&fmt);
635 rv = imaplistcmd(z, m[0]->box, "UID STORE", m, nm, s);
642 imapsearchbox(Imap *z, Box *b, char *search, Msg ***mm)
650 if(imapcmd(z, b, "UID SEARCH CHARSET UTF-8 TEXT %Z", search) < 0){
661 m = emalloc(nuid*sizeof m[0]);
663 for(i=0; i<nuid; i++)
664 if((m[nm] = msgbyimapuid(b, uid[i], 0)) != nil)
672 imapcheckbox(Imap *z, Box *b)
682 * Imap utility routines
685 _ioimapdial(va_list *arg)
690 server = va_arg(*arg, char*);
691 mode = va_arg(*arg, int);
692 return imapdial(server, mode);
695 ioimapdial(Ioproc *io, char *server, int mode)
697 return iocall(io, _ioimapdial, server, mode);
701 _ioBrdsx(va_list *arg)
706 b = va_arg(*arg, Biobuf*);
707 sx = va_arg(*arg, Sx**);
709 if((*sx) && (*sx)->type == SxList && (*sx)->nsx == 0){
716 ioBrdsx(Ioproc *io, Biobuf *b)
720 iocall(io, _ioBrdsx, b, &sx);
727 if(z->ticks && --z->ticks==0){
732 return ioBrdsx(z->io, &z->b);
736 imapdial(char *server, int mode)
745 return dial(netmkaddr(server, "tcp", "143"), nil, nil, nil);
748 werrstr("starttls not supported");
754 fd[0] = dup(p[0], -1);
755 fd[1] = dup(p[0], -1);
757 tmp = esmprint("%s:993", server);
758 if(threadspawnl(fd, "tlsclient", "tlsclient", tmp, nil) < 0
759 && threadspawnl(fd, "/usr/sbin/stunnel3", "stunnel3", "-c", "-r", tmp, nil) < 0
760 && threadspawnl(fd, "/usr/bin/stunnel3", "stunnel3", "-c", "-r", tmp, nil) < 0
761 && threadspawnl(fd, "/usr/sbin/stunnel", "stunnel", "-c", "-r", tmp, nil) < 0
762 && threadspawnl(fd, "/usr/bin/stunnel", "stunnel", "-c", "-r", tmp, nil) < 0){
778 fd[0] = dup(p[0], -1);
779 fd[1] = dup(p[0], -1);
781 if(threadspawnl(fd, "/usr/local/plan9/bin/rc", "rc", "-c", server, nil) < 0){
808 if(r=='\\' || r=='"')
820 s = va_arg(f->args, char*);
821 if(s == nil || *s == '\0')
822 return fmtstrcpy(f, "\"\"");
825 if(f->flags&FmtSharp)
828 w = chartorune(&r, t);
829 quotes |= needtoquote(r);
832 return fmtstrcpy(f, s);
836 w = chartorune(&r, t);
837 if(needtoquote(r) == Qbackslash)
841 return fmtrune(f, '"');
862 * Check S expression against format string.
865 sxmatch(Sx *sx, char *fmt)
869 for(i=0; fmt[i]; i++){
871 fmt--; /* like i-- but better */
872 if(i == sx->nsx && fmt[i+1] == '*')
878 if(sx->sx[i]->type == SxAtom && strcmp(sx->sx[i]->data, "NIL") == 0){
880 free(sx->sx[i]->data);
881 sx->sx[i]->data = nil;
882 sx->sx[i]->type = SxList;
886 else if(fmt[i] == 'S'){
887 free(sx->sx[i]->data);
888 sx->sx[i]->data = nil;
889 sx->sx[i]->type = SxString;
892 if(sx->sx[i]->type == SxAtom && fmt[i]=='S')
893 sx->sx[i]->type = SxString;
894 if(sx->sx[i]->type != fmttype(fmt[i])){
895 fprint(2, "sxmatch: %$ not %c\n", sx->sx[i], fmt[i]);
905 * Check string against format string.
908 stringmatch(char *fmt, char *s)
910 for(; *fmt && *s; fmt++, s++){
917 if(*s < '0' || *s > '9')
921 if(*s < 'A' || *s > 'Z')
925 if(*s < 'a' || *s > 'z')
929 if(*s != '-' && *s != '+')
944 * Parse simple S expressions and IMAP elements.
947 isatom(Sx *v, char *name)
951 if(v == nil || v->type != SxAtom)
954 if(cistrncmp(v->data, name, n) == 0)
955 if(v->data[n] == 0 || (n>0 && v->data[n-1] == '['))
963 if(sx->type == SxAtom)
965 return sx->type == SxString;
971 return sx->type == SxNumber;
978 (v->type==SxList && v->nsx == 0) ||
979 (v->type==SxAtom && strcmp(v->data, "NIL") == 0);
985 return isnil(v) || v->type==SxList;
993 if(v->type != SxList){
994 warn("malformed flags: %$", v);
998 for(i=0; i<v->nsx; i++){
999 if(v->sx[i]->type != SxAtom)
1001 for(j=0; j<nelem(flagstab); j++)
1002 if(cistrcmp(v->sx[i]->data, flagstab[j].name) == 0)
1003 f |= flagstab[j].flag;
1008 static char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
1014 for(i=0; months[i]; i+=3)
1015 if(memcmp(s, months+i, 3) == 0)
1028 if(v->type != SxString || !stringmatch("01-Aaa-1111 01:11:11 +1111", v->data)){
1030 warn("bad date: %$", v);
1034 /* cannot use atoi because 09 is malformed octal! */
1035 memset(&tm, 0, sizeof tm);
1037 tm.mday = strtol(p, 0, 10);
1038 tm.mon = parsemon(p+3);
1041 tm.year = strtol(p+7, 0, 10) - 1900;
1042 tm.hour = strtol(p+12, 0, 10);
1043 tm.min = strtol(p+15, 0, 10);
1044 tm.sec = strtol(p+18, 0, 10);
1045 strcpy(tm.zone, "GMT");
1048 delta = ((p[22]-'0')*10+p[23]-'0')*3600 + ((p[24]-'0')*10+p[25]-'0')*60;
1059 if(v->type != SxNumber)
1065 hash(DigestState *ds, char *tag, char *val)
1069 md5((uchar*)tag, strlen(tag)+1, nil, ds);
1070 md5((uchar*)val, strlen(val)+1, nil, ds);
1074 parseenvelope(Sx *v)
1080 if(v->type != SxList || !sxmatch(v, "SSLLLLLLSS")){
1081 warn("bad envelope: %$", v);
1085 hdr = emalloc(sizeof *hdr);
1086 hdr->date = nstring(v->sx[0]);
1087 hdr->subject = unrfc2047(nstring(v->sx[1]));
1088 hdr->from = copyaddrs(v->sx[2]);
1089 hdr->sender = copyaddrs(v->sx[3]);
1090 hdr->replyto = copyaddrs(v->sx[4]);
1091 hdr->to = copyaddrs(v->sx[5]);
1092 hdr->cc = copyaddrs(v->sx[6]);
1093 hdr->bcc = copyaddrs(v->sx[7]);
1094 hdr->inreplyto = unrfc2047(nstring(v->sx[8]));
1095 hdr->messageid = unrfc2047(nstring(v->sx[9]));
1097 memset(&ds, 0, sizeof ds);
1098 hash(&ds, "date", hdr->date);
1099 hash(&ds, "subject", hdr->subject);
1100 hash(&ds, "from", hdr->from);
1101 hash(&ds, "sender", hdr->sender);
1102 hash(&ds, "replyto", hdr->replyto);
1103 hash(&ds, "to", hdr->to);
1104 hash(&ds, "cc", hdr->cc);
1105 hash(&ds, "bcc", hdr->bcc);
1106 hash(&ds, "inreplyto", hdr->inreplyto);
1107 hash(&ds, "messageid", hdr->messageid);
1108 md5(0, 0, digest, &ds);
1109 hdr->digest = esmprint("%.16H", digest);
1122 if('A' <= *t && *t <= 'Z')
1140 * substitute all occurrences of a with b in s.
1143 gsub(char *s, char *a, char *b)
1145 char *p, *t, *w, *last;
1149 for(p=s; (p=strstr(p, a)) != nil; p+=strlen(a))
1153 t = emalloc(strlen(s)+n*strlen(b)+1);
1155 for(p=s; last=p, (p=strstr(p, a)) != nil; p+=strlen(a)){
1156 memmove(w, last, p-last);
1158 memmove(w, b, strlen(b));
1167 * Table-driven IMAP "unexpected response" parser.
1168 * All the interesting data is in the unexpected responses.
1170 static void xlist(Imap*, Sx*);
1171 static void xrecent(Imap*, Sx*);
1172 static void xexists(Imap*, Sx*);
1173 static void xok(Imap*, Sx*);
1174 static void xflags(Imap*, Sx*);
1175 static void xfetch(Imap*, Sx*);
1176 static void xexpunge(Imap*, Sx*);
1177 static void xbye(Imap*, Sx*);
1178 static void xsearch(Imap*, Sx*);
1184 void (*fn)(Imap*, Sx*);
1186 0, "BYE", nil, xbye,
1187 0, "FLAGS", "AAL", xflags,
1188 0, "LIST", "AALSS", xlist,
1190 0, "SEARCH", "AAN*", xsearch,
1192 1, "EXISTS", "ANA", xexists,
1193 1, "EXPUNGE", "ANA", xexpunge,
1194 1, "FETCH", "ANAL", xfetch,
1195 1, "RECENT", "ANA", xrecent
1199 unexpected(Imap *z, Sx *sx)
1204 if(sx->nsx >= 3 && sx->sx[1]->type == SxNumber && sx->sx[2]->type == SxAtom){
1206 name = sx->sx[2]->data;
1207 }else if(sx->nsx >= 2 && sx->sx[1]->type == SxAtom){
1209 name = sx->sx[1]->data;
1213 for(i=0; i<nelem(unextab); i++){
1214 if(unextab[i].num == num && cistrcmp(unextab[i].name, name) == 0){
1215 if(unextab[i].fmt && !sxmatch(sx, unextab[i].fmt)){
1216 warn("malformed %s: %$", name, sx);
1219 unextab[i].fn(z, sx);
1234 xlist(Imap *z, Sx *sx)
1240 s = estrdup(sx->sx[4]->data);
1241 if(sx->sx[3]->data && strcmp(sx->sx[3]->data, "/") != 0){
1242 s = gsub(s, "/", "_");
1243 s = gsub(s, sx->sx[3]->data, "/");
1247 * INBOX is the special imap name for the main mailbox.
1248 * All other mailbox names have the root prefix removed, if applicable.
1251 if(cistrcmp(s, "INBOX") == 0){
1254 s = estrdup("mbox");
1255 } else if(z->root && strstr(s, z->root) == s) {
1256 t = estrdup(s+strlen(z->root));
1262 * Plan 9 calls the main mailbox mbox.
1263 * Rename any existing mbox by appending a $.
1265 if(!inbox && strncmp(s, "mbox", 4) == 0 && alldollars(s+4)){
1266 t = emalloc(strlen(s)+2);
1276 box->imapname = estrdup(sx->sx[4]->data);
1280 box->flags = parseflags(sx->sx[2]);
1284 xrecent(Imap *z, Sx *sx)
1287 z->box->recent = sx->sx[1]->number;
1291 xexists(Imap *z, Sx *sx)
1294 z->box->exists = sx->sx[1]->number;
1295 if(z->box->exists < z->box->maxseen)
1296 z->box->maxseen = z->box->exists;
1301 xflags(Imap *z, Sx *sx)
1304 * This response contains in sx->sx[2] the list of flags
1305 * that can be validly attached to messages in z->box.
1306 * We don't have any use for this list, since we
1307 * use only the standard flags.
1312 xbye(Imap *z, Sx *sx)
1320 xexpunge(Imap *z, Sx *sx)
1325 if((b=z->box) == nil)
1327 n = sx->sx[1]->number;
1328 for(i=0; i<b->nmsg; i++){
1329 if(b->msg[i]->imapid == n){
1330 msgplumb(b->msg[i], 1);
1333 memmove(b->msg+i, b->msg+i+1, (b->nmsg-i)*sizeof b->msg[0]);
1339 if(b->msg[i]->imapid > n)
1340 b->msg[i]->imapid--;
1346 xsearch(Imap *z, Sx *sx)
1351 z->uid = emalloc((sx->nsx-2)*sizeof z->uid[0]);
1352 z->nuid = sx->nsx-2;
1353 for(i=0; i<z->nuid; i++)
1354 z->uid[i] = sx->sx[i+2]->number;
1358 * Table-driven FETCH message info parser.
1360 static void xmsgflags(Msg*, Sx*, Sx*);
1361 static void xmsgdate(Msg*, Sx*, Sx*);
1362 static void xmsgrfc822size(Msg*, Sx*, Sx*);
1363 static void xmsgenvelope(Msg*, Sx*, Sx*);
1364 static void xmsgbody(Msg*, Sx*, Sx*);
1365 static void xmsgbodydata(Msg*, Sx*, Sx*);
1369 void (*fn)(Msg*, Sx*, Sx*);
1372 "INTERNALDATE", xmsgdate,
1373 "RFC822.SIZE", xmsgrfc822size,
1374 "ENVELOPE", xmsgenvelope,
1376 "BODY[", xmsgbodydata
1380 xfetch(Imap *z, Sx *sx)
1386 warn("FETCH but no open box: %$", sx);
1390 /* * 152 FETCH (UID 185 FLAGS () ...) */
1391 if(sx->sx[3]->nsx%2){
1392 warn("malformed FETCH: %$", sx);
1396 n = sx->sx[1]->number;
1398 for(i=0; i<sx->nsx; i+=2){
1399 if(isatom(sx->sx[i], "UID")){
1400 if(sx->sx[i+1]->type == SxNumber){
1401 uid = sx->sx[i+1]->number;
1406 /* This happens: too bad.
1407 warn("FETCH without UID: %$", sx);
1412 msg = msgbyimapuid(z->box, uid, 1);
1413 if(msg->imapid && msg->imapid != n)
1414 warn("msg id mismatch: want %d have %d", msg->id, n);
1416 for(i=0; i<sx->nsx; i+=2){
1417 for(j=0; j<nelem(msgtab); j++)
1418 if(isatom(sx->sx[i], msgtab[j].name))
1419 msgtab[j].fn(msg, sx->sx[i], sx->sx[i+1]);
1424 xmsgflags(Msg *msg, Sx *k, Sx *v)
1427 msg->flags = parseflags(v);
1431 xmsgdate(Msg *msg, Sx *k, Sx *v)
1434 msg->date = parsedate(v);
1438 xmsgrfc822size(Msg *msg, Sx *k, Sx *v)
1441 msg->size = parsenumber(v);
1460 char *name, *email, *host, *mbox;
1469 for(i=0; i<v->nsx; i++){
1470 if(!sxmatch(v->sx[i], "SSSS"))
1471 warn("bad address: %$", v->sx[i]);
1472 name = unrfc2047(nstring(v->sx[i]->sx[0]));
1473 /* ignore sx[1] - route */
1474 mbox = unrfc2047(nstring(v->sx[i]->sx[2]));
1475 host = unrfc2047(nstring(v->sx[i]->sx[3]));
1476 if(mbox == nil || host == nil){ /* rfc822 group syntax */
1482 email = esmprint("%s@%s", mbox, host);
1485 fmtprint(&fmt, "%s%q %q", sep, name ? name : "", email ? email : "");
1490 s = fmtstrflush(&fmt);
1492 sysfatal("out of memory");
1497 xmsgenvelope(Msg *msg, Sx *k, Sx *v)
1499 hdrfree(msg->part[0]->hdr);
1500 msg->part[0]->hdr = parseenvelope(v);
1508 "charset", offsetof(Part, charset),
1509 "name", offsetof(Part, filename)
1513 parseparams(Part *part, Sx *v)
1521 warn("bad message params: %$", v);
1524 for(i=0; i<v->nsx; i+=2){
1525 s = nstring(v->sx[i]);
1526 t = nstring(v->sx[i+1]);
1527 for(j=0; j<nelem(paramtab); j++){
1528 if(cistrcmp(paramtab[j].name, s) == 0){
1529 p = (char**)((char*)part+paramtab[j].offset);
1542 parsestructure(Part *part, Sx *v)
1549 if(v->type != SxList){
1551 warn("bad structure: %$", v);
1554 if(islist(v->sx[0])){
1556 for(i=0; i<v->nsx && islist(v->sx[i]); i++)
1557 parsestructure(partcreate(part->msg, part), v->sx[i]);
1559 if(i != v->nsx-1 || !isstring(v->sx[i])){
1560 warn("bad multipart structure: %$", v);
1561 part->type = estrdup("multipart/mixed");
1564 s = nstring(v->sx[i]);
1566 part->type = esmprint("multipart/%s", s);
1571 if(!isstring(v->sx[0]) || v->nsx < 2)
1573 s = nstring(v->sx[0]);
1574 t = nstring(v->sx[1]);
1578 part->type = esmprint("%s/%s", s, t);
1579 if(v->nsx < 7 || !islist(v->sx[2]) || !isstring(v->sx[3])
1580 || !isstring(v->sx[4]) || !isstring(v->sx[5]) || !isnumber(v->sx[6]))
1582 parseparams(part, v->sx[2]);
1583 part->idstr = nstring(v->sx[3]);
1584 part->desc = nstring(v->sx[4]);
1585 part->encoding = nstring(v->sx[5]);
1586 part->size = v->sx[6]->number;
1587 if(strcmp(s, "message") == 0 && strcmp(t, "rfc822") == 0){
1588 if(v->nsx < 10 || !islist(v->sx[7]) || !islist(v->sx[8]) || !isnumber(v->sx[9]))
1590 part->hdr = parseenvelope(v->sx[7]);
1591 parsestructure(partcreate(part->msg, part), v->sx[8]);
1592 part->lines = v->sx[9]->number;
1594 if(strcmp(s, "text") == 0){
1595 if(v->nsx < 8 || !isnumber(v->sx[7]))
1597 part->lines = v->sx[7]->number;
1602 xmsgbody(Msg *msg, Sx *k, Sx *v)
1604 if(v->type != SxList){
1605 warn("bad body: %$", v);
1609 * To follow the structure exactly we should
1610 * be doing this to partcreate(msg, msg->part[0]),
1611 * and we should leave msg->part[0] with type message/rfc822,
1612 * but the extra layer is redundant - what else would be in a mailbox?
1614 parsestructure(msg->part[0], v);
1615 if(msg->box->maxseen < msg->imapid)
1616 msg->box->maxseen = msg->imapid;
1617 if(msg->imapuid >= msg->box->uidnext)
1618 msg->box->uidnext = msg->imapuid+1;
1622 xmsgbodydata(Msg *msg, Sx *k, Sx *v)
1629 name += 5; /* body[ */
1630 p = strchr(name, ']');
1634 /* now name is something like 1 or 3.2.MIME - walk down parts from root */
1635 part = msg->part[0];
1638 while('1' <= name[0] && name[0] <= '9'){
1639 i = strtol(name, &p, 10);
1643 warn("bad body name: %$", k);
1646 if((part = subpart(part, i-1)) == nil){
1647 warn("unknown body part: %$", k);
1654 if(cistrcmp(name, "") == 0){
1656 part->raw = nstring(v);
1658 }else if(cistrcmp(name, "HEADER") == 0){
1659 free(part->rawheader);
1660 part->rawheader = nstring(v);
1661 nocr(part->rawheader);
1662 }else if(cistrcmp(name, "MIME") == 0){
1663 free(part->mimeheader);
1664 part->mimeheader = nstring(v);
1665 nocr(part->mimeheader);
1666 }else if(cistrcmp(name, "TEXT") == 0){
1667 free(part->rawbody);
1668 part->rawbody = nstring(v);
1669 nocr(part->rawbody);
1674 * Table-driven OK info parser.
1676 static void xokuidvalidity(Imap*, Sx*);
1677 static void xokpermflags(Imap*, Sx*);
1678 static void xokunseen(Imap*, Sx*);
1679 static void xokreadwrite(Imap*, Sx*);
1680 static void xokreadonly(Imap*, Sx*);
1685 void (*fn)(Imap*, Sx*);
1687 "UIDVALIDITY", 'N', xokuidvalidity,
1688 "PERMANENTFLAGS", 'L', xokpermflags,
1689 "UNSEEN", 'N', xokunseen,
1690 "READ-WRITE", 0, xokreadwrite,
1691 "READ-ONLY", 0, xokreadonly
1695 xok(Imap *z, Sx *sx)
1701 if(sx->nsx >= 4 && sx->sx[2]->type == SxAtom && sx->sx[2]->data[0] == '['){
1702 if(sx->sx[3]->type == SxAtom && sx->sx[3]->data[0] == ']')
1704 else if(sx->sx[4]->type == SxAtom && sx->sx[4]->data[0] == ']')
1707 warn("cannot parse OK: %$", sx);
1710 name = sx->sx[2]->data+1;
1711 for(i=0; i<nelem(oktab); i++){
1712 if(cistrcmp(name, oktab[i].name) == 0){
1713 if(oktab[i].fmt && (arg==nil || arg->type != fmttype(oktab[i].fmt))){
1714 warn("malformed %s: %$", name, arg);
1717 oktab[i].fn(z, arg);
1724 xokuidvalidity(Imap *z, Sx *sx)
1729 if((b=z->box) == nil)
1731 if(b->validity != sx->number){
1732 b->validity = sx->number;
1734 for(i=0; i<b->nmsg; i++)
1743 xokpermflags(Imap *z, Sx *sx)
1745 /* z->permflags = parseflags(sx); */
1749 xokunseen(Imap *z, Sx *sx)
1751 /* z->unseen = sx->number; */
1755 xokreadwrite(Imap *z, Sx *sx)
1757 /* z->boxmode = ORDWR; */
1761 xokreadonly(Imap *z, Sx *sx)
1763 /* z->boxmode = OREAD; */