1 """Collection of functions for mass IMAP server manipulation"""
6 SERVER = 'intranator.m.i2n'
9 ANNOTATION_SHARED_SEEN = '"/vendor/cmu/cyrus-imapd/sharedseen"'
11 MAILBOX_RESP = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')
16 """Class for mass IMAP manipulation"""
18 def __init__(self, server, username, password):
24 self.mail_con = imaplib.IMAP4(server)
25 except Exception as ex:
26 raise UserWarning("Could not connect to host %s: %s" % (server, ex))
30 self.mail_con.login(username, password)
31 logging.info("Logged in as %s.", username)
33 self.logged_in = False
34 raise UserWarning("Could not log in as user " + username)
39 _result, self.mailboxes = self.mail_con.list()
40 except (self.mail_con.error):
41 raise UserWarning("Could not retrieve mailboxes for user " + username)
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)
50 print('Modifying ACL for mailbox %s' % mailbox)
52 self.mail_con.setacl(mailbox, identifier, rights)
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)
61 print('Setting sharedseen annotation on mailbox %s' % mailbox)
63 self.mail_con.setannotation(mailbox, ANNOTATION_SHARED_SEEN, '("value.shared" "true")')
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)
73 raw_annotations = self.mail_con.getannotation(mailbox, ANNOTATION_SHARED_SEEN, '"value.shared"')
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
80 print('sharedseen state on mailbox %s: %s' % (mailbox, sharedseen_enabled))
84 imap = ImapAction(SERVER, USERNAME, PASSWORD)
86 imap.mass_set_group_rights("group:infokonto", "lrswipcd")
87 imap.mass_set_shared_seen()
89 imap.list_sharedseen_state()
91 if(__name__ == "__main__"):