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