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.
9 #include "sbr/client.h"
10 #include "sbr/getcpy.h"
11 #include "sbr/brkstring.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
21 #include "sbr/base64.h"
23 #define IMAPTEST_SWITCHES \
24 X("host hostname", 0, HOSTSW) \
25 X("user username", 0, USERSW) \
26 X("port name/number", 0, PORTSW) \
27 X("snoop", 0, SNOOPSW) \
28 X("nosnoop", 0, NOSNOOPSW) \
29 X("sasl", 0, SASLSW) \
30 X("nosasl", 0, NOSASLSW) \
31 X("saslmech", 0, SASLMECHSW) \
32 X("authservice", 0, AUTHSERVICESW) \
34 X("notls", 0, NOTLSSW) \
35 X("initialtls", 0, INITIALTLSSW) \
36 X("queue", 0, QUEUESW) \
37 X("noqueue", 0, NOQUEUESW) \
38 X("append filename", 0, APPENDSW) \
39 X("afolder foldername", 0, AFOLDERSW) \
40 X("batch filename", 0, BATCHSW) \
41 X("timestamp", 0, TIMESTAMPSW) \
42 X("notimestamp", 0, NOTIMESTAMPSW) \
43 X("timeout", 0, TIMEOUTSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(IMAPTEST
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(IMAPTEST
, switches
);
58 char *command
; /* Command to send */
59 char *folder
; /* Folder (for append) */
60 bool queue
; /* If true, queue for later delivery */
61 bool append
; /* If true, append "command" to mbox */
62 size_t msgsize
; /* RFC822 size of message */
63 struct imap_msg
*next
; /* Next pointer */
66 struct imap_msg
*msgqueue_head
= NULL
;
67 struct imap_msg
*msgqueue_tail
= NULL
;
72 char tag
[16]; /* Command tag */
73 struct timeval start
; /* Time command was sent */
74 char prefix
[64]; /* Command prefix */
75 struct imap_cmd
*next
; /* Next pointer */
78 static struct imap_cmd
*cmdqueue
= NULL
;
80 static char *saslmechs
= NULL
;
81 svector_t imap_capabilities
= NULL
;
83 static int imap_sasl_callback(enum sasl_message_type
, unsigned const char *,
84 unsigned int, unsigned char **, unsigned int *,
87 static void parse_capability(const char *, unsigned int len
);
88 static int capability_set(const char *);
89 static void clear_capability(void);
90 static int have_capability(void);
91 static int send_imap_command(netsec_context
*, bool noflush
, char **errstr
,
92 const char *fmt
, ...) CHECK_PRINTF(4, 5);
93 static int send_append(netsec_context
*, struct imap_msg
*, char **errstr
);
94 static int get_imap_response(netsec_context
*, const char *token
,
95 char **tokenresp
, char **status
, bool contok
,
96 bool failerr
, char **errstr
);
98 static void ts_report(struct timeval
*tv
, const char *fmt
, ...)
100 static void imap_negotiate_tls(netsec_context
*);
102 static void add_msg(bool queue
, struct imap_msg
**, const char *fmt
, ...)
104 static void add_append(const char *filename
, const char *folder
, bool queue
);
106 static void batchfile(const char *filename
, char *afolder
, bool queue
);
108 static size_t rfc822size(const char *filename
);
110 static bool timestamp
= false;
113 main (int argc
, char **argv
)
115 bool sasl
= false, tls
= false, initialtls
= false;
116 bool snoop
= false, queue
= false;
118 char *saslmech
= NULL
, *host
= NULL
, *port
= "143", *user
= NULL
;
119 char *cp
, **argp
, buf
[BUFSIZ
], *oauth_svc
= NULL
, *errstr
, **arguments
, *p
;
120 char *afolder
= NULL
;
121 netsec_context
*nsc
= NULL
;
122 struct imap_msg
*imsg
;
124 struct timeval tv_start
, tv_connect
, tv_auth
;
126 if (nmh_init(argv
[0], true, true)) { return 1; }
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
131 while ((cp
= *argp
++)) {
133 switch (smatch (++cp
, switches
)) {
135 ambigsw (cp
, switches
);
138 die("-%s unknown", cp
);
141 snprintf (buf
, sizeof(buf
), "%s [switches] +folder command "
142 "[command ...]", invo_name
);
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
150 if (!(host
= *argp
++) || *host
== '-')
151 die("missing argument to %s", argp
[-2]);
154 if (!(port
= *argp
++) || *port
== '-')
155 die("missing argument to %s", argp
[-2]);
158 if (!(user
= *argp
++) || *user
== '-')
159 die("missing argument to %s", argp
[-2]);
163 if (!(afolder
= *argp
++) || *afolder
== '-')
164 die("missing argument to %s", argp
[-2]);
167 if (!*argp
|| (**argp
== '-'))
168 die("missing argument to %s", argp
[-1]);
170 die("Append folder must be set with -afolder first");
171 add_append(*argp
++, afolder
, queue
);
175 if (! *argp
|| (**argp
== '-'))
176 die("missing argument to %s", argp
[-1]);
177 batchfile(*argp
++, afolder
, queue
);
181 if (! *argp
|| (**argp
== '-'))
182 die("missing argument to %s", argp
[-1]);
183 if (! (timeout
= atoi(*argp
++)))
184 die("Invalid timeout: %s", argp
[-1]);
200 if (!(saslmech
= *argp
++) || *saslmech
== '-')
201 die("missing argument to %s", argp
[-2]);
204 if (!(oauth_svc
= *argp
++) || *oauth_svc
== '-')
205 die("missing argument to %s", argp
[-2]);
232 } else if (*cp
== '+') {
233 if (*(cp
+ 1) == '\0')
234 die("Invalid null folder name");
235 add_msg(false, NULL
, "SELECT \"%s\"", cp
+ 1);
237 add_msg(queue
, NULL
, "%s", cp
);
242 die("A hostname must be given with -host");
247 netsec_set_userid(nsc
, user
);
249 netsec_set_hostname(nsc
, host
);
252 netsec_set_timeout(nsc
, timeout
);
255 netsec_set_snoop(nsc
, 1);
258 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
259 die("OAuth2 not supported");
264 gettimeofday(&tv_start
, NULL
);
266 if ((fd
= client(host
, port
, buf
, sizeof(buf
), snoop
)) == NOTOK
)
267 die("Connect failed: %s", buf
);
270 ts_report(&tv_start
, "Connect time");
271 gettimeofday(&tv_connect
, NULL
);
274 netsec_set_fd(nsc
, fd
, fd
);
275 netsec_set_snoop(nsc
, snoop
);
277 if (initialtls
|| tls
) {
278 if (netsec_set_tls(nsc
, 1, 0, &errstr
) != OK
)
282 imap_negotiate_tls(nsc
);
286 if (netsec_set_sasl_params(nsc
, "imap", saslmech
, imap_sasl_callback
,
291 if ((cp
= netsec_readline(nsc
, &len
, &errstr
)) == NULL
) {
295 if (has_prefix(cp
, "* BYE")) {
296 fprintf(stderr
, "Connection rejected: %s\n", cp
+ 5);
300 if (!has_prefix(cp
, "* OK") && !has_prefix(cp
, "* PREAUTH")) {
301 fprintf(stderr
, "Invalid server response: %s\n", cp
);
305 if ((p
= strchr(cp
+ 2, ' ')) && *(p
+ 1) != '\0' &&
306 has_prefix(p
+ 1, "[CAPABILITY ")) {
308 * Parse the capability list out to the end
311 p
+= 13; /* 1 + [CAPABILITY + space */
313 if (!(q
= strchr(p
, ']'))) {
314 fprintf(stderr
, "Cannot find end of CAPABILITY announcement\n");
318 parse_capability(p
, q
- p
);
322 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
323 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
327 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false, true,
329 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
334 fprintf(stderr
, "No CAPABILITY response seen\n");
338 p
= capstring
+ 11; /* "CAPABILITY " */
340 parse_capability(p
, strlen(p
));
345 if (!capability_set("STARTTLS")) {
346 fprintf(stderr
, "Requested STARTTLS with -tls, but IMAP server "
347 "has no support for STARTTLS\n");
350 if (send_imap_command(nsc
, false, &errstr
, "STARTTLS") != OK
) {
351 fprintf(stderr
, "Unable to issue STARTTLS: %s\n", errstr
);
355 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, true,
357 fprintf(stderr
, "STARTTLS failed: %s\n", errstr
);
361 imap_negotiate_tls(nsc
);
365 if (netsec_negotiate_sasl(nsc
, saslmechs
, &errstr
) != OK
) {
366 fprintf(stderr
, "SASL negotiation failed: %s\n", errstr
);
370 * Sigh. If we negotiated a SSF AND we got a capability response
371 * as part of the AUTHENTICATE OK message, we can't actually trust
372 * it because it's not protected at that point. So discard the
373 * capability list and we will generate a fresh CAPABILITY command
376 if (netsec_get_sasl_ssf(nsc
) > 0) {
380 if (capability_set("LOGINDISABLED")) {
381 fprintf(stderr
, "User did not request SASL, but LOGIN "
387 if (!have_capability()) {
390 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
391 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
395 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false,
396 true, &errstr
) != OK
) {
397 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
402 fprintf(stderr
, "No CAPABILITY response seen\n");
406 p
= capstring
+ 11; /* "CAPABILITY " */
408 parse_capability(p
, strlen(p
));
413 ts_report(&tv_connect
, "Authentication time");
414 gettimeofday(&tv_auth
, NULL
);
417 while (msgqueue_head
!= NULL
) {
418 imsg
= msgqueue_head
;
421 if (send_append(nsc
, imsg
, &errstr
) != OK
) {
422 fprintf(stderr
, "Cannot send APPEND for %s to mbox %s: %s\n",
423 imsg
->command
, imsg
->folder
, errstr
);
427 } else if (send_imap_command(nsc
, imsg
->queue
, &errstr
, "%s",
428 imsg
->command
) != OK
) {
429 fprintf(stderr
, "Cannot send command \"%s\": %s\n",
430 imsg
->command
, errstr
);
436 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false,
438 fprintf(stderr
, "Unable to get response for command "
439 "\"%s\": %s\n", imsg
->command
, errstr
);
444 msgqueue_head
= imsg
->next
;
452 * Flush out any pending network data and get any responses
455 if (netsec_flush(nsc
, &errstr
) != OK
) {
456 fprintf(stderr
, "Error performing final network flush: %s\n", errstr
);
460 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, &errstr
) != OK
) {
461 fprintf(stderr
, "Error fetching final command responses: %s\n", errstr
);
466 ts_report(&tv_auth
, "Total command execution time");
468 send_imap_command(nsc
, false, NULL
, "LOGOUT");
469 get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, NULL
);
472 netsec_shutdown(nsc
);
475 ts_report(&tv_start
, "Total elapsed time");
481 * Parse a capability string
483 * Put these into an svector so we can check for stuff later. But special
484 * things, like AUTH, we parse now
488 parse_capability(const char *cap
, unsigned int len
)
490 char *str
= mh_xmalloc(len
+ 1);
494 trunccpy(str
, cap
, len
+ 1);
495 caplist
= brkstring(str
, " ", NULL
);
497 if (imap_capabilities
) {
498 svector_free(imap_capabilities
);
501 imap_capabilities
= svector_create(32);
508 for (i
= 0; caplist
[i
] != NULL
; i
++) {
509 if (has_prefix(caplist
[i
], "AUTH=") && *(caplist
[i
] + 5) != '\0') {
511 saslmechs
= add(" ", saslmechs
);
513 saslmechs
= add(caplist
[i
] + 5, saslmechs
);
515 svector_push_back(imap_capabilities
, getcpy(caplist
[i
]));
523 * Return 1 if a particular capability is set
527 capability_set(const char *capability
)
529 if (!imap_capabilities
)
532 return svector_find(imap_capabilities
, capability
) != NULL
;
536 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
540 clear_capability(void)
542 svector_free(imap_capabilities
);
543 imap_capabilities
= NULL
;
547 have_capability(void)
549 return imap_capabilities
!= NULL
;
553 * Our SASL callback, which handles the SASL authentication dialog
557 imap_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
558 unsigned int indatalen
, unsigned char **outdata
,
559 unsigned int *outdatalen
, void *context
, char **errstr
)
562 char *mech
, *line
, *capstring
, *p
;
564 netsec_context
*nsc
= (netsec_context
*) context
;
567 case NETSEC_SASL_START
:
569 * Generate our AUTHENTICATE message.
571 * If SASL-IR capability is set, we can include any initial response
572 * in the AUTHENTICATE command. Otherwise we have to wait for
573 * the first server response (which should be blank).
576 mech
= netsec_get_sasl_mechanism(nsc
);
580 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
581 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
582 if (capability_set("SASL-IR")) {
583 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
586 * Sigh. We're assuming a short tag length here. Really,
587 * I guess we should figure out a way to get the length
590 snoopoffset
= 17 + strlen(mech
);
591 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s %s",
594 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
598 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s", mech
);
599 line
= netsec_readline(nsc
, &len
, errstr
);
603 * We should get a "+ ", nothing else.
605 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
606 netsec_err(errstr
, "Did not get expected blank response "
607 "for initial challenge response");
610 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
614 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
615 rc
= netsec_flush(nsc
, errstr
);
616 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
621 if (send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s",
629 * Get a response, decode it and process it.
632 case NETSEC_SASL_READ
:
633 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
635 line
= netsec_readline(nsc
, &len
, errstr
);
636 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
641 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
642 netsec_err(errstr
, "Invalid format for SASL response");
650 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
653 netsec_err(errstr
, "Unable to decode base64 response");
660 * Simple request encoding
663 case NETSEC_SASL_WRITE
:
665 unsigned char *b64data
;
666 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
667 writeBase64raw(indata
, indatalen
, b64data
);
668 rc
= netsec_printf(nsc
, errstr
, "%s", b64data
);
674 if (netsec_printf(nsc
, errstr
, "\r\n") != OK
)
677 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
678 rc
= netsec_flush(nsc
, errstr
);
679 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
688 case NETSEC_SASL_FINISH
:
690 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, &line
,
691 false, true, errstr
) != OK
)
694 * We MIGHT get a capability response here. If so, be sure we
695 * parse it. We ALSO might get a untagged CAPABILITY response.
696 * Which one should we prefer? I guess we'll go with the untagged
701 p
= capstring
+ 11; /* "CAPABILITY " */
702 parse_capability(p
, strlen(p
));
704 } else if (line
&& has_prefix(line
, "OK [CAPABILITY ")) {
711 parse_capability(p
, q
- p
);
717 * Cancel an authentication dialog
720 case NETSEC_SASL_CANCEL
:
721 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
723 rc
= netsec_flush(nsc
, errstr
);
732 * Send a single command to the IMAP server.
736 send_imap_command(netsec_context
*nsc
, bool noflush
, char **errstr
,
737 const char *fmt
, ...)
739 static unsigned int seq
= 0; /* Tag sequence number */
742 struct imap_cmd
*cmd
;
744 cmd
= mh_xmalloc(sizeof(*cmd
));
746 snprintf(cmd
->tag
, sizeof(cmd
->tag
), "A%u ", seq
++);
751 vsnprintf(cmd
->prefix
, sizeof(cmd
->prefix
), fmt
, ap
);
754 p
= strchr(cmd
->prefix
, ' ');
759 gettimeofday(&cmd
->start
, NULL
);
762 if (netsec_write(nsc
, cmd
->tag
, strlen(cmd
->tag
), errstr
) != OK
) {
768 rc
= netsec_vprintf(nsc
, errstr
, fmt
, ap
);
772 rc
= netsec_write(nsc
, "\r\n", 2, errstr
);
779 if (!noflush
&& netsec_flush(nsc
, errstr
) != OK
) {
784 cmd
->next
= cmdqueue
;
791 * Send an APPEND to the server, which requires some extra semantics
795 send_append(netsec_context
*nsc
, struct imap_msg
*imsg
, char **errstr
)
798 bool nonsynlit
= false;
799 char *status
= NULL
, *line
= NULL
;
804 * If we have the LITERAL+ extension, or we have LITERAL- and our
805 * message is 4096 bytes or less, we can do it all in one message.
806 * Otherwise we have to wait for a contination marker (+).
809 if (capability_set("LITERAL+") ||
810 (capability_set("LITERAL-") && imsg
->msgsize
<= 4096)) {
815 * Send our APPEND command
818 if (send_imap_command(nsc
, nonsynlit
, errstr
, "APPEND \"%s\" {%u%s}",
819 imsg
->folder
, (unsigned int) imsg
->msgsize
,
820 nonsynlit
? "+" : "") != OK
)
824 * If we need to wait for a syncing literal, do that now
828 if (get_imap_response(nsc
, NULL
, NULL
, &status
, true,
829 true, errstr
) != OK
) {
830 imsg
->queue
= true; /* XXX Sigh */
831 fprintf(stderr
, "APPEND command failed: %s\n", *errstr
);
835 if (!(status
&& has_prefix(status
, "+"))) {
836 netsec_err(errstr
, "Expected contination (+), but got: %s", status
);
844 * Now write the message out, but make sure we end each line with \r\n
847 if ((f
= fopen(imsg
->command
, "r")) == NULL
) {
848 netsec_err(errstr
, "Unable to open %s: %s", imsg
->command
,
853 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
854 if (rc
> 1 && line
[rc
- 1] == '\n' && line
[rc
- 2] == '\r') {
855 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
861 if (line
[rc
- 1] == '\n')
863 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
868 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
) {
879 netsec_err(errstr
, "Error reading %s: %s", imsg
->command
,
888 * Send a final \r\n for the end of the command
891 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
)
895 return netsec_flush(nsc
, errstr
);
901 * Get all outstanding responses. If we were passed in a token string
902 * to look for, return it.
906 get_imap_response(netsec_context
*nsc
, const char *token
, char **tokenresponse
,
907 char **status
, bool condok
, bool failerr
, char **errstr
)
910 struct imap_cmd
*cmd
;
911 bool numerrs
= false;
914 *tokenresponse
= NULL
;
918 if (!(line
= netsec_readline(nsc
, NULL
, errstr
)))
920 if (has_prefix(line
, "* ") && *(line
+ 2) != '\0') {
921 if (token
&& tokenresponse
&& has_prefix(line
+ 2, token
)) {
923 free(*tokenresponse
);
924 *tokenresponse
= getcpy(line
+ 2);
926 } if (condok
&& has_prefix(line
, "+")) {
928 *status
= getcpy(line
);
931 * Special case; return now but don't dequeue the tag,
932 * since we will want to get final result later.
937 if (has_prefix(line
, cmdqueue
->tag
)) {
940 ts_report(&cmd
->start
, "Command (%s) execution time",
942 cmdqueue
= cmd
->next
;
945 for (cmd
= cmdqueue
; cmd
->next
!= NULL
; cmd
= cmd
->next
) {
946 if (has_prefix(line
, cmd
->next
->tag
)) {
947 struct imap_cmd
*cmd2
= cmd
->next
;
948 cmd
->next
= cmd
->next
->next
;
949 if (failerr
&& strncmp(line
+ strlen(cmd2
->tag
),
952 netsec_err(errstr
, "%s", line
+ strlen(cmd2
->tag
));
955 ts_report(&cmd2
->start
, "Command (%s) execution "
956 "time", cmd2
->prefix
);
959 *status
= getcpy(line
);
967 return numerrs
? NOTOK
: OK
;
971 * Add an IMAP command to the msg queue
975 add_msg(bool queue
, struct imap_msg
**ret_imsg
, const char *fmt
, ...)
977 struct imap_msg
*imsg
;
985 msg
= mh_xrealloc(msg
, msgbufsize
);
987 rc
= vsnprintf(msg
, msgbufsize
, fmt
, ap
);
989 } while (rc
>= (int) msgbufsize
);
991 imsg
= mh_xmalloc(sizeof(*imsg
));
995 imsg
->append
= false;
999 if (msgqueue_head
== NULL
) {
1000 msgqueue_head
= imsg
;
1001 msgqueue_tail
= imsg
;
1003 msgqueue_tail
->next
= imsg
;
1004 msgqueue_tail
= imsg
;
1012 * Add an APPEND command to the queue
1016 add_append(const char *filename
, const char *folder
, bool queue
)
1018 size_t filesize
= rfc822size(filename
);
1019 struct imap_msg
*imsg
;
1021 add_msg(queue
, &imsg
, "%s", filename
);
1023 imsg
->folder
= getcpy(folder
);
1024 imsg
->append
= true;
1025 imsg
->msgsize
= filesize
;
1029 * Process a batch file, which can contain commands (and some arguments)
1033 batchfile(const char *filename
, char *afolder
, bool queue
)
1037 size_t linesize
= 0;
1039 bool afolder_alloc
= false;
1041 if (!(f
= fopen(filename
, "r"))) {
1042 die("Unable to open batch file %s: %s", filename
, strerror(errno
));
1045 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
1046 line
[rc
- 1] = '\0';
1048 switch (smatch (line
+ 1, switches
)) {
1058 rc
= getline(&line
, &linesize
, f
);
1060 die("Unable to read next line for -afolder");
1062 die("Folder name cannot be blank");
1063 line
[rc
- 1] = '\0';
1064 afolder
= getcpy(line
);
1065 afolder_alloc
= true;
1069 rc
= getline(&line
, &linesize
, f
);
1071 die("Unable to read filename for -append");
1073 die("Filename for -append cannot be blank");
1074 line
[rc
- 1] = '\0';
1075 add_append(line
, afolder
, queue
);
1079 ambigsw (line
, switches
);
1082 die("%s unknown", line
);
1084 die("Switch %s not supported in batch mode", line
);
1086 } else if (*line
== '+') {
1087 if (*(line
+ 1) == '\0')
1088 die("Invalid null folder name");
1089 add_msg(false, NULL
, "SELECT \"%s\"", line
+ 1);
1092 continue; /* Ignore blank line */
1093 add_msg(queue
, NULL
, "%s", line
);
1098 die("Read of \"%s\" failed: %s", filename
, strerror(errno
));
1110 * Negotiate TLS connection, with optional timestamp
1114 imap_negotiate_tls(netsec_context
*nsc
)
1120 gettimeofday(&tv
, NULL
);
1122 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
)
1126 ts_report(&tv
, "TLS negotation time");
1130 * Give a timestamp report.
1134 ts_report(struct timeval
*tv
, const char *fmt
, ...)
1140 gettimeofday(&now
, NULL
);
1142 delta
= ((double) now
.tv_sec
) - ((double) tv
->tv_sec
) +
1143 (now
.tv_usec
/ 1E6
- tv
->tv_usec
/ 1E6
);
1146 vfprintf(stdout
, fmt
, ap
);
1149 printf(": %f sec\n", delta
);
1153 * Calculate the RFC 822 size of file.
1157 rfc822size(const char *filename
)
1160 size_t total
= 0, linecap
= 0;
1164 if (! (f
= fopen(filename
, "r")))
1165 die("Unable to open %s: %s", filename
, strerror(errno
));
1167 while ((rc
= getline(&line
, &linecap
, f
)) > 0) {
1169 if (line
[rc
- 1] == '\n' && (rc
== 1 || line
[rc
- 2] != '\r'))
1171 if (line
[rc
- 1] != '\n')
1178 die("Error while reading %s: %s", filename
, strerror(errno
));