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
, struct ccred
*ccred
);
192 static enum okay
imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
);
194 static enum okay
imap_login(struct mailbox
*mp
, struct ccred
*ccred
);
196 static enum okay
_imap_gssapi(struct mailbox
*mp
, struct ccred
*ccred
);
198 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
199 static void imap_init(struct mailbox
*mp
, int n
);
200 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
202 static int _imap_setfile1(struct url
*urlp
, int nmail
, int isedit
,
204 static int imap_fetchdata(struct mailbox
*mp
, struct message
*m
,
205 size_t expected
, int need
, const char *head
,
206 size_t headsize
, long headlines
);
207 static void imap_putstr(struct mailbox
*mp
, struct message
*m
,
208 const char *str
, const char *head
, size_t headsize
,
210 static enum okay
imap_get(struct mailbox
*mp
, struct message
*m
,
212 static void commitmsg(struct mailbox
*mp
, struct message
*to
,
213 struct message
*from
, enum havespec have
);
214 static enum okay
imap_fetchheaders(struct mailbox
*mp
, struct message
*m
,
216 static enum okay
imap_exit(struct mailbox
*mp
);
217 static enum okay
imap_delete(struct mailbox
*mp
, int n
, struct message
*m
,
219 static enum okay
imap_close(struct mailbox
*mp
);
220 static enum okay
imap_update(struct mailbox
*mp
);
221 static enum okay
imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
222 int c
, const char *sp
, int needstat
);
223 static enum okay
imap_unstore(struct message
*m
, int n
, const char *flag
);
224 static const char *tag(int new);
225 static char * imap_putflags(int f
);
226 static void imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
);
227 static enum okay
imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
228 off_t off1
, long xsize
, enum mflag flag
, time_t t
);
229 static enum okay
imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
);
230 static enum okay
imap_list1(struct mailbox
*mp
, const char *base
,
231 struct list_item
**list
, struct list_item
**lend
,
233 static enum okay
imap_list(struct mailbox
*mp
, const char *base
, int strip
,
235 static void dopr(FILE *fp
);
236 static enum okay
imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
,
238 static enum okay
imap_copyuid_parse(const char *cp
,
239 unsigned long *uidvalidity
, unsigned long *olduid
,
240 unsigned long *newuid
);
241 static enum okay
imap_appenduid_parse(const char *cp
,
242 unsigned long *uidvalidity
, unsigned long *uid
);
243 static enum okay
imap_copyuid(struct mailbox
*mp
, struct message
*m
,
245 static enum okay
imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
,
246 long off1
, long xsize
, long size
, long lines
, int flag
,
248 static enum okay
imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
);
249 #ifdef HAVE_IMAP_SEARCH
250 static enum okay
imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
251 const char *spec
, int f
);
253 static enum okay
imap_remove1(struct mailbox
*mp
, const char *name
);
254 static enum okay
imap_rename1(struct mailbox
*mp
, const char *old
,
256 static char * imap_strex(char const *cp
, char const **xp
);
257 static enum okay
check_expunged(void);
260 imap_other_get(char *pp
)
265 if (ascncasecmp(pp
, "FLAGS ", 6) == 0) {
267 response_other
= MAILBOX_DATA_FLAGS
;
268 } else if (ascncasecmp(pp
, "LIST ", 5) == 0) {
270 response_other
= MAILBOX_DATA_LIST
;
271 } else if (ascncasecmp(pp
, "LSUB ", 5) == 0) {
273 response_other
= MAILBOX_DATA_LSUB
;
274 } else if (ascncasecmp(pp
, "MAILBOX ", 8) == 0) {
276 response_other
= MAILBOX_DATA_MAILBOX
;
277 } else if (ascncasecmp(pp
, "SEARCH ", 7) == 0) {
279 response_other
= MAILBOX_DATA_SEARCH
;
280 } else if (ascncasecmp(pp
, "STATUS ", 7) == 0) {
282 response_other
= MAILBOX_DATA_STATUS
;
283 } else if (ascncasecmp(pp
, "CAPABILITY ", 11) == 0) {
285 response_other
= CAPABILITY_DATA
;
287 responded_other_number
= strtol(pp
, &xp
, 10);
290 if (ascncasecmp(xp
, "EXISTS\r\n", 8) == 0) {
291 response_other
= MAILBOX_DATA_EXISTS
;
292 } else if (ascncasecmp(xp
, "RECENT\r\n", 8) == 0) {
293 response_other
= MAILBOX_DATA_RECENT
;
294 } else if (ascncasecmp(xp
, "EXPUNGE\r\n", 9) == 0) {
295 response_other
= MESSAGE_DATA_EXPUNGE
;
296 } else if (ascncasecmp(xp
, "FETCH ", 6) == 0) {
298 response_other
= MESSAGE_DATA_FETCH
;
300 response_other
= RESPONSE_OTHER_UNKNOWN
;
302 responded_other_text
= pp
;
307 imap_response_get(const char **cp
)
310 if (ascncasecmp(*cp
, "OK ", 3) == 0) {
312 response_status
= RESPONSE_OK
;
313 } else if (ascncasecmp(*cp
, "NO ", 3) == 0) {
315 response_status
= RESPONSE_NO
;
316 } else if (ascncasecmp(*cp
, "BAD ", 4) == 0) {
318 response_status
= RESPONSE_BAD
;
319 } else if (ascncasecmp(*cp
, "PREAUTH ", 8) == 0) {
321 response_status
= RESPONSE_PREAUTH
;
322 } else if (ascncasecmp(*cp
, "BYE ", 4) == 0) {
324 response_status
= RESPONSE_BYE
;
326 response_status
= RESPONSE_OTHER
;
331 imap_response_parse(void)
333 static char *parsebuf
; /* TODO Use pool */
334 static size_t parsebufsize
;
336 const char *ip
= imapbuf
;
340 if (parsebufsize
< imapbufsize
)
341 parsebuf
= srealloc(parsebuf
, parsebufsize
= imapbufsize
);
342 memcpy(parsebuf
, imapbuf
, strlen(imapbuf
) + 1);
346 response_type
= RESPONSE_CONT
;
361 imap_response_get(&ip
);
362 pp
= &parsebuf
[ip
- imapbuf
];
363 switch (response_status
) {
365 response_type
= RESPONSE_FATAL
;
368 response_type
= RESPONSE_DATA
;
372 responded_tag
= parsebuf
;
373 while (*pp
&& *pp
!= ' ')
376 response_type
= RESPONSE_ILLEGAL
;
380 while (*pp
&& *pp
== ' ')
383 response_type
= RESPONSE_ILLEGAL
;
386 ip
= &imapbuf
[pp
- parsebuf
];
387 response_type
= RESPONSE_TAGGED
;
388 imap_response_get(&ip
);
389 pp
= &parsebuf
[ip
- imapbuf
];
392 if (response_type
!= RESPONSE_CONT
&& response_type
!= RESPONSE_ILLEGAL
&&
393 response_status
== RESPONSE_OTHER
)
399 imap_answer(struct mailbox
*mp
, int errprnt
)
406 if (mp
->mb_type
== MB_CACHE
)
410 if (sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
411 if (options
& OPT_VERBOSE
)
412 fputs(imapbuf
, stderr
);
413 imap_response_parse();
414 if (response_type
== RESPONSE_ILLEGAL
)
416 if (response_type
== RESPONSE_CONT
) {
420 if (response_status
== RESPONSE_OTHER
) {
421 if (response_other
== MAILBOX_DATA_EXISTS
) {
422 had_exists
= responded_other_number
;
423 rec_queue(REC_EXISTS
, responded_other_number
);
426 } else if (response_other
== MESSAGE_DATA_EXPUNGE
) {
427 rec_queue(REC_EXPUNGE
, responded_other_number
);
435 if (response_type
== RESPONSE_TAGGED
) {
436 if (asccasecmp(responded_tag
, tag(0)) == 0)
441 switch (response_status
) {
442 case RESPONSE_PREAUTH
:
443 mp
->mb_active
&= ~MB_PREAUTH
;
456 fprintf(stderr
, tr(270, "IMAP error: %s"), responded_text
);
458 case RESPONSE_UNKNOWN
: /* does not happen */
461 mp
->mb_active
= MB_NONE
;
469 if (response_status
!= RESPONSE_OTHER
&&
470 ascncasecmp(responded_text
, "[ALERT] ", 8) == 0)
471 fprintf(stderr
, "IMAP alert: %s", &responded_text
[8]);
473 mp
->mb_active
&= ~MB_COMD
;
476 mp
->mb_active
= MB_NONE
;
484 imap_parse_list(void)
492 cp
= responded_other_text
;
493 list_attributes
= LIST_NONE
;
495 while (*cp
&& *cp
!= ')') {
497 if (ascncasecmp(&cp
[1], "Noinferiors ", 12) == 0) {
498 list_attributes
|= LIST_NOINFERIORS
;
500 } else if (ascncasecmp(&cp
[1], "Noselect ", 9) == 0) {
501 list_attributes
|= LIST_NOSELECT
;
503 } else if (ascncasecmp(&cp
[1], "Marked ", 7) == 0) {
504 list_attributes
|= LIST_MARKED
;
506 } else if (ascncasecmp(&cp
[1], "Unmarked ", 9) == 0) {
507 list_attributes
|= LIST_UNMARKED
;
519 list_hierarchy_delimiter
= EOF
;
523 list_hierarchy_delimiter
= *cp
++ & 0377;
524 if (cp
[0] != '"' || cp
[1] != ' ')
527 } else if (cp
[0] == 'N' && cp
[1] == 'I' && cp
[2] == 'L' && cp
[3] == ' ') {
528 list_hierarchy_delimiter
= EOF
;
535 while (*cp
&& *cp
!= '\r')
545 imap_finish(struct mailbox
*mp
)
548 while (mp
->mb_sock
.s_fd
> 0 && mp
->mb_active
& MB_COMD
)
558 if (imapkeepalive
> 0) {
560 safe_signal(SIGALRM
, savealrm
);
568 NYD_X
; /* Signal handler */
571 fprintf(stderr
, tr(102, "Interrupt\n"));
572 siglongjmp(imapjmp
, 1);
575 fprintf(stderr
, tr(98, "Received SIGPIPE during IMAP operation\n"));
581 _imap_maincatch(int s
)
583 NYD_X
; /* Signal handler */
585 if (interrupts
++ == 0) {
586 fprintf(stderr
, tr(102, "Interrupt\n"));
593 imap_noop1(struct mailbox
*mp
)
596 FILE *queuefp
= NULL
;
599 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
600 IMAP_OUT(o
, MB_COMD
, return STOP
)
606 imap_fileof(char const *xcp
)
608 char const *cp
= xcp
;
613 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
617 if (cp
[0] == '/' && state
== 1) {
635 sighandler_type
volatile oldint
, oldpipe
;
639 if (mb
.mb_type
!= MB_IMAP
)
643 if ((oldint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
644 safe_signal(SIGINT
, &_imap_maincatch
);
645 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
646 if (sigsetjmp(imapjmp
, 1) == 0) {
647 if (oldpipe
!= SIG_IGN
)
648 safe_signal(SIGPIPE
, imapcatch
);
650 rv
= imap_noop1(&mb
);
652 safe_signal(SIGINT
, oldint
);
653 safe_signal(SIGPIPE
, oldpipe
);
663 rec_queue(enum rec_type rt
, unsigned long cnt
)
668 rp
= scalloc(1, sizeof *rp
);
671 if (record
&& recend
) {
672 recend
->rec_next
= rp
;
675 record
= recend
= rp
;
682 struct message
*omessage
;
683 struct record
*rp
, *rq
;
692 message
= smalloc((msgCount
+1) * sizeof *message
);
694 memcpy(message
, omessage
, msgCount
* sizeof *message
);
695 memset(&message
[msgCount
], 0, sizeof *message
);
697 rp
= record
, rq
= NULL
;
700 switch (rp
->rec_type
) {
702 exists
= rp
->rec_count
;
705 if (rp
->rec_count
== 0) {
709 if (rp
->rec_count
> (unsigned long)msgCount
) {
710 if (exists
== 0 || rp
->rec_count
> exists
--)
716 delcache(&mb
, &message
[rp
->rec_count
-1]);
717 memmove(&message
[rp
->rec_count
-1], &message
[rp
->rec_count
],
718 ((msgCount
- rp
->rec_count
+ 1) * sizeof *message
));
720 /* If the message was part of a collapsed thread,
721 * the m_collapsed field of one of its ancestors
722 * should be incremented. It seems hardly possible
723 * to do this with the current message structure,
724 * though. The result is that a '+' may be shown
725 * in the header summary even if no collapsed
737 record
= recend
= NULL
;
738 if (rv
== OKAY
&& UICMP(z
, exists
, >, msgCount
)) {
739 message
= srealloc(message
, (exists
+ 1) * sizeof *message
);
740 memset(&message
[msgCount
], 0, (exists
- msgCount
+ 1) * sizeof *message
);
741 for (i
= msgCount
; i
< exists
; ++i
)
743 imap_flags(&mb
, msgCount
+1, exists
);
762 for (rp
= record
; rp
!= NULL
;) {
763 struct record
*tmp
= rp
;
767 record
= recend
= NULL
;
775 sighandler_type
volatile saveint
, savepipe
;
776 NYD_X
; /* Signal handler */
779 if (imaplock
++ == 0) {
780 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
781 safe_signal(SIGINT
, &_imap_maincatch
);
782 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
783 if (sigsetjmp(imapjmp
, 1)) {
784 safe_signal(SIGINT
, saveint
);
785 safe_signal(SIGPIPE
, savepipe
);
788 if (savepipe
!= SIG_IGN
)
789 safe_signal(SIGPIPE
, imapcatch
);
790 if (imap_noop1(&mb
) != OKAY
) {
791 safe_signal(SIGINT
, saveint
);
792 safe_signal(SIGPIPE
, savepipe
);
795 safe_signal(SIGINT
, saveint
);
796 safe_signal(SIGPIPE
, savepipe
);
799 alarm(imapkeepalive
);
805 imap_use_starttls(const char *uhp
)
810 if (ok_blook(imap_use_starttls
))
813 char *var
= savecat("imap-use-starttls-", uhp
);
821 imap_preauth(struct mailbox
*mp
, const char *xserver
, const char *uhp
)
826 mp
->mb_active
|= MB_PREAUTH
;
828 if ((cp
= strchr(xserver
, ':')) != NULL
) {
829 char *x
= salloc(cp
- xserver
+ 1);
830 memcpy(x
, xserver
, cp
- xserver
);
831 x
[cp
- xserver
] = '\0';
835 if (mp
->mb_sock
.s_use_ssl
== 0 && imap_use_starttls(uhp
)) {
836 FILE *queuefp
= NULL
;
839 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
840 IMAP_OUT(o
, MB_COMD
, return STOP
)
842 if (ssl_open(xserver
, &mp
->mb_sock
, uhp
) != OKAY
)
846 if (imap_use_starttls(uhp
)) {
847 fprintf(stderr
, "No SSL support compiled in.\n");
856 imap_capability(struct mailbox
*mp
)
859 FILE *queuefp
= NULL
;
864 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
865 IMAP_OUT(o
, MB_COMD
, return STOP
)
866 while (mp
->mb_active
& MB_COMD
) {
867 ok
= imap_answer(mp
, 0);
868 if (response_status
== RESPONSE_OTHER
&&
869 response_other
== CAPABILITY_DATA
) {
870 cp
= responded_other_text
;
872 while (spacechar(*cp
))
874 if (strncmp(cp
, "UIDPLUS", 7) == 0 && spacechar(cp
[7]))
876 mp
->mb_flags
|= MB_UIDPLUS
;
877 while (*cp
&& !spacechar(*cp
))
886 imap_auth(struct mailbox
*mp
, struct ccred
*ccred
)
891 if (!(mp
->mb_active
& MB_PREAUTH
)) {
896 switch (ccred
->cc_authtype
) {
898 rv
= imap_login(mp
, ccred
);
901 case AUTHTYPE_CRAM_MD5
:
902 rv
= imap_cram_md5(mp
, ccred
);
906 case AUTHTYPE_GSSAPI
:
907 rv
= _imap_gssapi(mp
, ccred
);
919 /* Implementation of RFC 2194 */
922 imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
)
924 char o
[LINESIZE
], *cp
;
925 FILE *queuefp
= NULL
;
929 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
930 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
932 if (response_type
!= RESPONSE_CONT
)
935 cp
= cram_md5_string(ccred
->cc_user
.s
, ccred
->cc_pass
.s
, responded_text
);
936 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
937 while (mp
->mb_active
& MB_COMD
)
938 rv
= imap_answer(mp
, 1);
943 #endif /* HAVE_MD5 */
946 imap_login(struct mailbox
*mp
, struct ccred
*ccred
)
949 FILE *queuefp
= NULL
;
953 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
954 tag(1), imap_quotestr(ccred
->cc_user
.s
), imap_quotestr(ccred
->cc_pass
.s
));
955 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
956 while (mp
->mb_active
& MB_COMD
)
957 rv
= imap_answer(mp
, 1);
964 # include "imap_gssapi.h"
968 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
)
973 FILE *queuefp
= NULL
;
977 mp
->mb_uidvalidity
= 0;
978 snprintf(o
, sizeof o
, "%s SELECT %s\r\n", tag(1), imap_quotestr(mbx
));
979 IMAP_OUT(o
, MB_COMD
, return STOP
)
980 while (mp
->mb_active
& MB_COMD
) {
981 ok
= imap_answer(mp
, 1);
982 if (response_status
!= RESPONSE_OTHER
&&
983 (cp
= asccasestr(responded_text
, "[UIDVALIDITY ")) != NULL
)
984 mp
->mb_uidvalidity
= atol(&cp
[13]);
986 *cnt
= (had_exists
> 0) ? had_exists
: 0;
987 if (response_status
!= RESPONSE_OTHER
&&
988 ascncasecmp(responded_text
, "[READ-ONLY] ", 12) == 0)
994 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
997 FILE *queuefp
= NULL
;
1000 unsigned x
= X
, y
= Y
, n
;
1003 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
1004 IMAP_OUT(o
, MB_COMD
, return STOP
)
1005 while (mp
->mb_active
& MB_COMD
) {
1007 if (response_status
== RESPONSE_OTHER
&&
1008 response_other
== MESSAGE_DATA_FETCH
) {
1009 n
= responded_other_number
;
1017 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1022 imap_getflags(cp
, &cp
, &m
->m_flag
);
1025 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1026 m
->m_uid
= strtoul(&cp
[4], NULL
, 10);
1027 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1028 m
->m_flag
&= ~MHIDDEN
;
1031 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1033 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1036 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1038 IMAP_OUT(o
, MB_COMD
, return STOP
)
1039 while (mp
->mb_active
& MB_COMD
) {
1041 if (response_status
== RESPONSE_OTHER
&&
1042 response_other
== MESSAGE_DATA_FETCH
) {
1043 n
= responded_other_number
;
1049 if ((cp
= asccasestr(responded_other_text
, "RFC822.SIZE ")) != NULL
)
1050 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1051 if ((cp
= asccasestr(responded_other_text
, "INTERNALDATE ")) != NULL
)
1052 m
->m_time
= imap_read_date_time(&cp
[13]);
1056 for (n
= X
; n
<= Y
; n
++)
1057 putcache(mp
, &message
[n
-1]);
1062 imap_init(struct mailbox
*mp
, int n
)
1069 m
->m_flag
= MUSED
| MNOFROM
;
1076 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1078 struct message
*omessage
= 0;
1079 int i
, omsgCount
= 0;
1080 enum okay dequeued
= STOP
;
1083 if (nmail
|| transparent
) {
1085 omsgCount
= msgCount
;
1088 dequeued
= rec_dequeue();
1090 if (had_exists
>= 0) {
1091 if (dequeued
!= OKAY
)
1092 msgCount
= had_exists
;
1095 if (had_expunge
>= 0) {
1096 if (dequeued
!= OKAY
)
1097 msgCount
-= had_expunge
;
1101 if (nmail
&& expunged_messages
)
1102 printf("Expunged %ld message%s.\n", expunged_messages
,
1103 (expunged_messages
!= 1 ? "s" : ""));
1104 *prevcount
= omsgCount
- expunged_messages
;
1105 expunged_messages
= 0;
1107 fputs("IMAP error: Negative message count\n", stderr
);
1111 if (dequeued
!= OKAY
) {
1112 message
= scalloc(msgCount
+ 1, sizeof *message
);
1113 for (i
= 0; i
< msgCount
; i
++)
1115 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1118 imap_flags(mp
, 1, msgCount
);
1119 message
[msgCount
].m_size
= 0;
1120 message
[msgCount
].m_lines
= 0;
1123 if (nmail
|| transparent
)
1124 transflags(omessage
, omsgCount
, transparent
);
1131 imap_setfile(const char *xserver
, int nmail
, int isedit
)
1137 if (!url_parse(&url
, CPROTO_IMAP
, xserver
)) {
1141 if (!ok_blook(v15_compat
) &&
1142 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
1143 fprintf(stderr
, "New-style URL used without *v15-compat* being set!\n");
1145 rv
= _imap_setfile1(&url
, nmail
, isedit
, 0);
1152 _imap_setfile1(struct url
*urlp
, int nmail
, int isedit
,
1153 int volatile transparent
)
1157 sighandler_type
volatile saveint
, savepipe
;
1160 int volatile prevcount
= 0;
1161 enum mbflags same_flags
;
1165 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1166 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1167 if (saveint
!= SIG_IGN
)
1168 safe_signal(SIGINT
, imapcatch
);
1169 if (savepipe
!= SIG_IGN
)
1170 safe_signal(SIGPIPE
, imapcatch
);
1175 same_flags
= mb
.mb_flags
;
1176 same_imap_account
= 0;
1177 if (mb
.mb_imap_account
!= NULL
&&
1178 (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)) {
1179 if (mb
.mb_sock
.s_fd
> 0 && mb
.mb_sock
.s_rsz
>= 0 &&
1180 !strcmp(mb
.mb_imap_account
, urlp
->url_puhp
) &&
1181 disconnected(mb
.mb_imap_account
) == 0) {
1182 same_imap_account
= 1;
1183 if (urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1184 urlp
->url_pass
.s
= mb
.mb_imap_pass
;
1185 } else if ((transparent
|| mb
.mb_type
== MB_CACHE
) &&
1186 !strcmp(mb
.mb_imap_account
, urlp
->url_puhp
) &&
1187 urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1188 urlp
->url_pass
.s
= mb
.mb_imap_pass
;
1191 if (!(ok_blook(v15_compat
) ? ccred_lookup(&ccred
, urlp
)
1192 : ccred_lookup_old(&ccred
, CPROTO_IMAP
, urlp
->url_uhp
.s
))) {
1198 if (!same_imap_account
) {
1199 if (!disconnected(urlp
->url_puhp
) && !sopen(&so
, urlp
)) {
1208 edit
= (isedit
!= 0);
1209 if (mb
.mb_imap_account
!= NULL
)
1210 free(mb
.mb_imap_account
);
1211 if (mb
.mb_imap_pass
!= NULL
)
1212 free(mb
.mb_imap_pass
);
1213 mb
.mb_imap_account
= sstrdup(urlp
->url_puhp
);
1214 /* TODO This is a hack to allow '@boxname'; in the end everything will be an
1215 * TODO object, and mailbox will naturally have an URL and credentials */
1216 mb
.mb_imap_pass
= sstrdup(ccred
.cc_pass
.s
);
1218 if (!same_imap_account
) {
1219 if (mb
.mb_sock
.s_fd
>= 0)
1220 sclose(&mb
.mb_sock
);
1222 same_imap_account
= 0;
1233 if (mb
.mb_imap_mailbox
!= NULL
)
1234 free(mb
.mb_imap_mailbox
);
1235 mb
.mb_imap_mailbox
= sstrdup((urlp
->url_path
.s
!= NULL
)
1236 ? urlp
->url_path
.s
: "INBOX");
1237 initbox(urlp
->url_puhpp
);
1239 mb
.mb_type
= MB_VOID
;
1240 mb
.mb_active
= MB_NONE
;
1243 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1244 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1245 if (sigsetjmp(imapjmp
, 1)) {
1246 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1247 sclose(&mb
.mb_sock
);
1248 safe_signal(SIGINT
, saveint
);
1249 safe_signal(SIGPIPE
, savepipe
);
1252 mb
.mb_type
= MB_VOID
;
1253 mb
.mb_active
= MB_NONE
;
1257 if (saveint
!= SIG_IGN
)
1258 safe_signal(SIGINT
, imapcatch
);
1259 if (savepipe
!= SIG_IGN
)
1260 safe_signal(SIGPIPE
, imapcatch
);
1262 if (mb
.mb_sock
.s_fd
< 0) {
1263 if (disconnected(mb
.mb_imap_account
)) {
1264 if (cache_setptr(transparent
) == STOP
)
1265 fprintf(stderr
, "Mailbox \"%s\" is not cached.\n",
1269 if ((cp
= ok_vlook(imap_keepalive
)) != NULL
) {
1270 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1271 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1272 alarm(imapkeepalive
);
1277 mb
.mb_sock
.s_desc
= "IMAP";
1278 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1279 if (imap_preauth(&mb
, urlp
->url_hp
.s
, urlp
->url_uhp
.s
) != OKAY
||
1280 imap_auth(&mb
, &ccred
) != OKAY
) {
1281 sclose(&mb
.mb_sock
);
1283 safe_signal(SIGINT
, saveint
);
1284 safe_signal(SIGPIPE
, savepipe
);
1289 } else /* same account */
1290 mb
.mb_flags
|= same_flags
;
1292 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
1293 mb
.mb_type
= MB_IMAP
;
1295 if (imap_select(&mb
, &mailsize
, &msgCount
,
1296 (urlp
->url_path
.s
!= NULL
? urlp
->url_path
.s
: "INBOX")) != OKAY
) {
1297 /*sclose(&mb.mb_sock);
1299 safe_signal(SIGINT
, saveint
);
1300 safe_signal(SIGPIPE
, savepipe
);
1302 mb
.mb_type
= MB_VOID
;
1308 imap_setptr(&mb
, nmail
, transparent
, UNVOLATILE(&prevcount
));
1311 if (!nmail
&& !transparent
)
1313 safe_signal(SIGINT
, saveint
);
1314 safe_signal(SIGPIPE
, savepipe
);
1317 if (!nmail
&& mb
.mb_type
== MB_IMAP
)
1318 purgecache(&mb
, message
, msgCount
);
1319 if ((nmail
|| transparent
) && mb
.mb_sorted
) {
1324 if (!nmail
&& !edit
&& msgCount
== 0) {
1325 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1326 !ok_blook(emptystart
))
1327 fprintf(stderr
, tr(258, "No mail at %s\n"), urlp
->url_puhpp
);
1332 newmailinfo(prevcount
);
1340 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1341 int need
, const char *head
, size_t headsize
, long headlines
)
1343 char *line
= NULL
, *lp
;
1344 size_t linesize
= 0, linelen
, size
= 0;
1345 int emptyline
= 0, lines
= 0, excess
= 0;
1349 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1350 offset
= ftell(mp
->mb_otf
);
1353 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1355 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1357 if (linelen
> expected
) {
1358 excess
= linelen
- expected
;
1362 * Need to mask 'From ' lines. This cannot be done properly
1363 * since some servers pass them as 'From ' and others as
1364 * '>From '. Although one could identify the first kind of
1365 * server in principle, it is not possible to identify the
1366 * second as '>From ' may also come from a server of the
1367 * first type as actual data. So do what is absolutely
1368 * necessary only - mask 'From '.
1370 * If the line is the first line of the message header, it
1371 * is likely a real 'From ' line. In this case, it is just
1372 * ignored since it violates all standards.
1373 * TODO can the latter *really* happen??
1376 /* Since we simply copy over data without doing any transfer
1377 * encoding reclassification/adjustment we *have* to perform
1378 * RFC 4155 compliant From_ quoting here */
1379 if (is_head(lp
, linelen
)) {
1380 if (lines
+ headlines
== 0)
1382 fputc('>', mp
->mb_otf
);
1385 if (lp
[linelen
-1] == '\n' && (linelen
== 1 || lp
[linelen
-2] == '\r')) {
1386 emptyline
= linelen
<= 2;
1388 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1389 size
+= linelen
- 1;
1392 fputc('\n', mp
->mb_otf
);
1395 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1400 if ((expected
-= linelen
) <= 0)
1404 /* This is very ugly; but some IMAP daemons don't end a
1405 * message with \r\n\r\n, and we need \n\n for mbox format */
1406 fputc('\n', mp
->mb_otf
);
1413 m
->m_size
= size
+ headsize
;
1414 m
->m_lines
= lines
+ headlines
;
1415 m
->m_block
= mailx_blockof(offset
);
1416 m
->m_offset
= mailx_offsetof(offset
);
1419 m
->m_have
|= HAVE_HEADER
;
1422 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1423 m
->m_xlines
= m
->m_lines
;
1424 m
->m_xsize
= m
->m_size
;
1434 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1435 const char *head
, size_t headsize
, long headlines
)
1442 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1443 offset
= ftell(mp
->mb_otf
);
1445 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1447 fwrite(str
, 1, len
, mp
->mb_otf
);
1448 fputc('\n', mp
->mb_otf
);
1454 m
->m_size
= headsize
+ len
;
1455 m
->m_lines
= headlines
+ 1;
1456 m
->m_block
= mailx_blockof(offset
);
1457 m
->m_offset
= mailx_offsetof(offset
);
1458 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1459 m
->m_xlines
= m
->m_lines
;
1460 m
->m_xsize
= m
->m_size
;
1466 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
1470 sighandler_type
volatile saveint
, savepipe
;
1471 char * volatile head
;
1472 char const *cp
, *loc
, * volatile item
, * volatile resp
;
1474 size_t volatile headsize
;
1477 long volatile headlines
;
1483 saveint
= savepipe
= SIG_IGN
;
1485 cp
= loc
= item
= resp
= NULL
;
1487 number
= (int)PTR2SIZE(m
- message
+ 1);
1494 if (getcache(mp
, m
, need
) == OKAY
)
1496 if (mp
->mb_type
== MB_CACHE
) {
1497 fprintf(stderr
, "Message %u not available.\n", number
);
1501 if (mp
->mb_sock
.s_fd
< 0) {
1502 fprintf(stderr
, "IMAP connection closed.\n");
1508 resp
= item
= "RFC822.HEADER";
1511 item
= "BODY.PEEK[]";
1513 if (m
->m_flag
& HAVE_HEADER
&& m
->m_size
) {
1514 char *hdr
= smalloc(m
->m_size
);
1516 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
1518 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
) != m
->m_size
) {
1523 headsize
= m
->m_size
;
1524 headlines
= m
->m_lines
;
1525 item
= "BODY.PEEK[TEXT]";
1526 resp
= "BODY[TEXT]";
1534 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1535 if (sigsetjmp(imapjmp
, 1)) {
1536 safe_signal(SIGINT
, saveint
);
1537 safe_signal(SIGPIPE
, savepipe
);
1541 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1542 safe_signal(SIGINT
, &_imap_maincatch
);
1543 if (savepipe
!= SIG_IGN
)
1544 safe_signal(SIGPIPE
, imapcatch
);
1547 snprintf(o
, sizeof o
, "%s UID FETCH %lu (%s)\r\n",
1548 tag(1), m
->m_uid
, item
);
1550 if (check_expunged() == STOP
)
1552 snprintf(o
, sizeof o
, "%s FETCH %u (%s)\r\n", tag(1), number
, item
);
1554 IMAP_OUT(o
, MB_COMD
, goto out
)
1556 ok
= imap_answer(mp
, 1);
1559 if (response_status
!= RESPONSE_OTHER
||
1560 response_other
!= MESSAGE_DATA_FETCH
)
1562 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
1565 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
1573 n
= responded_other_number
;
1574 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
1575 if (m
->m_uid
? m
->m_uid
!= u
: n
!= number
)
1577 if ((cp
= strchr(loc
, '"')) != NULL
) {
1578 cp
= imap_unquotestr(cp
);
1579 imap_putstr(mp
, m
, cp
, head
, headsize
, headlines
);
1581 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1582 m
->m_xlines
= m
->m_lines
;
1583 m
->m_xsize
= m
->m_size
;
1587 expected
= atol(&cp
[1]);
1588 if (m
->m_uid
? n
== 0 && m
->m_uid
!= u
: n
!= number
) {
1589 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
1593 imap_fetchdata(mp
, &mt
, expected
, need
, head
, headsize
, headlines
);
1595 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1598 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1599 if (options
& OPT_VERBOSE
)
1600 fputs(imapbuf
, stderr
);
1601 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1603 if (u
== m
->m_uid
) {
1604 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1611 while (mp
->mb_active
& MB_COMD
)
1612 ok
= imap_answer(mp
, 1);
1614 if (saveint
!= SIG_IGN
)
1615 safe_signal(SIGINT
, saveint
);
1616 if (savepipe
!= SIG_IGN
)
1617 safe_signal(SIGPIPE
, savepipe
);
1630 imap_header(struct message
*m
)
1635 rv
= imap_get(&mb
, m
, NEED_HEADER
);
1642 imap_body(struct message
*m
)
1647 rv
= imap_get(&mb
, m
, NEED_BODY
);
1653 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
1657 tomp
->m_size
= frommp
->m_size
;
1658 tomp
->m_lines
= frommp
->m_lines
;
1659 tomp
->m_block
= frommp
->m_block
;
1660 tomp
->m_offset
= frommp
->m_offset
;
1661 tomp
->m_have
= have
;
1662 if (have
& HAVE_BODY
) {
1663 tomp
->m_xlines
= frommp
->m_lines
;
1664 tomp
->m_xsize
= frommp
->m_size
;
1671 imap_fetchheaders(struct mailbox
*mp
, struct message
*m
, int bot
, int topp
)
1679 FILE *queuefp
= NULL
;
1684 snprintf(o
, sizeof o
, "%s UID FETCH %lu:%lu (RFC822.HEADER)\r\n",
1685 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
1687 if (check_expunged() == STOP
)
1689 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.HEADER)\r\n",
1692 IMAP_OUT(o
, MB_COMD
, return STOP
)
1694 ok
= imap_answer(mp
, 1);
1695 if (response_status
!= RESPONSE_OTHER
)
1697 if (response_other
!= MESSAGE_DATA_FETCH
)
1699 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0)
1701 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
1703 expected
= atol(&cp
[1]);
1704 if (m
[bot
-1].m_uid
) {
1705 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
) {
1707 for (n
= bot
; n
<= topp
; n
++)
1708 if ((unsigned long)u
== m
[n
-1].m_uid
)
1711 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1717 n
= responded_other_number
;
1718 if (n
<= 0 || n
> msgCount
) {
1719 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1723 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
1724 if (n
>= 0 && !(m
[n
-1].m_have
& HAVE_HEADER
))
1725 commitmsg(mp
, &m
[n
-1], &mt
, HAVE_HEADER
);
1726 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1727 if (options
& OPT_VERBOSE
)
1728 fputs(imapbuf
, stderr
);
1729 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1731 for (n
= bot
; n
<= topp
; n
++)
1732 if ((unsigned long)u
== m
[n
-1].m_uid
)
1734 if (n
<= topp
&& !(m
[n
-1].m_have
& HAVE_HEADER
))
1735 commitmsg(mp
, &m
[n
-1], &mt
,HAVE_HEADER
);
1740 while (mp
->mb_active
& MB_COMD
)
1741 ok
= imap_answer(mp
, 1);
1746 imap_getheaders(int volatile bot
, int volatile topp
) /* TODO iterator!! */
1748 sighandler_type saveint
, savepipe
;
1749 /* enum okay ok = STOP;*/
1753 if (mb
.mb_type
== MB_CACHE
)
1757 if (topp
> msgCount
)
1759 for (i
= bot
; i
< topp
; i
++) {
1760 if (message
[i
-1].m_have
& HAVE_HEADER
||
1761 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1766 for (i
= topp
; i
> bot
; i
--) {
1767 if (message
[i
-1].m_have
& HAVE_HEADER
||
1768 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1777 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1778 safe_signal(SIGINT
, &_imap_maincatch
);
1779 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1780 if (sigsetjmp(imapjmp
, 1) == 0) {
1781 if (savepipe
!= SIG_IGN
)
1782 safe_signal(SIGPIPE
, imapcatch
);
1784 for (i
= bot
; i
<= topp
; i
+= chunk
) {
1785 int j
= i
+ chunk
- 1;
1787 if (visible(message
+ j
))
1788 /*ok = */imap_fetchheaders(&mb
, message
, i
, j
);
1790 onintr(0); /* XXX imaplock? */
1793 safe_signal(SIGINT
, saveint
);
1794 safe_signal(SIGPIPE
, savepipe
);
1799 __imap_exit(struct mailbox
*mp
)
1802 FILE *queuefp
= NULL
;
1805 mp
->mb_active
|= MB_BYE
;
1806 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
1807 IMAP_OUT(o
, MB_COMD
, return STOP
)
1813 imap_exit(struct mailbox
*mp
)
1818 rv
= __imap_exit(mp
);
1819 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
1820 free(mp
->mb_imap_pass
);
1821 free(mp
->mb_imap_account
);
1822 free(mp
->mb_imap_mailbox
);
1823 if (mp
->mb_cache_directory
!= NULL
)
1824 free(mp
->mb_cache_directory
);
1825 #ifndef HAVE_DEBUG /* TODO ASSERT LEGACY */
1826 mp
->mb_imap_account
=
1827 mp
->mb_imap_mailbox
=
1828 mp
->mb_cache_directory
= "";
1830 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
1831 mp
->mb_imap_mailbox
= NULL
;
1832 mp
->mb_cache_directory
= NULL
;
1835 sclose(&mp
->mb_sock
);
1841 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
1844 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
1845 if (mp
->mb_type
== MB_IMAP
)
1852 imap_close(struct mailbox
*mp
)
1855 FILE *queuefp
= NULL
;
1858 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
1859 IMAP_OUT(o
, MB_COMD
, return STOP
)
1865 imap_update(struct mailbox
*mp
)
1868 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
1871 if (!edit
&& mp
->mb_perm
!= 0) {
1874 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1875 if (m
->m_flag
& MBOX
)
1878 if (makembox() == STOP
)
1883 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
1884 if (mp
->mb_perm
== 0)
1887 dodel
= ((m
->m_flag
& MDELETED
) != 0);
1889 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
1891 /* Fetch the result after around each 800 STORE commands
1892 * sent (approx. 32k data sent). Otherwise, servers will
1893 * try to flush the return queue at some point, leading
1894 * to a deadlock if we are still writing commands but not
1895 * reading their results */
1896 needstat
= stored
> 0 && stored
% 800 == 0;
1897 /* Even if this message has been deleted, continue
1898 * to set further flags. This is necessary to support
1899 * Gmail semantics, where "delete" actually means
1900 * "archive", and the flags are applied to the copy
1902 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
1903 imap_store(mp
, m
, m
-message
+1, '+', "\\Seen", needstat
);
1906 if (m
->m_flag
& MFLAG
) {
1907 imap_store(mp
, m
, m
-message
+1, '+', "\\Flagged", needstat
);
1910 if (m
->m_flag
& MUNFLAG
) {
1911 imap_store(mp
, m
, m
-message
+1, '-', "\\Flagged", needstat
);
1914 if (m
->m_flag
& MANSWER
) {
1915 imap_store(mp
, m
, m
-message
+1, '+', "\\Answered", needstat
);
1918 if (m
->m_flag
& MUNANSWER
) {
1919 imap_store(mp
, m
, m
-message
+1, '-', "\\Answered", needstat
);
1922 if (m
->m_flag
& MDRAFT
) {
1923 imap_store(mp
, m
, m
-message
+1, '+', "\\Draft", needstat
);
1926 if (m
->m_flag
& MUNDRAFT
) {
1927 imap_store(mp
, m
, m
-message
+1, '-', "\\Draft", needstat
);
1932 imap_delete(mp
, m
-message
+1, m
, needstat
);
1935 } else if (mp
->mb_type
!= MB_CACHE
||
1936 (!edit
&& !(m
->m_flag
& (MBOXED
| MSAVED
| MDELETED
))) ||
1937 (m
->m_flag
& (MBOXED
| MPRESERVE
| MTOUCH
)) ==
1938 (MPRESERVE
| MTOUCH
) || (edit
&& !(m
->m_flag
& MDELETED
)))
1940 if (m
->m_flag
& MNEW
) {
1942 m
->m_flag
|= MSTATUS
;
1949 for (m
= &message
[0]; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1950 if (!(m
->m_flag
& MUNLINKED
) &&
1951 m
->m_flag
& (MBOXED
| MDELETED
| MSAVED
| MSTATUS
| MFLAG
|
1952 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
)) {
1956 if ((gotcha
|| modflags
) && edit
) {
1957 printf(tr(168, "\"%s\" "), displayname
);
1958 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1959 ? tr(170, "complete\n") : tr(212, "updated.\n"));
1960 } else if (held
&& !edit
&& mp
->mb_perm
!= 0) {
1962 printf(tr(155, "Held 1 message in %s\n"), displayname
);
1964 printf(tr(156, "Held %d messages in %s\n"), held
, displayname
);
1974 sighandler_type
volatile saveint
, savepipe
;
1977 if (mb
.mb_type
== MB_CACHE
) {
1982 if (mb
.mb_sock
.s_fd
< 0) {
1983 fprintf(stderr
, "IMAP connection closed.\n");
1988 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1989 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1990 if (sigsetjmp(imapjmp
, 1)) {
1991 safe_signal(SIGINT
, saveint
);
1992 safe_signal(SIGPIPE
, saveint
);
1996 if (saveint
!= SIG_IGN
)
1997 safe_signal(SIGINT
, imapcatch
);
1998 if (savepipe
!= SIG_IGN
)
1999 safe_signal(SIGPIPE
, imapcatch
);
2002 if (!same_imap_account
)
2005 safe_signal(SIGINT
, saveint
);
2006 safe_signal(SIGPIPE
, savepipe
);
2013 imap_store(struct mailbox
*mp
, struct message
*m
, int n
, int c
, const char *sp
,
2017 FILE *queuefp
= NULL
;
2020 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
2023 snprintf(o
, sizeof o
, "%s UID STORE %lu %cFLAGS (%s)\r\n",
2024 tag(1), m
->m_uid
, c
, sp
);
2026 if (check_expunged() == STOP
)
2028 snprintf(o
, sizeof o
, "%s STORE %u %cFLAGS (%s)\r\n", tag(1), n
, c
, sp
);
2030 IMAP_OUT(o
, MB_COMD
, return STOP
)
2034 mb
.mb_active
&= ~MB_COMD
;
2035 if (queuefp
!= NULL
)
2041 imap_undelete(struct message
*m
, int n
)
2046 rv
= imap_unstore(m
, n
, "\\Deleted");
2052 imap_unread(struct message
*m
, int n
)
2057 rv
= imap_unstore(m
, n
, "\\Seen");
2063 imap_unstore(struct message
*m
, int n
, const char *flag
)
2065 sighandler_type saveint
, savepipe
;
2066 enum okay rv
= STOP
;
2070 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2071 safe_signal(SIGINT
, &_imap_maincatch
);
2072 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2073 if (sigsetjmp(imapjmp
, 1) == 0) {
2074 if (savepipe
!= SIG_IGN
)
2075 safe_signal(SIGPIPE
, imapcatch
);
2077 rv
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2079 safe_signal(SIGINT
, saveint
);
2080 safe_signal(SIGPIPE
, savepipe
);
2098 snprintf(ts
, sizeof ts
, "T%lu", n
);
2104 c_imap_imap(void *vp
)
2107 sighandler_type saveint
, savepipe
;
2108 struct mailbox
*mp
= &mb
;
2109 FILE *queuefp
= NULL
;
2110 enum okay
volatile ok
= STOP
;
2113 if (mp
->mb_type
!= MB_IMAP
) {
2114 printf("Not operating on an IMAP mailbox.\n");
2118 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2119 safe_signal(SIGINT
, &_imap_maincatch
);
2120 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2121 if (sigsetjmp(imapjmp
, 1) == 0) {
2122 if (savepipe
!= SIG_IGN
)
2123 safe_signal(SIGPIPE
, imapcatch
);
2125 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2126 IMAP_OUT(o
, MB_COMD
, goto out
)
2127 while (mp
->mb_active
& MB_COMD
) {
2128 ok
= imap_answer(mp
, 0);
2129 fputs(responded_text
, stdout
);
2133 safe_signal(SIGINT
, saveint
);
2134 safe_signal(SIGPIPE
, savepipe
);
2143 imap_newmail(int nmail
)
2147 if (nmail
&& had_exists
< 0 && had_expunge
< 0) {
2153 if (had_exists
== msgCount
&& had_expunge
< 0)
2154 /* Some servers always respond with EXISTS to NOOP. If
2155 * the mailbox has been changed but the number of messages
2156 * has not, an EXPUNGE must also had been sent; otherwise,
2157 * nothing has changed */
2160 return (had_expunge
>= 0 ? 2 : (had_exists
>= 0 ? 1 : 0));
2164 imap_putflags(int f
)
2170 bp
= buf
= salloc(100);
2171 if (f
& (MREAD
| MFLAGGED
| MANSWERED
| MDRAFTED
)) {
2176 for (cp
= "\\Seen"; *cp
; cp
++)
2182 for (cp
= "\\Flagged"; *cp
; cp
++)
2185 if (f
& MANSWERED
) {
2188 for (cp
= "\\Answered"; *cp
; cp
++)
2194 for (cp
= "\\Draft"; *cp
; cp
++)
2206 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2209 while (*cp
!= ')') {
2211 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2213 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2215 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2217 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2219 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2221 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2233 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
, off_t off1
,
2234 long xsize
, enum mflag flag
, time_t t
)
2236 char o
[LINESIZE
], *buf
;
2237 size_t bufsize
, buflen
, cnt
;
2238 long size
, lines
, ysize
;
2240 FILE *queuefp
= NULL
;
2244 if (mp
->mb_type
== MB_CACHE
) {
2245 queuefp
= cache_queue(mp
);
2246 if (queuefp
== NULL
) {
2255 buf
= smalloc(bufsize
= LINESIZE
);
2260 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2265 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2266 tag(1), imap_quotestr(name
), imap_putflags(flag
),
2267 imap_make_date_time(t
), size
);
2268 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2269 while (mp
->mb_active
& MB_COMD
) {
2270 rv
= imap_answer(mp
, twice
);
2271 if (response_type
== RESPONSE_CONT
)
2275 if (mp
->mb_type
!= MB_CACHE
&& rv
== STOP
) {
2284 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2287 buf
[buflen
- 1] = '\r';
2289 if (mp
->mb_type
!= MB_CACHE
)
2290 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2292 fwrite(buf
, 1, buflen
+1, queuefp
);
2295 if (mp
->mb_type
!= MB_CACHE
)
2296 swrite(&mp
->mb_sock
, "\r\n");
2298 fputs("\r\n", queuefp
);
2299 while (mp
->mb_active
& MB_COMD
) {
2300 rv
= imap_answer(mp
, 0);
2301 if (response_status
== RESPONSE_NO
/*&&
2302 ascncasecmp(responded_text,
2303 "[TRYCREATE] ", 12) == 0*/) {
2309 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), imap_quotestr(name
));
2310 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2311 while (mp
->mb_active
& MB_COMD
)
2312 rv
= imap_answer(mp
, 1);
2315 imap_created_mailbox
++;
2317 } else if (rv
!= OKAY
)
2318 fprintf(stderr
, tr(270, "IMAP error: %s"), responded_text
);
2319 else if (response_status
== RESPONSE_OK
&& (mp
->mb_flags
& MB_UIDPLUS
))
2320 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
, flag
, name
);
2323 if (queuefp
!= NULL
)
2332 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2334 char *buf
, *bp
, *lp
;
2335 size_t bufsize
, buflen
, cnt
;
2336 off_t off1
= -1, offs
;
2337 int inhead
= 1, flag
= MNEW
| MNEWEST
;
2343 buf
= smalloc(bufsize
= LINESIZE
);
2350 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2351 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
2352 if (off1
!= (off_t
)-1) {
2353 rv
= imap_append1(mp
, name
, fp
, off1
, size
, flag
, tim
);
2356 fseek(fp
, offs
+buflen
, SEEK_SET
);
2358 off1
= offs
+ buflen
;
2363 tim
= unixtime(buf
);
2368 if (bp
&& buf
[0] == '\n')
2370 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
2372 while (whitechar(*lp
))
2375 while (*++lp
!= '\0')
2384 } else if (bp
&& inhead
&& ascncasecmp(buf
, "x-status", 8) == 0) {
2386 while (whitechar(*lp
))
2389 while (*++lp
!= '\0')
2402 } while (bp
!= NULL
);
2411 imap_append(const char *xserver
, FILE *fp
)
2413 sighandler_type
volatile saveint
, savepipe
;
2416 char const * volatile mbx
;
2417 enum okay rv
= STOP
;
2420 if (!url_parse(&url
, CPROTO_IMAP
, xserver
))
2422 if (!ok_blook(v15_compat
) &&
2423 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
2424 fprintf(stderr
, "New-style URL used without *v15-compat* being set!\n");
2425 mbx
= (url
.url_path
.s
!= NULL
) ? url
.url_path
.s
: "INBOX";
2428 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2429 safe_signal(SIGINT
, &_imap_maincatch
);
2430 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2431 if (sigsetjmp(imapjmp
, 1))
2433 if (savepipe
!= SIG_IGN
)
2434 safe_signal(SIGPIPE
, imapcatch
);
2436 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) && mb
.mb_imap_account
&&
2437 !strcmp(url
.url_puhp
, mb
.mb_imap_account
)) {
2438 rv
= imap_append0(&mb
, mbx
, fp
);
2442 if (!(ok_blook(v15_compat
) ? ccred_lookup(&ccred
, &url
)
2443 : ccred_lookup_old(&ccred
, CPROTO_IMAP
, url
.url_uhp
.s
)))
2446 memset(&mx
, 0, sizeof mx
);
2447 if (disconnected(url
.url_puhp
) == 0) {
2448 if (!sopen(&mx
.mb_sock
, &url
))
2450 mx
.mb_sock
.s_desc
= "IMAP";
2451 mx
.mb_type
= MB_IMAP
;
2452 mx
.mb_imap_account
= UNCONST(url
.url_puhp
);
2453 /* TODO the code now did
2454 * TODO mx.mb_imap_mailbox = mbx;
2455 * TODO though imap_mailbox is sfree()d and mbx
2456 * TODO is possibly even a constant
2457 * TODO i changed this to sstrdup() sofar, as is used
2458 * TODO somewhere else in this file for this! */
2459 mx
.mb_imap_mailbox
= sstrdup(mbx
);
2460 if (imap_preauth(&mx
, url
.url_hp
.s
, url
.url_uhp
.s
) != OKAY
||
2461 imap_auth(&mx
, &ccred
) != OKAY
) {
2462 sclose(&mx
.mb_sock
);
2465 rv
= imap_append0(&mx
, mbx
, fp
);
2468 mx
.mb_imap_account
= UNCONST(url
.url_puhp
);
2469 mx
.mb_imap_mailbox
= sstrdup(mbx
); /* TODO as above */
2470 mx
.mb_type
= MB_CACHE
;
2471 rv
= imap_append0(&mx
, mbx
, fp
);
2478 safe_signal(SIGINT
, saveint
);
2479 safe_signal(SIGPIPE
, savepipe
);
2489 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
2490 struct list_item
**lend
, int level
)
2492 char o
[LINESIZE
], *cp
;
2494 FILE *queuefp
= NULL
;
2495 struct list_item
*lp
;
2496 enum okay ok
= STOP
;
2499 *list
= *lend
= NULL
;
2500 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n", tag(1), imap_quotestr(base
));
2501 IMAP_OUT(o
, MB_COMD
, return STOP
)
2502 while (mp
->mb_active
& MB_COMD
) {
2503 ok
= imap_answer(mp
, 1);
2504 if (response_status
== RESPONSE_OTHER
&&
2505 response_other
== MAILBOX_DATA_LIST
&& imap_parse_list() == OKAY
) {
2506 cp
= imap_unquotestr(list_name
);
2507 lp
= csalloc(1, sizeof *lp
);
2509 for (bp
= base
; *bp
!= '\0' && *bp
== *cp
; ++bp
)
2511 lp
->l_base
= *cp
? cp
: savestr(base
);
2512 lp
->l_attr
= list_attributes
;
2513 lp
->l_level
= level
+1;
2514 lp
->l_delim
= list_hierarchy_delimiter
;
2515 if (*list
&& *lend
) {
2516 (*lend
)->l_next
= lp
;
2526 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
2528 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
2535 depth
= (cp
= ok_vlook(imap_list_depth
)) != NULL
? atoi(cp
) : 2;
2536 if ((rv
= imap_list1(mp
, base
, &list
, &lend
, 0)) == STOP
)
2539 if (list
== NULL
|| lend
== NULL
)
2542 for (lp
= list
; lp
; lp
= lp
->l_next
)
2543 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&& lp
->l_level
< depth
&&
2544 !(lp
->l_attr
& LIST_NOINFERIORS
)) {
2545 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
2546 memcpy(cp
, lp
->l_name
, n
);
2547 cp
[n
] = lp
->l_delim
;
2549 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&& lx
&& ly
) {
2550 lp
->l_has_children
= 1;
2551 if (strcmp(cp
, lx
->l_name
) == 0)
2560 for (lp
= list
; lp
; lp
= lp
->l_next
) {
2563 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2567 if (!(lp
->l_attr
& LIST_NOSELECT
))
2568 fprintf(fp
, "%s\n", *cp
? cp
: base
);
2569 else if (lp
->l_has_children
== 0)
2570 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
2571 (lp
->l_delim
!= EOF
? lp
->l_delim
: '\n'));
2579 imap_folders(const char * volatile name
, int strip
)
2581 sighandler_type saveint
, savepipe
;
2582 const char *fold
, *cp
, *sp
;
2586 cp
= protbase(name
);
2587 sp
= mb
.mb_imap_account
;
2588 if (sp
== NULL
|| strcmp(cp
, sp
)) {
2589 fprintf(stderr
, tr(502,
2590 "Cannot perform `folders' but when on the very IMAP "
2591 "account; the current one is\n `%s' -- "
2592 "try `folders @'.\n"),
2593 (sp
!= NULL
? sp
: tr(503, "[NONE]")));
2597 fold
= imap_fileof(name
);
2598 if (options
& OPT_TTYOUT
) {
2599 if ((fp
= Ftmp(NULL
, "imapfold", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
2608 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2609 safe_signal(SIGINT
, &_imap_maincatch
);
2610 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2611 if (sigsetjmp(imapjmp
, 1)) /* TODO imaplock? */
2613 if (savepipe
!= SIG_IGN
)
2614 safe_signal(SIGPIPE
, imapcatch
);
2616 if (mb
.mb_type
== MB_CACHE
)
2617 cache_list(&mb
, fold
, strip
, fp
);
2619 imap_list(&mb
, fold
, strip
, fp
);
2623 if (options
& OPT_TTYOUT
)
2629 if (options
& OPT_TTYOUT
) {
2634 fprintf(stderr
, "Folder not found.\n");
2637 safe_signal(SIGINT
, saveint
);
2638 safe_signal(SIGPIPE
, savepipe
);
2639 if (options
& OPT_TTYOUT
)
2652 long n
= 0, mx
= 0, columns
, width
;
2656 if ((out
= Ftmp(NULL
, "imapdopr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
2662 while ((c
= getc(fp
)) != EOF
) {
2673 if (mx
< width
/ 2) {
2674 columns
= width
/ (mx
+2);
2675 snprintf(o
, sizeof o
, "sort | pr -%lu -w%lu -t", columns
, width
);
2677 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
2678 run_command(XSHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
2679 page_or_print(out
, 0);
2686 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
2690 int twice
= 0, stored
= 0;
2691 FILE *queuefp
= NULL
;
2692 enum okay ok
= STOP
;
2695 if (mp
->mb_type
== MB_CACHE
) {
2696 if ((queuefp
= cache_queue(mp
)) == NULL
)
2700 qname
= imap_quotestr(name
= imap_fileof(name
));
2701 /* Since it is not possible to set flags on the copy, recently
2702 * set flags must be set on the original to include it in the copy */
2703 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
))
2704 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
2705 if (m
->m_flag
&MFLAG
)
2706 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2707 if (m
->m_flag
&MUNFLAG
)
2708 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2709 if (m
->m_flag
&MANSWER
)
2710 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2711 if (m
->m_flag
&MUNANSWER
)
2712 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2713 if (m
->m_flag
&MDRAFT
)
2714 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2715 if (m
->m_flag
&MUNDRAFT
)
2716 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2719 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n", tag(1), m
->m_uid
, qname
);
2721 if (check_expunged() == STOP
)
2723 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n", tag(1), n
, qname
);
2725 IMAP_OUT(o
, MB_COMD
, goto out
)
2726 while (mp
->mb_active
& MB_COMD
)
2727 ok
= imap_answer(mp
, twice
);
2729 if (mp
->mb_type
== MB_IMAP
&& mp
->mb_flags
& MB_UIDPLUS
&&
2730 response_status
== RESPONSE_OK
)
2731 imap_copyuid(mp
, m
, name
);
2733 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
2734 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2735 IMAP_OUT(o
, MB_COMD
, goto out
)
2736 while (mp
->mb_active
& MB_COMD
)
2737 ok
= imap_answer(mp
, 1);
2739 imap_created_mailbox
++;
2744 if (queuefp
!= NULL
)
2747 /* ... and reset the flag to its initial value so that the 'exit'
2748 * command still leaves the message unread */
2750 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
2751 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
2754 if (m
->m_flag
& MFLAG
) {
2755 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2758 if (m
->m_flag
& MUNFLAG
) {
2759 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2762 if (m
->m_flag
& MANSWER
) {
2763 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
2766 if (m
->m_flag
& MUNANSWER
) {
2767 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2770 if (m
->m_flag
& MDRAFT
) {
2771 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2774 if (m
->m_flag
& MUNDRAFT
) {
2775 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2779 mp
->mb_active
|= MB_COMD
;
2780 (void)imap_finish(mp
);
2786 imap_copy(struct message
*m
, int n
, const char *name
)
2788 sighandler_type saveint
, savepipe
;
2789 enum okay rv
= STOP
;
2793 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2794 safe_signal(SIGINT
, &_imap_maincatch
);
2795 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2796 if (sigsetjmp(imapjmp
, 1) == 0) {
2797 if (savepipe
!= SIG_IGN
)
2798 safe_signal(SIGPIPE
, imapcatch
);
2800 rv
= imap_copy1(&mb
, m
, n
, name
);
2802 safe_signal(SIGINT
, saveint
);
2803 safe_signal(SIGPIPE
, savepipe
);
2813 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
2814 unsigned long *olduid
, unsigned long *newuid
)
2820 *uidvalidity
= strtoul(cp
, &xp
, 10);
2821 *olduid
= strtoul(xp
, &yp
, 10);
2822 *newuid
= strtoul(yp
, &zp
, 10);
2823 rv
= (*uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
2824 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']');
2830 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
2837 *uidvalidity
= strtoul(cp
, &xp
, 10);
2838 *uid
= strtoul(xp
, &yp
, 10);
2839 rv
= (*uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' && yp
> xp
&&
2846 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
2851 unsigned long uidvalidity
, olduid
, newuid
;
2856 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
2857 imap_copyuid_parse(&cp
[9], &uidvalidity
, &olduid
, &newuid
) == STOP
)
2862 xmb
.mb_cache_directory
= NULL
;
2863 xmb
.mb_imap_mailbox
= savestr(name
);
2864 xmb
.mb_uidvalidity
= uidvalidity
;
2867 memset(&xm
, 0, sizeof xm
);
2869 if ((rv
= getcache1(mp
, &xm
, NEED_UNSPEC
, 3)) != OKAY
)
2871 getcache(mp
, &xm
, NEED_HEADER
);
2872 getcache(mp
, &xm
, NEED_BODY
);
2874 if ((m
->m_flag
& HAVE_HEADER
) == 0)
2875 getcache(mp
, m
, NEED_HEADER
);
2876 if ((m
->m_flag
& HAVE_BODY
) == 0)
2877 getcache(mp
, m
, NEED_BODY
);
2881 xm
.m_flag
&= ~MFULLYCACHED
;
2882 putcache(&xmb
, &xm
);
2889 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
, long xsize
,
2890 long size
, long lines
, int flag
, const char *name
)
2895 unsigned long uidvalidity
, uid
;
2900 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
2901 imap_appenduid_parse(&cp
[11], &uidvalidity
, &uid
) == STOP
)
2906 xmb
.mb_cache_directory
= NULL
;
2907 xmb
.mb_imap_mailbox
= savestr(name
);
2908 xmb
.mb_uidvalidity
= uidvalidity
;
2909 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
2911 memset(&xm
, 0, sizeof xm
);
2912 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
2914 xm
.m_block
= mailx_blockof(off1
);
2915 xm
.m_offset
= mailx_offsetof(off1
);
2918 xm
.m_lines
= xm
.m_xlines
= lines
;
2920 xm
.m_have
= HAVE_HEADER
| HAVE_BODY
;
2921 putcache(&xmb
, &xm
);
2928 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
2932 long size
, xsize
, ysize
, lines
;
2933 enum mflag flag
= MNEW
;
2934 char *name
, *buf
, *bp
;
2936 size_t bufsize
, buflen
, cnt
;
2937 enum okay rv
= STOP
;
2940 buf
= smalloc(bufsize
= LINESIZE
);
2943 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2946 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
2951 if ((cp
= strrchr(bp
, '{')) == NULL
)
2954 xsize
= atol(&cp
[1]) + 2;
2955 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
2961 imap_getflags(cp
, &cp
, &flag
);
2962 while (*++cp
== ' ')
2965 t
= imap_read_date_time(cp
);
2967 if ((tp
= Ftmp(NULL
, "imapapui", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
2974 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2977 buf
[--buflen
] = '\0';
2978 buf
[buflen
-1] = '\n';
2979 fwrite(buf
, 1, buflen
, tp
);
2986 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
2987 imap_unquotestr(name
));
2997 #ifdef HAVE_IMAP_SEARCH
2999 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
, const char *spec
,
3002 char *o
, *xp
, *cs
, c
;
3004 FILE *queuefp
= NULL
;
3008 enum okay ok
= STOP
;
3012 for (cp
= spec
; *cp
; cp
++)
3015 cp
= charset_get_lc();
3017 if (asccasecmp(cp
, "utf-8") && asccasecmp(cp
, "utf8")) {
3022 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
3023 sz
= strlen(spec
) + 1;
3024 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
3025 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
, FAL0
) == 0 &&
3034 cp
= imap_quotestr(cp
);
3035 cs
= salloc(n
= strlen(cp
) + 10);
3036 snprintf(cs
, n
, "CHARSET %s ", cp
);
3040 o
= ac_alloc(osize
= strlen(spec
) + 60);
3041 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
3042 IMAP_OUT(o
, MB_COMD
, goto out
)
3043 while (mp
->mb_active
& MB_COMD
) {
3044 ok
= imap_answer(mp
, 0);
3045 if (response_status
== RESPONSE_OTHER
&&
3046 response_other
== MAILBOX_DATA_SEARCH
) {
3047 xp
= responded_other_text
;
3048 while (*xp
&& *xp
!= '\r') {
3049 n
= strtoul(xp
, &xp
, 10);
3050 for (i
= 0; i
< cnt
; i
++)
3051 if (m
[i
].m_uid
== n
&& !(m
[i
].m_flag
& MHIDDEN
) &&
3052 (f
== MDELETED
|| !(m
[i
].m_flag
& MDELETED
)))
3063 imap_search1(const char * volatile spec
, int f
)
3065 sighandler_type saveint
, savepipe
;
3066 enum okay
volatile rv
= STOP
;
3069 if (mb
.mb_type
!= MB_IMAP
)
3073 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3074 safe_signal(SIGINT
, &_imap_maincatch
);
3075 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3076 if (sigsetjmp(imapjmp
, 1) == 0) {
3077 if (savepipe
!= SIG_IGN
)
3078 safe_signal(SIGPIPE
, imapcatch
);
3080 rv
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
3082 safe_signal(SIGINT
, saveint
);
3083 safe_signal(SIGPIPE
, savepipe
);
3091 #endif /* HAVE_IMAP_SEARCH */
3094 imap_thisaccount(const char *cp
)
3099 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
3101 else if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
3102 mb
.mb_imap_account
== NULL
)
3105 rv
= !strcmp(protbase(cp
), mb
.mb_imap_account
);
3111 imap_remove(const char * volatile name
)
3113 sighandler_type
volatile saveint
, savepipe
;
3114 enum okay rv
= STOP
;
3117 if (mb
.mb_type
!= MB_IMAP
) {
3118 fprintf(stderr
, "Refusing to remove \"%s\" in disconnected mode.\n",
3123 if (!imap_thisaccount(name
)) {
3124 fprintf(stderr
, "Can only remove mailboxes on current IMAP "
3125 "server: \"%s\" not removed.\n", name
);
3130 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3131 safe_signal(SIGINT
, &_imap_maincatch
);
3132 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3133 if (sigsetjmp(imapjmp
, 1) == 0) {
3134 if (savepipe
!= SIG_IGN
)
3135 safe_signal(SIGPIPE
, imapcatch
);
3137 rv
= imap_remove1(&mb
, imap_fileof(name
));
3139 safe_signal(SIGINT
, saveint
);
3140 safe_signal(SIGPIPE
, savepipe
);
3144 rv
= cache_remove(name
);
3153 imap_remove1(struct mailbox
*mp
, const char *name
)
3155 FILE *queuefp
= NULL
;
3158 enum okay ok
= STOP
;
3161 o
= ac_alloc(os
= 2*strlen(name
) + 100);
3162 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), imap_quotestr(name
));
3163 IMAP_OUT(o
, MB_COMD
, goto out
)
3164 while (mp
->mb_active
& MB_COMD
)
3165 ok
= imap_answer(mp
, 1);
3172 imap_rename(const char *old
, const char *new)
3174 sighandler_type saveint
, savepipe
;
3175 enum okay rv
= STOP
;
3178 if (mb
.mb_type
!= MB_IMAP
) {
3179 fprintf(stderr
, "Refusing to rename mailboxes in disconnected mode.\n");
3183 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3184 fprintf(stderr
, "Can only rename mailboxes on current IMAP "
3185 "server: \"%s\" not renamed to \"%s\".\n", old
, new);
3190 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3191 safe_signal(SIGINT
, &_imap_maincatch
);
3192 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3193 if (sigsetjmp(imapjmp
, 1) == 0) {
3194 if (savepipe
!= SIG_IGN
)
3195 safe_signal(SIGPIPE
, imapcatch
);
3197 rv
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3199 safe_signal(SIGINT
, saveint
);
3200 safe_signal(SIGPIPE
, savepipe
);
3204 rv
= cache_rename(old
, new);
3213 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3215 FILE *queuefp
= NULL
;
3218 enum okay ok
= STOP
;
3221 o
= ac_alloc(os
= 2*strlen(old
) + 2*strlen(new) + 100);
3222 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1), imap_quotestr(old
),
3223 imap_quotestr(new));
3224 IMAP_OUT(o
, MB_COMD
, goto out
)
3225 while (mp
->mb_active
& MB_COMD
)
3226 ok
= imap_answer(mp
, 1);
3233 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3235 char o
[LINESIZE
], *newname
, *buf
, *bp
, *cp
, iob
[4096];
3236 size_t bufsize
, buflen
, cnt
;
3237 long offs
, offs1
, offs2
, octets
;
3238 int twice
, gotcha
= 0;
3239 FILE *queuefp
= NULL
;
3240 enum okay ok
= OKAY
, rok
= OKAY
;
3243 buf
= smalloc(bufsize
= LINESIZE
);
3246 while ((offs1
= ftell(fp
)) >= 0 &&
3247 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) != NULL
) {
3248 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3253 if ((offs
= ftell(fp
)) < 0)
3256 snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3257 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3259 while (digitchar(*cp
))
3265 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3267 IMAP_OUT(o
, MB_COMD
, continue)
3268 while (mp
->mb_active
& MB_COMD
)
3269 ok
= imap_answer(mp
, twice
);
3270 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3272 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3273 imap_copyuid(mp
, NULL
, imap_unquotestr(newname
));
3275 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3276 IMAP_OUT(o
, MB_COMD
, continue)
3277 while (mp
->mb_active
& MB_COMD
)
3278 ok
= imap_answer(mp
, 1);
3281 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3282 if ((cp
= strrchr(bp
, '{')) == NULL
)
3284 octets
= atol(&cp
[1]) + 2;
3285 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3287 IMAP_OUT(o
, MB_COMD
, continue)
3288 while (mp
->mb_active
& MB_COMD
) {
3289 ok
= imap_answer(mp
, twice
);
3290 if (response_type
== RESPONSE_CONT
)
3294 if (twice
++ == 0 && fseek(fp
, offs
, SEEK_SET
) >= 0)
3298 while (octets
> 0) {
3299 size_t n
= (UICMP(z
, octets
, >, sizeof iob
)
3300 ? sizeof iob
: (size_t)octets
);
3302 if (n
!= fread(iob
, 1, n
, fp
))
3304 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3306 swrite(&mp
->mb_sock
, "");
3307 while (mp
->mb_active
& MB_COMD
) {
3308 ok
= imap_answer(mp
, 0);
3309 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
3310 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3315 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3316 if ((offs2
= ftell(fp
)) < 0)
3318 fseek(fp
, offs1
, SEEK_SET
);
3319 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3320 (void)fseek(fp
, offs2
, SEEK_SET
);
3326 fprintf(stderr
, "Invalid command in IMAP cache queue: \"%s\"\n", bp
);
3331 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), newname
);
3332 IMAP_OUT(o
, MB_COMD
, continue)
3333 while (mp
->mb_active
& MB_COMD
)
3334 ok
= imap_answer(mp
, 1);
3340 ftruncate(fileno(fp
), 0);
3348 imap_strex(char const *cp
, char const **xp
)
3357 for (cq
= cp
+ 1; *cq
!= '\0'; ++cq
) {
3360 else if (*cq
== '"')
3366 n
= salloc(cq
- cp
+ 2);
3367 memcpy(n
, cp
, cq
- cp
+1);
3368 n
[cq
- cp
+ 1] = '\0';
3377 check_expunged(void)
3382 if (expunged_messages
> 0) {
3383 fprintf(stderr
, "Command not executed - messages have been expunged\n");
3392 c_connect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
3395 int rv
, omsgCount
= msgCount
;
3399 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
3400 fprintf(stderr
, "Already connected.\n");
3405 if (!url_parse(&url
, CPROTO_IMAP
, mailname
)) {
3409 var_clear_allow_undefined
= TRU1
;
3410 ok_bclear(disconnected
);
3411 vok_bclear(savecat("disconnected-", url
.url_uhp
.s
));
3412 var_clear_allow_undefined
= FAL0
;
3414 if (mb
.mb_type
== MB_CACHE
) {
3415 _imap_setfile1(&url
, 0, edit
, 1);
3416 if (msgCount
> omsgCount
)
3417 newmailinfo(omsgCount
);
3426 c_disconnect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
3429 int rv
= 1, *msgvec
= vp
;
3432 if (mb
.mb_type
== MB_CACHE
) {
3433 fprintf(stderr
, "Not connected.\n");
3436 if (mb
.mb_type
!= MB_IMAP
|| cached_uidvalidity(&mb
) == 0) {
3437 fprintf(stderr
, "The current mailbox is not cached.\n");
3441 if (!url_parse(&url
, CPROTO_IMAP
, mailname
))
3446 ok_bset(disconnected
, TRU1
);
3447 if (mb
.mb_type
== MB_IMAP
) {
3448 sclose(&mb
.mb_sock
);
3449 _imap_setfile1(&url
, 0, edit
, 1);
3460 int rv
= 1, *msgvec
= vp
, *ip
;
3464 if (mb
.mb_type
!= MB_IMAP
) {
3465 fprintf(stderr
, "Not connected to an IMAP server.\n");
3468 if (cached_uidvalidity(&mb
) == 0) {
3469 fprintf(stderr
, "The current mailbox is not cached.\n");
3473 for (ip
= msgvec
; *ip
; ++ip
) {
3474 mp
= &message
[*ip
- 1];
3475 if (!(mp
->m_have
& HAVE_BODY
))
3485 disconnected(const char *file
)
3491 if (ok_blook(disconnected
)) {
3496 if (!url_parse(&url
, CPROTO_IMAP
, file
)) {
3500 rv
= vok_blook(savecat("disconnected-", url
.url_uhp
.s
));
3508 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
3510 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
3518 while (PTRCMP(omp
, <, omessage
+ omsgCount
) &&
3519 PTRCMP(nmp
, <, message
+ msgCount
)) {
3520 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
3522 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
3524 if (omp
->m_uid
== nmp
->m_uid
) {
3525 hf
= nmp
->m_flag
& MHIDDEN
;
3526 if (transparent
&& mb
.mb_type
== MB_IMAP
)
3527 omp
->m_flag
&= ~MHIDDEN
;
3529 if (transparent
&& mb
.mb_type
== MB_CACHE
)
3530 nmp
[-1].m_flag
|= hf
;
3531 } else if (omp
->m_uid
< nmp
->m_uid
)
3538 prevdot
= newprevdot
;
3544 imap_read_date_time(const char *cp
)
3548 int i
, year
, month
, day
, hour
, minute
, second
, sign
= -1;
3551 /* "25-Jul-2004 15:33:44 +0200"
3553 * 0 5 10 15 20 25 */
3554 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
3556 day
= strtol(&cp
[1], NULL
, 10);
3558 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
3560 if (month_names
[++i
][0] == '\0')
3564 year
= strtol(&cp
[8], NULL
, 10);
3565 hour
= strtol(&cp
[13], NULL
, 10);
3566 minute
= strtol(&cp
[16], NULL
, 10);
3567 second
= strtol(&cp
[19], NULL
, 10);
3568 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
3582 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
3585 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
3595 imap_make_date_time(time_t t
)
3599 int tzdiff
, tzdiff_hour
, tzdiff_min
;
3602 tzdiff
= t
- mktime(gmtime(&t
));
3603 tzdiff_hour
= (int)(tzdiff
/ 60);
3604 tzdiff_min
= tzdiff_hour
% 60;
3606 tmptr
= localtime(&t
);
3607 if (tmptr
->tm_isdst
> 0)
3609 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
3610 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
], tmptr
->tm_year
+ 1900,
3611 tmptr
->tm_hour
, tmptr
->tm_min
, tmptr
->tm_sec
, tzdiff_hour
, tzdiff_min
);
3615 #endif /* HAVE_IMAP */
3617 #if defined HAVE_IMAP || defined HAVE_IMAP_SEARCH
3619 imap_quotestr(char const *s
)
3624 np
= n
= salloc(2 * strlen(s
) + 3);
3627 if (*s
== '"' || *s
== '\\')
3638 imap_unquotestr(char const *s
)
3648 np
= n
= salloc(strlen(s
) + 1);
3661 #endif /* defined HAVE_IMAP || defined HAVE_IMAP_SEARCH */
3663 /* vim:set fenc=utf-8:s-it-mode */