2 * git-imap-send - drops patches into an imap Drafts folder
3 * derived from isync/mbsync - mailbox synchronizer
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
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.
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.
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
27 typedef struct store_conf {
29 const char *path; /* should this be here? its interpretation is driver-specific */
32 unsigned max_size; /* off_t is overkill */
33 unsigned trash_remote_new:1, trash_only_new:1;
36 typedef struct string_list {
37 struct string_list *next;
41 typedef struct channel_conf {
42 struct channel_conf *next;
44 store_conf_t *master, *slave;
45 char *master_name, *slave_name;
47 string_list_t *patterns;
49 unsigned max_messages; /* for slave only */
52 typedef struct group_conf {
53 struct group_conf *next;
55 string_list_t *channels;
58 /* For message->status */
59 #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
60 #define M_DEAD (1<<1) /* expunged */
61 #define M_FLAGS (1<<2) /* flags fetched */
63 typedef struct message {
65 /* string_list_t *keywords; */
66 size_t size; /* zero implies "not fetched" */
68 unsigned char flags, status;
71 typedef struct store {
72 store_conf_t *conf; /* foreign */
74 /* currently open mailbox */
75 const char *name; /* foreign! maybe preset? */
77 message_t *msgs; /* own */
79 unsigned char opts; /* maybe preset? */
80 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
81 int count; /* # of messages */
82 int recent; /* # of recent messages - don't trust this beyond the initial read */
93 #define DRV_MSG_BAD -1
94 #define DRV_BOX_BAD -2
95 #define DRV_STORE_BAD -3
97 static int Verbose, Quiet;
99 static void imap_info( const char *, ... );
100 static void imap_warn( const char *, ... );
102 static char *next_arg( char ** );
104 static void free_generic_messages( message_t * );
106 static int nfsnprintf( char *buf, int blen, const char *fmt, ... );
108 static int nfvasprintf(char **strp, const char *fmt, va_list ap)
113 len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
115 die("Fatal: Out of memory\n");
116 if (len >= sizeof(tmp))
117 die("imap command overflow !\n");
118 *strp = xmemdupz(tmp, len);
122 static void arc4_init( void );
123 static unsigned char arc4_getbyte( void );
125 typedef struct imap_server_conf {
132 } imap_server_conf_t;
134 typedef struct imap_store_conf {
136 imap_server_conf_t *server;
137 unsigned use_namespace:1;
140 #define NIL (void*)0x1
141 #define LIST (void*)0x2
143 typedef struct _list {
144 struct _list *next, *child;
162 typedef struct imap {
163 int uidnext; /* from SELECT responses */
164 list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
165 unsigned caps, rcaps; /* CAPABILITY results */
167 int nexttag, num_in_progress, literal_pending;
168 struct imap_cmd *in_progress, **in_progress_append;
169 buffer_t buf; /* this is BIG, so put it last */
172 typedef struct imap_store {
177 unsigned /*currentnc:1,*/ trashnc:1;
181 int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
182 void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response);
187 unsigned create:1, trycreate:1;
191 struct imap_cmd *next;
192 struct imap_cmd_cb cb;
197 #define CAP(cap) (imap->caps & (1 << (cap)))
206 static const char *cap_list[] = {
217 static int get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd );
220 static const char *Flags[] = {
229 socket_perror( const char *func, Socket_t *sock, int ret )
234 fprintf( stderr, "%s: unexpected EOF\n", func );
238 socket_read( Socket_t *sock, char *buf, int len )
240 ssize_t n = xread( sock->fd, buf, len );
242 socket_perror( "read", sock, n );
250 socket_write( Socket_t *sock, const char *buf, int len )
252 int n = write_in_full( sock->fd, buf, len );
254 socket_perror( "write", sock, n );
261 /* simple line buffering */
263 buffer_gets( buffer_t * b, char **s )
266 int start = b->offset;
271 /* make sure we have enough data to read the \r\n sequence */
272 if (b->offset + 1 >= b->bytes) {
274 /* shift down used bytes */
277 assert( start <= b->bytes );
278 n = b->bytes - start;
281 memmove(b->buf, b->buf + start, n);
287 n = socket_read( &b->sock, b->buf + b->bytes,
288 sizeof(b->buf) - b->bytes );
296 if (b->buf[b->offset] == '\r') {
297 assert( b->offset + 1 < b->bytes );
298 if (b->buf[b->offset + 1] == '\n') {
299 b->buf[b->offset] = 0; /* terminate the string */
300 b->offset += 2; /* next line */
313 imap_info( const char *msg, ... )
326 imap_warn( const char *msg, ... )
332 vfprintf( stderr, msg, va );
344 while (isspace( (unsigned char) **s ))
353 *s = strchr( *s, '"' );
356 while (**s && !isspace( (unsigned char) **s ))
369 free_generic_messages( message_t *msgs )
373 for (; msgs; msgs = tmsg) {
380 nfsnprintf( char *buf, int blen, const char *fmt, ... )
386 if (blen <= 0 || (unsigned)(ret = vsnprintf( buf, blen, fmt, va )) >= (unsigned)blen)
387 die( "Fatal: buffer too small. Please report a bug.\n");
393 unsigned char i, j, s[256];
400 unsigned char j, si, dat[128];
402 if ((fd = open( "/dev/urandom", O_RDONLY )) < 0 && (fd = open( "/dev/random", O_RDONLY )) < 0) {
403 fprintf( stderr, "Fatal: no random number source available.\n" );
406 if (read_in_full( fd, dat, 128 ) != 128) {
407 fprintf( stderr, "Fatal: cannot read random number source.\n" );
412 for (i = 0; i < 256; i++)
414 for (i = j = 0; i < 256; i++) {
416 j += si + dat[i & 127];
422 for (i = 0; i < 256; i++)
429 unsigned char si, sj;
437 return rs.s[(si + sj) & 0xff];
440 static struct imap_cmd *
441 v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb,
442 const char *fmt, va_list ap )
444 imap_t *imap = ctx->imap;
445 struct imap_cmd *cmd;
449 cmd = xmalloc( sizeof(struct imap_cmd) );
450 nfvasprintf( &cmd->cmd, fmt, ap );
451 cmd->tag = ++imap->nexttag;
456 memset( &cmd->cb, 0, sizeof(cmd->cb) );
458 while (imap->literal_pending)
459 get_cmd_result( ctx, NULL );
461 bufl = nfsnprintf( buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
462 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
463 cmd->tag, cmd->cmd, cmd->cb.dlen );
465 if (imap->num_in_progress)
466 printf( "(%d in progress) ", imap->num_in_progress );
467 if (memcmp( cmd->cmd, "LOGIN", 5 ))
468 printf( ">>> %s", buf );
470 printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
472 if (socket_write( &imap->buf.sock, buf, bufl ) != bufl) {
480 if (CAP(LITERALPLUS)) {
481 n = socket_write( &imap->buf.sock, cmd->cb.data, cmd->cb.dlen );
482 free( cmd->cb.data );
483 if (n != cmd->cb.dlen ||
484 (n = socket_write( &imap->buf.sock, "\r\n", 2 )) != 2)
492 imap->literal_pending = 1;
493 } else if (cmd->cb.cont)
494 imap->literal_pending = 1;
496 *imap->in_progress_append = cmd;
497 imap->in_progress_append = &cmd->next;
498 imap->num_in_progress++;
502 static struct imap_cmd *
503 issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
505 struct imap_cmd *ret;
509 ret = v_issue_imap_cmd( ctx, cb, fmt, ap );
515 imap_exec( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
518 struct imap_cmd *cmdp;
521 cmdp = v_issue_imap_cmd( ctx, cb, fmt, ap );
526 return get_cmd_result( ctx, cmdp );
530 imap_exec_m( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
533 struct imap_cmd *cmdp;
536 cmdp = v_issue_imap_cmd( ctx, cb, fmt, ap );
539 return DRV_STORE_BAD;
541 switch (get_cmd_result( ctx, cmdp )) {
542 case RESP_BAD: return DRV_STORE_BAD;
543 case RESP_NO: return DRV_MSG_BAD;
544 default: return DRV_OK;
549 is_atom( list_t *list )
551 return list && list->val && list->val != NIL && list->val != LIST;
555 is_list( list_t *list )
557 return list && list->val == LIST;
561 free_list( list_t *list )
565 for (; list; list = tmp) {
568 free_list( list->child );
569 else if (is_atom( list ))
576 parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )
583 while (isspace( (unsigned char)*s ))
585 if (level && *s == ')') {
589 *curp = cur = xmalloc( sizeof(*cur) );
591 cur->val = NULL; /* for clean bail */
596 if (parse_imap_list_l( imap, &s, &cur->child, level + 1 ))
598 } else if (imap && *s == '{') {
600 bytes = cur->len = strtol( s + 1, &s, 10 );
604 s = cur->val = xmalloc( cur->len );
606 /* dump whats left over in the input buffer */
607 n = imap->buf.bytes - imap->buf.offset;
610 /* the entire message fit in the buffer */
613 memcpy( s, imap->buf.buf + imap->buf.offset, n );
617 /* mark that we used part of the buffer */
618 imap->buf.offset += n;
620 /* now read the rest of the message */
622 if ((n = socket_read (&imap->buf.sock, s, bytes)) <= 0)
628 if (buffer_gets( &imap->buf, &s ))
630 } else if (*s == '"') {
634 for (; *s != '"'; s++)
639 cur->val = xmemdupz(p, cur->len);
643 for (; *s && !isspace( (unsigned char)*s ); s++)
644 if (level && *s == ')')
647 if (cur->len == 3 && !memcmp ("NIL", p, 3)) {
650 cur->val = xmemdupz(p, cur->len);
669 parse_imap_list( imap_t *imap, char **sp )
673 if (!parse_imap_list_l( imap, sp, &head, 0 ))
680 parse_list( char **sp )
682 return parse_imap_list( NULL, sp );
686 parse_capability( imap_t *imap, char *cmd )
691 imap->caps = 0x80000000;
692 while ((arg = next_arg( &cmd )))
693 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
694 if (!strcmp( cap_list[i], arg ))
695 imap->caps |= 1 << i;
696 imap->rcaps = imap->caps;
700 parse_response_code( imap_store_t *ctx, struct imap_cmd_cb *cb, char *s )
702 imap_t *imap = ctx->imap;
706 return RESP_OK; /* no response code */
708 if (!(p = strchr( s, ']' ))) {
709 fprintf( stderr, "IMAP error: malformed response code\n" );
713 arg = next_arg( &s );
714 if (!strcmp( "UIDVALIDITY", arg )) {
715 if (!(arg = next_arg( &s )) || !(ctx->gen.uidvalidity = atoi( arg ))) {
716 fprintf( stderr, "IMAP error: malformed UIDVALIDITY status\n" );
719 } else if (!strcmp( "UIDNEXT", arg )) {
720 if (!(arg = next_arg( &s )) || !(imap->uidnext = atoi( arg ))) {
721 fprintf( stderr, "IMAP error: malformed NEXTUID status\n" );
724 } else if (!strcmp( "CAPABILITY", arg )) {
725 parse_capability( imap, s );
726 } else if (!strcmp( "ALERT", arg )) {
727 /* RFC2060 says that these messages MUST be displayed
730 for (; isspace( (unsigned char)*p ); p++);
731 fprintf( stderr, "*** IMAP ALERT *** %s\n", p );
732 } else if (cb && cb->ctx && !strcmp( "APPENDUID", arg )) {
733 if (!(arg = next_arg( &s )) || !(ctx->gen.uidvalidity = atoi( arg )) ||
734 !(arg = next_arg( &s )) || !(*(int *)cb->ctx = atoi( arg )))
736 fprintf( stderr, "IMAP error: malformed APPENDUID status\n" );
744 get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
746 imap_t *imap = ctx->imap;
747 struct imap_cmd *cmdp, **pcmdp, *ncmdp;
748 char *cmd, *arg, *arg1, *p;
749 int n, resp, resp2, tag;
752 if (buffer_gets( &imap->buf, &cmd ))
755 arg = next_arg( &cmd );
757 arg = next_arg( &cmd );
759 fprintf( stderr, "IMAP error: unable to parse untagged response\n" );
763 if (!strcmp( "NAMESPACE", arg )) {
764 imap->ns_personal = parse_list( &cmd );
765 imap->ns_other = parse_list( &cmd );
766 imap->ns_shared = parse_list( &cmd );
767 } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) ||
768 !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
769 if ((resp = parse_response_code( ctx, NULL, cmd )) != RESP_OK)
771 } else if (!strcmp( "CAPABILITY", arg ))
772 parse_capability( imap, cmd );
773 else if ((arg1 = next_arg( &cmd ))) {
774 if (!strcmp( "EXISTS", arg1 ))
775 ctx->gen.count = atoi( arg );
776 else if (!strcmp( "RECENT", arg1 ))
777 ctx->gen.recent = atoi( arg );
779 fprintf( stderr, "IMAP error: unable to parse untagged response\n" );
782 } else if (!imap->in_progress) {
783 fprintf( stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
785 } else if (*arg == '+') {
786 /* This can happen only with the last command underway, as
787 it enforces a round-trip. */
788 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
789 offsetof(struct imap_cmd, next));
791 n = socket_write( &imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen );
792 free( cmdp->cb.data );
793 cmdp->cb.data = NULL;
794 if (n != (int)cmdp->cb.dlen)
796 } else if (cmdp->cb.cont) {
797 if (cmdp->cb.cont( ctx, cmdp, cmd ))
800 fprintf( stderr, "IMAP error: unexpected command continuation request\n" );
803 if (socket_write( &imap->buf.sock, "\r\n", 2 ) != 2)
806 imap->literal_pending = 0;
811 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
812 if (cmdp->tag == tag)
814 fprintf( stderr, "IMAP error: unexpected tag %s\n", arg );
817 if (!(*pcmdp = cmdp->next))
818 imap->in_progress_append = pcmdp;
819 imap->num_in_progress--;
820 if (cmdp->cb.cont || cmdp->cb.data)
821 imap->literal_pending = 0;
822 arg = next_arg( &cmd );
823 if (!strcmp( "OK", arg ))
826 if (!strcmp( "NO", arg )) {
827 if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
828 p = strchr( cmdp->cmd, '"' );
829 if (!issue_imap_cmd( ctx, NULL, "CREATE \"%.*s\"", strchr( p + 1, '"' ) - p + 1, p )) {
833 /* not waiting here violates the spec, but a server that does not
834 grok this nonetheless violates it too. */
836 if (!(ncmdp = issue_imap_cmd( ctx, &cmdp->cb, "%s", cmdp->cmd ))) {
843 return 0; /* ignored */
849 } else /*if (!strcmp( "BAD", arg ))*/
851 fprintf( stderr, "IMAP command '%s' returned response (%s) - %s\n",
852 memcmp (cmdp->cmd, "LOGIN", 5) ?
853 cmdp->cmd : "LOGIN <user> <pass>",
854 arg, cmd ? cmd : "");
856 if ((resp2 = parse_response_code( ctx, &cmdp->cb, cmd )) > resp)
860 cmdp->cb.done( ctx, cmdp, resp );
861 free( cmdp->cb.data );
864 if (!tcmd || tcmd == cmdp)
872 imap_close_server( imap_store_t *ictx )
874 imap_t *imap = ictx->imap;
876 if (imap->buf.sock.fd != -1) {
877 imap_exec( ictx, NULL, "LOGOUT" );
878 close( imap->buf.sock.fd );
880 free_list( imap->ns_personal );
881 free_list( imap->ns_other );
882 free_list( imap->ns_shared );
887 imap_close_store( store_t *ctx )
889 imap_close_server( (imap_store_t *)ctx );
890 free_generic_messages( ctx->msgs );
895 imap_open_store( imap_server_conf_t *srvc )
901 struct sockaddr_in addr;
902 int s, a[2], preauth;
905 ctx = xcalloc( sizeof(*ctx), 1 );
907 ctx->imap = imap = xcalloc( sizeof(*imap), 1 );
908 imap->buf.sock.fd = -1;
909 imap->in_progress_append = &imap->in_progress;
911 /* open connection to IMAP server */
914 imap_info( "Starting tunnel '%s'... ", srvc->tunnel );
916 if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
917 perror( "socketpair" );
925 if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
929 execl( "/bin/sh", "sh", "-c", srvc->tunnel, NULL );
935 imap->buf.sock.fd = a[1];
939 memset( &addr, 0, sizeof(addr) );
940 addr.sin_port = htons( srvc->port );
941 addr.sin_family = AF_INET;
943 imap_info( "Resolving %s... ", srvc->host );
944 he = gethostbyname( srvc->host );
946 perror( "gethostbyname" );
951 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
953 s = socket( PF_INET, SOCK_STREAM, 0 );
955 imap_info( "Connecting to %s:%hu... ", inet_ntoa( addr.sin_addr ), ntohs( addr.sin_port ) );
956 if (connect( s, (struct sockaddr *)&addr, sizeof(addr) )) {
963 imap->buf.sock.fd = s;
967 /* read the greeting string */
968 if (buffer_gets( &imap->buf, &rsp )) {
969 fprintf( stderr, "IMAP error: no greeting response\n" );
972 arg = next_arg( &rsp );
973 if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
974 fprintf( stderr, "IMAP error: invalid greeting response\n" );
978 if (!strcmp( "PREAUTH", arg ))
980 else if (strcmp( "OK", arg ) != 0) {
981 fprintf( stderr, "IMAP error: unknown greeting response\n" );
984 parse_response_code( ctx, NULL, rsp );
985 if (!imap->caps && imap_exec( ctx, NULL, "CAPABILITY" ) != RESP_OK)
990 imap_info ("Logging in...\n");
992 fprintf( stderr, "Skipping server %s, no user\n", srvc->host );
997 sprintf( prompt, "Password (%s@%s): ", srvc->user, srvc->host );
998 arg = getpass( prompt );
1000 perror( "getpass" );
1004 fprintf( stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host );
1008 * getpass() returns a pointer to a static buffer. make a copy
1009 * for long term storage.
1011 srvc->pass = xstrdup( arg );
1014 fprintf( stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host );
1017 imap_warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
1018 if (imap_exec( ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
1019 fprintf( stderr, "IMAP error: LOGIN failed\n" );
1026 return (store_t *)ctx;
1029 imap_close_store( &ctx->gen );
1034 imap_make_flags( int flags, char *buf )
1039 for (i = d = 0; i < ARRAY_SIZE(Flags); i++)
1040 if (flags & (1 << i)) {
1043 for (s = Flags[i]; *s; s++)
1054 imap_store_msg( store_t *gctx, msg_data_t *data, int *uid )
1056 imap_store_t *ctx = (imap_store_t *)gctx;
1057 imap_t *imap = ctx->imap;
1058 struct imap_cmd_cb cb;
1060 const char *prefix, *box;
1061 int ret, i, j, d, len, extra, nocr;
1062 int start, sbreak = 0, ebreak = 0;
1063 char flagstr[128], tuid[TUIDL * 2 + 1];
1065 memset( &cb, 0, sizeof(cb) );
1071 if (!CAP(UIDPLUS) && uid) {
1075 if (fmap[i++] == '\n') {
1077 if (i - 2 + nocr == start) {
1078 sbreak = ebreak = i - 2 + nocr;
1081 if (!memcmp( fmap + start, "X-TUID: ", 8 )) {
1082 extra -= (ebreak = i) - (sbreak = start) + nocr;
1087 /* invalid message */
1091 for (j = 0; j < TUIDL; j++)
1092 sprintf( tuid + j * 2, "%02x", arc4_getbyte() );
1093 extra += 8 + TUIDL * 2 + 2;
1096 for (; i < len; i++)
1097 if (fmap[i] == '\n')
1100 cb.dlen = len + extra;
1101 buf = cb.data = xmalloc( cb.dlen );
1103 if (!CAP(UIDPLUS) && uid) {
1105 for (; i < sbreak; i++)
1106 if (fmap[i] == '\n') {
1112 memcpy( buf, fmap, sbreak );
1115 memcpy( buf, "X-TUID: ", 8 );
1117 memcpy( buf, tuid, TUIDL * 2 );
1124 for (; i < len; i++)
1125 if (fmap[i] == '\n') {
1131 memcpy( buf, fmap + i, len - i );
1137 d = imap_make_flags( data->flags, flagstr );
1143 box = gctx->conf->trash;
1144 prefix = ctx->prefix;
1147 imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
1150 prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
1154 ret = imap_exec_m( ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr );
1155 imap->caps = imap->rcaps;
1166 #define CHUNKSIZE 0x1000
1169 read_message( FILE *f, msg_data_t *msg )
1173 memset(msg, 0, sizeof(*msg));
1174 strbuf_init(&buf, 0);
1177 if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
1182 msg->data = strbuf_detach(&buf, NULL);
1187 count_messages( msg_data_t *msg )
1190 char *p = msg->data;
1193 if (!prefixcmp(p, "From ")) {
1197 p = strstr( p+5, "\nFrom ");
1206 split_msg( msg_data_t *all_msgs, msg_data_t *msg, int *ofs )
1210 memset( msg, 0, sizeof *msg );
1211 if (*ofs >= all_msgs->len)
1214 data = &all_msgs->data[ *ofs ];
1215 msg->len = all_msgs->len - *ofs;
1217 if (msg->len < 5 || prefixcmp(data, "From "))
1220 p = strchr( data, '\n' );
1228 p = strstr( data, "\nFrom " );
1230 msg->len = &p[1] - data;
1232 msg->data = xmemdupz(data, msg->len);
1237 static imap_server_conf_t server =
1247 static char *imap_folder;
1250 git_imap_config(const char *key, const char *val, void *cb)
1252 char imap_key[] = "imap.";
1254 if (strncmp( key, imap_key, sizeof imap_key - 1 ))
1258 return config_error_nonbool(key);
1260 key += sizeof imap_key - 1;
1262 if (!strcmp( "folder", key )) {
1263 imap_folder = xstrdup( val );
1264 } else if (!strcmp( "host", key )) {
1266 if (!prefixcmp(val, "imap:"))
1271 if (!prefixcmp(val, "//"))
1273 server.host = xstrdup( val );
1275 else if (!strcmp( "user", key ))
1276 server.user = xstrdup( val );
1277 else if (!strcmp( "pass", key ))
1278 server.pass = xstrdup( val );
1279 else if (!strcmp( "port", key ))
1280 server.port = git_config_int( key, val );
1281 else if (!strcmp( "tunnel", key ))
1282 server.tunnel = xstrdup( val );
1287 main(int argc, char **argv)
1289 msg_data_t all_msgs, msg;
1290 store_t *ctx = NULL;
1296 /* init the random number generator */
1299 git_config(git_imap_config, NULL);
1302 fprintf( stderr, "no imap store specified\n" );
1306 if (!server.tunnel) {
1307 fprintf( stderr, "no imap host specified\n" );
1310 server.host = "tunnel";
1313 /* read the messages */
1314 if (!read_message( stdin, &all_msgs )) {
1315 fprintf(stderr,"nothing to send\n");
1319 total = count_messages( &all_msgs );
1321 fprintf(stderr,"no messages to send\n");
1325 /* write it to the imap server */
1326 ctx = imap_open_store( &server );
1328 fprintf( stderr,"failed to open store\n");
1332 fprintf( stderr, "sending %d message%s\n", total, (total!=1)?"s":"" );
1333 ctx->name = imap_folder;
1335 unsigned percent = n * 100 / total;
1336 fprintf( stderr, "%4u%% (%d/%d) done\r", percent, n, total );
1337 if (!split_msg( &all_msgs, &msg, &ofs ))
1339 r = imap_store_msg( ctx, &msg, &uid );
1340 if (r != DRV_OK) break;
1343 fprintf( stderr,"\n" );
1345 imap_close_store( ctx );