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/ambigsw.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
18 #include "sbr/base64.h"
20 #define IMAPTEST_SWITCHES \
21 X("host hostname", 0, HOSTSW) \
22 X("user username", 0, USERSW) \
23 X("port name/number", 0, PORTSW) \
24 X("snoop", 0, SNOOPSW) \
25 X("nosnoop", 0, NOSNOOPSW) \
26 X("sasl", 0, SASLSW) \
27 X("nosasl", 0, NOSASLSW) \
28 X("saslmech", 0, SASLMECHSW) \
29 X("authservice", 0, AUTHSERVICESW) \
31 X("notls", 0, NOTLSSW) \
32 X("initialtls", 0, INITIALTLSSW) \
33 X("queue", 0, QUEUESW) \
34 X("noqueue", 0, NOQUEUESW) \
35 X("append filename", 0, APPENDSW) \
36 X("afolder foldername", 0, AFOLDERSW) \
37 X("batch filename", 0, BATCHSW) \
38 X("timestamp", 0, TIMESTAMPSW) \
39 X("notimestamp", 0, NOTIMESTAMPSW) \
40 X("timeout", 0, TIMEOUTSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(IMAPTEST
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(IMAPTEST
, switches
);
55 char *command
; /* Command to send */
56 char *folder
; /* Folder (for append) */
57 bool queue
; /* If true, queue for later delivery */
58 bool append
; /* If true, append "command" to mbox */
59 size_t msgsize
; /* RFC822 size of message */
60 struct imap_msg
*next
; /* Next pointer */
63 struct imap_msg
*msgqueue_head
= NULL
;
64 struct imap_msg
*msgqueue_tail
= NULL
;
69 char tag
[16]; /* Command tag */
70 struct timeval start
; /* Time command was sent */
71 char prefix
[64]; /* Command prefix */
72 struct imap_cmd
*next
; /* Next pointer */
75 static struct imap_cmd
*cmdqueue
= NULL
;
77 static char *saslmechs
= NULL
;
78 svector_t imap_capabilities
= NULL
;
80 static int imap_sasl_callback(enum sasl_message_type
, unsigned const char *,
81 unsigned int, unsigned char **, unsigned int *,
84 static void parse_capability(const char *, unsigned int len
);
85 static int capability_set(const char *);
86 static void clear_capability(void);
87 static int have_capability(void);
88 static int send_imap_command(netsec_context
*, bool noflush
, char **errstr
,
89 const char *fmt
, ...) CHECK_PRINTF(4, 5);
90 static int send_append(netsec_context
*, struct imap_msg
*, char **errstr
);
91 static int get_imap_response(netsec_context
*, const char *token
,
92 char **tokenresp
, char **status
, bool contok
,
93 bool failerr
, char **errstr
);
95 static void ts_report(struct timeval
*tv
, const char *fmt
, ...)
97 static void imap_negotiate_tls(netsec_context
*);
99 static void add_msg(bool queue
, struct imap_msg
**, const char *fmt
, ...)
101 static void add_append(const char *filename
, const char *folder
, bool queue
);
103 static void batchfile(const char *filename
, char *afolder
, bool queue
);
105 static size_t rfc822size(const char *filename
);
107 static bool timestamp
= false;
110 main (int argc
, char **argv
)
112 bool sasl
= false, tls
= false, initialtls
= false;
113 bool snoop
= false, queue
= false;
115 char *saslmech
= NULL
, *host
= NULL
, *port
= "143", *user
= NULL
;
116 char *cp
, **argp
, buf
[BUFSIZ
], *oauth_svc
= NULL
, *errstr
, **arguments
, *p
;
117 char *afolder
= NULL
;
118 netsec_context
*nsc
= NULL
;
119 struct imap_msg
*imsg
;
121 struct timeval tv_start
, tv_connect
, tv_auth
;
123 if (nmh_init(argv
[0], true, true)) { return 1; }
125 arguments
= getarguments (invo_name
, argc
, argv
, 1);
128 while ((cp
= *argp
++)) {
130 switch (smatch (++cp
, switches
)) {
132 ambigsw (cp
, switches
);
135 die("-%s unknown", cp
);
138 snprintf (buf
, sizeof(buf
), "%s [switches] +folder command "
139 "[command ...]", invo_name
);
140 print_help (buf
, switches
, 1);
143 print_version(invo_name
);
147 if (!(host
= *argp
++) || *host
== '-')
148 die("missing argument to %s", argp
[-2]);
151 if (!(port
= *argp
++) || *port
== '-')
152 die("missing argument to %s", argp
[-2]);
155 if (!(user
= *argp
++) || *user
== '-')
156 die("missing argument to %s", argp
[-2]);
160 if (!(afolder
= *argp
++) || *afolder
== '-')
161 die("missing argument to %s", argp
[-2]);
164 if (!*argp
|| (**argp
== '-'))
165 die("missing argument to %s", argp
[-1]);
167 die("Append folder must be set with -afolder first");
168 add_append(*argp
++, afolder
, queue
);
172 if (! *argp
|| (**argp
== '-'))
173 die("missing argument to %s", argp
[-1]);
174 batchfile(*argp
++, afolder
, queue
);
178 if (! *argp
|| (**argp
== '-'))
179 die("missing argument to %s", argp
[-1]);
180 if (! (timeout
= atoi(*argp
++)))
181 die("Invalid timeout: %s", argp
[-1]);
197 if (!(saslmech
= *argp
++) || *saslmech
== '-')
198 die("missing argument to %s", argp
[-2]);
201 if (!(oauth_svc
= *argp
++) || *oauth_svc
== '-')
202 die("missing argument to %s", argp
[-2]);
229 } else if (*cp
== '+') {
230 if (*(cp
+ 1) == '\0')
231 die("Invalid null folder name");
232 add_msg(false, NULL
, "SELECT \"%s\"", cp
+ 1);
234 add_msg(queue
, NULL
, "%s", cp
);
239 die("A hostname must be given with -host");
244 netsec_set_userid(nsc
, user
);
246 netsec_set_hostname(nsc
, host
);
249 netsec_set_timeout(nsc
, timeout
);
252 netsec_set_snoop(nsc
, 1);
255 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
256 die("OAuth2 not supported");
261 gettimeofday(&tv_start
, NULL
);
263 if ((fd
= client(host
, port
, buf
, sizeof(buf
), snoop
)) == NOTOK
)
264 die("Connect failed: %s", buf
);
267 ts_report(&tv_start
, "Connect time");
268 gettimeofday(&tv_connect
, NULL
);
271 netsec_set_fd(nsc
, fd
, fd
);
272 netsec_set_snoop(nsc
, snoop
);
274 if (initialtls
|| tls
) {
275 if (netsec_set_tls(nsc
, 1, 0, &errstr
) != OK
)
279 imap_negotiate_tls(nsc
);
283 if (netsec_set_sasl_params(nsc
, "imap", saslmech
, imap_sasl_callback
,
288 if ((cp
= netsec_readline(nsc
, &len
, &errstr
)) == NULL
) {
292 if (has_prefix(cp
, "* BYE")) {
293 fprintf(stderr
, "Connection rejected: %s\n", cp
+ 5);
297 if (!has_prefix(cp
, "* OK") && !has_prefix(cp
, "* PREAUTH")) {
298 fprintf(stderr
, "Invalid server response: %s\n", cp
);
302 if ((p
= strchr(cp
+ 2, ' ')) && *(p
+ 1) != '\0' &&
303 has_prefix(p
+ 1, "[CAPABILITY ")) {
305 * Parse the capability list out to the end
308 p
+= 13; /* 1 + [CAPABILITY + space */
310 if (!(q
= strchr(p
, ']'))) {
311 fprintf(stderr
, "Cannot find end of CAPABILITY announcement\n");
315 parse_capability(p
, q
- p
);
319 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
320 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
324 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false, true,
326 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
331 fprintf(stderr
, "No CAPABILITY response seen\n");
335 p
= capstring
+ 11; /* "CAPABILITY " */
337 parse_capability(p
, strlen(p
));
342 if (!capability_set("STARTTLS")) {
343 fprintf(stderr
, "Requested STARTTLS with -tls, but IMAP server "
344 "has no support for STARTTLS\n");
347 if (send_imap_command(nsc
, false, &errstr
, "STARTTLS") != OK
) {
348 fprintf(stderr
, "Unable to issue STARTTLS: %s\n", errstr
);
352 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, true,
354 fprintf(stderr
, "STARTTLS failed: %s\n", errstr
);
358 imap_negotiate_tls(nsc
);
362 if (netsec_negotiate_sasl(nsc
, saslmechs
, &errstr
) != OK
) {
363 fprintf(stderr
, "SASL negotiation failed: %s\n", errstr
);
367 * Sigh. If we negotiated a SSF AND we got a capability response
368 * as part of the AUTHENTICATE OK message, we can't actually trust
369 * it because it's not protected at that point. So discard the
370 * capability list and we will generate a fresh CAPABILITY command
373 if (netsec_get_sasl_ssf(nsc
) > 0) {
377 if (capability_set("LOGINDISABLED")) {
378 fprintf(stderr
, "User did not request SASL, but LOGIN "
384 if (!have_capability()) {
387 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
388 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
392 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false,
393 true, &errstr
) != OK
) {
394 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
399 fprintf(stderr
, "No CAPABILITY response seen\n");
403 p
= capstring
+ 11; /* "CAPABILITY " */
405 parse_capability(p
, strlen(p
));
410 ts_report(&tv_connect
, "Authentication time");
411 gettimeofday(&tv_auth
, NULL
);
414 while (msgqueue_head
!= NULL
) {
415 imsg
= msgqueue_head
;
418 if (send_append(nsc
, imsg
, &errstr
) != OK
) {
419 fprintf(stderr
, "Cannot send APPEND for %s to mbox %s: %s\n",
420 imsg
->command
, imsg
->folder
, errstr
);
424 } else if (send_imap_command(nsc
, imsg
->queue
, &errstr
, "%s",
425 imsg
->command
) != OK
) {
426 fprintf(stderr
, "Cannot send command \"%s\": %s\n",
427 imsg
->command
, errstr
);
433 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false,
435 fprintf(stderr
, "Unable to get response for command "
436 "\"%s\": %s\n", imsg
->command
, errstr
);
441 msgqueue_head
= imsg
->next
;
449 * Flush out any pending network data and get any responses
452 if (netsec_flush(nsc
, &errstr
) != OK
) {
453 fprintf(stderr
, "Error performing final network flush: %s\n", errstr
);
457 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, &errstr
) != OK
) {
458 fprintf(stderr
, "Error fetching final command responses: %s\n", errstr
);
463 ts_report(&tv_auth
, "Total command execution time");
465 send_imap_command(nsc
, false, NULL
, "LOGOUT");
466 get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, NULL
);
469 netsec_shutdown(nsc
);
472 ts_report(&tv_start
, "Total elapsed time");
478 * Parse a capability string
480 * Put these into an svector so we can check for stuff later. But special
481 * things, like AUTH, we parse now
485 parse_capability(const char *cap
, unsigned int len
)
487 char *str
= mh_xmalloc(len
+ 1);
491 trunccpy(str
, cap
, len
+ 1);
492 caplist
= brkstring(str
, " ", NULL
);
494 if (imap_capabilities
) {
495 svector_free(imap_capabilities
);
498 imap_capabilities
= svector_create(32);
505 for (i
= 0; caplist
[i
] != NULL
; i
++) {
506 if (has_prefix(caplist
[i
], "AUTH=") && *(caplist
[i
] + 5) != '\0') {
508 saslmechs
= add(" ", saslmechs
);
510 saslmechs
= add(caplist
[i
] + 5, saslmechs
);
512 svector_push_back(imap_capabilities
, getcpy(caplist
[i
]));
520 * Return 1 if a particular capability is set
524 capability_set(const char *capability
)
526 if (!imap_capabilities
)
529 return svector_find(imap_capabilities
, capability
) != NULL
;
533 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
537 clear_capability(void)
539 svector_free(imap_capabilities
);
540 imap_capabilities
= NULL
;
544 have_capability(void)
546 return imap_capabilities
!= NULL
;
550 * Our SASL callback, which handles the SASL authentication dialog
554 imap_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
555 unsigned int indatalen
, unsigned char **outdata
,
556 unsigned int *outdatalen
, void *context
, char **errstr
)
559 char *mech
, *line
, *capstring
, *p
;
561 netsec_context
*nsc
= (netsec_context
*) context
;
564 case NETSEC_SASL_START
:
566 * Generate our AUTHENTICATE message.
568 * If SASL-IR capability is set, we can include any initial response
569 * in the AUTHENTICATE command. Otherwise we have to wait for
570 * the first server response (which should be blank).
573 mech
= netsec_get_sasl_mechanism(nsc
);
577 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
578 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
579 if (capability_set("SASL-IR")) {
580 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
583 * Sigh. We're assuming a short tag length here. Really,
584 * I guess we should figure out a way to get the length
587 snoopoffset
= 17 + strlen(mech
);
588 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s %s",
591 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
595 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s", mech
);
596 line
= netsec_readline(nsc
, &len
, errstr
);
600 * We should get a "+ ", nothing else.
602 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
603 netsec_err(errstr
, "Did not get expected blank response "
604 "for initial challenge response");
607 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
611 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
612 rc
= netsec_flush(nsc
, errstr
);
613 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
618 if (send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s",
626 * Get a response, decode it and process it.
629 case NETSEC_SASL_READ
:
630 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
632 line
= netsec_readline(nsc
, &len
, errstr
);
633 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
638 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
639 netsec_err(errstr
, "Invalid format for SASL response");
647 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
650 netsec_err(errstr
, "Unable to decode base64 response");
657 * Simple request encoding
660 case NETSEC_SASL_WRITE
:
662 unsigned char *b64data
;
663 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
664 writeBase64raw(indata
, indatalen
, b64data
);
665 rc
= netsec_printf(nsc
, errstr
, "%s", b64data
);
671 if (netsec_printf(nsc
, errstr
, "\r\n") != OK
)
674 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
675 rc
= netsec_flush(nsc
, errstr
);
676 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
685 case NETSEC_SASL_FINISH
:
687 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, &line
,
688 false, true, errstr
) != OK
)
691 * We MIGHT get a capability response here. If so, be sure we
692 * parse it. We ALSO might get a untagged CAPABILITY response.
693 * Which one should we prefer? I guess we'll go with the untagged
698 p
= capstring
+ 11; /* "CAPABILITY " */
699 parse_capability(p
, strlen(p
));
701 } else if (line
&& has_prefix(line
, "OK [CAPABILITY ")) {
708 parse_capability(p
, q
- p
);
714 * Cancel an authentication dialog
717 case NETSEC_SASL_CANCEL
:
718 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
720 rc
= netsec_flush(nsc
, errstr
);
729 * Send a single command to the IMAP server.
733 send_imap_command(netsec_context
*nsc
, bool noflush
, char **errstr
,
734 const char *fmt
, ...)
736 static unsigned int seq
= 0; /* Tag sequence number */
739 struct imap_cmd
*cmd
;
741 cmd
= mh_xmalloc(sizeof(*cmd
));
743 snprintf(cmd
->tag
, sizeof(cmd
->tag
), "A%u ", seq
++);
748 vsnprintf(cmd
->prefix
, sizeof(cmd
->prefix
), fmt
, ap
);
751 p
= strchr(cmd
->prefix
, ' ');
756 gettimeofday(&cmd
->start
, NULL
);
759 if (netsec_write(nsc
, cmd
->tag
, strlen(cmd
->tag
), errstr
) != OK
) {
765 rc
= netsec_vprintf(nsc
, errstr
, fmt
, ap
);
769 rc
= netsec_write(nsc
, "\r\n", 2, errstr
);
776 if (!noflush
&& netsec_flush(nsc
, errstr
) != OK
) {
781 cmd
->next
= cmdqueue
;
788 * Send an APPEND to the server, which requires some extra semantics
792 send_append(netsec_context
*nsc
, struct imap_msg
*imsg
, char **errstr
)
795 bool nonsynlit
= false;
796 char *status
= NULL
, *line
= NULL
;
801 * If we have the LITERAL+ extension, or we have LITERAL- and our
802 * message is 4096 bytes or less, we can do it all in one message.
803 * Otherwise we have to wait for a contination marker (+).
806 if (capability_set("LITERAL+") ||
807 (capability_set("LITERAL-") && imsg
->msgsize
<= 4096)) {
812 * Send our APPEND command
815 if (send_imap_command(nsc
, nonsynlit
, errstr
, "APPEND \"%s\" {%u%s}",
816 imsg
->folder
, (unsigned int) imsg
->msgsize
,
817 nonsynlit
? "+" : "") != OK
)
821 * If we need to wait for a syncing literal, do that now
825 if (get_imap_response(nsc
, NULL
, NULL
, &status
, true,
826 true, errstr
) != OK
) {
827 imsg
->queue
= true; /* XXX Sigh */
828 fprintf(stderr
, "APPEND command failed: %s\n", *errstr
);
832 if (!(status
&& has_prefix(status
, "+"))) {
833 netsec_err(errstr
, "Expected contination (+), but got: %s", status
);
841 * Now write the message out, but make sure we end each line with \r\n
844 if ((f
= fopen(imsg
->command
, "r")) == NULL
) {
845 netsec_err(errstr
, "Unable to open %s: %s", imsg
->command
,
850 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
851 if (rc
> 1 && line
[rc
- 1] == '\n' && line
[rc
- 2] == '\r') {
852 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
858 if (line
[rc
- 1] == '\n')
860 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
865 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
) {
876 netsec_err(errstr
, "Error reading %s: %s", imsg
->command
,
885 * Send a final \r\n for the end of the command
888 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
)
892 return netsec_flush(nsc
, errstr
);
898 * Get all outstanding responses. If we were passed in a token string
899 * to look for, return it.
903 get_imap_response(netsec_context
*nsc
, const char *token
, char **tokenresponse
,
904 char **status
, bool condok
, bool failerr
, char **errstr
)
907 struct imap_cmd
*cmd
;
908 bool numerrs
= false;
911 *tokenresponse
= NULL
;
915 if (!(line
= netsec_readline(nsc
, NULL
, errstr
)))
917 if (has_prefix(line
, "* ") && *(line
+ 2) != '\0') {
918 if (token
&& tokenresponse
&& has_prefix(line
+ 2, token
)) {
920 free(*tokenresponse
);
921 *tokenresponse
= getcpy(line
+ 2);
923 } if (condok
&& has_prefix(line
, "+")) {
925 *status
= getcpy(line
);
928 * Special case; return now but don't dequeue the tag,
929 * since we will want to get final result later.
934 if (has_prefix(line
, cmdqueue
->tag
)) {
937 ts_report(&cmd
->start
, "Command (%s) execution time",
939 cmdqueue
= cmd
->next
;
942 for (cmd
= cmdqueue
; cmd
->next
!= NULL
; cmd
= cmd
->next
) {
943 if (has_prefix(line
, cmd
->next
->tag
)) {
944 struct imap_cmd
*cmd2
= cmd
->next
;
945 cmd
->next
= cmd
->next
->next
;
946 if (failerr
&& strncmp(line
+ strlen(cmd2
->tag
),
949 netsec_err(errstr
, "%s", line
+ strlen(cmd2
->tag
));
952 ts_report(&cmd2
->start
, "Command (%s) execution "
953 "time", cmd2
->prefix
);
956 *status
= getcpy(line
);
964 return numerrs
? NOTOK
: OK
;
968 * Add an IMAP command to the msg queue
972 add_msg(bool queue
, struct imap_msg
**ret_imsg
, const char *fmt
, ...)
974 struct imap_msg
*imsg
;
982 msg
= mh_xrealloc(msg
, msgbufsize
);
984 rc
= vsnprintf(msg
, msgbufsize
, fmt
, ap
);
986 } while (rc
>= (int) msgbufsize
);
988 imsg
= mh_xmalloc(sizeof(*imsg
));
992 imsg
->append
= false;
996 if (msgqueue_head
== NULL
) {
997 msgqueue_head
= imsg
;
998 msgqueue_tail
= imsg
;
1000 msgqueue_tail
->next
= imsg
;
1001 msgqueue_tail
= imsg
;
1009 * Add an APPEND command to the queue
1013 add_append(const char *filename
, const char *folder
, bool queue
)
1015 size_t filesize
= rfc822size(filename
);
1016 struct imap_msg
*imsg
;
1018 add_msg(queue
, &imsg
, "%s", filename
);
1020 imsg
->folder
= getcpy(folder
);
1021 imsg
->append
= true;
1022 imsg
->msgsize
= filesize
;
1026 * Process a batch file, which can contain commands (and some arguments)
1030 batchfile(const char *filename
, char *afolder
, bool queue
)
1034 size_t linesize
= 0;
1036 bool afolder_alloc
= false;
1038 if (!(f
= fopen(filename
, "r"))) {
1039 die("Unable to open batch file %s: %s", filename
, strerror(errno
));
1042 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
1043 line
[rc
- 1] = '\0';
1045 switch (smatch (line
+ 1, switches
)) {
1055 rc
= getline(&line
, &linesize
, f
);
1057 die("Unable to read next line for -afolder");
1059 die("Folder name cannot be blank");
1060 line
[rc
- 1] = '\0';
1061 afolder
= getcpy(line
);
1062 afolder_alloc
= true;
1066 rc
= getline(&line
, &linesize
, f
);
1068 die("Unable to read filename for -append");
1070 die("Filename for -append cannot be blank");
1071 line
[rc
- 1] = '\0';
1072 add_append(line
, afolder
, queue
);
1076 ambigsw (line
, switches
);
1079 die("%s unknown", line
);
1081 die("Switch %s not supported in batch mode", line
);
1083 } else if (*line
== '+') {
1084 if (*(line
+ 1) == '\0')
1085 die("Invalid null folder name");
1086 add_msg(false, NULL
, "SELECT \"%s\"", line
+ 1);
1089 continue; /* Ignore blank line */
1090 add_msg(queue
, NULL
, "%s", line
);
1095 die("Read of \"%s\" failed: %s", filename
, strerror(errno
));
1107 * Negotiate TLS connection, with optional timestamp
1111 imap_negotiate_tls(netsec_context
*nsc
)
1117 gettimeofday(&tv
, NULL
);
1119 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
)
1123 ts_report(&tv
, "TLS negotation time");
1127 * Give a timestamp report.
1131 ts_report(struct timeval
*tv
, const char *fmt
, ...)
1137 gettimeofday(&now
, NULL
);
1139 delta
= ((double) now
.tv_sec
) - ((double) tv
->tv_sec
) +
1140 (now
.tv_usec
/ 1E6
- tv
->tv_usec
/ 1E6
);
1143 vfprintf(stdout
, fmt
, ap
);
1146 printf(": %f sec\n", delta
);
1150 * Calculate the RFC 822 size of file.
1154 rfc822size(const char *filename
)
1157 size_t total
= 0, linecap
= 0;
1161 if (! (f
= fopen(filename
, "r")))
1162 die("Unable to open %s: %s", filename
, strerror(errno
));
1164 while ((rc
= getline(&line
, &linecap
, f
)) > 0) {
1166 if (line
[rc
- 1] == '\n' && (rc
== 1 || line
[rc
- 2] != '\r'))
1168 if (line
[rc
- 1] != '\n')
1175 die("Error while reading %s: %s", filename
, strerror(errno
));