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