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
30 typedef struct store_conf {
32 const char *path; /* should this be here? its interpretation is driver-specific */
35 unsigned max_size; /* off_t is overkill */
36 unsigned trash_remote_new:1, trash_only_new:1;
39 typedef struct string_list {
40 struct string_list *next;
44 typedef struct channel_conf {
45 struct channel_conf *next;
47 store_conf_t *master, *slave;
48 char *master_name, *slave_name;
50 string_list_t *patterns;
52 unsigned max_messages; /* for slave only */
55 typedef struct group_conf {
56 struct group_conf *next;
58 string_list_t *channels;
61 /* For message->status */
62 #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
63 #define M_DEAD (1<<1) /* expunged */
64 #define M_FLAGS (1<<2) /* flags fetched */
66 typedef struct message {
68 /* string_list_t *keywords; */
69 size_t size; /* zero implies "not fetched" */
71 unsigned char flags, status;
74 typedef struct store {
75 store_conf_t *conf; /* foreign */
77 /* currently open mailbox */
78 const char *name; /* foreign! maybe preset? */
80 message_t *msgs; /* own */
82 unsigned char opts; /* maybe preset? */
83 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
84 int count; /* # of messages */
85 int recent; /* # of recent messages - don't trust this beyond the initial read */
96 #define DRV_MSG_BAD -1
97 #define DRV_BOX_BAD -2
98 #define DRV_STORE_BAD -3
100 static int Verbose, Quiet;
102 static void imap_info(const char *, ...);
103 static void imap_warn(const char *, ...);
105 static char *next_arg(char **);
107 static void free_generic_messages(message_t *);
109 static int nfsnprintf(char *buf, int blen, const char *fmt, ...);
111 static int nfvasprintf(char **strp, const char *fmt, va_list ap)
116 len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
118 die("Fatal: Out of memory\n");
119 if (len >= sizeof(tmp))
120 die("imap command overflow !\n");
121 *strp = xmemdupz(tmp, len);
125 static void arc4_init(void);
126 static unsigned char arc4_getbyte(void);
128 typedef struct imap_server_conf {
137 } imap_server_conf_t;
139 typedef struct imap_store_conf {
141 imap_server_conf_t *server;
142 unsigned use_namespace:1;
145 #define NIL (void*)0x1
146 #define LIST (void*)0x2
148 typedef struct _list {
149 struct _list *next, *child;
168 typedef struct imap {
169 int uidnext; /* from SELECT responses */
170 list_t *ns_personal, *ns_other, *ns_shared; /* NAMESPACE info */
171 unsigned caps, rcaps; /* CAPABILITY results */
173 int nexttag, num_in_progress, literal_pending;
174 struct imap_cmd *in_progress, **in_progress_append;
175 buffer_t buf; /* this is BIG, so put it last */
178 typedef struct imap_store {
183 unsigned /*currentnc:1,*/ trashnc:1;
187 int (*cont)(imap_store_t *ctx, struct imap_cmd *cmd, const char *prompt);
188 void (*done)(imap_store_t *ctx, struct imap_cmd *cmd, int response);
193 unsigned create:1, trycreate:1;
197 struct imap_cmd *next;
198 struct imap_cmd_cb cb;
203 #define CAP(cap) (imap->caps & (1 << (cap)))
213 static const char *cap_list[] = {
225 static int get_cmd_result(imap_store_t *ctx, struct imap_cmd *tcmd);
228 static const char *Flags[] = {
237 static void ssl_socket_perror(const char *func)
239 fprintf(stderr, "%s: %s\n", func, ERR_error_string(ERR_get_error(), 0));
243 static void socket_perror(const char *func, Socket_t *sock, int ret)
247 int sslerr = SSL_get_error(sock->ssl, ret);
251 case SSL_ERROR_SYSCALL:
252 perror("SSL_connect");
255 ssl_socket_perror("SSL_connect");
264 fprintf(stderr, "%s: unexpected EOF\n", func);
268 static int ssl_socket_connect(Socket_t *sock, int use_tls_only, int verify)
271 fprintf(stderr, "SSL requested but SSL support not compiled in\n");
279 SSL_load_error_strings();
282 meth = TLSv1_method();
284 meth = SSLv23_method();
287 ssl_socket_perror("SSLv23_method");
291 ctx = SSL_CTX_new(meth);
294 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
296 if (!SSL_CTX_set_default_verify_paths(ctx)) {
297 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
300 sock->ssl = SSL_new(ctx);
302 ssl_socket_perror("SSL_new");
305 if (!SSL_set_fd(sock->ssl, sock->fd)) {
306 ssl_socket_perror("SSL_set_fd");
310 ret = SSL_connect(sock->ssl);
312 socket_perror("SSL_connect", sock, ret);
320 static int socket_read(Socket_t *sock, char *buf, int len)
325 n = SSL_read(sock->ssl, buf, len);
328 n = xread(sock->fd, buf, len);
330 socket_perror("read", sock, n);
337 static int socket_write(Socket_t *sock, const char *buf, int len)
342 n = SSL_write(sock->ssl, buf, len);
345 n = write_in_full(sock->fd, buf, len);
347 socket_perror("write", sock, n);
354 static void socket_shutdown(Socket_t *sock)
358 SSL_shutdown(sock->ssl);
365 /* simple line buffering */
366 static int buffer_gets(buffer_t * b, char **s)
369 int start = b->offset;
374 /* make sure we have enough data to read the \r\n sequence */
375 if (b->offset + 1 >= b->bytes) {
377 /* shift down used bytes */
380 assert(start <= b->bytes);
381 n = b->bytes - start;
384 memmove(b->buf, b->buf + start, n);
390 n = socket_read(&b->sock, b->buf + b->bytes,
391 sizeof(b->buf) - b->bytes);
399 if (b->buf[b->offset] == '\r') {
400 assert(b->offset + 1 < b->bytes);
401 if (b->buf[b->offset + 1] == '\n') {
402 b->buf[b->offset] = 0; /* terminate the string */
403 b->offset += 2; /* next line */
415 static void imap_info(const char *msg, ...)
427 static void imap_warn(const char *msg, ...)
433 vfprintf(stderr, msg, va);
438 static char *next_arg(char **s)
444 while (isspace((unsigned char) **s))
453 *s = strchr(*s, '"');
456 while (**s && !isspace((unsigned char) **s))
468 static void free_generic_messages(message_t *msgs)
472 for (; msgs; msgs = tmsg) {
478 static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
484 if (blen <= 0 || (unsigned)(ret = vsnprintf(buf, blen, fmt, va)) >= (unsigned)blen)
485 die("Fatal: buffer too small. Please report a bug.\n");
491 unsigned char i, j, s[256];
494 static void arc4_init(void)
497 unsigned char j, si, dat[128];
499 if ((fd = open("/dev/urandom", O_RDONLY)) < 0 && (fd = open("/dev/random", O_RDONLY)) < 0) {
500 fprintf(stderr, "Fatal: no random number source available.\n");
503 if (read_in_full(fd, dat, 128) != 128) {
504 fprintf(stderr, "Fatal: cannot read random number source.\n");
509 for (i = 0; i < 256; i++)
511 for (i = j = 0; i < 256; i++) {
513 j += si + dat[i & 127];
519 for (i = 0; i < 256; i++)
523 static unsigned char arc4_getbyte(void)
525 unsigned char si, sj;
533 return rs.s[(si + sj) & 0xff];
536 static struct imap_cmd *v_issue_imap_cmd(imap_store_t *ctx,
537 struct imap_cmd_cb *cb,
538 const char *fmt, va_list ap)
540 imap_t *imap = ctx->imap;
541 struct imap_cmd *cmd;
545 cmd = xmalloc(sizeof(struct imap_cmd));
546 nfvasprintf(&cmd->cmd, fmt, ap);
547 cmd->tag = ++imap->nexttag;
552 memset(&cmd->cb, 0, sizeof(cmd->cb));
554 while (imap->literal_pending)
555 get_cmd_result(ctx, NULL);
557 bufl = nfsnprintf(buf, sizeof(buf), cmd->cb.data ? CAP(LITERALPLUS) ?
558 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
559 cmd->tag, cmd->cmd, cmd->cb.dlen);
561 if (imap->num_in_progress)
562 printf("(%d in progress) ", imap->num_in_progress);
563 if (memcmp(cmd->cmd, "LOGIN", 5))
564 printf(">>> %s", buf);
566 printf(">>> %d LOGIN <user> <pass>\n", cmd->tag);
568 if (socket_write(&imap->buf.sock, buf, bufl) != bufl) {
576 if (CAP(LITERALPLUS)) {
577 n = socket_write(&imap->buf.sock, cmd->cb.data, cmd->cb.dlen);
579 if (n != cmd->cb.dlen ||
580 (n = socket_write(&imap->buf.sock, "\r\n", 2)) != 2)
588 imap->literal_pending = 1;
589 } else if (cmd->cb.cont)
590 imap->literal_pending = 1;
592 *imap->in_progress_append = cmd;
593 imap->in_progress_append = &cmd->next;
594 imap->num_in_progress++;
598 static struct imap_cmd *issue_imap_cmd(imap_store_t *ctx,
599 struct imap_cmd_cb *cb,
600 const char *fmt, ...)
602 struct imap_cmd *ret;
606 ret = v_issue_imap_cmd(ctx, cb, fmt, ap);
611 static int imap_exec(imap_store_t *ctx, struct imap_cmd_cb *cb,
612 const char *fmt, ...)
615 struct imap_cmd *cmdp;
618 cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
623 return get_cmd_result(ctx, cmdp);
626 static int imap_exec_m(imap_store_t *ctx, struct imap_cmd_cb *cb,
627 const char *fmt, ...)
630 struct imap_cmd *cmdp;
633 cmdp = v_issue_imap_cmd(ctx, cb, fmt, ap);
636 return DRV_STORE_BAD;
638 switch (get_cmd_result(ctx, cmdp)) {
639 case RESP_BAD: return DRV_STORE_BAD;
640 case RESP_NO: return DRV_MSG_BAD;
641 default: return DRV_OK;
645 static int is_atom(list_t *list)
647 return list && list->val && list->val != NIL && list->val != LIST;
650 static int is_list(list_t *list)
652 return list && list->val == LIST;
655 static void free_list(list_t *list)
659 for (; list; list = tmp) {
662 free_list(list->child);
663 else if (is_atom(list))
669 static int parse_imap_list_l(imap_t *imap, char **sp, list_t **curp, int level)
676 while (isspace((unsigned char)*s))
678 if (level && *s == ')') {
682 *curp = cur = xmalloc(sizeof(*cur));
684 cur->val = NULL; /* for clean bail */
689 if (parse_imap_list_l(imap, &s, &cur->child, level + 1))
691 } else if (imap && *s == '{') {
693 bytes = cur->len = strtol(s + 1, &s, 10);
697 s = cur->val = xmalloc(cur->len);
699 /* dump whats left over in the input buffer */
700 n = imap->buf.bytes - imap->buf.offset;
703 /* the entire message fit in the buffer */
706 memcpy(s, imap->buf.buf + imap->buf.offset, n);
710 /* mark that we used part of the buffer */
711 imap->buf.offset += n;
713 /* now read the rest of the message */
715 if ((n = socket_read(&imap->buf.sock, s, bytes)) <= 0)
721 if (buffer_gets(&imap->buf, &s))
723 } else if (*s == '"') {
727 for (; *s != '"'; s++)
732 cur->val = xmemdupz(p, cur->len);
736 for (; *s && !isspace((unsigned char)*s); s++)
737 if (level && *s == ')')
740 if (cur->len == 3 && !memcmp("NIL", p, 3)) {
743 cur->val = xmemdupz(p, cur->len);
761 static list_t *parse_imap_list(imap_t *imap, char **sp)
765 if (!parse_imap_list_l(imap, sp, &head, 0))
771 static list_t *parse_list(char **sp)
773 return parse_imap_list(NULL, sp);
776 static void parse_capability(imap_t *imap, char *cmd)
781 imap->caps = 0x80000000;
782 while ((arg = next_arg(&cmd)))
783 for (i = 0; i < ARRAY_SIZE(cap_list); i++)
784 if (!strcmp(cap_list[i], arg))
785 imap->caps |= 1 << i;
786 imap->rcaps = imap->caps;
789 static int parse_response_code(imap_store_t *ctx, struct imap_cmd_cb *cb,
792 imap_t *imap = ctx->imap;
796 return RESP_OK; /* no response code */
798 if (!(p = strchr(s, ']'))) {
799 fprintf(stderr, "IMAP error: malformed response code\n");
804 if (!strcmp("UIDVALIDITY", arg)) {
805 if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg))) {
806 fprintf(stderr, "IMAP error: malformed UIDVALIDITY status\n");
809 } else if (!strcmp("UIDNEXT", arg)) {
810 if (!(arg = next_arg(&s)) || !(imap->uidnext = atoi(arg))) {
811 fprintf(stderr, "IMAP error: malformed NEXTUID status\n");
814 } else if (!strcmp("CAPABILITY", arg)) {
815 parse_capability(imap, s);
816 } else if (!strcmp("ALERT", arg)) {
817 /* RFC2060 says that these messages MUST be displayed
820 for (; isspace((unsigned char)*p); p++);
821 fprintf(stderr, "*** IMAP ALERT *** %s\n", p);
822 } else if (cb && cb->ctx && !strcmp("APPENDUID", arg)) {
823 if (!(arg = next_arg(&s)) || !(ctx->gen.uidvalidity = atoi(arg)) ||
824 !(arg = next_arg(&s)) || !(*(int *)cb->ctx = atoi(arg)))
826 fprintf(stderr, "IMAP error: malformed APPENDUID status\n");
833 static int get_cmd_result(imap_store_t *ctx, struct imap_cmd *tcmd)
835 imap_t *imap = ctx->imap;
836 struct imap_cmd *cmdp, **pcmdp, *ncmdp;
837 char *cmd, *arg, *arg1, *p;
838 int n, resp, resp2, tag;
841 if (buffer_gets(&imap->buf, &cmd))
844 arg = next_arg(&cmd);
846 arg = next_arg(&cmd);
848 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
852 if (!strcmp("NAMESPACE", arg)) {
853 imap->ns_personal = parse_list(&cmd);
854 imap->ns_other = parse_list(&cmd);
855 imap->ns_shared = parse_list(&cmd);
856 } else if (!strcmp("OK", arg) || !strcmp("BAD", arg) ||
857 !strcmp("NO", arg) || !strcmp("BYE", arg)) {
858 if ((resp = parse_response_code(ctx, NULL, cmd)) != RESP_OK)
860 } else if (!strcmp("CAPABILITY", arg))
861 parse_capability(imap, cmd);
862 else if ((arg1 = next_arg(&cmd))) {
863 if (!strcmp("EXISTS", arg1))
864 ctx->gen.count = atoi(arg);
865 else if (!strcmp("RECENT", arg1))
866 ctx->gen.recent = atoi(arg);
868 fprintf(stderr, "IMAP error: unable to parse untagged response\n");
871 } else if (!imap->in_progress) {
872 fprintf(stderr, "IMAP error: unexpected reply: %s %s\n", arg, cmd ? cmd : "");
874 } else if (*arg == '+') {
875 /* This can happen only with the last command underway, as
876 it enforces a round-trip. */
877 cmdp = (struct imap_cmd *)((char *)imap->in_progress_append -
878 offsetof(struct imap_cmd, next));
880 n = socket_write(&imap->buf.sock, cmdp->cb.data, cmdp->cb.dlen);
882 cmdp->cb.data = NULL;
883 if (n != (int)cmdp->cb.dlen)
885 } else if (cmdp->cb.cont) {
886 if (cmdp->cb.cont(ctx, cmdp, cmd))
889 fprintf(stderr, "IMAP error: unexpected command continuation request\n");
892 if (socket_write(&imap->buf.sock, "\r\n", 2) != 2)
895 imap->literal_pending = 0;
900 for (pcmdp = &imap->in_progress; (cmdp = *pcmdp); pcmdp = &cmdp->next)
901 if (cmdp->tag == tag)
903 fprintf(stderr, "IMAP error: unexpected tag %s\n", arg);
906 if (!(*pcmdp = cmdp->next))
907 imap->in_progress_append = pcmdp;
908 imap->num_in_progress--;
909 if (cmdp->cb.cont || cmdp->cb.data)
910 imap->literal_pending = 0;
911 arg = next_arg(&cmd);
912 if (!strcmp("OK", arg))
915 if (!strcmp("NO", arg)) {
916 if (cmdp->cb.create && cmd && (cmdp->cb.trycreate || !memcmp(cmd, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
917 p = strchr(cmdp->cmd, '"');
918 if (!issue_imap_cmd(ctx, NULL, "CREATE \"%.*s\"", strchr(p + 1, '"') - p + 1, p)) {
922 /* not waiting here violates the spec, but a server that does not
923 grok this nonetheless violates it too. */
925 if (!(ncmdp = issue_imap_cmd(ctx, &cmdp->cb, "%s", cmdp->cmd))) {
932 return 0; /* ignored */
938 } else /*if (!strcmp("BAD", arg))*/
940 fprintf(stderr, "IMAP command '%s' returned response (%s) - %s\n",
941 memcmp(cmdp->cmd, "LOGIN", 5) ?
942 cmdp->cmd : "LOGIN <user> <pass>",
943 arg, cmd ? cmd : "");
945 if ((resp2 = parse_response_code(ctx, &cmdp->cb, cmd)) > resp)
949 cmdp->cb.done(ctx, cmdp, resp);
953 if (!tcmd || tcmd == cmdp)
960 static void imap_close_server(imap_store_t *ictx)
962 imap_t *imap = ictx->imap;
964 if (imap->buf.sock.fd != -1) {
965 imap_exec(ictx, NULL, "LOGOUT");
966 socket_shutdown(&imap->buf.sock);
968 free_list(imap->ns_personal);
969 free_list(imap->ns_other);
970 free_list(imap->ns_shared);
974 static void imap_close_store(store_t *ctx)
976 imap_close_server((imap_store_t *)ctx);
977 free_generic_messages(ctx->msgs);
981 static store_t *imap_open_store(imap_server_conf_t *srvc)
987 struct sockaddr_in addr;
988 int s, a[2], preauth;
991 ctx = xcalloc(sizeof(*ctx), 1);
993 ctx->imap = imap = xcalloc(sizeof(*imap), 1);
994 imap->buf.sock.fd = -1;
995 imap->in_progress_append = &imap->in_progress;
997 /* open connection to IMAP server */
1000 imap_info("Starting tunnel '%s'... ", srvc->tunnel);
1002 if (socketpair(PF_UNIX, SOCK_STREAM, 0, a)) {
1003 perror("socketpair");
1011 if (dup2(a[0], 0) == -1 || dup2(a[0], 1) == -1)
1015 execl("/bin/sh", "sh", "-c", srvc->tunnel, NULL);
1021 imap->buf.sock.fd = a[1];
1025 memset(&addr, 0, sizeof(addr));
1026 addr.sin_port = htons(srvc->port);
1027 addr.sin_family = AF_INET;
1029 imap_info("Resolving %s... ", srvc->host);
1030 he = gethostbyname(srvc->host);
1032 perror("gethostbyname");
1037 addr.sin_addr.s_addr = *((int *) he->h_addr_list[0]);
1039 s = socket(PF_INET, SOCK_STREAM, 0);
1041 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
1042 if (connect(s, (struct sockaddr *)&addr, sizeof(addr))) {
1048 imap->buf.sock.fd = s;
1050 if (srvc->use_ssl &&
1051 ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1058 /* read the greeting string */
1059 if (buffer_gets(&imap->buf, &rsp)) {
1060 fprintf(stderr, "IMAP error: no greeting response\n");
1063 arg = next_arg(&rsp);
1064 if (!arg || *arg != '*' || (arg = next_arg(&rsp)) == NULL) {
1065 fprintf(stderr, "IMAP error: invalid greeting response\n");
1069 if (!strcmp("PREAUTH", arg))
1071 else if (strcmp("OK", arg) != 0) {
1072 fprintf(stderr, "IMAP error: unknown greeting response\n");
1075 parse_response_code(ctx, NULL, rsp);
1076 if (!imap->caps && imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
1081 if (!srvc->use_ssl && CAP(STARTTLS)) {
1082 if (imap_exec(ctx, 0, "STARTTLS") != RESP_OK)
1084 if (ssl_socket_connect(&imap->buf.sock, 1,
1087 /* capabilities may have changed, so get the new capabilities */
1088 if (imap_exec(ctx, 0, "CAPABILITY") != RESP_OK)
1092 imap_info("Logging in...\n");
1094 fprintf(stderr, "Skipping server %s, no user\n", srvc->host);
1099 sprintf(prompt, "Password (%s@%s): ", srvc->user, srvc->host);
1100 arg = getpass(prompt);
1106 fprintf(stderr, "Skipping account %s@%s, no password\n", srvc->user, srvc->host);
1110 * getpass() returns a pointer to a static buffer. make a copy
1111 * for long term storage.
1113 srvc->pass = xstrdup(arg);
1116 fprintf(stderr, "Skipping account %s@%s, server forbids LOGIN\n", srvc->user, srvc->host);
1119 if (!imap->buf.sock.ssl)
1120 imap_warn( "*** IMAP Warning *** Password is being "
1121 "sent in the clear\n" );
1122 if (imap_exec(ctx, NULL, "LOGIN \"%s\" \"%s\"", srvc->user, srvc->pass) != RESP_OK) {
1123 fprintf(stderr, "IMAP error: LOGIN failed\n");
1130 return (store_t *)ctx;
1133 imap_close_store(&ctx->gen);
1137 static int imap_make_flags(int flags, char *buf)
1142 for (i = d = 0; i < ARRAY_SIZE(Flags); i++)
1143 if (flags & (1 << i)) {
1146 for (s = Flags[i]; *s; s++)
1156 static int imap_store_msg(store_t *gctx, msg_data_t *data, int *uid)
1158 imap_store_t *ctx = (imap_store_t *)gctx;
1159 imap_t *imap = ctx->imap;
1160 struct imap_cmd_cb cb;
1162 const char *prefix, *box;
1163 int ret, i, j, d, len, extra, nocr;
1164 int start, sbreak = 0, ebreak = 0;
1165 char flagstr[128], tuid[TUIDL * 2 + 1];
1167 memset(&cb, 0, sizeof(cb));
1173 if (!CAP(UIDPLUS) && uid) {
1177 if (fmap[i++] == '\n') {
1179 if (i - 2 + nocr == start) {
1180 sbreak = ebreak = i - 2 + nocr;
1183 if (!memcmp(fmap + start, "X-TUID: ", 8)) {
1184 extra -= (ebreak = i) - (sbreak = start) + nocr;
1189 /* invalid message */
1193 for (j = 0; j < TUIDL; j++)
1194 sprintf(tuid + j * 2, "%02x", arc4_getbyte());
1195 extra += 8 + TUIDL * 2 + 2;
1198 for (; i < len; i++)
1199 if (fmap[i] == '\n')
1202 cb.dlen = len + extra;
1203 buf = cb.data = xmalloc(cb.dlen);
1205 if (!CAP(UIDPLUS) && uid) {
1207 for (; i < sbreak; i++)
1208 if (fmap[i] == '\n') {
1214 memcpy(buf, fmap, sbreak);
1217 memcpy(buf, "X-TUID: ", 8);
1219 memcpy(buf, tuid, TUIDL * 2);
1226 for (; i < len; i++)
1227 if (fmap[i] == '\n') {
1233 memcpy(buf, fmap + i, len - i);
1239 d = imap_make_flags(data->flags, flagstr);
1245 box = gctx->conf->trash;
1246 prefix = ctx->prefix;
1249 imap->caps = imap->rcaps & ~(1 << LITERALPLUS);
1252 prefix = !strcmp(box, "INBOX") ? "" : ctx->prefix;
1256 ret = imap_exec_m(ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr);
1257 imap->caps = imap->rcaps;
1268 #define CHUNKSIZE 0x1000
1270 static int read_message(FILE *f, msg_data_t *msg)
1274 memset(msg, 0, sizeof(*msg));
1275 strbuf_init(&buf, 0);
1278 if (strbuf_fread(&buf, CHUNKSIZE, f) <= 0)
1283 msg->data = strbuf_detach(&buf, NULL);
1287 static int count_messages(msg_data_t *msg)
1290 char *p = msg->data;
1293 if (!prefixcmp(p, "From ")) {
1297 p = strstr(p+5, "\nFrom ");
1305 static int split_msg(msg_data_t *all_msgs, msg_data_t *msg, int *ofs)
1309 memset(msg, 0, sizeof *msg);
1310 if (*ofs >= all_msgs->len)
1313 data = &all_msgs->data[*ofs];
1314 msg->len = all_msgs->len - *ofs;
1316 if (msg->len < 5 || prefixcmp(data, "From "))
1319 p = strchr(data, '\n');
1327 p = strstr(data, "\nFrom ");
1329 msg->len = &p[1] - data;
1331 msg->data = xmemdupz(data, msg->len);
1336 static imap_server_conf_t server = {
1347 static char *imap_folder;
1349 static int git_imap_config(const char *key, const char *val, void *cb)
1351 char imap_key[] = "imap.";
1353 if (strncmp(key, imap_key, sizeof imap_key - 1))
1357 return config_error_nonbool(key);
1359 key += sizeof imap_key - 1;
1361 if (!strcmp("folder", key)) {
1362 imap_folder = xstrdup(val);
1363 } else if (!strcmp("host", key)) {
1364 if (!prefixcmp(val, "imap:"))
1366 else if (!prefixcmp(val, "imaps:")) {
1370 if (!prefixcmp(val, "//"))
1372 server.host = xstrdup(val);
1374 else if (!strcmp("user", key))
1375 server.user = xstrdup(val);
1376 else if (!strcmp("pass", key))
1377 server.pass = xstrdup(val);
1378 else if (!strcmp("port", key))
1379 server.port = git_config_int(key, val);
1380 else if (!strcmp("tunnel", key))
1381 server.tunnel = xstrdup(val);
1382 else if (!strcmp("sslverify", key))
1383 server.ssl_verify = git_config_bool(key, val);
1387 int main(int argc, char **argv)
1389 msg_data_t all_msgs, msg;
1390 store_t *ctx = NULL;
1397 /* init the random number generator */
1400 setup_git_directory_gently(&nongit_ok);
1401 git_config(git_imap_config, NULL);
1404 server.port = server.use_ssl ? 993 : 143;
1407 fprintf(stderr, "no imap store specified\n");
1411 if (!server.tunnel) {
1412 fprintf(stderr, "no imap host specified\n");
1415 server.host = "tunnel";
1418 /* read the messages */
1419 if (!read_message(stdin, &all_msgs)) {
1420 fprintf(stderr,"nothing to send\n");
1424 total = count_messages(&all_msgs);
1426 fprintf(stderr,"no messages to send\n");
1430 /* write it to the imap server */
1431 ctx = imap_open_store(&server);
1433 fprintf(stderr,"failed to open store\n");
1437 fprintf(stderr, "sending %d message%s\n", total, (total!=1)?"s":"");
1438 ctx->name = imap_folder;
1440 unsigned percent = n * 100 / total;
1441 fprintf(stderr, "%4u%% (%d/%d) done\r", percent, n, total);
1442 if (!split_msg(&all_msgs, &msg, &ofs))
1444 r = imap_store_msg(ctx, &msg, &uid);
1445 if (r != DRV_OK) break;
1448 fprintf(stderr, "\n");
1450 imap_close_store(ctx);