1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ IMAP v4r1 client following RFC 2060.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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
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
40 #define n_FILE obs_imap
42 #ifndef HAVE_AMALGAMATION
47 # include <sys/socket.h>
51 # include <netinet/in.h>
53 # ifdef HAVE_ARPA_INET_H
54 # include <arpa/inet.h>
60 #define IMAP_ANSWER() \
62 if (mp->mb_type != MB_CACHE) {\
64 while (mp->mb_active & MB_COMD)\
65 ok = imap_answer(mp, 1);\
71 /* TODO IMAP_OUT() simply returns instead of doing "actioN" if imap_finish()
72 * TODO fails, which leaves behind leaks in, e.g., imap_append1()!
73 * TODO IMAP_XOUT() was added due to this, but (1) needs to be used everywhere
74 * TODO and (2) doesn't handle all I/O errors itself, yet, too.
75 * TODO I.e., that should be a function, not a macro ... or so.
76 * TODO This entire module needs MASSIVE work! */
77 #define IMAP_OUT(X,Y,ACTION) IMAP_XOUT(X, Y, ACTION, return STOP)
78 #define IMAP_XOUT(X,Y,ACTIONERR,ACTIONBAIL) \
80 if (mp->mb_type != MB_CACHE) {\
81 if (imap_finish(mp) == STOP) {\
84 if (n_poption & n_PO_D_VV)\
87 if (swrite(&mp->mb_sock, X) == STOP) {\
96 static struct record
{
97 struct record
*rec_next
;
98 unsigned long rec_count
;
123 static char *responded_tag
;
124 static char *responded_text
;
125 static char *responded_other_text
;
126 static long responded_other_number
;
132 MAILBOX_DATA_MAILBOX
,
137 MESSAGE_DATA_EXPUNGE
,
140 RESPONSE_OTHER_UNKNOWN
143 static enum list_attributes
{
145 LIST_NOINFERIORS
= 001,
151 static int list_hierarchy_delimiter
;
152 static char *list_name
;
155 struct list_item
*l_next
;
158 enum list_attributes l_attr
;
164 static char *imapbuf
; /* TODO not static, use pool */
165 static size_t imapbufsize
;
166 static sigjmp_buf imapjmp
;
167 static sighandler_type savealrm
;
168 static int imapkeepalive
;
169 static long had_exists
= -1;
170 static long had_expunge
= -1;
171 static long expunged_messages
;
172 static int volatile imaplock
;
173 static int same_imap_account
;
174 static bool_t _imap_rdonly
;
176 static char *imap_quotestr(char const *s
);
177 static char *imap_unquotestr(char const *s
);
178 static void imap_delim_init(struct mailbox
*mp
, struct url
const *urlp
);
179 static char const *imap_path_normalize(struct mailbox
*mp
, char const *cp
);
180 /* Returns NULL on error */
181 static char *imap_path_quote(struct mailbox
*mp
, char const *cp
);
182 static void imap_other_get(char *pp
);
183 static void imap_response_get(const char **cp
);
184 static void imap_response_parse(void);
185 static enum okay
imap_answer(struct mailbox
*mp
, int errprnt
);
186 static enum okay
imap_parse_list(void);
187 static enum okay
imap_finish(struct mailbox
*mp
);
188 static void imap_timer_off(void);
189 static void imapcatch(int s
);
190 static void _imap_maincatch(int s
);
191 static enum okay
imap_noop1(struct mailbox
*mp
);
192 static void rec_queue(enum rec_type type
, unsigned long cnt
);
193 static enum okay
rec_dequeue(void);
194 static void rec_rmqueue(void);
195 static void imapalarm(int s
);
196 static enum okay
imap_preauth(struct mailbox
*mp
, struct url
const *urlp
);
197 static enum okay
imap_capability(struct mailbox
*mp
);
198 static enum okay
imap_auth(struct mailbox
*mp
, struct ccred
*ccred
);
200 static enum okay
imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
);
202 static enum okay
imap_login(struct mailbox
*mp
, struct ccred
*ccred
);
204 static enum okay
_imap_gssapi(struct mailbox
*mp
, struct ccred
*ccred
);
206 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
207 static void imap_init(struct mailbox
*mp
, int n
);
208 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
210 static bool_t
_imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
,
212 static int _imap_setfile1(struct url
*urlp
, enum fedit_mode fm
,
214 static int imap_fetchdata(struct mailbox
*mp
, struct message
*m
,
215 size_t expected
, int need
, const char *head
,
216 size_t headsize
, long headlines
);
217 static void imap_putstr(struct mailbox
*mp
, struct message
*m
,
218 const char *str
, const char *head
, size_t headsize
,
220 static enum okay
imap_get(struct mailbox
*mp
, struct message
*m
,
222 static void commitmsg(struct mailbox
*mp
, struct message
*to
,
223 struct message
*from
, enum content_info content_info
);
224 static enum okay
imap_fetchheaders(struct mailbox
*mp
, struct message
*m
,
226 static enum okay
imap_exit(struct mailbox
*mp
);
227 static enum okay
imap_delete(struct mailbox
*mp
, int n
, struct message
*m
,
229 static enum okay
imap_close(struct mailbox
*mp
);
230 static enum okay
imap_update(struct mailbox
*mp
);
231 static enum okay
imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
232 int c
, const char *sp
, int needstat
);
233 static enum okay
imap_unstore(struct message
*m
, int n
, const char *flag
);
234 static const char *tag(int new);
235 static char * imap_putflags(int f
);
236 static void imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
);
237 static enum okay
imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
238 off_t off1
, long xsize
, enum mflag flag
, time_t t
);
239 static enum okay
imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
,
241 static enum okay
imap_list1(struct mailbox
*mp
, const char *base
,
242 struct list_item
**list
, struct list_item
**lend
,
244 static enum okay
imap_list(struct mailbox
*mp
, const char *base
, int strip
,
246 static enum okay
imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
,
248 static enum okay
imap_copyuid_parse(const char *cp
,
249 ui64_t
*uidvalidity
, ui64_t
*olduid
, ui64_t
*newuid
);
250 static enum okay
imap_appenduid_parse(const char *cp
,
251 ui64_t
*uidvalidity
, ui64_t
*uid
);
252 static enum okay
imap_copyuid(struct mailbox
*mp
, struct message
*m
,
254 static enum okay
imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
,
255 long off1
, long xsize
, long size
, long lines
, int flag
,
257 static enum okay
imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
);
258 #ifdef HAVE_IMAP_SEARCH
259 static enum okay
imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
260 const char *spec
, int f
);
262 static enum okay
imap_remove1(struct mailbox
*mp
, const char *name
);
263 static enum okay
imap_rename1(struct mailbox
*mp
, const char *old
,
265 static char * imap_strex(char const *cp
, char const **xp
);
266 static enum okay
check_expunged(void);
269 imap_quotestr(char const *s
)
274 np
= n
= salloc(2 * strlen(s
) + 3);
277 if (*s
== '"' || *s
== '\\')
288 imap_unquotestr(char const *s
)
298 np
= n
= salloc(strlen(s
) + 1);
313 imap_delim_init(struct mailbox
*mp
, struct url
const *urlp
){
318 mp
->mb_imap_delim
[0] = '\0';
320 if((cp
= xok_vlook(imap_delim
, urlp
, OXM_ALL
)) != NULL
){
325 i
= sizeof(n_IMAP_DELIM
) -1;
329 if(i
< n_NELEM(mp
->mb_imap_delim
))
331 memcpy(&mb
.mb_imap_delim
[0], cp
, i
+1);
333 n_err(_("*imap-delim* for %s is too long: %s\n"),
334 urlp
->url_input
, cp
);
340 imap_path_normalize(struct mailbox
*mp
, char const *cp
){
341 char *rv_base
, *rv
, dc2
, dc
, c
, lc
;
345 /* Unless we operate in free fly, honour a non-set *imap-delim* to mean "use
346 * exactly what i have specified" */
347 if(mp
== NULL
|| mp
->mb_imap_delim
[0] == '\0')
348 dcp
= &n_IMAP_DELIM
[0];
350 dcp
= &mp
->mb_imap_delim
[0];
351 dc2
= ((dc
= *dcp
) != '\0') ? *++dcp
: dc
;
353 /* Plain names don't need path quoting */
358 for(cpx
= cp
;; ++cpx
)
359 if((c
= *cpx
) == '\0')
362 if(strchr(n_IMAP_DELIM
, c
)){
368 else if(dc2
&& strchr(dcp
, c
) != NULL
)
371 /* And we don't need to reevaluate what we have seen yet */
372 i
= PTR2SIZE(cpx
- cp
);
373 rv
= rv_base
= salloc(i
+ (j
= strlen(cpx
) +1));
376 memcpy(&rv
[i
], cpx
, j
);
381 /* Squeeze adjacent delimiters, convert remain to dc */
382 for(lc
= '\0'; (c
= *cp
++) != '\0'; lc
= c
){
383 if(c
== dc
|| (lc
!= '\0' && dc2
&& strchr(dcp
, c
) != NULL
))
385 if(c
!= dc
|| lc
!= dc
)
397 imap_path_encode(char const *cp
, bool_t
*err_or_null
){
398 /* To a large extend inspired by dovecot(1) */
401 ui8_t
*be16p_base
, *be16p
;
407 if(err_or_null
== NULL
)
408 err_or_null
= &err_def
;
411 /* Is this a string that works out as "plain US-ASCII"? */
413 if((c
= cp
[l
]) == '\0')
415 else if(c
<= 0x1F || c
>= 0x7F || c
== '&')
420 /* We need to encode in mUTF-7! For that, we first have to convert the
421 * local charset to UTF-8, then convert all characters which need to be
422 * encoded (except plain "&") to UTF-16BE first, then that to mUTF-7.
423 * We can skip the UTF-8 conversion occasionally, however */
424 #if (defined HAVE_DEVEL && defined HAVE_ICONV) ||\
425 !defined HAVE_ALWAYS_UNICODE_LOCALE
426 if(!(n_psonce
& n_PSO_UNICODE
)){
429 emsg
= N_("iconv(3) from locale charset to UTF-8 failed");
430 if((x
= n_iconv_onetime_cp(n_ICONV_NONE
, "utf-8", ok_vlook(ttycharset
),
435 /* So: Why not start all over again?
436 * Is this a string that works out as "plain US-ASCII"? */
438 if((c
= cp
[l
]) == '\0')
440 else if(c
<= 0x1F || c
>= 0x7F || c
== '&')
445 /* We need to encode, save what we have, encode the rest */
448 for(cp
+= l
, l
= 0; cp
[l
] != '\0'; ++l
)
450 be16p_base
= salloc((l
<< 1) +1); /* XXX use n_string, resize */
452 out
.s
= salloc(l_plain
+ (l
<< 2) +1); /* XXX use n_string, resize */
454 memcpy(out
.s
, &cp
[-l_plain
], out
.l
= l_plain
);
457 DBG( l_plain
+= (l
<< 2); )
464 out
.s
[out
.l
+ 0] = '&';
465 out
.s
[out
.l
+ 1] = '-';
467 }else if(c
> 0x1F && c
< 0x7F)
470 static char const mb64ct
[] =
471 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
474 /* Convert consecutive non-representables */
475 emsg
= N_("Invalid UTF-8 sequence, cannot convert to UTF-32");
477 for(be16p
= be16p_base
, --cp
, ++l
;;){
478 if((utf32
= n_utf8_to_utf32(&cp
, &l
)) == UI32_MAX
)
481 /* TODO S-CText: magic utf16 conversions */
483 be16p
[1] = utf32
& 0xFF;
484 be16p
[0] = (utf32
>>= 8, utf32
&= 0xFF);
490 s7e
= 0xD800u
| (utf32
>> 10);
491 be16p
[1] = s7e
& 0xFF;
492 be16p
[0] = (s7e
>>= 8, s7e
&= 0xFF);
493 s7e
= 0xDC00u
| (utf32
&= 0x03FF);
494 be16p
[3] = s7e
& 0xFF;
495 be16p
[2] = (s7e
>>= 8, s7e
&= 0xFF);
501 if((c
= *cp
) > 0x1F && c
< 0x7F)
505 /* And then warp that UTF-16BE to mUTF-7 */
506 out
.s
[out
.l
++] = '&';
507 utf32
= (ui32_t
)PTR2SIZE(be16p
- be16p_base
);
510 for(; utf32
>= 3; be16p
+= 3, utf32
-= 3){
511 out
.s
[out
.l
+0] = mb64ct
[ be16p
[0] >> 2 ];
512 out
.s
[out
.l
+1] = mb64ct
[((be16p
[0] & 0x03) << 4) | (be16p
[1] >> 4)];
513 out
.s
[out
.l
+2] = mb64ct
[((be16p
[1] & 0x0F) << 2) | (be16p
[2] >> 6)];
514 out
.s
[out
.l
+3] = mb64ct
[ be16p
[2] & 0x3F];
518 out
.s
[out
.l
+ 0] = mb64ct
[be16p
[0] >> 2];
520 out
.s
[out
.l
+ 1] = mb64ct
[ (be16p
[0] & 0x03) << 4];
523 out
.s
[out
.l
+ 1] = mb64ct
[((be16p
[0] & 0x03) << 4) |
525 out
.s
[out
.l
+ 2] = mb64ct
[ (be16p
[1] & 0x0F) << 2];
529 out
.s
[out
.l
++] = '-';
533 assert(out
.l
<= l_plain
);
540 n_err(_("Cannot encode IMAP path %s\n %s\n"), cp
, V_(emsg
));
545 imap_path_decode(char const *path
, bool_t
*err_or_null
){
546 /* To a large extend inspired by dovecot(1) TODO use string */
548 ui8_t
*mb64p_base
, *mb64p
, *mb64xp
;
549 char const *emsg
, *cp
;
550 char *rv_base
, *rv
, c
;
554 if(err_or_null
== NULL
)
555 err_or_null
= &err_def
;
558 l
= l_orig
= strlen(path
);
559 rv
= rv_base
= salloc(l
<< 1);
560 memcpy(rv
, path
, l
+1);
562 /* xxx Don't check for invalid characters from malicious servers */
563 if(l
== 0 || (cp
= memchr(path
, '&', l
)) == NULL
)
568 emsg
= N_("Invalid mUTF-7 encoding");
569 i
= PTR2SIZE(cp
- path
);
575 if((c
= *cp
) != '&'){
576 if(c
<= 0x1F || c
>= 0x7F){
577 emsg
= N_("Invalid mUTF-7: unencoded control or 8-bit byte");
585 else if(*++cp
== '-'){
591 emsg
= N_("Invalid mUTF-7: incomplete input");
594 /* mUTF-7 -> UTF-16BE -> UTF-8 */
595 static ui8_t
const mb64dt
[256] = {
598 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
599 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
600 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,62, 63,XX
,XX
,XX
,
601 52,53,54,55, 56,57,58,59, 60,61,XX
,XX
, XX
,XX
,XX
,XX
,
602 XX
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
603 15,16,17,18, 19,20,21,22, 23,24,25,XX
, XX
,XX
,XX
,XX
,
604 XX
,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
605 41,42,43,44, 45,46,47,48, 49,50,51,XX
, XX
,XX
,XX
,XX
,
606 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
607 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
608 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
609 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
610 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
611 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
612 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
613 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
616 if(mb64p_base
== NULL
)
617 mb64p_base
= salloc(l
);
619 /* Decode the mUTF-7 to what is indeed UTF-16BE */
620 for(mb64p
= mb64p_base
;;){
622 if((mb64p
[0] = mb64dt
[(ui8_t
)cp
[0]]) == XX
||
623 (mb64p
[1] = mb64dt
[(ui8_t
)cp
[1]]) == XX
)
632 if((*mb64p
++ = mb64dt
[(ui8_t
)c
]) == XX
)
638 if((c
= *cp
++) == '-')
640 if((*mb64p
++ = mb64dt
[(ui8_t
)c
]) == XX
)
644 if(l
> 0 && *cp
== '-'){
654 if(l
>= 2 && cp
[0] == '&' && cp
[1] != '-'){
655 emsg
= N_("Invalid mUTF-7, consecutive encoded sequences");
659 /* Yet halfway decoded mUTF-7, go remaining way to gain UTF-16BE */
660 i
= PTR2SIZE(mb64p
- mb64p_base
);
661 mb64p
= mb64xp
= mb64p_base
;
664 ui8_t ul
, u0
, u1
, u2
, u3
;
666 ul
= (i
>= 4) ? 4 : i
& 0x3;
670 u2
= (ul
< 3) ? 0 : mb64xp
[2];
671 u3
= (ul
< 4) ? 0 : mb64xp
[3];
673 *mb64p
++ = (u0
<<= 2) | (u1
>> 4);
676 *mb64p
++ = (u1
<<= 4) | (u2
>> 2);
679 *mb64p
++ = (u2
<<= 6, u2
&= 0xC0) | u3
;
682 /* UTF-16BE we convert to UTF-8 */
683 i
= PTR2SIZE(mb64p
- mb64p_base
);
685 emsg
= N_("Odd bytecount for UTF-16BE input");
689 /* TODO S-CText: magic utf16 conversions */
690 emsg
= N_("Invalid UTF-16BE encoding");
692 for(mb64p
= mb64p_base
; i
> 0;){
700 /* Not a surrogate? */
701 if(uhi
< 0xD800 || uhi
> 0xDFFF){
705 }else if(uhi
> 0xDBFF)
708 emsg
= N_("Incomplete UTF-16BE surrogate pair");
714 if(ulo
< 0xDC00 || ulo
> 0xDFFF)
717 utf32
= (uhi
&= 0x03FF);
720 utf32
|= (ulo
&= 0x03FF);
725 utf32
= n_utf32_to_utf8(utf32
, rv
);
732 /* We can skip the UTF-8 conversion occasionally */
733 #if (defined HAVE_DEVEL && defined HAVE_ICONV) ||\
734 !defined HAVE_ALWAYS_UNICODE_LOCALE
735 if(!(n_psonce
& n_PSO_UNICODE
)){
736 emsg
= N_("iconv(3) from UTF-8 to locale charset failed");
737 if((rv
= n_iconv_onetime_cp(n_ICONV_NONE
, NULL
, NULL
, rv_base
)) == NULL
)
748 n_err(_("Cannot decode IMAP path %s\n %s\n"), path
, V_(emsg
));
749 memcpy(rv
= rv_base
, path
, ++l_orig
);
754 imap_path_quote(struct mailbox
*mp
, char const *cp
){
759 cp
= imap_path_normalize(mp
, cp
);
760 cp
= imap_path_encode(cp
, &err
);
761 rv
= err
? NULL
: imap_quotestr(cp
);
767 imap_other_get(char *pp
)
772 if (ascncasecmp(pp
, "FLAGS ", 6) == 0) {
774 response_other
= MAILBOX_DATA_FLAGS
;
775 } else if (ascncasecmp(pp
, "LIST ", 5) == 0) {
777 response_other
= MAILBOX_DATA_LIST
;
778 } else if (ascncasecmp(pp
, "LSUB ", 5) == 0) {
780 response_other
= MAILBOX_DATA_LSUB
;
781 } else if (ascncasecmp(pp
, "MAILBOX ", 8) == 0) {
783 response_other
= MAILBOX_DATA_MAILBOX
;
784 } else if (ascncasecmp(pp
, "SEARCH ", 7) == 0) {
786 response_other
= MAILBOX_DATA_SEARCH
;
787 } else if (ascncasecmp(pp
, "STATUS ", 7) == 0) {
789 response_other
= MAILBOX_DATA_STATUS
;
790 } else if (ascncasecmp(pp
, "CAPABILITY ", 11) == 0) {
792 response_other
= CAPABILITY_DATA
;
794 responded_other_number
= strtol(pp
, &xp
, 10);
797 if (ascncasecmp(xp
, "EXISTS\r\n", 8) == 0) {
798 response_other
= MAILBOX_DATA_EXISTS
;
799 } else if (ascncasecmp(xp
, "RECENT\r\n", 8) == 0) {
800 response_other
= MAILBOX_DATA_RECENT
;
801 } else if (ascncasecmp(xp
, "EXPUNGE\r\n", 9) == 0) {
802 response_other
= MESSAGE_DATA_EXPUNGE
;
803 } else if (ascncasecmp(xp
, "FETCH ", 6) == 0) {
805 response_other
= MESSAGE_DATA_FETCH
;
807 response_other
= RESPONSE_OTHER_UNKNOWN
;
809 responded_other_text
= pp
;
814 imap_response_get(const char **cp
)
817 if (ascncasecmp(*cp
, "OK ", 3) == 0) {
819 response_status
= RESPONSE_OK
;
820 } else if (ascncasecmp(*cp
, "NO ", 3) == 0) {
822 response_status
= RESPONSE_NO
;
823 } else if (ascncasecmp(*cp
, "BAD ", 4) == 0) {
825 response_status
= RESPONSE_BAD
;
826 } else if (ascncasecmp(*cp
, "PREAUTH ", 8) == 0) {
828 response_status
= RESPONSE_PREAUTH
;
829 } else if (ascncasecmp(*cp
, "BYE ", 4) == 0) {
831 response_status
= RESPONSE_BYE
;
833 response_status
= RESPONSE_OTHER
;
838 imap_response_parse(void)
840 static char *parsebuf
; /* TODO Use pool */
841 static size_t parsebufsize
;
843 const char *ip
= imapbuf
;
847 if (parsebufsize
< imapbufsize
+ 1)
848 parsebuf
= srealloc(parsebuf
, parsebufsize
= imapbufsize
);
849 memcpy(parsebuf
, imapbuf
, strlen(imapbuf
) + 1);
853 response_type
= RESPONSE_CONT
;
868 imap_response_get(&ip
);
869 pp
= &parsebuf
[ip
- imapbuf
];
870 switch (response_status
) {
872 response_type
= RESPONSE_FATAL
;
875 response_type
= RESPONSE_DATA
;
879 responded_tag
= parsebuf
;
880 while (*pp
&& *pp
!= ' ')
883 response_type
= RESPONSE_ILLEGAL
;
887 while (*pp
&& *pp
== ' ')
890 response_type
= RESPONSE_ILLEGAL
;
893 ip
= &imapbuf
[pp
- parsebuf
];
894 response_type
= RESPONSE_TAGGED
;
895 imap_response_get(&ip
);
896 pp
= &parsebuf
[ip
- imapbuf
];
899 if (response_type
!= RESPONSE_CONT
&& response_type
!= RESPONSE_ILLEGAL
&&
900 response_status
== RESPONSE_OTHER
)
906 imap_answer(struct mailbox
*mp
, int errprnt
)
913 if (mp
->mb_type
== MB_CACHE
)
917 if (sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
918 if (n_poption
& n_PO_D_VV
)
919 n_err(">>> SERVER: %s", imapbuf
);
920 imap_response_parse();
921 if (response_type
== RESPONSE_ILLEGAL
)
923 if (response_type
== RESPONSE_CONT
) {
927 if (response_status
== RESPONSE_OTHER
) {
928 if (response_other
== MAILBOX_DATA_EXISTS
) {
929 had_exists
= responded_other_number
;
930 rec_queue(REC_EXISTS
, responded_other_number
);
933 } else if (response_other
== MESSAGE_DATA_EXPUNGE
) {
934 rec_queue(REC_EXPUNGE
, responded_other_number
);
942 if (response_type
== RESPONSE_TAGGED
) {
943 if (asccasecmp(responded_tag
, tag(0)) == 0)
948 switch (response_status
) {
949 case RESPONSE_PREAUTH
:
950 mp
->mb_active
&= ~MB_PREAUTH
;
962 n_err(_("IMAP error: %s"), responded_text
);
964 case RESPONSE_UNKNOWN
: /* does not happen */
967 mp
->mb_active
= MB_NONE
;
975 if (response_status
!= RESPONSE_OTHER
&&
976 ascncasecmp(responded_text
, "[ALERT] ", 8) == 0)
977 n_err(_("IMAP alert: %s"), &responded_text
[8]);
979 mp
->mb_active
&= ~MB_COMD
;
981 mp
->mb_active
= MB_NONE
;
988 imap_parse_list(void)
996 cp
= responded_other_text
;
997 list_attributes
= LIST_NONE
;
999 while (*cp
&& *cp
!= ')') {
1001 if (ascncasecmp(&cp
[1], "Noinferiors ", 12) == 0) {
1002 list_attributes
|= LIST_NOINFERIORS
;
1004 } else if (ascncasecmp(&cp
[1], "Noselect ", 9) == 0) {
1005 list_attributes
|= LIST_NOSELECT
;
1007 } else if (ascncasecmp(&cp
[1], "Marked ", 7) == 0) {
1008 list_attributes
|= LIST_MARKED
;
1010 } else if (ascncasecmp(&cp
[1], "Unmarked ", 9) == 0) {
1011 list_attributes
|= LIST_UNMARKED
;
1023 list_hierarchy_delimiter
= EOF
;
1027 list_hierarchy_delimiter
= *cp
++ & 0377;
1028 if (cp
[0] != '"' || cp
[1] != ' ')
1031 } else if (cp
[0] == 'N' && cp
[1] == 'I' && cp
[2] == 'L' && cp
[3] == ' ') {
1032 list_hierarchy_delimiter
= EOF
;
1039 while (*cp
&& *cp
!= '\r')
1049 imap_finish(struct mailbox
*mp
)
1052 while (mp
->mb_sock
.s_fd
> 0 && mp
->mb_active
& MB_COMD
)
1059 imap_timer_off(void)
1062 if (imapkeepalive
> 0) {
1064 safe_signal(SIGALRM
, savealrm
);
1072 NYD_X
; /* Signal handler */
1075 n_err_sighdl(_("Interrupt\n"));
1076 siglongjmp(imapjmp
, 1);
1079 n_err_sighdl(_("Received SIGPIPE during IMAP operation\n"));
1085 _imap_maincatch(int s
)
1087 NYD_X
; /* Signal handler */
1089 if (interrupts
++ == 0) {
1090 n_err_sighdl(_("Interrupt\n"));
1093 n_go_onintr_for_imap();
1097 imap_noop1(struct mailbox
*mp
)
1100 FILE *queuefp
= NULL
;
1103 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
1104 IMAP_OUT(o
, MB_COMD
, return STOP
)
1110 imap_fileof(char const *xcp
)
1112 char const *cp
= xcp
;
1117 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
1121 if (cp
[0] == '/' && state
== 1) {
1139 sighandler_type
volatile oldint
, oldpipe
;
1140 enum okay
volatile rv
= STOP
;
1143 if (mb
.mb_type
!= MB_IMAP
)
1147 if ((oldint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1148 safe_signal(SIGINT
, &_imap_maincatch
);
1149 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1150 if (sigsetjmp(imapjmp
, 1) == 0) {
1151 if (oldpipe
!= SIG_IGN
)
1152 safe_signal(SIGPIPE
, imapcatch
);
1154 rv
= imap_noop1(&mb
);
1156 safe_signal(SIGINT
, oldint
);
1157 safe_signal(SIGPIPE
, oldpipe
);
1162 n_go_onintr_for_imap();
1167 rec_queue(enum rec_type rt
, unsigned long cnt
)
1172 rp
= scalloc(1, sizeof *rp
);
1174 rp
->rec_count
= cnt
;
1175 if (record
&& recend
) {
1176 recend
->rec_next
= rp
;
1179 record
= recend
= rp
;
1186 struct message
*omessage
;
1187 struct record
*rp
, *rq
;
1188 uiz_t exists
= 0, i
;
1189 enum okay rv
= STOP
;
1196 message
= smalloc((msgCount
+1) * sizeof *message
);
1198 memcpy(message
, omessage
, msgCount
* sizeof *message
);
1199 memset(&message
[msgCount
], 0, sizeof *message
);
1201 rp
= record
, rq
= NULL
;
1203 while (rp
!= NULL
) {
1204 switch (rp
->rec_type
) {
1206 exists
= rp
->rec_count
;
1209 if (rp
->rec_count
== 0) {
1213 if (rp
->rec_count
> (unsigned long)msgCount
) {
1214 if (exists
== 0 || rp
->rec_count
> exists
--)
1220 delcache(&mb
, &message
[rp
->rec_count
-1]);
1221 memmove(&message
[rp
->rec_count
-1], &message
[rp
->rec_count
],
1222 ((msgCount
- rp
->rec_count
+ 1) * sizeof *message
));
1224 /* If the message was part of a collapsed thread,
1225 * the m_collapsed field of one of its ancestors
1226 * should be incremented. It seems hardly possible
1227 * to do this with the current message structure,
1228 * though. The result is that a '+' may be shown
1229 * in the header summary even if no collapsed
1230 * children exists */
1241 record
= recend
= NULL
;
1242 if (rv
== OKAY
&& UICMP(z
, exists
, >, msgCount
)) {
1243 message
= srealloc(message
, (exists
+ 1) * sizeof *message
);
1244 memset(&message
[msgCount
], 0, (exists
- msgCount
+ 1) * sizeof *message
);
1245 for (i
= msgCount
; i
< exists
; ++i
)
1247 imap_flags(&mb
, msgCount
+1, exists
);
1266 for (rp
= record
; rp
!= NULL
;) {
1267 struct record
*tmp
= rp
;
1271 record
= recend
= NULL
;
1279 sighandler_type
volatile saveint
, savepipe
;
1280 NYD_X
; /* Signal handler */
1283 if (imaplock
++ == 0) {
1284 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1285 safe_signal(SIGINT
, &_imap_maincatch
);
1286 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1287 if (sigsetjmp(imapjmp
, 1)) {
1288 safe_signal(SIGINT
, saveint
);
1289 safe_signal(SIGPIPE
, savepipe
);
1292 if (savepipe
!= SIG_IGN
)
1293 safe_signal(SIGPIPE
, imapcatch
);
1294 if (imap_noop1(&mb
) != OKAY
) {
1295 safe_signal(SIGINT
, saveint
);
1296 safe_signal(SIGPIPE
, savepipe
);
1299 safe_signal(SIGINT
, saveint
);
1300 safe_signal(SIGPIPE
, savepipe
);
1303 alarm(imapkeepalive
);
1309 imap_preauth(struct mailbox
*mp
, struct url
const *urlp
)
1313 mp
->mb_active
|= MB_PREAUTH
;
1317 if (!mp
->mb_sock
.s_use_ssl
&& xok_blook(imap_use_starttls
, urlp
, OXM_ALL
)) {
1318 FILE *queuefp
= NULL
;
1321 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
1322 IMAP_OUT(o
, MB_COMD
, return STOP
)
1324 if (ssl_open(urlp
, &mp
->mb_sock
) != OKAY
)
1328 if (xok_blook(imap_use_starttls
, urlp
, OXM_ALL
)) {
1329 n_err(_("No SSL support compiled in\n"));
1334 imap_capability(mp
);
1339 imap_capability(struct mailbox
*mp
)
1342 FILE *queuefp
= NULL
;
1343 enum okay ok
= STOP
;
1347 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
1348 IMAP_OUT(o
, MB_COMD
, return STOP
)
1349 while (mp
->mb_active
& MB_COMD
) {
1350 ok
= imap_answer(mp
, 0);
1351 if (response_status
== RESPONSE_OTHER
&&
1352 response_other
== CAPABILITY_DATA
) {
1353 cp
= responded_other_text
;
1355 while (spacechar(*cp
))
1357 if (strncmp(cp
, "UIDPLUS", 7) == 0 && spacechar(cp
[7]))
1359 mp
->mb_flags
|= MB_UIDPLUS
;
1360 while (*cp
&& !spacechar(*cp
))
1369 imap_auth(struct mailbox
*mp
, struct ccred
*ccred
)
1374 if (!(mp
->mb_active
& MB_PREAUTH
)) {
1379 switch (ccred
->cc_authtype
) {
1380 case AUTHTYPE_LOGIN
:
1381 rv
= imap_login(mp
, ccred
);
1384 case AUTHTYPE_CRAM_MD5
:
1385 rv
= imap_cram_md5(mp
, ccred
);
1389 case AUTHTYPE_GSSAPI
:
1390 rv
= _imap_gssapi(mp
, ccred
);
1404 imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
)
1406 char o
[LINESIZE
], *cp
;
1407 FILE *queuefp
= NULL
;
1408 enum okay rv
= STOP
;
1411 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
1412 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
1414 if (response_type
!= RESPONSE_CONT
)
1417 cp
= cram_md5_string(&ccred
->cc_user
, &ccred
->cc_pass
, responded_text
);
1420 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
1421 while (mp
->mb_active
& MB_COMD
)
1422 rv
= imap_answer(mp
, 1);
1427 #endif /* HAVE_MD5 */
1430 imap_login(struct mailbox
*mp
, struct ccred
*ccred
)
1433 FILE *queuefp
= NULL
;
1434 enum okay rv
= STOP
;
1437 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
1438 tag(1), imap_quotestr(ccred
->cc_user
.s
), imap_quotestr(ccred
->cc_pass
.s
));
1439 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
1440 while (mp
->mb_active
& MB_COMD
)
1441 rv
= imap_answer(mp
, 1);
1448 # include "obs-imap-gssapi.h"
1452 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
,
1456 char const *qname
, *cp
;
1465 if((qname
= imap_path_quote(mp
, mbx
)) == NULL
)
1470 mp
->mb_uidvalidity
= 0;
1471 snprintf(o
, sizeof o
, "%s %s %s\r\n", tag(1),
1472 (fm
& FEDIT_RDONLY
? "EXAMINE" : "SELECT"), qname
);
1473 IMAP_OUT(o
, MB_COMD
, ok
= STOP
;goto jleave
)
1474 while (mp
->mb_active
& MB_COMD
) {
1475 ok
= imap_answer(mp
, 1);
1476 if (response_status
!= RESPONSE_OTHER
&&
1477 (cp
= asccasestr(responded_text
, "[UIDVALIDITY ")) != NULL
)
1478 n_idec_ui64_cp(&mp
->mb_uidvalidity
, &cp
[13], 10, NULL
);/* TODO err? */
1480 *cnt
= (had_exists
> 0) ? had_exists
: 0;
1481 if (response_status
!= RESPONSE_OTHER
&&
1482 ascncasecmp(responded_text
, "[READ-ONLY] ", 12) == 0)
1489 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
1492 FILE *queuefp
= NULL
;
1495 unsigned x
= X
, y
= Y
, n
;
1498 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
1499 IMAP_OUT(o
, MB_COMD
, return STOP
)
1500 while (mp
->mb_active
& MB_COMD
) {
1502 if (response_status
== RESPONSE_OTHER
&&
1503 response_other
== MESSAGE_DATA_FETCH
) {
1504 n
= responded_other_number
;
1512 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1517 imap_getflags(cp
, &cp
, &m
->m_flag
);
1520 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1521 n_idec_ui64_cp(&m
->m_uid
, &cp
[4], 10, NULL
);/* TODO errors? */
1522 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1523 m
->m_flag
&= ~MHIDDEN
;
1526 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1528 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1531 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1533 IMAP_OUT(o
, MB_COMD
, return STOP
)
1534 while (mp
->mb_active
& MB_COMD
) {
1536 if (response_status
== RESPONSE_OTHER
&&
1537 response_other
== MESSAGE_DATA_FETCH
) {
1538 n
= responded_other_number
;
1544 if ((cp
= asccasestr(responded_other_text
, "RFC822.SIZE ")) != NULL
)
1545 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1546 if ((cp
= asccasestr(responded_other_text
, "INTERNALDATE ")) != NULL
)
1547 m
->m_time
= imap_read_date_time(&cp
[13]);
1552 for (n
= X
; n
<= Y
; ++n
) {
1553 putcache(mp
, &message
[n
-1]);
1561 imap_init(struct mailbox
*mp
, int n
)
1568 m
->m_flag
= MUSED
| MNOFROM
;
1575 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1577 struct message
*omessage
= 0;
1578 int i
, omsgCount
= 0;
1579 enum okay dequeued
= STOP
;
1582 if (nmail
|| transparent
) {
1584 omsgCount
= msgCount
;
1587 dequeued
= rec_dequeue();
1589 if (had_exists
>= 0) {
1590 if (dequeued
!= OKAY
)
1591 msgCount
= had_exists
;
1594 if (had_expunge
>= 0) {
1595 if (dequeued
!= OKAY
)
1596 msgCount
-= had_expunge
;
1600 if (nmail
&& expunged_messages
)
1601 printf("Expunged %ld message%s.\n", expunged_messages
,
1602 (expunged_messages
!= 1 ? "s" : ""));
1603 *prevcount
= omsgCount
- expunged_messages
;
1604 expunged_messages
= 0;
1606 fputs("IMAP error: Negative message count\n", stderr
);
1610 if (dequeued
!= OKAY
) {
1611 message
= scalloc(msgCount
+ 1, sizeof *message
);
1612 for (i
= 0; i
< msgCount
; i
++)
1614 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1617 imap_flags(mp
, 1, msgCount
);
1618 message
[msgCount
].m_size
= 0;
1619 message
[msgCount
].m_lines
= 0;
1622 if (nmail
|| transparent
)
1623 transflags(omessage
, omsgCount
, transparent
);
1630 imap_setfile(const char *xserver
, enum fedit_mode fm
)
1636 if (!url_parse(&url
, CPROTO_IMAP
, xserver
)) {
1640 if (!ok_blook(v15_compat
) &&
1641 (!(url
.url_flags
& n_URL_HAD_USER
) || url
.url_pass
.s
!= NULL
))
1642 n_err(_("New-style URL used without *v15-compat* being set!\n"));
1644 _imap_rdonly
= ((fm
& FEDIT_RDONLY
) != 0);
1645 rv
= _imap_setfile1(&url
, fm
, 0);
1652 _imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
, struct url
*urlp
)
1657 if (ok_blook(v15_compat
))
1658 rv
= ccred_lookup(ccredp
, urlp
);
1661 *xuhp
= ((urlp
->url_flags
& n_URL_HAD_USER
) ? urlp
->url_eu_h_p
.s
1662 : urlp
->url_u_h_p
.s
);
1664 if ((var
= mbp
->mb_imap_pass
) != NULL
) {
1665 var
= savecat("password-", xuhp
);
1666 if ((old
= n_UNCONST(n_var_vlook(var
, FAL0
))) != NULL
)
1668 n_var_vset(var
, (uintptr_t)mbp
->mb_imap_pass
);
1670 rv
= ccred_lookup_old(ccredp
, CPROTO_IMAP
, xuhp
);
1673 n_var_vset(var
, (uintptr_t)old
);
1685 _imap_setfile1(struct url
*urlp
, enum fedit_mode
volatile fm
,
1686 int volatile transparent
)
1690 sighandler_type
volatile saveint
, savepipe
;
1693 int volatile prevcount
= 0;
1694 enum mbflags same_flags
;
1697 if (fm
& FEDIT_NEWMAIL
) {
1698 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1699 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1700 if (saveint
!= SIG_IGN
)
1701 safe_signal(SIGINT
, imapcatch
);
1702 if (savepipe
!= SIG_IGN
)
1703 safe_signal(SIGPIPE
, imapcatch
);
1708 same_flags
= mb
.mb_flags
;
1709 same_imap_account
= 0;
1710 if (mb
.mb_imap_account
!= NULL
&&
1711 (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)) {
1712 if (mb
.mb_sock
.s_fd
> 0 && mb
.mb_sock
.s_rsz
>= 0 &&
1713 !strcmp(mb
.mb_imap_account
, urlp
->url_p_eu_h_p
) &&
1714 disconnected(mb
.mb_imap_account
) == 0) {
1715 same_imap_account
= 1;
1716 if (urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1719 } else if ((transparent || mb.mb_type == MB_CACHE) &&
1720 !strcmp(mb.mb_imap_account, urlp->url_p_eu_h_p) &&
1721 urlp->url_pass.s == NULL && mb.mb_imap_pass != NULL)
1724 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= savestr(mb
.mb_imap_pass
));
1728 if (!same_imap_account
&& mb
.mb_imap_pass
!= NULL
) {
1729 free(mb
.mb_imap_pass
);
1730 mb
.mb_imap_pass
= NULL
;
1732 if (!_imap_getcred(&mb
, &ccred
, urlp
)) {
1737 memset(&so
, 0, sizeof so
);
1739 if (!same_imap_account
) {
1740 if (!disconnected(urlp
->url_p_eu_h_p
) && !sopen(&so
, urlp
)) {
1753 if (fm
& FEDIT_SYSBOX
)
1754 n_pstate
&= ~n_PS_EDIT
;
1756 n_pstate
|= n_PS_EDIT
;
1757 if (mb
.mb_imap_account
!= NULL
)
1758 free(mb
.mb_imap_account
);
1759 if (mb
.mb_imap_pass
!= NULL
)
1760 free(mb
.mb_imap_pass
);
1761 mb
.mb_imap_account
= sstrdup(urlp
->url_p_eu_h_p
);
1762 /* TODO This is a hack to allow '@boxname'; in the end everything will be an
1763 * TODO object, and mailbox will naturally have an URL and credentials */
1764 mb
.mb_imap_pass
= sbufdup(ccred
.cc_pass
.s
, ccred
.cc_pass
.l
);
1766 if (!same_imap_account
) {
1767 if (mb
.mb_sock
.s_fd
>= 0)
1768 sclose(&mb
.mb_sock
);
1770 same_imap_account
= 0;
1781 if (mb
.mb_imap_mailbox
!= NULL
)
1782 free(mb
.mb_imap_mailbox
);
1783 assert(urlp
->url_path
.s
!= NULL
);
1784 imap_delim_init(&mb
, urlp
);
1785 mb
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&mb
, urlp
->url_path
.s
));
1786 initbox(savecatsep(urlp
->url_p_eu_h_p
,
1787 (mb
.mb_imap_delim
[0] != '\0' ? mb
.mb_imap_delim
[0] : n_IMAP_DELIM
[0]),
1788 mb
.mb_imap_mailbox
));
1790 mb
.mb_type
= MB_VOID
;
1791 mb
.mb_active
= MB_NONE
;
1794 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1795 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1796 if (sigsetjmp(imapjmp
, 1)) {
1797 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1798 sclose(&mb
.mb_sock
);
1799 safe_signal(SIGINT
, saveint
);
1800 safe_signal(SIGPIPE
, savepipe
);
1803 mb
.mb_type
= MB_VOID
;
1804 mb
.mb_active
= MB_NONE
;
1805 rv
= (fm
& (FEDIT_SYSBOX
| FEDIT_NEWMAIL
)) ? 1 : -1;
1808 if (saveint
!= SIG_IGN
)
1809 safe_signal(SIGINT
, imapcatch
);
1810 if (savepipe
!= SIG_IGN
)
1811 safe_signal(SIGPIPE
, imapcatch
);
1813 if (mb
.mb_sock
.s_fd
< 0) {
1814 if (disconnected(mb
.mb_imap_account
)) {
1815 if (cache_setptr(fm
, transparent
) == STOP
)
1816 n_err(_("Mailbox \"%s\" is not cached\n"), urlp
->url_p_eu_h_p_p
);
1819 if ((cp
= xok_vlook(imap_keepalive
, urlp
, OXM_ALL
)) != NULL
) {
1820 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1821 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1822 alarm(imapkeepalive
);
1827 mb
.mb_sock
.s_desc
= "IMAP";
1828 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1829 if (imap_preauth(&mb
, urlp
) != OKAY
|| imap_auth(&mb
, &ccred
) != OKAY
) {
1830 sclose(&mb
.mb_sock
);
1832 safe_signal(SIGINT
, saveint
);
1833 safe_signal(SIGPIPE
, savepipe
);
1835 rv
= (fm
& (FEDIT_SYSBOX
| FEDIT_NEWMAIL
)) ? 1 : -1;
1838 } else /* same account */
1839 mb
.mb_flags
|= same_flags
;
1841 if (n_poption
& n_PO_R_FLAG
)
1843 mb
.mb_perm
= (fm
& FEDIT_RDONLY
) ? 0 : MB_DELE
;
1844 mb
.mb_type
= MB_IMAP
;
1846 assert(urlp
->url_path
.s
!= NULL
);
1847 if (imap_select(&mb
, &mailsize
, &msgCount
, urlp
->url_path
.s
, fm
) != OKAY
) {
1848 /*sclose(&mb.mb_sock);
1850 safe_signal(SIGINT
, saveint
);
1851 safe_signal(SIGPIPE
, savepipe
);
1853 mb
.mb_type
= MB_VOID
;
1854 rv
= (fm
& (FEDIT_SYSBOX
| FEDIT_NEWMAIL
)) ? 1 : -1;
1859 imap_setptr(&mb
, ((fm
& FEDIT_NEWMAIL
) != 0), transparent
,
1860 n_UNVOLATILE(&prevcount
));
1863 safe_signal(SIGINT
, saveint
);
1864 safe_signal(SIGPIPE
, savepipe
);
1867 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_IMAP
)
1868 purgecache(&mb
, message
, msgCount
);
1869 if (((fm
& FEDIT_NEWMAIL
) || transparent
) && mb
.mb_sorted
) {
1874 if (!(fm
& FEDIT_NEWMAIL
) && !transparent
) {
1875 n_pstate
&= ~n_PS_SAW_COMMAND
;
1876 n_pstate
|= n_PS_SETFILE_OPENED
;
1879 if ((n_poption
& n_PO_EXISTONLY
) && (mb
.mb_type
== MB_IMAP
||
1880 mb
.mb_type
== MB_CACHE
)) {
1881 rv
= (msgCount
== 0);
1885 if (!(fm
& FEDIT_NEWMAIL
) && !(n_pstate
& n_PS_EDIT
) && msgCount
== 0) {
1886 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1887 !ok_blook(emptystart
))
1888 n_err(_("No mail at %s\n"), urlp
->url_p_eu_h_p_p
);
1893 if (fm
& FEDIT_NEWMAIL
)
1894 newmailinfo(prevcount
);
1902 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1903 int need
, const char *head
, size_t headsize
, long headlines
)
1905 char *line
= NULL
, *lp
;
1906 size_t linesize
= 0, linelen
, size
= 0;
1907 int emptyline
= 0, lines
= 0, excess
= 0;
1911 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1912 offset
= ftell(mp
->mb_otf
);
1915 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1917 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1919 if (linelen
> expected
) {
1920 excess
= linelen
- expected
;
1924 * Need to mask 'From ' lines. This cannot be done properly
1925 * since some servers pass them as 'From ' and others as
1926 * '>From '. Although one could identify the first kind of
1927 * server in principle, it is not possible to identify the
1928 * second as '>From ' may also come from a server of the
1929 * first type as actual data. So do what is absolutely
1930 * necessary only - mask 'From '.
1932 * If the line is the first line of the message header, it
1933 * is likely a real 'From ' line. In this case, it is just
1934 * ignored since it violates all standards.
1935 * TODO can the latter *really* happen??
1938 /* Since we simply copy over data without doing any transfer
1939 * encoding reclassification/adjustment we *have* to perform
1940 * RFC 4155 compliant From_ quoting here */
1941 if (emptyline
&& is_head(lp
, linelen
, FAL0
)) {
1942 fputc('>', mp
->mb_otf
);
1946 if (lp
[linelen
-1] == '\n' && (linelen
== 1 || lp
[linelen
-2] == '\r')) {
1948 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1949 size
+= linelen
- 1;
1954 fputc('\n', mp
->mb_otf
);
1956 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1960 if ((expected
-= linelen
) <= 0)
1964 /* TODO This is very ugly; but some IMAP daemons don't end a
1965 * TODO message with \r\n\r\n, and we need \n\n for mbox format.
1966 * TODO That is to say we do it wrong here in order to get it right
1967 * TODO when send.c stuff or with MBOX handling, even though THIS
1968 * TODO line is solely a property of the MBOX database format! */
1969 fputc('\n', mp
->mb_otf
);
1976 m
->m_size
= size
+ headsize
;
1977 m
->m_lines
= lines
+ headlines
;
1978 m
->m_block
= mailx_blockof(offset
);
1979 m
->m_offset
= mailx_offsetof(offset
);
1982 m
->m_content_info
= CI_HAVE_HEADER
;
1985 m
->m_content_info
= CI_HAVE_HEADER
| CI_HAVE_BODY
;
1986 m
->m_xlines
= m
->m_lines
;
1987 m
->m_xsize
= m
->m_size
;
1997 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1998 const char *head
, size_t headsize
, long headlines
)
2005 fseek(mp
->mb_otf
, 0L, SEEK_END
);
2006 offset
= ftell(mp
->mb_otf
);
2008 fwrite(head
, 1, headsize
, mp
->mb_otf
);
2010 fwrite(str
, 1, len
, mp
->mb_otf
);
2011 fputc('\n', mp
->mb_otf
);
2017 m
->m_size
= headsize
+ len
;
2018 m
->m_lines
= headlines
+ 1;
2019 m
->m_block
= mailx_blockof(offset
);
2020 m
->m_offset
= mailx_offsetof(offset
);
2021 m
->m_content_info
|= CI_HAVE_HEADER
| CI_HAVE_BODY
;
2022 m
->m_xlines
= m
->m_lines
;
2023 m
->m_xsize
= m
->m_size
;
2029 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
2033 sighandler_type
volatile saveint
, savepipe
;
2034 char * volatile head
;
2035 char const *cp
, *loc
, * volatile item
, * volatile resp
;
2037 size_t volatile headsize
;
2040 long volatile headlines
;
2045 saveint
= savepipe
= SIG_IGN
;
2047 cp
= loc
= item
= resp
= NULL
;
2049 number
= (int)PTR2SIZE(m
- message
+ 1);
2054 if (getcache(mp
, m
, need
) == OKAY
)
2056 if (mp
->mb_type
== MB_CACHE
) {
2057 n_err(_("Message %lu not available\n"), (ul_i
)number
);
2061 if (mp
->mb_sock
.s_fd
< 0) {
2062 n_err(_("IMAP connection closed\n"));
2068 resp
= item
= "RFC822.HEADER";
2071 item
= "BODY.PEEK[]";
2073 if ((m
->m_content_info
& CI_HAVE_HEADER
) && m
->m_size
) {
2074 char *hdr
= smalloc(m
->m_size
);
2076 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
2078 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
) != m
->m_size
) {
2083 headsize
= m
->m_size
;
2084 headlines
= m
->m_lines
;
2085 item
= "BODY.PEEK[TEXT]";
2086 resp
= "BODY[TEXT]";
2094 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2095 if (sigsetjmp(imapjmp
, 1)) {
2096 safe_signal(SIGINT
, saveint
);
2097 safe_signal(SIGPIPE
, savepipe
);
2101 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2102 safe_signal(SIGINT
, &_imap_maincatch
);
2103 if (savepipe
!= SIG_IGN
)
2104 safe_signal(SIGPIPE
, imapcatch
);
2107 snprintf(o
, sizeof o
, "%s UID FETCH %" PRIu64
" (%s)\r\n",
2108 tag(1), m
->m_uid
, item
);
2110 if (check_expunged() == STOP
)
2112 snprintf(o
, sizeof o
, "%s FETCH %u (%s)\r\n", tag(1), number
, item
);
2114 IMAP_OUT(o
, MB_COMD
, goto out
)
2118 ok
= imap_answer(mp
, 1);
2121 if (response_status
!= RESPONSE_OTHER
||
2122 response_other
!= MESSAGE_DATA_FETCH
)
2124 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
2128 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
2129 n_idec_ui64_cp(&uid
, &cp
[4], 10, NULL
);/* TODO errors? */
2134 n
= responded_other_number
;
2135 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
2136 if (m
->m_uid
? m
->m_uid
!= uid
: n
!= number
)
2138 if ((cp
= strchr(loc
, '"')) != NULL
) {
2139 cp
= imap_unquotestr(cp
);
2140 imap_putstr(mp
, m
, cp
, head
, headsize
, headlines
);
2142 m
->m_content_info
|= CI_HAVE_HEADER
| CI_HAVE_BODY
;
2143 m
->m_xlines
= m
->m_lines
;
2144 m
->m_xsize
= m
->m_size
;
2148 expected
= atol(&cp
[1]);
2149 if (m
->m_uid
? n
== 0 && m
->m_uid
!= uid
: n
!= number
) {
2150 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
2154 imap_fetchdata(mp
, &mt
, expected
, need
, head
, headsize
, headlines
);
2156 commitmsg(mp
, m
, &mt
, mt
.m_content_info
);
2159 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
2160 if (n_poption
& n_PO_VERBVERB
)
2161 fputs(imapbuf
, stderr
);
2162 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
2163 n_idec_ui64_cp(&uid
, &cp
[4], 10, NULL
);/* TODO errors? */
2164 if (uid
== m
->m_uid
) {
2165 commitmsg(mp
, m
, &mt
, mt
.m_content_info
);
2172 while (mp
->mb_active
& MB_COMD
)
2173 ok
= imap_answer(mp
, 1);
2175 if (saveint
!= SIG_IGN
)
2176 safe_signal(SIGINT
, saveint
);
2177 if (savepipe
!= SIG_IGN
)
2178 safe_signal(SIGPIPE
, savepipe
);
2186 n_go_onintr_for_imap();
2191 imap_header(struct message
*m
)
2196 rv
= imap_get(&mb
, m
, NEED_HEADER
);
2203 imap_body(struct message
*m
)
2208 rv
= imap_get(&mb
, m
, NEED_BODY
);
2214 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
2215 enum content_info content_info
)
2218 tomp
->m_size
= frommp
->m_size
;
2219 tomp
->m_lines
= frommp
->m_lines
;
2220 tomp
->m_block
= frommp
->m_block
;
2221 tomp
->m_offset
= frommp
->m_offset
;
2222 tomp
->m_content_info
= content_info
& CI_HAVE_MASK
;
2223 if (content_info
& CI_HAVE_BODY
) {
2224 tomp
->m_xlines
= frommp
->m_lines
;
2225 tomp
->m_xsize
= frommp
->m_size
;
2232 imap_fetchheaders(struct mailbox
*mp
, struct message
*m
, int bot
, int topp
)
2240 FILE *queuefp
= NULL
;
2245 snprintf(o
, sizeof o
,
2246 "%s UID FETCH %" PRIu64
":%" PRIu64
" (RFC822.HEADER)\r\n",
2247 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
2249 if (check_expunged() == STOP
)
2251 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.HEADER)\r\n",
2254 IMAP_OUT(o
, MB_COMD
, return STOP
)
2258 ok
= imap_answer(mp
, 1);
2259 if (response_status
!= RESPONSE_OTHER
)
2261 if (response_other
!= MESSAGE_DATA_FETCH
)
2263 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0) {
2267 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
2269 expected
= atol(&cp
[1]);
2270 if (m
[bot
-1].m_uid
) {
2271 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
) {
2274 n_idec_ui64_cp(&uid
, &cp
[4], 10, NULL
);/* TODO errors? */
2275 for (n
= bot
; n
<= topp
; n
++)
2276 if (uid
== m
[n
-1].m_uid
)
2279 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
2285 n
= responded_other_number
;
2286 if (n
<= 0 || n
> msgCount
) {
2287 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
2291 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
2292 if (n
>= 0 && !(m
[n
-1].m_content_info
& CI_HAVE_HEADER
))
2293 commitmsg(mp
, &m
[n
-1], &mt
, CI_HAVE_HEADER
);
2294 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
2295 if (n_poption
& n_PO_VERBVERB
)
2296 fputs(imapbuf
, stderr
);
2297 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
2300 n_idec_ui64_cp(&uid
, &cp
[4], 10, NULL
);/* TODO errors? */
2301 for (n
= bot
; n
<= topp
; n
++)
2302 if (uid
== m
[n
-1].m_uid
)
2304 if (n
<= topp
&& !(m
[n
-1].m_content_info
& CI_HAVE_HEADER
))
2305 commitmsg(mp
, &m
[n
-1], &mt
, CI_HAVE_HEADER
);
2312 while (mp
->mb_active
& MB_COMD
)
2313 ok
= imap_answer(mp
, 1);
2318 imap_getheaders(int volatile bot
, int volatile topp
) /* TODO iterator!! */
2320 sighandler_type saveint
, savepipe
;
2321 /*enum okay ok = STOP;*/
2325 if (mb
.mb_type
== MB_CACHE
)
2329 if (topp
> msgCount
)
2331 for (i
= bot
; i
< topp
; i
++) {
2332 if ((message
[i
-1].m_content_info
& CI_HAVE_HEADER
) ||
2333 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
2338 for (i
= topp
; i
> bot
; i
--) {
2339 if ((message
[i
-1].m_content_info
& CI_HAVE_HEADER
) ||
2340 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
2349 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2350 safe_signal(SIGINT
, &_imap_maincatch
);
2351 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2352 if (sigsetjmp(imapjmp
, 1) == 0) {
2353 if (savepipe
!= SIG_IGN
)
2354 safe_signal(SIGPIPE
, imapcatch
);
2356 for (i
= bot
; i
<= topp
; i
+= chunk
) {
2357 int j
= i
+ chunk
- 1;
2359 if (visible(message
+ j
))
2360 /*ok = */imap_fetchheaders(&mb
, message
, i
, j
);
2362 n_go_onintr_for_imap(); /* XXX imaplock? */
2365 safe_signal(SIGINT
, saveint
);
2366 safe_signal(SIGPIPE
, savepipe
);
2371 __imap_exit(struct mailbox
*mp
)
2374 FILE *queuefp
= NULL
;
2377 mp
->mb_active
|= MB_BYE
;
2378 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
2379 IMAP_OUT(o
, MB_COMD
, return STOP
)
2385 imap_exit(struct mailbox
*mp
)
2390 rv
= __imap_exit(mp
);
2391 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
2392 free(mp
->mb_imap_pass
);
2393 free(mp
->mb_imap_account
);
2394 free(mp
->mb_imap_mailbox
);
2395 if (mp
->mb_cache_directory
!= NULL
)
2396 free(mp
->mb_cache_directory
);
2397 #ifndef HAVE_DEBUG /* TODO ASSERT LEGACY */
2398 mp
->mb_imap_account
=
2399 mp
->mb_imap_mailbox
=
2400 mp
->mb_cache_directory
= "";
2402 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
2403 mp
->mb_imap_mailbox
= NULL
;
2404 mp
->mb_cache_directory
= NULL
;
2407 sclose(&mp
->mb_sock
);
2413 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
2416 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
2417 if (mp
->mb_type
== MB_IMAP
)
2424 imap_close(struct mailbox
*mp
)
2427 FILE *queuefp
= NULL
;
2430 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
2431 IMAP_OUT(o
, MB_COMD
, return STOP
)
2437 imap_update(struct mailbox
*mp
)
2440 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
2443 if (!(n_pstate
& n_PS_EDIT
) && mp
->mb_perm
!= 0) {
2446 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
2447 if (m
->m_flag
& MBOX
)
2450 if (makembox() == STOP
)
2455 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
2456 if (mp
->mb_perm
== 0)
2458 else if (n_pstate
& n_PS_EDIT
)
2459 dodel
= ((m
->m_flag
& MDELETED
) != 0);
2461 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
2463 /* Fetch the result after around each 800 STORE commands
2464 * sent (approx. 32k data sent). Otherwise, servers will
2465 * try to flush the return queue at some point, leading
2466 * to a deadlock if we are still writing commands but not
2467 * reading their results */
2468 needstat
= stored
> 0 && stored
% 800 == 0;
2469 /* Even if this message has been deleted, continue
2470 * to set further flags. This is necessary to support
2471 * Gmail semantics, where "delete" actually means
2472 * "archive", and the flags are applied to the copy
2474 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
2475 imap_store(mp
, m
, m
-message
+1, '+', "\\Seen", needstat
);
2478 if (m
->m_flag
& MFLAG
) {
2479 imap_store(mp
, m
, m
-message
+1, '+', "\\Flagged", needstat
);
2482 if (m
->m_flag
& MUNFLAG
) {
2483 imap_store(mp
, m
, m
-message
+1, '-', "\\Flagged", needstat
);
2486 if (m
->m_flag
& MANSWER
) {
2487 imap_store(mp
, m
, m
-message
+1, '+', "\\Answered", needstat
);
2490 if (m
->m_flag
& MUNANSWER
) {
2491 imap_store(mp
, m
, m
-message
+1, '-', "\\Answered", needstat
);
2494 if (m
->m_flag
& MDRAFT
) {
2495 imap_store(mp
, m
, m
-message
+1, '+', "\\Draft", needstat
);
2498 if (m
->m_flag
& MUNDRAFT
) {
2499 imap_store(mp
, m
, m
-message
+1, '-', "\\Draft", needstat
);
2504 imap_delete(mp
, m
-message
+1, m
, needstat
);
2507 } else if (mp
->mb_type
!= MB_CACHE
||
2508 (!(n_pstate
& n_PS_EDIT
) &&
2509 !(m
->m_flag
& (MBOXED
| MSAVED
| MDELETED
))) ||
2510 (m
->m_flag
& (MBOXED
| MPRESERVE
| MTOUCH
)) ==
2511 (MPRESERVE
| MTOUCH
) ||
2512 ((n_pstate
& n_PS_EDIT
) && !(m
->m_flag
& MDELETED
)))
2514 if (m
->m_flag
& MNEW
) {
2516 m
->m_flag
|= MSTATUS
;
2523 for (m
= &message
[0]; PTRCMP(m
, <, message
+ msgCount
); ++m
)
2524 if (!(m
->m_flag
& MUNLINKED
) &&
2525 m
->m_flag
& (MBOXED
| MDELETED
| MSAVED
| MSTATUS
| MFLAG
|
2526 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
)) {
2531 /* XXX should be readonly (but our IMAP code is weird...) */
2532 if (!(n_poption
& (n_PO_EXISTONLY
| n_PO_HEADERSONLY
| n_PO_HEADERLIST
)) &&
2534 if ((gotcha
|| modflags
) && (n_pstate
& n_PS_EDIT
)) {
2535 printf(_("\"%s\" "), displayname
);
2536 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
2537 ? _("complete\n") : _("updated.\n"));
2538 } else if (held
&& !(n_pstate
& n_PS_EDIT
)) {
2540 printf(_("Held 1 message in %s\n"), displayname
);
2542 printf(_("Held %d messages in %s\n"), held
, displayname
);
2551 imap_quit(bool_t hold_sigs_on
)
2553 sighandler_type
volatile saveint
, savepipe
;
2560 if (mb
.mb_type
== MB_CACHE
) {
2561 rv
= (imap_update(&mb
) == OKAY
);
2567 if (mb
.mb_sock
.s_fd
< 0) {
2568 n_err(_("IMAP connection closed\n"));
2573 saveint
= safe_signal(SIGINT
, SIG_IGN
);
2574 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2575 if (sigsetjmp(imapjmp
, 1)) {
2576 safe_signal(SIGINT
, saveint
);
2577 safe_signal(SIGPIPE
, saveint
);
2581 if (saveint
!= SIG_IGN
)
2582 safe_signal(SIGINT
, imapcatch
);
2583 if (savepipe
!= SIG_IGN
)
2584 safe_signal(SIGPIPE
, imapcatch
);
2586 rv
= (imap_update(&mb
) == OKAY
);
2587 if(!same_imap_account
&& imap_exit(&mb
) != OKAY
)
2590 safe_signal(SIGINT
, saveint
);
2591 safe_signal(SIGPIPE
, savepipe
);
2601 imap_store(struct mailbox
*mp
, struct message
*m
, int n
, int c
, const char *sp
,
2605 FILE *queuefp
= NULL
;
2608 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
2611 snprintf(o
, sizeof o
, "%s UID STORE %" PRIu64
" %cFLAGS (%s)\r\n",
2612 tag(1), m
->m_uid
, c
, sp
);
2614 if (check_expunged() == STOP
)
2616 snprintf(o
, sizeof o
, "%s STORE %u %cFLAGS (%s)\r\n", tag(1), n
, c
, sp
);
2618 IMAP_OUT(o
, MB_COMD
, return STOP
)
2622 mb
.mb_active
&= ~MB_COMD
;
2623 if (queuefp
!= NULL
)
2629 imap_undelete(struct message
*m
, int n
)
2634 rv
= imap_unstore(m
, n
, "\\Deleted");
2640 imap_unread(struct message
*m
, int n
)
2645 rv
= imap_unstore(m
, n
, "\\Seen");
2651 imap_unstore(struct message
*m
, int n
, const char *flag
)
2653 sighandler_type saveint
, savepipe
;
2654 enum okay
volatile rv
= STOP
;
2658 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2659 safe_signal(SIGINT
, &_imap_maincatch
);
2660 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2661 if (sigsetjmp(imapjmp
, 1) == 0) {
2662 if (savepipe
!= SIG_IGN
)
2663 safe_signal(SIGPIPE
, imapcatch
);
2665 rv
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2667 safe_signal(SIGINT
, saveint
);
2668 safe_signal(SIGPIPE
, savepipe
);
2673 n_go_onintr_for_imap();
2686 snprintf(ts
, sizeof ts
, "T%lu", n
);
2692 c_imapcodec(void *vp
){
2695 char const **argv
, *varname
, *varres
, *act
, *cp
;
2699 varname
= (n_pstate
& n_PS_ARGMOD_VPUT
) ? *argv
++ : NULL
;
2702 for(cp
= act
; *cp
!= '\0' && !blankspacechar(*cp
); ++cp
)
2706 alen
= PTR2SIZE(cp
- act
);
2710 n_pstate_err_no
= n_ERR_NONE
;
2711 varres
= imap_path_normalize(NULL
, cp
);
2713 if(is_ascncaseprefix(act
, "encode", alen
))
2714 varres
= imap_path_encode(varres
, &err
);
2715 else if(is_ascncaseprefix(act
, "decode", alen
))
2716 varres
= imap_path_decode(varres
, &err
);
2721 n_pstate_err_no
= n_ERR_CANCELED
;
2726 if(varname
!= NULL
){
2727 if(!n_var_vset(varname
, (uintptr_t)varres
)){
2728 n_pstate_err_no
= n_ERR_NOTSUP
;
2734 in
.l
= strlen(in
.s
= n_UNCONST(varres
));
2735 makeprint(&in
, &out
);
2736 if(fprintf(n_stdout
, "%s\n", out
.s
) < 0){
2737 n_pstate_err_no
= n_err_no
;
2745 return (vp
!= NULL
? 0 : 1);
2747 n_err(_("Synopsis: imapcodec: <e[ncode]|d[ecode]> <rest-of-line>\n"));
2748 n_pstate_err_no
= n_ERR_INVAL
;
2754 c_imap_imap(void *vp
)
2757 sighandler_type saveint
, savepipe
;
2758 struct mailbox
*mp
= &mb
;
2759 FILE *queuefp
= NULL
;
2760 enum okay
volatile ok
= STOP
;
2763 if (mp
->mb_type
!= MB_IMAP
) {
2764 printf("Not operating on an IMAP mailbox.\n");
2768 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2769 safe_signal(SIGINT
, &_imap_maincatch
);
2770 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2771 if (sigsetjmp(imapjmp
, 1) == 0) {
2772 if (savepipe
!= SIG_IGN
)
2773 safe_signal(SIGPIPE
, imapcatch
);
2775 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2776 IMAP_OUT(o
, MB_COMD
, goto out
)
2777 while (mp
->mb_active
& MB_COMD
) {
2778 ok
= imap_answer(mp
, 0);
2779 fputs(responded_text
, stdout
);
2783 safe_signal(SIGINT
, saveint
);
2784 safe_signal(SIGPIPE
, savepipe
);
2788 n_go_onintr_for_imap();
2793 imap_newmail(int nmail
)
2797 if (nmail
&& had_exists
< 0 && had_expunge
< 0) {
2803 if (had_exists
== msgCount
&& had_expunge
< 0)
2804 /* Some servers always respond with EXISTS to NOOP. If
2805 * the mailbox has been changed but the number of messages
2806 * has not, an EXPUNGE must also had been sent; otherwise,
2807 * nothing has changed */
2810 return (had_expunge
>= 0 ? 2 : (had_exists
>= 0 ? 1 : 0));
2814 imap_putflags(int f
)
2820 bp
= buf
= salloc(100);
2821 if (f
& (MREAD
| MFLAGGED
| MANSWERED
| MDRAFTED
)) {
2826 for (cp
= "\\Seen"; *cp
; cp
++)
2832 for (cp
= "\\Flagged"; *cp
; cp
++)
2835 if (f
& MANSWERED
) {
2838 for (cp
= "\\Answered"; *cp
; cp
++)
2844 for (cp
= "\\Draft"; *cp
; cp
++)
2856 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2859 while (*cp
!= ')') {
2861 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2863 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2865 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2867 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2869 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2871 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2883 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
, off_t off1
,
2884 long xsize
, enum mflag flag
, time_t t
)
2886 char o
[LINESIZE
], *buf
;
2887 size_t bufsize
, buflen
, cnt
;
2888 long size
, lines
, ysize
;
2900 if((qname
= imap_path_quote(mp
, name
)) == NULL
)
2903 if (mp
->mb_type
== MB_CACHE
) {
2904 queuefp
= cache_queue(mp
);
2905 if (queuefp
== NULL
) {
2912 buf
= smalloc(bufsize
= LINESIZE
);
2917 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2922 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2923 tag(1), qname
, imap_putflags(flag
), imap_make_date_time(t
), size
);
2924 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2925 while (mp
->mb_active
& MB_COMD
) {
2926 rv
= imap_answer(mp
, twice
);
2927 if (response_type
== RESPONSE_CONT
)
2931 if (mp
->mb_type
!= MB_CACHE
&& rv
== STOP
) {
2940 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2943 buf
[buflen
- 1] = '\r';
2945 if (mp
->mb_type
!= MB_CACHE
)
2946 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2948 fwrite(buf
, 1, buflen
+1, queuefp
);
2951 if (mp
->mb_type
!= MB_CACHE
)
2952 swrite(&mp
->mb_sock
, "\r\n");
2954 fputs("\r\n", queuefp
);
2955 while (mp
->mb_active
& MB_COMD
) {
2956 rv
= imap_answer(mp
, 0);
2957 if (response_status
== RESPONSE_NO
/*&&
2958 ascncasecmp(responded_text,
2959 "[TRYCREATE] ", 12) == 0*/) {
2966 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2967 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2968 while (mp
->mb_active
& MB_COMD
)
2969 rv
= imap_answer(mp
, 1);
2972 imap_created_mailbox
++;
2974 } else if (rv
!= OKAY
)
2975 n_err(_("IMAP error: %s"), responded_text
);
2976 else if (response_status
== RESPONSE_OK
&& (mp
->mb_flags
& MB_UIDPLUS
))
2977 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
, flag
, name
);
2980 if (queuefp
!= NULL
)
2989 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
, long offset
)
2991 char *buf
, *bp
, *lp
;
2992 size_t bufsize
, buflen
, cnt
;
2993 off_t off1
= -1, offs
;
2995 enum {_NONE
= 0, _INHEAD
= 1<<0, _NLSEP
= 1<<1} state
;
3001 buf
= smalloc(bufsize
= LINESIZE
);
3004 offs
= offset
/* BSD will move due to O_APPEND! ftell(fp) */;
3008 for (flag
= MNEW
, state
= _NLSEP
;;) {
3009 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
3012 ((state
& (_INHEAD
| _NLSEP
)) == _NLSEP
&&
3013 is_head(buf
, buflen
, FAL0
))) {
3014 if (off1
!= (off_t
)-1) {
3015 rv
= imap_append1(mp
, name
, fp
, off1
, size
, flag
, tim
);
3018 fseek(fp
, offs
+buflen
, SEEK_SET
);
3020 off1
= offs
+ buflen
;
3026 tim
= unixtime(buf
);
3032 if (buf
[0] == '\n') {
3035 } else if (state
& _INHEAD
) {
3036 if (ascncasecmp(buf
, "status", 6) == 0) {
3038 while (whitechar(*lp
))
3041 while (*++lp
!= '\0')
3050 } else if (ascncasecmp(buf
, "x-status", 8) == 0) {
3052 while (whitechar(*lp
))
3055 while (*++lp
!= '\0')
3078 imap_append(const char *xserver
, FILE *fp
, long offset
)
3080 sighandler_type
volatile saveint
, savepipe
;
3083 enum okay rv
= STOP
;
3086 if (!url_parse(&url
, CPROTO_IMAP
, xserver
))
3088 if (!ok_blook(v15_compat
) &&
3089 (!(url
.url_flags
& n_URL_HAD_USER
) || url
.url_pass
.s
!= NULL
))
3090 n_err(_("New-style URL used without *v15-compat* being set!\n"));
3091 assert(url
.url_path
.s
!= NULL
);
3094 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3095 safe_signal(SIGINT
, &_imap_maincatch
);
3096 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3097 if (sigsetjmp(imapjmp
, 1))
3099 if (savepipe
!= SIG_IGN
)
3100 safe_signal(SIGPIPE
, imapcatch
);
3102 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) && mb
.mb_imap_account
&&
3103 !strcmp(url
.url_p_eu_h_p
, mb
.mb_imap_account
)) {
3104 rv
= imap_append0(&mb
, url
.url_path
.s
, fp
, offset
);
3108 memset(&mx
, 0, sizeof mx
);
3110 if (!_imap_getcred(&mx
, &ccred
, &url
))
3113 imap_delim_init(&mx
, &url
);
3114 mx
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&mx
, url
.url_path
.s
));
3116 if (disconnected(url
.url_p_eu_h_p
) == 0) {
3117 if (!sopen(&mx
.mb_sock
, &url
))
3119 mx
.mb_sock
.s_desc
= "IMAP";
3120 mx
.mb_type
= MB_IMAP
;
3121 mx
.mb_imap_account
= n_UNCONST(url
.url_p_eu_h_p
);
3122 /* TODO the code now did
3123 * TODO mx.mb_imap_mailbox = mbx->url.url_patth.s;
3124 * TODO though imap_mailbox is sfree()d and mbx
3125 * TODO is possibly even a constant
3126 * TODO i changed this to sstrdup() sofar, as is used
3127 * TODO somewhere else in this file for this! */
3128 if (imap_preauth(&mx
, &url
) != OKAY
||
3129 imap_auth(&mx
, &ccred
) != OKAY
) {
3130 sclose(&mx
.mb_sock
);
3133 rv
= imap_append0(&mx
, url
.url_path
.s
, fp
, offset
);
3136 mx
.mb_imap_account
= n_UNCONST(url
.url_p_eu_h_p
);
3137 mx
.mb_type
= MB_CACHE
;
3138 rv
= imap_append0(&mx
, url
.url_path
.s
, fp
, offset
);
3145 safe_signal(SIGINT
, saveint
);
3146 safe_signal(SIGPIPE
, savepipe
);
3151 n_go_onintr_for_imap();
3156 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
3157 struct list_item
**lend
, int level
)
3159 char o
[LINESIZE
], *cp
;
3160 struct list_item
*lp
;
3161 const char *qname
, *bp
;
3169 if((qname
= imap_path_quote(mp
, base
)) == NULL
)
3172 *list
= *lend
= NULL
;
3173 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n", tag(1), qname
);
3174 IMAP_OUT(o
, MB_COMD
, goto jleave
)
3175 while (mp
->mb_active
& MB_COMD
) {
3176 ok
= imap_answer(mp
, 1);
3177 if (response_status
== RESPONSE_OTHER
&&
3178 response_other
== MAILBOX_DATA_LIST
&& imap_parse_list() == OKAY
) {
3179 cp
= imap_path_decode(imap_unquotestr(list_name
), NULL
);
3180 lp
= csalloc(1, sizeof *lp
);
3182 for (bp
= base
; *bp
!= '\0' && *bp
== *cp
; ++bp
)
3184 lp
->l_base
= *cp
? cp
: savestr(base
);
3185 lp
->l_attr
= list_attributes
;
3186 lp
->l_level
= level
+1;
3187 lp
->l_delim
= list_hierarchy_delimiter
;
3188 if (*list
&& *lend
) {
3189 (*lend
)->l_next
= lp
;
3200 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
3202 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
3209 depth
= (cp
= ok_vlook(imap_list_depth
)) != NULL
? atoi(cp
) : 2;
3210 if ((rv
= imap_list1(mp
, base
, &list
, &lend
, 0)) == STOP
)
3213 if (list
== NULL
|| lend
== NULL
)
3216 for (lp
= list
; lp
; lp
= lp
->l_next
)
3217 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&& lp
->l_level
< depth
&&
3218 !(lp
->l_attr
& LIST_NOINFERIORS
)) {
3219 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
3220 memcpy(cp
, lp
->l_name
, n
);
3221 cp
[n
] = lp
->l_delim
;
3223 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&& lx
&& ly
) {
3224 lp
->l_has_children
= 1;
3225 if (strcmp(cp
, lx
->l_name
) == 0)
3234 for (lp
= list
; lp
; lp
= lp
->l_next
) {
3237 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
3241 if (!(lp
->l_attr
& LIST_NOSELECT
))
3242 fprintf(fp
, "%s\n", *cp
? cp
: base
);
3243 else if (lp
->l_has_children
== 0)
3244 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
3245 (lp
->l_delim
!= EOF
? lp
->l_delim
: '\n'));
3253 imap_folders(const char * volatile name
, int strip
)
3255 sighandler_type saveint
, savepipe
;
3256 const char * volatile fold
, *cp
, *sp
;
3261 cp
= protbase(name
);
3262 sp
= mb
.mb_imap_account
;
3263 if (sp
== NULL
|| strcmp(cp
, sp
)) {
3265 _("Cannot perform `folders' but when on the very IMAP "
3266 "account; the current one is\n `%s' -- "
3267 "try `folders @'\n"),
3268 (sp
!= NULL
? sp
: _("[NONE]")));
3272 fold
= imap_fileof(name
);
3273 if (n_psonce
& n_PSO_TTYOUT
) {
3274 if ((fp
= Ftmp(NULL
, "imapfold", OF_RDWR
| OF_UNLINK
| OF_REGISTER
))
3276 n_perr(_("tmpfile"), 0);
3283 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3284 safe_signal(SIGINT
, &_imap_maincatch
);
3285 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3286 if (sigsetjmp(imapjmp
, 1)) /* TODO imaplock? */
3288 if (savepipe
!= SIG_IGN
)
3289 safe_signal(SIGPIPE
, imapcatch
);
3291 if (mb
.mb_type
== MB_CACHE
)
3292 cache_list(&mb
, fold
, strip
, fp
);
3294 imap_list(&mb
, fold
, strip
, fp
);
3298 if (n_psonce
& n_PSO_TTYOUT
)
3305 if (n_psonce
& n_PSO_TTYOUT
) {
3308 page_or_print(fp
, 0);
3311 n_err(_("Folder not found\n"));
3315 safe_signal(SIGINT
, saveint
);
3316 safe_signal(SIGPIPE
, savepipe
);
3317 if (n_psonce
& n_PSO_TTYOUT
)
3322 n_go_onintr_for_imap();
3327 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
3331 bool_t twice
, stored
;
3338 twice
= stored
= FAL0
;
3343 i
= strlen(name
= imap_fileof(name
));
3344 if(i
== 0 || (i
> 0 && name
[i
- 1] == '/'))
3345 name
= savecat(name
, "INBOX");
3346 if((qname
= imap_path_quote(mp
, name
)) == NULL
)
3350 if (mp
->mb_type
== MB_CACHE
) {
3351 if ((queuefp
= cache_queue(mp
)) == NULL
)
3356 /* Since it is not possible to set flags on the copy, recently
3357 * set flags must be set on the original to include it in the copy */
3358 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
))
3359 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
3360 if (m
->m_flag
&MFLAG
)
3361 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
3362 if (m
->m_flag
&MUNFLAG
)
3363 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
3364 if (m
->m_flag
&MANSWER
)
3365 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
3366 if (m
->m_flag
&MUNANSWER
)
3367 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
3368 if (m
->m_flag
&MDRAFT
)
3369 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
3370 if (m
->m_flag
&MUNDRAFT
)
3371 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
3374 snprintf(o
, sizeof o
, "%s UID COPY %" PRIu64
" %s\r\n",
3375 tag(1), m
->m_uid
, qname
);
3377 if (check_expunged() == STOP
)
3379 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n", tag(1), n
, qname
);
3381 IMAP_OUT(o
, MB_COMD
, goto out
)
3382 while (mp
->mb_active
& MB_COMD
)
3383 ok
= imap_answer(mp
, twice
);
3385 if (mp
->mb_type
== MB_IMAP
&& mp
->mb_flags
& MB_UIDPLUS
&&
3386 response_status
== RESPONSE_OK
)
3387 imap_copyuid(mp
, m
, name
);
3389 if (response_status
== RESPONSE_NO
&& !twice
) {
3390 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
3391 IMAP_OUT(o
, MB_COMD
, goto out
)
3392 while (mp
->mb_active
& MB_COMD
)
3393 ok
= imap_answer(mp
, 1);
3395 imap_created_mailbox
++;
3400 if (queuefp
!= NULL
)
3403 /* ... and reset the flag to its initial value so that the 'exit'
3404 * command still leaves the message unread */
3406 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
3407 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
3410 if (m
->m_flag
& MFLAG
) {
3411 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
3414 if (m
->m_flag
& MUNFLAG
) {
3415 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
3418 if (m
->m_flag
& MANSWER
) {
3419 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
3422 if (m
->m_flag
& MUNANSWER
) {
3423 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
3426 if (m
->m_flag
& MDRAFT
) {
3427 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
3430 if (m
->m_flag
& MUNDRAFT
) {
3431 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
3435 mp
->mb_active
|= MB_COMD
;
3436 (void)imap_finish(mp
);
3443 imap_copy(struct message
*m
, int n
, const char *name
)
3445 sighandler_type saveint
, savepipe
;
3446 enum okay
volatile rv
= STOP
;
3450 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3451 safe_signal(SIGINT
, &_imap_maincatch
);
3452 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3453 if (sigsetjmp(imapjmp
, 1) == 0) {
3454 if (savepipe
!= SIG_IGN
)
3455 safe_signal(SIGPIPE
, imapcatch
);
3457 rv
= imap_copy1(&mb
, m
, n
, name
);
3459 safe_signal(SIGINT
, saveint
);
3460 safe_signal(SIGPIPE
, savepipe
);
3465 n_go_onintr_for_imap();
3470 imap_copyuid_parse(const char *cp
, ui64_t
*uidvalidity
, ui64_t
*olduid
,
3473 char const *xp
, *yp
, *zp
;
3477 n_idec_ui64_cp(uidvalidity
, cp
, 10, &xp
); /* TODO errors */
3478 n_idec_ui64_cp(olduid
, xp
, 10, &yp
); /* TODO errors */
3479 n_idec_ui64_cp(newuid
, yp
, 10, &zp
); /* TODO errors */
3480 rv
= (*uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
3481 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']');
3487 imap_appenduid_parse(const char *cp
, ui64_t
*uidvalidity
, ui64_t
*uid
)
3489 char const *xp
, *yp
;
3493 n_idec_ui64_cp(uidvalidity
, cp
, 10, &xp
); /* TODO errors */
3494 n_idec_ui64_cp(uid
, xp
, 10, &yp
); /* TODO errors */
3495 rv
= (*uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' && yp
> xp
&&
3502 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
3507 ui64_t uidvalidity
, olduid
, newuid
;
3513 memset(&xmb
, 0, sizeof xmb
);
3515 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
3516 imap_copyuid_parse(&cp
[9], &uidvalidity
, &olduid
, &newuid
) == STOP
)
3522 xmb
.mb_cache_directory
= NULL
;
3523 xmb
.mb_imap_account
= sstrdup(mp
->mb_imap_account
);
3524 xmb
.mb_imap_pass
= sstrdup(mp
->mb_imap_pass
);
3525 memcpy(&xmb
.mb_imap_delim
[0], &mp
->mb_imap_delim
[0],
3526 sizeof(xmb
.mb_imap_delim
));
3527 xmb
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&xmb
, name
));
3528 if (mp
->mb_cache_directory
!= NULL
)
3529 xmb
.mb_cache_directory
= sstrdup(mp
->mb_cache_directory
);
3530 xmb
.mb_uidvalidity
= uidvalidity
;
3534 memset(&xm
, 0, sizeof xm
);
3536 if ((rv
= getcache1(mp
, &xm
, NEED_UNSPEC
, 3)) != OKAY
)
3538 getcache(mp
, &xm
, NEED_HEADER
);
3539 getcache(mp
, &xm
, NEED_BODY
);
3541 if ((m
->m_content_info
& CI_HAVE_HEADER
) == 0)
3542 getcache(mp
, m
, NEED_HEADER
);
3543 if ((m
->m_content_info
& CI_HAVE_BODY
) == 0)
3544 getcache(mp
, m
, NEED_BODY
);
3548 xm
.m_flag
&= ~MFULLYCACHED
;
3549 putcache(&xmb
, &xm
);
3551 if (xmb
.mb_cache_directory
!= NULL
)
3552 free(xmb
.mb_cache_directory
);
3553 if (xmb
.mb_imap_mailbox
!= NULL
)
3554 free(xmb
.mb_imap_mailbox
);
3555 if (xmb
.mb_imap_pass
!= NULL
)
3556 free(xmb
.mb_imap_pass
);
3557 if (xmb
.mb_imap_account
!= NULL
)
3558 free(xmb
.mb_imap_account
);
3564 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
, long xsize
,
3565 long size
, long lines
, int flag
, const char *name
)
3570 ui64_t uidvalidity
, uid
;
3576 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
3577 imap_appenduid_parse(&cp
[11], &uidvalidity
, &uid
) == STOP
)
3583 xmb
.mb_cache_directory
= NULL
;
3584 /* XXX mb_imap_delim reused */
3585 xmb
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&xmb
, name
));
3586 xmb
.mb_uidvalidity
= uidvalidity
;
3587 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
3589 memset(&xm
, 0, sizeof xm
);
3590 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
3592 xm
.m_block
= mailx_blockof(off1
);
3593 xm
.m_offset
= mailx_offsetof(off1
);
3596 xm
.m_lines
= xm
.m_xlines
= lines
;
3598 xm
.m_content_info
= CI_HAVE_HEADER
| CI_HAVE_BODY
;
3599 putcache(&xmb
, &xm
);
3601 free(xmb
.mb_imap_mailbox
);
3608 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
3612 long size
, xsize
, ysize
, lines
;
3613 enum mflag flag
= MNEW
;
3614 char *name
, *buf
, *bp
;
3616 size_t bufsize
, buflen
, cnt
;
3617 enum okay rv
= STOP
;
3620 buf
= smalloc(bufsize
= LINESIZE
);
3623 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3626 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3631 if ((cp
= strrchr(bp
, '{')) == NULL
)
3634 xsize
= atol(&cp
[1]) + 2;
3635 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
3641 imap_getflags(cp
, &cp
, &flag
);
3642 while (*++cp
== ' ')
3645 t
= imap_read_date_time(cp
);
3647 if ((tp
= Ftmp(NULL
, "imapapui", OF_RDWR
| OF_UNLINK
| OF_REGISTER
))
3654 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3657 buf
[--buflen
] = '\0';
3658 buf
[buflen
-1] = '\n';
3659 fwrite(buf
, 1, buflen
, tp
);
3666 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
3667 imap_unquotestr(name
));
3677 #ifdef HAVE_IMAP_SEARCH
3679 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
, const char *spec
,
3684 FILE *queuefp
= NULL
;
3686 const char *cp
, *xp
;
3687 enum okay ok
= STOP
;
3691 for (cp
= spec
; *cp
; cp
++)
3694 cp
= ok_vlook(ttycharset
);
3696 if(asccasecmp(cp
, "utf-8") && asccasecmp(cp
, "utf8")){ /* XXX */
3699 if((nspec
= n_iconv_onetime_cp(n_ICONV_DEFAULT
, "utf-8", cp
, spec
)
3706 cp
= imap_quotestr(cp
);
3707 cs
= n_lofi_alloc(n
= strlen(cp
) + 10);
3708 snprintf(cs
, n
, "CHARSET %s ", cp
);
3710 cs
= n_UNCONST(n_empty
);
3712 o
= n_lofi_alloc(n
= strlen(spec
) + 60);
3713 snprintf(o
, n
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
3714 IMAP_OUT(o
, MB_COMD
, goto out
)
3715 while (mp
->mb_active
& MB_COMD
) {
3716 ok
= imap_answer(mp
, 0);
3717 if (response_status
== RESPONSE_OTHER
&&
3718 response_other
== MAILBOX_DATA_SEARCH
) {
3719 xp
= responded_other_text
;
3720 while (*xp
&& *xp
!= '\r') {
3723 n_idec_ui64_cp(&uid
, xp
, 10, &xp
);/* TODO errors? */
3724 for (i
= 0; i
< cnt
; i
++)
3725 if (m
[i
].m_uid
== uid
&& !(m
[i
].m_flag
& MHIDDEN
) &&
3726 (f
== MDELETED
|| !(m
[i
].m_flag
& MDELETED
)))
3739 imap_search1(const char * volatile spec
, int f
)
3741 sighandler_type saveint
, savepipe
;
3742 enum okay
volatile rv
= STOP
;
3745 if (mb
.mb_type
!= MB_IMAP
)
3749 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3750 safe_signal(SIGINT
, &_imap_maincatch
);
3751 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3752 if (sigsetjmp(imapjmp
, 1) == 0) {
3753 if (savepipe
!= SIG_IGN
)
3754 safe_signal(SIGPIPE
, imapcatch
);
3756 rv
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
3758 safe_signal(SIGINT
, saveint
);
3759 safe_signal(SIGPIPE
, savepipe
);
3764 n_go_onintr_for_imap();
3767 #endif /* HAVE_IMAP_SEARCH */
3770 imap_thisaccount(const char *cp
)
3775 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
3777 else if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
3778 mb
.mb_imap_account
== NULL
)
3781 rv
= !strcmp(protbase(cp
), mb
.mb_imap_account
);
3787 imap_remove(const char * volatile name
)
3789 sighandler_type
volatile saveint
, savepipe
;
3790 enum okay
volatile rv
= STOP
;
3793 if (mb
.mb_type
!= MB_IMAP
) {
3794 n_err(_("Refusing to remove \"%s\" in disconnected mode\n"), name
);
3798 if (!imap_thisaccount(name
)) {
3799 n_err(_("Can only remove mailboxes on current IMAP server: "
3800 "\"%s\" not removed\n"), name
);
3805 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3806 safe_signal(SIGINT
, &_imap_maincatch
);
3807 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3808 if (sigsetjmp(imapjmp
, 1) == 0) {
3809 if (savepipe
!= SIG_IGN
)
3810 safe_signal(SIGPIPE
, imapcatch
);
3812 rv
= imap_remove1(&mb
, imap_fileof(name
));
3814 safe_signal(SIGINT
, saveint
);
3815 safe_signal(SIGPIPE
, savepipe
);
3819 rv
= cache_remove(name
);
3823 n_go_onintr_for_imap();
3828 imap_remove1(struct mailbox
*mp
, const char *name
)
3840 if((qname
= imap_path_quote(mp
, name
)) != NULL
){
3841 o
= ac_alloc(os
= strlen(qname
) + 100);
3842 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), qname
);
3843 IMAP_OUT(o
, MB_COMD
, goto out
)
3844 while (mp
->mb_active
& MB_COMD
)
3845 ok
= imap_answer(mp
, 1);
3853 imap_rename(const char *old
, const char *new)
3855 sighandler_type saveint
, savepipe
;
3856 enum okay
volatile rv
= STOP
;
3859 if (mb
.mb_type
!= MB_IMAP
) {
3860 n_err(_("Refusing to rename mailboxes in disconnected mode\n"));
3864 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3865 n_err(_("Can only rename mailboxes on current IMAP "
3866 "server: \"%s\" not renamed to \"%s\"\n"), old
, new);
3871 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3872 safe_signal(SIGINT
, &_imap_maincatch
);
3873 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3874 if (sigsetjmp(imapjmp
, 1) == 0) {
3875 if (savepipe
!= SIG_IGN
)
3876 safe_signal(SIGPIPE
, imapcatch
);
3878 rv
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3880 safe_signal(SIGINT
, saveint
);
3881 safe_signal(SIGPIPE
, savepipe
);
3885 rv
= cache_rename(old
, new);
3889 n_go_onintr_for_imap();
3894 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3898 char const *qoname
, *qnname
;
3906 if((qoname
= imap_path_quote(mp
, old
)) != NULL
&&
3907 (qnname
= imap_path_quote(mp
, new)) != NULL
){
3908 o
= ac_alloc(os
= strlen(qoname
) + strlen(qnname
) + 100);
3909 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1), qoname
, qnname
);
3910 IMAP_OUT(o
, MB_COMD
, goto out
)
3911 while (mp
->mb_active
& MB_COMD
)
3912 ok
= imap_answer(mp
, 1);
3920 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3922 char o
[LINESIZE
], *newname
, *buf
, *bp
, *cp
, iob
[4096];
3923 size_t bufsize
, buflen
, cnt
;
3924 long offs
, offs1
, offs2
, octets
;
3925 int twice
, gotcha
= 0;
3926 FILE *queuefp
= NULL
;
3927 enum okay ok
= OKAY
, rok
= OKAY
;
3930 buf
= smalloc(bufsize
= LINESIZE
);
3933 while ((offs1
= ftell(fp
)) >= 0 &&
3934 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) != NULL
) {
3935 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3940 if ((offs
= ftell(fp
)) < 0)
3943 snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3944 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3946 while (digitchar(*cp
))
3952 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3954 IMAP_OUT(o
, MB_COMD
, continue)
3955 while (mp
->mb_active
& MB_COMD
)
3956 ok
= imap_answer(mp
, twice
);
3957 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3959 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3960 imap_copyuid(mp
, NULL
, imap_unquotestr(newname
));
3962 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3963 IMAP_OUT(o
, MB_COMD
, continue)
3964 while (mp
->mb_active
& MB_COMD
)
3965 ok
= imap_answer(mp
, 1);
3968 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3969 if ((cp
= strrchr(bp
, '{')) == NULL
)
3971 octets
= atol(&cp
[1]) + 2;
3972 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3974 IMAP_OUT(o
, MB_COMD
, continue)
3975 while (mp
->mb_active
& MB_COMD
) {
3976 ok
= imap_answer(mp
, twice
);
3977 if (response_type
== RESPONSE_CONT
)
3981 if (twice
++ == 0 && fseek(fp
, offs
, SEEK_SET
) >= 0)
3985 while (octets
> 0) {
3986 size_t n
= (UICMP(z
, octets
, >, sizeof iob
)
3987 ? sizeof iob
: (size_t)octets
);
3989 if (n
!= fread(iob
, 1, n
, fp
))
3991 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3993 swrite(&mp
->mb_sock
, "");
3994 while (mp
->mb_active
& MB_COMD
) {
3995 ok
= imap_answer(mp
, 0);
3996 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
3997 if (fseek(fp
, offs
, SEEK_SET
) < 0)
4002 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
4003 if ((offs2
= ftell(fp
)) < 0)
4005 fseek(fp
, offs1
, SEEK_SET
);
4006 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
4007 (void)fseek(fp
, offs2
, SEEK_SET
);
4013 n_err(_("Invalid command in IMAP cache queue: \"%s\"\n"), bp
);
4018 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), newname
);
4019 IMAP_OUT(o
, MB_COMD
, continue)
4020 while (mp
->mb_active
& MB_COMD
)
4021 ok
= imap_answer(mp
, 1);
4027 ftruncate(fileno(fp
), 0);
4035 imap_strex(char const *cp
, char const **xp
)
4044 for (cq
= cp
+ 1; *cq
!= '\0'; ++cq
) {
4047 else if (*cq
== '"')
4053 n
= salloc(cq
- cp
+ 2);
4054 memcpy(n
, cp
, cq
- cp
+1);
4055 n
[cq
- cp
+ 1] = '\0';
4064 check_expunged(void)
4069 if (expunged_messages
> 0) {
4070 n_err(_("Command not executed - messages have been expunged\n"));
4079 c_connect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
4082 int rv
, omsgCount
= msgCount
;
4086 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
4087 n_err(_("Already connected\n"));
4092 if (!url_parse(&url
, CPROTO_IMAP
, mailname
)) {
4096 ok_bclear(disconnected
);
4097 n_var_vclear(savecat("disconnected-", url
.url_u_h_p
.s
));
4099 if (mb
.mb_type
== MB_CACHE
) {
4100 enum fedit_mode fm
= FEDIT_NONE
;
4103 if (!(n_pstate
& n_PS_EDIT
))
4105 _imap_setfile1(&url
, fm
, 1);
4106 if (msgCount
> omsgCount
)
4107 newmailinfo(omsgCount
);
4116 c_disconnect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
4119 int rv
= 1, *msgvec
= vp
;
4122 if (mb
.mb_type
== MB_CACHE
) {
4123 n_err(_("Not connected\n"));
4126 if (mb
.mb_type
!= MB_IMAP
|| cached_uidvalidity(&mb
) == 0) {
4127 n_err(_("The current mailbox is not cached\n"));
4131 if (!url_parse(&url
, CPROTO_IMAP
, mailname
))
4136 ok_bset(disconnected
);
4137 if (mb
.mb_type
== MB_IMAP
) {
4138 enum fedit_mode fm
= FEDIT_NONE
;
4141 if (!(n_pstate
& n_PS_EDIT
))
4143 sclose(&mb
.mb_sock
);
4144 _imap_setfile1(&url
, fm
, 1);
4155 int rv
= 1, *msgvec
= vp
, *ip
;
4159 if (mb
.mb_type
!= MB_IMAP
) {
4160 n_err(_("Not connected to an IMAP server\n"));
4163 if (cached_uidvalidity(&mb
) == 0) {
4164 n_err(_("The current mailbox is not cached\n"));
4169 for (ip
= msgvec
; *ip
; ++ip
) {
4170 mp
= &message
[*ip
- 1];
4171 if (!(mp
->m_content_info
& CI_HAVE_BODY
)) {
4184 disconnected(const char *file
)
4190 if (ok_blook(disconnected
)) {
4195 if (!url_parse(&url
, CPROTO_IMAP
, file
)) {
4199 rv
= (n_var_vlook(savecat("disconnected-", url
.url_u_h_p
.s
), FAL0
) != NULL
);
4207 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
4209 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
4217 while (PTRCMP(omp
, <, omessage
+ omsgCount
) &&
4218 PTRCMP(nmp
, <, message
+ msgCount
)) {
4219 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
4221 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
4223 if (omp
->m_uid
== nmp
->m_uid
) {
4224 hf
= nmp
->m_flag
& MHIDDEN
;
4225 if (transparent
&& mb
.mb_type
== MB_IMAP
)
4226 omp
->m_flag
&= ~MHIDDEN
;
4228 if (transparent
&& mb
.mb_type
== MB_CACHE
)
4229 nmp
[-1].m_flag
|= hf
;
4230 } else if (omp
->m_uid
< nmp
->m_uid
)
4237 prevdot
= newprevdot
;
4243 imap_read_date_time(const char *cp
)
4247 int i
, year
, month
, day
, hour
, minute
, second
, sign
= -1;
4250 /* "25-Jul-2004 15:33:44 +0200"
4252 * 0 5 10 15 20 25 */
4253 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
4255 day
= strtol(&cp
[1], NULL
, 10);
4257 if (ascncasecmp(&cp
[4], n_month_names
[i
], 3) == 0)
4259 if (n_month_names
[++i
][0] == '\0')
4263 year
= strtol(&cp
[8], NULL
, 10);
4264 hour
= strtol(&cp
[13], NULL
, 10);
4265 minute
= strtol(&cp
[16], NULL
, 10);
4266 second
= strtol(&cp
[19], NULL
, 10);
4267 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
4281 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
4284 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
4294 imap_make_date_time(time_t t
)
4298 si32_t y
, md
, th
, tm
, ts
;
4300 int tzdiff
, tzdiff_hour
, tzdiff_min
;
4305 if((t2
= mktime(gmtime(&t
))) == (time_t)-1){
4310 if((tmp
= localtime(&t
)) == NULL
){
4315 tzdiff_hour
= (int)(tzdiff
/ 60);
4316 tzdiff_min
= tzdiff_hour
% 60;
4318 if (tmp
->tm_isdst
> 0)
4321 if(n_UNLIKELY((y
= tmp
->tm_year
) < 0 || y
>= 9999/*SI32_MAX*/ - 1900)){
4323 mn
= n_month_names
[0];
4328 mn
= (tmp
->tm_mon
>= 0 && tmp
->tm_mon
<= 11)
4329 ? n_month_names
[tmp
->tm_mon
] : n_qm
;
4331 if((md
= tmp
->tm_mday
) < 1 || md
> 31)
4334 if((th
= tmp
->tm_hour
) < 0 || th
> 23)
4336 if((tm
= tmp
->tm_min
) < 0 || tm
> 59)
4338 if((ts
= tmp
->tm_sec
) < 0 || ts
> 60)
4342 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
4343 md
, mn
, y
, th
, tm
, ts
, tzdiff_hour
, tzdiff_min
);
4349 (protbase
)(char const *cp n_MEMORY_DEBUG_ARGS
)
4354 np
= n
= (n_autorec_alloc_from_pool
)(NULL
, strlen(cp
) +1
4355 n_MEMORY_DEBUG_ARGSCALL
);
4357 /* Just ignore the `is-system-mailbox' prefix XXX */
4358 if (cp
[0] == '%' && cp
[1] == ':')
4361 while (*cp
!= '\0') {
4362 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
4366 } else if (cp
[0] == '/')
4375 #endif /* HAVE_IMAP */