(Pseudo) Fix history (non gabbiness) by stripping PS_ARGLIST_MASK
[s-mailx.git] / pop3.c
blob71ec276c845932b79615ef171c3cf120a41c6622
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ POP3 (RFCs 1939, 2595) client.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 2002
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 #undef n_FILE
40 #define n_FILE pop3
42 #ifndef HAVE_AMALGAMATION
43 # include "nail.h"
44 #endif
46 EMPTY_FILE()
47 #ifdef HAVE_POP3
49 #define POP3_ANSWER(RV,ACTIONSTOP) \
50 do if (((RV) = pop3_answer(mp)) == STOP) {\
51 ACTIONSTOP;\
52 } while (0)
54 #define POP3_OUT(RV,X,Y,ACTIONSTOP) \
55 do {\
56 if (((RV) = pop3_finish(mp)) == STOP) {\
57 ACTIONSTOP;\
59 if (options & OPT_VERBVERB)\
60 n_err(">>> %s", X);\
61 mp->mb_active |= Y;\
62 if (((RV) = swrite(&mp->mb_sock, X)) == STOP) {\
63 ACTIONSTOP;\
65 } while (0)
67 static char *_pop3_buf;
68 static size_t _pop3_bufsize;
69 static sigjmp_buf _pop3_jmp;
70 static sighandler_type _pop3_savealrm;
71 static int _pop3_keepalive;
72 static int volatile _pop3_lock;
74 /* Perform entire login handshake */
75 static enum okay _pop3_login(struct mailbox *mp, struct sockconn *scp);
77 /* APOP: get greeting credential or NULL */
78 #ifdef HAVE_MD5
79 static char * _pop3_lookup_apop_timestamp(char const *bp);
80 #endif
82 /* Several authentication methods */
83 #ifdef HAVE_MD5
84 static enum okay _pop3_auth_apop(struct mailbox *mp,
85 struct sockconn const *scp, char const *ts);
86 #endif
87 static enum okay _pop3_auth_plain(struct mailbox *mp,
88 struct sockconn const *scp);
90 static void pop3_timer_off(void);
91 static enum okay pop3_answer(struct mailbox *mp);
92 static enum okay pop3_finish(struct mailbox *mp);
93 static void pop3catch(int s);
94 static void _pop3_maincatch(int s);
95 static enum okay pop3_noop1(struct mailbox *mp);
96 static void pop3alarm(int s);
97 static enum okay pop3_stat(struct mailbox *mp, off_t *size, int *cnt);
98 static enum okay pop3_list(struct mailbox *mp, int n, size_t *size);
99 static void pop3_setptr(struct mailbox *mp,
100 struct sockconn const *scp);
101 static enum okay pop3_get(struct mailbox *mp, struct message *m,
102 enum needspec need);
103 static enum okay pop3_exit(struct mailbox *mp);
104 static enum okay pop3_delete(struct mailbox *mp, int n);
105 static enum okay pop3_update(struct mailbox *mp);
107 static enum okay
108 _pop3_login(struct mailbox *mp, struct sockconn *scp)
110 #ifdef HAVE_MD5
111 char *ts;
112 #endif
113 enum okey_xlook_mode oxm;
114 enum okay rv;
115 NYD_ENTER;
117 oxm = ok_blook(v15_compat) ? OXM_ALL : OXM_PLAIN | OXM_U_H_P;
119 /* Get the greeting, check whether APOP is advertised */
120 POP3_ANSWER(rv, goto jleave);
121 #ifdef HAVE_MD5
122 ts = _pop3_lookup_apop_timestamp(_pop3_buf);
123 #endif
125 /* If not yet secured, can we upgrade to TLS? */
126 #ifdef HAVE_SSL
127 if (!scp->sc_url.url_needs_tls &&
128 xok_blook(pop3_use_starttls, &scp->sc_url, oxm)) {
129 POP3_OUT(rv, "STLS" NETNL, MB_COMD, goto jleave);
130 POP3_ANSWER(rv, goto jleave);
131 if ((rv = ssl_open(&scp->sc_url, &scp->sc_sock)) != OKAY)
132 goto jleave;
134 #else
135 if (xok_blook(pop3_use_starttls, &scp->sc_url, oxm)) {
136 n_err(_("No SSL support compiled in\n"));
137 rv = STOP;
138 goto jleave;
140 #endif
142 /* Use the APOP single roundtrip? */
143 #ifdef HAVE_MD5
144 if (ts != NULL && !xok_blook(pop3_no_apop, &scp->sc_url, oxm)) {
145 if ((rv = _pop3_auth_apop(mp, scp, ts)) != OKAY) {
146 char const *ccp;
148 # ifdef HAVE_SSL
149 if (scp->sc_sock.s_use_ssl)
150 ccp = _("over an encrypted connection");
151 else
152 # endif
153 ccp = _("(unsafe clear text!)");
154 n_err(_("POP3 APOP authentication failed!\n"
155 " Server indicated support.. Set *pop3-no-apop*\n"
156 " for plain text authentication %s\n"), ccp);
158 goto jleave;
160 #endif
162 rv = _pop3_auth_plain(mp, scp);
163 jleave:
164 NYD_LEAVE;
165 return rv;
168 #ifdef HAVE_MD5
169 static char *
170 _pop3_lookup_apop_timestamp(char const *bp)
172 /* RFC 1939:
173 * A POP3 server which implements the APOP command will include
174 * a timestamp in its banner greeting. The syntax of the timestamp
175 * corresponds to the "msg-id" in [RFC822]
176 * RFC 822:
177 * msg-id = "<" addr-spec ">"
178 * addr-spec = local-part "@" domain */
179 char const *cp, *ep;
180 size_t tl;
181 char *rp = NULL;
182 bool_t hadat = FAL0;
183 NYD_ENTER;
185 if ((cp = strchr(bp, '<')) == NULL)
186 goto jleave;
188 /* xxx What about malformed APOP timestamp (<@>) here? */
189 for (ep = cp; *ep != '\0'; ++ep) {
190 if (spacechar(*ep))
191 goto jleave;
192 else if (*ep == '@')
193 hadat = TRU1;
194 else if (*ep == '>') {
195 if (!hadat)
196 goto jleave;
197 break;
200 if (*ep != '>')
201 goto jleave;
203 tl = PTR2SIZE(++ep - cp);
204 rp = salloc(tl +1);
205 memcpy(rp, cp, tl);
206 rp[tl] = '\0';
207 jleave:
208 NYD_LEAVE;
209 return rp;
211 #endif
213 #ifdef HAVE_MD5
214 static enum okay
215 _pop3_auth_apop(struct mailbox *mp, struct sockconn const *scp, char const *ts)
217 unsigned char digest[16];
218 char hex[MD5TOHEX_SIZE], *cp;
219 md5_ctx ctx;
220 size_t i;
221 enum okay rv = STOP;
222 NYD_ENTER;
224 md5_init(&ctx);
225 md5_update(&ctx, (uc_i*)UNCONST(ts), strlen(ts));
226 md5_update(&ctx, (uc_i*)scp->sc_cred.cc_pass.s, scp->sc_cred.cc_pass.l);
227 md5_final(digest, &ctx);
228 md5tohex(hex, digest);
230 i = scp->sc_cred.cc_user.l;
231 cp = ac_alloc(5 + i + 1 + MD5TOHEX_SIZE + sizeof(NETNL)-1 +1);
233 memcpy(cp, "APOP ", 5);
234 memcpy(cp + 5, scp->sc_cred.cc_user.s, i);
235 i += 5;
236 cp[i++] = ' ';
237 memcpy(cp + i, hex, MD5TOHEX_SIZE);
238 i += MD5TOHEX_SIZE;
239 memcpy(cp + i, NETNL, sizeof(NETNL));
240 POP3_OUT(rv, cp, MB_COMD, goto jleave);
241 POP3_ANSWER(rv, goto jleave);
243 rv = OKAY;
244 jleave:
245 ac_free(cp);
246 NYD_LEAVE;
247 return rv;
249 #endif /* HAVE_MD5 */
251 static enum okay
252 _pop3_auth_plain(struct mailbox *mp, struct sockconn const *scp)
254 char *cp;
255 enum okay rv = STOP;
256 NYD_ENTER;
258 /* The USER/PASS plain text version */
259 cp = ac_alloc(MAX(scp->sc_cred.cc_user.l, scp->sc_cred.cc_pass.l) + 5 +
260 sizeof(NETNL)-1 +1);
262 memcpy(cp, "USER ", 5);
263 memcpy(cp + 5, scp->sc_cred.cc_user.s, scp->sc_cred.cc_user.l);
264 memcpy(cp + 5 + scp->sc_cred.cc_user.l, NETNL, sizeof(NETNL));
265 POP3_OUT(rv, cp, MB_COMD, goto jleave);
266 POP3_ANSWER(rv, goto jleave);
268 memcpy(cp, "PASS ", 5);
269 memcpy(cp + 5, scp->sc_cred.cc_pass.s, scp->sc_cred.cc_pass.l);
270 memcpy(cp + 5 + scp->sc_cred.cc_pass.l, NETNL, sizeof(NETNL));
271 POP3_OUT(rv, cp, MB_COMD, goto jleave);
272 POP3_ANSWER(rv, goto jleave);
274 rv = OKAY;
275 jleave:
276 ac_free(cp);
277 NYD_LEAVE;
278 return rv;
281 static void
282 pop3_timer_off(void)
284 NYD_ENTER;
285 if (_pop3_keepalive > 0) {
286 alarm(0);
287 safe_signal(SIGALRM, _pop3_savealrm);
289 NYD_LEAVE;
292 static enum okay
293 pop3_answer(struct mailbox *mp)
295 int sz;
296 size_t blen;
297 enum okay rv = STOP;
298 NYD_ENTER;
300 jretry:
301 if ((sz = sgetline(&_pop3_buf, &_pop3_bufsize, &blen, &mp->mb_sock)) > 0) {
302 if ((mp->mb_active & (MB_COMD | MB_MULT)) == MB_MULT)
303 goto jmultiline;
304 if (options & OPT_VERBVERB)
305 n_err(_pop3_buf);
306 switch (*_pop3_buf) {
307 case '+':
308 rv = OKAY;
309 mp->mb_active &= ~MB_COMD;
310 break;
311 case '-':
312 rv = STOP;
313 mp->mb_active = MB_NONE;
314 while (blen > 0 &&
315 (_pop3_buf[blen - 1] == NETNL[0] ||
316 _pop3_buf[blen - 1] == NETNL[1]))
317 _pop3_buf[--blen] = '\0';
318 n_err(_("POP3 error: %s\n"), _pop3_buf);
319 break;
320 default:
321 /* If the answer starts neither with '+' nor with '-', it must be part
322 * of a multiline response. Get lines until a single dot appears */
323 jmultiline:
324 while (_pop3_buf[0] != '.' || _pop3_buf[1] != NETNL[0] ||
325 _pop3_buf[2] != NETNL[1] || _pop3_buf[3] != '\0') {
326 sz = sgetline(&_pop3_buf, &_pop3_bufsize, NULL, &mp->mb_sock);
327 if (sz <= 0)
328 goto jeof;
330 mp->mb_active &= ~MB_MULT;
331 if (mp->mb_active != MB_NONE)
332 goto jretry;
334 } else {
335 jeof:
336 rv = STOP;
337 mp->mb_active = MB_NONE;
339 NYD_LEAVE;
340 return rv;
343 static enum okay
344 pop3_finish(struct mailbox *mp)
346 NYD_ENTER;
347 while (mp->mb_sock.s_fd > 0 && mp->mb_active != MB_NONE)
348 pop3_answer(mp);
349 NYD_LEAVE;
350 return OKAY;
353 static void
354 pop3catch(int s)
356 NYD_X; /* Signal handler */
357 switch (s) {
358 case SIGINT:
359 /*n_err_sighdl(_("Interrupt during POP3 operation\n"));*/
360 interrupts = 2; /* Force "Interrupt" message shall we onintr(0) */
361 siglongjmp(_pop3_jmp, 1);
362 case SIGPIPE:
363 n_err_sighdl(_("Received SIGPIPE during POP3 operation\n"));
364 break;
368 static void
369 _pop3_maincatch(int s)
371 NYD_X; /* Signal handler */
372 UNUSED(s);
373 if (interrupts++ == 0)
374 n_err_sighdl(_("\n(Interrupt -- one more to abort operation)\n"));
375 else
376 siglongjmp(_pop3_jmp, 1);
379 static enum okay
380 pop3_noop1(struct mailbox *mp)
382 enum okay rv;
383 NYD_ENTER;
385 POP3_OUT(rv, "NOOP" NETNL, MB_COMD, goto jleave);
386 POP3_ANSWER(rv, goto jleave);
387 jleave:
388 NYD_LEAVE;
389 return rv;
392 static void
393 pop3alarm(int s)
395 sighandler_type volatile saveint, savepipe;
396 NYD_X; /* Signal handler */
397 UNUSED(s);
399 if (_pop3_lock++ == 0) {
400 hold_all_sigs();
401 if ((saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
402 safe_signal(SIGINT, &_pop3_maincatch);
403 savepipe = safe_signal(SIGPIPE, SIG_IGN);
404 if (sigsetjmp(_pop3_jmp, 1)) {
405 interrupts = 0;
406 safe_signal(SIGINT, saveint);
407 safe_signal(SIGPIPE, savepipe);
408 goto jbrk;
410 if (savepipe != SIG_IGN)
411 safe_signal(SIGPIPE, pop3catch);
412 rele_all_sigs();
413 if (pop3_noop1(&mb) != OKAY) {
414 safe_signal(SIGINT, saveint);
415 safe_signal(SIGPIPE, savepipe);
416 goto jleave;
418 safe_signal(SIGINT, saveint);
419 safe_signal(SIGPIPE, savepipe);
421 jbrk:
422 alarm(_pop3_keepalive);
423 jleave:
424 --_pop3_lock;
427 static enum okay
428 pop3_stat(struct mailbox *mp, off_t *size, int *cnt)
430 char *cp;
431 enum okay rv;
432 NYD_ENTER;
434 POP3_OUT(rv, "STAT" NETNL, MB_COMD, goto jleave);
435 POP3_ANSWER(rv, goto jleave);
437 for (cp = _pop3_buf; *cp != '\0' && !spacechar(*cp); ++cp)
439 while (*cp != '\0' && spacechar(*cp))
440 ++cp;
442 if (*cp != '\0') {
443 *cnt = (int)strtol(cp, NULL, 10);
444 while (*cp != '\0' && !spacechar(*cp))
445 ++cp;
446 while (*cp != '\0' && spacechar(*cp))
447 ++cp;
448 if (*cp != '\0')
449 *size = (int)strtol(cp, NULL, 10);
450 else
451 rv = STOP;
452 } else
453 rv = STOP;
455 if (rv == STOP)
456 n_err(_("Invalid POP3 STAT response: %s\n"), _pop3_buf);
457 jleave:
458 NYD_LEAVE;
459 return rv;
462 static enum okay
463 pop3_list(struct mailbox *mp, int n, size_t *size)
465 char o[LINESIZE], *cp;
466 enum okay rv;
467 NYD_ENTER;
469 snprintf(o, sizeof o, "LIST %u" NETNL, n);
470 POP3_OUT(rv, o, MB_COMD, goto jleave);
471 POP3_ANSWER(rv, goto jleave);
473 for (cp = _pop3_buf; *cp != '\0' && !spacechar(*cp); ++cp)
475 while (*cp != '\0' && spacechar(*cp))
476 ++cp;
477 while (*cp != '\0' && !spacechar(*cp))
478 ++cp;
479 while (*cp != '\0' && spacechar(*cp))
480 ++cp;
481 if (*cp != '\0')
482 *size = (size_t)strtol(cp, NULL, 10);
483 jleave:
484 NYD_LEAVE;
485 return rv;
488 static void
489 pop3_setptr(struct mailbox *mp, struct sockconn const *scp)
491 size_t i;
492 enum needspec ns;
493 NYD_ENTER;
495 message = scalloc(msgCount + 1, sizeof *message);
496 message[msgCount].m_size = 0;
497 message[msgCount].m_lines = 0;
498 dot = message; /* (Just do it: avoid crash -- shall i now do ointr(0).. */
500 for (i = 0; UICMP(z, i, <, msgCount); ++i) {
501 struct message *m = message + i;
502 m->m_flag = MUSED | MNEW | MNOFROM | MNEWEST;
503 m->m_block = 0;
504 m->m_offset = 0;
505 m->m_size = m->m_xsize = 0;
508 for (i = 0; UICMP(z, i, <, msgCount); ++i)
509 if (!pop3_list(mp, i + 1, &message[i].m_xsize))
510 goto jleave;
512 /* Force the load of all messages right now */
513 ns = xok_blook(pop3_bulk_load, &scp->sc_url, OXM_ALL)
514 ? NEED_BODY : NEED_HEADER;
515 for (i = 0; UICMP(z, i, <, msgCount); ++i)
516 if (!pop3_get(mp, message + i, ns))
517 goto jleave;
519 srelax_hold();
520 for (i = 0; UICMP(z, i, <, msgCount); ++i) {
521 struct message *m = message + i;
522 char const *cp;
524 if ((cp = hfield1("status", m)) != NULL)
525 while (*cp != '\0') {
526 if (*cp == 'R')
527 m->m_flag |= MREAD;
528 else if (*cp == 'O')
529 m->m_flag &= ~MNEW;
530 ++cp;
533 substdate(m);
534 srelax();
536 srelax_rele();
538 setdot(message);
539 jleave:
540 NYD_LEAVE;
543 static enum okay
544 pop3_get(struct mailbox *mp, struct message *m, enum needspec volatile need)
546 char o[LINESIZE], *line, *lp;
547 sighandler_type volatile saveint, savepipe;
548 size_t linesize, linelen, size;
549 int number, lines;
550 int volatile emptyline;
551 off_t offset;
552 enum okay volatile rv;
553 NYD_ENTER;
555 line = NULL; /* TODO line pool */
556 saveint = savepipe = SIG_IGN;
557 linesize = 0;
558 number = (int)PTR2SIZE(m - message + 1);
559 emptyline = 0;
560 rv = STOP;
562 if (mp->mb_sock.s_fd < 0) {
563 n_err(_("POP3 connection already closed\n"));
564 ++_pop3_lock;
565 goto jleave;
568 if (_pop3_lock++ == 0) {
569 hold_all_sigs();
570 if ((saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
571 safe_signal(SIGINT, &_pop3_maincatch);
572 savepipe = safe_signal(SIGPIPE, SIG_IGN);
573 if (sigsetjmp(_pop3_jmp, 1))
574 goto jleave;
575 if (savepipe != SIG_IGN)
576 safe_signal(SIGPIPE, pop3catch);
577 rele_all_sigs();
580 fseek(mp->mb_otf, 0L, SEEK_END);
581 offset = ftell(mp->mb_otf);
582 jretry:
583 switch (need) {
584 case NEED_HEADER:
585 snprintf(o, sizeof o, "TOP %u 0" NETNL, number);
586 break;
587 case NEED_BODY:
588 snprintf(o, sizeof o, "RETR %u" NETNL, number);
589 break;
590 case NEED_UNSPEC:
591 abort(); /* XXX */
593 POP3_OUT(rv, o, MB_COMD | MB_MULT, goto jleave);
595 if (pop3_answer(mp) == STOP) {
596 if (need == NEED_HEADER) {
597 /* The TOP POP3 command is optional, so retry with entire message */
598 need = NEED_BODY;
599 goto jretry;
601 goto jleave;
604 size = 0;
605 lines = 0;
606 while (sgetline(&line, &linesize, &linelen, &mp->mb_sock) > 0) {
607 if (line[0] == '.' && line[1] == NETNL[0] && line[2] == NETNL[1] &&
608 line[3] == '\0') {
609 mp->mb_active &= ~MB_MULT;
610 break;
612 if (line[0] == '.') {
613 lp = line + 1;
614 --linelen;
615 } else
616 lp = line;
617 /* TODO >>
618 * Need to mask 'From ' lines. This cannot be done properly
619 * since some servers pass them as 'From ' and others as
620 * '>From '. Although one could identify the first kind of
621 * server in principle, it is not possible to identify the
622 * second as '>From ' may also come from a server of the
623 * first type as actual data. So do what is absolutely
624 * necessary only - mask 'From '.
626 * If the line is the first line of the message header, it
627 * is likely a real 'From ' line. In this case, it is just
628 * ignored since it violates all standards.
629 * TODO i have *never* seen the latter?!?!?
630 * TODO <<
632 /* Since we simply copy over data without doing any transfer
633 * encoding reclassification/adjustment we *have* to perform
634 * RFC 4155 compliant From_ quoting here */
635 if (emptyline && is_head(lp, linelen, FAL0)) {
636 putc('>', mp->mb_otf);
637 ++size;
639 lines++;
640 if (lp[linelen-1] == NETNL[1] &&
641 (linelen == 1 || lp[linelen-2] == NETNL[0])) {
642 emptyline = linelen <= 2;
643 if (linelen > 2)
644 fwrite(lp, 1, linelen - 2, mp->mb_otf);
645 putc('\n', mp->mb_otf);
646 size += linelen - 1;
647 } else {
648 emptyline = 0;
649 fwrite(lp, 1, linelen, mp->mb_otf);
650 size += linelen;
653 if (!emptyline) {
654 /* This is very ugly; but some POP3 daemons don't end a
655 * message with NETNL NETNL, and we need \n\n for mbox format */
656 putc('\n', mp->mb_otf);
657 ++lines;
658 ++size;
660 m->m_size = size;
661 m->m_lines = lines;
662 m->m_block = mailx_blockof(offset);
663 m->m_offset = mailx_offsetof(offset);
664 fflush(mp->mb_otf);
666 switch (need) {
667 case NEED_HEADER:
668 m->m_have |= HAVE_HEADER;
669 break;
670 case NEED_BODY:
671 m->m_have |= HAVE_HEADER | HAVE_BODY;
672 m->m_xlines = m->m_lines;
673 m->m_xsize = m->m_size;
674 break;
675 case NEED_UNSPEC:
676 break;
679 rv = OKAY;
680 jleave:
681 if (line != NULL)
682 free(line);
683 if (saveint != SIG_IGN)
684 safe_signal(SIGINT, saveint);
685 if (savepipe != SIG_IGN)
686 safe_signal(SIGPIPE, savepipe);
687 --_pop3_lock;
688 NYD_LEAVE;
689 if (interrupts)
690 n_raise(SIGINT);
691 return rv;
694 static enum okay
695 pop3_exit(struct mailbox *mp)
697 enum okay rv;
698 NYD_ENTER;
700 POP3_OUT(rv, "QUIT" NETNL, MB_COMD, goto jleave);
701 POP3_ANSWER(rv, goto jleave);
702 jleave:
703 NYD_LEAVE;
704 return rv;
707 static enum okay
708 pop3_delete(struct mailbox *mp, int n)
710 char o[LINESIZE];
711 enum okay rv;
712 NYD_ENTER;
714 snprintf(o, sizeof o, "DELE %u" NETNL, n);
715 POP3_OUT(rv, o, MB_COMD, goto jleave);
716 POP3_ANSWER(rv, goto jleave);
717 jleave:
718 NYD_LEAVE;
719 return rv;
722 static enum okay
723 pop3_update(struct mailbox *mp)
725 struct message *m;
726 int dodel, c, gotcha, held;
727 NYD_ENTER;
729 if (!(pstate & PS_EDIT)) {
730 holdbits();
731 c = 0;
732 for (m = message; PTRCMP(m, <, message + msgCount); ++m)
733 if (m->m_flag & MBOX)
734 ++c;
735 if (c > 0)
736 makembox();
739 gotcha = held = 0;
740 for (m = message; PTRCMP(m, <, message + msgCount); ++m) {
741 if (pstate & PS_EDIT)
742 dodel = m->m_flag & MDELETED;
743 else
744 dodel = !((m->m_flag & MPRESERVE) || !(m->m_flag & MTOUCH));
745 if (dodel) {
746 pop3_delete(mp, PTR2SIZE(m - message + 1));
747 ++gotcha;
748 } else
749 ++held;
752 /* C99 */{
753 char const *dnq;
755 dnq = n_shexp_quote_cp(displayname, FAL0);
757 if (gotcha && (pstate & PS_EDIT)) {
758 printf(_("%s "), dnq);
759 printf((ok_blook(bsdcompat) || ok_blook(bsdmsgs))
760 ? _("complete\n") : _("updated\n"));
761 } else if (held && !(pstate & PS_EDIT)) {
762 if (held == 1)
763 printf(_("Held 1 message in %s\n"), dnq);
764 else
765 printf(_("Held %d messages in %s\n"), held, dnq);
768 fflush(stdout);
769 NYD_LEAVE;
770 return OKAY;
773 FL enum okay
774 pop3_noop(void)
776 sighandler_type volatile saveint, savepipe;
777 enum okay volatile rv = STOP;
778 NYD_ENTER;
780 _pop3_lock = 1;
781 hold_all_sigs();
782 if ((saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
783 safe_signal(SIGINT, &_pop3_maincatch);
784 savepipe = safe_signal(SIGPIPE, SIG_IGN);
785 if (sigsetjmp(_pop3_jmp, 1) == 0) {
786 if (savepipe != SIG_IGN)
787 safe_signal(SIGPIPE, pop3catch);
788 rele_all_sigs();
789 rv = pop3_noop1(&mb);
791 safe_signal(SIGINT, saveint);
792 safe_signal(SIGPIPE, savepipe);
793 _pop3_lock = 0;
794 NYD_LEAVE;
795 return rv;
798 FL int
799 pop3_setfile(char const *server, enum fedit_mode fm)
801 struct sockconn sc;
802 sighandler_type saveint, savepipe;
803 char const *cp;
804 int volatile rv;
805 NYD_ENTER;
807 rv = 1;
808 if (fm & FEDIT_NEWMAIL)
809 goto jleave;
810 rv = -1;
812 if (!url_parse(&sc.sc_url, CPROTO_POP3, server))
813 goto jleave;
814 if (!ok_blook(v15_compat) &&
815 (!sc.sc_url.url_had_user || sc.sc_url.url_pass.s != NULL)) {
816 n_err(_("New-style URL used without *v15-compat* being set\n"));
817 goto jleave;
820 if (!(ok_blook(v15_compat) ? ccred_lookup(&sc.sc_cred, &sc.sc_url)
821 : ccred_lookup_old(&sc.sc_cred, CPROTO_POP3,
822 (sc.sc_url.url_had_user ? sc.sc_url.url_eu_h_p.s
823 : sc.sc_url.url_u_h_p.s))))
824 goto jleave;
826 if (!quit())
827 goto jleave;
829 if (!sopen(&sc.sc_sock, &sc.sc_url))
830 goto jleave;
832 rv = 1;
834 if (fm & FEDIT_SYSBOX)
835 pstate &= ~PS_EDIT;
836 else
837 pstate |= PS_EDIT;
838 if (mb.mb_sock.s_fd >= 0)
839 sclose(&mb.mb_sock);
840 if (mb.mb_itf) {
841 fclose(mb.mb_itf);
842 mb.mb_itf = NULL;
844 if (mb.mb_otf) {
845 fclose(mb.mb_otf);
846 mb.mb_otf = NULL;
849 initbox(sc.sc_url.url_p_u_h_p);
850 mb.mb_type = MB_VOID;
851 _pop3_lock = 1;
852 mb.mb_sock = sc.sc_sock;
854 saveint = safe_signal(SIGINT, SIG_IGN);
855 savepipe = safe_signal(SIGPIPE, SIG_IGN);
856 if (sigsetjmp(_pop3_jmp, 1)) {
857 sclose(&mb.mb_sock);
858 n_err(_("POP3 connection closed\n"));
859 safe_signal(SIGINT, saveint);
860 safe_signal(SIGPIPE, savepipe);
861 _pop3_lock = 0;
862 rv = -1;
863 if (interrupts > 0)
864 n_raise(SIGINT);
865 goto jleave;
867 if (saveint != SIG_IGN)
868 safe_signal(SIGINT, pop3catch);
869 if (savepipe != SIG_IGN)
870 safe_signal(SIGPIPE, pop3catch);
872 if ((cp = xok_vlook(pop3_keepalive, &sc.sc_url, OXM_ALL)) != NULL) {
873 if ((_pop3_keepalive = (int)strtol(cp, NULL, 10)) > 0) {
874 _pop3_savealrm = safe_signal(SIGALRM, pop3alarm);
875 alarm(_pop3_keepalive);
879 mb.mb_sock.s_desc = sc.sc_url.url_needs_tls ? "POP3S" : "POP3";
880 mb.mb_sock.s_onclose = pop3_timer_off;
881 if (_pop3_login(&mb, &sc) != OKAY ||
882 pop3_stat(&mb, &mailsize, &msgCount) != OKAY) {
883 sclose(&mb.mb_sock);
884 pop3_timer_off();
885 safe_signal(SIGINT, saveint);
886 safe_signal(SIGPIPE, savepipe);
887 _pop3_lock = 0;
888 goto jleave;
891 setmsize(msgCount);
892 mb.mb_type = MB_POP3;
893 mb.mb_perm = ((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY)) ? 0 : MB_DELE;
894 pop3_setptr(&mb, &sc);
896 /*if (!(fm & FEDIT_NEWMAIL)) */{
897 pstate &= ~PS_SAW_COMMAND;
898 pstate |= PS_SETFILE_OPENED;
901 safe_signal(SIGINT, saveint);
902 safe_signal(SIGPIPE, savepipe);
903 _pop3_lock = 0;
905 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
906 rv = (msgCount == 0);
907 goto jleave;
910 if (!(pstate & PS_EDIT) && msgCount == 0) {
911 if (!ok_blook(emptystart))
912 n_err(_("No mail at %s\n"), server);
913 goto jleave;
916 rv = 0;
917 jleave:
918 NYD_LEAVE;
919 return rv;
922 FL enum okay
923 pop3_header(struct message *m)
925 enum okay rv;
926 NYD_ENTER;
928 /* TODO no URL here, no OXM possible; (however it is used in setfile()..) */
929 rv = pop3_get(&mb, m, (ok_blook(pop3_bulk_load) ? NEED_BODY : NEED_HEADER));
930 NYD_LEAVE;
931 return rv;
934 FL enum okay
935 pop3_body(struct message *m)
937 enum okay rv;
938 NYD_ENTER;
940 rv = pop3_get(&mb, m, NEED_BODY);
941 NYD_LEAVE;
942 return rv;
945 FL bool_t
946 pop3_quit(void)
948 sighandler_type volatile saveint, savepipe;
949 bool_t rv;
950 NYD_ENTER;
952 rv = FAL0;
954 if (mb.mb_sock.s_fd < 0) {
955 n_err(_("POP3 connection already closed\n"));
956 rv = TRU1;
957 goto jleave;
960 _pop3_lock = 1;
961 saveint = safe_signal(SIGINT, SIG_IGN);
962 savepipe = safe_signal(SIGPIPE, SIG_IGN);
963 if (sigsetjmp(_pop3_jmp, 1)) {
964 safe_signal(SIGINT, saveint);
965 safe_signal(SIGPIPE, savepipe);
966 _pop3_lock = 0;
967 interrupts = 0;
968 goto jleave;
970 if (saveint != SIG_IGN)
971 safe_signal(SIGINT, pop3catch);
972 if (savepipe != SIG_IGN)
973 safe_signal(SIGPIPE, pop3catch);
974 pop3_update(&mb);
975 pop3_exit(&mb);
976 sclose(&mb.mb_sock);
977 safe_signal(SIGINT, saveint);
978 safe_signal(SIGPIPE, savepipe);
979 _pop3_lock = 0;
981 rv = TRU1;
982 jleave:
983 NYD_LEAVE;
984 return rv;
986 #endif /* HAVE_POP3 */
988 /* s-it-mode */