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
33 const char *path
; /* should this be here? its interpretation is driver-specific */
36 unsigned max_size
; /* off_t is overkill */
37 unsigned trash_remote_new
:1, trash_only_new
:1;
41 struct string_list
*next
;
46 struct channel_conf
*next
;
48 struct store_conf
*master
, *slave
;
49 char *master_name
, *slave_name
;
51 struct string_list
*patterns
;
53 unsigned max_messages
; /* for slave only */
57 struct group_conf
*next
;
59 struct string_list
*channels
;
62 /* For message->status */
63 #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
64 #define M_DEAD (1<<1) /* expunged */
65 #define M_FLAGS (1<<2) /* flags fetched */
69 /* struct string_list *keywords; */
70 size_t size
; /* zero implies "not fetched" */
72 unsigned char flags
, status
;
76 struct store_conf
*conf
; /* foreign */
78 /* currently open mailbox */
79 const char *name
; /* foreign! maybe preset? */
81 struct message
*msgs
; /* own */
83 unsigned char opts
; /* maybe preset? */
84 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
85 int count
; /* # of messages */
86 int recent
; /* # of recent messages - don't trust this beyond the initial read */
96 static const char imap_send_usage
[] = "git imap-send < <mbox>";
99 #define DRV_MSG_BAD -1
100 #define DRV_BOX_BAD -2
101 #define DRV_STORE_BAD -3
103 static int Verbose
, Quiet
;
105 static void imap_info(const char *, ...);
106 static void imap_warn(const char *, ...);
108 static char *next_arg(char **);
110 static void free_generic_messages(struct message
*);
112 static int nfsnprintf(char *buf
, int blen
, const char *fmt
, ...);
114 static int nfvasprintf(char **strp
, const char *fmt
, va_list ap
)
119 len
= vsnprintf(tmp
, sizeof(tmp
), fmt
, ap
);
121 die("Fatal: Out of memory");
122 if (len
>= sizeof(tmp
))
123 die("imap command overflow!");
124 *strp
= xmemdupz(tmp
, len
);
128 static void arc4_init(void);
129 static unsigned char arc4_getbyte(void);
131 struct imap_server_conf
{
143 struct imap_store_conf
{
144 struct store_conf gen
;
145 struct imap_server_conf
*server
;
146 unsigned use_namespace
:1;
149 #define NIL (void *)0x1
150 #define LIST (void *)0x2
153 struct imap_list
*next
, *child
;
164 struct imap_socket sock
;
173 int uidnext
; /* from SELECT responses */
174 struct imap_list
*ns_personal
, *ns_other
, *ns_shared
; /* NAMESPACE info */
175 unsigned caps
, rcaps
; /* CAPABILITY results */
177 int nexttag
, num_in_progress
, literal_pending
;
178 struct imap_cmd
*in_progress
, **in_progress_append
;
179 struct imap_buffer buf
; /* this is BIG, so put it last */
187 unsigned /*currentnc:1,*/ trashnc
:1;
191 int (*cont
)(struct imap_store
*ctx
, struct imap_cmd
*cmd
, const char *prompt
);
192 void (*done
)(struct imap_store
*ctx
, struct imap_cmd
*cmd
, int response
);
197 unsigned create
:1, trycreate
:1;
201 struct imap_cmd
*next
;
202 struct imap_cmd_cb cb
;
207 #define CAP(cap) (imap->caps & (1 << (cap)))
217 static const char *cap_list
[] = {
229 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
);
232 static const char *Flags
[] = {
241 static void ssl_socket_perror(const char *func
)
243 fprintf(stderr
, "%s: %s\n", func
, ERR_error_string(ERR_get_error(), NULL
));
247 static void socket_perror(const char *func
, struct imap_socket
*sock
, int ret
)
251 int sslerr
= SSL_get_error(sock
->ssl
, ret
);
255 case SSL_ERROR_SYSCALL
:
256 perror("SSL_connect");
259 ssl_socket_perror("SSL_connect");
268 fprintf(stderr
, "%s: unexpected EOF\n", func
);
272 static int ssl_socket_connect(struct imap_socket
*sock
, int use_tls_only
, int verify
)
275 fprintf(stderr
, "SSL requested but SSL support not compiled in\n");
283 SSL_load_error_strings();
286 meth
= TLSv1_method();
288 meth
= SSLv23_method();
291 ssl_socket_perror("SSLv23_method");
295 ctx
= SSL_CTX_new(meth
);
298 SSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, NULL
);
300 if (!SSL_CTX_set_default_verify_paths(ctx
)) {
301 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
304 sock
->ssl
= SSL_new(ctx
);
306 ssl_socket_perror("SSL_new");
309 if (!SSL_set_fd(sock
->ssl
, sock
->fd
)) {
310 ssl_socket_perror("SSL_set_fd");
314 ret
= SSL_connect(sock
->ssl
);
316 socket_perror("SSL_connect", sock
, ret
);
324 static int socket_read(struct imap_socket
*sock
, char *buf
, int len
)
329 n
= SSL_read(sock
->ssl
, buf
, len
);
332 n
= xread(sock
->fd
, buf
, len
);
334 socket_perror("read", sock
, n
);
341 static int socket_write(struct imap_socket
*sock
, const char *buf
, int len
)
346 n
= SSL_write(sock
->ssl
, buf
, len
);
349 n
= write_in_full(sock
->fd
, buf
, len
);
351 socket_perror("write", sock
, n
);
358 static void socket_shutdown(struct imap_socket
*sock
)
362 SSL_shutdown(sock
->ssl
);
369 /* simple line buffering */
370 static int buffer_gets(struct imap_buffer
*b
, char **s
)
373 int start
= b
->offset
;
378 /* make sure we have enough data to read the \r\n sequence */
379 if (b
->offset
+ 1 >= b
->bytes
) {
381 /* shift down used bytes */
384 assert(start
<= b
->bytes
);
385 n
= b
->bytes
- start
;
388 memmove(b
->buf
, b
->buf
+ start
, n
);
394 n
= socket_read(&b
->sock
, b
->buf
+ b
->bytes
,
395 sizeof(b
->buf
) - b
->bytes
);
403 if (b
->buf
[b
->offset
] == '\r') {
404 assert(b
->offset
+ 1 < b
->bytes
);
405 if (b
->buf
[b
->offset
+ 1] == '\n') {
406 b
->buf
[b
->offset
] = 0; /* terminate the string */
407 b
->offset
+= 2; /* next line */
419 static void imap_info(const char *msg
, ...)
431 static void imap_warn(const char *msg
, ...)
437 vfprintf(stderr
, msg
, va
);
442 static char *next_arg(char **s
)
448 while (isspace((unsigned char) **s
))
457 *s
= strchr(*s
, '"');
460 while (**s
&& !isspace((unsigned char) **s
))
472 static void free_generic_messages(struct message
*msgs
)
474 struct message
*tmsg
;
476 for (; msgs
; msgs
= tmsg
) {
482 static int nfsnprintf(char *buf
, int blen
, const char *fmt
, ...)
488 if (blen
<= 0 || (unsigned)(ret
= vsnprintf(buf
, blen
, fmt
, va
)) >= (unsigned)blen
)
489 die("Fatal: buffer too small. Please report a bug.");
495 unsigned char i
, j
, s
[256];
498 static void arc4_init(void)
501 unsigned char j
, si
, dat
[128];
503 if ((fd
= open("/dev/urandom", O_RDONLY
)) < 0 && (fd
= open("/dev/random", O_RDONLY
)) < 0) {
504 fprintf(stderr
, "Fatal: no random number source available.\n");
507 if (read_in_full(fd
, dat
, 128) != 128) {
508 fprintf(stderr
, "Fatal: cannot read random number source.\n");
513 for (i
= 0; i
< 256; i
++)
515 for (i
= j
= 0; i
< 256; i
++) {
517 j
+= si
+ dat
[i
& 127];
523 for (i
= 0; i
< 256; i
++)
527 static unsigned char arc4_getbyte(void)
529 unsigned char si
, sj
;
537 return rs
.s
[(si
+ sj
) & 0xff];
540 static struct imap_cmd
*v_issue_imap_cmd(struct imap_store
*ctx
,
541 struct imap_cmd_cb
*cb
,
542 const char *fmt
, va_list ap
)
544 struct imap
*imap
= ctx
->imap
;
545 struct imap_cmd
*cmd
;
549 cmd
= xmalloc(sizeof(struct imap_cmd
));
550 nfvasprintf(&cmd
->cmd
, fmt
, ap
);
551 cmd
->tag
= ++imap
->nexttag
;
556 memset(&cmd
->cb
, 0, sizeof(cmd
->cb
));
558 while (imap
->literal_pending
)
559 get_cmd_result(ctx
, NULL
);
561 bufl
= nfsnprintf(buf
, sizeof(buf
), cmd
->cb
.data
? CAP(LITERALPLUS
) ?
562 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
563 cmd
->tag
, cmd
->cmd
, cmd
->cb
.dlen
);
565 if (imap
->num_in_progress
)
566 printf("(%d in progress) ", imap
->num_in_progress
);
567 if (memcmp(cmd
->cmd
, "LOGIN", 5))
568 printf(">>> %s", buf
);
570 printf(">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
572 if (socket_write(&imap
->buf
.sock
, buf
, bufl
) != bufl
) {
580 if (CAP(LITERALPLUS
)) {
581 n
= socket_write(&imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
583 if (n
!= cmd
->cb
.dlen
||
584 socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2) {
591 imap
->literal_pending
= 1;
592 } else if (cmd
->cb
.cont
)
593 imap
->literal_pending
= 1;
595 *imap
->in_progress_append
= cmd
;
596 imap
->in_progress_append
= &cmd
->next
;
597 imap
->num_in_progress
++;
601 static struct imap_cmd
*issue_imap_cmd(struct imap_store
*ctx
,
602 struct imap_cmd_cb
*cb
,
603 const char *fmt
, ...)
605 struct imap_cmd
*ret
;
609 ret
= v_issue_imap_cmd(ctx
, cb
, fmt
, ap
);
614 static int imap_exec(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
615 const char *fmt
, ...)
618 struct imap_cmd
*cmdp
;
621 cmdp
= v_issue_imap_cmd(ctx
, cb
, fmt
, ap
);
626 return get_cmd_result(ctx
, cmdp
);
629 static int imap_exec_m(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
630 const char *fmt
, ...)
633 struct imap_cmd
*cmdp
;
636 cmdp
= v_issue_imap_cmd(ctx
, cb
, fmt
, ap
);
639 return DRV_STORE_BAD
;
641 switch (get_cmd_result(ctx
, cmdp
)) {
642 case RESP_BAD
: return DRV_STORE_BAD
;
643 case RESP_NO
: return DRV_MSG_BAD
;
644 default: return DRV_OK
;
648 static int is_atom(struct imap_list
*list
)
650 return list
&& list
->val
&& list
->val
!= NIL
&& list
->val
!= LIST
;
653 static int is_list(struct imap_list
*list
)
655 return list
&& list
->val
== LIST
;
658 static void free_list(struct imap_list
*list
)
660 struct imap_list
*tmp
;
662 for (; list
; list
= tmp
) {
665 free_list(list
->child
);
666 else if (is_atom(list
))
672 static int parse_imap_list_l(struct imap
*imap
, char **sp
, struct imap_list
**curp
, int level
)
674 struct imap_list
*cur
;
679 while (isspace((unsigned char)*s
))
681 if (level
&& *s
== ')') {
685 *curp
= cur
= xmalloc(sizeof(*cur
));
687 cur
->val
= NULL
; /* for clean bail */
692 if (parse_imap_list_l(imap
, &s
, &cur
->child
, level
+ 1))
694 } else if (imap
&& *s
== '{') {
696 bytes
= cur
->len
= strtol(s
+ 1, &s
, 10);
700 s
= cur
->val
= xmalloc(cur
->len
);
702 /* dump whats left over in the input buffer */
703 n
= imap
->buf
.bytes
- imap
->buf
.offset
;
706 /* the entire message fit in the buffer */
709 memcpy(s
, imap
->buf
.buf
+ imap
->buf
.offset
, n
);
713 /* mark that we used part of the buffer */
714 imap
->buf
.offset
+= n
;
716 /* now read the rest of the message */
718 if ((n
= socket_read(&imap
->buf
.sock
, s
, bytes
)) <= 0)
724 if (buffer_gets(&imap
->buf
, &s
))
726 } else if (*s
== '"') {
730 for (; *s
!= '"'; s
++)
735 cur
->val
= xmemdupz(p
, cur
->len
);
739 for (; *s
&& !isspace((unsigned char)*s
); s
++)
740 if (level
&& *s
== ')')
743 if (cur
->len
== 3 && !memcmp("NIL", p
, 3))
746 cur
->val
= xmemdupz(p
, cur
->len
);
763 static struct imap_list
*parse_imap_list(struct imap
*imap
, char **sp
)
765 struct imap_list
*head
;
767 if (!parse_imap_list_l(imap
, sp
, &head
, 0))
773 static struct imap_list
*parse_list(char **sp
)
775 return parse_imap_list(NULL
, sp
);
778 static void parse_capability(struct imap
*imap
, char *cmd
)
783 imap
->caps
= 0x80000000;
784 while ((arg
= next_arg(&cmd
)))
785 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
786 if (!strcmp(cap_list
[i
], arg
))
787 imap
->caps
|= 1 << i
;
788 imap
->rcaps
= imap
->caps
;
791 static int parse_response_code(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
794 struct imap
*imap
= ctx
->imap
;
798 return RESP_OK
; /* no response code */
800 if (!(p
= strchr(s
, ']'))) {
801 fprintf(stderr
, "IMAP error: malformed response code\n");
806 if (!strcmp("UIDVALIDITY", arg
)) {
807 if (!(arg
= next_arg(&s
)) || !(ctx
->gen
.uidvalidity
= atoi(arg
))) {
808 fprintf(stderr
, "IMAP error: malformed UIDVALIDITY status\n");
811 } else if (!strcmp("UIDNEXT", arg
)) {
812 if (!(arg
= next_arg(&s
)) || !(imap
->uidnext
= atoi(arg
))) {
813 fprintf(stderr
, "IMAP error: malformed NEXTUID status\n");
816 } else if (!strcmp("CAPABILITY", arg
)) {
817 parse_capability(imap
, s
);
818 } else if (!strcmp("ALERT", arg
)) {
819 /* RFC2060 says that these messages MUST be displayed
822 for (; isspace((unsigned char)*p
); p
++);
823 fprintf(stderr
, "*** IMAP ALERT *** %s\n", p
);
824 } else if (cb
&& cb
->ctx
&& !strcmp("APPENDUID", arg
)) {
825 if (!(arg
= next_arg(&s
)) || !(ctx
->gen
.uidvalidity
= atoi(arg
)) ||
826 !(arg
= next_arg(&s
)) || !(*(int *)cb
->ctx
= atoi(arg
))) {
827 fprintf(stderr
, "IMAP error: malformed APPENDUID status\n");
834 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
)
836 struct imap
*imap
= ctx
->imap
;
837 struct imap_cmd
*cmdp
, **pcmdp
, *ncmdp
;
838 char *cmd
, *arg
, *arg1
, *p
;
839 int n
, resp
, resp2
, tag
;
842 if (buffer_gets(&imap
->buf
, &cmd
))
845 arg
= next_arg(&cmd
);
847 arg
= next_arg(&cmd
);
849 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
853 if (!strcmp("NAMESPACE", arg
)) {
854 imap
->ns_personal
= parse_list(&cmd
);
855 imap
->ns_other
= parse_list(&cmd
);
856 imap
->ns_shared
= parse_list(&cmd
);
857 } else if (!strcmp("OK", arg
) || !strcmp("BAD", arg
) ||
858 !strcmp("NO", arg
) || !strcmp("BYE", arg
)) {
859 if ((resp
= parse_response_code(ctx
, NULL
, cmd
)) != RESP_OK
)
861 } else if (!strcmp("CAPABILITY", arg
))
862 parse_capability(imap
, cmd
);
863 else if ((arg1
= next_arg(&cmd
))) {
864 if (!strcmp("EXISTS", arg1
))
865 ctx
->gen
.count
= atoi(arg
);
866 else if (!strcmp("RECENT", arg1
))
867 ctx
->gen
.recent
= atoi(arg
);
869 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
872 } else if (!imap
->in_progress
) {
873 fprintf(stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "");
875 } else if (*arg
== '+') {
876 /* This can happen only with the last command underway, as
877 it enforces a round-trip. */
878 cmdp
= (struct imap_cmd
*)((char *)imap
->in_progress_append
-
879 offsetof(struct imap_cmd
, next
));
881 n
= socket_write(&imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
883 cmdp
->cb
.data
= NULL
;
884 if (n
!= (int)cmdp
->cb
.dlen
)
886 } else if (cmdp
->cb
.cont
) {
887 if (cmdp
->cb
.cont(ctx
, cmdp
, cmd
))
890 fprintf(stderr
, "IMAP error: unexpected command continuation request\n");
893 if (socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2)
896 imap
->literal_pending
= 0;
901 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
902 if (cmdp
->tag
== tag
)
904 fprintf(stderr
, "IMAP error: unexpected tag %s\n", arg
);
907 if (!(*pcmdp
= cmdp
->next
))
908 imap
->in_progress_append
= pcmdp
;
909 imap
->num_in_progress
--;
910 if (cmdp
->cb
.cont
|| cmdp
->cb
.data
)
911 imap
->literal_pending
= 0;
912 arg
= next_arg(&cmd
);
913 if (!strcmp("OK", arg
))
916 if (!strcmp("NO", arg
)) {
917 if (cmdp
->cb
.create
&& cmd
&& (cmdp
->cb
.trycreate
|| !memcmp(cmd
, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
918 p
= strchr(cmdp
->cmd
, '"');
919 if (!issue_imap_cmd(ctx
, NULL
, "CREATE \"%.*s\"", strchr(p
+ 1, '"') - p
+ 1, p
)) {
923 /* not waiting here violates the spec, but a server that does not
924 grok this nonetheless violates it too. */
926 if (!(ncmdp
= issue_imap_cmd(ctx
, &cmdp
->cb
, "%s", cmdp
->cmd
))) {
933 return 0; /* ignored */
939 } else /*if (!strcmp("BAD", arg))*/
941 fprintf(stderr
, "IMAP command '%s' returned response (%s) - %s\n",
942 memcmp(cmdp
->cmd
, "LOGIN", 5) ?
943 cmdp
->cmd
: "LOGIN <user> <pass>",
944 arg
, cmd
? cmd
: "");
946 if ((resp2
= parse_response_code(ctx
, &cmdp
->cb
, cmd
)) > resp
)
950 cmdp
->cb
.done(ctx
, cmdp
, resp
);
954 if (!tcmd
|| tcmd
== cmdp
)
961 static void imap_close_server(struct imap_store
*ictx
)
963 struct imap
*imap
= ictx
->imap
;
965 if (imap
->buf
.sock
.fd
!= -1) {
966 imap_exec(ictx
, NULL
, "LOGOUT");
967 socket_shutdown(&imap
->buf
.sock
);
969 free_list(imap
->ns_personal
);
970 free_list(imap
->ns_other
);
971 free_list(imap
->ns_shared
);
975 static void imap_close_store(struct store
*ctx
)
977 imap_close_server((struct imap_store
*)ctx
);
978 free_generic_messages(ctx
->msgs
);
982 static struct store
*imap_open_store(struct imap_server_conf
*srvc
)
984 struct imap_store
*ctx
;
987 int s
= -1, a
[2], preauth
;
990 ctx
= xcalloc(sizeof(*ctx
), 1);
992 ctx
->imap
= imap
= xcalloc(sizeof(*imap
), 1);
993 imap
->buf
.sock
.fd
= -1;
994 imap
->in_progress_append
= &imap
->in_progress
;
996 /* open connection to IMAP server */
999 imap_info("Starting tunnel '%s'... ", srvc
->tunnel
);
1001 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, a
)) {
1002 perror("socketpair");
1010 if (dup2(a
[0], 0) == -1 || dup2(a
[0], 1) == -1)
1014 execl("/bin/sh", "sh", "-c", srvc
->tunnel
, NULL
);
1020 imap
->buf
.sock
.fd
= a
[1];
1025 struct addrinfo hints
, *ai0
, *ai
;
1029 snprintf(portstr
, sizeof(portstr
), "%hu", srvc
->port
);
1031 memset(&hints
, 0, sizeof(hints
));
1032 hints
.ai_socktype
= SOCK_STREAM
;
1033 hints
.ai_protocol
= IPPROTO_TCP
;
1035 imap_info("Resolving %s... ", srvc
->host
);
1036 gai
= getaddrinfo(srvc
->host
, portstr
, &hints
, &ai
);
1038 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(gai
));
1043 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
1044 char addr
[NI_MAXHOST
];
1046 s
= socket(ai
->ai_family
, ai
->ai_socktype
,
1051 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
,
1052 sizeof(addr
), NULL
, 0, NI_NUMERICHOST
);
1053 imap_info("Connecting to [%s]:%s... ", addr
, portstr
);
1055 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
1067 struct sockaddr_in addr
;
1069 memset(&addr
, 0, sizeof(addr
));
1070 addr
.sin_port
= htons(srvc
->port
);
1071 addr
.sin_family
= AF_INET
;
1073 imap_info("Resolving %s... ", srvc
->host
);
1074 he
= gethostbyname(srvc
->host
);
1076 perror("gethostbyname");
1081 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
1083 s
= socket(PF_INET
, SOCK_STREAM
, 0);
1085 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr
.sin_addr
), ntohs(addr
.sin_port
));
1086 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
1093 fputs("Error: unable to connect to server.\n", stderr
);
1097 imap
->buf
.sock
.fd
= s
;
1099 if (srvc
->use_ssl
&&
1100 ssl_socket_connect(&imap
->buf
.sock
, 0, srvc
->ssl_verify
)) {
1107 /* read the greeting string */
1108 if (buffer_gets(&imap
->buf
, &rsp
)) {
1109 fprintf(stderr
, "IMAP error: no greeting response\n");
1112 arg
= next_arg(&rsp
);
1113 if (!arg
|| *arg
!= '*' || (arg
= next_arg(&rsp
)) == NULL
) {
1114 fprintf(stderr
, "IMAP error: invalid greeting response\n");
1118 if (!strcmp("PREAUTH", arg
))
1120 else if (strcmp("OK", arg
) != 0) {
1121 fprintf(stderr
, "IMAP error: unknown greeting response\n");
1124 parse_response_code(ctx
, NULL
, rsp
);
1125 if (!imap
->caps
&& imap_exec(ctx
, NULL
, "CAPABILITY") != RESP_OK
)
1130 if (!srvc
->use_ssl
&& CAP(STARTTLS
)) {
1131 if (imap_exec(ctx
, 0, "STARTTLS") != RESP_OK
)
1133 if (ssl_socket_connect(&imap
->buf
.sock
, 1,
1136 /* capabilities may have changed, so get the new capabilities */
1137 if (imap_exec(ctx
, 0, "CAPABILITY") != RESP_OK
)
1141 imap_info("Logging in...\n");
1143 fprintf(stderr
, "Skipping server %s, no user\n", srvc
->host
);
1148 sprintf(prompt
, "Password (%s@%s): ", srvc
->user
, srvc
->host
);
1149 arg
= getpass(prompt
);
1155 fprintf(stderr
, "Skipping account %s@%s, no password\n", srvc
->user
, srvc
->host
);
1159 * getpass() returns a pointer to a static buffer. make a copy
1160 * for long term storage.
1162 srvc
->pass
= xstrdup(arg
);
1165 fprintf(stderr
, "Skipping account %s@%s, server forbids LOGIN\n", srvc
->user
, srvc
->host
);
1168 if (!imap
->buf
.sock
.ssl
)
1169 imap_warn("*** IMAP Warning *** Password is being "
1170 "sent in the clear\n");
1171 if (imap_exec(ctx
, NULL
, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1172 fprintf(stderr
, "IMAP error: LOGIN failed\n");
1179 return (struct store
*)ctx
;
1182 imap_close_store(&ctx
->gen
);
1186 static int imap_make_flags(int flags
, char *buf
)
1191 for (i
= d
= 0; i
< ARRAY_SIZE(Flags
); i
++)
1192 if (flags
& (1 << i
)) {
1195 for (s
= Flags
[i
]; *s
; s
++)
1205 static int imap_store_msg(struct store
*gctx
, struct msg_data
*data
, int *uid
)
1207 struct imap_store
*ctx
= (struct imap_store
*)gctx
;
1208 struct imap
*imap
= ctx
->imap
;
1209 struct imap_cmd_cb cb
;
1211 const char *prefix
, *box
;
1212 int ret
, i
, j
, d
, len
, extra
, nocr
;
1213 int start
, sbreak
= 0, ebreak
= 0;
1214 char flagstr
[128], tuid
[TUIDL
* 2 + 1];
1216 memset(&cb
, 0, sizeof(cb
));
1222 if (!CAP(UIDPLUS
) && uid
) {
1226 if (fmap
[i
++] == '\n') {
1228 if (i
- 2 + nocr
== start
) {
1229 sbreak
= ebreak
= i
- 2 + nocr
;
1232 if (!memcmp(fmap
+ start
, "X-TUID: ", 8)) {
1233 extra
-= (ebreak
= i
) - (sbreak
= start
) + nocr
;
1238 /* invalid message */
1242 for (j
= 0; j
< TUIDL
; j
++)
1243 sprintf(tuid
+ j
* 2, "%02x", arc4_getbyte());
1244 extra
+= 8 + TUIDL
* 2 + 2;
1247 for (; i
< len
; i
++)
1248 if (fmap
[i
] == '\n')
1251 cb
.dlen
= len
+ extra
;
1252 buf
= cb
.data
= xmalloc(cb
.dlen
);
1254 if (!CAP(UIDPLUS
) && uid
) {
1256 for (; i
< sbreak
; i
++)
1257 if (fmap
[i
] == '\n') {
1263 memcpy(buf
, fmap
, sbreak
);
1266 memcpy(buf
, "X-TUID: ", 8);
1268 memcpy(buf
, tuid
, TUIDL
* 2);
1275 for (; i
< len
; i
++)
1276 if (fmap
[i
] == '\n') {
1282 memcpy(buf
, fmap
+ i
, len
- i
);
1288 d
= imap_make_flags(data
->flags
, flagstr
);
1294 box
= gctx
->conf
->trash
;
1295 prefix
= ctx
->prefix
;
1298 imap
->caps
= imap
->rcaps
& ~(1 << LITERALPLUS
);
1301 prefix
= !strcmp(box
, "INBOX") ? "" : ctx
->prefix
;
1305 ret
= imap_exec_m(ctx
, &cb
, "APPEND \"%s%s\" %s", prefix
, box
, flagstr
);
1306 imap
->caps
= imap
->rcaps
;
1317 static void encode_html_chars(struct strbuf
*p
)
1320 for (i
= 0; i
< p
->len
; i
++) {
1321 if (p
->buf
[i
] == '&')
1322 strbuf_splice(p
, i
, 1, "&", 5);
1323 if (p
->buf
[i
] == '<')
1324 strbuf_splice(p
, i
, 1, "<", 4);
1325 if (p
->buf
[i
] == '>')
1326 strbuf_splice(p
, i
, 1, ">", 4);
1327 if (p
->buf
[i
] == '"')
1328 strbuf_splice(p
, i
, 1, """, 6);
1331 static void wrap_in_html(struct msg_data
*msg
)
1333 struct strbuf buf
= STRBUF_INIT
;
1334 struct strbuf
**lines
;
1336 static char *content_type
= "Content-Type: text/html;\n";
1337 static char *pre_open
= "<pre>\n";
1338 static char *pre_close
= "</pre>\n";
1339 int added_header
= 0;
1341 strbuf_attach(&buf
, msg
->data
, msg
->len
, msg
->len
);
1342 lines
= strbuf_split(&buf
, '\n');
1343 strbuf_release(&buf
);
1344 for (p
= lines
; *p
; p
++) {
1345 if (! added_header
) {
1346 if ((*p
)->len
== 1 && *((*p
)->buf
) == '\n') {
1347 strbuf_addstr(&buf
, content_type
);
1348 strbuf_addbuf(&buf
, *p
);
1349 strbuf_addstr(&buf
, pre_open
);
1355 encode_html_chars(*p
);
1356 strbuf_addbuf(&buf
, *p
);
1358 strbuf_addstr(&buf
, pre_close
);
1359 strbuf_list_free(lines
);
1361 msg
->data
= strbuf_detach(&buf
, NULL
);
1364 #define CHUNKSIZE 0x1000
1366 static int read_message(FILE *f
, struct msg_data
*msg
)
1368 struct strbuf buf
= STRBUF_INIT
;
1370 memset(msg
, 0, sizeof(*msg
));
1373 if (strbuf_fread(&buf
, CHUNKSIZE
, f
) <= 0)
1378 msg
->data
= strbuf_detach(&buf
, NULL
);
1382 static int count_messages(struct msg_data
*msg
)
1385 char *p
= msg
->data
;
1388 if (!prefixcmp(p
, "From ")) {
1392 p
= strstr(p
+5, "\nFrom ");
1400 static int split_msg(struct msg_data
*all_msgs
, struct msg_data
*msg
, int *ofs
)
1404 memset(msg
, 0, sizeof *msg
);
1405 if (*ofs
>= all_msgs
->len
)
1408 data
= &all_msgs
->data
[*ofs
];
1409 msg
->len
= all_msgs
->len
- *ofs
;
1411 if (msg
->len
< 5 || prefixcmp(data
, "From "))
1414 p
= strchr(data
, '\n');
1422 p
= strstr(data
, "\nFrom ");
1424 msg
->len
= &p
[1] - data
;
1426 msg
->data
= xmemdupz(data
, msg
->len
);
1431 static struct imap_server_conf server
= {
1443 static char *imap_folder
;
1445 static int git_imap_config(const char *key
, const char *val
, void *cb
)
1447 char imap_key
[] = "imap.";
1449 if (strncmp(key
, imap_key
, sizeof imap_key
- 1))
1453 return config_error_nonbool(key
);
1455 key
+= sizeof imap_key
- 1;
1457 if (!strcmp("folder", key
)) {
1458 imap_folder
= xstrdup(val
);
1459 } else if (!strcmp("host", key
)) {
1460 if (!prefixcmp(val
, "imap:"))
1462 else if (!prefixcmp(val
, "imaps:")) {
1466 if (!prefixcmp(val
, "//"))
1468 server
.host
= xstrdup(val
);
1469 } else if (!strcmp("user", key
))
1470 server
.user
= xstrdup(val
);
1471 else if (!strcmp("pass", key
))
1472 server
.pass
= xstrdup(val
);
1473 else if (!strcmp("port", key
))
1474 server
.port
= git_config_int(key
, val
);
1475 else if (!strcmp("tunnel", key
))
1476 server
.tunnel
= xstrdup(val
);
1477 else if (!strcmp("sslverify", key
))
1478 server
.ssl_verify
= git_config_bool(key
, val
);
1479 else if (!strcmp("preformattedHTML", key
))
1480 server
.use_html
= git_config_bool(key
, val
);
1484 int main(int argc
, char **argv
)
1486 struct msg_data all_msgs
, msg
;
1487 struct store
*ctx
= NULL
;
1494 git_extract_argv0_path(argv
[0]);
1497 usage(imap_send_usage
);
1499 /* init the random number generator */
1502 setup_git_directory_gently(&nongit_ok
);
1503 git_config(git_imap_config
, NULL
);
1506 server
.port
= server
.use_ssl
? 993 : 143;
1509 fprintf(stderr
, "no imap store specified\n");
1513 if (!server
.tunnel
) {
1514 fprintf(stderr
, "no imap host specified\n");
1517 server
.host
= "tunnel";
1520 /* read the messages */
1521 if (!read_message(stdin
, &all_msgs
)) {
1522 fprintf(stderr
, "nothing to send\n");
1526 total
= count_messages(&all_msgs
);
1528 fprintf(stderr
, "no messages to send\n");
1532 /* write it to the imap server */
1533 ctx
= imap_open_store(&server
);
1535 fprintf(stderr
, "failed to open store\n");
1539 fprintf(stderr
, "sending %d message%s\n", total
, (total
!= 1) ? "s" : "");
1540 ctx
->name
= imap_folder
;
1542 unsigned percent
= n
* 100 / total
;
1543 fprintf(stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1544 if (!split_msg(&all_msgs
, &msg
, &ofs
))
1546 if (server
.use_html
)
1548 r
= imap_store_msg(ctx
, &msg
, &uid
);
1553 fprintf(stderr
, "\n");
1555 imap_close_store(ctx
);