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! */
47 "NonJunk", FlagNonJunk,
48 "\\Answered", FlagReplied,
49 "\\Flagged", FlagFlagged,
50 "\\Deleted", FlagDeleted,
52 "\\Recent", FlagRecent,
54 "\\NoInferiors", FlagNoInferiors,
55 "\\NoSelect", FlagNoSelect,
56 "\\Marked", FlagMarked,
57 "\\UnMarked", FlagUnMarked
62 static char *tag = "#";
64 static void checkbox(Imap*, Box*);
65 static char* copyaddrs(Sx*);
66 static void freeup(UserPasswd*);
67 static int getbox(Imap*, Box*);
68 static int getboxes(Imap*);
69 static char* gsub(char*, char*, char*);
70 static int imapcmd(Imap*, Box*, char*, ...);
71 static Sx* imapcmdsxlit(Imap*, Box*, char*, ...);
72 static Sx* imapcmdsx(Imap*, Box*, char*, ...);
73 static Sx* imapcmdsx0(Imap*, char*, ...);
74 static Sx* imapvcmdsx(Imap*, Box*, char*, va_list, int);
75 static Sx* imapvcmdsx0(Imap*, char*, va_list, int);
76 static int imapdial(char*, int);
77 static int imaplogin(Imap*);
78 static int imapquote(Fmt*);
79 static int imapreconnect(Imap*);
80 static void imaprefreshthread(void*);
81 static void imaptimerproc(void*);
82 static Sx* imapwaitsx(Imap*);
83 static int isatom(Sx *v, char *name);
84 static int islist(Sx *v);
85 static int isnil(Sx *v);
86 static int isnumber(Sx *sx);
87 static int isstring(Sx *sx);
88 static int ioimapdial(Ioproc*, char*, int);
89 static char* nstring(Sx*);
90 static void unexpected(Imap*, Sx*);
91 static Sx* zBrdsx(Imap*);
94 * Imap connection maintenance and login.
98 imapconnect(char *server, int mode, char *root, char *user)
102 fmtinstall('H', encodefmt);
103 fmtinstall('Z', imapquote);
105 z = emalloc(sizeof *z);
106 z->server = estrdup(server);
110 if(root[0] != 0 && root[strlen(root)-1] != '/')
111 z->root = smprint("%s/", root);
117 z->autoreconnect = 0;
121 if(imapreconnect(z) < 0){
127 z->autoreconnect = 1;
130 proccreate(imaptimerproc, z, STACK);
131 mailthread(imaprefreshthread, z);
137 imaphangup(Imap *z, int ticks)
147 imapreconnect(Imap *z)
151 z->autoreconnect = 0;
161 fprint(2, "dial %s...\n", z->server);
162 if((z->fd = ioimapdial(z->io, z->server, z->mode)) < 0)
165 Binit(&z->b, z->fd, OREAD);
166 if((sx = zBrdsx(z)) == nil){
167 werrstr("no greeting");
171 fprint(2, "<I %#$\n", sx);
172 if(sx->nsx >= 2 && isatom(sx->sx[0], "*") && isatom(sx->sx[1], "PREAUTH")){
177 werrstr("bad greeting - %#$", sx);
185 if(getboxes(z) < 0 || getbox(z, z->inbox) < 0)
187 z->autoreconnect = 1;
197 z->autoreconnect = 1;
209 up = auth_getuserpasswd(auth_getkey, "proto=pass role=client service=imap server=%q user=%q", z->server, z->user);
211 up = auth_getuserpasswd(auth_getkey, "proto=pass role=client service=imap server=%q", z->server);
213 werrstr("getuserpasswd - %r");
217 sx = imapcmdsx(z, nil, "LOGIN %#Z %#Z", up->user, up->passwd);
223 werrstr("login rejected - %#$", sx);
235 for(i=0; i<nboxes; i++){
237 boxes[i]->exists = 0;
238 boxes[i]->maxseen = 0;
240 if(imapcmd(z, nil, "LIST %Z *", z->root) < 0)
242 if(z->root != nil && imapcmd(z, nil, "LIST %Z INBOX", "") < 0)
244 if(z->nextbox && z->nextbox->mark)
246 for(r=boxes, w=boxes, e=boxes+nboxes; r<e; r++){
248 {fprint(2, "*** free box %s %s\n", (*r)->name, (*r)->imapname);
259 getbox(Imap *z, Box *b)
267 for(i=0; i<b->nmsg; i++)
268 b->msg[i]->imapid = 0;
269 if(imapcmd(z, b, "UID FETCH 1:* FLAGS") < 0)
271 for(r=b->msg, w=b->msg, e=b->msg+b->nmsg; r<e; r++){
272 if((*r)->imapid == 0)
279 b->nmsg = w - b->msg;
286 freeup(UserPasswd *up)
288 memset(up->user, 0, strlen(up->user));
289 memset(up->passwd, 0, strlen(up->passwd));
294 imaptimerproc(void *v)
308 checkbox(Imap *z, Box *b)
310 if(imapcmd(z, b, "NOOP") >= 0){
315 if(b==z->box && b->exists > b->maxseen){
316 imapcmd(z, b, "UID FETCH %d:* FULL",
323 imaprefreshthread(void *v)
335 checkbox(z, z->inbox);
341 * Run a single command and return the Sx. Does NOT redial.
344 imapvcmdsx0(Imap *z, char *fmt, va_list arg, int dotag)
354 if(z->fd < 0 || !z->connected)
359 fmtprint(&f, "%s ", tag);
360 fmtvprint(&f, fmt, arg);
361 fmtprint(&f, "\r\n");
366 fprint(2, "I> %s\n", s);
368 if(iowrite(z->io, z->fd, s, len) < 0){
379 imapcmdsx0(Imap *z, char *fmt, ...)
385 sx = imapvcmdsx0(z, fmt, arg, 1);
391 * Run a single command on box b. Does redial.
394 imapvcmdsx(Imap *z, Box *b, char *fmt, va_list arg, int dotag)
402 if(z->fd < 0 || !z->connected){
404 if(!z->autoreconnect)
406 if(imapreconnect(z) < 0)
408 if(b && z->nextbox == nil) /* box disappeared on reconnect */
412 if(b && b != z->box){
414 z->box->imapinit = 0;
416 if((sx=imapcmdsx0(z, "SELECT %Z", b->imapname)) == nil){
418 if(tries++ == 0 && (z->fd < 0 || !z->connected))
425 if((sx=imapvcmdsx0(z, fmt, arg, dotag)) == nil){
426 if(tries++ == 0 && (z->fd < 0 || !z->connected))
434 imapcmd(Imap *z, Box *b, char *fmt, ...)
440 sx = imapvcmdsx(z, b, fmt, arg, 1);
444 if(sx->nsx < 2 || !isatom(sx->sx[1], "OK")){
454 imapcmdsx(Imap *z, Box *b, char *fmt, ...)
460 sx = imapvcmdsx(z, b, fmt, arg, 1);
466 imapcmdsxlit(Imap *z, Box *b, char *fmt, ...)
472 sx = imapvcmdsx(z, b, fmt, arg, 0);
482 while((sx = zBrdsx(z)) != nil){
484 fprint(2, "<| %#$\n", sx);
485 if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && cistrcmp(sx->sx[0]->data, tag) == 0)
487 if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && cistrcmp(sx->sx[0]->data, "+") == 0){
491 if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && strcmp(sx->sx[0]->data, "*") == 0)
493 if(sx->type == SxList && sx->nsx == 0){
504 * Imap interface to mail file system.
508 _bodyname(char *buf, char *ebuf, Part *p, char *extra)
511 fprint(2, "***** BUFFER TOO SMALL\n");
516 _bodyname(buf, ebuf, p->parent, "");
518 seprint(buf, ebuf, ".%d", p->pix+1);
521 seprint(buf, ebuf, "%s", extra);
525 bodyname(Part *p, char *extra)
527 static char buf[256];
528 memset(buf, 0, sizeof buf); /* can't see why this is necessary, but it is */
529 _bodyname(buf, buf+sizeof buf, p, extra);
530 return buf+1; /* buf[0] == '.' */
534 fetch1(Imap *z, Part *p, char *s)
537 imapcmd(z, p->msg->box, "UID FETCH %d BODY[%s]",
538 p->msg->imapuid, bodyname(p, s));
543 imapfetchrawheader(Imap *z, Part *p)
545 fetch1(z, p, ".HEADER");
549 imapfetchrawmime(Imap *z, Part *p)
551 fetch1(z, p, ".MIME");
555 imapfetchrawbody(Imap *z, Part *p)
557 fetch1(z, p, ".TEXT");
561 imapfetchraw(Imap *z, Part *p)
567 imaplistcmd(Imap *z, Box *box, char *before, Msg **m, uint nm, char *after)
577 fmtprint(&fmt, "%s ", before);
581 fmtprint(&fmt, "%ud", m[i]->imapuid);
583 fmtprint(&fmt, " %s", after);
584 cmd = fmtstrflush(&fmt);
587 if(imapcmd(z, box, "%s", cmd) < 0)
594 imapcopylist(Imap *z, char *nbox, Msg **m, uint nm)
603 if(strcmp(nbox, "mbox") == 0)
604 name = estrdup("INBOX");
606 p = esmprint("%s%s", z->root, nbox);
607 name = esmprint("%Z", p);
610 rv = imaplistcmd(z, m[0]->box, "UID COPY", m, nm, name);
617 imapremovelist(Imap *z, Msg **m, uint nm)
625 rv = imaplistcmd(z, m[0]->box, "UID STORE", m, nm, "+FLAGS.SILENT (\\Deleted)");
626 /* careful - box might be gone; use z->box instead */
627 if(rv == 0 && z->box)
628 rv = imapcmd(z, z->box, "EXPUNGE");
634 imapflaglist(Imap *z, int op, int flag, Msg **m, uint nm)
648 fmtprint(&fmt, "%sFLAGS (", mod);
650 for(i=0; i<nelem(flagstab); i++){
651 if(flagstab[i].flag & flag){
652 fmtprint(&fmt, "%s%s", sep, flagstab[i].name);
657 s = fmtstrflush(&fmt);
660 rv = imaplistcmd(z, m[0]->box, "UID STORE", m, nm, s);
667 imapsearchbox(Imap *z, Box *b, char *search, Msg ***mm)
676 sx = imapcmdsx(z, b, "UID SEARCH CHARSET UTF-8 TEXT {%d}", strlen(search));
682 if((sx = imapcmdsxlit(z, b, "%s", search)) == nil){
686 if(sx->nsx < 2 || !isatom(sx->sx[1], "OK")){
701 m = emalloc(nuid*sizeof m[0]);
703 for(i=0; i<nuid; i++)
704 if((m[nm] = msgbyimapuid(b, uid[i], 0)) != nil)
712 imapcheckbox(Imap *z, Box *b)
722 * Imap utility routines
725 _ioimapdial(va_list *arg)
730 server = va_arg(*arg, char*);
731 mode = va_arg(*arg, int);
732 return imapdial(server, mode);
735 ioimapdial(Ioproc *io, char *server, int mode)
737 return iocall(io, _ioimapdial, server, mode);
741 _ioBrdsx(va_list *arg)
746 b = va_arg(*arg, Biobuf*);
747 sx = va_arg(*arg, Sx**);
749 if((*sx) && (*sx)->type == SxList && (*sx)->nsx == 0){
756 ioBrdsx(Ioproc *io, Biobuf *b)
760 iocall(io, _ioBrdsx, b, &sx);
767 if(z->ticks && --z->ticks==0){
772 return ioBrdsx(z->io, &z->b);
776 imapdial(char *server, int mode)
786 return dial(netmkaddr(server, "tcp", "143"), nil, nil, nil);
789 werrstr("starttls not supported");
795 fd[0] = dup(p[0], -1);
796 fd[1] = dup(p[0], -1);
799 tmp = esmprint("%s:993", server);
800 fpath = searchpath("stunnel3");
802 werrstr("stunnel not found. it is required for tls support.");
805 if(threadspawnl(fd, fpath, "stunnel", "-c", "-r", tmp, nil) < 0) {
807 tmp = esmprint("tcp!%s!993", server);
808 if(threadspawnl(fd, "/bin/tlsclient", "tlsclient", tmp, nil) < 0){
825 fd[0] = dup(p[0], -1);
826 fd[1] = dup(p[0], -1);
828 if(threadspawnl(fd, "/usr/local/plan9/bin/rc", "rc", "-c", server, nil) < 0){
855 if(r=='\\' || r=='"')
867 s = va_arg(f->args, char*);
868 if(s == nil || *s == '\0')
869 return fmtstrcpy(f, "\"\"");
872 if(f->flags&FmtSharp)
875 w = chartorune(&r, t);
876 quotes |= needtoquote(r);
879 return fmtstrcpy(f, s);
883 w = chartorune(&r, t);
884 if(needtoquote(r) == Qbackslash)
888 return fmtrune(f, '"');
909 * Check S expression against format string.
912 sxmatch(Sx *sx, char *fmt)
916 for(i=0; fmt[i]; i++){
918 fmt--; /* like i-- but better */
919 if(i == sx->nsx && fmt[i+1] == '*')
925 if(sx->sx[i]->type == SxAtom && strcmp(sx->sx[i]->data, "NIL") == 0){
927 free(sx->sx[i]->data);
928 sx->sx[i]->data = nil;
929 sx->sx[i]->type = SxList;
933 else if(fmt[i] == 'S'){
934 free(sx->sx[i]->data);
935 sx->sx[i]->data = nil;
936 sx->sx[i]->type = SxString;
939 if(sx->sx[i]->type == SxAtom && fmt[i]=='S')
940 sx->sx[i]->type = SxString;
941 if(sx->sx[i]->type != fmttype(fmt[i])){
942 fprint(2, "sxmatch: %$ not %c\n", sx->sx[i], fmt[i]);
952 * Check string against format string.
955 stringmatch(char *fmt, char *s)
957 for(; *fmt && *s; fmt++, s++){
964 if(*s < '0' || *s > '9')
968 if(*s < 'A' || *s > 'Z')
972 if(*s < 'a' || *s > 'z')
976 if(*s != '-' && *s != '+')
991 * Parse simple S expressions and IMAP elements.
994 isatom(Sx *v, char *name)
998 if(v == nil || v->type != SxAtom)
1001 if(cistrncmp(v->data, name, n) == 0)
1002 if(v->data[n] == 0 || (n>0 && v->data[n-1] == '['))
1010 if(sx->type == SxAtom)
1011 sx->type = SxString;
1012 return sx->type == SxString;
1018 return sx->type == SxNumber;
1025 (v->type==SxList && v->nsx == 0) ||
1026 (v->type==SxAtom && strcmp(v->data, "NIL") == 0);
1032 return isnil(v) || v->type==SxList;
1040 if(v->type != SxList){
1041 warn("malformed flags: %$", v);
1045 for(i=0; i<v->nsx; i++){
1046 if(v->sx[i]->type != SxAtom)
1048 for(j=0; j<nelem(flagstab); j++)
1049 if(cistrcmp(v->sx[i]->data, flagstab[j].name) == 0)
1050 f |= flagstab[j].flag;
1055 static char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
1061 for(i=0; months[i]; i+=3)
1062 if(memcmp(s, months+i, 3) == 0)
1075 if(v->type != SxString || !stringmatch("01-Aaa-1111 01:11:11 +1111", v->data)){
1077 warn("bad date: %$", v);
1081 /* cannot use atoi because 09 is malformed octal! */
1082 memset(&tm, 0, sizeof tm);
1084 tm.mday = strtol(p, 0, 10);
1085 tm.mon = parsemon(p+3);
1088 tm.year = strtol(p+7, 0, 10) - 1900;
1089 tm.hour = strtol(p+12, 0, 10);
1090 tm.min = strtol(p+15, 0, 10);
1091 tm.sec = strtol(p+18, 0, 10);
1092 strcpy(tm.zone, "GMT");
1095 delta = ((p[22]-'0')*10+p[23]-'0')*3600 + ((p[24]-'0')*10+p[25]-'0')*60;
1106 if(v->type != SxNumber)
1112 hash(DigestState *ds, char *tag, char *val)
1116 md5((uchar*)tag, strlen(tag)+1, nil, ds);
1117 md5((uchar*)val, strlen(val)+1, nil, ds);
1121 parseenvelope(Sx *v)
1127 if(v->type != SxList || !sxmatch(v, "SSLLLLLLSS")){
1128 warn("bad envelope: %$", v);
1132 hdr = emalloc(sizeof *hdr);
1133 hdr->date = nstring(v->sx[0]);
1134 hdr->subject = unrfc2047(nstring(v->sx[1]));
1135 hdr->from = copyaddrs(v->sx[2]);
1136 hdr->sender = copyaddrs(v->sx[3]);
1137 hdr->replyto = copyaddrs(v->sx[4]);
1138 hdr->to = copyaddrs(v->sx[5]);
1139 hdr->cc = copyaddrs(v->sx[6]);
1140 hdr->bcc = copyaddrs(v->sx[7]);
1141 hdr->inreplyto = unrfc2047(nstring(v->sx[8]));
1142 hdr->messageid = unrfc2047(nstring(v->sx[9]));
1144 memset(&ds, 0, sizeof ds);
1145 hash(&ds, "date", hdr->date);
1146 hash(&ds, "subject", hdr->subject);
1147 hash(&ds, "from", hdr->from);
1148 hash(&ds, "sender", hdr->sender);
1149 hash(&ds, "replyto", hdr->replyto);
1150 hash(&ds, "to", hdr->to);
1151 hash(&ds, "cc", hdr->cc);
1152 hash(&ds, "bcc", hdr->bcc);
1153 hash(&ds, "inreplyto", hdr->inreplyto);
1154 hash(&ds, "messageid", hdr->messageid);
1155 md5(0, 0, digest, &ds);
1156 hdr->digest = esmprint("%.16H", digest);
1169 if('A' <= *t && *t <= 'Z')
1187 * substitute all occurrences of a with b in s.
1190 gsub(char *s, char *a, char *b)
1192 char *p, *t, *w, *last;
1196 for(p=s; (p=strstr(p, a)) != nil; p+=strlen(a))
1200 t = emalloc(strlen(s)+n*strlen(b)+1);
1202 for(p=s; last=p, (p=strstr(p, a)) != nil; p+=strlen(a)){
1203 memmove(w, last, p-last);
1205 memmove(w, b, strlen(b));
1214 * Table-driven IMAP "unexpected response" parser.
1215 * All the interesting data is in the unexpected responses.
1217 static void xlist(Imap*, Sx*);
1218 static void xrecent(Imap*, Sx*);
1219 static void xexists(Imap*, Sx*);
1220 static void xok(Imap*, Sx*);
1221 static void xflags(Imap*, Sx*);
1222 static void xfetch(Imap*, Sx*);
1223 static void xexpunge(Imap*, Sx*);
1224 static void xbye(Imap*, Sx*);
1225 static void xsearch(Imap*, Sx*);
1231 void (*fn)(Imap*, Sx*);
1233 0, "BYE", nil, xbye,
1234 0, "FLAGS", "AAL", xflags,
1235 0, "LIST", "AALSS", xlist,
1237 0, "SEARCH", "AAN*", xsearch,
1239 1, "EXISTS", "ANA", xexists,
1240 1, "EXPUNGE", "ANA", xexpunge,
1241 1, "FETCH", "ANAL", xfetch,
1242 1, "RECENT", "ANA", xrecent
1246 unexpected(Imap *z, Sx *sx)
1251 if(sx->nsx >= 3 && sx->sx[1]->type == SxNumber && sx->sx[2]->type == SxAtom){
1253 name = sx->sx[2]->data;
1254 }else if(sx->nsx >= 2 && sx->sx[1]->type == SxAtom){
1256 name = sx->sx[1]->data;
1260 for(i=0; i<nelem(unextab); i++){
1261 if(unextab[i].num == num && cistrcmp(unextab[i].name, name) == 0){
1262 if(unextab[i].fmt && !sxmatch(sx, unextab[i].fmt)){
1263 warn("malformed %s: %$", name, sx);
1266 unextab[i].fn(z, sx);
1281 xlist(Imap *z, Sx *sx)
1287 s = estrdup(sx->sx[4]->data);
1288 if(sx->sx[3]->data && strcmp(sx->sx[3]->data, "/") != 0){
1289 s = gsub(s, "/", "_");
1290 s = gsub(s, sx->sx[3]->data, "/");
1294 * INBOX is the special imap name for the main mailbox.
1295 * All other mailbox names have the root prefix removed, if applicable.
1298 if(cistrcmp(s, "INBOX") == 0){
1301 s = estrdup("mbox");
1302 } else if(z->root && strstr(s, z->root) == s) {
1303 t = estrdup(s+strlen(z->root));
1309 * Plan 9 calls the main mailbox mbox.
1310 * Rename any existing mbox by appending a $.
1312 if(!inbox && strncmp(s, "mbox", 4) == 0 && alldollars(s+4)){
1313 t = emalloc(strlen(s)+2);
1323 box->imapname = estrdup(sx->sx[4]->data);
1327 box->flags = parseflags(sx->sx[2]);
1331 xrecent(Imap *z, Sx *sx)
1334 z->box->recent = sx->sx[1]->number;
1338 xexists(Imap *z, Sx *sx)
1341 z->box->exists = sx->sx[1]->number;
1342 if(z->box->exists < z->box->maxseen)
1343 z->box->maxseen = z->box->exists;
1348 xflags(Imap *z, Sx *sx)
1353 * This response contains in sx->sx[2] the list of flags
1354 * that can be validly attached to messages in z->box.
1355 * We don't have any use for this list, since we
1356 * use only the standard flags.
1361 xbye(Imap *z, Sx *sx)
1370 xexpunge(Imap *z, Sx *sx)
1375 if((b=z->box) == nil)
1377 n = sx->sx[1]->number;
1378 for(i=0; i<b->nmsg; i++){
1379 if(b->msg[i]->imapid == n){
1380 msgplumb(b->msg[i], 1);
1383 memmove(b->msg+i, b->msg+i+1, (b->nmsg-i)*sizeof b->msg[0]);
1389 if(b->msg[i]->imapid > n)
1390 b->msg[i]->imapid--;
1396 xsearch(Imap *z, Sx *sx)
1401 z->uid = emalloc((sx->nsx-2)*sizeof z->uid[0]);
1402 z->nuid = sx->nsx-2;
1403 for(i=0; i<z->nuid; i++)
1404 z->uid[i] = sx->sx[i+2]->number;
1408 * Table-driven FETCH message info parser.
1410 static void xmsgflags(Msg*, Sx*, Sx*);
1411 static void xmsgdate(Msg*, Sx*, Sx*);
1412 static void xmsgrfc822size(Msg*, Sx*, Sx*);
1413 static void xmsgenvelope(Msg*, Sx*, Sx*);
1414 static void xmsgbody(Msg*, Sx*, Sx*);
1415 static void xmsgbodydata(Msg*, Sx*, Sx*);
1419 void (*fn)(Msg*, Sx*, Sx*);
1422 "INTERNALDATE", xmsgdate,
1423 "RFC822.SIZE", xmsgrfc822size,
1424 "ENVELOPE", xmsgenvelope,
1426 "BODY[", xmsgbodydata
1430 xfetch(Imap *z, Sx *sx)
1436 warn("FETCH but no open box: %$", sx);
1440 /* * 152 FETCH (UID 185 FLAGS () ...) */
1441 if(sx->sx[3]->nsx%2){
1442 warn("malformed FETCH: %$", sx);
1446 n = sx->sx[1]->number;
1448 for(i=0; i<sx->nsx; i+=2){
1449 if(isatom(sx->sx[i], "UID")){
1450 if(sx->sx[i+1]->type == SxNumber){
1451 uid = sx->sx[i+1]->number;
1456 /* This happens: too bad.
1457 warn("FETCH without UID: %$", sx);
1462 msg = msgbyimapuid(z->box, uid, 1);
1463 if(msg->imapid && msg->imapid != n)
1464 warn("msg id mismatch: want %d have %d", msg->id, n);
1466 for(i=0; i<sx->nsx; i+=2){
1467 for(j=0; j<nelem(msgtab); j++)
1468 if(isatom(sx->sx[i], msgtab[j].name))
1469 msgtab[j].fn(msg, sx->sx[i], sx->sx[i+1]);
1475 xmsgflags(Msg *msg, Sx *k, Sx *v)
1478 msg->flags = parseflags(v);
1482 xmsgdate(Msg *msg, Sx *k, Sx *v)
1485 msg->date = parsedate(v);
1489 xmsgrfc822size(Msg *msg, Sx *k, Sx *v)
1492 msg->size = parsenumber(v);
1511 char *name, *email, *host, *mbox;
1520 for(i=0; i<v->nsx; i++){
1521 if(!sxmatch(v->sx[i], "SSSS"))
1522 warn("bad address: %$", v->sx[i]);
1523 name = unrfc2047(nstring(v->sx[i]->sx[0]));
1524 /* ignore sx[1] - route */
1525 mbox = unrfc2047(nstring(v->sx[i]->sx[2]));
1526 host = unrfc2047(nstring(v->sx[i]->sx[3]));
1527 if(mbox == nil || host == nil){ /* rfc822 group syntax */
1533 email = esmprint("%s@%s", mbox, host);
1536 fmtprint(&fmt, "%s%q %q", sep, name ? name : "", email ? email : "");
1541 s = fmtstrflush(&fmt);
1543 sysfatal("out of memory");
1548 xmsgenvelope(Msg *msg, Sx *k, Sx *v)
1551 hdrfree(msg->part[0]->hdr);
1552 msg->part[0]->hdr = parseenvelope(v);
1559 "charset", offsetof(Part, charset),
1560 "name", offsetof(Part, filename)
1564 parseparams(Part *part, Sx *v)
1572 warn("bad message params: %$", v);
1575 for(i=0; i<v->nsx; i+=2){
1576 s = nstring(v->sx[i]);
1577 t = nstring(v->sx[i+1]);
1578 for(j=0; j<nelem(paramtab); j++){
1579 if(cistrcmp(paramtab[j].name, s) == 0){
1580 p = (char**)((char*)part+paramtab[j].offset);
1593 parsestructure(Part *part, Sx *v)
1600 if(v->type != SxList){
1602 warn("bad structure: %$", v);
1605 if(islist(v->sx[0])){
1607 for(i=0; i<v->nsx && islist(v->sx[i]); i++)
1608 parsestructure(partcreate(part->msg, part), v->sx[i]);
1610 if(i != v->nsx-1 || !isstring(v->sx[i])){
1611 warn("bad multipart structure: %$", v);
1612 part->type = estrdup("multipart/mixed");
1615 s = nstring(v->sx[i]);
1617 part->type = esmprint("multipart/%s", s);
1622 if(!isstring(v->sx[0]) || v->nsx < 2)
1624 s = nstring(v->sx[0]);
1625 t = nstring(v->sx[1]);
1629 part->type = esmprint("%s/%s", s, t);
1630 if(v->nsx < 7 || !islist(v->sx[2]) || !isstring(v->sx[3])
1631 || !isstring(v->sx[4]) || !isstring(v->sx[5]) || !isnumber(v->sx[6]))
1633 parseparams(part, v->sx[2]);
1634 part->idstr = nstring(v->sx[3]);
1635 part->desc = nstring(v->sx[4]);
1636 part->encoding = nstring(v->sx[5]);
1637 part->size = v->sx[6]->number;
1638 if(strcmp(s, "message") == 0 && strcmp(t, "rfc822") == 0){
1639 if(v->nsx < 10 || !islist(v->sx[7]) || !islist(v->sx[8]) || !isnumber(v->sx[9]))
1641 part->hdr = parseenvelope(v->sx[7]);
1642 parsestructure(partcreate(part->msg, part), v->sx[8]);
1643 part->lines = v->sx[9]->number;
1645 if(strcmp(s, "text") == 0){
1646 if(v->nsx < 8 || !isnumber(v->sx[7]))
1648 part->lines = v->sx[7]->number;
1653 xmsgbody(Msg *msg, Sx *k, Sx *v)
1656 if(v->type != SxList){
1657 warn("bad body: %$", v);
1661 * To follow the structure exactly we should
1662 * be doing this to partcreate(msg, msg->part[0]),
1663 * and we should leave msg->part[0] with type message/rfc822,
1664 * but the extra layer is redundant - what else would be in a mailbox?
1666 parsestructure(msg->part[0], v);
1667 if(msg->box->maxseen < msg->imapid)
1668 msg->box->maxseen = msg->imapid;
1669 if(msg->imapuid >= msg->box->uidnext)
1670 msg->box->uidnext = msg->imapuid+1;
1674 xmsgbodydata(Msg *msg, Sx *k, Sx *v)
1681 name += 5; /* body[ */
1682 p = strchr(name, ']');
1686 /* now name is something like 1 or 3.2.MIME - walk down parts from root */
1687 part = msg->part[0];
1690 while('1' <= name[0] && name[0] <= '9'){
1691 i = strtol(name, &p, 10);
1695 warn("bad body name: %$", k);
1698 if((part = subpart(part, i-1)) == nil){
1699 warn("unknown body part: %$", k);
1706 if(cistrcmp(name, "") == 0){
1708 part->raw = nstring(v);
1710 }else if(cistrcmp(name, "HEADER") == 0){
1711 free(part->rawheader);
1712 part->rawheader = nstring(v);
1713 nocr(part->rawheader);
1714 }else if(cistrcmp(name, "MIME") == 0){
1715 free(part->mimeheader);
1716 part->mimeheader = nstring(v);
1717 nocr(part->mimeheader);
1718 }else if(cistrcmp(name, "TEXT") == 0){
1719 free(part->rawbody);
1720 part->rawbody = nstring(v);
1721 nocr(part->rawbody);
1726 * Table-driven OK info parser.
1728 static void xokuidvalidity(Imap*, Sx*);
1729 static void xokpermflags(Imap*, Sx*);
1730 static void xokunseen(Imap*, Sx*);
1731 static void xokreadwrite(Imap*, Sx*);
1732 static void xokreadonly(Imap*, Sx*);
1737 void (*fn)(Imap*, Sx*);
1739 "UIDVALIDITY", 'N', xokuidvalidity,
1740 "PERMANENTFLAGS", 'L', xokpermflags,
1741 "UNSEEN", 'N', xokunseen,
1742 "READ-WRITE", 0, xokreadwrite,
1743 "READ-ONLY", 0, xokreadonly
1747 xok(Imap *z, Sx *sx)
1753 if(sx->nsx >= 4 && sx->sx[2]->type == SxAtom && sx->sx[2]->data[0] == '['){
1754 if(sx->sx[3]->type == SxAtom && sx->sx[3]->data[0] == ']')
1756 else if(sx->sx[4]->type == SxAtom && sx->sx[4]->data[0] == ']')
1759 warn("cannot parse OK: %$", sx);
1762 name = sx->sx[2]->data+1;
1763 for(i=0; i<nelem(oktab); i++){
1764 if(cistrcmp(name, oktab[i].name) == 0){
1765 if(oktab[i].fmt && (arg==nil || arg->type != fmttype(oktab[i].fmt))){
1766 warn("malformed %s: %$", name, arg);
1769 oktab[i].fn(z, arg);
1776 xokuidvalidity(Imap *z, Sx *sx)
1781 if((b=z->box) == nil)
1783 if(b->validity != sx->number){
1784 b->validity = sx->number;
1786 for(i=0; i<b->nmsg; i++)
1795 xokpermflags(Imap *z, Sx *sx)
1799 /* z->permflags = parseflags(sx); */
1803 xokunseen(Imap *z, Sx *sx)
1807 /* z->unseen = sx->number; */
1811 xokreadwrite(Imap *z, Sx *sx)
1815 /* z->boxmode = ORDWR; */
1819 xokreadonly(Imap *z, Sx *sx)
1823 /* z->boxmode = OREAD; */