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, ... );
109 static void arc4_init( void );
110 static unsigned char arc4_getbyte( void );
112 typedef struct imap_server_conf {
119 } imap_server_conf_t;
121 typedef struct imap_store_conf {
123 imap_server_conf_t *server;
124 unsigned use_namespace:1;
127 #define NIL (void*)0x1
128 #define LIST (void*)0x2
130 typedef struct _list {
131 struct _list *next, *child;
149 typedef struct imap {
150 int uidnext; /* from SELECT responses */
151 list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
152 unsigned caps, rcaps; /* CAPABILITY results */
154 int nexttag, num_in_progress, literal_pending;
155 struct imap_cmd *in_progress, **in_progress_append;
156 buffer_t buf; /* this is BIG, so put it last */
159 typedef struct imap_store {
164 unsigned /*currentnc:1,*/ trashnc:1;
168 int (*cont)( imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt );
169 void (*done)( imap_store_t *ctx, struct imap_cmd *cmd, int response);
174 unsigned create:1, trycreate:1;
178 struct imap_cmd *next;
179 struct imap_cmd_cb cb;
184 #define CAP(cap) (imap->caps & (1 << (cap)))
193 static const char *cap_list[] = {
204 static int get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd );
207 static const char *Flags[] = {
216 socket_perror( const char *func, Socket_t *sock, int ret )
221 fprintf( stderr, "%s: unexpected EOF\n", func );
225 socket_read( Socket_t *sock, char *buf, int len )
227 ssize_t n = xread( sock->fd, buf, len );
229 socket_perror( "read", sock, n );
237 socket_write( Socket_t *sock, const char *buf, int len )
239 int n = write_in_full( sock->fd, buf, len );
241 socket_perror( "write", sock, n );
248 /* simple line buffering */
250 buffer_gets( buffer_t * b, char **s )
253 int start = b->offset;
258 /* make sure we have enough data to read the \r\n sequence */
259 if (b->offset + 1 >= b->bytes) {
261 /* shift down used bytes */
264 assert( start <= b->bytes );
265 n = b->bytes - start;
268 memmove(b->buf, b->buf + start, n);
274 n = socket_read( &b->sock, b->buf + b->bytes,
275 sizeof(b->buf) - b->bytes );
283 if (b->buf[b->offset] == '\r') {
284 assert( b->offset + 1 < b->bytes );
285 if (b->buf[b->offset + 1] == '\n') {
286 b->buf[b->offset] = 0; /* terminate the string */
287 b->offset += 2; /* next line */
300 imap_info( const char *msg, ... )
313 imap_warn( const char *msg, ... )
319 vfprintf( stderr, msg, va );
331 while (isspace( (unsigned char) **s ))
340 *s = strchr( *s, '"' );
343 while (**s && !isspace( (unsigned char) **s ))
356 free_generic_messages( message_t *msgs )
360 for (; msgs; msgs = tmsg) {
367 nfsnprintf( char *buf, int blen, const char *fmt, ... )
373 if (blen <= 0 || (unsigned)(ret = vsnprintf( buf, blen, fmt, va )) >= (unsigned)blen)
374 die( "Fatal: buffer too small. Please report a bug.\n");
380 unsigned char i, j, s[256];
387 unsigned char j, si, dat[128];
389 if ((fd = open( "/dev/urandom", O_RDONLY )) < 0 && (fd = open( "/dev/random", O_RDONLY )) < 0) {
390 fprintf( stderr, "Fatal: no random number source available.\n" );
393 if (read_in_full( fd, dat, 128 ) != 128) {
394 fprintf( stderr, "Fatal: cannot read random number source.\n" );
399 for (i = 0; i < 256; i++)
401 for (i = j = 0; i < 256; i++) {
403 j += si + dat[i & 127];
409 for (i = 0; i < 256; i++)
416 unsigned char si, sj;
424 return rs.s[(si + sj) & 0xff];
427 static struct imap_cmd *
428 v_issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb,
429 const char *fmt, va_list ap )
431 imap_t *imap = ctx->imap;
432 struct imap_cmd *cmd;
436 cmd = xmalloc( sizeof(struct imap_cmd) );
437 nfvasprintf( &cmd->cmd, fmt, ap );
438 cmd->tag = ++imap->nexttag;
443 memset( &cmd->cb, 0, sizeof(cmd->cb) );
445 while (imap->literal_pending)
446 get_cmd_result( ctx, NULL );
448 bufl = nfsnprintf( buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
449 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
450 cmd->tag, cmd->cmd, cmd->cb.dlen );
452 if (imap->num_in_progress)
453 printf( "(%d in progress) ", imap->num_in_progress );
454 if (memcmp( cmd->cmd, "LOGIN", 5 ))
455 printf( ">>> %s", buf );
457 printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
459 if (socket_write( &imap->buf.sock, buf, bufl ) != bufl) {
467 if (CAP(LITERALPLUS)) {
468 n = socket_write( &imap->buf.sock, cmd->cb.data, cmd->cb.dlen );
469 free( cmd->cb.data );
470 if (n != cmd->cb.dlen ||
471 (n = socket_write( &imap->buf.sock, "\r\n", 2 )) != 2)
479 imap->literal_pending = 1;
480 } else if (cmd->cb.cont)
481 imap->literal_pending = 1;
483 *imap->in_progress_append = cmd;
484 imap->in_progress_append = &cmd->next;
485 imap->num_in_progress++;
489 static struct imap_cmd *
490 issue_imap_cmd( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
492 struct imap_cmd *ret;
496 ret = v_issue_imap_cmd( ctx, cb, fmt, ap );
502 imap_exec( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
505 struct imap_cmd *cmdp;
508 cmdp = v_issue_imap_cmd( ctx, cb, fmt, ap );
513 return get_cmd_result( ctx, cmdp );
517 imap_exec_m( imap_store_t *ctx, struct imap_cmd_cb *cb, const char *fmt, ... )
520 struct imap_cmd *cmdp;
523 cmdp = v_issue_imap_cmd( ctx, cb, fmt, ap );
526 return DRV_STORE_BAD;
528 switch (get_cmd_result( ctx, cmdp )) {
529 case RESP_BAD: return DRV_STORE_BAD;
530 case RESP_NO: return DRV_MSG_BAD;
531 default: return DRV_OK;
536 is_atom( list_t *list )
538 return list && list->val && list->val != NIL && list->val != LIST;
542 is_list( list_t *list )
544 return list && list->val == LIST;
548 free_list( list_t *list )
552 for (; list; list = tmp) {
555 free_list( list->child );
556 else if (is_atom( list ))
563 parse_imap_list_l( imap_t *imap, char **sp, list_t **curp, int level )
570 while (isspace( (unsigned char)*s ))
572 if (level && *s == ')') {
576 *curp = cur = xmalloc( sizeof(*cur) );
578 cur->val = NULL; /* for clean bail */
583 if (parse_imap_list_l( imap, &s, &cur->child, level + 1 ))
585 } else if (imap && *s == '{') {
587 bytes = cur->len = strtol( s + 1, &s, 10 );
591 s = cur->val = xmalloc( cur->len );
593 /* dump whats left over in the input buffer */
594 n = imap->buf.bytes - imap->buf.offset;
597 /* the entire message fit in the buffer */
600 memcpy( s, imap->buf.buf + imap->buf.offset, n );
604 /* mark that we used part of the buffer */
605 imap->buf.offset += n;
607 /* now read the rest of the message */
609 if ((n = socket_read (&imap->buf.sock, s, bytes)) <= 0)
615 if (buffer_gets( &imap->buf, &s ))
617 } else if (*s == '"') {
621 for (; *s != '"'; s++)
626 cur->val = xmemdupz(p, cur->len);
630 for (; *s && !isspace( (unsigned char)*s ); s++)
631 if (level && *s == ')')
634 if (cur->len == 3 && !memcmp ("NIL", p, 3)) {
637 cur->val = xmemdupz(p, cur->len);
656 parse_imap_list( imap_t *imap, char **sp )
660 if (!parse_imap_list_l( imap, sp, &head, 0 ))
667 parse_list( char **sp )
669 return parse_imap_list( NULL, sp );
673 parse_capability( imap_t *imap, char *cmd )
678 imap->caps = 0x80000000;
679 while ((arg = next_arg( &cmd )))
680 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
681 if (!strcmp( cap_list[i], arg ))
682 imap->caps |= 1 << i;
683 imap->rcaps = imap->caps;
687 parse_response_code( imap_store_t *ctx, struct imap_cmd_cb *cb, char *s )
689 imap_t *imap = ctx->imap;
693 return RESP_OK; /* no response code */
695 if (!(p = strchr( s, ']' ))) {
696 fprintf( stderr, "IMAP error: malformed response code\n" );
700 arg = next_arg( &s );
701 if (!strcmp( "UIDVALIDITY", arg )) {
702 if (!(arg = next_arg( &s )) || !(ctx->gen.uidvalidity = atoi( arg ))) {
703 fprintf( stderr, "IMAP error: malformed UIDVALIDITY status\n" );
706 } else if (!strcmp( "UIDNEXT", arg )) {
707 if (!(arg = next_arg( &s )) || !(imap->uidnext = atoi( arg ))) {
708 fprintf( stderr, "IMAP error: malformed NEXTUID status\n" );
711 } else if (!strcmp( "CAPABILITY", arg )) {
712 parse_capability( imap, s );
713 } else if (!strcmp( "ALERT", arg )) {
714 /* RFC2060 says that these messages MUST be displayed
717 for (; isspace( (unsigned char)*p ); p++);
718 fprintf( stderr, "*** IMAP ALERT *** %s\n", p );
719 } else if (cb && cb->ctx && !strcmp( "APPENDUID", arg )) {
720 if (!(arg = next_arg( &s )) || !(ctx->gen.uidvalidity = atoi( arg )) ||
721 !(arg = next_arg( &s )) || !(*(int *)cb->ctx = atoi( arg )))
723 fprintf( stderr, "IMAP error: malformed APPENDUID status\n" );
731 get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
733 imap_t *imap = ctx->imap;
734 struct imap_cmd *cmdp, **pcmdp, *ncmdp;
735 char *cmd, *arg, *arg1, *p;
736 int n, resp, resp2, tag;
739 if (buffer_gets( &imap->buf, &cmd ))
742 arg = next_arg( &cmd );
744 arg = next_arg( &cmd );
746 fprintf( stderr, "IMAP error: unable to parse untagged response\n" );
750 if (!strcmp( "NAMESPACE", arg )) {
751 imap->ns_personal = parse_list( &cmd );
752 imap->ns_other = parse_list( &cmd );
753 imap->ns_shared = parse_list( &cmd );
754 } else if (!strcmp( "OK", arg ) || !strcmp( "BAD", arg ) ||
755 !strcmp( "NO", arg ) || !strcmp( "BYE", arg )) {
756 if ((resp = parse_response_code( ctx, NULL, cmd )) != RESP_OK)
758 } else if (!strcmp( "CAPABILITY", arg ))
759 parse_capability( imap, cmd );
760 else if ((arg1 = next_arg( &cmd ))) {
761 if (!strcmp( "EXISTS", arg1 ))
762 ctx->gen.count = atoi( arg );
763 else if (!strcmp( "RECENT", arg1 ))
764 ctx->gen.recent = atoi( arg );
766 fprintf( stderr, "IMAP error: unable to parse untagged response\n" );
769 } else if (!imap->in_progress) {
770 fprintf( stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "" );
772 } else if (*arg == '+') {
773 /* This can happen only with the last command underway, as
774 it enforces a round-trip. */
775 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
776 offsetof(struct imap_cmd, next));
778 n = socket_write( &imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen );
779 free( cmdp->cb.data );
780 cmdp->cb.data = NULL;
781 if (n != (int)cmdp->cb.dlen)
783 } else if (cmdp->cb.cont) {
784 if (cmdp->cb.cont( ctx, cmdp, cmd ))
787 fprintf( stderr, "IMAP error: unexpected command continuation request\n" );
790 if (socket_write( &imap->buf.sock, "\r\n", 2 ) != 2)
793 imap->literal_pending = 0;
798 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
799 if (cmdp->tag == tag)
801 fprintf( stderr, "IMAP error: unexpected tag %s\n", arg );
804 if (!(*pcmdp = cmdp->next))
805 imap->in_progress_append = pcmdp;
806 imap->num_in_progress--;
807 if (cmdp->cb.cont || cmdp->cb.data)
808 imap->literal_pending = 0;
809 arg = next_arg( &cmd );
810 if (!strcmp( "OK", arg ))
813 if (!strcmp( "NO", arg )) {
814 if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
815 p = strchr( cmdp->cmd, '"' );
816 if (!issue_imap_cmd( ctx, NULL, "CREATE \"%.*s\"", strchr( p + 1, '"' ) - p + 1, p )) {
820 /* not waiting here violates the spec, but a server that does not
821 grok this nonetheless violates it too. */
823 if (!(ncmdp = issue_imap_cmd( ctx, &cmdp->cb, "%s", cmdp->cmd ))) {
830 return 0; /* ignored */
836 } else /*if (!strcmp( "BAD", arg ))*/
838 fprintf( stderr, "IMAP command '%s' returned response (%s) - %s\n",
839 memcmp (cmdp->cmd, "LOGIN", 5) ?
840 cmdp->cmd : "LOGIN <user> <pass>",
841 arg, cmd ? cmd : "");
843 if ((resp2 = parse_response_code( ctx, &cmdp->cb, cmd )) > resp)
847 cmdp->cb.done( ctx, cmdp, resp );
849 free( cmdp->cb.data );
852 if (!tcmd || tcmd == cmdp)
860 imap_close_server( imap_store_t *ictx )
862 imap_t *imap = ictx->imap;
864 if (imap->buf.sock.fd != -1) {
865 imap_exec( ictx, NULL, "LOGOUT" );
866 close( imap->buf.sock.fd );
868 free_list( imap->ns_personal );
869 free_list( imap->ns_other );
870 free_list( imap->ns_shared );
875 imap_close_store( store_t *ctx )
877 imap_close_server( (imap_store_t *)ctx );
878 free_generic_messages( ctx->msgs );
883 imap_open_store( imap_server_conf_t *srvc )
889 struct sockaddr_in addr;
890 int s, a[2], preauth;
893 ctx = xcalloc( sizeof(*ctx), 1 );
895 ctx->imap = imap = xcalloc( sizeof(*imap), 1 );
896 imap->buf.sock.fd = -1;
897 imap->in_progress_append = &imap->in_progress;
899 /* open connection to IMAP server */
902 imap_info( "Starting tunnel '%s'... ", srvc->tunnel );
904 if (socketpair( PF_UNIX, SOCK_STREAM, 0, a )) {
905 perror( "socketpair" );
913 if (dup2( a[0], 0 ) == -1 || dup2( a[0], 1 ) == -1)
917 execl( "/bin/sh", "sh", "-c", srvc->tunnel, NULL );
923 imap->buf.sock.fd = a[1];
927 memset( &addr, 0, sizeof(addr) );
928 addr.sin_port = htons( srvc->port );
929 addr.sin_family = AF_INET;
931 imap_info( "Resolving %s... ", srvc->host );
932 he = gethostbyname( srvc->host );
934 perror( "gethostbyname" );
939 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
941 s = socket( PF_INET, SOCK_STREAM, 0 );
943 imap_info( "Connecting to %s:%hu... ", inet_ntoa( addr.sin_addr ), ntohs( addr.sin_port ) );
944 if (connect( s, (struct sockaddr *)&addr, sizeof(addr) )) {
951 imap->buf.sock.fd = s;
955 /* read the greeting string */
956 if (buffer_gets( &imap->buf, &rsp )) {
957 fprintf( stderr, "IMAP error: no greeting response\n" );
960 arg = next_arg( &rsp );
961 if (!arg || *arg != '*' || (arg = next_arg( &rsp )) == NULL) {
962 fprintf( stderr, "IMAP error: invalid greeting response\n" );
966 if (!strcmp( "PREAUTH", arg ))
968 else if (strcmp( "OK", arg ) != 0) {
969 fprintf( stderr, "IMAP error: unknown greeting response\n" );
972 parse_response_code( ctx, NULL, rsp );
973 if (!imap->caps && imap_exec( ctx, NULL, "CAPABILITY" ) != RESP_OK)
978 imap_info ("Logging in...\n");
980 fprintf( stderr, "Skipping server %s, no user\n", srvc->host );
985 sprintf( prompt, "Password (%s@%s): ", srvc->user, srvc->host );
986 arg = getpass( prompt );
992 fprintf( stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host );
996 * getpass() returns a pointer to a static buffer. make a copy
997 * for long term storage.
999 srvc->pass = xstrdup( arg );
1002 fprintf( stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host );
1005 imap_warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
1006 if (imap_exec( ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass ) != RESP_OK) {
1007 fprintf( stderr, "IMAP error: LOGIN failed\n" );
1014 return (store_t *)ctx;
1017 imap_close_store( &ctx->gen );
1022 imap_make_flags( int flags, char *buf )
1027 for (i = d = 0; i < ARRAY_SIZE(Flags); i++)
1028 if (flags & (1 << i)) {
1031 for (s = Flags[i]; *s; s++)
1042 imap_store_msg( store_t *gctx, msg_data_t *data, int *uid )
1044 imap_store_t *ctx = (imap_store_t *)gctx;
1045 imap_t *imap = ctx->imap;
1046 struct imap_cmd_cb cb;
1048 const char *prefix, *box;
1049 int ret, i, j, d, len, extra, nocr;
1050 int start, sbreak = 0, ebreak = 0;
1051 char flagstr[128], tuid[TUIDL * 2 + 1];
1053 memset( &cb, 0, sizeof(cb) );
1059 if (!CAP(UIDPLUS) && uid) {
1063 if (fmap[i++] == '\n') {
1065 if (i - 2 + nocr == start) {
1066 sbreak = ebreak = i - 2 + nocr;
1069 if (!memcmp( fmap + start, "X-TUID: ", 8 )) {
1070 extra -= (ebreak = i) - (sbreak = start) + nocr;
1075 /* invalid message */
1079 for (j = 0; j < TUIDL; j++)
1080 sprintf( tuid + j * 2, "%02x", arc4_getbyte() );
1081 extra += 8 + TUIDL * 2 + 2;
1084 for (; i < len; i++)
1085 if (fmap[i] == '\n')
1088 cb.dlen = len + extra;
1089 buf = cb.data = xmalloc( cb.dlen );
1091 if (!CAP(UIDPLUS) && uid) {
1093 for (; i < sbreak; i++)
1094 if (fmap[i] == '\n') {
1100 memcpy( buf, fmap, sbreak );
1103 memcpy( buf, "X-TUID: ", 8 );
1105 memcpy( buf, tuid, TUIDL * 2 );
1112 for (; i < len; i++)
1113 if (fmap[i] == '\n') {
1119 memcpy( buf, fmap + i, len - i );
1125 d = imap_make_flags( data->flags, flagstr );
1131 box = gctx->conf->trash;
1132 prefix = ctx->prefix;
1135 imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
1138 prefix = !strcmp( box, "INBOX" ) ? "" : ctx->prefix;
1142 ret = imap_exec_m( ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr );
1143 imap->caps = imap->rcaps;
1154 #define CHUNKSIZE 0x1000
1157 read_message( FILE *f, msg_data_t *msg )
1161 memset(msg, 0, sizeof(*msg));
1162 strbuf_init(&buf, 0);
1165 if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
1170 msg->data = strbuf_detach(&buf);
1175 count_messages( msg_data_t *msg )
1178 char *p = msg->data;
1181 if (!prefixcmp(p, "From ")) {
1185 p = strstr( p+5, "\nFrom ");
1194 split_msg( msg_data_t *all_msgs, msg_data_t *msg, int *ofs )
1198 memset( msg, 0, sizeof *msg );
1199 if (*ofs >= all_msgs->len)
1202 data = &all_msgs->data[ *ofs ];
1203 msg->len = all_msgs->len - *ofs;
1205 if (msg->len < 5 || prefixcmp(data, "From "))
1208 p = strchr( data, '\n' );
1216 p = strstr( data, "\nFrom " );
1218 msg->len = &p[1] - data;
1220 msg->data = xmemdupz(data, msg->len);
1225 static imap_server_conf_t server =
1235 static char *imap_folder;
1238 git_imap_config(const char *key, const char *val)
1240 char imap_key[] = "imap.";
1242 if (strncmp( key, imap_key, sizeof imap_key - 1 ))
1244 key += sizeof imap_key - 1;
1246 if (!strcmp( "folder", key )) {
1247 imap_folder = xstrdup( val );
1248 } else if (!strcmp( "host", key )) {
1250 if (!prefixcmp(val, "imap:"))
1255 if (!prefixcmp(val, "//"))
1257 server.host = xstrdup( val );
1259 else if (!strcmp( "user", key ))
1260 server.user = xstrdup( val );
1261 else if (!strcmp( "pass", key ))
1262 server.pass = xstrdup( val );
1263 else if (!strcmp( "port", key ))
1264 server.port = git_config_int( key, val );
1265 else if (!strcmp( "tunnel", key ))
1266 server.tunnel = xstrdup( val );
1271 main(int argc, char **argv)
1273 msg_data_t all_msgs, msg;
1274 store_t *ctx = NULL;
1280 /* init the random number generator */
1283 git_config( git_imap_config );
1286 fprintf( stderr, "no imap store specified\n" );
1290 /* read the messages */
1291 if (!read_message( stdin, &all_msgs )) {
1292 fprintf(stderr,"nothing to send\n");
1296 total = count_messages( &all_msgs );
1298 fprintf(stderr,"no messages to send\n");
1302 /* write it to the imap server */
1303 ctx = imap_open_store( &server );
1305 fprintf( stderr,"failed to open store\n");
1309 fprintf( stderr, "sending %d message%s\n", total, (total!=1)?"s":"" );
1310 ctx->name = imap_folder;
1312 unsigned percent = n * 100 / total;
1313 fprintf( stderr, "%4u%% (%d/%d) done\r", percent, n, total );
1314 if (!split_msg( &all_msgs, &msg, &ofs ))
1316 r = imap_store_msg( ctx, &msg, &uid );
1317 if (r != DRV_OK) break;
1320 fprintf( stderr,"\n" );
1322 imap_close_store( ctx );