Skip to content

Instantly share code, notes, and snippets.

@orivej
Forked from stepahn/isync_recursive_imap-1.0.4.diff
Last active December 17, 2015 00:10
Show Gist options
  • Save orivej/5519020 to your computer and use it in GitHub Desktop.
Save orivej/5519020 to your computer and use it in GitHub Desktop.
From 2a6144e940e27fdf39b42454ec8a6b6ff2589d93 Mon Sep 17 00:00:00 2001
From: Orivej Desh <[email protected]>
Date: Sun, 5 May 2013 02:29:41 +0400
Subject: [PATCH] recursive-imap
---
src/drv_imap.c | 2 +-
src/drv_maildir.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 60 insertions(+), 5 deletions(-)
diff --git a/src/drv_imap.c b/src/drv_imap.c
index 4c27bcf..100f1b2 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -1751,7 +1751,7 @@ imap_list( store_t *gctx, string_list_t **retb )
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;
diff --git a/src/drv_maildir.c b/src/drv_maildir.c
index 4383ec2..5da49cd 100644
--- a/src/drv_maildir.c
+++ b/src/drv_maildir.c
@@ -24,6 +24,7 @@
#include "isync.h"
+#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
@@ -46,6 +47,56 @@
#include <db.h>
#endif /* USE_DB */
+static void encode_maildir_box(const char* in, char* out, size_t size)
+{
+ const char* p;
+ char c;
+ size_t out_chars;
+
+ for (p = in, out_chars = 0; (c = *p); ++p, ++out, ++out_chars) {
+ assert(out_chars < size);
+ if (c == '/') {
+ assert(out_chars < size - 1);
+ *(out++) = '~';
+ *out = '-';
+ ++out_chars;
+ }
+ else if (c == '~') {
+ assert(out_chars < size - 1);
+ *(out++) = '~';
+ *out = '~';
+ ++out_chars;
+ }
+ else {
+ *out = c;
+ }
+ }
+ assert(out_chars < size);
+ *out = 0;
+}
+
+static void decode_maildir_box(const char* in, char* out, size_t size)
+{
+ const char* p;
+ char c;
+ size_t out_chars;
+
+ for (p = in, out_chars = 0; (c = *p); ++p, ++out, ++out_chars) {
+ assert(out_chars < size);
+ if (c == '~') {
+ assert(out_chars < size - 1);
+ c = *(++p);
+ *out = (c == '-' ? '/' : '~');
+ ++out_chars;
+ }
+ else {
+ *out = c;
+ }
+ }
+ assert(out_chars < size);
+ *out = 0;
+}
+
typedef struct maildir_store_conf {
store_conf_t gen;
char *inbox;
@@ -164,14 +215,15 @@ maildir_list( store_t *gctx, string_list_t **retb )
const char *inbox = ((maildir_store_conf_t *)gctx->conf)->inbox;
int bl;
struct stat st;
- char buf[PATH_MAX];
+ char buf[PATH_MAX], box[PATH_MAX];
if (*de->d_name == '.')
continue;
bl = nfsnprintf( buf, sizeof(buf), "%s%s/cur", gctx->conf->path, de->d_name );
if (stat( buf, &st ) || !S_ISDIR(st.st_mode))
continue;
- add_string_list( retb, !memcmp( buf, inbox, bl - 4 ) && !inbox[bl - 4] ? "INBOX" : de->d_name );
+ decode_maildir_box(de->d_name, box, PATH_MAX);
+ add_string_list( retb, !memcmp( buf, inbox, bl - 4 ) && !inbox[bl - 4] ? "INBOX" : box );
}
closedir (dir);
@@ -717,8 +769,11 @@ maildir_prepare( store_t *gctx, int opts )
#endif /* USE_DB */
if (!strcmp( gctx->name, "INBOX" ))
gctx->path = nfstrdup( ((maildir_store_conf_t *)gctx->conf)->inbox );
- else
- nfasprintf( &gctx->path, "%s%s", gctx->conf->path, gctx->name );
+ else {
+ char box[_POSIX_PATH_MAX];
+ encode_maildir_box(gctx->name, box, _POSIX_PATH_MAX);
+ nfasprintf( &gctx->path, "%s%s", gctx->conf->path, box );
+ }
if (opts & OPEN_SETFLAGS)
opts |= OPEN_OLD;
if (opts & OPEN_EXPUNGE)
--
1.8.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment