1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ IMAP v4r1 client following RFC 2060.
3 *@ CRAM-MD5 as of RFC 2195.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
10 * Gunnar Ritter. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Gunnar Ritter
23 * and his contributors.
24 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #ifndef HAVE_AMALGAMATION
46 # include <sys/socket.h>
50 # include <netinet/in.h>
52 # ifdef HAVE_ARPA_INET_H
53 # include <arpa/inet.h>
58 #define IMAP_ANSWER() \
60 if (mp->mb_type != MB_CACHE) {\
62 while (mp->mb_active & MB_COMD)\
63 ok = imap_answer(mp, 1);\
69 /* TODO IMAP_OUT() simply returns instead of doing "actioN" if imap_finish()
70 * TODO fails, which leaves behind leaks in, e.g., imap_append1()!
71 * TODO IMAP_XOUT() was added due to this, but (1) needs to be used everywhere
72 * TODO and (2) doesn't handle all I/O errors itself, yet, too.
73 * TODO I.e., that should be a function, not a macro ... or so.
74 * TODO This entire module needs MASSIVE work! */
75 #define IMAP_OUT(X,Y,ACTION) IMAP_XOUT(X, Y, ACTION, return STOP)
76 #define IMAP_XOUT(X,Y,ACTIONERR,ACTIONBAIL) \
78 if (mp->mb_type != MB_CACHE) {\
79 if (imap_finish(mp) == STOP) {\
82 if (options & OPT_VERBVERB)\
85 if (swrite(&mp->mb_sock, X) == STOP) {\
94 static struct record
{
95 struct record
*rec_next
;
96 unsigned long rec_count
;
121 static char *responded_tag
;
122 static char *responded_text
;
123 static char *responded_other_text
;
124 static long responded_other_number
;
130 MAILBOX_DATA_MAILBOX
,
135 MESSAGE_DATA_EXPUNGE
,
138 RESPONSE_OTHER_UNKNOWN
141 static enum list_attributes
{
143 LIST_NOINFERIORS
= 001,
149 static int list_hierarchy_delimiter
;
150 static char *list_name
;
153 struct list_item
*l_next
;
156 enum list_attributes l_attr
;
162 static char *imapbuf
; /* TODO not static, use pool */
163 static size_t imapbufsize
;
164 static sigjmp_buf imapjmp
;
165 static sighandler_type savealrm
;
166 static int imapkeepalive
;
167 static long had_exists
= -1;
168 static long had_expunge
= -1;
169 static long expunged_messages
;
170 static int volatile imaplock
;
171 static int same_imap_account
;
172 static bool_t _imap_rdonly
;
174 static void imap_delim_init(struct mailbox
*mp
, struct url
const *urlp
);
175 static char const *imap_path_normalize(struct mailbox
*mp
, char const *cp
);
176 /* Returns NULL on error */
177 static char *imap_path_quote(struct mailbox
*mp
, char const *cp
);
178 static void imap_other_get(char *pp
);
179 static void imap_response_get(const char **cp
);
180 static void imap_response_parse(void);
181 static enum okay
imap_answer(struct mailbox
*mp
, int errprnt
);
182 static enum okay
imap_parse_list(void);
183 static enum okay
imap_finish(struct mailbox
*mp
);
184 static void imap_timer_off(void);
185 static void imapcatch(int s
);
186 static void _imap_maincatch(int s
);
187 static enum okay
imap_noop1(struct mailbox
*mp
);
188 static void rec_queue(enum rec_type type
, unsigned long cnt
);
189 static enum okay
rec_dequeue(void);
190 static void rec_rmqueue(void);
191 static void imapalarm(int s
);
192 static enum okay
imap_preauth(struct mailbox
*mp
, struct url
const *urlp
);
193 static enum okay
imap_capability(struct mailbox
*mp
);
194 static enum okay
imap_auth(struct mailbox
*mp
, struct ccred
*ccred
);
196 static enum okay
imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
);
198 static enum okay
imap_login(struct mailbox
*mp
, struct ccred
*ccred
);
200 static enum okay
_imap_gssapi(struct mailbox
*mp
, struct ccred
*ccred
);
202 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
203 static void imap_init(struct mailbox
*mp
, int n
);
204 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
206 static bool_t
_imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
,
208 static int _imap_setfile1(struct url
*urlp
, enum fedit_mode fm
,
210 static int imap_fetchdata(struct mailbox
*mp
, struct message
*m
,
211 size_t expected
, int need
, const char *head
,
212 size_t headsize
, long headlines
);
213 static void imap_putstr(struct mailbox
*mp
, struct message
*m
,
214 const char *str
, const char *head
, size_t headsize
,
216 static enum okay
imap_get(struct mailbox
*mp
, struct message
*m
,
218 static void commitmsg(struct mailbox
*mp
, struct message
*to
,
219 struct message
*from
, enum havespec have
);
220 static enum okay
imap_fetchheaders(struct mailbox
*mp
, struct message
*m
,
222 static enum okay
imap_exit(struct mailbox
*mp
);
223 static enum okay
imap_delete(struct mailbox
*mp
, int n
, struct message
*m
,
225 static enum okay
imap_close(struct mailbox
*mp
);
226 static enum okay
imap_update(struct mailbox
*mp
);
227 static enum okay
imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
228 int c
, const char *sp
, int needstat
);
229 static enum okay
imap_unstore(struct message
*m
, int n
, const char *flag
);
230 static const char *tag(int new);
231 static char * imap_putflags(int f
);
232 static void imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
);
233 static enum okay
imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
234 off_t off1
, long xsize
, enum mflag flag
, time_t t
);
235 static enum okay
imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
);
236 static enum okay
imap_list1(struct mailbox
*mp
, const char *base
,
237 struct list_item
**list
, struct list_item
**lend
,
239 static enum okay
imap_list(struct mailbox
*mp
, const char *base
, int strip
,
241 static void dopr(FILE *fp
);
242 static enum okay
imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
,
244 static enum okay
imap_copyuid_parse(const char *cp
,
245 unsigned long *uidvalidity
, unsigned long *olduid
,
246 unsigned long *newuid
);
247 static enum okay
imap_appenduid_parse(const char *cp
,
248 unsigned long *uidvalidity
, unsigned long *uid
);
249 static enum okay
imap_copyuid(struct mailbox
*mp
, struct message
*m
,
251 static enum okay
imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
,
252 long off1
, long xsize
, long size
, long lines
, int flag
,
254 static enum okay
imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
);
255 #ifdef HAVE_IMAP_SEARCH
256 static enum okay
imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
257 const char *spec
, int f
);
259 static enum okay
imap_remove1(struct mailbox
*mp
, const char *name
);
260 static enum okay
imap_rename1(struct mailbox
*mp
, const char *old
,
262 static char * imap_strex(char const *cp
, char const **xp
);
263 static enum okay
check_expunged(void);
266 imap_delim_init(struct mailbox
*mp
, struct url
const *urlp
){
271 mp
->mb_imap_delim
[0] = '\0';
273 if((cp
= xok_vlook(imap_delim
, urlp
, OXM_ALL
)) != NULL
){
278 i
= sizeof(n_IMAP_DELIM
) -1;
282 if(i
< NELEM(mp
->mb_imap_delim
))
284 memcpy(&mb
.mb_imap_delim
[0], cp
, i
+1);
286 n_err(_("*imap-delim* for %s is too long: %s\n"),
287 urlp
->url_input
, cp
);
293 imap_path_normalize(struct mailbox
*mp
, char const *cp
){
294 char *rv_base
, *rv
, dc2
, dc
, c
, lc
;
298 /* Unless we operate in free fly, honour a non-set *imap-delim* to mean "use
299 * exactly what i have specified" */
300 dcp
= (mp
== NULL
) ? &n_IMAP_DELIM
[0] : &mp
->mb_imap_delim
[0];
301 dc2
= ((dc
= *dcp
) != '\0') ? *++dcp
: dc
;
303 /* Plain names don't need path quoting */
308 for(cpx
= cp
;; ++cpx
)
309 if((c
= *cpx
) == '\0')
312 if(strchr(n_IMAP_DELIM
, c
)){
318 else if(dc2
&& strchr(dcp
, c
) != NULL
)
321 /* And we don't need to reevaluate what we have seen yet */
322 i
= PTR2SIZE(cpx
- cp
);
323 rv
= rv_base
= salloc(i
+ (j
= strlen(cpx
) +1));
326 memcpy(&rv
[i
], cpx
, j
);
331 /* Squeeze adjacent delimiters, convert remain to dc */
332 for(lc
= '\0'; (c
= *cp
++) != '\0'; lc
= c
){
333 if(c
== dc
|| (dc2
&& strchr(dcp
, c
) != NULL
))
335 if(c
!= dc
|| lc
!= dc
)
347 imap_path_encode(char const *cp
, bool_t
*err_or_null
){
348 /* To a large extend inspired by dovecot(1) */
351 ui8_t
*be16p_base
, *be16p
;
357 if(err_or_null
== NULL
)
358 err_or_null
= &err_def
;
361 /* Is this a string that works out as "plain US-ASCII"? */
363 if((c
= cp
[l
]) == '\0')
365 else if(c
<= 0x1F || c
>= 0x7F || c
== '&')
370 /* We need to encode in mUTF-7! For that, we first have to convert the
371 * local charset to UTF-8, then convert all characters which need to be
372 * encoded (except plain "&") to UTF-16BE first, then that to mUTF-7.
373 * We can skip the UTF-8 conversion occasionally, however */
374 if(!(options
& OPT_UNICODE
)){
378 emsg
= N_("iconv(3) from locale charset to UTF-8 failed");
380 if((icd
= iconv_open("utf-8", charset_get_lc())) == (iconv_t
)-1)
383 out
.s
= NULL
, out
.l
= 0;
384 in
.s
= UNCONST(cp
); /* logical */
387 if((ir
= n_iconv_str(icd
, &out
, &in
, NULL
, FAL0
)) == 0)
388 cp
= savestrbuf(out
.s
, out
.l
);
398 * So: Why not start all over again?
401 /* Is this a string that works out as "plain US-ASCII"? */
403 if((c
= cp
[l
]) == '\0')
405 else if(c
<= 0x1F || c
>= 0x7F || c
== '&')
409 /* We need to encode, save what we have, encode the rest */
412 for(cp
+= l
, l
= 0; cp
[l
] != '\0'; ++l
)
414 be16p_base
= salloc((l
<< 1) +1); /* XXX use n_string, resize */
416 out
.s
= salloc(l_plain
+ (l
<< 2) +1); /* XXX use n_string, resize */
418 memcpy(out
.s
, &cp
[-l_plain
], out
.l
= l_plain
);
421 DBG( l_plain
+= (l
<< 2); )
428 out
.s
[out
.l
+ 0] = '&';
429 out
.s
[out
.l
+ 1] = '-';
431 }else if(c
> 0x1F && c
< 0x7F)
434 static char const mb64ct
[] =
435 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
438 /* Convert consecutive non-representables */
439 emsg
= N_("Invalid UTF-8 sequence, cannot convert to UTF-32");
441 for(be16p
= be16p_base
, --cp
, ++l
;;){
442 if((utf32
= n_utf8_to_utf32(&cp
, &l
)) == UI32_MAX
)
445 /* TODO S-CText: magic utf16 conversions */
447 be16p
[1] = utf32
& 0xFF;
448 be16p
[0] = (utf32
>>= 8, utf32
&= 0xFF);
453 s7e
= 0xD800u
| ((utf32
- 0x10000) >> 10);
454 be16p
[1] = s7e
& 0xFF;
455 be16p
[0] = (s7e
>>= 8, s7e
&= 0xFF);
456 s7e
= 0xDC00u
| ((utf32
- 0x10000) & 0x03FF);
457 be16p
[3] = s7e
& 0xFF;
458 be16p
[2] = (s7e
>>= 8, s7e
&= 0xFF);
464 if((c
= *cp
) > 0x1F && c
< 0x7F)
468 /* And then warp that UTF-16BE to mUTF-7 */
469 out
.s
[out
.l
++] = '&';
470 utf32
= (ui32_t
)PTR2SIZE(be16p
- be16p_base
);
473 for(; utf32
>= 3; be16p
+= 3, utf32
-= 3){
474 out
.s
[out
.l
+0] = mb64ct
[ be16p
[0] >> 2 ];
475 out
.s
[out
.l
+1] = mb64ct
[((be16p
[0] & 0x03) << 4) | (be16p
[1] >> 4)];
476 out
.s
[out
.l
+2] = mb64ct
[((be16p
[1] & 0x0F) << 2) | (be16p
[2] >> 6)];
477 out
.s
[out
.l
+3] = mb64ct
[ be16p
[2] & 0x3F];
481 out
.s
[out
.l
+ 0] = mb64ct
[be16p
[0] >> 2];
483 out
.s
[out
.l
+ 1] = mb64ct
[ (be16p
[0] & 0x03) << 4];
486 out
.s
[out
.l
+ 1] = mb64ct
[((be16p
[0] & 0x03) << 4) |
488 out
.s
[out
.l
+ 2] = mb64ct
[ (be16p
[1] & 0x0F) << 2];
492 out
.s
[out
.l
++] = '-';
496 assert(out
.l
<= l_plain
);
503 n_err(_("Cannot encode IMAP path %s\n %s\n"), cp
, V_(emsg
));
508 imap_path_decode(char const *path
, bool_t
*err_or_null
){
509 /* To a large extend inspired by dovecot(1) TODO use string */
512 ui8_t
*mb64p_base
, *mb64p
, *mb64xp
;
513 char const *emsg
, *cp
;
514 char *rv_base
, *rv
, c
;
518 if(err_or_null
== NULL
)
519 err_or_null
= &err_def
;
522 l
= l_orig
= strlen(path
);
523 rv
= rv_base
= salloc(l
<< 1);
524 memcpy(rv
, path
, l
+1);
526 /* xxx Don't check for invalid characters from malicious servers */
527 if(l
== 0 || (cp
= memchr(path
, '&', l
)) == NULL
)
532 emsg
= N_("Invalid mUTF-7 encoding");
533 i
= PTR2SIZE(cp
- path
);
539 if((c
= *cp
) != '&'){
540 if(c
<= 0x1F || c
>= 0x7F){
541 emsg
= N_("Invalid mUTF-7: unencoded control or 8-bit byte");
549 else if(*++cp
== '-'){
555 emsg
= N_("Invalid mUTF-7: incomplete input");
558 /* mUTF-7 -> UTF-16BE -> UTF-8 */
559 static ui8_t
const mb64dt
[256] = {
562 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
563 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
564 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,62, 63,XX
,XX
,XX
,
565 52,53,54,55, 56,57,58,59, 60,61,XX
,XX
, XX
,XX
,XX
,XX
,
566 XX
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
567 15,16,17,18, 19,20,21,22, 23,24,25,XX
, XX
,XX
,XX
,XX
,
568 XX
,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
569 41,42,43,44, 45,46,47,48, 49,50,51,XX
, XX
,XX
,XX
,XX
,
570 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
571 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
572 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
573 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
574 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
575 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
576 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
,
577 XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
, XX
,XX
,XX
,XX
580 if(mb64p_base
== NULL
)
581 mb64p_base
= salloc(l
);
583 /* Decode the mUTF-7 to what is indeed UTF-16BE */
584 for(mb64p
= mb64p_base
;;){
586 if((mb64p
[0] = mb64dt
[(ui8_t
)cp
[0]]) == XX
||
587 (mb64p
[1] = mb64dt
[(ui8_t
)cp
[1]]) == XX
)
596 if((*mb64p
++ = mb64dt
[(ui8_t
)c
]) == XX
)
602 if((c
= *cp
++) == '-')
604 if((*mb64p
++ = mb64dt
[(ui8_t
)c
]) == XX
)
608 if(l
> 0 && *cp
== '-'){
618 if(l
>= 2 && cp
[0] == '&' && cp
[1] != '-'){
619 emsg
= N_("Invalid mUTF-7, consecutive encoded sequences");
623 /* Yet halfway decoded mUTF-7, go remaining way to gain UTF-16BE */
624 i
= PTR2SIZE(mb64p
- mb64p_base
);
625 mb64p
= mb64xp
= mb64p_base
;
628 ui8_t ul
, u0
, u1
, u2
, u3
;
630 ul
= (i
>= 4) ? 4 : i
& 0x3;
634 u2
= (ul
< 3) ? 0 : mb64xp
[2];
635 u3
= (ul
< 4) ? 0 : mb64xp
[3];
637 *mb64p
++ = (u0
<<= 2) | (u1
>> 4);
640 *mb64p
++ = (u1
<<= 4) | (u2
>> 2);
643 *mb64p
++ = (u2
<<= 6, u2
&= 0xC0) | u3
;
646 /* UTF-16BE we convert to UTF-8 */
647 i
= PTR2SIZE(mb64p
- mb64p_base
);
649 emsg
= N_("Odd bytecount for UTF-16BE input");
653 /* TODO S-CText: magic utf16 conversions */
654 emsg
= N_("Invalid UTF-16BE encoding");
656 for(mb64p
= mb64p_base
; i
> 0;){
664 /* Not a surrogate? */
665 if(uhi
< 0xD800 || uhi
> 0xDFFF){
669 }else if(uhi
> 0xDBFF)
672 emsg
= N_("Incomplete UTF-16BE surrogate pair");
678 if(ulo
< 0xDC00 || ulo
> 0xDFFF){
687 utf32
= 0x10000u
+ uhi
;
692 utf32
= n_utf32_to_utf8(utf32
, rv
);
699 /* We can skip the UTF-8 conversion occasionally */
700 if(!(options
& OPT_UNICODE
)){
704 emsg
= N_("iconv(3) from UTF-8 to locale charset failed");
706 if((icd
= iconv_open(charset_get_lc(), "utf-8")) == (iconv_t
)-1)
709 out
.s
= NULL
, out
.l
= 0;
710 in
.l
= strlen(in
.s
= rv_base
);
711 if((ir
= n_iconv_str(icd
, &out
, &in
, NULL
, FAL0
)) == 0)
712 /* Because the length of this is unpredictable, copy*/
713 rv_base
= savestrbuf(out
.s
, out
.l
);
729 n_err(_("Cannot decode IMAP path %s\n %s\n"), path
, V_(emsg
));
730 memcpy(rv
= rv_base
, path
, ++l_orig
);
735 imap_path_quote(struct mailbox
*mp
, char const *cp
){
740 cp
= imap_path_normalize(mp
, cp
);
741 cp
= imap_path_encode(cp
, &err
);
742 rv
= err
? NULL
: imap_quotestr(cp
);
748 imap_other_get(char *pp
)
753 if (ascncasecmp(pp
, "FLAGS ", 6) == 0) {
755 response_other
= MAILBOX_DATA_FLAGS
;
756 } else if (ascncasecmp(pp
, "LIST ", 5) == 0) {
758 response_other
= MAILBOX_DATA_LIST
;
759 } else if (ascncasecmp(pp
, "LSUB ", 5) == 0) {
761 response_other
= MAILBOX_DATA_LSUB
;
762 } else if (ascncasecmp(pp
, "MAILBOX ", 8) == 0) {
764 response_other
= MAILBOX_DATA_MAILBOX
;
765 } else if (ascncasecmp(pp
, "SEARCH ", 7) == 0) {
767 response_other
= MAILBOX_DATA_SEARCH
;
768 } else if (ascncasecmp(pp
, "STATUS ", 7) == 0) {
770 response_other
= MAILBOX_DATA_STATUS
;
771 } else if (ascncasecmp(pp
, "CAPABILITY ", 11) == 0) {
773 response_other
= CAPABILITY_DATA
;
775 responded_other_number
= strtol(pp
, &xp
, 10);
778 if (ascncasecmp(xp
, "EXISTS\r\n", 8) == 0) {
779 response_other
= MAILBOX_DATA_EXISTS
;
780 } else if (ascncasecmp(xp
, "RECENT\r\n", 8) == 0) {
781 response_other
= MAILBOX_DATA_RECENT
;
782 } else if (ascncasecmp(xp
, "EXPUNGE\r\n", 9) == 0) {
783 response_other
= MESSAGE_DATA_EXPUNGE
;
784 } else if (ascncasecmp(xp
, "FETCH ", 6) == 0) {
786 response_other
= MESSAGE_DATA_FETCH
;
788 response_other
= RESPONSE_OTHER_UNKNOWN
;
790 responded_other_text
= pp
;
795 imap_response_get(const char **cp
)
798 if (ascncasecmp(*cp
, "OK ", 3) == 0) {
800 response_status
= RESPONSE_OK
;
801 } else if (ascncasecmp(*cp
, "NO ", 3) == 0) {
803 response_status
= RESPONSE_NO
;
804 } else if (ascncasecmp(*cp
, "BAD ", 4) == 0) {
806 response_status
= RESPONSE_BAD
;
807 } else if (ascncasecmp(*cp
, "PREAUTH ", 8) == 0) {
809 response_status
= RESPONSE_PREAUTH
;
810 } else if (ascncasecmp(*cp
, "BYE ", 4) == 0) {
812 response_status
= RESPONSE_BYE
;
814 response_status
= RESPONSE_OTHER
;
819 imap_response_parse(void)
821 static char *parsebuf
; /* TODO Use pool */
822 static size_t parsebufsize
;
824 const char *ip
= imapbuf
;
828 if (parsebufsize
< imapbufsize
)
829 parsebuf
= srealloc(parsebuf
, parsebufsize
= imapbufsize
);
830 memcpy(parsebuf
, imapbuf
, strlen(imapbuf
) + 1);
834 response_type
= RESPONSE_CONT
;
849 imap_response_get(&ip
);
850 pp
= &parsebuf
[ip
- imapbuf
];
851 switch (response_status
) {
853 response_type
= RESPONSE_FATAL
;
856 response_type
= RESPONSE_DATA
;
860 responded_tag
= parsebuf
;
861 while (*pp
&& *pp
!= ' ')
864 response_type
= RESPONSE_ILLEGAL
;
868 while (*pp
&& *pp
== ' ')
871 response_type
= RESPONSE_ILLEGAL
;
874 ip
= &imapbuf
[pp
- parsebuf
];
875 response_type
= RESPONSE_TAGGED
;
876 imap_response_get(&ip
);
877 pp
= &parsebuf
[ip
- imapbuf
];
880 if (response_type
!= RESPONSE_CONT
&& response_type
!= RESPONSE_ILLEGAL
&&
881 response_status
== RESPONSE_OTHER
)
887 imap_answer(struct mailbox
*mp
, int errprnt
)
894 if (mp
->mb_type
== MB_CACHE
)
898 if (sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
899 if (options
& OPT_VERBVERB
)
900 fputs(imapbuf
, stderr
);
901 imap_response_parse();
902 if (response_type
== RESPONSE_ILLEGAL
)
904 if (response_type
== RESPONSE_CONT
) {
908 if (response_status
== RESPONSE_OTHER
) {
909 if (response_other
== MAILBOX_DATA_EXISTS
) {
910 had_exists
= responded_other_number
;
911 rec_queue(REC_EXISTS
, responded_other_number
);
914 } else if (response_other
== MESSAGE_DATA_EXPUNGE
) {
915 rec_queue(REC_EXPUNGE
, responded_other_number
);
923 if (response_type
== RESPONSE_TAGGED
) {
924 if (asccasecmp(responded_tag
, tag(0)) == 0)
929 switch (response_status
) {
930 case RESPONSE_PREAUTH
:
931 mp
->mb_active
&= ~MB_PREAUTH
;
944 n_err(_("IMAP error: %s"), responded_text
);
946 case RESPONSE_UNKNOWN
: /* does not happen */
949 mp
->mb_active
= MB_NONE
;
957 if (response_status
!= RESPONSE_OTHER
&&
958 ascncasecmp(responded_text
, "[ALERT] ", 8) == 0)
959 n_err(_("IMAP alert: %s"), &responded_text
[8]);
961 mp
->mb_active
&= ~MB_COMD
;
964 mp
->mb_active
= MB_NONE
;
972 imap_parse_list(void)
980 cp
= responded_other_text
;
981 list_attributes
= LIST_NONE
;
983 while (*cp
&& *cp
!= ')') {
985 if (ascncasecmp(&cp
[1], "Noinferiors ", 12) == 0) {
986 list_attributes
|= LIST_NOINFERIORS
;
988 } else if (ascncasecmp(&cp
[1], "Noselect ", 9) == 0) {
989 list_attributes
|= LIST_NOSELECT
;
991 } else if (ascncasecmp(&cp
[1], "Marked ", 7) == 0) {
992 list_attributes
|= LIST_MARKED
;
994 } else if (ascncasecmp(&cp
[1], "Unmarked ", 9) == 0) {
995 list_attributes
|= LIST_UNMARKED
;
1007 list_hierarchy_delimiter
= EOF
;
1011 list_hierarchy_delimiter
= *cp
++ & 0377;
1012 if (cp
[0] != '"' || cp
[1] != ' ')
1015 } else if (cp
[0] == 'N' && cp
[1] == 'I' && cp
[2] == 'L' && cp
[3] == ' ') {
1016 list_hierarchy_delimiter
= EOF
;
1023 while (*cp
&& *cp
!= '\r')
1033 imap_finish(struct mailbox
*mp
)
1036 while (mp
->mb_sock
.s_fd
> 0 && mp
->mb_active
& MB_COMD
)
1043 imap_timer_off(void)
1046 if (imapkeepalive
> 0) {
1048 safe_signal(SIGALRM
, savealrm
);
1056 NYD_X
; /* Signal handler */
1059 n_err_sighdl(_("Interrupt\n"));
1060 siglongjmp(imapjmp
, 1);
1063 n_err_sighdl(_("Received SIGPIPE during IMAP operation\n"));
1069 _imap_maincatch(int s
)
1071 NYD_X
; /* Signal handler */
1073 if (interrupts
++ == 0) {
1074 n_err_sighdl(_("Interrupt\n"));
1081 imap_noop1(struct mailbox
*mp
)
1084 FILE *queuefp
= NULL
;
1087 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
1088 IMAP_OUT(o
, MB_COMD
, return STOP
)
1094 imap_fileof(char const *xcp
)
1096 char const *cp
= xcp
;
1101 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
1105 if (cp
[0] == '/' && state
== 1) {
1123 sighandler_type
volatile oldint
, oldpipe
;
1124 enum okay rv
= STOP
;
1127 if (mb
.mb_type
!= MB_IMAP
)
1131 if ((oldint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1132 safe_signal(SIGINT
, &_imap_maincatch
);
1133 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1134 if (sigsetjmp(imapjmp
, 1) == 0) {
1135 if (oldpipe
!= SIG_IGN
)
1136 safe_signal(SIGPIPE
, imapcatch
);
1138 rv
= imap_noop1(&mb
);
1140 safe_signal(SIGINT
, oldint
);
1141 safe_signal(SIGPIPE
, oldpipe
);
1151 rec_queue(enum rec_type rt
, unsigned long cnt
)
1156 rp
= scalloc(1, sizeof *rp
);
1158 rp
->rec_count
= cnt
;
1159 if (record
&& recend
) {
1160 recend
->rec_next
= rp
;
1163 record
= recend
= rp
;
1170 struct message
*omessage
;
1171 struct record
*rp
, *rq
;
1172 uiz_t exists
= 0, i
;
1173 enum okay rv
= STOP
;
1180 message
= smalloc((msgCount
+1) * sizeof *message
);
1182 memcpy(message
, omessage
, msgCount
* sizeof *message
);
1183 memset(&message
[msgCount
], 0, sizeof *message
);
1185 rp
= record
, rq
= NULL
;
1187 while (rp
!= NULL
) {
1188 switch (rp
->rec_type
) {
1190 exists
= rp
->rec_count
;
1193 if (rp
->rec_count
== 0) {
1197 if (rp
->rec_count
> (unsigned long)msgCount
) {
1198 if (exists
== 0 || rp
->rec_count
> exists
--)
1204 delcache(&mb
, &message
[rp
->rec_count
-1]);
1205 memmove(&message
[rp
->rec_count
-1], &message
[rp
->rec_count
],
1206 ((msgCount
- rp
->rec_count
+ 1) * sizeof *message
));
1208 /* If the message was part of a collapsed thread,
1209 * the m_collapsed field of one of its ancestors
1210 * should be incremented. It seems hardly possible
1211 * to do this with the current message structure,
1212 * though. The result is that a '+' may be shown
1213 * in the header summary even if no collapsed
1214 * children exists */
1225 record
= recend
= NULL
;
1226 if (rv
== OKAY
&& UICMP(z
, exists
, >, msgCount
)) {
1227 message
= srealloc(message
, (exists
+ 1) * sizeof *message
);
1228 memset(&message
[msgCount
], 0, (exists
- msgCount
+ 1) * sizeof *message
);
1229 for (i
= msgCount
; i
< exists
; ++i
)
1231 imap_flags(&mb
, msgCount
+1, exists
);
1250 for (rp
= record
; rp
!= NULL
;) {
1251 struct record
*tmp
= rp
;
1255 record
= recend
= NULL
;
1263 sighandler_type
volatile saveint
, savepipe
;
1264 NYD_X
; /* Signal handler */
1267 if (imaplock
++ == 0) {
1268 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1269 safe_signal(SIGINT
, &_imap_maincatch
);
1270 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1271 if (sigsetjmp(imapjmp
, 1)) {
1272 safe_signal(SIGINT
, saveint
);
1273 safe_signal(SIGPIPE
, savepipe
);
1276 if (savepipe
!= SIG_IGN
)
1277 safe_signal(SIGPIPE
, imapcatch
);
1278 if (imap_noop1(&mb
) != OKAY
) {
1279 safe_signal(SIGINT
, saveint
);
1280 safe_signal(SIGPIPE
, savepipe
);
1283 safe_signal(SIGINT
, saveint
);
1284 safe_signal(SIGPIPE
, savepipe
);
1287 alarm(imapkeepalive
);
1293 imap_preauth(struct mailbox
*mp
, struct url
const *urlp
)
1297 mp
->mb_active
|= MB_PREAUTH
;
1301 if (!mp
->mb_sock
.s_use_ssl
&& xok_blook(imap_use_starttls
, urlp
, OXM_ALL
)) {
1302 FILE *queuefp
= NULL
;
1305 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
1306 IMAP_OUT(o
, MB_COMD
, return STOP
)
1308 if (ssl_open(urlp
, &mp
->mb_sock
) != OKAY
)
1312 if (xok_blook(imap_use_starttls
, urlp
, OXM_ALL
)) {
1313 n_err(_("No SSL support compiled in\n"));
1318 imap_capability(mp
);
1323 imap_capability(struct mailbox
*mp
)
1326 FILE *queuefp
= NULL
;
1327 enum okay ok
= STOP
;
1331 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
1332 IMAP_OUT(o
, MB_COMD
, return STOP
)
1333 while (mp
->mb_active
& MB_COMD
) {
1334 ok
= imap_answer(mp
, 0);
1335 if (response_status
== RESPONSE_OTHER
&&
1336 response_other
== CAPABILITY_DATA
) {
1337 cp
= responded_other_text
;
1339 while (spacechar(*cp
))
1341 if (strncmp(cp
, "UIDPLUS", 7) == 0 && spacechar(cp
[7]))
1343 mp
->mb_flags
|= MB_UIDPLUS
;
1344 while (*cp
&& !spacechar(*cp
))
1353 imap_auth(struct mailbox
*mp
, struct ccred
*ccred
)
1358 if (!(mp
->mb_active
& MB_PREAUTH
)) {
1363 switch (ccred
->cc_authtype
) {
1364 case AUTHTYPE_LOGIN
:
1365 rv
= imap_login(mp
, ccred
);
1368 case AUTHTYPE_CRAM_MD5
:
1369 rv
= imap_cram_md5(mp
, ccred
);
1373 case AUTHTYPE_GSSAPI
:
1374 rv
= _imap_gssapi(mp
, ccred
);
1388 imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
)
1390 char o
[LINESIZE
], *cp
;
1391 FILE *queuefp
= NULL
;
1392 enum okay rv
= STOP
;
1395 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
1396 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
1398 if (response_type
!= RESPONSE_CONT
)
1401 cp
= cram_md5_string(&ccred
->cc_user
, &ccred
->cc_pass
, responded_text
);
1402 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
1403 while (mp
->mb_active
& MB_COMD
)
1404 rv
= imap_answer(mp
, 1);
1409 #endif /* HAVE_MD5 */
1412 imap_login(struct mailbox
*mp
, struct ccred
*ccred
)
1415 FILE *queuefp
= NULL
;
1416 enum okay rv
= STOP
;
1419 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
1420 tag(1), imap_quotestr(ccred
->cc_user
.s
), imap_quotestr(ccred
->cc_pass
.s
));
1421 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
1422 while (mp
->mb_active
& MB_COMD
)
1423 rv
= imap_answer(mp
, 1);
1430 # include "imap_gssapi.h"
1434 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
,
1438 char const *qname
, *cp
;
1447 if((qname
= imap_path_quote(mp
, mbx
)) == NULL
)
1452 mp
->mb_uidvalidity
= 0;
1453 snprintf(o
, sizeof o
, "%s %s %s\r\n", tag(1),
1454 (fm
& FEDIT_RDONLY
? "EXAMINE" : "SELECT"), qname
);
1455 IMAP_OUT(o
, MB_COMD
, ok
= STOP
;goto jleave
)
1456 while (mp
->mb_active
& MB_COMD
) {
1457 ok
= imap_answer(mp
, 1);
1458 if (response_status
!= RESPONSE_OTHER
&&
1459 (cp
= asccasestr(responded_text
, "[UIDVALIDITY ")) != NULL
)
1460 mp
->mb_uidvalidity
= atol(&cp
[13]);
1462 *cnt
= (had_exists
> 0) ? had_exists
: 0;
1463 if (response_status
!= RESPONSE_OTHER
&&
1464 ascncasecmp(responded_text
, "[READ-ONLY] ", 12) == 0)
1471 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
1474 FILE *queuefp
= NULL
;
1477 unsigned x
= X
, y
= Y
, n
;
1480 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
1481 IMAP_OUT(o
, MB_COMD
, return STOP
)
1482 while (mp
->mb_active
& MB_COMD
) {
1484 if (response_status
== RESPONSE_OTHER
&&
1485 response_other
== MESSAGE_DATA_FETCH
) {
1486 n
= responded_other_number
;
1494 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1499 imap_getflags(cp
, &cp
, &m
->m_flag
);
1502 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1503 m
->m_uid
= strtoul(&cp
[4], NULL
, 10);
1504 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1505 m
->m_flag
&= ~MHIDDEN
;
1508 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1510 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1513 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1515 IMAP_OUT(o
, MB_COMD
, return STOP
)
1516 while (mp
->mb_active
& MB_COMD
) {
1518 if (response_status
== RESPONSE_OTHER
&&
1519 response_other
== MESSAGE_DATA_FETCH
) {
1520 n
= responded_other_number
;
1526 if ((cp
= asccasestr(responded_other_text
, "RFC822.SIZE ")) != NULL
)
1527 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1528 if ((cp
= asccasestr(responded_other_text
, "INTERNALDATE ")) != NULL
)
1529 m
->m_time
= imap_read_date_time(&cp
[13]);
1534 for (n
= X
; n
<= Y
; ++n
) {
1535 putcache(mp
, &message
[n
-1]);
1543 imap_init(struct mailbox
*mp
, int n
)
1550 m
->m_flag
= MUSED
| MNOFROM
;
1557 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1559 struct message
*omessage
= 0;
1560 int i
, omsgCount
= 0;
1561 enum okay dequeued
= STOP
;
1564 if (nmail
|| transparent
) {
1566 omsgCount
= msgCount
;
1569 dequeued
= rec_dequeue();
1571 if (had_exists
>= 0) {
1572 if (dequeued
!= OKAY
)
1573 msgCount
= had_exists
;
1576 if (had_expunge
>= 0) {
1577 if (dequeued
!= OKAY
)
1578 msgCount
-= had_expunge
;
1582 if (nmail
&& expunged_messages
)
1583 printf("Expunged %ld message%s.\n", expunged_messages
,
1584 (expunged_messages
!= 1 ? "s" : ""));
1585 *prevcount
= omsgCount
- expunged_messages
;
1586 expunged_messages
= 0;
1588 fputs("IMAP error: Negative message count\n", stderr
);
1592 if (dequeued
!= OKAY
) {
1593 message
= scalloc(msgCount
+ 1, sizeof *message
);
1594 for (i
= 0; i
< msgCount
; i
++)
1596 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1599 imap_flags(mp
, 1, msgCount
);
1600 message
[msgCount
].m_size
= 0;
1601 message
[msgCount
].m_lines
= 0;
1604 if (nmail
|| transparent
)
1605 transflags(omessage
, omsgCount
, transparent
);
1612 imap_setfile(const char *xserver
, enum fedit_mode fm
)
1618 if (!url_parse(&url
, CPROTO_IMAP
, xserver
)) {
1622 if (!ok_blook(v15_compat
) &&
1623 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
1624 n_err(_("New-style URL used without *v15-compat* being set!\n"));
1626 _imap_rdonly
= ((fm
& FEDIT_RDONLY
) != 0);
1627 rv
= _imap_setfile1(&url
, fm
, 0);
1634 _imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
, struct url
*urlp
)
1639 if (ok_blook(v15_compat
))
1640 rv
= ccred_lookup(ccredp
, urlp
);
1643 *xuhp
= (urlp
->url_had_user
? urlp
->url_eu_h_p
.s
: urlp
->url_u_h_p
.s
);
1645 if ((var
= mbp
->mb_imap_pass
) != NULL
) {
1646 var
= savecat("password-", xuhp
);
1647 if ((old
= vok_vlook(var
)) != NULL
)
1649 vok_vset(var
, mbp
->mb_imap_pass
);
1651 rv
= ccred_lookup_old(ccredp
, CPROTO_IMAP
, xuhp
);
1666 _imap_setfile1(struct url
*urlp
, enum fedit_mode
volatile fm
,
1667 int volatile transparent
)
1671 sighandler_type
volatile saveint
, savepipe
;
1674 int volatile prevcount
= 0;
1675 enum mbflags same_flags
;
1678 if (fm
& FEDIT_NEWMAIL
) {
1679 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1680 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1681 if (saveint
!= SIG_IGN
)
1682 safe_signal(SIGINT
, imapcatch
);
1683 if (savepipe
!= SIG_IGN
)
1684 safe_signal(SIGPIPE
, imapcatch
);
1689 same_flags
= mb
.mb_flags
;
1690 same_imap_account
= 0;
1691 if (mb
.mb_imap_account
!= NULL
&&
1692 (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)) {
1693 if (mb
.mb_sock
.s_fd
> 0 && mb
.mb_sock
.s_rsz
>= 0 &&
1694 !strcmp(mb
.mb_imap_account
, urlp
->url_p_eu_h_p
) &&
1695 disconnected(mb
.mb_imap_account
) == 0) {
1696 same_imap_account
= 1;
1697 if (urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1700 } else if ((transparent || mb.mb_type == MB_CACHE) &&
1701 !strcmp(mb.mb_imap_account, urlp->url_p_eu_h_p) &&
1702 urlp->url_pass.s == NULL && mb.mb_imap_pass != NULL)
1705 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= savestr(mb
.mb_imap_pass
));
1709 if (!same_imap_account
&& mb
.mb_imap_pass
!= NULL
) {
1710 free(mb
.mb_imap_pass
);
1711 mb
.mb_imap_pass
= NULL
;
1713 if (!_imap_getcred(&mb
, &ccred
, urlp
)) {
1719 if (!same_imap_account
) {
1720 if (!disconnected(urlp
->url_p_eu_h_p
) && !sopen(&so
, urlp
)) {
1729 if (fm
& FEDIT_SYSBOX
)
1733 if (mb
.mb_imap_account
!= NULL
)
1734 free(mb
.mb_imap_account
);
1735 if (mb
.mb_imap_pass
!= NULL
)
1736 free(mb
.mb_imap_pass
);
1737 mb
.mb_imap_account
= sstrdup(urlp
->url_p_eu_h_p
);
1738 /* TODO This is a hack to allow '@boxname'; in the end everything will be an
1739 * TODO object, and mailbox will naturally have an URL and credentials */
1740 mb
.mb_imap_pass
= sbufdup(ccred
.cc_pass
.s
, ccred
.cc_pass
.l
);
1742 if (!same_imap_account
) {
1743 if (mb
.mb_sock
.s_fd
>= 0)
1744 sclose(&mb
.mb_sock
);
1746 same_imap_account
= 0;
1757 if (mb
.mb_imap_mailbox
!= NULL
)
1758 free(mb
.mb_imap_mailbox
);
1759 assert(urlp
->url_path
.s
!= NULL
);
1760 imap_delim_init(&mb
, urlp
);
1761 mb
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&mb
, urlp
->url_path
.s
));
1762 initbox(savecatsep(urlp
->url_p_eu_h_p
,
1763 (mb
.mb_imap_delim
[0] != '\0' ? mb
.mb_imap_delim
[0] : n_IMAP_DELIM
[0]),
1764 mb
.mb_imap_mailbox
));
1766 mb
.mb_type
= MB_VOID
;
1767 mb
.mb_active
= MB_NONE
;
1770 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1771 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1772 if (sigsetjmp(imapjmp
, 1)) {
1773 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1774 sclose(&mb
.mb_sock
);
1775 safe_signal(SIGINT
, saveint
);
1776 safe_signal(SIGPIPE
, savepipe
);
1779 mb
.mb_type
= MB_VOID
;
1780 mb
.mb_active
= MB_NONE
;
1781 rv
= (fm
& (FEDIT_SYSBOX
| FEDIT_NEWMAIL
)) ? 1 : -1;
1784 if (saveint
!= SIG_IGN
)
1785 safe_signal(SIGINT
, imapcatch
);
1786 if (savepipe
!= SIG_IGN
)
1787 safe_signal(SIGPIPE
, imapcatch
);
1789 if (mb
.mb_sock
.s_fd
< 0) {
1790 if (disconnected(mb
.mb_imap_account
)) {
1791 if (cache_setptr(fm
, transparent
) == STOP
)
1792 n_err(_("Mailbox \"%s\" is not cached\n"), urlp
->url_p_eu_h_p_p
);
1795 if ((cp
= xok_vlook(imap_keepalive
, urlp
, OXM_ALL
)) != NULL
) {
1796 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1797 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1798 alarm(imapkeepalive
);
1803 mb
.mb_sock
.s_desc
= "IMAP";
1804 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1805 if (imap_preauth(&mb
, urlp
) != OKAY
|| imap_auth(&mb
, &ccred
) != OKAY
) {
1806 sclose(&mb
.mb_sock
);
1808 safe_signal(SIGINT
, saveint
);
1809 safe_signal(SIGPIPE
, savepipe
);
1811 rv
= (fm
& (FEDIT_SYSBOX
| FEDIT_NEWMAIL
)) ? 1 : -1;
1814 } else /* same account */
1815 mb
.mb_flags
|= same_flags
;
1817 if (options
& OPT_R_FLAG
)
1819 mb
.mb_perm
= (fm
& FEDIT_RDONLY
) ? 0 : MB_DELE
;
1820 mb
.mb_type
= MB_IMAP
;
1822 assert(urlp
->url_path
.s
!= NULL
);
1823 if (imap_select(&mb
, &mailsize
, &msgCount
, urlp
->url_path
.s
, fm
) != OKAY
) {
1824 /*sclose(&mb.mb_sock);
1826 safe_signal(SIGINT
, saveint
);
1827 safe_signal(SIGPIPE
, savepipe
);
1829 mb
.mb_type
= MB_VOID
;
1830 rv
= (fm
& (FEDIT_SYSBOX
| FEDIT_NEWMAIL
)) ? 1 : -1;
1835 imap_setptr(&mb
, ((fm
& FEDIT_NEWMAIL
) != 0), transparent
,
1836 UNVOLATILE(&prevcount
));
1839 if (!(fm
& FEDIT_NEWMAIL
) && !transparent
)
1840 pstate
&= ~PS_SAW_COMMAND
;
1841 safe_signal(SIGINT
, saveint
);
1842 safe_signal(SIGPIPE
, savepipe
);
1845 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_IMAP
)
1846 purgecache(&mb
, message
, msgCount
);
1847 if (((fm
& FEDIT_NEWMAIL
) || transparent
) && mb
.mb_sorted
) {
1852 if ((options
& OPT_EXISTONLY
) && (mb
.mb_type
== MB_IMAP
||
1853 mb
.mb_type
== MB_CACHE
)) {
1854 rv
= (msgCount
== 0);
1858 if (!(fm
& FEDIT_NEWMAIL
) && !(pstate
& PS_EDIT
) && msgCount
== 0) {
1859 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1860 !ok_blook(emptystart
))
1861 n_err(_("No mail at %s\n"), urlp
->url_p_eu_h_p_p
);
1866 if (fm
& FEDIT_NEWMAIL
)
1867 newmailinfo(prevcount
);
1875 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1876 int need
, const char *head
, size_t headsize
, long headlines
)
1878 char *line
= NULL
, *lp
;
1879 size_t linesize
= 0, linelen
, size
= 0;
1880 int emptyline
= 0, lines
= 0, excess
= 0;
1884 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1885 offset
= ftell(mp
->mb_otf
);
1888 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1890 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1892 if (linelen
> expected
) {
1893 excess
= linelen
- expected
;
1897 * Need to mask 'From ' lines. This cannot be done properly
1898 * since some servers pass them as 'From ' and others as
1899 * '>From '. Although one could identify the first kind of
1900 * server in principle, it is not possible to identify the
1901 * second as '>From ' may also come from a server of the
1902 * first type as actual data. So do what is absolutely
1903 * necessary only - mask 'From '.
1905 * If the line is the first line of the message header, it
1906 * is likely a real 'From ' line. In this case, it is just
1907 * ignored since it violates all standards.
1908 * TODO can the latter *really* happen??
1911 /* Since we simply copy over data without doing any transfer
1912 * encoding reclassification/adjustment we *have* to perform
1913 * RFC 4155 compliant From_ quoting here */
1914 if (emptyline
&& is_head(lp
, linelen
, FAL0
)) {
1915 fputc('>', mp
->mb_otf
);
1919 if (lp
[linelen
-1] == '\n' && (linelen
== 1 || lp
[linelen
-2] == '\r')) {
1921 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1922 size
+= linelen
- 1;
1927 fputc('\n', mp
->mb_otf
);
1929 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1933 if ((expected
-= linelen
) <= 0)
1937 /* This is very ugly; but some IMAP daemons don't end a
1938 * message with \r\n\r\n, and we need \n\n for mbox format */
1939 fputc('\n', mp
->mb_otf
);
1946 m
->m_size
= size
+ headsize
;
1947 m
->m_lines
= lines
+ headlines
;
1948 m
->m_block
= mailx_blockof(offset
);
1949 m
->m_offset
= mailx_offsetof(offset
);
1952 m
->m_have
|= HAVE_HEADER
;
1955 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1956 m
->m_xlines
= m
->m_lines
;
1957 m
->m_xsize
= m
->m_size
;
1967 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1968 const char *head
, size_t headsize
, long headlines
)
1975 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1976 offset
= ftell(mp
->mb_otf
);
1978 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1980 fwrite(str
, 1, len
, mp
->mb_otf
);
1981 fputc('\n', mp
->mb_otf
);
1987 m
->m_size
= headsize
+ len
;
1988 m
->m_lines
= headlines
+ 1;
1989 m
->m_block
= mailx_blockof(offset
);
1990 m
->m_offset
= mailx_offsetof(offset
);
1991 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1992 m
->m_xlines
= m
->m_lines
;
1993 m
->m_xsize
= m
->m_size
;
1999 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
2003 sighandler_type
volatile saveint
, savepipe
;
2004 char * volatile head
;
2005 char const *cp
, *loc
, * volatile item
, * volatile resp
;
2007 size_t volatile headsize
;
2010 long volatile headlines
;
2016 saveint
= savepipe
= SIG_IGN
;
2018 cp
= loc
= item
= resp
= NULL
;
2020 number
= (int)PTR2SIZE(m
- message
+ 1);
2027 if (getcache(mp
, m
, need
) == OKAY
)
2029 if (mp
->mb_type
== MB_CACHE
) {
2030 n_err(_("Message %lu not available\n"), (ul_i
)number
);
2034 if (mp
->mb_sock
.s_fd
< 0) {
2035 n_err(_("IMAP connection closed\n"));
2041 resp
= item
= "RFC822.HEADER";
2044 item
= "BODY.PEEK[]";
2046 if (m
->m_flag
& HAVE_HEADER
&& m
->m_size
) {
2047 char *hdr
= smalloc(m
->m_size
);
2049 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
2051 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
) != m
->m_size
) {
2056 headsize
= m
->m_size
;
2057 headlines
= m
->m_lines
;
2058 item
= "BODY.PEEK[TEXT]";
2059 resp
= "BODY[TEXT]";
2067 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2068 if (sigsetjmp(imapjmp
, 1)) {
2069 safe_signal(SIGINT
, saveint
);
2070 safe_signal(SIGPIPE
, savepipe
);
2074 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2075 safe_signal(SIGINT
, &_imap_maincatch
);
2076 if (savepipe
!= SIG_IGN
)
2077 safe_signal(SIGPIPE
, imapcatch
);
2080 snprintf(o
, sizeof o
, "%s UID FETCH %lu (%s)\r\n",
2081 tag(1), m
->m_uid
, item
);
2083 if (check_expunged() == STOP
)
2085 snprintf(o
, sizeof o
, "%s FETCH %u (%s)\r\n", tag(1), number
, item
);
2087 IMAP_OUT(o
, MB_COMD
, goto out
)
2089 ok
= imap_answer(mp
, 1);
2092 if (response_status
!= RESPONSE_OTHER
||
2093 response_other
!= MESSAGE_DATA_FETCH
)
2095 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
2098 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
2106 n
= responded_other_number
;
2107 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
2108 if (m
->m_uid
? m
->m_uid
!= u
: n
!= number
)
2110 if ((cp
= strchr(loc
, '"')) != NULL
) {
2111 cp
= imap_unquotestr(cp
);
2112 imap_putstr(mp
, m
, cp
, head
, headsize
, headlines
);
2114 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
2115 m
->m_xlines
= m
->m_lines
;
2116 m
->m_xsize
= m
->m_size
;
2120 expected
= atol(&cp
[1]);
2121 if (m
->m_uid
? n
== 0 && m
->m_uid
!= u
: n
!= number
) {
2122 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
2126 imap_fetchdata(mp
, &mt
, expected
, need
, head
, headsize
, headlines
);
2128 commitmsg(mp
, m
, &mt
, mt
.m_have
);
2131 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
2132 if (options
& OPT_VERBVERB
)
2133 fputs(imapbuf
, stderr
);
2134 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
2136 if (u
== m
->m_uid
) {
2137 commitmsg(mp
, m
, &mt
, mt
.m_have
);
2144 while (mp
->mb_active
& MB_COMD
)
2145 ok
= imap_answer(mp
, 1);
2147 if (saveint
!= SIG_IGN
)
2148 safe_signal(SIGINT
, saveint
);
2149 if (savepipe
!= SIG_IGN
)
2150 safe_signal(SIGPIPE
, savepipe
);
2163 imap_header(struct message
*m
)
2168 rv
= imap_get(&mb
, m
, NEED_HEADER
);
2175 imap_body(struct message
*m
)
2180 rv
= imap_get(&mb
, m
, NEED_BODY
);
2186 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
2190 tomp
->m_size
= frommp
->m_size
;
2191 tomp
->m_lines
= frommp
->m_lines
;
2192 tomp
->m_block
= frommp
->m_block
;
2193 tomp
->m_offset
= frommp
->m_offset
;
2194 tomp
->m_have
= have
;
2195 if (have
& HAVE_BODY
) {
2196 tomp
->m_xlines
= frommp
->m_lines
;
2197 tomp
->m_xsize
= frommp
->m_size
;
2204 imap_fetchheaders(struct mailbox
*mp
, struct message
*m
, int bot
, int topp
)
2212 FILE *queuefp
= NULL
;
2217 snprintf(o
, sizeof o
, "%s UID FETCH %lu:%lu (RFC822.HEADER)\r\n",
2218 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
2220 if (check_expunged() == STOP
)
2222 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.HEADER)\r\n",
2225 IMAP_OUT(o
, MB_COMD
, return STOP
)
2229 ok
= imap_answer(mp
, 1);
2230 if (response_status
!= RESPONSE_OTHER
)
2232 if (response_other
!= MESSAGE_DATA_FETCH
)
2234 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0) {
2238 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
2240 expected
= atol(&cp
[1]);
2241 if (m
[bot
-1].m_uid
) {
2242 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
) {
2244 for (n
= bot
; n
<= topp
; n
++)
2245 if ((unsigned long)u
== m
[n
-1].m_uid
)
2248 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
2254 n
= responded_other_number
;
2255 if (n
<= 0 || n
> msgCount
) {
2256 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
2260 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
2261 if (n
>= 0 && !(m
[n
-1].m_have
& HAVE_HEADER
))
2262 commitmsg(mp
, &m
[n
-1], &mt
, HAVE_HEADER
);
2263 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
2264 if (options
& OPT_VERBVERB
)
2265 fputs(imapbuf
, stderr
);
2266 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
2268 for (n
= bot
; n
<= topp
; n
++)
2269 if ((unsigned long)u
== m
[n
-1].m_uid
)
2271 if (n
<= topp
&& !(m
[n
-1].m_have
& HAVE_HEADER
))
2272 commitmsg(mp
, &m
[n
-1], &mt
,HAVE_HEADER
);
2280 while (mp
->mb_active
& MB_COMD
)
2281 ok
= imap_answer(mp
, 1);
2286 imap_getheaders(int volatile bot
, int volatile topp
) /* TODO iterator!! */
2288 sighandler_type saveint
, savepipe
;
2289 /*enum okay ok = STOP;*/
2293 if (mb
.mb_type
== MB_CACHE
)
2297 if (topp
> msgCount
)
2299 for (i
= bot
; i
< topp
; i
++) {
2300 if (message
[i
-1].m_have
& HAVE_HEADER
||
2301 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
2306 for (i
= topp
; i
> bot
; i
--) {
2307 if (message
[i
-1].m_have
& HAVE_HEADER
||
2308 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
2317 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2318 safe_signal(SIGINT
, &_imap_maincatch
);
2319 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2320 if (sigsetjmp(imapjmp
, 1) == 0) {
2321 if (savepipe
!= SIG_IGN
)
2322 safe_signal(SIGPIPE
, imapcatch
);
2324 for (i
= bot
; i
<= topp
; i
+= chunk
) {
2325 int j
= i
+ chunk
- 1;
2327 if (visible(message
+ j
))
2328 /*ok = */imap_fetchheaders(&mb
, message
, i
, j
);
2330 onintr(0); /* XXX imaplock? */
2333 safe_signal(SIGINT
, saveint
);
2334 safe_signal(SIGPIPE
, savepipe
);
2339 __imap_exit(struct mailbox
*mp
)
2342 FILE *queuefp
= NULL
;
2345 mp
->mb_active
|= MB_BYE
;
2346 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
2347 IMAP_OUT(o
, MB_COMD
, return STOP
)
2353 imap_exit(struct mailbox
*mp
)
2358 rv
= __imap_exit(mp
);
2359 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
2360 free(mp
->mb_imap_pass
);
2361 free(mp
->mb_imap_account
);
2362 free(mp
->mb_imap_mailbox
);
2363 if (mp
->mb_cache_directory
!= NULL
)
2364 free(mp
->mb_cache_directory
);
2365 #ifndef HAVE_DEBUG /* TODO ASSERT LEGACY */
2366 mp
->mb_imap_account
=
2367 mp
->mb_imap_mailbox
=
2368 mp
->mb_cache_directory
= "";
2370 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
2371 mp
->mb_imap_mailbox
= NULL
;
2372 mp
->mb_cache_directory
= NULL
;
2375 sclose(&mp
->mb_sock
);
2381 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
2384 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
2385 if (mp
->mb_type
== MB_IMAP
)
2392 imap_close(struct mailbox
*mp
)
2395 FILE *queuefp
= NULL
;
2398 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
2399 IMAP_OUT(o
, MB_COMD
, return STOP
)
2405 imap_update(struct mailbox
*mp
)
2408 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
2411 if (!(pstate
& PS_EDIT
) && mp
->mb_perm
!= 0) {
2414 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
2415 if (m
->m_flag
& MBOX
)
2418 if (makembox() == STOP
)
2423 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
2424 if (mp
->mb_perm
== 0)
2426 else if (pstate
& PS_EDIT
)
2427 dodel
= ((m
->m_flag
& MDELETED
) != 0);
2429 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
2431 /* Fetch the result after around each 800 STORE commands
2432 * sent (approx. 32k data sent). Otherwise, servers will
2433 * try to flush the return queue at some point, leading
2434 * to a deadlock if we are still writing commands but not
2435 * reading their results */
2436 needstat
= stored
> 0 && stored
% 800 == 0;
2437 /* Even if this message has been deleted, continue
2438 * to set further flags. This is necessary to support
2439 * Gmail semantics, where "delete" actually means
2440 * "archive", and the flags are applied to the copy
2442 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
2443 imap_store(mp
, m
, m
-message
+1, '+', "\\Seen", needstat
);
2446 if (m
->m_flag
& MFLAG
) {
2447 imap_store(mp
, m
, m
-message
+1, '+', "\\Flagged", needstat
);
2450 if (m
->m_flag
& MUNFLAG
) {
2451 imap_store(mp
, m
, m
-message
+1, '-', "\\Flagged", needstat
);
2454 if (m
->m_flag
& MANSWER
) {
2455 imap_store(mp
, m
, m
-message
+1, '+', "\\Answered", needstat
);
2458 if (m
->m_flag
& MUNANSWER
) {
2459 imap_store(mp
, m
, m
-message
+1, '-', "\\Answered", needstat
);
2462 if (m
->m_flag
& MDRAFT
) {
2463 imap_store(mp
, m
, m
-message
+1, '+', "\\Draft", needstat
);
2466 if (m
->m_flag
& MUNDRAFT
) {
2467 imap_store(mp
, m
, m
-message
+1, '-', "\\Draft", needstat
);
2472 imap_delete(mp
, m
-message
+1, m
, needstat
);
2475 } else if (mp
->mb_type
!= MB_CACHE
||
2476 (!(pstate
& PS_EDIT
) &&
2477 !(m
->m_flag
& (MBOXED
| MSAVED
| MDELETED
))) ||
2478 (m
->m_flag
& (MBOXED
| MPRESERVE
| MTOUCH
)) ==
2479 (MPRESERVE
| MTOUCH
) ||
2480 ((pstate
& PS_EDIT
) && !(m
->m_flag
& MDELETED
)))
2482 if (m
->m_flag
& MNEW
) {
2484 m
->m_flag
|= MSTATUS
;
2491 for (m
= &message
[0]; PTRCMP(m
, <, message
+ msgCount
); ++m
)
2492 if (!(m
->m_flag
& MUNLINKED
) &&
2493 m
->m_flag
& (MBOXED
| MDELETED
| MSAVED
| MSTATUS
| MFLAG
|
2494 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
)) {
2499 /* XXX should be readonly (but our IMAP code is weird...) */
2500 if (!(options
& (OPT_EXISTONLY
| OPT_HEADERSONLY
| OPT_HEADERLIST
)) &&
2502 if ((gotcha
|| modflags
) && (pstate
& PS_EDIT
)) {
2503 printf(_("\"%s\" "), displayname
);
2504 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
2505 ? _("complete\n") : _("updated.\n"));
2506 } else if (held
&& !(pstate
& PS_EDIT
)) {
2508 printf(_("Held 1 message in %s\n"), displayname
);
2510 printf(_("Held %d messages in %s\n"), held
, displayname
);
2521 sighandler_type
volatile saveint
, savepipe
;
2524 if (mb
.mb_type
== MB_CACHE
) {
2529 if (mb
.mb_sock
.s_fd
< 0) {
2530 n_err(_("IMAP connection closed\n"));
2535 saveint
= safe_signal(SIGINT
, SIG_IGN
);
2536 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2537 if (sigsetjmp(imapjmp
, 1)) {
2538 safe_signal(SIGINT
, saveint
);
2539 safe_signal(SIGPIPE
, saveint
);
2543 if (saveint
!= SIG_IGN
)
2544 safe_signal(SIGINT
, imapcatch
);
2545 if (savepipe
!= SIG_IGN
)
2546 safe_signal(SIGPIPE
, imapcatch
);
2549 if (!same_imap_account
)
2552 safe_signal(SIGINT
, saveint
);
2553 safe_signal(SIGPIPE
, savepipe
);
2560 imap_store(struct mailbox
*mp
, struct message
*m
, int n
, int c
, const char *sp
,
2564 FILE *queuefp
= NULL
;
2567 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
2570 snprintf(o
, sizeof o
, "%s UID STORE %lu %cFLAGS (%s)\r\n",
2571 tag(1), m
->m_uid
, c
, sp
);
2573 if (check_expunged() == STOP
)
2575 snprintf(o
, sizeof o
, "%s STORE %u %cFLAGS (%s)\r\n", tag(1), n
, c
, sp
);
2577 IMAP_OUT(o
, MB_COMD
, return STOP
)
2581 mb
.mb_active
&= ~MB_COMD
;
2582 if (queuefp
!= NULL
)
2588 imap_undelete(struct message
*m
, int n
)
2593 rv
= imap_unstore(m
, n
, "\\Deleted");
2599 imap_unread(struct message
*m
, int n
)
2604 rv
= imap_unstore(m
, n
, "\\Seen");
2610 imap_unstore(struct message
*m
, int n
, const char *flag
)
2612 sighandler_type saveint
, savepipe
;
2613 enum okay rv
= STOP
;
2617 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2618 safe_signal(SIGINT
, &_imap_maincatch
);
2619 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2620 if (sigsetjmp(imapjmp
, 1) == 0) {
2621 if (savepipe
!= SIG_IGN
)
2622 safe_signal(SIGPIPE
, imapcatch
);
2624 rv
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2626 safe_signal(SIGINT
, saveint
);
2627 safe_signal(SIGPIPE
, savepipe
);
2645 snprintf(ts
, sizeof ts
, "T%lu", n
);
2651 c_imapcodec(void *v
){
2653 char const **argv
, *cp
, *res
;
2656 if(is_prefix(cp
= *(argv
= v
), "encode")){
2657 while((cp
= *++argv
) != NULL
){
2658 res
= imap_path_normalize(NULL
, cp
);
2659 res
= imap_path_encode(res
, &err
);
2660 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s%s (%" PRIuZ
" bytes)\n",
2661 cp
, strlen(cp
), (err
? "ERROR " : ""), res
, strlen(res
));
2663 }else if(is_prefix(cp
, "decode")){
2666 while((cp
= *++argv
) != NULL
){
2667 res
= imap_path_normalize(NULL
, cp
);
2668 res
= imap_path_decode(res
, &err
);
2669 in
.l
= strlen(in
.s
= UNCONST(res
)); /* logical */
2670 makeprint(&in
, &out
);
2671 printf(" in: %s (%" PRIuZ
" bytes)\nout: %s%s (%" PRIuZ
" bytes)\n",
2672 cp
, strlen(cp
), (err
? "ERROR " : ""), out
.s
, in
.l
);
2676 n_err(_("`imapcodec': invalid subcommand: %s\n"), *argv
);
2680 return (cp
!= NULL
? OKAY
: STOP
);
2684 c_imap_imap(void *vp
)
2687 sighandler_type saveint
, savepipe
;
2688 struct mailbox
*mp
= &mb
;
2689 FILE *queuefp
= NULL
;
2690 enum okay
volatile ok
= STOP
;
2693 if (mp
->mb_type
!= MB_IMAP
) {
2694 printf("Not operating on an IMAP mailbox.\n");
2698 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2699 safe_signal(SIGINT
, &_imap_maincatch
);
2700 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2701 if (sigsetjmp(imapjmp
, 1) == 0) {
2702 if (savepipe
!= SIG_IGN
)
2703 safe_signal(SIGPIPE
, imapcatch
);
2705 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2706 IMAP_OUT(o
, MB_COMD
, goto out
)
2707 while (mp
->mb_active
& MB_COMD
) {
2708 ok
= imap_answer(mp
, 0);
2709 fputs(responded_text
, stdout
);
2713 safe_signal(SIGINT
, saveint
);
2714 safe_signal(SIGPIPE
, savepipe
);
2723 imap_newmail(int nmail
)
2727 if (nmail
&& had_exists
< 0 && had_expunge
< 0) {
2733 if (had_exists
== msgCount
&& had_expunge
< 0)
2734 /* Some servers always respond with EXISTS to NOOP. If
2735 * the mailbox has been changed but the number of messages
2736 * has not, an EXPUNGE must also had been sent; otherwise,
2737 * nothing has changed */
2740 return (had_expunge
>= 0 ? 2 : (had_exists
>= 0 ? 1 : 0));
2744 imap_putflags(int f
)
2750 bp
= buf
= salloc(100);
2751 if (f
& (MREAD
| MFLAGGED
| MANSWERED
| MDRAFTED
)) {
2756 for (cp
= "\\Seen"; *cp
; cp
++)
2762 for (cp
= "\\Flagged"; *cp
; cp
++)
2765 if (f
& MANSWERED
) {
2768 for (cp
= "\\Answered"; *cp
; cp
++)
2774 for (cp
= "\\Draft"; *cp
; cp
++)
2786 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2789 while (*cp
!= ')') {
2791 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2793 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2795 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2797 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2799 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2801 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2813 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
, off_t off1
,
2814 long xsize
, enum mflag flag
, time_t t
)
2816 char o
[LINESIZE
], *buf
;
2817 size_t bufsize
, buflen
, cnt
;
2818 long size
, lines
, ysize
;
2830 if((qname
= imap_path_quote(mp
, name
)) == NULL
)
2833 if (mp
->mb_type
== MB_CACHE
) {
2834 queuefp
= cache_queue(mp
);
2835 if (queuefp
== NULL
) {
2842 buf
= smalloc(bufsize
= LINESIZE
);
2847 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2852 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2853 tag(1), qname
, imap_putflags(flag
), imap_make_date_time(t
), size
);
2854 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2855 while (mp
->mb_active
& MB_COMD
) {
2856 rv
= imap_answer(mp
, twice
);
2857 if (response_type
== RESPONSE_CONT
)
2861 if (mp
->mb_type
!= MB_CACHE
&& rv
== STOP
) {
2870 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2873 buf
[buflen
- 1] = '\r';
2875 if (mp
->mb_type
!= MB_CACHE
)
2876 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2878 fwrite(buf
, 1, buflen
+1, queuefp
);
2881 if (mp
->mb_type
!= MB_CACHE
)
2882 swrite(&mp
->mb_sock
, "\r\n");
2884 fputs("\r\n", queuefp
);
2885 while (mp
->mb_active
& MB_COMD
) {
2886 rv
= imap_answer(mp
, 0);
2887 if (response_status
== RESPONSE_NO
/*&&
2888 ascncasecmp(responded_text,
2889 "[TRYCREATE] ", 12) == 0*/) {
2896 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2897 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2898 while (mp
->mb_active
& MB_COMD
)
2899 rv
= imap_answer(mp
, 1);
2902 imap_created_mailbox
++;
2904 } else if (rv
!= OKAY
)
2905 n_err(_("IMAP error: %s"), responded_text
);
2906 else if (response_status
== RESPONSE_OK
&& (mp
->mb_flags
& MB_UIDPLUS
))
2907 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
, flag
, name
);
2910 if (queuefp
!= NULL
)
2919 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2921 char *buf
, *bp
, *lp
;
2922 size_t bufsize
, buflen
, cnt
;
2923 off_t off1
= -1, offs
;
2925 enum {_NONE
= 0, _INHEAD
= 1<<0, _NLSEP
= 1<<1} state
;
2931 buf
= smalloc(bufsize
= LINESIZE
);
2938 for (flag
= MNEW
, state
= _NLSEP
;;) {
2939 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2942 ((state
& (_INHEAD
| _NLSEP
)) == _NLSEP
&&
2943 is_head(buf
, buflen
, FAL0
))) {
2944 if (off1
!= (off_t
)-1) {
2945 rv
= imap_append1(mp
, name
, fp
, off1
, size
, flag
, tim
);
2948 fseek(fp
, offs
+buflen
, SEEK_SET
);
2950 off1
= offs
+ buflen
;
2956 tim
= unixtime(buf
);
2962 if (buf
[0] == '\n') {
2965 } else if (state
& _INHEAD
) {
2966 if (ascncasecmp(buf
, "status", 6) == 0) {
2968 while (whitechar(*lp
))
2971 while (*++lp
!= '\0')
2980 } else if (ascncasecmp(buf
, "x-status", 8) == 0) {
2982 while (whitechar(*lp
))
2985 while (*++lp
!= '\0')
3008 imap_append(const char *xserver
, FILE *fp
)
3010 sighandler_type
volatile saveint
, savepipe
;
3013 enum okay rv
= STOP
;
3016 if (!url_parse(&url
, CPROTO_IMAP
, xserver
))
3018 if (!ok_blook(v15_compat
) &&
3019 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
3020 n_err(_("New-style URL used without *v15-compat* being set!\n"));
3021 assert(url
.url_path
.s
!= NULL
);
3024 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3025 safe_signal(SIGINT
, &_imap_maincatch
);
3026 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3027 if (sigsetjmp(imapjmp
, 1))
3029 if (savepipe
!= SIG_IGN
)
3030 safe_signal(SIGPIPE
, imapcatch
);
3032 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) && mb
.mb_imap_account
&&
3033 !strcmp(url
.url_p_eu_h_p
, mb
.mb_imap_account
)) {
3034 rv
= imap_append0(&mb
, url
.url_path
.s
, fp
);
3038 memset(&mx
, 0, sizeof mx
);
3040 if (!_imap_getcred(&mx
, &ccred
, &url
))
3043 imap_delim_init(&mx
, &url
);
3044 mx
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&mx
, url
.url_path
.s
));
3046 if (disconnected(url
.url_p_eu_h_p
) == 0) {
3047 if (!sopen(&mx
.mb_sock
, &url
))
3049 mx
.mb_sock
.s_desc
= "IMAP";
3050 mx
.mb_type
= MB_IMAP
;
3051 mx
.mb_imap_account
= UNCONST(url
.url_p_eu_h_p
);
3052 /* TODO the code now did
3053 * TODO mx.mb_imap_mailbox = mbx->url.url_patth.s;
3054 * TODO though imap_mailbox is sfree()d and mbx
3055 * TODO is possibly even a constant
3056 * TODO i changed this to sstrdup() sofar, as is used
3057 * TODO somewhere else in this file for this! */
3058 if (imap_preauth(&mx
, &url
) != OKAY
||
3059 imap_auth(&mx
, &ccred
) != OKAY
) {
3060 sclose(&mx
.mb_sock
);
3063 rv
= imap_append0(&mx
, url
.url_path
.s
, fp
);
3066 mx
.mb_imap_account
= UNCONST(url
.url_p_eu_h_p
);
3067 mx
.mb_type
= MB_CACHE
;
3068 rv
= imap_append0(&mx
, url
.url_path
.s
, fp
);
3075 safe_signal(SIGINT
, saveint
);
3076 safe_signal(SIGPIPE
, savepipe
);
3086 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
3087 struct list_item
**lend
, int level
)
3089 char o
[LINESIZE
], *cp
;
3090 struct list_item
*lp
;
3091 const char *qname
, *bp
;
3099 if((qname
= imap_path_quote(mp
, base
)) == NULL
)
3102 *list
= *lend
= NULL
;
3103 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n", tag(1), qname
);
3104 IMAP_OUT(o
, MB_COMD
, goto jleave
)
3105 while (mp
->mb_active
& MB_COMD
) {
3106 ok
= imap_answer(mp
, 1);
3107 if (response_status
== RESPONSE_OTHER
&&
3108 response_other
== MAILBOX_DATA_LIST
&& imap_parse_list() == OKAY
) {
3109 cp
= imap_path_decode(imap_unquotestr(list_name
), NULL
);
3110 lp
= csalloc(1, sizeof *lp
);
3112 for (bp
= base
; *bp
!= '\0' && *bp
== *cp
; ++bp
)
3114 lp
->l_base
= *cp
? cp
: savestr(base
);
3115 lp
->l_attr
= list_attributes
;
3116 lp
->l_level
= level
+1;
3117 lp
->l_delim
= list_hierarchy_delimiter
;
3118 if (*list
&& *lend
) {
3119 (*lend
)->l_next
= lp
;
3130 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
3132 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
3139 depth
= (cp
= ok_vlook(imap_list_depth
)) != NULL
? atoi(cp
) : 2;
3140 if ((rv
= imap_list1(mp
, base
, &list
, &lend
, 0)) == STOP
)
3143 if (list
== NULL
|| lend
== NULL
)
3146 for (lp
= list
; lp
; lp
= lp
->l_next
)
3147 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&& lp
->l_level
< depth
&&
3148 !(lp
->l_attr
& LIST_NOINFERIORS
)) {
3149 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
3150 memcpy(cp
, lp
->l_name
, n
);
3151 cp
[n
] = lp
->l_delim
;
3153 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&& lx
&& ly
) {
3154 lp
->l_has_children
= 1;
3155 if (strcmp(cp
, lx
->l_name
) == 0)
3164 for (lp
= list
; lp
; lp
= lp
->l_next
) {
3167 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
3171 if (!(lp
->l_attr
& LIST_NOSELECT
))
3172 fprintf(fp
, "%s\n", *cp
? cp
: base
);
3173 else if (lp
->l_has_children
== 0)
3174 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
3175 (lp
->l_delim
!= EOF
? lp
->l_delim
: '\n'));
3183 imap_folders(const char * volatile name
, int strip
)
3185 sighandler_type saveint
, savepipe
;
3186 const char *fold
, *cp
, *sp
;
3190 cp
= protbase(name
);
3191 sp
= mb
.mb_imap_account
;
3192 if (sp
== NULL
|| strcmp(cp
, sp
)) {
3194 _("Cannot perform `folders' but when on the very IMAP "
3195 "account; the current one is\n `%s' -- "
3196 "try `folders @'\n"),
3197 (sp
!= NULL
? sp
: _("[NONE]")));
3201 fold
= imap_fileof(name
);
3202 if (options
& OPT_TTYOUT
) {
3203 if ((fp
= Ftmp(NULL
, "imapfold", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
3205 n_perr(_("tmpfile"), 0);
3212 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3213 safe_signal(SIGINT
, &_imap_maincatch
);
3214 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3215 if (sigsetjmp(imapjmp
, 1)) /* TODO imaplock? */
3217 if (savepipe
!= SIG_IGN
)
3218 safe_signal(SIGPIPE
, imapcatch
);
3220 if (mb
.mb_type
== MB_CACHE
)
3221 cache_list(&mb
, fold
, strip
, fp
);
3223 imap_list(&mb
, fold
, strip
, fp
);
3227 if (options
& OPT_TTYOUT
)
3233 if (options
& OPT_TTYOUT
) {
3238 n_err(_("Folder not found\n"));
3241 safe_signal(SIGINT
, saveint
);
3242 safe_signal(SIGPIPE
, savepipe
);
3243 if (options
& OPT_TTYOUT
)
3256 long n
= 0, mx
= 0, columns
, width
;
3260 if ((out
= Ftmp(NULL
, "imapdopr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
3262 n_perr(_("tmpfile"), 0);
3266 while ((c
= getc(fp
)) != EOF
) {
3277 if (mx
< width
/ 2) {
3278 columns
= width
/ (mx
+2);
3279 snprintf(o
, sizeof o
, "sort | pr -%lu -w%lu -t", columns
, width
);
3281 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
3282 run_command(XSHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
3283 page_or_print(out
, 0);
3290 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
3294 bool_t twice
, stored
;
3301 twice
= stored
= FAL0
;
3306 i
= strlen(name
= imap_fileof(name
));
3307 if(i
== 0 || (i
> 0 && name
[i
- 1] == '/'))
3308 name
= savecat(name
, "INBOX");
3309 if((qname
= imap_path_quote(mp
, name
)) == NULL
)
3313 if (mp
->mb_type
== MB_CACHE
) {
3314 if ((queuefp
= cache_queue(mp
)) == NULL
)
3319 /* Since it is not possible to set flags on the copy, recently
3320 * set flags must be set on the original to include it in the copy */
3321 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
))
3322 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
3323 if (m
->m_flag
&MFLAG
)
3324 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
3325 if (m
->m_flag
&MUNFLAG
)
3326 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
3327 if (m
->m_flag
&MANSWER
)
3328 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
3329 if (m
->m_flag
&MUNANSWER
)
3330 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
3331 if (m
->m_flag
&MDRAFT
)
3332 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
3333 if (m
->m_flag
&MUNDRAFT
)
3334 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
3337 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n", tag(1), m
->m_uid
, qname
);
3339 if (check_expunged() == STOP
)
3341 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n", tag(1), n
, qname
);
3343 IMAP_OUT(o
, MB_COMD
, goto out
)
3344 while (mp
->mb_active
& MB_COMD
)
3345 ok
= imap_answer(mp
, twice
);
3347 if (mp
->mb_type
== MB_IMAP
&& mp
->mb_flags
& MB_UIDPLUS
&&
3348 response_status
== RESPONSE_OK
)
3349 imap_copyuid(mp
, m
, name
);
3351 if (response_status
== RESPONSE_NO
&& !twice
) {
3352 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
3353 IMAP_OUT(o
, MB_COMD
, goto out
)
3354 while (mp
->mb_active
& MB_COMD
)
3355 ok
= imap_answer(mp
, 1);
3357 imap_created_mailbox
++;
3363 if (queuefp
!= NULL
)
3366 /* ... and reset the flag to its initial value so that the 'exit'
3367 * command still leaves the message unread */
3369 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
3370 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
3373 if (m
->m_flag
& MFLAG
) {
3374 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
3377 if (m
->m_flag
& MUNFLAG
) {
3378 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
3381 if (m
->m_flag
& MANSWER
) {
3382 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
3385 if (m
->m_flag
& MUNANSWER
) {
3386 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
3389 if (m
->m_flag
& MDRAFT
) {
3390 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
3393 if (m
->m_flag
& MUNDRAFT
) {
3394 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
3398 mp
->mb_active
|= MB_COMD
;
3399 (void)imap_finish(mp
);
3406 imap_copy(struct message
*m
, int n
, const char *name
)
3408 sighandler_type saveint
, savepipe
;
3409 enum okay rv
= STOP
;
3413 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3414 safe_signal(SIGINT
, &_imap_maincatch
);
3415 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3416 if (sigsetjmp(imapjmp
, 1) == 0) {
3417 if (savepipe
!= SIG_IGN
)
3418 safe_signal(SIGPIPE
, imapcatch
);
3420 rv
= imap_copy1(&mb
, m
, n
, name
);
3422 safe_signal(SIGINT
, saveint
);
3423 safe_signal(SIGPIPE
, savepipe
);
3433 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
3434 unsigned long *olduid
, unsigned long *newuid
)
3440 *uidvalidity
= strtoul(cp
, &xp
, 10);
3441 *olduid
= strtoul(xp
, &yp
, 10);
3442 *newuid
= strtoul(yp
, &zp
, 10);
3443 rv
= (*uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
3444 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']');
3450 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
3457 *uidvalidity
= strtoul(cp
, &xp
, 10);
3458 *uid
= strtoul(xp
, &yp
, 10);
3459 rv
= (*uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' && yp
> xp
&&
3466 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
3471 unsigned long uidvalidity
, olduid
, newuid
;
3477 memset(&xmb
, 0, sizeof xmb
);
3479 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
3480 imap_copyuid_parse(&cp
[9], &uidvalidity
, &olduid
, &newuid
) == STOP
)
3486 xmb
.mb_cache_directory
= NULL
;
3487 xmb
.mb_imap_account
= sstrdup(mp
->mb_imap_account
);
3488 xmb
.mb_imap_pass
= sstrdup(mp
->mb_imap_pass
);
3489 memcpy(&xmb
.mb_imap_delim
[0], &mp
->mb_imap_delim
[0],
3490 sizeof(xmb
.mb_imap_delim
));
3491 xmb
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&xmb
, name
));
3492 if (mp
->mb_cache_directory
!= NULL
)
3493 xmb
.mb_cache_directory
= sstrdup(mp
->mb_cache_directory
);
3494 xmb
.mb_uidvalidity
= uidvalidity
;
3498 memset(&xm
, 0, sizeof xm
);
3500 if ((rv
= getcache1(mp
, &xm
, NEED_UNSPEC
, 3)) != OKAY
)
3502 getcache(mp
, &xm
, NEED_HEADER
);
3503 getcache(mp
, &xm
, NEED_BODY
);
3505 if ((m
->m_flag
& HAVE_HEADER
) == 0)
3506 getcache(mp
, m
, NEED_HEADER
);
3507 if ((m
->m_flag
& HAVE_BODY
) == 0)
3508 getcache(mp
, m
, NEED_BODY
);
3512 xm
.m_flag
&= ~MFULLYCACHED
;
3513 putcache(&xmb
, &xm
);
3515 if (xmb
.mb_cache_directory
!= NULL
)
3516 free(xmb
.mb_cache_directory
);
3517 if (xmb
.mb_imap_mailbox
!= NULL
)
3518 free(xmb
.mb_imap_mailbox
);
3519 if (xmb
.mb_imap_pass
!= NULL
)
3520 free(xmb
.mb_imap_pass
);
3521 if (xmb
.mb_imap_account
!= NULL
)
3522 free(xmb
.mb_imap_account
);
3528 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
, long xsize
,
3529 long size
, long lines
, int flag
, const char *name
)
3534 unsigned long uidvalidity
, uid
;
3540 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
3541 imap_appenduid_parse(&cp
[11], &uidvalidity
, &uid
) == STOP
)
3547 xmb
.mb_cache_directory
= NULL
;
3548 /* XXX mb_imap_delim reused */
3549 xmb
.mb_imap_mailbox
= sstrdup(imap_path_normalize(&xmb
, name
));
3550 xmb
.mb_uidvalidity
= uidvalidity
;
3551 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
3553 memset(&xm
, 0, sizeof xm
);
3554 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
3556 xm
.m_block
= mailx_blockof(off1
);
3557 xm
.m_offset
= mailx_offsetof(off1
);
3560 xm
.m_lines
= xm
.m_xlines
= lines
;
3562 xm
.m_have
= HAVE_HEADER
| HAVE_BODY
;
3563 putcache(&xmb
, &xm
);
3565 free(xmb
.mb_imap_mailbox
);
3572 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
3576 long size
, xsize
, ysize
, lines
;
3577 enum mflag flag
= MNEW
;
3578 char *name
, *buf
, *bp
;
3580 size_t bufsize
, buflen
, cnt
;
3581 enum okay rv
= STOP
;
3584 buf
= smalloc(bufsize
= LINESIZE
);
3587 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3590 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3595 if ((cp
= strrchr(bp
, '{')) == NULL
)
3598 xsize
= atol(&cp
[1]) + 2;
3599 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
3605 imap_getflags(cp
, &cp
, &flag
);
3606 while (*++cp
== ' ')
3609 t
= imap_read_date_time(cp
);
3611 if ((tp
= Ftmp(NULL
, "imapapui", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
3618 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3621 buf
[--buflen
] = '\0';
3622 buf
[buflen
-1] = '\n';
3623 fwrite(buf
, 1, buflen
, tp
);
3630 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
3631 imap_unquotestr(name
));
3641 #ifdef HAVE_IMAP_SEARCH
3643 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
, const char *spec
,
3646 char *o
, *xp
, *cs
, c
;
3648 FILE *queuefp
= NULL
;
3652 enum okay ok
= STOP
;
3656 for (cp
= spec
; *cp
; cp
++)
3659 cp
= charset_get_lc();
3661 if (asccasecmp(cp
, "utf-8") && asccasecmp(cp
, "utf8")) {
3666 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
3667 sz
= strlen(spec
) + 1;
3668 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
3669 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
, FAL0
) == 0 &&
3678 cp
= imap_quotestr(cp
);
3679 cs
= salloc(n
= strlen(cp
) + 10);
3680 snprintf(cs
, n
, "CHARSET %s ", cp
);
3684 o
= ac_alloc(osize
= strlen(spec
) + 60);
3685 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
3686 IMAP_OUT(o
, MB_COMD
, goto out
)
3687 while (mp
->mb_active
& MB_COMD
) {
3688 ok
= imap_answer(mp
, 0);
3689 if (response_status
== RESPONSE_OTHER
&&
3690 response_other
== MAILBOX_DATA_SEARCH
) {
3691 xp
= responded_other_text
;
3692 while (*xp
&& *xp
!= '\r') {
3693 n
= strtoul(xp
, &xp
, 10);
3694 for (i
= 0; i
< cnt
; i
++)
3695 if (m
[i
].m_uid
== n
&& !(m
[i
].m_flag
& MHIDDEN
) &&
3696 (f
== MDELETED
|| !(m
[i
].m_flag
& MDELETED
)))
3707 imap_search1(const char * volatile spec
, int f
)
3709 sighandler_type saveint
, savepipe
;
3710 enum okay
volatile rv
= STOP
;
3713 if (mb
.mb_type
!= MB_IMAP
)
3717 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3718 safe_signal(SIGINT
, &_imap_maincatch
);
3719 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3720 if (sigsetjmp(imapjmp
, 1) == 0) {
3721 if (savepipe
!= SIG_IGN
)
3722 safe_signal(SIGPIPE
, imapcatch
);
3724 rv
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
3726 safe_signal(SIGINT
, saveint
);
3727 safe_signal(SIGPIPE
, savepipe
);
3735 #endif /* HAVE_IMAP_SEARCH */
3738 imap_thisaccount(const char *cp
)
3743 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
3745 else if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
3746 mb
.mb_imap_account
== NULL
)
3749 rv
= !strcmp(protbase(cp
), mb
.mb_imap_account
);
3755 imap_remove(const char * volatile name
)
3757 sighandler_type
volatile saveint
, savepipe
;
3758 enum okay
volatile rv
= STOP
;
3761 if (mb
.mb_type
!= MB_IMAP
) {
3762 n_err(_("Refusing to remove \"%s\" in disconnected mode\n"), name
);
3766 if (!imap_thisaccount(name
)) {
3767 n_err(_("Can only remove mailboxes on current IMAP server: "
3768 "\"%s\" not removed\n"), name
);
3773 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3774 safe_signal(SIGINT
, &_imap_maincatch
);
3775 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3776 if (sigsetjmp(imapjmp
, 1) == 0) {
3777 if (savepipe
!= SIG_IGN
)
3778 safe_signal(SIGPIPE
, imapcatch
);
3780 rv
= imap_remove1(&mb
, imap_fileof(name
));
3782 safe_signal(SIGINT
, saveint
);
3783 safe_signal(SIGPIPE
, savepipe
);
3787 rv
= cache_remove(name
);
3796 imap_remove1(struct mailbox
*mp
, const char *name
)
3808 if((qname
= imap_path_quote(mp
, name
)) != NULL
){
3809 o
= ac_alloc(os
= strlen(qname
) + 100);
3810 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), qname
);
3811 IMAP_OUT(o
, MB_COMD
, goto out
)
3812 while (mp
->mb_active
& MB_COMD
)
3813 ok
= imap_answer(mp
, 1);
3821 imap_rename(const char *old
, const char *new)
3823 sighandler_type saveint
, savepipe
;
3824 enum okay rv
= STOP
;
3827 if (mb
.mb_type
!= MB_IMAP
) {
3828 n_err(_("Refusing to rename mailboxes in disconnected mode\n"));
3832 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3833 n_err(_("Can only rename mailboxes on current IMAP "
3834 "server: \"%s\" not renamed to \"%s\"\n"), old
, new);
3839 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3840 safe_signal(SIGINT
, &_imap_maincatch
);
3841 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3842 if (sigsetjmp(imapjmp
, 1) == 0) {
3843 if (savepipe
!= SIG_IGN
)
3844 safe_signal(SIGPIPE
, imapcatch
);
3846 rv
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3848 safe_signal(SIGINT
, saveint
);
3849 safe_signal(SIGPIPE
, savepipe
);
3853 rv
= cache_rename(old
, new);
3862 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3866 char const *qoname
, *qnname
;
3874 if((qoname
= imap_path_quote(mp
, old
)) != NULL
&&
3875 (qnname
= imap_path_quote(mp
, new)) != NULL
){
3876 o
= ac_alloc(os
= strlen(qoname
) + strlen(qnname
) + 100);
3877 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1), qoname
, qnname
);
3878 IMAP_OUT(o
, MB_COMD
, goto out
)
3879 while (mp
->mb_active
& MB_COMD
)
3880 ok
= imap_answer(mp
, 1);
3888 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3890 char o
[LINESIZE
], *newname
, *buf
, *bp
, *cp
, iob
[4096];
3891 size_t bufsize
, buflen
, cnt
;
3892 long offs
, offs1
, offs2
, octets
;
3893 int twice
, gotcha
= 0;
3894 FILE *queuefp
= NULL
;
3895 enum okay ok
= OKAY
, rok
= OKAY
;
3898 buf
= smalloc(bufsize
= LINESIZE
);
3901 while ((offs1
= ftell(fp
)) >= 0 &&
3902 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) != NULL
) {
3903 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3908 if ((offs
= ftell(fp
)) < 0)
3911 snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3912 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3914 while (digitchar(*cp
))
3920 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3922 IMAP_OUT(o
, MB_COMD
, continue)
3923 while (mp
->mb_active
& MB_COMD
)
3924 ok
= imap_answer(mp
, twice
);
3925 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3927 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3928 imap_copyuid(mp
, NULL
, imap_unquotestr(newname
));
3930 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3931 IMAP_OUT(o
, MB_COMD
, continue)
3932 while (mp
->mb_active
& MB_COMD
)
3933 ok
= imap_answer(mp
, 1);
3936 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3937 if ((cp
= strrchr(bp
, '{')) == NULL
)
3939 octets
= atol(&cp
[1]) + 2;
3940 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3942 IMAP_OUT(o
, MB_COMD
, continue)
3943 while (mp
->mb_active
& MB_COMD
) {
3944 ok
= imap_answer(mp
, twice
);
3945 if (response_type
== RESPONSE_CONT
)
3949 if (twice
++ == 0 && fseek(fp
, offs
, SEEK_SET
) >= 0)
3953 while (octets
> 0) {
3954 size_t n
= (UICMP(z
, octets
, >, sizeof iob
)
3955 ? sizeof iob
: (size_t)octets
);
3957 if (n
!= fread(iob
, 1, n
, fp
))
3959 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3961 swrite(&mp
->mb_sock
, "");
3962 while (mp
->mb_active
& MB_COMD
) {
3963 ok
= imap_answer(mp
, 0);
3964 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
3965 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3970 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3971 if ((offs2
= ftell(fp
)) < 0)
3973 fseek(fp
, offs1
, SEEK_SET
);
3974 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3975 (void)fseek(fp
, offs2
, SEEK_SET
);
3981 n_err(_("Invalid command in IMAP cache queue: \"%s\"\n"), bp
);
3986 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), newname
);
3987 IMAP_OUT(o
, MB_COMD
, continue)
3988 while (mp
->mb_active
& MB_COMD
)
3989 ok
= imap_answer(mp
, 1);
3995 ftruncate(fileno(fp
), 0);
4003 imap_strex(char const *cp
, char const **xp
)
4012 for (cq
= cp
+ 1; *cq
!= '\0'; ++cq
) {
4015 else if (*cq
== '"')
4021 n
= salloc(cq
- cp
+ 2);
4022 memcpy(n
, cp
, cq
- cp
+1);
4023 n
[cq
- cp
+ 1] = '\0';
4032 check_expunged(void)
4037 if (expunged_messages
> 0) {
4038 n_err(_("Command not executed - messages have been expunged\n"));
4047 c_connect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
4050 int rv
, omsgCount
= msgCount
;
4054 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
4055 n_err(_("Already connected\n"));
4060 if (!url_parse(&url
, CPROTO_IMAP
, mailname
)) {
4064 ok_bclear(disconnected
);
4065 vok_bclear(savecat("disconnected-", url
.url_u_h_p
.s
));
4067 if (mb
.mb_type
== MB_CACHE
) {
4068 enum fedit_mode fm
= FEDIT_NONE
;
4071 if (!(pstate
& PS_EDIT
))
4073 _imap_setfile1(&url
, fm
, 1);
4074 if (msgCount
> omsgCount
)
4075 newmailinfo(omsgCount
);
4084 c_disconnect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
4087 int rv
= 1, *msgvec
= vp
;
4090 if (mb
.mb_type
== MB_CACHE
) {
4091 n_err(_("Not connected\n"));
4094 if (mb
.mb_type
!= MB_IMAP
|| cached_uidvalidity(&mb
) == 0) {
4095 n_err(_("The current mailbox is not cached\n"));
4099 if (!url_parse(&url
, CPROTO_IMAP
, mailname
))
4104 ok_bset(disconnected
, TRU1
);
4105 if (mb
.mb_type
== MB_IMAP
) {
4106 enum fedit_mode fm
= FEDIT_NONE
;
4109 if (!(pstate
& PS_EDIT
))
4111 sclose(&mb
.mb_sock
);
4112 _imap_setfile1(&url
, fm
, 1);
4123 int rv
= 1, *msgvec
= vp
, *ip
;
4127 if (mb
.mb_type
!= MB_IMAP
) {
4128 n_err(_("Not connected to an IMAP server\n"));
4131 if (cached_uidvalidity(&mb
) == 0) {
4132 n_err(_("The current mailbox is not cached\n"));
4137 for (ip
= msgvec
; *ip
; ++ip
) {
4138 mp
= &message
[*ip
- 1];
4139 if (!(mp
->m_have
& HAVE_BODY
)) {
4152 disconnected(const char *file
)
4158 if (ok_blook(disconnected
)) {
4163 if (!url_parse(&url
, CPROTO_IMAP
, file
)) {
4167 rv
= vok_blook(savecat("disconnected-", url
.url_u_h_p
.s
));
4175 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
4177 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
4185 while (PTRCMP(omp
, <, omessage
+ omsgCount
) &&
4186 PTRCMP(nmp
, <, message
+ msgCount
)) {
4187 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
4189 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
4191 if (omp
->m_uid
== nmp
->m_uid
) {
4192 hf
= nmp
->m_flag
& MHIDDEN
;
4193 if (transparent
&& mb
.mb_type
== MB_IMAP
)
4194 omp
->m_flag
&= ~MHIDDEN
;
4196 if (transparent
&& mb
.mb_type
== MB_CACHE
)
4197 nmp
[-1].m_flag
|= hf
;
4198 } else if (omp
->m_uid
< nmp
->m_uid
)
4205 prevdot
= newprevdot
;
4211 imap_read_date_time(const char *cp
)
4215 int i
, year
, month
, day
, hour
, minute
, second
, sign
= -1;
4218 /* "25-Jul-2004 15:33:44 +0200"
4220 * 0 5 10 15 20 25 */
4221 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
4223 day
= strtol(&cp
[1], NULL
, 10);
4225 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
4227 if (month_names
[++i
][0] == '\0')
4231 year
= strtol(&cp
[8], NULL
, 10);
4232 hour
= strtol(&cp
[13], NULL
, 10);
4233 minute
= strtol(&cp
[16], NULL
, 10);
4234 second
= strtol(&cp
[19], NULL
, 10);
4235 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
4249 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
4252 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
4262 imap_make_date_time(time_t t
)
4266 int tzdiff
, tzdiff_hour
, tzdiff_min
;
4269 tzdiff
= t
- mktime(gmtime(&t
));
4270 tzdiff_hour
= (int)(tzdiff
/ 60);
4271 tzdiff_min
= tzdiff_hour
% 60;
4273 tmptr
= localtime(&t
);
4274 if (tmptr
->tm_isdst
> 0)
4276 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
4277 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
], tmptr
->tm_year
+ 1900,
4278 tmptr
->tm_hour
, tmptr
->tm_min
, tmptr
->tm_sec
, tzdiff_hour
, tzdiff_min
);
4282 #endif /* HAVE_IMAP */
4284 #if defined HAVE_IMAP || defined HAVE_IMAP_SEARCH
4286 imap_quotestr(char const *s
)
4291 np
= n
= salloc(2 * strlen(s
) + 3);
4294 if (*s
== '"' || *s
== '\\')
4305 imap_unquotestr(char const *s
)
4315 np
= n
= salloc(strlen(s
) + 1);
4328 #endif /* defined HAVE_IMAP || defined HAVE_IMAP_SEARCH */