def delete(self, mailbox_name, confirm=False): """Delete a mailbox; return True.""" mailbox_name = self._validate_mailbox_name(mailbox_name) mailbox = self.sep.join([self.mailbox_group, mailbox_name]) self.log.debug('Deleting mailbox %s' % mailbox) res, msg = self.imap.delete(mailbox) msg = msg[0] if res != 'OK': raise error.SpokeIMAPError(msg) self.log.debug(msg) return True
def create(self, mailbox_name): """Create a mailbox; return True.""" mailbox_name = self._validate_mailbox_name(mailbox_name) mailbox = self.sep.join([self.mailbox_group, mailbox_name]) self.log.debug('Creating mailbox %s' % mailbox) res, msg = self.imap.create(mailbox) msg = msg[0] if res != 'OK': raise error.SpokeIMAPError(msg) self.imap.setacl(mailbox, self.user, 'c') # Grant admin user delete self.log.debug(msg) return True
def __init__(self): """Get config, setup logging and cyrus connection.""" self.config = config.setup() self.log = logging.getLogger(__name__) server = self.config.get('IMAP', 'server') port = int(self.config.get('IMAP', 'port', 143)) self.user = self.config.get('IMAP', 'user') password = self.config.get('IMAP', 'password') self.mailbox_group = self.config.get('IMAP', 'mailbox_group', 'user') self.sep = '/' try: self.imap = imaplib.IMAP4(server, port) except imaplib.socket.error: trace = traceback.format_exec() msg = '%s: %s' % ('IMAP connection error') raise error.SpokeIMAPError(msg, trace) self.imap.login(self.user, password)
def get(self, mailbox_name, unique=False): """Find a mailbox; return result (list of tuples).""" mailbox_name = self._validate_mailbox_name(mailbox_name) mailbox = self.sep.join([self.mailbox_group, mailbox_name]) self.log.debug('Searching for mailbox %s' % mailbox) res, data = self.imap.list(pattern=mailbox) self.log.debug(data) if res != 'OK': raise error.SpokeIMAPError(data) if data == [None]: self.log.debug('Mailbox %s not found' % mailbox) return [] if unique is True and len(data) > 1: self.log.error('Multiple results when uniqueness requested.') raise error.SearchUniqueError(data) pattern = re.compile\ (r'\((?P<flags>.*)\) "(?P<sep>.*)" ".*/(?P<mbx>.*)"') result = [] for line in data: flags, sep, mbx = pattern.match(line).groups() result.append((mbx, sep, flags)) self.log.debug(result) return result