1 /* imaptest.c -- A program to send commands to an IMAP server
3 * This code is Copyright (c) 2017, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
14 #include "sbr/base64.h"
16 #define IMAPTEST_SWITCHES \
17 X("host hostname", 0, HOSTSW) \
18 X("user username", 0, USERSW) \
19 X("port name/number", 0, PORTSW) \
20 X("snoop", 0, SNOOPSW) \
21 X("nosnoop", 0, NOSNOOPSW) \
22 X("sasl", 0, SASLSW) \
23 X("nosasl", 0, NOSASLSW) \
24 X("saslmech", 0, SASLMECHSW) \
25 X("authservice", 0, AUTHSERVICESW) \
27 X("notls", 0, NOTLSSW) \
28 X("initialtls", 0, INITIALTLSSW) \
29 X("queue", 0, QUEUESW) \
30 X("noqueue", 0, NOQUEUESW) \
31 X("append filename", 0, APPENDSW) \
32 X("afolder foldername", 0, AFOLDERSW) \
33 X("batch filename", 0, BATCHSW) \
34 X("timestamp", 0, TIMESTAMPSW) \
35 X("notimestamp", 0, NOTIMESTAMPSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(IMAPTEST
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(IMAPTEST
, switches
);
50 char *command
; /* Command to send */
51 char *folder
; /* Folder (for append) */
52 bool queue
; /* If true, queue for later delivery */
53 bool append
; /* If true, append "command" to mbox */
54 size_t msgsize
; /* RFC822 size of message */
55 struct imap_msg
*next
; /* Next pointer */
58 struct imap_msg
*msgqueue_head
= NULL
;
59 struct imap_msg
*msgqueue_tail
= NULL
;
64 char tag
[16]; /* Command tag */
65 struct timeval start
; /* Time command was sent */
66 char prefix
[64]; /* Command prefix */
67 struct imap_cmd
*next
; /* Next pointer */
70 static struct imap_cmd
*cmdqueue
= NULL
;
72 static char *saslmechs
= NULL
;
73 svector_t imap_capabilities
= NULL
;
75 static int imap_sasl_callback(enum sasl_message_type
, unsigned const char *,
76 unsigned int, unsigned char **, unsigned int *,
79 static void parse_capability(const char *, unsigned int len
);
80 static int capability_set(const char *);
81 static void clear_capability(void);
82 static int have_capability(void);
83 static int send_imap_command(netsec_context
*, bool noflush
, char **errstr
,
84 const char *fmt
, ...) CHECK_PRINTF(4, 5);
85 static int send_append(netsec_context
*, struct imap_msg
*, char **errstr
);
86 static int get_imap_response(netsec_context
*, const char *token
,
87 char **tokenresp
, char **status
, bool contok
,
88 bool failerr
, char **errstr
);
90 static void ts_report(struct timeval
*tv
, const char *fmt
, ...)
92 static void imap_negotiate_tls(netsec_context
*);
94 static void add_msg(bool queue
, struct imap_msg
**, const char *fmt
, ...)
96 static void add_append(const char *filename
, const char *folder
, bool queue
);
98 static void batchfile(const char *filename
, char *afolder
, bool queue
);
100 static size_t rfc822size(const char *filename
);
102 static bool timestamp
= false;
105 main (int argc
, char **argv
)
107 bool sasl
= false, tls
= false, initialtls
= false;
108 bool snoop
= false, queue
= false;
110 char *saslmech
= NULL
, *host
= NULL
, *port
= "143", *user
= NULL
;
111 char *cp
, **argp
, buf
[BUFSIZ
], *oauth_svc
= NULL
, *errstr
, **arguments
, *p
;
112 char *afolder
= NULL
;
113 netsec_context
*nsc
= NULL
;
114 struct imap_msg
*imsg
;
116 struct timeval tv_start
, tv_connect
, tv_auth
;
118 if (nmh_init(argv
[0], true, true)) { return 1; }
120 arguments
= getarguments (invo_name
, argc
, argv
, 1);
123 while ((cp
= *argp
++)) {
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 die("-%s unknown", cp
);
133 snprintf (buf
, sizeof(buf
), "%s [switches] +folder command "
134 "[command ...]", invo_name
);
135 print_help (buf
, switches
, 1);
138 print_version(invo_name
);
142 if (!(host
= *argp
++) || *host
== '-')
143 die("missing argument to %s", argp
[-2]);
146 if (!(port
= *argp
++) || *port
== '-')
147 die("missing argument to %s", argp
[-2]);
150 if (!(user
= *argp
++) || *user
== '-')
151 die("missing argument to %s", argp
[-2]);
155 if (!(afolder
= *argp
++) || *afolder
== '-')
156 die("missing argument to %s", argp
[-2]);
159 if (!*argp
|| (**argp
== '-'))
160 die("missing argument to %s", argp
[-1]);
162 die("Append folder must be set with -afolder first");
163 add_append(*argp
++, afolder
, queue
);
167 if (! *argp
|| (**argp
== '-'))
168 die("missing argument to %s", argp
[-1]);
169 batchfile(*argp
++, afolder
, queue
);
185 if (!(saslmech
= *argp
++) || *saslmech
== '-')
186 die("missing argument to %s", argp
[-2]);
189 if (!(oauth_svc
= *argp
++) || *oauth_svc
== '-')
190 die("missing argument to %s", argp
[-2]);
217 } else if (*cp
== '+') {
218 if (*(cp
+ 1) == '\0')
219 die("Invalid null folder name");
220 add_msg(false, NULL
, "SELECT \"%s\"", cp
+ 1);
222 add_msg(queue
, NULL
, "%s", cp
);
227 die("A hostname must be given with -host");
232 netsec_set_userid(nsc
, user
);
234 netsec_set_hostname(nsc
, host
);
237 netsec_set_snoop(nsc
, 1);
240 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
241 die("OAuth2 not supported");
246 gettimeofday(&tv_start
, NULL
);
248 if ((fd
= client(host
, port
, buf
, sizeof(buf
), snoop
)) == NOTOK
)
249 die("Connect failed: %s", buf
);
252 ts_report(&tv_start
, "Connect time");
253 gettimeofday(&tv_connect
, NULL
);
256 netsec_set_fd(nsc
, fd
, fd
);
257 netsec_set_snoop(nsc
, snoop
);
259 if (initialtls
|| tls
) {
260 if (netsec_set_tls(nsc
, 1, 0, &errstr
) != OK
)
264 imap_negotiate_tls(nsc
);
268 if (netsec_set_sasl_params(nsc
, "imap", saslmech
, imap_sasl_callback
,
273 if ((cp
= netsec_readline(nsc
, &len
, &errstr
)) == NULL
) {
277 if (has_prefix(cp
, "* BYE")) {
278 fprintf(stderr
, "Connection rejected: %s\n", cp
+ 5);
282 if (!has_prefix(cp
, "* OK") && !has_prefix(cp
, "* PREAUTH")) {
283 fprintf(stderr
, "Invalid server response: %s\n", cp
);
287 if ((p
= strchr(cp
+ 2, ' ')) && *(p
+ 1) != '\0' &&
288 has_prefix(p
+ 1, "[CAPABILITY ")) {
290 * Parse the capability list out to the end
293 p
+= 13; /* 1 + [CAPABILITY + space */
295 if (!(q
= strchr(p
, ']'))) {
296 fprintf(stderr
, "Cannot find end of CAPABILITY announcement\n");
300 parse_capability(p
, q
- p
);
304 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
305 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
309 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false, true,
311 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
316 fprintf(stderr
, "No CAPABILITY response seen\n");
320 p
= capstring
+ 11; /* "CAPABILITY " */
322 parse_capability(p
, strlen(p
));
327 if (!capability_set("STARTTLS")) {
328 fprintf(stderr
, "Requested STARTTLS with -tls, but IMAP server "
329 "has no support for STARTTLS\n");
332 if (send_imap_command(nsc
, false, &errstr
, "STARTTLS") != OK
) {
333 fprintf(stderr
, "Unable to issue STARTTLS: %s\n", errstr
);
337 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, true,
339 fprintf(stderr
, "STARTTLS failed: %s\n", errstr
);
343 imap_negotiate_tls(nsc
);
347 if (netsec_negotiate_sasl(nsc
, saslmechs
, &errstr
) != OK
) {
348 fprintf(stderr
, "SASL negotiation failed: %s\n", errstr
);
352 * Sigh. If we negotiated a SSF AND we got a capability response
353 * as part of the AUTHENTICATE OK message, we can't actually trust
354 * it because it's not protected at that point. So discard the
355 * capability list and we will generate a fresh CAPABILITY command
358 if (netsec_get_sasl_ssf(nsc
) > 0) {
362 if (capability_set("LOGINDISABLED")) {
363 fprintf(stderr
, "User did not request SASL, but LOGIN "
369 if (!have_capability()) {
372 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
373 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
377 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false,
378 true, &errstr
) != OK
) {
379 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
384 fprintf(stderr
, "No CAPABILITY response seen\n");
388 p
= capstring
+ 11; /* "CAPABILITY " */
390 parse_capability(p
, strlen(p
));
395 ts_report(&tv_connect
, "Authentication time");
396 gettimeofday(&tv_auth
, NULL
);
399 while (msgqueue_head
!= NULL
) {
400 imsg
= msgqueue_head
;
403 if (send_append(nsc
, imsg
, &errstr
) != OK
) {
404 fprintf(stderr
, "Cannot send APPEND for %s to mbox %s: %s\n",
405 imsg
->command
, imsg
->folder
, errstr
);
409 } else if (send_imap_command(nsc
, imsg
->queue
, &errstr
, "%s",
410 imsg
->command
) != OK
) {
411 fprintf(stderr
, "Cannot send command \"%s\": %s\n",
412 imsg
->command
, errstr
);
418 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false,
420 fprintf(stderr
, "Unable to get response for command "
421 "\"%s\": %s\n", imsg
->command
, errstr
);
426 msgqueue_head
= imsg
->next
;
434 * Flush out any pending network data and get any responses
437 if (netsec_flush(nsc
, &errstr
) != OK
) {
438 fprintf(stderr
, "Error performing final network flush: %s\n", errstr
);
442 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, &errstr
) != OK
) {
443 fprintf(stderr
, "Error fetching final command responses: %s\n", errstr
);
448 ts_report(&tv_auth
, "Total command execution time");
450 send_imap_command(nsc
, false, NULL
, "LOGOUT");
451 get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, NULL
);
454 netsec_shutdown(nsc
);
457 ts_report(&tv_start
, "Total elapsed time");
463 * Parse a capability string
465 * Put these into an svector so we can check for stuff later. But special
466 * things, like AUTH, we parse now
470 parse_capability(const char *cap
, unsigned int len
)
472 char *str
= mh_xmalloc(len
+ 1);
476 trunccpy(str
, cap
, len
+ 1);
477 caplist
= brkstring(str
, " ", NULL
);
479 if (imap_capabilities
) {
480 svector_free(imap_capabilities
);
483 imap_capabilities
= svector_create(32);
490 for (i
= 0; caplist
[i
] != NULL
; i
++) {
491 if (has_prefix(caplist
[i
], "AUTH=") && *(caplist
[i
] + 5) != '\0') {
493 saslmechs
= add(" ", saslmechs
);
495 saslmechs
= add(caplist
[i
] + 5, saslmechs
);
497 svector_push_back(imap_capabilities
, getcpy(caplist
[i
]));
505 * Return 1 if a particular capability is set
509 capability_set(const char *capability
)
511 if (!imap_capabilities
)
514 return svector_find(imap_capabilities
, capability
) != NULL
;
518 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
522 clear_capability(void)
524 svector_free(imap_capabilities
);
525 imap_capabilities
= NULL
;
529 have_capability(void)
531 return imap_capabilities
!= NULL
;
535 * Our SASL callback, which handles the SASL authentication dialog
539 imap_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
540 unsigned int indatalen
, unsigned char **outdata
,
541 unsigned int *outdatalen
, void *context
, char **errstr
)
544 char *mech
, *line
, *capstring
, *p
;
546 netsec_context
*nsc
= (netsec_context
*) context
;
549 case NETSEC_SASL_START
:
551 * Generate our AUTHENTICATE message.
553 * If SASL-IR capability is set, we can include any initial response
554 * in the AUTHENTICATE command. Otherwise we have to wait for
555 * the first server response (which should be blank).
558 mech
= netsec_get_sasl_mechanism(nsc
);
562 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
563 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
564 if (capability_set("SASL-IR")) {
565 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
568 * Sigh. We're assuming a short tag length here. Really,
569 * I guess we should figure out a way to get the length
572 snoopoffset
= 17 + strlen(mech
);
573 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s %s",
576 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
580 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s", mech
);
581 line
= netsec_readline(nsc
, &len
, errstr
);
585 * We should get a "+ ", nothing else.
587 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
588 netsec_err(errstr
, "Did not get expected blank response "
589 "for initial challenge response");
592 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
596 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
597 rc
= netsec_flush(nsc
, errstr
);
598 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
603 if (send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s",
611 * Get a response, decode it and process it.
614 case NETSEC_SASL_READ
:
615 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
617 line
= netsec_readline(nsc
, &len
, errstr
);
618 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
623 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
624 netsec_err(errstr
, "Invalid format for SASL response");
632 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
635 netsec_err(errstr
, "Unable to decode base64 response");
642 * Simple request encoding
645 case NETSEC_SASL_WRITE
:
647 unsigned char *b64data
;
648 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
649 writeBase64raw(indata
, indatalen
, b64data
);
650 rc
= netsec_printf(nsc
, errstr
, "%s", b64data
);
656 if (netsec_printf(nsc
, errstr
, "\r\n") != OK
)
659 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
660 rc
= netsec_flush(nsc
, errstr
);
661 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
670 case NETSEC_SASL_FINISH
:
672 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, &line
,
673 false, true, errstr
) != OK
)
676 * We MIGHT get a capability response here. If so, be sure we
677 * parse it. We ALSO might get a untagged CAPABILITY response.
678 * Which one should we prefer? I guess we'll go with the untagged
683 p
= capstring
+ 11; /* "CAPABILITY " */
684 parse_capability(p
, strlen(p
));
686 } else if (line
&& has_prefix(line
, "OK [CAPABILITY ")) {
693 parse_capability(p
, q
- p
);
699 * Cancel an authentication dialog
702 case NETSEC_SASL_CANCEL
:
703 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
705 rc
= netsec_flush(nsc
, errstr
);
714 * Send a single command to the IMAP server.
718 send_imap_command(netsec_context
*nsc
, bool noflush
, char **errstr
,
719 const char *fmt
, ...)
721 static unsigned int seq
= 0; /* Tag sequence number */
724 struct imap_cmd
*cmd
;
726 cmd
= mh_xmalloc(sizeof(*cmd
));
728 snprintf(cmd
->tag
, sizeof(cmd
->tag
), "A%u ", seq
++);
733 vsnprintf(cmd
->prefix
, sizeof(cmd
->prefix
), fmt
, ap
);
736 p
= strchr(cmd
->prefix
, ' ');
741 gettimeofday(&cmd
->start
, NULL
);
744 if (netsec_write(nsc
, cmd
->tag
, strlen(cmd
->tag
), errstr
) != OK
) {
750 rc
= netsec_vprintf(nsc
, errstr
, fmt
, ap
);
754 rc
= netsec_write(nsc
, "\r\n", 2, errstr
);
761 if (!noflush
&& netsec_flush(nsc
, errstr
) != OK
) {
766 cmd
->next
= cmdqueue
;
773 * Send an APPEND to the server, which requires some extra semantics
777 send_append(netsec_context
*nsc
, struct imap_msg
*imsg
, char **errstr
)
780 bool nonsynlit
= false;
781 char *status
= NULL
, *line
= NULL
;
786 * If we have the LITERAL+ extension, or we have LITERAL- and our
787 * message is 4096 bytes or less, we can do it all in one message.
788 * Otherwise we have to wait for a contination marker (+).
791 if (capability_set("LITERAL+") ||
792 (capability_set("LITERAL-") && imsg
->msgsize
<= 4096)) {
797 * Send our APPEND command
800 if (send_imap_command(nsc
, nonsynlit
, errstr
, "APPEND \"%s\" {%u%s}",
801 imsg
->folder
, (unsigned int) imsg
->msgsize
,
802 nonsynlit
? "+" : "") != OK
)
806 * If we need to wait for a syncing literal, do that now
810 if (get_imap_response(nsc
, NULL
, NULL
, &status
, true,
811 true, errstr
) != OK
) {
812 imsg
->queue
= true; /* XXX Sigh */
813 fprintf(stderr
, "APPEND command failed: %s\n", *errstr
);
817 if (!(status
&& has_prefix(status
, "+"))) {
818 netsec_err(errstr
, "Expected contination (+), but got: %s", status
);
826 * Now write the message out, but make sure we end each line with \r\n
829 if ((f
= fopen(imsg
->command
, "r")) == NULL
) {
830 netsec_err(errstr
, "Unable to open %s: %s", imsg
->command
,
835 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
836 if (rc
> 1 && line
[rc
- 1] == '\n' && line
[rc
- 2] == '\r') {
837 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
843 if (line
[rc
- 1] == '\n')
845 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
850 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
) {
861 netsec_err(errstr
, "Error reading %s: %s", imsg
->command
,
870 * Send a final \r\n for the end of the command
873 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
)
877 return netsec_flush(nsc
, errstr
);
883 * Get all outstanding responses. If we were passed in a token string
884 * to look for, return it.
888 get_imap_response(netsec_context
*nsc
, const char *token
, char **tokenresponse
,
889 char **status
, bool condok
, bool failerr
, char **errstr
)
892 struct imap_cmd
*cmd
;
893 bool numerrs
= false;
896 *tokenresponse
= NULL
;
900 if (!(line
= netsec_readline(nsc
, NULL
, errstr
)))
902 if (has_prefix(line
, "* ") && *(line
+ 2) != '\0') {
903 if (token
&& tokenresponse
&& has_prefix(line
+ 2, token
)) {
905 free(*tokenresponse
);
906 *tokenresponse
= getcpy(line
+ 2);
908 } if (condok
&& has_prefix(line
, "+")) {
910 *status
= getcpy(line
);
913 * Special case; return now but don't dequeue the tag,
914 * since we will want to get final result later.
919 if (has_prefix(line
, cmdqueue
->tag
)) {
922 ts_report(&cmd
->start
, "Command (%s) execution time",
924 cmdqueue
= cmd
->next
;
927 for (cmd
= cmdqueue
; cmd
->next
!= NULL
; cmd
= cmd
->next
) {
928 if (has_prefix(line
, cmd
->next
->tag
)) {
929 struct imap_cmd
*cmd2
= cmd
->next
;
930 cmd
->next
= cmd
->next
->next
;
931 if (failerr
&& strncmp(line
+ strlen(cmd2
->tag
),
934 netsec_err(errstr
, "%s", line
+ strlen(cmd2
->tag
));
937 ts_report(&cmd2
->start
, "Command (%s) execution "
938 "time", cmd2
->prefix
);
941 *status
= getcpy(line
);
949 return numerrs
? NOTOK
: OK
;
953 * Add an IMAP command to the msg queue
957 add_msg(bool queue
, struct imap_msg
**ret_imsg
, const char *fmt
, ...)
959 struct imap_msg
*imsg
;
967 msg
= mh_xrealloc(msg
, msgbufsize
);
969 rc
= vsnprintf(msg
, msgbufsize
, fmt
, ap
);
971 } while (rc
>= (int) msgbufsize
);
973 imsg
= mh_xmalloc(sizeof(*imsg
));
977 imsg
->append
= false;
981 if (msgqueue_head
== NULL
) {
982 msgqueue_head
= imsg
;
983 msgqueue_tail
= imsg
;
985 msgqueue_tail
->next
= imsg
;
986 msgqueue_tail
= imsg
;
994 * Add an APPEND command to the queue
998 add_append(const char *filename
, const char *folder
, bool queue
)
1000 size_t filesize
= rfc822size(filename
);
1001 struct imap_msg
*imsg
;
1003 add_msg(queue
, &imsg
, "%s", filename
);
1005 imsg
->folder
= getcpy(folder
);
1006 imsg
->append
= true;
1007 imsg
->msgsize
= filesize
;
1011 * Process a batch file, which can contain commands (and some arguments)
1015 batchfile(const char *filename
, char *afolder
, bool queue
)
1019 size_t linesize
= 0;
1021 bool afolder_alloc
= false;
1023 if (!(f
= fopen(filename
, "r"))) {
1024 die("Unable to open batch file %s: %s", filename
, strerror(errno
));
1027 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
1028 line
[rc
- 1] = '\0';
1030 switch (smatch (line
+ 1, switches
)) {
1040 rc
= getline(&line
, &linesize
, f
);
1042 die("Unable to read next line for -afolder");
1044 die("Folder name cannot be blank");
1045 line
[rc
- 1] = '\0';
1046 afolder
= getcpy(line
);
1047 afolder_alloc
= true;
1051 rc
= getline(&line
, &linesize
, f
);
1053 die("Unable to read filename for -append");
1055 die("Filename for -append cannot be blank");
1056 line
[rc
- 1] = '\0';
1057 add_append(line
, afolder
, queue
);
1061 ambigsw (line
, switches
);
1064 die("%s unknown", line
);
1066 die("Switch %s not supported in batch mode", line
);
1068 } else if (*line
== '+') {
1069 if (*(line
+ 1) == '\0')
1070 die("Invalid null folder name");
1071 add_msg(false, NULL
, "SELECT \"%s\"", line
+ 1);
1074 continue; /* Ignore blank line */
1075 add_msg(queue
, NULL
, "%s", line
);
1080 die("Read of \"%s\" failed: %s", filename
, strerror(errno
));
1092 * Negotiate TLS connection, with optional timestamp
1096 imap_negotiate_tls(netsec_context
*nsc
)
1102 gettimeofday(&tv
, NULL
);
1104 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
)
1108 ts_report(&tv
, "TLS negotation time");
1112 * Give a timestamp report.
1116 ts_report(struct timeval
*tv
, const char *fmt
, ...)
1122 gettimeofday(&now
, NULL
);
1124 delta
= ((double) now
.tv_sec
) - ((double) tv
->tv_sec
) +
1125 (now
.tv_usec
/ 1E6
- tv
->tv_usec
/ 1E6
);
1128 vfprintf(stdout
, fmt
, ap
);
1131 printf(": %f sec\n", delta
);
1135 * Calculate the RFC 822 size of file.
1139 rfc822size(const char *filename
)
1142 size_t total
= 0, linecap
= 0;
1146 if (! (f
= fopen(filename
, "r")))
1147 die("Unable to open %s: %s", filename
, strerror(errno
));
1149 while ((rc
= getline(&line
, &linecap
, f
)) > 0) {
1151 if (line
[rc
- 1] == '\n' && (rc
== 1 || line
[rc
- 2] != '\r'))
1153 if (line
[rc
- 1] != '\n')
1160 die("Error while reading %s: %s", filename
, strerror(errno
));