rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1 | /* |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #include "a.h" |
| 12 | #include <libsec.h> |
| 13 | |
| 14 | struct Imap |
| 15 | { |
| 16 | int connected; |
| 17 | int autoreconnect; |
| 18 | int ticks; /* until boom! */ |
| 19 | char* server; |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 20 | char* root; |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 21 | int mode; |
| 22 | int fd; |
| 23 | Biobuf b; |
| 24 | Ioproc* io; |
| 25 | QLock lk; |
| 26 | QLock rlk; |
| 27 | Rendez r; |
| 28 | |
| 29 | Box* inbox; |
| 30 | Box* box; |
| 31 | Box* nextbox; |
| 32 | |
| 33 | /* SEARCH results */ |
| 34 | uint *uid; |
| 35 | uint nuid; |
| 36 | }; |
| 37 | |
| 38 | static struct { |
| 39 | char *name; |
| 40 | int flag; |
| 41 | } flagstab[] = |
| 42 | { |
| 43 | "Junk", FlagJunk, |
| 44 | "NonJunk", FlagNonJunk, |
| 45 | "\\Answered", FlagReplied, |
| 46 | "\\Flagged", FlagFlagged, |
| 47 | "\\Deleted", FlagDeleted, |
| 48 | "\\Draft", FlagDraft, |
rsc | abb0a67 | 2006-02-23 11:48:52 +0000 | [diff] [blame] | 49 | "\\Recent", FlagRecent, |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 50 | "\\Seen", FlagSeen, |
| 51 | "\\NoInferiors", FlagNoInferiors, |
| 52 | "\\NoSelect", FlagNoSelect, |
| 53 | "\\Marked", FlagMarked, |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 54 | "\\UnMarked", FlagUnMarked |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | int chattyimap; |
| 58 | |
rsc | b99162d | 2006-03-03 17:49:38 +0000 | [diff] [blame] | 59 | static char *tag = "#"; |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 60 | |
| 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*); |
| 88 | |
| 89 | /* |
| 90 | * Imap connection maintenance and login. |
| 91 | */ |
| 92 | |
| 93 | Imap* |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 94 | imapconnect(char *server, int mode, char *root) |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 95 | { |
| 96 | Imap *z; |
| 97 | |
| 98 | fmtinstall('H', encodefmt); |
| 99 | fmtinstall('Z', imapquote); |
| 100 | |
| 101 | z = emalloc(sizeof *z); |
| 102 | z->server = estrdup(server); |
| 103 | z->mode = mode; |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 104 | if(root) |
| 105 | if(root[0] != 0 && root[strlen(root)-1] != '/') |
| 106 | z->root = smprint("%s/", root); |
| 107 | else |
| 108 | z->root = root; |
| 109 | else |
| 110 | z->root = ""; |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 111 | z->fd = -1; |
| 112 | z->autoreconnect = 0; |
| 113 | z->io = ioproc(); |
| 114 | |
| 115 | qlock(&z->lk); |
| 116 | if(imapreconnect(z) < 0){ |
| 117 | free(z); |
| 118 | return nil; |
| 119 | } |
| 120 | |
| 121 | z->r.l = &z->rlk; |
| 122 | z->autoreconnect = 1; |
| 123 | qunlock(&z->lk); |
| 124 | |
| 125 | proccreate(imaptimerproc, z, STACK); |
| 126 | mailthread(imaprefreshthread, z); |
| 127 | |
| 128 | return z; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | imaphangup(Imap *z, int ticks) |
| 133 | { |
| 134 | z->ticks = ticks; |
| 135 | if(ticks == 0){ |
| 136 | close(z->fd); |
| 137 | z->fd = -1; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static int |
| 142 | imapreconnect(Imap *z) |
| 143 | { |
| 144 | Sx *sx; |
| 145 | |
| 146 | z->autoreconnect = 0; |
| 147 | z->box = nil; |
| 148 | z->inbox = nil; |
| 149 | |
| 150 | if(z->fd >= 0){ |
| 151 | close(z->fd); |
| 152 | z->fd = -1; |
| 153 | } |
| 154 | |
| 155 | if(chattyimap) |
| 156 | fprint(2, "dial %s...\n", z->server); |
| 157 | if((z->fd = ioimapdial(z->io, z->server, z->mode)) < 0) |
| 158 | return -1; |
| 159 | z->connected = 1; |
| 160 | Binit(&z->b, z->fd, OREAD); |
| 161 | if((sx = zBrdsx(z)) == nil){ |
| 162 | werrstr("no greeting"); |
| 163 | goto err; |
| 164 | } |
| 165 | if(chattyimap) |
| 166 | fprint(2, "<I %#$\n", sx); |
| 167 | if(sx->nsx >= 2 && isatom(sx->sx[0], "*") && isatom(sx->sx[1], "PREAUTH")){ |
| 168 | freesx(sx); |
| 169 | goto preauth; |
| 170 | } |
| 171 | if(!oksx(sx)){ |
| 172 | werrstr("bad greeting - %#$", sx); |
| 173 | goto err; |
| 174 | } |
| 175 | freesx(sx); |
| 176 | sx = nil; |
| 177 | if(imaplogin(z) < 0) |
| 178 | goto err; |
| 179 | preauth: |
| 180 | if(getboxes(z) < 0 || getbox(z, z->inbox) < 0) |
| 181 | goto err; |
| 182 | z->autoreconnect = 1; |
| 183 | return 0; |
| 184 | |
| 185 | err: |
| 186 | if(z->fd >= 0){ |
| 187 | close(z->fd); |
| 188 | z->fd = -1; |
| 189 | } |
| 190 | if(sx) |
| 191 | freesx(sx); |
| 192 | z->autoreconnect = 1; |
| 193 | z->connected = 0; |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | static int |
| 198 | imaplogin(Imap *z) |
| 199 | { |
| 200 | Sx *sx; |
| 201 | UserPasswd *up; |
| 202 | |
| 203 | if((up = auth_getuserpasswd(auth_getkey, "proto=pass role=client service=imap server=%q", z->server)) == nil){ |
| 204 | werrstr("getuserpasswd - %r"); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | sx = imapcmdsx(z, nil, "LOGIN %Z %Z", up->user, up->passwd); |
| 209 | freeup(up); |
| 210 | if(sx == nil) |
| 211 | return -1; |
| 212 | if(!oksx(sx)){ |
| 213 | freesx(sx); |
| 214 | werrstr("login rejected - %#$", sx); |
| 215 | return -1; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | getboxes(Imap *z) |
| 222 | { |
| 223 | int i; |
| 224 | Box **r, **w, **e; |
| 225 | |
| 226 | for(i=0; i<nboxes; i++){ |
| 227 | boxes[i]->mark = 1; |
| 228 | boxes[i]->exists = 0; |
| 229 | boxes[i]->maxseen = 0; |
| 230 | } |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 231 | if(imapcmd(z, nil, "LIST %Z *", z->root) < 0) |
| 232 | return -1; |
| 233 | if(z->root != nil && imapcmd(z, nil, "LIST %Z INBOX", "") < 0) |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 234 | return -1; |
| 235 | if(z->nextbox && z->nextbox->mark) |
| 236 | z->nextbox = nil; |
| 237 | for(r=boxes, w=boxes, e=boxes+nboxes; r<e; r++){ |
| 238 | if((*r)->mark) |
| 239 | {fprint(2, "*** free box %s %s\n", (*r)->name, (*r)->imapname); |
| 240 | boxfree(*r); |
| 241 | } |
| 242 | else |
| 243 | *w++ = *r; |
| 244 | } |
| 245 | nboxes = w - boxes; |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int |
| 250 | getbox(Imap *z, Box *b) |
| 251 | { |
| 252 | int i; |
| 253 | Msg **r, **w, **e; |
| 254 | |
| 255 | if(b == nil) |
| 256 | return 0; |
| 257 | |
| 258 | for(i=0; i<b->nmsg; i++) |
| 259 | b->msg[i]->imapid = 0; |
| 260 | if(imapcmd(z, b, "UID FETCH 1:* FLAGS") < 0) |
| 261 | return -1; |
| 262 | for(r=b->msg, w=b->msg, e=b->msg+b->nmsg; r<e; r++){ |
| 263 | if((*r)->imapid == 0) |
| 264 | msgfree(*r); |
| 265 | else{ |
| 266 | (*r)->ix = w-b->msg; |
| 267 | *w++ = *r; |
| 268 | } |
| 269 | } |
| 270 | b->nmsg = w - b->msg; |
| 271 | b->imapinit = 1; |
| 272 | checkbox(z, b); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static void |
| 277 | freeup(UserPasswd *up) |
| 278 | { |
| 279 | memset(up->user, 0, strlen(up->user)); |
| 280 | memset(up->passwd, 0, strlen(up->passwd)); |
| 281 | free(up); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | imaptimerproc(void *v) |
| 286 | { |
| 287 | Imap *z; |
| 288 | |
| 289 | z = v; |
| 290 | for(;;){ |
| 291 | sleep(60*1000); |
| 292 | qlock(z->r.l); |
| 293 | rwakeup(&z->r); |
| 294 | qunlock(z->r.l); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static void |
| 299 | checkbox(Imap *z, Box *b) |
| 300 | { |
| 301 | if(imapcmd(z, b, "NOOP") >= 0){ |
| 302 | if(!b->imapinit) |
| 303 | getbox(z, b); |
| 304 | if(!b->imapinit) |
| 305 | return; |
| 306 | if(b==z->box && b->exists > b->maxseen){ |
| 307 | imapcmd(z, b, "UID FETCH %d:* FULL", |
| 308 | b->uidnext); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | imaprefreshthread(void *v) |
| 315 | { |
| 316 | Imap *z; |
| 317 | |
| 318 | z = v; |
| 319 | for(;;){ |
| 320 | qlock(z->r.l); |
| 321 | rsleep(&z->r); |
| 322 | qunlock(z->r.l); |
| 323 | |
| 324 | qlock(&z->lk); |
| 325 | if(z->inbox) |
| 326 | checkbox(z, z->inbox); |
| 327 | qunlock(&z->lk); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Run a single command and return the Sx. Does NOT redial. |
| 333 | */ |
| 334 | static Sx* |
| 335 | imapvcmdsx0(Imap *z, char *fmt, va_list arg) |
| 336 | { |
| 337 | char *s; |
| 338 | Fmt f; |
| 339 | int prefix, len; |
| 340 | Sx *sx; |
| 341 | |
| 342 | if(canqlock(&z->lk)) |
| 343 | abort(); |
| 344 | |
| 345 | if(z->fd < 0 || !z->connected) |
| 346 | return nil; |
| 347 | |
| 348 | prefix = strlen(tag)+1; |
| 349 | fmtstrinit(&f); |
| 350 | fmtprint(&f, "%s ", tag); |
| 351 | fmtvprint(&f, fmt, arg); |
| 352 | fmtprint(&f, "\r\n"); |
| 353 | s = fmtstrflush(&f); |
| 354 | len = strlen(s); |
| 355 | s[len-2] = 0; |
| 356 | if(chattyimap) |
| 357 | fprint(2, "I> %s\n", s); |
| 358 | s[len-2] = '\r'; |
| 359 | if(iowrite(z->io, z->fd, s, len) < 0){ |
| 360 | z->connected = 0; |
| 361 | free(s); |
| 362 | return nil; |
| 363 | } |
| 364 | sx = imapwaitsx(z); |
| 365 | free(s); |
| 366 | return sx; |
| 367 | } |
| 368 | |
| 369 | static Sx* |
| 370 | imapcmdsx0(Imap *z, char *fmt, ...) |
| 371 | { |
| 372 | va_list arg; |
| 373 | Sx *sx; |
| 374 | |
| 375 | va_start(arg, fmt); |
| 376 | sx = imapvcmdsx0(z, fmt, arg); |
| 377 | va_end(arg); |
| 378 | return sx; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Run a single command on box b. Does redial. |
| 383 | */ |
| 384 | static Sx* |
| 385 | imapvcmdsx(Imap *z, Box *b, char *fmt, va_list arg) |
| 386 | { |
| 387 | int tries; |
| 388 | Sx *sx; |
| 389 | |
| 390 | tries = 0; |
| 391 | z->nextbox = b; |
| 392 | |
| 393 | if(z->fd < 0 || !z->connected){ |
| 394 | reconnect: |
| 395 | if(!z->autoreconnect) |
| 396 | return nil; |
| 397 | if(imapreconnect(z) < 0) |
| 398 | return nil; |
| 399 | if(b && z->nextbox == nil) /* box disappeared on reconnect */ |
| 400 | return nil; |
| 401 | } |
| 402 | |
| 403 | if(b && b != z->box){ |
| 404 | if(z->box) |
| 405 | z->box->imapinit = 0; |
| 406 | z->box = b; |
| 407 | if((sx=imapcmdsx0(z, "SELECT %Z", b->imapname)) == nil){ |
| 408 | z->box = nil; |
| 409 | if(tries++ == 0 && (z->fd < 0 || !z->connected)) |
| 410 | goto reconnect; |
| 411 | return nil; |
| 412 | } |
| 413 | freesx(sx); |
| 414 | } |
| 415 | |
| 416 | if((sx=imapvcmdsx0(z, fmt, arg)) == nil){ |
| 417 | if(tries++ == 0 && (z->fd < 0 || !z->connected)) |
| 418 | goto reconnect; |
| 419 | return nil; |
| 420 | } |
| 421 | return sx; |
| 422 | } |
| 423 | |
| 424 | static int |
| 425 | imapcmd(Imap *z, Box *b, char *fmt, ...) |
| 426 | { |
| 427 | Sx *sx; |
| 428 | va_list arg; |
| 429 | |
| 430 | va_start(arg, fmt); |
| 431 | sx = imapvcmdsx(z, b, fmt, arg); |
| 432 | va_end(arg); |
| 433 | if(sx == nil) |
| 434 | return -1; |
| 435 | if(sx->nsx < 2 || !isatom(sx->sx[1], "OK")){ |
| 436 | werrstr("%$", sx); |
| 437 | freesx(sx); |
| 438 | return -1; |
| 439 | } |
| 440 | freesx(sx); |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static Sx* |
| 445 | imapcmdsx(Imap *z, Box *b, char *fmt, ...) |
| 446 | { |
| 447 | Sx *sx; |
| 448 | va_list arg; |
| 449 | |
| 450 | va_start(arg, fmt); |
| 451 | sx = imapvcmdsx(z, b, fmt, arg); |
| 452 | va_end(arg); |
| 453 | return sx; |
| 454 | } |
| 455 | |
| 456 | static Sx* |
| 457 | imapwaitsx(Imap *z) |
| 458 | { |
| 459 | Sx *sx; |
| 460 | |
| 461 | while((sx = zBrdsx(z)) != nil){ |
| 462 | if(chattyimap) |
| 463 | fprint(2, "<| %#$\n", sx); |
| 464 | if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && cistrcmp(sx->sx[0]->data, tag) == 0) |
| 465 | return sx; |
| 466 | if(sx->nsx >= 1 && sx->sx[0]->type == SxAtom && strcmp(sx->sx[0]->data, "*") == 0) |
| 467 | unexpected(z, sx); |
| 468 | if(sx->type == SxList && sx->nsx == 0){ |
| 469 | freesx(sx); |
| 470 | break; |
| 471 | } |
| 472 | freesx(sx); |
| 473 | } |
| 474 | z->connected = 0; |
| 475 | return nil; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Imap interface to mail file system. |
| 480 | */ |
| 481 | |
| 482 | static void |
| 483 | _bodyname(char *buf, char *ebuf, Part *p, char *extra) |
| 484 | { |
| 485 | if(buf >= ebuf){ |
| 486 | fprint(2, "***** BUFFER TOO SMALL\n"); |
| 487 | return; |
| 488 | } |
| 489 | *buf = 0; |
| 490 | if(p->parent){ |
| 491 | _bodyname(buf, ebuf, p->parent, ""); |
| 492 | buf += strlen(buf); |
| 493 | seprint(buf, ebuf, ".%d", p->pix+1); |
| 494 | } |
| 495 | buf += strlen(buf); |
| 496 | seprint(buf, ebuf, "%s", extra); |
| 497 | } |
| 498 | |
| 499 | static char* |
| 500 | bodyname(Part *p, char *extra) |
| 501 | { |
| 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] == '.' */ |
| 506 | } |
| 507 | |
| 508 | static void |
| 509 | fetch1(Imap *z, Part *p, char *s) |
| 510 | { |
| 511 | qlock(&z->lk); |
| 512 | imapcmd(z, p->msg->box, "UID FETCH %d BODY[%s]", |
| 513 | p->msg->imapuid, bodyname(p, s)); |
| 514 | qunlock(&z->lk); |
| 515 | } |
| 516 | |
| 517 | void |
| 518 | imapfetchrawheader(Imap *z, Part *p) |
| 519 | { |
| 520 | fetch1(z, p, ".HEADER"); |
| 521 | } |
| 522 | |
| 523 | void |
| 524 | imapfetchrawmime(Imap *z, Part *p) |
| 525 | { |
| 526 | fetch1(z, p, ".MIME"); |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | imapfetchrawbody(Imap *z, Part *p) |
| 531 | { |
| 532 | fetch1(z, p, ".TEXT"); |
| 533 | } |
| 534 | |
| 535 | void |
| 536 | imapfetchraw(Imap *z, Part *p) |
| 537 | { |
| 538 | fetch1(z, p, ""); |
| 539 | } |
| 540 | |
| 541 | static int |
| 542 | imaplistcmd(Imap *z, Box *box, char *before, Msg **m, uint nm, char *after) |
| 543 | { |
| 544 | int i, r; |
| 545 | char *cmd; |
| 546 | Fmt fmt; |
| 547 | |
| 548 | if(nm == 0) |
| 549 | return 0; |
| 550 | |
| 551 | fmtstrinit(&fmt); |
| 552 | fmtprint(&fmt, "%s ", before); |
| 553 | for(i=0; i<nm; i++){ |
| 554 | if(i > 0) |
| 555 | fmtrune(&fmt, ','); |
| 556 | fmtprint(&fmt, "%ud", m[i]->imapuid); |
| 557 | } |
| 558 | fmtprint(&fmt, " %s", after); |
| 559 | cmd = fmtstrflush(&fmt); |
| 560 | |
| 561 | r = 0; |
| 562 | if(imapcmd(z, box, "%s", cmd) < 0) |
| 563 | r = -1; |
| 564 | free(cmd); |
| 565 | return r; |
| 566 | } |
| 567 | |
| 568 | int |
| 569 | imapcopylist(Imap *z, char *nbox, Msg **m, uint nm) |
| 570 | { |
| 571 | int rv; |
Russ Cox | 83ab7d8 | 2007-11-27 15:39:06 -0500 | [diff] [blame] | 572 | char *name, *p; |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 573 | |
| 574 | if(nm == 0) |
| 575 | return 0; |
| 576 | |
| 577 | qlock(&z->lk); |
Russ Cox | 83ab7d8 | 2007-11-27 15:39:06 -0500 | [diff] [blame] | 578 | if(strcmp(nbox, "mbox") == 0) |
| 579 | name = estrdup("INBOX"); |
| 580 | else{ |
| 581 | p = esmprint("%s%s", z->root, nbox); |
| 582 | name = esmprint("%Z", p); |
| 583 | free(p); |
| 584 | } |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 585 | rv = imaplistcmd(z, m[0]->box, "UID COPY", m, nm, name); |
| 586 | free(name); |
| 587 | qunlock(&z->lk); |
| 588 | return rv; |
| 589 | } |
| 590 | |
| 591 | int |
| 592 | imapremovelist(Imap *z, Msg **m, uint nm) |
| 593 | { |
| 594 | int rv; |
| 595 | |
| 596 | if(nm == 0) |
| 597 | return 0; |
| 598 | |
| 599 | qlock(&z->lk); |
| 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"); |
| 604 | qunlock(&z->lk); |
| 605 | return rv; |
| 606 | } |
| 607 | |
| 608 | int |
| 609 | imapflaglist(Imap *z, int op, int flag, Msg **m, uint nm) |
| 610 | { |
| 611 | char *mod, *s, *sep; |
| 612 | int i, rv; |
| 613 | Fmt fmt; |
| 614 | |
| 615 | if(op > 0) |
| 616 | mod = "+"; |
| 617 | else if(op == 0) |
| 618 | mod = ""; |
| 619 | else |
| 620 | mod = "-"; |
| 621 | |
| 622 | fmtstrinit(&fmt); |
| 623 | fmtprint(&fmt, "%sFLAGS (", mod); |
| 624 | sep = ""; |
| 625 | for(i=0; i<nelem(flagstab); i++){ |
| 626 | if(flagstab[i].flag & flag){ |
| 627 | fmtprint(&fmt, "%s%s", sep, flagstab[i].name); |
| 628 | sep = " "; |
| 629 | } |
| 630 | } |
| 631 | fmtprint(&fmt, ")"); |
| 632 | s = fmtstrflush(&fmt); |
| 633 | |
| 634 | qlock(&z->lk); |
| 635 | rv = imaplistcmd(z, m[0]->box, "UID STORE", m, nm, s); |
| 636 | qunlock(&z->lk); |
| 637 | free(s); |
| 638 | return rv; |
| 639 | } |
| 640 | |
| 641 | int |
| 642 | imapsearchbox(Imap *z, Box *b, char *search, Msg ***mm) |
| 643 | { |
| 644 | uint *uid; |
| 645 | int i, nuid; |
| 646 | Msg **m; |
| 647 | int nm; |
| 648 | |
| 649 | qlock(&z->lk); |
| 650 | if(imapcmd(z, b, "UID SEARCH CHARSET UTF-8 TEXT %Z", search) < 0){ |
| 651 | qunlock(&z->lk); |
| 652 | return -1; |
| 653 | } |
| 654 | |
| 655 | uid = z->uid; |
| 656 | nuid = z->nuid; |
| 657 | z->uid = nil; |
| 658 | z->nuid = 0; |
| 659 | qunlock(&z->lk); |
| 660 | |
| 661 | m = emalloc(nuid*sizeof m[0]); |
| 662 | nm = 0; |
| 663 | for(i=0; i<nuid; i++) |
| 664 | if((m[nm] = msgbyimapuid(b, uid[i], 0)) != nil) |
| 665 | nm++; |
| 666 | *mm = m; |
| 667 | free(uid); |
| 668 | return nm; |
| 669 | } |
| 670 | |
| 671 | void |
| 672 | imapcheckbox(Imap *z, Box *b) |
| 673 | { |
| 674 | if(b == nil) |
| 675 | return; |
| 676 | qlock(&z->lk); |
| 677 | checkbox(z, b); |
| 678 | qunlock(&z->lk); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Imap utility routines |
| 683 | */ |
| 684 | static long |
| 685 | _ioimapdial(va_list *arg) |
| 686 | { |
| 687 | char *server; |
| 688 | int mode; |
| 689 | |
| 690 | server = va_arg(*arg, char*); |
| 691 | mode = va_arg(*arg, int); |
| 692 | return imapdial(server, mode); |
| 693 | } |
| 694 | static int |
| 695 | ioimapdial(Ioproc *io, char *server, int mode) |
| 696 | { |
| 697 | return iocall(io, _ioimapdial, server, mode); |
| 698 | } |
| 699 | |
| 700 | static long |
| 701 | _ioBrdsx(va_list *arg) |
| 702 | { |
| 703 | Biobuf *b; |
| 704 | Sx **sx; |
| 705 | |
| 706 | b = va_arg(*arg, Biobuf*); |
| 707 | sx = va_arg(*arg, Sx**); |
| 708 | *sx = Brdsx(b); |
| 709 | if((*sx) && (*sx)->type == SxList && (*sx)->nsx == 0){ |
| 710 | freesx(*sx); |
| 711 | *sx = nil; |
| 712 | } |
| 713 | return 0; |
| 714 | } |
| 715 | static Sx* |
| 716 | ioBrdsx(Ioproc *io, Biobuf *b) |
| 717 | { |
| 718 | Sx *sx; |
| 719 | |
| 720 | iocall(io, _ioBrdsx, b, &sx); |
| 721 | return sx; |
| 722 | } |
| 723 | |
| 724 | static Sx* |
| 725 | zBrdsx(Imap *z) |
| 726 | { |
| 727 | if(z->ticks && --z->ticks==0){ |
| 728 | close(z->fd); |
| 729 | z->fd = -1; |
| 730 | return nil; |
| 731 | } |
| 732 | return ioBrdsx(z->io, &z->b); |
| 733 | } |
| 734 | |
| 735 | static int |
| 736 | imapdial(char *server, int mode) |
| 737 | { |
| 738 | int p[2]; |
| 739 | int fd[3]; |
| 740 | char *tmp; |
| 741 | |
| 742 | switch(mode){ |
| 743 | default: |
| 744 | case Unencrypted: |
| 745 | return dial(netmkaddr(server, "tcp", "143"), nil, nil, nil); |
| 746 | |
| 747 | case Starttls: |
| 748 | werrstr("starttls not supported"); |
| 749 | return -1; |
| 750 | |
| 751 | case Tls: |
| 752 | if(pipe(p) < 0) |
| 753 | return -1; |
| 754 | fd[0] = dup(p[0], -1); |
| 755 | fd[1] = dup(p[0], -1); |
| 756 | fd[2] = dup(2, -1); |
| 757 | tmp = esmprint("%s:993", server); |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 758 | if(threadspawnl(fd, "/usr/sbin/stunnel", "stunnel", "-c", "-r", tmp, nil) < 0 |
| 759 | && threadspawnl(fd, "/usr/bin/stunnel", "stunnel", "-c", "-r", tmp, nil) < 0){ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 760 | free(tmp); |
| 761 | close(p[0]); |
| 762 | close(p[1]); |
| 763 | close(fd[0]); |
| 764 | close(fd[1]); |
| 765 | close(fd[2]); |
| 766 | return -1; |
| 767 | } |
| 768 | free(tmp); |
| 769 | close(p[0]); |
| 770 | return p[1]; |
| 771 | |
| 772 | case Cmd: |
| 773 | if(pipe(p) < 0) |
| 774 | return -1; |
| 775 | fd[0] = dup(p[0], -1); |
| 776 | fd[1] = dup(p[0], -1); |
| 777 | fd[2] = dup(2, -1); |
| 778 | if(threadspawnl(fd, "/usr/local/plan9/bin/rc", "rc", "-c", server, nil) < 0){ |
| 779 | close(p[0]); |
| 780 | close(p[1]); |
| 781 | close(fd[0]); |
| 782 | close(fd[1]); |
| 783 | close(fd[2]); |
| 784 | return -1; |
| 785 | } |
| 786 | close(p[0]); |
| 787 | return p[1]; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | enum |
| 792 | { |
| 793 | Qok = 0, |
| 794 | Qquote, |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 795 | Qbackslash |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | static int |
| 799 | needtoquote(Rune r) |
| 800 | { |
| 801 | if(r >= Runeself) |
| 802 | return Qquote; |
| 803 | if(r <= ' ') |
| 804 | return Qquote; |
| 805 | if(r=='\\' || r=='"') |
| 806 | return Qbackslash; |
| 807 | return Qok; |
| 808 | } |
| 809 | |
| 810 | static int |
| 811 | imapquote(Fmt *f) |
| 812 | { |
| 813 | char *s, *t; |
| 814 | int w, quotes; |
| 815 | Rune r; |
| 816 | |
| 817 | s = va_arg(f->args, char*); |
| 818 | if(s == nil || *s == '\0') |
| 819 | return fmtstrcpy(f, "\"\""); |
| 820 | |
| 821 | quotes = 0; |
| 822 | if(f->flags&FmtSharp) |
| 823 | quotes = 1; |
| 824 | for(t=s; *t; t+=w){ |
| 825 | w = chartorune(&r, t); |
| 826 | quotes |= needtoquote(r); |
| 827 | } |
| 828 | if(quotes == 0) |
| 829 | return fmtstrcpy(f, s); |
| 830 | |
| 831 | fmtrune(f, '"'); |
| 832 | for(t=s; *t; t+=w){ |
| 833 | w = chartorune(&r, t); |
| 834 | if(needtoquote(r) == Qbackslash) |
| 835 | fmtrune(f, '\\'); |
| 836 | fmtrune(f, r); |
| 837 | } |
| 838 | return fmtrune(f, '"'); |
| 839 | } |
| 840 | |
| 841 | static int |
| 842 | fmttype(char c) |
| 843 | { |
| 844 | switch(c){ |
| 845 | case 'A': |
| 846 | return SxAtom; |
| 847 | case 'L': |
| 848 | return SxList; |
| 849 | case 'N': |
| 850 | return SxNumber; |
| 851 | case 'S': |
| 852 | return SxString; |
| 853 | default: |
| 854 | return -1; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * Check S expression against format string. |
| 860 | */ |
| 861 | static int |
| 862 | sxmatch(Sx *sx, char *fmt) |
| 863 | { |
| 864 | int i; |
| 865 | |
| 866 | for(i=0; fmt[i]; i++){ |
| 867 | if(fmt[i] == '*') |
| 868 | fmt--; /* like i-- but better */ |
| 869 | if(i == sx->nsx && fmt[i+1] == '*') |
| 870 | return 1; |
| 871 | if(i >= sx->nsx) |
| 872 | return 0; |
| 873 | if(sx->sx[i] == nil) |
| 874 | return 0; |
| 875 | if(sx->sx[i]->type == SxAtom && strcmp(sx->sx[i]->data, "NIL") == 0){ |
| 876 | if(fmt[i] == 'L'){ |
| 877 | free(sx->sx[i]->data); |
| 878 | sx->sx[i]->data = nil; |
| 879 | sx->sx[i]->type = SxList; |
| 880 | sx->sx[i]->sx = nil; |
| 881 | sx->sx[i]->nsx = 0; |
| 882 | } |
| 883 | else if(fmt[i] == 'S'){ |
| 884 | free(sx->sx[i]->data); |
| 885 | sx->sx[i]->data = nil; |
| 886 | sx->sx[i]->type = SxString; |
| 887 | } |
| 888 | } |
| 889 | if(sx->sx[i]->type == SxAtom && fmt[i]=='S') |
| 890 | sx->sx[i]->type = SxString; |
| 891 | if(sx->sx[i]->type != fmttype(fmt[i])){ |
| 892 | fprint(2, "sxmatch: %$ not %c\n", sx->sx[i], fmt[i]); |
| 893 | return 0; |
| 894 | } |
| 895 | } |
| 896 | if(i != sx->nsx) |
| 897 | return 0; |
| 898 | return 1; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Check string against format string. |
| 903 | */ |
| 904 | static int |
| 905 | stringmatch(char *fmt, char *s) |
| 906 | { |
| 907 | for(; *fmt && *s; fmt++, s++){ |
| 908 | switch(*fmt){ |
| 909 | case '0': |
| 910 | if(*s == ' ') |
| 911 | break; |
| 912 | /* fall through */ |
| 913 | case '1': |
| 914 | if(*s < '0' || *s > '9') |
| 915 | return 0; |
| 916 | break; |
| 917 | case 'A': |
| 918 | if(*s < 'A' || *s > 'Z') |
| 919 | return 0; |
| 920 | break; |
| 921 | case 'a': |
| 922 | if(*s < 'a' || *s > 'z') |
| 923 | return 0; |
| 924 | break; |
| 925 | case '+': |
| 926 | if(*s != '-' && *s != '+') |
| 927 | return 0; |
| 928 | break; |
| 929 | default: |
| 930 | if(*s != *fmt) |
| 931 | return 0; |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | if(*fmt || *s) |
| 936 | return 0; |
| 937 | return 1; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Parse simple S expressions and IMAP elements. |
| 942 | */ |
| 943 | static int |
| 944 | isatom(Sx *v, char *name) |
| 945 | { |
| 946 | int n; |
| 947 | |
| 948 | if(v == nil || v->type != SxAtom) |
| 949 | return 0; |
| 950 | n = strlen(name); |
| 951 | if(cistrncmp(v->data, name, n) == 0) |
| 952 | if(v->data[n] == 0 || (n>0 && v->data[n-1] == '[')) |
| 953 | return 1; |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static int |
| 958 | isstring(Sx *sx) |
| 959 | { |
| 960 | if(sx->type == SxAtom) |
| 961 | sx->type = SxString; |
| 962 | return sx->type == SxString; |
| 963 | } |
| 964 | |
| 965 | static int |
| 966 | isnumber(Sx *sx) |
| 967 | { |
| 968 | return sx->type == SxNumber; |
| 969 | } |
| 970 | |
| 971 | static int |
| 972 | isnil(Sx *v) |
| 973 | { |
| 974 | return v == nil || |
| 975 | (v->type==SxList && v->nsx == 0) || |
| 976 | (v->type==SxAtom && strcmp(v->data, "NIL") == 0); |
| 977 | } |
| 978 | |
| 979 | static int |
| 980 | islist(Sx *v) |
| 981 | { |
| 982 | return isnil(v) || v->type==SxList; |
| 983 | } |
| 984 | |
| 985 | static uint |
| 986 | parseflags(Sx *v) |
| 987 | { |
| 988 | int f, i, j; |
| 989 | |
| 990 | if(v->type != SxList){ |
| 991 | warn("malformed flags: %$", v); |
| 992 | return 0; |
| 993 | } |
| 994 | f = 0; |
| 995 | for(i=0; i<v->nsx; i++){ |
| 996 | if(v->sx[i]->type != SxAtom) |
| 997 | continue; |
| 998 | for(j=0; j<nelem(flagstab); j++) |
| 999 | if(cistrcmp(v->sx[i]->data, flagstab[j].name) == 0) |
| 1000 | f |= flagstab[j].flag; |
| 1001 | } |
| 1002 | return f; |
| 1003 | } |
| 1004 | |
| 1005 | static char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
| 1006 | static int |
| 1007 | parsemon(char *s) |
| 1008 | { |
| 1009 | int i; |
| 1010 | |
| 1011 | for(i=0; months[i]; i+=3) |
| 1012 | if(memcmp(s, months+i, 3) == 0) |
| 1013 | return i/3; |
| 1014 | return -1; |
| 1015 | } |
| 1016 | |
| 1017 | static uint |
| 1018 | parsedate(Sx *v) |
| 1019 | { |
| 1020 | Tm tm; |
| 1021 | uint t; |
| 1022 | int delta; |
| 1023 | char *p; |
| 1024 | |
| 1025 | if(v->type != SxString || !stringmatch("01-Aaa-1111 01:11:11 +1111", v->data)){ |
| 1026 | bad: |
| 1027 | warn("bad date: %$", v); |
| 1028 | return 0; |
| 1029 | } |
rsc | 95456b2 | 2006-02-18 15:23:34 +0000 | [diff] [blame] | 1030 | |
| 1031 | /* cannot use atoi because 09 is malformed octal! */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1032 | memset(&tm, 0, sizeof tm); |
| 1033 | p = v->data; |
rsc | 95456b2 | 2006-02-18 15:23:34 +0000 | [diff] [blame] | 1034 | tm.mday = strtol(p, 0, 10); |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1035 | tm.mon = parsemon(p+3); |
| 1036 | if(tm.mon == -1) |
| 1037 | goto bad; |
rsc | 95456b2 | 2006-02-18 15:23:34 +0000 | [diff] [blame] | 1038 | tm.year = strtol(p+7, 0, 10) - 1900; |
| 1039 | tm.hour = strtol(p+12, 0, 10); |
| 1040 | tm.min = strtol(p+15, 0, 10); |
| 1041 | tm.sec = strtol(p+18, 0, 10); |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1042 | strcpy(tm.zone, "GMT"); |
| 1043 | |
| 1044 | t = tm2sec(&tm); |
| 1045 | delta = ((p[22]-'0')*10+p[23]-'0')*3600 + ((p[24]-'0')*10+p[25]-'0')*60; |
| 1046 | if(p[21] == '-') |
| 1047 | delta = -delta; |
| 1048 | |
| 1049 | t -= delta; |
| 1050 | return t; |
| 1051 | } |
| 1052 | |
| 1053 | static uint |
| 1054 | parsenumber(Sx *v) |
| 1055 | { |
| 1056 | if(v->type != SxNumber) |
| 1057 | return 0; |
| 1058 | return v->number; |
| 1059 | } |
| 1060 | |
| 1061 | static void |
| 1062 | hash(DigestState *ds, char *tag, char *val) |
| 1063 | { |
| 1064 | if(val == nil) |
| 1065 | val = ""; |
| 1066 | md5((uchar*)tag, strlen(tag)+1, nil, ds); |
| 1067 | md5((uchar*)val, strlen(val)+1, nil, ds); |
| 1068 | } |
| 1069 | |
| 1070 | static Hdr* |
| 1071 | parseenvelope(Sx *v) |
| 1072 | { |
| 1073 | Hdr *hdr; |
| 1074 | uchar digest[16]; |
| 1075 | DigestState ds; |
| 1076 | |
| 1077 | if(v->type != SxList || !sxmatch(v, "SSLLLLLLSS")){ |
| 1078 | warn("bad envelope: %$", v); |
| 1079 | return nil; |
| 1080 | } |
| 1081 | |
| 1082 | hdr = emalloc(sizeof *hdr); |
| 1083 | hdr->date = nstring(v->sx[0]); |
| 1084 | hdr->subject = unrfc2047(nstring(v->sx[1])); |
| 1085 | hdr->from = copyaddrs(v->sx[2]); |
| 1086 | hdr->sender = copyaddrs(v->sx[3]); |
| 1087 | hdr->replyto = copyaddrs(v->sx[4]); |
| 1088 | hdr->to = copyaddrs(v->sx[5]); |
| 1089 | hdr->cc = copyaddrs(v->sx[6]); |
| 1090 | hdr->bcc = copyaddrs(v->sx[7]); |
| 1091 | hdr->inreplyto = unrfc2047(nstring(v->sx[8])); |
| 1092 | hdr->messageid = unrfc2047(nstring(v->sx[9])); |
| 1093 | |
| 1094 | memset(&ds, 0, sizeof ds); |
| 1095 | hash(&ds, "date", hdr->date); |
| 1096 | hash(&ds, "subject", hdr->subject); |
| 1097 | hash(&ds, "from", hdr->from); |
| 1098 | hash(&ds, "sender", hdr->sender); |
| 1099 | hash(&ds, "replyto", hdr->replyto); |
| 1100 | hash(&ds, "to", hdr->to); |
| 1101 | hash(&ds, "cc", hdr->cc); |
| 1102 | hash(&ds, "bcc", hdr->bcc); |
| 1103 | hash(&ds, "inreplyto", hdr->inreplyto); |
| 1104 | hash(&ds, "messageid", hdr->messageid); |
| 1105 | md5(0, 0, digest, &ds); |
| 1106 | hdr->digest = esmprint("%.16H", digest); |
| 1107 | |
| 1108 | return hdr; |
| 1109 | } |
| 1110 | |
| 1111 | static void |
| 1112 | strlwr(char *s) |
| 1113 | { |
| 1114 | char *t; |
| 1115 | |
| 1116 | if(s == nil) |
| 1117 | return; |
| 1118 | for(t=s; *t; t++) |
| 1119 | if('A' <= *t && *t <= 'Z') |
| 1120 | *t += 'a' - 'A'; |
| 1121 | } |
| 1122 | |
| 1123 | static void |
| 1124 | nocr(char *s) |
| 1125 | { |
| 1126 | char *r, *w; |
| 1127 | |
| 1128 | if(s == nil) |
| 1129 | return; |
| 1130 | for(r=w=s; *r; r++) |
| 1131 | if(*r != '\r') |
| 1132 | *w++ = *r; |
| 1133 | *w = 0; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * substitute all occurrences of a with b in s. |
| 1138 | */ |
| 1139 | static char* |
| 1140 | gsub(char *s, char *a, char *b) |
| 1141 | { |
| 1142 | char *p, *t, *w, *last; |
| 1143 | int n; |
| 1144 | |
| 1145 | n = 0; |
| 1146 | for(p=s; (p=strstr(p, a)) != nil; p+=strlen(a)) |
| 1147 | n++; |
| 1148 | if(n == 0) |
| 1149 | return s; |
| 1150 | t = emalloc(strlen(s)+n*strlen(b)+1); |
| 1151 | w = t; |
| 1152 | for(p=s; last=p, (p=strstr(p, a)) != nil; p+=strlen(a)){ |
| 1153 | memmove(w, last, p-last); |
| 1154 | w += p-last; |
| 1155 | memmove(w, b, strlen(b)); |
| 1156 | w += strlen(b); |
| 1157 | } |
| 1158 | strcpy(w, last); |
| 1159 | free(s); |
| 1160 | return t; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * Table-driven IMAP "unexpected response" parser. |
| 1165 | * All the interesting data is in the unexpected responses. |
| 1166 | */ |
| 1167 | static void xlist(Imap*, Sx*); |
| 1168 | static void xrecent(Imap*, Sx*); |
| 1169 | static void xexists(Imap*, Sx*); |
| 1170 | static void xok(Imap*, Sx*); |
| 1171 | static void xflags(Imap*, Sx*); |
| 1172 | static void xfetch(Imap*, Sx*); |
| 1173 | static void xexpunge(Imap*, Sx*); |
| 1174 | static void xbye(Imap*, Sx*); |
| 1175 | static void xsearch(Imap*, Sx*); |
| 1176 | |
| 1177 | static struct { |
| 1178 | int num; |
| 1179 | char *name; |
| 1180 | char *fmt; |
| 1181 | void (*fn)(Imap*, Sx*); |
| 1182 | } unextab[] = { |
| 1183 | 0, "BYE", nil, xbye, |
| 1184 | 0, "FLAGS", "AAL", xflags, |
| 1185 | 0, "LIST", "AALSS", xlist, |
| 1186 | 0, "OK", nil, xok, |
| 1187 | 0, "SEARCH", "AAN*", xsearch, |
| 1188 | |
| 1189 | 1, "EXISTS", "ANA", xexists, |
| 1190 | 1, "EXPUNGE", "ANA", xexpunge, |
| 1191 | 1, "FETCH", "ANAL", xfetch, |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1192 | 1, "RECENT", "ANA", xrecent |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1193 | }; |
| 1194 | |
| 1195 | static void |
| 1196 | unexpected(Imap *z, Sx *sx) |
| 1197 | { |
| 1198 | int i, num; |
| 1199 | char *name; |
| 1200 | |
| 1201 | if(sx->nsx >= 3 && sx->sx[1]->type == SxNumber && sx->sx[2]->type == SxAtom){ |
| 1202 | num = 1; |
| 1203 | name = sx->sx[2]->data; |
| 1204 | }else if(sx->nsx >= 2 && sx->sx[1]->type == SxAtom){ |
| 1205 | num = 0; |
| 1206 | name = sx->sx[1]->data; |
| 1207 | }else |
| 1208 | return; |
| 1209 | |
| 1210 | for(i=0; i<nelem(unextab); i++){ |
| 1211 | if(unextab[i].num == num && cistrcmp(unextab[i].name, name) == 0){ |
| 1212 | if(unextab[i].fmt && !sxmatch(sx, unextab[i].fmt)){ |
| 1213 | warn("malformed %s: %$", name, sx); |
| 1214 | continue; |
| 1215 | } |
| 1216 | unextab[i].fn(z, sx); |
| 1217 | } |
| 1218 | } |
| 1219 | } |
| 1220 | |
rsc | bb70a84 | 2007-06-23 06:12:50 +0000 | [diff] [blame] | 1221 | static int |
| 1222 | alldollars(char *s) |
| 1223 | { |
| 1224 | for(; *s; s++) |
| 1225 | if(*s != '$') |
| 1226 | return 0; |
| 1227 | return 1; |
| 1228 | } |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1229 | |
| 1230 | static void |
| 1231 | xlist(Imap *z, Sx *sx) |
| 1232 | { |
| 1233 | int inbox; |
| 1234 | char *s, *t; |
| 1235 | Box *box; |
| 1236 | |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1237 | s = estrdup(sx->sx[4]->data); |
| 1238 | if(sx->sx[3]->data && strcmp(sx->sx[3]->data, "/") != 0){ |
| 1239 | s = gsub(s, "/", "_"); |
| 1240 | s = gsub(s, sx->sx[3]->data, "/"); |
| 1241 | } |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 1242 | |
| 1243 | /* |
| 1244 | * INBOX is the special imap name for the main mailbox. |
| 1245 | * All other mailbox names have the root prefix removed, if applicable. |
| 1246 | */ |
| 1247 | inbox = 0; |
| 1248 | if(cistrcmp(s, "INBOX") == 0){ |
| 1249 | inbox = 1; |
| 1250 | free(s); |
| 1251 | s = estrdup("mbox"); |
| 1252 | } else if(z->root && strstr(s, z->root) == s) { |
| 1253 | t = estrdup(s+strlen(z->root)); |
| 1254 | free(s); |
| 1255 | s = t; |
| 1256 | } |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1257 | |
| 1258 | /* |
| 1259 | * Plan 9 calls the main mailbox mbox. |
| 1260 | * Rename any existing mbox by appending a $. |
| 1261 | */ |
Russ Cox | e84044b | 2007-11-05 10:55:26 -0500 | [diff] [blame] | 1262 | if(!inbox && strncmp(s, "mbox", 4) == 0 && alldollars(s+4)){ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1263 | t = emalloc(strlen(s)+2); |
| 1264 | strcpy(t, s); |
| 1265 | strcat(t, "$"); |
| 1266 | free(s); |
| 1267 | s = t; |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | box = boxcreate(s); |
| 1271 | if(box == nil) |
| 1272 | return; |
| 1273 | box->imapname = estrdup(sx->sx[4]->data); |
| 1274 | if(inbox) |
| 1275 | z->inbox = box; |
| 1276 | box->mark = 0; |
| 1277 | box->flags = parseflags(sx->sx[2]); |
| 1278 | } |
| 1279 | |
| 1280 | static void |
| 1281 | xrecent(Imap *z, Sx *sx) |
| 1282 | { |
| 1283 | if(z->box) |
| 1284 | z->box->recent = sx->sx[1]->number; |
| 1285 | } |
| 1286 | |
| 1287 | static void |
| 1288 | xexists(Imap *z, Sx *sx) |
| 1289 | { |
| 1290 | if(z->box){ |
| 1291 | z->box->exists = sx->sx[1]->number; |
| 1292 | if(z->box->exists < z->box->maxseen) |
| 1293 | z->box->maxseen = z->box->exists; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | static void |
| 1298 | xflags(Imap *z, Sx *sx) |
| 1299 | { |
Russ Cox | 6b0a42e | 2007-08-22 11:21:52 -0400 | [diff] [blame] | 1300 | /* |
| 1301 | * This response contains in sx->sx[2] the list of flags |
| 1302 | * that can be validly attached to messages in z->box. |
| 1303 | * We don't have any use for this list, since we |
| 1304 | * use only the standard flags. |
| 1305 | */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | static void |
| 1309 | xbye(Imap *z, Sx *sx) |
| 1310 | { |
| 1311 | close(z->fd); |
| 1312 | z->fd = -1; |
| 1313 | z->connected = 0; |
| 1314 | } |
| 1315 | |
| 1316 | static void |
| 1317 | xexpunge(Imap *z, Sx *sx) |
| 1318 | { |
| 1319 | int i, n; |
| 1320 | Box *b; |
| 1321 | |
| 1322 | if((b=z->box) == nil) |
| 1323 | return; |
| 1324 | n = sx->sx[1]->number; |
| 1325 | for(i=0; i<b->nmsg; i++){ |
| 1326 | if(b->msg[i]->imapid == n){ |
| 1327 | msgplumb(b->msg[i], 1); |
| 1328 | msgfree(b->msg[i]); |
| 1329 | b->nmsg--; |
| 1330 | memmove(b->msg+i, b->msg+i+1, (b->nmsg-i)*sizeof b->msg[0]); |
| 1331 | i--; |
| 1332 | b->maxseen--; |
| 1333 | b->exists--; |
| 1334 | continue; |
| 1335 | } |
| 1336 | if(b->msg[i]->imapid > n) |
| 1337 | b->msg[i]->imapid--; |
| 1338 | b->msg[i]->ix = i; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | static void |
| 1343 | xsearch(Imap *z, Sx *sx) |
| 1344 | { |
| 1345 | int i; |
| 1346 | |
| 1347 | free(z->uid); |
| 1348 | z->uid = emalloc((sx->nsx-2)*sizeof z->uid[0]); |
| 1349 | z->nuid = sx->nsx-2; |
| 1350 | for(i=0; i<z->nuid; i++) |
| 1351 | z->uid[i] = sx->sx[i+2]->number; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Table-driven FETCH message info parser. |
| 1356 | */ |
| 1357 | static void xmsgflags(Msg*, Sx*, Sx*); |
| 1358 | static void xmsgdate(Msg*, Sx*, Sx*); |
| 1359 | static void xmsgrfc822size(Msg*, Sx*, Sx*); |
| 1360 | static void xmsgenvelope(Msg*, Sx*, Sx*); |
| 1361 | static void xmsgbody(Msg*, Sx*, Sx*); |
| 1362 | static void xmsgbodydata(Msg*, Sx*, Sx*); |
| 1363 | |
| 1364 | static struct { |
| 1365 | char *name; |
| 1366 | void (*fn)(Msg*, Sx*, Sx*); |
| 1367 | } msgtab[] = { |
| 1368 | "FLAGS", xmsgflags, |
| 1369 | "INTERNALDATE", xmsgdate, |
| 1370 | "RFC822.SIZE", xmsgrfc822size, |
| 1371 | "ENVELOPE", xmsgenvelope, |
| 1372 | "BODY", xmsgbody, |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1373 | "BODY[", xmsgbodydata |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1374 | }; |
| 1375 | |
| 1376 | static void |
| 1377 | xfetch(Imap *z, Sx *sx) |
| 1378 | { |
| 1379 | int i, j, n, uid; |
| 1380 | Msg *msg; |
| 1381 | |
| 1382 | if(z->box == nil){ |
| 1383 | warn("FETCH but no open box: %$", sx); |
| 1384 | return; |
| 1385 | } |
| 1386 | |
| 1387 | /* * 152 FETCH (UID 185 FLAGS () ...) */ |
| 1388 | if(sx->sx[3]->nsx%2){ |
| 1389 | warn("malformed FETCH: %$", sx); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | n = sx->sx[1]->number; |
| 1394 | sx = sx->sx[3]; |
| 1395 | for(i=0; i<sx->nsx; i+=2){ |
| 1396 | if(isatom(sx->sx[i], "UID")){ |
| 1397 | if(sx->sx[i+1]->type == SxNumber){ |
| 1398 | uid = sx->sx[i+1]->number; |
| 1399 | goto haveuid; |
| 1400 | } |
| 1401 | } |
| 1402 | } |
rsc | e12bc7c | 2007-06-23 06:35:51 +0000 | [diff] [blame] | 1403 | /* This happens: too bad. |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1404 | warn("FETCH without UID: %$", sx); |
rsc | e12bc7c | 2007-06-23 06:35:51 +0000 | [diff] [blame] | 1405 | */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1406 | return; |
| 1407 | |
| 1408 | haveuid: |
| 1409 | msg = msgbyimapuid(z->box, uid, 1); |
| 1410 | if(msg->imapid && msg->imapid != n) |
| 1411 | warn("msg id mismatch: want %d have %d", msg->id, n); |
| 1412 | msg->imapid = n; |
| 1413 | for(i=0; i<sx->nsx; i+=2){ |
| 1414 | for(j=0; j<nelem(msgtab); j++) |
| 1415 | if(isatom(sx->sx[i], msgtab[j].name)) |
| 1416 | msgtab[j].fn(msg, sx->sx[i], sx->sx[i+1]); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | static void |
| 1421 | xmsgflags(Msg *msg, Sx *k, Sx *v) |
| 1422 | { |
| 1423 | USED(k); |
| 1424 | msg->flags = parseflags(v); |
| 1425 | } |
| 1426 | |
| 1427 | static void |
| 1428 | xmsgdate(Msg *msg, Sx *k, Sx *v) |
| 1429 | { |
| 1430 | USED(k); |
| 1431 | msg->date = parsedate(v); |
| 1432 | } |
| 1433 | |
| 1434 | static void |
| 1435 | xmsgrfc822size(Msg *msg, Sx *k, Sx *v) |
| 1436 | { |
| 1437 | USED(k); |
| 1438 | msg->size = parsenumber(v); |
| 1439 | } |
| 1440 | |
| 1441 | static char* |
| 1442 | nstring(Sx *v) |
| 1443 | { |
| 1444 | char *p; |
| 1445 | |
Russ Cox | f1ea0d2 | 2007-11-05 10:52:30 -0500 | [diff] [blame] | 1446 | if(isnil(v)) |
| 1447 | return estrdup(""); |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1448 | p = v->data; |
| 1449 | v->data = nil; |
| 1450 | return p; |
| 1451 | } |
| 1452 | |
| 1453 | static char* |
| 1454 | copyaddrs(Sx *v) |
| 1455 | { |
| 1456 | char *s, *sep; |
| 1457 | char *name, *email, *host, *mbox; |
| 1458 | int i; |
| 1459 | Fmt fmt; |
| 1460 | |
| 1461 | if(v->nsx == 0) |
| 1462 | return nil; |
| 1463 | |
| 1464 | fmtstrinit(&fmt); |
| 1465 | sep = ""; |
| 1466 | for(i=0; i<v->nsx; i++){ |
| 1467 | if(!sxmatch(v->sx[i], "SSSS")) |
| 1468 | warn("bad address: %$", v->sx[i]); |
| 1469 | name = unrfc2047(nstring(v->sx[i]->sx[0])); |
| 1470 | /* ignore sx[1] - route */ |
| 1471 | mbox = unrfc2047(nstring(v->sx[i]->sx[2])); |
| 1472 | host = unrfc2047(nstring(v->sx[i]->sx[3])); |
| 1473 | if(mbox == nil || host == nil){ /* rfc822 group syntax */ |
| 1474 | free(name); |
| 1475 | free(mbox); |
| 1476 | free(host); |
| 1477 | continue; |
| 1478 | } |
| 1479 | email = esmprint("%s@%s", mbox, host); |
| 1480 | free(mbox); |
| 1481 | free(host); |
| 1482 | fmtprint(&fmt, "%s%q %q", sep, name ? name : "", email ? email : ""); |
| 1483 | free(name); |
| 1484 | free(email); |
| 1485 | sep = " "; |
| 1486 | } |
| 1487 | s = fmtstrflush(&fmt); |
| 1488 | if(s == nil) |
| 1489 | sysfatal("out of memory"); |
| 1490 | return s; |
| 1491 | } |
| 1492 | |
| 1493 | static void |
| 1494 | xmsgenvelope(Msg *msg, Sx *k, Sx *v) |
| 1495 | { |
| 1496 | hdrfree(msg->part[0]->hdr); |
| 1497 | msg->part[0]->hdr = parseenvelope(v); |
Russ Cox | 478054e | 2008-12-06 18:54:02 -0800 | [diff] [blame] | 1498 | msgplumb(msg, 0); |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | static struct { |
| 1502 | char *name; |
| 1503 | int offset; |
| 1504 | } paramtab[] = { |
rsc | 141d600 | 2006-06-30 04:53:51 +0000 | [diff] [blame] | 1505 | "charset", offsetof(Part, charset), |
| 1506 | "name", offsetof(Part, filename) |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1507 | }; |
| 1508 | |
| 1509 | static void |
| 1510 | parseparams(Part *part, Sx *v) |
| 1511 | { |
| 1512 | int i, j; |
| 1513 | char *s, *t, **p; |
| 1514 | |
| 1515 | if(isnil(v)) |
| 1516 | return; |
| 1517 | if(v->nsx%2){ |
| 1518 | warn("bad message params: %$", v); |
| 1519 | return; |
| 1520 | } |
| 1521 | for(i=0; i<v->nsx; i+=2){ |
| 1522 | s = nstring(v->sx[i]); |
| 1523 | t = nstring(v->sx[i+1]); |
| 1524 | for(j=0; j<nelem(paramtab); j++){ |
| 1525 | if(cistrcmp(paramtab[j].name, s) == 0){ |
| 1526 | p = (char**)((char*)part+paramtab[j].offset); |
| 1527 | free(*p); |
| 1528 | *p = t; |
| 1529 | t = nil; |
| 1530 | break; |
| 1531 | } |
| 1532 | } |
| 1533 | free(s); |
| 1534 | free(t); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | static void |
| 1539 | parsestructure(Part *part, Sx *v) |
| 1540 | { |
| 1541 | int i; |
| 1542 | char *s, *t; |
| 1543 | |
| 1544 | if(isnil(v)) |
| 1545 | return; |
| 1546 | if(v->type != SxList){ |
| 1547 | bad: |
| 1548 | warn("bad structure: %$", v); |
| 1549 | return; |
| 1550 | } |
| 1551 | if(islist(v->sx[0])){ |
| 1552 | /* multipart */ |
| 1553 | for(i=0; i<v->nsx && islist(v->sx[i]); i++) |
| 1554 | parsestructure(partcreate(part->msg, part), v->sx[i]); |
| 1555 | free(part->type); |
| 1556 | if(i != v->nsx-1 || !isstring(v->sx[i])){ |
| 1557 | warn("bad multipart structure: %$", v); |
| 1558 | part->type = estrdup("multipart/mixed"); |
| 1559 | return; |
| 1560 | } |
| 1561 | s = nstring(v->sx[i]); |
| 1562 | strlwr(s); |
| 1563 | part->type = esmprint("multipart/%s", s); |
| 1564 | free(s); |
| 1565 | return; |
| 1566 | } |
| 1567 | /* single part */ |
| 1568 | if(!isstring(v->sx[0]) || v->nsx < 2) |
| 1569 | goto bad; |
| 1570 | s = nstring(v->sx[0]); |
| 1571 | t = nstring(v->sx[1]); |
| 1572 | strlwr(s); |
| 1573 | strlwr(t); |
| 1574 | free(part->type); |
| 1575 | part->type = esmprint("%s/%s", s, t); |
| 1576 | if(v->nsx < 7 || !islist(v->sx[2]) || !isstring(v->sx[3]) |
| 1577 | || !isstring(v->sx[4]) || !isstring(v->sx[5]) || !isnumber(v->sx[6])) |
| 1578 | goto bad; |
| 1579 | parseparams(part, v->sx[2]); |
| 1580 | part->idstr = nstring(v->sx[3]); |
| 1581 | part->desc = nstring(v->sx[4]); |
| 1582 | part->encoding = nstring(v->sx[5]); |
| 1583 | part->size = v->sx[6]->number; |
| 1584 | if(strcmp(s, "message") == 0 && strcmp(t, "rfc822") == 0){ |
| 1585 | if(v->nsx < 10 || !islist(v->sx[7]) || !islist(v->sx[8]) || !isnumber(v->sx[9])) |
| 1586 | goto bad; |
| 1587 | part->hdr = parseenvelope(v->sx[7]); |
| 1588 | parsestructure(partcreate(part->msg, part), v->sx[8]); |
| 1589 | part->lines = v->sx[9]->number; |
| 1590 | } |
| 1591 | if(strcmp(s, "text") == 0){ |
| 1592 | if(v->nsx < 8 || !isnumber(v->sx[7])) |
| 1593 | goto bad; |
| 1594 | part->lines = v->sx[7]->number; |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | static void |
| 1599 | xmsgbody(Msg *msg, Sx *k, Sx *v) |
| 1600 | { |
| 1601 | if(v->type != SxList){ |
| 1602 | warn("bad body: %$", v); |
| 1603 | return; |
| 1604 | } |
| 1605 | /* |
| 1606 | * To follow the structure exactly we should |
| 1607 | * be doing this to partcreate(msg, msg->part[0]), |
| 1608 | * and we should leave msg->part[0] with type message/rfc822, |
| 1609 | * but the extra layer is redundant - what else would be in a mailbox? |
| 1610 | */ |
| 1611 | parsestructure(msg->part[0], v); |
| 1612 | if(msg->box->maxseen < msg->imapid) |
| 1613 | msg->box->maxseen = msg->imapid; |
| 1614 | if(msg->imapuid >= msg->box->uidnext) |
| 1615 | msg->box->uidnext = msg->imapuid+1; |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | static void |
| 1619 | xmsgbodydata(Msg *msg, Sx *k, Sx *v) |
| 1620 | { |
| 1621 | int i; |
| 1622 | char *name, *p; |
| 1623 | Part *part; |
| 1624 | |
| 1625 | name = k->data; |
| 1626 | name += 5; /* body[ */ |
| 1627 | p = strchr(name, ']'); |
| 1628 | if(p) |
| 1629 | *p = 0; |
| 1630 | |
| 1631 | /* now name is something like 1 or 3.2.MIME - walk down parts from root */ |
| 1632 | part = msg->part[0]; |
| 1633 | |
rsc | bb70a84 | 2007-06-23 06:12:50 +0000 | [diff] [blame] | 1634 | |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1635 | while('1' <= name[0] && name[0] <= '9'){ |
| 1636 | i = strtol(name, &p, 10); |
| 1637 | if(*p == '.') |
| 1638 | p++; |
| 1639 | else if(*p != 0){ |
| 1640 | warn("bad body name: %$", k); |
| 1641 | return; |
| 1642 | } |
| 1643 | if((part = subpart(part, i-1)) == nil){ |
| 1644 | warn("unknown body part: %$", k); |
| 1645 | return; |
| 1646 | } |
| 1647 | name = p; |
| 1648 | } |
| 1649 | |
rsc | bb70a84 | 2007-06-23 06:12:50 +0000 | [diff] [blame] | 1650 | |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1651 | if(cistrcmp(name, "") == 0){ |
| 1652 | free(part->raw); |
| 1653 | part->raw = nstring(v); |
| 1654 | nocr(part->raw); |
| 1655 | }else if(cistrcmp(name, "HEADER") == 0){ |
| 1656 | free(part->rawheader); |
| 1657 | part->rawheader = nstring(v); |
| 1658 | nocr(part->rawheader); |
| 1659 | }else if(cistrcmp(name, "MIME") == 0){ |
| 1660 | free(part->mimeheader); |
| 1661 | part->mimeheader = nstring(v); |
| 1662 | nocr(part->mimeheader); |
| 1663 | }else if(cistrcmp(name, "TEXT") == 0){ |
| 1664 | free(part->rawbody); |
| 1665 | part->rawbody = nstring(v); |
| 1666 | nocr(part->rawbody); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * Table-driven OK info parser. |
| 1672 | */ |
| 1673 | static void xokuidvalidity(Imap*, Sx*); |
| 1674 | static void xokpermflags(Imap*, Sx*); |
| 1675 | static void xokunseen(Imap*, Sx*); |
| 1676 | static void xokreadwrite(Imap*, Sx*); |
| 1677 | static void xokreadonly(Imap*, Sx*); |
| 1678 | |
| 1679 | struct { |
| 1680 | char *name; |
| 1681 | char fmt; |
| 1682 | void (*fn)(Imap*, Sx*); |
| 1683 | } oktab[] = { |
| 1684 | "UIDVALIDITY", 'N', xokuidvalidity, |
| 1685 | "PERMANENTFLAGS", 'L', xokpermflags, |
| 1686 | "UNSEEN", 'N', xokunseen, |
| 1687 | "READ-WRITE", 0, xokreadwrite, |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1688 | "READ-ONLY", 0, xokreadonly |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1689 | }; |
| 1690 | |
| 1691 | static void |
| 1692 | xok(Imap *z, Sx *sx) |
| 1693 | { |
| 1694 | int i; |
| 1695 | char *name; |
| 1696 | Sx *arg; |
| 1697 | |
| 1698 | if(sx->nsx >= 4 && sx->sx[2]->type == SxAtom && sx->sx[2]->data[0] == '['){ |
| 1699 | if(sx->sx[3]->type == SxAtom && sx->sx[3]->data[0] == ']') |
| 1700 | arg = nil; |
| 1701 | else if(sx->sx[4]->type == SxAtom && sx->sx[4]->data[0] == ']') |
| 1702 | arg = sx->sx[3]; |
| 1703 | else{ |
| 1704 | warn("cannot parse OK: %$", sx); |
| 1705 | return; |
| 1706 | } |
| 1707 | name = sx->sx[2]->data+1; |
| 1708 | for(i=0; i<nelem(oktab); i++){ |
| 1709 | if(cistrcmp(name, oktab[i].name) == 0){ |
| 1710 | if(oktab[i].fmt && (arg==nil || arg->type != fmttype(oktab[i].fmt))){ |
| 1711 | warn("malformed %s: %$", name, arg); |
| 1712 | continue; |
| 1713 | } |
| 1714 | oktab[i].fn(z, arg); |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | static void |
| 1721 | xokuidvalidity(Imap *z, Sx *sx) |
| 1722 | { |
| 1723 | int i; |
| 1724 | Box *b; |
| 1725 | |
| 1726 | if((b=z->box) == nil) |
| 1727 | return; |
| 1728 | if(b->validity != sx->number){ |
| 1729 | b->validity = sx->number; |
| 1730 | b->uidnext = 1; |
| 1731 | for(i=0; i<b->nmsg; i++) |
| 1732 | msgfree(b->msg[i]); |
| 1733 | free(b->msg); |
| 1734 | b->msg = nil; |
| 1735 | b->nmsg = 0; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | static void |
| 1740 | xokpermflags(Imap *z, Sx *sx) |
| 1741 | { |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1742 | /* z->permflags = parseflags(sx); */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static void |
| 1746 | xokunseen(Imap *z, Sx *sx) |
| 1747 | { |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1748 | /* z->unseen = sx->number; */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | static void |
| 1752 | xokreadwrite(Imap *z, Sx *sx) |
| 1753 | { |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1754 | /* z->boxmode = ORDWR; */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static void |
| 1758 | xokreadonly(Imap *z, Sx *sx) |
| 1759 | { |
rsc | cbeb0b2 | 2006-04-01 19:24:03 +0000 | [diff] [blame] | 1760 | /* z->boxmode = OREAD; */ |
rsc | 941e171 | 2006-02-15 12:39:09 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |