aboutsummaryrefslogtreecommitdiffstats
path: root/python/xkeywordsync.py
blob: 9a7cd799428b99202e8d9115c5302f75bc71fdf8 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/bin/usr/env python3
# -*- coding: utf-8 -*-
#
# Credits:
# [1] Gaute Hope: gauteh/abunchoftags
#     https://github.com/gauteh/abunchoftags/blob/master/keywsync.cc
#
# TODO:
# * Support case-insensitive tags merge
#   (ref: http://stackoverflow.com/a/1480230)
# * Accept a specified mtime, and only deal with files with newer mtime.
#
# Aaron LI
# Created: 2016-01-24
#

"""
Sync message 'X-Keywords' header with notmuch tags.

* tags-to-keywords:
  Check if the messages in the query have a matching 'X-Keywords' header
  to the list of notmuch tags.
  If not, update the 'X-Keywords' and re-write the message.

* keywords-to-tags:
  Check if the messages in the query have matching notmuch tags to the
  'X-Keywords' header.
  If not, update the tags in the notmuch database.

* merge-keywords-tags:
  Merge the 'X-Keywords' labels and notmuch tags, and update both.
"""

__version__ = "0.1.2"
__date__    = "2016-01-25"

import os
import sys
import argparse
import email

# Require Python 3.4, or install package 'enum34'
from enum import Enum

from notmuch import Database, Query

from imapUTF7 import imapUTF7Decode, imapUTF7Encode


class SyncDirection(Enum):
    """
    Synchronization direction
    """
    MERGE_KEYWORDS_TAGS = 0  # Merge 'X-Keywords' and notmuch tags and
                             # update both
    KEYWORDS_TO_TAGS    = 1  # Sync 'X-Keywords' header to notmuch tags
    TAGS_TO_KEYWORDS    = 2  # Sync notmuch tags to 'X-Keywords' header

class SyncMode(Enum):
    """
    Sync mode
    """
    ADD_REMOVE  = 0  # Allow add & remove tags/keywords
    ADD_ONLY    = 1  # Only allow add tags/keywords
    REMOVE_ONLY = 2  # Only allow remove tags/keywords


class KwMessage:
    """
    Message class to deal with 'X-Keywords' header synchronization
    with notmuch tags.

    NOTE:
    * The same message may have multiple files with different keywords
      (e.g, the same message exported under each label by Gmail)
      managed by OfflineIMAP.
      For example: a message file in OfflineIMAP synced folder of
      '[Gmail]/All Mail' have keywords ['google', 'test']; however,
      the file in synced folder 'test' of the same message only have
      keywords ['google'] without the keyword 'test'.
    * All files associated to the same message are regarded as the same.
      The keywords are extracted from all files and merged.
      And the same updated keywords are written back to all files, which
      results all files finally having the same 'X-Keywords' header.
    * You may only sync the '[Gmail]/All Mail' folder without other
      folders exported according the labels by Gmail.
    """
    # Replace some special characters before mapping keyword to tag
    enable_replace_chars = True
    chars_replace = {
            '/' : '.',
    }
    # Mapping between (Gmail) keywords and notmuch tags (before ignoring tags)
    keywords_mapping = {
            '\\Inbox'     : 'inbox',
            '\\Important' : 'important',
            '\\Starred'   : 'flagged',
            '\\Sent'      : 'sent',
            '\\Muted'     : 'killed',
            '\\Draft'     : 'draft',
            '\\Trash'     : 'deleted',
            '\\Junk'      : 'spam',
    }
    # Tags ignored from syncing
    # These tags are either internal tags or tags handled by maildir flags.
    enable_ignore_tags = True
    tags_ignored = set([
            'new', 'unread', 'attachment', 'signed', 'encrypted',
            'flagged', 'replied', 'passed', 'draft',
    ])
    # Ignore case when merging tags
    tags_ignorecase = True

    # Whether the tags updated against the message 'X-Keywords' header
    tags_updated = False
    # Added & removed tags for notmuch database against 'X-Keywords'
    tags_added   = []
    tags_removed = []
    # Newly updated/merged notmuch tags against 'X-Keywords'
    tags_new     = []

    # Whether the keywords updated against the notmuch tags
    keywords_updated = False
    # Added & removed tags for 'X-Keywords' against notmuch database
    tags_kw_added    = []
    tags_kw_removed  = []
    # Newly updated/merged tags for 'X-Keywords' against notmuch database
    tags_kw_new      = []

    def __init__(self, msg, filename=None):
        self.message  = msg
        self.filename = filename
        self.allfiles = [ fn for fn in msg.get_filenames() ]
        self.tags     = set(msg.get_tags())

    def sync(self, direction, mode=SyncMode.ADD_REMOVE,
             dryrun=False, verbose=False):
        """
        Wrapper function to sync between 'X-Keywords' and notmuch tags.
        """
        if direction == SyncDirection.KEYWORDS_TO_TAGS:
            self.sync_keywords_to_tags(sync_mode=mode, dryrun=dryrun,
                                       verbose=verbose)
        elif direction == SyncDirection.TAGS_TO_KEYWORDS:
            self.sync_tags_to_keywords(sync_mode=mode, dryrun=dryrun,
                                       verbose=verbose)
        elif direction == SyncDirection.MERGE_KEYWORDS_TAGS:
            self.merge_keywords_tags(sync_mode=mode, dryrun=dryrun,
                                     verbose=verbose)
        else:
            raise ValueError("Invalid sync direction: %s" % direction)

    def sync_keywords_to_tags(self, sync_mode=SyncMode.ADD_REMOVE,
                              dryrun=False, verbose=False):
        """
        Wrapper function to sync 'X-Keywords' to notmuch tags.
        """
        self.get_keywords()
        self.map_keywords()
        self.merge_tags(sync_direction=SyncDirection.KEYWORDS_TO_TAGS,
                        sync_mode=sync_mode)
        if dryrun or verbose:
            print('* MSG: %s' % self.message)
            print('  TAG: [%s]  +[%s] -[%s]  =>  [%s]' % (
                ','.join(self.tags), ','.join(self.tags_added),
                ','.join(self.tags_removed), ','.join(self.tags_new)))
        if not dryrun:
            self.update_tags()

    def sync_tags_to_keywords(self, sync_mode=SyncMode.ADD_REMOVE,
                              dryrun=False, verbose=False):
        """
        Wrapper function to sync notmuch tags to 'X-Keywords'
        """
        self.get_keywords()
        self.map_keywords()
        self.merge_tags(sync_direction=SyncDirection.TAGS_TO_KEYWORDS,
                        sync_mode=sync_mode)
        keywords_new = self.map_tags(tags=self.tags_kw_new)
        if dryrun or verbose:
            print('* MSG: %s' % self.message)
            print('* FILES: %s' % ' ; '.join(self.allfiles))
            print('  XKW: {%s}  +[%s] -[%s]  =>  {%s}' % (
                ','.join(self.keywords), ','.join(self.tags_kw_added),
                ','.join(self.tags_kw_removed), ','.join(keywords_new)))
        if not dryrun:
            self.update_keywords(keywords_new=keywords_new)

    def merge_keywords_tags(self, sync_mode=SyncMode.ADD_REMOVE,
                            dryrun=False, verbose=False):
        """
        Wrapper function to merge 'X-Keywords' and notmuch tags
        """
        self.get_keywords()
        self.map_keywords()
        self.merge_tags(sync_direction=SyncDirection.MERGE_KEYWORDS_TAGS,
                        sync_mode=sync_mode)
        keywords_new = self.map_tags(tags=self.tags_kw_new)
        if dryrun or verbose:
            print('* MSG: %s' % self.message)
            print('* FILES: %s' % ' ; '.join(self.allfiles))
            print('  TAG: [%s]  +[%s] -[%s]  =>  [%s]' % (
                ','.join(self.tags), ','.join(self.tags_added),
                ','.join(self.tags_removed), ','.join(self.tags_new)))
            print('  XKW: {%s}  +[%s] -[%s]  =>  {%s}' % (
                ','.join(self.keywords), ','.join(self.tags_kw_added),
                ','.join(self.tags_kw_removed), ','.join(keywords_new)))
        if not dryrun:
            self.update_tags()
            self.update_keywords(keywords_new=keywords_new)

    def get_keywords(self):
        """
        Get 'X-Keywords' header from all files associated with the same
        message, decode, split and merge.

        NOTE: Do NOT simply use the `message.get_header()` method, which
              cannot get the complete keywords from all files.
        """
        keywords_utf7 = []
        for fn in self.allfiles:
            msg = email.message_from_file(open(fn, 'r'))
            val = msg['X-Keywords']
            if val:
                keywords_utf7.append(val)
            else:
                print("WARNING: 'X-Keywords' header not found or empty " +\
                        "for file: %s" % fn, file=sys.stderr)
        keywords_utf7 = ','.join(keywords_utf7)
        if keywords_utf7 != '':
            keywords = imapUTF7Decode(keywords_utf7.encode()).split(',')
            keywords = [ kw.strip() for kw in keywords ]
            # Remove duplications
            keywords = set(keywords)
        else:
            keywords = set()
        self.keywords = keywords
        return keywords

    def map_keywords(self, keywords=None):
        """
        Map keywords to notmuch tags according to the mapping table.
        """
        if keywords is None:
            keywords = self.keywords
        if self.enable_replace_chars:
            # Replace specified characters in keywords
            trans = str.maketrans(self.chars_replace)
            keywords = [ kw.translate(trans) for kw in keywords ]
        # Map keywords to tags
        tags = set([ self.keywords_mapping.get(kw, kw) for kw in keywords ])
        self.tags_kw = tags
        return tags

    def map_tags(self, tags=None):
        """
        Map tags to keywords according to the inversed mapping table.
        """
        if tags is None:
            tags = self.tags
        if self.enable_replace_chars:
            # Inversely replace specified characters in tags
            chars_replace_inv = { v: k for k, v in self.chars_replace.items() }
            trans = str.maketrans(chars_replace_inv)
            tags = [ tag.translate(trans) for tag in tags ]
        # Map keywords to tags
        keywords_mapping_inv = { v:k for k,v in self.keywords_mapping.items() }
        keywords = set([ keywords_mapping_inv.get(tag, tag) for tag in tags ])
        self.keywords_tags = keywords
        return keywords

    def merge_tags(self, sync_direction, sync_mode=SyncMode.ADD_REMOVE,
                   tags_nm=None, tags_kw=None):
        """
        Merge the tags from notmuch database and 'X-Keywords' header,
        according to the specified sync direction and operation restriction.

        TODO: support case-insensitive set operations
        """
        # Added & removed tags for notmuch database against 'X-Keywords'
        tags_added       = []
        tags_removed     = []
        # Newly updated/merged notmuch tags against 'X-Keywords'
        tags_new         = []
        # Added & removed tags for 'X-Keywords' against notmuch database
        tags_kw_added    = []
        tags_kw_removed  = []
        # Newly updated/merged tags for 'X-Keywords' against notmuch database
        tags_kw_new      = []
        #
        if tags_nm is None:
            tags_nm = self.tags
        if tags_kw is None:
            tags_kw = self.tags_kw
        if self.enable_ignore_tags:
            # Remove ignored tags before merge
            tags_nm2 = tags_nm.difference(self.tags_ignored)
            tags_kw2 = tags_kw.difference(self.tags_ignored)
        else:
            tags_nm2 = tags_nm
            tags_kw2 = tags_kw
        #
        if sync_direction == SyncDirection.KEYWORDS_TO_TAGS:
            # Sync 'X-Keywords' to notmuch tags
            tags_added      = tags_kw2.difference(tags_nm2)
            tags_removed    = tags_nm2.difference(tags_kw2)
        elif sync_direction == SyncDirection.TAGS_TO_KEYWORDS:
            # Sync notmuch tags to 'X-Keywords'
            tags_kw_added   = tags_nm2.difference(tags_kw2)
            tags_kw_removed = tags_kw2.difference(tags_nm2)
        elif sync_direction == SyncDirection.MERGE_KEYWORDS_TAGS:
            # Merge both notmuch tags and 'X-Keywords'
            tags_merged     = tags_nm2.union(tags_kw2)
            # notmuch tags
            tags_added      = tags_merged.difference(tags_nm2)
            tags_removed    = tags_nm2.difference(tags_merged)
            # tags for 'X-Keywords'
            tags_kw_added   = tags_merged.difference(tags_kw2)
            tags_kw_removed = tags_kw2.difference(tags_merged)
        else:
            raise ValueError("Invalid synchronization direction")
        # Apply sync operation restriction
        self.tags_added       = []
        self.tags_removed     = []
        self.tags_kw_added    = []
        self.tags_kw_removed  = []
        tags_new              = tags_nm  # Use un-ignored notmuch tags
        tags_kw_new           = tags_kw  # Use un-ignored 'X-Keywords' tags
        if sync_mode != SyncMode.REMOVE_ONLY:
            self.tags_added      = tags_added
            self.tags_kw_added   = tags_kw_added
            tags_new             = tags_new.union(tags_added)
            tags_kw_new          = tags_kw_new.union(tags_kw_added)
        if sync_mode != SyncMode.ADD_ONLY:
            self.tags_removed    = tags_removed
            self.tags_kw_removed = tags_kw_removed
            tags_new             = tags_new.difference(tags_removed)
            tags_kw_new          = tags_kw_new.difference(tags_kw_removed)
        #
        self.tags_new         = tags_new
        self.tags_kw_new      = tags_kw_new
        if self.tags_added or self.tags_removed:
            self.tags_updated = True
        if self.tags_kw_added or self.tags_kw_removed:
            self.keywords_updated = True
        #
        return {
                'tags_updated'     : self.tags_updated,
                'tags_added'       : self.tags_added,
                'tags_removed'     : self.tags_removed,
                'tags_new'         : self.tags_new,
                'keywords_updated' : self.keywords_updated,
                'tags_kw_added'    : self.tags_kw_added,
                'tags_kw_removed'  : self.tags_kw_removed,
                'tags_kw_new'      : self.tags_kw_new,
        }

    def update_keywords(self, keywords_new=None, outfile=None):
        """
        Encode the keywords (default: self.keywords_new) and write back to
        all message files.

        If parameter 'outfile' specified, then write the updated message
        to that file instead of overwriting.

        NOTE:
        * The modification time of the message file should be kept to prevent
          OfflineIMAP from treating it as a new one (and the previous a
          deleted one).
        * All files associated with the same message are updated to have
          the same 'X-Keywords' header.
        """
        if not self.keywords_updated:
            # keywords NOT updated, just skip
            return

        if keywords_new is None:
            keywords_new = self.keywords_new
        #
        if outfile is not None:
            infile  = self.allfiles[0:1]
            outfile = [ os.path.expanduser(outfile) ]
        else:
            infile  = self.allfiles
            outfile = self.allfiles
        #
        for ifname, ofname in zip(infile, outfile):
            msg   = email.message_from_file(open(ifname, 'r'))
            fstat = os.stat(ifname)
            if keywords_new == []:
                # Delete 'X-Keywords' header
                print("WARNING: delete 'X-Keywords' header from file: %s" %
                        ifname, file=sys.stderr)
                del msg['X-Keywords']
            else:
                # Update 'X-Keywords' header
                keywords = ','.join(keywords_new)
                keywords_utf7 = imapUTF7Encode(keywords).decode()
                # Delete then add, to avoid multiple occurrences
                del msg['X-Keywords']
                msg['X-Keywords'] = keywords_utf7
            # Write updated message
            with open(ofname, 'w') as fp:
                fp.write(msg.as_string())
            # Reset the timestamps
            os.utime(ofname, ns=(fstat.st_atime_ns, fstat.st_mtime_ns))

    def update_tags(self, tags_added=None, tags_removed=None):
        """
        Update notmuch tags according to keywords.
        """
        if not self.tags_updated:
            # tags NOT updated, just skip
            return

        if tags_added is None:
            tags_added   = self.tags_added
        if tags_removed is None:
            tags_removed = self.tags_removed
        # Use freeze/thaw for safer transactions to change tag values.
        self.message.freeze()
        for tag in tags_added:
            self.message.add_tag(tag, sync_maildir_flags=False)
        for tag in tags_removed:
            self.message.remove_tag(tag, sync_maildir_flags=False)
        self.message.thaw()


def get_notmuch_revision(dbpath=None):
    """
    Get the current revision and UUID of notmuch database.
    """
    import subprocess
    import tempfile
    if dbpath:
        tf = tempfile.NamedTemporaryFile()
        # Create a minimal notmuch config for the specified dbpath
        config = '[database]\npath=%s\n' % os.path.expanduser(dbpath)
        tf.file.write(config.encode())
        tf.file.flush()
        cmd = 'notmuch --config=%s count --lastmod' % tf.name
        output = subprocess.check_output(cmd, shell=True)
        tf.close()
    else:
        cmd = 'notmuch count --lastmod'
        output = subprocess.check_output(cmd, shell=True)
    # Extract output
    dbinfo = output.decode().split()
    return { 'revision': int(dbinfo[2]), 'uuid': dbinfo[1] }


def main():
    parser = argparse.ArgumentParser(
            description="Sync message 'X-Keywords' header with notmuch tags.")
    parser.add_argument("-V", "--version", action="version",
            version="%(prog)s " + "v%s (%s)" % (__version__, __date__))
    parser.add_argument("-q", "--query", dest="query", required=True,
            help="notmuch database query string")
    parser.add_argument("-p", "--db-path", dest="dbpath",
            help="notmuch database path (default to try user configuration)")
    parser.add_argument("-n", "--dry-run", dest="dryrun",
            action="store_true", help="dry run")
    parser.add_argument("-v", "--verbose", dest="verbose",
            action="store_true", help="show verbose information")
    # Exclusive argument group for sync mode
    exgroup1 = parser.add_mutually_exclusive_group(required=True)
    exgroup1.add_argument("-m", "--merge-keywords-tags",
            dest="direction_merge", action="store_true",
            help="merge 'X-Keywords' and tags and update both")
    exgroup1.add_argument("-k", "--keywords-to-tags",
            dest="direction_keywords2tags", action="store_true",
            help="sync 'X-Keywords' to notmuch tags")
    exgroup1.add_argument("-t", "--tags-to-keywords",
            dest="direction_tags2keywords", action="store_true",
            help="sync notmuch tags to 'X-Keywords'")
    # Exclusive argument group for tag operation mode
    exgroup2 = parser.add_mutually_exclusive_group(required=False)
    exgroup2.add_argument("-a", "--add-only", dest="mode_addonly",
            action="store_true", help="only add notmuch tags")
    exgroup2.add_argument("-r", "--remove-only", dest="mode_removeonly",
            action="store_true", help="only remove notmuch tags")
    # Parse
    args = parser.parse_args()
    # Sync direction
    if args.direction_merge:
        sync_direction = SyncDirection.MERGE_KEYWORDS_TAGS
    elif args.direction_keywords2tags:
        sync_direction = SyncDirection.KEYWORDS_TO_TAGS
    elif args.direction_tags2keywords:
        sync_direction = SyncDirection.TAGS_TO_KEYWORDS
    else:
        raise ValueError("Invalid synchronization direction")
    # Sync mode
    if args.mode_addonly:
        sync_mode = SyncMode.ADD_ONLY
    elif args.mode_removeonly:
        sync_mode = SyncMode.REMOVE_ONLY
    else:
        sync_mode = SyncMode.ADD_REMOVE
    #
    if args.dbpath:
        dbpath = os.path.abspath(os.path.expanduser(args.dbpath))
    else:
        dbpath = None
    #
    db = Database(path=dbpath, create=False, mode=Database.MODE.READ_WRITE)
    dbinfo = get_notmuch_revision(dbpath=dbpath)
    q = Query(db, args.query)
    total_msgs = q.count_messages()
    msgs = q.search_messages()
    #
    if args.verbose:
        print("# Notmuch database path: %s" % dbpath)
        print("# Database revision: %d (uuid: %s)" %
                (dbinfo['revision'], dbinfo['uuid']))
        print("# Query: %s" % args.query)
        print("# Sync direction: %s" % sync_direction.name)
        print("# Sync mode: %s" % sync_mode.name)
        print("# Total messages to check: %d" % total_msgs)
        print("# Dryn run: %s" % args.dryrun)
    #
    for msg in msgs:
        kwmsg = KwMessage(msg)
        kwmsg.sync(direction=sync_direction, mode=sync_mode,
                   dryrun=args.dryrun, verbose=args.verbose)
    #
    db.close()


if __name__ == "__main__":
    main()

#  vim: set ts=4 sw=4 tw=0 fenc=utf-8 ft=python: #