def create_task_assignment(self, due=None, summary="test", sequence=0, user=None, attendees=None): if due is None: due = datetime.datetime.now(pytz.timezone("Europe/Berlin")) + datetime.timedelta(days=2) if user is None: user = self.john if attendees is None: attendees = [self.jane] todo = pykolab.xml.Todo() todo.set_due(due) todo.set_organizer(user['mail'], user['displayname']) for attendee in attendees: todo.add_attendee(attendee['mail'], attendee['displayname'], role="REQ-PARTICIPANT", participant_status="NEEDS-ACTION", rsvp=True) todo.set_summary(summary) todo.set_sequence(sequence) imap = IMAP() imap.connect() mailbox = imap.folder_quote(user['kolabtasksfolder']) imap.set_acl(mailbox, "cyrus-admin", "lrswipkxtecda") imap.imap.m.select(mailbox) result = imap.imap.m.append( mailbox, None, None, todo.to_message().as_string() ) return todo.get_uid()
def check_message_received(self, subject, from_addr=None, mailbox=None): if mailbox is None: mailbox = self.john['mailbox'] imap = IMAP() imap.connect() imap.set_acl(mailbox, "cyrus-admin", "lrs") imap.imap.m.select(mailbox) found = None retries = 10 while not found and retries > 0: retries -= 1 typ, data = imap.imap.m.search(None, '(UNDELETED HEADER FROM "%s")' % (from_addr) if from_addr else 'UNDELETED') for num in data[0].split(): typ, msg = imap.imap.m.fetch(num, '(RFC822)') message = message_from_string(msg[0][1]) if message['Subject'] == subject: found = message break time.sleep(1) imap.disconnect() return found
def check_user_imap_object(self, mailbox, uid=None, type='event'): imap = IMAP() imap.connect() mailbox = imap.folder_quote(mailbox) imap.set_acl(mailbox, "cyrus-admin", "lrs") imap.imap.m.select(mailbox) found = None retries = 15 while not found and retries > 0: retries -= 1 typ, data = imap.imap.m.search(None, '(UNDELETED HEADER SUBJECT "%s")' % (uid) if uid else '(UNDELETED HEADER X-Kolab-Type "application/x-vnd.kolab.' + type + '")') for num in data[0].split(): typ, data = imap.imap.m.fetch(num, '(RFC822)') object_message = message_from_string(data[0][1]) # return matching UID or first event found if uid and object_message['subject'] != uid: continue if type == 'task': found = todo_from_message(object_message) else: found = event_from_message(object_message) if found: break time.sleep(1) return found
def execute(*args, **kw): """ List mailboxes """ try: aci_subject = conf.cli_args.pop(0) except: aci_subject = None imap = IMAP() imap.connect() folders = imap.lm() for folder in folders: acls = imap.list_acls(folder) if not aci_subject == None: if aci_subject in acls.keys(): log.debug(_("Deleting ACL %s for subject %s on folder %s") % ( acls[aci_subject], aci_subject, folder ), level=8) imap.set_acl(folder, aci_subject, '') #else: #for _aci_subject in acls.keys(): # connect to auth(!) # find recipient result_attr=aci_subject # if no entry, expire acl
def check_resource_calendar_event(self, mailbox, uid=None): imap = IMAP() imap.connect() imap.set_acl(mailbox, "cyrus-admin", "lrs") imap.imap.m.select(imap.folder_quote(mailbox)) found = None retries = 10 while not found and retries > 0: retries -= 1 typ, data = imap.imap.m.search(None, '(UNDELETED HEADER SUBJECT "%s")' % (uid) if uid else '(UNDELETED HEADER X-Kolab-Type "application/x-vnd.kolab.event")') for num in data[0].split(): typ, data = imap.imap.m.fetch(num, '(RFC822)') event_message = message_from_string(data[0][1]) # return matching UID or first event found if uid and event_message['subject'] != uid: continue found = event_from_message(event_message) if found: break time.sleep(1) imap.disconnect() return found
def execute(*args, **kw): """ Delete a message from a mail folder """ try: folder = conf.cli_args.pop(0) try: uid = conf.cli_args.pop(0) except: log.error(_("Specify a UID")) sys.exit(1) except: log.error(_("Specify a folder")) sys.exit(1) imap = IMAP() imap.connect() _folder = imap.lm(folder) if _folder == None or _folder == []: log.error(_("No such folder")) sys.exit(1) imap.set_acl(folder, 'cyrus-admin', 'lrswt') imap.select(folder) imap.store(uid, '+FLAGS', '\\Deleted')
def update_calendar_event(self, uid, start=None, summary=None, sequence=0, user=None): if user is None: user = self.john event = self.check_user_calendar_event(user['kolabcalendarfolder'], uid) if event: if start is not None: event.set_start(start) if summary is not None: event.set_summary(summary) if sequence is not None: event.set_sequence(sequence) imap = IMAP() imap.connect() mailbox = imap.folder_quote(user['kolabcalendarfolder']) imap.set_acl(mailbox, "cyrus-admin", "lrswipkxtecda") imap.imap.m.select(mailbox) return imap.imap.m.append( mailbox, None, None, event.to_message().as_string() ) return False
def execute(*args, **kw): """ List mailboxes """ try: aci_subject = conf.cli_args.pop(0) except: aci_subject = None imap = IMAP() imap.connect() folders = imap.lm() for folder in folders: acls = imap.list_acls(folder) if not aci_subject == None: if aci_subject in acls.keys(): log.debug(_("Deleting ACL %s for subject %s on folder %s") % (acls[aci_subject], aci_subject, folder), level=8) imap.set_acl(folder, aci_subject, '')
def purge_mailbox(self, mailbox): imap = IMAP() imap.connect() imap.set_acl(mailbox, "cyrus-admin", "lrwcdest") imap.imap.m.select(imap.folder_quote(mailbox)) typ, data = imap.imap.m.search(None, 'ALL') for num in data[0].split(): imap.imap.m.store(num, '+FLAGS', '\\Deleted') imap.imap.m.expunge() imap.disconnect()
def execute(*args, **kw): """ List messages in a folder """ try: folder = conf.cli_args.pop(0) except: log.error(_("Specify a folder")) sys.exit(1) imap = IMAP() imap.connect() _folder = imap.lm(imap_utf7.encode(folder)) if _folder == None or _folder == []: log.error(_("No such folder")) sys.exit(1) imap.set_acl(folder, 'cyrus-admin', 'lrs') imap.select(imap_utf7.encode(folder)) if conf.list_deleted: typ, data = imap.search(None, 'ALL') else: typ, data = imap.search(None, '(ALL UNDELETED)') num_messages = len(data[0].split()) for num in data[0].split(): typ, flags = imap.fetch(num, 'FLAGS') flags = flags[0].split() if len(flags) >= 3: # Any flags are set if flags[2] == '(\\Deleted))': print num, '\Deleted' elif flags[2] == '(\\Deleted': print num, '\Deleted' elif '\\Deleted' in flags[3:]: print num, '\Deleted' elif '\\Deleted))' in flags[3:]: print num, '\Deleted' else: print num else: print num imap.set_acl(folder, 'cyrus-admin', '')
def create_calendar_event(self, start=None, summary="test", sequence=0, user=None, attendees=None, folder=None): if start is None: start = datetime.datetime.now(pytz.timezone("Europe/Berlin")) if user is None: user = self.john if attendees is None: attendees = [self.jane] if folder is None: folder = user['kolabcalendarfolder'] end = start + datetime.timedelta(hours=4) event = pykolab.xml.Event() event.set_start(start) event.set_end(end) event.set_organizer(user['mail'], user['displayname']) for attendee in attendees: event.add_attendee(attendee['mail'], attendee['displayname'], role="REQ-PARTICIPANT", participant_status="NEEDS-ACTION", rsvp=True) event.set_summary(summary) event.set_sequence(sequence) # create event with attachment vattach = event.get_attachments() attachment = kolabformat.Attachment() attachment.setLabel('attach.txt') attachment.setData('This is a text attachment', 'text/plain') vattach.append(attachment) event.event.setAttachments(vattach) imap = IMAP() imap.connect() mailbox = imap.folder_quote(folder) imap.set_acl(mailbox, "cyrus-admin", "lrswipkxtecda") imap.imap.m.select(mailbox) result = imap.imap.m.append( mailbox, None, None, event.to_message().as_string() ) return event.get_uid()
def test_002_send_forwarded_email(self): import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate from email import Encoders smtp = smtplib.SMTP('localhost', 10026) subject = "%s" % (time.time()) body = "This is a test message" msg = MIMEMultipart() msg['From'] = '"Doe, Jane" <*****@*****.**>' msg['To'] = '"Doe, John" <*****@*****.**>' msg['Subject'] = subject msg['Date'] = formatdate(localtime=True) msg.attach(MIMEText(body)) send_to = '*****@*****.**' send_from = '*****@*****.**' smtp.sendmail(send_from, send_to, msg.as_string()) imap = IMAP() imap.connect() imap.set_acl("user/[email protected]", "cyrus-admin", "lrs") imap.imap.m.select("user/[email protected]") found = False max_tries = 20 while not found and max_tries > 0: max_tries -= 1 typ, data = imap.imap.m.search(None, 'ALL') for num in data[0].split(): typ, msg = imap.imap.m.fetch(num, '(RFC822)') _msg = message_from_string(msg[0][1]) if _msg['Subject'] == subject: found = True time.sleep(1) if not found: raise Exception
def check_message_delivered(self, subject): imap = IMAP() imap.connect() imap.set_acl("user/[email protected]", "cyrus-admin", "lrs") imap.imap.m.select("user/[email protected]") found = False max_tries = 20 while not found and max_tries > 0: max_tries -= 1 typ, data = imap.imap.m.search(None, 'ALL') for num in data[0].split(): typ, msg = imap.imap.m.fetch(num, '(RFC822)') _msg = message_from_string(msg[0][1]) if _msg['Subject'] == subject: found = True time.sleep(1) return found
def description(): return """Delete an ACL entry for a folder.""" def execute(*args, **kw): try: folder = conf.cli_args.pop(0) try: identifier = conf.cli_args.pop(0) except IndexError, errmsg: identifier = utils.ask_question(_("ACI Subject")) except IndexError, errmsg: folder = utils.ask_question(_("Folder name")) quota = utils.ask_question(_("ACI Subject")) if len(folder.split('@')) > 1: domain = folder.split('@')[1] else: domain = conf.get('kolab', 'primary_domain') imap = IMAP() imap.connect(domain=domain) if not imap.has_folder(folder): print >> sys.stderr, _("No such folder %r") % (folder) else: folders = imap.lm(folder) for folder in folders: imap.set_acl(folder, identifier, '')
identifier = conf.cli_args.pop(0) try: acl = conf.cli_args.pop(0) except IndexError, errmsg: acl = utils.ask_question(_("ACI Permissions")) except IndexError, errmsg: identifier = utils.ask_question(_("ACI Subject")) acl = utils.ask_question(_("ACI Permissions")) except IndexError, errmsg: folder = utils.ask_question(_("Folder name")) identifier = utils.ask_question(_("ACI Subject")) acl = utils.ask_question(_("ACI Permissions")) if len(folder.split('@')) > 1: domain = folder.split('@')[1] else: domain = conf.get('kolab', 'primary_domain') imap = IMAP() imap.connect(domain=domain) if not imap.has_folder(folder): print >> sys.stderr, _("No such folder %r") % (folder) else: folders = imap.lm(folder) for folder in folders: imap.set_acl(folder, identifier, acl)
imap.append(folder, None, None, e.to_message().as_string()) if __name__ == '__main__': conf = pykolab.getConf() conf.finalize_conf() imap = IMAP() imap.connect() user = "******" start = datetime(2015, 1, 1, 0, 0, 0, tzinfo=pytz.timezone("Europe/Berlin")) end = datetime(2015, 1, 1, 10, 0, 0, tzinfo=pytz.timezone("Europe/Berlin")) event = ical.createEvent(start, end) # Admin normally has no right to do anything with the mailbox # so first add the right to append message (i) after adding the message # remove the right again imap.set_acl(calendar(user), 'cyrus-admin', 'lrsi') try: appendEvent(user, event) finally: imap.set_acl(calendar(user), 'cyrus-admin', '')
def calendar(user): return imap_utf7.encode( "user/{username}/[email protected]".format(username=user)) def appendEvent(user, event): """event must be a ical object (python icalendar Event)""" e = event_from_ical(event) # construct a kolab object folder = calendar(user) imap.append(folder, None, None, e.to_message().as_string()) if __name__ == '__main__': conf = pykolab.getConf() conf.finalize_conf() imap = IMAP() imap.connect() user = "******" start = datetime(2015, 1, 1, 0, 0, 0, tzinfo=pytz.timezone("Europe/Berlin")) end = datetime(2015, 1, 1, 10, 0, 0, tzinfo=pytz.timezone("Europe/Berlin")) event = ical.createEvent(start, end) # Admin normally has no right to do anything with the mailbox # so first add the right to append message (i) after adding the message # remove the right again imap.set_acl(calendar(user), 'cyrus-admin', 'lrsi') try: appendEvent(user, event) finally: imap.set_acl(calendar(user), 'cyrus-admin', '')
try: folder = conf.cli_args.pop(0) try: identifier = conf.cli_args.pop(0) except IndexError, errmsg: identifier = utils.ask_question(_("ACI Subject")) except IndexError, errmsg: folder = utils.ask_question(_("Folder name")) quota = utils.ask_question(_("ACI Subject")) if len(folder.split('@')) > 1: domain = folder.split('@')[1] else: domain = conf.get('kolab', 'primary_domain') imap = IMAP() imap.connect(domain=domain) if not imap.has_folder(folder): print >> sys.stderr, _("No such folder %r") % (folder) else: folders = imap.list_folders(folder) for folder in folders: try: imap.set_acl(folder, identifier, '') except: # Mailbox no longer exists? pass
identifier = conf.cli_args.pop(0) try: acl = conf.cli_args.pop(0) except IndexError, errmsg: acl = utils.ask_question(_("ACI Permissions")) except IndexError, errmsg: identifier = utils.ask_question(_("ACI Subject")) acl = utils.ask_question(_("ACI Permissions")) except IndexError, errmsg: folder = utils.ask_question(_("Folder name")) identifier = utils.ask_question(_("ACI Subject")) acl = utils.ask_question(_("ACI Permissions")) if len(folder.split('@')) > 1: domain = folder.split('@')[1] else: domain = conf.get('kolab', 'primary_domain') imap = IMAP() imap.connect(domain=domain) if not imap.has_folder(folder): print >> sys.stderr, _("No such folder %r") % (folder) else: folders = imap.list_folders(folder) for folder in folders: imap.set_acl(folder, identifier, acl)