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/brkstring.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
19 #include "sbr/base64.h"
21 #define IMAPTEST_SWITCHES \
22 X("host hostname", 0, HOSTSW) \
23 X("user username", 0, USERSW) \
24 X("port name/number", 0, PORTSW) \
25 X("snoop", 0, SNOOPSW) \
26 X("nosnoop", 0, NOSNOOPSW) \
27 X("sasl", 0, SASLSW) \
28 X("nosasl", 0, NOSASLSW) \
29 X("saslmech", 0, SASLMECHSW) \
30 X("authservice", 0, AUTHSERVICESW) \
32 X("notls", 0, NOTLSSW) \
33 X("initialtls", 0, INITIALTLSSW) \
34 X("queue", 0, QUEUESW) \
35 X("noqueue", 0, NOQUEUESW) \
36 X("append filename", 0, APPENDSW) \
37 X("afolder foldername", 0, AFOLDERSW) \
38 X("batch filename", 0, BATCHSW) \
39 X("timestamp", 0, TIMESTAMPSW) \
40 X("notimestamp", 0, NOTIMESTAMPSW) \
41 X("timeout", 0, TIMEOUTSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(IMAPTEST
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(IMAPTEST
, switches
);
56 char *command
; /* Command to send */
57 char *folder
; /* Folder (for append) */
58 bool queue
; /* If true, queue for later delivery */
59 bool append
; /* If true, append "command" to mbox */
60 size_t msgsize
; /* RFC822 size of message */
61 struct imap_msg
*next
; /* Next pointer */
64 struct imap_msg
*msgqueue_head
= NULL
;
65 struct imap_msg
*msgqueue_tail
= NULL
;
70 char tag
[16]; /* Command tag */
71 struct timeval start
; /* Time command was sent */
72 char prefix
[64]; /* Command prefix */
73 struct imap_cmd
*next
; /* Next pointer */
76 static struct imap_cmd
*cmdqueue
= NULL
;
78 static char *saslmechs
= NULL
;
79 svector_t imap_capabilities
= NULL
;
81 static int imap_sasl_callback(enum sasl_message_type
, unsigned const char *,
82 unsigned int, unsigned char **, unsigned int *,
85 static void parse_capability(const char *, unsigned int len
);
86 static int capability_set(const char *);
87 static void clear_capability(void);
88 static int have_capability(void);
89 static int send_imap_command(netsec_context
*, bool noflush
, char **errstr
,
90 const char *fmt
, ...) CHECK_PRINTF(4, 5);
91 static int send_append(netsec_context
*, struct imap_msg
*, char **errstr
);
92 static int get_imap_response(netsec_context
*, const char *token
,
93 char **tokenresp
, char **status
, bool contok
,
94 bool failerr
, char **errstr
);
96 static void ts_report(struct timeval
*tv
, const char *fmt
, ...)
98 static void imap_negotiate_tls(netsec_context
*);
100 static void add_msg(bool queue
, struct imap_msg
**, const char *fmt
, ...)
102 static void add_append(const char *filename
, const char *folder
, bool queue
);
104 static void batchfile(const char *filename
, char *afolder
, bool queue
);
106 static size_t rfc822size(const char *filename
);
108 static bool timestamp
= false;
111 main (int argc
, char **argv
)
113 bool sasl
= false, tls
= false, initialtls
= false;
114 bool snoop
= false, queue
= false;
116 char *saslmech
= NULL
, *host
= NULL
, *port
= "143", *user
= NULL
;
117 char *cp
, **argp
, buf
[BUFSIZ
], *oauth_svc
= NULL
, *errstr
, **arguments
, *p
;
118 char *afolder
= NULL
;
119 netsec_context
*nsc
= NULL
;
120 struct imap_msg
*imsg
;
122 struct timeval tv_start
, tv_connect
, tv_auth
;
124 if (nmh_init(argv
[0], true, true)) { return 1; }
126 arguments
= getarguments (invo_name
, argc
, argv
, 1);
129 while ((cp
= *argp
++)) {
131 switch (smatch (++cp
, switches
)) {
133 ambigsw (cp
, switches
);
136 die("-%s unknown", cp
);
139 snprintf (buf
, sizeof(buf
), "%s [switches] +folder command "
140 "[command ...]", invo_name
);
141 print_help (buf
, switches
, 1);
144 print_version(invo_name
);
148 if (!(host
= *argp
++) || *host
== '-')
149 die("missing argument to %s", argp
[-2]);
152 if (!(port
= *argp
++) || *port
== '-')
153 die("missing argument to %s", argp
[-2]);
156 if (!(user
= *argp
++) || *user
== '-')
157 die("missing argument to %s", argp
[-2]);
161 if (!(afolder
= *argp
++) || *afolder
== '-')
162 die("missing argument to %s", argp
[-2]);
165 if (!*argp
|| (**argp
== '-'))
166 die("missing argument to %s", argp
[-1]);
168 die("Append folder must be set with -afolder first");
169 add_append(*argp
++, afolder
, queue
);
173 if (! *argp
|| (**argp
== '-'))
174 die("missing argument to %s", argp
[-1]);
175 batchfile(*argp
++, afolder
, queue
);
179 if (! *argp
|| (**argp
== '-'))
180 die("missing argument to %s", argp
[-1]);
181 if (! (timeout
= atoi(*argp
++)))
182 die("Invalid timeout: %s", argp
[-1]);
198 if (!(saslmech
= *argp
++) || *saslmech
== '-')
199 die("missing argument to %s", argp
[-2]);
202 if (!(oauth_svc
= *argp
++) || *oauth_svc
== '-')
203 die("missing argument to %s", argp
[-2]);
230 } else if (*cp
== '+') {
231 if (*(cp
+ 1) == '\0')
232 die("Invalid null folder name");
233 add_msg(false, NULL
, "SELECT \"%s\"", cp
+ 1);
235 add_msg(queue
, NULL
, "%s", cp
);
240 die("A hostname must be given with -host");
245 netsec_set_userid(nsc
, user
);
247 netsec_set_hostname(nsc
, host
);
250 netsec_set_timeout(nsc
, timeout
);
253 netsec_set_snoop(nsc
, 1);
256 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
257 die("OAuth2 not supported");
262 gettimeofday(&tv_start
, NULL
);
264 if ((fd
= client(host
, port
, buf
, sizeof(buf
), snoop
)) == NOTOK
)
265 die("Connect failed: %s", buf
);
268 ts_report(&tv_start
, "Connect time");
269 gettimeofday(&tv_connect
, NULL
);
272 netsec_set_fd(nsc
, fd
, fd
);
273 netsec_set_snoop(nsc
, snoop
);
275 if (initialtls
|| tls
) {
276 if (netsec_set_tls(nsc
, 1, 0, &errstr
) != OK
)
280 imap_negotiate_tls(nsc
);
284 if (netsec_set_sasl_params(nsc
, "imap", saslmech
, imap_sasl_callback
,
289 if ((cp
= netsec_readline(nsc
, &len
, &errstr
)) == NULL
) {
293 if (has_prefix(cp
, "* BYE")) {
294 fprintf(stderr
, "Connection rejected: %s\n", cp
+ 5);
298 if (!has_prefix(cp
, "* OK") && !has_prefix(cp
, "* PREAUTH")) {
299 fprintf(stderr
, "Invalid server response: %s\n", cp
);
303 if ((p
= strchr(cp
+ 2, ' ')) && *(p
+ 1) != '\0' &&
304 has_prefix(p
+ 1, "[CAPABILITY ")) {
306 * Parse the capability list out to the end
309 p
+= 13; /* 1 + [CAPABILITY + space */
311 if (!(q
= strchr(p
, ']'))) {
312 fprintf(stderr
, "Cannot find end of CAPABILITY announcement\n");
316 parse_capability(p
, q
- p
);
320 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
321 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
325 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false, true,
327 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
332 fprintf(stderr
, "No CAPABILITY response seen\n");
336 p
= capstring
+ 11; /* "CAPABILITY " */
338 parse_capability(p
, strlen(p
));
343 if (!capability_set("STARTTLS")) {
344 fprintf(stderr
, "Requested STARTTLS with -tls, but IMAP server "
345 "has no support for STARTTLS\n");
348 if (send_imap_command(nsc
, false, &errstr
, "STARTTLS") != OK
) {
349 fprintf(stderr
, "Unable to issue STARTTLS: %s\n", errstr
);
353 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, true,
355 fprintf(stderr
, "STARTTLS failed: %s\n", errstr
);
359 imap_negotiate_tls(nsc
);
363 if (netsec_negotiate_sasl(nsc
, saslmechs
, &errstr
) != OK
) {
364 fprintf(stderr
, "SASL negotiation failed: %s\n", errstr
);
368 * Sigh. If we negotiated a SSF AND we got a capability response
369 * as part of the AUTHENTICATE OK message, we can't actually trust
370 * it because it's not protected at that point. So discard the
371 * capability list and we will generate a fresh CAPABILITY command
374 if (netsec_get_sasl_ssf(nsc
) > 0) {
378 if (capability_set("LOGINDISABLED")) {
379 fprintf(stderr
, "User did not request SASL, but LOGIN "
385 if (!have_capability()) {
388 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
389 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
393 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false,
394 true, &errstr
) != OK
) {
395 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
400 fprintf(stderr
, "No CAPABILITY response seen\n");
404 p
= capstring
+ 11; /* "CAPABILITY " */
406 parse_capability(p
, strlen(p
));
411 ts_report(&tv_connect
, "Authentication time");
412 gettimeofday(&tv_auth
, NULL
);
415 while (msgqueue_head
!= NULL
) {
416 imsg
= msgqueue_head
;
419 if (send_append(nsc
, imsg
, &errstr
) != OK
) {
420 fprintf(stderr
, "Cannot send APPEND for %s to mbox %s: %s\n",
421 imsg
->command
, imsg
->folder
, errstr
);
425 } else if (send_imap_command(nsc
, imsg
->queue
, &errstr
, "%s",
426 imsg
->command
) != OK
) {
427 fprintf(stderr
, "Cannot send command \"%s\": %s\n",
428 imsg
->command
, errstr
);
434 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false,
436 fprintf(stderr
, "Unable to get response for command "
437 "\"%s\": %s\n", imsg
->command
, errstr
);
442 msgqueue_head
= imsg
->next
;
450 * Flush out any pending network data and get any responses
453 if (netsec_flush(nsc
, &errstr
) != OK
) {
454 fprintf(stderr
, "Error performing final network flush: %s\n", errstr
);
458 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, &errstr
) != OK
) {
459 fprintf(stderr
, "Error fetching final command responses: %s\n", errstr
);
464 ts_report(&tv_auth
, "Total command execution time");
466 send_imap_command(nsc
, false, NULL
, "LOGOUT");
467 get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, NULL
);
470 netsec_shutdown(nsc
);
473 ts_report(&tv_start
, "Total elapsed time");
479 * Parse a capability string
481 * Put these into an svector so we can check for stuff later. But special
482 * things, like AUTH, we parse now
486 parse_capability(const char *cap
, unsigned int len
)
488 char *str
= mh_xmalloc(len
+ 1);
492 trunccpy(str
, cap
, len
+ 1);
493 caplist
= brkstring(str
, " ", NULL
);
495 if (imap_capabilities
) {
496 svector_free(imap_capabilities
);
499 imap_capabilities
= svector_create(32);
506 for (i
= 0; caplist
[i
] != NULL
; i
++) {
507 if (has_prefix(caplist
[i
], "AUTH=") && *(caplist
[i
] + 5) != '\0') {
509 saslmechs
= add(" ", saslmechs
);
511 saslmechs
= add(caplist
[i
] + 5, saslmechs
);
513 svector_push_back(imap_capabilities
, getcpy(caplist
[i
]));
521 * Return 1 if a particular capability is set
525 capability_set(const char *capability
)
527 if (!imap_capabilities
)
530 return svector_find(imap_capabilities
, capability
) != NULL
;
534 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
538 clear_capability(void)
540 svector_free(imap_capabilities
);
541 imap_capabilities
= NULL
;
545 have_capability(void)
547 return imap_capabilities
!= NULL
;
551 * Our SASL callback, which handles the SASL authentication dialog
555 imap_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
556 unsigned int indatalen
, unsigned char **outdata
,
557 unsigned int *outdatalen
, void *context
, char **errstr
)
560 char *mech
, *line
, *capstring
, *p
;
562 netsec_context
*nsc
= (netsec_context
*) context
;
565 case NETSEC_SASL_START
:
567 * Generate our AUTHENTICATE message.
569 * If SASL-IR capability is set, we can include any initial response
570 * in the AUTHENTICATE command. Otherwise we have to wait for
571 * the first server response (which should be blank).
574 mech
= netsec_get_sasl_mechanism(nsc
);
578 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
579 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
580 if (capability_set("SASL-IR")) {
581 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
584 * Sigh. We're assuming a short tag length here. Really,
585 * I guess we should figure out a way to get the length
588 snoopoffset
= 17 + strlen(mech
);
589 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s %s",
592 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
596 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s", mech
);
597 line
= netsec_readline(nsc
, &len
, errstr
);
601 * We should get a "+ ", nothing else.
603 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
604 netsec_err(errstr
, "Did not get expected blank response "
605 "for initial challenge response");
608 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
612 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
613 rc
= netsec_flush(nsc
, errstr
);
614 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
619 if (send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s",
627 * Get a response, decode it and process it.
630 case NETSEC_SASL_READ
:
631 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
633 line
= netsec_readline(nsc
, &len
, errstr
);
634 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
639 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
640 netsec_err(errstr
, "Invalid format for SASL response");
648 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
651 netsec_err(errstr
, "Unable to decode base64 response");
658 * Simple request encoding
661 case NETSEC_SASL_WRITE
:
663 unsigned char *b64data
;
664 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
665 writeBase64raw(indata
, indatalen
, b64data
);
666 rc
= netsec_printf(nsc
, errstr
, "%s", b64data
);
672 if (netsec_printf(nsc
, errstr
, "\r\n") != OK
)
675 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
676 rc
= netsec_flush(nsc
, errstr
);
677 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
686 case NETSEC_SASL_FINISH
:
688 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, &line
,
689 false, true, errstr
) != OK
)
692 * We MIGHT get a capability response here. If so, be sure we
693 * parse it. We ALSO might get a untagged CAPABILITY response.
694 * Which one should we prefer? I guess we'll go with the untagged
699 p
= capstring
+ 11; /* "CAPABILITY " */
700 parse_capability(p
, strlen(p
));
702 } else if (line
&& has_prefix(line
, "OK [CAPABILITY ")) {
709 parse_capability(p
, q
- p
);
715 * Cancel an authentication dialog
718 case NETSEC_SASL_CANCEL
:
719 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
721 rc
= netsec_flush(nsc
, errstr
);
730 * Send a single command to the IMAP server.
734 send_imap_command(netsec_context
*nsc
, bool noflush
, char **errstr
,
735 const char *fmt
, ...)
737 static unsigned int seq
= 0; /* Tag sequence number */
740 struct imap_cmd
*cmd
;
742 cmd
= mh_xmalloc(sizeof(*cmd
));
744 snprintf(cmd
->tag
, sizeof(cmd
->tag
), "A%u ", seq
++);
749 vsnprintf(cmd
->prefix
, sizeof(cmd
->prefix
), fmt
, ap
);
752 p
= strchr(cmd
->prefix
, ' ');
757 gettimeofday(&cmd
->start
, NULL
);
760 if (netsec_write(nsc
, cmd
->tag
, strlen(cmd
->tag
), errstr
) != OK
) {
766 rc
= netsec_vprintf(nsc
, errstr
, fmt
, ap
);
770 rc
= netsec_write(nsc
, "\r\n", 2, errstr
);
777 if (!noflush
&& netsec_flush(nsc
, errstr
) != OK
) {
782 cmd
->next
= cmdqueue
;
789 * Send an APPEND to the server, which requires some extra semantics
793 send_append(netsec_context
*nsc
, struct imap_msg
*imsg
, char **errstr
)
796 bool nonsynlit
= false;
797 char *status
= NULL
, *line
= NULL
;
802 * If we have the LITERAL+ extension, or we have LITERAL- and our
803 * message is 4096 bytes or less, we can do it all in one message.
804 * Otherwise we have to wait for a contination marker (+).
807 if (capability_set("LITERAL+") ||
808 (capability_set("LITERAL-") && imsg
->msgsize
<= 4096)) {
813 * Send our APPEND command
816 if (send_imap_command(nsc
, nonsynlit
, errstr
, "APPEND \"%s\" {%u%s}",
817 imsg
->folder
, (unsigned int) imsg
->msgsize
,
818 nonsynlit
? "+" : "") != OK
)
822 * If we need to wait for a syncing literal, do that now
826 if (get_imap_response(nsc
, NULL
, NULL
, &status
, true,
827 true, errstr
) != OK
) {
828 imsg
->queue
= true; /* XXX Sigh */
829 fprintf(stderr
, "APPEND command failed: %s\n", *errstr
);
833 if (!(status
&& has_prefix(status
, "+"))) {
834 netsec_err(errstr
, "Expected contination (+), but got: %s", status
);
842 * Now write the message out, but make sure we end each line with \r\n
845 if ((f
= fopen(imsg
->command
, "r")) == NULL
) {
846 netsec_err(errstr
, "Unable to open %s: %s", imsg
->command
,
851 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
852 if (rc
> 1 && line
[rc
- 1] == '\n' && line
[rc
- 2] == '\r') {
853 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
859 if (line
[rc
- 1] == '\n')
861 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
866 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
) {
877 netsec_err(errstr
, "Error reading %s: %s", imsg
->command
,
886 * Send a final \r\n for the end of the command
889 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
)
893 return netsec_flush(nsc
, errstr
);
899 * Get all outstanding responses. If we were passed in a token string
900 * to look for, return it.
904 get_imap_response(netsec_context
*nsc
, const char *token
, char **tokenresponse
,
905 char **status
, bool condok
, bool failerr
, char **errstr
)
908 struct imap_cmd
*cmd
;
909 bool numerrs
= false;
912 *tokenresponse
= NULL
;
916 if (!(line
= netsec_readline(nsc
, NULL
, errstr
)))
918 if (has_prefix(line
, "* ") && *(line
+ 2) != '\0') {
919 if (token
&& tokenresponse
&& has_prefix(line
+ 2, token
)) {
921 free(*tokenresponse
);
922 *tokenresponse
= getcpy(line
+ 2);
924 } if (condok
&& has_prefix(line
, "+")) {
926 *status
= getcpy(line
);
929 * Special case; return now but don't dequeue the tag,
930 * since we will want to get final result later.
935 if (has_prefix(line
, cmdqueue
->tag
)) {
938 ts_report(&cmd
->start
, "Command (%s) execution time",
940 cmdqueue
= cmd
->next
;
943 for (cmd
= cmdqueue
; cmd
->next
!= NULL
; cmd
= cmd
->next
) {
944 if (has_prefix(line
, cmd
->next
->tag
)) {
945 struct imap_cmd
*cmd2
= cmd
->next
;
946 cmd
->next
= cmd
->next
->next
;
947 if (failerr
&& strncmp(line
+ strlen(cmd2
->tag
),
950 netsec_err(errstr
, "%s", line
+ strlen(cmd2
->tag
));
953 ts_report(&cmd2
->start
, "Command (%s) execution "
954 "time", cmd2
->prefix
);
957 *status
= getcpy(line
);
965 return numerrs
? NOTOK
: OK
;
969 * Add an IMAP command to the msg queue
973 add_msg(bool queue
, struct imap_msg
**ret_imsg
, const char *fmt
, ...)
975 struct imap_msg
*imsg
;
983 msg
= mh_xrealloc(msg
, msgbufsize
);
985 rc
= vsnprintf(msg
, msgbufsize
, fmt
, ap
);
987 } while (rc
>= (int) msgbufsize
);
989 imsg
= mh_xmalloc(sizeof(*imsg
));
993 imsg
->append
= false;
997 if (msgqueue_head
== NULL
) {
998 msgqueue_head
= imsg
;
999 msgqueue_tail
= imsg
;
1001 msgqueue_tail
->next
= imsg
;
1002 msgqueue_tail
= imsg
;
1010 * Add an APPEND command to the queue
1014 add_append(const char *filename
, const char *folder
, bool queue
)
1016 size_t filesize
= rfc822size(filename
);
1017 struct imap_msg
*imsg
;
1019 add_msg(queue
, &imsg
, "%s", filename
);
1021 imsg
->folder
= getcpy(folder
);
1022 imsg
->append
= true;
1023 imsg
->msgsize
= filesize
;
1027 * Process a batch file, which can contain commands (and some arguments)
1031 batchfile(const char *filename
, char *afolder
, bool queue
)
1035 size_t linesize
= 0;
1037 bool afolder_alloc
= false;
1039 if (!(f
= fopen(filename
, "r"))) {
1040 die("Unable to open batch file %s: %s", filename
, strerror(errno
));
1043 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
1044 line
[rc
- 1] = '\0';
1046 switch (smatch (line
+ 1, switches
)) {
1056 rc
= getline(&line
, &linesize
, f
);
1058 die("Unable to read next line for -afolder");
1060 die("Folder name cannot be blank");
1061 line
[rc
- 1] = '\0';
1062 afolder
= getcpy(line
);
1063 afolder_alloc
= true;
1067 rc
= getline(&line
, &linesize
, f
);
1069 die("Unable to read filename for -append");
1071 die("Filename for -append cannot be blank");
1072 line
[rc
- 1] = '\0';
1073 add_append(line
, afolder
, queue
);
1077 ambigsw (line
, switches
);
1080 die("%s unknown", line
);
1082 die("Switch %s not supported in batch mode", line
);
1084 } else if (*line
== '+') {
1085 if (*(line
+ 1) == '\0')
1086 die("Invalid null folder name");
1087 add_msg(false, NULL
, "SELECT \"%s\"", line
+ 1);
1090 continue; /* Ignore blank line */
1091 add_msg(queue
, NULL
, "%s", line
);
1096 die("Read of \"%s\" failed: %s", filename
, strerror(errno
));
1108 * Negotiate TLS connection, with optional timestamp
1112 imap_negotiate_tls(netsec_context
*nsc
)
1118 gettimeofday(&tv
, NULL
);
1120 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
)
1124 ts_report(&tv
, "TLS negotation time");
1128 * Give a timestamp report.
1132 ts_report(struct timeval
*tv
, const char *fmt
, ...)
1138 gettimeofday(&now
, NULL
);
1140 delta
= ((double) now
.tv_sec
) - ((double) tv
->tv_sec
) +
1141 (now
.tv_usec
/ 1E6
- tv
->tv_usec
/ 1E6
);
1144 vfprintf(stdout
, fmt
, ap
);
1147 printf(": %f sec\n", delta
);
1151 * Calculate the RFC 822 size of file.
1155 rfc822size(const char *filename
)
1158 size_t total
= 0, linecap
= 0;
1162 if (! (f
= fopen(filename
, "r")))
1163 die("Unable to open %s: %s", filename
, strerror(errno
));
1165 while ((rc
= getline(&line
, &linecap
, f
)) > 0) {
1167 if (line
[rc
- 1] == '\n' && (rc
== 1 || line
[rc
- 2] != '\r'))
1169 if (line
[rc
- 1] != '\n')
1176 die("Error while reading %s: %s", filename
, strerror(errno
));