aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/corpus/cur/32:2,
blob: c1633cdddd75882cad8713daec1f5a6db9834032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
From: "Jan Janak" <jan@ryngle.com>
To: notmuch@notmuchmail.org
Date: Wed, 18 Nov 2009 05:57:03 +0100
Subject: [notmuch] [PATCH] notmuch new: Support for conversion of spool
	subdirectories into tags
Message-ID: <1258520223-15328-1-git-send-email-jan@ryngle.com>

This patch makes it possible to convert subdirectory names inside the
spool directory into message tags. Messages stored in subdirectory
"foo" will be marked with tag "foo". Message duplicates found in several
subdirectories will be given one tag per subdirectory.

This feature can be used to synchronize notmuch's tags with with gmail
tags, for example. Gmail IMAP servers convert tags to IMAP
subdirectories and those subdirectories can be converted back to tags
in notmuch.

The patch modifies notmuch_database_add_message function to return a
pointer to the message even if a message duplicate was found in the
database. This is needed if we want to add a tag for each subdirectory
in which a message duplicate was found.

In addition to that, it makes the pointer to notmuch_config_t global
(previously it was a local variable in notmuch_new_command). The
configuration data structure is used by the function which converts
subdirectory names to tags.

Finally, there is a new function called subdir_to_tag. The function
extracts the name of the subdirectory inside the spool from the full
path of the message (also removing Maildir's cur,dir,and tmp
subdirectories) and adds it as a new tag to the message.

Signed-off-by: Jan Janak <jan at ryngle.com>
---
 lib/database.cc |    3 +-
 notmuch-new.c   |   74 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 74 insertions(+), 3 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 3c8d626..f7799d2 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -949,7 +949,8 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
 
   DONE:
     if (message) {
-	if (ret == NOTMUCH_STATUS_SUCCESS && message_ret)
+		if ((ret == NOTMUCH_STATUS_SUCCESS ||
+			 ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) && message_ret)
 	    *message_ret = message;
 	else
 	    notmuch_message_destroy (message);
diff --git a/notmuch-new.c b/notmuch-new.c
index 83a05ba..d94ce16 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -19,6 +19,9 @@
  */
 
 #include "notmuch-client.h"
+#include <libgen.h>
+
+static notmuch_config_t *config = 0;
 
 static volatile sig_atomic_t do_add_files_print_progress = 0;
 
@@ -45,6 +48,69 @@ tag_inbox_and_unread (notmuch_message_t *message)
     notmuch_message_add_tag (message, "unread");
 }
 
+/*
+ * Extracts the sub-directory from the filename and adds it as a new tag to
+ * the message. The filename must begin with the database directory configured
+ * in the configuration file. This prefix is then removed. If the remaining
+ * sub-directory ends with one of the Maildir special directories (/tmp, /new,
+ * /cur) then they are removed as well. If there is anything left then the
+ * function adds it as a new tag to the message.
+ *
+ * The function does nothing if it cannot extract a sub-directory from
+ * filename.
+ */
+static void
+subdir_to_tag (char* filename, notmuch_message_t *message)
+{
+	const char* db_path;
+	char *msg_dir, *tmp;
+	int db_path_len, msg_dir_len;
+
+	if (config == NULL) return;
+    db_path = notmuch_config_get_database_path (config);
+	if (db_path == NULL) return;
+	db_path_len = strlen(db_path);
+
+	/* Make a copy of the string as dirname may need to modify it. */
+	tmp = talloc_strdup(message, filename);
+	msg_dir = dirname(tmp);
+	msg_dir_len = strlen(msg_dir);
+
+	/* If msg_dir starts with db_path, remove it, including the / which delimits
+	 * it from the rest of the directory name. */
+	if (db_path_len < msg_dir_len &&
+		!strncmp(db_path, msg_dir, db_path_len)) {
+		msg_dir += db_path_len + 1;
+		msg_dir_len -= db_path_len + 1;
+	} else {
+		/* If we get here, either the message filename is not inside the
+		 * database directory configured in the configuration file, or it is a
+		 * file in the root directory of the database. Either way we just skip
+		 * it because we do not know how to convert it to a meaningful
+		 * subdirectory string that we could add as tag. */
+		goto out;
+	}
+
+	/* Special conditioning for Maildirs. If the remainder of the directory
+	 * name ends with /new, /cur, or /tmp then remove it. */
+	if ((msg_dir_len >= 4) &&
+		(!strncmp(msg_dir + msg_dir_len - 4, "/new", 4) ||
+		 !strncmp(msg_dir + msg_dir_len - 4, "/cur", 4) ||
+		 !strncmp(msg_dir + msg_dir_len - 4, "/tmp", 4))) {
+		msg_dir[msg_dir_len - 4] = '\0';
+	}
+
+	/* If, after all the modifications, we still have a subdirectory, add it
+	 * as tag. */
+	if (strlen(msg_dir)) {
+		notmuch_message_add_tag (message, msg_dir);
+	}
+
+out:
+	talloc_free(tmp);
+}
+
+
 static void
 add_files_print_progress (add_files_state_t *state)
 {
@@ -186,10 +252,15 @@ add_files_recursive (notmuch_database_t *notmuch,
 		    case NOTMUCH_STATUS_SUCCESS:
 			state->added_messages++;
 			tag_inbox_and_unread (message);
+			subdir_to_tag(next, message);
 			break;
 		    /* Non-fatal issues (go on to next file) */
 		    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
-		        /* Stay silent on this one. */
+		        /* Stay silent on this one. The message already exists in the
+				 * database, that means we may have found a duplicate in
+				 * another directory. If that's the case then we add another
+				 * tag to the message with the sub-directory. */
+				subdir_to_tag(next, message);
 			break;
 		    case NOTMUCH_STATUS_FILE_NOT_EMAIL:
 			fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
@@ -386,7 +457,6 @@ int
 notmuch_new_command (void *ctx,
 		     unused (int argc), unused (char *argv[]))
 {
-    notmuch_config_t *config;
     notmuch_database_t *notmuch;
     add_files_state_t add_files_state;
     double elapsed;
-- 
1.6.3.3