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 typedef struct store_conf
{
30 const char *path
; /* should this be here? its interpretation is driver-specific */
33 unsigned max_size
; /* off_t is overkill */
34 unsigned trash_remote_new
:1, trash_only_new
:1;
37 typedef struct string_list
{
38 struct string_list
*next
;
42 typedef struct channel_conf
{
43 struct channel_conf
*next
;
45 store_conf_t
*master
, *slave
;
46 char *master_name
, *slave_name
;
48 string_list_t
*patterns
;
50 unsigned max_messages
; /* for slave only */
53 typedef struct group_conf
{
54 struct group_conf
*next
;
56 string_list_t
*channels
;
59 /* For message->status */
60 #define M_RECENT (1<<0) /* unsyncable flag; maildir_* depend on this being 1<<0 */
61 #define M_DEAD (1<<1) /* expunged */
62 #define M_FLAGS (1<<2) /* flags fetched */
64 typedef struct message
{
66 /* string_list_t *keywords; */
67 size_t size
; /* zero implies "not fetched" */
69 unsigned char flags
, status
;
72 typedef struct store
{
73 store_conf_t
*conf
; /* foreign */
75 /* currently open mailbox */
76 const char *name
; /* foreign! maybe preset? */
78 message_t
*msgs
; /* own */
80 unsigned char opts
; /* maybe preset? */
81 /* note that the following do _not_ reflect stats from msgs, but mailbox totals */
82 int count
; /* # of messages */
83 int recent
; /* # of recent messages - don't trust this beyond the initial read */
94 #define DRV_MSG_BAD -1
95 #define DRV_BOX_BAD -2
96 #define DRV_STORE_BAD -3
98 static int Verbose
, Quiet
;
100 static void imap_info( const char *, ... );
101 static void imap_warn( const char *, ... );
103 static char *next_arg( char ** );
105 static void free_generic_messages( message_t
* );
107 static int nfsnprintf( char *buf
, int blen
, const char *fmt
, ... );
110 static void arc4_init( void );
111 static unsigned char arc4_getbyte( void );
113 typedef struct imap_server_conf
{
120 } imap_server_conf_t
;
122 typedef struct imap_store_conf
{
124 imap_server_conf_t
*server
;
125 unsigned use_namespace
:1;
128 #define NIL (void*)0x1
129 #define LIST (void*)0x2
131 typedef struct _list
{
132 struct _list
*next
, *child
;
150 typedef struct imap
{
151 int uidnext
; /* from SELECT responses */
152 list_t
*ns_personal
, *ns_other
, *ns_shared
; /* NAMESPACE info */
153 unsigned caps
, rcaps
; /* CAPABILITY results */
155 int nexttag
, num_in_progress
, literal_pending
;
156 struct imap_cmd
*in_progress
, **in_progress_append
;
157 buffer_t buf
; /* this is BIG, so put it last */
160 typedef struct imap_store
{
165 unsigned /*currentnc:1,*/ trashnc
:1;
169 int (*cont
)( imap_store_t
*ctx
, struct imap_cmd
*cmd
, const char *prompt
);
170 void (*done
)( imap_store_t
*ctx
, struct imap_cmd
*cmd
, int response
);
175 unsigned create
:1, trycreate
:1;
179 struct imap_cmd
*next
;
180 struct imap_cmd_cb cb
;
185 #define CAP(cap) (imap->caps & (1 << (cap)))
194 static const char *cap_list
[] = {
205 static int get_cmd_result( imap_store_t
*ctx
, struct imap_cmd
*tcmd
);
208 static const char *Flags
[] = {
217 socket_perror( const char *func
, Socket_t
*sock
, int ret
)
222 fprintf( stderr
, "%s: unexpected EOF\n", func
);
226 socket_read( Socket_t
*sock
, char *buf
, int len
)
228 ssize_t n
= xread( sock
->fd
, buf
, len
);
230 socket_perror( "read", sock
, n
);
238 socket_write( Socket_t
*sock
, const char *buf
, int len
)
240 int n
= write_in_full( sock
->fd
, buf
, len
);
242 socket_perror( "write", sock
, n
);
249 /* simple line buffering */
251 buffer_gets( buffer_t
* b
, char **s
)
254 int start
= b
->offset
;
259 /* make sure we have enough data to read the \r\n sequence */
260 if (b
->offset
+ 1 >= b
->bytes
) {
262 /* shift down used bytes */
265 assert( start
<= b
->bytes
);
266 n
= b
->bytes
- start
;
269 memmove(b
->buf
, b
->buf
+ start
, n
);
275 n
= socket_read( &b
->sock
, b
->buf
+ b
->bytes
,
276 sizeof(b
->buf
) - b
->bytes
);
284 if (b
->buf
[b
->offset
] == '\r') {
285 assert( b
->offset
+ 1 < b
->bytes
);
286 if (b
->buf
[b
->offset
+ 1] == '\n') {
287 b
->buf
[b
->offset
] = 0; /* terminate the string */
288 b
->offset
+= 2; /* next line */
301 imap_info( const char *msg
, ... )
314 imap_warn( const char *msg
, ... )
320 vfprintf( stderr
, msg
, va
);
332 while (isspace( (unsigned char) **s
))
341 *s
= strchr( *s
, '"' );
344 while (**s
&& !isspace( (unsigned char) **s
))
357 free_generic_messages( message_t
*msgs
)
361 for (; msgs
; msgs
= tmsg
) {
368 nfsnprintf( char *buf
, int blen
, const char *fmt
, ... )
374 if (blen
<= 0 || (unsigned)(ret
= vsnprintf( buf
, blen
, fmt
, va
)) >= (unsigned)blen
)
375 die( "Fatal: buffer too small. Please report a bug.\n");
381 unsigned char i
, j
, s
[256];
388 unsigned char j
, si
, dat
[128];
390 if ((fd
= open( "/dev/urandom", O_RDONLY
)) < 0 && (fd
= open( "/dev/random", O_RDONLY
)) < 0) {
391 fprintf( stderr
, "Fatal: no random number source available.\n" );
394 if (read_in_full( fd
, dat
, 128 ) != 128) {
395 fprintf( stderr
, "Fatal: cannot read random number source.\n" );
400 for (i
= 0; i
< 256; i
++)
402 for (i
= j
= 0; i
< 256; i
++) {
404 j
+= si
+ dat
[i
& 127];
410 for (i
= 0; i
< 256; i
++)
417 unsigned char si
, sj
;
425 return rs
.s
[(si
+ sj
) & 0xff];
428 static struct imap_cmd
*
429 v_issue_imap_cmd( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
,
430 const char *fmt
, va_list ap
)
432 imap_t
*imap
= ctx
->imap
;
433 struct imap_cmd
*cmd
;
437 cmd
= xmalloc( sizeof(struct imap_cmd
) );
438 nfvasprintf( &cmd
->cmd
, fmt
, ap
);
439 cmd
->tag
= ++imap
->nexttag
;
444 memset( &cmd
->cb
, 0, sizeof(cmd
->cb
) );
446 while (imap
->literal_pending
)
447 get_cmd_result( ctx
, NULL
);
449 bufl
= nfsnprintf( buf
, sizeof(buf
), cmd
->cb
.data
? CAP(LITERALPLUS
) ?
450 "%d %s{%d+}\r\n" : "%d %s{%d}\r\n" : "%d %s\r\n",
451 cmd
->tag
, cmd
->cmd
, cmd
->cb
.dlen
);
453 if (imap
->num_in_progress
)
454 printf( "(%d in progress) ", imap
->num_in_progress
);
455 if (memcmp( cmd
->cmd
, "LOGIN", 5 ))
456 printf( ">>> %s", buf
);
458 printf( ">>> %d LOGIN <user> <pass>\n", cmd
->tag
);
460 if (socket_write( &imap
->buf
.sock
, buf
, bufl
) != bufl
) {
468 if (CAP(LITERALPLUS
)) {
469 n
= socket_write( &imap
->buf
.sock
, cmd
->cb
.data
, cmd
->cb
.dlen
);
470 free( cmd
->cb
.data
);
471 if (n
!= cmd
->cb
.dlen
||
472 (n
= socket_write( &imap
->buf
.sock
, "\r\n", 2 )) != 2)
480 imap
->literal_pending
= 1;
481 } else if (cmd
->cb
.cont
)
482 imap
->literal_pending
= 1;
484 *imap
->in_progress_append
= cmd
;
485 imap
->in_progress_append
= &cmd
->next
;
486 imap
->num_in_progress
++;
490 static struct imap_cmd
*
491 issue_imap_cmd( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
493 struct imap_cmd
*ret
;
497 ret
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
503 imap_exec( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
506 struct imap_cmd
*cmdp
;
509 cmdp
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
514 return get_cmd_result( ctx
, cmdp
);
518 imap_exec_m( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, const char *fmt
, ... )
521 struct imap_cmd
*cmdp
;
524 cmdp
= v_issue_imap_cmd( ctx
, cb
, fmt
, ap
);
527 return DRV_STORE_BAD
;
529 switch (get_cmd_result( ctx
, cmdp
)) {
530 case RESP_BAD
: return DRV_STORE_BAD
;
531 case RESP_NO
: return DRV_MSG_BAD
;
532 default: return DRV_OK
;
537 is_atom( list_t
*list
)
539 return list
&& list
->val
&& list
->val
!= NIL
&& list
->val
!= LIST
;
543 is_list( list_t
*list
)
545 return list
&& list
->val
== LIST
;
549 free_list( list_t
*list
)
553 for (; list
; list
= tmp
) {
556 free_list( list
->child
);
557 else if (is_atom( list
))
564 parse_imap_list_l( imap_t
*imap
, char **sp
, list_t
**curp
, int level
)
571 while (isspace( (unsigned char)*s
))
573 if (level
&& *s
== ')') {
577 *curp
= cur
= xmalloc( sizeof(*cur
) );
579 cur
->val
= NULL
; /* for clean bail */
584 if (parse_imap_list_l( imap
, &s
, &cur
->child
, level
+ 1 ))
586 } else if (imap
&& *s
== '{') {
588 bytes
= cur
->len
= strtol( s
+ 1, &s
, 10 );
592 s
= cur
->val
= xmalloc( cur
->len
);
594 /* dump whats left over in the input buffer */
595 n
= imap
->buf
.bytes
- imap
->buf
.offset
;
598 /* the entire message fit in the buffer */
601 memcpy( s
, imap
->buf
.buf
+ imap
->buf
.offset
, n
);
605 /* mark that we used part of the buffer */
606 imap
->buf
.offset
+= n
;
608 /* now read the rest of the message */
610 if ((n
= socket_read (&imap
->buf
.sock
, s
, bytes
)) <= 0)
616 if (buffer_gets( &imap
->buf
, &s
))
618 } else if (*s
== '"') {
622 for (; *s
!= '"'; s
++)
627 cur
->val
= xmalloc( cur
->len
+ 1 );
628 memcpy( cur
->val
, p
, cur
->len
);
629 cur
->val
[cur
->len
] = 0;
633 for (; *s
&& !isspace( (unsigned char)*s
); s
++)
634 if (level
&& *s
== ')')
637 if (cur
->len
== 3 && !memcmp ("NIL", p
, 3))
640 cur
->val
= xmalloc( cur
->len
+ 1 );
641 memcpy( cur
->val
, p
, cur
->len
);
642 cur
->val
[cur
->len
] = 0;
661 parse_imap_list( imap_t
*imap
, char **sp
)
665 if (!parse_imap_list_l( imap
, sp
, &head
, 0 ))
672 parse_list( char **sp
)
674 return parse_imap_list( NULL
, sp
);
678 parse_capability( imap_t
*imap
, char *cmd
)
683 imap
->caps
= 0x80000000;
684 while ((arg
= next_arg( &cmd
)))
685 for (i
= 0; i
< ARRAY_SIZE(cap_list
); i
++)
686 if (!strcmp( cap_list
[i
], arg
))
687 imap
->caps
|= 1 << i
;
688 imap
->rcaps
= imap
->caps
;
692 parse_response_code( imap_store_t
*ctx
, struct imap_cmd_cb
*cb
, char *s
)
694 imap_t
*imap
= ctx
->imap
;
698 return RESP_OK
; /* no response code */
700 if (!(p
= strchr( s
, ']' ))) {
701 fprintf( stderr
, "IMAP error: malformed response code\n" );
705 arg
= next_arg( &s
);
706 if (!strcmp( "UIDVALIDITY", arg
)) {
707 if (!(arg
= next_arg( &s
)) || !(ctx
->gen
.uidvalidity
= atoi( arg
))) {
708 fprintf( stderr
, "IMAP error: malformed UIDVALIDITY status\n" );
711 } else if (!strcmp( "UIDNEXT", arg
)) {
712 if (!(arg
= next_arg( &s
)) || !(imap
->uidnext
= atoi( arg
))) {
713 fprintf( stderr
, "IMAP error: malformed NEXTUID status\n" );
716 } else if (!strcmp( "CAPABILITY", arg
)) {
717 parse_capability( imap
, s
);
718 } else if (!strcmp( "ALERT", arg
)) {
719 /* RFC2060 says that these messages MUST be displayed
722 for (; isspace( (unsigned char)*p
); p
++);
723 fprintf( stderr
, "*** IMAP ALERT *** %s\n", p
);
724 } else if (cb
&& cb
->ctx
&& !strcmp( "APPENDUID", arg
)) {
725 if (!(arg
= next_arg( &s
)) || !(ctx
->gen
.uidvalidity
= atoi( arg
)) ||
726 !(arg
= next_arg( &s
)) || !(*(int *)cb
->ctx
= atoi( arg
)))
728 fprintf( stderr
, "IMAP error: malformed APPENDUID status\n" );
736 get_cmd_result( imap_store_t
*ctx
, struct imap_cmd
*tcmd
)
738 imap_t
*imap
= ctx
->imap
;
739 struct imap_cmd
*cmdp
, **pcmdp
, *ncmdp
;
740 char *cmd
, *arg
, *arg1
, *p
;
741 int n
, resp
, resp2
, tag
;
744 if (buffer_gets( &imap
->buf
, &cmd
))
747 arg
= next_arg( &cmd
);
749 arg
= next_arg( &cmd
);
751 fprintf( stderr
, "IMAP error: unable to parse untagged response\n" );
755 if (!strcmp( "NAMESPACE", arg
)) {
756 imap
->ns_personal
= parse_list( &cmd
);
757 imap
->ns_other
= parse_list( &cmd
);
758 imap
->ns_shared
= parse_list( &cmd
);
759 } else if (!strcmp( "OK", arg
) || !strcmp( "BAD", arg
) ||
760 !strcmp( "NO", arg
) || !strcmp( "BYE", arg
)) {
761 if ((resp
= parse_response_code( ctx
, NULL
, cmd
)) != RESP_OK
)
763 } else if (!strcmp( "CAPABILITY", arg
))
764 parse_capability( imap
, cmd
);
765 else if ((arg1
= next_arg( &cmd
))) {
766 if (!strcmp( "EXISTS", arg1
))
767 ctx
->gen
.count
= atoi( arg
);
768 else if (!strcmp( "RECENT", arg1
))
769 ctx
->gen
.recent
= atoi( arg
);
771 fprintf( stderr
, "IMAP error: unable to parse untagged response\n" );
774 } else if (!imap
->in_progress
) {
775 fprintf( stderr
, "IMAP error: unexpected reply: %s %s\n", arg
, cmd
? cmd
: "" );
777 } else if (*arg
== '+') {
778 /* This can happen only with the last command underway, as
779 it enforces a round-trip. */
780 cmdp
= (struct imap_cmd
*)((char *)imap
->in_progress_append
-
781 offsetof(struct imap_cmd
, next
));
783 n
= socket_write( &imap
->buf
.sock
, cmdp
->cb
.data
, cmdp
->cb
.dlen
);
784 free( cmdp
->cb
.data
);
785 cmdp
->cb
.data
= NULL
;
786 if (n
!= (int)cmdp
->cb
.dlen
)
788 } else if (cmdp
->cb
.cont
) {
789 if (cmdp
->cb
.cont( ctx
, cmdp
, cmd
))
792 fprintf( stderr
, "IMAP error: unexpected command continuation request\n" );
795 if (socket_write( &imap
->buf
.sock
, "\r\n", 2 ) != 2)
798 imap
->literal_pending
= 0;
803 for (pcmdp
= &imap
->in_progress
; (cmdp
= *pcmdp
); pcmdp
= &cmdp
->next
)
804 if (cmdp
->tag
== tag
)
806 fprintf( stderr
, "IMAP error: unexpected tag %s\n", arg
);
809 if (!(*pcmdp
= cmdp
->next
))
810 imap
->in_progress_append
= pcmdp
;
811 imap
->num_in_progress
--;
812 if (cmdp
->cb
.cont
|| cmdp
->cb
.data
)
813 imap
->literal_pending
= 0;
814 arg
= next_arg( &cmd
);
815 if (!strcmp( "OK", arg
))
818 if (!strcmp( "NO", arg
)) {
819 if (cmdp
->cb
.create
&& cmd
&& (cmdp
->cb
.trycreate
|| !memcmp( cmd
, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND or UID COPY */
820 p
= strchr( cmdp
->cmd
, '"' );
821 if (!issue_imap_cmd( ctx
, NULL
, "CREATE \"%.*s\"", strchr( p
+ 1, '"' ) - p
+ 1, p
)) {
825 /* not waiting here violates the spec, but a server that does not
826 grok this nonetheless violates it too. */
828 if (!(ncmdp
= issue_imap_cmd( ctx
, &cmdp
->cb
, "%s", cmdp
->cmd
))) {
835 return 0; /* ignored */
841 } else /*if (!strcmp( "BAD", arg ))*/
843 fprintf( stderr
, "IMAP command '%s' returned response (%s) - %s\n",
844 memcmp (cmdp
->cmd
, "LOGIN", 5) ?
845 cmdp
->cmd
: "LOGIN <user> <pass>",
846 arg
, cmd
? cmd
: "");
848 if ((resp2
= parse_response_code( ctx
, &cmdp
->cb
, cmd
)) > resp
)
852 cmdp
->cb
.done( ctx
, cmdp
, resp
);
854 free( cmdp
->cb
.data
);
857 if (!tcmd
|| tcmd
== cmdp
)
865 imap_close_server( imap_store_t
*ictx
)
867 imap_t
*imap
= ictx
->imap
;
869 if (imap
->buf
.sock
.fd
!= -1) {
870 imap_exec( ictx
, NULL
, "LOGOUT" );
871 close( imap
->buf
.sock
.fd
);
873 free_list( imap
->ns_personal
);
874 free_list( imap
->ns_other
);
875 free_list( imap
->ns_shared
);
880 imap_close_store( store_t
*ctx
)
882 imap_close_server( (imap_store_t
*)ctx
);
883 free_generic_messages( ctx
->msgs
);
888 imap_open_store( imap_server_conf_t
*srvc
)
894 struct sockaddr_in addr
;
895 int s
, a
[2], preauth
;
898 ctx
= xcalloc( sizeof(*ctx
), 1 );
900 ctx
->imap
= imap
= xcalloc( sizeof(*imap
), 1 );
901 imap
->buf
.sock
.fd
= -1;
902 imap
->in_progress_append
= &imap
->in_progress
;
904 /* open connection to IMAP server */
907 imap_info( "Starting tunnel '%s'... ", srvc
->tunnel
);
909 if (socketpair( PF_UNIX
, SOCK_STREAM
, 0, a
)) {
910 perror( "socketpair" );
918 if (dup2( a
[0], 0 ) == -1 || dup2( a
[0], 1 ) == -1)
922 execl( "/bin/sh", "sh", "-c", srvc
->tunnel
, NULL
);
928 imap
->buf
.sock
.fd
= a
[1];
932 memset( &addr
, 0, sizeof(addr
) );
933 addr
.sin_port
= htons( srvc
->port
);
934 addr
.sin_family
= AF_INET
;
936 imap_info( "Resolving %s... ", srvc
->host
);
937 he
= gethostbyname( srvc
->host
);
939 perror( "gethostbyname" );
944 addr
.sin_addr
.s_addr
= *((int *) he
->h_addr_list
[0]);
946 s
= socket( PF_INET
, SOCK_STREAM
, 0 );
948 imap_info( "Connecting to %s:%hu... ", inet_ntoa( addr
.sin_addr
), ntohs( addr
.sin_port
) );
949 if (connect( s
, (struct sockaddr
*)&addr
, sizeof(addr
) )) {
956 imap
->buf
.sock
.fd
= s
;
960 /* read the greeting string */
961 if (buffer_gets( &imap
->buf
, &rsp
)) {
962 fprintf( stderr
, "IMAP error: no greeting response\n" );
965 arg
= next_arg( &rsp
);
966 if (!arg
|| *arg
!= '*' || (arg
= next_arg( &rsp
)) == NULL
) {
967 fprintf( stderr
, "IMAP error: invalid greeting response\n" );
971 if (!strcmp( "PREAUTH", arg
))
973 else if (strcmp( "OK", arg
) != 0) {
974 fprintf( stderr
, "IMAP error: unknown greeting response\n" );
977 parse_response_code( ctx
, NULL
, rsp
);
978 if (!imap
->caps
&& imap_exec( ctx
, NULL
, "CAPABILITY" ) != RESP_OK
)
983 imap_info ("Logging in...\n");
985 fprintf( stderr
, "Skipping server %s, no user\n", srvc
->host
);
990 sprintf( prompt
, "Password (%s@%s): ", srvc
->user
, srvc
->host
);
991 arg
= getpass( prompt
);
997 fprintf( stderr
, "Skipping account %s@%s, no password\n", srvc
->user
, srvc
->host
);
1001 * getpass() returns a pointer to a static buffer. make a copy
1002 * for long term storage.
1004 srvc
->pass
= xstrdup( arg
);
1007 fprintf( stderr
, "Skipping account %s@%s, server forbids LOGIN\n", srvc
->user
, srvc
->host
);
1010 imap_warn( "*** IMAP Warning *** Password is being sent in the clear\n" );
1011 if (imap_exec( ctx
, NULL
, "LOGIN \"%s\" \"%s\"", srvc
->user
, srvc
->pass
) != RESP_OK
) {
1012 fprintf( stderr
, "IMAP error: LOGIN failed\n" );
1019 return (store_t
*)ctx
;
1022 imap_close_store( &ctx
->gen
);
1027 imap_make_flags( int flags
, char *buf
)
1032 for (i
= d
= 0; i
< ARRAY_SIZE(Flags
); i
++)
1033 if (flags
& (1 << i
)) {
1036 for (s
= Flags
[i
]; *s
; s
++)
1047 imap_store_msg( store_t
*gctx
, msg_data_t
*data
, int *uid
)
1049 imap_store_t
*ctx
= (imap_store_t
*)gctx
;
1050 imap_t
*imap
= ctx
->imap
;
1051 struct imap_cmd_cb cb
;
1053 const char *prefix
, *box
;
1054 int ret
, i
, j
, d
, len
, extra
, nocr
;
1055 int start
, sbreak
= 0, ebreak
= 0;
1056 char flagstr
[128], tuid
[TUIDL
* 2 + 1];
1058 memset( &cb
, 0, sizeof(cb
) );
1064 if (!CAP(UIDPLUS
) && uid
) {
1068 if (fmap
[i
++] == '\n') {
1070 if (i
- 2 + nocr
== start
) {
1071 sbreak
= ebreak
= i
- 2 + nocr
;
1074 if (!memcmp( fmap
+ start
, "X-TUID: ", 8 )) {
1075 extra
-= (ebreak
= i
) - (sbreak
= start
) + nocr
;
1080 /* invalid message */
1084 for (j
= 0; j
< TUIDL
; j
++)
1085 sprintf( tuid
+ j
* 2, "%02x", arc4_getbyte() );
1086 extra
+= 8 + TUIDL
* 2 + 2;
1089 for (; i
< len
; i
++)
1090 if (fmap
[i
] == '\n')
1093 cb
.dlen
= len
+ extra
;
1094 buf
= cb
.data
= xmalloc( cb
.dlen
);
1096 if (!CAP(UIDPLUS
) && uid
) {
1098 for (; i
< sbreak
; i
++)
1099 if (fmap
[i
] == '\n') {
1105 memcpy( buf
, fmap
, sbreak
);
1108 memcpy( buf
, "X-TUID: ", 8 );
1110 memcpy( buf
, tuid
, TUIDL
* 2 );
1117 for (; i
< len
; i
++)
1118 if (fmap
[i
] == '\n') {
1124 memcpy( buf
, fmap
+ i
, len
- i
);
1130 d
= imap_make_flags( data
->flags
, flagstr
);
1136 box
= gctx
->conf
->trash
;
1137 prefix
= ctx
->prefix
;
1140 imap
->caps
= imap
->rcaps
& ~(1 << LITERALPLUS
);
1143 prefix
= !strcmp( box
, "INBOX" ) ? "" : ctx
->prefix
;
1147 ret
= imap_exec_m( ctx
, &cb
, "APPEND \"%s%s\" %s", prefix
, box
, flagstr
);
1148 imap
->caps
= imap
->rcaps
;
1159 #define CHUNKSIZE 0x1000
1162 read_message( FILE *f
, msg_data_t
*msg
)
1166 memset(msg
, 0, sizeof(*msg
));
1167 strbuf_init(&buf
, 0);
1170 if (strbuf_fread(&buf
, CHUNKSIZE
, f
) <= 0)
1175 msg
->data
= strbuf_detach(&buf
);
1180 count_messages( msg_data_t
*msg
)
1183 char *p
= msg
->data
;
1186 if (!prefixcmp(p
, "From ")) {
1190 p
= strstr( p
+5, "\nFrom ");
1199 split_msg( msg_data_t
*all_msgs
, msg_data_t
*msg
, int *ofs
)
1203 memset( msg
, 0, sizeof *msg
);
1204 if (*ofs
>= all_msgs
->len
)
1207 data
= &all_msgs
->data
[ *ofs
];
1208 msg
->len
= all_msgs
->len
- *ofs
;
1210 if (msg
->len
< 5 || prefixcmp(data
, "From "))
1213 p
= strchr( data
, '\n' );
1221 p
= strstr( data
, "\nFrom " );
1223 msg
->len
= &p
[1] - data
;
1225 msg
->data
= xmalloc( msg
->len
+ 1 );
1229 memcpy( msg
->data
, data
, msg
->len
);
1230 msg
->data
[ msg
->len
] = 0;
1236 static imap_server_conf_t server
=
1246 static char *imap_folder
;
1249 git_imap_config(const char *key
, const char *val
)
1251 char imap_key
[] = "imap.";
1253 if (strncmp( key
, imap_key
, sizeof imap_key
- 1 ))
1255 key
+= sizeof imap_key
- 1;
1257 if (!strcmp( "folder", key
)) {
1258 imap_folder
= xstrdup( val
);
1259 } else if (!strcmp( "host", key
)) {
1261 if (!prefixcmp(val
, "imap:"))
1266 if (!prefixcmp(val
, "//"))
1268 server
.host
= xstrdup( val
);
1270 else if (!strcmp( "user", key
))
1271 server
.user
= xstrdup( val
);
1272 else if (!strcmp( "pass", key
))
1273 server
.pass
= xstrdup( val
);
1274 else if (!strcmp( "port", key
))
1275 server
.port
= git_config_int( key
, val
);
1276 else if (!strcmp( "tunnel", key
))
1277 server
.tunnel
= xstrdup( val
);
1282 main(int argc
, char **argv
)
1284 msg_data_t all_msgs
, msg
;
1285 store_t
*ctx
= NULL
;
1291 /* init the random number generator */
1294 git_config( git_imap_config
);
1297 fprintf( stderr
, "no imap store specified\n" );
1301 /* read the messages */
1302 if (!read_message( stdin
, &all_msgs
)) {
1303 fprintf(stderr
,"nothing to send\n");
1307 total
= count_messages( &all_msgs
);
1309 fprintf(stderr
,"no messages to send\n");
1313 /* write it to the imap server */
1314 ctx
= imap_open_store( &server
);
1316 fprintf( stderr
,"failed to open store\n");
1320 fprintf( stderr
, "sending %d message%s\n", total
, (total
!=1)?"s":"" );
1321 ctx
->name
= imap_folder
;
1323 unsigned percent
= n
* 100 / total
;
1324 fprintf( stderr
, "%4u%% (%d/%d) done\r", percent
, n
, total
);
1325 if (!split_msg( &all_msgs
, &msg
, &ofs
))
1327 r
= imap_store_msg( ctx
, &msg
, &uid
);
1328 if (r
!= DRV_OK
) break;
1331 fprintf( stderr
,"\n" );
1333 imap_close_store( ctx
);