1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ IMAP v4r1 client following RFC 2060.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
9 * Gunnar Ritter. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Gunnar Ritter
22 * and his contributors.
23 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
45 # include <sys/socket.h>
49 # include <netinet/in.h>
51 # ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h>
57 #define IMAP_ANSWER() \
59 if (mp->mb_type != MB_CACHE) {\
61 while (mp->mb_active & MB_COMD)\
62 ok = imap_answer(mp, 1);\
68 /* TODO IMAP_OUT() simply returns instead of doing "actioN" if imap_finish()
69 * TODO fails, which leaves behind leaks in, e.g., imap_append1()!
70 * TODO IMAP_XOUT() was added due to this, but (1) needs to be used everywhere
71 * TODO and (2) doesn't handle all I/O errors itself, yet, too.
72 * TODO I.e., that should be a function, not a macro ... or so.
73 * TODO This entire module needs MASSIVE work! */
74 #define IMAP_OUT(X,Y,ACTION) IMAP_XOUT(X, Y, ACTION, return STOP)
75 #define IMAP_XOUT(X,Y,ACTIONERR,ACTIONBAIL) \
77 if (mp->mb_type != MB_CACHE) {\
78 if (imap_finish(mp) == STOP) {\
81 if (options & OPT_VERBOSE)\
82 fprintf(stderr, ">>> %s", X);\
84 if (swrite(&mp->mb_sock, X) == STOP) {\
93 static struct record
{
94 struct record
*rec_next
;
95 unsigned long rec_count
;
120 static char *responded_tag
;
121 static char *responded_text
;
122 static char *responded_other_text
;
123 static long responded_other_number
;
129 MAILBOX_DATA_MAILBOX
,
134 MESSAGE_DATA_EXPUNGE
,
137 RESPONSE_OTHER_UNKNOWN
140 static enum list_attributes
{
142 LIST_NOINFERIORS
= 001,
148 static int list_hierarchy_delimiter
;
149 static char *list_name
;
152 struct list_item
*l_next
;
155 enum list_attributes l_attr
;
161 static char *imapbuf
; /* TODO not static, use pool */
162 static size_t imapbufsize
;
163 static sigjmp_buf imapjmp
;
164 static sighandler_type savealrm
;
165 static int imapkeepalive
;
166 static long had_exists
= -1;
167 static long had_expunge
= -1;
168 static long expunged_messages
;
169 static int volatile imaplock
;
170 static int same_imap_account
;
172 static void imap_other_get(char *pp
);
173 static void imap_response_get(const char **cp
);
174 static void imap_response_parse(void);
175 static enum okay
imap_answer(struct mailbox
*mp
, int errprnt
);
176 static enum okay
imap_parse_list(void);
177 static enum okay
imap_finish(struct mailbox
*mp
);
178 static void imap_timer_off(void);
179 static void imapcatch(int s
);
180 static void _imap_maincatch(int s
);
181 static enum okay
imap_noop1(struct mailbox
*mp
);
182 static void rec_queue(enum rec_type type
, unsigned long cnt
);
183 static enum okay
rec_dequeue(void);
184 static void rec_rmqueue(void);
185 static void imapalarm(int s
);
186 static int imap_use_starttls(const char *uhp
);
187 static enum okay
imap_preauth(struct mailbox
*mp
, const char *xserver
,
189 static enum okay
imap_capability(struct mailbox
*mp
);
190 static enum okay
imap_auth(struct mailbox
*mp
, const char *uhp
,
191 char *xuser
, char *pass
);
193 static enum okay
imap_cram_md5(struct mailbox
*mp
, char *xuser
, char *xpass
);
195 static enum okay
imap_login(struct mailbox
*mp
, char *xuser
, char *xpass
);
197 static enum okay
imap_gss(struct mailbox
*mp
, char *user
);
199 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
200 static void imap_init(struct mailbox
*mp
, int n
);
201 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
203 static void imap_split(char **server
, const char **sp
, int *use_ssl
,
204 const char **cp
, char const **uhp
, char const **mbx
,
205 char **pass
, char **user
);
206 static int imap_setfile1(const char *xserver
, int nmail
, int isedit
,
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_VERBOSE
)
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
, tr(270, "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 */
573 termios_state_reset();
576 fprintf(stderr
, tr(102, "Interrupt\n"));
577 siglongjmp(imapjmp
, 1);
580 fprintf(stderr
, tr(98, "Received SIGPIPE during IMAP operation\n"));
586 _imap_maincatch(int s
)
588 NYD_X
; /* Signal handler */
590 if (interrupts
++ == 0) {
591 fprintf(stderr
, tr(102, "Interrupt\n"));
598 imap_noop1(struct mailbox
*mp
)
601 FILE *queuefp
= NULL
;
604 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
605 IMAP_OUT(o
, MB_COMD
, return STOP
)
611 imap_fileof(char const *xcp
)
613 char const *cp
= xcp
;
618 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
622 if (cp
[0] == '/' && state
== 1) {
640 sighandler_type
volatile oldint
, oldpipe
;
644 if (mb
.mb_type
!= MB_IMAP
)
648 if ((oldint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
649 safe_signal(SIGINT
, &_imap_maincatch
);
650 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
651 if (sigsetjmp(imapjmp
, 1) == 0) {
652 if (oldpipe
!= SIG_IGN
)
653 safe_signal(SIGPIPE
, imapcatch
);
655 rv
= imap_noop1(&mb
);
657 safe_signal(SIGINT
, oldint
);
658 safe_signal(SIGPIPE
, oldpipe
);
668 rec_queue(enum rec_type rt
, unsigned long cnt
)
673 rp
= scalloc(1, sizeof *rp
);
676 if (record
&& recend
) {
677 recend
->rec_next
= rp
;
680 record
= recend
= rp
;
687 struct message
*omessage
;
688 struct record
*rp
, *rq
;
697 message
= smalloc((msgCount
+1) * sizeof *message
);
699 memcpy(message
, omessage
, msgCount
* sizeof *message
);
700 memset(&message
[msgCount
], 0, sizeof *message
);
702 rp
= record
, rq
= NULL
;
705 switch (rp
->rec_type
) {
707 exists
= rp
->rec_count
;
710 if (rp
->rec_count
== 0) {
714 if (rp
->rec_count
> (unsigned long)msgCount
) {
715 if (exists
== 0 || rp
->rec_count
> exists
--)
721 delcache(&mb
, &message
[rp
->rec_count
-1]);
722 memmove(&message
[rp
->rec_count
-1], &message
[rp
->rec_count
],
723 ((msgCount
- rp
->rec_count
+ 1) * sizeof *message
));
725 /* If the message was part of a collapsed thread,
726 * the m_collapsed field of one of its ancestors
727 * should be incremented. It seems hardly possible
728 * to do this with the current message structure,
729 * though. The result is that a '+' may be shown
730 * in the header summary even if no collapsed
742 record
= recend
= NULL
;
743 if (rv
== OKAY
&& UICMP(z
, exists
, >, msgCount
)) {
744 message
= srealloc(message
, (exists
+ 1) * sizeof *message
);
745 memset(&message
[msgCount
], 0, (exists
- msgCount
+ 1) * sizeof *message
);
746 for (i
= msgCount
; i
< exists
; ++i
)
748 imap_flags(&mb
, msgCount
+1, exists
);
767 for (rp
= record
; rp
!= NULL
;) {
768 struct record
*tmp
= rp
;
772 record
= recend
= NULL
;
780 sighandler_type
volatile saveint
, savepipe
;
781 NYD_X
; /* Signal handler */
784 if (imaplock
++ == 0) {
785 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
786 safe_signal(SIGINT
, &_imap_maincatch
);
787 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
788 if (sigsetjmp(imapjmp
, 1)) {
789 safe_signal(SIGINT
, saveint
);
790 safe_signal(SIGPIPE
, savepipe
);
793 if (savepipe
!= SIG_IGN
)
794 safe_signal(SIGPIPE
, imapcatch
);
795 if (imap_noop1(&mb
) != OKAY
) {
796 safe_signal(SIGINT
, saveint
);
797 safe_signal(SIGPIPE
, savepipe
);
800 safe_signal(SIGINT
, saveint
);
801 safe_signal(SIGPIPE
, savepipe
);
804 alarm(imapkeepalive
);
810 imap_use_starttls(const char *uhp
)
815 if (ok_blook(imap_use_starttls
))
818 char *var
= savecat("imap-use-starttls-", uhp
);
826 imap_preauth(struct mailbox
*mp
, const char *xserver
, const char *uhp
)
831 mp
->mb_active
|= MB_PREAUTH
;
833 if ((cp
= strchr(xserver
, ':')) != NULL
) {
834 char *x
= salloc(cp
- xserver
+ 1);
835 memcpy(x
, xserver
, cp
- xserver
);
836 x
[cp
- xserver
] = '\0';
840 if (mp
->mb_sock
.s_use_ssl
== 0 && imap_use_starttls(uhp
)) {
841 FILE *queuefp
= NULL
;
844 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
845 IMAP_OUT(o
, MB_COMD
, return STOP
)
847 if (ssl_open(xserver
, &mp
->mb_sock
, uhp
) != OKAY
)
851 if (imap_use_starttls(uhp
)) {
852 fprintf(stderr
, "No SSL support compiled in.\n");
861 imap_capability(struct mailbox
*mp
)
864 FILE *queuefp
= NULL
;
869 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
870 IMAP_OUT(o
, MB_COMD
, return STOP
)
871 while (mp
->mb_active
& MB_COMD
) {
872 ok
= imap_answer(mp
, 0);
873 if (response_status
== RESPONSE_OTHER
&&
874 response_other
== CAPABILITY_DATA
) {
875 cp
= responded_other_text
;
877 while (spacechar(*cp
))
879 if (strncmp(cp
, "UIDPLUS", 7) == 0 && spacechar(cp
[7]))
881 mp
->mb_flags
|= MB_UIDPLUS
;
882 while (*cp
&& !spacechar(*cp
))
891 imap_auth(struct mailbox
*mp
, const char *uhp
, char *xuser
, char *pass
)
897 if (!(mp
->mb_active
& MB_PREAUTH
)) {
902 if ((auth
= ok_vlook(imap_auth
)) == NULL
) {
903 size_t i
= strlen(uhp
) + 1;
904 var
= ac_alloc(i
+ 10);
905 memcpy(var
, "imap-auth-", 10);
906 memcpy(var
+ 10, uhp
, i
);
907 auth
= vok_vlook(var
);
911 if (auth
== NULL
|| !strcmp(auth
, "login"))
912 rv
= imap_login(mp
, xuser
, pass
);
913 else if (!strcmp(auth
, "cram-md5")) {
915 rv
= imap_cram_md5(mp
, xuser
, pass
);
917 fprintf(stderr
, tr(277, "No CRAM-MD5 support compiled in.\n"));
920 } else if (!strcmp(auth
, "gssapi")) {
922 rv
= imap_gss(mp
, xuser
);
924 fprintf(stderr
, tr(272, "No GSSAPI support compiled in.\n"));
928 fprintf(stderr
, tr(273,
929 "Unknown IMAP authentication method: %s\n"), auth
);
937 /* Implementation of RFC 2194 */
940 imap_cram_md5(struct mailbox
*mp
, char *xuser
, char *xpass
)
942 char o
[LINESIZE
], *user
, *pass
, *cp
;
943 FILE *queuefp
= NULL
;
950 if (!getcredentials(&user
, &pass
))
953 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
954 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
956 if (response_type
!= RESPONSE_CONT
)
959 cp
= cram_md5_string(user
, pass
, responded_text
);
960 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
961 while (mp
->mb_active
& MB_COMD
)
962 rv
= imap_answer(mp
, 1);
971 #endif /* HAVE_MD5 */
974 imap_login(struct mailbox
*mp
, char *xuser
, char *xpass
)
978 FILE *queuefp
= NULL
;
985 if (!getcredentials(&user
, &pass
))
988 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
989 tag(1), imap_quotestr(user
), imap_quotestr(pass
));
990 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
991 while (mp
->mb_active
& MB_COMD
)
992 rv
= imap_answer(mp
, 1);
1003 # include "imap_gssapi.h"
1007 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
)
1009 enum okay ok
= OKAY
;
1012 FILE *queuefp
= NULL
;
1016 mp
->mb_uidvalidity
= 0;
1017 snprintf(o
, sizeof o
, "%s SELECT %s\r\n", tag(1), imap_quotestr(mbx
));
1018 IMAP_OUT(o
, MB_COMD
, return STOP
)
1019 while (mp
->mb_active
& MB_COMD
) {
1020 ok
= imap_answer(mp
, 1);
1021 if (response_status
!= RESPONSE_OTHER
&&
1022 (cp
= asccasestr(responded_text
, "[UIDVALIDITY ")) != NULL
)
1023 mp
->mb_uidvalidity
= atol(&cp
[13]);
1025 *cnt
= (had_exists
> 0) ? had_exists
: 0;
1026 if (response_status
!= RESPONSE_OTHER
&&
1027 ascncasecmp(responded_text
, "[READ-ONLY] ", 12) == 0)
1033 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
1036 FILE *queuefp
= NULL
;
1039 unsigned x
= X
, y
= Y
, n
;
1042 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
1043 IMAP_OUT(o
, MB_COMD
, return STOP
)
1044 while (mp
->mb_active
& MB_COMD
) {
1046 if (response_status
== RESPONSE_OTHER
&&
1047 response_other
== MESSAGE_DATA_FETCH
) {
1048 n
= responded_other_number
;
1056 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1061 imap_getflags(cp
, &cp
, &m
->m_flag
);
1064 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1065 m
->m_uid
= strtoul(&cp
[4], NULL
, 10);
1066 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1067 m
->m_flag
&= ~MHIDDEN
;
1070 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1072 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1075 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1077 IMAP_OUT(o
, MB_COMD
, return STOP
)
1078 while (mp
->mb_active
& MB_COMD
) {
1080 if (response_status
== RESPONSE_OTHER
&&
1081 response_other
== MESSAGE_DATA_FETCH
) {
1082 n
= responded_other_number
;
1088 if ((cp
= asccasestr(responded_other_text
, "RFC822.SIZE ")) != NULL
)
1089 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1090 if ((cp
= asccasestr(responded_other_text
, "INTERNALDATE ")) != NULL
)
1091 m
->m_time
= imap_read_date_time(&cp
[13]);
1095 for (n
= X
; n
<= Y
; n
++)
1096 putcache(mp
, &message
[n
-1]);
1101 imap_init(struct mailbox
*mp
, int n
)
1108 m
->m_flag
= MUSED
| MNOFROM
;
1115 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1117 struct message
*omessage
= 0;
1118 int i
, omsgCount
= 0;
1119 enum okay dequeued
= STOP
;
1122 if (nmail
|| transparent
) {
1124 omsgCount
= msgCount
;
1127 dequeued
= rec_dequeue();
1129 if (had_exists
>= 0) {
1130 if (dequeued
!= OKAY
)
1131 msgCount
= had_exists
;
1134 if (had_expunge
>= 0) {
1135 if (dequeued
!= OKAY
)
1136 msgCount
-= had_expunge
;
1140 if (nmail
&& expunged_messages
)
1141 printf("Expunged %ld message%s.\n", expunged_messages
,
1142 (expunged_messages
!= 1 ? "s" : ""));
1143 *prevcount
= omsgCount
- expunged_messages
;
1144 expunged_messages
= 0;
1146 fputs("IMAP error: Negative message count\n", stderr
);
1150 if (dequeued
!= OKAY
) {
1151 message
= scalloc(msgCount
+ 1, sizeof *message
);
1152 for (i
= 0; i
< msgCount
; i
++)
1154 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1157 imap_flags(mp
, 1, msgCount
);
1158 message
[msgCount
].m_size
= 0;
1159 message
[msgCount
].m_lines
= 0;
1162 if (nmail
|| transparent
)
1163 transflags(omessage
, omsgCount
, transparent
);
1170 imap_split(char **server
, const char **sp
, int *use_ssl
, const char **cp
,
1171 char const **uhp
, char const **mbx
, char **pass
, char **user
)
1175 if (strncmp(*sp
, "imap://", 7) == 0) {
1179 } else if (strncmp(*sp
, "imaps://", 8) == 0) {
1184 *use_ssl
= 0; /* (silence CC) */
1186 if ((*cp
= strchr(*sp
, '/')) != NULL
&& (*cp
)[1] != '\0') {
1187 char *x
= savestr(*sp
);
1188 x
[*cp
- *sp
] = '\0';
1193 (*server
)[*cp
- *server
] = '\0';
1198 *pass
= lookup_password_for_token(*uhp
);
1199 if ((*cp
= last_at_before_slash(*uhp
)) != NULL
) {
1200 *user
= salloc(*cp
- *uhp
+ 1);
1201 memcpy(*user
, *uhp
, *cp
- *uhp
);
1202 (*user
)[*cp
- *uhp
] = '\0';
1204 *user
= urlxdec(*user
);
1213 imap_setfile(const char *xserver
, int nmail
, int isedit
)
1218 rv
= imap_setfile1(xserver
, nmail
, isedit
, 0);
1224 imap_setfile1(const char *xserver
, int nmail
, int isedit
,
1225 int volatile transparent
)
1228 sighandler_type
volatile saveint
, savepipe
;
1229 char *server
, *user
, *pass
, *acc
;
1230 char const *cp
, *sp
, * volatile mbx
, *uhp
;
1231 int rv
, use_ssl
= 0, prevcount
= 0;
1232 enum mbflags same_flags
;
1235 server
= savestr(xserver
);
1237 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1238 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1239 if (saveint
!= SIG_IGN
)
1240 safe_signal(SIGINT
, imapcatch
);
1241 if (savepipe
!= SIG_IGN
)
1242 safe_signal(SIGPIPE
, imapcatch
);
1247 same_flags
= mb
.mb_flags
;
1248 same_imap_account
= 0;
1249 sp
= protbase(server
);
1250 if (mb
.mb_imap_account
&& mb
.mb_type
== MB_IMAP
) {
1251 if (mb
.mb_sock
.s_fd
> 0 && strcmp(mb
.mb_imap_account
, sp
) == 0 &&
1252 disconnected(mb
.mb_imap_account
) == 0)
1253 same_imap_account
= 1;
1259 imap_split(&server
, &sp
, &use_ssl
, &cp
, &uhp
, &xmbx
, &pass
, &user
);
1263 if (!same_imap_account
) {
1264 if (!disconnected(acc
) &&
1265 sopen(sp
, &so
, use_ssl
, uhp
, (use_ssl
? "imaps" : "imap")
1276 edit
= (isedit
!= 0);
1277 if (mb
.mb_imap_account
!= NULL
)
1278 free(mb
.mb_imap_account
);
1279 mb
.mb_imap_account
= acc
;
1280 if (!same_imap_account
) {
1281 if (mb
.mb_sock
.s_fd
>= 0)
1282 sclose(&mb
.mb_sock
);
1284 same_imap_account
= 0;
1295 if (mb
.mb_imap_mailbox
!= NULL
)
1296 free(mb
.mb_imap_mailbox
);
1297 mb
.mb_imap_mailbox
= sstrdup(mbx
);
1300 mb
.mb_type
= MB_VOID
;
1301 mb
.mb_active
= MB_NONE
;
1304 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1305 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1306 if (sigsetjmp(imapjmp
, 1)) {
1307 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1308 sclose(&mb
.mb_sock
);
1309 safe_signal(SIGINT
, saveint
);
1310 safe_signal(SIGPIPE
, savepipe
);
1313 mb
.mb_type
= MB_VOID
;
1314 mb
.mb_active
= MB_NONE
;
1318 if (saveint
!= SIG_IGN
)
1319 safe_signal(SIGINT
, imapcatch
);
1320 if (savepipe
!= SIG_IGN
)
1321 safe_signal(SIGPIPE
, imapcatch
);
1323 if (mb
.mb_sock
.s_fd
< 0) {
1324 if (disconnected(mb
.mb_imap_account
)) {
1325 if (cache_setptr(transparent
) == STOP
)
1327 "Mailbox \"%s\" is not cached.\n",
1331 if ((cp
= ok_vlook(imap_keepalive
)) != NULL
) {
1332 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1333 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1334 alarm(imapkeepalive
);
1339 mb
.mb_sock
.s_desc
= "IMAP";
1340 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1341 if (imap_preauth(&mb
, sp
, uhp
) != OKAY
||
1342 imap_auth(&mb
, uhp
, user
, pass
) != OKAY
) {
1343 sclose(&mb
.mb_sock
);
1345 safe_signal(SIGINT
, saveint
);
1346 safe_signal(SIGPIPE
, savepipe
);
1351 } else /* same account */
1352 mb
.mb_flags
|= same_flags
;
1354 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
1355 mb
.mb_type
= MB_IMAP
;
1357 if (imap_select(&mb
, &mailsize
, &msgCount
, mbx
) != OKAY
) {
1358 /*sclose(&mb.mb_sock);
1360 safe_signal(SIGINT
, saveint
);
1361 safe_signal(SIGPIPE
, savepipe
);
1363 mb
.mb_type
= MB_VOID
;
1369 imap_setptr(&mb
, nmail
, transparent
, &prevcount
);
1372 if (!nmail
&& !transparent
)
1374 safe_signal(SIGINT
, saveint
);
1375 safe_signal(SIGPIPE
, savepipe
);
1378 if (!nmail
&& mb
.mb_type
== MB_IMAP
)
1379 purgecache(&mb
, message
, msgCount
);
1380 if ((nmail
|| transparent
) && mb
.mb_sorted
) {
1385 if (!nmail
&& !edit
&& msgCount
== 0) {
1386 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1387 !ok_blook(emptystart
))
1388 fprintf(stderr
, tr(258, "No mail at %s\n"), server
);
1393 newmailinfo(prevcount
);
1401 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1402 int need
, const char *head
, size_t headsize
, long headlines
)
1404 char *line
= NULL
, *lp
;
1405 size_t linesize
= 0, linelen
, size
= 0;
1406 int emptyline
= 0, lines
= 0, excess
= 0;
1410 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1411 offset
= ftell(mp
->mb_otf
);
1414 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1416 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1418 if (linelen
> expected
) {
1419 excess
= linelen
- expected
;
1423 * Need to mask 'From ' lines. This cannot be done properly
1424 * since some servers pass them as 'From ' and others as
1425 * '>From '. Although one could identify the first kind of
1426 * server in principle, it is not possible to identify the
1427 * second as '>From ' may also come from a server of the
1428 * first type as actual data. So do what is absolutely
1429 * necessary only - mask 'From '.
1431 * If the line is the first line of the message header, it
1432 * is likely a real 'From ' line. In this case, it is just
1433 * ignored since it violates all standards.
1434 * TODO can the latter *really* happen??
1437 /* Since we simply copy over data without doing any transfer
1438 * encoding reclassification/adjustment we *have* to perform
1439 * RFC 4155 compliant From_ quoting here */
1440 if (is_head(lp
, linelen
)) {
1441 if (lines
+ headlines
== 0)
1443 fputc('>', mp
->mb_otf
);
1446 if (lp
[linelen
-1] == '\n' && (linelen
== 1 || lp
[linelen
-2] == '\r')) {
1447 emptyline
= linelen
<= 2;
1449 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1450 size
+= linelen
- 1;
1453 fputc('\n', mp
->mb_otf
);
1456 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1461 if ((expected
-= linelen
) <= 0)
1465 /* This is very ugly; but some IMAP daemons don't end a
1466 * message with \r\n\r\n, and we need \n\n for mbox format */
1467 fputc('\n', mp
->mb_otf
);
1474 m
->m_size
= size
+ headsize
;
1475 m
->m_lines
= lines
+ headlines
;
1476 m
->m_block
= mailx_blockof(offset
);
1477 m
->m_offset
= mailx_offsetof(offset
);
1480 m
->m_have
|= HAVE_HEADER
;
1483 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1484 m
->m_xlines
= m
->m_lines
;
1485 m
->m_xsize
= m
->m_size
;
1495 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1496 const char *head
, size_t headsize
, long headlines
)
1503 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1504 offset
= ftell(mp
->mb_otf
);
1506 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1508 fwrite(str
, 1, len
, mp
->mb_otf
);
1509 fputc('\n', mp
->mb_otf
);
1515 m
->m_size
= headsize
+ len
;
1516 m
->m_lines
= headlines
+ 1;
1517 m
->m_block
= mailx_blockof(offset
);
1518 m
->m_offset
= mailx_offsetof(offset
);
1519 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1520 m
->m_xlines
= m
->m_lines
;
1521 m
->m_xsize
= m
->m_size
;
1527 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
1531 sighandler_type
volatile saveint
, savepipe
;
1532 char * volatile head
;
1533 char const *cp
, *loc
, * volatile item
, * volatile resp
;
1535 size_t volatile headsize
;
1538 long volatile headlines
;
1544 saveint
= savepipe
= SIG_IGN
;
1546 cp
= loc
= item
= resp
= NULL
;
1548 number
= (int)PTR2SIZE(m
- message
+ 1);
1555 if (getcache(mp
, m
, need
) == OKAY
)
1557 if (mp
->mb_type
== MB_CACHE
) {
1558 fprintf(stderr
, "Message %u not available.\n", number
);
1562 if (mp
->mb_sock
.s_fd
< 0) {
1563 fprintf(stderr
, "IMAP connection closed.\n");
1569 resp
= item
= "RFC822.HEADER";
1572 item
= "BODY.PEEK[]";
1574 if (m
->m_flag
& HAVE_HEADER
&& m
->m_size
) {
1575 char *hdr
= smalloc(m
->m_size
);
1577 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
1579 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
) != m
->m_size
) {
1584 headsize
= m
->m_size
;
1585 headlines
= m
->m_lines
;
1586 item
= "BODY.PEEK[TEXT]";
1587 resp
= "BODY[TEXT]";
1595 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1596 if (sigsetjmp(imapjmp
, 1)) {
1597 safe_signal(SIGINT
, saveint
);
1598 safe_signal(SIGPIPE
, savepipe
);
1602 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1603 safe_signal(SIGINT
, &_imap_maincatch
);
1604 if (savepipe
!= SIG_IGN
)
1605 safe_signal(SIGPIPE
, imapcatch
);
1608 snprintf(o
, sizeof o
, "%s UID FETCH %lu (%s)\r\n",
1609 tag(1), m
->m_uid
, item
);
1611 if (check_expunged() == STOP
)
1613 snprintf(o
, sizeof o
, "%s FETCH %u (%s)\r\n", tag(1), number
, item
);
1615 IMAP_OUT(o
, MB_COMD
, goto out
)
1617 ok
= imap_answer(mp
, 1);
1620 if (response_status
!= RESPONSE_OTHER
||
1621 response_other
!= MESSAGE_DATA_FETCH
)
1623 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
1626 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
1634 n
= responded_other_number
;
1635 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
1636 if (m
->m_uid
? m
->m_uid
!= u
: n
!= number
)
1638 if ((cp
= strchr(loc
, '"')) != NULL
) {
1639 cp
= imap_unquotestr(cp
);
1640 imap_putstr(mp
, m
, cp
, head
, headsize
, headlines
);
1642 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1643 m
->m_xlines
= m
->m_lines
;
1644 m
->m_xsize
= m
->m_size
;
1648 expected
= atol(&cp
[1]);
1649 if (m
->m_uid
? n
== 0 && m
->m_uid
!= u
: n
!= number
) {
1650 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
1654 imap_fetchdata(mp
, &mt
, expected
, need
, head
, headsize
, headlines
);
1656 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1659 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1660 if (options
& OPT_VERBOSE
)
1661 fputs(imapbuf
, stderr
);
1662 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1664 if (u
== m
->m_uid
) {
1665 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1672 while (mp
->mb_active
& MB_COMD
)
1673 ok
= imap_answer(mp
, 1);
1675 if (saveint
!= SIG_IGN
)
1676 safe_signal(SIGINT
, saveint
);
1677 if (savepipe
!= SIG_IGN
)
1678 safe_signal(SIGPIPE
, savepipe
);
1691 imap_header(struct message
*m
)
1696 rv
= imap_get(&mb
, m
, NEED_HEADER
);
1703 imap_body(struct message
*m
)
1708 rv
= imap_get(&mb
, m
, NEED_BODY
);
1714 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
1718 tomp
->m_size
= frommp
->m_size
;
1719 tomp
->m_lines
= frommp
->m_lines
;
1720 tomp
->m_block
= frommp
->m_block
;
1721 tomp
->m_offset
= frommp
->m_offset
;
1722 tomp
->m_have
= have
;
1723 if (have
& HAVE_BODY
) {
1724 tomp
->m_xlines
= frommp
->m_lines
;
1725 tomp
->m_xsize
= frommp
->m_size
;
1732 imap_fetchheaders(struct mailbox
*mp
, struct message
*m
, int bot
, int topp
)
1740 FILE *queuefp
= NULL
;
1745 snprintf(o
, sizeof o
, "%s UID FETCH %lu:%lu (RFC822.HEADER)\r\n",
1746 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
1748 if (check_expunged() == STOP
)
1750 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.HEADER)\r\n",
1753 IMAP_OUT(o
, MB_COMD
, return STOP
)
1755 ok
= imap_answer(mp
, 1);
1756 if (response_status
!= RESPONSE_OTHER
)
1758 if (response_other
!= MESSAGE_DATA_FETCH
)
1760 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0)
1762 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
1764 expected
= atol(&cp
[1]);
1765 if (m
[bot
-1].m_uid
) {
1766 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
) {
1768 for (n
= bot
; n
<= topp
; n
++)
1769 if ((unsigned long)u
== m
[n
-1].m_uid
)
1772 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1778 n
= responded_other_number
;
1779 if (n
<= 0 || n
> msgCount
) {
1780 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1784 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
1785 if (n
>= 0 && !(m
[n
-1].m_have
& HAVE_HEADER
))
1786 commitmsg(mp
, &m
[n
-1], &mt
, HAVE_HEADER
);
1787 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1788 if (options
& OPT_VERBOSE
)
1789 fputs(imapbuf
, stderr
);
1790 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1792 for (n
= bot
; n
<= topp
; n
++)
1793 if ((unsigned long)u
== m
[n
-1].m_uid
)
1795 if (n
<= topp
&& !(m
[n
-1].m_have
& HAVE_HEADER
))
1796 commitmsg(mp
, &m
[n
-1], &mt
,HAVE_HEADER
);
1801 while (mp
->mb_active
& MB_COMD
)
1802 ok
= imap_answer(mp
, 1);
1807 imap_getheaders(int volatile bot
, int topp
) /* TODO should take iterator!! */
1809 sighandler_type saveint
, savepipe
;
1810 /* enum okay ok = STOP;*/
1814 if (mb
.mb_type
== MB_CACHE
)
1818 if (topp
> msgCount
)
1820 for (i
= bot
; i
< topp
; i
++) {
1821 if (message
[i
-1].m_have
& HAVE_HEADER
||
1822 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1827 for (i
= topp
; i
> bot
; i
--) {
1828 if (message
[i
-1].m_have
& HAVE_HEADER
||
1829 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1838 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1839 safe_signal(SIGINT
, &_imap_maincatch
);
1840 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1841 if (sigsetjmp(imapjmp
, 1) == 0) {
1842 if (savepipe
!= SIG_IGN
)
1843 safe_signal(SIGPIPE
, imapcatch
);
1845 for (i
= bot
; i
<= topp
; i
+= chunk
) {
1846 int j
= i
+ chunk
- 1;
1847 /*ok = */imap_fetchheaders(&mb
, message
, i
, (j
< topp
? j
: topp
));
1849 onintr(0); /* XXX imaplock? */
1852 safe_signal(SIGINT
, saveint
);
1853 safe_signal(SIGPIPE
, savepipe
);
1858 __imap_exit(struct mailbox
*mp
)
1861 FILE *queuefp
= NULL
;
1864 mp
->mb_active
|= MB_BYE
;
1865 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
1866 IMAP_OUT(o
, MB_COMD
, return STOP
)
1872 imap_exit(struct mailbox
*mp
)
1877 rv
= __imap_exit(mp
);
1878 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
1879 free(mp
->mb_imap_account
);
1880 free(mp
->mb_imap_mailbox
);
1881 if (mp
->mb_cache_directory
!= NULL
)
1882 free(mp
->mb_cache_directory
);
1883 #ifndef HAVE_DEBUG /* TODO ASSERT LEGACY */
1884 mp
->mb_imap_account
=
1885 mp
->mb_imap_mailbox
=
1886 mp
->mb_cache_directory
= "";
1888 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
1889 mp
->mb_imap_mailbox
= NULL
;
1890 mp
->mb_cache_directory
= NULL
;
1893 sclose(&mp
->mb_sock
);
1899 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
1902 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
1903 if (mp
->mb_type
== MB_IMAP
)
1910 imap_close(struct mailbox
*mp
)
1913 FILE *queuefp
= NULL
;
1916 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
1917 IMAP_OUT(o
, MB_COMD
, return STOP
)
1923 imap_update(struct mailbox
*mp
)
1926 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
1929 if (!edit
&& mp
->mb_perm
!= 0) {
1932 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1933 if (m
->m_flag
& MBOX
)
1936 if (makembox() == STOP
)
1941 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
1942 if (mp
->mb_perm
== 0)
1945 dodel
= ((m
->m_flag
& MDELETED
) != 0);
1947 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
1949 /* Fetch the result after around each 800 STORE commands
1950 * sent (approx. 32k data sent). Otherwise, servers will
1951 * try to flush the return queue at some point, leading
1952 * to a deadlock if we are still writing commands but not
1953 * reading their results */
1954 needstat
= stored
> 0 && stored
% 800 == 0;
1955 /* Even if this message has been deleted, continue
1956 * to set further flags. This is necessary to support
1957 * Gmail semantics, where "delete" actually means
1958 * "archive", and the flags are applied to the copy
1960 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
1961 imap_store(mp
, m
, m
-message
+1, '+', "\\Seen", needstat
);
1964 if (m
->m_flag
& MFLAG
) {
1965 imap_store(mp
, m
, m
-message
+1, '+', "\\Flagged", needstat
);
1968 if (m
->m_flag
& MUNFLAG
) {
1969 imap_store(mp
, m
, m
-message
+1, '-', "\\Flagged", needstat
);
1972 if (m
->m_flag
& MANSWER
) {
1973 imap_store(mp
, m
, m
-message
+1, '+', "\\Answered", needstat
);
1976 if (m
->m_flag
& MUNANSWER
) {
1977 imap_store(mp
, m
, m
-message
+1, '-', "\\Answered", needstat
);
1980 if (m
->m_flag
& MDRAFT
) {
1981 imap_store(mp
, m
, m
-message
+1, '+', "\\Draft", needstat
);
1984 if (m
->m_flag
& MUNDRAFT
) {
1985 imap_store(mp
, m
, m
-message
+1, '-', "\\Draft", needstat
);
1990 imap_delete(mp
, m
-message
+1, m
, needstat
);
1993 } else if (mp
->mb_type
!= MB_CACHE
||
1994 (!edit
&& !(m
->m_flag
& (MBOXED
| MSAVED
| MDELETED
))) ||
1995 (m
->m_flag
& (MBOXED
| MPRESERVE
| MTOUCH
)) ==
1996 (MPRESERVE
| MTOUCH
) || (edit
&& !(m
->m_flag
& MDELETED
)))
1998 if (m
->m_flag
& MNEW
) {
2000 m
->m_flag
|= MSTATUS
;
2007 for (m
= &message
[0]; PTRCMP(m
, <, message
+ msgCount
); ++m
)
2008 if (!(m
->m_flag
& MUNLINKED
) &&
2009 m
->m_flag
& (MBOXED
| MDELETED
| MSAVED
| MSTATUS
| MFLAG
|
2010 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
)) {
2014 if ((gotcha
|| modflags
) && edit
) {
2015 printf(tr(168, "\"%s\" "), displayname
);
2016 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
2017 ? tr(170, "complete\n") : tr(212, "updated.\n"));
2018 } else if (held
&& !edit
&& mp
->mb_perm
!= 0) {
2020 printf(tr(155, "Held 1 message in %s\n"), displayname
);
2022 printf(tr(156, "Held %d messages in %s\n"), held
, displayname
);
2032 sighandler_type
volatile saveint
, savepipe
;
2035 if (mb
.mb_type
== MB_CACHE
) {
2040 if (mb
.mb_sock
.s_fd
< 0) {
2041 fprintf(stderr
, "IMAP connection closed.\n");
2046 saveint
= safe_signal(SIGINT
, SIG_IGN
);
2047 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2048 if (sigsetjmp(imapjmp
, 1)) {
2049 safe_signal(SIGINT
, saveint
);
2050 safe_signal(SIGPIPE
, saveint
);
2054 if (saveint
!= SIG_IGN
)
2055 safe_signal(SIGINT
, imapcatch
);
2056 if (savepipe
!= SIG_IGN
)
2057 safe_signal(SIGPIPE
, imapcatch
);
2060 if (!same_imap_account
)
2063 safe_signal(SIGINT
, saveint
);
2064 safe_signal(SIGPIPE
, savepipe
);
2071 imap_store(struct mailbox
*mp
, struct message
*m
, int n
, int c
, const char *sp
,
2075 FILE *queuefp
= NULL
;
2078 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
2081 snprintf(o
, sizeof o
, "%s UID STORE %lu %cFLAGS (%s)\r\n",
2082 tag(1), m
->m_uid
, c
, sp
);
2084 if (check_expunged() == STOP
)
2086 snprintf(o
, sizeof o
, "%s STORE %u %cFLAGS (%s)\r\n", tag(1), n
, c
, sp
);
2088 IMAP_OUT(o
, MB_COMD
, return STOP
)
2092 mb
.mb_active
&= ~MB_COMD
;
2093 if (queuefp
!= NULL
)
2099 imap_undelete(struct message
*m
, int n
)
2104 rv
= imap_unstore(m
, n
, "\\Deleted");
2110 imap_unread(struct message
*m
, int n
)
2115 rv
= imap_unstore(m
, n
, "\\Seen");
2121 imap_unstore(struct message
*m
, int n
, const char *flag
)
2123 sighandler_type saveint
, savepipe
;
2124 enum okay rv
= STOP
;
2128 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2129 safe_signal(SIGINT
, &_imap_maincatch
);
2130 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2131 if (sigsetjmp(imapjmp
, 1) == 0) {
2132 if (savepipe
!= SIG_IGN
)
2133 safe_signal(SIGPIPE
, imapcatch
);
2135 rv
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2137 safe_signal(SIGINT
, saveint
);
2138 safe_signal(SIGPIPE
, savepipe
);
2156 snprintf(ts
, sizeof ts
, "T%lu", n
);
2162 c_imap_imap(void *vp
)
2165 sighandler_type saveint
, savepipe
;
2166 struct mailbox
*mp
= &mb
;
2167 FILE *queuefp
= NULL
;
2168 enum okay ok
= STOP
;
2171 if (mp
->mb_type
!= MB_IMAP
) {
2172 printf("Not operating on an IMAP mailbox.\n");
2176 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2177 safe_signal(SIGINT
, &_imap_maincatch
);
2178 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2179 if (sigsetjmp(imapjmp
, 1) == 0) {
2180 if (savepipe
!= SIG_IGN
)
2181 safe_signal(SIGPIPE
, imapcatch
);
2183 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2184 IMAP_OUT(o
, MB_COMD
, goto out
)
2185 while (mp
->mb_active
& MB_COMD
) {
2186 ok
= imap_answer(mp
, 0);
2187 fputs(responded_text
, stdout
);
2191 safe_signal(SIGINT
, saveint
);
2192 safe_signal(SIGPIPE
, savepipe
);
2201 imap_newmail(int nmail
)
2205 if (nmail
&& had_exists
< 0 && had_expunge
< 0) {
2211 if (had_exists
== msgCount
&& had_expunge
< 0)
2212 /* Some servers always respond with EXISTS to NOOP. If
2213 * the mailbox has been changed but the number of messages
2214 * has not, an EXPUNGE must also had been sent; otherwise,
2215 * nothing has changed */
2218 return (had_expunge
>= 0 ? 2 : (had_exists
>= 0 ? 1 : 0));
2222 imap_putflags(int f
)
2228 bp
= buf
= salloc(100);
2229 if (f
& (MREAD
| MFLAGGED
| MANSWERED
| MDRAFTED
)) {
2234 for (cp
= "\\Seen"; *cp
; cp
++)
2240 for (cp
= "\\Flagged"; *cp
; cp
++)
2243 if (f
& MANSWERED
) {
2246 for (cp
= "\\Answered"; *cp
; cp
++)
2252 for (cp
= "\\Draft"; *cp
; cp
++)
2264 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2267 while (*cp
!= ')') {
2269 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2271 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2273 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2275 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2277 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2279 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2291 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
, off_t off1
,
2292 long xsize
, enum mflag flag
, time_t t
)
2294 char o
[LINESIZE
], *buf
;
2295 size_t bufsize
, buflen
, cnt
;
2296 long size
, lines
, ysize
;
2298 FILE *queuefp
= NULL
;
2302 if (mp
->mb_type
== MB_CACHE
) {
2303 queuefp
= cache_queue(mp
);
2304 if (queuefp
== NULL
) {
2313 buf
= smalloc(bufsize
= LINESIZE
);
2318 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2323 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2324 tag(1), imap_quotestr(name
), imap_putflags(flag
),
2325 imap_make_date_time(t
), size
);
2326 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2327 while (mp
->mb_active
& MB_COMD
) {
2328 rv
= imap_answer(mp
, twice
);
2329 if (response_type
== RESPONSE_CONT
)
2333 if (mp
->mb_type
!= MB_CACHE
&& rv
== STOP
) {
2342 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2345 buf
[buflen
- 1] = '\r';
2347 if (mp
->mb_type
!= MB_CACHE
)
2348 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2350 fwrite(buf
, 1, buflen
+1, queuefp
);
2353 if (mp
->mb_type
!= MB_CACHE
)
2354 swrite(&mp
->mb_sock
, "\r\n");
2356 fputs("\r\n", queuefp
);
2357 while (mp
->mb_active
& MB_COMD
) {
2358 rv
= imap_answer(mp
, 0);
2359 if (response_status
== RESPONSE_NO
/*&&
2360 ascncasecmp(responded_text,
2361 "[TRYCREATE] ", 12) == 0*/) {
2367 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), imap_quotestr(name
));
2368 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2369 while (mp
->mb_active
& MB_COMD
)
2370 rv
= imap_answer(mp
, 1);
2373 imap_created_mailbox
++;
2375 } else if (rv
!= OKAY
)
2376 fprintf(stderr
, tr(270, "IMAP error: %s"), responded_text
);
2377 else if (response_status
== RESPONSE_OK
&& (mp
->mb_flags
& MB_UIDPLUS
))
2378 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
, flag
, name
);
2381 if (queuefp
!= NULL
)
2390 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2392 char *buf
, *bp
, *lp
;
2393 size_t bufsize
, buflen
, cnt
;
2394 off_t off1
= -1, offs
;
2395 int inhead
= 1, flag
= MNEW
| MNEWEST
;
2401 buf
= smalloc(bufsize
= LINESIZE
);
2408 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2409 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
2410 if (off1
!= (off_t
)-1) {
2411 rv
= imap_append1(mp
, name
, fp
, off1
, size
, flag
, tim
);
2414 fseek(fp
, offs
+buflen
, SEEK_SET
);
2416 off1
= offs
+ buflen
;
2421 tim
= unixtime(buf
);
2426 if (bp
&& buf
[0] == '\n')
2428 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
2430 while (whitechar(*lp
))
2433 while (*++lp
!= '\0')
2442 } else if (bp
&& inhead
&& ascncasecmp(buf
, "x-status", 8) == 0) {
2444 while (whitechar(*lp
))
2447 while (*++lp
!= '\0')
2460 } while (bp
!= NULL
);
2469 imap_append(const char *xserver
, FILE *fp
)
2471 sighandler_type
volatile saveint
, savepipe
;
2472 char *server
, *user
, *pass
;
2473 char const *sp
, *cp
, * volatile mbx
, *uhp
;
2474 int volatile use_ssl
;
2475 enum okay rv
= STOP
;
2478 server
= savestr(xserver
);
2482 imap_split(&server
, &sp
, &xus
, &cp
, &uhp
, &xmbx
, &pass
, &user
);
2488 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2489 safe_signal(SIGINT
, &_imap_maincatch
);
2490 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2491 if (sigsetjmp(imapjmp
, 1))
2493 if (savepipe
!= SIG_IGN
)
2494 safe_signal(SIGPIPE
, imapcatch
);
2496 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) && mb
.mb_imap_account
&&
2497 strcmp(protbase(server
), mb
.mb_imap_account
) == 0) {
2498 rv
= imap_append0(&mb
, mbx
, fp
);
2502 memset(&mx
, 0, sizeof mx
);
2503 if (disconnected(server
) == 0) {
2504 if (sopen(sp
, &mx
.mb_sock
, use_ssl
, uhp
,
2505 (use_ssl
? "imaps" : "imap")) != OKAY
)
2507 mx
.mb_sock
.s_desc
= "IMAP";
2508 mx
.mb_type
= MB_IMAP
;
2509 mx
.mb_imap_account
= (char *)protbase(server
);
2510 /* TODO the code now did
2511 * TODO mx.mb_imap_mailbox = mbx;
2512 * TODO though imap_mailbox is sfree()d and mbx
2513 * TODO is possibly even a constant
2514 * TODO i changed this to sstrdup() sofar, as is used
2515 * TODO somewhere else in this file for this! */
2516 mx
.mb_imap_mailbox
= sstrdup(mbx
);
2517 if (imap_preauth(&mx
, sp
, uhp
) != OKAY
||
2518 imap_auth(&mx
, uhp
, user
, pass
)!=OKAY
) {
2519 sclose(&mx
.mb_sock
);
2522 rv
= imap_append0(&mx
, mbx
, fp
);
2525 mx
.mb_imap_account
= (char*)protbase(server
);
2526 mx
.mb_imap_mailbox
= sstrdup(mbx
); /* TODO as above */
2527 mx
.mb_type
= MB_CACHE
;
2528 rv
= imap_append0(&mx
, mbx
, fp
);
2534 safe_signal(SIGINT
, saveint
);
2535 safe_signal(SIGPIPE
, savepipe
);
2545 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
2546 struct list_item
**lend
, int level
)
2548 char o
[LINESIZE
], *cp
;
2550 FILE *queuefp
= NULL
;
2551 struct list_item
*lp
;
2552 enum okay ok
= STOP
;
2555 *list
= *lend
= NULL
;
2556 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n", tag(1), imap_quotestr(base
));
2557 IMAP_OUT(o
, MB_COMD
, return STOP
)
2558 while (mp
->mb_active
& MB_COMD
) {
2559 ok
= imap_answer(mp
, 1);
2560 if (response_status
== RESPONSE_OTHER
&&
2561 response_other
== MAILBOX_DATA_LIST
&& imap_parse_list() == OKAY
) {
2562 cp
= imap_unquotestr(list_name
);
2563 lp
= csalloc(1, sizeof *lp
);
2565 for (bp
= base
; *bp
!= '\0' && *bp
== *cp
; ++bp
)
2567 lp
->l_base
= *cp
? cp
: savestr(base
);
2568 lp
->l_attr
= list_attributes
;
2569 lp
->l_level
= level
+1;
2570 lp
->l_delim
= list_hierarchy_delimiter
;
2571 if (*list
&& *lend
) {
2572 (*lend
)->l_next
= lp
;
2582 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
2584 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
2591 depth
= (cp
= ok_vlook(imap_list_depth
)) != NULL
? atoi(cp
) : 2;
2592 if ((rv
= imap_list1(mp
, base
, &list
, &lend
, 0)) == STOP
)
2595 if (list
== NULL
|| lend
== NULL
)
2598 for (lp
= list
; lp
; lp
= lp
->l_next
)
2599 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&& lp
->l_level
< depth
&&
2600 !(lp
->l_attr
& LIST_NOINFERIORS
)) {
2601 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
2602 memcpy(cp
, lp
->l_name
, n
);
2603 cp
[n
] = lp
->l_delim
;
2605 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&& lx
&& ly
) {
2606 lp
->l_has_children
= 1;
2607 if (strcmp(cp
, lx
->l_name
) == 0)
2616 for (lp
= list
; lp
; lp
= lp
->l_next
) {
2619 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2623 if (!(lp
->l_attr
& LIST_NOSELECT
))
2624 fprintf(fp
, "%s\n", *cp
? cp
: base
);
2625 else if (lp
->l_has_children
== 0)
2626 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
2627 (lp
->l_delim
!= EOF
? lp
->l_delim
: '\n'));
2635 imap_folders(const char * volatile name
, int strip
)
2637 sighandler_type saveint
, savepipe
;
2638 const char *fold
, *cp
, *sp
;
2642 cp
= protbase(name
);
2643 sp
= mb
.mb_imap_account
;
2644 if (sp
== NULL
|| strcmp(cp
, sp
)) {
2645 fprintf(stderr
, tr(502,
2646 "Cannot perform `folders' but when on the very IMAP "
2647 "account; the current one is\n `%s' -- "
2648 "try `folders @'.\n"),
2649 (sp
!= NULL
? sp
: tr(503, "[NONE]")));
2653 fold
= imap_fileof(name
);
2654 if (options
& OPT_TTYOUT
) {
2655 if ((fp
= Ftmp(NULL
, "imapfold", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
2664 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2665 safe_signal(SIGINT
, &_imap_maincatch
);
2666 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2667 if (sigsetjmp(imapjmp
, 1)) /* TODO imaplock? */
2669 if (savepipe
!= SIG_IGN
)
2670 safe_signal(SIGPIPE
, imapcatch
);
2672 if (mb
.mb_type
== MB_CACHE
)
2673 cache_list(&mb
, fold
, strip
, fp
);
2675 imap_list(&mb
, fold
, strip
, fp
);
2679 if (options
& OPT_TTYOUT
)
2685 if (options
& OPT_TTYOUT
) {
2690 fprintf(stderr
, "Folder not found.\n");
2693 safe_signal(SIGINT
, saveint
);
2694 safe_signal(SIGPIPE
, savepipe
);
2695 if (options
& OPT_TTYOUT
)
2708 long n
= 0, mx
= 0, columns
, width
;
2712 if ((out
= Ftmp(NULL
, "imapdopr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
2718 while ((c
= getc(fp
)) != EOF
) {
2729 if (mx
< width
/ 2) {
2730 columns
= width
/ (mx
+2);
2731 snprintf(o
, sizeof o
, "sort | pr -%lu -w%lu -t", columns
, width
);
2733 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
2734 run_command(XSHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
2742 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
2746 int twice
= 0, stored
= 0;
2747 FILE *queuefp
= NULL
;
2748 enum okay ok
= STOP
;
2751 if (mp
->mb_type
== MB_CACHE
) {
2752 if ((queuefp
= cache_queue(mp
)) == NULL
)
2756 qname
= imap_quotestr(name
= imap_fileof(name
));
2757 /* Since it is not possible to set flags on the copy, recently
2758 * set flags must be set on the original to include it in the copy */
2759 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
))
2760 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
2761 if (m
->m_flag
&MFLAG
)
2762 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2763 if (m
->m_flag
&MUNFLAG
)
2764 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2765 if (m
->m_flag
&MANSWER
)
2766 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2767 if (m
->m_flag
&MUNANSWER
)
2768 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2769 if (m
->m_flag
&MDRAFT
)
2770 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2771 if (m
->m_flag
&MUNDRAFT
)
2772 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2775 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n", tag(1), m
->m_uid
, qname
);
2777 if (check_expunged() == STOP
)
2779 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n", tag(1), n
, qname
);
2781 IMAP_OUT(o
, MB_COMD
, goto out
)
2782 while (mp
->mb_active
& MB_COMD
)
2783 ok
= imap_answer(mp
, twice
);
2785 if (mp
->mb_type
== MB_IMAP
&& mp
->mb_flags
& MB_UIDPLUS
&&
2786 response_status
== RESPONSE_OK
)
2787 imap_copyuid(mp
, m
, name
);
2789 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
2790 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2791 IMAP_OUT(o
, MB_COMD
, goto out
)
2792 while (mp
->mb_active
& MB_COMD
)
2793 ok
= imap_answer(mp
, 1);
2795 imap_created_mailbox
++;
2800 if (queuefp
!= NULL
)
2803 /* ... and reset the flag to its initial value so that the 'exit'
2804 * command still leaves the message unread */
2806 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
2807 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
2810 if (m
->m_flag
& MFLAG
) {
2811 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2814 if (m
->m_flag
& MUNFLAG
) {
2815 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2818 if (m
->m_flag
& MANSWER
) {
2819 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
2822 if (m
->m_flag
& MUNANSWER
) {
2823 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2826 if (m
->m_flag
& MDRAFT
) {
2827 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2830 if (m
->m_flag
& MUNDRAFT
) {
2831 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2835 mp
->mb_active
|= MB_COMD
;
2836 (void)imap_finish(mp
);
2842 imap_copy(struct message
*m
, int n
, const char *name
)
2844 sighandler_type saveint
, savepipe
;
2845 enum okay rv
= STOP
;
2849 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2850 safe_signal(SIGINT
, &_imap_maincatch
);
2851 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2852 if (sigsetjmp(imapjmp
, 1) == 0) {
2853 if (savepipe
!= SIG_IGN
)
2854 safe_signal(SIGPIPE
, imapcatch
);
2856 rv
= imap_copy1(&mb
, m
, n
, name
);
2858 safe_signal(SIGINT
, saveint
);
2859 safe_signal(SIGPIPE
, savepipe
);
2869 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
2870 unsigned long *olduid
, unsigned long *newuid
)
2876 *uidvalidity
= strtoul(cp
, &xp
, 10);
2877 *olduid
= strtoul(xp
, &yp
, 10);
2878 *newuid
= strtoul(yp
, &zp
, 10);
2879 rv
= (*uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
2880 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']');
2886 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
2893 *uidvalidity
= strtoul(cp
, &xp
, 10);
2894 *uid
= strtoul(xp
, &yp
, 10);
2895 rv
= (*uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' && yp
> xp
&&
2902 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
2907 unsigned long uidvalidity
, olduid
, newuid
;
2912 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
2913 imap_copyuid_parse(&cp
[9], &uidvalidity
, &olduid
, &newuid
) == STOP
)
2918 xmb
.mb_cache_directory
= NULL
;
2919 xmb
.mb_imap_mailbox
= savestr(name
);
2920 xmb
.mb_uidvalidity
= uidvalidity
;
2923 memset(&xm
, 0, sizeof xm
);
2925 if ((rv
= getcache1(mp
, &xm
, NEED_UNSPEC
, 3)) != OKAY
)
2927 getcache(mp
, &xm
, NEED_HEADER
);
2928 getcache(mp
, &xm
, NEED_BODY
);
2930 if ((m
->m_flag
& HAVE_HEADER
) == 0)
2931 getcache(mp
, m
, NEED_HEADER
);
2932 if ((m
->m_flag
& HAVE_BODY
) == 0)
2933 getcache(mp
, m
, NEED_BODY
);
2937 xm
.m_flag
&= ~MFULLYCACHED
;
2938 putcache(&xmb
, &xm
);
2945 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
, long xsize
,
2946 long size
, long lines
, int flag
, const char *name
)
2951 unsigned long uidvalidity
, uid
;
2956 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
2957 imap_appenduid_parse(&cp
[11], &uidvalidity
, &uid
) == STOP
)
2962 xmb
.mb_cache_directory
= NULL
;
2963 xmb
.mb_imap_mailbox
= savestr(name
);
2964 xmb
.mb_uidvalidity
= uidvalidity
;
2965 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
2967 memset(&xm
, 0, sizeof xm
);
2968 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
2970 xm
.m_block
= mailx_blockof(off1
);
2971 xm
.m_offset
= mailx_offsetof(off1
);
2974 xm
.m_lines
= xm
.m_xlines
= lines
;
2976 xm
.m_have
= HAVE_HEADER
| HAVE_BODY
;
2977 putcache(&xmb
, &xm
);
2984 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
2988 long size
, xsize
, ysize
, lines
;
2989 enum mflag flag
= MNEW
;
2990 char *name
, *buf
, *bp
;
2992 size_t bufsize
, buflen
, cnt
;
2993 enum okay rv
= STOP
;
2996 buf
= smalloc(bufsize
= LINESIZE
);
2999 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3002 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3007 if ((cp
= strrchr(bp
, '{')) == NULL
)
3010 xsize
= atol(&cp
[1]) + 2;
3011 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
3017 imap_getflags(cp
, &cp
, &flag
);
3018 while (*++cp
== ' ')
3021 t
= imap_read_date_time(cp
);
3023 if ((tp
= Ftmp(NULL
, "imapapui", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
3030 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3033 buf
[--buflen
] = '\0';
3034 buf
[buflen
-1] = '\n';
3035 fwrite(buf
, 1, buflen
, tp
);
3042 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
3043 imap_unquotestr(name
));
3053 #ifdef HAVE_IMAP_SEARCH
3055 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
, const char *spec
,
3058 char *o
, *xp
, *cs
, c
;
3060 FILE *queuefp
= NULL
;
3064 enum okay ok
= STOP
;
3068 for (cp
= spec
; *cp
; cp
++)
3071 cp
= charset_get_lc();
3073 if (asccasecmp(cp
, "utf-8") && asccasecmp(cp
, "utf8")) {
3078 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
3079 sz
= strlen(spec
) + 1;
3080 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
3081 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
, FAL0
) == 0 &&
3090 cp
= imap_quotestr(cp
);
3091 cs
= salloc(n
= strlen(cp
) + 10);
3092 snprintf(cs
, n
, "CHARSET %s ", cp
);
3096 o
= ac_alloc(osize
= strlen(spec
) + 60);
3097 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
3098 IMAP_OUT(o
, MB_COMD
, goto out
)
3099 while (mp
->mb_active
& MB_COMD
) {
3100 ok
= imap_answer(mp
, 0);
3101 if (response_status
== RESPONSE_OTHER
&&
3102 response_other
== MAILBOX_DATA_SEARCH
) {
3103 xp
= responded_other_text
;
3104 while (*xp
&& *xp
!= '\r') {
3105 n
= strtoul(xp
, &xp
, 10);
3106 for (i
= 0; i
< cnt
; i
++)
3107 if (m
[i
].m_uid
== n
&& !(m
[i
].m_flag
& MHIDDEN
) &&
3108 (f
== MDELETED
|| !(m
[i
].m_flag
& MDELETED
)))
3119 imap_search1(const char * volatile spec
, int f
)
3121 sighandler_type saveint
, savepipe
;
3122 enum okay rv
= STOP
;
3125 if (mb
.mb_type
!= MB_IMAP
)
3129 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3130 safe_signal(SIGINT
, &_imap_maincatch
);
3131 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3132 if (sigsetjmp(imapjmp
, 1) == 0) {
3133 if (savepipe
!= SIG_IGN
)
3134 safe_signal(SIGPIPE
, imapcatch
);
3136 rv
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
3138 safe_signal(SIGINT
, saveint
);
3139 safe_signal(SIGPIPE
, savepipe
);
3147 #endif /* HAVE_IMAP_SEARCH */
3150 imap_thisaccount(const char *cp
)
3155 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
3157 else if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
3158 mb
.mb_imap_account
== NULL
)
3161 rv
= !strcmp(protbase(cp
), mb
.mb_imap_account
);
3167 imap_remove(const char * volatile name
)
3169 sighandler_type
volatile saveint
, savepipe
;
3170 enum okay rv
= STOP
;
3173 if (mb
.mb_type
!= MB_IMAP
) {
3174 fprintf(stderr
, "Refusing to remove \"%s\" in disconnected mode.\n",
3179 if (!imap_thisaccount(name
)) {
3180 fprintf(stderr
, "Can only remove mailboxes on current IMAP "
3181 "server: \"%s\" not removed.\n", name
);
3186 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3187 safe_signal(SIGINT
, &_imap_maincatch
);
3188 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3189 if (sigsetjmp(imapjmp
, 1) == 0) {
3190 if (savepipe
!= SIG_IGN
)
3191 safe_signal(SIGPIPE
, imapcatch
);
3193 rv
= imap_remove1(&mb
, imap_fileof(name
));
3195 safe_signal(SIGINT
, saveint
);
3196 safe_signal(SIGPIPE
, savepipe
);
3200 rv
= cache_remove(name
);
3209 imap_remove1(struct mailbox
*mp
, const char *name
)
3211 FILE *queuefp
= NULL
;
3214 enum okay ok
= STOP
;
3217 o
= ac_alloc(os
= 2*strlen(name
) + 100);
3218 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), imap_quotestr(name
));
3219 IMAP_OUT(o
, MB_COMD
, goto out
)
3220 while (mp
->mb_active
& MB_COMD
)
3221 ok
= imap_answer(mp
, 1);
3228 imap_rename(const char *old
, const char *new)
3230 sighandler_type saveint
, savepipe
;
3231 enum okay rv
= STOP
;
3234 if (mb
.mb_type
!= MB_IMAP
) {
3235 fprintf(stderr
, "Refusing to rename mailboxes in disconnected mode.\n");
3239 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3240 fprintf(stderr
, "Can only rename mailboxes on current IMAP "
3241 "server: \"%s\" not renamed to \"%s\".\n", old
, new);
3246 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3247 safe_signal(SIGINT
, &_imap_maincatch
);
3248 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3249 if (sigsetjmp(imapjmp
, 1) == 0) {
3250 if (savepipe
!= SIG_IGN
)
3251 safe_signal(SIGPIPE
, imapcatch
);
3253 rv
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3255 safe_signal(SIGINT
, saveint
);
3256 safe_signal(SIGPIPE
, savepipe
);
3260 rv
= cache_rename(old
, new);
3269 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3271 FILE *queuefp
= NULL
;
3274 enum okay ok
= STOP
;
3277 o
= ac_alloc(os
= 2*strlen(old
) + 2*strlen(new) + 100);
3278 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1), imap_quotestr(old
),
3279 imap_quotestr(new));
3280 IMAP_OUT(o
, MB_COMD
, goto out
)
3281 while (mp
->mb_active
& MB_COMD
)
3282 ok
= imap_answer(mp
, 1);
3289 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3291 char o
[LINESIZE
], *newname
, *buf
, *bp
, *cp
, iob
[4096];
3292 size_t bufsize
, buflen
, cnt
;
3293 long offs
, offs1
, offs2
, octets
;
3294 int twice
, gotcha
= 0;
3295 FILE *queuefp
= NULL
;
3296 enum okay ok
= OKAY
, rok
= OKAY
;
3299 buf
= smalloc(bufsize
= LINESIZE
);
3302 while ((offs1
= ftell(fp
)) >= 0 &&
3303 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) != NULL
) {
3304 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3309 if ((offs
= ftell(fp
)) < 0)
3312 snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3313 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3315 while (digitchar(*cp
))
3321 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3323 IMAP_OUT(o
, MB_COMD
, continue)
3324 while (mp
->mb_active
& MB_COMD
)
3325 ok
= imap_answer(mp
, twice
);
3326 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3328 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3329 imap_copyuid(mp
, NULL
, imap_unquotestr(newname
));
3331 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3332 IMAP_OUT(o
, MB_COMD
, continue)
3333 while (mp
->mb_active
& MB_COMD
)
3334 ok
= imap_answer(mp
, 1);
3337 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3338 if ((cp
= strrchr(bp
, '{')) == NULL
)
3340 octets
= atol(&cp
[1]) + 2;
3341 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3343 IMAP_OUT(o
, MB_COMD
, continue)
3344 while (mp
->mb_active
& MB_COMD
) {
3345 ok
= imap_answer(mp
, twice
);
3346 if (response_type
== RESPONSE_CONT
)
3350 if (twice
++ == 0 && fseek(fp
, offs
, SEEK_SET
) >= 0)
3354 while (octets
> 0) {
3355 size_t n
= (UICMP(z
, octets
, >, sizeof iob
)
3356 ? sizeof iob
: (size_t)octets
);
3358 if (n
!= fread(iob
, 1, n
, fp
))
3360 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3362 swrite(&mp
->mb_sock
, "");
3363 while (mp
->mb_active
& MB_COMD
) {
3364 ok
= imap_answer(mp
, 0);
3365 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
3366 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3371 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3372 if ((offs2
= ftell(fp
)) < 0)
3374 fseek(fp
, offs1
, SEEK_SET
);
3375 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3376 (void)fseek(fp
, offs2
, SEEK_SET
);
3382 fprintf(stderr
, "Invalid command in IMAP cache queue: \"%s\"\n", bp
);
3387 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), newname
);
3388 IMAP_OUT(o
, MB_COMD
, continue)
3389 while (mp
->mb_active
& MB_COMD
)
3390 ok
= imap_answer(mp
, 1);
3396 ftruncate(fileno(fp
), 0);
3404 imap_strex(char const *cp
, char const **xp
)
3413 for (cq
= cp
+ 1; *cq
!= '\0'; ++cq
) {
3416 else if (*cq
== '"')
3422 n
= salloc(cq
- cp
+ 2);
3423 memcpy(n
, cp
, cq
- cp
+1);
3424 n
[cq
- cp
+ 1] = '\0';
3433 check_expunged(void)
3438 if (expunged_messages
> 0) {
3439 fprintf(stderr
, "Command not executed - messages have been expunged\n");
3451 int rv
, omsgCount
= msgCount
;
3455 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
3456 fprintf(stderr
, "Already connected.\n");
3461 var_clear_allow_undefined
= TRU1
;
3462 ok_bclear(disconnected
);
3464 cp
= protbase(mailname
);
3465 if (strncmp(cp
, "imap://", 7) == 0)
3467 else if (strncmp(cp
, "imaps://", 8) == 0)
3469 if ((cq
= strchr(cp
, ':')) != NULL
)
3472 vok_bclear(savecat("disconnected-", cp
));
3473 var_clear_allow_undefined
= FAL0
;
3475 if (mb
.mb_type
== MB_CACHE
) {
3476 imap_setfile1(mailname
, 0, edit
, 1);
3477 if (msgCount
> omsgCount
)
3478 newmailinfo(omsgCount
);
3487 c_disconnect(void *vp
)
3489 int rv
= 1, *msgvec
= vp
;
3492 if (mb
.mb_type
== MB_CACHE
) {
3493 fprintf(stderr
, "Not connected.\n");
3496 if (mb
.mb_type
== MB_IMAP
) {
3497 if (cached_uidvalidity(&mb
) == 0) {
3498 fprintf(stderr
, "The current mailbox is not cached.\n");
3505 ok_bset(disconnected
, TRU1
);
3506 if (mb
.mb_type
== MB_IMAP
) {
3507 sclose(&mb
.mb_sock
);
3508 imap_setfile1(mailname
, 0, edit
, 1);
3519 int rv
= 1, *msgvec
= vp
, *ip
;
3523 if (mb
.mb_type
!= MB_IMAP
) {
3524 fprintf(stderr
, "Not connected to an IMAP server.\n");
3527 if (cached_uidvalidity(&mb
) == 0) {
3528 fprintf(stderr
, "The current mailbox is not cached.\n");
3532 for (ip
= msgvec
; *ip
; ++ip
) {
3533 mp
= &message
[*ip
- 1];
3534 if (!(mp
->m_have
& HAVE_BODY
))
3544 disconnected(const char *file
)
3550 if (ok_blook(disconnected
))
3553 cp
= protbase(file
);
3554 if (strncmp(cp
, "imap://", 7) == 0)
3556 else if (strncmp(cp
, "imaps://", 8) == 0)
3563 if ((cq
= strchr(cp
, ':')) != NULL
)
3565 vp
= ac_alloc(vs
= strlen(cp
) + 14);
3566 snprintf(vp
, vs
, "disconnected-%s", cp
);
3575 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
3577 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
3585 while (PTRCMP(omp
, <, omessage
+ omsgCount
) &&
3586 PTRCMP(nmp
, <, message
+ msgCount
)) {
3587 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
3589 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
3591 if (omp
->m_uid
== nmp
->m_uid
) {
3592 hf
= nmp
->m_flag
& MHIDDEN
;
3593 if (transparent
&& mb
.mb_type
== MB_IMAP
)
3594 omp
->m_flag
&= ~MHIDDEN
;
3596 if (transparent
&& mb
.mb_type
== MB_CACHE
)
3597 nmp
[-1].m_flag
|= hf
;
3598 } else if (omp
->m_uid
< nmp
->m_uid
)
3605 prevdot
= newprevdot
;
3611 imap_read_date_time(const char *cp
)
3615 int i
, year
, month
, day
, hour
, minute
, second
, sign
= -1;
3618 /* "25-Jul-2004 15:33:44 +0200"
3620 * 0 5 10 15 20 25 */
3621 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
3623 day
= strtol(&cp
[1], NULL
, 10);
3625 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
3627 if (month_names
[++i
][0] == '\0')
3631 year
= strtol(&cp
[8], NULL
, 10);
3632 hour
= strtol(&cp
[13], NULL
, 10);
3633 minute
= strtol(&cp
[16], NULL
, 10);
3634 second
= strtol(&cp
[19], NULL
, 10);
3635 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
3649 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
3652 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
3662 imap_make_date_time(time_t t
)
3666 int tzdiff
, tzdiff_hour
, tzdiff_min
;
3669 tzdiff
= t
- mktime(gmtime(&t
));
3670 tzdiff_hour
= (int)(tzdiff
/ 60);
3671 tzdiff_min
= tzdiff_hour
% 60;
3673 tmptr
= localtime(&t
);
3674 if (tmptr
->tm_isdst
> 0)
3676 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
3677 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
], tmptr
->tm_year
+ 1900,
3678 tmptr
->tm_hour
, tmptr
->tm_min
, tmptr
->tm_sec
, tzdiff_hour
, tzdiff_min
);
3682 #endif /* HAVE_IMAP */
3684 #if defined HAVE_IMAP || defined HAVE_IMAP_SEARCH
3686 imap_quotestr(char const *s
)
3691 np
= n
= salloc(2 * strlen(s
) + 3);
3694 if (*s
== '"' || *s
== '\\')
3705 imap_unquotestr(char const *s
)
3715 np
= n
= salloc(strlen(s
) + 1);
3728 #endif /* defined HAVE_IMAP || defined HAVE_IMAP_SEARCH */
3730 /* vim:set fenc=utf-8:s-it-mode */