Add mmassive e-mail update for performance by using message set STORE command
[imap-mark-seen] / imap_set_annotation.py
1 """Collection of functions for mass IMAP server manipulation"""
2 import imaplib
3 import logging
4 import re
5
6 SERVER = 'intranator.m.i2n'
7 USERNAME = 'FIXME'
8 PASSWORD = 'FIXME'
9 ANNOTATION_SHARED_SEEN = '"/vendor/cmu/cyrus-imapd/sharedseen"'
10
11 MAILBOX_RESP = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')
12
13 # imaplib.Debug = 5
14
15 class ImapAction:
16     """Class for mass IMAP manipulation"""
17
18     def __init__(self, server, username, password):
19         self.mail_con = None
20         self.mailboxes = None
21
22         # connect to server
23         try:
24             self.mail_con = imaplib.IMAP4(server)
25         except Exception as ex:
26             raise UserWarning("Could not connect to host %s: %s" % (server, ex))
27
28         # log in
29         try:
30             self.mail_con.login(username, password)
31             logging.info("Logged in as %s.", username)
32         except:
33             self.logged_in = False
34             raise UserWarning("Could not log in as user " + username)
35         self.logged_in = True
36
37         # list mailboxes
38         try:
39             _result, self.mailboxes = self.mail_con.list()
40         except (self.mail_con.error):
41             raise UserWarning("Could not retrieve mailboxes for user " + username)
42
43     def mass_set_group_rights(self, identifier, rights):
44         """Mass set ACL rights on mailboxes"""
45         for raw_mailbox in self.mailboxes:
46             mailbox = MAILBOX_RESP.match(raw_mailbox.decode('iso-8859-1')).groups()[2]
47             if not mailbox.startswith('\"INBOX'):
48                 print('Skipping mailbox %s' % mailbox)
49                 continue
50             print('Modifying ACL for mailbox %s' % mailbox)
51
52             self.mail_con.setacl(mailbox, identifier, rights)
53
54     def mass_set_shared_seen(self):
55         """Enable shared seen state on all mailboxes"""
56         for raw_mailbox in self.mailboxes:
57             mailbox = MAILBOX_RESP.match(raw_mailbox.decode('iso-8859-1')).groups()[2]
58             if not mailbox.startswith('\"INBOX'):
59                 print('Skipping mailbox %s' % mailbox)
60                 continue
61             print('Setting sharedseen annotation on mailbox %s' % mailbox)
62
63             self.mail_con.setannotation(mailbox, ANNOTATION_SHARED_SEEN, '("value.shared" "true")')
64
65     def list_sharedseen_state(self):
66         """List shared seen state on all mailboxes"""
67         for raw_mailbox in self.mailboxes:
68             mailbox = MAILBOX_RESP.match(raw_mailbox.decode('iso-8859-1')).groups()[2]
69             if not mailbox.startswith('\"INBOX'):
70                 print('Skipping mailbox %s' % mailbox)
71                 continue
72
73             raw_annotations = self.mail_con.getannotation(mailbox, ANNOTATION_SHARED_SEEN, '"value.shared"')
74
75             # Hack to parse annotation result
76             sharedseen_enabled = False
77             if str(raw_annotations[1]).find('("value.shared" "true")') != -1:
78                 sharedseen_enabled = True
79
80             print('sharedseen state on mailbox %s: %s' % (mailbox, sharedseen_enabled))
81
82 def main():
83     """Main function"""
84     imap = ImapAction(SERVER, USERNAME, PASSWORD)
85
86     imap.mass_set_group_rights("group:infokonto", "lrswipcd")
87     imap.mass_set_shared_seen()
88
89     imap.list_sharedseen_state()
90
91 if(__name__ == "__main__"):
92     main()