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