add a very limited INCLUDE_ONLY option

This commit is contained in:
Emil Lerch 2023-12-04 15:23:04 -08:00
parent e0690b07eb
commit 79e19d3d15
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -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