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