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 #include "run-command.h"
34 const char *path
; /* should this be here? its interpretation is driver-specific */
37 unsigned max_size
; /* off_t is overkill */
38 unsigned trash_remote_new
:1, trash_only_new
:1;
42 struct string_list
*next
;
47 struct channel_conf
*next
;
49 struct store_conf
*master
, *slave
;
50 char *master_name
, *slave_name
;
52 struct string_list
*patterns
;
54 unsigned max_messages
; /* for slave only */
58 struct group_conf
*next
;
60 struct string_list
*channels
;
63 /* For message->status */
64 #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
65 #define M_DEAD (1<<1) /* expunged */
66 #define M_FLAGS (1<<2) /* flags fetched */
70 /* struct string_list *keywords; */
71 size_t size
; /* zero implies "not fetched" */
73 unsigned char flags
, status
;
77 struct store_conf
*conf
; /* foreign */
79 /* currently open mailbox */
80 const char *name
; /* foreign! maybe preset? */
82 struct message
*msgs
; /* own */
84 unsigned char opts
; /* maybe preset? */
85 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
86 int count
; /* # of messages */
87 int recent
; /* # of recent messages - don't trust this beyond the initial read */
96 static const char imap_send_usage
[] = "git imap-send < <mbox>";
100 #define DRV_MSG_BAD -1
101 #define DRV_BOX_BAD -2
102 #define DRV_STORE_BAD -3
104 static int Verbose
, Quiet
;
106 __attribute__((format (printf
, 1, 2)))
107 static void imap_info(const char *, ...);
108 __attribute__((format (printf
, 1, 2)))
109 static void imap_warn(const char *, ...);
111 static char *next_arg(char **);
113 static void free_generic_messages(struct message
*);
115 __attribute__((format (printf
, 3, 4)))
116 static int nfsnprintf(char *buf
, int blen
, const char *fmt
, ...);
118 static int nfvasprintf(char **strp
, const char *fmt
, va_list ap
)
123 len
= vsnprintf(tmp
, sizeof(tmp
), fmt
, ap
);
125 die("Fatal: Out of memory");
126 if (len
>= sizeof(tmp
))
127 die("imap command overflow!");
128 *strp
= xmemdupz(tmp
, len
);
132 struct imap_server_conf
{
144 struct imap_store_conf
{
145 struct store_conf gen
;
146 struct imap_server_conf
*server
;
147 unsigned use_namespace
:1;
150 #define NIL (void *)0x1
151 #define LIST (void *)0x2
154 struct imap_list
*next
, *child
;
165 struct imap_socket sock
;
174 int uidnext
; /* from SELECT responses */
175 struct imap_list
*ns_personal
, *ns_other
, *ns_shared
; /* NAMESPACE info */
176 unsigned caps
, rcaps
; /* CAPABILITY results */
178 int nexttag
, num_in_progress
, literal_pending
;
179 struct imap_cmd
*in_progress
, **in_progress_append
;
180 struct imap_buffer buf
; /* this is BIG, so put it last */
188 unsigned /*currentnc:1,*/ trashnc
:1;
192 int (*cont
)(struct imap_store
*ctx
, struct imap_cmd
*cmd
, const char *prompt
);
193 void (*done
)(struct imap_store
*ctx
, struct imap_cmd
*cmd
, int response
);
198 unsigned create
:1, trycreate
:1;
202 struct imap_cmd
*next
;
203 struct imap_cmd_cb cb
;
208 #define CAP(cap) (imap->caps & (1 << (cap)))
218 static const char *cap_list
[] = {
230 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
);
233 static const char *Flags
[] = {
242 static void ssl_socket_perror(const char *func
)
244 fprintf(stderr
, "%s: %s\n", func
, ERR_error_string(ERR_get_error(), NULL
));
248 static void socket_perror(const char *func
, struct imap_socket
*sock
, int ret
)
252 int sslerr
= SSL_get_error(sock
->ssl
, ret
);
256 case SSL_ERROR_SYSCALL
:
257 perror("SSL_connect");
260 ssl_socket_perror("SSL_connect");
269 fprintf(stderr
, "%s: unexpected EOF\n", func
);
273 static int ssl_socket_connect(struct imap_socket
*sock
, int use_tls_only
, int verify
)
276 fprintf(stderr
, "SSL requested but SSL support not compiled in\n");
279 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
280 const SSL_METHOD
*meth
;
288 SSL_load_error_strings();
291 meth
= TLSv1_method();
293 meth
= SSLv23_method();
296 ssl_socket_perror("SSLv23_method");
300 ctx
= SSL_CTX_new(meth
);
303 SSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, NULL
);
305 if (!SSL_CTX_set_default_verify_paths(ctx
)) {
306 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
309 sock
->ssl
= SSL_new(ctx
);
311 ssl_socket_perror("SSL_new");
314 if (!SSL_set_rfd(sock
->ssl
, sock
->fd
[0])) {
315 ssl_socket_perror("SSL_set_rfd");
318 if (!SSL_set_wfd(sock
->ssl
, sock
->fd
[1])) {
319 ssl_socket_perror("SSL_set_wfd");
323 ret
= SSL_connect(sock
->ssl
);
325 socket_perror("SSL_connect", sock
, ret
);
333 static int socket_read(struct imap_socket
*sock
, char *buf
, int len
)
338 n
= SSL_read(sock
->ssl
, buf
, len
);
341 n
= xread(sock
->fd
[0], buf
, len
);
343 socket_perror("read", sock
, n
);
346 sock
->fd
[0] = sock
->fd
[1] = -1;
351 static int socket_write(struct imap_socket
*sock
, const char *buf
, int len
)
356 n
= SSL_write(sock
->ssl
, buf
, len
);
359 n
= write_in_full(sock
->fd
[1], buf
, len
);
361 socket_perror("write", sock
, n
);
364 sock
->fd
[0] = sock
->fd
[1] = -1;
369 static void socket_shutdown(struct imap_socket
*sock
)
373 SSL_shutdown(sock
->ssl
);
381 /* simple line buffering */
382 static int buffer_gets(struct imap_buffer
*b
, char **s
)
385 int start
= b
->offset
;
390 /* make sure we have enough data to read the \r\n sequence */
391 if (b
->offset
+ 1 >= b
->bytes
) {
393 /* shift down used bytes */
396 assert(start
<= b
->bytes
);
397 n
= b
->bytes
- start
;
400 memmove(b
->buf
, b
->buf
+ start
, n
);
406 n
= socket_read(&b
->sock
, b
->buf
+ b
->bytes
,
407 sizeof(b
->buf
) - b
->bytes
);
415 if (b
->buf
[b
->offset
] == '\r') {
416 assert(b
->offset
+ 1 < b
->bytes
);
417 if (b
->buf
[b
->offset
+ 1] == '\n') {
418 b
->buf
[b
->offset
] = 0; /* terminate the string */
419 b
->offset
+= 2; /* next line */
431 static void imap_info(const char *msg
, ...)
443 static void imap_warn(const char *msg
, ...)
449 vfprintf(stderr
, msg
, va
);
454 static char *next_arg(char **s
)
460 while (isspace((unsigned char) **s
))
469 *s
= strchr(*s
, '"');
472 while (**s
&& !isspace((unsigned char) **s
))
484 static void free_generic_messages(struct message
*msgs
)
486 struct message
*tmsg
;
488 for (; msgs
; msgs
= tmsg
) {
494 static int nfsnprintf(char *buf
, int blen
, const char *fmt
, ...)
500 if (blen
<= 0 || (unsigned)(ret
= vsnprintf(buf
, blen
, fmt
, va
)) >= (unsigned)blen
)
501 die("Fatal: buffer too small. Please report a bug.");
506 static struct imap_cmd
*v_issue_imap_cmd(struct imap_store
*ctx
,
507 struct imap_cmd_cb
*cb
,
508 const char *fmt
, va_list ap
)
510 struct imap
*imap
= ctx
->imap
;
511 struct imap_cmd
*cmd
;
515 cmd
= xmalloc(sizeof(struct imap_cmd
));
516 nfvasprintf(&cmd
->cmd
, fmt
, ap
);
517 cmd
->tag
= ++imap
->nexttag
;
522 memset(&cmd
->cb
, 0, sizeof(cmd
->cb
));
524 while (imap
->literal_pending
)
525 get_cmd_result(ctx
, NULL
);
527 bufl
= nfsnprintf(buf
, sizeof(buf
), cmd
->cb
.data
? CAP(LITERALPLUS
) ?
528 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
529 cmd
->tag
, cmd
->cmd
, cmd
->cb
.dlen
);
531 if (imap
->num_in_progress
)
532 printf("(%d in progress) ", imap
->num_in_progress
);
533 if (memcmp(cmd
->cmd
, "LOGIN", 5))
534 printf(">>> %s", buf
);
536 printf(">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
538 if (socket_write(&imap
->buf
.sock
, buf
, bufl
) != bufl
) {
546 if (CAP(LITERALPLUS
)) {
547 n
= socket_write(&imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
549 if (n
!= cmd
->cb
.dlen
||
550 socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2) {
557 imap
->literal_pending
= 1;
558 } else if (cmd
->cb
.cont
)
559 imap
->literal_pending
= 1;
561 *imap
->in_progress_append
= cmd
;
562 imap
->in_progress_append
= &cmd
->next
;
563 imap
->num_in_progress
++;
567 __attribute__((format (printf
, 3, 4)))
568 static struct imap_cmd
*issue_imap_cmd(struct imap_store
*ctx
,
569 struct imap_cmd_cb
*cb
,
570 const char *fmt
, ...)
572 struct imap_cmd
*ret
;
576 ret
= v_issue_imap_cmd(ctx
, cb
, fmt
, ap
);
581 __attribute__((format (printf
, 3, 4)))
582 static int imap_exec(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
583 const char *fmt
, ...)
586 struct imap_cmd
*cmdp
;
589 cmdp
= v_issue_imap_cmd(ctx
, cb
, fmt
, ap
);
594 return get_cmd_result(ctx
, cmdp
);
597 __attribute__((format (printf
, 3, 4)))
598 static int imap_exec_m(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
599 const char *fmt
, ...)
602 struct imap_cmd
*cmdp
;
605 cmdp
= v_issue_imap_cmd(ctx
, cb
, fmt
, ap
);
608 return DRV_STORE_BAD
;
610 switch (get_cmd_result(ctx
, cmdp
)) {
611 case RESP_BAD
: return DRV_STORE_BAD
;
612 case RESP_NO
: return DRV_MSG_BAD
;
613 default: return DRV_OK
;
617 static int is_atom(struct imap_list
*list
)
619 return list
&& list
->val
&& list
->val
!= NIL
&& list
->val
!= LIST
;
622 static int is_list(struct imap_list
*list
)
624 return list
&& list
->val
== LIST
;
627 static void free_list(struct imap_list
*list
)
629 struct imap_list
*tmp
;
631 for (; list
; list
= tmp
) {
634 free_list(list
->child
);
635 else if (is_atom(list
))
641 static int parse_imap_list_l(struct imap
*imap
, char **sp
, struct imap_list
**curp
, int level
)
643 struct imap_list
*cur
;
648 while (isspace((unsigned char)*s
))
650 if (level
&& *s
== ')') {
654 *curp
= cur
= xmalloc(sizeof(*cur
));
656 cur
->val
= NULL
; /* for clean bail */
661 if (parse_imap_list_l(imap
, &s
, &cur
->child
, level
+ 1))
663 } else if (imap
&& *s
== '{') {
665 bytes
= cur
->len
= strtol(s
+ 1, &s
, 10);
669 s
= cur
->val
= xmalloc(cur
->len
);
671 /* dump whats left over in the input buffer */
672 n
= imap
->buf
.bytes
- imap
->buf
.offset
;
675 /* the entire message fit in the buffer */
678 memcpy(s
, imap
->buf
.buf
+ imap
->buf
.offset
, n
);
682 /* mark that we used part of the buffer */
683 imap
->buf
.offset
+= n
;
685 /* now read the rest of the message */
687 if ((n
= socket_read(&imap
->buf
.sock
, s
, bytes
)) <= 0)
693 if (buffer_gets(&imap
->buf
, &s
))
695 } else if (*s
== '"') {
699 for (; *s
!= '"'; s
++)
704 cur
->val
= xmemdupz(p
, cur
->len
);
708 for (; *s
&& !isspace((unsigned char)*s
); s
++)
709 if (level
&& *s
== ')')
712 if (cur
->len
== 3 && !memcmp("NIL", p
, 3))
715 cur
->val
= xmemdupz(p
, cur
->len
);
732 static struct imap_list
*parse_imap_list(struct imap
*imap
, char **sp
)
734 struct imap_list
*head
;
736 if (!parse_imap_list_l(imap
, sp
, &head
, 0))
742 static struct imap_list
*parse_list(char **sp
)
744 return parse_imap_list(NULL
, sp
);
747 static void parse_capability(struct imap
*imap
, char *cmd
)
752 imap
->caps
= 0x80000000;
753 while ((arg
= next_arg(&cmd
)))
754 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
755 if (!strcmp(cap_list
[i
], arg
))
756 imap
->caps
|= 1 << i
;
757 imap
->rcaps
= imap
->caps
;
760 static int parse_response_code(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
763 struct imap
*imap
= ctx
->imap
;
767 return RESP_OK
; /* no response code */
769 if (!(p
= strchr(s
, ']'))) {
770 fprintf(stderr
, "IMAP error: malformed response code\n");
775 if (!strcmp("UIDVALIDITY", arg
)) {
776 if (!(arg
= next_arg(&s
)) || !(ctx
->gen
.uidvalidity
= atoi(arg
))) {
777 fprintf(stderr
, "IMAP error: malformed UIDVALIDITY status\n");
780 } else if (!strcmp("UIDNEXT", arg
)) {
781 if (!(arg
= next_arg(&s
)) || !(imap
->uidnext
= atoi(arg
))) {
782 fprintf(stderr
, "IMAP error: malformed NEXTUID status\n");
785 } else if (!strcmp("CAPABILITY", arg
)) {
786 parse_capability(imap
, s
);
787 } else if (!strcmp("ALERT", arg
)) {
788 /* RFC2060 says that these messages MUST be displayed
791 for (; isspace((unsigned char)*p
); p
++);
792 fprintf(stderr
, "*** IMAP ALERT *** %s\n", p
);
793 } else if (cb
&& cb
->ctx
&& !strcmp("APPENDUID", arg
)) {
794 if (!(arg
= next_arg(&s
)) || !(ctx
->gen
.uidvalidity
= atoi(arg
)) ||
795 !(arg
= next_arg(&s
)) || !(*(int *)cb
->ctx
= atoi(arg
))) {
796 fprintf(stderr
, "IMAP error: malformed APPENDUID status\n");
803 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
)
805 struct imap
*imap
= ctx
->imap
;
806 struct imap_cmd
*cmdp
, **pcmdp
, *ncmdp
;
807 char *cmd
, *arg
, *arg1
, *p
;
808 int n
, resp
, resp2
, tag
;
811 if (buffer_gets(&imap
->buf
, &cmd
))
814 arg
= next_arg(&cmd
);
816 arg
= next_arg(&cmd
);
818 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
822 if (!strcmp("NAMESPACE", arg
)) {
823 imap
->ns_personal
= parse_list(&cmd
);
824 imap
->ns_other
= parse_list(&cmd
);
825 imap
->ns_shared
= parse_list(&cmd
);
826 } else if (!strcmp("OK", arg
) || !strcmp("BAD", arg
) ||
827 !strcmp("NO", arg
) || !strcmp("BYE", arg
)) {
828 if ((resp
= parse_response_code(ctx
, NULL
, cmd
)) != RESP_OK
)
830 } else if (!strcmp("CAPABILITY", arg
))
831 parse_capability(imap
, cmd
);
832 else if ((arg1
= next_arg(&cmd
))) {
833 if (!strcmp("EXISTS", arg1
))
834 ctx
->gen
.count
= atoi(arg
);
835 else if (!strcmp("RECENT", arg1
))
836 ctx
->gen
.recent
= atoi(arg
);
838 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
841 } else if (!imap
->in_progress
) {
842 fprintf(stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "");
844 } else if (*arg
== '+') {
845 /* This can happen only with the last command underway, as
846 it enforces a round-trip. */
847 cmdp
= (struct imap_cmd
*)((char *)imap
->in_progress_append
-
848 offsetof(struct imap_cmd
, next
));
850 n
= socket_write(&imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
852 cmdp
->cb
.data
= NULL
;
853 if (n
!= (int)cmdp
->cb
.dlen
)
855 } else if (cmdp
->cb
.cont
) {
856 if (cmdp
->cb
.cont(ctx
, cmdp
, cmd
))
859 fprintf(stderr
, "IMAP error: unexpected command continuation request\n");
862 if (socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2)
865 imap
->literal_pending
= 0;
870 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
871 if (cmdp
->tag
== tag
)
873 fprintf(stderr
, "IMAP error: unexpected tag %s\n", arg
);
876 if (!(*pcmdp
= cmdp
->next
))
877 imap
->in_progress_append
= pcmdp
;
878 imap
->num_in_progress
--;
879 if (cmdp
->cb
.cont
|| cmdp
->cb
.data
)
880 imap
->literal_pending
= 0;
881 arg
= next_arg(&cmd
);
882 if (!strcmp("OK", arg
))
885 if (!strcmp("NO", arg
)) {
886 if (cmdp
->cb
.create
&& cmd
&& (cmdp
->cb
.trycreate
|| !memcmp(cmd
, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
887 p
= strchr(cmdp
->cmd
, '"');
888 if (!issue_imap_cmd(ctx
, NULL
, "CREATE \"%.*s\"", (int)(strchr(p
+ 1, '"') - p
+ 1), p
)) {
892 /* not waiting here violates the spec, but a server that does not
893 grok this nonetheless violates it too. */
895 if (!(ncmdp
= issue_imap_cmd(ctx
, &cmdp
->cb
, "%s", cmdp
->cmd
))) {
902 return 0; /* ignored */
908 } else /*if (!strcmp("BAD", arg))*/
910 fprintf(stderr
, "IMAP command '%s' returned response (%s) - %s\n",
911 memcmp(cmdp
->cmd
, "LOGIN", 5) ?
912 cmdp
->cmd
: "LOGIN <user> <pass>",
913 arg
, cmd
? cmd
: "");
915 if ((resp2
= parse_response_code(ctx
, &cmdp
->cb
, cmd
)) > resp
)
919 cmdp
->cb
.done(ctx
, cmdp
, resp
);
923 if (!tcmd
|| tcmd
== cmdp
)
930 static void imap_close_server(struct imap_store
*ictx
)
932 struct imap
*imap
= ictx
->imap
;
934 if (imap
->buf
.sock
.fd
[0] != -1) {
935 imap_exec(ictx
, NULL
, "LOGOUT");
936 socket_shutdown(&imap
->buf
.sock
);
938 free_list(imap
->ns_personal
);
939 free_list(imap
->ns_other
);
940 free_list(imap
->ns_shared
);
944 static void imap_close_store(struct store
*ctx
)
946 imap_close_server((struct imap_store
*)ctx
);
947 free_generic_messages(ctx
->msgs
);
951 static struct store
*imap_open_store(struct imap_server_conf
*srvc
)
953 struct imap_store
*ctx
;
958 ctx
= xcalloc(sizeof(*ctx
), 1);
960 ctx
->imap
= imap
= xcalloc(sizeof(*imap
), 1);
961 imap
->buf
.sock
.fd
[0] = imap
->buf
.sock
.fd
[1] = -1;
962 imap
->in_progress_append
= &imap
->in_progress
;
964 /* open connection to IMAP server */
968 struct child_process tunnel
= {0};
970 imap_info("Starting tunnel '%s'... ", srvc
->tunnel
);
974 argv
[2] = srvc
->tunnel
;
980 if (start_command(&tunnel
))
981 die("cannot start proxy %s", argv
[0]);
983 imap
->buf
.sock
.fd
[0] = tunnel
.out
;
984 imap
->buf
.sock
.fd
[1] = tunnel
.in
;
989 struct addrinfo hints
, *ai0
, *ai
;
993 snprintf(portstr
, sizeof(portstr
), "%hu", srvc
->port
);
995 memset(&hints
, 0, sizeof(hints
));
996 hints
.ai_socktype
= SOCK_STREAM
;
997 hints
.ai_protocol
= IPPROTO_TCP
;
999 imap_info("Resolving %s... ", srvc
->host
);
1000 gai
= getaddrinfo(srvc
->host
, portstr
, &hints
, &ai
);
1002 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(gai
));
1007 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
1008 char addr
[NI_MAXHOST
];
1010 s
= socket(ai
->ai_family
, ai
->ai_socktype
,
1015 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, addr
,
1016 sizeof(addr
), NULL
, 0, NI_NUMERICHOST
);
1017 imap_info("Connecting to [%s]:%s... ", addr
, portstr
);
1019 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
1031 struct sockaddr_in addr
;
1033 memset(&addr
, 0, sizeof(addr
));
1034 addr
.sin_port
= htons(srvc
->port
);
1035 addr
.sin_family
= AF_INET
;
1037 imap_info("Resolving %s... ", srvc
->host
);
1038 he
= gethostbyname(srvc
->host
);
1040 perror("gethostbyname");
1045 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
1047 s
= socket(PF_INET
, SOCK_STREAM
, 0);
1049 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr
.sin_addr
), ntohs(addr
.sin_port
));
1050 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
1057 fputs("Error: unable to connect to server.\n", stderr
);
1061 imap
->buf
.sock
.fd
[0] = s
;
1062 imap
->buf
.sock
.fd
[1] = dup(s
);
1064 if (srvc
->use_ssl
&&
1065 ssl_socket_connect(&imap
->buf
.sock
, 0, srvc
->ssl_verify
)) {
1072 /* read the greeting string */
1073 if (buffer_gets(&imap
->buf
, &rsp
)) {
1074 fprintf(stderr
, "IMAP error: no greeting response\n");
1077 arg
= next_arg(&rsp
);
1078 if (!arg
|| *arg
!= '*' || (arg
= next_arg(&rsp
)) == NULL
) {
1079 fprintf(stderr
, "IMAP error: invalid greeting response\n");
1083 if (!strcmp("PREAUTH", arg
))
1085 else if (strcmp("OK", arg
) != 0) {
1086 fprintf(stderr
, "IMAP error: unknown greeting response\n");
1089 parse_response_code(ctx
, NULL
, rsp
);
1090 if (!imap
->caps
&& imap_exec(ctx
, NULL
, "CAPABILITY") != RESP_OK
)
1095 if (!srvc
->use_ssl
&& CAP(STARTTLS
)) {
1096 if (imap_exec(ctx
, 0, "STARTTLS") != RESP_OK
)
1098 if (ssl_socket_connect(&imap
->buf
.sock
, 1,
1101 /* capabilities may have changed, so get the new capabilities */
1102 if (imap_exec(ctx
, 0, "CAPABILITY") != RESP_OK
)
1106 imap_info("Logging in...\n");
1108 fprintf(stderr
, "Skipping server %s, no user\n", srvc
->host
);
1113 sprintf(prompt
, "Password (%s@%s): ", srvc
->user
, srvc
->host
);
1114 arg
= getpass(prompt
);
1120 fprintf(stderr
, "Skipping account %s@%s, no password\n", srvc
->user
, srvc
->host
);
1124 * getpass() returns a pointer to a static buffer. make a copy
1125 * for long term storage.
1127 srvc
->pass
= xstrdup(arg
);
1130 fprintf(stderr
, "Skipping account %s@%s, server forbids LOGIN\n", srvc
->user
, srvc
->host
);
1133 if (!imap
->buf
.sock
.ssl
)
1134 imap_warn("*** IMAP Warning *** Password is being "
1135 "sent in the clear\n");
1136 if (imap_exec(ctx
, NULL
, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1137 fprintf(stderr
, "IMAP error: LOGIN failed\n");
1144 return (struct store
*)ctx
;
1147 imap_close_store(&ctx
->gen
);
1151 static int imap_make_flags(int flags
, char *buf
)
1156 for (i
= d
= 0; i
< ARRAY_SIZE(Flags
); i
++)
1157 if (flags
& (1 << i
)) {
1160 for (s
= Flags
[i
]; *s
; s
++)
1168 static void lf_to_crlf(struct msg_data
*msg
)
1171 int i
, j
, lfnum
= 0;
1173 if (msg
->data
[0] == '\n')
1175 for (i
= 1; i
< msg
->len
; i
++) {
1176 if (msg
->data
[i
- 1] != '\r' && msg
->data
[i
] == '\n')
1180 new = xmalloc(msg
->len
+ lfnum
);
1181 if (msg
->data
[0] == '\n') {
1187 new[0] = msg
->data
[0];
1191 for ( ; i
< msg
->len
; i
++) {
1192 if (msg
->data
[i
] != '\n') {
1193 new[j
++] = msg
->data
[i
];
1196 if (msg
->data
[i
- 1] != '\r')
1198 /* otherwise it already had CR before */
1206 static int imap_store_msg(struct store
*gctx
, struct msg_data
*data
)
1208 struct imap_store
*ctx
= (struct imap_store
*)gctx
;
1209 struct imap
*imap
= ctx
->imap
;
1210 struct imap_cmd_cb cb
;
1211 const char *prefix
, *box
;
1216 memset(&cb
, 0, sizeof(cb
));
1218 cb
.dlen
= data
->len
;
1219 cb
.data
= xmalloc(cb
.dlen
);
1220 memcpy(cb
.data
, data
->data
, data
->len
);
1224 d
= imap_make_flags(data
->flags
, flagstr
);
1230 prefix
= !strcmp(box
, "INBOX") ? "" : ctx
->prefix
;
1232 ret
= imap_exec_m(ctx
, &cb
, "APPEND \"%s%s\" %s", prefix
, box
, flagstr
);
1233 imap
->caps
= imap
->rcaps
;
1241 static void encode_html_chars(struct strbuf
*p
)
1244 for (i
= 0; i
< p
->len
; i
++) {
1245 if (p
->buf
[i
] == '&')
1246 strbuf_splice(p
, i
, 1, "&", 5);
1247 if (p
->buf
[i
] == '<')
1248 strbuf_splice(p
, i
, 1, "<", 4);
1249 if (p
->buf
[i
] == '>')
1250 strbuf_splice(p
, i
, 1, ">", 4);
1251 if (p
->buf
[i
] == '"')
1252 strbuf_splice(p
, i
, 1, """, 6);
1255 static void wrap_in_html(struct msg_data
*msg
)
1257 struct strbuf buf
= STRBUF_INIT
;
1258 struct strbuf
**lines
;
1260 static char *content_type
= "Content-Type: text/html;\n";
1261 static char *pre_open
= "<pre>\n";
1262 static char *pre_close
= "</pre>\n";
1263 int added_header
= 0;
1265 strbuf_attach(&buf
, msg
->data
, msg
->len
, msg
->len
);
1266 lines
= strbuf_split(&buf
, '\n');
1267 strbuf_release(&buf
);
1268 for (p
= lines
; *p
; p
++) {
1269 if (! added_header
) {
1270 if ((*p
)->len
== 1 && *((*p
)->buf
) == '\n') {
1271 strbuf_addstr(&buf
, content_type
);
1272 strbuf_addbuf(&buf
, *p
);
1273 strbuf_addstr(&buf
, pre_open
);
1279 encode_html_chars(*p
);
1280 strbuf_addbuf(&buf
, *p
);
1282 strbuf_addstr(&buf
, pre_close
);
1283 strbuf_list_free(lines
);
1285 msg
->data
= strbuf_detach(&buf
, NULL
);
1288 #define CHUNKSIZE 0x1000
1290 static int read_message(FILE *f
, struct msg_data
*msg
)
1292 struct strbuf buf
= STRBUF_INIT
;
1294 memset(msg
, 0, sizeof(*msg
));
1297 if (strbuf_fread(&buf
, CHUNKSIZE
, f
) <= 0)
1302 msg
->data
= strbuf_detach(&buf
, NULL
);
1306 static int count_messages(struct msg_data
*msg
)
1309 char *p
= msg
->data
;
1312 if (!prefixcmp(p
, "From ")) {
1316 p
= strstr(p
+5, "\nFrom ");
1324 static int split_msg(struct msg_data
*all_msgs
, struct msg_data
*msg
, int *ofs
)
1328 memset(msg
, 0, sizeof *msg
);
1329 if (*ofs
>= all_msgs
->len
)
1332 data
= &all_msgs
->data
[*ofs
];
1333 msg
->len
= all_msgs
->len
- *ofs
;
1335 if (msg
->len
< 5 || prefixcmp(data
, "From "))
1338 p
= strchr(data
, '\n');
1346 p
= strstr(data
, "\nFrom ");
1348 msg
->len
= &p
[1] - data
;
1350 msg
->data
= xmemdupz(data
, msg
->len
);
1355 static struct imap_server_conf server
= {
1367 static char *imap_folder
;
1369 static int git_imap_config(const char *key
, const char *val
, void *cb
)
1371 char imap_key
[] = "imap.";
1373 if (strncmp(key
, imap_key
, sizeof imap_key
- 1))
1376 key
+= sizeof imap_key
- 1;
1378 /* check booleans first, and barf on others */
1379 if (!strcmp("sslverify", key
))
1380 server
.ssl_verify
= git_config_bool(key
, val
);
1381 else if (!strcmp("preformattedhtml", key
))
1382 server
.use_html
= git_config_bool(key
, val
);
1384 return config_error_nonbool(key
);
1386 if (!strcmp("folder", key
)) {
1387 imap_folder
= xstrdup(val
);
1388 } else if (!strcmp("host", key
)) {
1389 if (!prefixcmp(val
, "imap:"))
1391 else if (!prefixcmp(val
, "imaps:")) {
1395 if (!prefixcmp(val
, "//"))
1397 server
.host
= xstrdup(val
);
1398 } else if (!strcmp("user", key
))
1399 server
.user
= xstrdup(val
);
1400 else if (!strcmp("pass", key
))
1401 server
.pass
= xstrdup(val
);
1402 else if (!strcmp("port", key
))
1403 server
.port
= git_config_int(key
, val
);
1404 else if (!strcmp("tunnel", key
))
1405 server
.tunnel
= xstrdup(val
);
1409 int main(int argc
, char **argv
)
1411 struct msg_data all_msgs
, msg
;
1412 struct store
*ctx
= NULL
;
1418 git_extract_argv0_path(argv
[0]);
1421 usage(imap_send_usage
);
1423 setup_git_directory_gently(&nongit_ok
);
1424 git_config(git_imap_config
, NULL
);
1427 server
.port
= server
.use_ssl
? 993 : 143;
1430 fprintf(stderr
, "no imap store specified\n");
1434 if (!server
.tunnel
) {
1435 fprintf(stderr
, "no imap host specified\n");
1438 server
.host
= "tunnel";
1441 /* read the messages */
1442 if (!read_message(stdin
, &all_msgs
)) {
1443 fprintf(stderr
, "nothing to send\n");
1447 total
= count_messages(&all_msgs
);
1449 fprintf(stderr
, "no messages to send\n");
1453 /* write it to the imap server */
1454 ctx
= imap_open_store(&server
);
1456 fprintf(stderr
, "failed to open store\n");
1460 fprintf(stderr
, "sending %d message%s\n", total
, (total
!= 1) ? "s" : "");
1461 ctx
->name
= imap_folder
;
1463 unsigned percent
= n
* 100 / total
;
1464 fprintf(stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1465 if (!split_msg(&all_msgs
, &msg
, &ofs
))
1467 if (server
.use_html
)
1469 r
= imap_store_msg(ctx
, &msg
);
1474 fprintf(stderr
, "\n");
1476 imap_close_store(ctx
);