]> git.notmuchmail.org Git - notmuch/blob - tag-util.h
notmuch-restore: handle empty input file, leading blank lines and comments.
[notmuch] / tag-util.h
1 #ifndef _TAG_UTIL_H
2 #define _TAG_UTIL_H
3
4 #include "notmuch-client.h"
5
6 typedef struct _tag_operation_t tag_operation_t;
7 typedef struct _tag_op_list_t tag_op_list_t;
8
9 /* Use powers of 2 */
10 typedef enum {
11     TAG_FLAG_NONE = 0,
12
13     /* Operations are synced to maildir, if possible.
14      */
15     TAG_FLAG_MAILDIR_SYNC = (1 << 0),
16
17     /* Remove all tags from message before applying list.
18      */
19     TAG_FLAG_REMOVE_ALL = (1 << 1),
20
21     /* Don't try to avoid database operations. Useful when we
22      * know that message passed needs these operations.
23      */
24     TAG_FLAG_PRE_OPTIMIZED = (1 << 2),
25
26     /* Accept strange tags that might be user error;
27      * intended for use by notmuch-restore.
28      */
29     TAG_FLAG_BE_GENEROUS = (1 << 3)
30
31 } tag_op_flag_t;
32
33 /* These should obey the convention that fatal errors are negative,
34  * skipped lines are positive.
35  */
36 typedef enum {
37     TAG_PARSE_OUT_OF_MEMORY = -1,
38
39     /* Line parsed successfuly. */
40     TAG_PARSE_SUCCESS = 0,
41
42     /* Line has a syntax error */
43     TAG_PARSE_INVALID = 1,
44
45     /* Line was blank or a comment */
46     TAG_PARSE_SKIPPED = 2
47
48 } tag_parse_status_t;
49
50 /* Parse a string of the following format:
51  *
52  * +<tag>|-<tag> [...] [--] <search-terms>
53  *
54  * Each line is interpreted similarly to "notmuch tag" command line
55  * arguments. The delimiter is one or more spaces ' '. Any characters
56  * in <tag> and <search-terms> MAY be hex encoded with %NN where NN is
57  * the hexadecimal value of the character. Any ' ' and '%' characters
58  * in <tag> and <search-terms> MUST be hex encoded (using %20 and %25,
59  * respectively). Any characters that are not part of <tag> or
60  * <search-terms> MUST NOT be hex encoded.
61  *
62  * Leading and trailing space ' ' is ignored. Empty lines and lines
63  * beginning with '#' are ignored.
64  *
65  *
66  * Output Parameters:
67  *      ops     contains a list of tag operations
68  *      query_str the search terms.
69  */
70 tag_parse_status_t
71 parse_tag_line (void *ctx, char *line,
72                 tag_op_flag_t flags,
73                 char **query_str, tag_op_list_t *ops);
74
75 /*
76  * Create an empty list of tag operations
77  *
78  * ctx is passed to talloc
79  */
80
81 tag_op_list_t *
82 tag_op_list_create (void *ctx);
83
84 /*
85  * Add a tag operation (delete iff remove == TRUE) to a list.
86  * The list is expanded as necessary.
87  */
88
89 int
90 tag_op_list_append (tag_op_list_t *list,
91                     const char *tag,
92                     notmuch_bool_t remove);
93
94 /*
95  * Apply a list of tag operations, in order, to a given message.
96  *
97  * Flags can be bitwise ORed; see enum above for possibilies.
98  */
99
100 notmuch_status_t
101 tag_op_list_apply (notmuch_message_t *message,
102                    tag_op_list_t *tag_ops,
103                    tag_op_flag_t flags);
104
105 /*
106  * Return the number of operations in a list
107  */
108
109 size_t
110 tag_op_list_size (const tag_op_list_t *list);
111
112 /*
113  * Reset a list to contain no operations
114  */
115
116 void
117 tag_op_list_reset (tag_op_list_t *list);
118
119
120 /*
121  *   return the i'th tag in the list
122  */
123
124 const char *
125 tag_op_list_tag (const tag_op_list_t *list, size_t i);
126
127 /*
128  *   Is the i'th tag operation a remove?
129  */
130
131 notmuch_bool_t
132 tag_op_list_isremove (const tag_op_list_t *list, size_t i);
133
134 #endif