29c10a417c0dd5f71340728dfade4e3bda7b93a3
1/*
2 * git-imap-send - drops patches into an imap Drafts folder
3 * derived from isync/mbsync - mailbox synchronizer
4 *
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
9 *
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.
14 *
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.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include "cache.h"
26#include "exec_cmd.h"
27#include "run-command.h"
28#include "prompt.h"
29#ifdef NO_OPENSSL
30typedef void *SSL;
31#else
32#include <openssl/evp.h>
33#include <openssl/hmac.h>
34#endif
35
36struct store_conf {
37 char *name;
38 const char *path; /* should this be here? its interpretation is driver-specific */
39 char *map_inbox;
40 char *trash;
41 unsigned max_size; /* off_t is overkill */
42 unsigned trash_remote_new:1, trash_only_new:1;
43};
44
45/* For message->status */
46#define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
47#define M_DEAD (1<<1) /* expunged */
48#define M_FLAGS (1<<2) /* flags fetched */
49
50struct message {
51 struct message *next;
52 size_t size; /* zero implies "not fetched" */
53 int uid;
54 unsigned char flags, status;
55};
56
57struct store {
58 struct store_conf *conf; /* foreign */
59
60 /* currently open mailbox */
61 const char *name; /* foreign! maybe preset? */
62 char *path; /* own */
63 struct message *msgs; /* own */
64 int uidvalidity;
65 unsigned char opts; /* maybe preset? */
66 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
67 int count; /* # of messages */
68 int recent; /* # of recent messages - don't trust this beyond the initial read */
69};
70
71static const char imap_send_usage[] = "git imap-send < <mbox>";
72
73#undef DRV_OK
74#define DRV_OK 0
75#define DRV_MSG_BAD -1
76#define DRV_BOX_BAD -2
77#define DRV_STORE_BAD -3
78
79static int Verbose, Quiet;
80
81__attribute__((format (printf, 1, 2)))
82static void imap_info(const char *, ...);
83__attribute__((format (printf, 1, 2)))
84static void imap_warn(const char *, ...);
85
86static char *next_arg(char **);
87
88static void free_generic_messages(struct message *);
89
90__attribute__((format (printf, 3, 4)))
91static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
92
93static int nfvasprintf(char **strp, const char *fmt, va_list ap)
94{
95 int len;
96 char tmp[8192];
97
98 len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
99 if (len < 0)
100 die("Fatal: Out of memory");
101 if (len >= sizeof(tmp))
102 die("imap command overflow!");
103 *strp = xmemdupz(tmp, len);
104 return len;
105}
106
107struct imap_server_conf {
108 char *name;
109 char *tunnel;
110 char *host;
111 int port;
112 char *user;
113 char *pass;
114 int use_ssl;
115 int ssl_verify;
116 int use_html;
117 char *auth_method;
118};
119
120static struct imap_server_conf server = {
121 NULL, /* name */
122 NULL, /* tunnel */
123 NULL, /* host */
124 0, /* port */
125 NULL, /* user */
126 NULL, /* pass */
127 0, /* use_ssl */
128 1, /* ssl_verify */
129 0, /* use_html */
130 NULL, /* auth_method */
131};
132
133struct imap_store_conf {
134 struct store_conf gen;
135 struct imap_server_conf *server;
136};
137
138#define NIL (void *)0x1
139#define LIST (void *)0x2
140
141struct imap_list {
142 struct imap_list *next, *child;
143 char *val;
144 int len;
145};
146
147struct imap_socket {
148 int fd[2];
149 SSL *ssl;
150};
151
152struct imap_buffer {
153 struct imap_socket sock;
154 int bytes;
155 int offset;
156 char buf[1024];
157};
158
159struct imap_cmd;
160
161struct imap {
162 int uidnext; /* from SELECT responses */
163 struct imap_list *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
164 unsigned caps, rcaps; /* CAPABILITY results */
165 /* command queue */
166 int nexttag, num_in_progress, literal_pending;
167 struct imap_cmd *in_progress, **in_progress_append;
168 struct imap_buffer buf; /* this is BIG, so put it last */
169};
170
171struct imap_store {
172 struct store gen;
173 int uidvalidity;
174 struct imap *imap;
175 const char *prefix;
176 unsigned /*currentnc:1,*/ trashnc:1;
177};
178
179struct imap_cmd_cb {
180 int (*cont)(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt);
181 void (*done)(struct imap_store *ctx, struct imap_cmd *cmd, int response);
182 void *ctx;
183 char *data;
184 int dlen;
185 int uid;
186 unsigned create:1, trycreate:1;
187};
188
189struct imap_cmd {
190 struct imap_cmd *next;
191 struct imap_cmd_cb cb;
192 char *cmd;
193 int tag;
194};
195
196#define CAP(cap) (imap->caps & (1 << (cap)))
197
198enum CAPABILITY {
199 NOLOGIN = 0,
200 UIDPLUS,
201 LITERALPLUS,
202 NAMESPACE,
203 STARTTLS,
204 AUTH_CRAM_MD5
205};
206
207static const char *cap_list[] = {
208 "LOGINDISABLED",
209 "UIDPLUS",
210 "LITERAL+",
211 "NAMESPACE",
212 "STARTTLS",
213 "AUTH=CRAM-MD5",
214};
215
216#define RESP_OK 0
217#define RESP_NO 1
218#define RESP_BAD 2
219
220static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd);
221
222
223#ifndef NO_OPENSSL
224static void ssl_socket_perror(const char *func)
225{
226 fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), NULL));
227}
228#endif
229
230static void socket_perror(const char *func, struct imap_socket *sock, int ret)
231{
232#ifndef NO_OPENSSL
233 if (sock->ssl) {
234 int sslerr = SSL_get_error(sock->ssl, ret);
235 switch (sslerr) {
236 case SSL_ERROR_NONE:
237 break;
238 case SSL_ERROR_SYSCALL:
239 perror("SSL_connect");
240 break;
241 default:
242 ssl_socket_perror("SSL_connect");
243 break;
244 }
245 } else
246#endif
247 {
248 if (ret < 0)
249 perror(func);
250 else
251 fprintf(stderr, "%s: unexpected EOF\n", func);
252 }
253}
254
255static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
256{
257#ifdef NO_OPENSSL
258 fprintf(stderr, "SSL requested but SSL support not compiled in\n");
259 return -1;
260#else
261#if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
262 const SSL_METHOD *meth;
263#else
264 SSL_METHOD *meth;
265#endif
266 SSL_CTX *ctx;
267 int ret;
268
269 SSL_library_init();
270 SSL_load_error_strings();
271
272 if (use_tls_only)
273 meth = TLSv1_method();
274 else
275 meth = SSLv23_method();
276
277 if (!meth) {
278 ssl_socket_perror("SSLv23_method");
279 return -1;
280 }
281
282 ctx = SSL_CTX_new(meth);
283
284 if (verify)
285 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
286
287 if (!SSL_CTX_set_default_verify_paths(ctx)) {
288 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
289 return -1;
290 }
291 sock->ssl = SSL_new(ctx);
292 if (!sock->ssl) {
293 ssl_socket_perror("SSL_new");
294 return -1;
295 }
296 if (!SSL_set_rfd(sock->ssl, sock->fd[0])) {
297 ssl_socket_perror("SSL_set_rfd");
298 return -1;
299 }
300 if (!SSL_set_wfd(sock->ssl, sock->fd[1])) {
301 ssl_socket_perror("SSL_set_wfd");
302 return -1;
303 }
304
305 ret = SSL_connect(sock->ssl);
306 if (ret <= 0) {
307 socket_perror("SSL_connect", sock, ret);
308 return -1;
309 }
310
311 return 0;
312#endif
313}
314
315static int socket_read(struct imap_socket *sock, char *buf, int len)
316{
317 ssize_t n;
318#ifndef NO_OPENSSL
319 if (sock->ssl)
320 n = SSL_read(sock->ssl, buf, len);
321 else
322#endif
323 n = xread(sock->fd[0], buf, len);
324 if (n <= 0) {
325 socket_perror("read", sock, n);
326 close(sock->fd[0]);
327 close(sock->fd[1]);
328 sock->fd[0] = sock->fd[1] = -1;
329 }
330 return n;
331}
332
333static int socket_write(struct imap_socket *sock, const char *buf, int len)
334{
335 int n;
336#ifndef NO_OPENSSL
337 if (sock->ssl)
338 n = SSL_write(sock->ssl, buf, len);
339 else
340#endif
341 n = write_in_full(sock->fd[1], buf, len);
342 if (n != len) {
343 socket_perror("write", sock, n);
344 close(sock->fd[0]);
345 close(sock->fd[1]);
346 sock->fd[0] = sock->fd[1] = -1;
347 }
348 return n;
349}
350
351static void socket_shutdown(struct imap_socket *sock)
352{
353#ifndef NO_OPENSSL
354 if (sock->ssl) {
355 SSL_shutdown(sock->ssl);
356 SSL_free(sock->ssl);
357 }
358#endif
359 close(sock->fd[0]);
360 close(sock->fd[1]);
361}
362
363/* simple line buffering */
364static int buffer_gets(struct imap_buffer *b, char **s)
365{
366 int n;
367 int start = b->offset;
368
369 *s = b->buf + start;
370
371 for (;;) {
372 /* make sure we have enough data to read the \r\n sequence */
373 if (b->offset + 1 >= b->bytes) {
374 if (start) {
375 /* shift down used bytes */
376 *s = b->buf;
377
378 assert(start <= b->bytes);
379 n = b->bytes - start;
380
381 if (n)
382 memmove(b->buf, b->buf + start, n);
383 b->offset -= start;
384 b->bytes = n;
385 start = 0;
386 }
387
388 n = socket_read(&b->sock, b->buf + b->bytes,
389 sizeof(b->buf) - b->bytes);
390
391 if (n <= 0)
392 return -1;
393
394 b->bytes += n;
395 }
396
397 if (b->buf[b->offset] == '\r') {
398 assert(b->offset + 1 < b->bytes);
399 if (b->buf[b->offset + 1] == '\n') {
400 b->buf[b->offset] = 0; /* terminate the string */
401 b->offset += 2; /* next line */
402 if (Verbose)
403 puts(*s);
404 return 0;
405 }
406 }
407
408 b->offset++;
409 }
410 /* not reached */
411}
412
413static void imap_info(const char *msg, ...)
414{
415 va_list va;
416
417 if (!Quiet) {
418 va_start(va, msg);
419 vprintf(msg, va);
420 va_end(va);
421 fflush(stdout);
422 }
423}
424
425static void imap_warn(const char *msg, ...)
426{
427 va_list va;
428
429 if (Quiet < 2) {
430 va_start(va, msg);
431 vfprintf(stderr, msg, va);
432 va_end(va);
433 }
434}
435
436static char *next_arg(char **s)
437{
438 char *ret;
439
440 if (!s || !*s)
441 return NULL;
442 while (isspace((unsigned char) **s))
443 (*s)++;
444 if (!**s) {
445 *s = NULL;
446 return NULL;
447 }
448 if (**s == '"') {
449 ++*s;
450 ret = *s;
451 *s = strchr(*s, '"');
452 } else {
453 ret = *s;
454 while (**s && !isspace((unsigned char) **s))
455 (*s)++;
456 }
457 if (*s) {
458 if (**s)
459 *(*s)++ = 0;
460 if (!**s)
461 *s = NULL;
462 }
463 return ret;
464}
465
466static void free_generic_messages(struct message *msgs)
467{
468 struct message *tmsg;
469
470 for (; msgs; msgs = tmsg) {
471 tmsg = msgs->next;
472 free(msgs);
473 }
474}
475
476static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
477{
478 int ret;
479 va_list va;
480
481 va_start(va, fmt);
482 if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
483 die("Fatal: buffer too small. Please report a bug.");
484 va_end(va);
485 return ret;
486}
487
488static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
489 struct imap_cmd_cb *cb,
490 const char *fmt, va_list ap)
491{
492 struct imap *imap = ctx->imap;
493 struct imap_cmd *cmd;
494 int n, bufl;
495 char buf[1024];
496
497 cmd = xmalloc(sizeof(struct imap_cmd));
498 nfvasprintf(&cmd->cmd, fmt, ap);
499 cmd->tag = ++imap->nexttag;
500
501 if (cb)
502 cmd->cb = *cb;
503 else
504 memset(&cmd->cb, 0, sizeof(cmd->cb));
505
506 while (imap->literal_pending)
507 get_cmd_result(ctx, NULL);
508
509 if (!cmd->cb.data)
510 bufl = nfsnprintf(buf, sizeof(buf), "%d %s\r\n", cmd->tag, cmd->cmd);
511 else
512 bufl = nfsnprintf(buf, sizeof(buf), "%d %s{%d%s}\r\n",
513 cmd->tag, cmd->cmd, cmd->cb.dlen,
514 CAP(LITERALPLUS) ? "+" : "");
515
516 if (Verbose) {
517 if (imap->num_in_progress)
518 printf("(%d in progress) ", imap->num_in_progress);
519 if (memcmp(cmd->cmd, "LOGIN", 5))
520 printf(">>> %s", buf);
521 else
522 printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
523 }
524 if (socket_write(&imap->buf.sock, buf, bufl) != bufl) {
525 free(cmd->cmd);
526 free(cmd);
527 if (cb)
528 free(cb->data);
529 return NULL;
530 }
531 if (cmd->cb.data) {
532 if (CAP(LITERALPLUS)) {
533 n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);
534 free(cmd->cb.data);
535 if (n != cmd->cb.dlen ||
536 socket_write(&imap->buf.sock, "\r\n", 2) != 2) {
537 free(cmd->cmd);
538 free(cmd);
539 return NULL;
540 }
541 cmd->cb.data = NULL;
542 } else
543 imap->literal_pending = 1;
544 } else if (cmd->cb.cont)
545 imap->literal_pending = 1;
546 cmd->next = NULL;
547 *imap->in_progress_append = cmd;
548 imap->in_progress_append = &cmd->next;
549 imap->num_in_progress++;
550 return cmd;
551}
552
553__attribute__((format (printf, 3, 4)))
554static struct imap_cmd *issue_imap_cmd(struct imap_store *ctx,
555 struct imap_cmd_cb *cb,
556 const char *fmt, ...)
557{
558 struct imap_cmd *ret;
559 va_list ap;
560
561 va_start(ap, fmt);
562 ret = v_issue_imap_cmd(ctx, cb, fmt, ap);
563 va_end(ap);
564 return ret;
565}
566
567__attribute__((format (printf, 3, 4)))
568static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb,
569 const char *fmt, ...)
570{
571 va_list ap;
572 struct imap_cmd *cmdp;
573
574 va_start(ap, fmt);
575 cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
576 va_end(ap);
577 if (!cmdp)
578 return RESP_BAD;
579
580 return get_cmd_result(ctx, cmdp);
581}
582
583__attribute__((format (printf, 3, 4)))
584static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb,
585 const char *fmt, ...)
586{
587 va_list ap;
588 struct imap_cmd *cmdp;
589
590 va_start(ap, fmt);
591 cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
592 va_end(ap);
593 if (!cmdp)
594 return DRV_STORE_BAD;
595
596 switch (get_cmd_result(ctx, cmdp)) {
597 case RESP_BAD: return DRV_STORE_BAD;
598 case RESP_NO: return DRV_MSG_BAD;
599 default: return DRV_OK;
600 }
601}
602
603static int is_atom(struct imap_list *list)
604{
605 return list && list->val && list->val != NIL && list->val != LIST;
606}
607
608static int is_list(struct imap_list *list)
609{
610 return list && list->val == LIST;
611}
612
613static void free_list(struct imap_list *list)
614{
615 struct imap_list *tmp;
616
617 for (; list; list = tmp) {
618 tmp = list->next;
619 if (is_list(list))
620 free_list(list->child);
621 else if (is_atom(list))
622 free(list->val);
623 free(list);
624 }
625}
626
627static int parse_imap_list_l(struct imap *imap, char **sp, struct imap_list **curp, int level)
628{
629 struct imap_list *cur;
630 char *s = *sp, *p;
631 int n, bytes;
632
633 for (;;) {
634 while (isspace((unsigned char)*s))
635 s++;
636 if (level && *s == ')') {
637 s++;
638 break;
639 }
640 *curp = cur = xmalloc(sizeof(*cur));
641 curp = &cur->next;
642 cur->val = NULL; /* for clean bail */
643 if (*s == '(') {
644 /* sublist */
645 s++;
646 cur->val = LIST;
647 if (parse_imap_list_l(imap, &s, &cur->child, level + 1))
648 goto bail;
649 } else if (imap && *s == '{') {
650 /* literal */
651 bytes = cur->len = strtol(s + 1, &s, 10);
652 if (*s != '}')
653 goto bail;
654
655 s = cur->val = xmalloc(cur->len);
656
657 /* dump whats left over in the input buffer */
658 n = imap->buf.bytes - imap->buf.offset;
659
660 if (n > bytes)
661 /* the entire message fit in the buffer */
662 n = bytes;
663
664 memcpy(s, imap->buf.buf + imap->buf.offset, n);
665 s += n;
666 bytes -= n;
667
668 /* mark that we used part of the buffer */
669 imap->buf.offset += n;
670
671 /* now read the rest of the message */
672 while (bytes > 0) {
673 if ((n = socket_read(&imap->buf.sock, s, bytes)) <= 0)
674 goto bail;
675 s += n;
676 bytes -= n;
677 }
678
679 if (buffer_gets(&imap->buf, &s))
680 goto bail;
681 } else if (*s == '"') {
682 /* quoted string */
683 s++;
684 p = s;
685 for (; *s != '"'; s++)
686 if (!*s)
687 goto bail;
688 cur->len = s - p;
689 s++;
690 cur->val = xmemdupz(p, cur->len);
691 } else {
692 /* atom */
693 p = s;
694 for (; *s && !isspace((unsigned char)*s); s++)
695 if (level && *s == ')')
696 break;
697 cur->len = s - p;
698 if (cur->len == 3 && !memcmp("NIL", p, 3))
699 cur->val = NIL;
700 else
701 cur->val = xmemdupz(p, cur->len);
702 }
703
704 if (!level)
705 break;
706 if (!*s)
707 goto bail;
708 }
709 *sp = s;
710 *curp = NULL;
711 return 0;
712
713bail:
714 *curp = NULL;
715 return -1;
716}
717
718static struct imap_list *parse_imap_list(struct imap *imap, char **sp)
719{
720 struct imap_list *head;
721
722 if (!parse_imap_list_l(imap, sp, &head, 0))
723 return head;
724 free_list(head);
725 return NULL;
726}
727
728static struct imap_list *parse_list(char **sp)
729{
730 return parse_imap_list(NULL, sp);
731}
732
733static void parse_capability(struct imap *imap, char *cmd)
734{
735 char *arg;
736 unsigned i;
737
738 imap->caps = 0x80000000;
739 while ((arg = next_arg(&cmd)))
740 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
741 if (!strcmp(cap_list[i], arg))
742 imap->caps |= 1 << i;
743 imap->rcaps = imap->caps;
744}
745
746static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
747 char *s)
748{
749 struct imap *imap = ctx->imap;
750 char *arg, *p;
751
752 if (*s != '[')
753 return RESP_OK; /* no response code */
754 s++;
755 if (!(p = strchr(s, ']'))) {
756 fprintf(stderr, "IMAP error: malformed response code\n");
757 return RESP_BAD;
758 }
759 *p++ = 0;
760 arg = next_arg(&s);
761 if (!strcmp("UIDVALIDITY", arg)) {
762 if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg))) {
763 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
764 return RESP_BAD;
765 }
766 } else if (!strcmp("UIDNEXT", arg)) {
767 if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
768 fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
769 return RESP_BAD;
770 }
771 } else if (!strcmp("CAPABILITY", arg)) {
772 parse_capability(imap, s);
773 } else if (!strcmp("ALERT", arg)) {
774 /* RFC2060 says that these messages MUST be displayed
775 * to the user
776 */
777 for (; isspace((unsigned char)*p); p++);
778 fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
779 } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
780 if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg)) ||
781 !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg))) {
782 fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
783 return RESP_BAD;
784 }
785 }
786 return RESP_OK;
787}
788
789static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
790{
791 struct imap *imap = ctx->imap;
792 struct imap_cmd *cmdp, **pcmdp, *ncmdp;
793 char *cmd, *arg, *arg1, *p;
794 int n, resp, resp2, tag;
795
796 for (;;) {
797 if (buffer_gets(&imap->buf, &cmd))
798 return RESP_BAD;
799
800 arg = next_arg(&cmd);
801 if (*arg == '*') {
802 arg = next_arg(&cmd);
803 if (!arg) {
804 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
805 return RESP_BAD;
806 }
807
808 if (!strcmp("NAMESPACE", arg)) {
809 imap->ns_personal = parse_list(&cmd);
810 imap->ns_other = parse_list(&cmd);
811 imap->ns_shared = parse_list(&cmd);
812 } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
813 !strcmp("NO", arg) || !strcmp("BYE", arg)) {
814 if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
815 return resp;
816 } else if (!strcmp("CAPABILITY", arg))
817 parse_capability(imap, cmd);
818 else if ((arg1 = next_arg(&cmd))) {
819 if (!strcmp("EXISTS", arg1))
820 ctx->gen.count = atoi(arg);
821 else if (!strcmp("RECENT", arg1))
822 ctx->gen.recent = atoi(arg);
823 } else {
824 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
825 return RESP_BAD;
826 }
827 } else if (!imap->in_progress) {
828 fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
829 return RESP_BAD;
830 } else if (*arg == '+') {
831 /* This can happen only with the last command underway, as
832 it enforces a round-trip. */
833 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
834 offsetof(struct imap_cmd, next));
835 if (cmdp->cb.data) {
836 n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
837 free(cmdp->cb.data);
838 cmdp->cb.data = NULL;
839 if (n != (int)cmdp->cb.dlen)
840 return RESP_BAD;
841 } else if (cmdp->cb.cont) {
842 if (cmdp->cb.cont(ctx, cmdp, cmd))
843 return RESP_BAD;
844 } else {
845 fprintf(stderr, "IMAP error: unexpected command continuation request\n");
846 return RESP_BAD;
847 }
848 if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
849 return RESP_BAD;
850 if (!cmdp->cb.cont)
851 imap->literal_pending = 0;
852 if (!tcmd)
853 return DRV_OK;
854 } else {
855 tag = atoi(arg);
856 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
857 if (cmdp->tag == tag)
858 goto gottag;
859 fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
860 return RESP_BAD;
861 gottag:
862 if (!(*pcmdp = cmdp->next))
863 imap->in_progress_append = pcmdp;
864 imap->num_in_progress--;
865 if (cmdp->cb.cont || cmdp->cb.data)
866 imap->literal_pending = 0;
867 arg = next_arg(&cmd);
868 if (!strcmp("OK", arg))
869 resp = DRV_OK;
870 else {
871 if (!strcmp("NO", arg)) {
872 if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
873 p = strchr(cmdp->cmd, '"');
874 if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", (int)(strchr(p + 1, '"') - p + 1), p)) {
875 resp = RESP_BAD;
876 goto normal;
877 }
878 /* not waiting here violates the spec, but a server that does not
879 grok this nonetheless violates it too. */
880 cmdp->cb.create = 0;
881 if (!(ncmdp = issue_imap_cmd(ctx, &cmdp->cb, "%s", cmdp->cmd))) {
882 resp = RESP_BAD;
883 goto normal;
884 }
885 free(cmdp->cmd);
886 free(cmdp);
887 if (!tcmd)
888 return 0; /* ignored */
889 if (cmdp == tcmd)
890 tcmd = ncmdp;
891 continue;
892 }
893 resp = RESP_NO;
894 } else /*if (!strcmp("BAD", arg))*/
895 resp = RESP_BAD;
896 fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
897 memcmp(cmdp->cmd, "LOGIN", 5) ?
898 cmdp->cmd : "LOGIN <user> <pass>",
899 arg, cmd ? cmd : "");
900 }
901 if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp)
902 resp = resp2;
903 normal:
904 if (cmdp->cb.done)
905 cmdp->cb.done(ctx, cmdp, resp);
906 free(cmdp->cb.data);
907 free(cmdp->cmd);
908 free(cmdp);
909 if (!tcmd || tcmd == cmdp)
910 return resp;
911 }
912 }
913 /* not reached */
914}
915
916static void imap_close_server(struct imap_store *ictx)
917{
918 struct imap *imap = ictx->imap;
919
920 if (imap->buf.sock.fd[0] != -1) {
921 imap_exec(ictx, NULL, "LOGOUT");
922 socket_shutdown(&imap->buf.sock);
923 }
924 free_list(imap->ns_personal);
925 free_list(imap->ns_other);
926 free_list(imap->ns_shared);
927 free(imap);
928}
929
930static void imap_close_store(struct store *ctx)
931{
932 imap_close_server((struct imap_store *)ctx);
933 free_generic_messages(ctx->msgs);
934 free(ctx);
935}
936
937#ifndef NO_OPENSSL
938
939/*
940 * hexchar() and cram() functions are based on the code from the isync
941 * project (http://isync.sf.net/).
942 */
943static char hexchar(unsigned int b)
944{
945 return b < 10 ? '0' + b : 'a' + (b - 10);
946}
947
948#define ENCODED_SIZE(n) (4*((n+2)/3))
949static char *cram(const char *challenge_64, const char *user, const char *pass)
950{
951 int i, resp_len, encoded_len, decoded_len;
952 HMAC_CTX hmac;
953 unsigned char hash[16];
954 char hex[33];
955 char *response, *response_64, *challenge;
956
957 /*
958 * length of challenge_64 (i.e. base-64 encoded string) is a good
959 * enough upper bound for challenge (decoded result).
960 */
961 encoded_len = strlen(challenge_64);
962 challenge = xmalloc(encoded_len);
963 decoded_len = EVP_DecodeBlock((unsigned char *)challenge,
964 (unsigned char *)challenge_64, encoded_len);
965 if (decoded_len < 0)
966 die("invalid challenge %s", challenge_64);
967 HMAC_Init(&hmac, (unsigned char *)pass, strlen(pass), EVP_md5());
968 HMAC_Update(&hmac, (unsigned char *)challenge, decoded_len);
969 HMAC_Final(&hmac, hash, NULL);
970 HMAC_CTX_cleanup(&hmac);
971
972 hex[32] = 0;
973 for (i = 0; i < 16; i++) {
974 hex[2 * i] = hexchar((hash[i] >> 4) & 0xf);
975 hex[2 * i + 1] = hexchar(hash[i] & 0xf);
976 }
977
978 /* response: "<user> <digest in hex>" */
979 resp_len = strlen(user) + 1 + strlen(hex) + 1;
980 response = xmalloc(resp_len);
981 sprintf(response, "%s %s", user, hex);
982
983 response_64 = xmalloc(ENCODED_SIZE(resp_len) + 1);
984 encoded_len = EVP_EncodeBlock((unsigned char *)response_64,
985 (unsigned char *)response, resp_len);
986 if (encoded_len < 0)
987 die("EVP_EncodeBlock error");
988 response_64[encoded_len] = '\0';
989 return (char *)response_64;
990}
991
992#else
993
994static char *cram(const char *challenge_64, const char *user, const char *pass)
995{
996 die("If you want to use CRAM-MD5 authenticate method, "
997 "you have to build git-imap-send with OpenSSL library.");
998}
999
1000#endif
1001
1002static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const char *prompt)
1003{
1004 int ret;
1005 char *response;
1006
1007 response = cram(prompt, server.user, server.pass);
1008
1009 ret = socket_write(&ctx->imap->buf.sock, response, strlen(response));
1010 if (ret != strlen(response))
1011 return error("IMAP error: sending response failed");
1012
1013 free(response);
1014
1015 return 0;
1016}
1017
1018static struct store *imap_open_store(struct imap_server_conf *srvc)
1019{
1020 struct imap_store *ctx;
1021 struct imap *imap;
1022 char *arg, *rsp;
1023 int s = -1, preauth;
1024
1025 ctx = xcalloc(sizeof(*ctx), 1);
1026
1027 ctx->imap = imap = xcalloc(sizeof(*imap), 1);
1028 imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
1029 imap->in_progress_append = &imap->in_progress;
1030
1031 /* open connection to IMAP server */
1032
1033 if (srvc->tunnel) {
1034 const char *argv[] = { srvc->tunnel, NULL };
1035 struct child_process tunnel = {NULL};
1036
1037 imap_info("Starting tunnel '%s'... ", srvc->tunnel);
1038
1039 tunnel.argv = argv;
1040 tunnel.use_shell = 1;
1041 tunnel.in = -1;
1042 tunnel.out = -1;
1043 if (start_command(&tunnel))
1044 die("cannot start proxy %s", argv[0]);
1045
1046 imap->buf.sock.fd[0] = tunnel.out;
1047 imap->buf.sock.fd[1] = tunnel.in;
1048
1049 imap_info("ok\n");
1050 } else {
1051#ifndef NO_IPV6
1052 struct addrinfo hints, *ai0, *ai;
1053 int gai;
1054 char portstr[6];
1055
1056 snprintf(portstr, sizeof(portstr), "%d", srvc->port);
1057
1058 memset(&hints, 0, sizeof(hints));
1059 hints.ai_socktype = SOCK_STREAM;
1060 hints.ai_protocol = IPPROTO_TCP;
1061
1062 imap_info("Resolving %s... ", srvc->host);
1063 gai = getaddrinfo(srvc->host, portstr, &hints, &ai);
1064 if (gai) {
1065 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai));
1066 goto bail;
1067 }
1068 imap_info("ok\n");
1069
1070 for (ai0 = ai; ai; ai = ai->ai_next) {
1071 char addr[NI_MAXHOST];
1072
1073 s = socket(ai->ai_family, ai->ai_socktype,
1074 ai->ai_protocol);
1075 if (s < 0)
1076 continue;
1077
1078 getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
1079 sizeof(addr), NULL, 0, NI_NUMERICHOST);
1080 imap_info("Connecting to [%s]:%s... ", addr, portstr);
1081
1082 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) {
1083 close(s);
1084 s = -1;
1085 perror("connect");
1086 continue;
1087 }
1088
1089 break;
1090 }
1091 freeaddrinfo(ai0);
1092#else /* NO_IPV6 */
1093 struct hostent *he;
1094 struct sockaddr_in addr;
1095
1096 memset(&addr, 0, sizeof(addr));
1097 addr.sin_port = htons(srvc->port);
1098 addr.sin_family = AF_INET;
1099
1100 imap_info("Resolving %s... ", srvc->host);
1101 he = gethostbyname(srvc->host);
1102 if (!he) {
1103 perror("gethostbyname");
1104 goto bail;
1105 }
1106 imap_info("ok\n");
1107
1108 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
1109
1110 s = socket(PF_INET, SOCK_STREAM, 0);
1111
1112 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1113 if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1114 close(s);
1115 s = -1;
1116 perror("connect");
1117 }
1118#endif
1119 if (s < 0) {
1120 fputs("Error: unable to connect to server.\n", stderr);
1121 goto bail;
1122 }
1123
1124 imap->buf.sock.fd[0] = s;
1125 imap->buf.sock.fd[1] = dup(s);
1126
1127 if (srvc->use_ssl &&
1128 ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1129 close(s);
1130 goto bail;
1131 }
1132 imap_info("ok\n");
1133 }
1134
1135 /* read the greeting string */
1136 if (buffer_gets(&imap->buf, &rsp)) {
1137 fprintf(stderr, "IMAP error: no greeting response\n");
1138 goto bail;
1139 }
1140 arg = next_arg(&rsp);
1141 if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) {
1142 fprintf(stderr, "IMAP error: invalid greeting response\n");
1143 goto bail;
1144 }
1145 preauth = 0;
1146 if (!strcmp("PREAUTH", arg))
1147 preauth = 1;
1148 else if (strcmp("OK", arg) != 0) {
1149 fprintf(stderr, "IMAP error: unknown greeting response\n");
1150 goto bail;
1151 }
1152 parse_response_code(ctx, NULL, rsp);
1153 if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1154 goto bail;
1155
1156 if (!preauth) {
1157#ifndef NO_OPENSSL
1158 if (!srvc->use_ssl && CAP(STARTTLS)) {
1159 if (imap_exec(ctx, NULL, "STARTTLS") != RESP_OK)
1160 goto bail;
1161 if (ssl_socket_connect(&imap->buf.sock, 1,
1162 srvc->ssl_verify))
1163 goto bail;
1164 /* capabilities may have changed, so get the new capabilities */
1165 if (imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1166 goto bail;
1167 }
1168#endif
1169 imap_info("Logging in...\n");
1170 if (!srvc->user) {
1171 fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
1172 goto bail;
1173 }
1174 if (!srvc->pass) {
1175 struct strbuf prompt = STRBUF_INIT;
1176 strbuf_addf(&prompt, "Password (%s@%s): ", srvc->user, srvc->host);
1177 arg = git_getpass(prompt.buf);
1178 strbuf_release(&prompt);
1179 if (!*arg) {
1180 fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
1181 goto bail;
1182 }
1183 /*
1184 * getpass() returns a pointer to a static buffer. make a copy
1185 * for long term storage.
1186 */
1187 srvc->pass = xstrdup(arg);
1188 }
1189 if (CAP(NOLOGIN)) {
1190 fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
1191 goto bail;
1192 }
1193
1194 if (srvc->auth_method) {
1195 struct imap_cmd_cb cb;
1196
1197 if (!strcmp(srvc->auth_method, "CRAM-MD5")) {
1198 if (!CAP(AUTH_CRAM_MD5)) {
1199 fprintf(stderr, "You specified"
1200 "CRAM-MD5 as authentication method, "
1201 "but %s doesn't support it.\n", srvc->host);
1202 goto bail;
1203 }
1204 /* CRAM-MD5 */
1205
1206 memset(&cb, 0, sizeof(cb));
1207 cb.cont = auth_cram_md5;
1208 if (imap_exec(ctx, &cb, "AUTHENTICATE CRAM-MD5") != RESP_OK) {
1209 fprintf(stderr, "IMAP error: AUTHENTICATE CRAM-MD5 failed\n");
1210 goto bail;
1211 }
1212 } else {
1213 fprintf(stderr, "Unknown authentication method:%s\n", srvc->host);
1214 goto bail;
1215 }
1216 } else {
1217 if (!imap->buf.sock.ssl)
1218 imap_warn("*** IMAP Warning *** Password is being "
1219 "sent in the clear\n");
1220 if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) {
1221 fprintf(stderr, "IMAP error: LOGIN failed\n");
1222 goto bail;
1223 }
1224 }
1225 } /* !preauth */
1226
1227 ctx->prefix = "";
1228 ctx->trashnc = 1;
1229 return (struct store *)ctx;
1230
1231bail:
1232 imap_close_store(&ctx->gen);
1233 return NULL;
1234}
1235
1236static void lf_to_crlf(struct strbuf *msg)
1237{
1238 size_t new_len;
1239 char *new;
1240 int i, j, lfnum = 0;
1241
1242 if (msg->buf[0] == '\n')
1243 lfnum++;
1244 for (i = 1; i < msg->len; i++) {
1245 if (msg->buf[i - 1] != '\r' && msg->buf[i] == '\n')
1246 lfnum++;
1247 }
1248
1249 new_len = msg->len + lfnum;
1250 new = xmalloc(new_len + 1);
1251 if (msg->buf[0] == '\n') {
1252 new[0] = '\r';
1253 new[1] = '\n';
1254 i = 1;
1255 j = 2;
1256 } else {
1257 new[0] = msg->buf[0];
1258 i = 1;
1259 j = 1;
1260 }
1261 for ( ; i < msg->len; i++) {
1262 if (msg->buf[i] != '\n') {
1263 new[j++] = msg->buf[i];
1264 continue;
1265 }
1266 if (msg->buf[i - 1] != '\r')
1267 new[j++] = '\r';
1268 /* otherwise it already had CR before */
1269 new[j++] = '\n';
1270 }
1271 strbuf_attach(msg, new, new_len, new_len + 1);
1272}
1273
1274/*
1275 * Store msg to IMAP. Also detach and free the data from msg->data,
1276 * leaving msg->data empty.
1277 */
1278static int imap_store_msg(struct store *gctx, struct strbuf *msg)
1279{
1280 struct imap_store *ctx = (struct imap_store *)gctx;
1281 struct imap *imap = ctx->imap;
1282 struct imap_cmd_cb cb;
1283 const char *prefix, *box;
1284 int ret;
1285
1286 lf_to_crlf(msg);
1287 memset(&cb, 0, sizeof(cb));
1288
1289 cb.dlen = msg->len;
1290 cb.data = strbuf_detach(msg, NULL);
1291
1292 box = gctx->name;
1293 prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1294 cb.create = 0;
1295 ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" ", prefix, box);
1296 imap->caps = imap->rcaps;
1297 if (ret != DRV_OK)
1298 return ret;
1299 gctx->count++;
1300
1301 return DRV_OK;
1302}
1303
1304static void wrap_in_html(struct strbuf *msg)
1305{
1306 struct strbuf buf = STRBUF_INIT;
1307 static char *content_type = "Content-Type: text/html;\n";
1308 static char *pre_open = "<pre>\n";
1309 static char *pre_close = "</pre>\n";
1310 const char *body = strstr(msg->buf, "\n\n");
1311
1312 if (!body)
1313 return; /* Headers but no body; no wrapping needed */
1314
1315 body += 2;
1316
1317 strbuf_add(&buf, msg->buf, body - msg->buf - 1);
1318 strbuf_addstr(&buf, content_type);
1319 strbuf_addch(&buf, '\n');
1320 strbuf_addstr(&buf, pre_open);
1321 strbuf_addstr_xml_quoted(&buf, body);
1322 strbuf_addstr(&buf, pre_close);
1323
1324 strbuf_release(msg);
1325 *msg = buf;
1326}
1327
1328#define CHUNKSIZE 0x1000
1329
1330static int read_message(FILE *f, struct strbuf *all_msgs)
1331{
1332 do {
1333 if (strbuf_fread(all_msgs, CHUNKSIZE, f) <= 0)
1334 break;
1335 } while (!feof(f));
1336
1337 return ferror(f) ? -1 : 0;
1338}
1339
1340static int count_messages(struct strbuf *all_msgs)
1341{
1342 int count = 0;
1343 char *p = all_msgs->buf;
1344
1345 while (1) {
1346 if (!prefixcmp(p, "From ")) {
1347 p = strstr(p+5, "\nFrom: ");
1348 if (!p) break;
1349 p = strstr(p+7, "\nDate: ");
1350 if (!p) break;
1351 p = strstr(p+7, "\nSubject: ");
1352 if (!p) break;
1353 p += 10;
1354 count++;
1355 }
1356 p = strstr(p+5, "\nFrom ");
1357 if (!p)
1358 break;
1359 p++;
1360 }
1361 return count;
1362}
1363
1364/*
1365 * Copy the next message from all_msgs, starting at offset *ofs, to
1366 * msg. Update *ofs to the start of the following message. Return
1367 * true iff a message was successfully copied.
1368 */
1369static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
1370{
1371 char *p, *data;
1372 size_t len;
1373
1374 if (*ofs >= all_msgs->len)
1375 return 0;
1376
1377 data = &all_msgs->buf[*ofs];
1378 len = all_msgs->len - *ofs;
1379
1380 if (len < 5 || prefixcmp(data, "From "))
1381 return 0;
1382
1383 p = strchr(data, '\n');
1384 if (p) {
1385 p++;
1386 len -= p - data;
1387 *ofs += p - data;
1388 data = p;
1389 }
1390
1391 p = strstr(data, "\nFrom ");
1392 if (p)
1393 len = &p[1] - data;
1394
1395 strbuf_add(msg, data, len);
1396 *ofs += len;
1397 return 1;
1398}
1399
1400static char *imap_folder;
1401
1402static int git_imap_config(const char *key, const char *val, void *cb)
1403{
1404 char imap_key[] = "imap.";
1405
1406 if (strncmp(key, imap_key, sizeof imap_key - 1))
1407 return 0;
1408
1409 key += sizeof imap_key - 1;
1410
1411 /* check booleans first, and barf on others */
1412 if (!strcmp("sslverify", key))
1413 server.ssl_verify = git_config_bool(key, val);
1414 else if (!strcmp("preformattedhtml", key))
1415 server.use_html = git_config_bool(key, val);
1416 else if (!val)
1417 return config_error_nonbool(key);
1418
1419 if (!strcmp("folder", key)) {
1420 imap_folder = xstrdup(val);
1421 } else if (!strcmp("host", key)) {
1422 if (!prefixcmp(val, "imap:"))
1423 val += 5;
1424 else if (!prefixcmp(val, "imaps:")) {
1425 val += 6;
1426 server.use_ssl = 1;
1427 }
1428 if (!prefixcmp(val, "//"))
1429 val += 2;
1430 server.host = xstrdup(val);
1431 } else if (!strcmp("user", key))
1432 server.user = xstrdup(val);
1433 else if (!strcmp("pass", key))
1434 server.pass = xstrdup(val);
1435 else if (!strcmp("port", key))
1436 server.port = git_config_int(key, val);
1437 else if (!strcmp("tunnel", key))
1438 server.tunnel = xstrdup(val);
1439 else if (!strcmp("authmethod", key))
1440 server.auth_method = xstrdup(val);
1441
1442 return 0;
1443}
1444
1445int main(int argc, char **argv)
1446{
1447 struct strbuf all_msgs = STRBUF_INIT;
1448 struct strbuf msg = STRBUF_INIT;
1449 struct store *ctx = NULL;
1450 int ofs = 0;
1451 int r;
1452 int total, n = 0;
1453 int nongit_ok;
1454
1455 git_extract_argv0_path(argv[0]);
1456
1457 git_setup_gettext();
1458
1459 if (argc != 1)
1460 usage(imap_send_usage);
1461
1462 setup_git_directory_gently(&nongit_ok);
1463 git_config(git_imap_config, NULL);
1464
1465 if (!server.port)
1466 server.port = server.use_ssl ? 993 : 143;
1467
1468 if (!imap_folder) {
1469 fprintf(stderr, "no imap store specified\n");
1470 return 1;
1471 }
1472 if (!server.host) {
1473 if (!server.tunnel) {
1474 fprintf(stderr, "no imap host specified\n");
1475 return 1;
1476 }
1477 server.host = "tunnel";
1478 }
1479
1480 /* read the messages */
1481 if (read_message(stdin, &all_msgs)) {
1482 fprintf(stderr, "error reading input\n");
1483 return 1;
1484 }
1485
1486 if (all_msgs.len == 0) {
1487 fprintf(stderr, "nothing to send\n");
1488 return 1;
1489 }
1490
1491 total = count_messages(&all_msgs);
1492 if (!total) {
1493 fprintf(stderr, "no messages to send\n");
1494 return 1;
1495 }
1496
1497 /* write it to the imap server */
1498 ctx = imap_open_store(&server);
1499 if (!ctx) {
1500 fprintf(stderr, "failed to open store\n");
1501 return 1;
1502 }
1503
1504 fprintf(stderr, "sending %d message%s\n", total, (total != 1) ? "s" : "");
1505 ctx->name = imap_folder;
1506 while (1) {
1507 unsigned percent = n * 100 / total;
1508
1509 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1510 if (!split_msg(&all_msgs, &msg, &ofs))
1511 break;
1512 if (server.use_html)
1513 wrap_in_html(&msg);
1514 r = imap_store_msg(ctx, &msg);
1515 if (r != DRV_OK)
1516 break;
1517 n++;
1518 }
1519 fprintf(stderr, "\n");
1520
1521 imap_close_store(ctx);
1522
1523 return 0;
1524}