Merge branch 'rs/ls-tree-prefix-simplify'
[alt-git.git] / imap-send.c
blobc807e36a7efe6258bddcee30d106c30b13a947d6
1 /*
2 * git-imap-send - drops patches into an imap Drafts folder
3 * derived from isync/mbsync - mailbox synchronizer
5 * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
6 * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
7 * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
8 * Copyright (C) 2006 Mike McCormack
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "git-compat-util.h"
25 #include "config.h"
26 #include "credential.h"
27 #include "exec-cmd.h"
28 #include "gettext.h"
29 #include "run-command.h"
30 #include "parse-options.h"
31 #include "setup.h"
32 #include "strbuf.h"
33 #include "wrapper.h"
34 #if defined(NO_OPENSSL) && !defined(HAVE_OPENSSL_CSPRNG)
35 typedef void *SSL;
36 #endif
37 #ifdef USE_CURL_FOR_IMAP_SEND
38 #include "http.h"
39 #endif
41 #if defined(USE_CURL_FOR_IMAP_SEND)
42 /* Always default to curl if it's available. */
43 #define USE_CURL_DEFAULT 1
44 #else
45 /* We don't have curl, so continue to use the historical implementation */
46 #define USE_CURL_DEFAULT 0
47 #endif
49 static int verbosity;
50 static int use_curl = USE_CURL_DEFAULT;
52 static const char * const imap_send_usage[] = { "git imap-send [-v] [-q] [--[no-]curl] < <mbox>", NULL };
54 static struct option imap_send_options[] = {
55 OPT__VERBOSITY(&verbosity),
56 OPT_BOOL(0, "curl", &use_curl, "use libcurl to communicate with the IMAP server"),
57 OPT_END()
60 #undef DRV_OK
61 #define DRV_OK 0
62 #define DRV_MSG_BAD -1
63 #define DRV_BOX_BAD -2
64 #define DRV_STORE_BAD -3
66 __attribute__((format (printf, 1, 2)))
67 static void imap_info(const char *, ...);
68 __attribute__((format (printf, 1, 2)))
69 static void imap_warn(const char *, ...);
71 static char *next_arg(char **);
73 __attribute__((format (printf, 3, 4)))
74 static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
76 static int nfvasprintf(char **strp, const char *fmt, va_list ap)
78 int len;
79 char tmp[8192];
81 len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
82 if (len < 0)
83 die("Fatal: Out of memory");
84 if (len >= sizeof(tmp))
85 die("imap command overflow!");
86 *strp = xmemdupz(tmp, len);
87 return len;
90 struct imap_server_conf {
91 const char *name;
92 const char *tunnel;
93 const char *host;
94 int port;
95 const char *folder;
96 const char *user;
97 const char *pass;
98 int use_ssl;
99 int ssl_verify;
100 int use_html;
101 const char *auth_method;
104 static struct imap_server_conf server = {
105 .ssl_verify = 1,
108 struct imap_socket {
109 int fd[2];
110 SSL *ssl;
113 struct imap_buffer {
114 struct imap_socket sock;
115 int bytes;
116 int offset;
117 char buf[1024];
120 struct imap_cmd;
122 struct imap {
123 int uidnext; /* from SELECT responses */
124 unsigned caps, rcaps; /* CAPABILITY results */
125 /* command queue */
126 int nexttag, num_in_progress, literal_pending;
127 struct imap_cmd *in_progress, **in_progress_append;
128 struct imap_buffer buf; /* this is BIG, so put it last */
131 struct imap_store {
132 /* currently open mailbox */
133 const char *name; /* foreign! maybe preset? */
134 int uidvalidity;
135 struct imap *imap;
136 const char *prefix;
139 struct imap_cmd_cb {
140 int (*cont)(struct imap_store *ctx, const char *prompt);
141 void *ctx;
142 char *data;
143 int dlen;
146 struct imap_cmd {
147 struct imap_cmd *next;
148 struct imap_cmd_cb cb;
149 char *cmd;
150 int tag;
153 #define CAP(cap) (imap->caps & (1 << (cap)))
155 enum CAPABILITY {
156 NOLOGIN = 0,
157 UIDPLUS,
158 LITERALPLUS,
159 NAMESPACE,
160 STARTTLS,
161 AUTH_CRAM_MD5
164 static const char *cap_list[] = {
165 "LOGINDISABLED",
166 "UIDPLUS",
167 "LITERAL+",
168 "NAMESPACE",
169 "STARTTLS",
170 "AUTH=CRAM-MD5",
173 #define RESP_OK 0
174 #define RESP_NO 1
175 #define RESP_BAD 2
177 static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd);
180 #ifndef NO_OPENSSL
181 static void ssl_socket_perror(const char *func)
183 fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), NULL));
185 #endif
187 static void socket_perror(const char *func, struct imap_socket *sock, int ret)
189 #ifndef NO_OPENSSL
190 if (sock->ssl) {
191 int sslerr = SSL_get_error(sock->ssl, ret);
192 switch (sslerr) {
193 case SSL_ERROR_NONE:
194 break;
195 case SSL_ERROR_SYSCALL:
196 perror("SSL_connect");
197 break;
198 default:
199 ssl_socket_perror("SSL_connect");
200 break;
202 } else
203 #endif
205 if (ret < 0)
206 perror(func);
207 else
208 fprintf(stderr, "%s: unexpected EOF\n", func);
212 #ifdef NO_OPENSSL
213 static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
215 fprintf(stderr, "SSL requested but SSL support not compiled in\n");
216 return -1;
219 #else
221 static int host_matches(const char *host, const char *pattern)
223 if (pattern[0] == '*' && pattern[1] == '.') {
224 pattern += 2;
225 if (!(host = strchr(host, '.')))
226 return 0;
227 host++;
230 return *host && *pattern && !strcasecmp(host, pattern);
233 static int verify_hostname(X509 *cert, const char *hostname)
235 int len;
236 X509_NAME *subj;
237 char cname[1000];
238 int i, found;
239 STACK_OF(GENERAL_NAME) *subj_alt_names;
241 /* try the DNS subjectAltNames */
242 found = 0;
243 if ((subj_alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL))) {
244 int num_subj_alt_names = sk_GENERAL_NAME_num(subj_alt_names);
245 for (i = 0; !found && i < num_subj_alt_names; i++) {
246 GENERAL_NAME *subj_alt_name = sk_GENERAL_NAME_value(subj_alt_names, i);
247 if (subj_alt_name->type == GEN_DNS &&
248 strlen((const char *)subj_alt_name->d.ia5->data) == (size_t)subj_alt_name->d.ia5->length &&
249 host_matches(hostname, (const char *)(subj_alt_name->d.ia5->data)))
250 found = 1;
252 sk_GENERAL_NAME_pop_free(subj_alt_names, GENERAL_NAME_free);
254 if (found)
255 return 0;
257 /* try the common name */
258 if (!(subj = X509_get_subject_name(cert)))
259 return error("cannot get certificate subject");
260 if ((len = X509_NAME_get_text_by_NID(subj, NID_commonName, cname, sizeof(cname))) < 0)
261 return error("cannot get certificate common name");
262 if (strlen(cname) == (size_t)len && host_matches(hostname, cname))
263 return 0;
264 return error("certificate owner '%s' does not match hostname '%s'",
265 cname, hostname);
268 static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
270 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
271 const SSL_METHOD *meth;
272 #else
273 SSL_METHOD *meth;
274 #endif
275 SSL_CTX *ctx;
276 int ret;
277 X509 *cert;
279 SSL_library_init();
280 SSL_load_error_strings();
282 meth = SSLv23_method();
283 if (!meth) {
284 ssl_socket_perror("SSLv23_method");
285 return -1;
288 ctx = SSL_CTX_new(meth);
289 if (!ctx) {
290 ssl_socket_perror("SSL_CTX_new");
291 return -1;
294 if (use_tls_only)
295 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
297 if (verify)
298 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
300 if (!SSL_CTX_set_default_verify_paths(ctx)) {
301 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
302 return -1;
304 sock->ssl = SSL_new(ctx);
305 if (!sock->ssl) {
306 ssl_socket_perror("SSL_new");
307 return -1;
309 if (!SSL_set_rfd(sock->ssl, sock->fd[0])) {
310 ssl_socket_perror("SSL_set_rfd");
311 return -1;
313 if (!SSL_set_wfd(sock->ssl, sock->fd[1])) {
314 ssl_socket_perror("SSL_set_wfd");
315 return -1;
318 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
320 * SNI (RFC4366)
321 * OpenSSL does not document this function, but the implementation
322 * returns 1 on success, 0 on failure after calling SSLerr().
324 ret = SSL_set_tlsext_host_name(sock->ssl, server.host);
325 if (ret != 1)
326 warning("SSL_set_tlsext_host_name(%s) failed.", server.host);
327 #endif
329 ret = SSL_connect(sock->ssl);
330 if (ret <= 0) {
331 socket_perror("SSL_connect", sock, ret);
332 return -1;
335 if (verify) {
336 /* make sure the hostname matches that of the certificate */
337 cert = SSL_get_peer_certificate(sock->ssl);
338 if (!cert)
339 return error("unable to get peer certificate.");
340 if (verify_hostname(cert, server.host) < 0)
341 return -1;
344 return 0;
346 #endif
348 static int socket_read(struct imap_socket *sock, char *buf, int len)
350 ssize_t n;
351 #ifndef NO_OPENSSL
352 if (sock->ssl)
353 n = SSL_read(sock->ssl, buf, len);
354 else
355 #endif
356 n = xread(sock->fd[0], buf, len);
357 if (n <= 0) {
358 socket_perror("read", sock, n);
359 close(sock->fd[0]);
360 close(sock->fd[1]);
361 sock->fd[0] = sock->fd[1] = -1;
363 return n;
366 static int socket_write(struct imap_socket *sock, const char *buf, int len)
368 int n;
369 #ifndef NO_OPENSSL
370 if (sock->ssl)
371 n = SSL_write(sock->ssl, buf, len);
372 else
373 #endif
374 n = write_in_full(sock->fd[1], buf, len);
375 if (n != len) {
376 socket_perror("write", sock, n);
377 close(sock->fd[0]);
378 close(sock->fd[1]);
379 sock->fd[0] = sock->fd[1] = -1;
381 return n;
384 static void socket_shutdown(struct imap_socket *sock)
386 #ifndef NO_OPENSSL
387 if (sock->ssl) {
388 SSL_shutdown(sock->ssl);
389 SSL_free(sock->ssl);
391 #endif
392 close(sock->fd[0]);
393 close(sock->fd[1]);
396 /* simple line buffering */
397 static int buffer_gets(struct imap_buffer *b, char **s)
399 int n;
400 int start = b->offset;
402 *s = b->buf + start;
404 for (;;) {
405 /* make sure we have enough data to read the \r\n sequence */
406 if (b->offset + 1 >= b->bytes) {
407 if (start) {
408 /* shift down used bytes */
409 *s = b->buf;
411 assert(start <= b->bytes);
412 n = b->bytes - start;
414 if (n)
415 memmove(b->buf, b->buf + start, n);
416 b->offset -= start;
417 b->bytes = n;
418 start = 0;
421 n = socket_read(&b->sock, b->buf + b->bytes,
422 sizeof(b->buf) - b->bytes);
424 if (n <= 0)
425 return -1;
427 b->bytes += n;
430 if (b->buf[b->offset] == '\r') {
431 assert(b->offset + 1 < b->bytes);
432 if (b->buf[b->offset + 1] == '\n') {
433 b->buf[b->offset] = 0; /* terminate the string */
434 b->offset += 2; /* next line */
435 if (0 < verbosity)
436 puts(*s);
437 return 0;
441 b->offset++;
443 /* not reached */
446 __attribute__((format (printf, 1, 2)))
447 static void imap_info(const char *msg, ...)
449 va_list va;
451 if (0 <= verbosity) {
452 va_start(va, msg);
453 vprintf(msg, va);
454 va_end(va);
455 fflush(stdout);
459 __attribute__((format (printf, 1, 2)))
460 static void imap_warn(const char *msg, ...)
462 va_list va;
464 if (-2 < verbosity) {
465 va_start(va, msg);
466 vfprintf(stderr, msg, va);
467 va_end(va);
471 static char *next_arg(char **s)
473 char *ret;
475 if (!s || !*s)
476 return NULL;
477 while (isspace((unsigned char) **s))
478 (*s)++;
479 if (!**s) {
480 *s = NULL;
481 return NULL;
483 if (**s == '"') {
484 ++*s;
485 ret = *s;
486 *s = strchr(*s, '"');
487 } else {
488 ret = *s;
489 while (**s && !isspace((unsigned char) **s))
490 (*s)++;
492 if (*s) {
493 if (**s)
494 *(*s)++ = 0;
495 if (!**s)
496 *s = NULL;
498 return ret;
501 __attribute__((format (printf, 3, 4)))
502 static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
504 int ret;
505 va_list va;
507 va_start(va, fmt);
508 if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
509 BUG("buffer too small. Please report a bug.");
510 va_end(va);
511 return ret;
514 static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
515 struct imap_cmd_cb *cb,
516 const char *fmt, va_list ap)
518 struct imap *imap = ctx->imap;
519 struct imap_cmd *cmd;
520 int n, bufl;
521 char buf[1024];
523 cmd = xmalloc(sizeof(struct imap_cmd));
524 nfvasprintf(&cmd->cmd, fmt, ap);
525 cmd->tag = ++imap->nexttag;
527 if (cb)
528 cmd->cb = *cb;
529 else
530 memset(&cmd->cb, 0, sizeof(cmd->cb));
532 while (imap->literal_pending)
533 get_cmd_result(ctx, NULL);
535 if (!cmd->cb.data)
536 bufl = nfsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
537 else
538 bufl = nfsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
539 cmd->tag, cmd->cmd, cmd->cb.dlen,
540 CAP(LITERALPLUS) ? "+" : "");
542 if (0 < verbosity) {
543 if (imap->num_in_progress)
544 printf("(%d in progress) ", imap->num_in_progress);
545 if (!starts_with(cmd->cmd, "LOGIN"))
546 printf(">>> %s", buf);
547 else
548 printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
550 if (socket_write(&imap->buf.sock, buf, bufl) != bufl) {
551 free(cmd->cmd);
552 free(cmd);
553 if (cb)
554 free(cb->data);
555 return NULL;
557 if (cmd->cb.data) {
558 if (CAP(LITERALPLUS)) {
559 n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);
560 free(cmd->cb.data);
561 if (n != cmd->cb.dlen ||
562 socket_write(&imap->buf.sock, "\r\n", 2) != 2) {
563 free(cmd->cmd);
564 free(cmd);
565 return NULL;
567 cmd->cb.data = NULL;
568 } else
569 imap->literal_pending = 1;
570 } else if (cmd->cb.cont)
571 imap->literal_pending = 1;
572 cmd->next = NULL;
573 *imap->in_progress_append = cmd;
574 imap->in_progress_append = &cmd->next;
575 imap->num_in_progress++;
576 return cmd;
579 __attribute__((format (printf, 3, 4)))
580 static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb,
581 const char *fmt, ...)
583 va_list ap;
584 struct imap_cmd *cmdp;
586 va_start(ap, fmt);
587 cmdp = issue_imap_cmd(ctx, cb, fmt, ap);
588 va_end(ap);
589 if (!cmdp)
590 return RESP_BAD;
592 return get_cmd_result(ctx, cmdp);
595 __attribute__((format (printf, 3, 4)))
596 static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb,
597 const char *fmt, ...)
599 va_list ap;
600 struct imap_cmd *cmdp;
602 va_start(ap, fmt);
603 cmdp = issue_imap_cmd(ctx, cb, fmt, ap);
604 va_end(ap);
605 if (!cmdp)
606 return DRV_STORE_BAD;
608 switch (get_cmd_result(ctx, cmdp)) {
609 case RESP_BAD: return DRV_STORE_BAD;
610 case RESP_NO: return DRV_MSG_BAD;
611 default: return DRV_OK;
615 static int skip_imap_list_l(char **sp, int level)
617 char *s = *sp;
619 for (;;) {
620 while (isspace((unsigned char)*s))
621 s++;
622 if (level && *s == ')') {
623 s++;
624 break;
626 if (*s == '(') {
627 /* sublist */
628 s++;
629 if (skip_imap_list_l(&s, level + 1))
630 goto bail;
631 } else if (*s == '"') {
632 /* quoted string */
633 s++;
634 for (; *s != '"'; s++)
635 if (!*s)
636 goto bail;
637 s++;
638 } else {
639 /* atom */
640 for (; *s && !isspace((unsigned char)*s); s++)
641 if (level && *s == ')')
642 break;
645 if (!level)
646 break;
647 if (!*s)
648 goto bail;
650 *sp = s;
651 return 0;
653 bail:
654 return -1;
657 static void skip_list(char **sp)
659 skip_imap_list_l(sp, 0);
662 static void parse_capability(struct imap *imap, char *cmd)
664 char *arg;
665 unsigned i;
667 imap->caps = 0x80000000;
668 while ((arg = next_arg(&cmd)))
669 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
670 if (!strcmp(cap_list[i], arg))
671 imap->caps |= 1 << i;
672 imap->rcaps = imap->caps;
675 static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
676 char *s)
678 struct imap *imap = ctx->imap;
679 char *arg, *p;
681 if (!s || *s != '[')
682 return RESP_OK; /* no response code */
683 s++;
684 if (!(p = strchr(s, ']'))) {
685 fprintf(stderr, "IMAP error: malformed response code\n");
686 return RESP_BAD;
688 *p++ = 0;
689 arg = next_arg(&s);
690 if (!arg) {
691 fprintf(stderr, "IMAP error: empty response code\n");
692 return RESP_BAD;
694 if (!strcmp("UIDVALIDITY", arg)) {
695 if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg))) {
696 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
697 return RESP_BAD;
699 } else if (!strcmp("UIDNEXT", arg)) {
700 if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
701 fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
702 return RESP_BAD;
704 } else if (!strcmp("CAPABILITY", arg)) {
705 parse_capability(imap, s);
706 } else if (!strcmp("ALERT", arg)) {
707 /* RFC2060 says that these messages MUST be displayed
708 * to the user
710 for (; isspace((unsigned char)*p); p++);
711 fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
712 } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
713 if (!(arg = next_arg(&s)) || !(ctx->uidvalidity = atoi(arg)) ||
714 !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
715 fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
716 return RESP_BAD;
719 return RESP_OK;
722 static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
724 struct imap *imap = ctx->imap;
725 struct imap_cmd *cmdp, **pcmdp;
726 char *cmd;
727 const char *arg, *arg1;
728 int n, resp, resp2, tag;
730 for (;;) {
731 if (buffer_gets(&imap->buf, &cmd))
732 return RESP_BAD;
734 arg = next_arg(&cmd);
735 if (!arg) {
736 fprintf(stderr, "IMAP error: empty response\n");
737 return RESP_BAD;
739 if (*arg == '*') {
740 arg = next_arg(&cmd);
741 if (!arg) {
742 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
743 return RESP_BAD;
746 if (!strcmp("NAMESPACE", arg)) {
747 /* rfc2342 NAMESPACE response. */
748 skip_list(&cmd); /* Personal mailboxes */
749 skip_list(&cmd); /* Others' mailboxes */
750 skip_list(&cmd); /* Shared mailboxes */
751 } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
752 !strcmp("NO", arg) || !strcmp("BYE", arg)) {
753 if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
754 return resp;
755 } else if (!strcmp("CAPABILITY", arg)) {
756 parse_capability(imap, cmd);
757 } else if ((arg1 = next_arg(&cmd))) {
758 ; /*
759 * Unhandled response-data with at least two words.
760 * Ignore it.
762 * NEEDSWORK: Previously this case handled '<num> EXISTS'
763 * and '<num> RECENT' but as a probably-unintended side
764 * effect it ignores other unrecognized two-word
765 * responses. imap-send doesn't ever try to read
766 * messages or mailboxes these days, so consider
767 * eliminating this case.
769 } else {
770 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
771 return RESP_BAD;
773 } else if (!imap->in_progress) {
774 fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
775 return RESP_BAD;
776 } else if (*arg == '+') {
777 /* This can happen only with the last command underway, as
778 it enforces a round-trip. */
779 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
780 offsetof(struct imap_cmd, next));
781 if (cmdp->cb.data) {
782 n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
783 FREE_AND_NULL(cmdp->cb.data);
784 if (n != (int)cmdp->cb.dlen)
785 return RESP_BAD;
786 } else if (cmdp->cb.cont) {
787 if (cmdp->cb.cont(ctx, cmd))
788 return RESP_BAD;
789 } else {
790 fprintf(stderr, "IMAP error: unexpected command continuation request\n");
791 return RESP_BAD;
793 if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
794 return RESP_BAD;
795 if (!cmdp->cb.cont)
796 imap->literal_pending = 0;
797 if (!tcmd)
798 return DRV_OK;
799 } else {
800 tag = atoi(arg);
801 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
802 if (cmdp->tag == tag)
803 goto gottag;
804 fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
805 return RESP_BAD;
806 gottag:
807 if (!(*pcmdp = cmdp->next))
808 imap->in_progress_append = pcmdp;
809 imap->num_in_progress--;
810 if (cmdp->cb.cont || cmdp->cb.data)
811 imap->literal_pending = 0;
812 arg = next_arg(&cmd);
813 if (!arg)
814 arg = "";
815 if (!strcmp("OK", arg))
816 resp = DRV_OK;
817 else {
818 if (!strcmp("NO", arg))
819 resp = RESP_NO;
820 else /*if (!strcmp("BAD", arg))*/
821 resp = RESP_BAD;
822 fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
823 !starts_with(cmdp->cmd, "LOGIN") ?
824 cmdp->cmd : "LOGIN <user> <pass>",
825 arg, cmd ? cmd : "");
827 if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp)
828 resp = resp2;
829 free(cmdp->cb.data);
830 free(cmdp->cmd);
831 free(cmdp);
832 if (!tcmd || tcmd == cmdp)
833 return resp;
836 /* not reached */
839 static void imap_close_server(struct imap_store *ictx)
841 struct imap *imap = ictx->imap;
843 if (imap->buf.sock.fd[0] != -1) {
844 imap_exec(ictx, NULL, "LOGOUT");
845 socket_shutdown(&imap->buf.sock);
847 free(imap);
850 static void imap_close_store(struct imap_store *ctx)
852 imap_close_server(ctx);
853 free(ctx);
856 #ifndef NO_OPENSSL
859 * hexchar() and cram() functions are based on the code from the isync
860 * project (http://isync.sf.net/).
862 static char hexchar(unsigned int b)
864 return b < 10 ? '0' + b : 'a' + (b - 10);
867 #define ENCODED_SIZE(n) (4 * DIV_ROUND_UP((n), 3))
868 static char *cram(const char *challenge_64, const char *user, const char *pass)
870 int i, resp_len, encoded_len, decoded_len;
871 unsigned char hash[16];
872 char hex[33];
873 char *response, *response_64, *challenge;
876 * length of challenge_64 (i.e. base-64 encoded string) is a good
877 * enough upper bound for challenge (decoded result).
879 encoded_len = strlen(challenge_64);
880 challenge = xmalloc(encoded_len);
881 decoded_len = EVP_DecodeBlock((unsigned char *)challenge,
882 (unsigned char *)challenge_64, encoded_len);
883 if (decoded_len < 0)
884 die("invalid challenge %s", challenge_64);
885 if (!HMAC(EVP_md5(), pass, strlen(pass), (unsigned char *)challenge, decoded_len, hash, NULL))
886 die("HMAC error");
888 hex[32] = 0;
889 for (i = 0; i < 16; i++) {
890 hex[2 * i] = hexchar((hash[i] >> 4) & 0xf);
891 hex[2 * i + 1] = hexchar(hash[i] & 0xf);
894 /* response: "<user> <digest in hex>" */
895 response = xstrfmt("%s %s", user, hex);
896 resp_len = strlen(response);
898 response_64 = xmallocz(ENCODED_SIZE(resp_len));
899 encoded_len = EVP_EncodeBlock((unsigned char *)response_64,
900 (unsigned char *)response, resp_len);
901 if (encoded_len < 0)
902 die("EVP_EncodeBlock error");
903 return (char *)response_64;
906 #else
908 static char *cram(const char *challenge_64, const char *user, const char *pass)
910 die("If you want to use CRAM-MD5 authenticate method, "
911 "you have to build git-imap-send with OpenSSL library.");
914 #endif
916 static int auth_cram_md5(struct imap_store *ctx, const char *prompt)
918 int ret;
919 char *response;
921 response = cram(prompt, server.user, server.pass);
923 ret = socket_write(&ctx->imap->buf.sock, response, strlen(response));
924 if (ret != strlen(response))
925 return error("IMAP error: sending response failed");
927 free(response);
929 return 0;
932 static void server_fill_credential(struct imap_server_conf *srvc, struct credential *cred)
934 if (srvc->user && srvc->pass)
935 return;
937 cred->protocol = xstrdup(srvc->use_ssl ? "imaps" : "imap");
938 cred->host = xstrdup(srvc->host);
940 cred->username = xstrdup_or_null(srvc->user);
941 cred->password = xstrdup_or_null(srvc->pass);
943 credential_fill(cred);
945 if (!srvc->user)
946 srvc->user = xstrdup(cred->username);
947 if (!srvc->pass)
948 srvc->pass = xstrdup(cred->password);
951 static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const char *folder)
953 struct credential cred = CREDENTIAL_INIT;
954 struct imap_store *ctx;
955 struct imap *imap;
956 char *arg, *rsp;
957 int s = -1, preauth;
959 CALLOC_ARRAY(ctx, 1);
961 ctx->imap = CALLOC_ARRAY(imap, 1);
962 imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
963 imap->in_progress_append = &imap->in_progress;
965 /* open connection to IMAP server */
967 if (srvc->tunnel) {
968 struct child_process tunnel = CHILD_PROCESS_INIT;
970 imap_info("Starting tunnel '%s'... ", srvc->tunnel);
972 strvec_push(&tunnel.args, srvc->tunnel);
973 tunnel.use_shell = 1;
974 tunnel.in = -1;
975 tunnel.out = -1;
976 if (start_command(&tunnel))
977 die("cannot start proxy %s", srvc->tunnel);
979 imap->buf.sock.fd[0] = tunnel.out;
980 imap->buf.sock.fd[1] = tunnel.in;
982 imap_info("ok\n");
983 } else {
984 #ifndef NO_IPV6
985 struct addrinfo hints, *ai0, *ai;
986 int gai;
987 char portstr[6];
989 xsnprintf(portstr, sizeof(portstr), "%d", srvc->port);
991 memset(&hints, 0, sizeof(hints));
992 hints.ai_socktype = SOCK_STREAM;
993 hints.ai_protocol = IPPROTO_TCP;
995 imap_info("Resolving %s... ", srvc->host);
996 gai = getaddrinfo(srvc->host, portstr, &hints, &ai);
997 if (gai) {
998 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai));
999 goto bail;
1001 imap_info("ok\n");
1003 for (ai0 = ai; ai; ai = ai->ai_next) {
1004 char addr[NI_MAXHOST];
1006 s = socket(ai->ai_family, ai->ai_socktype,
1007 ai->ai_protocol);
1008 if (s < 0)
1009 continue;
1011 getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1012 sizeof(addr), NULL, 0, NI_NUMERICHOST);
1013 imap_info("Connecting to [%s]:%s... ", addr, portstr);
1015 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1016 close(s);
1017 s = -1;
1018 perror("connect");
1019 continue;
1022 break;
1024 freeaddrinfo(ai0);
1025 #else /* NO_IPV6 */
1026 struct hostent *he;
1027 struct sockaddr_in addr;
1029 memset(&addr, 0, sizeof(addr));
1030 addr.sin_port = htons(srvc->port);
1031 addr.sin_family = AF_INET;
1033 imap_info("Resolving %s... ", srvc->host);
1034 he = gethostbyname(srvc->host);
1035 if (!he) {
1036 perror("gethostbyname");
1037 goto bail;
1039 imap_info("ok\n");
1041 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
1043 s = socket(PF_INET, SOCK_STREAM, 0);
1045 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1046 if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1047 close(s);
1048 s = -1;
1049 perror("connect");
1051 #endif
1052 if (s < 0) {
1053 fputs("Error: unable to connect to server.\n", stderr);
1054 goto bail;
1057 imap->buf.sock.fd[0] = s;
1058 imap->buf.sock.fd[1] = dup(s);
1060 if (srvc->use_ssl &&
1061 ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1062 close(s);
1063 goto bail;
1065 imap_info("ok\n");
1068 /* read the greeting string */
1069 if (buffer_gets(&imap->buf, &rsp)) {
1070 fprintf(stderr, "IMAP error: no greeting response\n");
1071 goto bail;
1073 arg = next_arg(&rsp);
1074 if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) {
1075 fprintf(stderr, "IMAP error: invalid greeting response\n");
1076 goto bail;
1078 preauth = 0;
1079 if (!strcmp("PREAUTH", arg))
1080 preauth = 1;
1081 else if (strcmp("OK", arg) != 0) {
1082 fprintf(stderr, "IMAP error: unknown greeting response\n");
1083 goto bail;
1085 parse_response_code(ctx, NULL, rsp);
1086 if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1087 goto bail;
1089 if (!preauth) {
1090 #ifndef NO_OPENSSL
1091 if (!srvc->use_ssl && CAP(STARTTLS)) {
1092 if (imap_exec(ctx, NULL, "STARTTLS") != RESP_OK)
1093 goto bail;
1094 if (ssl_socket_connect(&imap->buf.sock, 1,
1095 srvc->ssl_verify))
1096 goto bail;
1097 /* capabilities may have changed, so get the new capabilities */
1098 if (imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1099 goto bail;
1101 #endif
1102 imap_info("Logging in...\n");
1103 server_fill_credential(srvc, &cred);
1105 if (srvc->auth_method) {
1106 struct imap_cmd_cb cb;
1108 if (!strcmp(srvc->auth_method, "CRAM-MD5")) {
1109 if (!CAP(AUTH_CRAM_MD5)) {
1110 fprintf(stderr, "You specified "
1111 "CRAM-MD5 as authentication method, "
1112 "but %s doesn't support it.\n", srvc->host);
1113 goto bail;
1115 /* CRAM-MD5 */
1117 memset(&cb, 0, sizeof(cb));
1118 cb.cont = auth_cram_md5;
1119 if (imap_exec(ctx, &cb, "AUTHENTICATE CRAM-MD5") != RESP_OK) {
1120 fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n");
1121 goto bail;
1123 } else {
1124 fprintf(stderr, "Unknown authentication method:%s\n", srvc->host);
1125 goto bail;
1127 } else {
1128 if (CAP(NOLOGIN)) {
1129 fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n",
1130 srvc->user, srvc->host);
1131 goto bail;
1133 if (!imap->buf.sock.ssl)
1134 imap_warn("*** IMAP Warning *** Password is being "
1135 "sent in the clear\n");
1136 if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) {
1137 fprintf(stderr, "IMAP error: LOGIN failed\n");
1138 goto bail;
1141 } /* !preauth */
1143 if (cred.username)
1144 credential_approve(&cred);
1145 credential_clear(&cred);
1147 /* check the target mailbox exists */
1148 ctx->name = folder;
1149 switch (imap_exec(ctx, NULL, "EXAMINE \"%s\"", ctx->name)) {
1150 case RESP_OK:
1151 /* ok */
1152 break;
1153 case RESP_BAD:
1154 fprintf(stderr, "IMAP error: could not check mailbox\n");
1155 goto out;
1156 case RESP_NO:
1157 if (imap_exec(ctx, NULL, "CREATE \"%s\"", ctx->name) == RESP_OK) {
1158 imap_info("Created missing mailbox\n");
1159 } else {
1160 fprintf(stderr, "IMAP error: could not create missing mailbox\n");
1161 goto out;
1163 break;
1166 ctx->prefix = "";
1167 return ctx;
1169 bail:
1170 if (cred.username)
1171 credential_reject(&cred);
1172 credential_clear(&cred);
1174 out:
1175 imap_close_store(ctx);
1176 return NULL;
1180 * Insert CR characters as necessary in *msg to ensure that every LF
1181 * character in *msg is preceded by a CR.
1183 static void lf_to_crlf(struct strbuf *msg)
1185 char *new_msg;
1186 size_t i, j;
1187 char lastc;
1189 /* First pass: tally, in j, the size of the new_msg string: */
1190 for (i = j = 0, lastc = '\0'; i < msg->len; i++) {
1191 if (msg->buf[i] == '\n' && lastc != '\r')
1192 j++; /* a CR will need to be added here */
1193 lastc = msg->buf[i];
1194 j++;
1197 new_msg = xmallocz(j);
1200 * Second pass: write the new_msg string. Note that this loop is
1201 * otherwise identical to the first pass.
1203 for (i = j = 0, lastc = '\0'; i < msg->len; i++) {
1204 if (msg->buf[i] == '\n' && lastc != '\r')
1205 new_msg[j++] = '\r';
1206 lastc = new_msg[j++] = msg->buf[i];
1208 strbuf_attach(msg, new_msg, j, j + 1);
1212 * Store msg to IMAP. Also detach and free the data from msg->data,
1213 * leaving msg->data empty.
1215 static int imap_store_msg(struct imap_store *ctx, struct strbuf *msg)
1217 struct imap *imap = ctx->imap;
1218 struct imap_cmd_cb cb;
1219 const char *prefix, *box;
1220 int ret;
1222 lf_to_crlf(msg);
1223 memset(&cb, 0, sizeof(cb));
1225 cb.dlen = msg->len;
1226 cb.data = strbuf_detach(msg, NULL);
1228 box = ctx->name;
1229 prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1230 ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" ", prefix, box);
1231 imap->caps = imap->rcaps;
1232 if (ret != DRV_OK)
1233 return ret;
1235 return DRV_OK;
1238 static void wrap_in_html(struct strbuf *msg)
1240 struct strbuf buf = STRBUF_INIT;
1241 static char *content_type = "Content-Type: text/html;\n";
1242 static char *pre_open = "<pre>\n";
1243 static char *pre_close = "</pre>\n";
1244 const char *body = strstr(msg->buf, "\n\n");
1246 if (!body)
1247 return; /* Headers but no body; no wrapping needed */
1249 body += 2;
1251 strbuf_add(&buf, msg->buf, body - msg->buf - 1);
1252 strbuf_addstr(&buf, content_type);
1253 strbuf_addch(&buf, '\n');
1254 strbuf_addstr(&buf, pre_open);
1255 strbuf_addstr_xml_quoted(&buf, body);
1256 strbuf_addstr(&buf, pre_close);
1258 strbuf_release(msg);
1259 *msg = buf;
1262 static int count_messages(struct strbuf *all_msgs)
1264 int count = 0;
1265 char *p = all_msgs->buf;
1267 while (1) {
1268 if (starts_with(p, "From ")) {
1269 p = strstr(p+5, "\nFrom: ");
1270 if (!p) break;
1271 p = strstr(p+7, "\nDate: ");
1272 if (!p) break;
1273 p = strstr(p+7, "\nSubject: ");
1274 if (!p) break;
1275 p += 10;
1276 count++;
1278 p = strstr(p+5, "\nFrom ");
1279 if (!p)
1280 break;
1281 p++;
1283 return count;
1287 * Copy the next message from all_msgs, starting at offset *ofs, to
1288 * msg. Update *ofs to the start of the following message. Return
1289 * true iff a message was successfully copied.
1291 static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
1293 char *p, *data;
1294 size_t len;
1296 if (*ofs >= all_msgs->len)
1297 return 0;
1299 data = &all_msgs->buf[*ofs];
1300 len = all_msgs->len - *ofs;
1302 if (len < 5 || !starts_with(data, "From "))
1303 return 0;
1305 p = strchr(data, '\n');
1306 if (p) {
1307 p++;
1308 len -= p - data;
1309 *ofs += p - data;
1310 data = p;
1313 p = strstr(data, "\nFrom ");
1314 if (p)
1315 len = &p[1] - data;
1317 strbuf_add(msg, data, len);
1318 *ofs += len;
1319 return 1;
1322 static int git_imap_config(const char *var, const char *val,
1323 const struct config_context *ctx, void *cb)
1326 if (!strcmp("imap.sslverify", var))
1327 server.ssl_verify = git_config_bool(var, val);
1328 else if (!strcmp("imap.preformattedhtml", var))
1329 server.use_html = git_config_bool(var, val);
1330 else if (!strcmp("imap.folder", var))
1331 return git_config_string(&server.folder, var, val);
1332 else if (!strcmp("imap.user", var))
1333 return git_config_string(&server.user, var, val);
1334 else if (!strcmp("imap.pass", var))
1335 return git_config_string(&server.pass, var, val);
1336 else if (!strcmp("imap.tunnel", var))
1337 return git_config_string(&server.tunnel, var, val);
1338 else if (!strcmp("imap.authmethod", var))
1339 return git_config_string(&server.auth_method, var, val);
1340 else if (!strcmp("imap.port", var))
1341 server.port = git_config_int(var, val, ctx->kvi);
1342 else if (!strcmp("imap.host", var)) {
1343 if (!val) {
1344 git_die_config("imap.host", "Missing value for 'imap.host'");
1345 } else {
1346 if (starts_with(val, "imap:"))
1347 val += 5;
1348 else if (starts_with(val, "imaps:")) {
1349 val += 6;
1350 server.use_ssl = 1;
1352 if (starts_with(val, "//"))
1353 val += 2;
1354 server.host = xstrdup(val);
1356 } else
1357 return git_default_config(var, val, ctx, cb);
1359 return 0;
1362 static int append_msgs_to_imap(struct imap_server_conf *server,
1363 struct strbuf* all_msgs, int total)
1365 struct strbuf msg = STRBUF_INIT;
1366 struct imap_store *ctx = NULL;
1367 int ofs = 0;
1368 int r;
1369 int n = 0;
1371 ctx = imap_open_store(server, server->folder);
1372 if (!ctx) {
1373 fprintf(stderr, "failed to open store\n");
1374 return 1;
1376 ctx->name = server->folder;
1378 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1379 while (1) {
1380 unsigned percent = n * 100 / total;
1382 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1384 if (!split_msg(all_msgs, &msg, &ofs))
1385 break;
1386 if (server->use_html)
1387 wrap_in_html(&msg);
1388 r = imap_store_msg(ctx, &msg);
1389 if (r != DRV_OK)
1390 break;
1391 n++;
1393 fprintf(stderr, "\n");
1395 imap_close_store(ctx);
1397 return 0;
1400 #ifdef USE_CURL_FOR_IMAP_SEND
1401 static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
1403 CURL *curl;
1404 struct strbuf path = STRBUF_INIT;
1405 char *uri_encoded_folder;
1407 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1408 die("curl_global_init failed");
1410 curl = curl_easy_init();
1412 if (!curl)
1413 die("curl_easy_init failed");
1415 server_fill_credential(srvc, cred);
1416 curl_easy_setopt(curl, CURLOPT_USERNAME, srvc->user);
1417 curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass);
1419 strbuf_addstr(&path, srvc->use_ssl ? "imaps://" : "imap://");
1420 strbuf_addstr(&path, srvc->host);
1421 if (!path.len || path.buf[path.len - 1] != '/')
1422 strbuf_addch(&path, '/');
1424 uri_encoded_folder = curl_easy_escape(curl, srvc->folder, 0);
1425 if (!uri_encoded_folder)
1426 die("failed to encode server folder");
1427 strbuf_addstr(&path, uri_encoded_folder);
1428 curl_free(uri_encoded_folder);
1430 curl_easy_setopt(curl, CURLOPT_URL, path.buf);
1431 strbuf_release(&path);
1432 curl_easy_setopt(curl, CURLOPT_PORT, srvc->port);
1434 if (srvc->auth_method) {
1435 #ifndef GIT_CURL_HAVE_CURLOPT_LOGIN_OPTIONS
1436 warning("No LOGIN_OPTIONS support in this cURL version");
1437 #else
1438 struct strbuf auth = STRBUF_INIT;
1439 strbuf_addstr(&auth, "AUTH=");
1440 strbuf_addstr(&auth, srvc->auth_method);
1441 curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, auth.buf);
1442 strbuf_release(&auth);
1443 #endif
1446 if (!srvc->use_ssl)
1447 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
1449 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, srvc->ssl_verify);
1450 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, srvc->ssl_verify);
1452 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
1454 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
1456 if (0 < verbosity || getenv("GIT_CURL_VERBOSE"))
1457 http_trace_curl_no_data();
1458 setup_curl_trace(curl);
1460 return curl;
1463 static int curl_append_msgs_to_imap(struct imap_server_conf *server,
1464 struct strbuf* all_msgs, int total)
1466 int ofs = 0;
1467 int n = 0;
1468 struct buffer msgbuf = { STRBUF_INIT, 0 };
1469 CURL *curl;
1470 CURLcode res = CURLE_OK;
1471 struct credential cred = CREDENTIAL_INIT;
1473 curl = setup_curl(server, &cred);
1474 curl_easy_setopt(curl, CURLOPT_READDATA, &msgbuf);
1476 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1477 while (1) {
1478 unsigned percent = n * 100 / total;
1479 int prev_len;
1481 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1483 prev_len = msgbuf.buf.len;
1484 if (!split_msg(all_msgs, &msgbuf.buf, &ofs))
1485 break;
1486 if (server->use_html)
1487 wrap_in_html(&msgbuf.buf);
1488 lf_to_crlf(&msgbuf.buf);
1490 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
1491 (curl_off_t)(msgbuf.buf.len-prev_len));
1493 res = curl_easy_perform(curl);
1495 if(res != CURLE_OK) {
1496 fprintf(stderr, "curl_easy_perform() failed: %s\n",
1497 curl_easy_strerror(res));
1498 break;
1501 n++;
1503 fprintf(stderr, "\n");
1505 curl_easy_cleanup(curl);
1506 curl_global_cleanup();
1508 if (cred.username) {
1509 if (res == CURLE_OK)
1510 credential_approve(&cred);
1511 else if (res == CURLE_LOGIN_DENIED)
1512 credential_reject(&cred);
1515 credential_clear(&cred);
1517 return res != CURLE_OK;
1519 #endif
1521 int cmd_main(int argc, const char **argv)
1523 struct strbuf all_msgs = STRBUF_INIT;
1524 int total;
1525 int nongit_ok;
1527 setup_git_directory_gently(&nongit_ok);
1528 git_config(git_imap_config, NULL);
1530 argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0);
1532 if (argc)
1533 usage_with_options(imap_send_usage, imap_send_options);
1535 #ifndef USE_CURL_FOR_IMAP_SEND
1536 if (use_curl) {
1537 warning("--curl not supported in this build");
1538 use_curl = 0;
1540 #elif defined(NO_OPENSSL)
1541 if (!use_curl) {
1542 warning("--no-curl not supported in this build");
1543 use_curl = 1;
1545 #endif
1547 if (!server.port)
1548 server.port = server.use_ssl ? 993 : 143;
1550 if (!server.folder) {
1551 fprintf(stderr, "no imap store specified\n");
1552 return 1;
1554 if (!server.host) {
1555 if (!server.tunnel) {
1556 fprintf(stderr, "no imap host specified\n");
1557 return 1;
1559 server.host = "tunnel";
1562 /* read the messages */
1563 if (strbuf_read(&all_msgs, 0, 0) < 0) {
1564 error_errno(_("could not read from stdin"));
1565 return 1;
1568 if (all_msgs.len == 0) {
1569 fprintf(stderr, "nothing to send\n");
1570 return 1;
1573 total = count_messages(&all_msgs);
1574 if (!total) {
1575 fprintf(stderr, "no messages to send\n");
1576 return 1;
1579 /* write it to the imap server */
1581 if (server.tunnel)
1582 return append_msgs_to_imap(&server, &all_msgs, total);
1584 #ifdef USE_CURL_FOR_IMAP_SEND
1585 if (use_curl)
1586 return curl_append_msgs_to_imap(&server, &all_msgs, total);
1587 #endif
1589 return append_msgs_to_imap(&server, &all_msgs, total);