@@ -128,7 +128,6 @@ struct imap_cmd_cb {
128
128
char * data ;
129
129
int dlen ;
130
130
int uid ;
131
- unsigned create :1 , trycreate :1 ;
132
131
};
133
132
134
133
struct imap_cmd {
@@ -493,9 +492,9 @@ static int nfsnprintf(char *buf, int blen, const char *fmt, ...)
493
492
return ret ;
494
493
}
495
494
496
- static struct imap_cmd * v_issue_imap_cmd (struct imap_store * ctx ,
497
- struct imap_cmd_cb * cb ,
498
- const char * fmt , va_list ap )
495
+ static struct imap_cmd * issue_imap_cmd (struct imap_store * ctx ,
496
+ struct imap_cmd_cb * cb ,
497
+ const char * fmt , va_list ap )
499
498
{
500
499
struct imap * imap = ctx -> imap ;
501
500
struct imap_cmd * cmd ;
@@ -558,20 +557,6 @@ static struct imap_cmd *v_issue_imap_cmd(struct imap_store *ctx,
558
557
return cmd ;
559
558
}
560
559
561
- __attribute__((format (printf , 3 , 4 )))
562
- static struct imap_cmd * issue_imap_cmd (struct imap_store * ctx ,
563
- struct imap_cmd_cb * cb ,
564
- const char * fmt , ...)
565
- {
566
- struct imap_cmd * ret ;
567
- va_list ap ;
568
-
569
- va_start (ap , fmt );
570
- ret = v_issue_imap_cmd (ctx , cb , fmt , ap );
571
- va_end (ap );
572
- return ret ;
573
- }
574
-
575
560
__attribute__((format (printf , 3 , 4 )))
576
561
static int imap_exec (struct imap_store * ctx , struct imap_cmd_cb * cb ,
577
562
const char * fmt , ...)
@@ -580,7 +565,7 @@ static int imap_exec(struct imap_store *ctx, struct imap_cmd_cb *cb,
580
565
struct imap_cmd * cmdp ;
581
566
582
567
va_start (ap , fmt );
583
- cmdp = v_issue_imap_cmd (ctx , cb , fmt , ap );
568
+ cmdp = issue_imap_cmd (ctx , cb , fmt , ap );
584
569
va_end (ap );
585
570
if (!cmdp )
586
571
return RESP_BAD ;
@@ -596,7 +581,7 @@ static int imap_exec_m(struct imap_store *ctx, struct imap_cmd_cb *cb,
596
581
struct imap_cmd * cmdp ;
597
582
598
583
va_start (ap , fmt );
599
- cmdp = v_issue_imap_cmd (ctx , cb , fmt , ap );
584
+ cmdp = issue_imap_cmd (ctx , cb , fmt , ap );
600
585
va_end (ap );
601
586
if (!cmdp )
602
587
return DRV_STORE_BAD ;
@@ -714,8 +699,8 @@ static int parse_response_code(struct imap_store *ctx, struct imap_cmd_cb *cb,
714
699
static int get_cmd_result (struct imap_store * ctx , struct imap_cmd * tcmd )
715
700
{
716
701
struct imap * imap = ctx -> imap ;
717
- struct imap_cmd * cmdp , * * pcmdp , * ncmdp ;
718
- char * cmd , * arg , * arg1 , * p ;
702
+ struct imap_cmd * cmdp , * * pcmdp ;
703
+ char * cmd , * arg , * arg1 ;
719
704
int n , resp , resp2 , tag ;
720
705
721
706
for (;;) {
@@ -801,30 +786,9 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
801
786
if (!strcmp ("OK" , arg ))
802
787
resp = DRV_OK ;
803
788
else {
804
- if (!strcmp ("NO" , arg )) {
805
- if (cmdp -> cb .create && cmd && (cmdp -> cb .trycreate || !memcmp (cmd , "[TRYCREATE]" , 11 ))) { /* SELECT, APPEND or UID COPY */
806
- p = strchr (cmdp -> cmd , '"' );
807
- if (!issue_imap_cmd (ctx , NULL , "CREATE \"%.*s\"" , (int )(strchr (p + 1 , '"' ) - p + 1 ), p )) {
808
- resp = RESP_BAD ;
809
- goto normal ;
810
- }
811
- /* not waiting here violates the spec, but a server that does not
812
- grok this nonetheless violates it too. */
813
- cmdp -> cb .create = 0 ;
814
- if (!(ncmdp = issue_imap_cmd (ctx , & cmdp -> cb , "%s" , cmdp -> cmd ))) {
815
- resp = RESP_BAD ;
816
- goto normal ;
817
- }
818
- free (cmdp -> cmd );
819
- free (cmdp );
820
- if (!tcmd )
821
- return 0 ; /* ignored */
822
- if (cmdp == tcmd )
823
- tcmd = ncmdp ;
824
- continue ;
825
- }
789
+ if (!strcmp ("NO" , arg ))
826
790
resp = RESP_NO ;
827
- } else /*if (!strcmp("BAD", arg))*/
791
+ else /*if (!strcmp("BAD", arg))*/
828
792
resp = RESP_BAD ;
829
793
fprintf (stderr , "IMAP command '%s' returned response (%s) - %s\n" ,
830
794
memcmp (cmdp -> cmd , "LOGIN" , 5 ) ?
@@ -833,7 +797,6 @@ static int get_cmd_result(struct imap_store *ctx, struct imap_cmd *tcmd)
833
797
}
834
798
if ((resp2 = parse_response_code (ctx , & cmdp -> cb , cmd )) > resp )
835
799
resp = resp2 ;
836
- normal :
837
800
if (cmdp -> cb .done )
838
801
cmdp -> cb .done (ctx , cmdp , resp );
839
802
free (cmdp -> cb .data );
@@ -944,7 +907,7 @@ static int auth_cram_md5(struct imap_store *ctx, struct imap_cmd *cmd, const cha
944
907
return 0 ;
945
908
}
946
909
947
- static struct imap_store * imap_open_store (struct imap_server_conf * srvc )
910
+ static struct imap_store * imap_open_store (struct imap_server_conf * srvc , char * folder )
948
911
{
949
912
struct credential cred = CREDENTIAL_INIT ;
950
913
struct imap_store * ctx ;
@@ -1156,6 +1119,25 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
1156
1119
credential_approve (& cred );
1157
1120
credential_clear (& cred );
1158
1121
1122
+ /* check the target mailbox exists */
1123
+ ctx -> name = folder ;
1124
+ switch (imap_exec (ctx , NULL , "EXAMINE \"%s\"" , ctx -> name )) {
1125
+ case RESP_OK :
1126
+ /* ok */
1127
+ break ;
1128
+ case RESP_BAD :
1129
+ fprintf (stderr , "IMAP error: could not check mailbox\n" );
1130
+ goto out ;
1131
+ case RESP_NO :
1132
+ if (imap_exec (ctx , NULL , "CREATE \"%s\"" , ctx -> name ) == RESP_OK ) {
1133
+ imap_info ("Created missing mailbox\n" );
1134
+ } else {
1135
+ fprintf (stderr , "IMAP error: could not create missing mailbox\n" );
1136
+ goto out ;
1137
+ }
1138
+ break ;
1139
+ }
1140
+
1159
1141
ctx -> prefix = "" ;
1160
1142
return ctx ;
1161
1143
@@ -1164,6 +1146,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc)
1164
1146
credential_reject (& cred );
1165
1147
credential_clear (& cred );
1166
1148
1149
+ out :
1167
1150
imap_close_store (ctx );
1168
1151
return NULL ;
1169
1152
}
@@ -1219,7 +1202,6 @@ static int imap_store_msg(struct imap_store *ctx, struct strbuf *msg)
1219
1202
1220
1203
box = ctx -> name ;
1221
1204
prefix = !strcmp (box , "INBOX" ) ? "" : ctx -> prefix ;
1222
- cb .create = 0 ;
1223
1205
ret = imap_exec_m (ctx , & cb , "APPEND \"%s%s\" " , prefix , box );
1224
1206
imap -> caps = imap -> rcaps ;
1225
1207
if (ret != DRV_OK )
@@ -1418,14 +1400,13 @@ int main(int argc, char **argv)
1418
1400
}
1419
1401
1420
1402
/* write it to the imap server */
1421
- ctx = imap_open_store (& server );
1403
+ ctx = imap_open_store (& server , imap_folder );
1422
1404
if (!ctx ) {
1423
1405
fprintf (stderr , "failed to open store\n" );
1424
1406
return 1 ;
1425
1407
}
1426
1408
1427
1409
fprintf (stderr , "sending %d message%s\n" , total , (total != 1 ) ? "s" : "" );
1428
- ctx -> name = imap_folder ;
1429
1410
while (1 ) {
1430
1411
unsigned percent = n * 100 / total ;
1431
1412
0 commit comments