From 79e19d3d15894e4b314d6874916973b81090a9af Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 4 Dec 2023 15:23:04 -0800 Subject: [PATCH] add a very limited INCLUDE_ONLY option --- src/drv_maildir.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/drv_maildir.c b/src/drv_maildir.c index 6080877..61f67d6 100644 --- a/src/drv_maildir.c +++ b/src/drv_maildir.c @@ -920,6 +920,28 @@ maildir_compare( const void *l, const void *r ) return strcmp( lm->base, rm->base ); } +int is_gmail(const char* name) { + // NOTE: This is not an exact science. For instance, there is a good + // shot that mail sent from containers will end up matching this pattern + char token[16]; // If host name is longer than 12, then we already have our answer + + // we expect files in the form of: "1700172103.R12128272304961211247.hostname,U=27:2,S" + // we are looking for "hostname" + // All gmail mails have a hostname in the form of "ff6d55f971cc". + // All are 12 characters long + // All are hexadecimal + if (sscanf(name, "%*[^.].%*[^.].%15[^,]", token) == 1) { + token[14] = '\0'; + size_t len = strlen(token); + if (len != 12) return 0; + if (strspn(token, "0123456789abcdef") == len) return 1; + } + + // We are here because a) the input format is not as expected, + // or b) the hostname is not gmail + return 0; +} + static int maildir_scan( maildir_store_t *ctx, msg_t_array_alloc_t *msglist ) { @@ -992,11 +1014,24 @@ maildir_scan( maildir_store_t *ctx, msg_t_array_alloc_t *msglist ) return DRV_BOX_BAD; } const char *filter = getenv("MBSYNC_MAILDIR_IGNORE"); + const char *MAGIC_INCLUDE = "^[^\\.]+\\.[^\\.]+\\.[0-9a-f]{12}\\,.*"; + char *include = getenv("MBSYNC_MAILDIR_INCLUDE_ONLY"); + if (include && strncmp(MAGIC_INCLUDE, include, strlen(MAGIC_INCLUDE)) != 0) { + include = NULL; + error("MBSYNC_MAILDIR_INCLUDE_ONLY can only be '%s'\n", MAGIC_INCLUDE); + } + if (include && filter) { + include = NULL; + error("MBSYNC_MAILDIR_IGNORE cannot be used with MBSYNC_MAILDIR_INCLUDE_ONLY. INCLUDE_ONLY will be ignored\n"); + } + while ((e = readdir( d ))) { if (*e->d_name == '.') continue; if (filter && strstr(e->d_name, filter)) continue; + if (include && !is_gmail(e->d_name)) + continue; ctx->total_msgs++; ctx->recent_msgs += i; #ifdef USE_DB