[PATCH 1/2] Automatic tagging based on maildir

Michal Nazarewicz mina86 at mina86.com
Fri Oct 26 06:19:54 PDT 2012


On Fri, Oct 26 2012, Taesoo Kim <taesoo at mit.edu> wrote:
> @@ -240,6 +242,32 @@ _entry_in_ignore_list (const char *entry, add_files_state_t *state)
>      return FALSE;
>  }
>  
> +static void
> +_add_maildir_as_tag(notmuch_database_t *notmuch,
> +		    notmuch_message_t *msg, const char *path) 
> +{
> +    char *tok = talloc_strdup (notmuch, path);
> +    int len = strlen(tok);
> +    
> +    /* asserts path ends with /cur|/tmp|/new */

It actually does not assert that.  It just checks “/”.

> +    if (len > 4 && tok[len - 4] == '/') {
> +	char *iter = tok + len - 4;
> +	*iter = '\0';
> +	while (-- iter && iter >= tok) {
> +	    char c = *iter;
> +	    if (c == '/' || c == '.') {
> +		*iter = '\0';
> +		notmuch_message_add_tag (msg, iter + 1);
> +		if (c == '/') {
> +		    break;
> +		}
> +	    }
> +	    *iter = tolower(*iter);
> +	}

strtok() would make it easier.

> +    }
> +    talloc_free (tok);
> +}

How about (not tested):

static void
_add_maildir_as_tag(notmuch_database_t *notmuch,
		    notmuch_message_t *msg, const char *path)
{
    const char *a = NULL, *b = NULL;
    char *dir, *tag, *saveptr;

    while ((path = strchr(path, '/'))) {
	a = b;
	b = path;
    }

    if (!a || ++a >= b ||
	(strcmp(b, "/cur") && strcmp(b, "/new") && strcmp(b, "/tmp")))
	return;

    dir = talloc_strndup(notmuch, a, b - a);
    while ((tag = strtok_r(dir, ".", &saveptr)))
	notmuch_message_add_tag(msg, tag);

    talloc_free(dir);
}

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michał “mina86” Nazarewicz    (o o)
ooo +----<email/xmpp: mpn at google.com>--------------ooO--(_)--Ooo--
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://notmuchmail.org/pipermail/notmuch/attachments/20121026/80a0a639/attachment.pgp>


More information about the notmuch mailing list