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
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <arpa/inet.h>
31 #include <sys/socket.h>
34 typedef struct store_conf
{
36 const char *path
; /* should this be here? its interpretation is driver-specific */
39 unsigned max_size
; /* off_t is overkill */
40 unsigned trash_remote_new
:1, trash_only_new
:1;
43 typedef struct string_list
{
44 struct string_list
*next
;
48 typedef struct channel_conf
{
49 struct channel_conf
*next
;
51 store_conf_t
*master
, *slave
;
52 char *master_name
, *slave_name
;
54 string_list_t
*patterns
;
56 unsigned max_messages
; /* for slave only */
59 typedef struct group_conf
{
60 struct group_conf
*next
;
62 string_list_t
*channels
;
65 /* For message->status */
66 #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
67 #define M_DEAD (1<<1) /* expunged */
68 #define M_FLAGS (1<<2) /* flags fetched */
70 typedef struct message
{
72 /* string_list_t *keywords; */
73 size_t size
; /* zero implies "not fetched" */
75 unsigned char flags
, status
;
78 typedef struct store
{
79 store_conf_t
*conf
; /* foreign */
81 /* currently open mailbox */
82 const char *name
; /* foreign! maybe preset? */
84 message_t
*msgs
; /* own */
86 unsigned char opts
; /* maybe preset? */
87 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
88 int count
; /* # of messages */
89 int recent
; /* # of recent messages - don't trust this beyond the initial read */
100 #define DRV_MSG_BAD -1
101 #define DRV_BOX_BAD -2
102 #define DRV_STORE_BAD -3
104 static int Verbose
, Quiet
;
106 static void info( const char *, ... );
107 static void warn( const char *, ... );
109 static char *next_arg( char ** );
111 static void free_generic_messages( message_t
* );
113 static int nfvasprintf( char **str
, const char *fmt
, va_list va
);
114 static int nfsnprintf( char *buf
, int blen
, const char *fmt
, ... );
117 static void arc4_init( void );
118 static unsigned char arc4_getbyte( void );
120 typedef struct imap_server_conf
{
127 } imap_server_conf_t
;
129 typedef struct imap_store_conf
{
131 imap_server_conf_t
*server
;
132 unsigned use_namespace
:1;
135 #define NIL (void*)0x1
136 #define LIST (void*)0x2
138 typedef struct _list
{
139 struct _list
*next
, *child
;
157 typedef struct imap
{
158 int uidnext
; /* from SELECT responses */
159 list_t
*ns_personal
, *ns_other
, *ns_shared
; /* NAMESPACE info */
160 unsigned caps
, rcaps
; /* CAPABILITY results */
162 int nexttag
, num_in_progress
, literal_pending
;
163 struct imap_cmd
*in_progress
, **in_progress_append
;
164 buffer_t buf
; /* this is BIG, so put it last */
167 typedef struct imap_store
{
172 unsigned /*currentnc:1,*/ trashnc
:1;
176 int (*cont
)( imap_store_t
*ctx
, struct imap_cmd
*cmd
, const char *prompt
);
177 void (*done
)( imap_store_t
*ctx
, struct imap_cmd
*cmd
, int response
);
182 unsigned create
:1, trycreate
:1;
186 struct imap_cmd
*next
;
187 struct imap_cmd_cb cb
;
192 #define CAP(cap) (imap->caps & (1 << (cap)))
201 static const char *cap_list
[] = {
212 static int get_cmd_result( imap_store_t
*ctx
, struct imap_cmd
*tcmd
);
215 static const char *Flags
[] = {
224 socket_perror( const char *func
, Socket_t
*sock
, int ret
)
229 fprintf( stderr
, "%s: unexpected EOF\n", func
);
233 socket_read( Socket_t
*sock
, char *buf
, int len
)
235 int n
= read( sock
->fd
, buf
, len
);
237 socket_perror( "read", sock
, n
);
245 socket_write( Socket_t
*sock
, char *buf
, int len
)
247 int n
= write( sock
->fd
, buf
, len
);
249 socket_perror( "write", sock
, n
);
256 /* simple line buffering */
258 buffer_gets( buffer_t
* b
, char **s
)
261 int start
= b
->offset
;
266 /* make sure we have enough data to read the \r\n sequence */
267 if (b
->offset
+ 1 >= b
->bytes
) {
269 /* shift down used bytes */
272 assert( start
<= b
->bytes
);
273 n
= b
->bytes
- start
;
276 memcpy( b
->buf
, b
->buf
+ start
, n
);
282 n
= socket_read( &b
->sock
, b
->buf
+ b
->bytes
,
283 sizeof(b
->buf
) - b
->bytes
);
291 if (b
->buf
[b
->offset
] == '\r') {
292 assert( b
->offset
+ 1 < b
->bytes
);
293 if (b
->buf
[b
->offset
+ 1] == '\n') {
294 b
->buf
[b
->offset
] = 0; /* terminate the string */
295 b
->offset
+= 2; /* next line */
308 info( const char *msg
, ... )
321 warn( const char *msg
, ... )
327 vfprintf( stderr
, msg
, va
);
339 while (isspace( (unsigned char) **s
))
348 *s
= strchr( *s
, '"' );
351 while (**s
&& !isspace( (unsigned char) **s
))
364 free_generic_messages( message_t
*msgs
)
368 for (; msgs
; msgs
= tmsg
) {
375 vasprintf( char **strp
, const char *fmt
, va_list ap
)
380 if ((len
= vsnprintf( tmp
, sizeof(tmp
), fmt
, ap
)) < 0 || !(*strp
= xmalloc( len
+ 1 )))
382 if (len
>= (int)sizeof(tmp
))
383 vsprintf( *strp
, fmt
, ap
);
385 memcpy( *strp
, tmp
, len
+ 1 );
390 nfsnprintf( char *buf
, int blen
, const char *fmt
, ... )
396 if (blen
<= 0 || (unsigned)(ret
= vsnprintf( buf
, blen
, fmt
, va
)) >= (unsigned)blen
)
397 die( "Fatal: buffer too small. Please report a bug.\n");
403 nfvasprintf( char **str
, const char *fmt
, va_list va
)
405 int ret
= vasprintf( str
, fmt
, va
);
407 die( "Fatal: Out of memory\n");
412 unsigned char i
, j
, s
[256];
419 unsigned char j
, si
, dat
[128];
421 if ((fd
= open( "/dev/urandom", O_RDONLY
)) < 0 && (fd
= open( "/dev/random", O_RDONLY
)) < 0) {
422 fprintf( stderr
, "Fatal: no random number source available.\n" );
425 if (read( fd
, dat
, 128 ) != 128) {
426 fprintf( stderr
, "Fatal: cannot read random number source.\n" );
431 for (i
= 0; i
< 256; i
++)
433 for (i
= j
= 0; i
< 256; i
++) {
435 j
+= si
+ dat
[i
& 127];
441 for (i
= 0; i
< 256; i
++)
448 unsigned char si
, sj
;
456 return rs
.s
[(si
+ sj
) & 0xff];
459 static struct imap_cmd
*
460 v_issue_imap_cmd( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
,
461 const char *fmt
, va_list ap
)
463 imap_t
*imap
= ctx
->imap
;
464 struct imap_cmd
*cmd
;
468 cmd
= xmalloc( sizeof(struct imap_cmd
) );
469 nfvasprintf( &cmd
->cmd
, fmt
, ap
);
470 cmd
->tag
= ++imap
->nexttag
;
475 memset( &cmd
->cb
, 0, sizeof(cmd
->cb
) );
477 while (imap
->literal_pending
)
478 get_cmd_result( ctx
, 0 );
480 bufl
= nfsnprintf( buf
, sizeof(buf
), cmd
->cb
.data
? CAP(LITERALPLUS
) ?
481 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
482 cmd
->tag
, cmd
->cmd
, cmd
->cb
.dlen
);
484 if (imap
->num_in_progress
)
485 printf( "(%d in progress) ", imap
->num_in_progress
);
486 if (memcmp( cmd
->cmd
, "LOGIN", 5 ))
487 printf( ">>> %s", buf
);
489 printf( ">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
491 if (socket_write( &imap
->buf
.sock
, buf
, bufl
) != bufl
) {
499 if (CAP(LITERALPLUS
)) {
500 n
= socket_write( &imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
501 free( cmd
->cb
.data
);
502 if (n
!= cmd
->cb
.dlen
||
503 (n
= socket_write( &imap
->buf
.sock
, "\r\n", 2 )) != 2)
511 imap
->literal_pending
= 1;
512 } else if (cmd
->cb
.cont
)
513 imap
->literal_pending
= 1;
515 *imap
->in_progress_append
= cmd
;
516 imap
->in_progress_append
= &cmd
->next
;
517 imap
->num_in_progress
++;
521 static struct imap_cmd
*
522 issue_imap_cmd( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
524 struct imap_cmd
*ret
;
528 ret
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
534 imap_exec( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
537 struct imap_cmd
*cmdp
;
540 cmdp
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
545 return get_cmd_result( ctx
, cmdp
);
549 imap_exec_m( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
552 struct imap_cmd
*cmdp
;
555 cmdp
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
558 return DRV_STORE_BAD
;
560 switch (get_cmd_result( ctx
, cmdp
)) {
561 case RESP_BAD
: return DRV_STORE_BAD
;
562 case RESP_NO
: return DRV_MSG_BAD
;
563 default: return DRV_OK
;
568 is_atom( list_t
*list
)
570 return list
&& list
->val
&& list
->val
!= NIL
&& list
->val
!= LIST
;
574 is_list( list_t
*list
)
576 return list
&& list
->val
== LIST
;
580 free_list( list_t
*list
)
584 for (; list
; list
= tmp
) {
587 free_list( list
->child
);
588 else if (is_atom( list
))
595 parse_imap_list_l( imap_t
*imap
, char **sp
, list_t
**curp
, int level
)
602 while (isspace( (unsigned char)*s
))
604 if (level
&& *s
== ')') {
608 *curp
= cur
= xmalloc( sizeof(*cur
) );
610 cur
->val
= 0; /* for clean bail */
615 if (parse_imap_list_l( imap
, &s
, &cur
->child
, level
+ 1 ))
617 } else if (imap
&& *s
== '{') {
619 bytes
= cur
->len
= strtol( s
+ 1, &s
, 10 );
623 s
= cur
->val
= xmalloc( cur
->len
);
625 /* dump whats left over in the input buffer */
626 n
= imap
->buf
.bytes
- imap
->buf
.offset
;
629 /* the entire message fit in the buffer */
632 memcpy( s
, imap
->buf
.buf
+ imap
->buf
.offset
, n
);
636 /* mark that we used part of the buffer */
637 imap
->buf
.offset
+= n
;
639 /* now read the rest of the message */
641 if ((n
= socket_read (&imap
->buf
.sock
, s
, bytes
)) <= 0)
647 if (buffer_gets( &imap
->buf
, &s
))
649 } else if (*s
== '"') {
653 for (; *s
!= '"'; s
++)
658 cur
->val
= xmalloc( cur
->len
+ 1 );
659 memcpy( cur
->val
, p
, cur
->len
);
660 cur
->val
[cur
->len
] = 0;
664 for (; *s
&& !isspace( (unsigned char)*s
); s
++)
665 if (level
&& *s
== ')')
668 if (cur
->len
== 3 && !memcmp ("NIL", p
, 3))
671 cur
->val
= xmalloc( cur
->len
+ 1 );
672 memcpy( cur
->val
, p
, cur
->len
);
673 cur
->val
[cur
->len
] = 0;
692 parse_imap_list( imap_t
*imap
, char **sp
)
696 if (!parse_imap_list_l( imap
, sp
, &head
, 0 ))
703 parse_list( char **sp
)
705 return parse_imap_list( 0, sp
);
709 parse_capability( imap_t
*imap
, char *cmd
)
714 imap
->caps
= 0x80000000;
715 while ((arg
= next_arg( &cmd
)))
716 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
717 if (!strcmp( cap_list
[i
], arg
))
718 imap
->caps
|= 1 << i
;
719 imap
->rcaps
= imap
->caps
;
723 parse_response_code( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, char *s
)
725 imap_t
*imap
= ctx
->imap
;
729 return RESP_OK
; /* no response code */
731 if (!(p
= strchr( s
, ']' ))) {
732 fprintf( stderr
, "IMAP error: malformed response code\n" );
736 arg
= next_arg( &s
);
737 if (!strcmp( "UIDVALIDITY", arg
)) {
738 if (!(arg
= next_arg( &s
)) || !(ctx
->gen
.uidvalidity
= atoi( arg
))) {
739 fprintf( stderr
, "IMAP error: malformed UIDVALIDITY status\n" );
742 } else if (!strcmp( "UIDNEXT", arg
)) {
743 if (!(arg
= next_arg( &s
)) || !(imap
->uidnext
= atoi( arg
))) {
744 fprintf( stderr
, "IMAP error: malformed NEXTUID status\n" );
747 } else if (!strcmp( "CAPABILITY", arg
)) {
748 parse_capability( imap
, s
);
749 } else if (!strcmp( "ALERT", arg
)) {
750 /* RFC2060 says that these messages MUST be displayed
753 for (; isspace( (unsigned char)*p
); p
++);
754 fprintf( stderr
, "*** IMAP ALERT *** %s\n", p
);
755 } else if (cb
&& cb
->ctx
&& !strcmp( "APPENDUID", arg
)) {
756 if (!(arg
= next_arg( &s
)) || !(ctx
->gen
.uidvalidity
= atoi( arg
)) ||
757 !(arg
= next_arg( &s
)) || !(*(int *)cb
->ctx
= atoi( arg
)))
759 fprintf( stderr
, "IMAP error: malformed APPENDUID status\n" );
767 get_cmd_result( imap_store_t
*ctx
, struct imap_cmd
*tcmd
)
769 imap_t
*imap
= ctx
->imap
;
770 struct imap_cmd
*cmdp
, **pcmdp
, *ncmdp
;
771 char *cmd
, *arg
, *arg1
, *p
;
772 int n
, resp
, resp2
, tag
;
775 if (buffer_gets( &imap
->buf
, &cmd
))
778 arg
= next_arg( &cmd
);
780 arg
= next_arg( &cmd
);
782 fprintf( stderr
, "IMAP error: unable to parse untagged response\n" );
786 if (!strcmp( "NAMESPACE", arg
)) {
787 imap
->ns_personal
= parse_list( &cmd
);
788 imap
->ns_other
= parse_list( &cmd
);
789 imap
->ns_shared
= parse_list( &cmd
);
790 } else if (!strcmp( "OK", arg
) || !strcmp( "BAD", arg
) ||
791 !strcmp( "NO", arg
) || !strcmp( "BYE", arg
)) {
792 if ((resp
= parse_response_code( ctx
, 0, cmd
)) != RESP_OK
)
794 } else if (!strcmp( "CAPABILITY", arg
))
795 parse_capability( imap
, cmd
);
796 else if ((arg1
= next_arg( &cmd
))) {
797 if (!strcmp( "EXISTS", arg1
))
798 ctx
->gen
.count
= atoi( arg
);
799 else if (!strcmp( "RECENT", arg1
))
800 ctx
->gen
.recent
= atoi( arg
);
802 fprintf( stderr
, "IMAP error: unable to parse untagged response\n" );
805 } else if (!imap
->in_progress
) {
806 fprintf( stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "" );
808 } else if (*arg
== '+') {
809 /* This can happen only with the last command underway, as
810 it enforces a round-trip. */
811 cmdp
= (struct imap_cmd
*)((char *)imap
->in_progress_append
-
812 offsetof(struct imap_cmd
, next
));
814 n
= socket_write( &imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
815 free( cmdp
->cb
.data
);
817 if (n
!= (int)cmdp
->cb
.dlen
)
819 } else if (cmdp
->cb
.cont
) {
820 if (cmdp
->cb
.cont( ctx
, cmdp
, cmd
))
823 fprintf( stderr
, "IMAP error: unexpected command continuation request\n" );
826 if (socket_write( &imap
->buf
.sock
, "\r\n", 2 ) != 2)
829 imap
->literal_pending
= 0;
834 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
835 if (cmdp
->tag
== tag
)
837 fprintf( stderr
, "IMAP error: unexpected tag %s\n", arg
);
840 if (!(*pcmdp
= cmdp
->next
))
841 imap
->in_progress_append
= pcmdp
;
842 imap
->num_in_progress
--;
843 if (cmdp
->cb
.cont
|| cmdp
->cb
.data
)
844 imap
->literal_pending
= 0;
845 arg
= next_arg( &cmd
);
846 if (!strcmp( "OK", arg
))
849 if (!strcmp( "NO", arg
)) {
850 if (cmdp
->cb
.create
&& cmd
&& (cmdp
->cb
.trycreate
|| !memcmp( cmd
, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
851 p
= strchr( cmdp
->cmd
, '"' );
852 if (!issue_imap_cmd( ctx
, 0, "CREATE \"%.*s\"", strchr( p
+ 1, '"' ) - p
+ 1, p
)) {
856 /* not waiting here violates the spec, but a server that does not
857 grok this nonetheless violates it too. */
859 if (!(ncmdp
= issue_imap_cmd( ctx
, &cmdp
->cb
, "%s", cmdp
->cmd
))) {
866 return 0; /* ignored */
872 } else /*if (!strcmp( "BAD", arg ))*/
874 fprintf( stderr
, "IMAP command '%s' returned response (%s) - %s\n",
875 memcmp (cmdp
->cmd
, "LOGIN", 5) ?
876 cmdp
->cmd
: "LOGIN <user> <pass>",
877 arg
, cmd
? cmd
: "");
879 if ((resp2
= parse_response_code( ctx
, &cmdp
->cb
, cmd
)) > resp
)
883 cmdp
->cb
.done( ctx
, cmdp
, resp
);
885 free( cmdp
->cb
.data
);
888 if (!tcmd
|| tcmd
== cmdp
)
896 imap_close_server( imap_store_t
*ictx
)
898 imap_t
*imap
= ictx
->imap
;
900 if (imap
->buf
.sock
.fd
!= -1) {
901 imap_exec( ictx
, 0, "LOGOUT" );
902 close( imap
->buf
.sock
.fd
);
904 free_list( imap
->ns_personal
);
905 free_list( imap
->ns_other
);
906 free_list( imap
->ns_shared
);
911 imap_close_store( store_t
*ctx
)
913 imap_close_server( (imap_store_t
*)ctx
);
914 free_generic_messages( ctx
->msgs
);
919 imap_open_store( imap_server_conf_t
*srvc
)
925 struct sockaddr_in addr
;
926 int s
, a
[2], preauth
;
928 ctx
= xcalloc( sizeof(*ctx
), 1 );
930 ctx
->imap
= imap
= xcalloc( sizeof(*imap
), 1 );
931 imap
->buf
.sock
.fd
= -1;
932 imap
->in_progress_append
= &imap
->in_progress
;
934 /* open connection to IMAP server */
937 info( "Starting tunnel '%s'... ", srvc
->tunnel
);
939 if (socketpair( PF_UNIX
, SOCK_STREAM
, 0, a
)) {
940 perror( "socketpair" );
945 if (dup2( a
[0], 0 ) == -1 || dup2( a
[0], 1 ) == -1)
949 execl( "/bin/sh", "sh", "-c", srvc
->tunnel
, NULL
);
955 imap
->buf
.sock
.fd
= a
[1];
959 memset( &addr
, 0, sizeof(addr
) );
960 addr
.sin_port
= htons( srvc
->port
);
961 addr
.sin_family
= AF_INET
;
963 info( "Resolving %s... ", srvc
->host
);
964 he
= gethostbyname( srvc
->host
);
966 perror( "gethostbyname" );
971 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
973 s
= socket( PF_INET
, SOCK_STREAM
, 0 );
975 info( "Connecting to %s:%hu... ", inet_ntoa( addr
.sin_addr
), ntohs( addr
.sin_port
) );
976 if (connect( s
, (struct sockaddr
*)&addr
, sizeof(addr
) )) {
983 imap
->buf
.sock
.fd
= s
;
987 /* read the greeting string */
988 if (buffer_gets( &imap
->buf
, &rsp
)) {
989 fprintf( stderr
, "IMAP error: no greeting response\n" );
992 arg
= next_arg( &rsp
);
993 if (!arg
|| *arg
!= '*' || (arg
= next_arg( &rsp
)) == NULL
) {
994 fprintf( stderr
, "IMAP error: invalid greeting response\n" );
998 if (!strcmp( "PREAUTH", arg
))
1000 else if (strcmp( "OK", arg
) != 0) {
1001 fprintf( stderr
, "IMAP error: unknown greeting response\n" );
1004 parse_response_code( ctx
, 0, rsp
);
1005 if (!imap
->caps
&& imap_exec( ctx
, 0, "CAPABILITY" ) != RESP_OK
)
1010 info ("Logging in...\n");
1012 fprintf( stderr
, "Skipping server %s, no user\n", srvc
->host
);
1017 sprintf( prompt
, "Password (%s@%s): ", srvc
->user
, srvc
->host
);
1018 arg
= getpass( prompt
);
1020 perror( "getpass" );
1024 fprintf( stderr
, "Skipping account %s@%s, no password\n", srvc
->user
, srvc
->host
);
1028 * getpass() returns a pointer to a static buffer. make a copy
1029 * for long term storage.
1031 srvc
->pass
= strdup( arg
);
1034 fprintf( stderr
, "Skipping account %s@%s, server forbids LOGIN\n", srvc
->user
, srvc
->host
);
1037 warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
1038 if (imap_exec( ctx
, 0, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1039 fprintf( stderr
, "IMAP error: LOGIN failed\n" );
1046 return (store_t
*)ctx
;
1049 imap_close_store( &ctx
->gen
);
1054 imap_make_flags( int flags
, char *buf
)
1059 for (i
= d
= 0; i
< ARRAY_SIZE(Flags
); i
++)
1060 if (flags
& (1 << i
)) {
1063 for (s
= Flags
[i
]; *s
; s
++)
1074 imap_store_msg( store_t
*gctx
, msg_data_t
*data
, int *uid
)
1076 imap_store_t
*ctx
= (imap_store_t
*)gctx
;
1077 imap_t
*imap
= ctx
->imap
;
1078 struct imap_cmd_cb cb
;
1080 const char *prefix
, *box
;
1081 int ret
, i
, j
, d
, len
, extra
, nocr
;
1082 int start
, sbreak
= 0, ebreak
= 0;
1083 char flagstr
[128], tuid
[TUIDL
* 2 + 1];
1085 memset( &cb
, 0, sizeof(cb
) );
1091 if (!CAP(UIDPLUS
) && uid
) {
1095 if (fmap
[i
++] == '\n') {
1097 if (i
- 2 + nocr
== start
) {
1098 sbreak
= ebreak
= i
- 2 + nocr
;
1101 if (!memcmp( fmap
+ start
, "X-TUID: ", 8 )) {
1102 extra
-= (ebreak
= i
) - (sbreak
= start
) + nocr
;
1107 /* invalid message */
1111 for (j
= 0; j
< TUIDL
; j
++)
1112 sprintf( tuid
+ j
* 2, "%02x", arc4_getbyte() );
1113 extra
+= 8 + TUIDL
* 2 + 2;
1116 for (; i
< len
; i
++)
1117 if (fmap
[i
] == '\n')
1120 cb
.dlen
= len
+ extra
;
1121 buf
= cb
.data
= xmalloc( cb
.dlen
);
1123 if (!CAP(UIDPLUS
) && uid
) {
1125 for (; i
< sbreak
; i
++)
1126 if (fmap
[i
] == '\n') {
1132 memcpy( buf
, fmap
, sbreak
);
1135 memcpy( buf
, "X-TUID: ", 8 );
1137 memcpy( buf
, tuid
, TUIDL
* 2 );
1144 for (; i
< len
; i
++)
1145 if (fmap
[i
] == '\n') {
1151 memcpy( buf
, fmap
+ i
, len
- i
);
1157 d
= imap_make_flags( data
->flags
, flagstr
);
1163 box
= gctx
->conf
->trash
;
1164 prefix
= ctx
->prefix
;
1167 imap
->caps
= imap
->rcaps
& ~(1 << LITERALPLUS
);
1170 prefix
= !strcmp( box
, "INBOX" ) ? "" : ctx
->prefix
;
1174 ret
= imap_exec_m( ctx
, &cb
, "APPEND \"%s%s\" %s", prefix
, box
, flagstr
);
1175 imap
->caps
= imap
->rcaps
;
1186 #define CHUNKSIZE 0x1000
1189 read_message( FILE *f
, msg_data_t
*msg
)
1193 memset( msg
, 0, sizeof *msg
);
1195 msg
->data
= xmalloc( len
+1 );
1199 if (msg
->len
>= len
) {
1202 p
= xrealloc(msg
->data
, len
+1);
1206 r
= fread( &msg
->data
[msg
->len
], 1, len
- msg
->len
, f
);
1211 msg
->data
[msg
->len
] = 0;
1216 count_messages( msg_data_t
*msg
)
1219 char *p
= msg
->data
;
1222 if (!strncmp( "From ", p
, 5 )) {
1226 p
= strstr( p
+5, "\nFrom ");
1235 split_msg( msg_data_t
*all_msgs
, msg_data_t
*msg
, int *ofs
)
1239 memset( msg
, 0, sizeof *msg
);
1240 if (*ofs
>= all_msgs
->len
)
1243 data
= &all_msgs
->data
[ *ofs
];
1244 msg
->len
= all_msgs
->len
- *ofs
;
1246 if (msg
->len
< 5 || strncmp( data
, "From ", 5 ))
1249 p
= strstr( data
, "\nFrom " );
1251 msg
->len
= &p
[1] - data
;
1253 msg
->data
= xmalloc( msg
->len
+ 1 );
1257 memcpy( msg
->data
, data
, msg
->len
);
1258 msg
->data
[ msg
->len
] = 0;
1264 static imap_server_conf_t server
=
1274 static char *imap_folder
;
1277 git_imap_config(const char *key
, const char *val
)
1279 char imap_key
[] = "imap.";
1281 if (strncmp( key
, imap_key
, sizeof imap_key
- 1 ))
1283 key
+= sizeof imap_key
- 1;
1285 if (!strcmp( "folder", key
)) {
1286 imap_folder
= strdup( val
);
1287 } else if (!strcmp( "host", key
)) {
1289 if (!strncmp( "imap:", val
, 5 ))
1294 if (!strncmp( "//", val
, 2 ))
1296 server
.host
= strdup( val
);
1298 else if (!strcmp( "user", key
))
1299 server
.user
= strdup( val
);
1300 else if (!strcmp( "pass", key
))
1301 server
.pass
= strdup( val
);
1302 else if (!strcmp( "port", key
))
1303 server
.port
= git_config_int( key
, val
);
1304 else if (!strcmp( "tunnel", key
))
1305 server
.tunnel
= strdup( val
);
1310 main(int argc
, char **argv
)
1312 msg_data_t all_msgs
, msg
;
1319 /* init the random number generator */
1322 git_config( git_imap_config
);
1325 fprintf( stderr
, "no imap store specified\n" );
1329 /* read the messages */
1330 if (!read_message( stdin
, &all_msgs
)) {
1331 fprintf(stderr
,"nothing to send\n");
1335 /* write it to the imap server */
1336 ctx
= imap_open_store( &server
);
1338 fprintf( stderr
,"failed to open store\n");
1342 total
= count_messages( &all_msgs
);
1343 fprintf( stderr
, "sending %d message%s\n", total
, (total
!=1)?"s":"" );
1344 ctx
->name
= imap_folder
;
1346 unsigned percent
= n
* 100 / total
;
1347 fprintf( stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1348 if (!split_msg( &all_msgs
, &msg
, &ofs
))
1350 r
= imap_store_msg( ctx
, &msg
, &uid
);
1351 if (r
!= DRV_OK
) break;
1354 fprintf( stderr
,"\n" );
1356 imap_close_store( ctx
);