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 nfsnprintf( char *buf
, int blen
, const char *fmt
, ... );
116 static void arc4_init( void );
117 static unsigned char arc4_getbyte( void );
119 typedef struct imap_server_conf
{
126 } imap_server_conf_t
;
128 typedef struct imap_store_conf
{
130 imap_server_conf_t
*server
;
131 unsigned use_namespace
:1;
134 #define NIL (void*)0x1
135 #define LIST (void*)0x2
137 typedef struct _list
{
138 struct _list
*next
, *child
;
156 typedef struct imap
{
157 int uidnext
; /* from SELECT responses */
158 list_t
*ns_personal
, *ns_other
, *ns_shared
; /* NAMESPACE info */
159 unsigned caps
, rcaps
; /* CAPABILITY results */
161 int nexttag
, num_in_progress
, literal_pending
;
162 struct imap_cmd
*in_progress
, **in_progress_append
;
163 buffer_t buf
; /* this is BIG, so put it last */
166 typedef struct imap_store
{
171 unsigned /*currentnc:1,*/ trashnc
:1;
175 int (*cont
)( imap_store_t
*ctx
, struct imap_cmd
*cmd
, const char *prompt
);
176 void (*done
)( imap_store_t
*ctx
, struct imap_cmd
*cmd
, int response
);
181 unsigned create
:1, trycreate
:1;
185 struct imap_cmd
*next
;
186 struct imap_cmd_cb cb
;
191 #define CAP(cap) (imap->caps & (1 << (cap)))
200 static const char *cap_list
[] = {
211 static int get_cmd_result( imap_store_t
*ctx
, struct imap_cmd
*tcmd
);
214 static const char *Flags
[] = {
223 socket_perror( const char *func
, Socket_t
*sock
, int ret
)
228 fprintf( stderr
, "%s: unexpected EOF\n", func
);
232 socket_read( Socket_t
*sock
, char *buf
, int len
)
234 int n
= read( sock
->fd
, buf
, len
);
236 socket_perror( "read", sock
, n
);
244 socket_write( Socket_t
*sock
, const char *buf
, int len
)
246 int n
= write( sock
->fd
, buf
, len
);
248 socket_perror( "write", sock
, n
);
255 /* simple line buffering */
257 buffer_gets( buffer_t
* b
, char **s
)
260 int start
= b
->offset
;
265 /* make sure we have enough data to read the \r\n sequence */
266 if (b
->offset
+ 1 >= b
->bytes
) {
268 /* shift down used bytes */
271 assert( start
<= b
->bytes
);
272 n
= b
->bytes
- start
;
275 memmove(b
->buf
, b
->buf
+ start
, n
);
281 n
= socket_read( &b
->sock
, b
->buf
+ b
->bytes
,
282 sizeof(b
->buf
) - b
->bytes
);
290 if (b
->buf
[b
->offset
] == '\r') {
291 assert( b
->offset
+ 1 < b
->bytes
);
292 if (b
->buf
[b
->offset
+ 1] == '\n') {
293 b
->buf
[b
->offset
] = 0; /* terminate the string */
294 b
->offset
+= 2; /* next line */
307 info( const char *msg
, ... )
320 warn( const char *msg
, ... )
326 vfprintf( stderr
, msg
, va
);
338 while (isspace( (unsigned char) **s
))
347 *s
= strchr( *s
, '"' );
350 while (**s
&& !isspace( (unsigned char) **s
))
363 free_generic_messages( message_t
*msgs
)
367 for (; msgs
; msgs
= tmsg
) {
374 nfsnprintf( char *buf
, int blen
, const char *fmt
, ... )
380 if (blen
<= 0 || (unsigned)(ret
= vsnprintf( buf
, blen
, fmt
, va
)) >= (unsigned)blen
)
381 die( "Fatal: buffer too small. Please report a bug.\n");
387 unsigned char i
, j
, s
[256];
394 unsigned char j
, si
, dat
[128];
396 if ((fd
= open( "/dev/urandom", O_RDONLY
)) < 0 && (fd
= open( "/dev/random", O_RDONLY
)) < 0) {
397 fprintf( stderr
, "Fatal: no random number source available.\n" );
400 if (read( fd
, dat
, 128 ) != 128) {
401 fprintf( stderr
, "Fatal: cannot read random number source.\n" );
406 for (i
= 0; i
< 256; i
++)
408 for (i
= j
= 0; i
< 256; i
++) {
410 j
+= si
+ dat
[i
& 127];
416 for (i
= 0; i
< 256; i
++)
423 unsigned char si
, sj
;
431 return rs
.s
[(si
+ sj
) & 0xff];
434 static struct imap_cmd
*
435 v_issue_imap_cmd( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
,
436 const char *fmt
, va_list ap
)
438 imap_t
*imap
= ctx
->imap
;
439 struct imap_cmd
*cmd
;
443 cmd
= xmalloc( sizeof(struct imap_cmd
) );
444 nfvasprintf( &cmd
->cmd
, fmt
, ap
);
445 cmd
->tag
= ++imap
->nexttag
;
450 memset( &cmd
->cb
, 0, sizeof(cmd
->cb
) );
452 while (imap
->literal_pending
)
453 get_cmd_result( ctx
, NULL
);
455 bufl
= nfsnprintf( buf
, sizeof(buf
), cmd
->cb
.data
? CAP(LITERALPLUS
) ?
456 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
457 cmd
->tag
, cmd
->cmd
, cmd
->cb
.dlen
);
459 if (imap
->num_in_progress
)
460 printf( "(%d in progress) ", imap
->num_in_progress
);
461 if (memcmp( cmd
->cmd
, "LOGIN", 5 ))
462 printf( ">>> %s", buf
);
464 printf( ">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
466 if (socket_write( &imap
->buf
.sock
, buf
, bufl
) != bufl
) {
474 if (CAP(LITERALPLUS
)) {
475 n
= socket_write( &imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
476 free( cmd
->cb
.data
);
477 if (n
!= cmd
->cb
.dlen
||
478 (n
= socket_write( &imap
->buf
.sock
, "\r\n", 2 )) != 2)
486 imap
->literal_pending
= 1;
487 } else if (cmd
->cb
.cont
)
488 imap
->literal_pending
= 1;
490 *imap
->in_progress_append
= cmd
;
491 imap
->in_progress_append
= &cmd
->next
;
492 imap
->num_in_progress
++;
496 static struct imap_cmd
*
497 issue_imap_cmd( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
499 struct imap_cmd
*ret
;
503 ret
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
509 imap_exec( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
512 struct imap_cmd
*cmdp
;
515 cmdp
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
520 return get_cmd_result( ctx
, cmdp
);
524 imap_exec_m( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
527 struct imap_cmd
*cmdp
;
530 cmdp
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
533 return DRV_STORE_BAD
;
535 switch (get_cmd_result( ctx
, cmdp
)) {
536 case RESP_BAD
: return DRV_STORE_BAD
;
537 case RESP_NO
: return DRV_MSG_BAD
;
538 default: return DRV_OK
;
543 is_atom( list_t
*list
)
545 return list
&& list
->val
&& list
->val
!= NIL
&& list
->val
!= LIST
;
549 is_list( list_t
*list
)
551 return list
&& list
->val
== LIST
;
555 free_list( list_t
*list
)
559 for (; list
; list
= tmp
) {
562 free_list( list
->child
);
563 else if (is_atom( list
))
570 parse_imap_list_l( imap_t
*imap
, char **sp
, list_t
**curp
, int level
)
577 while (isspace( (unsigned char)*s
))
579 if (level
&& *s
== ')') {
583 *curp
= cur
= xmalloc( sizeof(*cur
) );
585 cur
->val
= NULL
; /* for clean bail */
590 if (parse_imap_list_l( imap
, &s
, &cur
->child
, level
+ 1 ))
592 } else if (imap
&& *s
== '{') {
594 bytes
= cur
->len
= strtol( s
+ 1, &s
, 10 );
598 s
= cur
->val
= xmalloc( cur
->len
);
600 /* dump whats left over in the input buffer */
601 n
= imap
->buf
.bytes
- imap
->buf
.offset
;
604 /* the entire message fit in the buffer */
607 memcpy( s
, imap
->buf
.buf
+ imap
->buf
.offset
, n
);
611 /* mark that we used part of the buffer */
612 imap
->buf
.offset
+= n
;
614 /* now read the rest of the message */
616 if ((n
= socket_read (&imap
->buf
.sock
, s
, bytes
)) <= 0)
622 if (buffer_gets( &imap
->buf
, &s
))
624 } else if (*s
== '"') {
628 for (; *s
!= '"'; s
++)
633 cur
->val
= xmalloc( cur
->len
+ 1 );
634 memcpy( cur
->val
, p
, cur
->len
);
635 cur
->val
[cur
->len
] = 0;
639 for (; *s
&& !isspace( (unsigned char)*s
); s
++)
640 if (level
&& *s
== ')')
643 if (cur
->len
== 3 && !memcmp ("NIL", p
, 3))
646 cur
->val
= xmalloc( cur
->len
+ 1 );
647 memcpy( cur
->val
, p
, cur
->len
);
648 cur
->val
[cur
->len
] = 0;
667 parse_imap_list( imap_t
*imap
, char **sp
)
671 if (!parse_imap_list_l( imap
, sp
, &head
, 0 ))
678 parse_list( char **sp
)
680 return parse_imap_list( NULL
, sp
);
684 parse_capability( imap_t
*imap
, char *cmd
)
689 imap
->caps
= 0x80000000;
690 while ((arg
= next_arg( &cmd
)))
691 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
692 if (!strcmp( cap_list
[i
], arg
))
693 imap
->caps
|= 1 << i
;
694 imap
->rcaps
= imap
->caps
;
698 parse_response_code( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, char *s
)
700 imap_t
*imap
= ctx
->imap
;
704 return RESP_OK
; /* no response code */
706 if (!(p
= strchr( s
, ']' ))) {
707 fprintf( stderr
, "IMAP error: malformed response code\n" );
711 arg
= next_arg( &s
);
712 if (!strcmp( "UIDVALIDITY", arg
)) {
713 if (!(arg
= next_arg( &s
)) || !(ctx
->gen
.uidvalidity
= atoi( arg
))) {
714 fprintf( stderr
, "IMAP error: malformed UIDVALIDITY status\n" );
717 } else if (!strcmp( "UIDNEXT", arg
)) {
718 if (!(arg
= next_arg( &s
)) || !(imap
->uidnext
= atoi( arg
))) {
719 fprintf( stderr
, "IMAP error: malformed NEXTUID status\n" );
722 } else if (!strcmp( "CAPABILITY", arg
)) {
723 parse_capability( imap
, s
);
724 } else if (!strcmp( "ALERT", arg
)) {
725 /* RFC2060 says that these messages MUST be displayed
728 for (; isspace( (unsigned char)*p
); p
++);
729 fprintf( stderr
, "*** IMAP ALERT *** %s\n", p
);
730 } else if (cb
&& cb
->ctx
&& !strcmp( "APPENDUID", arg
)) {
731 if (!(arg
= next_arg( &s
)) || !(ctx
->gen
.uidvalidity
= atoi( arg
)) ||
732 !(arg
= next_arg( &s
)) || !(*(int *)cb
->ctx
= atoi( arg
)))
734 fprintf( stderr
, "IMAP error: malformed APPENDUID status\n" );
742 get_cmd_result( imap_store_t
*ctx
, struct imap_cmd
*tcmd
)
744 imap_t
*imap
= ctx
->imap
;
745 struct imap_cmd
*cmdp
, **pcmdp
, *ncmdp
;
746 char *cmd
, *arg
, *arg1
, *p
;
747 int n
, resp
, resp2
, tag
;
750 if (buffer_gets( &imap
->buf
, &cmd
))
753 arg
= next_arg( &cmd
);
755 arg
= next_arg( &cmd
);
757 fprintf( stderr
, "IMAP error: unable to parse untagged response\n" );
761 if (!strcmp( "NAMESPACE", arg
)) {
762 imap
->ns_personal
= parse_list( &cmd
);
763 imap
->ns_other
= parse_list( &cmd
);
764 imap
->ns_shared
= parse_list( &cmd
);
765 } else if (!strcmp( "OK", arg
) || !strcmp( "BAD", arg
) ||
766 !strcmp( "NO", arg
) || !strcmp( "BYE", arg
)) {
767 if ((resp
= parse_response_code( ctx
, NULL
, cmd
)) != RESP_OK
)
769 } else if (!strcmp( "CAPABILITY", arg
))
770 parse_capability( imap
, cmd
);
771 else if ((arg1
= next_arg( &cmd
))) {
772 if (!strcmp( "EXISTS", arg1
))
773 ctx
->gen
.count
= atoi( arg
);
774 else if (!strcmp( "RECENT", arg1
))
775 ctx
->gen
.recent
= atoi( arg
);
777 fprintf( stderr
, "IMAP error: unable to parse untagged response\n" );
780 } else if (!imap
->in_progress
) {
781 fprintf( stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "" );
783 } else if (*arg
== '+') {
784 /* This can happen only with the last command underway, as
785 it enforces a round-trip. */
786 cmdp
= (struct imap_cmd
*)((char *)imap
->in_progress_append
-
787 offsetof(struct imap_cmd
, next
));
789 n
= socket_write( &imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
790 free( cmdp
->cb
.data
);
791 cmdp
->cb
.data
= NULL
;
792 if (n
!= (int)cmdp
->cb
.dlen
)
794 } else if (cmdp
->cb
.cont
) {
795 if (cmdp
->cb
.cont( ctx
, cmdp
, cmd
))
798 fprintf( stderr
, "IMAP error: unexpected command continuation request\n" );
801 if (socket_write( &imap
->buf
.sock
, "\r\n", 2 ) != 2)
804 imap
->literal_pending
= 0;
809 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
810 if (cmdp
->tag
== tag
)
812 fprintf( stderr
, "IMAP error: unexpected tag %s\n", arg
);
815 if (!(*pcmdp
= cmdp
->next
))
816 imap
->in_progress_append
= pcmdp
;
817 imap
->num_in_progress
--;
818 if (cmdp
->cb
.cont
|| cmdp
->cb
.data
)
819 imap
->literal_pending
= 0;
820 arg
= next_arg( &cmd
);
821 if (!strcmp( "OK", arg
))
824 if (!strcmp( "NO", arg
)) {
825 if (cmdp
->cb
.create
&& cmd
&& (cmdp
->cb
.trycreate
|| !memcmp( cmd
, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
826 p
= strchr( cmdp
->cmd
, '"' );
827 if (!issue_imap_cmd( ctx
, NULL
, "CREATE \"%.*s\"", strchr( p
+ 1, '"' ) - p
+ 1, p
)) {
831 /* not waiting here violates the spec, but a server that does not
832 grok this nonetheless violates it too. */
834 if (!(ncmdp
= issue_imap_cmd( ctx
, &cmdp
->cb
, "%s", cmdp
->cmd
))) {
841 return 0; /* ignored */
847 } else /*if (!strcmp( "BAD", arg ))*/
849 fprintf( stderr
, "IMAP command '%s' returned response (%s) - %s\n",
850 memcmp (cmdp
->cmd
, "LOGIN", 5) ?
851 cmdp
->cmd
: "LOGIN <user> <pass>",
852 arg
, cmd
? cmd
: "");
854 if ((resp2
= parse_response_code( ctx
, &cmdp
->cb
, cmd
)) > resp
)
858 cmdp
->cb
.done( ctx
, cmdp
, resp
);
860 free( cmdp
->cb
.data
);
863 if (!tcmd
|| tcmd
== cmdp
)
871 imap_close_server( imap_store_t
*ictx
)
873 imap_t
*imap
= ictx
->imap
;
875 if (imap
->buf
.sock
.fd
!= -1) {
876 imap_exec( ictx
, NULL
, "LOGOUT" );
877 close( imap
->buf
.sock
.fd
);
879 free_list( imap
->ns_personal
);
880 free_list( imap
->ns_other
);
881 free_list( imap
->ns_shared
);
886 imap_close_store( store_t
*ctx
)
888 imap_close_server( (imap_store_t
*)ctx
);
889 free_generic_messages( ctx
->msgs
);
894 imap_open_store( imap_server_conf_t
*srvc
)
900 struct sockaddr_in addr
;
901 int s
, a
[2], preauth
;
904 ctx
= xcalloc( sizeof(*ctx
), 1 );
906 ctx
->imap
= imap
= xcalloc( sizeof(*imap
), 1 );
907 imap
->buf
.sock
.fd
= -1;
908 imap
->in_progress_append
= &imap
->in_progress
;
910 /* open connection to IMAP server */
913 info( "Starting tunnel '%s'... ", srvc
->tunnel
);
915 if (socketpair( PF_UNIX
, SOCK_STREAM
, 0, a
)) {
916 perror( "socketpair" );
924 if (dup2( a
[0], 0 ) == -1 || dup2( a
[0], 1 ) == -1)
928 execl( "/bin/sh", "sh", "-c", srvc
->tunnel
, NULL
);
934 imap
->buf
.sock
.fd
= a
[1];
938 memset( &addr
, 0, sizeof(addr
) );
939 addr
.sin_port
= htons( srvc
->port
);
940 addr
.sin_family
= AF_INET
;
942 info( "Resolving %s... ", srvc
->host
);
943 he
= gethostbyname( srvc
->host
);
945 perror( "gethostbyname" );
950 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
952 s
= socket( PF_INET
, SOCK_STREAM
, 0 );
954 info( "Connecting to %s:%hu... ", inet_ntoa( addr
.sin_addr
), ntohs( addr
.sin_port
) );
955 if (connect( s
, (struct sockaddr
*)&addr
, sizeof(addr
) )) {
962 imap
->buf
.sock
.fd
= s
;
966 /* read the greeting string */
967 if (buffer_gets( &imap
->buf
, &rsp
)) {
968 fprintf( stderr
, "IMAP error: no greeting response\n" );
971 arg
= next_arg( &rsp
);
972 if (!arg
|| *arg
!= '*' || (arg
= next_arg( &rsp
)) == NULL
) {
973 fprintf( stderr
, "IMAP error: invalid greeting response\n" );
977 if (!strcmp( "PREAUTH", arg
))
979 else if (strcmp( "OK", arg
) != 0) {
980 fprintf( stderr
, "IMAP error: unknown greeting response\n" );
983 parse_response_code( ctx
, NULL
, rsp
);
984 if (!imap
->caps
&& imap_exec( ctx
, NULL
, "CAPABILITY" ) != RESP_OK
)
989 info ("Logging in...\n");
991 fprintf( stderr
, "Skipping server %s, no user\n", srvc
->host
);
996 sprintf( prompt
, "Password (%s@%s): ", srvc
->user
, srvc
->host
);
997 arg
= getpass( prompt
);
1003 fprintf( stderr
, "Skipping account %s@%s, no password\n", srvc
->user
, srvc
->host
);
1007 * getpass() returns a pointer to a static buffer. make a copy
1008 * for long term storage.
1010 srvc
->pass
= xstrdup( arg
);
1013 fprintf( stderr
, "Skipping account %s@%s, server forbids LOGIN\n", srvc
->user
, srvc
->host
);
1016 warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
1017 if (imap_exec( ctx
, NULL
, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1018 fprintf( stderr
, "IMAP error: LOGIN failed\n" );
1025 return (store_t
*)ctx
;
1028 imap_close_store( &ctx
->gen
);
1033 imap_make_flags( int flags
, char *buf
)
1038 for (i
= d
= 0; i
< ARRAY_SIZE(Flags
); i
++)
1039 if (flags
& (1 << i
)) {
1042 for (s
= Flags
[i
]; *s
; s
++)
1053 imap_store_msg( store_t
*gctx
, msg_data_t
*data
, int *uid
)
1055 imap_store_t
*ctx
= (imap_store_t
*)gctx
;
1056 imap_t
*imap
= ctx
->imap
;
1057 struct imap_cmd_cb cb
;
1059 const char *prefix
, *box
;
1060 int ret
, i
, j
, d
, len
, extra
, nocr
;
1061 int start
, sbreak
= 0, ebreak
= 0;
1062 char flagstr
[128], tuid
[TUIDL
* 2 + 1];
1064 memset( &cb
, 0, sizeof(cb
) );
1070 if (!CAP(UIDPLUS
) && uid
) {
1074 if (fmap
[i
++] == '\n') {
1076 if (i
- 2 + nocr
== start
) {
1077 sbreak
= ebreak
= i
- 2 + nocr
;
1080 if (!memcmp( fmap
+ start
, "X-TUID: ", 8 )) {
1081 extra
-= (ebreak
= i
) - (sbreak
= start
) + nocr
;
1086 /* invalid message */
1090 for (j
= 0; j
< TUIDL
; j
++)
1091 sprintf( tuid
+ j
* 2, "%02x", arc4_getbyte() );
1092 extra
+= 8 + TUIDL
* 2 + 2;
1095 for (; i
< len
; i
++)
1096 if (fmap
[i
] == '\n')
1099 cb
.dlen
= len
+ extra
;
1100 buf
= cb
.data
= xmalloc( cb
.dlen
);
1102 if (!CAP(UIDPLUS
) && uid
) {
1104 for (; i
< sbreak
; i
++)
1105 if (fmap
[i
] == '\n') {
1111 memcpy( buf
, fmap
, sbreak
);
1114 memcpy( buf
, "X-TUID: ", 8 );
1116 memcpy( buf
, tuid
, TUIDL
* 2 );
1123 for (; i
< len
; i
++)
1124 if (fmap
[i
] == '\n') {
1130 memcpy( buf
, fmap
+ i
, len
- i
);
1136 d
= imap_make_flags( data
->flags
, flagstr
);
1142 box
= gctx
->conf
->trash
;
1143 prefix
= ctx
->prefix
;
1146 imap
->caps
= imap
->rcaps
& ~(1 << LITERALPLUS
);
1149 prefix
= !strcmp( box
, "INBOX" ) ? "" : ctx
->prefix
;
1153 ret
= imap_exec_m( ctx
, &cb
, "APPEND \"%s%s\" %s", prefix
, box
, flagstr
);
1154 imap
->caps
= imap
->rcaps
;
1165 #define CHUNKSIZE 0x1000
1168 read_message( FILE *f
, msg_data_t
*msg
)
1172 memset( msg
, 0, sizeof *msg
);
1174 msg
->data
= xmalloc( len
+1 );
1178 if (msg
->len
>= len
) {
1181 p
= xrealloc(msg
->data
, len
+1);
1186 r
= fread( &msg
->data
[msg
->len
], 1, len
- msg
->len
, f
);
1191 msg
->data
[msg
->len
] = 0;
1196 count_messages( msg_data_t
*msg
)
1199 char *p
= msg
->data
;
1202 if (!strncmp( "From ", p
, 5 )) {
1206 p
= strstr( p
+5, "\nFrom ");
1215 split_msg( msg_data_t
*all_msgs
, msg_data_t
*msg
, int *ofs
)
1219 memset( msg
, 0, sizeof *msg
);
1220 if (*ofs
>= all_msgs
->len
)
1223 data
= &all_msgs
->data
[ *ofs
];
1224 msg
->len
= all_msgs
->len
- *ofs
;
1226 if (msg
->len
< 5 || strncmp( data
, "From ", 5 ))
1229 p
= strchr( data
, '\n' );
1237 p
= strstr( data
, "\nFrom " );
1239 msg
->len
= &p
[1] - data
;
1241 msg
->data
= xmalloc( msg
->len
+ 1 );
1245 memcpy( msg
->data
, data
, msg
->len
);
1246 msg
->data
[ msg
->len
] = 0;
1252 static imap_server_conf_t server
=
1262 static char *imap_folder
;
1265 git_imap_config(const char *key
, const char *val
)
1267 char imap_key
[] = "imap.";
1269 if (strncmp( key
, imap_key
, sizeof imap_key
- 1 ))
1271 key
+= sizeof imap_key
- 1;
1273 if (!strcmp( "folder", key
)) {
1274 imap_folder
= xstrdup( val
);
1275 } else if (!strcmp( "host", key
)) {
1277 if (!strncmp( "imap:", val
, 5 ))
1282 if (!strncmp( "//", val
, 2 ))
1284 server
.host
= xstrdup( val
);
1286 else if (!strcmp( "user", key
))
1287 server
.user
= xstrdup( val
);
1288 else if (!strcmp( "pass", key
))
1289 server
.pass
= xstrdup( val
);
1290 else if (!strcmp( "port", key
))
1291 server
.port
= git_config_int( key
, val
);
1292 else if (!strcmp( "tunnel", key
))
1293 server
.tunnel
= xstrdup( val
);
1298 main(int argc
, char **argv
)
1300 msg_data_t all_msgs
, msg
;
1301 store_t
*ctx
= NULL
;
1307 /* init the random number generator */
1310 git_config( git_imap_config
);
1313 fprintf( stderr
, "no imap store specified\n" );
1317 /* read the messages */
1318 if (!read_message( stdin
, &all_msgs
)) {
1319 fprintf(stderr
,"nothing to send\n");
1323 total
= count_messages( &all_msgs
);
1325 fprintf(stderr
,"no messages to send\n");
1329 /* write it to the imap server */
1330 ctx
= imap_open_store( &server
);
1332 fprintf( stderr
,"failed to open store\n");
1336 fprintf( stderr
, "sending %d message%s\n", total
, (total
!=1)?"s":"" );
1337 ctx
->name
= imap_folder
;
1339 unsigned percent
= n
* 100 / total
;
1340 fprintf( stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1341 if (!split_msg( &all_msgs
, &msg
, &ofs
))
1343 r
= imap_store_msg( ctx
, &msg
, &uid
);
1344 if (r
!= DRV_OK
) break;
1347 fprintf( stderr
,"\n" );
1349 imap_close_store( ctx
);