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/print_help.h"
10 #include "sbr/error.h"
16 #include "sbr/base64.h"
18 #define IMAPTEST_SWITCHES \
19 X("host hostname", 0, HOSTSW) \
20 X("user username", 0, USERSW) \
21 X("port name/number", 0, PORTSW) \
22 X("snoop", 0, SNOOPSW) \
23 X("nosnoop", 0, NOSNOOPSW) \
24 X("sasl", 0, SASLSW) \
25 X("nosasl", 0, NOSASLSW) \
26 X("saslmech", 0, SASLMECHSW) \
27 X("authservice", 0, AUTHSERVICESW) \
29 X("notls", 0, NOTLSSW) \
30 X("initialtls", 0, INITIALTLSSW) \
31 X("queue", 0, QUEUESW) \
32 X("noqueue", 0, NOQUEUESW) \
33 X("append filename", 0, APPENDSW) \
34 X("afolder foldername", 0, AFOLDERSW) \
35 X("batch filename", 0, BATCHSW) \
36 X("timestamp", 0, TIMESTAMPSW) \
37 X("notimestamp", 0, NOTIMESTAMPSW) \
38 X("timeout", 0, TIMEOUTSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(IMAPTEST
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(IMAPTEST
, switches
);
53 char *command
; /* Command to send */
54 char *folder
; /* Folder (for append) */
55 bool queue
; /* If true, queue for later delivery */
56 bool append
; /* If true, append "command" to mbox */
57 size_t msgsize
; /* RFC822 size of message */
58 struct imap_msg
*next
; /* Next pointer */
61 struct imap_msg
*msgqueue_head
= NULL
;
62 struct imap_msg
*msgqueue_tail
= NULL
;
67 char tag
[16]; /* Command tag */
68 struct timeval start
; /* Time command was sent */
69 char prefix
[64]; /* Command prefix */
70 struct imap_cmd
*next
; /* Next pointer */
73 static struct imap_cmd
*cmdqueue
= NULL
;
75 static char *saslmechs
= NULL
;
76 svector_t imap_capabilities
= NULL
;
78 static int imap_sasl_callback(enum sasl_message_type
, unsigned const char *,
79 unsigned int, unsigned char **, unsigned int *,
82 static void parse_capability(const char *, unsigned int len
);
83 static int capability_set(const char *);
84 static void clear_capability(void);
85 static int have_capability(void);
86 static int send_imap_command(netsec_context
*, bool noflush
, char **errstr
,
87 const char *fmt
, ...) CHECK_PRINTF(4, 5);
88 static int send_append(netsec_context
*, struct imap_msg
*, char **errstr
);
89 static int get_imap_response(netsec_context
*, const char *token
,
90 char **tokenresp
, char **status
, bool contok
,
91 bool failerr
, char **errstr
);
93 static void ts_report(struct timeval
*tv
, const char *fmt
, ...)
95 static void imap_negotiate_tls(netsec_context
*);
97 static void add_msg(bool queue
, struct imap_msg
**, const char *fmt
, ...)
99 static void add_append(const char *filename
, const char *folder
, bool queue
);
101 static void batchfile(const char *filename
, char *afolder
, bool queue
);
103 static size_t rfc822size(const char *filename
);
105 static bool timestamp
= false;
108 main (int argc
, char **argv
)
110 bool sasl
= false, tls
= false, initialtls
= false;
111 bool snoop
= false, queue
= false;
113 char *saslmech
= NULL
, *host
= NULL
, *port
= "143", *user
= NULL
;
114 char *cp
, **argp
, buf
[BUFSIZ
], *oauth_svc
= NULL
, *errstr
, **arguments
, *p
;
115 char *afolder
= NULL
;
116 netsec_context
*nsc
= NULL
;
117 struct imap_msg
*imsg
;
119 struct timeval tv_start
, tv_connect
, tv_auth
;
121 if (nmh_init(argv
[0], true, true)) { return 1; }
123 arguments
= getarguments (invo_name
, argc
, argv
, 1);
126 while ((cp
= *argp
++)) {
128 switch (smatch (++cp
, switches
)) {
130 ambigsw (cp
, switches
);
133 die("-%s unknown", cp
);
136 snprintf (buf
, sizeof(buf
), "%s [switches] +folder command "
137 "[command ...]", invo_name
);
138 print_help (buf
, switches
, 1);
141 print_version(invo_name
);
145 if (!(host
= *argp
++) || *host
== '-')
146 die("missing argument to %s", argp
[-2]);
149 if (!(port
= *argp
++) || *port
== '-')
150 die("missing argument to %s", argp
[-2]);
153 if (!(user
= *argp
++) || *user
== '-')
154 die("missing argument to %s", argp
[-2]);
158 if (!(afolder
= *argp
++) || *afolder
== '-')
159 die("missing argument to %s", argp
[-2]);
162 if (!*argp
|| (**argp
== '-'))
163 die("missing argument to %s", argp
[-1]);
165 die("Append folder must be set with -afolder first");
166 add_append(*argp
++, afolder
, queue
);
170 if (! *argp
|| (**argp
== '-'))
171 die("missing argument to %s", argp
[-1]);
172 batchfile(*argp
++, afolder
, queue
);
176 if (! *argp
|| (**argp
== '-'))
177 die("missing argument to %s", argp
[-1]);
178 if (! (timeout
= atoi(*argp
++)))
179 die("Invalid timeout: %s", argp
[-1]);
195 if (!(saslmech
= *argp
++) || *saslmech
== '-')
196 die("missing argument to %s", argp
[-2]);
199 if (!(oauth_svc
= *argp
++) || *oauth_svc
== '-')
200 die("missing argument to %s", argp
[-2]);
227 } else if (*cp
== '+') {
228 if (*(cp
+ 1) == '\0')
229 die("Invalid null folder name");
230 add_msg(false, NULL
, "SELECT \"%s\"", cp
+ 1);
232 add_msg(queue
, NULL
, "%s", cp
);
237 die("A hostname must be given with -host");
242 netsec_set_userid(nsc
, user
);
244 netsec_set_hostname(nsc
, host
);
247 netsec_set_timeout(nsc
, timeout
);
250 netsec_set_snoop(nsc
, 1);
253 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
254 die("OAuth2 not supported");
259 gettimeofday(&tv_start
, NULL
);
261 if ((fd
= client(host
, port
, buf
, sizeof(buf
), snoop
)) == NOTOK
)
262 die("Connect failed: %s", buf
);
265 ts_report(&tv_start
, "Connect time");
266 gettimeofday(&tv_connect
, NULL
);
269 netsec_set_fd(nsc
, fd
, fd
);
270 netsec_set_snoop(nsc
, snoop
);
272 if (initialtls
|| tls
) {
273 if (netsec_set_tls(nsc
, 1, 0, &errstr
) != OK
)
277 imap_negotiate_tls(nsc
);
281 if (netsec_set_sasl_params(nsc
, "imap", saslmech
, imap_sasl_callback
,
286 if ((cp
= netsec_readline(nsc
, &len
, &errstr
)) == NULL
) {
290 if (has_prefix(cp
, "* BYE")) {
291 fprintf(stderr
, "Connection rejected: %s\n", cp
+ 5);
295 if (!has_prefix(cp
, "* OK") && !has_prefix(cp
, "* PREAUTH")) {
296 fprintf(stderr
, "Invalid server response: %s\n", cp
);
300 if ((p
= strchr(cp
+ 2, ' ')) && *(p
+ 1) != '\0' &&
301 has_prefix(p
+ 1, "[CAPABILITY ")) {
303 * Parse the capability list out to the end
306 p
+= 13; /* 1 + [CAPABILITY + space */
308 if (!(q
= strchr(p
, ']'))) {
309 fprintf(stderr
, "Cannot find end of CAPABILITY announcement\n");
313 parse_capability(p
, q
- p
);
317 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
318 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
322 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false, true,
324 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
329 fprintf(stderr
, "No CAPABILITY response seen\n");
333 p
= capstring
+ 11; /* "CAPABILITY " */
335 parse_capability(p
, strlen(p
));
340 if (!capability_set("STARTTLS")) {
341 fprintf(stderr
, "Requested STARTTLS with -tls, but IMAP server "
342 "has no support for STARTTLS\n");
345 if (send_imap_command(nsc
, false, &errstr
, "STARTTLS") != OK
) {
346 fprintf(stderr
, "Unable to issue STARTTLS: %s\n", errstr
);
350 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, true,
352 fprintf(stderr
, "STARTTLS failed: %s\n", errstr
);
356 imap_negotiate_tls(nsc
);
360 if (netsec_negotiate_sasl(nsc
, saslmechs
, &errstr
) != OK
) {
361 fprintf(stderr
, "SASL negotiation failed: %s\n", errstr
);
365 * Sigh. If we negotiated a SSF AND we got a capability response
366 * as part of the AUTHENTICATE OK message, we can't actually trust
367 * it because it's not protected at that point. So discard the
368 * capability list and we will generate a fresh CAPABILITY command
371 if (netsec_get_sasl_ssf(nsc
) > 0) {
375 if (capability_set("LOGINDISABLED")) {
376 fprintf(stderr
, "User did not request SASL, but LOGIN "
382 if (!have_capability()) {
385 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
386 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
390 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false,
391 true, &errstr
) != OK
) {
392 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
397 fprintf(stderr
, "No CAPABILITY response seen\n");
401 p
= capstring
+ 11; /* "CAPABILITY " */
403 parse_capability(p
, strlen(p
));
408 ts_report(&tv_connect
, "Authentication time");
409 gettimeofday(&tv_auth
, NULL
);
412 while (msgqueue_head
!= NULL
) {
413 imsg
= msgqueue_head
;
416 if (send_append(nsc
, imsg
, &errstr
) != OK
) {
417 fprintf(stderr
, "Cannot send APPEND for %s to mbox %s: %s\n",
418 imsg
->command
, imsg
->folder
, errstr
);
422 } else if (send_imap_command(nsc
, imsg
->queue
, &errstr
, "%s",
423 imsg
->command
) != OK
) {
424 fprintf(stderr
, "Cannot send command \"%s\": %s\n",
425 imsg
->command
, errstr
);
431 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false,
433 fprintf(stderr
, "Unable to get response for command "
434 "\"%s\": %s\n", imsg
->command
, errstr
);
439 msgqueue_head
= imsg
->next
;
447 * Flush out any pending network data and get any responses
450 if (netsec_flush(nsc
, &errstr
) != OK
) {
451 fprintf(stderr
, "Error performing final network flush: %s\n", errstr
);
455 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, &errstr
) != OK
) {
456 fprintf(stderr
, "Error fetching final command responses: %s\n", errstr
);
461 ts_report(&tv_auth
, "Total command execution time");
463 send_imap_command(nsc
, false, NULL
, "LOGOUT");
464 get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, NULL
);
467 netsec_shutdown(nsc
);
470 ts_report(&tv_start
, "Total elapsed time");
476 * Parse a capability string
478 * Put these into an svector so we can check for stuff later. But special
479 * things, like AUTH, we parse now
483 parse_capability(const char *cap
, unsigned int len
)
485 char *str
= mh_xmalloc(len
+ 1);
489 trunccpy(str
, cap
, len
+ 1);
490 caplist
= brkstring(str
, " ", NULL
);
492 if (imap_capabilities
) {
493 svector_free(imap_capabilities
);
496 imap_capabilities
= svector_create(32);
503 for (i
= 0; caplist
[i
] != NULL
; i
++) {
504 if (has_prefix(caplist
[i
], "AUTH=") && *(caplist
[i
] + 5) != '\0') {
506 saslmechs
= add(" ", saslmechs
);
508 saslmechs
= add(caplist
[i
] + 5, saslmechs
);
510 svector_push_back(imap_capabilities
, getcpy(caplist
[i
]));
518 * Return 1 if a particular capability is set
522 capability_set(const char *capability
)
524 if (!imap_capabilities
)
527 return svector_find(imap_capabilities
, capability
) != NULL
;
531 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
535 clear_capability(void)
537 svector_free(imap_capabilities
);
538 imap_capabilities
= NULL
;
542 have_capability(void)
544 return imap_capabilities
!= NULL
;
548 * Our SASL callback, which handles the SASL authentication dialog
552 imap_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
553 unsigned int indatalen
, unsigned char **outdata
,
554 unsigned int *outdatalen
, void *context
, char **errstr
)
557 char *mech
, *line
, *capstring
, *p
;
559 netsec_context
*nsc
= (netsec_context
*) context
;
562 case NETSEC_SASL_START
:
564 * Generate our AUTHENTICATE message.
566 * If SASL-IR capability is set, we can include any initial response
567 * in the AUTHENTICATE command. Otherwise we have to wait for
568 * the first server response (which should be blank).
571 mech
= netsec_get_sasl_mechanism(nsc
);
575 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
576 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
577 if (capability_set("SASL-IR")) {
578 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
581 * Sigh. We're assuming a short tag length here. Really,
582 * I guess we should figure out a way to get the length
585 snoopoffset
= 17 + strlen(mech
);
586 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s %s",
589 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
593 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s", mech
);
594 line
= netsec_readline(nsc
, &len
, errstr
);
598 * We should get a "+ ", nothing else.
600 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
601 netsec_err(errstr
, "Did not get expected blank response "
602 "for initial challenge response");
605 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
609 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
610 rc
= netsec_flush(nsc
, errstr
);
611 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
616 if (send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s",
624 * Get a response, decode it and process it.
627 case NETSEC_SASL_READ
:
628 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
630 line
= netsec_readline(nsc
, &len
, errstr
);
631 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
636 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
637 netsec_err(errstr
, "Invalid format for SASL response");
645 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
648 netsec_err(errstr
, "Unable to decode base64 response");
655 * Simple request encoding
658 case NETSEC_SASL_WRITE
:
660 unsigned char *b64data
;
661 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
662 writeBase64raw(indata
, indatalen
, b64data
);
663 rc
= netsec_printf(nsc
, errstr
, "%s", b64data
);
669 if (netsec_printf(nsc
, errstr
, "\r\n") != OK
)
672 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
673 rc
= netsec_flush(nsc
, errstr
);
674 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
683 case NETSEC_SASL_FINISH
:
685 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, &line
,
686 false, true, errstr
) != OK
)
689 * We MIGHT get a capability response here. If so, be sure we
690 * parse it. We ALSO might get a untagged CAPABILITY response.
691 * Which one should we prefer? I guess we'll go with the untagged
696 p
= capstring
+ 11; /* "CAPABILITY " */
697 parse_capability(p
, strlen(p
));
699 } else if (line
&& has_prefix(line
, "OK [CAPABILITY ")) {
706 parse_capability(p
, q
- p
);
712 * Cancel an authentication dialog
715 case NETSEC_SASL_CANCEL
:
716 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
718 rc
= netsec_flush(nsc
, errstr
);
727 * Send a single command to the IMAP server.
731 send_imap_command(netsec_context
*nsc
, bool noflush
, char **errstr
,
732 const char *fmt
, ...)
734 static unsigned int seq
= 0; /* Tag sequence number */
737 struct imap_cmd
*cmd
;
739 cmd
= mh_xmalloc(sizeof(*cmd
));
741 snprintf(cmd
->tag
, sizeof(cmd
->tag
), "A%u ", seq
++);
746 vsnprintf(cmd
->prefix
, sizeof(cmd
->prefix
), fmt
, ap
);
749 p
= strchr(cmd
->prefix
, ' ');
754 gettimeofday(&cmd
->start
, NULL
);
757 if (netsec_write(nsc
, cmd
->tag
, strlen(cmd
->tag
), errstr
) != OK
) {
763 rc
= netsec_vprintf(nsc
, errstr
, fmt
, ap
);
767 rc
= netsec_write(nsc
, "\r\n", 2, errstr
);
774 if (!noflush
&& netsec_flush(nsc
, errstr
) != OK
) {
779 cmd
->next
= cmdqueue
;
786 * Send an APPEND to the server, which requires some extra semantics
790 send_append(netsec_context
*nsc
, struct imap_msg
*imsg
, char **errstr
)
793 bool nonsynlit
= false;
794 char *status
= NULL
, *line
= NULL
;
799 * If we have the LITERAL+ extension, or we have LITERAL- and our
800 * message is 4096 bytes or less, we can do it all in one message.
801 * Otherwise we have to wait for a contination marker (+).
804 if (capability_set("LITERAL+") ||
805 (capability_set("LITERAL-") && imsg
->msgsize
<= 4096)) {
810 * Send our APPEND command
813 if (send_imap_command(nsc
, nonsynlit
, errstr
, "APPEND \"%s\" {%u%s}",
814 imsg
->folder
, (unsigned int) imsg
->msgsize
,
815 nonsynlit
? "+" : "") != OK
)
819 * If we need to wait for a syncing literal, do that now
823 if (get_imap_response(nsc
, NULL
, NULL
, &status
, true,
824 true, errstr
) != OK
) {
825 imsg
->queue
= true; /* XXX Sigh */
826 fprintf(stderr
, "APPEND command failed: %s\n", *errstr
);
830 if (!(status
&& has_prefix(status
, "+"))) {
831 netsec_err(errstr
, "Expected contination (+), but got: %s", status
);
839 * Now write the message out, but make sure we end each line with \r\n
842 if ((f
= fopen(imsg
->command
, "r")) == NULL
) {
843 netsec_err(errstr
, "Unable to open %s: %s", imsg
->command
,
848 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
849 if (rc
> 1 && line
[rc
- 1] == '\n' && line
[rc
- 2] == '\r') {
850 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
856 if (line
[rc
- 1] == '\n')
858 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
863 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
) {
874 netsec_err(errstr
, "Error reading %s: %s", imsg
->command
,
883 * Send a final \r\n for the end of the command
886 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
)
890 return netsec_flush(nsc
, errstr
);
896 * Get all outstanding responses. If we were passed in a token string
897 * to look for, return it.
901 get_imap_response(netsec_context
*nsc
, const char *token
, char **tokenresponse
,
902 char **status
, bool condok
, bool failerr
, char **errstr
)
905 struct imap_cmd
*cmd
;
906 bool numerrs
= false;
909 *tokenresponse
= NULL
;
913 if (!(line
= netsec_readline(nsc
, NULL
, errstr
)))
915 if (has_prefix(line
, "* ") && *(line
+ 2) != '\0') {
916 if (token
&& tokenresponse
&& has_prefix(line
+ 2, token
)) {
918 free(*tokenresponse
);
919 *tokenresponse
= getcpy(line
+ 2);
921 } if (condok
&& has_prefix(line
, "+")) {
923 *status
= getcpy(line
);
926 * Special case; return now but don't dequeue the tag,
927 * since we will want to get final result later.
932 if (has_prefix(line
, cmdqueue
->tag
)) {
935 ts_report(&cmd
->start
, "Command (%s) execution time",
937 cmdqueue
= cmd
->next
;
940 for (cmd
= cmdqueue
; cmd
->next
!= NULL
; cmd
= cmd
->next
) {
941 if (has_prefix(line
, cmd
->next
->tag
)) {
942 struct imap_cmd
*cmd2
= cmd
->next
;
943 cmd
->next
= cmd
->next
->next
;
944 if (failerr
&& strncmp(line
+ strlen(cmd2
->tag
),
947 netsec_err(errstr
, "%s", line
+ strlen(cmd2
->tag
));
950 ts_report(&cmd2
->start
, "Command (%s) execution "
951 "time", cmd2
->prefix
);
954 *status
= getcpy(line
);
962 return numerrs
? NOTOK
: OK
;
966 * Add an IMAP command to the msg queue
970 add_msg(bool queue
, struct imap_msg
**ret_imsg
, const char *fmt
, ...)
972 struct imap_msg
*imsg
;
980 msg
= mh_xrealloc(msg
, msgbufsize
);
982 rc
= vsnprintf(msg
, msgbufsize
, fmt
, ap
);
984 } while (rc
>= (int) msgbufsize
);
986 imsg
= mh_xmalloc(sizeof(*imsg
));
990 imsg
->append
= false;
994 if (msgqueue_head
== NULL
) {
995 msgqueue_head
= imsg
;
996 msgqueue_tail
= imsg
;
998 msgqueue_tail
->next
= imsg
;
999 msgqueue_tail
= imsg
;
1007 * Add an APPEND command to the queue
1011 add_append(const char *filename
, const char *folder
, bool queue
)
1013 size_t filesize
= rfc822size(filename
);
1014 struct imap_msg
*imsg
;
1016 add_msg(queue
, &imsg
, "%s", filename
);
1018 imsg
->folder
= getcpy(folder
);
1019 imsg
->append
= true;
1020 imsg
->msgsize
= filesize
;
1024 * Process a batch file, which can contain commands (and some arguments)
1028 batchfile(const char *filename
, char *afolder
, bool queue
)
1032 size_t linesize
= 0;
1034 bool afolder_alloc
= false;
1036 if (!(f
= fopen(filename
, "r"))) {
1037 die("Unable to open batch file %s: %s", filename
, strerror(errno
));
1040 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
1041 line
[rc
- 1] = '\0';
1043 switch (smatch (line
+ 1, switches
)) {
1053 rc
= getline(&line
, &linesize
, f
);
1055 die("Unable to read next line for -afolder");
1057 die("Folder name cannot be blank");
1058 line
[rc
- 1] = '\0';
1059 afolder
= getcpy(line
);
1060 afolder_alloc
= true;
1064 rc
= getline(&line
, &linesize
, f
);
1066 die("Unable to read filename for -append");
1068 die("Filename for -append cannot be blank");
1069 line
[rc
- 1] = '\0';
1070 add_append(line
, afolder
, queue
);
1074 ambigsw (line
, switches
);
1077 die("%s unknown", line
);
1079 die("Switch %s not supported in batch mode", line
);
1081 } else if (*line
== '+') {
1082 if (*(line
+ 1) == '\0')
1083 die("Invalid null folder name");
1084 add_msg(false, NULL
, "SELECT \"%s\"", line
+ 1);
1087 continue; /* Ignore blank line */
1088 add_msg(queue
, NULL
, "%s", line
);
1093 die("Read of \"%s\" failed: %s", filename
, strerror(errno
));
1105 * Negotiate TLS connection, with optional timestamp
1109 imap_negotiate_tls(netsec_context
*nsc
)
1115 gettimeofday(&tv
, NULL
);
1117 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
)
1121 ts_report(&tv
, "TLS negotation time");
1125 * Give a timestamp report.
1129 ts_report(struct timeval
*tv
, const char *fmt
, ...)
1135 gettimeofday(&now
, NULL
);
1137 delta
= ((double) now
.tv_sec
) - ((double) tv
->tv_sec
) +
1138 (now
.tv_usec
/ 1E6
- tv
->tv_usec
/ 1E6
);
1141 vfprintf(stdout
, fmt
, ap
);
1144 printf(": %f sec\n", delta
);
1148 * Calculate the RFC 822 size of file.
1152 rfc822size(const char *filename
)
1155 size_t total
= 0, linecap
= 0;
1159 if (! (f
= fopen(filename
, "r")))
1160 die("Unable to open %s: %s", filename
, strerror(errno
));
1162 while ((rc
= getline(&line
, &linecap
, f
)) > 0) {
1164 if (line
[rc
- 1] == '\n' && (rc
== 1 || line
[rc
- 2] != '\r'))
1166 if (line
[rc
- 1] != '\n')
1173 die("Error while reading %s: %s", filename
, strerror(errno
));