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