6 #pragma varargck type "M" uchar*
7 #pragma varargck argpos pop3cmd 2
8 #define pdprint(p, ...) if((p)->debug) fprint(2, __VA_ARGS__); else{}
10 typedef struct Popm Popm;
15 typedef struct Pop Pop;
17 char *freep; /* free this to free the strings below */
31 Biobuf bin; /* open network connection */
34 char *lastline; /* from Brdstr */
52 errstr(err, sizeof(err));
57 * get pop3 response line , without worrying
58 * about multiline responses; the clients
59 * will deal with that.
64 return s!=nil && strncmp(s, "+OK", 3)==0;
68 pop3cmd(Pop *pop, char *fmt, ...)
74 vseprint(buf, buf + sizeof buf, fmt, va);
77 p = buf + strlen(buf);
78 if(p > buf + sizeof buf - 3)
79 sysfatal("pop3 command too long");
80 pdprint(pop, "<- %s\n", buf);
82 Bwrite(&pop->bout, buf, strlen(buf));
92 if((s = Brdstr(&pop->bin, '\n', 0)) == nil){
95 return "unexpected eof";
98 p = s + strlen(s) - 1;
99 while(p >= s && (*p == '\r' || *p == '\n'))
102 pdprint(pop, "-> %s\n", s);
109 * get capability list, possibly start tls
117 pop3cmd(pop, "CAPA");
118 if(!isokay(pop3resp(pop)))
124 if(strcmp(s, ".") == 0 || strcmp(s, "unexpected eof") == 0)
126 if(strcmp(s, "STLS") == 0)
128 if(strcmp(s, "PIPELINING") == 0)
130 if(strcmp(s, "EXPIRE 0") == 0)
131 return "server does not allow mail to be left on server";
134 if(hastls && !pop->notls){
135 pop3cmd(pop, "STLS");
136 if(!isokay(s = pop3resp(pop)))
140 if((pop->fd = wraptls(pop->fd, pop->host)) < 0)
143 Binit(&pop->bin, pop->fd, OREAD);
144 Binit(&pop->bout, pop->fd, OWRITE);
150 * log in using APOP if possible, password if allowed by user
157 char ubuf[128], user[128];
163 return "error in initial handshake";
166 snprint(ubuf, sizeof ubuf, " user=%q", pop->user);
170 /* look for apop banner */
171 if(pop->ppop == 0 && (p = strchr(s, '<')) && (q = strchr(p + 1, '>'))) {
173 if((n=auth_respond(p, q - p, user, sizeof user, buf, sizeof buf, auth_getkey, "proto=apop role=client server=%q%s",
174 pop->host, ubuf)) < 0)
175 return "factotum failed";
177 return "factotum did not return a user name";
179 if(s = pop3capa(pop))
182 pop3cmd(pop, "APOP %s %.*s", user, utfnlen(buf, n), buf);
183 if(!isokay(s = pop3resp(pop)))
189 return "no APOP hdr from server";
191 if(s = pop3capa(pop))
194 if(pop->needtls && !pop->encrypted)
195 return "could not negotiate TLS";
197 up = auth_getuserpasswd(auth_getkey, "proto=pass service=pop dom=%q%s",
200 return "no usable keys found";
202 pop3cmd(pop, "USER %s", up->user);
203 if(!isokay(s = pop3resp(pop))){
207 pop3cmd(pop, "PASS %s", up->passwd);
209 if(!isokay(s = pop3resp(pop)))
217 * dial and handshake with pop server
224 if((pop->fd = dial(netmkaddr(pop->host, "net", pop->needssl ? "pop3s" : "pop3"), 0, 0, 0)) < 0)
226 if(pop->needssl && (pop->fd = wraptls(pop->fd, pop->host)) < 0)
228 pop->encrypted = pop->needssl;
229 Binit(&pop->bin, pop->fd, OREAD);
230 Binit(&pop->bout, pop->fd, OWRITE);
231 if(err = pop3login(pop)) {
245 pop3cmd(pop, "QUIT");
251 * download a single message
254 pop3download(Mailbox *mb, Pop *pop, Message *m)
256 char *s, *f[3], *wp, *ep;
262 pop3cmd(pop, "LIST %d", a->mesgno);
263 if(!isokay(s = pop3resp(pop)))
266 if(tokenize(s, f, 3) != 3)
267 return "syntax error in LIST response";
269 if(atoi(f[1]) != a->mesgno)
270 return "out of sync with pop3 server";
272 sz = atoi(f[2]) + 200; /* 200 because the plan9 pop3 server lies */
274 return "invalid size in LIST response";
276 m->start = wp = emalloc(sz + 1);
280 pop3cmd(pop, "RETR %d", a->mesgno);
281 if(!isokay(s = pop3resp(pop))) {
290 if(strcmp(s, "unexpected eof") == 0) {
293 return "unexpected end of conversation";
295 if(strcmp(s, ".") == 0)
304 * grow by 10%/200bytes - some servers
305 * lie about message sizes
313 m->start = erealloc(m->start, sz + 1);
317 memmove(wp, s, l - 1);
322 if(s == nil || strcmp(s, ".") != 0)
323 return "out of sync with pop3 server";
328 * make sure there's a trailing null
329 * (helps in body searches)
332 m->bend = m->rbend = m->end;
333 m->header = m->start;
334 m->size = m->end - m->start;
336 digestmessage(mb, m);
342 * check for new messages on pop server
343 * UIDL is not required by RFC 1939, but
344 * netscape requires it, so almost every server supports it.
345 * we'll use it to make our lives easier.
348 pop3read(Pop *pop, Mailbox *mb)
350 char *s, *p, *uidl, *f[2];
352 Message *m, *next, **l;
355 /* Some POP servers disallow UIDL if the maildrop is empty. */
356 pop3cmd(pop, "STAT");
357 if(!isokay(s = pop3resp(pop)))
360 /* fetch message listing; note messages to grab */
362 if(strncmp(s, "+OK 0 ", 6) != 0) {
363 pop3cmd(pop, "UIDL");
364 if(!isokay(s = pop3resp(pop)))
369 if(strcmp(p, ".") == 0 || strcmp(p, "unexpected eof") == 0)
372 if(tokenize(p, f, 2) != 2)
377 if(strlen(uidl) > 75) /* RFC 1939 says 70 characters max */
383 if(strcmp((*l)->idxaux, uidl) == 0){
388 m->aux = a = emalloc(sizeof *a);
390 /* matches mail we already have, note mesgno for deletion */
396 /* old mail no longer in box mark deleted */
398 (*l)->deleted = Deleted;
405 m = newmessage(mb->root);
408 m->idxaux = strdup(uidl);
409 m->aux = a = emalloc(sizeof *a);
412 /* chain in; will fill in message later */
418 /* whatever is left has been removed from the mbox, mark as deleted */
421 (*l)->deleted = Disappear;
425 /* download new messages */
427 switch(rfork(RFPROC|RFMEM)){
429 eprint("pop3: rfork: %r\n");
436 for(m = mb->root->part; m != nil; m = m->next){
437 if(m->start != nil || m->deleted)
439 Bprint(&pop->bout, "LIST %d\r\nRETR %d\r\n", mesgno(m), mesgno(m));
446 for(m = mb->root->part; m != nil; m = next) {
449 if(m->start != nil || m->deleted)
451 if(s = pop3download(mb, pop, m)) {
452 eprint("pop3: download %d: %s\n", mesgno(m), s);
453 unnewmessage(mb, mb->root, m);
464 * delete marked messages
467 pop3purge(Pop *pop, Mailbox *mb)
472 switch(rfork(RFPROC|RFMEM)){
474 eprint("pop3: rfork: %r\n");
481 for(m = mb->root->part; m != nil; m = m->next){
482 if(m->deleted && m->inmbox)
483 Bprint(&pop->bout, "DELE %d\r\n", mesgno(m));
489 for(m = mb->root->part; m != nil; m = m->next) {
490 if(m->deleted && m->inmbox) {
492 pop3cmd(pop, "DELE %d", mesgno(m));
493 if(!isokay(pop3resp(pop)))
501 /* connect to pop3 server, sync mailbox */
503 pop3sync(Mailbox *mb)
509 if(err = pop3dial(pop))
511 if((err = pop3read(pop, mb)) == nil)
515 mb->waketime = (ulong)time(0) + pop->refreshtime;
519 static char Epop3ctl[] = "bad pop3 control message";
522 pop3ctl(Mailbox *mb, int argc, char **argv)
531 if(argc==1 && strcmp(argv[0], "debug")==0){
536 if(argc==1 && strcmp(argv[0], "nodebug")==0){
541 if(strcmp(argv[0], "refresh")==0){
543 pop->refreshtime = 60;
550 pop->refreshtime = n;
558 /* free extra memory associated with mb */
560 pop3close(Mailbox *mb)
570 mkmbox(Pop *pop, char *p, char *e)
572 p = seprint(p, e, "%s/box/%s/pop.%s", MAILROOT, getlog(), pop->host);
573 if(pop->user && strcmp(pop->user, getlog()))
574 p = seprint(p, e, ".%s", pop->user);
579 * open mailboxes of the form /pop/host/user or /apop/host/user
582 pop3mbox(Mailbox *mb, char *path)
585 int nf, apop, ppop, popssl, apopssl, apoptls, popnotls, apopnotls, poptls;
588 popssl = strncmp(path, "/pops/", 6) == 0;
589 apopssl = strncmp(path, "/apops/", 7) == 0;
590 poptls = strncmp(path, "/poptls/", 8) == 0;
591 popnotls = strncmp(path, "/popnotls/", 10) == 0;
592 ppop = popssl || poptls || popnotls || strncmp(path, "/pop/", 5) == 0;
593 apoptls = strncmp(path, "/apoptls/", 9) == 0;
594 apopnotls = strncmp(path, "/apopnotls/", 11) == 0;
595 apop = apopssl || apoptls || apopnotls || strncmp(path, "/apop/", 6) == 0;
602 return "out of memory";
604 nf = getfields(path, f, nelem(f), 0, "/");
605 if(nf != 3 && nf != 4) {
607 return "bad pop3 path syntax /[a]pop[tls|ssl]/system[/user]";
610 pop = emalloc(sizeof *pop);
618 pop->needssl = popssl || apopssl;
619 pop->needtls = poptls || apoptls;
620 pop->refreshtime = 60;
621 pop->notls = popnotls || apopnotls;
622 mkmbox(pop, mb->path, mb->path + sizeof mb->path);
625 mb->close = pop3close;