X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/805bd2f9d717c019630293bcd82be25965d9f355..dc4d0c4bf247cfc88e1f3f9463fa2264d3d226b5:/uip/imaptest.c diff --git a/uip/imaptest.c b/uip/imaptest.c index 0e721ce2..01d3ee52 100644 --- a/uip/imaptest.c +++ b/uip/imaptest.c @@ -44,7 +44,7 @@ struct imap_msg; struct imap_msg { char *command; /* Command to send */ - int queue; /* If true, queue for later delivery */ + bool queue; /* If true, queue for later delivery */ struct imap_msg *next; /* Next pointer */ }; @@ -56,6 +56,7 @@ struct imap_cmd; struct imap_cmd { char tag[16]; /* Command tag */ struct timeval start; /* Time command was sent */ + char prefix[64]; /* Command prefix */ struct imap_cmd *next; /* Next pointer */ }; @@ -72,15 +73,16 @@ static void parse_capability(const char *, unsigned int len); static int capability_set(const char *); static void clear_capability(void); static int have_capability(void); -static int send_imap_command(netsec_context *, int noflush, char **errstr, +static int send_imap_command(netsec_context *, bool noflush, char **errstr, const char *fmt, ...) CHECK_PRINTF(4, 5); static int get_imap_response(netsec_context *, const char *token, char **tokenresp, char **status, int failerr, char **errstr); -static void ts_report(const char *str, struct timeval *tv); +static void ts_report(struct timeval *tv, const char *fmt, ...) + CHECK_PRINTF(2, 3); -static void add_msg(int queue, const char *fmt, ...) CHECK_PRINTF(2, 3); +static void add_msg(bool queue, const char *fmt, ...) CHECK_PRINTF(2, 3); static bool timestamp = false; @@ -97,7 +99,7 @@ main (int argc, char **argv) size_t len; struct timeval tv_start, tv_connect, tv_auth; - if (nmh_init(argv[0], 1)) { return 1; } + if (nmh_init(argv[0], true, true)) { return 1; } arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; @@ -109,7 +111,7 @@ main (int argc, char **argv) ambigsw (cp, switches); done (1); case UNKWNSW: - adios (NULL, "-%s unknown", cp); + die("-%s unknown", cp); case HELPSW: snprintf (buf, sizeof(buf), "%s [switches] +folder command " @@ -122,15 +124,15 @@ main (int argc, char **argv) case HOSTSW: if (!(host = *argp++) || *host == '-') - adios(NULL, "missing argument to %s", argp[-2]); + die("missing argument to %s", argp[-2]); continue; case PORTSW: if (!(port = *argp++) || *port == '-') - adios(NULL, "missing argument to %s", argp[-2]); + die("missing argument to %s", argp[-2]); continue; case USERSW: if (!(user = *argp++) || *user == '-') - adios(NULL, "missing argument to %s", argp[-2]); + die("missing argument to %s", argp[-2]); continue; case SNOOPSW: @@ -147,11 +149,11 @@ main (int argc, char **argv) continue; case SASLMECHSW: if (!(saslmech = *argp++) || *saslmech == '-') - adios(NULL, "missing argument to %s", argp[-2]); + die("missing argument to %s", argp[-2]); continue; case AUTHSERVICESW: if (!(oauth_svc = *argp++) || *oauth_svc == '-') - adios(NULL, "missing argument to %s", argp[-2]); + die("missing argument to %s", argp[-2]); continue; case TLSSW: tls = true; @@ -174,7 +176,7 @@ main (int argc, char **argv) } } else if (*cp == '+') { if (*(cp + 1) == '\0') - adios(NULL, "Invalid null folder name"); + die("Invalid null folder name"); add_msg(0, "SELECT \"%s\"", cp + 1); } else { add_msg(0, "%s", cp); @@ -182,7 +184,7 @@ main (int argc, char **argv) } if (! host) - adios(NULL, "A hostname must be given with -host"); + die("A hostname must be given with -host"); nsc = netsec_init(); @@ -196,7 +198,7 @@ main (int argc, char **argv) if (oauth_svc) { if (netsec_set_oauth_service(nsc, oauth_svc) != OK) { - adios(NULL, "OAuth2 not supported"); + die("OAuth2 not supported"); } } @@ -204,10 +206,10 @@ main (int argc, char **argv) gettimeofday(&tv_start, NULL); if ((fd = client(host, port, buf, sizeof(buf), snoop)) == NOTOK) - adios(NULL, "Connect failed: %s", buf); + die("Connect failed: %s", buf); if (timestamp) { - ts_report("Connect time", &tv_start); + ts_report(&tv_start, "Connect time"); gettimeofday(&tv_connect, NULL); } @@ -216,20 +218,20 @@ main (int argc, char **argv) if (initialtls || tls) { if (netsec_set_tls(nsc, 1, 0, &errstr) != OK) - adios(NULL, errstr); + die("%s", errstr); if (initialtls && netsec_negotiate_tls(nsc, &errstr) != OK) - adios(NULL, errstr); + die("%s", errstr); } if (sasl) { if (netsec_set_sasl_params(nsc, "imap", saslmech, imap_sasl_callback, nsc, &errstr) != OK) - adios(NULL, errstr); + die("%s", errstr); } if ((cp = netsec_readline(nsc, &len, &errstr)) == NULL) { - adios(NULL, errstr); + die("%s", errstr); } if (has_prefix(cp, "* BYE")) { @@ -259,7 +261,7 @@ main (int argc, char **argv) } else { char *capstring; - if (send_imap_command(nsc, 0, &errstr, "CAPABILITY") != OK) { + if (send_imap_command(nsc, false, &errstr, "CAPABILITY") != OK) { fprintf(stderr, "Unable to send CAPABILITY command: %s\n", errstr); goto finish; } @@ -285,7 +287,7 @@ main (int argc, char **argv) "has no support for STARTTLS\n"); goto finish; } - if (send_imap_command(nsc, 0, &errstr, "STARTTLS") != OK) { + if (send_imap_command(nsc, false, &errstr, "STARTTLS") != OK) { fprintf(stderr, "Unable to issue STARTTLS: %s\n", errstr); goto finish; } @@ -295,7 +297,7 @@ main (int argc, char **argv) goto finish; } if (netsec_negotiate_tls(nsc, &errstr) != OK) { - adios(NULL, errstr); + die("%s", errstr); } } @@ -325,7 +327,7 @@ main (int argc, char **argv) if (!have_capability()) { char *capstring; - if (send_imap_command(nsc, 0, &errstr, "CAPABILITY") != OK) { + if (send_imap_command(nsc, false, &errstr, "CAPABILITY") != OK) { fprintf(stderr, "Unable to send CAPABILITY command: %s\n", errstr); goto finish; } @@ -346,7 +348,7 @@ main (int argc, char **argv) } if (timestamp) { - ts_report("Authentication time", &tv_connect); + ts_report(&tv_connect, "Authentication time"); gettimeofday(&tv_auth, NULL); } @@ -375,7 +377,8 @@ main (int argc, char **argv) free(imsg); } - ts_report("Total command execution time", &tv_auth); + if (timestamp) + ts_report(&tv_auth, "Total command execution time"); send_imap_command(nsc, 0, NULL, "LOGOUT"); get_imap_response(nsc, NULL, NULL, NULL, 0, NULL); @@ -383,7 +386,8 @@ main (int argc, char **argv) finish: netsec_shutdown(nsc); - ts_report("Total elapsed time", &tv_start); + if (timestamp) + ts_report(&tv_start, "Total elapsed time"); exit(0); } @@ -597,6 +601,7 @@ imap_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata, */ case NETSEC_SASL_FINISH: + line = NULL; if (get_imap_response(nsc, NULL, NULL, &line, 1, errstr) != OK) return NOTOK; /* @@ -604,7 +609,7 @@ imap_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata, * parse it. */ - if (has_prefix(line, "OK [CAPABILITY ")) { + if (line && has_prefix(line, "OK [CAPABILITY ")) { char *p = line + 15, *q; q = strchr(p, ']'); @@ -634,7 +639,7 @@ imap_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata, */ static int -send_imap_command(netsec_context *nsc, int noflush, char **errstr, +send_imap_command(netsec_context *nsc, bool noflush, char **errstr, const char *fmt, ...) { static unsigned int seq = 0; /* Tag sequence number */ @@ -646,8 +651,19 @@ send_imap_command(netsec_context *nsc, int noflush, char **errstr, snprintf(cmd->tag, sizeof(cmd->tag), "A%u ", seq++); - if (timestamp) + if (timestamp) { + char *p; + va_start(ap, fmt); + vsnprintf(cmd->prefix, sizeof(cmd->prefix), fmt, ap); + va_end(ap); + + p = strchr(cmd->prefix, ' '); + + if (p) + *p = '\0'; + gettimeofday(&cmd->start, NULL); + } if (netsec_write(nsc, cmd->tag, strlen(cmd->tag), errstr) != OK) { free(cmd); @@ -688,7 +704,7 @@ get_imap_response(netsec_context *nsc, const char *token, char **tokenresponse, { char *line; struct imap_cmd *cmd; - int numerrs = 0; + bool numerrs = false; if (tokenresponse) *tokenresponse = NULL; @@ -708,7 +724,8 @@ getline: if (has_prefix(line, cmdqueue->tag)) { cmd = cmdqueue; if (timestamp) - ts_report("Command execution time", &cmd->start); + ts_report(&cmd->start, "Command (%s) execution time", + cmd->prefix); cmdqueue = cmd->next; free(cmd); } else { @@ -718,7 +735,7 @@ getline: cmd->next = cmd->next->next; if (failerr && strncmp(line + strlen(cmd2->tag), "OK ", 3) != 0) { - numerrs++; + numerrs = true; netsec_err(errstr, "%s", line + strlen(cmd2->tag)); } free(cmd2); @@ -734,7 +751,7 @@ getline: } } - return numerrs == 0 ? OK : NOTOK; + return numerrs ? NOTOK : OK; } /* @@ -742,7 +759,7 @@ getline: */ static void -add_msg(int queue, const char *fmt, ...) +add_msg(bool queue, const char *fmt, ...) { struct imap_msg *imsg; va_list ap; @@ -778,15 +795,20 @@ add_msg(int queue, const char *fmt, ...) */ static void -ts_report(const char *str, struct timeval *tv) +ts_report(struct timeval *tv, const char *fmt, ...) { struct timeval now; double delta; + va_list ap; gettimeofday(&now, NULL); delta = ((double) now.tv_sec) - ((double) tv->tv_sec) + (now.tv_usec / 1E6 - tv->tv_usec / 1E6); - printf("%s: %f sec\n", str, delta); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + + printf(": %f sec\n", delta); }