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 - 2014 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)\
83 fprintf(stderr, ">>> %s", X);\
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_other_get(char *pp
);
175 static void imap_response_get(const char **cp
);
176 static void imap_response_parse(void);
177 static enum okay
imap_answer(struct mailbox
*mp
, int errprnt
);
178 static enum okay
imap_parse_list(void);
179 static enum okay
imap_finish(struct mailbox
*mp
);
180 static void imap_timer_off(void);
181 static void imapcatch(int s
);
182 static void _imap_maincatch(int s
);
183 static enum okay
imap_noop1(struct mailbox
*mp
);
184 static void rec_queue(enum rec_type type
, unsigned long cnt
);
185 static enum okay
rec_dequeue(void);
186 static void rec_rmqueue(void);
187 static void imapalarm(int s
);
188 static int imap_use_starttls(const char *uhp
);
189 static enum okay
imap_preauth(struct mailbox
*mp
, const char *xserver
,
191 static enum okay
imap_capability(struct mailbox
*mp
);
192 static enum okay
imap_auth(struct mailbox
*mp
, struct ccred
*ccred
);
194 static enum okay
imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
);
196 static enum okay
imap_login(struct mailbox
*mp
, struct ccred
*ccred
);
198 static enum okay
_imap_gssapi(struct mailbox
*mp
, struct ccred
*ccred
);
200 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
201 static void imap_init(struct mailbox
*mp
, int n
);
202 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
204 static bool_t
_imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
,
206 static int _imap_setfile1(struct url
*urlp
, enum fedit_mode fm
,
208 static int imap_fetchdata(struct mailbox
*mp
, struct message
*m
,
209 size_t expected
, int need
, const char *head
,
210 size_t headsize
, long headlines
);
211 static void imap_putstr(struct mailbox
*mp
, struct message
*m
,
212 const char *str
, const char *head
, size_t headsize
,
214 static enum okay
imap_get(struct mailbox
*mp
, struct message
*m
,
216 static void commitmsg(struct mailbox
*mp
, struct message
*to
,
217 struct message
*from
, enum havespec have
);
218 static enum okay
imap_fetchheaders(struct mailbox
*mp
, struct message
*m
,
220 static enum okay
imap_exit(struct mailbox
*mp
);
221 static enum okay
imap_delete(struct mailbox
*mp
, int n
, struct message
*m
,
223 static enum okay
imap_close(struct mailbox
*mp
);
224 static enum okay
imap_update(struct mailbox
*mp
);
225 static enum okay
imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
226 int c
, const char *sp
, int needstat
);
227 static enum okay
imap_unstore(struct message
*m
, int n
, const char *flag
);
228 static const char *tag(int new);
229 static char * imap_putflags(int f
);
230 static void imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
);
231 static enum okay
imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
232 off_t off1
, long xsize
, enum mflag flag
, time_t t
);
233 static enum okay
imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
);
234 static enum okay
imap_list1(struct mailbox
*mp
, const char *base
,
235 struct list_item
**list
, struct list_item
**lend
,
237 static enum okay
imap_list(struct mailbox
*mp
, const char *base
, int strip
,
239 static void dopr(FILE *fp
);
240 static enum okay
imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
,
242 static enum okay
imap_copyuid_parse(const char *cp
,
243 unsigned long *uidvalidity
, unsigned long *olduid
,
244 unsigned long *newuid
);
245 static enum okay
imap_appenduid_parse(const char *cp
,
246 unsigned long *uidvalidity
, unsigned long *uid
);
247 static enum okay
imap_copyuid(struct mailbox
*mp
, struct message
*m
,
249 static enum okay
imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
,
250 long off1
, long xsize
, long size
, long lines
, int flag
,
252 static enum okay
imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
);
253 #ifdef HAVE_IMAP_SEARCH
254 static enum okay
imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
255 const char *spec
, int f
);
257 static enum okay
imap_remove1(struct mailbox
*mp
, const char *name
);
258 static enum okay
imap_rename1(struct mailbox
*mp
, const char *old
,
260 static char * imap_strex(char const *cp
, char const **xp
);
261 static enum okay
check_expunged(void);
264 imap_other_get(char *pp
)
269 if (ascncasecmp(pp
, "FLAGS ", 6) == 0) {
271 response_other
= MAILBOX_DATA_FLAGS
;
272 } else if (ascncasecmp(pp
, "LIST ", 5) == 0) {
274 response_other
= MAILBOX_DATA_LIST
;
275 } else if (ascncasecmp(pp
, "LSUB ", 5) == 0) {
277 response_other
= MAILBOX_DATA_LSUB
;
278 } else if (ascncasecmp(pp
, "MAILBOX ", 8) == 0) {
280 response_other
= MAILBOX_DATA_MAILBOX
;
281 } else if (ascncasecmp(pp
, "SEARCH ", 7) == 0) {
283 response_other
= MAILBOX_DATA_SEARCH
;
284 } else if (ascncasecmp(pp
, "STATUS ", 7) == 0) {
286 response_other
= MAILBOX_DATA_STATUS
;
287 } else if (ascncasecmp(pp
, "CAPABILITY ", 11) == 0) {
289 response_other
= CAPABILITY_DATA
;
291 responded_other_number
= strtol(pp
, &xp
, 10);
294 if (ascncasecmp(xp
, "EXISTS\r\n", 8) == 0) {
295 response_other
= MAILBOX_DATA_EXISTS
;
296 } else if (ascncasecmp(xp
, "RECENT\r\n", 8) == 0) {
297 response_other
= MAILBOX_DATA_RECENT
;
298 } else if (ascncasecmp(xp
, "EXPUNGE\r\n", 9) == 0) {
299 response_other
= MESSAGE_DATA_EXPUNGE
;
300 } else if (ascncasecmp(xp
, "FETCH ", 6) == 0) {
302 response_other
= MESSAGE_DATA_FETCH
;
304 response_other
= RESPONSE_OTHER_UNKNOWN
;
306 responded_other_text
= pp
;
311 imap_response_get(const char **cp
)
314 if (ascncasecmp(*cp
, "OK ", 3) == 0) {
316 response_status
= RESPONSE_OK
;
317 } else if (ascncasecmp(*cp
, "NO ", 3) == 0) {
319 response_status
= RESPONSE_NO
;
320 } else if (ascncasecmp(*cp
, "BAD ", 4) == 0) {
322 response_status
= RESPONSE_BAD
;
323 } else if (ascncasecmp(*cp
, "PREAUTH ", 8) == 0) {
325 response_status
= RESPONSE_PREAUTH
;
326 } else if (ascncasecmp(*cp
, "BYE ", 4) == 0) {
328 response_status
= RESPONSE_BYE
;
330 response_status
= RESPONSE_OTHER
;
335 imap_response_parse(void)
337 static char *parsebuf
; /* TODO Use pool */
338 static size_t parsebufsize
;
340 const char *ip
= imapbuf
;
344 if (parsebufsize
< imapbufsize
)
345 parsebuf
= srealloc(parsebuf
, parsebufsize
= imapbufsize
);
346 memcpy(parsebuf
, imapbuf
, strlen(imapbuf
) + 1);
350 response_type
= RESPONSE_CONT
;
365 imap_response_get(&ip
);
366 pp
= &parsebuf
[ip
- imapbuf
];
367 switch (response_status
) {
369 response_type
= RESPONSE_FATAL
;
372 response_type
= RESPONSE_DATA
;
376 responded_tag
= parsebuf
;
377 while (*pp
&& *pp
!= ' ')
380 response_type
= RESPONSE_ILLEGAL
;
384 while (*pp
&& *pp
== ' ')
387 response_type
= RESPONSE_ILLEGAL
;
390 ip
= &imapbuf
[pp
- parsebuf
];
391 response_type
= RESPONSE_TAGGED
;
392 imap_response_get(&ip
);
393 pp
= &parsebuf
[ip
- imapbuf
];
396 if (response_type
!= RESPONSE_CONT
&& response_type
!= RESPONSE_ILLEGAL
&&
397 response_status
== RESPONSE_OTHER
)
403 imap_answer(struct mailbox
*mp
, int errprnt
)
410 if (mp
->mb_type
== MB_CACHE
)
414 if (sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
415 if (options
& OPT_VERBVERB
)
416 fputs(imapbuf
, stderr
);
417 imap_response_parse();
418 if (response_type
== RESPONSE_ILLEGAL
)
420 if (response_type
== RESPONSE_CONT
) {
424 if (response_status
== RESPONSE_OTHER
) {
425 if (response_other
== MAILBOX_DATA_EXISTS
) {
426 had_exists
= responded_other_number
;
427 rec_queue(REC_EXISTS
, responded_other_number
);
430 } else if (response_other
== MESSAGE_DATA_EXPUNGE
) {
431 rec_queue(REC_EXPUNGE
, responded_other_number
);
439 if (response_type
== RESPONSE_TAGGED
) {
440 if (asccasecmp(responded_tag
, tag(0)) == 0)
445 switch (response_status
) {
446 case RESPONSE_PREAUTH
:
447 mp
->mb_active
&= ~MB_PREAUTH
;
460 fprintf(stderr
, _("IMAP error: %s"), responded_text
);
462 case RESPONSE_UNKNOWN
: /* does not happen */
465 mp
->mb_active
= MB_NONE
;
473 if (response_status
!= RESPONSE_OTHER
&&
474 ascncasecmp(responded_text
, "[ALERT] ", 8) == 0)
475 fprintf(stderr
, "IMAP alert: %s", &responded_text
[8]);
477 mp
->mb_active
&= ~MB_COMD
;
480 mp
->mb_active
= MB_NONE
;
488 imap_parse_list(void)
496 cp
= responded_other_text
;
497 list_attributes
= LIST_NONE
;
499 while (*cp
&& *cp
!= ')') {
501 if (ascncasecmp(&cp
[1], "Noinferiors ", 12) == 0) {
502 list_attributes
|= LIST_NOINFERIORS
;
504 } else if (ascncasecmp(&cp
[1], "Noselect ", 9) == 0) {
505 list_attributes
|= LIST_NOSELECT
;
507 } else if (ascncasecmp(&cp
[1], "Marked ", 7) == 0) {
508 list_attributes
|= LIST_MARKED
;
510 } else if (ascncasecmp(&cp
[1], "Unmarked ", 9) == 0) {
511 list_attributes
|= LIST_UNMARKED
;
523 list_hierarchy_delimiter
= EOF
;
527 list_hierarchy_delimiter
= *cp
++ & 0377;
528 if (cp
[0] != '"' || cp
[1] != ' ')
531 } else if (cp
[0] == 'N' && cp
[1] == 'I' && cp
[2] == 'L' && cp
[3] == ' ') {
532 list_hierarchy_delimiter
= EOF
;
539 while (*cp
&& *cp
!= '\r')
549 imap_finish(struct mailbox
*mp
)
552 while (mp
->mb_sock
.s_fd
> 0 && mp
->mb_active
& MB_COMD
)
562 if (imapkeepalive
> 0) {
564 safe_signal(SIGALRM
, savealrm
);
572 NYD_X
; /* Signal handler */
575 fprintf(stderr
, _("Interrupt\n"));
576 siglongjmp(imapjmp
, 1);
579 fprintf(stderr
, _("Received SIGPIPE during IMAP operation\n"));
585 _imap_maincatch(int s
)
587 NYD_X
; /* Signal handler */
589 if (interrupts
++ == 0) {
590 fprintf(stderr
, _("Interrupt\n"));
597 imap_noop1(struct mailbox
*mp
)
600 FILE *queuefp
= NULL
;
603 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
604 IMAP_OUT(o
, MB_COMD
, return STOP
)
610 imap_fileof(char const *xcp
)
612 char const *cp
= xcp
;
617 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
621 if (cp
[0] == '/' && state
== 1) {
639 sighandler_type
volatile oldint
, oldpipe
;
643 if (mb
.mb_type
!= MB_IMAP
)
647 if ((oldint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
648 safe_signal(SIGINT
, &_imap_maincatch
);
649 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
650 if (sigsetjmp(imapjmp
, 1) == 0) {
651 if (oldpipe
!= SIG_IGN
)
652 safe_signal(SIGPIPE
, imapcatch
);
654 rv
= imap_noop1(&mb
);
656 safe_signal(SIGINT
, oldint
);
657 safe_signal(SIGPIPE
, oldpipe
);
667 rec_queue(enum rec_type rt
, unsigned long cnt
)
672 rp
= scalloc(1, sizeof *rp
);
675 if (record
&& recend
) {
676 recend
->rec_next
= rp
;
679 record
= recend
= rp
;
686 struct message
*omessage
;
687 struct record
*rp
, *rq
;
696 message
= smalloc((msgCount
+1) * sizeof *message
);
698 memcpy(message
, omessage
, msgCount
* sizeof *message
);
699 memset(&message
[msgCount
], 0, sizeof *message
);
701 rp
= record
, rq
= NULL
;
704 switch (rp
->rec_type
) {
706 exists
= rp
->rec_count
;
709 if (rp
->rec_count
== 0) {
713 if (rp
->rec_count
> (unsigned long)msgCount
) {
714 if (exists
== 0 || rp
->rec_count
> exists
--)
720 delcache(&mb
, &message
[rp
->rec_count
-1]);
721 memmove(&message
[rp
->rec_count
-1], &message
[rp
->rec_count
],
722 ((msgCount
- rp
->rec_count
+ 1) * sizeof *message
));
724 /* If the message was part of a collapsed thread,
725 * the m_collapsed field of one of its ancestors
726 * should be incremented. It seems hardly possible
727 * to do this with the current message structure,
728 * though. The result is that a '+' may be shown
729 * in the header summary even if no collapsed
741 record
= recend
= NULL
;
742 if (rv
== OKAY
&& UICMP(z
, exists
, >, msgCount
)) {
743 message
= srealloc(message
, (exists
+ 1) * sizeof *message
);
744 memset(&message
[msgCount
], 0, (exists
- msgCount
+ 1) * sizeof *message
);
745 for (i
= msgCount
; i
< exists
; ++i
)
747 imap_flags(&mb
, msgCount
+1, exists
);
766 for (rp
= record
; rp
!= NULL
;) {
767 struct record
*tmp
= rp
;
771 record
= recend
= NULL
;
779 sighandler_type
volatile saveint
, savepipe
;
780 NYD_X
; /* Signal handler */
783 if (imaplock
++ == 0) {
784 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
785 safe_signal(SIGINT
, &_imap_maincatch
);
786 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
787 if (sigsetjmp(imapjmp
, 1)) {
788 safe_signal(SIGINT
, saveint
);
789 safe_signal(SIGPIPE
, savepipe
);
792 if (savepipe
!= SIG_IGN
)
793 safe_signal(SIGPIPE
, imapcatch
);
794 if (imap_noop1(&mb
) != OKAY
) {
795 safe_signal(SIGINT
, saveint
);
796 safe_signal(SIGPIPE
, savepipe
);
799 safe_signal(SIGINT
, saveint
);
800 safe_signal(SIGPIPE
, savepipe
);
803 alarm(imapkeepalive
);
809 imap_use_starttls(const char *uhp
)
814 if (ok_blook(imap_use_starttls
))
817 char *var
= savecat("imap-use-starttls-", uhp
);
825 imap_preauth(struct mailbox
*mp
, const char *xserver
, const char *uhp
)
830 mp
->mb_active
|= MB_PREAUTH
;
832 if ((cp
= strchr(xserver
, ':')) != NULL
) {
833 char *x
= salloc(cp
- xserver
+ 1);
834 memcpy(x
, xserver
, cp
- xserver
);
835 x
[cp
- xserver
] = '\0';
839 if (mp
->mb_sock
.s_use_ssl
== 0 && imap_use_starttls(uhp
)) {
840 FILE *queuefp
= NULL
;
843 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
844 IMAP_OUT(o
, MB_COMD
, return STOP
)
846 if (ssl_open(xserver
, &mp
->mb_sock
, uhp
) != OKAY
)
850 if (imap_use_starttls(uhp
)) {
851 fprintf(stderr
, "No SSL support compiled in.\n");
860 imap_capability(struct mailbox
*mp
)
863 FILE *queuefp
= NULL
;
868 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
869 IMAP_OUT(o
, MB_COMD
, return STOP
)
870 while (mp
->mb_active
& MB_COMD
) {
871 ok
= imap_answer(mp
, 0);
872 if (response_status
== RESPONSE_OTHER
&&
873 response_other
== CAPABILITY_DATA
) {
874 cp
= responded_other_text
;
876 while (spacechar(*cp
))
878 if (strncmp(cp
, "UIDPLUS", 7) == 0 && spacechar(cp
[7]))
880 mp
->mb_flags
|= MB_UIDPLUS
;
881 while (*cp
&& !spacechar(*cp
))
890 imap_auth(struct mailbox
*mp
, struct ccred
*ccred
)
895 if (!(mp
->mb_active
& MB_PREAUTH
)) {
900 switch (ccred
->cc_authtype
) {
902 rv
= imap_login(mp
, ccred
);
905 case AUTHTYPE_CRAM_MD5
:
906 rv
= imap_cram_md5(mp
, ccred
);
910 case AUTHTYPE_GSSAPI
:
911 rv
= _imap_gssapi(mp
, ccred
);
925 imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
)
927 char o
[LINESIZE
], *cp
;
928 FILE *queuefp
= NULL
;
932 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
933 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
935 if (response_type
!= RESPONSE_CONT
)
938 cp
= cram_md5_string(&ccred
->cc_user
, &ccred
->cc_pass
, responded_text
);
939 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
940 while (mp
->mb_active
& MB_COMD
)
941 rv
= imap_answer(mp
, 1);
946 #endif /* HAVE_MD5 */
949 imap_login(struct mailbox
*mp
, struct ccred
*ccred
)
952 FILE *queuefp
= NULL
;
956 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
957 tag(1), imap_quotestr(ccred
->cc_user
.s
), imap_quotestr(ccred
->cc_pass
.s
));
958 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
959 while (mp
->mb_active
& MB_COMD
)
960 rv
= imap_answer(mp
, 1);
967 # include "imap_gssapi.h"
971 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
)
976 FILE *queuefp
= NULL
;
980 mp
->mb_uidvalidity
= 0;
981 snprintf(o
, sizeof o
, "%s SELECT %s\r\n", tag(1), imap_quotestr(mbx
));
982 IMAP_OUT(o
, MB_COMD
, return STOP
)
983 while (mp
->mb_active
& MB_COMD
) {
984 ok
= imap_answer(mp
, 1);
985 if (response_status
!= RESPONSE_OTHER
&&
986 (cp
= asccasestr(responded_text
, "[UIDVALIDITY ")) != NULL
)
987 mp
->mb_uidvalidity
= atol(&cp
[13]);
989 *cnt
= (had_exists
> 0) ? had_exists
: 0;
990 if (response_status
!= RESPONSE_OTHER
&&
991 ascncasecmp(responded_text
, "[READ-ONLY] ", 12) == 0)
997 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
1000 FILE *queuefp
= NULL
;
1003 unsigned x
= X
, y
= Y
, n
;
1006 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
1007 IMAP_OUT(o
, MB_COMD
, return STOP
)
1008 while (mp
->mb_active
& MB_COMD
) {
1010 if (response_status
== RESPONSE_OTHER
&&
1011 response_other
== MESSAGE_DATA_FETCH
) {
1012 n
= responded_other_number
;
1020 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1025 imap_getflags(cp
, &cp
, &m
->m_flag
);
1028 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1029 m
->m_uid
= strtoul(&cp
[4], NULL
, 10);
1030 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1031 m
->m_flag
&= ~MHIDDEN
;
1034 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1036 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1039 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1041 IMAP_OUT(o
, MB_COMD
, return STOP
)
1042 while (mp
->mb_active
& MB_COMD
) {
1044 if (response_status
== RESPONSE_OTHER
&&
1045 response_other
== MESSAGE_DATA_FETCH
) {
1046 n
= responded_other_number
;
1052 if ((cp
= asccasestr(responded_other_text
, "RFC822.SIZE ")) != NULL
)
1053 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1054 if ((cp
= asccasestr(responded_other_text
, "INTERNALDATE ")) != NULL
)
1055 m
->m_time
= imap_read_date_time(&cp
[13]);
1060 for (n
= X
; n
<= Y
; ++n
) {
1061 putcache(mp
, &message
[n
-1]);
1069 imap_init(struct mailbox
*mp
, int n
)
1076 m
->m_flag
= MUSED
| MNOFROM
;
1083 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1085 struct message
*omessage
= 0;
1086 int i
, omsgCount
= 0;
1087 enum okay dequeued
= STOP
;
1090 if (nmail
|| transparent
) {
1092 omsgCount
= msgCount
;
1095 dequeued
= rec_dequeue();
1097 if (had_exists
>= 0) {
1098 if (dequeued
!= OKAY
)
1099 msgCount
= had_exists
;
1102 if (had_expunge
>= 0) {
1103 if (dequeued
!= OKAY
)
1104 msgCount
-= had_expunge
;
1108 if (nmail
&& expunged_messages
)
1109 printf("Expunged %ld message%s.\n", expunged_messages
,
1110 (expunged_messages
!= 1 ? "s" : ""));
1111 *prevcount
= omsgCount
- expunged_messages
;
1112 expunged_messages
= 0;
1114 fputs("IMAP error: Negative message count\n", stderr
);
1118 if (dequeued
!= OKAY
) {
1119 message
= scalloc(msgCount
+ 1, sizeof *message
);
1120 for (i
= 0; i
< msgCount
; i
++)
1122 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1125 imap_flags(mp
, 1, msgCount
);
1126 message
[msgCount
].m_size
= 0;
1127 message
[msgCount
].m_lines
= 0;
1130 if (nmail
|| transparent
)
1131 transflags(omessage
, omsgCount
, transparent
);
1138 imap_setfile(const char *xserver
, enum fedit_mode fm
)
1144 if (!url_parse(&url
, CPROTO_IMAP
, xserver
)) {
1148 if (!ok_blook(v15_compat
) &&
1149 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
1150 fprintf(stderr
, "New-style URL used without *v15-compat* being set!\n");
1152 _imap_rdonly
= ((fm
& FEDIT_RDONLY
) != 0);
1153 rv
= _imap_setfile1(&url
, fm
, 0);
1160 _imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
, struct url
*urlp
)
1165 if (ok_blook(v15_compat
))
1166 rv
= ccred_lookup(ccredp
, urlp
);
1169 *xuhp
= (urlp
->url_had_user
? urlp
->url_eu_h_p
.s
: urlp
->url_u_h_p
.s
);
1171 if ((var
= mbp
->mb_imap_pass
) != NULL
) {
1172 var
= savecat("password-", xuhp
);
1173 old
= vok_vlook(var
);
1174 vok_vset(var
, mbp
->mb_imap_pass
);
1176 rv
= ccred_lookup_old(ccredp
, CPROTO_IMAP
, xuhp
);
1190 _imap_setfile1(struct url
*urlp
, enum fedit_mode fm
, int volatile transparent
)
1194 sighandler_type
volatile saveint
, savepipe
;
1197 int volatile prevcount
= 0;
1198 enum mbflags same_flags
;
1201 if (fm
& FEDIT_NEWMAIL
) {
1202 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1203 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1204 if (saveint
!= SIG_IGN
)
1205 safe_signal(SIGINT
, imapcatch
);
1206 if (savepipe
!= SIG_IGN
)
1207 safe_signal(SIGPIPE
, imapcatch
);
1212 same_flags
= mb
.mb_flags
;
1213 same_imap_account
= 0;
1214 if (mb
.mb_imap_account
!= NULL
&&
1215 (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)) {
1216 if (mb
.mb_sock
.s_fd
> 0 && mb
.mb_sock
.s_rsz
>= 0 &&
1217 !strcmp(mb
.mb_imap_account
, urlp
->url_p_eu_h_p
) &&
1218 disconnected(mb
.mb_imap_account
) == 0) {
1219 same_imap_account
= 1;
1220 if (urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1222 } else if ((transparent
|| mb
.mb_type
== MB_CACHE
) &&
1223 !strcmp(mb
.mb_imap_account
, urlp
->url_p_eu_h_p
) &&
1224 urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1226 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= savestr(mb
.mb_imap_pass
));
1229 if (!_imap_getcred(&mb
, &ccred
, urlp
)) {
1235 if (!same_imap_account
) {
1236 if (!disconnected(urlp
->url_p_eu_h_p
) && !sopen(&so
, urlp
)) {
1245 edit
= !(fm
& FEDIT_SYSBOX
);
1246 if (mb
.mb_imap_account
!= NULL
)
1247 free(mb
.mb_imap_account
);
1248 if (mb
.mb_imap_pass
!= NULL
)
1249 free(mb
.mb_imap_pass
);
1250 mb
.mb_imap_account
= sstrdup(urlp
->url_p_eu_h_p
);
1251 /* TODO This is a hack to allow '@boxname'; in the end everything will be an
1252 * TODO object, and mailbox will naturally have an URL and credentials */
1253 mb
.mb_imap_pass
= sbufdup(ccred
.cc_pass
.s
, ccred
.cc_pass
.l
);
1255 if (!same_imap_account
) {
1256 if (mb
.mb_sock
.s_fd
>= 0)
1257 sclose(&mb
.mb_sock
);
1259 same_imap_account
= 0;
1270 if (mb
.mb_imap_mailbox
!= NULL
)
1271 free(mb
.mb_imap_mailbox
);
1272 mb
.mb_imap_mailbox
= sstrdup((urlp
->url_path
.s
!= NULL
)
1273 ? urlp
->url_path
.s
: "INBOX");
1274 initbox(urlp
->url_p_eu_h_p_p
);
1276 mb
.mb_type
= MB_VOID
;
1277 mb
.mb_active
= MB_NONE
;
1280 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1281 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1282 if (sigsetjmp(imapjmp
, 1)) {
1283 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1284 sclose(&mb
.mb_sock
);
1285 safe_signal(SIGINT
, saveint
);
1286 safe_signal(SIGPIPE
, savepipe
);
1289 mb
.mb_type
= MB_VOID
;
1290 mb
.mb_active
= MB_NONE
;
1294 if (saveint
!= SIG_IGN
)
1295 safe_signal(SIGINT
, imapcatch
);
1296 if (savepipe
!= SIG_IGN
)
1297 safe_signal(SIGPIPE
, imapcatch
);
1299 if (mb
.mb_sock
.s_fd
< 0) {
1300 if (disconnected(mb
.mb_imap_account
)) {
1301 if (cache_setptr(fm
, transparent
) == STOP
)
1302 fprintf(stderr
, "Mailbox \"%s\" is not cached.\n",
1303 urlp
->url_p_eu_h_p_p
);
1306 if ((cp
= ok_vlook(imap_keepalive
)) != NULL
) {
1307 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1308 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1309 alarm(imapkeepalive
);
1314 mb
.mb_sock
.s_desc
= "IMAP";
1315 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1316 if (imap_preauth(&mb
, urlp
->url_h_p
.s
, urlp
->url_u_h_p
.s
) != OKAY
||
1317 imap_auth(&mb
, &ccred
) != OKAY
) {
1318 sclose(&mb
.mb_sock
);
1320 safe_signal(SIGINT
, saveint
);
1321 safe_signal(SIGPIPE
, savepipe
);
1326 } else /* same account */
1327 mb
.mb_flags
|= same_flags
;
1329 mb
.mb_perm
= ((options
& OPT_R_FLAG
) || (fm
& FEDIT_RDONLY
)) ? 0 : MB_DELE
;
1330 mb
.mb_type
= MB_IMAP
;
1332 if (imap_select(&mb
, &mailsize
, &msgCount
,
1333 (urlp
->url_path
.s
!= NULL
? urlp
->url_path
.s
: "INBOX")) != OKAY
) {
1334 /*sclose(&mb.mb_sock);
1336 safe_signal(SIGINT
, saveint
);
1337 safe_signal(SIGPIPE
, savepipe
);
1339 mb
.mb_type
= MB_VOID
;
1345 imap_setptr(&mb
, ((fm
& FEDIT_NEWMAIL
) != 0), transparent
,
1346 UNVOLATILE(&prevcount
));
1349 if (!(fm
& FEDIT_NEWMAIL
) && !transparent
)
1351 safe_signal(SIGINT
, saveint
);
1352 safe_signal(SIGPIPE
, savepipe
);
1355 if (!(fm
& FEDIT_NEWMAIL
) && mb
.mb_type
== MB_IMAP
)
1356 purgecache(&mb
, message
, msgCount
);
1357 if (((fm
& FEDIT_NEWMAIL
) || transparent
) && mb
.mb_sorted
) {
1362 if (!(fm
& FEDIT_NEWMAIL
) && !edit
&& msgCount
== 0) {
1363 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1364 !ok_blook(emptystart
))
1365 fprintf(stderr
, _("No mail at %s\n"), urlp
->url_p_eu_h_p_p
);
1369 if (fm
& FEDIT_NEWMAIL
)
1370 newmailinfo(prevcount
);
1378 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1379 int need
, const char *head
, size_t headsize
, long headlines
)
1381 char *line
= NULL
, *lp
;
1382 size_t linesize
= 0, linelen
, size
= 0;
1383 int emptyline
= 0, lines
= 0, excess
= 0;
1387 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1388 offset
= ftell(mp
->mb_otf
);
1391 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1393 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1395 if (linelen
> expected
) {
1396 excess
= linelen
- expected
;
1400 * Need to mask 'From ' lines. This cannot be done properly
1401 * since some servers pass them as 'From ' and others as
1402 * '>From '. Although one could identify the first kind of
1403 * server in principle, it is not possible to identify the
1404 * second as '>From ' may also come from a server of the
1405 * first type as actual data. So do what is absolutely
1406 * necessary only - mask 'From '.
1408 * If the line is the first line of the message header, it
1409 * is likely a real 'From ' line. In this case, it is just
1410 * ignored since it violates all standards.
1411 * TODO can the latter *really* happen??
1414 /* Since we simply copy over data without doing any transfer
1415 * encoding reclassification/adjustment we *have* to perform
1416 * RFC 4155 compliant From_ quoting here */
1417 if (is_head(lp
, linelen
)) {
1418 if (lines
+ headlines
== 0)
1420 fputc('>', mp
->mb_otf
);
1423 if (lp
[linelen
-1] == '\n' && (linelen
== 1 || lp
[linelen
-2] == '\r')) {
1424 emptyline
= linelen
<= 2;
1426 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1427 size
+= linelen
- 1;
1430 fputc('\n', mp
->mb_otf
);
1433 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1438 if ((expected
-= linelen
) <= 0)
1442 /* This is very ugly; but some IMAP daemons don't end a
1443 * message with \r\n\r\n, and we need \n\n for mbox format */
1444 fputc('\n', mp
->mb_otf
);
1451 m
->m_size
= size
+ headsize
;
1452 m
->m_lines
= lines
+ headlines
;
1453 m
->m_block
= mailx_blockof(offset
);
1454 m
->m_offset
= mailx_offsetof(offset
);
1457 m
->m_have
|= HAVE_HEADER
;
1460 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1461 m
->m_xlines
= m
->m_lines
;
1462 m
->m_xsize
= m
->m_size
;
1472 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1473 const char *head
, size_t headsize
, long headlines
)
1480 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1481 offset
= ftell(mp
->mb_otf
);
1483 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1485 fwrite(str
, 1, len
, mp
->mb_otf
);
1486 fputc('\n', mp
->mb_otf
);
1492 m
->m_size
= headsize
+ len
;
1493 m
->m_lines
= headlines
+ 1;
1494 m
->m_block
= mailx_blockof(offset
);
1495 m
->m_offset
= mailx_offsetof(offset
);
1496 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1497 m
->m_xlines
= m
->m_lines
;
1498 m
->m_xsize
= m
->m_size
;
1504 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
1508 sighandler_type
volatile saveint
, savepipe
;
1509 char * volatile head
;
1510 char const *cp
, *loc
, * volatile item
, * volatile resp
;
1512 size_t volatile headsize
;
1515 long volatile headlines
;
1521 saveint
= savepipe
= SIG_IGN
;
1523 cp
= loc
= item
= resp
= NULL
;
1525 number
= (int)PTR2SIZE(m
- message
+ 1);
1532 if (getcache(mp
, m
, need
) == OKAY
)
1534 if (mp
->mb_type
== MB_CACHE
) {
1535 fprintf(stderr
, "Message %u not available.\n", number
);
1539 if (mp
->mb_sock
.s_fd
< 0) {
1540 fprintf(stderr
, "IMAP connection closed.\n");
1546 resp
= item
= "RFC822.HEADER";
1549 item
= "BODY.PEEK[]";
1551 if (m
->m_flag
& HAVE_HEADER
&& m
->m_size
) {
1552 char *hdr
= smalloc(m
->m_size
);
1554 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
1556 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
) != m
->m_size
) {
1561 headsize
= m
->m_size
;
1562 headlines
= m
->m_lines
;
1563 item
= "BODY.PEEK[TEXT]";
1564 resp
= "BODY[TEXT]";
1572 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1573 if (sigsetjmp(imapjmp
, 1)) {
1574 safe_signal(SIGINT
, saveint
);
1575 safe_signal(SIGPIPE
, savepipe
);
1579 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1580 safe_signal(SIGINT
, &_imap_maincatch
);
1581 if (savepipe
!= SIG_IGN
)
1582 safe_signal(SIGPIPE
, imapcatch
);
1585 snprintf(o
, sizeof o
, "%s UID FETCH %lu (%s)\r\n",
1586 tag(1), m
->m_uid
, item
);
1588 if (check_expunged() == STOP
)
1590 snprintf(o
, sizeof o
, "%s FETCH %u (%s)\r\n", tag(1), number
, item
);
1592 IMAP_OUT(o
, MB_COMD
, goto out
)
1594 ok
= imap_answer(mp
, 1);
1597 if (response_status
!= RESPONSE_OTHER
||
1598 response_other
!= MESSAGE_DATA_FETCH
)
1600 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
1603 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
1611 n
= responded_other_number
;
1612 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
1613 if (m
->m_uid
? m
->m_uid
!= u
: n
!= number
)
1615 if ((cp
= strchr(loc
, '"')) != NULL
) {
1616 cp
= imap_unquotestr(cp
);
1617 imap_putstr(mp
, m
, cp
, head
, headsize
, headlines
);
1619 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1620 m
->m_xlines
= m
->m_lines
;
1621 m
->m_xsize
= m
->m_size
;
1625 expected
= atol(&cp
[1]);
1626 if (m
->m_uid
? n
== 0 && m
->m_uid
!= u
: n
!= number
) {
1627 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
1631 imap_fetchdata(mp
, &mt
, expected
, need
, head
, headsize
, headlines
);
1633 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1636 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1637 if (options
& OPT_VERBVERB
)
1638 fputs(imapbuf
, stderr
);
1639 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1641 if (u
== m
->m_uid
) {
1642 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1649 while (mp
->mb_active
& MB_COMD
)
1650 ok
= imap_answer(mp
, 1);
1652 if (saveint
!= SIG_IGN
)
1653 safe_signal(SIGINT
, saveint
);
1654 if (savepipe
!= SIG_IGN
)
1655 safe_signal(SIGPIPE
, savepipe
);
1668 imap_header(struct message
*m
)
1673 rv
= imap_get(&mb
, m
, NEED_HEADER
);
1680 imap_body(struct message
*m
)
1685 rv
= imap_get(&mb
, m
, NEED_BODY
);
1691 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
1695 tomp
->m_size
= frommp
->m_size
;
1696 tomp
->m_lines
= frommp
->m_lines
;
1697 tomp
->m_block
= frommp
->m_block
;
1698 tomp
->m_offset
= frommp
->m_offset
;
1699 tomp
->m_have
= have
;
1700 if (have
& HAVE_BODY
) {
1701 tomp
->m_xlines
= frommp
->m_lines
;
1702 tomp
->m_xsize
= frommp
->m_size
;
1709 imap_fetchheaders(struct mailbox
*mp
, struct message
*m
, int bot
, int topp
)
1717 FILE *queuefp
= NULL
;
1722 snprintf(o
, sizeof o
, "%s UID FETCH %lu:%lu (RFC822.HEADER)\r\n",
1723 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
1725 if (check_expunged() == STOP
)
1727 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.HEADER)\r\n",
1730 IMAP_OUT(o
, MB_COMD
, return STOP
)
1734 ok
= imap_answer(mp
, 1);
1735 if (response_status
!= RESPONSE_OTHER
)
1737 if (response_other
!= MESSAGE_DATA_FETCH
)
1739 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0)
1741 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
1743 expected
= atol(&cp
[1]);
1744 if (m
[bot
-1].m_uid
) {
1745 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
) {
1747 for (n
= bot
; n
<= topp
; n
++)
1748 if ((unsigned long)u
== m
[n
-1].m_uid
)
1751 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1757 n
= responded_other_number
;
1758 if (n
<= 0 || n
> msgCount
) {
1759 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1763 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
1764 if (n
>= 0 && !(m
[n
-1].m_have
& HAVE_HEADER
))
1765 commitmsg(mp
, &m
[n
-1], &mt
, HAVE_HEADER
);
1766 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1767 if (options
& OPT_VERBVERB
)
1768 fputs(imapbuf
, stderr
);
1769 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1771 for (n
= bot
; n
<= topp
; n
++)
1772 if ((unsigned long)u
== m
[n
-1].m_uid
)
1774 if (n
<= topp
&& !(m
[n
-1].m_have
& HAVE_HEADER
))
1775 commitmsg(mp
, &m
[n
-1], &mt
,HAVE_HEADER
);
1783 while (mp
->mb_active
& MB_COMD
)
1784 ok
= imap_answer(mp
, 1);
1789 imap_getheaders(int volatile bot
, int volatile topp
) /* TODO iterator!! */
1791 sighandler_type saveint
, savepipe
;
1792 /*enum okay ok = STOP;*/
1796 if (mb
.mb_type
== MB_CACHE
)
1800 if (topp
> msgCount
)
1802 for (i
= bot
; i
< topp
; i
++) {
1803 if (message
[i
-1].m_have
& HAVE_HEADER
||
1804 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1809 for (i
= topp
; i
> bot
; i
--) {
1810 if (message
[i
-1].m_have
& HAVE_HEADER
||
1811 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1820 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1821 safe_signal(SIGINT
, &_imap_maincatch
);
1822 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1823 if (sigsetjmp(imapjmp
, 1) == 0) {
1824 if (savepipe
!= SIG_IGN
)
1825 safe_signal(SIGPIPE
, imapcatch
);
1827 for (i
= bot
; i
<= topp
; i
+= chunk
) {
1828 int j
= i
+ chunk
- 1;
1830 if (visible(message
+ j
))
1831 /*ok = */imap_fetchheaders(&mb
, message
, i
, j
);
1833 onintr(0); /* XXX imaplock? */
1836 safe_signal(SIGINT
, saveint
);
1837 safe_signal(SIGPIPE
, savepipe
);
1842 __imap_exit(struct mailbox
*mp
)
1845 FILE *queuefp
= NULL
;
1848 mp
->mb_active
|= MB_BYE
;
1849 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
1850 IMAP_OUT(o
, MB_COMD
, return STOP
)
1856 imap_exit(struct mailbox
*mp
)
1861 rv
= __imap_exit(mp
);
1862 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
1863 free(mp
->mb_imap_pass
);
1864 free(mp
->mb_imap_account
);
1865 free(mp
->mb_imap_mailbox
);
1866 if (mp
->mb_cache_directory
!= NULL
)
1867 free(mp
->mb_cache_directory
);
1868 #ifndef HAVE_DEBUG /* TODO ASSERT LEGACY */
1869 mp
->mb_imap_account
=
1870 mp
->mb_imap_mailbox
=
1871 mp
->mb_cache_directory
= "";
1873 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
1874 mp
->mb_imap_mailbox
= NULL
;
1875 mp
->mb_cache_directory
= NULL
;
1878 sclose(&mp
->mb_sock
);
1884 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
1887 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
1888 if (mp
->mb_type
== MB_IMAP
)
1895 imap_close(struct mailbox
*mp
)
1898 FILE *queuefp
= NULL
;
1901 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
1902 IMAP_OUT(o
, MB_COMD
, return STOP
)
1908 imap_update(struct mailbox
*mp
)
1911 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
1914 if (!edit
&& mp
->mb_perm
!= 0) {
1917 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1918 if (m
->m_flag
& MBOX
)
1921 if (makembox() == STOP
)
1926 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
1927 if (mp
->mb_perm
== 0)
1930 dodel
= ((m
->m_flag
& MDELETED
) != 0);
1932 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
1934 /* Fetch the result after around each 800 STORE commands
1935 * sent (approx. 32k data sent). Otherwise, servers will
1936 * try to flush the return queue at some point, leading
1937 * to a deadlock if we are still writing commands but not
1938 * reading their results */
1939 needstat
= stored
> 0 && stored
% 800 == 0;
1940 /* Even if this message has been deleted, continue
1941 * to set further flags. This is necessary to support
1942 * Gmail semantics, where "delete" actually means
1943 * "archive", and the flags are applied to the copy
1945 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
1946 imap_store(mp
, m
, m
-message
+1, '+', "\\Seen", needstat
);
1949 if (m
->m_flag
& MFLAG
) {
1950 imap_store(mp
, m
, m
-message
+1, '+', "\\Flagged", needstat
);
1953 if (m
->m_flag
& MUNFLAG
) {
1954 imap_store(mp
, m
, m
-message
+1, '-', "\\Flagged", needstat
);
1957 if (m
->m_flag
& MANSWER
) {
1958 imap_store(mp
, m
, m
-message
+1, '+', "\\Answered", needstat
);
1961 if (m
->m_flag
& MUNANSWER
) {
1962 imap_store(mp
, m
, m
-message
+1, '-', "\\Answered", needstat
);
1965 if (m
->m_flag
& MDRAFT
) {
1966 imap_store(mp
, m
, m
-message
+1, '+', "\\Draft", needstat
);
1969 if (m
->m_flag
& MUNDRAFT
) {
1970 imap_store(mp
, m
, m
-message
+1, '-', "\\Draft", needstat
);
1975 imap_delete(mp
, m
-message
+1, m
, needstat
);
1978 } else if (mp
->mb_type
!= MB_CACHE
||
1979 (!edit
&& !(m
->m_flag
& (MBOXED
| MSAVED
| MDELETED
))) ||
1980 (m
->m_flag
& (MBOXED
| MPRESERVE
| MTOUCH
)) ==
1981 (MPRESERVE
| MTOUCH
) || (edit
&& !(m
->m_flag
& MDELETED
)))
1983 if (m
->m_flag
& MNEW
) {
1985 m
->m_flag
|= MSTATUS
;
1992 for (m
= &message
[0]; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1993 if (!(m
->m_flag
& MUNLINKED
) &&
1994 m
->m_flag
& (MBOXED
| MDELETED
| MSAVED
| MSTATUS
| MFLAG
|
1995 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
)) {
2000 if ((gotcha
|| modflags
) && edit
) {
2001 printf(_("\"%s\" "), displayname
);
2002 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
2003 ? _("complete\n") : _("updated.\n"));
2004 } else if (held
&& !edit
&& mp
->mb_perm
!= 0) {
2006 printf(_("Held 1 message in %s\n"), displayname
);
2008 printf(_("Held %d messages in %s\n"), held
, displayname
);
2018 sighandler_type
volatile saveint
, savepipe
;
2021 if (mb
.mb_type
== MB_CACHE
) {
2026 if (mb
.mb_sock
.s_fd
< 0) {
2027 fprintf(stderr
, "IMAP connection closed.\n");
2032 saveint
= safe_signal(SIGINT
, SIG_IGN
);
2033 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2034 if (sigsetjmp(imapjmp
, 1)) {
2035 safe_signal(SIGINT
, saveint
);
2036 safe_signal(SIGPIPE
, saveint
);
2040 if (saveint
!= SIG_IGN
)
2041 safe_signal(SIGINT
, imapcatch
);
2042 if (savepipe
!= SIG_IGN
)
2043 safe_signal(SIGPIPE
, imapcatch
);
2046 if (!same_imap_account
)
2049 safe_signal(SIGINT
, saveint
);
2050 safe_signal(SIGPIPE
, savepipe
);
2057 imap_store(struct mailbox
*mp
, struct message
*m
, int n
, int c
, const char *sp
,
2061 FILE *queuefp
= NULL
;
2064 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
2067 snprintf(o
, sizeof o
, "%s UID STORE %lu %cFLAGS (%s)\r\n",
2068 tag(1), m
->m_uid
, c
, sp
);
2070 if (check_expunged() == STOP
)
2072 snprintf(o
, sizeof o
, "%s STORE %u %cFLAGS (%s)\r\n", tag(1), n
, c
, sp
);
2074 IMAP_OUT(o
, MB_COMD
, return STOP
)
2078 mb
.mb_active
&= ~MB_COMD
;
2079 if (queuefp
!= NULL
)
2085 imap_undelete(struct message
*m
, int n
)
2090 rv
= imap_unstore(m
, n
, "\\Deleted");
2096 imap_unread(struct message
*m
, int n
)
2101 rv
= imap_unstore(m
, n
, "\\Seen");
2107 imap_unstore(struct message
*m
, int n
, const char *flag
)
2109 sighandler_type saveint
, savepipe
;
2110 enum okay rv
= STOP
;
2114 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2115 safe_signal(SIGINT
, &_imap_maincatch
);
2116 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2117 if (sigsetjmp(imapjmp
, 1) == 0) {
2118 if (savepipe
!= SIG_IGN
)
2119 safe_signal(SIGPIPE
, imapcatch
);
2121 rv
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2123 safe_signal(SIGINT
, saveint
);
2124 safe_signal(SIGPIPE
, savepipe
);
2142 snprintf(ts
, sizeof ts
, "T%lu", n
);
2148 c_imap_imap(void *vp
)
2151 sighandler_type saveint
, savepipe
;
2152 struct mailbox
*mp
= &mb
;
2153 FILE *queuefp
= NULL
;
2154 enum okay
volatile ok
= STOP
;
2157 if (mp
->mb_type
!= MB_IMAP
) {
2158 printf("Not operating on an IMAP mailbox.\n");
2162 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2163 safe_signal(SIGINT
, &_imap_maincatch
);
2164 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2165 if (sigsetjmp(imapjmp
, 1) == 0) {
2166 if (savepipe
!= SIG_IGN
)
2167 safe_signal(SIGPIPE
, imapcatch
);
2169 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2170 IMAP_OUT(o
, MB_COMD
, goto out
)
2171 while (mp
->mb_active
& MB_COMD
) {
2172 ok
= imap_answer(mp
, 0);
2173 fputs(responded_text
, stdout
);
2177 safe_signal(SIGINT
, saveint
);
2178 safe_signal(SIGPIPE
, savepipe
);
2187 imap_newmail(int nmail
)
2191 if (nmail
&& had_exists
< 0 && had_expunge
< 0) {
2197 if (had_exists
== msgCount
&& had_expunge
< 0)
2198 /* Some servers always respond with EXISTS to NOOP. If
2199 * the mailbox has been changed but the number of messages
2200 * has not, an EXPUNGE must also had been sent; otherwise,
2201 * nothing has changed */
2204 return (had_expunge
>= 0 ? 2 : (had_exists
>= 0 ? 1 : 0));
2208 imap_putflags(int f
)
2214 bp
= buf
= salloc(100);
2215 if (f
& (MREAD
| MFLAGGED
| MANSWERED
| MDRAFTED
)) {
2220 for (cp
= "\\Seen"; *cp
; cp
++)
2226 for (cp
= "\\Flagged"; *cp
; cp
++)
2229 if (f
& MANSWERED
) {
2232 for (cp
= "\\Answered"; *cp
; cp
++)
2238 for (cp
= "\\Draft"; *cp
; cp
++)
2250 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2253 while (*cp
!= ')') {
2255 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2257 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2259 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2261 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2263 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2265 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2277 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
, off_t off1
,
2278 long xsize
, enum mflag flag
, time_t t
)
2280 char o
[LINESIZE
], *buf
;
2281 size_t bufsize
, buflen
, cnt
;
2282 long size
, lines
, ysize
;
2284 FILE *queuefp
= NULL
;
2288 if (mp
->mb_type
== MB_CACHE
) {
2289 queuefp
= cache_queue(mp
);
2290 if (queuefp
== NULL
) {
2299 buf
= smalloc(bufsize
= LINESIZE
);
2304 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2309 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2310 tag(1), imap_quotestr(name
), imap_putflags(flag
),
2311 imap_make_date_time(t
), size
);
2312 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2313 while (mp
->mb_active
& MB_COMD
) {
2314 rv
= imap_answer(mp
, twice
);
2315 if (response_type
== RESPONSE_CONT
)
2319 if (mp
->mb_type
!= MB_CACHE
&& rv
== STOP
) {
2328 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2331 buf
[buflen
- 1] = '\r';
2333 if (mp
->mb_type
!= MB_CACHE
)
2334 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2336 fwrite(buf
, 1, buflen
+1, queuefp
);
2339 if (mp
->mb_type
!= MB_CACHE
)
2340 swrite(&mp
->mb_sock
, "\r\n");
2342 fputs("\r\n", queuefp
);
2343 while (mp
->mb_active
& MB_COMD
) {
2344 rv
= imap_answer(mp
, 0);
2345 if (response_status
== RESPONSE_NO
/*&&
2346 ascncasecmp(responded_text,
2347 "[TRYCREATE] ", 12) == 0*/) {
2353 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), imap_quotestr(name
));
2354 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2355 while (mp
->mb_active
& MB_COMD
)
2356 rv
= imap_answer(mp
, 1);
2359 imap_created_mailbox
++;
2361 } else if (rv
!= OKAY
)
2362 fprintf(stderr
, _("IMAP error: %s"), responded_text
);
2363 else if (response_status
== RESPONSE_OK
&& (mp
->mb_flags
& MB_UIDPLUS
))
2364 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
, flag
, name
);
2367 if (queuefp
!= NULL
)
2376 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2378 char *buf
, *bp
, *lp
;
2379 size_t bufsize
, buflen
, cnt
;
2380 off_t off1
= -1, offs
;
2381 int inhead
= 1, flag
= MNEW
| MNEWEST
;
2387 buf
= smalloc(bufsize
= LINESIZE
);
2394 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2395 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
2396 if (off1
!= (off_t
)-1) {
2397 rv
= imap_append1(mp
, name
, fp
, off1
, size
, flag
, tim
);
2400 fseek(fp
, offs
+buflen
, SEEK_SET
);
2402 off1
= offs
+ buflen
;
2407 tim
= unixtime(buf
);
2412 if (bp
&& buf
[0] == '\n')
2414 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
2416 while (whitechar(*lp
))
2419 while (*++lp
!= '\0')
2428 } else if (bp
&& inhead
&& ascncasecmp(buf
, "x-status", 8) == 0) {
2430 while (whitechar(*lp
))
2433 while (*++lp
!= '\0')
2446 } while (bp
!= NULL
);
2455 imap_append(const char *xserver
, FILE *fp
)
2457 sighandler_type
volatile saveint
, savepipe
;
2460 char const * volatile mbx
;
2461 enum okay rv
= STOP
;
2464 if (!url_parse(&url
, CPROTO_IMAP
, xserver
))
2466 if (!ok_blook(v15_compat
) &&
2467 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
2468 fprintf(stderr
, "New-style URL used without *v15-compat* being set!\n");
2469 mbx
= (url
.url_path
.s
!= NULL
) ? url
.url_path
.s
: "INBOX";
2472 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2473 safe_signal(SIGINT
, &_imap_maincatch
);
2474 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2475 if (sigsetjmp(imapjmp
, 1))
2477 if (savepipe
!= SIG_IGN
)
2478 safe_signal(SIGPIPE
, imapcatch
);
2480 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) && mb
.mb_imap_account
&&
2481 !strcmp(url
.url_p_eu_h_p
, mb
.mb_imap_account
)) {
2482 rv
= imap_append0(&mb
, mbx
, fp
);
2486 memset(&mx
, 0, sizeof mx
);
2488 if (!_imap_getcred(&mx
, &ccred
, &url
))
2491 if (disconnected(url
.url_p_eu_h_p
) == 0) {
2492 if (!sopen(&mx
.mb_sock
, &url
))
2494 mx
.mb_sock
.s_desc
= "IMAP";
2495 mx
.mb_type
= MB_IMAP
;
2496 mx
.mb_imap_account
= UNCONST(url
.url_p_eu_h_p
);
2497 /* TODO the code now did
2498 * TODO mx.mb_imap_mailbox = mbx;
2499 * TODO though imap_mailbox is sfree()d and mbx
2500 * TODO is possibly even a constant
2501 * TODO i changed this to sstrdup() sofar, as is used
2502 * TODO somewhere else in this file for this! */
2503 mx
.mb_imap_mailbox
= sstrdup(mbx
);
2504 if (imap_preauth(&mx
, url
.url_h_p
.s
, url
.url_u_h_p
.s
) != OKAY
||
2505 imap_auth(&mx
, &ccred
) != OKAY
) {
2506 sclose(&mx
.mb_sock
);
2509 rv
= imap_append0(&mx
, mbx
, fp
);
2512 mx
.mb_imap_account
= UNCONST(url
.url_p_eu_h_p
);
2513 mx
.mb_imap_mailbox
= sstrdup(mbx
); /* TODO as above */
2514 mx
.mb_type
= MB_CACHE
;
2515 rv
= imap_append0(&mx
, mbx
, fp
);
2522 safe_signal(SIGINT
, saveint
);
2523 safe_signal(SIGPIPE
, savepipe
);
2533 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
2534 struct list_item
**lend
, int level
)
2536 char o
[LINESIZE
], *cp
;
2538 FILE *queuefp
= NULL
;
2539 struct list_item
*lp
;
2540 enum okay ok
= STOP
;
2543 *list
= *lend
= NULL
;
2544 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n", tag(1), imap_quotestr(base
));
2545 IMAP_OUT(o
, MB_COMD
, return STOP
)
2546 while (mp
->mb_active
& MB_COMD
) {
2547 ok
= imap_answer(mp
, 1);
2548 if (response_status
== RESPONSE_OTHER
&&
2549 response_other
== MAILBOX_DATA_LIST
&& imap_parse_list() == OKAY
) {
2550 cp
= imap_unquotestr(list_name
);
2551 lp
= csalloc(1, sizeof *lp
);
2553 for (bp
= base
; *bp
!= '\0' && *bp
== *cp
; ++bp
)
2555 lp
->l_base
= *cp
? cp
: savestr(base
);
2556 lp
->l_attr
= list_attributes
;
2557 lp
->l_level
= level
+1;
2558 lp
->l_delim
= list_hierarchy_delimiter
;
2559 if (*list
&& *lend
) {
2560 (*lend
)->l_next
= lp
;
2570 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
2572 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
2579 depth
= (cp
= ok_vlook(imap_list_depth
)) != NULL
? atoi(cp
) : 2;
2580 if ((rv
= imap_list1(mp
, base
, &list
, &lend
, 0)) == STOP
)
2583 if (list
== NULL
|| lend
== NULL
)
2586 for (lp
= list
; lp
; lp
= lp
->l_next
)
2587 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&& lp
->l_level
< depth
&&
2588 !(lp
->l_attr
& LIST_NOINFERIORS
)) {
2589 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
2590 memcpy(cp
, lp
->l_name
, n
);
2591 cp
[n
] = lp
->l_delim
;
2593 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&& lx
&& ly
) {
2594 lp
->l_has_children
= 1;
2595 if (strcmp(cp
, lx
->l_name
) == 0)
2604 for (lp
= list
; lp
; lp
= lp
->l_next
) {
2607 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2611 if (!(lp
->l_attr
& LIST_NOSELECT
))
2612 fprintf(fp
, "%s\n", *cp
? cp
: base
);
2613 else if (lp
->l_has_children
== 0)
2614 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
2615 (lp
->l_delim
!= EOF
? lp
->l_delim
: '\n'));
2623 imap_folders(const char * volatile name
, int strip
)
2625 sighandler_type saveint
, savepipe
;
2626 const char *fold
, *cp
, *sp
;
2630 cp
= protbase(name
);
2631 sp
= mb
.mb_imap_account
;
2632 if (sp
== NULL
|| strcmp(cp
, sp
)) {
2634 "Cannot perform `folders' but when on the very IMAP "
2635 "account; the current one is\n `%s' -- "
2636 "try `folders @'.\n"),
2637 (sp
!= NULL
? sp
: _("[NONE]")));
2641 fold
= imap_fileof(name
);
2642 if (options
& OPT_TTYOUT
) {
2643 if ((fp
= Ftmp(NULL
, "imapfold", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
2652 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2653 safe_signal(SIGINT
, &_imap_maincatch
);
2654 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2655 if (sigsetjmp(imapjmp
, 1)) /* TODO imaplock? */
2657 if (savepipe
!= SIG_IGN
)
2658 safe_signal(SIGPIPE
, imapcatch
);
2660 if (mb
.mb_type
== MB_CACHE
)
2661 cache_list(&mb
, fold
, strip
, fp
);
2663 imap_list(&mb
, fold
, strip
, fp
);
2667 if (options
& OPT_TTYOUT
)
2673 if (options
& OPT_TTYOUT
) {
2678 fprintf(stderr
, "Folder not found.\n");
2681 safe_signal(SIGINT
, saveint
);
2682 safe_signal(SIGPIPE
, savepipe
);
2683 if (options
& OPT_TTYOUT
)
2696 long n
= 0, mx
= 0, columns
, width
;
2700 if ((out
= Ftmp(NULL
, "imapdopr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
2706 while ((c
= getc(fp
)) != EOF
) {
2717 if (mx
< width
/ 2) {
2718 columns
= width
/ (mx
+2);
2719 snprintf(o
, sizeof o
, "sort | pr -%lu -w%lu -t", columns
, width
);
2721 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
2722 run_command(XSHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
2723 page_or_print(out
, 0);
2730 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
2734 int twice
= 0, stored
= 0;
2735 FILE *queuefp
= NULL
;
2736 enum okay ok
= STOP
;
2739 if (mp
->mb_type
== MB_CACHE
) {
2740 if ((queuefp
= cache_queue(mp
)) == NULL
)
2744 qname
= imap_quotestr(name
= imap_fileof(name
));
2745 /* Since it is not possible to set flags on the copy, recently
2746 * set flags must be set on the original to include it in the copy */
2747 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
))
2748 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
2749 if (m
->m_flag
&MFLAG
)
2750 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2751 if (m
->m_flag
&MUNFLAG
)
2752 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2753 if (m
->m_flag
&MANSWER
)
2754 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2755 if (m
->m_flag
&MUNANSWER
)
2756 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2757 if (m
->m_flag
&MDRAFT
)
2758 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2759 if (m
->m_flag
&MUNDRAFT
)
2760 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2763 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n", tag(1), m
->m_uid
, qname
);
2765 if (check_expunged() == STOP
)
2767 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n", tag(1), n
, qname
);
2769 IMAP_OUT(o
, MB_COMD
, goto out
)
2770 while (mp
->mb_active
& MB_COMD
)
2771 ok
= imap_answer(mp
, twice
);
2773 if (mp
->mb_type
== MB_IMAP
&& mp
->mb_flags
& MB_UIDPLUS
&&
2774 response_status
== RESPONSE_OK
)
2775 imap_copyuid(mp
, m
, name
);
2777 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
2778 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2779 IMAP_OUT(o
, MB_COMD
, goto out
)
2780 while (mp
->mb_active
& MB_COMD
)
2781 ok
= imap_answer(mp
, 1);
2783 imap_created_mailbox
++;
2788 if (queuefp
!= NULL
)
2791 /* ... and reset the flag to its initial value so that the 'exit'
2792 * command still leaves the message unread */
2794 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
2795 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
2798 if (m
->m_flag
& MFLAG
) {
2799 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2802 if (m
->m_flag
& MUNFLAG
) {
2803 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2806 if (m
->m_flag
& MANSWER
) {
2807 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
2810 if (m
->m_flag
& MUNANSWER
) {
2811 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2814 if (m
->m_flag
& MDRAFT
) {
2815 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2818 if (m
->m_flag
& MUNDRAFT
) {
2819 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2823 mp
->mb_active
|= MB_COMD
;
2824 (void)imap_finish(mp
);
2830 imap_copy(struct message
*m
, int n
, const char *name
)
2832 sighandler_type saveint
, savepipe
;
2833 enum okay rv
= STOP
;
2837 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2838 safe_signal(SIGINT
, &_imap_maincatch
);
2839 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2840 if (sigsetjmp(imapjmp
, 1) == 0) {
2841 if (savepipe
!= SIG_IGN
)
2842 safe_signal(SIGPIPE
, imapcatch
);
2844 rv
= imap_copy1(&mb
, m
, n
, name
);
2846 safe_signal(SIGINT
, saveint
);
2847 safe_signal(SIGPIPE
, savepipe
);
2857 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
2858 unsigned long *olduid
, unsigned long *newuid
)
2864 *uidvalidity
= strtoul(cp
, &xp
, 10);
2865 *olduid
= strtoul(xp
, &yp
, 10);
2866 *newuid
= strtoul(yp
, &zp
, 10);
2867 rv
= (*uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
2868 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']');
2874 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
2881 *uidvalidity
= strtoul(cp
, &xp
, 10);
2882 *uid
= strtoul(xp
, &yp
, 10);
2883 rv
= (*uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' && yp
> xp
&&
2890 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
2895 unsigned long uidvalidity
, olduid
, newuid
;
2900 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
2901 imap_copyuid_parse(&cp
[9], &uidvalidity
, &olduid
, &newuid
) == STOP
)
2906 xmb
.mb_cache_directory
= NULL
;
2907 xmb
.mb_imap_mailbox
= savestr(name
);
2908 xmb
.mb_uidvalidity
= uidvalidity
;
2911 memset(&xm
, 0, sizeof xm
);
2913 if ((rv
= getcache1(mp
, &xm
, NEED_UNSPEC
, 3)) != OKAY
)
2915 getcache(mp
, &xm
, NEED_HEADER
);
2916 getcache(mp
, &xm
, NEED_BODY
);
2918 if ((m
->m_flag
& HAVE_HEADER
) == 0)
2919 getcache(mp
, m
, NEED_HEADER
);
2920 if ((m
->m_flag
& HAVE_BODY
) == 0)
2921 getcache(mp
, m
, NEED_BODY
);
2925 xm
.m_flag
&= ~MFULLYCACHED
;
2926 putcache(&xmb
, &xm
);
2933 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
, long xsize
,
2934 long size
, long lines
, int flag
, const char *name
)
2939 unsigned long uidvalidity
, uid
;
2944 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
2945 imap_appenduid_parse(&cp
[11], &uidvalidity
, &uid
) == STOP
)
2950 xmb
.mb_cache_directory
= NULL
;
2951 xmb
.mb_imap_mailbox
= savestr(name
);
2952 xmb
.mb_uidvalidity
= uidvalidity
;
2953 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
2955 memset(&xm
, 0, sizeof xm
);
2956 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
2958 xm
.m_block
= mailx_blockof(off1
);
2959 xm
.m_offset
= mailx_offsetof(off1
);
2962 xm
.m_lines
= xm
.m_xlines
= lines
;
2964 xm
.m_have
= HAVE_HEADER
| HAVE_BODY
;
2965 putcache(&xmb
, &xm
);
2972 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
2976 long size
, xsize
, ysize
, lines
;
2977 enum mflag flag
= MNEW
;
2978 char *name
, *buf
, *bp
;
2980 size_t bufsize
, buflen
, cnt
;
2981 enum okay rv
= STOP
;
2984 buf
= smalloc(bufsize
= LINESIZE
);
2987 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2990 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
2995 if ((cp
= strrchr(bp
, '{')) == NULL
)
2998 xsize
= atol(&cp
[1]) + 2;
2999 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
3005 imap_getflags(cp
, &cp
, &flag
);
3006 while (*++cp
== ' ')
3009 t
= imap_read_date_time(cp
);
3011 if ((tp
= Ftmp(NULL
, "imapapui", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
3018 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3021 buf
[--buflen
] = '\0';
3022 buf
[buflen
-1] = '\n';
3023 fwrite(buf
, 1, buflen
, tp
);
3030 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
3031 imap_unquotestr(name
));
3041 #ifdef HAVE_IMAP_SEARCH
3043 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
, const char *spec
,
3046 char *o
, *xp
, *cs
, c
;
3048 FILE *queuefp
= NULL
;
3052 enum okay ok
= STOP
;
3056 for (cp
= spec
; *cp
; cp
++)
3059 cp
= charset_get_lc();
3061 if (asccasecmp(cp
, "utf-8") && asccasecmp(cp
, "utf8")) {
3066 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
3067 sz
= strlen(spec
) + 1;
3068 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
3069 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
, FAL0
) == 0 &&
3078 cp
= imap_quotestr(cp
);
3079 cs
= salloc(n
= strlen(cp
) + 10);
3080 snprintf(cs
, n
, "CHARSET %s ", cp
);
3084 o
= ac_alloc(osize
= strlen(spec
) + 60);
3085 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
3086 IMAP_OUT(o
, MB_COMD
, goto out
)
3087 while (mp
->mb_active
& MB_COMD
) {
3088 ok
= imap_answer(mp
, 0);
3089 if (response_status
== RESPONSE_OTHER
&&
3090 response_other
== MAILBOX_DATA_SEARCH
) {
3091 xp
= responded_other_text
;
3092 while (*xp
&& *xp
!= '\r') {
3093 n
= strtoul(xp
, &xp
, 10);
3094 for (i
= 0; i
< cnt
; i
++)
3095 if (m
[i
].m_uid
== n
&& !(m
[i
].m_flag
& MHIDDEN
) &&
3096 (f
== MDELETED
|| !(m
[i
].m_flag
& MDELETED
)))
3107 imap_search1(const char * volatile spec
, int f
)
3109 sighandler_type saveint
, savepipe
;
3110 enum okay
volatile rv
= STOP
;
3113 if (mb
.mb_type
!= MB_IMAP
)
3117 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3118 safe_signal(SIGINT
, &_imap_maincatch
);
3119 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3120 if (sigsetjmp(imapjmp
, 1) == 0) {
3121 if (savepipe
!= SIG_IGN
)
3122 safe_signal(SIGPIPE
, imapcatch
);
3124 rv
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
3126 safe_signal(SIGINT
, saveint
);
3127 safe_signal(SIGPIPE
, savepipe
);
3135 #endif /* HAVE_IMAP_SEARCH */
3138 imap_thisaccount(const char *cp
)
3143 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
3145 else if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
3146 mb
.mb_imap_account
== NULL
)
3149 rv
= !strcmp(protbase(cp
), mb
.mb_imap_account
);
3155 imap_remove(const char * volatile name
)
3157 sighandler_type
volatile saveint
, savepipe
;
3158 enum okay rv
= STOP
;
3161 if (mb
.mb_type
!= MB_IMAP
) {
3162 fprintf(stderr
, "Refusing to remove \"%s\" in disconnected mode.\n",
3167 if (!imap_thisaccount(name
)) {
3168 fprintf(stderr
, "Can only remove mailboxes on current IMAP "
3169 "server: \"%s\" not removed.\n", name
);
3174 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3175 safe_signal(SIGINT
, &_imap_maincatch
);
3176 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3177 if (sigsetjmp(imapjmp
, 1) == 0) {
3178 if (savepipe
!= SIG_IGN
)
3179 safe_signal(SIGPIPE
, imapcatch
);
3181 rv
= imap_remove1(&mb
, imap_fileof(name
));
3183 safe_signal(SIGINT
, saveint
);
3184 safe_signal(SIGPIPE
, savepipe
);
3188 rv
= cache_remove(name
);
3197 imap_remove1(struct mailbox
*mp
, const char *name
)
3199 FILE *queuefp
= NULL
;
3202 enum okay ok
= STOP
;
3205 o
= ac_alloc(os
= 2*strlen(name
) + 100);
3206 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), imap_quotestr(name
));
3207 IMAP_OUT(o
, MB_COMD
, goto out
)
3208 while (mp
->mb_active
& MB_COMD
)
3209 ok
= imap_answer(mp
, 1);
3216 imap_rename(const char *old
, const char *new)
3218 sighandler_type saveint
, savepipe
;
3219 enum okay rv
= STOP
;
3222 if (mb
.mb_type
!= MB_IMAP
) {
3223 fprintf(stderr
, "Refusing to rename mailboxes in disconnected mode.\n");
3227 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3228 fprintf(stderr
, "Can only rename mailboxes on current IMAP "
3229 "server: \"%s\" not renamed to \"%s\".\n", old
, new);
3234 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3235 safe_signal(SIGINT
, &_imap_maincatch
);
3236 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3237 if (sigsetjmp(imapjmp
, 1) == 0) {
3238 if (savepipe
!= SIG_IGN
)
3239 safe_signal(SIGPIPE
, imapcatch
);
3241 rv
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3243 safe_signal(SIGINT
, saveint
);
3244 safe_signal(SIGPIPE
, savepipe
);
3248 rv
= cache_rename(old
, new);
3257 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3259 FILE *queuefp
= NULL
;
3262 enum okay ok
= STOP
;
3265 o
= ac_alloc(os
= 2*strlen(old
) + 2*strlen(new) + 100);
3266 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1), imap_quotestr(old
),
3267 imap_quotestr(new));
3268 IMAP_OUT(o
, MB_COMD
, goto out
)
3269 while (mp
->mb_active
& MB_COMD
)
3270 ok
= imap_answer(mp
, 1);
3277 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3279 char o
[LINESIZE
], *newname
, *buf
, *bp
, *cp
, iob
[4096];
3280 size_t bufsize
, buflen
, cnt
;
3281 long offs
, offs1
, offs2
, octets
;
3282 int twice
, gotcha
= 0;
3283 FILE *queuefp
= NULL
;
3284 enum okay ok
= OKAY
, rok
= OKAY
;
3287 buf
= smalloc(bufsize
= LINESIZE
);
3290 while ((offs1
= ftell(fp
)) >= 0 &&
3291 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) != NULL
) {
3292 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3297 if ((offs
= ftell(fp
)) < 0)
3300 snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3301 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3303 while (digitchar(*cp
))
3309 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3311 IMAP_OUT(o
, MB_COMD
, continue)
3312 while (mp
->mb_active
& MB_COMD
)
3313 ok
= imap_answer(mp
, twice
);
3314 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3316 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3317 imap_copyuid(mp
, NULL
, imap_unquotestr(newname
));
3319 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3320 IMAP_OUT(o
, MB_COMD
, continue)
3321 while (mp
->mb_active
& MB_COMD
)
3322 ok
= imap_answer(mp
, 1);
3325 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3326 if ((cp
= strrchr(bp
, '{')) == NULL
)
3328 octets
= atol(&cp
[1]) + 2;
3329 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3331 IMAP_OUT(o
, MB_COMD
, continue)
3332 while (mp
->mb_active
& MB_COMD
) {
3333 ok
= imap_answer(mp
, twice
);
3334 if (response_type
== RESPONSE_CONT
)
3338 if (twice
++ == 0 && fseek(fp
, offs
, SEEK_SET
) >= 0)
3342 while (octets
> 0) {
3343 size_t n
= (UICMP(z
, octets
, >, sizeof iob
)
3344 ? sizeof iob
: (size_t)octets
);
3346 if (n
!= fread(iob
, 1, n
, fp
))
3348 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3350 swrite(&mp
->mb_sock
, "");
3351 while (mp
->mb_active
& MB_COMD
) {
3352 ok
= imap_answer(mp
, 0);
3353 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
3354 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3359 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3360 if ((offs2
= ftell(fp
)) < 0)
3362 fseek(fp
, offs1
, SEEK_SET
);
3363 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3364 (void)fseek(fp
, offs2
, SEEK_SET
);
3370 fprintf(stderr
, "Invalid command in IMAP cache queue: \"%s\"\n", bp
);
3375 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), newname
);
3376 IMAP_OUT(o
, MB_COMD
, continue)
3377 while (mp
->mb_active
& MB_COMD
)
3378 ok
= imap_answer(mp
, 1);
3384 ftruncate(fileno(fp
), 0);
3392 imap_strex(char const *cp
, char const **xp
)
3401 for (cq
= cp
+ 1; *cq
!= '\0'; ++cq
) {
3404 else if (*cq
== '"')
3410 n
= salloc(cq
- cp
+ 2);
3411 memcpy(n
, cp
, cq
- cp
+1);
3412 n
[cq
- cp
+ 1] = '\0';
3421 check_expunged(void)
3426 if (expunged_messages
> 0) {
3427 fprintf(stderr
, "Command not executed - messages have been expunged\n");
3436 c_connect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
3439 int rv
, omsgCount
= msgCount
;
3443 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
3444 fprintf(stderr
, "Already connected.\n");
3449 if (!url_parse(&url
, CPROTO_IMAP
, mailname
)) {
3453 var_clear_allow_undefined
= TRU1
;
3454 ok_bclear(disconnected
);
3455 vok_bclear(savecat("disconnected-", url
.url_u_h_p
.s
));
3456 var_clear_allow_undefined
= FAL0
;
3458 if (mb
.mb_type
== MB_CACHE
) {
3459 enum fedit_mode fm
= FEDIT_NONE
;
3464 _imap_setfile1(&url
, fm
, 1);
3465 if (msgCount
> omsgCount
)
3466 newmailinfo(omsgCount
);
3475 c_disconnect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
3478 int rv
= 1, *msgvec
= vp
;
3481 if (mb
.mb_type
== MB_CACHE
) {
3482 fprintf(stderr
, "Not connected.\n");
3485 if (mb
.mb_type
!= MB_IMAP
|| cached_uidvalidity(&mb
) == 0) {
3486 fprintf(stderr
, "The current mailbox is not cached.\n");
3490 if (!url_parse(&url
, CPROTO_IMAP
, mailname
))
3495 ok_bset(disconnected
, TRU1
);
3496 if (mb
.mb_type
== MB_IMAP
) {
3497 enum fedit_mode fm
= FEDIT_NONE
;
3502 sclose(&mb
.mb_sock
);
3503 _imap_setfile1(&url
, fm
, 1);
3514 int rv
= 1, *msgvec
= vp
, *ip
;
3518 if (mb
.mb_type
!= MB_IMAP
) {
3519 fprintf(stderr
, "Not connected to an IMAP server.\n");
3522 if (cached_uidvalidity(&mb
) == 0) {
3523 fprintf(stderr
, "The current mailbox is not cached.\n");
3528 for (ip
= msgvec
; *ip
; ++ip
) {
3529 mp
= &message
[*ip
- 1];
3530 if (!(mp
->m_have
& HAVE_BODY
)) {
3543 disconnected(const char *file
)
3549 if (ok_blook(disconnected
)) {
3554 if (!url_parse(&url
, CPROTO_IMAP
, file
)) {
3558 rv
= vok_blook(savecat("disconnected-", url
.url_u_h_p
.s
));
3566 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
3568 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
3576 while (PTRCMP(omp
, <, omessage
+ omsgCount
) &&
3577 PTRCMP(nmp
, <, message
+ msgCount
)) {
3578 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
3580 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
3582 if (omp
->m_uid
== nmp
->m_uid
) {
3583 hf
= nmp
->m_flag
& MHIDDEN
;
3584 if (transparent
&& mb
.mb_type
== MB_IMAP
)
3585 omp
->m_flag
&= ~MHIDDEN
;
3587 if (transparent
&& mb
.mb_type
== MB_CACHE
)
3588 nmp
[-1].m_flag
|= hf
;
3589 } else if (omp
->m_uid
< nmp
->m_uid
)
3596 prevdot
= newprevdot
;
3602 imap_read_date_time(const char *cp
)
3606 int i
, year
, month
, day
, hour
, minute
, second
, sign
= -1;
3609 /* "25-Jul-2004 15:33:44 +0200"
3611 * 0 5 10 15 20 25 */
3612 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
3614 day
= strtol(&cp
[1], NULL
, 10);
3616 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
3618 if (month_names
[++i
][0] == '\0')
3622 year
= strtol(&cp
[8], NULL
, 10);
3623 hour
= strtol(&cp
[13], NULL
, 10);
3624 minute
= strtol(&cp
[16], NULL
, 10);
3625 second
= strtol(&cp
[19], NULL
, 10);
3626 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
3640 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
3643 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
3653 imap_make_date_time(time_t t
)
3657 int tzdiff
, tzdiff_hour
, tzdiff_min
;
3660 tzdiff
= t
- mktime(gmtime(&t
));
3661 tzdiff_hour
= (int)(tzdiff
/ 60);
3662 tzdiff_min
= tzdiff_hour
% 60;
3664 tmptr
= localtime(&t
);
3665 if (tmptr
->tm_isdst
> 0)
3667 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
3668 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
], tmptr
->tm_year
+ 1900,
3669 tmptr
->tm_hour
, tmptr
->tm_min
, tmptr
->tm_sec
, tzdiff_hour
, tzdiff_min
);
3673 #endif /* HAVE_IMAP */
3675 #if defined HAVE_IMAP || defined HAVE_IMAP_SEARCH
3677 imap_quotestr(char const *s
)
3682 np
= n
= salloc(2 * strlen(s
) + 3);
3685 if (*s
== '"' || *s
== '\\')
3696 imap_unquotestr(char const *s
)
3706 np
= n
= salloc(strlen(s
) + 1);
3719 #endif /* defined HAVE_IMAP || defined HAVE_IMAP_SEARCH */