git-tar-tree: no more void pointer arithmetic
[git.git] / imap-send.c
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
34 typedef 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
43 typedef struct string_list {
44         struct string_list *next;
45         char string[1];
46 } string_list_t;
47
48 typedef 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
59 typedef 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
70 typedef 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
78 typedef 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
92 typedef 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
104 static int Verbose, Quiet;
105
106 static void info( const char *, ... );
107 static void warn( const char *, ... );
108
109 static char *next_arg( char ** );
110
111 static void free_generic_messages( message_t * );
112
113 static int nfvasprintf( char **str, const char *fmt, va_list va );
114 static int nfsnprintf( char *buf, int blen, const char *fmt, ... );
115
116
117 static void arc4_init( void );
118 static unsigned char arc4_getbyte( void );
119
120 typedef 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
129 typedef 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
138 typedef struct _list {
139         struct _list *next, *child;
140         char *val;
141         int len;
142 } list_t;
143
144 typedef struct {
145         int fd;
146 } Socket_t;
147
148 typedef struct {
149         Socket_t sock;
150         int bytes;
151         int offset;
152         char buf[1024];
153 } buffer_t;
154
155 struct imap_cmd;
156
157 typedef 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
167 typedef 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
175 struct 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
185 struct 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
194 enum CAPABILITY {
195         NOLOGIN = 0,
196         UIDPLUS,
197         LITERALPLUS,
198         NAMESPACE,
199 };
200
201 static 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
212 static int get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd );
213
214
215 static const char *Flags[] = {
216         "Draft",
217         "Flagged",
218         "Answered",
219         "Seen",
220         "Deleted",
221 };
222
223 static void
224 socket_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
232 static int
233 socket_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
244 static int
245 socket_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 */
257 static int
258 buffer_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
307 static void
308 info( 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
320 static void
321 warn( 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
332 static char *
333 next_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
363 static void
364 free_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
374 static int
375 git_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
389 static int
390 nfsnprintf( 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
402 static int
403 nfvasprintf( 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
411 static struct {
412         unsigned char i, j, s[256];
413 } rs;
414
415 static void
416 arc4_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
445 static unsigned char
446 arc4_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
459 static struct imap_cmd *
460 v_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
521 static struct imap_cmd *
522 issue_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
533 static int
534 imap_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
548 static int
549 imap_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
567 static int
568 is_atom( list_t *list )
569 {
570         return list && list->val && list->val != NIL && list->val != LIST;
571 }
572
573 static int
574 is_list( list_t *list )
575 {
576         return list && list->val == LIST;
577 }
578
579 static void
580 free_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
594 static int
595 parse_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
691 static list_t *
692 parse_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
702 static list_t *
703 parse_list( char **sp )
704 {
705         return parse_imap_list( NULL, sp );
706 }
707
708 static void
709 parse_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
722 static int
723 parse_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
766 static int
767 get_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
895 static void
896 imap_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
910 static void
911 imap_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
918 static store_t *
919 imap_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         pid_t pid;
928
929         ctx = xcalloc( sizeof(*ctx), 1 );
930
931         ctx->imap = imap = xcalloc( sizeof(*imap), 1 );
932         imap->buf.sock.fd = -1;
933         imap->in_progress_append = &imap->in_progress;
934
935         /* open connection to IMAP server */
936
937         if (srvc->tunnel) {
938                 info( "Starting tunnel '%s'... ", srvc->tunnel );
939
940                 if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
941                         perror( "socketpair" );
942                         exit( 1 );
943                 }
944
945                 pid = fork();
946                 if (pid < 0)
947                         _exit( 127 );
948                 if (!pid) {
949                         if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
950                                 _exit( 127 );
951                         close( a[0] );
952                         close( a[1] );
953                         execl( "/bin/sh", "sh", "-c", srvc->tunnel, NULL );
954                         _exit( 127 );
955                 }
956
957                 close (a[0]);
958
959                 imap->buf.sock.fd = a[1];
960
961                 info( "ok\n" );
962         } else {
963                 memset( &addr, 0, sizeof(addr) );
964                 addr.sin_port = htons( srvc->port );
965                 addr.sin_family = AF_INET;
966
967                 info( "Resolving %s... ", srvc->host );
968                 he = gethostbyname( srvc->host );
969                 if (!he) {
970                         perror( "gethostbyname" );
971                         goto bail;
972                 }
973                 info( "ok\n" );
974
975                 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
976
977                 s = socket( PF_INET, SOCK_STREAM, 0 );
978
979                 info( "Connecting to %s:%hu... ", inet_ntoa( addr.sin_addr ), ntohs( addr.sin_port ) );
980                 if (connect( s, (struct sockaddr *)&addr, sizeof(addr) )) {
981                         close( s );
982                         perror( "connect" );
983                         goto bail;
984                 }
985                 info( "ok\n" );
986
987                 imap->buf.sock.fd = s;
988
989         }
990
991         /* read the greeting string */
992         if (buffer_gets( &imap->buf, &rsp )) {
993                 fprintf( stderr, "IMAP error: no greeting response\n" );
994                 goto bail;
995         }
996         arg = next_arg( &rsp );
997         if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
998                 fprintf( stderr, "IMAP error: invalid greeting response\n" );
999                 goto bail;
1000         }
1001         preauth = 0;
1002         if (!strcmp( "PREAUTH", arg ))
1003                 preauth = 1;
1004         else if (strcmp( "OK", arg ) != 0) {
1005                 fprintf( stderr, "IMAP error: unknown greeting response\n" );
1006                 goto bail;
1007         }
1008         parse_response_code( ctx, NULL, rsp );
1009         if (!imap->caps && imap_exec( ctx, NULL, "CAPABILITY" ) != RESP_OK)
1010                 goto bail;
1011
1012         if (!preauth) {
1013
1014                 info ("Logging in...\n");
1015                 if (!srvc->user) {
1016                         fprintf( stderr, "Skipping server %s, no user\n", srvc->host );
1017                         goto bail;
1018                 }
1019                 if (!srvc->pass) {
1020                         char prompt[80];
1021                         sprintf( prompt, "Password (%s@%s): ", srvc->user, srvc->host );
1022                         arg = getpass( prompt );
1023                         if (!arg) {
1024                                 perror( "getpass" );
1025                                 exit( 1 );
1026                         }
1027                         if (!*arg) {
1028                                 fprintf( stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host );
1029                                 goto bail;
1030                         }
1031                         /*
1032                          * getpass() returns a pointer to a static buffer.  make a copy
1033                          * for long term storage.
1034                          */
1035                         srvc->pass = strdup( arg );
1036                 }
1037                 if (CAP(NOLOGIN)) {
1038                         fprintf( stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host );
1039                         goto bail;
1040                 }
1041                 warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
1042                 if (imap_exec( ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
1043                         fprintf( stderr, "IMAP error: LOGIN failed\n" );
1044                         goto bail;
1045                 }
1046         } /* !preauth */
1047
1048         ctx->prefix = "";
1049         ctx->trashnc = 1;
1050         return (store_t *)ctx;
1051
1052   bail:
1053         imap_close_store( &ctx->gen );
1054         return NULL;
1055 }
1056
1057 static int
1058 imap_make_flags( int flags, char *buf )
1059 {
1060         const char *s;
1061         unsigned i, d;
1062
1063         for (i = d = 0; i < ARRAY_SIZE(Flags); i++)
1064                 if (flags & (1 << i)) {
1065                         buf[d++] = ' ';
1066                         buf[d++] = '\\';
1067                         for (s = Flags[i]; *s; s++)
1068                                 buf[d++] = *s;
1069                 }
1070         buf[0] = '(';
1071         buf[d++] = ')';
1072         return d;
1073 }
1074
1075 #define TUIDL 8
1076
1077 static int
1078 imap_store_msg( store_t *gctx, msg_data_t *data, int *uid )
1079 {
1080         imap_store_t *ctx = (imap_store_t *)gctx;
1081         imap_t *imap = ctx->imap;
1082         struct imap_cmd_cb cb;
1083         char *fmap, *buf;
1084         const char *prefix, *box;
1085         int ret, i, j, d, len, extra, nocr;
1086         int start, sbreak = 0, ebreak = 0;
1087         char flagstr[128], tuid[TUIDL * 2 + 1];
1088
1089         memset( &cb, 0, sizeof(cb) );
1090
1091         fmap = data->data;
1092         len = data->len;
1093         nocr = !data->crlf;
1094         extra = 0, i = 0;
1095         if (!CAP(UIDPLUS) && uid) {
1096           nloop:
1097                 start = i;
1098                 while (i < len)
1099                         if (fmap[i++] == '\n') {
1100                                 extra += nocr;
1101                                 if (i - 2 + nocr == start) {
1102                                         sbreak = ebreak = i - 2 + nocr;
1103                                         goto mktid;
1104                                 }
1105                                 if (!memcmp( fmap + start, "X-TUID: ", 8 )) {
1106                                         extra -= (ebreak = i) - (sbreak = start) + nocr;
1107                                         goto mktid;
1108                                 }
1109                                 goto nloop;
1110                         }
1111                 /* invalid message */
1112                 free( fmap );
1113                 return DRV_MSG_BAD;
1114          mktid:
1115                 for (j = 0; j < TUIDL; j++)
1116                         sprintf( tuid + j * 2, "%02x", arc4_getbyte() );
1117                 extra += 8 + TUIDL * 2 + 2;
1118         }
1119         if (nocr)
1120                 for (; i < len; i++)
1121                         if (fmap[i] == '\n')
1122                                 extra++;
1123
1124         cb.dlen = len + extra;
1125         buf = cb.data = xmalloc( cb.dlen );
1126         i = 0;
1127         if (!CAP(UIDPLUS) && uid) {
1128                 if (nocr) {
1129                         for (; i < sbreak; i++)
1130                                 if (fmap[i] == '\n') {
1131                                         *buf++ = '\r';
1132                                         *buf++ = '\n';
1133                                 } else
1134                                         *buf++ = fmap[i];
1135                 } else {
1136                         memcpy( buf, fmap, sbreak );
1137                         buf += sbreak;
1138                 }
1139                 memcpy( buf, "X-TUID: ", 8 );
1140                 buf += 8;
1141                 memcpy( buf, tuid, TUIDL * 2 );
1142                 buf += TUIDL * 2;
1143                 *buf++ = '\r';
1144                 *buf++ = '\n';
1145                 i = ebreak;
1146         }
1147         if (nocr) {
1148                 for (; i < len; i++)
1149                         if (fmap[i] == '\n') {
1150                                 *buf++ = '\r';
1151                                 *buf++ = '\n';
1152                         } else
1153                                 *buf++ = fmap[i];
1154         } else
1155                 memcpy( buf, fmap + i, len - i );
1156
1157         free( fmap );
1158
1159         d = 0;
1160         if (data->flags) {
1161                 d = imap_make_flags( data->flags, flagstr );
1162                 flagstr[d++] = ' ';
1163         }
1164         flagstr[d] = 0;
1165
1166         if (!uid) {
1167                 box = gctx->conf->trash;
1168                 prefix = ctx->prefix;
1169                 cb.create = 1;
1170                 if (ctx->trashnc)
1171                         imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
1172         } else {
1173                 box = gctx->name;
1174                 prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
1175                 cb.create = 0;
1176         }
1177         cb.ctx = uid;
1178         ret = imap_exec_m( ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr );
1179         imap->caps = imap->rcaps;
1180         if (ret != DRV_OK)
1181                 return ret;
1182         if (!uid)
1183                 ctx->trashnc = 0;
1184         else
1185                 gctx->count++;
1186
1187         return DRV_OK;
1188 }
1189
1190 #define CHUNKSIZE 0x1000
1191
1192 static int
1193 read_message( FILE *f, msg_data_t *msg )
1194 {
1195         int len, r;
1196
1197         memset( msg, 0, sizeof *msg );
1198         len = CHUNKSIZE;
1199         msg->data = xmalloc( len+1 );
1200         msg->data[0] = 0;
1201
1202         while(!feof( f )) {
1203                 if (msg->len >= len) {
1204                         void *p;
1205                         len += CHUNKSIZE;
1206                         p = xrealloc(msg->data, len+1);
1207                         if (!p)
1208                                 break;
1209                         msg->data = p;
1210                 }
1211                 r = fread( &msg->data[msg->len], 1, len - msg->len, f );
1212                 if (r <= 0)
1213                         break;
1214                 msg->len += r;
1215         }
1216         msg->data[msg->len] = 0;
1217         return msg->len;
1218 }
1219
1220 static int
1221 count_messages( msg_data_t *msg )
1222 {
1223         int count = 0;
1224         char *p = msg->data;
1225
1226         while (1) {
1227                 if (!strncmp( "From ", p, 5 )) {
1228                         count++;
1229                         p += 5;
1230                 }
1231                 p = strstr( p+5, "\nFrom ");
1232                 if (!p)
1233                         break;
1234                 p++;
1235         }
1236         return count;
1237 }
1238
1239 static int
1240 split_msg( msg_data_t *all_msgs, msg_data_t *msg, int *ofs )
1241 {
1242         char *p, *data;
1243
1244         memset( msg, 0, sizeof *msg );
1245         if (*ofs >= all_msgs->len)
1246                 return 0;
1247
1248         data = &all_msgs->data[ *ofs ];
1249         msg->len = all_msgs->len - *ofs;
1250
1251         if (msg->len < 5 || strncmp( data, "From ", 5 ))
1252                 return 0;
1253
1254         p = strstr( data, "\nFrom " );
1255         if (p)
1256                 msg->len = &p[1] - data;
1257
1258         msg->data = xmalloc( msg->len + 1 );
1259         if (!msg->data)
1260                 return 0;
1261
1262         memcpy( msg->data, data, msg->len );
1263         msg->data[ msg->len ] = 0;
1264
1265         *ofs += msg->len;
1266         return 1;
1267 }
1268
1269 static imap_server_conf_t server =
1270 {
1271         NULL,   /* name */
1272         NULL,   /* tunnel */
1273         NULL,   /* host */
1274         0,      /* port */
1275         NULL,   /* user */
1276         NULL,   /* pass */
1277 };
1278
1279 static char *imap_folder;
1280
1281 static int
1282 git_imap_config(const char *key, const char *val)
1283 {
1284         char imap_key[] = "imap.";
1285
1286         if (strncmp( key, imap_key, sizeof imap_key - 1 ))
1287                 return 0;
1288         key += sizeof imap_key - 1;
1289
1290         if (!strcmp( "folder", key )) {
1291                 imap_folder = strdup( val );
1292         } else if (!strcmp( "host", key )) {
1293                 {
1294                         if (!strncmp( "imap:", val, 5 ))
1295                                 val += 5;
1296                         if (!server.port)
1297                                 server.port = 143;
1298                 }
1299                 if (!strncmp( "//", val, 2 ))
1300                         val += 2;
1301                 server.host = strdup( val );
1302         }
1303         else if (!strcmp( "user", key ))
1304                 server.user = strdup( val );
1305         else if (!strcmp( "pass", key ))
1306                 server.pass = strdup( val );
1307         else if (!strcmp( "port", key ))
1308                 server.port = git_config_int( key, val );
1309         else if (!strcmp( "tunnel", key ))
1310                 server.tunnel = strdup( val );
1311         return 0;
1312 }
1313
1314 int
1315 main(int argc, char **argv)
1316 {
1317         msg_data_t all_msgs, msg;
1318         store_t *ctx = NULL;
1319         int uid = 0;
1320         int ofs = 0;
1321         int r;
1322         int total, n = 0;
1323
1324         /* init the random number generator */
1325         arc4_init();
1326
1327         git_config( git_imap_config );
1328
1329         if (!imap_folder) {
1330                 fprintf( stderr, "no imap store specified\n" );
1331                 return 1;
1332         }
1333
1334         /* read the messages */
1335         if (!read_message( stdin, &all_msgs )) {
1336                 fprintf(stderr,"nothing to send\n");
1337                 return 1;
1338         }
1339
1340         total = count_messages( &all_msgs );
1341         if (!total) {
1342                 fprintf(stderr,"no messages to send\n");
1343                 return 1;
1344         }
1345
1346         /* write it to the imap server */
1347         ctx = imap_open_store( &server );
1348         if (!ctx) {
1349                 fprintf( stderr,"failed to open store\n");
1350                 return 1;
1351         }
1352
1353         fprintf( stderr, "sending %d message%s\n", total, (total!=1)?"s":"" );
1354         ctx->name = imap_folder;
1355         while (1) {
1356                 unsigned percent = n * 100 / total;
1357                 fprintf( stderr, "%4u%% (%d/%d) done\r", percent, n, total );
1358                 if (!split_msg( &all_msgs, &msg, &ofs ))
1359                         break;
1360                 r = imap_store_msg( ctx, &msg, &uid );
1361                 if (r != DRV_OK) break;
1362                 n++;
1363         }
1364         fprintf( stderr,"\n" );
1365
1366         imap_close_store( ctx );
1367
1368         return 0;
1369 }