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/smatch.h"
10 #include "sbr/client.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/brkstring.h"
13 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
22 #include "sbr/base64.h"
24 #define IMAPTEST_SWITCHES \
25 X("host hostname", 0, HOSTSW) \
26 X("user username", 0, USERSW) \
27 X("port name/number", 0, PORTSW) \
28 X("snoop", 0, SNOOPSW) \
29 X("nosnoop", 0, NOSNOOPSW) \
30 X("sasl", 0, SASLSW) \
31 X("nosasl", 0, NOSASLSW) \
32 X("saslmech", 0, SASLMECHSW) \
33 X("authservice", 0, AUTHSERVICESW) \
35 X("notls", 0, NOTLSSW) \
36 X("initialtls", 0, INITIALTLSSW) \
37 X("queue", 0, QUEUESW) \
38 X("noqueue", 0, NOQUEUESW) \
39 X("append filename", 0, APPENDSW) \
40 X("afolder foldername", 0, AFOLDERSW) \
41 X("batch filename", 0, BATCHSW) \
42 X("timestamp", 0, TIMESTAMPSW) \
43 X("notimestamp", 0, NOTIMESTAMPSW) \
44 X("timeout", 0, TIMEOUTSW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(IMAPTEST
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(IMAPTEST
, switches
);
59 char *command
; /* Command to send */
60 char *folder
; /* Folder (for append) */
61 bool queue
; /* If true, queue for later delivery */
62 bool append
; /* If true, append "command" to mbox */
63 size_t msgsize
; /* RFC822 size of message */
64 struct imap_msg
*next
; /* Next pointer */
67 struct imap_msg
*msgqueue_head
= NULL
;
68 struct imap_msg
*msgqueue_tail
= NULL
;
73 char tag
[16]; /* Command tag */
74 struct timeval start
; /* Time command was sent */
75 char prefix
[64]; /* Command prefix */
76 struct imap_cmd
*next
; /* Next pointer */
79 static struct imap_cmd
*cmdqueue
= NULL
;
81 static char *saslmechs
= NULL
;
82 svector_t imap_capabilities
= NULL
;
84 static int imap_sasl_callback(enum sasl_message_type
, unsigned const char *,
85 unsigned int, unsigned char **, unsigned int *,
88 static void parse_capability(const char *, unsigned int len
);
89 static int capability_set(const char *);
90 static void clear_capability(void);
91 static int have_capability(void);
92 static int send_imap_command(netsec_context
*, bool noflush
, char **errstr
,
93 const char *fmt
, ...) CHECK_PRINTF(4, 5);
94 static int send_append(netsec_context
*, struct imap_msg
*, char **errstr
);
95 static int get_imap_response(netsec_context
*, const char *token
,
96 char **tokenresp
, char **status
, bool contok
,
97 bool failerr
, char **errstr
);
99 static void ts_report(struct timeval
*tv
, const char *fmt
, ...)
101 static void imap_negotiate_tls(netsec_context
*);
103 static void add_msg(bool queue
, struct imap_msg
**, const char *fmt
, ...)
105 static void add_append(const char *filename
, const char *folder
, bool queue
);
107 static void batchfile(const char *filename
, char *afolder
, bool queue
);
109 static size_t rfc822size(const char *filename
);
111 static bool timestamp
= false;
114 main (int argc
, char **argv
)
116 bool sasl
= false, tls
= false, initialtls
= false;
117 bool snoop
= false, queue
= false;
119 char *saslmech
= NULL
, *host
= NULL
, *port
= "143", *user
= NULL
;
120 char *cp
, **argp
, buf
[BUFSIZ
], *oauth_svc
= NULL
, *errstr
, **arguments
, *p
;
121 char *afolder
= NULL
;
122 netsec_context
*nsc
= NULL
;
123 struct imap_msg
*imsg
;
125 struct timeval tv_start
, tv_connect
, tv_auth
;
127 if (nmh_init(argv
[0], true, true)) { return 1; }
129 arguments
= getarguments (invo_name
, argc
, argv
, 1);
132 while ((cp
= *argp
++)) {
134 switch (smatch (++cp
, switches
)) {
136 ambigsw (cp
, switches
);
139 die("-%s unknown", cp
);
142 snprintf (buf
, sizeof(buf
), "%s [switches] +folder command "
143 "[command ...]", invo_name
);
144 print_help (buf
, switches
, 1);
147 print_version(invo_name
);
151 if (!(host
= *argp
++) || *host
== '-')
152 die("missing argument to %s", argp
[-2]);
155 if (!(port
= *argp
++) || *port
== '-')
156 die("missing argument to %s", argp
[-2]);
159 if (!(user
= *argp
++) || *user
== '-')
160 die("missing argument to %s", argp
[-2]);
164 if (!(afolder
= *argp
++) || *afolder
== '-')
165 die("missing argument to %s", argp
[-2]);
168 if (!*argp
|| (**argp
== '-'))
169 die("missing argument to %s", argp
[-1]);
171 die("Append folder must be set with -afolder first");
172 add_append(*argp
++, afolder
, queue
);
176 if (! *argp
|| (**argp
== '-'))
177 die("missing argument to %s", argp
[-1]);
178 batchfile(*argp
++, afolder
, queue
);
182 if (! *argp
|| (**argp
== '-'))
183 die("missing argument to %s", argp
[-1]);
184 if (! (timeout
= atoi(*argp
++)))
185 die("Invalid timeout: %s", argp
[-1]);
201 if (!(saslmech
= *argp
++) || *saslmech
== '-')
202 die("missing argument to %s", argp
[-2]);
205 if (!(oauth_svc
= *argp
++) || *oauth_svc
== '-')
206 die("missing argument to %s", argp
[-2]);
233 } else if (*cp
== '+') {
234 if (*(cp
+ 1) == '\0')
235 die("Invalid null folder name");
236 add_msg(false, NULL
, "SELECT \"%s\"", cp
+ 1);
238 add_msg(queue
, NULL
, "%s", cp
);
243 die("A hostname must be given with -host");
248 netsec_set_userid(nsc
, user
);
250 netsec_set_hostname(nsc
, host
);
253 netsec_set_timeout(nsc
, timeout
);
256 netsec_set_snoop(nsc
, 1);
259 if (netsec_set_oauth_service(nsc
, oauth_svc
) != OK
) {
260 die("OAuth2 not supported");
265 gettimeofday(&tv_start
, NULL
);
267 if ((fd
= client(host
, port
, buf
, sizeof(buf
), snoop
)) == NOTOK
)
268 die("Connect failed: %s", buf
);
271 ts_report(&tv_start
, "Connect time");
272 gettimeofday(&tv_connect
, NULL
);
275 netsec_set_fd(nsc
, fd
, fd
);
276 netsec_set_snoop(nsc
, snoop
);
278 if (initialtls
|| tls
) {
279 if (netsec_set_tls(nsc
, 1, 0, &errstr
) != OK
)
283 imap_negotiate_tls(nsc
);
287 if (netsec_set_sasl_params(nsc
, "imap", saslmech
, imap_sasl_callback
,
292 if ((cp
= netsec_readline(nsc
, &len
, &errstr
)) == NULL
) {
296 if (has_prefix(cp
, "* BYE")) {
297 fprintf(stderr
, "Connection rejected: %s\n", cp
+ 5);
301 if (!has_prefix(cp
, "* OK") && !has_prefix(cp
, "* PREAUTH")) {
302 fprintf(stderr
, "Invalid server response: %s\n", cp
);
306 if ((p
= strchr(cp
+ 2, ' ')) && *(p
+ 1) != '\0' &&
307 has_prefix(p
+ 1, "[CAPABILITY ")) {
309 * Parse the capability list out to the end
312 p
+= 13; /* 1 + [CAPABILITY + space */
314 if (!(q
= strchr(p
, ']'))) {
315 fprintf(stderr
, "Cannot find end of CAPABILITY announcement\n");
319 parse_capability(p
, q
- p
);
323 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
324 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
328 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false, true,
330 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
335 fprintf(stderr
, "No CAPABILITY response seen\n");
339 p
= capstring
+ 11; /* "CAPABILITY " */
341 parse_capability(p
, strlen(p
));
346 if (!capability_set("STARTTLS")) {
347 fprintf(stderr
, "Requested STARTTLS with -tls, but IMAP server "
348 "has no support for STARTTLS\n");
351 if (send_imap_command(nsc
, false, &errstr
, "STARTTLS") != OK
) {
352 fprintf(stderr
, "Unable to issue STARTTLS: %s\n", errstr
);
356 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, true,
358 fprintf(stderr
, "STARTTLS failed: %s\n", errstr
);
362 imap_negotiate_tls(nsc
);
366 if (netsec_negotiate_sasl(nsc
, saslmechs
, &errstr
) != OK
) {
367 fprintf(stderr
, "SASL negotiation failed: %s\n", errstr
);
371 * Sigh. If we negotiated a SSF AND we got a capability response
372 * as part of the AUTHENTICATE OK message, we can't actually trust
373 * it because it's not protected at that point. So discard the
374 * capability list and we will generate a fresh CAPABILITY command
377 if (netsec_get_sasl_ssf(nsc
) > 0) {
381 if (capability_set("LOGINDISABLED")) {
382 fprintf(stderr
, "User did not request SASL, but LOGIN "
388 if (!have_capability()) {
391 if (send_imap_command(nsc
, false, &errstr
, "CAPABILITY") != OK
) {
392 fprintf(stderr
, "Unable to send CAPABILITY command: %s\n", errstr
);
396 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, NULL
, false,
397 true, &errstr
) != OK
) {
398 fprintf(stderr
, "Cannot get CAPABILITY response: %s\n", errstr
);
403 fprintf(stderr
, "No CAPABILITY response seen\n");
407 p
= capstring
+ 11; /* "CAPABILITY " */
409 parse_capability(p
, strlen(p
));
414 ts_report(&tv_connect
, "Authentication time");
415 gettimeofday(&tv_auth
, NULL
);
418 while (msgqueue_head
!= NULL
) {
419 imsg
= msgqueue_head
;
422 if (send_append(nsc
, imsg
, &errstr
) != OK
) {
423 fprintf(stderr
, "Cannot send APPEND for %s to mbox %s: %s\n",
424 imsg
->command
, imsg
->folder
, errstr
);
428 } else if (send_imap_command(nsc
, imsg
->queue
, &errstr
, "%s",
429 imsg
->command
) != OK
) {
430 fprintf(stderr
, "Cannot send command \"%s\": %s\n",
431 imsg
->command
, errstr
);
437 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false,
439 fprintf(stderr
, "Unable to get response for command "
440 "\"%s\": %s\n", imsg
->command
, errstr
);
445 msgqueue_head
= imsg
->next
;
453 * Flush out any pending network data and get any responses
456 if (netsec_flush(nsc
, &errstr
) != OK
) {
457 fprintf(stderr
, "Error performing final network flush: %s\n", errstr
);
461 if (get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, &errstr
) != OK
) {
462 fprintf(stderr
, "Error fetching final command responses: %s\n", errstr
);
467 ts_report(&tv_auth
, "Total command execution time");
469 send_imap_command(nsc
, false, NULL
, "LOGOUT");
470 get_imap_response(nsc
, NULL
, NULL
, NULL
, false, false, NULL
);
473 netsec_shutdown(nsc
);
476 ts_report(&tv_start
, "Total elapsed time");
482 * Parse a capability string
484 * Put these into an svector so we can check for stuff later. But special
485 * things, like AUTH, we parse now
489 parse_capability(const char *cap
, unsigned int len
)
491 char *str
= mh_xmalloc(len
+ 1);
495 trunccpy(str
, cap
, len
+ 1);
496 caplist
= brkstring(str
, " ", NULL
);
498 if (imap_capabilities
) {
499 svector_free(imap_capabilities
);
502 imap_capabilities
= svector_create(32);
509 for (i
= 0; caplist
[i
] != NULL
; i
++) {
510 if (has_prefix(caplist
[i
], "AUTH=") && *(caplist
[i
] + 5) != '\0') {
512 saslmechs
= add(" ", saslmechs
);
514 saslmechs
= add(caplist
[i
] + 5, saslmechs
);
516 svector_push_back(imap_capabilities
, getcpy(caplist
[i
]));
524 * Return 1 if a particular capability is set
528 capability_set(const char *capability
)
530 if (!imap_capabilities
)
533 return svector_find(imap_capabilities
, capability
) != NULL
;
537 * Clear the CAPABILITY list (used after we call STARTTLS, for example)
541 clear_capability(void)
543 svector_free(imap_capabilities
);
544 imap_capabilities
= NULL
;
548 have_capability(void)
550 return imap_capabilities
!= NULL
;
554 * Our SASL callback, which handles the SASL authentication dialog
558 imap_sasl_callback(enum sasl_message_type mtype
, unsigned const char *indata
,
559 unsigned int indatalen
, unsigned char **outdata
,
560 unsigned int *outdatalen
, void *context
, char **errstr
)
563 char *mech
, *line
, *capstring
, *p
;
565 netsec_context
*nsc
= (netsec_context
*) context
;
568 case NETSEC_SASL_START
:
570 * Generate our AUTHENTICATE message.
572 * If SASL-IR capability is set, we can include any initial response
573 * in the AUTHENTICATE command. Otherwise we have to wait for
574 * the first server response (which should be blank).
577 mech
= netsec_get_sasl_mechanism(nsc
);
581 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
582 writeBase64raw(indata
, indatalen
, (unsigned char *) b64data
);
583 if (capability_set("SASL-IR")) {
584 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
,
587 * Sigh. We're assuming a short tag length here. Really,
588 * I guess we should figure out a way to get the length
591 snoopoffset
= 17 + strlen(mech
);
592 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s %s",
595 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
599 rc
= send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s", mech
);
600 line
= netsec_readline(nsc
, &len
, errstr
);
604 * We should get a "+ ", nothing else.
606 if (len
!= 2 || strcmp(line
, "+ ") != 0) {
607 netsec_err(errstr
, "Did not get expected blank response "
608 "for initial challenge response");
611 rc
= netsec_printf(nsc
, errstr
, "%s\r\n", b64data
);
615 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
616 rc
= netsec_flush(nsc
, errstr
);
617 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
622 if (send_imap_command(nsc
, 0, errstr
, "AUTHENTICATE %s",
630 * Get a response, decode it and process it.
633 case NETSEC_SASL_READ
:
634 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, &snoopoffset
);
636 line
= netsec_readline(nsc
, &len
, errstr
);
637 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
642 if (len
< 2 || (len
== 2 && strcmp(line
, "+ ") != 0)) {
643 netsec_err(errstr
, "Invalid format for SASL response");
651 rc
= decodeBase64(line
+ 2, outdata
, &len
, 0, NULL
);
654 netsec_err(errstr
, "Unable to decode base64 response");
661 * Simple request encoding
664 case NETSEC_SASL_WRITE
:
666 unsigned char *b64data
;
667 b64data
= mh_xmalloc(BASE64SIZE(indatalen
));
668 writeBase64raw(indata
, indatalen
, b64data
);
669 rc
= netsec_printf(nsc
, errstr
, "%s", b64data
);
675 if (netsec_printf(nsc
, errstr
, "\r\n") != OK
)
678 netsec_set_snoop_callback(nsc
, netsec_b64_snoop_decoder
, NULL
);
679 rc
= netsec_flush(nsc
, errstr
);
680 netsec_set_snoop_callback(nsc
, NULL
, NULL
);
689 case NETSEC_SASL_FINISH
:
691 if (get_imap_response(nsc
, "CAPABILITY ", &capstring
, &line
,
692 false, true, errstr
) != OK
)
695 * We MIGHT get a capability response here. If so, be sure we
696 * parse it. We ALSO might get a untagged CAPABILITY response.
697 * Which one should we prefer? I guess we'll go with the untagged
702 p
= capstring
+ 11; /* "CAPABILITY " */
703 parse_capability(p
, strlen(p
));
705 } else if (line
&& has_prefix(line
, "OK [CAPABILITY ")) {
712 parse_capability(p
, q
- p
);
718 * Cancel an authentication dialog
721 case NETSEC_SASL_CANCEL
:
722 rc
= netsec_printf(nsc
, errstr
, "*\r\n");
724 rc
= netsec_flush(nsc
, errstr
);
733 * Send a single command to the IMAP server.
737 send_imap_command(netsec_context
*nsc
, bool noflush
, char **errstr
,
738 const char *fmt
, ...)
740 static unsigned int seq
= 0; /* Tag sequence number */
743 struct imap_cmd
*cmd
;
745 cmd
= mh_xmalloc(sizeof(*cmd
));
747 snprintf(cmd
->tag
, sizeof(cmd
->tag
), "A%u ", seq
++);
752 vsnprintf(cmd
->prefix
, sizeof(cmd
->prefix
), fmt
, ap
);
755 p
= strchr(cmd
->prefix
, ' ');
760 gettimeofday(&cmd
->start
, NULL
);
763 if (netsec_write(nsc
, cmd
->tag
, strlen(cmd
->tag
), errstr
) != OK
) {
769 rc
= netsec_vprintf(nsc
, errstr
, fmt
, ap
);
773 rc
= netsec_write(nsc
, "\r\n", 2, errstr
);
780 if (!noflush
&& netsec_flush(nsc
, errstr
) != OK
) {
785 cmd
->next
= cmdqueue
;
792 * Send an APPEND to the server, which requires some extra semantics
796 send_append(netsec_context
*nsc
, struct imap_msg
*imsg
, char **errstr
)
799 bool nonsynlit
= false;
800 char *status
= NULL
, *line
= NULL
;
805 * If we have the LITERAL+ extension, or we have LITERAL- and our
806 * message is 4096 bytes or less, we can do it all in one message.
807 * Otherwise we have to wait for a contination marker (+).
810 if (capability_set("LITERAL+") ||
811 (capability_set("LITERAL-") && imsg
->msgsize
<= 4096)) {
816 * Send our APPEND command
819 if (send_imap_command(nsc
, nonsynlit
, errstr
, "APPEND \"%s\" {%u%s}",
820 imsg
->folder
, (unsigned int) imsg
->msgsize
,
821 nonsynlit
? "+" : "") != OK
)
825 * If we need to wait for a syncing literal, do that now
829 if (get_imap_response(nsc
, NULL
, NULL
, &status
, true,
830 true, errstr
) != OK
) {
831 imsg
->queue
= true; /* XXX Sigh */
832 fprintf(stderr
, "APPEND command failed: %s\n", *errstr
);
836 if (!(status
&& has_prefix(status
, "+"))) {
837 netsec_err(errstr
, "Expected contination (+), but got: %s", status
);
845 * Now write the message out, but make sure we end each line with \r\n
848 if ((f
= fopen(imsg
->command
, "r")) == NULL
) {
849 netsec_err(errstr
, "Unable to open %s: %s", imsg
->command
,
854 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
855 if (rc
> 1 && line
[rc
- 1] == '\n' && line
[rc
- 2] == '\r') {
856 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
862 if (line
[rc
- 1] == '\n')
864 if (netsec_write(nsc
, line
, rc
, errstr
) != OK
) {
869 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
) {
880 netsec_err(errstr
, "Error reading %s: %s", imsg
->command
,
889 * Send a final \r\n for the end of the command
892 if (netsec_write(nsc
, "\r\n", 2, errstr
) != OK
)
896 return netsec_flush(nsc
, errstr
);
902 * Get all outstanding responses. If we were passed in a token string
903 * to look for, return it.
907 get_imap_response(netsec_context
*nsc
, const char *token
, char **tokenresponse
,
908 char **status
, bool condok
, bool failerr
, char **errstr
)
911 struct imap_cmd
*cmd
;
912 bool numerrs
= false;
915 *tokenresponse
= NULL
;
919 if (!(line
= netsec_readline(nsc
, NULL
, errstr
)))
921 if (has_prefix(line
, "* ") && *(line
+ 2) != '\0') {
922 if (token
&& tokenresponse
&& has_prefix(line
+ 2, token
)) {
924 free(*tokenresponse
);
925 *tokenresponse
= getcpy(line
+ 2);
927 } if (condok
&& has_prefix(line
, "+")) {
929 *status
= getcpy(line
);
932 * Special case; return now but don't dequeue the tag,
933 * since we will want to get final result later.
938 if (has_prefix(line
, cmdqueue
->tag
)) {
941 ts_report(&cmd
->start
, "Command (%s) execution time",
943 cmdqueue
= cmd
->next
;
946 for (cmd
= cmdqueue
; cmd
->next
!= NULL
; cmd
= cmd
->next
) {
947 if (has_prefix(line
, cmd
->next
->tag
)) {
948 struct imap_cmd
*cmd2
= cmd
->next
;
949 cmd
->next
= cmd
->next
->next
;
950 if (failerr
&& strncmp(line
+ strlen(cmd2
->tag
),
953 netsec_err(errstr
, "%s", line
+ strlen(cmd2
->tag
));
956 ts_report(&cmd2
->start
, "Command (%s) execution "
957 "time", cmd2
->prefix
);
960 *status
= getcpy(line
);
968 return numerrs
? NOTOK
: OK
;
972 * Add an IMAP command to the msg queue
976 add_msg(bool queue
, struct imap_msg
**ret_imsg
, const char *fmt
, ...)
978 struct imap_msg
*imsg
;
986 msg
= mh_xrealloc(msg
, msgbufsize
);
988 rc
= vsnprintf(msg
, msgbufsize
, fmt
, ap
);
990 } while (rc
>= (int) msgbufsize
);
992 imsg
= mh_xmalloc(sizeof(*imsg
));
996 imsg
->append
= false;
1000 if (msgqueue_head
== NULL
) {
1001 msgqueue_head
= imsg
;
1002 msgqueue_tail
= imsg
;
1004 msgqueue_tail
->next
= imsg
;
1005 msgqueue_tail
= imsg
;
1013 * Add an APPEND command to the queue
1017 add_append(const char *filename
, const char *folder
, bool queue
)
1019 size_t filesize
= rfc822size(filename
);
1020 struct imap_msg
*imsg
;
1022 add_msg(queue
, &imsg
, "%s", filename
);
1024 imsg
->folder
= getcpy(folder
);
1025 imsg
->append
= true;
1026 imsg
->msgsize
= filesize
;
1030 * Process a batch file, which can contain commands (and some arguments)
1034 batchfile(const char *filename
, char *afolder
, bool queue
)
1038 size_t linesize
= 0;
1040 bool afolder_alloc
= false;
1042 if (!(f
= fopen(filename
, "r"))) {
1043 die("Unable to open batch file %s: %s", filename
, strerror(errno
));
1046 while ((rc
= getline(&line
, &linesize
, f
)) > 0) {
1047 line
[rc
- 1] = '\0';
1049 switch (smatch (line
+ 1, switches
)) {
1059 rc
= getline(&line
, &linesize
, f
);
1061 die("Unable to read next line for -afolder");
1063 die("Folder name cannot be blank");
1064 line
[rc
- 1] = '\0';
1065 afolder
= getcpy(line
);
1066 afolder_alloc
= true;
1070 rc
= getline(&line
, &linesize
, f
);
1072 die("Unable to read filename for -append");
1074 die("Filename for -append cannot be blank");
1075 line
[rc
- 1] = '\0';
1076 add_append(line
, afolder
, queue
);
1080 ambigsw (line
, switches
);
1083 die("%s unknown", line
);
1085 die("Switch %s not supported in batch mode", line
);
1087 } else if (*line
== '+') {
1088 if (*(line
+ 1) == '\0')
1089 die("Invalid null folder name");
1090 add_msg(false, NULL
, "SELECT \"%s\"", line
+ 1);
1093 continue; /* Ignore blank line */
1094 add_msg(queue
, NULL
, "%s", line
);
1099 die("Read of \"%s\" failed: %s", filename
, strerror(errno
));
1111 * Negotiate TLS connection, with optional timestamp
1115 imap_negotiate_tls(netsec_context
*nsc
)
1121 gettimeofday(&tv
, NULL
);
1123 if (netsec_negotiate_tls(nsc
, &errstr
) != OK
)
1127 ts_report(&tv
, "TLS negotation time");
1131 * Give a timestamp report.
1135 ts_report(struct timeval
*tv
, const char *fmt
, ...)
1141 gettimeofday(&now
, NULL
);
1143 delta
= ((double) now
.tv_sec
) - ((double) tv
->tv_sec
) +
1144 (now
.tv_usec
/ 1E6
- tv
->tv_usec
/ 1E6
);
1147 vfprintf(stdout
, fmt
, ap
);
1150 printf(": %f sec\n", delta
);
1154 * Calculate the RFC 822 size of file.
1158 rfc822size(const char *filename
)
1161 size_t total
= 0, linecap
= 0;
1165 if (! (f
= fopen(filename
, "r")))
1166 die("Unable to open %s: %s", filename
, strerror(errno
));
1168 while ((rc
= getline(&line
, &linecap
, f
)) > 0) {
1170 if (line
[rc
- 1] == '\n' && (rc
== 1 || line
[rc
- 2] != '\r'))
1172 if (line
[rc
- 1] != '\n')
1179 die("Error while reading %s: %s", filename
, strerror(errno
));