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 int j
= i
+ chunk
- 1;
1727 /*ok = */imap_fetchheaders(&mb
, message
, i
,
1728 j
< topp
? j
: topp
);
1733 safe_signal(SIGINT
, saveint
);
1734 safe_signal(SIGPIPE
, savepipe
);
1739 __imap_exit(struct mailbox
*mp
)
1742 FILE *queuefp
= NULL
;
1744 mp
->mb_active
|= MB_BYE
;
1745 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
1746 IMAP_OUT(o
, MB_COMD
, return STOP
)
1752 imap_exit(struct mailbox
*mp
)
1754 enum okay ret
= __imap_exit(mp
);
1755 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
1756 free(mp
->mb_imap_account
);
1757 free(mp
->mb_imap_mailbox
);
1758 if (mp
->mb_cache_directory
!= NULL
)
1759 free(mp
->mb_cache_directory
);
1760 #ifndef HAVE_ASSERTS /* TODO ASSERT LEGACY */
1761 mp
->mb_imap_account
=
1762 mp
->mb_imap_mailbox
=
1763 mp
->mb_cache_directory
= "";
1765 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
1766 mp
->mb_imap_mailbox
= NULL
;
1767 mp
->mb_cache_directory
= NULL
;
1770 sclose(&mp
->mb_sock
);
1775 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
1777 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
1778 if (mp
->mb_type
== MB_IMAP
)
1784 imap_close(struct mailbox
*mp
)
1787 FILE *queuefp
= NULL
;
1789 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
1790 IMAP_OUT(o
, MB_COMD
, return STOP
)
1796 imap_update(struct mailbox
*mp
)
1799 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
1801 if (!edit
&& mp
->mb_perm
!= 0) {
1803 for (m
= &message
[0], c
= 0; m
< &message
[msgCount
]; m
++) {
1804 if (m
->m_flag
& MBOX
)
1808 if (makembox() == STOP
)
1811 for (m
= &message
[0], gotcha
=0, held
=0; m
< &message
[msgCount
]; m
++) {
1812 if (mp
->mb_perm
== 0) {
1815 dodel
= m
->m_flag
& MDELETED
;
1817 dodel
= !((m
->m_flag
&MPRESERVE
) ||
1818 (m
->m_flag
&MTOUCH
) == 0);
1821 * Fetch the result after around each 800 STORE commands
1822 * sent (approx. 32k data sent). Otherwise, servers will
1823 * try to flush the return queue at some point, leading
1824 * to a deadlock if we are still writing commands but not
1825 * reading their results.
1827 needstat
= stored
> 0 && stored
% 800 == 0;
1829 * Even if this message has been deleted, continue
1830 * to set further flags. This is necessary to support
1831 * Gmail semantics, where "delete" actually means
1832 * "archive", and the flags are applied to the copy
1835 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
)) {
1836 imap_store(mp
, m
, m
-message
+1,
1837 '+', "\\Seen", needstat
);
1840 if (m
->m_flag
& MFLAG
) {
1841 imap_store(mp
, m
, m
-message
+1,
1842 '+', "\\Flagged", needstat
);
1845 if (m
->m_flag
& MUNFLAG
) {
1846 imap_store(mp
, m
, m
-message
+1,
1847 '-', "\\Flagged", needstat
);
1850 if (m
->m_flag
& MANSWER
) {
1851 imap_store(mp
, m
, m
-message
+1,
1852 '+', "\\Answered", needstat
);
1855 if (m
->m_flag
& MUNANSWER
) {
1856 imap_store(mp
, m
, m
-message
+1,
1857 '-', "\\Answered", needstat
);
1860 if (m
->m_flag
& MDRAFT
) {
1861 imap_store(mp
, m
, m
-message
+1,
1862 '+', "\\Draft", needstat
);
1865 if (m
->m_flag
& MUNDRAFT
) {
1866 imap_store(mp
, m
, m
-message
+1,
1867 '-', "\\Draft", needstat
);
1871 imap_delete(mp
, m
-message
+1, m
, needstat
);
1874 } else if (mp
->mb_type
!= MB_CACHE
||
1875 (! edit
&& ! (m
->m_flag
&(MBOXED
|MSAVED
|MDELETED
))) ||
1876 (m
->m_flag
& (MBOXED
|MPRESERVE
|MTOUCH
)) ==
1877 (MPRESERVE
|MTOUCH
) ||
1878 (edit
&& ! (m
->m_flag
& MDELETED
)))
1880 if (m
->m_flag
& MNEW
) {
1882 m
->m_flag
|= MSTATUS
;
1888 for (m
= &message
[0]; m
< &message
[msgCount
]; m
++)
1889 if (!(m
->m_flag
&MUNLINKED
) &&
1890 m
->m_flag
&(MBOXED
|MDELETED
|MSAVED
|MSTATUS
|
1891 MFLAG
|MUNFLAG
|MANSWER
|MUNANSWER
|
1896 if ((gotcha
|| modflags
) && edit
) {
1897 printf(tr(168, "\"%s\" "), displayname
);
1898 printf((value("bsdcompat") || value("bsdmsgs"))
1899 ? tr(170, "complete\n") : tr(212, "updated.\n"));
1900 } else if (held
&& !edit
&& mp
->mb_perm
!= 0) {
1902 printf(tr(155, "Held 1 message in %s\n"), displayname
);
1904 printf(tr(156, "Held %d messages in %s\n"), held
,
1914 sighandler_type saveint
;
1915 sighandler_type savepipe
;
1917 if (mb
.mb_type
== MB_CACHE
) {
1921 if (mb
.mb_sock
.s_fd
< 0) {
1922 fprintf(stderr
, "IMAP connection closed.\n");
1926 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1927 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1928 if (sigsetjmp(imapjmp
, 1)) {
1929 safe_signal(SIGINT
, saveint
);
1930 safe_signal(SIGPIPE
, saveint
);
1934 if (saveint
!= SIG_IGN
)
1935 safe_signal(SIGINT
, imapcatch
);
1936 if (savepipe
!= SIG_IGN
)
1937 safe_signal(SIGPIPE
, imapcatch
);
1939 if (!same_imap_account
) {
1942 safe_signal(SIGINT
, saveint
);
1943 safe_signal(SIGPIPE
, savepipe
);
1948 imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
1949 int c
, const char *sp
, int needstat
)
1952 FILE *queuefp
= NULL
;
1954 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
1957 snprintf(o
, sizeof o
,
1958 "%s UID STORE %lu %cFLAGS (%s)\r\n",
1959 tag(1), m
->m_uid
, c
, sp
);
1961 if (check_expunged() == STOP
)
1963 snprintf(o
, sizeof o
,
1964 "%s STORE %u %cFLAGS (%s)\r\n",
1967 IMAP_OUT(o
, MB_COMD
, return STOP
)
1971 mb
.mb_active
&= ~MB_COMD
;
1972 if (queuefp
!= NULL
)
1978 imap_undelete(struct message
*m
, int n
)
1980 return imap_unstore(m
, n
, "\\Deleted");
1984 imap_unread(struct message
*m
, int n
)
1986 return imap_unstore(m
, n
, "\\Seen");
1990 imap_unstore(struct message
*m
, int n
, const char *flag
)
1992 sighandler_type saveint
, savepipe
;
1993 enum okay ok
= STOP
;
1999 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2000 safe_signal(SIGINT
, maincatch
);
2001 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2002 if (sigsetjmp(imapjmp
, 1) == 0) {
2003 if (savepipe
!= SIG_IGN
)
2004 safe_signal(SIGPIPE
, imapcatch
);
2005 ok
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2007 safe_signal(SIGINT
, saveint
);
2008 safe_signal(SIGPIPE
, savepipe
);
2023 snprintf(ts
, sizeof ts
, "T%lu", n
);
2030 sighandler_type saveint
, savepipe
;
2032 enum okay ok
= STOP
;
2033 struct mailbox
*mp
= &mb
;
2034 FILE *queuefp
= NULL
;
2039 if (mp
->mb_type
!= MB_IMAP
) {
2040 printf("Not operating on an IMAP mailbox.\n");
2044 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2045 safe_signal(SIGINT
, maincatch
);
2046 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2047 if (sigsetjmp(imapjmp
, 1) == 0) {
2048 if (savepipe
!= SIG_IGN
)
2049 safe_signal(SIGPIPE
, imapcatch
);
2050 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2051 IMAP_OUT(o
, MB_COMD
, goto out
)
2052 while (mp
->mb_active
& MB_COMD
) {
2053 ok
= imap_answer(mp
, 0);
2054 fputs(responded_text
, stdout
);
2057 out
: safe_signal(SIGINT
, saveint
);
2058 safe_signal(SIGPIPE
, savepipe
);
2066 imap_newmail(int autoinc
)
2068 if (autoinc
&& had_exists
< 0 && had_expunge
< 0) {
2073 if (had_exists
== msgCount
&& had_expunge
< 0)
2075 * Some servers always respond with EXISTS to NOOP. If
2076 * the mailbox has been changed but the number of messages
2077 * has not, an EXPUNGE must also had been sent; otherwise,
2078 * nothing has changed.
2081 return had_expunge
>= 0 ? 2 : had_exists
>= 0 ? 1 : 0;
2085 imap_putflags(int f
)
2090 bp
= buf
= salloc(100);
2091 if (f
& (MREAD
|MFLAGGED
|MANSWERED
|MDRAFTED
)) {
2096 for (cp
= "\\Seen"; *cp
; cp
++)
2102 for (cp
= "\\Flagged"; *cp
; cp
++)
2105 if (f
& MANSWERED
) {
2108 for (cp
= "\\Answered"; *cp
; cp
++)
2114 for (cp
= "\\Draft"; *cp
; cp
++)
2125 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2127 while (*cp
!= ')') {
2129 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2131 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2133 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2135 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2137 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2139 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2149 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
2150 off_t off1
, long xsize
, enum mflag flag
, time_t t
)
2154 size_t bufsize
, buflen
, cnt
;
2155 enum okay ok
= STOP
;
2156 long size
, lines
, ysize
;
2158 FILE *queuefp
= NULL
;
2160 if (mp
->mb_type
== MB_CACHE
) {
2161 queuefp
= cache_queue(mp
);
2162 if (queuefp
== NULL
)
2166 buf
= smalloc(bufsize
= LINESIZE
);
2168 again
: size
= xsize
;
2170 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2174 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2175 tag(1), imap_quotestr(name
),
2176 imap_putflags(flag
),
2177 imap_make_date_time(t
),
2179 IMAP_XOUT(o
, MB_COMD
, goto out
, ok
= STOP
;goto out
)
2180 while (mp
->mb_active
& MB_COMD
) {
2181 ok
= imap_answer(mp
, twice
);
2182 if (response_type
== RESPONSE_CONT
)
2185 if (mp
->mb_type
!= MB_CACHE
&& ok
== STOP
) {
2193 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2196 buf
[buflen
-1] = '\r';
2198 if (mp
->mb_type
!= MB_CACHE
)
2199 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2201 fwrite(buf
, 1, buflen
+1, queuefp
);
2204 if (mp
->mb_type
!= MB_CACHE
)
2205 swrite(&mp
->mb_sock
, "\r\n");
2207 fputs("\r\n", queuefp
);
2208 while (mp
->mb_active
& MB_COMD
) {
2209 ok
= imap_answer(mp
, 0);
2210 if (response_status
== RESPONSE_NO
/*&&
2211 ascncasecmp(responded_text,
2212 "[TRYCREATE] ", 12) == 0*/) {
2213 trycreate
: if (twice
++) {
2217 snprintf(o
, sizeof o
, "%s CREATE %s\r\n",
2219 imap_quotestr(name
));
2220 IMAP_XOUT(o
, MB_COMD
, goto out
, ok
= STOP
;goto out
)
2221 while (mp
->mb_active
& MB_COMD
)
2222 ok
= imap_answer(mp
, 1);
2225 imap_created_mailbox
++;
2227 } else if (ok
!= OKAY
)
2228 fprintf(stderr
, tr(270, "IMAP error: %s"),
2230 else if (response_status
== RESPONSE_OK
&&
2231 mp
->mb_flags
& MB_UIDPLUS
)
2232 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
,
2235 out
: if (queuefp
!= NULL
)
2242 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2244 char *buf
, *bp
, *lp
;
2245 size_t bufsize
, buflen
, cnt
;
2246 off_t off1
= -1, offs
;
2248 int flag
= MNEW
|MNEWEST
;
2253 buf
= smalloc(bufsize
= LINESIZE
);
2259 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2260 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
2261 if (off1
!= (off_t
)-1) {
2262 ok
=imap_append1(mp
, name
, fp
, off1
,
2266 fseek(fp
, offs
+buflen
, SEEK_SET
);
2268 off1
= offs
+ buflen
;
2273 tim
= unixtime(buf
);
2277 if (bp
&& buf
[0] == '\n')
2279 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
2281 while (whitechar(*lp
&0377))
2284 while (*++lp
!= '\0')
2293 } else if (bp
&& inhead
&&
2294 ascncasecmp(buf
, "x-status", 8) == 0) {
2296 while (whitechar(*lp
&0377))
2299 while (*++lp
!= '\0')
2312 } while (bp
!= NULL
);
2318 imap_append(const char *xserver
, FILE *fp
)
2320 sighandler_type saveint
, savepipe
;
2321 char *server
, *user
, *pass
;
2322 char const *sp
, *cp
, *mbx
, *uhp
;
2324 enum okay ok
= STOP
;
2326 server
= savestr(xserver
);
2327 imap_split(&server
, &sp
, &use_ssl
, &cp
, &uhp
, &mbx
, &pass
, &user
);
2329 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2330 safe_signal(SIGINT
, maincatch
);
2331 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2332 if (sigsetjmp(imapjmp
, 1))
2334 if (savepipe
!= SIG_IGN
)
2335 safe_signal(SIGPIPE
, imapcatch
);
2336 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) &&
2337 mb
.mb_imap_account
&&
2338 strcmp(protbase(server
), mb
.mb_imap_account
) == 0) {
2339 ok
= imap_append0(&mb
, mbx
, fp
);
2344 memset(&mx
, 0, sizeof mx
);
2345 if (disconnected(server
) == 0) {
2346 if (sopen(sp
, &mx
.mb_sock
, use_ssl
, uhp
,
2347 use_ssl
? "imaps" : "imap",
2348 (options
& OPT_VERBOSE
) != 0) != OKAY
)
2350 mx
.mb_sock
.s_desc
= "IMAP";
2351 mx
.mb_type
= MB_IMAP
;
2352 mx
.mb_imap_account
= (char *)protbase(server
);
2353 /* TODO the code now did
2354 * TODO mx.mb_imap_mailbox = mbx;
2355 * TODO though imap_mailbox is sfree()d and mbx
2356 * TODO is possibly even a constant
2357 * TODO i changed this to sstrdup() sofar, as is used
2358 * TODO somewhere else in this file for this! */
2359 mx
.mb_imap_mailbox
= sstrdup(mbx
);
2360 if (imap_preauth(&mx
, sp
, uhp
) != OKAY
||
2361 imap_auth(&mx
, uhp
, user
, pass
)!=OKAY
) {
2362 sclose(&mx
.mb_sock
);
2365 ok
= imap_append0(&mx
, mbx
, fp
);
2368 mx
.mb_imap_account
= (char *)protbase(server
);
2369 mx
.mb_imap_mailbox
= sstrdup(mbx
); /* TODO as above */
2370 mx
.mb_type
= MB_CACHE
;
2371 ok
= imap_append0(&mx
, mbx
, fp
);
2375 out
: safe_signal(SIGINT
, saveint
);
2376 safe_signal(SIGPIPE
, savepipe
);
2384 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
2385 struct list_item
**lend
, int level
)
2388 enum okay ok
= STOP
;
2391 FILE *queuefp
= NULL
;
2392 struct list_item
*lp
;
2394 *list
= *lend
= NULL
;
2395 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n",
2396 tag(1), imap_quotestr(base
));
2397 IMAP_OUT(o
, MB_COMD
, return STOP
)
2398 while (mp
->mb_active
& MB_COMD
) {
2399 ok
= imap_answer(mp
, 1);
2400 if (response_status
== RESPONSE_OTHER
&&
2401 response_other
== MAILBOX_DATA_LIST
&&
2402 imap_parse_list() == OKAY
) {
2403 cp
= imap_unquotestr(list_name
);
2404 lp
= csalloc(1, sizeof *lp
);
2406 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2408 lp
->l_base
= *cp
? cp
: savestr(base
);
2409 lp
->l_attr
= list_attributes
;
2410 lp
->l_level
= level
+1;
2411 lp
->l_delim
= list_hierarchy_delimiter
;
2412 if (*list
&& *lend
) {
2413 (*lend
)->l_next
= lp
;
2423 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
2425 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
2431 depth
= (cp
= value("imap-list-depth")) != NULL
? atoi(cp
) : 2;
2432 if (imap_list1(mp
, base
, &list
, &lend
, 0) == STOP
)
2434 if (list
== NULL
|| lend
== NULL
)
2436 for (lp
= list
; lp
; lp
= lp
->l_next
)
2437 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&&
2438 lp
->l_level
< depth
&&
2439 (lp
->l_attr
&LIST_NOINFERIORS
) == 0) {
2440 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
2441 memcpy(cp
, lp
->l_name
, n
);
2442 cp
[n
] = lp
->l_delim
;
2444 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&&
2446 lp
->l_has_children
= 1;
2447 if (strcmp(cp
, lx
->l_name
) == 0)
2455 for (lp
= list
; lp
; lp
= lp
->l_next
) {
2458 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2462 if ((lp
->l_attr
&LIST_NOSELECT
) == 0)
2463 fprintf(fp
, "%s\n", *cp
? cp
: base
);
2464 else if (lp
->l_has_children
== 0)
2465 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
2466 lp
->l_delim
!= EOF
? lp
->l_delim
: '\n');
2472 imap_folders(const char *name
, int strip
)
2474 sighandler_type saveint
, savepipe
;
2475 const char *fold
, *cp
, *sp
;
2479 cp
= protbase(name
);
2480 sp
= mb
.mb_imap_account
;
2481 if (sp
== NULL
|| strcmp(cp
, sp
)) {
2482 fprintf(stderr
, tr(502,
2483 "Cannot perform `folders' but when on the very IMAP "
2484 "account; the current one is\n `%s' -- "
2485 "try `folders @'.\n"),
2486 (sp
!= NULL
) ? sp
: tr(503, "[NONE]"));
2489 fold
= imap_fileof(name
);
2490 if (options
& OPT_TTYOUT
) {
2491 if ((fp
= Ftemp(&tempfn
, "Ri", "w+", 0600, 1)) == NULL
) {
2500 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2501 safe_signal(SIGINT
, maincatch
);
2502 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2503 if (sigsetjmp(imapjmp
, 1))
2505 if (savepipe
!= SIG_IGN
)
2506 safe_signal(SIGPIPE
, imapcatch
);
2507 if (mb
.mb_type
== MB_CACHE
)
2508 cache_list(&mb
, fold
, strip
, fp
);
2510 imap_list(&mb
, fold
, strip
, fp
);
2513 if (options
& OPT_TTYOUT
)
2518 if (options
& OPT_TTYOUT
) {
2523 fprintf(stderr
, "Folder not found.\n");
2526 safe_signal(SIGINT
, saveint
);
2527 safe_signal(SIGPIPE
, savepipe
);
2528 if (options
& OPT_TTYOUT
)
2535 char o
[LINESIZE
], *tempfn
;
2537 long n
= 0, mx
= 0, columns
, width
;
2540 if ((out
= Ftemp(&tempfn
, "Ro", "w+", 0600, 1)) == NULL
) {
2546 while ((c
= getc(fp
)) != EOF
) {
2556 if (mx
< width
/ 2) {
2557 columns
= width
/ (mx
+2);
2558 snprintf(o
, sizeof o
,
2559 "sort | pr -%lu -w%lu -t",
2562 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
2563 run_command(SHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
2569 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
2573 enum okay ok
= STOP
;
2576 FILE *queuefp
= NULL
;
2578 if (mp
->mb_type
== MB_CACHE
) {
2579 if ((queuefp
= cache_queue(mp
)) == NULL
)
2583 qname
= imap_quotestr(name
= imap_fileof(name
));
2585 * Since it is not possible to set flags on the copy, recently
2586 * set flags must be set on the original to include it in the copy.
2588 if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
))
2589 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
2590 if (m
->m_flag
&MFLAG
)
2591 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2592 if (m
->m_flag
&MUNFLAG
)
2593 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2594 if (m
->m_flag
&MANSWER
)
2595 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2596 if (m
->m_flag
&MUNANSWER
)
2597 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2598 if (m
->m_flag
&MDRAFT
)
2599 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2600 if (m
->m_flag
&MUNDRAFT
)
2601 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2602 again
: if (m
->m_uid
)
2603 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n",
2604 tag(1), m
->m_uid
, qname
);
2606 if (check_expunged() == STOP
)
2608 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n",
2611 IMAP_OUT(o
, MB_COMD
, goto out
)
2612 while (mp
->mb_active
& MB_COMD
)
2613 ok
= imap_answer(mp
, twice
);
2614 if (mp
->mb_type
== MB_IMAP
&&
2615 mp
->mb_flags
& MB_UIDPLUS
&&
2616 response_status
== RESPONSE_OK
)
2617 imap_copyuid(mp
, m
, name
);
2618 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
2619 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2620 IMAP_OUT(o
, MB_COMD
, goto out
)
2621 while (mp
->mb_active
& MB_COMD
)
2622 ok
= imap_answer(mp
, 1);
2624 imap_created_mailbox
++;
2628 if (queuefp
!= NULL
)
2631 * ... and reset the flag to its initial value so that
2632 * the 'exit' command still leaves the message unread.
2634 out
: if ((m
->m_flag
&(MREAD
|MSTATUS
)) == (MREAD
|MSTATUS
)) {
2635 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
2638 if (m
->m_flag
&MFLAG
) {
2639 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2642 if (m
->m_flag
&MUNFLAG
) {
2643 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2646 if (m
->m_flag
&MANSWER
) {
2647 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
2650 if (m
->m_flag
&MUNANSWER
) {
2651 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2654 if (m
->m_flag
&MDRAFT
) {
2655 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2658 if (m
->m_flag
&MUNDRAFT
) {
2659 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2663 mp
->mb_active
|= MB_COMD
;
2664 (void)imap_finish(mp
);
2670 imap_copy(struct message
*m
, int n
, const char *name
)
2672 sighandler_type saveint
, savepipe
;
2673 enum okay ok
= STOP
;
2679 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2680 safe_signal(SIGINT
, maincatch
);
2681 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2682 if (sigsetjmp(imapjmp
, 1) == 0) {
2683 if (savepipe
!= SIG_IGN
)
2684 safe_signal(SIGPIPE
, imapcatch
);
2685 ok
= imap_copy1(&mb
, m
, n
, name
);
2687 safe_signal(SIGINT
, saveint
);
2688 safe_signal(SIGPIPE
, savepipe
);
2696 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
2697 unsigned long *olduid
, unsigned long *newuid
)
2701 *uidvalidity
= strtoul(cp
, &xp
, 10);
2702 *olduid
= strtoul(xp
, &yp
, 10);
2703 *newuid
= strtoul(yp
, &zp
, 10);
2704 return *uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
2705 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']';
2709 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
2714 *uidvalidity
= strtoul(cp
, &xp
, 10);
2715 *uid
= strtoul(xp
, &yp
, 10);
2716 return *uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' &&
2717 yp
> xp
&& *yp
== ']';
2721 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
2724 unsigned long uidvalidity
, olduid
, newuid
;
2728 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
2729 imap_copyuid_parse(&cp
[9], &uidvalidity
,
2730 &olduid
, &newuid
) == STOP
)
2733 xmb
.mb_cache_directory
= NULL
;
2734 xmb
.mb_imap_mailbox
= savestr(name
);
2735 xmb
.mb_uidvalidity
= uidvalidity
;
2738 memset(&xm
, 0, sizeof xm
);
2740 if (getcache1(mp
, &xm
, NEED_UNSPEC
, 3) != OKAY
)
2742 getcache(mp
, &xm
, NEED_HEADER
);
2743 getcache(mp
, &xm
, NEED_BODY
);
2745 if ((m
->m_flag
& HAVE_HEADER
) == 0)
2746 getcache(mp
, m
, NEED_HEADER
);
2747 if ((m
->m_flag
& HAVE_BODY
) == 0)
2748 getcache(mp
, m
, NEED_BODY
);
2752 xm
.m_flag
&= ~MFULLYCACHED
;
2753 putcache(&xmb
, &xm
);
2758 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
,
2759 long xsize
, long size
, long lines
, int flag
, const char *name
)
2762 unsigned long uidvalidity
, uid
;
2766 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
2767 imap_appenduid_parse(&cp
[11], &uidvalidity
,
2771 xmb
.mb_cache_directory
= NULL
;
2772 xmb
.mb_imap_mailbox
= savestr(name
);
2773 xmb
.mb_uidvalidity
= uidvalidity
;
2774 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
2776 memset(&xm
, 0, sizeof xm
);
2777 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
2779 xm
.m_block
= mailx_blockof(off1
);
2780 xm
.m_offset
= mailx_offsetof(off1
);
2783 xm
.m_lines
= xm
.m_xlines
= lines
;
2785 xm
.m_have
= HAVE_HEADER
|HAVE_BODY
;
2786 putcache(&xmb
, &xm
);
2791 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
2795 long size
, xsize
, ysize
, lines
;
2796 enum mflag flag
= MNEW
;
2797 char *name
, *buf
, *bp
, *tempCopy
;
2799 size_t bufsize
, buflen
, cnt
;
2800 enum okay ok
= STOP
;
2802 buf
= smalloc(bufsize
= LINESIZE
);
2805 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2807 for (bp
= buf
; *bp
!= ' '; bp
++); /* strip old tag */
2810 if ((cp
= strrchr(bp
, '{')) == NULL
)
2812 xsize
= atol(&cp
[1]) + 2;
2813 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
2818 imap_getflags(cp
, &cp
, &flag
);
2819 while (*++cp
== ' ');
2821 t
= imap_read_date_time(cp
);
2822 if ((tp
= Ftemp(&tempCopy
, "Rc", "w+", 0600, 1)) == NULL
)
2829 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2832 buf
[--buflen
] = '\0';
2833 buf
[buflen
-1] = '\n';
2834 fwrite(buf
, 1, buflen
, tp
);
2840 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
2841 imap_unquotestr(name
));
2850 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
2851 const char *spec
, int f
)
2855 FILE *queuefp
= NULL
;
2856 enum okay ok
= STOP
;
2863 for (cp
= spec
; *cp
; cp
++)
2866 cp
= charset_get_lc();
2868 if (asccasecmp(cp
, "utf-8")) {
2872 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
2873 sz
= strlen(spec
) + 1;
2874 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
2875 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
,
2876 FAL0
) == 0 && sz
== 0) {
2884 cp
= imap_quotestr(cp
);
2885 cs
= salloc(n
= strlen(cp
) + 10);
2886 snprintf(cs
, n
, "CHARSET %s ", cp
);
2889 o
= ac_alloc(osize
= strlen(spec
) + 60);
2890 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
2891 IMAP_OUT(o
, MB_COMD
, goto out
)
2892 while (mp
->mb_active
& MB_COMD
) {
2893 ok
= imap_answer(mp
, 0);
2894 if (response_status
== RESPONSE_OTHER
&&
2895 response_other
== MAILBOX_DATA_SEARCH
) {
2896 xp
= responded_other_text
;
2897 while (*xp
&& *xp
!= '\r') {
2898 n
= strtoul(xp
, &xp
, 10);
2899 for (i
= 0; i
< cnt
; i
++)
2900 if (m
[i
].m_uid
== n
&&
2901 (m
[i
].m_flag
&MHIDDEN
)
2904 (m
[i
].m_flag
&MDELETED
)
2915 imap_search1(const char *volatile spec
, int f
)
2917 sighandler_type saveint
, savepipe
;
2918 enum okay ok
= STOP
;
2923 if (mb
.mb_type
!= MB_IMAP
)
2926 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2927 safe_signal(SIGINT
, maincatch
);
2928 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2929 if (sigsetjmp(imapjmp
, 1) == 0) {
2930 if (savepipe
!= SIG_IGN
)
2931 safe_signal(SIGPIPE
, imapcatch
);
2932 ok
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
2934 safe_signal(SIGINT
, saveint
);
2935 safe_signal(SIGPIPE
, savepipe
);
2943 imap_thisaccount(const char *cp
)
2945 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
2947 if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
2948 mb
.mb_imap_account
== NULL
)
2950 return strcmp(protbase(cp
), mb
.mb_imap_account
) == 0;
2954 imap_remove(const char *name
)
2956 sighandler_type saveint
, savepipe
;
2957 enum okay ok
= STOP
;
2962 if (mb
.mb_type
!= MB_IMAP
) {
2963 fprintf(stderr
, "Refusing to remove \"%s\" "
2964 "in disconnected mode.\n", name
);
2967 if (!imap_thisaccount(name
)) {
2968 fprintf(stderr
, "Can only remove mailboxes on current IMAP "
2969 "server: \"%s\" not removed.\n", name
);
2973 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2974 safe_signal(SIGINT
, maincatch
);
2975 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2976 if (sigsetjmp(imapjmp
, 1) == 0) {
2977 if (savepipe
!= SIG_IGN
)
2978 safe_signal(SIGPIPE
, imapcatch
);
2979 ok
= imap_remove1(&mb
, imap_fileof(name
));
2981 safe_signal(SIGINT
, saveint
);
2982 safe_signal(SIGPIPE
, savepipe
);
2985 ok
= cache_remove(name
);
2992 imap_remove1(struct mailbox
*mp
, const char *name
)
2994 FILE *queuefp
= NULL
;
2997 enum okay ok
= STOP
;
2999 o
= ac_alloc(os
= 2*strlen(name
) + 100);
3000 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), imap_quotestr(name
));
3001 IMAP_OUT(o
, MB_COMD
, goto out
)
3002 while (mp
->mb_active
& MB_COMD
)
3003 ok
= imap_answer(mp
, 1);
3009 imap_rename(const char *old
, const char *new)
3011 sighandler_type saveint
, savepipe
;
3012 enum okay ok
= STOP
;
3017 if (mb
.mb_type
!= MB_IMAP
) {
3018 fprintf(stderr
, "Refusing to rename mailboxes "
3019 "in disconnected mode.\n");
3022 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3023 fprintf(stderr
, "Can only rename mailboxes on current IMAP "
3024 "server: \"%s\" not renamed to \"%s\".\n",
3029 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3030 safe_signal(SIGINT
, maincatch
);
3031 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3032 if (sigsetjmp(imapjmp
, 1) == 0) {
3033 if (savepipe
!= SIG_IGN
)
3034 safe_signal(SIGPIPE
, imapcatch
);
3035 ok
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3037 safe_signal(SIGINT
, saveint
);
3038 safe_signal(SIGPIPE
, savepipe
);
3041 ok
= cache_rename(old
, new);
3048 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3050 FILE *queuefp
= NULL
;
3053 enum okay ok
= STOP
;
3055 o
= ac_alloc(os
= 2*strlen(old
) + 2*strlen(new) + 100);
3056 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1),
3057 imap_quotestr(old
), imap_quotestr(new));
3058 IMAP_OUT(o
, MB_COMD
, goto out
)
3059 while (mp
->mb_active
& MB_COMD
)
3060 ok
= imap_answer(mp
, 1);
3066 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3068 FILE *queuefp
= NULL
;
3069 char o
[LINESIZE
], *newname
;
3070 char *buf
, *bp
, *cp
, iob
[4096];
3071 size_t bufsize
, buflen
, cnt
;
3072 enum okay ok
= OKAY
, rok
= OKAY
;
3073 long offs
, offs1
, offs2
, octets
;
3074 int twice
, gotcha
= 0;
3076 buf
= smalloc(bufsize
= LINESIZE
);
3079 while ((offs1
= ftell(fp
)) >= 0 &&
3080 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0)
3082 for (bp
= buf
; *bp
!= ' '; bp
++); /* strip old tag */
3086 if ((offs
= ftell(fp
)) < 0)
3088 again
: snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3089 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3091 while (digitchar(*cp
&0377))
3097 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3099 IMAP_OUT(o
, MB_COMD
, continue)
3100 while (mp
->mb_active
& MB_COMD
)
3101 ok
= imap_answer(mp
, twice
);
3102 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3104 if (response_status
== RESPONSE_OK
&&
3105 mp
->mb_flags
& MB_UIDPLUS
) {
3106 imap_copyuid(mp
, NULL
,
3107 imap_unquotestr(newname
));
3109 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3110 IMAP_OUT(o
, MB_COMD
, continue)
3111 while (mp
->mb_active
& MB_COMD
)
3112 ok
= imap_answer(mp
, 1);
3115 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3116 if ((cp
= strrchr(bp
, '{')) == NULL
)
3118 octets
= atol(&cp
[1]) + 2;
3119 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3121 IMAP_OUT(o
, MB_COMD
, continue)
3122 while (mp
->mb_active
& MB_COMD
) {
3123 ok
= imap_answer(mp
, twice
);
3124 if (response_type
== RESPONSE_CONT
)
3129 fseek(fp
, offs
, SEEK_SET
) >= 0)
3133 while (octets
> 0) {
3134 size_t n
= (size_t)octets
> sizeof iob
3135 ? sizeof iob
: (size_t)octets
;
3137 if (n
!= fread(iob
, 1, n
, fp
))
3139 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3141 swrite(&mp
->mb_sock
, "");
3142 while (mp
->mb_active
& MB_COMD
) {
3143 ok
= imap_answer(mp
, 0);
3144 if (response_status
== RESPONSE_NO
&&
3146 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3151 if (response_status
== RESPONSE_OK
&&
3152 mp
->mb_flags
& MB_UIDPLUS
) {
3153 if ((offs2
= ftell(fp
)) < 0)
3155 fseek(fp
, offs1
, SEEK_SET
);
3156 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3157 (void)fseek(fp
, offs2
, SEEK_SET
);
3162 fail
: fprintf(stderr
,
3163 "Invalid command in IMAP cache queue: \"%s\"\n",
3169 snprintf(o
, sizeof o
, "%s CREATE %s\r\n",
3171 IMAP_OUT(o
, MB_COMD
, continue)
3172 while (mp
->mb_active
& MB_COMD
)
3173 ok
= imap_answer(mp
, 1);
3179 ftruncate(fileno(fp
), 0);
3187 imap_strex(char const *cp
, char const **xp
)
3194 for (cq
= &cp
[1]; *cq
; cq
++) {
3197 else if (*cq
== '"')
3202 n
= salloc(cq
- cp
+ 2);
3203 memcpy(n
, cp
, cq
- cp
+ 1);
3204 n
[cq
- cp
+ 1] = '\0';
3211 check_expunged(void)
3213 if (expunged_messages
> 0) {
3215 "Command not executed - messages have been expunged\n");
3226 int omsgCount
= msgCount
;
3229 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
3230 fprintf(stderr
, "Already connected.\n");
3233 unset_allow_undefined
= TRU1
;
3234 unset_internal("disconnected");
3235 cp
= protbase(mailname
);
3236 if (strncmp(cp
, "imap://", 7) == 0)
3238 else if (strncmp(cp
, "imaps://", 8) == 0)
3240 if ((cq
= strchr(cp
, ':')) != NULL
)
3242 unset_internal(savecat("disconnected-", cp
));
3243 unset_allow_undefined
= FAL0
;
3244 if (mb
.mb_type
== MB_CACHE
) {
3245 imap_setfile1(mailname
, 0, edit
, 1);
3246 if (msgCount
> omsgCount
)
3247 newmailinfo(omsgCount
);
3253 cdisconnect(void *vp
)
3257 if (mb
.mb_type
== MB_CACHE
) {
3258 fprintf(stderr
, "Not connected.\n");
3260 } else if (mb
.mb_type
== MB_IMAP
) {
3261 if (cached_uidvalidity(&mb
) == 0) {
3262 fprintf(stderr
, "The current mailbox is not cached.\n");
3268 assign("disconnected", "");
3269 if (mb
.mb_type
== MB_IMAP
) {
3270 sclose(&mb
.mb_sock
);
3271 imap_setfile1(mailname
, 0, edit
, 1);
3279 int *msgvec
= vp
, *ip
;
3282 if (mb
.mb_type
!= MB_IMAP
) {
3283 fprintf(stderr
, "Not connected to an IMAP server.\n");
3286 if (cached_uidvalidity(&mb
) == 0) {
3287 fprintf(stderr
, "The current mailbox is not cached.\n");
3290 for (ip
= msgvec
; *ip
; ip
++) {
3291 mp
= &message
[*ip
-1];
3292 if (!(mp
->m_have
& HAVE_BODY
))
3299 disconnected(const char *file
)
3304 if (value("disconnected"))
3306 cp
= protbase(file
);
3307 if (strncmp(cp
, "imap://", 7) == 0)
3309 else if (strncmp(cp
, "imaps://", 8) == 0)
3313 if ((cq
= strchr(cp
, ':')) != NULL
)
3315 vp
= ac_alloc(vs
= strlen(cp
) + 14);
3316 snprintf(vp
, vs
, "disconnected-%s", cp
);
3317 r
= value(vp
) != NULL
;
3323 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
3325 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
3332 while (omp
< &omessage
[omsgCount
] &&
3333 nmp
< &message
[msgCount
]) {
3334 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
3336 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
3338 if (omp
->m_uid
== nmp
->m_uid
) {
3339 hf
= nmp
->m_flag
& MHIDDEN
;
3340 if (transparent
&& mb
.mb_type
== MB_IMAP
)
3341 omp
->m_flag
&= ~MHIDDEN
;
3343 if (transparent
&& mb
.mb_type
== MB_CACHE
)
3344 nmp
[-1].m_flag
|= hf
;
3345 } else if (omp
->m_uid
< nmp
->m_uid
)
3352 prevdot
= newprevdot
;
3355 #endif /* HAVE_IMAP */
3358 imap_read_date_time(const char *cp
)
3361 int i
, year
, month
, day
, hour
, minute
, second
;
3366 * "25-Jul-2004 15:33:44 +0200"
3370 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
3372 day
= strtol(&cp
[1], NULL
, 10);
3374 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
3376 if (month_names
[++i
][0] == '\0')
3380 year
= strtol(&cp
[8], NULL
, 10);
3381 hour
= strtol(&cp
[13], NULL
, 10);
3382 minute
= strtol(&cp
[16], NULL
, 10);
3383 second
= strtol(&cp
[19], NULL
, 10);
3384 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) ==
3399 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
3402 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
3410 imap_read_date(const char *cp
)
3413 int year
, month
, day
, i
, tzdiff
;
3419 day
= strtol(cp
, &xp
, 10);
3420 if (day
<= 0 || day
> 31 || *xp
++ != '-')
3423 if (ascncasecmp(xp
, month_names
[i
], 3) == 0)
3425 if (month_names
[++i
][0] == '\0')
3431 year
= strtol(&xp
[4], &yp
, 10);
3432 if (year
< 1970 || year
> 2037 || yp
!= &xp
[8])
3434 if (yp
[0] != '\0' && (yp
[1] != '"' || yp
[2] != '\0'))
3436 if ((t
= combinetime(year
, month
, day
, 0, 0, 0)) == (time_t)-1)
3438 tzdiff
= t
- mktime(gmtime(&t
));
3439 tmptr
= localtime(&t
);
3440 if (tmptr
->tm_isdst
> 0)
3447 imap_make_date_time(time_t t
)
3451 int tzdiff
, tzdiff_hour
, tzdiff_min
;
3453 tzdiff
= t
- mktime(gmtime(&t
));
3454 tzdiff_hour
= (int)(tzdiff
/ 60);
3455 tzdiff_min
= tzdiff_hour
% 60;
3457 tmptr
= localtime(&t
);
3458 if (tmptr
->tm_isdst
> 0)
3460 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
3462 month_names
[tmptr
->tm_mon
],
3463 tmptr
->tm_year
+ 1900,
3473 imap_quotestr(const char *s
)
3477 np
= n
= salloc(2 * strlen(s
) + 3);
3480 if (*s
== '"' || *s
== '\\')
3490 imap_unquotestr(const char *s
)
3496 np
= n
= salloc(strlen(s
) + 1);