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 - 2013 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
43 # include <sys/socket.h>
47 # include <netinet/in.h>
49 # ifdef HAVE_ARPA_INET_H
50 # include <arpa/inet.h>
58 #define IMAP_ANSWER() { \
59 if (mp->mb_type != MB_CACHE) { \
60 enum okay ok = OKAY; \
61 while (mp->mb_active & MB_COMD) \
62 ok = imap_answer(mp, 1); \
67 /* TODO IMAP_OUT() simply returns instead of doing "actioN" if imap_finish()
68 * TODO fails, which leaves behind leaks in, e.g., imap_append1()!
69 * TODO IMAP_XOUT() was added due to this, but (1) needs to be used everywhere
70 * TODO and (2) doesn't handle all I/O errors itself, yet, too.
71 * TODO I.e., that should be a function, not a macro ... or so.
72 * TODO This entire module needs MASSIVE work! */
73 #define IMAP_OUT(X,Y,ACTION) IMAP_XOUT(X, Y, ACTION, return STOP)
74 #define IMAP_XOUT(X,Y,ACTIONERR,ACTIONBAIL)\
76 if (mp->mb_type != MB_CACHE) {\
77 if (imap_finish(mp) == STOP) {\
80 if (options & OPT_VERBOSE)\
81 fprintf(stderr, ">>> %s", X);\
83 if (swrite(&mp->mb_sock, X) == STOP) {\
92 static struct record
{
93 struct record
*rec_next
;
94 unsigned long rec_count
;
119 static char *responded_tag
;
120 static char *responded_text
;
121 static char *responded_other_text
;
122 static long responded_other_number
;
128 MAILBOX_DATA_MAILBOX
,
133 MESSAGE_DATA_EXPUNGE
,
136 RESPONSE_OTHER_UNKNOWN
139 static enum list_attributes
{
141 LIST_NOINFERIORS
= 001,
147 static int list_hierarchy_delimiter
;
148 static char *list_name
;
151 struct list_item
*l_next
;
154 enum list_attributes l_attr
;
160 static char *imapbuf
; /* TODO not static, use pool */
161 static size_t imapbufsize
;
162 static sigjmp_buf imapjmp
;
163 static sighandler_type savealrm
;
164 static int imapkeepalive
;
165 static long had_exists
= -1;
166 static long had_expunge
= -1;
167 static long expunged_messages
;
168 static volatile int 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 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
,
194 char *xuser
, char *xpass
);
196 static enum okay
imap_login(struct mailbox
*mp
, char *xuser
, char *xpass
);
198 static enum okay
imap_gss(struct mailbox
*mp
, char *user
);
200 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
201 static void imap_init(struct mailbox
*mp
, int n
);
202 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
204 static void imap_split(char **server
, const char **sp
, int *use_ssl
,
205 const char **cp
, char const **uhp
, char const **mbx
,
206 char **pass
, char **user
);
207 static int imap_setfile1(const char *xserver
, int nmail
, int isedit
,
209 static int imap_fetchdata(struct mailbox
*mp
, struct message
*m
,
210 size_t expected
, int need
,
211 const char *head
, size_t headsize
, long headlines
);
212 static void imap_putstr(struct mailbox
*mp
, struct message
*m
,
214 const char *head
, size_t headsize
, long headlines
);
215 static enum okay
imap_get(struct mailbox
*mp
, struct message
*m
,
217 static void commitmsg(struct mailbox
*mp
, struct message
*to
,
218 struct message
*from
, enum havespec have
);
219 static enum okay
imap_fetchheaders(struct mailbox
*mp
, struct message
*m
,
221 static enum okay
imap_exit(struct mailbox
*mp
);
222 static enum okay
imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int
224 static enum okay
imap_close(struct mailbox
*mp
);
225 static enum okay
imap_update(struct mailbox
*mp
);
226 static enum okay
imap_store(struct mailbox
*mp
, struct message
*m
,
227 int n
, int c
, const char *sp
, int needstat
);
228 static enum okay
imap_unstore(struct message
*m
, int n
, const char *flag
);
229 static const char *tag(int new);
230 static char *imap_putflags(int f
);
231 static void imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
);
232 static enum okay
imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
233 off_t off1
, long xsize
, enum mflag flag
, time_t t
);
234 static enum okay
imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
);
235 static enum okay
imap_list1(struct mailbox
*mp
, const char *base
,
236 struct list_item
**list
, struct list_item
**lend
, int level
);
237 static enum okay
imap_list(struct mailbox
*mp
, const char *base
,
238 int strip
, FILE *fp
);
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
, unsigned long *uidvalidity
,
243 unsigned long *olduid
, unsigned long *newuid
);
244 static enum okay
imap_appenduid_parse(const char *cp
,
245 unsigned long *uidvalidity
, unsigned long *uid
);
246 static enum okay
imap_copyuid(struct mailbox
*mp
, struct message
*m
,
248 static enum okay
imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
,
249 long off1
, long xsize
, long size
, long lines
,
250 int flag
, const char *name
);
251 static enum okay
imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
);
252 static enum okay
imap_search2(struct mailbox
*mp
, struct message
*m
,
253 int cnt
, const char *spec
, int f
);
254 static enum okay
imap_remove1(struct mailbox
*mp
, const char *name
);
255 static enum okay
imap_rename1(struct mailbox
*mp
, const char *old
,
257 static char *imap_strex(char const *cp
, char const **xp
);
258 static enum okay
check_expunged(void);
261 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
;
306 imap_response_get(const char **cp
)
308 if (ascncasecmp(*cp
, "OK ", 3) == 0) {
310 response_status
= RESPONSE_OK
;
311 } else if (ascncasecmp(*cp
, "NO ", 3) == 0) {
313 response_status
= RESPONSE_NO
;
314 } else if (ascncasecmp(*cp
, "BAD ", 4) == 0) {
316 response_status
= RESPONSE_BAD
;
317 } else if (ascncasecmp(*cp
, "PREAUTH ", 8) == 0) {
319 response_status
= RESPONSE_PREAUTH
;
320 } else if (ascncasecmp(*cp
, "BYE ", 4) == 0) {
322 response_status
= RESPONSE_BYE
;
324 response_status
= RESPONSE_OTHER
;
328 imap_response_parse(void)
330 static char *parsebuf
; /* TODO Use pool */
331 static size_t parsebufsize
;
332 const char *ip
= imapbuf
;
335 if (parsebufsize
< imapbufsize
)
336 parsebuf
= srealloc(parsebuf
, parsebufsize
= imapbufsize
);
337 memcpy(parsebuf
, imapbuf
, strlen(imapbuf
) + 1);
341 response_type
= RESPONSE_CONT
;
356 imap_response_get(&ip
);
357 pp
= &parsebuf
[ip
- imapbuf
];
358 switch (response_status
) {
360 response_type
= RESPONSE_FATAL
;
363 response_type
= RESPONSE_DATA
;
367 responded_tag
= parsebuf
;
368 while (*pp
&& *pp
!= ' ')
371 response_type
= RESPONSE_ILLEGAL
;
375 while (*pp
&& *pp
== ' ')
378 response_type
= RESPONSE_ILLEGAL
;
381 ip
= &imapbuf
[pp
- parsebuf
];
382 response_type
= RESPONSE_TAGGED
;
383 imap_response_get(&ip
);
384 pp
= &parsebuf
[ip
- imapbuf
];
387 if (response_type
!= RESPONSE_CONT
&&
388 response_type
!= RESPONSE_ILLEGAL
&&
389 response_status
== RESPONSE_OTHER
)
394 imap_answer(struct mailbox
*mp
, int errprnt
)
399 if (mp
->mb_type
== MB_CACHE
)
401 again
: if (sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
402 if (options
& OPT_VERBOSE
)
403 fputs(imapbuf
, stderr
);
404 imap_response_parse();
405 if (response_type
== RESPONSE_ILLEGAL
)
407 if (response_type
== RESPONSE_CONT
)
409 if (response_status
== RESPONSE_OTHER
) {
410 if (response_other
== MAILBOX_DATA_EXISTS
) {
411 had_exists
= responded_other_number
;
412 rec_queue(REC_EXISTS
, responded_other_number
);
415 } else if (response_other
== MESSAGE_DATA_EXPUNGE
) {
416 rec_queue(REC_EXPUNGE
, responded_other_number
);
424 if (response_type
== RESPONSE_TAGGED
) {
425 if (asccasecmp(responded_tag
, tag(0)) == 0)
430 switch (response_status
) {
431 case RESPONSE_PREAUTH
:
432 mp
->mb_active
&= ~MB_PREAUTH
;
443 fprintf(stderr
, tr(270, "IMAP error: %s"),
446 case RESPONSE_UNKNOWN
: /* does not happen */
449 mp
->mb_active
= MB_NONE
;
457 if (response_status
!= RESPONSE_OTHER
&&
458 ascncasecmp(responded_text
, "[ALERT] ", 8) == 0)
459 fprintf(stderr
, "IMAP alert: %s", &responded_text
[8]);
461 mp
->mb_active
&= ~MB_COMD
;
464 mp
->mb_active
= MB_NONE
;
470 imap_parse_list(void)
474 cp
= responded_other_text
;
475 list_attributes
= LIST_NONE
;
477 while (*cp
&& *cp
!= ')') {
479 if (ascncasecmp(&cp
[1], "Noinferiors ", 12)
481 list_attributes
|= LIST_NOINFERIORS
;
483 } else if (ascncasecmp(&cp
[1], "Noselect ", 9)
485 list_attributes
|= LIST_NOSELECT
;
487 } else if (ascncasecmp(&cp
[1], "Marked ", 7)
489 list_attributes
|= LIST_MARKED
;
491 } else if (ascncasecmp(&cp
[1], "Unmarked ", 9)
493 list_attributes
|= LIST_UNMARKED
;
504 list_hierarchy_delimiter
= EOF
;
508 list_hierarchy_delimiter
= *cp
++ & 0377;
509 if (cp
[0] != '"' || cp
[1] != ' ')
512 } else if (cp
[0] == 'N' && cp
[1] == 'I' && cp
[2] == 'L' &&
514 list_hierarchy_delimiter
= EOF
;
520 while (*cp
&& *cp
!= '\r')
527 imap_finish(struct mailbox
*mp
)
529 while (mp
->mb_sock
.s_fd
> 0 && mp
->mb_active
& MB_COMD
)
537 if (imapkeepalive
> 0) {
539 safe_signal(SIGALRM
, savealrm
);
543 /* TODO OOH MY GOOOOOOOOD WE SIGLONGJMP() FROM WITHIN SIGNAL HANDLERS!!! */
547 termios_state_reset();
550 fprintf(stderr
, tr(102, "Interrupt\n"));
551 siglongjmp(imapjmp
, 1);
554 fprintf(stderr
, tr(98,
555 "Received SIGPIPE during IMAP operation\n"));
560 /* TODO OOOOOHH MYYYY GOOOOOD WE DO ALL SORTS OF UNSAFE STUFF IN SIGHDLSS! */
565 if (interrupts
++ == 0) {
566 fprintf(stderr
, tr(102, "Interrupt\n"));
573 imap_noop1(struct mailbox
*mp
)
576 FILE *queuefp
= NULL
;
578 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
579 IMAP_OUT(o
, MB_COMD
, return STOP
)
585 imap_fileof(char const *xcp
)
587 char const *cp
= xcp
;
591 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
595 if (cp
[0] == '/' && state
== 1)
607 sighandler_type saveint
, savepipe
;
613 if (mb
.mb_type
!= MB_IMAP
)
616 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
617 safe_signal(SIGINT
, maincatch
);
618 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
619 if (sigsetjmp(imapjmp
, 1) == 0) {
620 if (savepipe
!= SIG_IGN
)
621 safe_signal(SIGPIPE
, imapcatch
);
622 ok
= imap_noop1(&mb
);
624 safe_signal(SIGINT
, saveint
);
625 safe_signal(SIGPIPE
, savepipe
);
633 rec_queue(enum rec_type rt
, unsigned long cnt
)
637 rp
= scalloc(1, sizeof *rp
);
640 if (record
&& recend
) {
641 recend
->rec_next
= rp
;
644 record
= recend
= rp
;
650 struct message
*omessage
;
652 struct record
*rp
= record
, *rq
= NULL
;
653 unsigned long exists
= 0, i
;
658 message
= smalloc((msgCount
+1) * sizeof *message
);
660 memcpy(message
, omessage
, msgCount
* sizeof *message
);
661 memset(&message
[msgCount
], 0, sizeof *message
);
663 switch (rp
->rec_type
) {
665 exists
= rp
->rec_count
;
668 if (rp
->rec_count
== 0) {
672 if (rp
->rec_count
> (unsigned long)msgCount
) {
673 if (exists
== 0 || rp
->rec_count
> exists
--)
679 delcache(&mb
, &message
[rp
->rec_count
-1]);
680 memmove(&message
[rp
->rec_count
-1],
681 &message
[rp
->rec_count
],
682 (msgCount
- rp
->rec_count
+ 1) *
686 * If the message was part of a collapsed thread,
687 * the m_collapsed field of one of its ancestors
688 * should be incremented. It seems hardly possible
689 * to do this with the current message structure,
690 * though. The result is that a '+' may be shown
691 * in the header summary even if no collapsed
703 record
= recend
= NULL
;
704 if (ok
== OKAY
&& exists
> (unsigned long)msgCount
) {
705 message
= srealloc(message
,
706 (exists
+ 1) * sizeof *message
);
707 memset(&message
[msgCount
], 0,
708 (exists
- msgCount
+ 1) * sizeof *message
);
709 for (i
= msgCount
; i
< exists
; i
++)
711 imap_flags(&mb
, msgCount
+1, exists
);
726 for (rp
= record
; rp
!= NULL
;) {
727 struct record
*tmp
= rp
;
731 record
= recend
= NULL
;
738 sighandler_type saveint
;
739 sighandler_type savepipe
;
742 if (imaplock
++ == 0) {
743 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
744 safe_signal(SIGINT
, maincatch
);
745 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
746 if (sigsetjmp(imapjmp
, 1)) {
747 safe_signal(SIGINT
, saveint
);
748 safe_signal(SIGPIPE
, savepipe
);
751 if (savepipe
!= SIG_IGN
)
752 safe_signal(SIGPIPE
, imapcatch
);
753 if (imap_noop1(&mb
) != OKAY
) {
754 safe_signal(SIGINT
, saveint
);
755 safe_signal(SIGPIPE
, savepipe
);
758 safe_signal(SIGINT
, saveint
);
759 safe_signal(SIGPIPE
, savepipe
);
761 brk
: alarm(imapkeepalive
);
766 imap_use_starttls(const char *uhp
)
770 if (value("imap-use-starttls"))
772 var
= savecat("imap-use-starttls-", uhp
);
773 return value(var
) != NULL
;
777 imap_preauth(struct mailbox
*mp
, const char *xserver
, const char *uhp
)
781 mp
->mb_active
|= MB_PREAUTH
;
783 if ((cp
= strchr(xserver
, ':')) != NULL
) {
784 char *x
= salloc(cp
- xserver
+ 1);
785 memcpy(x
, xserver
, cp
- xserver
);
786 x
[cp
- xserver
] = '\0';
790 if (mp
->mb_sock
.s_use_ssl
== 0 && imap_use_starttls(uhp
)) {
791 FILE *queuefp
= NULL
;
794 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
795 IMAP_OUT(o
, MB_COMD
, return STOP
)
797 if (ssl_open(xserver
, &mp
->mb_sock
, uhp
) != OKAY
)
801 if (imap_use_starttls(uhp
)) {
802 fprintf(stderr
, "No SSL support compiled in.\n");
811 imap_capability(struct mailbox
*mp
)
814 FILE *queuefp
= NULL
;
818 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
819 IMAP_OUT(o
, MB_COMD
, return STOP
)
820 while (mp
->mb_active
& MB_COMD
) {
821 ok
= imap_answer(mp
, 0);
822 if (response_status
== RESPONSE_OTHER
&&
823 response_other
== CAPABILITY_DATA
) {
824 cp
= responded_other_text
;
826 while (spacechar(*cp
&0377))
828 if (strncmp(cp
, "UIDPLUS", 7) == 0 &&
829 spacechar(cp
[7]&0377))
831 mp
->mb_flags
|= MB_UIDPLUS
;
832 while (*cp
&& !spacechar(*cp
&0377))
841 imap_auth(struct mailbox
*mp
, const char *uhp
, char *xuser
, char *pass
)
846 if (!(mp
->mb_active
& MB_PREAUTH
))
848 if ((auth
= value("imap-auth")) == NULL
) {
849 size_t i
= strlen(uhp
) + 1;
850 var
= ac_alloc(i
+ 10);
851 memcpy(var
, "imap-auth-", 10);
852 memcpy(var
+ 10, uhp
, i
);
856 if (auth
== NULL
|| strcmp(auth
, "login") == 0)
857 return imap_login(mp
, xuser
, pass
);
858 if (strcmp(auth
, "cram-md5") == 0) {
860 return imap_cram_md5(mp
, xuser
, pass
);
862 fprintf(stderr
, tr(277, "No CRAM-MD5 support compiled in.\n"));
866 if (strcmp(auth
, "gssapi") == 0) {
868 return imap_gss(mp
, xuser
);
870 fprintf(stderr
, tr(272, "No GSSAPI support compiled in.\n"));
874 fprintf(stderr
, tr(273, "Unknown IMAP authentication method: %s\n"),
880 * Implementation of RFC 2194.
884 imap_cram_md5(struct mailbox
*mp
, char *xuser
, char *xpass
)
886 char o
[LINESIZE
], *user
, *pass
, *cp
;
887 FILE *queuefp
= NULL
;
893 if (! getcredentials(&user
, &pass
))
896 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
897 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
899 if (response_type
!= RESPONSE_CONT
)
902 cp
= cram_md5_string(user
, pass
, responded_text
);
903 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
904 while (mp
->mb_active
& MB_COMD
)
905 ok
= imap_answer(mp
, 1);
913 #endif /* HAVE_MD5 */
916 imap_login(struct mailbox
*mp
, char *xuser
, char *xpass
)
920 FILE *queuefp
= NULL
;
926 if (! getcredentials(&user
, &pass
))
929 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
930 tag(1), imap_quotestr(user
), imap_quotestr(pass
));
931 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
932 while (mp
->mb_active
& MB_COMD
)
933 ok
= imap_answer(mp
, 1);
943 # include "imap_gssapi.h"
947 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
)
952 FILE *queuefp
= NULL
;
955 mp
->mb_uidvalidity
= 0;
956 snprintf(o
, sizeof o
, "%s SELECT %s\r\n", tag(1), imap_quotestr(mbx
));
957 IMAP_OUT(o
, MB_COMD
, return STOP
)
958 while (mp
->mb_active
& MB_COMD
) {
959 ok
= imap_answer(mp
, 1);
960 if (response_status
!= RESPONSE_OTHER
&&
961 (cp
= asccasestr(responded_text
,
962 "[UIDVALIDITY ")) != NULL
)
963 mp
->mb_uidvalidity
= atol(&cp
[13]);
965 *cnt
= had_exists
> 0 ? had_exists
: 0;
966 if (response_status
!= RESPONSE_OTHER
&&
967 ascncasecmp(responded_text
, "[READ-ONLY] ", 12)
974 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
977 FILE *queuefp
= NULL
;
980 unsigned x
= X
, y
= Y
, n
;
982 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
983 IMAP_OUT(o
, MB_COMD
, return STOP
)
984 while (mp
->mb_active
& MB_COMD
) {
986 if (response_status
== RESPONSE_OTHER
&&
987 response_other
== MESSAGE_DATA_FETCH
) {
988 n
= responded_other_number
;
995 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1000 imap_getflags(cp
, &cp
, &m
->m_flag
);
1002 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1003 m
->m_uid
= strtoul(&cp
[4], NULL
, 10);
1004 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1005 m
->m_flag
&= ~MHIDDEN
;
1007 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1009 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1012 snprintf(o
, sizeof o
,
1013 "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1015 IMAP_OUT(o
, MB_COMD
, return STOP
)
1016 while (mp
->mb_active
& MB_COMD
) {
1018 if (response_status
== RESPONSE_OTHER
&&
1019 response_other
== MESSAGE_DATA_FETCH
) {
1020 n
= responded_other_number
;
1026 if ((cp
= asccasestr(responded_other_text
,
1027 "RFC822.SIZE ")) != NULL
)
1028 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1029 if ((cp
= asccasestr(responded_other_text
,
1030 "INTERNALDATE ")) != NULL
)
1031 m
->m_time
= imap_read_date_time(&cp
[13]);
1034 for (n
= X
; n
<= Y
; n
++)
1035 putcache(mp
, &message
[n
-1]);
1040 imap_init(struct mailbox
*mp
, int n
)
1042 struct message
*m
= &message
[n
];
1045 m
->m_flag
= MUSED
|MNOFROM
;
1051 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1053 struct message
*omessage
= 0;
1054 int i
, omsgCount
= 0;
1055 enum okay dequeued
= STOP
;
1057 if (nmail
|| transparent
) {
1059 omsgCount
= msgCount
;
1062 dequeued
= rec_dequeue();
1063 if (had_exists
>= 0) {
1064 if (dequeued
!= OKAY
)
1065 msgCount
= had_exists
;
1068 if (had_expunge
>= 0) {
1069 if (dequeued
!= OKAY
)
1070 msgCount
-= had_expunge
;
1073 if (nmail
&& expunged_messages
)
1074 printf("Expunged %ld message%s.\n",
1076 expunged_messages
!= 1 ? "s" : "");
1077 *prevcount
= omsgCount
- expunged_messages
;
1078 expunged_messages
= 0;
1080 fputs("IMAP error: Negative message count\n", stderr
);
1083 if (dequeued
!= OKAY
) {
1084 message
= scalloc(msgCount
+ 1, sizeof *message
);
1085 for (i
= 0; i
< msgCount
; i
++)
1087 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1090 imap_flags(mp
, 1, msgCount
);
1091 message
[msgCount
].m_size
= 0;
1092 message
[msgCount
].m_lines
= 0;
1095 if (nmail
|| transparent
)
1096 transflags(omessage
, omsgCount
, transparent
);
1102 imap_split(char **server
, const char **sp
, int *use_ssl
, const char **cp
,
1103 char const **uhp
, char const **mbx
, char **pass
, char **user
)
1106 if (strncmp(*sp
, "imap://", 7) == 0) {
1110 } else if (strncmp(*sp
, "imaps://", 8) == 0) {
1115 if ((*cp
= strchr(*sp
, '/')) != NULL
&& (*cp
)[1] != '\0') {
1116 char *x
= savestr(*sp
);
1117 x
[*cp
- *sp
] = '\0';
1122 (*server
)[*cp
- *server
] = '\0';
1126 *pass
= lookup_password_for_token(*uhp
);
1127 if ((*cp
= last_at_before_slash(*uhp
)) != NULL
) {
1128 *user
= salloc(*cp
- *uhp
+ 1);
1129 memcpy(*user
, *uhp
, *cp
- *uhp
);
1130 (*user
)[*cp
- *uhp
] = '\0';
1132 *user
= urlxdec(*user
);
1140 imap_setfile(const char *xserver
, int nmail
, int isedit
)
1142 return imap_setfile1(xserver
, nmail
, isedit
, 0);
1146 imap_setfile1(const char *xserver
, int nmail
, int isedit
,
1147 int volatile transparent
)
1150 sighandler_type
volatile saveint
, savepipe
;
1151 char *server
, *user
, *pass
, *acc
;
1152 char const *cp
, *sp
, *mbx
, *uhp
;
1153 int use_ssl
= 0, prevcount
= 0;
1154 enum mbflags same_flags
;
1156 server
= savestr(xserver
);
1158 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1159 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1160 if (saveint
!= SIG_IGN
)
1161 safe_signal(SIGINT
, imapcatch
);
1162 if (savepipe
!= SIG_IGN
)
1163 safe_signal(SIGPIPE
, imapcatch
);
1167 same_flags
= mb
.mb_flags
;
1168 same_imap_account
= 0;
1169 sp
= protbase(server
);
1170 if (mb
.mb_imap_account
&& mb
.mb_type
== MB_IMAP
) {
1171 if (mb
.mb_sock
.s_fd
> 0 &&
1172 strcmp(mb
.mb_imap_account
, sp
) == 0 &&
1173 disconnected(mb
.mb_imap_account
) == 0)
1174 same_imap_account
= 1;
1177 imap_split(&server
, &sp
, &use_ssl
, &cp
, &uhp
, &mbx
, &pass
, &user
);
1179 if (!same_imap_account
) {
1180 if (!disconnected(acc
) &&
1181 sopen(sp
, &so
, use_ssl
, uhp
,
1182 use_ssl
? "imaps" : "imap",
1183 (options
& OPT_VERBOSE
) != 0) != OKAY
) {
1191 edit
= (isedit
!= 0);
1192 if (mb
.mb_imap_account
!= NULL
)
1193 free(mb
.mb_imap_account
);
1194 mb
.mb_imap_account
= acc
;
1195 if (!same_imap_account
) {
1196 if (mb
.mb_sock
.s_fd
>= 0)
1197 sclose(&mb
.mb_sock
);
1199 same_imap_account
= 0;
1209 if (mb
.mb_imap_mailbox
!= NULL
)
1210 free(mb
.mb_imap_mailbox
);
1211 mb
.mb_imap_mailbox
= sstrdup(mbx
);
1214 mb
.mb_type
= MB_VOID
;
1215 mb
.mb_active
= MB_NONE
;
1217 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1218 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1219 if (sigsetjmp(imapjmp
, 1)) {
1220 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1221 sclose(&mb
.mb_sock
);
1222 safe_signal(SIGINT
, saveint
);
1223 safe_signal(SIGPIPE
, savepipe
);
1225 mb
.mb_type
= MB_VOID
;
1226 mb
.mb_active
= MB_NONE
;
1229 if (saveint
!= SIG_IGN
)
1230 safe_signal(SIGINT
, imapcatch
);
1231 if (savepipe
!= SIG_IGN
)
1232 safe_signal(SIGPIPE
, imapcatch
);
1233 if (mb
.mb_sock
.s_fd
< 0) {
1234 if (disconnected(mb
.mb_imap_account
)) {
1235 if (cache_setptr(transparent
) == STOP
)
1237 "Mailbox \"%s\" is not cached.\n",
1241 if ((cp
= value("imap-keepalive")) != NULL
) {
1242 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1243 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1244 alarm(imapkeepalive
);
1248 mb
.mb_sock
.s_desc
= "IMAP";
1249 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1250 if (imap_preauth(&mb
, sp
, uhp
) != OKAY
||
1251 imap_auth(&mb
, uhp
, user
, pass
) != OKAY
) {
1252 sclose(&mb
.mb_sock
);
1254 safe_signal(SIGINT
, saveint
);
1255 safe_signal(SIGPIPE
, savepipe
);
1259 } else /* same account */
1260 mb
.mb_flags
|= same_flags
;
1261 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
1262 mb
.mb_type
= MB_IMAP
;
1264 if (imap_select(&mb
, &mailsize
, &msgCount
, mbx
) != OKAY
) {
1265 /*sclose(&mb.mb_sock);
1267 safe_signal(SIGINT
, saveint
);
1268 safe_signal(SIGPIPE
, savepipe
);
1270 mb
.mb_type
= MB_VOID
;
1274 imap_setptr(&mb
, nmail
, transparent
, &prevcount
);
1275 done
: setmsize(msgCount
);
1276 if (!nmail
&& !transparent
)
1278 safe_signal(SIGINT
, saveint
);
1279 safe_signal(SIGPIPE
, savepipe
);
1281 if (!nmail
&& mb
.mb_type
== MB_IMAP
)
1282 purgecache(&mb
, message
, msgCount
);
1283 if ((nmail
|| transparent
) && mb
.mb_sorted
) {
1287 if (!nmail
&& !edit
&& msgCount
== 0) {
1288 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1289 value("emptystart") == NULL
)
1290 fprintf(stderr
, tr(258, "No mail at %s\n"), server
);
1294 newmailinfo(prevcount
);
1299 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1301 const char *head
, size_t headsize
, long headlines
)
1303 char *line
= NULL
, *lp
;
1304 size_t linesize
= 0, linelen
, size
= 0;
1305 int emptyline
= 0, lines
= 0, excess
= 0;
1308 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1309 offset
= ftell(mp
->mb_otf
);
1311 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1312 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1314 if (linelen
> expected
) {
1315 excess
= linelen
- expected
;
1319 * Need to mask 'From ' lines. This cannot be done properly
1320 * since some servers pass them as 'From ' and others as
1321 * '>From '. Although one could identify the first kind of
1322 * server in principle, it is not possible to identify the
1323 * second as '>From ' may also come from a server of the
1324 * first type as actual data. So do what is absolutely
1325 * necessary only - mask 'From '.
1327 * If the line is the first line of the message header, it
1328 * is likely a real 'From ' line. In this case, it is just
1329 * ignored since it violates all standards.
1330 * TODO can the latter *really* happen??
1334 * Since we simply copy over data without doing any transfer
1335 * encoding reclassification/adjustment we *have* to perform
1336 * RFC 4155 compliant From_ quoting here
1338 if (is_head(lp
, linelen
)) {
1339 if (lines
+ headlines
== 0)
1341 fputc('>', mp
->mb_otf
);
1344 if (lp
[linelen
-1] == '\n' && (linelen
== 1 ||
1345 lp
[linelen
-2] == '\r')) {
1346 emptyline
= linelen
<= 2;
1348 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1349 size
+= linelen
- 1;
1352 fputc('\n', mp
->mb_otf
);
1355 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1359 skip
: if ((expected
-= linelen
) <= 0)
1364 * This is very ugly; but some IMAP daemons don't end a
1365 * message with \r\n\r\n, and we need \n\n for mbox format.
1367 fputc('\n', mp
->mb_otf
);
1373 m
->m_size
= size
+ headsize
;
1374 m
->m_lines
= lines
+ headlines
;
1375 m
->m_block
= mailx_blockof(offset
);
1376 m
->m_offset
= mailx_offsetof(offset
);
1379 m
->m_have
|= HAVE_HEADER
;
1382 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1383 m
->m_xlines
= m
->m_lines
;
1384 m
->m_xsize
= m
->m_size
;
1393 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1394 const char *head
, size_t headsize
, long headlines
)
1400 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1401 offset
= ftell(mp
->mb_otf
);
1403 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1405 fwrite(str
, 1, len
, mp
->mb_otf
);
1406 fputc('\n', mp
->mb_otf
);
1411 m
->m_size
= headsize
+ len
;
1412 m
->m_lines
= headlines
+ 1;
1413 m
->m_block
= mailx_blockof(offset
);
1414 m
->m_offset
= mailx_offsetof(offset
);
1415 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1416 m
->m_xlines
= m
->m_lines
;
1417 m
->m_xsize
= m
->m_size
;
1422 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
1426 sighandler_type saveint
= SIG_IGN
, savepipe
= SIG_IGN
;
1427 char *volatile head
= NULL
;
1428 char const *cp
= NULL
, *loc
= NULL
,
1429 *volatile item
= NULL
, *volatile resp
= NULL
;
1431 size_t volatile headsize
= 0;
1432 int number
= m
- message
+ 1;
1433 enum okay ok
= STOP
;
1434 FILE *queuefp
= NULL
;
1435 long volatile headlines
= 0;
1437 unsigned long u
= 0;
1439 if (getcache(mp
, m
, need
) == OKAY
)
1441 if (mp
->mb_type
== MB_CACHE
) {
1442 fprintf(stderr
, "Message %u not available.\n", number
);
1445 if (mp
->mb_sock
.s_fd
< 0) {
1446 fprintf(stderr
, "IMAP connection closed.\n");
1451 resp
= item
= "RFC822.HEADER";
1454 item
= "BODY.PEEK[]";
1456 if (m
->m_flag
& HAVE_HEADER
&& m
->m_size
) {
1457 char *hdr
= smalloc(m
->m_size
);
1459 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
,
1460 m
->m_offset
), SEEK_SET
) < 0 ||
1461 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
)
1467 headsize
= m
->m_size
;
1468 headlines
= m
->m_lines
;
1469 item
= "BODY.PEEK[TEXT]";
1470 resp
= "BODY[TEXT]";
1477 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1478 if (sigsetjmp(imapjmp
, 1)) {
1479 safe_signal(SIGINT
, saveint
);
1480 safe_signal(SIGPIPE
, savepipe
);
1484 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1485 safe_signal(SIGINT
, maincatch
);
1486 if (savepipe
!= SIG_IGN
)
1487 safe_signal(SIGPIPE
, imapcatch
);
1489 snprintf(o
, sizeof o
,
1490 "%s UID FETCH %lu (%s)\r\n",
1491 tag(1), m
->m_uid
, item
);
1493 if (check_expunged() == STOP
)
1495 snprintf(o
, sizeof o
,
1496 "%s FETCH %u (%s)\r\n",
1497 tag(1), number
, item
);
1499 IMAP_OUT(o
, MB_COMD
, goto out
)
1501 ok
= imap_answer(mp
, 1);
1504 if (response_status
!= RESPONSE_OTHER
||
1505 response_other
!= MESSAGE_DATA_FETCH
)
1507 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
1510 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
1518 n
= responded_other_number
;
1519 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
1520 if (m
->m_uid
? m
->m_uid
!= u
: n
!= number
)
1522 if ((cp
= strchr(loc
, '"')) != NULL
) {
1523 cp
= imap_unquotestr(cp
);
1524 imap_putstr(mp
, m
, cp
,
1525 head
, headsize
, headlines
);
1527 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1528 m
->m_xlines
= m
->m_lines
;
1529 m
->m_xsize
= m
->m_size
;
1533 expected
= atol(&cp
[1]);
1534 if (m
->m_uid
? n
== 0 && m
->m_uid
!= u
: n
!= number
) {
1535 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
1539 imap_fetchdata(mp
, &mt
, expected
, need
,
1540 head
, headsize
, headlines
);
1542 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1545 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
,
1546 &mp
->mb_sock
) > 0) {
1547 if (options
& OPT_VERBOSE
)
1548 fputs(imapbuf
, stderr
);
1549 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1551 if (u
== m
->m_uid
) {
1552 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1558 out
: while (mp
->mb_active
& MB_COMD
)
1559 ok
= imap_answer(mp
, 1);
1560 if (saveint
!= SIG_IGN
)
1561 safe_signal(SIGINT
, saveint
);
1562 if (savepipe
!= SIG_IGN
)
1563 safe_signal(SIGPIPE
, savepipe
);
1575 imap_header(struct message
*m
)
1577 return imap_get(&mb
, m
, NEED_HEADER
);
1582 imap_body(struct message
*m
)
1584 return imap_get(&mb
, m
, NEED_BODY
);
1588 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
1591 tomp
->m_size
= frommp
->m_size
;
1592 tomp
->m_lines
= frommp
->m_lines
;
1593 tomp
->m_block
= frommp
->m_block
;
1594 tomp
->m_offset
= frommp
->m_offset
;
1595 tomp
->m_have
= have
;
1596 if (have
& HAVE_BODY
) {
1597 tomp
->m_xlines
= frommp
->m_lines
;
1598 tomp
->m_xsize
= frommp
->m_size
;
1608 int topp
/* bot > topp */
1617 FILE *queuefp
= NULL
;
1620 snprintf(o
, sizeof o
,
1621 "%s UID FETCH %lu:%lu (RFC822.HEADER)\r\n",
1622 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
1624 if (check_expunged() == STOP
)
1626 snprintf(o
, sizeof o
,
1627 "%s FETCH %u:%u (RFC822.HEADER)\r\n",
1630 IMAP_OUT(o
, MB_COMD
, return STOP
)
1632 ok
= imap_answer(mp
, 1);
1633 if (response_status
!= RESPONSE_OTHER
)
1635 if (response_other
!= MESSAGE_DATA_FETCH
)
1637 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0)
1639 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
1641 expected
= atol(&cp
[1]);
1642 if (m
[bot
-1].m_uid
) {
1643 if ((cp
=asccasestr(responded_other_text
, "UID "))) {
1645 for (n
= bot
; n
<= topp
; n
++)
1646 if ((unsigned long)u
== m
[n
-1].m_uid
)
1649 imap_fetchdata(mp
, NULL
, expected
,
1657 n
= responded_other_number
;
1658 if (n
<= 0 || n
> msgCount
) {
1659 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
,
1664 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
1665 if (n
>= 0 && !(m
[n
-1].m_have
& HAVE_HEADER
))
1666 commitmsg(mp
, &m
[n
-1], &mt
, HAVE_HEADER
);
1667 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
,
1668 &mp
->mb_sock
) > 0) {
1669 if (options
& OPT_VERBOSE
)
1670 fputs(imapbuf
, stderr
);
1671 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1673 for (n
= bot
; n
<= topp
; n
++)
1674 if ((unsigned long)u
== m
[n
-1].m_uid
)
1676 if (n
<= topp
&& !(m
[n
-1].m_have
& HAVE_HEADER
))
1677 commitmsg(mp
, &m
[n
-1], &mt
,HAVE_HEADER
);
1682 while (mp
->mb_active
& MB_COMD
)
1683 ok
= imap_answer(mp
, 1);
1688 imap_getheaders(int volatile bot
, int topp
)
1690 sighandler_type saveint
, savepipe
;
1691 /* XXX enum okay ok = STOP;*/
1694 if (mb
.mb_type
== MB_CACHE
)
1698 if (topp
> msgCount
)
1700 for (i
= bot
; i
< topp
; i
++) {
1701 if (message
[i
-1].m_have
& HAVE_HEADER
||
1702 getcache(&mb
, &message
[i
-1], NEED_HEADER
)
1708 for (i
= topp
; i
> bot
; i
--) {
1709 if (message
[i
-1].m_have
& HAVE_HEADER
||
1710 getcache(&mb
, &message
[i
-1], NEED_HEADER
)
1719 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1720 safe_signal(SIGINT
, maincatch
);
1721 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1722 if (sigsetjmp(imapjmp
, 1) == 0) {
1723 if (savepipe
!= SIG_IGN
)
1724 safe_signal(SIGPIPE
, imapcatch
);
1725 for (i
= bot
; i
<= topp
; i
+= chunk
) {
1726 /*ok = */imap_fetchheaders(&mb
, message
, i
,
1727 i
+chunk
-1 < topp
? i
+chunk
-1 : topp
);
1732 safe_signal(SIGINT
, saveint
);
1733 safe_signal(SIGPIPE
, savepipe
);
1738 __imap_exit(struct mailbox
*mp
)
1741 FILE *queuefp
= NULL
;
1743 mp
->mb_active
|= MB_BYE
;
1744 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
1745 IMAP_OUT(o
, MB_COMD
, return STOP
)
1751 imap_exit(struct mailbox
*mp
)
1753 enum okay ret
= __imap_exit(mp
);
1754 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
1755 free(mp
->mb_imap_account
);
1756 free(mp
->mb_imap_mailbox
);
1757 if (mp
->mb_cache_directory
!= NULL
)
1758 free(mp
->mb_cache_directory
);
1759 #ifndef HAVE_ASSERTS /* TODO ASSERT LEGACY */
1760 mp
->mb_imap_account
=
1761 mp
->mb_imap_mailbox
=
1762 mp
->mb_cache_directory
= "";
1764 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
1765 mp
->mb_imap_mailbox
= NULL
;
1766 mp
->mb_cache_directory
= NULL
;
1769 sclose(&mp
->mb_sock
);
1774 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
1776 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
1777 if (mp
->mb_type
== MB_IMAP
)
1783 imap_close(struct mailbox
*mp
)
1786 FILE *queuefp
= NULL
;
1788 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
1789 IMAP_OUT(o
, MB_COMD
, return STOP
)
1795 imap_update(struct mailbox
*mp
)
1798 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
1800 if (!edit
&& mp
->mb_perm
!= 0) {
1802 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
1803 if (m
->m_flag
& MBOX
)
1807 if (makembox() == STOP
)
1810 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
1811 if (mp
->mb_perm
== 0) {
1814 dodel
= m
->m_flag
& MDELETED
;
1816 dodel
= !((m
->m_flag
&MPRESERVE
) ||
1817 (m
->m_flag
&MTOUCH
) == 0);
1820 * Fetch the result after around each 800 STORE commands
1821 * sent (approx. 32k data sent). Otherwise, servers will
1822 * try to flush the return queue at some point, leading
1823 * to a deadlock if we are still writing commands but not
1824 * reading their results.
1826 needstat
= stored
> 0 && stored
% 800 == 0;
1828 * Even if this message has been deleted, continue
1829 * to set further flags. This is necessary to support
1830 * Gmail semantics, where "delete" actually means
1831 * "archive", and the flags are applied to the copy
1834 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
)) {
1835 imap_store(mp
, m
, m
-message
+1,
1836 '+', "\\Seen", needstat
);
1839 if (m
->m_flag
& MFLAG
) {
1840 imap_store(mp
, m
, m
-message
+1,
1841 '+', "\\Flagged", needstat
);
1844 if (m
->m_flag
& MUNFLAG
) {
1845 imap_store(mp
, m
, m
-message
+1,
1846 '-', "\\Flagged", needstat
);
1849 if (m
->m_flag
& MANSWER
) {
1850 imap_store(mp
, m
, m
-message
+1,
1851 '+', "\\Answered", needstat
);
1854 if (m
->m_flag
& MUNANSWER
) {
1855 imap_store(mp
, m
, m
-message
+1,
1856 '-', "\\Answered", needstat
);
1859 if (m
->m_flag
& MDRAFT
) {
1860 imap_store(mp
, m
, m
-message
+1,
1861 '+', "\\Draft", needstat
);
1864 if (m
->m_flag
& MUNDRAFT
) {
1865 imap_store(mp
, m
, m
-message
+1,
1866 '-', "\\Draft", needstat
);
1870 imap_delete(mp
, m
-message
+1, m
, needstat
);
1873 } else if (mp
->mb_type
!= MB_CACHE
||
1874 (! edit
&& ! (m
->m_flag
&(MBOXED
|MSAVED
|MDELETED
))) ||
1875 (m
->m_flag
& (MBOXED
|MPRESERVE
|MTOUCH
)) ==
1876 (MPRESERVE
|MTOUCH
) ||
1877 (edit
&& ! (m
->m_flag
& MDELETED
)))
1879 if (m
->m_flag
& MNEW
) {
1881 m
->m_flag
|= MSTATUS
;
1887 for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
1888 if (!(m
->m_flag
&MUNLINKED
) &&
1889 m
->m_flag
&(MBOXED
|MDELETED
|MSAVED
|MSTATUS
|
1890 MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
1895 if ((gotcha
|| modflags
) && edit
) {
1896 printf(tr(168, "\"%s\" "), displayname
);
1897 printf((value("bsdcompat") || value("bsdmsgs"))
1898 ? tr(170, "complete\n") : tr(212, "updated.\n"));
1899 } else if (held
&& !edit
&& mp
->mb_perm
!= 0) {
1901 printf(tr(155, "Held 1 message in %s\n"), displayname
);
1903 printf(tr(156, "Held %d messages in %s\n"), held
,
1913 sighandler_type saveint
;
1914 sighandler_type savepipe
;
1916 if (mb
.mb_type
== MB_CACHE
) {
1920 if (mb
.mb_sock
.s_fd
< 0) {
1921 fprintf(stderr
, "IMAP connection closed.\n");
1925 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1926 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1927 if (sigsetjmp(imapjmp
, 1)) {
1928 safe_signal(SIGINT
, saveint
);
1929 safe_signal(SIGPIPE
, saveint
);
1933 if (saveint
!= SIG_IGN
)
1934 safe_signal(SIGINT
, imapcatch
);
1935 if (savepipe
!= SIG_IGN
)
1936 safe_signal(SIGPIPE
, imapcatch
);
1938 if (!same_imap_account
) {
1941 safe_signal(SIGINT
, saveint
);
1942 safe_signal(SIGPIPE
, savepipe
);
1947 imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
1948 int c
, const char *sp
, int needstat
)
1951 FILE *queuefp
= NULL
;
1953 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
1956 snprintf(o
, sizeof o
,
1957 "%s UID STORE %lu %cFLAGS (%s)\r\n",
1958 tag(1), m
->m_uid
, c
, sp
);
1960 if (check_expunged() == STOP
)
1962 snprintf(o
, sizeof o
,
1963 "%s STORE %u %cFLAGS (%s)\r\n",
1966 IMAP_OUT(o
, MB_COMD
, return STOP
)
1970 mb
.mb_active
&= ~MB_COMD
;
1971 if (queuefp
!= NULL
)
1977 imap_undelete(struct message
*m
, int n
)
1979 return imap_unstore(m
, n
, "\\Deleted");
1983 imap_unread(struct message
*m
, int n
)
1985 return imap_unstore(m
, n
, "\\Seen");
1989 imap_unstore(struct message
*m
, int n
, const char *flag
)
1991 sighandler_type saveint
, savepipe
;
1992 enum okay ok
= STOP
;
1998 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1999 safe_signal(SIGINT
, maincatch
);
2000 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2001 if (sigsetjmp(imapjmp
, 1) == 0) {
2002 if (savepipe
!= SIG_IGN
)
2003 safe_signal(SIGPIPE
, imapcatch
);
2004 ok
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2006 safe_signal(SIGINT
, saveint
);
2007 safe_signal(SIGPIPE
, savepipe
);
2022 snprintf(ts
, sizeof ts
, "T%lu", n
);
2029 sighandler_type saveint
, savepipe
;
2031 enum okay ok
= STOP
;
2032 struct mailbox
*mp
= &mb
;
2033 FILE *queuefp
= NULL
;
2038 if (mp
->mb_type
!= MB_IMAP
) {
2039 printf("Not operating on an IMAP mailbox.\n");
2043 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2044 safe_signal(SIGINT
, maincatch
);
2045 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2046 if (sigsetjmp(imapjmp
, 1) == 0) {
2047 if (savepipe
!= SIG_IGN
)
2048 safe_signal(SIGPIPE
, imapcatch
);
2049 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2050 IMAP_OUT(o
, MB_COMD
, goto out
)
2051 while (mp
->mb_active
& MB_COMD
) {
2052 ok
= imap_answer(mp
, 0);
2053 fputs(responded_text
, stdout
);
2056 out
: safe_signal(SIGINT
, saveint
);
2057 safe_signal(SIGPIPE
, savepipe
);
2065 imap_newmail(int autoinc
)
2067 if (autoinc
&& had_exists
< 0 && had_expunge
< 0) {
2072 if (had_exists
== msgCount
&& had_expunge
< 0)
2074 * Some servers always respond with EXISTS to NOOP. If
2075 * the mailbox has been changed but the number of messages
2076 * has not, an EXPUNGE must also had been sent; otherwise,
2077 * nothing has changed.
2080 return had_expunge
>= 0 ? 2 : had_exists
>= 0 ? 1 : 0;
2084 imap_putflags(int f
)
2089 bp
= buf
= salloc(100);
2090 if (f
& (MREAD
|MFLAGGED
|MANSWERED
|MDRAFTED
)) {
2095 for (cp
= "\\Seen"; *cp
; cp
++)
2101 for (cp
= "\\Flagged"; *cp
; cp
++)
2104 if (f
& MANSWERED
) {
2107 for (cp
= "\\Answered"; *cp
; cp
++)
2113 for (cp
= "\\Draft"; *cp
; cp
++)
2124 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2126 while (*cp
!= ')') {
2128 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2130 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2132 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2134 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2136 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2138 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2148 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
2149 off_t off1
, long xsize
, enum mflag flag
, time_t t
)
2153 size_t bufsize
, buflen
, cnt
;
2154 enum okay ok
= STOP
;
2155 long size
, lines
, ysize
;
2157 FILE *queuefp
= NULL
;
2159 if (mp
->mb_type
== MB_CACHE
) {
2160 queuefp
= cache_queue(mp
);
2161 if (queuefp
== NULL
)
2165 buf
= smalloc(bufsize
= LINESIZE
);
2167 again
: size
= xsize
;
2169 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2173 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2174 tag(1), imap_quotestr(name
),
2175 imap_putflags(flag
),
2176 imap_make_date_time(t
),
2178 IMAP_XOUT(o
, MB_COMD
, goto out
, ok
= STOP
;goto out
)
2179 while (mp
->mb_active
& MB_COMD
) {
2180 ok
= imap_answer(mp
, twice
);
2181 if (response_type
== RESPONSE_CONT
)
2184 if (mp
->mb_type
!= MB_CACHE
&& ok
== STOP
) {
2192 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2195 buf
[buflen
-1] = '\r';
2197 if (mp
->mb_type
!= MB_CACHE
)
2198 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2200 fwrite(buf
, 1, buflen
+1, queuefp
);
2203 if (mp
->mb_type
!= MB_CACHE
)
2204 swrite(&mp
->mb_sock
, "\r\n");
2206 fputs("\r\n", queuefp
);
2207 while (mp
->mb_active
& MB_COMD
) {
2208 ok
= imap_answer(mp
, 0);
2209 if (response_status
== RESPONSE_NO
/*&&
2210 ascncasecmp(responded_text,
2211 "[TRYCREATE] ", 12) == 0*/) {
2212 trycreate
: if (twice
++) {
2216 snprintf(o
, sizeof o
, "%s CREATE %s\r\n",
2218 imap_quotestr(name
));
2219 IMAP_XOUT(o
, MB_COMD
, goto out
, ok
= STOP
;goto out
)
2220 while (mp
->mb_active
& MB_COMD
)
2221 ok
= imap_answer(mp
, 1);
2224 imap_created_mailbox
++;
2226 } else if (ok
!= OKAY
)
2227 fprintf(stderr
, tr(270, "IMAP error: %s"),
2229 else if (response_status
== RESPONSE_OK
&&
2230 mp
->mb_flags
& MB_UIDPLUS
)
2231 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
,
2234 out
: if (queuefp
!= NULL
)
2241 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2243 char *buf
, *bp
, *lp
;
2244 size_t bufsize
, buflen
, cnt
;
2245 off_t off1
= -1, offs
;
2247 int flag
= MNEW
|MNEWEST
;
2252 buf
= smalloc(bufsize
= LINESIZE
);
2258 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2259 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
2260 if (off1
!= (off_t
)-1) {
2261 ok
=imap_append1(mp
, name
, fp
, off1
,
2265 fseek(fp
, offs
+buflen
, SEEK_SET
);
2267 off1
= offs
+ buflen
;
2272 tim
= unixtime(buf
);
2276 if (bp
&& buf
[0] == '\n')
2278 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
2280 while (whitechar(*lp
&0377))
2283 while (*++lp
!= '\0')
2292 } else if (bp
&& inhead
&&
2293 ascncasecmp(buf
, "x-status", 8) == 0) {
2295 while (whitechar(*lp
&0377))
2298 while (*++lp
!= '\0')
2311 } while (bp
!= NULL
);
2317 imap_append(const char *xserver
, FILE *fp
)
2319 sighandler_type saveint
, savepipe
;
2320 char *server
, *user
, *pass
;
2321 char const *sp
, *cp
, *mbx
, *uhp
;
2323 enum okay ok
= STOP
;
2325 server
= savestr(xserver
);
2326 imap_split(&server
, &sp
, &use_ssl
, &cp
, &uhp
, &mbx
, &pass
, &user
);
2328 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2329 safe_signal(SIGINT
, maincatch
);
2330 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2331 if (sigsetjmp(imapjmp
, 1))
2333 if (savepipe
!= SIG_IGN
)
2334 safe_signal(SIGPIPE
, imapcatch
);
2335 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) &&
2336 mb
.mb_imap_account
&&
2337 strcmp(protbase(server
), mb
.mb_imap_account
) == 0) {
2338 ok
= imap_append0(&mb
, mbx
, fp
);
2343 memset(&mx
, 0, sizeof mx
);
2344 if (disconnected(server
) == 0) {
2345 if (sopen(sp
, &mx
.mb_sock
, use_ssl
, uhp
,
2346 use_ssl
? "imaps" : "imap",
2347 (options
& OPT_VERBOSE
) != 0) != OKAY
)
2349 mx
.mb_sock
.s_desc
= "IMAP";
2350 mx
.mb_type
= MB_IMAP
;
2351 mx
.mb_imap_account
= (char *)protbase(server
);
2352 /* TODO the code now did
2353 * TODO mx.mb_imap_mailbox = mbx;
2354 * TODO though imap_mailbox is sfree()d and mbx
2355 * TODO is possibly even a constant
2356 * TODO i changed this to sstrdup() sofar, as is used
2357 * TODO somewhere else in this file for this! */
2358 mx
.mb_imap_mailbox
= sstrdup(mbx
);
2359 if (imap_preauth(&mx
, sp
, uhp
) != OKAY
||
2360 imap_auth(&mx
, uhp
, user
, pass
)!=OKAY
) {
2361 sclose(&mx
.mb_sock
);
2364 ok
= imap_append0(&mx
, mbx
, fp
);
2367 mx
.mb_imap_account
= (char *)protbase(server
);
2368 mx
.mb_imap_mailbox
= sstrdup(mbx
); /* TODO as above */
2369 mx
.mb_type
= MB_CACHE
;
2370 ok
= imap_append0(&mx
, mbx
, fp
);
2374 out
: safe_signal(SIGINT
, saveint
);
2375 safe_signal(SIGPIPE
, savepipe
);
2383 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
2384 struct list_item
**lend
, int level
)
2387 enum okay ok
= STOP
;
2390 FILE *queuefp
= NULL
;
2391 struct list_item
*lp
;
2393 *list
= *lend
= NULL
;
2394 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n",
2395 tag(1), imap_quotestr(base
));
2396 IMAP_OUT(o
, MB_COMD
, return STOP
)
2397 while (mp
->mb_active
& MB_COMD
) {
2398 ok
= imap_answer(mp
, 1);
2399 if (response_status
== RESPONSE_OTHER
&&
2400 response_other
== MAILBOX_DATA_LIST
&&
2401 imap_parse_list() == OKAY
) {
2402 cp
= imap_unquotestr(list_name
);
2403 lp
= csalloc(1, sizeof *lp
);
2405 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2407 lp
->l_base
= *cp
? cp
: savestr(base
);
2408 lp
->l_attr
= list_attributes
;
2409 lp
->l_level
= level
+1;
2410 lp
->l_delim
= list_hierarchy_delimiter
;
2411 if (*list
&& *lend
) {
2412 (*lend
)->l_next
= lp
;
2422 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
2424 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
2430 depth
= (cp
= value("imap-list-depth")) != NULL
? atoi(cp
) : 2;
2431 if (imap_list1(mp
, base
, &list
, &lend
, 0) == STOP
)
2433 if (list
== NULL
|| lend
== NULL
)
2435 for (lp
= list
; lp
; lp
= lp
->l_next
)
2436 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&&
2437 lp
->l_level
< depth
&&
2438 (lp
->l_attr
&LIST_NOINFERIORS
) == 0) {
2439 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
2440 memcpy(cp
, lp
->l_name
, n
);
2441 cp
[n
] = lp
->l_delim
;
2443 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&&
2445 lp
->l_has_children
= 1;
2446 if (strcmp(cp
, lx
->l_name
) == 0)
2454 for (lp
= list
; lp
; lp
= lp
->l_next
) {
2457 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2461 if ((lp
->l_attr
&LIST_NOSELECT
) == 0)
2462 fprintf(fp
, "%s\n", *cp
? cp
: base
);
2463 else if (lp
->l_has_children
== 0)
2464 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
2465 lp
->l_delim
!= EOF
? lp
->l_delim
: '\n');
2471 imap_folders(const char *name
, int strip
)
2473 sighandler_type saveint
, savepipe
;
2474 const char *fold
, *cp
, *sp
;
2478 cp
= protbase(name
);
2479 sp
= mb
.mb_imap_account
;
2480 if (sp
== NULL
|| strcmp(cp
, sp
)) {
2481 fprintf(stderr
, tr(502,
2482 "Cannot perform `folders' but when on the very IMAP "
2483 "account; the current one is\n `%s' -- "
2484 "try `folders @'.\n"),
2485 (sp
!= NULL
) ? sp
: tr(503, "[NONE]"));
2488 fold
= imap_fileof(name
);
2489 if (options
& OPT_TTYOUT
) {
2490 if ((fp
= Ftemp(&tempfn
, "Ri", "w+", 0600, 1)) == NULL
) {
2499 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2500 safe_signal(SIGINT
, maincatch
);
2501 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2502 if (sigsetjmp(imapjmp
, 1))
2504 if (savepipe
!= SIG_IGN
)
2505 safe_signal(SIGPIPE
, imapcatch
);
2506 if (mb
.mb_type
== MB_CACHE
)
2507 cache_list(&mb
, fold
, strip
, fp
);
2509 imap_list(&mb
, fold
, strip
, fp
);
2512 if (options
& OPT_TTYOUT
)
2517 if (options
& OPT_TTYOUT
) {
2522 fprintf(stderr
, "Folder not found.\n");
2525 safe_signal(SIGINT
, saveint
);
2526 safe_signal(SIGPIPE
, savepipe
);
2527 if (options
& OPT_TTYOUT
)
2534 char o
[LINESIZE
], *tempfn
;
2536 long n
= 0, mx
= 0, columns
, width
;
2539 if ((out
= Ftemp(&tempfn
, "Ro", "w+", 0600, 1)) == NULL
) {
2545 while ((c
= getc(fp
)) != EOF
) {
2555 if (mx
< width
/ 2) {
2556 columns
= width
/ (mx
+2);
2557 snprintf(o
, sizeof o
,
2558 "sort | pr -%lu -w%lu -t",
2561 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
2562 run_command(SHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
2568 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
2572 enum okay ok
= STOP
;
2575 FILE *queuefp
= NULL
;
2577 if (mp
->mb_type
== MB_CACHE
) {
2578 if ((queuefp
= cache_queue(mp
)) == NULL
)
2582 qname
= imap_quotestr(name
= imap_fileof(name
));
2584 * Since it is not possible to set flags on the copy, recently
2585 * set flags must be set on the original to include it in the copy.
2587 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
))
2588 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
2589 if (m
->m_flag
&MFLAG
)
2590 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2591 if (m
->m_flag
&MUNFLAG
)
2592 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2593 if (m
->m_flag
&MANSWER
)
2594 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2595 if (m
->m_flag
&MUNANSWER
)
2596 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2597 if (m
->m_flag
&MDRAFT
)
2598 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2599 if (m
->m_flag
&MUNDRAFT
)
2600 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2601 again
: if (m
->m_uid
)
2602 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n",
2603 tag(1), m
->m_uid
, qname
);
2605 if (check_expunged() == STOP
)
2607 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n",
2610 IMAP_OUT(o
, MB_COMD
, goto out
)
2611 while (mp
->mb_active
& MB_COMD
)
2612 ok
= imap_answer(mp
, twice
);
2613 if (mp
->mb_type
== MB_IMAP
&&
2614 mp
->mb_flags
& MB_UIDPLUS
&&
2615 response_status
== RESPONSE_OK
)
2616 imap_copyuid(mp
, m
, name
);
2617 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
2618 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2619 IMAP_OUT(o
, MB_COMD
, goto out
)
2620 while (mp
->mb_active
& MB_COMD
)
2621 ok
= imap_answer(mp
, 1);
2623 imap_created_mailbox
++;
2627 if (queuefp
!= NULL
)
2630 * ... and reset the flag to its initial value so that
2631 * the 'exit' command still leaves the message unread.
2633 out
: if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
)) {
2634 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
2637 if (m
->m_flag
&MFLAG
) {
2638 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2641 if (m
->m_flag
&MUNFLAG
) {
2642 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2645 if (m
->m_flag
&MANSWER
) {
2646 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
2649 if (m
->m_flag
&MUNANSWER
) {
2650 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2653 if (m
->m_flag
&MDRAFT
) {
2654 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2657 if (m
->m_flag
&MUNDRAFT
) {
2658 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2662 mp
->mb_active
|= MB_COMD
;
2663 (void)imap_finish(mp
);
2669 imap_copy(struct message
*m
, int n
, const char *name
)
2671 sighandler_type saveint
, savepipe
;
2672 enum okay ok
= STOP
;
2678 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2679 safe_signal(SIGINT
, maincatch
);
2680 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2681 if (sigsetjmp(imapjmp
, 1) == 0) {
2682 if (savepipe
!= SIG_IGN
)
2683 safe_signal(SIGPIPE
, imapcatch
);
2684 ok
= imap_copy1(&mb
, m
, n
, name
);
2686 safe_signal(SIGINT
, saveint
);
2687 safe_signal(SIGPIPE
, savepipe
);
2695 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
2696 unsigned long *olduid
, unsigned long *newuid
)
2700 *uidvalidity
= strtoul(cp
, &xp
, 10);
2701 *olduid
= strtoul(xp
, &yp
, 10);
2702 *newuid
= strtoul(yp
, &zp
, 10);
2703 return *uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
2704 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']';
2708 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
2713 *uidvalidity
= strtoul(cp
, &xp
, 10);
2714 *uid
= strtoul(xp
, &yp
, 10);
2715 return *uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' &&
2716 yp
> xp
&& *yp
== ']';
2720 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
2723 unsigned long uidvalidity
, olduid
, newuid
;
2727 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
2728 imap_copyuid_parse(&cp
[9], &uidvalidity
,
2729 &olduid
, &newuid
) == STOP
)
2732 xmb
.mb_cache_directory
= NULL
;
2733 xmb
.mb_imap_mailbox
= savestr(name
);
2734 xmb
.mb_uidvalidity
= uidvalidity
;
2737 memset(&xm
, 0, sizeof xm
);
2739 if (getcache1(mp
, &xm
, NEED_UNSPEC
, 3) != OKAY
)
2741 getcache(mp
, &xm
, NEED_HEADER
);
2742 getcache(mp
, &xm
, NEED_BODY
);
2744 if ((m
->m_flag
& HAVE_HEADER
) == 0)
2745 getcache(mp
, m
, NEED_HEADER
);
2746 if ((m
->m_flag
& HAVE_BODY
) == 0)
2747 getcache(mp
, m
, NEED_BODY
);
2751 xm
.m_flag
&= ~MFULLYCACHED
;
2752 putcache(&xmb
, &xm
);
2757 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
,
2758 long xsize
, long size
, long lines
, int flag
, const char *name
)
2761 unsigned long uidvalidity
, uid
;
2765 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
2766 imap_appenduid_parse(&cp
[11], &uidvalidity
,
2770 xmb
.mb_cache_directory
= NULL
;
2771 xmb
.mb_imap_mailbox
= savestr(name
);
2772 xmb
.mb_uidvalidity
= uidvalidity
;
2773 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
2775 memset(&xm
, 0, sizeof xm
);
2776 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
2778 xm
.m_block
= mailx_blockof(off1
);
2779 xm
.m_offset
= mailx_offsetof(off1
);
2782 xm
.m_lines
= xm
.m_xlines
= lines
;
2784 xm
.m_have
= HAVE_HEADER
|HAVE_BODY
;
2785 putcache(&xmb
, &xm
);
2790 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
2794 long size
, xsize
, ysize
, lines
;
2795 enum mflag flag
= MNEW
;
2796 char *name
, *buf
, *bp
, *tempCopy
;
2798 size_t bufsize
, buflen
, cnt
;
2799 enum okay ok
= STOP
;
2801 buf
= smalloc(bufsize
= LINESIZE
);
2804 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2806 for (bp
= buf
; *bp
!= ' '; bp
++); /* strip old tag */
2809 if ((cp
= strrchr(bp
, '{')) == NULL
)
2811 xsize
= atol(&cp
[1]) + 2;
2812 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
2817 imap_getflags(cp
, &cp
, &flag
);
2818 while (*++cp
== ' ');
2820 t
= imap_read_date_time(cp
);
2821 if ((tp
= Ftemp(&tempCopy
, "Rc", "w+", 0600, 1)) == NULL
)
2828 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2831 buf
[--buflen
] = '\0';
2832 buf
[buflen
-1] = '\n';
2833 fwrite(buf
, 1, buflen
, tp
);
2839 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
2840 imap_unquotestr(name
));
2849 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
2850 const char *spec
, int f
)
2854 FILE *queuefp
= NULL
;
2855 enum okay ok
= STOP
;
2862 for (cp
= spec
; *cp
; cp
++)
2865 cp
= charset_get_lc();
2867 if (asccasecmp(cp
, "utf-8")) {
2871 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
2872 sz
= strlen(spec
) + 1;
2873 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
2874 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
,
2875 FAL0
) == 0 && sz
== 0) {
2883 cp
= imap_quotestr(cp
);
2884 cs
= salloc(n
= strlen(cp
) + 10);
2885 snprintf(cs
, n
, "CHARSET %s ", cp
);
2888 o
= ac_alloc(osize
= strlen(spec
) + 60);
2889 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
2890 IMAP_OUT(o
, MB_COMD
, goto out
)
2891 while (mp
->mb_active
& MB_COMD
) {
2892 ok
= imap_answer(mp
, 0);
2893 if (response_status
== RESPONSE_OTHER
&&
2894 response_other
== MAILBOX_DATA_SEARCH
) {
2895 xp
= responded_other_text
;
2896 while (*xp
&& *xp
!= '\r') {
2897 n
= strtoul(xp
, &xp
, 10);
2898 for (i
= 0; i
< cnt
; i
++)
2899 if (m
[i
].m_uid
== n
&&
2900 (m
[i
].m_flag
&MHIDDEN
)
2903 (m
[i
].m_flag
&MDELETED
)
2914 imap_search1(const char *volatile spec
, int f
)
2916 sighandler_type saveint
, savepipe
;
2917 enum okay ok
= STOP
;
2922 if (mb
.mb_type
!= MB_IMAP
)
2925 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2926 safe_signal(SIGINT
, maincatch
);
2927 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2928 if (sigsetjmp(imapjmp
, 1) == 0) {
2929 if (savepipe
!= SIG_IGN
)
2930 safe_signal(SIGPIPE
, imapcatch
);
2931 ok
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
2933 safe_signal(SIGINT
, saveint
);
2934 safe_signal(SIGPIPE
, savepipe
);
2942 imap_thisaccount(const char *cp
)
2944 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
2946 if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
2947 mb
.mb_imap_account
== NULL
)
2949 return strcmp(protbase(cp
), mb
.mb_imap_account
) == 0;
2953 imap_remove(const char *name
)
2955 sighandler_type saveint
, savepipe
;
2956 enum okay ok
= STOP
;
2961 if (mb
.mb_type
!= MB_IMAP
) {
2962 fprintf(stderr
, "Refusing to remove \"%s\" "
2963 "in disconnected mode.\n", name
);
2966 if (!imap_thisaccount(name
)) {
2967 fprintf(stderr
, "Can only remove mailboxes on current IMAP "
2968 "server: \"%s\" not removed.\n", name
);
2972 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2973 safe_signal(SIGINT
, maincatch
);
2974 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2975 if (sigsetjmp(imapjmp
, 1) == 0) {
2976 if (savepipe
!= SIG_IGN
)
2977 safe_signal(SIGPIPE
, imapcatch
);
2978 ok
= imap_remove1(&mb
, imap_fileof(name
));
2980 safe_signal(SIGINT
, saveint
);
2981 safe_signal(SIGPIPE
, savepipe
);
2984 ok
= cache_remove(name
);
2991 imap_remove1(struct mailbox
*mp
, const char *name
)
2993 FILE *queuefp
= NULL
;
2996 enum okay ok
= STOP
;
2998 o
= ac_alloc(os
= 2*strlen(name
) + 100);
2999 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), imap_quotestr(name
));
3000 IMAP_OUT(o
, MB_COMD
, goto out
)
3001 while (mp
->mb_active
& MB_COMD
)
3002 ok
= imap_answer(mp
, 1);
3008 imap_rename(const char *old
, const char *new)
3010 sighandler_type saveint
, savepipe
;
3011 enum okay ok
= STOP
;
3016 if (mb
.mb_type
!= MB_IMAP
) {
3017 fprintf(stderr
, "Refusing to rename mailboxes "
3018 "in disconnected mode.\n");
3021 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3022 fprintf(stderr
, "Can only rename mailboxes on current IMAP "
3023 "server: \"%s\" not renamed to \"%s\".\n",
3028 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3029 safe_signal(SIGINT
, maincatch
);
3030 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3031 if (sigsetjmp(imapjmp
, 1) == 0) {
3032 if (savepipe
!= SIG_IGN
)
3033 safe_signal(SIGPIPE
, imapcatch
);
3034 ok
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3036 safe_signal(SIGINT
, saveint
);
3037 safe_signal(SIGPIPE
, savepipe
);
3040 ok
= cache_rename(old
, new);
3047 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3049 FILE *queuefp
= NULL
;
3052 enum okay ok
= STOP
;
3054 o
= ac_alloc(os
= 2*strlen(old
) + 2*strlen(new) + 100);
3055 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1),
3056 imap_quotestr(old
), imap_quotestr(new));
3057 IMAP_OUT(o
, MB_COMD
, goto out
)
3058 while (mp
->mb_active
& MB_COMD
)
3059 ok
= imap_answer(mp
, 1);
3065 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3067 FILE *queuefp
= NULL
;
3068 char o
[LINESIZE
], *newname
;
3069 char *buf
, *bp
, *cp
, iob
[4096];
3070 size_t bufsize
, buflen
, cnt
;
3071 enum okay ok
= OKAY
, rok
= OKAY
;
3072 long offs
, offs1
, offs2
, octets
;
3073 int twice
, gotcha
= 0;
3075 buf
= smalloc(bufsize
= LINESIZE
);
3078 while ((offs1
= ftell(fp
)) >= 0 &&
3079 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0)
3081 for (bp
= buf
; *bp
!= ' '; bp
++); /* strip old tag */
3085 if ((offs
= ftell(fp
)) < 0)
3087 again
: snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3088 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3090 while (digitchar(*cp
&0377))
3096 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3098 IMAP_OUT(o
, MB_COMD
, continue)
3099 while (mp
->mb_active
& MB_COMD
)
3100 ok
= imap_answer(mp
, twice
);
3101 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3103 if (response_status
== RESPONSE_OK
&&
3104 mp
->mb_flags
& MB_UIDPLUS
) {
3105 imap_copyuid(mp
, NULL
,
3106 imap_unquotestr(newname
));
3108 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3109 IMAP_OUT(o
, MB_COMD
, continue)
3110 while (mp
->mb_active
& MB_COMD
)
3111 ok
= imap_answer(mp
, 1);
3114 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3115 if ((cp
= strrchr(bp
, '{')) == NULL
)
3117 octets
= atol(&cp
[1]) + 2;
3118 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3120 IMAP_OUT(o
, MB_COMD
, continue)
3121 while (mp
->mb_active
& MB_COMD
) {
3122 ok
= imap_answer(mp
, twice
);
3123 if (response_type
== RESPONSE_CONT
)
3128 fseek(fp
, offs
, SEEK_SET
) >= 0)
3132 while (octets
> 0) {
3133 size_t n
= (size_t)octets
> sizeof iob
3134 ? sizeof iob
: (size_t)octets
;
3136 if (n
!= fread(iob
, 1, n
, fp
))
3138 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3140 swrite(&mp
->mb_sock
, "");
3141 while (mp
->mb_active
& MB_COMD
) {
3142 ok
= imap_answer(mp
, 0);
3143 if (response_status
== RESPONSE_NO
&&
3145 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3150 if (response_status
== RESPONSE_OK
&&
3151 mp
->mb_flags
& MB_UIDPLUS
) {
3152 if ((offs2
= ftell(fp
)) < 0)
3154 fseek(fp
, offs1
, SEEK_SET
);
3155 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3156 (void)fseek(fp
, offs2
, SEEK_SET
);
3161 fail
: fprintf(stderr
,
3162 "Invalid command in IMAP cache queue: \"%s\"\n",
3168 snprintf(o
, sizeof o
, "%s CREATE %s\r\n",
3170 IMAP_OUT(o
, MB_COMD
, continue)
3171 while (mp
->mb_active
& MB_COMD
)
3172 ok
= imap_answer(mp
, 1);
3178 ftruncate(fileno(fp
), 0);
3186 imap_strex(char const *cp
, char const **xp
)
3193 for (cq
= &cp
[1]; *cq
; cq
++) {
3196 else if (*cq
== '"')
3201 n
= salloc(cq
- cp
+ 2);
3202 memcpy(n
, cp
, cq
- cp
+ 1);
3203 n
[cq
- cp
+ 1] = '\0';
3210 check_expunged(void)
3212 if (expunged_messages
> 0) {
3214 "Command not executed - messages have been expunged\n");
3225 int omsgCount
= msgCount
;
3228 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
3229 fprintf(stderr
, "Already connected.\n");
3232 unset_allow_undefined
= TRU1
;
3233 unset_internal("disconnected");
3234 cp
= protbase(mailname
);
3235 if (strncmp(cp
, "imap://", 7) == 0)
3237 else if (strncmp(cp
, "imaps://", 8) == 0)
3239 if ((cq
= strchr(cp
, ':')) != NULL
)
3241 unset_internal(savecat("disconnected-", cp
));
3242 unset_allow_undefined
= FAL0
;
3243 if (mb
.mb_type
== MB_CACHE
) {
3244 imap_setfile1(mailname
, 0, edit
, 1);
3245 if (msgCount
> omsgCount
)
3246 newmailinfo(omsgCount
);
3252 cdisconnect(void *vp
)
3256 if (mb
.mb_type
== MB_CACHE
) {
3257 fprintf(stderr
, "Not connected.\n");
3259 } else if (mb
.mb_type
== MB_IMAP
) {
3260 if (cached_uidvalidity(&mb
) == 0) {
3261 fprintf(stderr
, "The current mailbox is not cached.\n");
3267 assign("disconnected", "");
3268 if (mb
.mb_type
== MB_IMAP
) {
3269 sclose(&mb
.mb_sock
);
3270 imap_setfile1(mailname
, 0, edit
, 1);
3278 int *msgvec
= vp
, *ip
;
3281 if (mb
.mb_type
!= MB_IMAP
) {
3282 fprintf(stderr
, "Not connected to an IMAP server.\n");
3285 if (cached_uidvalidity(&mb
) == 0) {
3286 fprintf(stderr
, "The current mailbox is not cached.\n");
3289 for (ip
= msgvec
; *ip
; ip
++) {
3290 mp
= &message
[*ip
-1];
3291 if (!(mp
->m_have
& HAVE_BODY
))
3298 disconnected(const char *file
)
3303 if (value("disconnected"))
3305 cp
= protbase(file
);
3306 if (strncmp(cp
, "imap://", 7) == 0)
3308 else if (strncmp(cp
, "imaps://", 8) == 0)
3312 if ((cq
= strchr(cp
, ':')) != NULL
)
3314 vp
= ac_alloc(vs
= strlen(cp
) + 14);
3315 snprintf(vp
, vs
, "disconnected-%s", cp
);
3316 r
= value(vp
) != NULL
;
3322 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
3324 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
3331 while (omp
< &omessage
[omsgCount
] &&
3332 nmp
< &message
[msgCount
]) {
3333 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
3335 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
3337 if (omp
->m_uid
== nmp
->m_uid
) {
3338 hf
= nmp
->m_flag
& MHIDDEN
;
3339 if (transparent
&& mb
.mb_type
== MB_IMAP
)
3340 omp
->m_flag
&= ~MHIDDEN
;
3342 if (transparent
&& mb
.mb_type
== MB_CACHE
)
3343 nmp
[-1].m_flag
|= hf
;
3344 } else if (omp
->m_uid
< nmp
->m_uid
)
3351 prevdot
= newprevdot
;
3354 #endif /* HAVE_IMAP */
3357 imap_read_date_time(const char *cp
)
3360 int i
, year
, month
, day
, hour
, minute
, second
;
3365 * "25-Jul-2004 15:33:44 +0200"
3369 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
3371 day
= strtol(&cp
[1], NULL
, 10);
3372 for (i
= 0; month_names
[i
]; i
++)
3373 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
3375 if (month_names
[i
] == NULL
)
3378 year
= strtol(&cp
[8], NULL
, 10);
3379 hour
= strtol(&cp
[13], NULL
, 10);
3380 minute
= strtol(&cp
[16], NULL
, 10);
3381 second
= strtol(&cp
[19], NULL
, 10);
3382 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) ==
3397 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
3400 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
3408 imap_read_date(const char *cp
)
3411 int year
, month
, day
, i
, tzdiff
;
3417 day
= strtol(cp
, &xp
, 10);
3418 if (day
<= 0 || day
> 31 || *xp
++ != '-')
3420 for (i
= 0; month_names
[i
]; i
++)
3421 if (ascncasecmp(xp
, month_names
[i
], 3) == 0)
3423 if (month_names
[i
] == NULL
)
3428 year
= strtol(&xp
[4], &yp
, 10);
3429 if (year
< 1970 || year
> 2037 || yp
!= &xp
[8])
3431 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
3433 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
3435 tzdiff
= t
- mktime(gmtime(&t
));
3436 tmptr
= localtime(&t
);
3437 if (tmptr
->tm_isdst
> 0)
3444 imap_make_date_time(time_t t
)
3448 int tzdiff
, tzdiff_hour
, tzdiff_min
;
3450 tzdiff
= t
- mktime(gmtime(&t
));
3451 tzdiff_hour
= (int)(tzdiff
/ 60);
3452 tzdiff_min
= tzdiff_hour
% 60;
3454 tmptr
= localtime(&t
);
3455 if (tmptr
->tm_isdst
> 0)
3457 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
3459 month_names
[tmptr
->tm_mon
],
3460 tmptr
->tm_year
+ 1900,
3470 imap_quotestr(const char *s
)
3474 np
= n
= salloc(2 * strlen(s
) + 3);
3477 if (*s
== '"' || *s
== '\\')
3487 imap_unquotestr(const char *s
)
3493 np
= n
= salloc(strlen(s
) + 1);