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 */
97 #define DRV_MSG_BAD -1
98 #define DRV_BOX_BAD -2
99 #define DRV_STORE_BAD -3
101 static int Verbose
, Quiet
;
103 static void imap_info(const char *, ...);
104 static void imap_warn(const char *, ...);
106 static char *next_arg(char **);
108 static void free_generic_messages(struct message
*);
110 static int nfsnprintf(char *buf
, int blen
, const char *fmt
, ...);
112 static int nfvasprintf(char **strp
, const char *fmt
, va_list ap
)
117 len
= vsnprintf(tmp
, sizeof(tmp
), fmt
, ap
);
119 die("Fatal: Out of memory");
120 if (len
>= sizeof(tmp
))
121 die("imap command overflow!");
122 *strp
= xmemdupz(tmp
, len
);
126 static void arc4_init(void);
127 static unsigned char arc4_getbyte(void);
129 struct imap_server_conf
{
140 struct imap_store_conf
{
141 struct store_conf gen
;
142 struct imap_server_conf
*server
;
143 unsigned use_namespace
:1;
146 #define NIL (void *)0x1
147 #define LIST (void *)0x2
150 struct imap_list
*next
, *child
;
161 struct imap_socket sock
;
170 int uidnext
; /* from SELECT responses */
171 struct imap_list
*ns_personal
, *ns_other
, *ns_shared
; /* NAMESPACE info */
172 unsigned caps
, rcaps
; /* CAPABILITY results */
174 int nexttag
, num_in_progress
, literal_pending
;
175 struct imap_cmd
*in_progress
, **in_progress_append
;
176 struct imap_buffer buf
; /* this is BIG, so put it last */
184 unsigned /*currentnc:1,*/ trashnc
:1;
188 int (*cont
)(struct imap_store
*ctx
, struct imap_cmd
*cmd
, const char *prompt
);
189 void (*done
)(struct imap_store
*ctx
, struct imap_cmd
*cmd
, int response
);
194 unsigned create
:1, trycreate
:1;
198 struct imap_cmd
*next
;
199 struct imap_cmd_cb cb
;
204 #define CAP(cap) (imap->caps & (1 << (cap)))
214 static const char *cap_list
[] = {
226 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
);
229 static const char *Flags
[] = {
238 static void ssl_socket_perror(const char *func
)
240 fprintf(stderr
, "%s: %s\n", func
, ERR_error_string(ERR_get_error(), 0));
244 static void socket_perror(const char *func
, struct imap_socket
*sock
, int ret
)
248 int sslerr
= SSL_get_error(sock
->ssl
, ret
);
252 case SSL_ERROR_SYSCALL
:
253 perror("SSL_connect");
256 ssl_socket_perror("SSL_connect");
265 fprintf(stderr
, "%s: unexpected EOF\n", func
);
269 static int ssl_socket_connect(struct imap_socket
*sock
, int use_tls_only
, int verify
)
272 fprintf(stderr
, "SSL requested but SSL support not compiled in\n");
280 SSL_load_error_strings();
283 meth
= TLSv1_method();
285 meth
= SSLv23_method();
288 ssl_socket_perror("SSLv23_method");
292 ctx
= SSL_CTX_new(meth
);
295 SSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, NULL
);
297 if (!SSL_CTX_set_default_verify_paths(ctx
)) {
298 ssl_socket_perror("SSL_CTX_set_default_verify_paths");
301 sock
->ssl
= SSL_new(ctx
);
303 ssl_socket_perror("SSL_new");
306 if (!SSL_set_fd(sock
->ssl
, sock
->fd
)) {
307 ssl_socket_perror("SSL_set_fd");
311 ret
= SSL_connect(sock
->ssl
);
313 socket_perror("SSL_connect", sock
, ret
);
321 static int socket_read(struct imap_socket
*sock
, char *buf
, int len
)
326 n
= SSL_read(sock
->ssl
, buf
, len
);
329 n
= xread(sock
->fd
, buf
, len
);
331 socket_perror("read", sock
, n
);
338 static int socket_write(struct imap_socket
*sock
, const char *buf
, int len
)
343 n
= SSL_write(sock
->ssl
, buf
, len
);
346 n
= write_in_full(sock
->fd
, buf
, len
);
348 socket_perror("write", sock
, n
);
355 static void socket_shutdown(struct imap_socket
*sock
)
359 SSL_shutdown(sock
->ssl
);
366 /* simple line buffering */
367 static int buffer_gets(struct imap_buffer
*b
, char **s
)
370 int start
= b
->offset
;
375 /* make sure we have enough data to read the \r\n sequence */
376 if (b
->offset
+ 1 >= b
->bytes
) {
378 /* shift down used bytes */
381 assert(start
<= b
->bytes
);
382 n
= b
->bytes
- start
;
385 memmove(b
->buf
, b
->buf
+ start
, n
);
391 n
= socket_read(&b
->sock
, b
->buf
+ b
->bytes
,
392 sizeof(b
->buf
) - b
->bytes
);
400 if (b
->buf
[b
->offset
] == '\r') {
401 assert(b
->offset
+ 1 < b
->bytes
);
402 if (b
->buf
[b
->offset
+ 1] == '\n') {
403 b
->buf
[b
->offset
] = 0; /* terminate the string */
404 b
->offset
+= 2; /* next line */
416 static void imap_info(const char *msg
, ...)
428 static void imap_warn(const char *msg
, ...)
434 vfprintf(stderr
, msg
, va
);
439 static char *next_arg(char **s
)
445 while (isspace((unsigned char) **s
))
454 *s
= strchr(*s
, '"');
457 while (**s
&& !isspace((unsigned char) **s
))
469 static void free_generic_messages(struct message
*msgs
)
471 struct message
*tmsg
;
473 for (; msgs
; msgs
= tmsg
) {
479 static int nfsnprintf(char *buf
, int blen
, const char *fmt
, ...)
485 if (blen
<= 0 || (unsigned)(ret
= vsnprintf(buf
, blen
, fmt
, va
)) >= (unsigned)blen
)
486 die("Fatal: buffer too small. Please report a bug.");
492 unsigned char i
, j
, s
[256];
495 static void arc4_init(void)
498 unsigned char j
, si
, dat
[128];
500 if ((fd
= open("/dev/urandom", O_RDONLY
)) < 0 && (fd
= open("/dev/random", O_RDONLY
)) < 0) {
501 fprintf(stderr
, "Fatal: no random number source available.\n");
504 if (read_in_full(fd
, dat
, 128) != 128) {
505 fprintf(stderr
, "Fatal: cannot read random number source.\n");
510 for (i
= 0; i
< 256; i
++)
512 for (i
= j
= 0; i
< 256; i
++) {
514 j
+= si
+ dat
[i
& 127];
520 for (i
= 0; i
< 256; i
++)
524 static unsigned char arc4_getbyte(void)
526 unsigned char si
, sj
;
534 return rs
.s
[(si
+ sj
) & 0xff];
537 static struct imap_cmd
*v_issue_imap_cmd(struct imap_store
*ctx
,
538 struct imap_cmd_cb
*cb
,
539 const char *fmt
, va_list ap
)
541 struct imap
*imap
= ctx
->imap
;
542 struct imap_cmd
*cmd
;
546 cmd
= xmalloc(sizeof(struct imap_cmd
));
547 nfvasprintf(&cmd
->cmd
, fmt
, ap
);
548 cmd
->tag
= ++imap
->nexttag
;
553 memset(&cmd
->cb
, 0, sizeof(cmd
->cb
));
555 while (imap
->literal_pending
)
556 get_cmd_result(ctx
, NULL
);
558 bufl
= nfsnprintf(buf
, sizeof(buf
), cmd
->cb
.data
? CAP(LITERALPLUS
) ?
559 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
560 cmd
->tag
, cmd
->cmd
, cmd
->cb
.dlen
);
562 if (imap
->num_in_progress
)
563 printf("(%d in progress) ", imap
->num_in_progress
);
564 if (memcmp(cmd
->cmd
, "LOGIN", 5))
565 printf(">>> %s", buf
);
567 printf(">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
569 if (socket_write(&imap
->buf
.sock
, buf
, bufl
) != bufl
) {
577 if (CAP(LITERALPLUS
)) {
578 n
= socket_write(&imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
580 if (n
!= cmd
->cb
.dlen
||
581 (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(struct imap_store
*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(struct imap_store
*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(struct imap_store
*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(struct imap_list
*list
)
647 return list
&& list
->val
&& list
->val
!= NIL
&& list
->val
!= LIST
;
650 static int is_list(struct imap_list
*list
)
652 return list
&& list
->val
== LIST
;
655 static void free_list(struct imap_list
*list
)
657 struct imap_list
*tmp
;
659 for (; list
; list
= tmp
) {
662 free_list(list
->child
);
663 else if (is_atom(list
))
669 static int parse_imap_list_l(struct imap
*imap
, char **sp
, struct imap_list
**curp
, int level
)
671 struct imap_list
*cur
;
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
);
760 static struct imap_list
*parse_imap_list(struct imap
*imap
, char **sp
)
762 struct imap_list
*head
;
764 if (!parse_imap_list_l(imap
, sp
, &head
, 0))
770 static struct imap_list
*parse_list(char **sp
)
772 return parse_imap_list(NULL
, sp
);
775 static void parse_capability(struct imap
*imap
, char *cmd
)
780 imap
->caps
= 0x80000000;
781 while ((arg
= next_arg(&cmd
)))
782 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
783 if (!strcmp(cap_list
[i
], arg
))
784 imap
->caps
|= 1 << i
;
785 imap
->rcaps
= imap
->caps
;
788 static int parse_response_code(struct imap_store
*ctx
, struct imap_cmd_cb
*cb
,
791 struct imap
*imap
= ctx
->imap
;
795 return RESP_OK
; /* no response code */
797 if (!(p
= strchr(s
, ']'))) {
798 fprintf(stderr
, "IMAP error: malformed response code\n");
803 if (!strcmp("UIDVALIDITY", arg
)) {
804 if (!(arg
= next_arg(&s
)) || !(ctx
->gen
.uidvalidity
= atoi(arg
))) {
805 fprintf(stderr
, "IMAP error: malformed UIDVALIDITY status\n");
808 } else if (!strcmp("UIDNEXT", arg
)) {
809 if (!(arg
= next_arg(&s
)) || !(imap
->uidnext
= atoi(arg
))) {
810 fprintf(stderr
, "IMAP error: malformed NEXTUID status\n");
813 } else if (!strcmp("CAPABILITY", arg
)) {
814 parse_capability(imap
, s
);
815 } else if (!strcmp("ALERT", arg
)) {
816 /* RFC2060 says that these messages MUST be displayed
819 for (; isspace((unsigned char)*p
); p
++);
820 fprintf(stderr
, "*** IMAP ALERT *** %s\n", p
);
821 } else if (cb
&& cb
->ctx
&& !strcmp("APPENDUID", arg
)) {
822 if (!(arg
= next_arg(&s
)) || !(ctx
->gen
.uidvalidity
= atoi(arg
)) ||
823 !(arg
= next_arg(&s
)) || !(*(int *)cb
->ctx
= atoi(arg
))) {
824 fprintf(stderr
, "IMAP error: malformed APPENDUID status\n");
831 static int get_cmd_result(struct imap_store
*ctx
, struct imap_cmd
*tcmd
)
833 struct imap
*imap
= ctx
->imap
;
834 struct imap_cmd
*cmdp
, **pcmdp
, *ncmdp
;
835 char *cmd
, *arg
, *arg1
, *p
;
836 int n
, resp
, resp2
, tag
;
839 if (buffer_gets(&imap
->buf
, &cmd
))
842 arg
= next_arg(&cmd
);
844 arg
= next_arg(&cmd
);
846 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
850 if (!strcmp("NAMESPACE", arg
)) {
851 imap
->ns_personal
= parse_list(&cmd
);
852 imap
->ns_other
= parse_list(&cmd
);
853 imap
->ns_shared
= parse_list(&cmd
);
854 } else if (!strcmp("OK", arg
) || !strcmp("BAD", arg
) ||
855 !strcmp("NO", arg
) || !strcmp("BYE", arg
)) {
856 if ((resp
= parse_response_code(ctx
, NULL
, cmd
)) != RESP_OK
)
858 } else if (!strcmp("CAPABILITY", arg
))
859 parse_capability(imap
, cmd
);
860 else if ((arg1
= next_arg(&cmd
))) {
861 if (!strcmp("EXISTS", arg1
))
862 ctx
->gen
.count
= atoi(arg
);
863 else if (!strcmp("RECENT", arg1
))
864 ctx
->gen
.recent
= atoi(arg
);
866 fprintf(stderr
, "IMAP error: unable to parse untagged response\n");
869 } else if (!imap
->in_progress
) {
870 fprintf(stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "");
872 } else if (*arg
== '+') {
873 /* This can happen only with the last command underway, as
874 it enforces a round-trip. */
875 cmdp
= (struct imap_cmd
*)((char *)imap
->in_progress_append
-
876 offsetof(struct imap_cmd
, next
));
878 n
= socket_write(&imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
880 cmdp
->cb
.data
= NULL
;
881 if (n
!= (int)cmdp
->cb
.dlen
)
883 } else if (cmdp
->cb
.cont
) {
884 if (cmdp
->cb
.cont(ctx
, cmdp
, cmd
))
887 fprintf(stderr
, "IMAP error: unexpected command continuation request\n");
890 if (socket_write(&imap
->buf
.sock
, "\r\n", 2) != 2)
893 imap
->literal_pending
= 0;
898 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
899 if (cmdp
->tag
== tag
)
901 fprintf(stderr
, "IMAP error: unexpected tag %s\n", arg
);
904 if (!(*pcmdp
= cmdp
->next
))
905 imap
->in_progress_append
= pcmdp
;
906 imap
->num_in_progress
--;
907 if (cmdp
->cb
.cont
|| cmdp
->cb
.data
)
908 imap
->literal_pending
= 0;
909 arg
= next_arg(&cmd
);
910 if (!strcmp("OK", arg
))
913 if (!strcmp("NO", arg
)) {
914 if (cmdp
->cb
.create
&& cmd
&& (cmdp
->cb
.trycreate
|| !memcmp(cmd
, "[TRYCREATE]", 11))) { /* SELECT, APPEND or UID COPY */
915 p
= strchr(cmdp
->cmd
, '"');
916 if (!issue_imap_cmd(ctx
, NULL
, "CREATE \"%.*s\"", strchr(p
+ 1, '"') - p
+ 1, p
)) {
920 /* not waiting here violates the spec, but a server that does not
921 grok this nonetheless violates it too. */
923 if (!(ncmdp
= issue_imap_cmd(ctx
, &cmdp
->cb
, "%s", cmdp
->cmd
))) {
930 return 0; /* ignored */
936 } else /*if (!strcmp("BAD", arg))*/
938 fprintf(stderr
, "IMAP command '%s' returned response (%s) - %s\n",
939 memcmp(cmdp
->cmd
, "LOGIN", 5) ?
940 cmdp
->cmd
: "LOGIN <user> <pass>",
941 arg
, cmd
? cmd
: "");
943 if ((resp2
= parse_response_code(ctx
, &cmdp
->cb
, cmd
)) > resp
)
947 cmdp
->cb
.done(ctx
, cmdp
, resp
);
951 if (!tcmd
|| tcmd
== cmdp
)
958 static void imap_close_server(struct imap_store
*ictx
)
960 struct imap
*imap
= ictx
->imap
;
962 if (imap
->buf
.sock
.fd
!= -1) {
963 imap_exec(ictx
, NULL
, "LOGOUT");
964 socket_shutdown(&imap
->buf
.sock
);
966 free_list(imap
->ns_personal
);
967 free_list(imap
->ns_other
);
968 free_list(imap
->ns_shared
);
972 static void imap_close_store(struct store
*ctx
)
974 imap_close_server((struct imap_store
*)ctx
);
975 free_generic_messages(ctx
->msgs
);
979 static struct store
*imap_open_store(struct imap_server_conf
*srvc
)
981 struct imap_store
*ctx
;
985 struct sockaddr_in addr
;
986 int s
, a
[2], preauth
;
989 ctx
= xcalloc(sizeof(*ctx
), 1);
991 ctx
->imap
= imap
= xcalloc(sizeof(*imap
), 1);
992 imap
->buf
.sock
.fd
= -1;
993 imap
->in_progress_append
= &imap
->in_progress
;
995 /* open connection to IMAP server */
998 imap_info("Starting tunnel '%s'... ", srvc
->tunnel
);
1000 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, a
)) {
1001 perror("socketpair");
1009 if (dup2(a
[0], 0) == -1 || dup2(a
[0], 1) == -1)
1013 execl("/bin/sh", "sh", "-c", srvc
->tunnel
, NULL
);
1019 imap
->buf
.sock
.fd
= a
[1];
1023 memset(&addr
, 0, sizeof(addr
));
1024 addr
.sin_port
= htons(srvc
->port
);
1025 addr
.sin_family
= AF_INET
;
1027 imap_info("Resolving %s... ", srvc
->host
);
1028 he
= gethostbyname(srvc
->host
);
1030 perror("gethostbyname");
1035 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
1037 s
= socket(PF_INET
, SOCK_STREAM
, 0);
1039 imap_info("Connecting to %s:%hu... ", inet_ntoa(addr
.sin_addr
), ntohs(addr
.sin_port
));
1040 if (connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
))) {
1046 imap
->buf
.sock
.fd
= s
;
1048 if (srvc
->use_ssl
&&
1049 ssl_socket_connect(&imap
->buf
.sock
, 0, srvc
->ssl_verify
)) {
1056 /* read the greeting string */
1057 if (buffer_gets(&imap
->buf
, &rsp
)) {
1058 fprintf(stderr
, "IMAP error: no greeting response\n");
1061 arg
= next_arg(&rsp
);
1062 if (!arg
|| *arg
!= '*' || (arg
= next_arg(&rsp
)) == NULL
) {
1063 fprintf(stderr
, "IMAP error: invalid greeting response\n");
1067 if (!strcmp("PREAUTH", arg
))
1069 else if (strcmp("OK", arg
) != 0) {
1070 fprintf(stderr
, "IMAP error: unknown greeting response\n");
1073 parse_response_code(ctx
, NULL
, rsp
);
1074 if (!imap
->caps
&& imap_exec(ctx
, NULL
, "CAPABILITY") != RESP_OK
)
1079 if (!srvc
->use_ssl
&& CAP(STARTTLS
)) {
1080 if (imap_exec(ctx
, 0, "STARTTLS") != RESP_OK
)
1082 if (ssl_socket_connect(&imap
->buf
.sock
, 1,
1085 /* capabilities may have changed, so get the new capabilities */
1086 if (imap_exec(ctx
, 0, "CAPABILITY") != RESP_OK
)
1090 imap_info("Logging in...\n");
1092 fprintf(stderr
, "Skipping server %s, no user\n", srvc
->host
);
1097 sprintf(prompt
, "Password (%s@%s): ", srvc
->user
, srvc
->host
);
1098 arg
= getpass(prompt
);
1104 fprintf(stderr
, "Skipping account %s@%s, no password\n", srvc
->user
, srvc
->host
);
1108 * getpass() returns a pointer to a static buffer. make a copy
1109 * for long term storage.
1111 srvc
->pass
= xstrdup(arg
);
1114 fprintf(stderr
, "Skipping account %s@%s, server forbids LOGIN\n", srvc
->user
, srvc
->host
);
1117 if (!imap
->buf
.sock
.ssl
)
1118 imap_warn("*** IMAP Warning *** Password is being "
1119 "sent in the clear\n");
1120 if (imap_exec(ctx
, NULL
, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1121 fprintf(stderr
, "IMAP error: LOGIN failed\n");
1128 return (struct store
*)ctx
;
1131 imap_close_store(&ctx
->gen
);
1135 static int imap_make_flags(int flags
, char *buf
)
1140 for (i
= d
= 0; i
< ARRAY_SIZE(Flags
); i
++)
1141 if (flags
& (1 << i
)) {
1144 for (s
= Flags
[i
]; *s
; s
++)
1154 static int imap_store_msg(struct store
*gctx
, struct msg_data
*data
, int *uid
)
1156 struct imap_store
*ctx
= (struct imap_store
*)gctx
;
1157 struct imap
*imap
= ctx
->imap
;
1158 struct imap_cmd_cb cb
;
1160 const char *prefix
, *box
;
1161 int ret
, i
, j
, d
, len
, extra
, nocr
;
1162 int start
, sbreak
= 0, ebreak
= 0;
1163 char flagstr
[128], tuid
[TUIDL
* 2 + 1];
1165 memset(&cb
, 0, sizeof(cb
));
1171 if (!CAP(UIDPLUS
) && uid
) {
1175 if (fmap
[i
++] == '\n') {
1177 if (i
- 2 + nocr
== start
) {
1178 sbreak
= ebreak
= i
- 2 + nocr
;
1181 if (!memcmp(fmap
+ start
, "X-TUID: ", 8)) {
1182 extra
-= (ebreak
= i
) - (sbreak
= start
) + nocr
;
1187 /* invalid message */
1191 for (j
= 0; j
< TUIDL
; j
++)
1192 sprintf(tuid
+ j
* 2, "%02x", arc4_getbyte());
1193 extra
+= 8 + TUIDL
* 2 + 2;
1196 for (; i
< len
; i
++)
1197 if (fmap
[i
] == '\n')
1200 cb
.dlen
= len
+ extra
;
1201 buf
= cb
.data
= xmalloc(cb
.dlen
);
1203 if (!CAP(UIDPLUS
) && uid
) {
1205 for (; i
< sbreak
; i
++)
1206 if (fmap
[i
] == '\n') {
1212 memcpy(buf
, fmap
, sbreak
);
1215 memcpy(buf
, "X-TUID: ", 8);
1217 memcpy(buf
, tuid
, TUIDL
* 2);
1224 for (; i
< len
; i
++)
1225 if (fmap
[i
] == '\n') {
1231 memcpy(buf
, fmap
+ i
, len
- i
);
1237 d
= imap_make_flags(data
->flags
, flagstr
);
1243 box
= gctx
->conf
->trash
;
1244 prefix
= ctx
->prefix
;
1247 imap
->caps
= imap
->rcaps
& ~(1 << LITERALPLUS
);
1250 prefix
= !strcmp(box
, "INBOX") ? "" : ctx
->prefix
;
1254 ret
= imap_exec_m(ctx
, &cb
, "APPEND \"%s%s\" %s", prefix
, box
, flagstr
);
1255 imap
->caps
= imap
->rcaps
;
1266 #define CHUNKSIZE 0x1000
1268 static int read_message(FILE *f
, struct msg_data
*msg
)
1270 struct strbuf buf
= STRBUF_INIT
;
1272 memset(msg
, 0, sizeof(*msg
));
1275 if (strbuf_fread(&buf
, CHUNKSIZE
, f
) <= 0)
1280 msg
->data
= strbuf_detach(&buf
, NULL
);
1284 static int count_messages(struct msg_data
*msg
)
1287 char *p
= msg
->data
;
1290 if (!prefixcmp(p
, "From ")) {
1294 p
= strstr(p
+5, "\nFrom ");
1302 static int split_msg(struct msg_data
*all_msgs
, struct msg_data
*msg
, int *ofs
)
1306 memset(msg
, 0, sizeof *msg
);
1307 if (*ofs
>= all_msgs
->len
)
1310 data
= &all_msgs
->data
[*ofs
];
1311 msg
->len
= all_msgs
->len
- *ofs
;
1313 if (msg
->len
< 5 || prefixcmp(data
, "From "))
1316 p
= strchr(data
, '\n');
1324 p
= strstr(data
, "\nFrom ");
1326 msg
->len
= &p
[1] - data
;
1328 msg
->data
= xmemdupz(data
, msg
->len
);
1333 static struct imap_server_conf server
= {
1344 static char *imap_folder
;
1346 static int git_imap_config(const char *key
, const char *val
, void *cb
)
1348 char imap_key
[] = "imap.";
1350 if (strncmp(key
, imap_key
, sizeof imap_key
- 1))
1354 return config_error_nonbool(key
);
1356 key
+= sizeof imap_key
- 1;
1358 if (!strcmp("folder", key
)) {
1359 imap_folder
= xstrdup(val
);
1360 } else if (!strcmp("host", key
)) {
1361 if (!prefixcmp(val
, "imap:"))
1363 else if (!prefixcmp(val
, "imaps:")) {
1367 if (!prefixcmp(val
, "//"))
1369 server
.host
= xstrdup(val
);
1370 } else if (!strcmp("user", key
))
1371 server
.user
= xstrdup(val
);
1372 else if (!strcmp("pass", key
))
1373 server
.pass
= xstrdup(val
);
1374 else if (!strcmp("port", key
))
1375 server
.port
= git_config_int(key
, val
);
1376 else if (!strcmp("tunnel", key
))
1377 server
.tunnel
= xstrdup(val
);
1378 else if (!strcmp("sslverify", key
))
1379 server
.ssl_verify
= git_config_bool(key
, val
);
1383 int main(int argc
, char **argv
)
1385 struct msg_data all_msgs
, msg
;
1386 struct store
*ctx
= NULL
;
1393 git_extract_argv0_path(argv
[0]);
1395 /* init the random number generator */
1398 setup_git_directory_gently(&nongit_ok
);
1399 git_config(git_imap_config
, NULL
);
1402 server
.port
= server
.use_ssl
? 993 : 143;
1405 fprintf(stderr
, "no imap store specified\n");
1409 if (!server
.tunnel
) {
1410 fprintf(stderr
, "no imap host specified\n");
1413 server
.host
= "tunnel";
1416 /* read the messages */
1417 if (!read_message(stdin
, &all_msgs
)) {
1418 fprintf(stderr
, "nothing to send\n");
1422 total
= count_messages(&all_msgs
);
1424 fprintf(stderr
, "no messages to send\n");
1428 /* write it to the imap server */
1429 ctx
= imap_open_store(&server
);
1431 fprintf(stderr
, "failed to open store\n");
1435 fprintf(stderr
, "sending %d message%s\n", total
, (total
!= 1) ? "s" : "");
1436 ctx
->name
= imap_folder
;
1438 unsigned percent
= n
* 100 / total
;
1439 fprintf(stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1440 if (!split_msg(&all_msgs
, &msg
, &ofs
))
1442 r
= imap_store_msg(ctx
, &msg
, &uid
);
1447 fprintf(stderr
, "\n");
1449 imap_close_store(ctx
);