Hi!

I needed to synchronize two IMAP accounts with hierarchical mailboxes. Mbsync missed this feature, so I have implemented it. Here is the patch.
diff -ur isync-1.0.2.orig/src/drv_imap.c isync-1.0.2/src/drv_imap.c
--- isync-1.0.2.orig/src/drv_imap.c     2006-01-31 16:13:59.000000000 +0100
+++ isync-1.0.2/src/drv_imap.c  2006-11-03 00:27:15.000000000 +0100
@@ -50,6 +50,8 @@
 # include <openssl/hmac.h>
 #endif
 
+#define DELIMITER      ((char)0xff)
+
 typedef struct imap_server_conf {
        struct imap_server_conf *next;
        char *name;
@@ -126,6 +128,7 @@
        store_t gen;
        imap_t *imap;
        const char *prefix;
+       char delimiter;
        unsigned /*currentnc:1,*/ trashnc:1;
 } imap_store_t;
 
@@ -187,6 +190,26 @@
 
 #if HAVE_LIBSSL
 
+static char* 
+convert_mailbox_name( imap_store_t *ctx, char *src )
+{
+       char *p;
+
+       for( p = src; *p; p++ )
+               if ( *p == DELIMITER )
+                       *p = ctx->delimiter;
+       return src;
+}
+
+static char* 
+get_mailbox_name( imap_store_t *ctx, const char *src )
+{
+       char *dst;
+
+       dst = strdup( src );
+       return convert_mailbox_name( ctx, dst );
+}
+
 /* this gets called when a certificate is to be verified */
 static int
 verify_cert( SSL *ssl )
@@ -888,7 +911,7 @@
 parse_list_rsp( imap_store_t *ctx, char *cmd )
 {
        imap_t *imap = ctx->imap;
-       char *arg;
+       char *arg, *p;
        list_t *list, *lp;
        int l;
 
@@ -900,7 +923,8 @@
                                return;
                        }
        free_list( list );
-       (void) next_arg( &cmd ); /* skip delimiter */
+       arg = next_arg( &cmd );
+       ctx->delimiter = arg[ 0 ];
        arg = next_arg( &cmd );
        l = strlen( ctx->gen.conf->path );
        if (memcmp( arg, ctx->gen.conf->path, l ))
@@ -908,6 +932,9 @@
        arg += l;
        if (!memcmp( arg + strlen( arg ) - 5, ".lock", 5 )) /* workaround 
broken servers */
                return;
+       for( p = arg; *p != 0; p++ )
+               if ( *p == ctx->delimiter )
+                       *p = DELIMITER;
        add_string_list( &imap->boxes, arg );
 }
 
@@ -916,8 +943,8 @@
 {
        imap_t *imap = ctx->imap;
        struct imap_cmd *cmdp, **pcmdp, *ncmdp;
-       char *cmd, *arg, *arg1, *p;
-       int n, resp, resp2, tag;
+       char *cmd, *arg, *arg1, *p, *s;
+       int n, resp, resp2, tag, len;
 
        for (;;) {
                if (buffer_gets( &imap->buf, &cmd ))
@@ -1005,10 +1032,17 @@
                                if (!strcmp( "NO", arg )) {
                                        if (cmdp->cb.create && cmd && 
(cmdp->cb.trycreate || !memcmp( cmd, "[TRYCREATE]", 11 ))) { /* SELECT, APPEND 
or UID COPY */
                                                p = strchr( cmdp->cmd, '"' );
-                                               if (!issue_imap_cmd( ctx, 0, 
"CREATE %.*s", strchr( p + 1, '"' ) - p + 1, p )) {
+                                               len = strchr( p + 1, '"' ) - p 
+ 1;     
+                                               s = malloc( len + 1 );
+                                               strncpy( s, p, len );
+                                               s[ len ] = 0;
+                                               convert_mailbox_name( ctx, s );
+                                               if (!issue_imap_cmd( ctx, 0, 
"CREATE %s", s )) {
                                                        resp = RESP_BAD;
+                                                       free( s );
                                                        goto normal;
                                                }
+                                               free( s );
                                                /* not waiting here violates 
the spec, but a server that does not
                                                   grok this nonetheless 
violates it too. */
                                                cmdp->cb.create = 0;
@@ -1414,6 +1448,7 @@
        int ret, i, j, bl;
        struct imap_cmd_cb cb;
        char buf[1000];
+       char *box = 0;
 
 
        if (!strcmp( gctx->name, "INBOX" )) {
@@ -1425,9 +1460,10 @@
        }
 
        memset( &cb, 0, sizeof(cb) );
+       box = get_mailbox_name( ctx, gctx->name ); 
        cb.create = (gctx->opts & OPEN_CREATE) != 0;
        cb.trycreate = 1;
-       if ((ret = imap_exec_b( ctx, &cb, "SELECT \"%s%s\"", prefix, gctx->name 
)) != DRV_OK)
+       if ((ret = imap_exec_b( ctx, &cb, "SELECT \"%s%s\"", prefix, box )) != 
DRV_OK)
                goto bail;
 
        if (gctx->count) {
@@ -1460,6 +1496,8 @@
        ret = DRV_OK;
 
   bail:
+       if ( box )
+               free( box );
        if (excs)
                free( excs );
        return ret;
@@ -1646,7 +1684,9 @@
                        imap->caps = imap->rcaps & ~(1 << LITERALPLUS);*/
        }
        cb.ctx = uid;
+       box = get_mailbox_name( ctx, box );
        ret = imap_exec_m( ctx, &cb, "APPEND \"%s%s\" %s", prefix, box, flagstr 
);
+       free( (char*) box );
        imap->caps = imap->rcaps;
        if (ret != DRV_OK)
                return ret;
@@ -1674,7 +1714,7 @@
        int ret;
 
        imap->boxes = 0;
-       if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s%%\"", ctx->prefix )) != 
DRV_OK)
+       if ((ret = imap_exec_b( ctx, 0, "LIST \"\" \"%s*\"", ctx->prefix )) != 
DRV_OK)
                return ret;
        *retb = imap->boxes;
        return DRV_OK;
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
isync-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to