1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ IMAP v4r1 client following RFC 2060.
3 *@ CRAM-MD5 as of RFC 2195.
5 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
6 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
10 * Gunnar Ritter. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Gunnar Ritter
23 * and his contributors.
24 * 4. Neither the name of Gunnar Ritter nor the names of his contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY GUNNAR RITTER AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL GUNNAR RITTER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #ifndef HAVE_AMALGAMATION
46 # include <sys/socket.h>
50 # include <netinet/in.h>
52 # ifdef HAVE_ARPA_INET_H
53 # include <arpa/inet.h>
58 #define IMAP_ANSWER() \
60 if (mp->mb_type != MB_CACHE) {\
62 while (mp->mb_active & MB_COMD)\
63 ok = imap_answer(mp, 1);\
69 /* TODO IMAP_OUT() simply returns instead of doing "actioN" if imap_finish()
70 * TODO fails, which leaves behind leaks in, e.g., imap_append1()!
71 * TODO IMAP_XOUT() was added due to this, but (1) needs to be used everywhere
72 * TODO and (2) doesn't handle all I/O errors itself, yet, too.
73 * TODO I.e., that should be a function, not a macro ... or so.
74 * TODO This entire module needs MASSIVE work! */
75 #define IMAP_OUT(X,Y,ACTION) IMAP_XOUT(X, Y, ACTION, return STOP)
76 #define IMAP_XOUT(X,Y,ACTIONERR,ACTIONBAIL) \
78 if (mp->mb_type != MB_CACHE) {\
79 if (imap_finish(mp) == STOP) {\
82 if (options & OPT_VERBVERB)\
83 fprintf(stderr, ">>> %s", X);\
85 if (swrite(&mp->mb_sock, X) == STOP) {\
94 static struct record
{
95 struct record
*rec_next
;
96 unsigned long rec_count
;
121 static char *responded_tag
;
122 static char *responded_text
;
123 static char *responded_other_text
;
124 static long responded_other_number
;
130 MAILBOX_DATA_MAILBOX
,
135 MESSAGE_DATA_EXPUNGE
,
138 RESPONSE_OTHER_UNKNOWN
141 static enum list_attributes
{
143 LIST_NOINFERIORS
= 001,
149 static int list_hierarchy_delimiter
;
150 static char *list_name
;
153 struct list_item
*l_next
;
156 enum list_attributes l_attr
;
162 static char *imapbuf
; /* TODO not static, use pool */
163 static size_t imapbufsize
;
164 static sigjmp_buf imapjmp
;
165 static sighandler_type savealrm
;
166 static int imapkeepalive
;
167 static long had_exists
= -1;
168 static long had_expunge
= -1;
169 static long expunged_messages
;
170 static int volatile imaplock
;
171 static int same_imap_account
;
173 static void imap_other_get(char *pp
);
174 static void imap_response_get(const char **cp
);
175 static void imap_response_parse(void);
176 static enum okay
imap_answer(struct mailbox
*mp
, int errprnt
);
177 static enum okay
imap_parse_list(void);
178 static enum okay
imap_finish(struct mailbox
*mp
);
179 static void imap_timer_off(void);
180 static void imapcatch(int s
);
181 static void _imap_maincatch(int s
);
182 static enum okay
imap_noop1(struct mailbox
*mp
);
183 static void rec_queue(enum rec_type type
, unsigned long cnt
);
184 static enum okay
rec_dequeue(void);
185 static void rec_rmqueue(void);
186 static void imapalarm(int s
);
187 static int imap_use_starttls(const char *uhp
);
188 static enum okay
imap_preauth(struct mailbox
*mp
, const char *xserver
,
190 static enum okay
imap_capability(struct mailbox
*mp
);
191 static enum okay
imap_auth(struct mailbox
*mp
, struct ccred
*ccred
);
193 static enum okay
imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
);
195 static enum okay
imap_login(struct mailbox
*mp
, struct ccred
*ccred
);
197 static enum okay
_imap_gssapi(struct mailbox
*mp
, struct ccred
*ccred
);
199 static enum okay
imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
);
200 static void imap_init(struct mailbox
*mp
, int n
);
201 static void imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
,
203 static bool_t
_imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
,
205 static int _imap_setfile1(struct url
*urlp
, int nmail
, int isedit
,
207 static int imap_fetchdata(struct mailbox
*mp
, struct message
*m
,
208 size_t expected
, int need
, const char *head
,
209 size_t headsize
, long headlines
);
210 static void imap_putstr(struct mailbox
*mp
, struct message
*m
,
211 const char *str
, const char *head
, size_t headsize
,
213 static enum okay
imap_get(struct mailbox
*mp
, struct message
*m
,
215 static void commitmsg(struct mailbox
*mp
, struct message
*to
,
216 struct message
*from
, enum havespec have
);
217 static enum okay
imap_fetchheaders(struct mailbox
*mp
, struct message
*m
,
219 static enum okay
imap_exit(struct mailbox
*mp
);
220 static enum okay
imap_delete(struct mailbox
*mp
, int n
, struct message
*m
,
222 static enum okay
imap_close(struct mailbox
*mp
);
223 static enum okay
imap_update(struct mailbox
*mp
);
224 static enum okay
imap_store(struct mailbox
*mp
, struct message
*m
, int n
,
225 int c
, const char *sp
, int needstat
);
226 static enum okay
imap_unstore(struct message
*m
, int n
, const char *flag
);
227 static const char *tag(int new);
228 static char * imap_putflags(int f
);
229 static void imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
);
230 static enum okay
imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
,
231 off_t off1
, long xsize
, enum mflag flag
, time_t t
);
232 static enum okay
imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
);
233 static enum okay
imap_list1(struct mailbox
*mp
, const char *base
,
234 struct list_item
**list
, struct list_item
**lend
,
236 static enum okay
imap_list(struct mailbox
*mp
, const char *base
, int strip
,
238 static void dopr(FILE *fp
);
239 static enum okay
imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
,
241 static enum okay
imap_copyuid_parse(const char *cp
,
242 unsigned long *uidvalidity
, unsigned long *olduid
,
243 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
, int flag
,
251 static enum okay
imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
);
252 #ifdef HAVE_IMAP_SEARCH
253 static enum okay
imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
,
254 const char *spec
, int f
);
256 static enum okay
imap_remove1(struct mailbox
*mp
, const char *name
);
257 static enum okay
imap_rename1(struct mailbox
*mp
, const char *old
,
259 static char * imap_strex(char const *cp
, char const **xp
);
260 static enum okay
check_expunged(void);
263 imap_other_get(char *pp
)
268 if (ascncasecmp(pp
, "FLAGS ", 6) == 0) {
270 response_other
= MAILBOX_DATA_FLAGS
;
271 } else if (ascncasecmp(pp
, "LIST ", 5) == 0) {
273 response_other
= MAILBOX_DATA_LIST
;
274 } else if (ascncasecmp(pp
, "LSUB ", 5) == 0) {
276 response_other
= MAILBOX_DATA_LSUB
;
277 } else if (ascncasecmp(pp
, "MAILBOX ", 8) == 0) {
279 response_other
= MAILBOX_DATA_MAILBOX
;
280 } else if (ascncasecmp(pp
, "SEARCH ", 7) == 0) {
282 response_other
= MAILBOX_DATA_SEARCH
;
283 } else if (ascncasecmp(pp
, "STATUS ", 7) == 0) {
285 response_other
= MAILBOX_DATA_STATUS
;
286 } else if (ascncasecmp(pp
, "CAPABILITY ", 11) == 0) {
288 response_other
= CAPABILITY_DATA
;
290 responded_other_number
= strtol(pp
, &xp
, 10);
293 if (ascncasecmp(xp
, "EXISTS\r\n", 8) == 0) {
294 response_other
= MAILBOX_DATA_EXISTS
;
295 } else if (ascncasecmp(xp
, "RECENT\r\n", 8) == 0) {
296 response_other
= MAILBOX_DATA_RECENT
;
297 } else if (ascncasecmp(xp
, "EXPUNGE\r\n", 9) == 0) {
298 response_other
= MESSAGE_DATA_EXPUNGE
;
299 } else if (ascncasecmp(xp
, "FETCH ", 6) == 0) {
301 response_other
= MESSAGE_DATA_FETCH
;
303 response_other
= RESPONSE_OTHER_UNKNOWN
;
305 responded_other_text
= pp
;
310 imap_response_get(const char **cp
)
313 if (ascncasecmp(*cp
, "OK ", 3) == 0) {
315 response_status
= RESPONSE_OK
;
316 } else if (ascncasecmp(*cp
, "NO ", 3) == 0) {
318 response_status
= RESPONSE_NO
;
319 } else if (ascncasecmp(*cp
, "BAD ", 4) == 0) {
321 response_status
= RESPONSE_BAD
;
322 } else if (ascncasecmp(*cp
, "PREAUTH ", 8) == 0) {
324 response_status
= RESPONSE_PREAUTH
;
325 } else if (ascncasecmp(*cp
, "BYE ", 4) == 0) {
327 response_status
= RESPONSE_BYE
;
329 response_status
= RESPONSE_OTHER
;
334 imap_response_parse(void)
336 static char *parsebuf
; /* TODO Use pool */
337 static size_t parsebufsize
;
339 const char *ip
= imapbuf
;
343 if (parsebufsize
< imapbufsize
)
344 parsebuf
= srealloc(parsebuf
, parsebufsize
= imapbufsize
);
345 memcpy(parsebuf
, imapbuf
, strlen(imapbuf
) + 1);
349 response_type
= RESPONSE_CONT
;
364 imap_response_get(&ip
);
365 pp
= &parsebuf
[ip
- imapbuf
];
366 switch (response_status
) {
368 response_type
= RESPONSE_FATAL
;
371 response_type
= RESPONSE_DATA
;
375 responded_tag
= parsebuf
;
376 while (*pp
&& *pp
!= ' ')
379 response_type
= RESPONSE_ILLEGAL
;
383 while (*pp
&& *pp
== ' ')
386 response_type
= RESPONSE_ILLEGAL
;
389 ip
= &imapbuf
[pp
- parsebuf
];
390 response_type
= RESPONSE_TAGGED
;
391 imap_response_get(&ip
);
392 pp
= &parsebuf
[ip
- imapbuf
];
395 if (response_type
!= RESPONSE_CONT
&& response_type
!= RESPONSE_ILLEGAL
&&
396 response_status
== RESPONSE_OTHER
)
402 imap_answer(struct mailbox
*mp
, int errprnt
)
409 if (mp
->mb_type
== MB_CACHE
)
413 if (sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
414 if (options
& OPT_VERBVERB
)
415 fputs(imapbuf
, stderr
);
416 imap_response_parse();
417 if (response_type
== RESPONSE_ILLEGAL
)
419 if (response_type
== RESPONSE_CONT
) {
423 if (response_status
== RESPONSE_OTHER
) {
424 if (response_other
== MAILBOX_DATA_EXISTS
) {
425 had_exists
= responded_other_number
;
426 rec_queue(REC_EXISTS
, responded_other_number
);
429 } else if (response_other
== MESSAGE_DATA_EXPUNGE
) {
430 rec_queue(REC_EXPUNGE
, responded_other_number
);
438 if (response_type
== RESPONSE_TAGGED
) {
439 if (asccasecmp(responded_tag
, tag(0)) == 0)
444 switch (response_status
) {
445 case RESPONSE_PREAUTH
:
446 mp
->mb_active
&= ~MB_PREAUTH
;
459 fprintf(stderr
, _("IMAP error: %s"), responded_text
);
461 case RESPONSE_UNKNOWN
: /* does not happen */
464 mp
->mb_active
= MB_NONE
;
472 if (response_status
!= RESPONSE_OTHER
&&
473 ascncasecmp(responded_text
, "[ALERT] ", 8) == 0)
474 fprintf(stderr
, "IMAP alert: %s", &responded_text
[8]);
476 mp
->mb_active
&= ~MB_COMD
;
479 mp
->mb_active
= MB_NONE
;
487 imap_parse_list(void)
495 cp
= responded_other_text
;
496 list_attributes
= LIST_NONE
;
498 while (*cp
&& *cp
!= ')') {
500 if (ascncasecmp(&cp
[1], "Noinferiors ", 12) == 0) {
501 list_attributes
|= LIST_NOINFERIORS
;
503 } else if (ascncasecmp(&cp
[1], "Noselect ", 9) == 0) {
504 list_attributes
|= LIST_NOSELECT
;
506 } else if (ascncasecmp(&cp
[1], "Marked ", 7) == 0) {
507 list_attributes
|= LIST_MARKED
;
509 } else if (ascncasecmp(&cp
[1], "Unmarked ", 9) == 0) {
510 list_attributes
|= LIST_UNMARKED
;
522 list_hierarchy_delimiter
= EOF
;
526 list_hierarchy_delimiter
= *cp
++ & 0377;
527 if (cp
[0] != '"' || cp
[1] != ' ')
530 } else if (cp
[0] == 'N' && cp
[1] == 'I' && cp
[2] == 'L' && cp
[3] == ' ') {
531 list_hierarchy_delimiter
= EOF
;
538 while (*cp
&& *cp
!= '\r')
548 imap_finish(struct mailbox
*mp
)
551 while (mp
->mb_sock
.s_fd
> 0 && mp
->mb_active
& MB_COMD
)
561 if (imapkeepalive
> 0) {
563 safe_signal(SIGALRM
, savealrm
);
571 NYD_X
; /* Signal handler */
574 fprintf(stderr
, _("Interrupt\n"));
575 siglongjmp(imapjmp
, 1);
578 fprintf(stderr
, _("Received SIGPIPE during IMAP operation\n"));
584 _imap_maincatch(int s
)
586 NYD_X
; /* Signal handler */
588 if (interrupts
++ == 0) {
589 fprintf(stderr
, _("Interrupt\n"));
596 imap_noop1(struct mailbox
*mp
)
599 FILE *queuefp
= NULL
;
602 snprintf(o
, sizeof o
, "%s NOOP\r\n", tag(1));
603 IMAP_OUT(o
, MB_COMD
, return STOP
)
609 imap_fileof(char const *xcp
)
611 char const *cp
= xcp
;
616 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
620 if (cp
[0] == '/' && state
== 1) {
638 sighandler_type
volatile oldint
, oldpipe
;
642 if (mb
.mb_type
!= MB_IMAP
)
646 if ((oldint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
647 safe_signal(SIGINT
, &_imap_maincatch
);
648 oldpipe
= safe_signal(SIGPIPE
, SIG_IGN
);
649 if (sigsetjmp(imapjmp
, 1) == 0) {
650 if (oldpipe
!= SIG_IGN
)
651 safe_signal(SIGPIPE
, imapcatch
);
653 rv
= imap_noop1(&mb
);
655 safe_signal(SIGINT
, oldint
);
656 safe_signal(SIGPIPE
, oldpipe
);
666 rec_queue(enum rec_type rt
, unsigned long cnt
)
671 rp
= scalloc(1, sizeof *rp
);
674 if (record
&& recend
) {
675 recend
->rec_next
= rp
;
678 record
= recend
= rp
;
685 struct message
*omessage
;
686 struct record
*rp
, *rq
;
695 message
= smalloc((msgCount
+1) * sizeof *message
);
697 memcpy(message
, omessage
, msgCount
* sizeof *message
);
698 memset(&message
[msgCount
], 0, sizeof *message
);
700 rp
= record
, rq
= NULL
;
703 switch (rp
->rec_type
) {
705 exists
= rp
->rec_count
;
708 if (rp
->rec_count
== 0) {
712 if (rp
->rec_count
> (unsigned long)msgCount
) {
713 if (exists
== 0 || rp
->rec_count
> exists
--)
719 delcache(&mb
, &message
[rp
->rec_count
-1]);
720 memmove(&message
[rp
->rec_count
-1], &message
[rp
->rec_count
],
721 ((msgCount
- rp
->rec_count
+ 1) * sizeof *message
));
723 /* If the message was part of a collapsed thread,
724 * the m_collapsed field of one of its ancestors
725 * should be incremented. It seems hardly possible
726 * to do this with the current message structure,
727 * though. The result is that a '+' may be shown
728 * in the header summary even if no collapsed
740 record
= recend
= NULL
;
741 if (rv
== OKAY
&& UICMP(z
, exists
, >, msgCount
)) {
742 message
= srealloc(message
, (exists
+ 1) * sizeof *message
);
743 memset(&message
[msgCount
], 0, (exists
- msgCount
+ 1) * sizeof *message
);
744 for (i
= msgCount
; i
< exists
; ++i
)
746 imap_flags(&mb
, msgCount
+1, exists
);
765 for (rp
= record
; rp
!= NULL
;) {
766 struct record
*tmp
= rp
;
770 record
= recend
= NULL
;
778 sighandler_type
volatile saveint
, savepipe
;
779 NYD_X
; /* Signal handler */
782 if (imaplock
++ == 0) {
783 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
784 safe_signal(SIGINT
, &_imap_maincatch
);
785 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
786 if (sigsetjmp(imapjmp
, 1)) {
787 safe_signal(SIGINT
, saveint
);
788 safe_signal(SIGPIPE
, savepipe
);
791 if (savepipe
!= SIG_IGN
)
792 safe_signal(SIGPIPE
, imapcatch
);
793 if (imap_noop1(&mb
) != OKAY
) {
794 safe_signal(SIGINT
, saveint
);
795 safe_signal(SIGPIPE
, savepipe
);
798 safe_signal(SIGINT
, saveint
);
799 safe_signal(SIGPIPE
, savepipe
);
802 alarm(imapkeepalive
);
808 imap_use_starttls(const char *uhp
)
813 if (ok_blook(imap_use_starttls
))
816 char *var
= savecat("imap-use-starttls-", uhp
);
824 imap_preauth(struct mailbox
*mp
, const char *xserver
, const char *uhp
)
829 mp
->mb_active
|= MB_PREAUTH
;
831 if ((cp
= strchr(xserver
, ':')) != NULL
) {
832 char *x
= salloc(cp
- xserver
+ 1);
833 memcpy(x
, xserver
, cp
- xserver
);
834 x
[cp
- xserver
] = '\0';
838 if (mp
->mb_sock
.s_use_ssl
== 0 && imap_use_starttls(uhp
)) {
839 FILE *queuefp
= NULL
;
842 snprintf(o
, sizeof o
, "%s STARTTLS\r\n", tag(1));
843 IMAP_OUT(o
, MB_COMD
, return STOP
)
845 if (ssl_open(xserver
, &mp
->mb_sock
, uhp
) != OKAY
)
849 if (imap_use_starttls(uhp
)) {
850 fprintf(stderr
, "No SSL support compiled in.\n");
859 imap_capability(struct mailbox
*mp
)
862 FILE *queuefp
= NULL
;
867 snprintf(o
, sizeof o
, "%s CAPABILITY\r\n", tag(1));
868 IMAP_OUT(o
, MB_COMD
, return STOP
)
869 while (mp
->mb_active
& MB_COMD
) {
870 ok
= imap_answer(mp
, 0);
871 if (response_status
== RESPONSE_OTHER
&&
872 response_other
== CAPABILITY_DATA
) {
873 cp
= responded_other_text
;
875 while (spacechar(*cp
))
877 if (strncmp(cp
, "UIDPLUS", 7) == 0 && spacechar(cp
[7]))
879 mp
->mb_flags
|= MB_UIDPLUS
;
880 while (*cp
&& !spacechar(*cp
))
889 imap_auth(struct mailbox
*mp
, struct ccred
*ccred
)
894 if (!(mp
->mb_active
& MB_PREAUTH
)) {
899 switch (ccred
->cc_authtype
) {
901 rv
= imap_login(mp
, ccred
);
904 case AUTHTYPE_CRAM_MD5
:
905 rv
= imap_cram_md5(mp
, ccred
);
909 case AUTHTYPE_GSSAPI
:
910 rv
= _imap_gssapi(mp
, ccred
);
924 imap_cram_md5(struct mailbox
*mp
, struct ccred
*ccred
)
926 char o
[LINESIZE
], *cp
;
927 FILE *queuefp
= NULL
;
931 snprintf(o
, sizeof o
, "%s AUTHENTICATE CRAM-MD5\r\n", tag(1));
932 IMAP_XOUT(o
, 0, goto jleave
, goto jleave
);
934 if (response_type
!= RESPONSE_CONT
)
937 cp
= cram_md5_string(&ccred
->cc_user
, &ccred
->cc_pass
, responded_text
);
938 IMAP_XOUT(cp
, MB_COMD
, goto jleave
, goto jleave
);
939 while (mp
->mb_active
& MB_COMD
)
940 rv
= imap_answer(mp
, 1);
945 #endif /* HAVE_MD5 */
948 imap_login(struct mailbox
*mp
, struct ccred
*ccred
)
951 FILE *queuefp
= NULL
;
955 snprintf(o
, sizeof o
, "%s LOGIN %s %s\r\n",
956 tag(1), imap_quotestr(ccred
->cc_user
.s
), imap_quotestr(ccred
->cc_pass
.s
));
957 IMAP_XOUT(o
, MB_COMD
, goto jleave
, goto jleave
);
958 while (mp
->mb_active
& MB_COMD
)
959 rv
= imap_answer(mp
, 1);
966 # include "imap_gssapi.h"
970 imap_select(struct mailbox
*mp
, off_t
*size
, int *cnt
, const char *mbx
)
975 FILE *queuefp
= NULL
;
979 mp
->mb_uidvalidity
= 0;
980 snprintf(o
, sizeof o
, "%s SELECT %s\r\n", tag(1), imap_quotestr(mbx
));
981 IMAP_OUT(o
, MB_COMD
, return STOP
)
982 while (mp
->mb_active
& MB_COMD
) {
983 ok
= imap_answer(mp
, 1);
984 if (response_status
!= RESPONSE_OTHER
&&
985 (cp
= asccasestr(responded_text
, "[UIDVALIDITY ")) != NULL
)
986 mp
->mb_uidvalidity
= atol(&cp
[13]);
988 *cnt
= (had_exists
> 0) ? had_exists
: 0;
989 if (response_status
!= RESPONSE_OTHER
&&
990 ascncasecmp(responded_text
, "[READ-ONLY] ", 12) == 0)
996 imap_flags(struct mailbox
*mp
, unsigned X
, unsigned Y
)
999 FILE *queuefp
= NULL
;
1002 unsigned x
= X
, y
= Y
, n
;
1005 snprintf(o
, sizeof o
, "%s FETCH %u:%u (FLAGS UID)\r\n", tag(1), x
, y
);
1006 IMAP_OUT(o
, MB_COMD
, return STOP
)
1007 while (mp
->mb_active
& MB_COMD
) {
1009 if (response_status
== RESPONSE_OTHER
&&
1010 response_other
== MESSAGE_DATA_FETCH
) {
1011 n
= responded_other_number
;
1019 if ((cp
= asccasestr(responded_other_text
, "FLAGS ")) != NULL
) {
1024 imap_getflags(cp
, &cp
, &m
->m_flag
);
1027 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
)
1028 m
->m_uid
= strtoul(&cp
[4], NULL
, 10);
1029 getcache1(mp
, m
, NEED_UNSPEC
, 1);
1030 m
->m_flag
&= ~MHIDDEN
;
1033 while (x
<= y
&& message
[x
-1].m_xsize
&& message
[x
-1].m_time
)
1035 while (y
> x
&& message
[y
-1].m_xsize
&& message
[y
-1].m_time
)
1038 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.SIZE INTERNALDATE)\r\n",
1040 IMAP_OUT(o
, MB_COMD
, return STOP
)
1041 while (mp
->mb_active
& MB_COMD
) {
1043 if (response_status
== RESPONSE_OTHER
&&
1044 response_other
== MESSAGE_DATA_FETCH
) {
1045 n
= responded_other_number
;
1051 if ((cp
= asccasestr(responded_other_text
, "RFC822.SIZE ")) != NULL
)
1052 m
->m_xsize
= strtol(&cp
[12], NULL
, 10);
1053 if ((cp
= asccasestr(responded_other_text
, "INTERNALDATE ")) != NULL
)
1054 m
->m_time
= imap_read_date_time(&cp
[13]);
1058 for (n
= X
; n
<= Y
; n
++)
1059 putcache(mp
, &message
[n
-1]);
1064 imap_init(struct mailbox
*mp
, int n
)
1071 m
->m_flag
= MUSED
| MNOFROM
;
1078 imap_setptr(struct mailbox
*mp
, int nmail
, int transparent
, int *prevcount
)
1080 struct message
*omessage
= 0;
1081 int i
, omsgCount
= 0;
1082 enum okay dequeued
= STOP
;
1085 if (nmail
|| transparent
) {
1087 omsgCount
= msgCount
;
1090 dequeued
= rec_dequeue();
1092 if (had_exists
>= 0) {
1093 if (dequeued
!= OKAY
)
1094 msgCount
= had_exists
;
1097 if (had_expunge
>= 0) {
1098 if (dequeued
!= OKAY
)
1099 msgCount
-= had_expunge
;
1103 if (nmail
&& expunged_messages
)
1104 printf("Expunged %ld message%s.\n", expunged_messages
,
1105 (expunged_messages
!= 1 ? "s" : ""));
1106 *prevcount
= omsgCount
- expunged_messages
;
1107 expunged_messages
= 0;
1109 fputs("IMAP error: Negative message count\n", stderr
);
1113 if (dequeued
!= OKAY
) {
1114 message
= scalloc(msgCount
+ 1, sizeof *message
);
1115 for (i
= 0; i
< msgCount
; i
++)
1117 if (!nmail
&& mp
->mb_type
== MB_IMAP
)
1120 imap_flags(mp
, 1, msgCount
);
1121 message
[msgCount
].m_size
= 0;
1122 message
[msgCount
].m_lines
= 0;
1125 if (nmail
|| transparent
)
1126 transflags(omessage
, omsgCount
, transparent
);
1133 imap_setfile(const char *xserver
, int nmail
, int isedit
)
1139 if (!url_parse(&url
, CPROTO_IMAP
, xserver
)) {
1143 if (!ok_blook(v15_compat
) &&
1144 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
1145 fprintf(stderr
, "New-style URL used without *v15-compat* being set!\n");
1147 rv
= _imap_setfile1(&url
, nmail
, isedit
, 0);
1154 _imap_getcred(struct mailbox
*mbp
, struct ccred
*ccredp
, struct url
*urlp
)
1159 if (ok_blook(v15_compat
))
1160 rv
= ccred_lookup(ccredp
, urlp
);
1164 if ((var
= mbp
->mb_imap_pass
) != NULL
) {
1165 var
= savecat("password-", urlp
->url_u_h_p
.s
);
1166 old
= vok_vlook(var
);
1167 vok_vset(var
, mbp
->mb_imap_pass
);
1169 rv
= ccred_lookup_old(ccredp
, CPROTO_IMAP
, urlp
->url_u_h_p
.s
);
1183 _imap_setfile1(struct url
*urlp
, int nmail
, int isedit
,
1184 int volatile transparent
)
1188 sighandler_type
volatile saveint
, savepipe
;
1191 int volatile prevcount
= 0;
1192 enum mbflags same_flags
;
1196 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1197 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1198 if (saveint
!= SIG_IGN
)
1199 safe_signal(SIGINT
, imapcatch
);
1200 if (savepipe
!= SIG_IGN
)
1201 safe_signal(SIGPIPE
, imapcatch
);
1206 same_flags
= mb
.mb_flags
;
1207 same_imap_account
= 0;
1208 if (mb
.mb_imap_account
!= NULL
&&
1209 (mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
)) {
1210 if (mb
.mb_sock
.s_fd
> 0 && mb
.mb_sock
.s_rsz
>= 0 &&
1211 !strcmp(mb
.mb_imap_account
, urlp
->url_p_eu_h_p
) &&
1212 disconnected(mb
.mb_imap_account
) == 0) {
1213 same_imap_account
= 1;
1214 if (urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1216 } else if ((transparent
|| mb
.mb_type
== MB_CACHE
) &&
1217 !strcmp(mb
.mb_imap_account
, urlp
->url_p_eu_h_p
) &&
1218 urlp
->url_pass
.s
== NULL
&& mb
.mb_imap_pass
!= NULL
)
1220 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= savestr(mb
.mb_imap_pass
));
1223 if (!_imap_getcred(&mb
, &ccred
, urlp
)) {
1229 if (!same_imap_account
) {
1230 if (!disconnected(urlp
->url_p_eu_h_p
) && !sopen(&so
, urlp
)) {
1239 edit
= (isedit
!= 0);
1240 if (mb
.mb_imap_account
!= NULL
)
1241 free(mb
.mb_imap_account
);
1242 if (mb
.mb_imap_pass
!= NULL
)
1243 free(mb
.mb_imap_pass
);
1244 mb
.mb_imap_account
= sstrdup(urlp
->url_p_eu_h_p
);
1245 /* TODO This is a hack to allow '@boxname'; in the end everything will be an
1246 * TODO object, and mailbox will naturally have an URL and credentials */
1247 mb
.mb_imap_pass
= sbufdup(ccred
.cc_pass
.s
, ccred
.cc_pass
.l
);
1249 if (!same_imap_account
) {
1250 if (mb
.mb_sock
.s_fd
>= 0)
1251 sclose(&mb
.mb_sock
);
1253 same_imap_account
= 0;
1264 if (mb
.mb_imap_mailbox
!= NULL
)
1265 free(mb
.mb_imap_mailbox
);
1266 mb
.mb_imap_mailbox
= sstrdup((urlp
->url_path
.s
!= NULL
)
1267 ? urlp
->url_path
.s
: "INBOX");
1268 initbox(urlp
->url_p_eu_h_p_p
);
1270 mb
.mb_type
= MB_VOID
;
1271 mb
.mb_active
= MB_NONE
;
1274 saveint
= safe_signal(SIGINT
, SIG_IGN
);
1275 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1276 if (sigsetjmp(imapjmp
, 1)) {
1277 /* Not safe to use &so; save to use mb.mb_sock?? :-( TODO */
1278 sclose(&mb
.mb_sock
);
1279 safe_signal(SIGINT
, saveint
);
1280 safe_signal(SIGPIPE
, savepipe
);
1283 mb
.mb_type
= MB_VOID
;
1284 mb
.mb_active
= MB_NONE
;
1288 if (saveint
!= SIG_IGN
)
1289 safe_signal(SIGINT
, imapcatch
);
1290 if (savepipe
!= SIG_IGN
)
1291 safe_signal(SIGPIPE
, imapcatch
);
1293 if (mb
.mb_sock
.s_fd
< 0) {
1294 if (disconnected(mb
.mb_imap_account
)) {
1295 if (cache_setptr(transparent
) == STOP
)
1296 fprintf(stderr
, "Mailbox \"%s\" is not cached.\n",
1297 urlp
->url_p_eu_h_p_p
);
1300 if ((cp
= ok_vlook(imap_keepalive
)) != NULL
) {
1301 if ((imapkeepalive
= strtol(cp
, NULL
, 10)) > 0) {
1302 savealrm
= safe_signal(SIGALRM
, imapalarm
);
1303 alarm(imapkeepalive
);
1308 mb
.mb_sock
.s_desc
= "IMAP";
1309 mb
.mb_sock
.s_onclose
= imap_timer_off
;
1310 if (imap_preauth(&mb
, urlp
->url_h_p
.s
, urlp
->url_u_h_p
.s
) != OKAY
||
1311 imap_auth(&mb
, &ccred
) != OKAY
) {
1312 sclose(&mb
.mb_sock
);
1314 safe_signal(SIGINT
, saveint
);
1315 safe_signal(SIGPIPE
, savepipe
);
1320 } else /* same account */
1321 mb
.mb_flags
|= same_flags
;
1323 mb
.mb_perm
= (options
& OPT_R_FLAG
) ? 0 : MB_DELE
;
1324 mb
.mb_type
= MB_IMAP
;
1326 if (imap_select(&mb
, &mailsize
, &msgCount
,
1327 (urlp
->url_path
.s
!= NULL
? urlp
->url_path
.s
: "INBOX")) != OKAY
) {
1328 /*sclose(&mb.mb_sock);
1330 safe_signal(SIGINT
, saveint
);
1331 safe_signal(SIGPIPE
, savepipe
);
1333 mb
.mb_type
= MB_VOID
;
1339 imap_setptr(&mb
, nmail
, transparent
, UNVOLATILE(&prevcount
));
1342 if (!nmail
&& !transparent
)
1344 safe_signal(SIGINT
, saveint
);
1345 safe_signal(SIGPIPE
, savepipe
);
1348 if (!nmail
&& mb
.mb_type
== MB_IMAP
)
1349 purgecache(&mb
, message
, msgCount
);
1350 if ((nmail
|| transparent
) && mb
.mb_sorted
) {
1355 if (!nmail
&& !edit
&& msgCount
== 0) {
1356 if ((mb
.mb_type
== MB_IMAP
|| mb
.mb_type
== MB_CACHE
) &&
1357 !ok_blook(emptystart
))
1358 fprintf(stderr
, _("No mail at %s\n"), urlp
->url_p_eu_h_p_p
);
1363 newmailinfo(prevcount
);
1371 imap_fetchdata(struct mailbox
*mp
, struct message
*m
, size_t expected
,
1372 int need
, const char *head
, size_t headsize
, long headlines
)
1374 char *line
= NULL
, *lp
;
1375 size_t linesize
= 0, linelen
, size
= 0;
1376 int emptyline
= 0, lines
= 0, excess
= 0;
1380 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1381 offset
= ftell(mp
->mb_otf
);
1384 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1386 while (sgetline(&line
, &linesize
, &linelen
, &mp
->mb_sock
) > 0) {
1388 if (linelen
> expected
) {
1389 excess
= linelen
- expected
;
1393 * Need to mask 'From ' lines. This cannot be done properly
1394 * since some servers pass them as 'From ' and others as
1395 * '>From '. Although one could identify the first kind of
1396 * server in principle, it is not possible to identify the
1397 * second as '>From ' may also come from a server of the
1398 * first type as actual data. So do what is absolutely
1399 * necessary only - mask 'From '.
1401 * If the line is the first line of the message header, it
1402 * is likely a real 'From ' line. In this case, it is just
1403 * ignored since it violates all standards.
1404 * TODO can the latter *really* happen??
1407 /* Since we simply copy over data without doing any transfer
1408 * encoding reclassification/adjustment we *have* to perform
1409 * RFC 4155 compliant From_ quoting here */
1410 if (is_head(lp
, linelen
)) {
1411 if (lines
+ headlines
== 0)
1413 fputc('>', mp
->mb_otf
);
1416 if (lp
[linelen
-1] == '\n' && (linelen
== 1 || lp
[linelen
-2] == '\r')) {
1417 emptyline
= linelen
<= 2;
1419 fwrite(lp
, 1, linelen
- 2, mp
->mb_otf
);
1420 size
+= linelen
- 1;
1423 fputc('\n', mp
->mb_otf
);
1426 fwrite(lp
, 1, linelen
, mp
->mb_otf
);
1431 if ((expected
-= linelen
) <= 0)
1435 /* This is very ugly; but some IMAP daemons don't end a
1436 * message with \r\n\r\n, and we need \n\n for mbox format */
1437 fputc('\n', mp
->mb_otf
);
1444 m
->m_size
= size
+ headsize
;
1445 m
->m_lines
= lines
+ headlines
;
1446 m
->m_block
= mailx_blockof(offset
);
1447 m
->m_offset
= mailx_offsetof(offset
);
1450 m
->m_have
|= HAVE_HEADER
;
1453 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1454 m
->m_xlines
= m
->m_lines
;
1455 m
->m_xsize
= m
->m_size
;
1465 imap_putstr(struct mailbox
*mp
, struct message
*m
, const char *str
,
1466 const char *head
, size_t headsize
, long headlines
)
1473 fseek(mp
->mb_otf
, 0L, SEEK_END
);
1474 offset
= ftell(mp
->mb_otf
);
1476 fwrite(head
, 1, headsize
, mp
->mb_otf
);
1478 fwrite(str
, 1, len
, mp
->mb_otf
);
1479 fputc('\n', mp
->mb_otf
);
1485 m
->m_size
= headsize
+ len
;
1486 m
->m_lines
= headlines
+ 1;
1487 m
->m_block
= mailx_blockof(offset
);
1488 m
->m_offset
= mailx_offsetof(offset
);
1489 m
->m_have
|= HAVE_HEADER
| HAVE_BODY
;
1490 m
->m_xlines
= m
->m_lines
;
1491 m
->m_xsize
= m
->m_size
;
1497 imap_get(struct mailbox
*mp
, struct message
*m
, enum needspec need
)
1501 sighandler_type
volatile saveint
, savepipe
;
1502 char * volatile head
;
1503 char const *cp
, *loc
, * volatile item
, * volatile resp
;
1505 size_t volatile headsize
;
1508 long volatile headlines
;
1514 saveint
= savepipe
= SIG_IGN
;
1516 cp
= loc
= item
= resp
= NULL
;
1518 number
= (int)PTR2SIZE(m
- message
+ 1);
1525 if (getcache(mp
, m
, need
) == OKAY
)
1527 if (mp
->mb_type
== MB_CACHE
) {
1528 fprintf(stderr
, "Message %u not available.\n", number
);
1532 if (mp
->mb_sock
.s_fd
< 0) {
1533 fprintf(stderr
, "IMAP connection closed.\n");
1539 resp
= item
= "RFC822.HEADER";
1542 item
= "BODY.PEEK[]";
1544 if (m
->m_flag
& HAVE_HEADER
&& m
->m_size
) {
1545 char *hdr
= smalloc(m
->m_size
);
1547 if (fseek(mp
->mb_itf
, (long)mailx_positionof(m
->m_block
, m
->m_offset
),
1549 fread(hdr
, 1, m
->m_size
, mp
->mb_itf
) != m
->m_size
) {
1554 headsize
= m
->m_size
;
1555 headlines
= m
->m_lines
;
1556 item
= "BODY.PEEK[TEXT]";
1557 resp
= "BODY[TEXT]";
1565 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1566 if (sigsetjmp(imapjmp
, 1)) {
1567 safe_signal(SIGINT
, saveint
);
1568 safe_signal(SIGPIPE
, savepipe
);
1572 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1573 safe_signal(SIGINT
, &_imap_maincatch
);
1574 if (savepipe
!= SIG_IGN
)
1575 safe_signal(SIGPIPE
, imapcatch
);
1578 snprintf(o
, sizeof o
, "%s UID FETCH %lu (%s)\r\n",
1579 tag(1), m
->m_uid
, item
);
1581 if (check_expunged() == STOP
)
1583 snprintf(o
, sizeof o
, "%s FETCH %u (%s)\r\n", tag(1), number
, item
);
1585 IMAP_OUT(o
, MB_COMD
, goto out
)
1587 ok
= imap_answer(mp
, 1);
1590 if (response_status
!= RESPONSE_OTHER
||
1591 response_other
!= MESSAGE_DATA_FETCH
)
1593 if ((loc
= asccasestr(responded_other_text
, resp
)) == NULL
)
1596 if ((cp
= asccasestr(responded_other_text
, "UID "))) {
1604 n
= responded_other_number
;
1605 if ((cp
= strrchr(responded_other_text
, '{')) == NULL
) {
1606 if (m
->m_uid
? m
->m_uid
!= u
: n
!= number
)
1608 if ((cp
= strchr(loc
, '"')) != NULL
) {
1609 cp
= imap_unquotestr(cp
);
1610 imap_putstr(mp
, m
, cp
, head
, headsize
, headlines
);
1612 m
->m_have
|= HAVE_HEADER
|HAVE_BODY
;
1613 m
->m_xlines
= m
->m_lines
;
1614 m
->m_xsize
= m
->m_size
;
1618 expected
= atol(&cp
[1]);
1619 if (m
->m_uid
? n
== 0 && m
->m_uid
!= u
: n
!= number
) {
1620 imap_fetchdata(mp
, NULL
, expected
, need
, NULL
, 0, 0);
1624 imap_fetchdata(mp
, &mt
, expected
, need
, head
, headsize
, headlines
);
1626 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1629 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1630 if (options
& OPT_VERBVERB
)
1631 fputs(imapbuf
, stderr
);
1632 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1634 if (u
== m
->m_uid
) {
1635 commitmsg(mp
, m
, &mt
, mt
.m_have
);
1642 while (mp
->mb_active
& MB_COMD
)
1643 ok
= imap_answer(mp
, 1);
1645 if (saveint
!= SIG_IGN
)
1646 safe_signal(SIGINT
, saveint
);
1647 if (savepipe
!= SIG_IGN
)
1648 safe_signal(SIGPIPE
, savepipe
);
1661 imap_header(struct message
*m
)
1666 rv
= imap_get(&mb
, m
, NEED_HEADER
);
1673 imap_body(struct message
*m
)
1678 rv
= imap_get(&mb
, m
, NEED_BODY
);
1684 commitmsg(struct mailbox
*mp
, struct message
*tomp
, struct message
*frommp
,
1688 tomp
->m_size
= frommp
->m_size
;
1689 tomp
->m_lines
= frommp
->m_lines
;
1690 tomp
->m_block
= frommp
->m_block
;
1691 tomp
->m_offset
= frommp
->m_offset
;
1692 tomp
->m_have
= have
;
1693 if (have
& HAVE_BODY
) {
1694 tomp
->m_xlines
= frommp
->m_lines
;
1695 tomp
->m_xsize
= frommp
->m_size
;
1702 imap_fetchheaders(struct mailbox
*mp
, struct message
*m
, int bot
, int topp
)
1710 FILE *queuefp
= NULL
;
1715 snprintf(o
, sizeof o
, "%s UID FETCH %lu:%lu (RFC822.HEADER)\r\n",
1716 tag(1), m
[bot
-1].m_uid
, m
[topp
-1].m_uid
);
1718 if (check_expunged() == STOP
)
1720 snprintf(o
, sizeof o
, "%s FETCH %u:%u (RFC822.HEADER)\r\n",
1723 IMAP_OUT(o
, MB_COMD
, return STOP
)
1725 ok
= imap_answer(mp
, 1);
1726 if (response_status
!= RESPONSE_OTHER
)
1728 if (response_other
!= MESSAGE_DATA_FETCH
)
1730 if (ok
== STOP
|| (cp
=strrchr(responded_other_text
, '{')) == 0)
1732 if (asccasestr(responded_other_text
, "RFC822.HEADER") == NULL
)
1734 expected
= atol(&cp
[1]);
1735 if (m
[bot
-1].m_uid
) {
1736 if ((cp
= asccasestr(responded_other_text
, "UID ")) != NULL
) {
1738 for (n
= bot
; n
<= topp
; n
++)
1739 if ((unsigned long)u
== m
[n
-1].m_uid
)
1742 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1748 n
= responded_other_number
;
1749 if (n
<= 0 || n
> msgCount
) {
1750 imap_fetchdata(mp
, NULL
, expected
, NEED_HEADER
, NULL
, 0, 0);
1754 imap_fetchdata(mp
, &mt
, expected
, NEED_HEADER
, NULL
, 0, 0);
1755 if (n
>= 0 && !(m
[n
-1].m_have
& HAVE_HEADER
))
1756 commitmsg(mp
, &m
[n
-1], &mt
, HAVE_HEADER
);
1757 if (n
== -1 && sgetline(&imapbuf
, &imapbufsize
, NULL
, &mp
->mb_sock
) > 0) {
1758 if (options
& OPT_VERBVERB
)
1759 fputs(imapbuf
, stderr
);
1760 if ((cp
= asccasestr(imapbuf
, "UID ")) != NULL
) {
1762 for (n
= bot
; n
<= topp
; n
++)
1763 if ((unsigned long)u
== m
[n
-1].m_uid
)
1765 if (n
<= topp
&& !(m
[n
-1].m_have
& HAVE_HEADER
))
1766 commitmsg(mp
, &m
[n
-1], &mt
,HAVE_HEADER
);
1771 while (mp
->mb_active
& MB_COMD
)
1772 ok
= imap_answer(mp
, 1);
1777 imap_getheaders(int volatile bot
, int volatile topp
) /* TODO iterator!! */
1779 sighandler_type saveint
, savepipe
;
1780 /* enum okay ok = STOP;*/
1784 if (mb
.mb_type
== MB_CACHE
)
1788 if (topp
> msgCount
)
1790 for (i
= bot
; i
< topp
; i
++) {
1791 if (message
[i
-1].m_have
& HAVE_HEADER
||
1792 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1797 for (i
= topp
; i
> bot
; i
--) {
1798 if (message
[i
-1].m_have
& HAVE_HEADER
||
1799 getcache(&mb
, &message
[i
-1], NEED_HEADER
) == OKAY
)
1808 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
1809 safe_signal(SIGINT
, &_imap_maincatch
);
1810 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
1811 if (sigsetjmp(imapjmp
, 1) == 0) {
1812 if (savepipe
!= SIG_IGN
)
1813 safe_signal(SIGPIPE
, imapcatch
);
1815 for (i
= bot
; i
<= topp
; i
+= chunk
) {
1816 int j
= i
+ chunk
- 1;
1818 if (visible(message
+ j
))
1819 /*ok = */imap_fetchheaders(&mb
, message
, i
, j
);
1821 onintr(0); /* XXX imaplock? */
1824 safe_signal(SIGINT
, saveint
);
1825 safe_signal(SIGPIPE
, savepipe
);
1830 __imap_exit(struct mailbox
*mp
)
1833 FILE *queuefp
= NULL
;
1836 mp
->mb_active
|= MB_BYE
;
1837 snprintf(o
, sizeof o
, "%s LOGOUT\r\n", tag(1));
1838 IMAP_OUT(o
, MB_COMD
, return STOP
)
1844 imap_exit(struct mailbox
*mp
)
1849 rv
= __imap_exit(mp
);
1850 #if 0 /* TODO the option today: memory leak(s) and halfway reuse or nottin */
1851 free(mp
->mb_imap_pass
);
1852 free(mp
->mb_imap_account
);
1853 free(mp
->mb_imap_mailbox
);
1854 if (mp
->mb_cache_directory
!= NULL
)
1855 free(mp
->mb_cache_directory
);
1856 #ifndef HAVE_DEBUG /* TODO ASSERT LEGACY */
1857 mp
->mb_imap_account
=
1858 mp
->mb_imap_mailbox
=
1859 mp
->mb_cache_directory
= "";
1861 mp
->mb_imap_account
= NULL
; /* for assert legacy time.. */
1862 mp
->mb_imap_mailbox
= NULL
;
1863 mp
->mb_cache_directory
= NULL
;
1866 sclose(&mp
->mb_sock
);
1872 imap_delete(struct mailbox
*mp
, int n
, struct message
*m
, int needstat
)
1875 imap_store(mp
, m
, n
, '+', "\\Deleted", needstat
);
1876 if (mp
->mb_type
== MB_IMAP
)
1883 imap_close(struct mailbox
*mp
)
1886 FILE *queuefp
= NULL
;
1889 snprintf(o
, sizeof o
, "%s CLOSE\r\n", tag(1));
1890 IMAP_OUT(o
, MB_COMD
, return STOP
)
1896 imap_update(struct mailbox
*mp
)
1899 int dodel
, c
, gotcha
= 0, held
= 0, modflags
= 0, needstat
, stored
= 0;
1902 if (!edit
&& mp
->mb_perm
!= 0) {
1905 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1906 if (m
->m_flag
& MBOX
)
1909 if (makembox() == STOP
)
1914 for (m
= message
; PTRCMP(m
, <, message
+ msgCount
); ++m
) {
1915 if (mp
->mb_perm
== 0)
1918 dodel
= ((m
->m_flag
& MDELETED
) != 0);
1920 dodel
= !((m
->m_flag
& MPRESERVE
) || !(m
->m_flag
& MTOUCH
));
1922 /* Fetch the result after around each 800 STORE commands
1923 * sent (approx. 32k data sent). Otherwise, servers will
1924 * try to flush the return queue at some point, leading
1925 * to a deadlock if we are still writing commands but not
1926 * reading their results */
1927 needstat
= stored
> 0 && stored
% 800 == 0;
1928 /* Even if this message has been deleted, continue
1929 * to set further flags. This is necessary to support
1930 * Gmail semantics, where "delete" actually means
1931 * "archive", and the flags are applied to the copy
1933 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
1934 imap_store(mp
, m
, m
-message
+1, '+', "\\Seen", needstat
);
1937 if (m
->m_flag
& MFLAG
) {
1938 imap_store(mp
, m
, m
-message
+1, '+', "\\Flagged", needstat
);
1941 if (m
->m_flag
& MUNFLAG
) {
1942 imap_store(mp
, m
, m
-message
+1, '-', "\\Flagged", needstat
);
1945 if (m
->m_flag
& MANSWER
) {
1946 imap_store(mp
, m
, m
-message
+1, '+', "\\Answered", needstat
);
1949 if (m
->m_flag
& MUNANSWER
) {
1950 imap_store(mp
, m
, m
-message
+1, '-', "\\Answered", needstat
);
1953 if (m
->m_flag
& MDRAFT
) {
1954 imap_store(mp
, m
, m
-message
+1, '+', "\\Draft", needstat
);
1957 if (m
->m_flag
& MUNDRAFT
) {
1958 imap_store(mp
, m
, m
-message
+1, '-', "\\Draft", needstat
);
1963 imap_delete(mp
, m
-message
+1, m
, needstat
);
1966 } else if (mp
->mb_type
!= MB_CACHE
||
1967 (!edit
&& !(m
->m_flag
& (MBOXED
| MSAVED
| MDELETED
))) ||
1968 (m
->m_flag
& (MBOXED
| MPRESERVE
| MTOUCH
)) ==
1969 (MPRESERVE
| MTOUCH
) || (edit
&& !(m
->m_flag
& MDELETED
)))
1971 if (m
->m_flag
& MNEW
) {
1973 m
->m_flag
|= MSTATUS
;
1980 for (m
= &message
[0]; PTRCMP(m
, <, message
+ msgCount
); ++m
)
1981 if (!(m
->m_flag
& MUNLINKED
) &&
1982 m
->m_flag
& (MBOXED
| MDELETED
| MSAVED
| MSTATUS
| MFLAG
|
1983 MUNFLAG
| MANSWER
| MUNANSWER
| MDRAFT
| MUNDRAFT
)) {
1987 if ((gotcha
|| modflags
) && edit
) {
1988 printf(_("\"%s\" "), displayname
);
1989 printf((ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1990 ? _("complete\n") : _("updated.\n"));
1991 } else if (held
&& !edit
&& mp
->mb_perm
!= 0) {
1993 printf(_("Held 1 message in %s\n"), displayname
);
1995 printf(_("Held %d messages in %s\n"), held
, displayname
);
2005 sighandler_type
volatile saveint
, savepipe
;
2008 if (mb
.mb_type
== MB_CACHE
) {
2013 if (mb
.mb_sock
.s_fd
< 0) {
2014 fprintf(stderr
, "IMAP connection closed.\n");
2019 saveint
= safe_signal(SIGINT
, SIG_IGN
);
2020 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2021 if (sigsetjmp(imapjmp
, 1)) {
2022 safe_signal(SIGINT
, saveint
);
2023 safe_signal(SIGPIPE
, saveint
);
2027 if (saveint
!= SIG_IGN
)
2028 safe_signal(SIGINT
, imapcatch
);
2029 if (savepipe
!= SIG_IGN
)
2030 safe_signal(SIGPIPE
, imapcatch
);
2033 if (!same_imap_account
)
2036 safe_signal(SIGINT
, saveint
);
2037 safe_signal(SIGPIPE
, savepipe
);
2044 imap_store(struct mailbox
*mp
, struct message
*m
, int n
, int c
, const char *sp
,
2048 FILE *queuefp
= NULL
;
2051 if (mp
->mb_type
== MB_CACHE
&& (queuefp
= cache_queue(mp
)) == NULL
)
2054 snprintf(o
, sizeof o
, "%s UID STORE %lu %cFLAGS (%s)\r\n",
2055 tag(1), m
->m_uid
, c
, sp
);
2057 if (check_expunged() == STOP
)
2059 snprintf(o
, sizeof o
, "%s STORE %u %cFLAGS (%s)\r\n", tag(1), n
, c
, sp
);
2061 IMAP_OUT(o
, MB_COMD
, return STOP
)
2065 mb
.mb_active
&= ~MB_COMD
;
2066 if (queuefp
!= NULL
)
2072 imap_undelete(struct message
*m
, int n
)
2077 rv
= imap_unstore(m
, n
, "\\Deleted");
2083 imap_unread(struct message
*m
, int n
)
2088 rv
= imap_unstore(m
, n
, "\\Seen");
2094 imap_unstore(struct message
*m
, int n
, const char *flag
)
2096 sighandler_type saveint
, savepipe
;
2097 enum okay rv
= STOP
;
2101 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2102 safe_signal(SIGINT
, &_imap_maincatch
);
2103 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2104 if (sigsetjmp(imapjmp
, 1) == 0) {
2105 if (savepipe
!= SIG_IGN
)
2106 safe_signal(SIGPIPE
, imapcatch
);
2108 rv
= imap_store(&mb
, m
, n
, '-', flag
, 1);
2110 safe_signal(SIGINT
, saveint
);
2111 safe_signal(SIGPIPE
, savepipe
);
2129 snprintf(ts
, sizeof ts
, "T%lu", n
);
2135 c_imap_imap(void *vp
)
2138 sighandler_type saveint
, savepipe
;
2139 struct mailbox
*mp
= &mb
;
2140 FILE *queuefp
= NULL
;
2141 enum okay
volatile ok
= STOP
;
2144 if (mp
->mb_type
!= MB_IMAP
) {
2145 printf("Not operating on an IMAP mailbox.\n");
2149 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2150 safe_signal(SIGINT
, &_imap_maincatch
);
2151 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2152 if (sigsetjmp(imapjmp
, 1) == 0) {
2153 if (savepipe
!= SIG_IGN
)
2154 safe_signal(SIGPIPE
, imapcatch
);
2156 snprintf(o
, sizeof o
, "%s %s\r\n", tag(1), (char *)vp
);
2157 IMAP_OUT(o
, MB_COMD
, goto out
)
2158 while (mp
->mb_active
& MB_COMD
) {
2159 ok
= imap_answer(mp
, 0);
2160 fputs(responded_text
, stdout
);
2164 safe_signal(SIGINT
, saveint
);
2165 safe_signal(SIGPIPE
, savepipe
);
2174 imap_newmail(int nmail
)
2178 if (nmail
&& had_exists
< 0 && had_expunge
< 0) {
2184 if (had_exists
== msgCount
&& had_expunge
< 0)
2185 /* Some servers always respond with EXISTS to NOOP. If
2186 * the mailbox has been changed but the number of messages
2187 * has not, an EXPUNGE must also had been sent; otherwise,
2188 * nothing has changed */
2191 return (had_expunge
>= 0 ? 2 : (had_exists
>= 0 ? 1 : 0));
2195 imap_putflags(int f
)
2201 bp
= buf
= salloc(100);
2202 if (f
& (MREAD
| MFLAGGED
| MANSWERED
| MDRAFTED
)) {
2207 for (cp
= "\\Seen"; *cp
; cp
++)
2213 for (cp
= "\\Flagged"; *cp
; cp
++)
2216 if (f
& MANSWERED
) {
2219 for (cp
= "\\Answered"; *cp
; cp
++)
2225 for (cp
= "\\Draft"; *cp
; cp
++)
2237 imap_getflags(const char *cp
, char const **xp
, enum mflag
*f
)
2240 while (*cp
!= ')') {
2242 if (ascncasecmp(cp
, "\\Seen", 5) == 0)
2244 else if (ascncasecmp(cp
, "\\Recent", 7) == 0)
2246 else if (ascncasecmp(cp
, "\\Deleted", 8) == 0)
2248 else if (ascncasecmp(cp
, "\\Flagged", 8) == 0)
2250 else if (ascncasecmp(cp
, "\\Answered", 9) == 0)
2252 else if (ascncasecmp(cp
, "\\Draft", 6) == 0)
2264 imap_append1(struct mailbox
*mp
, const char *name
, FILE *fp
, off_t off1
,
2265 long xsize
, enum mflag flag
, time_t t
)
2267 char o
[LINESIZE
], *buf
;
2268 size_t bufsize
, buflen
, cnt
;
2269 long size
, lines
, ysize
;
2271 FILE *queuefp
= NULL
;
2275 if (mp
->mb_type
== MB_CACHE
) {
2276 queuefp
= cache_queue(mp
);
2277 if (queuefp
== NULL
) {
2286 buf
= smalloc(bufsize
= LINESIZE
);
2291 if (fseek(fp
, off1
, SEEK_SET
) < 0) {
2296 snprintf(o
, sizeof o
, "%s APPEND %s %s%s {%ld}\r\n",
2297 tag(1), imap_quotestr(name
), imap_putflags(flag
),
2298 imap_make_date_time(t
), size
);
2299 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2300 while (mp
->mb_active
& MB_COMD
) {
2301 rv
= imap_answer(mp
, twice
);
2302 if (response_type
== RESPONSE_CONT
)
2306 if (mp
->mb_type
!= MB_CACHE
&& rv
== STOP
) {
2315 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2318 buf
[buflen
- 1] = '\r';
2320 if (mp
->mb_type
!= MB_CACHE
)
2321 swrite1(&mp
->mb_sock
, buf
, buflen
+1, 1);
2323 fwrite(buf
, 1, buflen
+1, queuefp
);
2326 if (mp
->mb_type
!= MB_CACHE
)
2327 swrite(&mp
->mb_sock
, "\r\n");
2329 fputs("\r\n", queuefp
);
2330 while (mp
->mb_active
& MB_COMD
) {
2331 rv
= imap_answer(mp
, 0);
2332 if (response_status
== RESPONSE_NO
/*&&
2333 ascncasecmp(responded_text,
2334 "[TRYCREATE] ", 12) == 0*/) {
2340 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), imap_quotestr(name
));
2341 IMAP_XOUT(o
, MB_COMD
, goto jleave
, rv
=STOP
;goto jleave
)
2342 while (mp
->mb_active
& MB_COMD
)
2343 rv
= imap_answer(mp
, 1);
2346 imap_created_mailbox
++;
2348 } else if (rv
!= OKAY
)
2349 fprintf(stderr
, _("IMAP error: %s"), responded_text
);
2350 else if (response_status
== RESPONSE_OK
&& (mp
->mb_flags
& MB_UIDPLUS
))
2351 imap_appenduid(mp
, fp
, t
, off1
, xsize
, ysize
, lines
, flag
, name
);
2354 if (queuefp
!= NULL
)
2363 imap_append0(struct mailbox
*mp
, const char *name
, FILE *fp
)
2365 char *buf
, *bp
, *lp
;
2366 size_t bufsize
, buflen
, cnt
;
2367 off_t off1
= -1, offs
;
2368 int inhead
= 1, flag
= MNEW
| MNEWEST
;
2374 buf
= smalloc(bufsize
= LINESIZE
);
2381 bp
= fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 1);
2382 if (bp
== NULL
|| strncmp(buf
, "From ", 5) == 0) {
2383 if (off1
!= (off_t
)-1) {
2384 rv
= imap_append1(mp
, name
, fp
, off1
, size
, flag
, tim
);
2387 fseek(fp
, offs
+buflen
, SEEK_SET
);
2389 off1
= offs
+ buflen
;
2394 tim
= unixtime(buf
);
2399 if (bp
&& buf
[0] == '\n')
2401 else if (bp
&& inhead
&& ascncasecmp(buf
, "status", 6) == 0) {
2403 while (whitechar(*lp
))
2406 while (*++lp
!= '\0')
2415 } else if (bp
&& inhead
&& ascncasecmp(buf
, "x-status", 8) == 0) {
2417 while (whitechar(*lp
))
2420 while (*++lp
!= '\0')
2433 } while (bp
!= NULL
);
2442 imap_append(const char *xserver
, FILE *fp
)
2444 sighandler_type
volatile saveint
, savepipe
;
2447 char const * volatile mbx
;
2448 enum okay rv
= STOP
;
2451 if (!url_parse(&url
, CPROTO_IMAP
, xserver
))
2453 if (!ok_blook(v15_compat
) &&
2454 (!url
.url_had_user
|| url
.url_pass
.s
!= NULL
))
2455 fprintf(stderr
, "New-style URL used without *v15-compat* being set!\n");
2456 mbx
= (url
.url_path
.s
!= NULL
) ? url
.url_path
.s
: "INBOX";
2459 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2460 safe_signal(SIGINT
, &_imap_maincatch
);
2461 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2462 if (sigsetjmp(imapjmp
, 1))
2464 if (savepipe
!= SIG_IGN
)
2465 safe_signal(SIGPIPE
, imapcatch
);
2467 if ((mb
.mb_type
== MB_CACHE
|| mb
.mb_sock
.s_fd
> 0) && mb
.mb_imap_account
&&
2468 !strcmp(url
.url_p_eu_h_p
, mb
.mb_imap_account
)) {
2469 rv
= imap_append0(&mb
, mbx
, fp
);
2473 memset(&mx
, 0, sizeof mx
);
2475 if (!_imap_getcred(&mx
, &ccred
, &url
))
2478 if (disconnected(url
.url_p_eu_h_p
) == 0) {
2479 if (!sopen(&mx
.mb_sock
, &url
))
2481 mx
.mb_sock
.s_desc
= "IMAP";
2482 mx
.mb_type
= MB_IMAP
;
2483 mx
.mb_imap_account
= UNCONST(url
.url_p_eu_h_p
);
2484 /* TODO the code now did
2485 * TODO mx.mb_imap_mailbox = mbx;
2486 * TODO though imap_mailbox is sfree()d and mbx
2487 * TODO is possibly even a constant
2488 * TODO i changed this to sstrdup() sofar, as is used
2489 * TODO somewhere else in this file for this! */
2490 mx
.mb_imap_mailbox
= sstrdup(mbx
);
2491 if (imap_preauth(&mx
, url
.url_h_p
.s
, url
.url_u_h_p
.s
) != OKAY
||
2492 imap_auth(&mx
, &ccred
) != OKAY
) {
2493 sclose(&mx
.mb_sock
);
2496 rv
= imap_append0(&mx
, mbx
, fp
);
2499 mx
.mb_imap_account
= UNCONST(url
.url_p_eu_h_p
);
2500 mx
.mb_imap_mailbox
= sstrdup(mbx
); /* TODO as above */
2501 mx
.mb_type
= MB_CACHE
;
2502 rv
= imap_append0(&mx
, mbx
, fp
);
2509 safe_signal(SIGINT
, saveint
);
2510 safe_signal(SIGPIPE
, savepipe
);
2520 imap_list1(struct mailbox
*mp
, const char *base
, struct list_item
**list
,
2521 struct list_item
**lend
, int level
)
2523 char o
[LINESIZE
], *cp
;
2525 FILE *queuefp
= NULL
;
2526 struct list_item
*lp
;
2527 enum okay ok
= STOP
;
2530 *list
= *lend
= NULL
;
2531 snprintf(o
, sizeof o
, "%s LIST %s %%\r\n", tag(1), imap_quotestr(base
));
2532 IMAP_OUT(o
, MB_COMD
, return STOP
)
2533 while (mp
->mb_active
& MB_COMD
) {
2534 ok
= imap_answer(mp
, 1);
2535 if (response_status
== RESPONSE_OTHER
&&
2536 response_other
== MAILBOX_DATA_LIST
&& imap_parse_list() == OKAY
) {
2537 cp
= imap_unquotestr(list_name
);
2538 lp
= csalloc(1, sizeof *lp
);
2540 for (bp
= base
; *bp
!= '\0' && *bp
== *cp
; ++bp
)
2542 lp
->l_base
= *cp
? cp
: savestr(base
);
2543 lp
->l_attr
= list_attributes
;
2544 lp
->l_level
= level
+1;
2545 lp
->l_delim
= list_hierarchy_delimiter
;
2546 if (*list
&& *lend
) {
2547 (*lend
)->l_next
= lp
;
2557 imap_list(struct mailbox
*mp
, const char *base
, int strip
, FILE *fp
)
2559 struct list_item
*list
, *lend
, *lp
, *lx
, *ly
;
2566 depth
= (cp
= ok_vlook(imap_list_depth
)) != NULL
? atoi(cp
) : 2;
2567 if ((rv
= imap_list1(mp
, base
, &list
, &lend
, 0)) == STOP
)
2570 if (list
== NULL
|| lend
== NULL
)
2573 for (lp
= list
; lp
; lp
= lp
->l_next
)
2574 if (lp
->l_delim
!= '/' && lp
->l_delim
!= EOF
&& lp
->l_level
< depth
&&
2575 !(lp
->l_attr
& LIST_NOINFERIORS
)) {
2576 cp
= salloc((n
= strlen(lp
->l_name
)) + 2);
2577 memcpy(cp
, lp
->l_name
, n
);
2578 cp
[n
] = lp
->l_delim
;
2580 if (imap_list1(mp
, cp
, &lx
, &ly
, lp
->l_level
) == OKAY
&& lx
&& ly
) {
2581 lp
->l_has_children
= 1;
2582 if (strcmp(cp
, lx
->l_name
) == 0)
2591 for (lp
= list
; lp
; lp
= lp
->l_next
) {
2594 for (bp
= base
; *bp
&& *bp
== *cp
; bp
++)
2598 if (!(lp
->l_attr
& LIST_NOSELECT
))
2599 fprintf(fp
, "%s\n", *cp
? cp
: base
);
2600 else if (lp
->l_has_children
== 0)
2601 fprintf(fp
, "%s%c\n", *cp
? cp
: base
,
2602 (lp
->l_delim
!= EOF
? lp
->l_delim
: '\n'));
2610 imap_folders(const char * volatile name
, int strip
)
2612 sighandler_type saveint
, savepipe
;
2613 const char *fold
, *cp
, *sp
;
2617 cp
= protbase(name
);
2618 sp
= mb
.mb_imap_account
;
2619 if (sp
== NULL
|| strcmp(cp
, sp
)) {
2621 "Cannot perform `folders' but when on the very IMAP "
2622 "account; the current one is\n `%s' -- "
2623 "try `folders @'.\n"),
2624 (sp
!= NULL
? sp
: _("[NONE]")));
2628 fold
= imap_fileof(name
);
2629 if (options
& OPT_TTYOUT
) {
2630 if ((fp
= Ftmp(NULL
, "imapfold", OF_RDWR
| OF_UNLINK
| OF_REGISTER
,
2639 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2640 safe_signal(SIGINT
, &_imap_maincatch
);
2641 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2642 if (sigsetjmp(imapjmp
, 1)) /* TODO imaplock? */
2644 if (savepipe
!= SIG_IGN
)
2645 safe_signal(SIGPIPE
, imapcatch
);
2647 if (mb
.mb_type
== MB_CACHE
)
2648 cache_list(&mb
, fold
, strip
, fp
);
2650 imap_list(&mb
, fold
, strip
, fp
);
2654 if (options
& OPT_TTYOUT
)
2660 if (options
& OPT_TTYOUT
) {
2665 fprintf(stderr
, "Folder not found.\n");
2668 safe_signal(SIGINT
, saveint
);
2669 safe_signal(SIGPIPE
, savepipe
);
2670 if (options
& OPT_TTYOUT
)
2683 long n
= 0, mx
= 0, columns
, width
;
2687 if ((out
= Ftmp(NULL
, "imapdopr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600))
2693 while ((c
= getc(fp
)) != EOF
) {
2704 if (mx
< width
/ 2) {
2705 columns
= width
/ (mx
+2);
2706 snprintf(o
, sizeof o
, "sort | pr -%lu -w%lu -t", columns
, width
);
2708 strncpy(o
, "sort", sizeof o
)[sizeof o
- 1] = '\0';
2709 run_command(XSHELL
, 0, fileno(fp
), fileno(out
), "-c", o
, NULL
);
2710 page_or_print(out
, 0);
2717 imap_copy1(struct mailbox
*mp
, struct message
*m
, int n
, const char *name
)
2721 int twice
= 0, stored
= 0;
2722 FILE *queuefp
= NULL
;
2723 enum okay ok
= STOP
;
2726 if (mp
->mb_type
== MB_CACHE
) {
2727 if ((queuefp
= cache_queue(mp
)) == NULL
)
2731 qname
= imap_quotestr(name
= imap_fileof(name
));
2732 /* Since it is not possible to set flags on the copy, recently
2733 * set flags must be set on the original to include it in the copy */
2734 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
))
2735 imap_store(mp
, m
, n
, '+', "\\Seen", 0);
2736 if (m
->m_flag
&MFLAG
)
2737 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2738 if (m
->m_flag
&MUNFLAG
)
2739 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2740 if (m
->m_flag
&MANSWER
)
2741 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2742 if (m
->m_flag
&MUNANSWER
)
2743 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2744 if (m
->m_flag
&MDRAFT
)
2745 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2746 if (m
->m_flag
&MUNDRAFT
)
2747 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2750 snprintf(o
, sizeof o
, "%s UID COPY %lu %s\r\n", tag(1), m
->m_uid
, qname
);
2752 if (check_expunged() == STOP
)
2754 snprintf(o
, sizeof o
, "%s COPY %u %s\r\n", tag(1), n
, qname
);
2756 IMAP_OUT(o
, MB_COMD
, goto out
)
2757 while (mp
->mb_active
& MB_COMD
)
2758 ok
= imap_answer(mp
, twice
);
2760 if (mp
->mb_type
== MB_IMAP
&& mp
->mb_flags
& MB_UIDPLUS
&&
2761 response_status
== RESPONSE_OK
)
2762 imap_copyuid(mp
, m
, name
);
2764 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
2765 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), qname
);
2766 IMAP_OUT(o
, MB_COMD
, goto out
)
2767 while (mp
->mb_active
& MB_COMD
)
2768 ok
= imap_answer(mp
, 1);
2770 imap_created_mailbox
++;
2775 if (queuefp
!= NULL
)
2778 /* ... and reset the flag to its initial value so that the 'exit'
2779 * command still leaves the message unread */
2781 if ((m
->m_flag
& (MREAD
| MSTATUS
)) == (MREAD
| MSTATUS
)) {
2782 imap_store(mp
, m
, n
, '-', "\\Seen", 0);
2785 if (m
->m_flag
& MFLAG
) {
2786 imap_store(mp
, m
, n
, '-', "\\Flagged", 0);
2789 if (m
->m_flag
& MUNFLAG
) {
2790 imap_store(mp
, m
, n
, '+', "\\Flagged", 0);
2793 if (m
->m_flag
& MANSWER
) {
2794 imap_store(mp
, m
, n
, '-', "\\Answered", 0);
2797 if (m
->m_flag
& MUNANSWER
) {
2798 imap_store(mp
, m
, n
, '+', "\\Answered", 0);
2801 if (m
->m_flag
& MDRAFT
) {
2802 imap_store(mp
, m
, n
, '-', "\\Draft", 0);
2805 if (m
->m_flag
& MUNDRAFT
) {
2806 imap_store(mp
, m
, n
, '+', "\\Draft", 0);
2810 mp
->mb_active
|= MB_COMD
;
2811 (void)imap_finish(mp
);
2817 imap_copy(struct message
*m
, int n
, const char *name
)
2819 sighandler_type saveint
, savepipe
;
2820 enum okay rv
= STOP
;
2824 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
2825 safe_signal(SIGINT
, &_imap_maincatch
);
2826 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
2827 if (sigsetjmp(imapjmp
, 1) == 0) {
2828 if (savepipe
!= SIG_IGN
)
2829 safe_signal(SIGPIPE
, imapcatch
);
2831 rv
= imap_copy1(&mb
, m
, n
, name
);
2833 safe_signal(SIGINT
, saveint
);
2834 safe_signal(SIGPIPE
, savepipe
);
2844 imap_copyuid_parse(const char *cp
, unsigned long *uidvalidity
,
2845 unsigned long *olduid
, unsigned long *newuid
)
2851 *uidvalidity
= strtoul(cp
, &xp
, 10);
2852 *olduid
= strtoul(xp
, &yp
, 10);
2853 *newuid
= strtoul(yp
, &zp
, 10);
2854 rv
= (*uidvalidity
&& *olduid
&& *newuid
&& xp
> cp
&& *xp
== ' ' &&
2855 yp
> xp
&& *yp
== ' ' && zp
> yp
&& *zp
== ']');
2861 imap_appenduid_parse(const char *cp
, unsigned long *uidvalidity
,
2868 *uidvalidity
= strtoul(cp
, &xp
, 10);
2869 *uid
= strtoul(xp
, &yp
, 10);
2870 rv
= (*uidvalidity
&& *uid
&& xp
> cp
&& *xp
== ' ' && yp
> xp
&&
2877 imap_copyuid(struct mailbox
*mp
, struct message
*m
, const char *name
)
2882 unsigned long uidvalidity
, olduid
, newuid
;
2887 if ((cp
= asccasestr(responded_text
, "[COPYUID ")) == NULL
||
2888 imap_copyuid_parse(&cp
[9], &uidvalidity
, &olduid
, &newuid
) == STOP
)
2893 xmb
.mb_cache_directory
= NULL
;
2894 xmb
.mb_imap_mailbox
= savestr(name
);
2895 xmb
.mb_uidvalidity
= uidvalidity
;
2898 memset(&xm
, 0, sizeof xm
);
2900 if ((rv
= getcache1(mp
, &xm
, NEED_UNSPEC
, 3)) != OKAY
)
2902 getcache(mp
, &xm
, NEED_HEADER
);
2903 getcache(mp
, &xm
, NEED_BODY
);
2905 if ((m
->m_flag
& HAVE_HEADER
) == 0)
2906 getcache(mp
, m
, NEED_HEADER
);
2907 if ((m
->m_flag
& HAVE_BODY
) == 0)
2908 getcache(mp
, m
, NEED_BODY
);
2912 xm
.m_flag
&= ~MFULLYCACHED
;
2913 putcache(&xmb
, &xm
);
2920 imap_appenduid(struct mailbox
*mp
, FILE *fp
, time_t t
, long off1
, long xsize
,
2921 long size
, long lines
, int flag
, const char *name
)
2926 unsigned long uidvalidity
, uid
;
2931 if ((cp
= asccasestr(responded_text
, "[APPENDUID ")) == NULL
||
2932 imap_appenduid_parse(&cp
[11], &uidvalidity
, &uid
) == STOP
)
2937 xmb
.mb_cache_directory
= NULL
;
2938 xmb
.mb_imap_mailbox
= savestr(name
);
2939 xmb
.mb_uidvalidity
= uidvalidity
;
2940 xmb
.mb_otf
= xmb
.mb_itf
= fp
;
2942 memset(&xm
, 0, sizeof xm
);
2943 xm
.m_flag
= (flag
& MREAD
) | MNEW
;
2945 xm
.m_block
= mailx_blockof(off1
);
2946 xm
.m_offset
= mailx_offsetof(off1
);
2949 xm
.m_lines
= xm
.m_xlines
= lines
;
2951 xm
.m_have
= HAVE_HEADER
| HAVE_BODY
;
2952 putcache(&xmb
, &xm
);
2959 imap_appenduid_cached(struct mailbox
*mp
, FILE *fp
)
2963 long size
, xsize
, ysize
, lines
;
2964 enum mflag flag
= MNEW
;
2965 char *name
, *buf
, *bp
;
2967 size_t bufsize
, buflen
, cnt
;
2968 enum okay rv
= STOP
;
2971 buf
= smalloc(bufsize
= LINESIZE
);
2974 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
2977 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
2982 if ((cp
= strrchr(bp
, '{')) == NULL
)
2985 xsize
= atol(&cp
[1]) + 2;
2986 if ((name
= imap_strex(&bp
[7], &cp
)) == NULL
)
2992 imap_getflags(cp
, &cp
, &flag
);
2993 while (*++cp
== ' ')
2996 t
= imap_read_date_time(cp
);
2998 if ((tp
= Ftmp(NULL
, "imapapui", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
3005 if (fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) == NULL
)
3008 buf
[--buflen
] = '\0';
3009 buf
[buflen
-1] = '\n';
3010 fwrite(buf
, 1, buflen
, tp
);
3017 imap_appenduid(mp
, tp
, t
, 0, xsize
-2, ysize
-1, lines
-1, flag
,
3018 imap_unquotestr(name
));
3028 #ifdef HAVE_IMAP_SEARCH
3030 imap_search2(struct mailbox
*mp
, struct message
*m
, int cnt
, const char *spec
,
3033 char *o
, *xp
, *cs
, c
;
3035 FILE *queuefp
= NULL
;
3039 enum okay ok
= STOP
;
3043 for (cp
= spec
; *cp
; cp
++)
3046 cp
= charset_get_lc();
3048 if (asccasecmp(cp
, "utf-8") && asccasecmp(cp
, "utf8")) {
3053 if ((it
= n_iconv_open("utf-8", cp
)) != (iconv_t
)-1) {
3054 sz
= strlen(spec
) + 1;
3055 nsp
= nspec
= salloc(nsz
= 6*strlen(spec
) + 1);
3056 if (n_iconv_buf(it
, &spec
, &sz
, &nsp
, &nsz
, FAL0
) == 0 &&
3065 cp
= imap_quotestr(cp
);
3066 cs
= salloc(n
= strlen(cp
) + 10);
3067 snprintf(cs
, n
, "CHARSET %s ", cp
);
3071 o
= ac_alloc(osize
= strlen(spec
) + 60);
3072 snprintf(o
, osize
, "%s UID SEARCH %s%s\r\n", tag(1), cs
, spec
);
3073 IMAP_OUT(o
, MB_COMD
, goto out
)
3074 while (mp
->mb_active
& MB_COMD
) {
3075 ok
= imap_answer(mp
, 0);
3076 if (response_status
== RESPONSE_OTHER
&&
3077 response_other
== MAILBOX_DATA_SEARCH
) {
3078 xp
= responded_other_text
;
3079 while (*xp
&& *xp
!= '\r') {
3080 n
= strtoul(xp
, &xp
, 10);
3081 for (i
= 0; i
< cnt
; i
++)
3082 if (m
[i
].m_uid
== n
&& !(m
[i
].m_flag
& MHIDDEN
) &&
3083 (f
== MDELETED
|| !(m
[i
].m_flag
& MDELETED
)))
3094 imap_search1(const char * volatile spec
, int f
)
3096 sighandler_type saveint
, savepipe
;
3097 enum okay
volatile rv
= STOP
;
3100 if (mb
.mb_type
!= MB_IMAP
)
3104 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3105 safe_signal(SIGINT
, &_imap_maincatch
);
3106 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3107 if (sigsetjmp(imapjmp
, 1) == 0) {
3108 if (savepipe
!= SIG_IGN
)
3109 safe_signal(SIGPIPE
, imapcatch
);
3111 rv
= imap_search2(&mb
, message
, msgCount
, spec
, f
);
3113 safe_signal(SIGINT
, saveint
);
3114 safe_signal(SIGPIPE
, savepipe
);
3122 #endif /* HAVE_IMAP_SEARCH */
3125 imap_thisaccount(const char *cp
)
3130 if (mb
.mb_type
!= MB_CACHE
&& mb
.mb_type
!= MB_IMAP
)
3132 else if ((mb
.mb_type
!= MB_CACHE
&& mb
.mb_sock
.s_fd
< 0) ||
3133 mb
.mb_imap_account
== NULL
)
3136 rv
= !strcmp(protbase(cp
), mb
.mb_imap_account
);
3142 imap_remove(const char * volatile name
)
3144 sighandler_type
volatile saveint
, savepipe
;
3145 enum okay rv
= STOP
;
3148 if (mb
.mb_type
!= MB_IMAP
) {
3149 fprintf(stderr
, "Refusing to remove \"%s\" in disconnected mode.\n",
3154 if (!imap_thisaccount(name
)) {
3155 fprintf(stderr
, "Can only remove mailboxes on current IMAP "
3156 "server: \"%s\" not removed.\n", name
);
3161 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3162 safe_signal(SIGINT
, &_imap_maincatch
);
3163 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3164 if (sigsetjmp(imapjmp
, 1) == 0) {
3165 if (savepipe
!= SIG_IGN
)
3166 safe_signal(SIGPIPE
, imapcatch
);
3168 rv
= imap_remove1(&mb
, imap_fileof(name
));
3170 safe_signal(SIGINT
, saveint
);
3171 safe_signal(SIGPIPE
, savepipe
);
3175 rv
= cache_remove(name
);
3184 imap_remove1(struct mailbox
*mp
, const char *name
)
3186 FILE *queuefp
= NULL
;
3189 enum okay ok
= STOP
;
3192 o
= ac_alloc(os
= 2*strlen(name
) + 100);
3193 snprintf(o
, os
, "%s DELETE %s\r\n", tag(1), imap_quotestr(name
));
3194 IMAP_OUT(o
, MB_COMD
, goto out
)
3195 while (mp
->mb_active
& MB_COMD
)
3196 ok
= imap_answer(mp
, 1);
3203 imap_rename(const char *old
, const char *new)
3205 sighandler_type saveint
, savepipe
;
3206 enum okay rv
= STOP
;
3209 if (mb
.mb_type
!= MB_IMAP
) {
3210 fprintf(stderr
, "Refusing to rename mailboxes in disconnected mode.\n");
3214 if (!imap_thisaccount(old
) || !imap_thisaccount(new)) {
3215 fprintf(stderr
, "Can only rename mailboxes on current IMAP "
3216 "server: \"%s\" not renamed to \"%s\".\n", old
, new);
3221 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
3222 safe_signal(SIGINT
, &_imap_maincatch
);
3223 savepipe
= safe_signal(SIGPIPE
, SIG_IGN
);
3224 if (sigsetjmp(imapjmp
, 1) == 0) {
3225 if (savepipe
!= SIG_IGN
)
3226 safe_signal(SIGPIPE
, imapcatch
);
3228 rv
= imap_rename1(&mb
, imap_fileof(old
), imap_fileof(new));
3230 safe_signal(SIGINT
, saveint
);
3231 safe_signal(SIGPIPE
, savepipe
);
3235 rv
= cache_rename(old
, new);
3244 imap_rename1(struct mailbox
*mp
, const char *old
, const char *new)
3246 FILE *queuefp
= NULL
;
3249 enum okay ok
= STOP
;
3252 o
= ac_alloc(os
= 2*strlen(old
) + 2*strlen(new) + 100);
3253 snprintf(o
, os
, "%s RENAME %s %s\r\n", tag(1), imap_quotestr(old
),
3254 imap_quotestr(new));
3255 IMAP_OUT(o
, MB_COMD
, goto out
)
3256 while (mp
->mb_active
& MB_COMD
)
3257 ok
= imap_answer(mp
, 1);
3264 imap_dequeue(struct mailbox
*mp
, FILE *fp
)
3266 char o
[LINESIZE
], *newname
, *buf
, *bp
, *cp
, iob
[4096];
3267 size_t bufsize
, buflen
, cnt
;
3268 long offs
, offs1
, offs2
, octets
;
3269 int twice
, gotcha
= 0;
3270 FILE *queuefp
= NULL
;
3271 enum okay ok
= OKAY
, rok
= OKAY
;
3274 buf
= smalloc(bufsize
= LINESIZE
);
3277 while ((offs1
= ftell(fp
)) >= 0 &&
3278 fgetline(&buf
, &bufsize
, &cnt
, &buflen
, fp
, 0) != NULL
) {
3279 for (bp
= buf
; *bp
!= ' '; ++bp
) /* strip old tag */
3284 if ((offs
= ftell(fp
)) < 0)
3287 snprintf(o
, sizeof o
, "%s %s", tag(1), bp
);
3288 if (ascncasecmp(bp
, "UID COPY ", 9) == 0) {
3290 while (digitchar(*cp
))
3296 if ((newname
= imap_strex(cp
, NULL
)) == NULL
)
3298 IMAP_OUT(o
, MB_COMD
, continue)
3299 while (mp
->mb_active
& MB_COMD
)
3300 ok
= imap_answer(mp
, twice
);
3301 if (response_status
== RESPONSE_NO
&& twice
++ == 0)
3303 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3304 imap_copyuid(mp
, NULL
, imap_unquotestr(newname
));
3306 } else if (ascncasecmp(bp
, "UID STORE ", 10) == 0) {
3307 IMAP_OUT(o
, MB_COMD
, continue)
3308 while (mp
->mb_active
& MB_COMD
)
3309 ok
= imap_answer(mp
, 1);
3312 } else if (ascncasecmp(bp
, "APPEND ", 7) == 0) {
3313 if ((cp
= strrchr(bp
, '{')) == NULL
)
3315 octets
= atol(&cp
[1]) + 2;
3316 if ((newname
= imap_strex(&bp
[7], NULL
)) == NULL
)
3318 IMAP_OUT(o
, MB_COMD
, continue)
3319 while (mp
->mb_active
& MB_COMD
) {
3320 ok
= imap_answer(mp
, twice
);
3321 if (response_type
== RESPONSE_CONT
)
3325 if (twice
++ == 0 && fseek(fp
, offs
, SEEK_SET
) >= 0)
3329 while (octets
> 0) {
3330 size_t n
= (UICMP(z
, octets
, >, sizeof iob
)
3331 ? sizeof iob
: (size_t)octets
);
3333 if (n
!= fread(iob
, 1, n
, fp
))
3335 swrite1(&mp
->mb_sock
, iob
, n
, 1);
3337 swrite(&mp
->mb_sock
, "");
3338 while (mp
->mb_active
& MB_COMD
) {
3339 ok
= imap_answer(mp
, 0);
3340 if (response_status
== RESPONSE_NO
&& twice
++ == 0) {
3341 if (fseek(fp
, offs
, SEEK_SET
) < 0)
3346 if (response_status
== RESPONSE_OK
&& mp
->mb_flags
& MB_UIDPLUS
) {
3347 if ((offs2
= ftell(fp
)) < 0)
3349 fseek(fp
, offs1
, SEEK_SET
);
3350 if (imap_appenduid_cached(mp
, fp
) == STOP
) {
3351 (void)fseek(fp
, offs2
, SEEK_SET
);
3357 fprintf(stderr
, "Invalid command in IMAP cache queue: \"%s\"\n", bp
);
3362 snprintf(o
, sizeof o
, "%s CREATE %s\r\n", tag(1), newname
);
3363 IMAP_OUT(o
, MB_COMD
, continue)
3364 while (mp
->mb_active
& MB_COMD
)
3365 ok
= imap_answer(mp
, 1);
3371 ftruncate(fileno(fp
), 0);
3379 imap_strex(char const *cp
, char const **xp
)
3388 for (cq
= cp
+ 1; *cq
!= '\0'; ++cq
) {
3391 else if (*cq
== '"')
3397 n
= salloc(cq
- cp
+ 2);
3398 memcpy(n
, cp
, cq
- cp
+1);
3399 n
[cq
- cp
+ 1] = '\0';
3408 check_expunged(void)
3413 if (expunged_messages
> 0) {
3414 fprintf(stderr
, "Command not executed - messages have been expunged\n");
3423 c_connect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
3426 int rv
, omsgCount
= msgCount
;
3430 if (mb
.mb_type
== MB_IMAP
&& mb
.mb_sock
.s_fd
> 0) {
3431 fprintf(stderr
, "Already connected.\n");
3436 if (!url_parse(&url
, CPROTO_IMAP
, mailname
)) {
3440 var_clear_allow_undefined
= TRU1
;
3441 ok_bclear(disconnected
);
3442 vok_bclear(savecat("disconnected-", url
.url_u_h_p
.s
));
3443 var_clear_allow_undefined
= FAL0
;
3445 if (mb
.mb_type
== MB_CACHE
) {
3446 _imap_setfile1(&url
, 0, edit
, 1);
3447 if (msgCount
> omsgCount
)
3448 newmailinfo(omsgCount
);
3457 c_disconnect(void *vp
) /* TODO v15-compat mailname<->URL (with password) */
3460 int rv
= 1, *msgvec
= vp
;
3463 if (mb
.mb_type
== MB_CACHE
) {
3464 fprintf(stderr
, "Not connected.\n");
3467 if (mb
.mb_type
!= MB_IMAP
|| cached_uidvalidity(&mb
) == 0) {
3468 fprintf(stderr
, "The current mailbox is not cached.\n");
3472 if (!url_parse(&url
, CPROTO_IMAP
, mailname
))
3477 ok_bset(disconnected
, TRU1
);
3478 if (mb
.mb_type
== MB_IMAP
) {
3479 sclose(&mb
.mb_sock
);
3480 _imap_setfile1(&url
, 0, edit
, 1);
3491 int rv
= 1, *msgvec
= vp
, *ip
;
3495 if (mb
.mb_type
!= MB_IMAP
) {
3496 fprintf(stderr
, "Not connected to an IMAP server.\n");
3499 if (cached_uidvalidity(&mb
) == 0) {
3500 fprintf(stderr
, "The current mailbox is not cached.\n");
3504 for (ip
= msgvec
; *ip
; ++ip
) {
3505 mp
= &message
[*ip
- 1];
3506 if (!(mp
->m_have
& HAVE_BODY
))
3516 disconnected(const char *file
)
3522 if (ok_blook(disconnected
)) {
3527 if (!url_parse(&url
, CPROTO_IMAP
, file
)) {
3531 rv
= vok_blook(savecat("disconnected-", url
.url_u_h_p
.s
));
3539 transflags(struct message
*omessage
, long omsgCount
, int transparent
)
3541 struct message
*omp
, *nmp
, *newdot
, *newprevdot
;
3549 while (PTRCMP(omp
, <, omessage
+ omsgCount
) &&
3550 PTRCMP(nmp
, <, message
+ msgCount
)) {
3551 if (dot
&& nmp
->m_uid
== dot
->m_uid
)
3553 if (prevdot
&& nmp
->m_uid
== prevdot
->m_uid
)
3555 if (omp
->m_uid
== nmp
->m_uid
) {
3556 hf
= nmp
->m_flag
& MHIDDEN
;
3557 if (transparent
&& mb
.mb_type
== MB_IMAP
)
3558 omp
->m_flag
&= ~MHIDDEN
;
3560 if (transparent
&& mb
.mb_type
== MB_CACHE
)
3561 nmp
[-1].m_flag
|= hf
;
3562 } else if (omp
->m_uid
< nmp
->m_uid
)
3569 prevdot
= newprevdot
;
3575 imap_read_date_time(const char *cp
)
3579 int i
, year
, month
, day
, hour
, minute
, second
, sign
= -1;
3582 /* "25-Jul-2004 15:33:44 +0200"
3584 * 0 5 10 15 20 25 */
3585 if (cp
[0] != '"' || strlen(cp
) < 28 || cp
[27] != '"')
3587 day
= strtol(&cp
[1], NULL
, 10);
3589 if (ascncasecmp(&cp
[4], month_names
[i
], 3) == 0)
3591 if (month_names
[++i
][0] == '\0')
3595 year
= strtol(&cp
[8], NULL
, 10);
3596 hour
= strtol(&cp
[13], NULL
, 10);
3597 minute
= strtol(&cp
[16], NULL
, 10);
3598 second
= strtol(&cp
[19], NULL
, 10);
3599 if ((t
= combinetime(year
, month
, day
, hour
, minute
, second
)) == (time_t)-1)
3613 t
+= strtol(buf
, NULL
, 10) * sign
* 3600;
3616 t
+= strtol(buf
, NULL
, 10) * sign
* 60;
3626 imap_make_date_time(time_t t
)
3630 int tzdiff
, tzdiff_hour
, tzdiff_min
;
3633 tzdiff
= t
- mktime(gmtime(&t
));
3634 tzdiff_hour
= (int)(tzdiff
/ 60);
3635 tzdiff_min
= tzdiff_hour
% 60;
3637 tmptr
= localtime(&t
);
3638 if (tmptr
->tm_isdst
> 0)
3640 snprintf(s
, sizeof s
, "\"%02d-%s-%04d %02d:%02d:%02d %+03d%02d\"",
3641 tmptr
->tm_mday
, month_names
[tmptr
->tm_mon
], tmptr
->tm_year
+ 1900,
3642 tmptr
->tm_hour
, tmptr
->tm_min
, tmptr
->tm_sec
, tzdiff_hour
, tzdiff_min
);
3646 #endif /* HAVE_IMAP */
3648 #if defined HAVE_IMAP || defined HAVE_IMAP_SEARCH
3650 imap_quotestr(char const *s
)
3655 np
= n
= salloc(2 * strlen(s
) + 3);
3658 if (*s
== '"' || *s
== '\\')
3669 imap_unquotestr(char const *s
)
3679 np
= n
= salloc(strlen(s
) + 1);
3692 #endif /* defined HAVE_IMAP || defined HAVE_IMAP_SEARCH */
3694 /* vim:set fenc=utf-8:s-it-mode */