diff --git a/pykolab/xml/contact.py b/pykolab/xml/contact.py index 7f7e768..d2bf358 100644 --- a/pykolab/xml/contact.py +++ b/pykolab/xml/contact.py @@ -1,345 +1,348 @@ from six import string_types import kolabformat import datetime import pytz import base64 import sys from pykolab.xml import utils as xmlutils from pykolab.xml.utils import ustr def contact_from_vcard(string): # TODO: implement this pass def contact_from_string(string): + if isinstance(string, bytes): + string = string.decode('utf-8') + _xml = kolabformat.readContact(string, False) return Contact(_xml) def contact_from_message(message): contact = None if message.is_multipart(): for part in message.walk(): if part.get_content_type() == "application/vcard+xml": payload = part.get_payload(decode=True) if sys.version_info.major >= 3: contact = contact_from_string(payload.decode("utf-8")) else: contact = contact_from_string(payload) # append attachment parts to Contact object elif contact and 'Content-ID' in part: contact._attachment_parts.append(part) return contact class Contact(kolabformat.Contact): type = 'contact' related_map = { 'manager': kolabformat.Related.Manager, 'assistant': kolabformat.Related.Assistant, 'spouse': kolabformat.Related.Spouse, 'children': kolabformat.Related.Child, None: kolabformat.Related.NoRelation, } addresstype_map = { 'home': kolabformat.Address.Home, 'work': kolabformat.Address.Work, } phonetype_map = { 'home': kolabformat.Telephone.Home, 'work': kolabformat.Telephone.Work, 'text': kolabformat.Telephone.Text, 'main': kolabformat.Telephone.Voice, 'homefax': kolabformat.Telephone.Fax, 'workfax': kolabformat.Telephone.Fax, 'mobile': kolabformat.Telephone.Cell, 'video': kolabformat.Telephone.Video, 'pager': kolabformat.Telephone.Pager, 'car': kolabformat.Telephone.Car, 'other': kolabformat.Telephone.Textphone, } emailtype_map = { 'home': kolabformat.Email.Home, 'work': kolabformat.Email.Work, 'other': kolabformat.Email.Work, } urltype_map = { 'homepage': kolabformat.Url.NoType, 'blog': kolabformat.Url.Blog, } keytype_map = { 'pgp': kolabformat.Key.PGP, 'pkcs7': kolabformat.Key.PKCS7_MIME, None: kolabformat.Key.Invalid, } gender_map = { 'female': kolabformat.Contact.Female, 'male': kolabformat.Contact.Male, None: kolabformat.Contact.NotSet, } properties_map = { 'uid': 'get_uid', 'lastmodified-date': 'get_lastmodified', 'fn': 'name', 'nickname': 'nickNames', 'title': 'titles', 'email': 'emailAddresses', 'tel': 'telephones', 'url': 'urls', 'im': 'imAddresses', 'address': 'addresses', 'note': 'note', 'freebusyurl': 'freeBusyUrl', 'birthday': 'bDay', 'anniversary': 'anniversary', 'categories': 'categories', 'lang': 'languages', 'gender': 'get_gender', 'gpspos': 'gpsPos', 'key': 'keys', } def __init__(self, *args, **kw): self._attachment_parts = [] kolabformat.Contact.__init__(self, *args, **kw) def get_uid(self): uid = self.uid() if not uid == '': return uid else: self.__str__() return kolabformat.getSerializedUID() def get_lastmodified(self): try: _datetime = self.lastModified() if _datetime == None or not _datetime.isValid(): self.__str__() except: return datetime.datetime.now(pytz.utc) return xmlutils.from_cdatetime(self.lastModified(), True) def get_email(self, preferred=True): if preferred: return self.emailAddresses()[self.emailAddressPreferredIndex()] else: return [x for x in self.emailAddresses()] def set_email(self, email, preferred_index=0): if isinstance(email, string_types): self.setEmailAddresses([email], preferred_index) else: self.setEmailAddresses(email, preferred_index) def add_email(self, email): if isinstance(email, string_types): self.add_emails([email]) elif isinstance(email, list): self.add_emails(email) def add_emails(self, emails): preferred_email = self.get_email() emails = [x for x in set(self.get_email(preferred=False) + emails)] preferred_email_index = emails.index(preferred_email) self.setEmailAddresses(emails, preferred_email_index) def set_name(self, name): self.setName(ustr(name)) def get_gender(self, translated=True): _gender = self.gender() if translated: return self._translate_value(_gender, self.gender_map) return _gender def _translate_value(self, val, map): name_map = dict([(v, k) for (k, v) in map.items()]) return name_map[val] if val in name_map else 'UNKNOWN' def to_dict(self): if not self.isValid(): return None data = self._names2dict(self.nameComponents()) for p, getter in self.properties_map.items(): val = None if hasattr(self, getter): val = getattr(self, getter)() if isinstance(val, kolabformat.cDateTime): val = xmlutils.from_cdatetime(val, True) elif isinstance(val, kolabformat.vectori): val = [int(x) for x in val] elif isinstance(val, kolabformat.vectors): val = [str(x) for x in val] elif isinstance(val, kolabformat.vectortelephone): val = [self._struct2dict(x, 'number', self.phonetype_map) for x in val] elif isinstance(val, kolabformat.vectoremail): val = [self._struct2dict(x, 'address', self.emailtype_map) for x in val] elif isinstance(val, kolabformat.vectorurl): val = [self._struct2dict(x, 'url', self.urltype_map) for x in val] elif isinstance(val, kolabformat.vectorkey): val = [self._struct2dict(x, 'key', self.keytype_map) for x in val] elif isinstance(val, kolabformat.vectoraddress): val = [self._address2dict(x) for x in val] elif isinstance(val, kolabformat.vectorgeo): val = [[x.latitude, x.longitude] for x in val] if val is not None: data[p] = val affiliations = self.affiliations() if len(affiliations) > 0: _affiliation = self._affiliation2dict(affiliations[0]) if 'address' in _affiliation: data['address'].extend(_affiliation['address']) _affiliation.pop('address', None) data.update(_affiliation) data.update(self._relateds2dict(self.relateds())) if self.photoMimetype(): if sys.version_info.major >= 3: data['photo'] = dict(mimetype=self.photoMimetype(), base64=base64.b64encode(self.photo().encode('utf-8', 'surrogateescape'))) else: data['photo'] = dict(mimetype=self.photoMimetype(), base64=base64.b64encode(self.photo())) elif self.photo(): data['photo'] = dict(uri=self.photo()) return data def _names2dict(self, namecomp): names_map = { 'surname': 'surnames', 'given': 'given', 'additional': 'additional', 'prefix': 'prefixes', 'suffix': 'suffixes', } data = dict() for p, getter in names_map.items(): val = None if hasattr(namecomp, getter): val = getattr(namecomp, getter)() if isinstance(val, kolabformat.vectors): val = [str(x) for x in val][0] if len(val) > 0 else None if val is not None: data[p] = val return data def _affiliation2dict(self, affiliation): props_map = { 'organization': 'organisation', 'department': 'organisationalUnits', 'role': 'roles', } data = dict() for p, getter in props_map.items(): val = None if hasattr(affiliation, getter): val = getattr(affiliation, getter)() if isinstance(val, kolabformat.vectors): val = [str(x) for x in val][0] if len(val) > 0 else None if val is not None: data[p] = val data.update(self._relateds2dict(affiliation.relateds(), True)) addresses = affiliation.addresses() if len(addresses): data['address'] = [self._address2dict(adr, 'office') for adr in addresses] return data def _address2dict(self, adr, adrtype=None): props_map = { 'label': 'label', 'street': 'street', 'locality': 'locality', 'region': 'region', 'code': 'code', 'country': 'country', } addresstype_map = dict([(v, k) for (k, v) in self.addresstype_map.items()]) data = dict() if adrtype is None: adrtype = addresstype_map.get(adr.types(), None) if adrtype is not None: data['type'] = adrtype for p, getter in props_map.items(): val = None if hasattr(adr, getter): val = getattr(adr, getter)() if isinstance(val, kolabformat.vectors): val = [str(x) for x in val][0] if len(val) > 0 else None if val is not None: data[p] = val return data def _relateds2dict(self, relateds, aslist=True): data = dict() related_map = dict([(v, k) for (k, v) in self.related_map.items()]) for rel in relateds: reltype = related_map.get(rel.relationTypes(), None) val = rel.uri() if rel.type() == kolabformat.Related.Uid else rel.text() if reltype and val is not None: if aslist: if reltype not in data: data[reltype] = [] data[reltype].append(val) else: data[reltype] = val return data def _struct2dict(self, struct, propname, map): type_map = dict([(v, k) for (k, v) in map.items()]) result = dict() if hasattr(struct, 'types'): result['type'] = type_map.get(struct.types(), None) elif hasattr(struct, 'type'): result['type'] = type_map.get(struct.type(), None) if hasattr(struct, propname): result[propname] = getattr(struct, propname)() return result def __str__(self): xml = kolabformat.writeContact(self) error = kolabformat.error() if error == None or not error: return xml else: raise ContactIntegrityError(kolabformat.errorMessage()) class ContactIntegrityError(Exception): def __init__(self, message): Exception.__init__(self, message) diff --git a/pykolab/xml/event.py b/pykolab/xml/event.py index 1ca1681..c02bca0 100644 --- a/pykolab/xml/event.py +++ b/pykolab/xml/event.py @@ -1,1526 +1,1528 @@ from six import string_types import datetime import icalendar import kolabformat import pytz import time import uuid import base64 import re import sys import pykolab from pykolab import constants from pykolab import utils from pykolab.xml import utils as xmlutils from pykolab.xml import participant_status_label from pykolab.xml.utils import ustr from pykolab.translate import _ from os import path from .attendee import Attendee from .contact_reference import ContactReference from .recurrence_rule import RecurrenceRule from collections import OrderedDict log = pykolab.getLogger('pykolab.xml_event') def event_from_ical(ical, string=None): return Event(from_ical=ical, from_string=string) def event_from_string(string): + if isinstance(string, bytes): + string = string.decode('utf-8') return Event(from_string=string) def event_from_message(message): event = None if message.is_multipart(): for part in message.walk(): if part.get_content_type() == "application/calendar+xml": payload = part.get_payload(decode=True) event = event_from_string(payload) # append attachment parts to Event object elif event and 'Content-ID' in part: event._attachment_parts.append(part) return event class Event(object): type = 'event' thisandfuture = False status_map = { None: kolabformat.StatusUndefined, "TENTATIVE": kolabformat.StatusTentative, "CONFIRMED": kolabformat.StatusConfirmed, "CANCELLED": kolabformat.StatusCancelled, "COMPLETED": kolabformat.StatusCompleted, "IN-PROCESS": kolabformat.StatusInProcess, "NEEDS-ACTION": kolabformat.StatusNeedsAction, } classification_map = { "PUBLIC": kolabformat.ClassPublic, "PRIVATE": kolabformat.ClassPrivate, "CONFIDENTIAL": kolabformat.ClassConfidential, } alarm_type_map = { 'EMAIL': kolabformat.Alarm.EMailAlarm, 'DISPLAY': kolabformat.Alarm.DisplayAlarm, 'AUDIO': kolabformat.Alarm.AudioAlarm } related_map = { 'START': kolabformat.Start, 'END': kolabformat.End } properties_map = { # property: getter "uid": "get_uid", "created": "get_created", "lastmodified-date": "get_lastmodified", "sequence": "sequence", "classification": "get_classification", "categories": "categories", "start": "get_start", "end": "get_end", "duration": "get_duration", "transparency": "transparency", "rrule": "recurrenceRule", "rdate": "recurrenceDates", "exdate": "exceptionDates", "recurrence-id": "recurrenceID", "summary": "summary", "description": "description", "priority": "priority", "status": "get_ical_status", "location": "location", "organizer": "organizer", "attendee": "get_attendees", "attach": "attachments", "url": "url", "alarm": "alarms", "x-custom": "customProperties", # TODO: add to_dict() support for these # "exception": "exceptions", } def __init__(self, from_ical="", from_string=""): self._attendees = [] self._categories = [] self._exceptions = [] self._attachment_parts = [] if isinstance(from_ical, str) and from_ical == "": if from_string == "": self.event = kolabformat.Event() else: self.event = kolabformat.readEvent(from_string, False) self._load_attendees() self._load_exceptions() else: self.from_ical(from_ical, from_string) self.set_created(self.get_created()) self.uid = self.get_uid() def _load_attendees(self): for a in self.event.attendees(): att = Attendee(a.contact().email()) att.copy_from(a) self._attendees.append(att) def _load_exceptions(self): for ex in self.event.exceptions(): exception = Event() exception.uid = ex.uid() exception.event = ex exception._load_attendees() self._exceptions.append(exception) def add_attendee(self, email_or_attendee, name=None, rsvp=False, role=None, participant_status=None, cutype="INDIVIDUAL", params=None): if isinstance(email_or_attendee, Attendee): attendee = email_or_attendee else: attendee = Attendee(email_or_attendee, name, rsvp, role, participant_status, cutype, params) # apply update to self and all exceptions self.update_attendees([attendee], True) def add_category(self, category): self._categories.append(ustr(category)) self.event.setCategories(self._categories) def add_recurrence_date(self, _datetime): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): # If no timezone information is passed on, make it UTC if _datetime.tzinfo is None: _datetime = _datetime.replace(tzinfo=pytz.utc) valid_datetime = True if not valid_datetime: raise InvalidEventDateError(_("Rdate needs datetime.date or datetime.datetime instance, got %r") % (type(_datetime))) self.event.addRecurrenceDate(xmlutils.to_cdatetime(_datetime, True)) def add_exception_date(self, _datetime): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): # If no timezone information is passed on, make it UTC if _datetime.tzinfo == None: _datetime = _datetime.replace(tzinfo=pytz.utc) valid_datetime = True if not valid_datetime: raise InvalidEventDateError(_("Exdate needs datetime.date or datetime.datetime instance, got %r") % (type(_datetime))) self.event.addExceptionDate(xmlutils.to_cdatetime(_datetime, True)) def add_exception(self, exception): recurrence_id = exception.get_recurrence_id() if recurrence_id is None: raise EventIntegrityError("Recurrence exceptions require a Recurrence-ID property") # check if an exception with the given recurrence-id already exists append = True vexceptions = self.event.exceptions() for i, ex in enumerate(self._exceptions): if ex.get_recurrence_id() == recurrence_id and ex.thisandfuture == exception.thisandfuture: # update the existing exception vexceptions[i] = exception.event self._exceptions[i] = exception append = False # check if main event matches the given recurrence-id if append and self.get_recurrence_id() == recurrence_id: self.event = exception.event self._load_attendees() self._load_exceptions() append = False if append: vexceptions.append(exception.event) self._exceptions.append(exception) self.event.setExceptions(vexceptions) def del_exception(self, exception): recurrence_id = exception.get_recurrence_id() if recurrence_id is None: raise EventIntegrityError("Recurrence exceptions require a Recurrence-ID property") updated = False vexceptions = self.event.exceptions() for i, ex in enumerate(self._exceptions): if ex.get_recurrence_id() == recurrence_id and ex.thisandfuture == exception.thisandfuture: del vexceptions[i] del self._exceptions[i] updated = True if updated: self.event.setExceptions(vexceptions) def as_string_itip(self, method="REQUEST"): cal = icalendar.Calendar() cal.add( 'prodid', '-//pykolab-%s-%s//kolab.org//' % ( constants.__version__, constants.__release__ ) ) cal.add('version', '2.0') # TODO: Really? cal.add('calscale', 'GREGORIAN') # TODO: Not always a request... cal.add('method', method) # TODO: Add timezone information using icalendar.?() # Not sure if there is a class for it. cal.add_component(self.to_ical()) # add recurrence exceptions if len(self._exceptions) > 0 and not method == 'REPLY': for exception in self._exceptions: cal.add_component(exception.to_ical()) if hasattr(cal, 'to_ical'): return ustr(cal.to_ical()) elif hasattr(cal, 'as_string'): return ustr(cal.as_string()) def to_ical(self): event = icalendar.Event() # Required event['uid'] = self.get_uid() # NOTE: Make sure to list(set()) or duplicates may arise for attr in list(set(event.singletons)): _attr = attr.lower().replace('-', '') ical_getter = 'get_ical_%s' % (_attr) default_getter = 'get_%s' % (_attr) retval = None if hasattr(self, ical_getter): retval = getattr(self, ical_getter)() if not retval == None and not retval == "": event.add(attr.lower(), retval) elif hasattr(self, default_getter): retval = getattr(self, default_getter)() if not retval == None and not retval == "": event[attr.lower()] = retval # NOTE: Make sure to list(set()) or duplicates may arise for attr in list(set(event.multiple)): _attr = attr.lower().replace('-', '') ical_getter = 'get_ical_%s' % (_attr) default_getter = 'get_%s' % (_attr) retval = None if hasattr(self, ical_getter): retval = getattr(self, ical_getter)() elif hasattr(self, default_getter): retval = getattr(self, default_getter)() if isinstance(retval, list) and not len(retval) == 0: for _retval in retval: event.add(attr.lower(), _retval, encode=0) # copy custom properties to iCal for cs in self.event.customProperties(): event.add(cs.identifier, cs.value) return event def delegate(self, delegators, delegatees, names=None): if not isinstance(delegators, list): delegators = [delegators] if not isinstance(delegatees, list): delegatees = [delegatees] if not isinstance(names, list): names = [names] _delegators = [] for delegator in delegators: _delegators.append(self.get_attendee(delegator)) _delegatees = [] for i,delegatee in enumerate(delegatees): try: _delegatees.append(self.get_attendee(delegatee)) except: # TODO: An iTip needs to be sent out to the new attendee self.add_attendee(delegatee, names[i] if i < len(names) else None) _delegatees.append(self.get_attendee(delegatee)) for delegator in _delegators: delegator.delegate_to(_delegatees) for delegatee in _delegatees: delegatee.delegate_from(_delegators) self.event.setAttendees(self._attendees) def from_ical(self, ical, raw=None): if isinstance(ical, icalendar.Event) or isinstance(ical, icalendar.Calendar): ical_event = ical elif hasattr(icalendar.Event, 'from_ical'): ical_event = icalendar.Event.from_ical(ical) elif hasattr(icalendar.Event, 'from_string'): ical_event = icalendar.Event.from_string(ical) # VCALENDAR block was given, find the first VEVENT item if isinstance(ical_event, icalendar.Calendar): for c in ical_event.walk(): if c.name == 'VEVENT': ical_event = c break # use the libkolab calendaring bindings to load the full iCal data if 'RRULE' in ical_event or 'ATTACH' in ical_event \ or [part for part in ical_event.walk() if part.name == 'VALARM']: if raw is None or raw == "": raw = ical if isinstance(ical, str) else ical.to_ical() self._xml_from_ical(raw) else: self.event = kolabformat.Event() # TODO: Clause the timestamps for zulu suffix causing datetime.datetime # to fail substitution. for attr in list(set(ical_event.required)): if attr in ical_event: self.set_from_ical(attr.lower(), ical_event[attr]) # NOTE: Make sure to list(set()) or duplicates may arise # NOTE: Keep the original order e.g. to read DTSTART before RECURRENCE-ID for attr in list(OrderedDict.fromkeys(ical_event.singletons)): if attr in ical_event: if isinstance(ical_event[attr], list): ical_event[attr] = ical_event[attr][0]; self.set_from_ical(attr.lower(), ical_event[attr]) # NOTE: Make sure to list(set()) or duplicates may arise for attr in list(set(ical_event.multiple)): if attr in ical_event: self.set_from_ical(attr.lower(), ical_event[attr]) def _xml_from_ical(self, ical): if not "BEGIN:VCALENDAR" in ical: ical = "BEGIN:VCALENDAR\nVERSION:2.0\n" + ical + "\nEND:VCALENDAR" from kolab.calendaring import EventCal self.event = EventCal() success = self.event.fromICal(ical) if success: self._load_exceptions() return success def get_attendee_participant_status(self, attendee): return attendee.get_participant_status() def get_attendee(self, attendee): if isinstance(attendee, string_types): if attendee in [x.get_email() for x in self.get_attendees()]: attendee = self.get_attendee_by_email(attendee) elif attendee in [x.get_name() for x in self.get_attendees()]: attendee = self.get_attendee_by_name(attendee) else: raise ValueError(_("No attendee with email or name %r") %(attendee)) return attendee elif isinstance(attendee, Attendee): return attendee else: raise ValueError(_("Invalid argument value attendee %r, must be basestring or Attendee") % (attendee)) def find_attendee(self, attendee): try: return self.get_attendee(attendee) except: return None def get_attendee_by_email(self, email): if email in [x.get_email() for x in self.get_attendees()]: return [x for x in self.get_attendees() if x.get_email() == email][0] raise ValueError(_("No attendee with email %r") %(email)) def get_attendee_by_name(self, name): if name in [x.get_name() for x in self.get_attendees()]: return [x for x in self.get_attendees() if x.get_name() == name][0] raise ValueError(_("No attendee with name %r") %(name)) def get_attendees(self): return self._attendees def get_categories(self): return [str(c) for c in self.event.categories()] def get_classification(self): return self.event.classification() def get_created(self): try: return xmlutils.from_cdatetime(self.event.created(), True) except ValueError: return datetime.datetime.now() def get_description(self): return self.event.description() def get_comment(self): if hasattr(self.event, 'comment'): return self.event.comment() else: return None def get_duration(self): duration = self.event.duration() if duration and duration.isValid(): dtd = datetime.timedelta( days=duration.days(), seconds=duration.seconds(), minutes=duration.minutes(), hours=duration.hours(), weeks=duration.weeks() ) return dtd return None def get_end(self): dt = xmlutils.from_cdatetime(self.event.end(), True) if not dt: duration = self.get_duration() if duration is not None: dt = self.get_start() + duration return dt def get_date_text(self, date_format=None, time_format=None): if date_format is None: date_format = _("%Y-%m-%d") if time_format is None: time_format = _("%H:%M (%Z)") start = self.get_start() end = self.get_end() all_day = not hasattr(start, 'date') start_date = start.date() if not all_day else start end_date = end.date() if not all_day else end if start_date == end_date: end_format = time_format else: end_format = date_format + " " + time_format if all_day: time_format = '' if start_date == end_date: return start.strftime(date_format) return "%s - %s" % (start.strftime(date_format + " " + time_format), end.strftime(end_format)) def get_recurrence_dates(self): return list(map(lambda _: xmlutils.from_cdatetime(_, True), self.event.recurrenceDates())) def get_exception_dates(self): return list(map(lambda _: xmlutils.from_cdatetime(_, True), self.event.exceptionDates())) def get_exceptions(self): return self._exceptions def has_exceptions(self): return len(self._exceptions) > 0 def get_attachments(self): return self.event.attachments() def get_attachment_data(self, i): vattach = self.event.attachments() if i < len(vattach): attachment = vattach[i] uri = attachment.uri() if uri and uri[0:4] == 'cid:': # get data from MIME part with matching content-id cid = '<' + uri[4:] + '>' for p in self._attachment_parts: if p['Content-ID'] == cid: return p.get_payload(decode=True) else: return attachment.data() return None def get_alarms(self): return self.event.alarms() def get_ical_attendee(self): # TODO: Formatting, aye? See also the example snippet: # # ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP: # MAILTO:employee-A@host.com attendees = [] for attendee in self.get_attendees(): email = attendee.get_email() name = attendee.get_name() rsvp = attendee.get_rsvp() role = attendee.get_role() partstat = attendee.get_participant_status() cutype = attendee.get_cutype() delegators = attendee.get_delegated_from() delegatees = attendee.get_delegated_to() if rsvp in attendee.rsvp_map: _rsvp = rsvp elif rsvp in attendee.rsvp_map.values(): _rsvp = [k for k, v in attendee.rsvp_map.items() if v == rsvp][0] else: _rsvp = None if role in attendee.role_map: _role = role elif role in attendee.role_map.values(): _role = [k for k, v in attendee.role_map.items() if v == role][0] else: _role = None if partstat in attendee.participant_status_map: _partstat = partstat elif partstat in attendee.participant_status_map.values(): _partstat = [k for k, v in attendee.participant_status_map.items() if v == partstat][0] else: _partstat = None if cutype in attendee.cutype_map: _cutype = cutype elif cutype in attendee.cutype_map.values(): _cutype = [k for k, v in attendee.cutype_map.items() if v == cutype][0] else: _cutype = None _attendee = icalendar.vCalAddress("MAILTO:%s" % email) if not name == None and not name == "": _attendee.params['CN'] = icalendar.vText(name) if not _rsvp == None: _attendee.params['RSVP'] = icalendar.vText(_rsvp) if not _role == None: _attendee.params['ROLE'] = icalendar.vText(_role) if not _partstat == None: _attendee.params['PARTSTAT'] = icalendar.vText(_partstat) if not _cutype == None: _attendee.params['CUTYPE'] = icalendar.vText(_cutype) if not delegators == None and len(delegators) > 0: _attendee.params['DELEGATED-FROM'] = icalendar.vText(delegators[0].email()) if not delegatees == None and len(delegatees) > 0: _attendee.params['DELEGATED-TO'] = icalendar.vText(delegatees[0].email()) attendees.append(_attendee) return attendees def get_ical_attendee_participant_status(self, attendee): attendee = self.get_attendee(attendee) if attendee.get_participant_status() in attendee.participant_status_map: return attendee.get_participant_status() elif attendee.get_participant_status() in attendee.participant_status_map.values(): return [k for k, v in attendee.participant_status_map.items() if v == attendee.get_participant_status()][0] else: raise ValueError(_("Invalid participant status")) def get_ical_created(self): return self.get_created() def get_ical_dtend(self): dtend = self.get_end() # shift end by one day on all-day events if not hasattr(dtend, 'hour'): dtend = dtend + datetime.timedelta(days=1) return dtend def get_ical_dtstamp(self): try: retval = self.get_lastmodified() if retval == None or retval == "": return datetime.datetime.now() except: return datetime.datetime.now() def get_ical_lastmodified(self): return self.get_ical_dtstamp() def get_ical_dtstart(self): return self.get_start() def get_ical_organizer(self): contact = self.get_organizer() organizer = icalendar.vCalAddress("MAILTO:%s" % contact.email()) name = contact.name() if not name == None and not name == "": organizer.params["CN"] = icalendar.vText(name) return organizer def get_ical_status(self): status = self.event.status() if status in self.status_map: return status return self._translate_value(status, self.status_map) if status else None def get_ical_class(self): class_ = self.event.classification() return self._translate_value(class_, self.classification_map) if class_ else None def get_ical_sequence(self): return str(self.event.sequence()) if self.event.sequence() else None def get_ical_comment(self): comment = self.get_comment() if comment is not None: return [ comment ] return None def get_ical_recurrenceid(self): rid = self.get_recurrence_id() if isinstance(rid, datetime.datetime) or isinstance(rid, datetime.date): prop = icalendar.vDatetime(rid) if isinstance(rid, datetime.datetime) else icalendar.vDate(rid) if self.thisandfuture: prop.params.update({'RANGE':'THISANDFUTURE'}) return prop return None def get_ical_rrule(self): result = [] rrule = self.get_recurrence() if rrule.isValid(): result.append(rrule.to_ical()) return result def get_ical_rdate(self): rdates = self.get_recurrence_dates() for i in range(len(rdates)): rdates[i] = icalendar.prop.vDDDLists(rdates[i]) return rdates def get_location(self): return self.event.location() def get_lastmodified(self): try: _datetime = self.event.lastModified() if _datetime == None or not _datetime.isValid(): self.__str__() except: self.__str__() return xmlutils.from_cdatetime(self.event.lastModified(), True) def get_organizer(self): organizer = self.event.organizer() return organizer def get_priority(self): return str(self.event.priority()) def get_start(self): return xmlutils.from_cdatetime(self.event.start(), True) def get_status(self, translated=False): status = self.event.status() if translated: return self._translate_value(status, self.status_map) if status else None return status def get_summary(self): return self.event.summary() def get_uid(self): uid = self.event.uid() if not uid == '': return uid else: self.set_uid(uuid.uuid4()) return self.get_uid() def get_recurrence_id(self): self.thisandfuture = self.event.thisAndFuture(); recurrence_id = xmlutils.from_cdatetime(self.event.recurrenceID(), True) # fix recurrence-id type if stored as date instead of datetime if recurrence_id is not None and isinstance(recurrence_id, datetime.date): dtstart = self.get_start() if not type(recurrence_id) == type(dtstart): recurrence_id = datetime.datetime.combine(recurrence_id, dtstart.time()).replace(tzinfo=dtstart.tzinfo) return recurrence_id def get_thisandfuture(self): self.thisandfuture = self.event.thisAndFuture(); return self.thisandfuture def get_sequence(self): return self.event.sequence() def get_url(self): return self.event.url() def get_transparency(self): return self.event.transparency() def get_recurrence(self): return RecurrenceRule(self.event.recurrenceRule()) def set_attendees(self, _attendees, recursive=False): if recursive: self._attendees = [] self.update_attendees(_attendees, True) else: self._attendees = _attendees self.event.setAttendees(self._attendees) def set_attendee_participant_status(self, attendee, status, rsvp=None): """ Set the participant status of an attendee to status. As the attendee arg, pass an email address or name, for this function to obtain the attendee object by searching the list of attendees for this event. """ attendee = self.get_attendee(attendee) attendee.set_participant_status(status) if rsvp is not None: attendee.set_rsvp(rsvp) # apply update to self and all exceptions self.update_attendees([attendee], False) def update_attendees(self, _attendees, append=True): self.merge_attendee_data(_attendees, append) if len(self._exceptions): vexceptions = self.event.exceptions() for i, exception in enumerate(self._exceptions): exception.merge_attendee_data(_attendees, append) vexceptions[i] = exception.event self.event.setExceptions(vexceptions) def merge_attendee_data(self, _attendees, append=True): for attendee in _attendees: found = False for candidate in self._attendees: if candidate.get_email() == attendee.get_email(): candidate.copy_from(attendee) found = True break if not found and append: self._attendees.append(attendee) self.event.setAttendees(self._attendees) def set_classification(self, classification): if classification in self.classification_map: self.event.setClassification(self.classification_map[classification]) elif classification in self.classification_map.values(): self.event.setClassification(classification) else: raise ValueError(_("Invalid classification %r") % (classification)) def set_created(self, _datetime=None): if _datetime is None or isinstance(_datetime, datetime.time): _datetime = datetime.datetime.utcnow() self.event.setCreated(xmlutils.to_cdatetime(_datetime, False, True)) def set_description(self, description): self.event.setDescription(ustr(description)) def set_comment(self, comment): if hasattr(self.event, 'setComment'): self.event.setComment(ustr(comment)) def set_dtstamp(self, _datetime): self.event.setLastModified(xmlutils.to_cdatetime(_datetime, False, True)) def set_end(self, _datetime): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): # If no timezone information is passed on, make it UTC if _datetime.tzinfo == None: _datetime = _datetime.replace(tzinfo=pytz.utc) valid_datetime = True if not valid_datetime: raise InvalidEventDateError(_("Event end needs datetime.date or datetime.datetime instance, got %r") % (type(_datetime))) self.event.setEnd(xmlutils.to_cdatetime(_datetime, True)) def set_exception_dates(self, _datetimes): for _datetime in _datetimes: self.add_exception_date(_datetime) def set_recurrence_dates(self, _datetimes): for _datetime in _datetimes: self.add_recurrence_date(_datetime) def add_custom_property(self, name, value): if not name.upper().startswith('X-'): raise ValueError(_("Invalid custom property name %r") % (name)) props = self.event.customProperties() props.append(kolabformat.CustomProperty(name.upper(), value)) self.event.setCustomProperties(props) def set_from_ical(self, attr, value): attr = attr.replace('-', '') ical_setter = 'set_ical_' + attr default_setter = 'set_' + attr params = value.params if hasattr(value, 'params') else {} if isinstance(value, icalendar.vDDDTypes) and hasattr(value, 'dt'): value = value.dt if attr == "categories": self.add_category(value) elif attr == "class": if (value and value[:2] not in ['X-', 'x-']): self.set_classification(value) elif attr == "recurrenceid": self.set_ical_recurrenceid(value, params) elif hasattr(self, ical_setter): getattr(self, ical_setter)(value) elif hasattr(self, default_setter): getattr(self, default_setter)(value) def set_ical_attendee(self, _attendee): if isinstance(_attendee, string_types): _attendee = [_attendee] if isinstance(_attendee, list): for attendee in _attendee: address = str(attendee).split(':')[-1] if hasattr(attendee, 'params'): params = attendee.params else: params = {} if 'CN' in params: name = ustr(params['CN']) else: name = None if 'ROLE' in params: role = params['ROLE'] else: role = None if 'PARTSTAT' in params: partstat = params['PARTSTAT'] else: partstat = None if 'RSVP' in params: rsvp = params['RSVP'] else: rsvp = None if 'CUTYPE' in params: cutype = params['CUTYPE'] else: cutype = kolabformat.CutypeIndividual att = self.add_attendee(address, name=name, rsvp=rsvp, role=role, participant_status=partstat, cutype=cutype, params=params) def set_ical_dtend(self, dtend): # shift end by one day on all-day events if not hasattr(dtend, 'hour'): dtend = dtend - datetime.timedelta(days=1) self.set_end(dtend) def set_ical_dtstamp(self, dtstamp): self.set_dtstamp(dtstamp) def set_ical_dtstart(self, dtstart): self.set_start(dtstart) def set_ical_lastmodified(self, lastmod): self.set_lastmodified(lastmod) def set_ical_duration(self, value): if hasattr(value, 'dt'): value = value.dt duration = kolabformat.Duration(value.days, 0, 0, value.seconds, False) self.event.setDuration(duration) def set_ical_organizer(self, organizer): address = str(organizer).split(':')[-1] cn = None if hasattr(organizer, 'params'): params = organizer.params else: params = {} if 'CN' in params: cn = ustr(params['CN']) self.set_organizer(str(address), name=cn) def set_ical_priority(self, priority): self.set_priority(priority) def set_ical_sequence(self, sequence): self.set_sequence(sequence) def set_ical_summary(self, summary): self.set_summary(ustr(summary)) def set_ical_uid(self, uid): self.set_uid(str(uid)) def set_ical_rdate(self, rdate): rdates = [] # rdate here can be vDDDLists or a list of vDDDLists, why?! if isinstance(rdate, icalendar.prop.vDDDLists): rdate = [rdate] for _rdate in rdate: if isinstance(_rdate, icalendar.prop.vDDDLists): tzid = None if hasattr(_rdate, 'params') and 'TZID' in _rdate.params: tzid = _rdate.params['TZID'] dts = icalendar.prop.vDDDLists.from_ical(_rdate.to_ical().decode(), tzid) for datetime in dts: rdates.append(datetime) self.set_recurrence_dates(rdates) def set_ical_recurrenceid(self, value, params): try: self.thisandfuture = params.get('RANGE', '') == 'THISANDFUTURE' self.set_recurrence_id(value, self.thisandfuture) except InvalidEventDateError: pass def set_lastmodified(self, _datetime=None): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): valid_datetime = True if _datetime is None or isinstance(_datetime, datetime.time): valid_datetime = True _datetime = datetime.datetime.utcnow() if not valid_datetime: raise InvalidEventDateError(_("Event last-modified needs datetime.date or datetime.datetime instance, got %r") % (type(_datetime))) self.event.setLastModified(xmlutils.to_cdatetime(_datetime, False, True)) def set_location(self, location): self.event.setLocation(ustr(location)) def set_organizer(self, email, name=None): contactreference = ContactReference(email) if not name == None: contactreference.setName(name) self.event.setOrganizer(contactreference) def set_priority(self, priority): self.event.setPriority(priority) def set_sequence(self, sequence): self.event.setSequence(int(sequence)) def set_url(self, url): self.event.setUrl(ustr(url)) def set_recurrence(self, recurrence): self.event.setRecurrenceRule(recurrence) # reset eventcal instance if hasattr(self, 'eventcal'): del self.eventcal def set_start(self, _datetime): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): # If no timezone information is passed on, make it UTC if _datetime.tzinfo == None: _datetime = _datetime.replace(tzinfo=pytz.utc) valid_datetime = True if not valid_datetime: raise InvalidEventDateError(_("Event start needs datetime.date or datetime.datetime instance, got %r") % (type(_datetime))) self.event.setStart(xmlutils.to_cdatetime(_datetime, True)) def set_status(self, status): if status in self.status_map: self.event.setStatus(self.status_map[status]) elif status in self.status_map.values(): self.event.setStatus(status) elif not status == kolabformat.StatusUndefined: raise InvalidEventStatusError(_("Invalid status set: %r") % (status)) def set_summary(self, summary): self.event.setSummary(summary) def set_uid(self, uid): self.uid = uid self.event.setUid(str(uid)) def set_recurrence_id(self, _datetime, _thisandfuture=None): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): # If no timezone information is passed on, use the one from event start if _datetime.tzinfo == None: _start = self.get_start() _datetime = _datetime.replace(tzinfo=_start.tzinfo) valid_datetime = True if not valid_datetime: raise InvalidEventDateError(_("Event recurrence-id needs datetime.datetime instance")) if _thisandfuture is None: _thisandfuture = self.thisandfuture self.event.setRecurrenceID(xmlutils.to_cdatetime(_datetime), _thisandfuture) def set_transparency(self, transp): return self.event.setTransparency(transp) def __str__(self): event_xml = kolabformat.writeEvent(self.event) error = kolabformat.error() if error == None or not error: return event_xml else: raise EventIntegrityError(kolabformat.errorMessage()) def to_dict(self): data = dict() for p, getter in self.properties_map.items(): val = None if hasattr(self, getter): val = getattr(self, getter)() elif hasattr(self.event, getter): val = getattr(self.event, getter)() if isinstance(val, kolabformat.cDateTime): val = xmlutils.from_cdatetime(val, True) elif isinstance(val, kolabformat.vectordatetime): val = [xmlutils.from_cdatetime(x, True) for x in val] elif isinstance(val, kolabformat.vectors): val = [str(x) for x in val] elif isinstance(val, kolabformat.vectorcs): for x in val: data[x.identifier] = x.value val = None elif isinstance(val, kolabformat.ContactReference): val = ContactReference(val).to_dict() elif isinstance(val, kolabformat.RecurrenceRule): val = RecurrenceRule(val).to_dict() elif isinstance(val, kolabformat.vectorattachment): val = [dict(fmttype=x.mimetype(), label=x.label(), uri=x.uri()) for x in val] elif isinstance(val, kolabformat.vectoralarm): val = [self._alarm_to_dict(x) for x in val] elif isinstance(val, list): val = [x.to_dict() for x in val if hasattr(x, 'to_dict')] if val is not None: data[p] = val return data def _alarm_to_dict(self, alarm): ret = dict( action=self._translate_value(alarm.type(), self.alarm_type_map), summary=alarm.summary(), description=alarm.description(), trigger=None ) start = alarm.start() if start and start.isValid(): ret['trigger'] = xmlutils.from_cdatetime(start, True) else: ret['trigger'] = dict(related=self._translate_value(alarm.relativeTo(), self.related_map)) duration = alarm.relativeStart() if duration and duration.isValid(): prefix = '-' if duration.isNegative() else '+' value = prefix + "P%dW%dDT%dH%dM%dS" % ( duration.weeks(), duration.days(), duration.hours(), duration.minutes(), duration.seconds() ) ret['trigger']['value'] = re.sub(r"T$", '', re.sub(r"0[WDHMS]", '', value)) if alarm.type() == kolabformat.Alarm.EMailAlarm: ret['attendee'] = [ContactReference(a).to_dict() for a in alarm.attendees()] return ret def _translate_value(self, val, map): name_map = dict([(v, k) for (k, v) in map.items()]) return name_map[val] if val in name_map else 'UNKNOWN' def to_message(self, creator=None): if sys.version_info.major >= 3: from email.mime.text import MIMEText from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.utils import COMMASPACE, formatdate else: from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate msg = MIMEMultipart() organizer = self.get_organizer() email = organizer.email() name = organizer.name() if creator: msg['From'] = creator elif not name: msg['From'] = email else: msg['From'] = '"%s" <%s>' % (name, email) msg['To'] = ', '.join([x.__str__() for x in self.get_attendees()]) msg['Date'] = formatdate(localtime=True) msg.add_header('X-Kolab-MIME-Version', '3.0') msg.add_header('X-Kolab-Type', 'application/x-vnd.kolab.' + self.type) text = utils.multiline_message(""" This is a Kolab Groupware object. To view this object you will need an email client that understands the Kolab Groupware format. For a list of such email clients please visit http://www.kolab.org/ """) msg.attach( MIMEText(text) ) part = MIMEBase('application', "calendar+xml") part.set_charset('UTF-8') msg["Subject"] = self.get_uid() # extract attachment data into separate MIME parts vattach = self.event.attachments() i = 0 for attach in vattach: if attach.uri(): continue mimetype = attach.mimetype() (primary, seconday) = mimetype.split('/') name = attach.label() if not name: name = 'unknown.x' (basename, suffix) = path.splitext(name) t = datetime.datetime.now() cid = "%s.%s.%s%s" % (basename, time.mktime(t.timetuple()), t.microsecond + len(self._attachment_parts), suffix) p = MIMEBase(primary, seconday) p.add_header('Content-Disposition', 'attachment', filename=name) p.add_header('Content-Transfer-Encoding', 'base64') p.add_header('Content-ID', '<' + cid + '>') p.set_payload(base64.b64encode(attach.data())) self._attachment_parts.append(p) # modify attachment object attach.setData('', mimetype) attach.setUri('cid:' + cid, mimetype) vattach[i] = attach i += 1 self.event.setAttachments(vattach) part.set_payload(str(self)) part.add_header('Content-Disposition', 'attachment; filename="kolab.xml"') part.replace_header('Content-Transfer-Encoding', '8bit') msg.attach(part) # append attachment parts for p in self._attachment_parts: msg.attach(p) return msg def to_message_itip(self, from_address, method="REQUEST", participant_status="ACCEPTED", subject=None, message_text=None): if sys.version_info.major >= 3: from email.mime.text import MIMEText from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.utils import COMMASPACE, formatdate else: from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email.Utils import COMMASPACE, formatdate # encode unicode strings with quoted-printable from email import charset charset.add_charset('utf-8', charset.SHORTEST, charset.QP) msg = MIMEMultipart("alternative") msg_from = None attendees = None if method == "REPLY": # TODO: Make user friendly name msg['To'] = self.get_organizer().email() attendees = self.get_attendees() reply_attendees = [] # There's an exception here for delegation (partstat DELEGATED) for attendee in attendees: if attendee.get_email() == from_address: # Only the attendee is supposed to be listed in a reply attendee.set_participant_status(participant_status) attendee.set_rsvp(False) reply_attendees.append(attendee) name = attendee.get_name() email = attendee.get_email() if not name: msg_from = email else: msg_from = '"%s" <%s>' % (name, email) elif from_address in attendee.get_delegated_from(True): reply_attendees.append(attendee) # keep only replying (and delegated) attendee(s) self._attendees = reply_attendees self.event.setAttendees(self._attendees) if msg_from == None: organizer = self.get_organizer() email = organizer.email() name = organizer.name() if email == from_address: if not name: msg_from = email else: msg_from = '"%s" <%s>' % (name, email) elif method == "REQUEST": organizer = self.get_organizer() email = organizer.email() name = organizer.name() if not name: msg_from = email else: msg_from = '"%s" <%s>' % (name, email) if msg_from == None: if from_address == None: log.error(_("No sender specified")) else: msg_from = from_address msg['From'] = utils.str2unicode(msg_from) msg['Date'] = formatdate(localtime=True) if subject is None: subject = _("Invitation for %s was %s") % (self.get_summary(), participant_status_label(participant_status)) msg['Subject'] = utils.str2unicode(subject) if message_text is None: message_text = _("""This is an automated response to one of your event requests.""") msg.attach(MIMEText(utils.stripped_message(message_text), _charset='utf-8')) part = MIMEBase('text', 'calendar', charset='UTF-8', method=method) del part['MIME-Version'] # mime parts don't need this part.set_payload(self.as_string_itip(method=method)) part.add_header('Content-Transfer-Encoding', '8bit') msg.attach(part) # restore the original list of attendees # attendees being reduced to the replying attendee above if attendees is not None: self._attendees = attendees self.event.setAttendees(self._attendees) return msg def is_recurring(self): return self.event.recurrenceRule().isValid() or len(self.get_recurrence_dates()) > 0 def to_event_cal(self): from kolab.calendaring import EventCal return EventCal(self.event) def get_next_occurence(self, _datetime): if not hasattr(self, 'eventcal'): self.eventcal = self.to_event_cal() next_cdatetime = self.eventcal.getNextOccurence(xmlutils.to_cdatetime(_datetime, True)) next_datetime = xmlutils.from_cdatetime(next_cdatetime, True) if next_cdatetime is not None else None # cut infinite recurrence at a reasonable point if next_datetime and not self.get_last_occurrence() and next_datetime > xmlutils.to_dt(self._recurrence_end()): next_datetime = None # next_datetime is always a cdatetime, convert to date if necessary if next_datetime and not isinstance(self.get_start(), datetime.datetime): next_datetime = datetime.date(next_datetime.year, next_datetime.month, next_datetime.day) return next_datetime def get_occurence_end_date(self, datetime): if not datetime: return None if not hasattr(self, 'eventcal'): return None end_cdatetime = self.eventcal.getOccurenceEndDate(xmlutils.to_cdatetime(datetime, True)) return xmlutils.from_cdatetime(end_cdatetime, True) if end_cdatetime is not None else None def get_last_occurrence(self, force=False): if not hasattr(self, 'eventcal'): self.eventcal = self.to_event_cal() last = self.eventcal.getLastOccurrence() last_datetime = xmlutils.from_cdatetime(last, True) if last is not None else None # we're forced to return some date if last_datetime is None and force: last_datetime = self._recurrence_end() return last_datetime def get_next_instance(self, datetime): next_start = self.get_next_occurence(datetime) if next_start: instance = Event(from_string=str(self)) instance.set_start(next_start) instance.event.setRecurrenceID(xmlutils.to_cdatetime(next_start), False) next_end = self.get_occurence_end_date(next_start) if next_end: instance.set_end(next_end) # unset recurrence rule and exceptions instance.set_recurrence(kolabformat.RecurrenceRule()) instance.event.setExceptions(kolabformat.vectorevent()) instance.event.setExceptionDates(kolabformat.vectordatetime()) instance._exceptions = [] instance._isexception = False # unset attachments list (only stored in main event) instance.event.setAttachments(kolabformat.vectorattachment()) # copy data from matching exception # (give precedence to single occurrence exceptions over thisandfuture) for exception in self._exceptions: recurrence_id = exception.get_recurrence_id() if recurrence_id == next_start and (not exception.thisandfuture or not instance._isexception): instance = exception instance._isexception = True if not exception.thisandfuture: break elif exception.thisandfuture and next_start > recurrence_id: # TODO: merge exception properties over this instance + adjust start/end with the according offset pass return instance return None def get_instance(self, _datetime): # If no timezone information is given, use the one from event start if isinstance(_datetime, datetime.datetime) and _datetime.tzinfo == None: _start = self.get_start() if hasattr(_start, 'tzinfo'): _datetime = _datetime.replace(tzinfo=_start.tzinfo) if self.is_recurring(): instance = self.get_next_instance(_datetime - datetime.timedelta(days=1)) while instance: recurrence_id = instance.get_recurrence_id() if type(recurrence_id) == type(_datetime) and recurrence_id <= _datetime: if xmlutils.dates_equal(recurrence_id, _datetime): return instance instance = self.get_next_instance(instance.get_start()) else: break elif self.has_exceptions(): for exception in self._exceptions: recurrence_id = exception.get_recurrence_id() if type(recurrence_id) == type(_datetime) and xmlutils.dates_equal(recurrence_id, _datetime): return exception if self.get_recurrence_id(): recurrence_id = self.get_recurrence_id() if type(recurrence_id) == type(_datetime) and xmlutils.dates_equal(recurrence_id, _datetime): return self return None def _recurrence_end(self): """ Determine a reasonable end date for infinitely recurring events """ rrule = self.event.recurrenceRule() if rrule.isValid() and rrule.count() < 0 and not rrule.end().isValid(): now = datetime.datetime.now() switch = { kolabformat.RecurrenceRule.Yearly: 100, kolabformat.RecurrenceRule.Monthly: 20 } intvl = switch[rrule.frequency()] if rrule.frequency() in switch else 10 return self.get_start().replace(year=now.year + intvl) return xmlutils.from_cdatetime(rrule.end(), True) class EventIntegrityError(Exception): def __init__(self, message): Exception.__init__(self, message) class InvalidEventDateError(Exception): def __init__(self, message): Exception.__init__(self, message) class InvalidEventStatusError(Exception): def __init__(self, message): Exception.__init__(self, message) diff --git a/pykolab/xml/note.py b/pykolab/xml/note.py index 936315b..81504c2 100644 --- a/pykolab/xml/note.py +++ b/pykolab/xml/note.py @@ -1,138 +1,140 @@ import pytz import datetime import kolabformat from pykolab.translate import _ from pykolab.xml import utils as xmlutils from pykolab.xml.utils import ustr def note_from_string(string): + if isinstance(string, bytes): + string = string.decode('utf-8') _xml = kolabformat.readNote(string, False) return Note(_xml) def note_from_message(message): note = None if message.is_multipart(): for part in message.walk(): if part.get_content_type() == "application/vnd.kolab+xml": payload = part.get_payload(decode=True) note = note_from_string(payload) # append attachment parts to Note object elif note and 'Content-ID' in part: note._attachment_parts.append(part) return note class Note(kolabformat.Note): type = 'note' classification_map = { 'PUBLIC': kolabformat.ClassPublic, 'PRIVATE': kolabformat.ClassPrivate, 'CONFIDENTIAL': kolabformat.ClassConfidential, } properties_map = { 'uid': 'get_uid', 'summary': 'summary', 'description': 'description', 'created': 'get_created', 'lastmodified-date': 'get_lastmodified', 'classification': 'get_classification', 'categories': 'categories', 'color': 'color', } def __init__(self, *args, **kw): self._attachment_parts = [] kolabformat.Note.__init__(self, *args, **kw) def get_uid(self): uid = self.uid() if not uid == '': return uid else: self.__str__() return kolabformat.getSerializedUID() def get_created(self): try: return xmlutils.from_cdatetime(self.created(), True) except ValueError: return datetime.datetime.now() def get_lastmodified(self): try: _datetime = self.lastModified() if _datetime == None or not _datetime.isValid(): self.__str__() except: return datetime.datetime.now(pytz.utc) return xmlutils.from_cdatetime(self.lastModified(), True) def set_summary(self, summary): self.setSummary(ustr(summary)) def set_description(self, description): self.setDescription(ustr(description)) def get_classification(self, translated=True): _class = self.classification() if translated: return self._translate_value(_class, self.classification_map) return _class def set_classification(self, classification): if classification in self.classification_map: self.setClassification(self.classification_map[classification]) elif classification in self.classification_map.values(): self.setClassification(status) else: raise ValueError(_("Invalid classification %r") % (classification)) def add_category(self, category): _categories = self.categories() _categories.append(ustr(category)) self.setCategories(_categories) def _translate_value(self, val, map): name_map = dict([(v, k) for (k, v) in map.items()]) return name_map[val] if val in name_map else 'UNKNOWN' def to_dict(self): if not self.isValid(): return None data = dict() for p, getter in self.properties_map.items(): val = None if hasattr(self, getter): val = getattr(self, getter)() if isinstance(val, kolabformat.cDateTime): val = xmlutils.from_cdatetime(val, True) elif isinstance(val, kolabformat.vectori): val = [int(x) for x in val] elif isinstance(val, kolabformat.vectors): val = [str(x) for x in val] if val is not None: data[p] = val return data def __str__(self): xml = kolabformat.writeNote(self) error = kolabformat.error() if error == None or not error: return xml else: raise NoteIntegrityError(kolabformat.errorMessage()) class NoteIntegrityError(Exception): def __init__(self, message): Exception.__init__(self, message) diff --git a/pykolab/xml/todo.py b/pykolab/xml/todo.py index 8fa4abd..da65c45 100644 --- a/pykolab/xml/todo.py +++ b/pykolab/xml/todo.py @@ -1,272 +1,274 @@ import datetime import kolabformat import icalendar import pytz import base64 import sys import pykolab from pykolab import constants from pykolab.xml import Event from pykolab.xml import RecurrenceRule from pykolab.xml import utils as xmlutils from pykolab.xml.event import InvalidEventDateError from pykolab.translate import _ log = pykolab.getLogger('pykolab.xml_todo') def todo_from_ical(ical, string=None): return Todo(from_ical=ical, from_string=string) def todo_from_string(string): + if isinstance(string, bytes): + string = string.decode('utf-8') return Todo(from_string=string) def todo_from_message(message): todo = None if message.is_multipart(): for part in message.walk(): if part.get_content_type() == "application/calendar+xml": payload = part.get_payload(decode=True) todo = todo_from_string(payload) # append attachment parts to Todo object elif todo and 'Content-ID' in part: todo._attachment_parts.append(part) return todo # FIXME: extend a generic pykolab.xml.Xcal class instead of Event class Todo(Event): type = 'task' # This have to be a copy (see T1221) properties_map = Event.properties_map.copy() def __init__(self, from_ical="", from_string=""): self._attendees = [] self._categories = [] self._exceptions = [] self._attachment_parts = [] self.properties_map.update({ "due": "get_due", "percent-complete": "get_percentcomplete", "related-to": "get_related_to", "duration": "void", "end": "void" }) if isinstance(from_ical, str) and from_ical == "": if from_string == "": self.event = kolabformat.Todo() else: self.event = kolabformat.readTodo(from_string, False) self._load_attendees() else: self.from_ical(from_ical, from_string) self.set_created(self.get_created()) self.uid = self.get_uid() def from_ical(self, ical, raw): if isinstance(ical, icalendar.Todo): ical_todo = ical elif hasattr(icalendar.Todo, 'from_ical'): ical_todo = icalendar.Todo.from_ical(ical) elif hasattr(icalendar.Todo, 'from_string'): ical_todo = icalendar.Todo.from_string(ical) # VCALENDAR block was given, find the first VTODO item if isinstance(ical_todo, icalendar.Calendar): for c in ical_todo.walk(): if c.name == 'VTODO': ical_todo = c break log.debug("Todo.from_ical(); %r, %r, %r" % (type(ical_todo), 'ATTACH' in ical_todo, 'ATTENDEE' in ical_todo), level=8) # DISABLED: use the libkolab calendaring bindings to load the full iCal data # TODO: this requires support for iCal parsing in the kolab.calendaring bindings if False and 'ATTACH' in ical_todo or [part for part in ical_todo.walk() if part.name == 'VALARM']: if raw is None or raw == "": raw = ical if isinstance(ical, str) else ical.to_ical() self._xml_from_ical(raw) else: self.event = kolabformat.Todo() for attr in list(set(ical_todo.required)): if attr in ical_todo: self.set_from_ical(attr.lower(), ical_todo[attr]) for attr in list(set(ical_todo.singletons)): if attr in ical_todo: if isinstance(ical_todo[attr], list): ical_todo[attr] = ical_todo[attr][0]; self.set_from_ical(attr.lower(), ical_todo[attr]) for attr in list(set(ical_todo.multiple)): if attr in ical_todo: self.set_from_ical(attr.lower(), ical_todo[attr]) # although specified by RFC 2445/5545, icalendar doesn't have this property listed if 'PERCENT-COMPLETE' in ical_todo: self.set_from_ical('percentcomplete', ical_todo['PERCENT-COMPLETE']) def _xml_from_ical(self, ical): # FIXME: kolabformat or kolab.calendaring modules do not provide bindings to import Todo from iCal self.event = Todo() def set_ical_attach(self, attachment): if hasattr(attachment, 'params'): params = attachment.params else: params = {} _attachment = kolabformat.Attachment() if 'FMTTYPE' in params: mimetype = str(params['FMTTYPE']) else: mimetype = 'application/octet-stream' if 'X-LABEL' in params: _attachment.setLabel(str(params['X-LABEL'])) decode = False if 'ENCODING' in params: if params['ENCODING'] == "BASE64" or params['ENCODING'] == "B": decode = True if sys.version_info.major >= 3: _attachment.setData(base64.b64decode(attachment.encode('latin-1')).decode('latin-1') if decode else str(attachment), mimetype) else: _attachment.setData(base64.b64decode(str(attachment)) if decode else str(attachment), mimetype) vattach = self.event.attachments() vattach.append(_attachment) self.event.setAttachments(vattach) def set_ical_rrule(self, rrule): _rrule = RecurrenceRule() _rrule.from_ical(rrule) if _rrule.isValid(): self.event.setRecurrenceRule(_rrule) def set_ical_due(self, due): self.set_due(due) def set_due(self, _datetime): valid_datetime = False if isinstance(_datetime, datetime.date): valid_datetime = True if isinstance(_datetime, datetime.datetime): # If no timezone information is passed on, make it UTC if _datetime.tzinfo == None: _datetime = _datetime.replace(tzinfo=pytz.utc) valid_datetime = True if not valid_datetime: raise InvalidEventDateError(_("Todo due needs datetime.date or datetime.datetime instance")) self.event.setDue(xmlutils.to_cdatetime(_datetime, True)) def set_ical_percent(self, percent): self.set_percentcomplete(percent) def set_percentcomplete(self, percent): self.event.setPercentComplete(int(percent)) def set_transparency(self, transp): # empty stub pass def get_due(self): return xmlutils.from_cdatetime(self.event.due(), True) def get_ical_due(self): dt = self.get_due() if dt: return icalendar.vDatetime(dt) return None def get_percentcomplete(self): return self.event.percentComplete() def get_duration(self): return None def get_related_to(self): for x in self.event.relatedTo(): return x return None def as_string_itip(self, method="REQUEST"): cal = icalendar.Calendar() cal.add( 'prodid', '-//pykolab-%s-%s//kolab.org//' % ( constants.__version__, constants.__release__ ) ) cal.add('version', '2.0') cal.add('calscale', 'GREGORIAN') cal.add('method', method) ical_todo = icalendar.Todo() singletons = list(set(ical_todo.singletons)) singletons.extend(['PERCENT-COMPLETE']) for attr in singletons: ical_getter = 'get_ical_%s' % (attr.lower()) default_getter = 'get_%s' % (attr.lower()) retval = None if hasattr(self, ical_getter): retval = getattr(self, ical_getter)() if not retval == None and not retval == "": ical_todo.add(attr.lower(), retval) elif hasattr(self, default_getter): retval = getattr(self, default_getter)() if not retval == None and not retval == "": ical_todo.add(attr.lower(), retval, encode=0) for attr in list(set(ical_todo.multiple)): ical_getter = 'get_ical_%s' % (attr.lower()) default_getter = 'get_%s' % (attr.lower()) retval = None if hasattr(self, ical_getter): retval = getattr(self, ical_getter)() elif hasattr(self, default_getter): retval = getattr(self, default_getter)() if isinstance(retval, list) and not len(retval) == 0: for _retval in retval: ical_todo.add(attr.lower(), _retval, encode=0) # copy custom properties to iCal for cs in self.event.customProperties(): ical_todo.add(cs.identifier, cs.value) cal.add_component(ical_todo) if hasattr(cal, 'to_ical'): return cal.to_ical() elif hasattr(cal, 'as_string'): return cal.as_string() def __str__(self): xml = kolabformat.writeTodo(self.event) error = kolabformat.error() if error == None or not error: return xml else: raise TodoIntegrityError(kolabformat.errorMessage()) class TodoIntegrityError(Exception): def __init__(self, message): Exception.__init__(self, message) diff --git a/wallace/module_gpgencrypt.py b/wallace/module_gpgencrypt.py index a0ddc2e..7a4a152 100644 --- a/wallace/module_gpgencrypt.py +++ b/wallace/module_gpgencrypt.py @@ -1,293 +1,292 @@ # -*- coding: utf-8 -*- # Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com) # # Jeroen van Meeuwen (Kolab Systems) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 3 or, at your option, any later version # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # import os import tempfile import time -from email import message_from_string from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.parser import Parser from email.utils import formataddr from email.utils import getaddresses import email.mime.application import email.mime.multipart import email.mime.text import email.encoders import gnupg from . import modules import pykolab from pykolab.translate import _ log = pykolab.getLogger('pykolab.wallace/gpgencrypt') extra_log_params = {'qid': '-'} log = pykolab.logger.LoggerAdapter(log, extra_log_params) conf = pykolab.getConf() mybasepath = '/var/spool/pykolab/wallace/gpgencrypt/' def __init__(): modules.register('gpgencrypt', execute, description=description()) def description(): return """Encrypt messages to the recipient(s).""" def pgp_mime(msg, recepients): gpg = gnupg.GPG(gnupghome='/var/lib/kolab/.gnupg', verbose=conf.debuglevel > 8) gpg.encoding = 'utf-8' msg = msg msg_boundary = str(msg.get_boundary()) msg_content_type = str(msg.get_content_type()) payload = msg.get_payload() content = "Content-Type: " + msg_content_type + ";" + "\n boundary=\"" + msg_boundary + "\"\n\n" + payload encrypted_content = gpg.encrypt(content, recepients, always_trust=True) msg.set_type("multipart/encrypted") msg.set_param("protocol","application/pgp-encrypted") msg_boundary_gpg = "--boundary-gpg-encryption-42" msg_preamble = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)\n\ " + msg_boundary_gpg + "\n\ Content-Type: application/pgp-encrypted\n\ Content-Description: PGP/MIME version identification\n\ \n\ Version: 1\n\ \n\ " + msg_boundary_gpg + "\n\ Content-Type: application/octet-stream; name=\"encrypted.asc\"\n\ Content-Description: OpenPGP encrypted message\n\ Content-Disposition: inline; filename=\"encrypted.asc\"\n\n" msg.set_boundary(msg_boundary_gpg) msg.set_payload(msg_preamble + str(encrypted_content) + "\n" + msg_boundary_gpg) return msg def execute(*args, **kw): global extra_log_params # TODO: Test for correct call. filepath = args[0] extra_log_params['qid'] = os.path.basename(filepath) if not os.path.isdir(mybasepath): os.makedirs(mybasepath) for stage in ['incoming', 'ACCEPT' ]: if not os.path.isdir(os.path.join(mybasepath, stage)): os.makedirs(os.path.join(mybasepath, stage)) if 'stage' in kw: log.debug(_("Issuing callback after processing to stage %s") % (kw['stage']), level=8) log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8) if hasattr(modules, 'cb_action_%s' % (kw['stage'])): log.debug(_("Attempting to execute cb_action_%s()") % (kw['stage']), level=8) exec('modules.cb_action_%s(%r, %r)' % (kw['stage'],'gpgencrypt',filepath)) log.debug(_("Executing module gpgencrypt for %r, %r") % (args, kw), level=8) new_filepath = os.path.join('/var/spool/pykolab/wallace/gpgencrypt/incoming', os.path.basename(filepath)) if not filepath == new_filepath: log.debug("Renaming %r to %r" % (filepath, new_filepath)) os.rename(filepath, new_filepath) filepath = new_filepath # parse message headers # @TODO: make sure we can use True as the 2nd argument here message = Parser().parse(open(filepath, 'r'), True) # Possible gpgencrypt answers are limited to ACCEPT only answers = [ 'ACCEPT' ] # from Mail::GnuPG.is_encrypted # #sub is_encrypted { # my ($self,$entity) = @_; # return 1 # if (($entity->effective_type =~ m!multipart/encrypted!) # || # ($entity->as_string =~ m!^-----BEGIN PGP MESSAGE-----!m)); # return 0; #} message_already_encrypted = False for part in message.walk(): if part.get_content_type() in [ "application/pgp-encrypted" ]: message_already_encrypted = True log.debug(_("Message is already encrypted (app/pgp-enc content-type)"), level=8) if message.get_content_type() in [ "multipart/encrypted" ]: message_already_encrypted = True log.debug(_("Message already encrypted by main content-type header"), level=8) if message_already_encrypted: return filepath try: # What are recipient addresses to encrypt to (bitmask)? # 1 - organization key # 2 - envelope to # 4 - to # 8 - cc # 16 - resent-to # 32 - resent-cc encrypt_to_rcpts = conf.get('wallace', 'gpgencrypt_to_rcpts') if encrypt_to_rcpts == None: encrypt_to_rcpts = 14 else: encrypt_to_rcpts = (int)(encrypt_to_rcpts) # Only encrypt to keys that are trusted strict_crypt = conf.get('wallace', 'gpgencrypt_strict') if strict_crypt == None: strict_crypt = False # Organization key ID if encrypt_to_rcpts & 1: encrypt_to_org = conf.get('wallace', 'gpgencrypt_to_org_key') if encrypt_to_org == None and encrypt_to_rcpts & 1: if strict_crypt: log.error(_("Configured to encrypt to a key not configured, and strict policy enabled. Bailing out.")) modules.cb_action_REJECT('gpgencrypt',filepath) else: log.error(_("Configured to encrypt to a key not configured, but continuing anyway (see 'gpgencrypt_strict').")) else: encrypt_to_org = [] # Bounce the message if encryption fails? force_crypt = conf.get('wallace', 'gpgencrypt_force') if force_crypt == None: force_crypt = False # Retrieve keys from remote server(s) automatically? retrieve_keys = conf.get('wallace', 'gpgencrypt_retrieve_keys') if retrieve_keys == None: retrieve_keys = False if retrieve_keys: gpgserver = conf.get('wallace', 'gpgencrypt_server') if gpgserver == None: gpgserver = 'pgp.mit.edu' encrypt_to = [] if encrypt_to_rcpts & 2: encrypt_to.extend(message.get_all('X-Kolab-To', [])) if encrypt_to_rcpts & 4: encrypt_to.extend(message.get_all('to', [])) if encrypt_to_rcpts & 8: encrypt_to.extend(message.get_all('cc', [])) if encrypt_to_rcpts & 16: encrypt_to.extend(message.get_all('resent-to', [])) if encrypt_to_rcpts & 32: encrypt_to.extend(message.get_all('resent-cc', [])) recipients = [address for displayname,address in getaddresses(encrypt_to)] log.debug(_("Recipients: %r") % (recipients)) # Split between recipients we can encrypt for/to, and ones we can not encrypt_rcpts = [] nocrypt_rcpts = [] gpg = gnupg.GPG(gnupghome='/var/lib/kolab/.gnupg', verbose=conf.debuglevel > 8) gpg.encoding = 'utf-8' local_keys = gpg.list_keys() log.debug(_("Current keys: %r") % (local_keys), level=8) for recipient in recipients: key_local = False log.debug(_("Retrieving key for recipient: %r") % (recipient)) for key in local_keys: for address in [x for x in [address for displayname,address in getaddresses(key['uids'])] if x == recipient]: log.debug(_("Found matching address %r") % (address)) key_local = key['keyid'] if key_local == False: if retrieve_keys: remote_keys = gpg.search_keys(recipient, gpgserver) if len(remote_keys) == 1: for address in [x for x in [address for displayname,address in getaddresses(remote_keys[0]['uids'])] if x == recipient]: log.debug(_("Found matching address %r in remote keys") % (address)) gpg.recv_keys(gpgserver, remote_keys[0]['keyid']) local_keys = gpg.list_keys() else: nocrypt_rcpts.append(recipient) for key in local_keys: for address in [x for x in [address for displayname,address in getaddresses(key['uids'])] if x == recipient]: log.debug(_("Found matching address %r") % (address)) key_local = key['keyid'] if not key_local == False: encrypt_rcpts.append(key_local) payload = message.get_payload() #print "payload:", payload if len(encrypt_rcpts) < 1: return filepath if "multipart" in message.get_content_type(): log.debug(_("Mime Message - we need to build multipart/encrypted structure"), level=8) msg = message enc_mime_message = pgp_mime(msg, encrypt_rcpts) message = enc_mime_message else: log.debug(_("No Mime Message - encypt plain"), level=8) encrypted_data = gpg.encrypt(payload, encrypt_rcpts, always_trust=True) encrypted_string = str(encrypted_data) message.set_payload(encrypted_string) message.add_header('X-wallace-gpg-encrypted', 'true') (fp, new_filepath) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/gpgencrypt/ACCEPT") os.write(fp, message.as_string()) os.close(fp) os.unlink(filepath) exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','gpgencrypt', new_filepath)) except Exception as errmsg: log.error(_("An error occurred: %r") % (errmsg)) if conf.debuglevel > 8: import traceback traceback.print_exc() diff --git a/wallace/module_invitationpolicy.py b/wallace/module_invitationpolicy.py index 9cfcac1..efe26fa 100644 --- a/wallace/module_invitationpolicy.py +++ b/wallace/module_invitationpolicy.py @@ -1,1498 +1,1498 @@ # -*- coding: utf-8 -*- # Copyright 2014 Kolab Systems AG (http://www.kolabsys.com) # # Thomas Bruederli (Kolab Systems) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # from six import string_types import datetime import os import random import signal import sys import tempfile import time try: from urlparse import urlparse except ImportError: from urllib.parse import urlparse import urllib import hashlib import traceback import re -from email import message_from_string +from email import message_from_bytes from email.parser import Parser from email.utils import formataddr from email.utils import getaddresses from . import modules import pykolab import kolabformat from pykolab import utils from pykolab.auth import Auth from pykolab.conf import Conf from pykolab.imap import IMAP from pykolab.xml import to_dt from pykolab.xml import utils as xmlutils from pykolab.xml import todo_from_message from pykolab.xml import event_from_message from pykolab.xml import participant_status_label from pykolab.itip import objects_from_message from pykolab.itip import check_event_conflict from pykolab.itip import send_reply from pykolab.translate import _ # define some contstants used in the code below ACT_MANUAL = 1 ACT_ACCEPT = 2 ACT_DELEGATE = 4 ACT_REJECT = 8 ACT_UPDATE = 16 ACT_CANCEL_DELETE = 32 ACT_SAVE_TO_FOLDER = 64 COND_IF_AVAILABLE = 128 COND_IF_CONFLICT = 256 COND_TENTATIVE = 512 COND_NOTIFY = 1024 COND_FORWARD = 2048 COND_TYPE_EVENT = 4096 COND_TYPE_TASK = 8192 COND_TYPE_ALL = COND_TYPE_EVENT + COND_TYPE_TASK ACT_TENTATIVE = ACT_ACCEPT + COND_TENTATIVE ACT_UPDATE_AND_NOTIFY = ACT_UPDATE + COND_NOTIFY ACT_SAVE_AND_FORWARD = ACT_SAVE_TO_FOLDER + COND_FORWARD ACT_CANCEL_DELETE_AND_NOTIFY = ACT_CANCEL_DELETE + COND_NOTIFY FOLDER_TYPE_ANNOTATION = '/vendor/kolab/folder-type' MESSAGE_PROCESSED = 1 MESSAGE_FORWARD = 2 policy_name_map = { # policy values applying to all object types 'ALL_MANUAL': ACT_MANUAL + COND_TYPE_ALL, 'ALL_ACCEPT': ACT_ACCEPT + COND_TYPE_ALL, 'ALL_REJECT': ACT_REJECT + COND_TYPE_ALL, 'ALL_DELEGATE': ACT_DELEGATE + COND_TYPE_ALL, # not implemented 'ALL_UPDATE': ACT_UPDATE + COND_TYPE_ALL, 'ALL_UPDATE_AND_NOTIFY': ACT_UPDATE_AND_NOTIFY + COND_TYPE_ALL, 'ALL_SAVE_TO_FOLDER': ACT_SAVE_TO_FOLDER + COND_TYPE_ALL, 'ALL_SAVE_AND_FORWARD': ACT_SAVE_AND_FORWARD + COND_TYPE_ALL, 'ALL_CANCEL_DELETE': ACT_CANCEL_DELETE + COND_TYPE_ALL, 'ALL_CANCEL_DELETE_AND_NOTIFY': ACT_CANCEL_DELETE_AND_NOTIFY + COND_TYPE_ALL, # event related policy values 'EVENT_MANUAL': ACT_MANUAL + COND_TYPE_EVENT, 'EVENT_ACCEPT': ACT_ACCEPT + COND_TYPE_EVENT, 'EVENT_TENTATIVE': ACT_TENTATIVE + COND_TYPE_EVENT, 'EVENT_REJECT': ACT_REJECT + COND_TYPE_EVENT, 'EVENT_DELEGATE': ACT_DELEGATE + COND_TYPE_EVENT, # not implemented 'EVENT_UPDATE': ACT_UPDATE + COND_TYPE_EVENT, 'EVENT_UPDATE_AND_NOTIFY': ACT_UPDATE_AND_NOTIFY + COND_TYPE_EVENT, 'EVENT_ACCEPT_IF_NO_CONFLICT': ACT_ACCEPT + COND_IF_AVAILABLE + COND_TYPE_EVENT, 'EVENT_TENTATIVE_IF_NO_CONFLICT': ACT_ACCEPT + COND_TENTATIVE + COND_IF_AVAILABLE + COND_TYPE_EVENT, 'EVENT_DELEGATE_IF_CONFLICT': ACT_DELEGATE + COND_IF_CONFLICT + COND_TYPE_EVENT, 'EVENT_REJECT_IF_CONFLICT': ACT_REJECT + COND_IF_CONFLICT + COND_TYPE_EVENT, 'EVENT_SAVE_TO_FOLDER': ACT_SAVE_TO_FOLDER + COND_TYPE_EVENT, 'EVENT_SAVE_AND_FORWARD': ACT_SAVE_AND_FORWARD + COND_TYPE_EVENT, 'EVENT_CANCEL_DELETE': ACT_CANCEL_DELETE + COND_TYPE_EVENT, 'EVENT_CANCEL_DELETE_AND_NOTIFY': ACT_CANCEL_DELETE_AND_NOTIFY + COND_TYPE_EVENT, # task related policy values 'TASK_MANUAL': ACT_MANUAL + COND_TYPE_TASK, 'TASK_ACCEPT': ACT_ACCEPT + COND_TYPE_TASK, 'TASK_REJECT': ACT_REJECT + COND_TYPE_TASK, 'TASK_DELEGATE': ACT_DELEGATE + COND_TYPE_TASK, # not implemented 'TASK_UPDATE': ACT_UPDATE + COND_TYPE_TASK, 'TASK_UPDATE_AND_NOTIFY': ACT_UPDATE_AND_NOTIFY + COND_TYPE_TASK, 'TASK_SAVE_TO_FOLDER': ACT_SAVE_TO_FOLDER + COND_TYPE_TASK, 'TASK_SAVE_AND_FORWARD': ACT_SAVE_AND_FORWARD + COND_TYPE_TASK, 'TASK_CANCEL_DELETE': ACT_CANCEL_DELETE + COND_TYPE_TASK, 'TASK_CANCEL_DELETE_AND_NOTIFY': ACT_CANCEL_DELETE_AND_NOTIFY + COND_TYPE_TASK, # legacy values 'ACT_MANUAL': ACT_MANUAL + COND_TYPE_ALL, 'ACT_ACCEPT': ACT_ACCEPT + COND_TYPE_ALL, 'ACT_ACCEPT_IF_NO_CONFLICT': ACT_ACCEPT + COND_IF_AVAILABLE + COND_TYPE_EVENT, 'ACT_TENTATIVE': ACT_TENTATIVE + COND_TYPE_EVENT, 'ACT_TENTATIVE_IF_NO_CONFLICT': ACT_ACCEPT + COND_TENTATIVE + COND_IF_AVAILABLE + COND_TYPE_EVENT, 'ACT_DELEGATE': ACT_DELEGATE + COND_TYPE_ALL, 'ACT_DELEGATE_IF_CONFLICT': ACT_DELEGATE + COND_IF_CONFLICT + COND_TYPE_EVENT, 'ACT_REJECT': ACT_REJECT + COND_TYPE_ALL, 'ACT_REJECT_IF_CONFLICT': ACT_REJECT + COND_IF_CONFLICT + COND_TYPE_EVENT, 'ACT_UPDATE': ACT_UPDATE + COND_TYPE_ALL, 'ACT_UPDATE_AND_NOTIFY': ACT_UPDATE_AND_NOTIFY + COND_TYPE_ALL, 'ACT_CANCEL_DELETE': ACT_CANCEL_DELETE + COND_TYPE_ALL, 'ACT_CANCEL_DELETE_AND_NOTIFY': ACT_CANCEL_DELETE_AND_NOTIFY + COND_TYPE_ALL, 'ACT_SAVE_TO_CALENDAR': ACT_SAVE_TO_FOLDER + COND_TYPE_EVENT, 'ACT_SAVE_AND_FORWARD': ACT_SAVE_AND_FORWARD + COND_TYPE_EVENT, } policy_value_map = dict([(v &~ COND_TYPE_ALL, k) for (k, v) in policy_name_map.items()]) object_type_conditons = { 'event': COND_TYPE_EVENT, 'task': COND_TYPE_TASK } log = pykolab.getLogger('pykolab.wallace/invitationpolicy') extra_log_params = {'qid': '-'} log = pykolab.logger.LoggerAdapter(log, extra_log_params) conf = pykolab.getConf() mybasepath = '/var/spool/pykolab/wallace/invitationpolicy/' auth = None imap = None write_locks = [] def __init__(): modules.register('invitationpolicy', execute, description=description()) def accept(filepath): new_filepath = os.path.join( mybasepath, 'ACCEPT', os.path.basename(filepath) ) cleanup() os.rename(filepath, new_filepath) filepath = new_filepath exec('modules.cb_action_ACCEPT(%r, %r)' % ('invitationpolicy',filepath)) def reject(filepath): new_filepath = os.path.join( mybasepath, 'REJECT', os.path.basename(filepath) ) os.rename(filepath, new_filepath) filepath = new_filepath exec('modules.cb_action_REJECT(%r, %r)' % ('invitationpolicy',filepath)) def description(): return """Invitation policy execution module.""" def cleanup(): global auth, imap, write_locks, extra_log_params log.debug("cleanup(): %r, %r" % (auth, imap), level=8) extra_log_params['qid'] = '-' auth.disconnect() del auth # Disconnect IMAP or we lock the mailbox almost constantly imap.disconnect() del imap # remove remaining write locks for key in write_locks: remove_write_lock(key, False) def execute(*args, **kw): global auth, imap, extra_log_params filepath = args[0] extra_log_params['qid'] = os.path.basename(filepath) # (re)set language to default pykolab.translate.setUserLanguage(conf.get('kolab','default_locale')) if not os.path.isdir(mybasepath): os.makedirs(mybasepath) for stage in ['incoming', 'ACCEPT', 'REJECT', 'HOLD', 'DEFER', 'locks']: if not os.path.isdir(os.path.join(mybasepath, stage)): os.makedirs(os.path.join(mybasepath, stage)) log.debug(_("Invitation policy called for %r, %r") % (args, kw), level=8) auth = Auth() imap = IMAP() # ignore calls on lock files if '/locks/' in filepath or 'stage' in kw and kw['stage'] == 'locks': return False log.debug("Invitation policy executing for %r, %r" % (filepath, '/locks/' in filepath), level=8) if 'stage' in kw: log.debug(_("Issuing callback after processing to stage %s") % (kw['stage']), level=8) log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8) if hasattr(modules, 'cb_action_%s' % (kw['stage'])): log.debug(_("Attempting to execute cb_action_%s()") % (kw['stage']), level=8) exec( 'modules.cb_action_%s(%r, %r)' % ( kw['stage'], 'invitationpolicy', filepath ) ) return filepath else: # Move to incoming new_filepath = os.path.join( mybasepath, 'incoming', os.path.basename(filepath) ) if not filepath == new_filepath: log.debug("Renaming %r to %r" % (filepath, new_filepath)) os.rename(filepath, new_filepath) filepath = new_filepath # parse full message message = Parser().parse(open(filepath, 'r')) # invalid message, skip if not message.get('X-Kolab-To'): return filepath recipients = [address for displayname,address in getaddresses(message.get_all('X-Kolab-To'))] sender_email = [address for displayname,address in getaddresses(message.get_all('X-Kolab-From'))][0] any_itips = False recipient_email = None recipient_emails = [] recipient_user_dn = None # An iTip message may contain multiple events. Later on, test if the message # is an iTip message by checking the length of this list. try: itip_events = objects_from_message(message, ['VEVENT','VTODO'], ['REQUEST', 'REPLY', 'CANCEL']) except Exception as errmsg: log.error(_("Failed to parse iTip objects from message: %r" % (errmsg))) itip_events = [] if not len(itip_events) > 0: log.info(_("Message is not an iTip message or does not contain any (valid) iTip objects.")) else: any_itips = True log.debug(_("iTip objects attached to this message contain the following information: %r") % (itip_events), level=8) # See if any iTip actually allocates a user. if any_itips and len([x['uid'] for x in itip_events if 'attendees' in x or 'organizer' in x]) > 0: auth.connect() # we're looking at the first itip object itip_event = itip_events[0] for recipient in recipients: recipient_user_dn = user_dn_from_email_address(recipient) if recipient_user_dn: receiving_user = auth.get_entry_attributes(None, recipient_user_dn, ['*']) recipient_emails = auth.extract_recipient_addresses(receiving_user) recipient_email = recipient # extend with addresses from delegators # (only do this lookup for REPLY messages) receiving_user['_delegated_mailboxes'] = [] if itip_event['method'] == 'REPLY': for _delegator in auth.list_delegators(recipient_user_dn): if not _delegator['_mailbox_basename'] == None: receiving_user['_delegated_mailboxes'].append( _delegator['_mailbox_basename'].split('@')[0] ) log.debug(_("Recipient emails for %s: %r") % (recipient_user_dn, recipient_emails), level=8) break if not any_itips: log.debug(_("No itips, no users, pass along %r") % (filepath), level=5) return filepath elif recipient_email is None: log.debug(_("iTips, but no users, pass along %r") % (filepath), level=5) return filepath # for replies, the organizer is the recipient if itip_event['method'] == 'REPLY': # Outlook can send iTip replies without an organizer property if 'organizer' in itip_event: organizer_mailto = str(itip_event['organizer']).split(':')[-1] user_attendees = [organizer_mailto] if organizer_mailto in recipient_emails else [] else: user_attendees = [recipient_email] else: # Limit the attendees to the one that is actually invited with the current message. attendees = [str(a).split(':')[-1] for a in (itip_event['attendees'] if 'attendees' in itip_event else [])] user_attendees = [a for a in attendees if a in recipient_emails] if 'organizer' in itip_event: sender_email = itip_event['xml'].get_organizer().email() # abort if no attendee matches the envelope recipient if len(user_attendees) == 0: log.info(_("No user attendee matching envelope recipient %s, skip message") % (recipient_email)) return filepath log.debug(_("Receiving user: %r") % (receiving_user), level=8) # set recipient_email to the matching attendee mailto: address recipient_email = user_attendees[0] # change gettext language to the preferredlanguage setting of the receiving user if 'preferredlanguage' in receiving_user: pykolab.translate.setUserLanguage(receiving_user['preferredlanguage']) # find user's kolabInvitationPolicy settings and the matching policy values type_condition = object_type_conditons.get(itip_event['type'], COND_TYPE_ALL) policies = get_matching_invitation_policies(receiving_user, sender_email, type_condition) # select a processing function according to the iTip request method method_processing_map = { 'REQUEST': process_itip_request, 'REPLY': process_itip_reply, 'CANCEL': process_itip_cancel } done = None if itip_event['method'] in method_processing_map: processor_func = method_processing_map[itip_event['method']] # connect as cyrus-admin imap.connect() for policy in policies: log.debug(_("Apply invitation policy %r for sender %r") % (policy_value_map[policy], sender_email), level=8) done = processor_func(itip_event, policy, recipient_email, sender_email, receiving_user) # matching policy found if done is not None: break # remove possible write lock from this iteration remove_write_lock(get_lock_key(receiving_user, itip_event['uid'])) else: log.debug(_("Ignoring '%s' iTip method") % (itip_event['method']), level=8) # message has been processed by the module, remove it if done == MESSAGE_PROCESSED: log.debug(_("iTip message %r consumed by the invitationpolicy module") % (message.get('Message-ID')), level=5) os.unlink(filepath) cleanup() return None # accept message into the destination inbox accept(filepath) def process_itip_request(itip_event, policy, recipient_email, sender_email, receiving_user): """ Process an iTip REQUEST message according to the given policy """ # if invitation policy is set to MANUAL, pass message along if policy & ACT_MANUAL: log.info(_("Pass invitation for manual processing")) return MESSAGE_FORWARD try: receiving_attendee = itip_event['xml'].get_attendee_by_email(recipient_email) log.debug(_("Receiving attendee: %r") % (receiving_attendee.to_dict()), level=8) except Exception as errmsg: log.error("Could not find envelope attendee: %r" % (errmsg)) return MESSAGE_FORWARD # process request to participating attendees with RSVP=TRUE or PARTSTAT=NEEDS-ACTION is_task = itip_event['type'] == 'task' nonpart = receiving_attendee.get_role() == kolabformat.NonParticipant partstat = receiving_attendee.get_participant_status() save_object = not nonpart or not partstat == kolabformat.PartNeedsAction rsvp = receiving_attendee.get_rsvp() scheduling_required = rsvp or partstat == kolabformat.PartNeedsAction respond_with = receiving_attendee.get_participant_status(True) condition_fulfilled = True # find existing event in user's calendar (existing, master) = find_existing_object(itip_event['uid'], itip_event['type'], itip_event['recurrence-id'], receiving_user, True) # compare sequence number to determine a (re-)scheduling request if existing is not None: scheduling_required = itip_event['sequence'] > 0 and itip_event['sequence'] > existing.get_sequence() log.debug(_("Scheduling required: %r, for existing %s: %s") % (scheduling_required, existing.type, existing.get_uid()), level=8) save_object = True # if scheduling: check availability (skip that for tasks) if scheduling_required: if not is_task and policy & (COND_IF_AVAILABLE | COND_IF_CONFLICT): condition_fulfilled = check_availability(itip_event, receiving_user) if not is_task and policy & COND_IF_CONFLICT: condition_fulfilled = not condition_fulfilled log.debug(_("Precondition for object %r fulfilled: %r") % (itip_event['uid'], condition_fulfilled), level=5) if existing: respond_with = None if policy & ACT_ACCEPT and condition_fulfilled: respond_with = 'TENTATIVE' if policy & COND_TENTATIVE else 'ACCEPTED' elif policy & ACT_REJECT and condition_fulfilled: respond_with = 'DECLINED' # TODO: only save declined invitation when a certain config option is set? elif policy & ACT_DELEGATE and condition_fulfilled: # TODO: delegate (but to whom?) return None # auto-update changes if enabled for this user elif policy & ACT_UPDATE and existing: # compare sequence number to avoid outdated updates if not itip_event['sequence'] == existing.get_sequence(): log.info(_("The iTip request sequence (%r) doesn't match the referred object version (%r). Ignoring.") % ( itip_event['sequence'], existing.get_sequence() )) return None log.debug(_("Auto-updating %s %r on iTip REQUEST (no re-scheduling)") % (existing.type, existing.uid), level=8) save_object = True rsvp = False # retain task status and percent-complete properties from my old copy if is_task: itip_event['xml'].set_status(existing.get_status()) itip_event['xml'].set_percentcomplete(existing.get_percentcomplete()) if policy & COND_NOTIFY: sender = itip_event['xml'].get_organizer() comment = itip_event['xml'].get_comment() send_update_notification(itip_event['xml'], receiving_user, existing, False, sender, comment) # if RSVP, send an iTip REPLY if rsvp or scheduling_required: # set attendee's CN from LDAP record if yet missing if not receiving_attendee.get_name() and 'cn' in receiving_user: receiving_attendee.set_name(receiving_user['cn']) # send iTip reply if respond_with is not None and not respond_with == 'NEEDS-ACTION': receiving_attendee.set_participant_status(respond_with) send_reply(recipient_email, itip_event, invitation_response_text(itip_event['type']), subject=_('"%(summary)s" has been %(status)s')) elif policy & ACT_SAVE_TO_FOLDER: # copy the invitation into the user's default folder with PARTSTAT=NEEDS-ACTION itip_event['xml'].set_attendee_participant_status(receiving_attendee, respond_with or 'NEEDS-ACTION') save_object = True else: # policy doesn't match, pass on to next one return None if save_object: targetfolder = None # delete old version from IMAP if existing: targetfolder = existing._imap_folder delete_object(existing) elif master and hasattr(master, '_imap_folder'): targetfolder = master._imap_folder delete_object(master) if not nonpart or existing: # save new copy from iTip if store_object(itip_event['xml'], receiving_user, targetfolder, master): if policy & COND_FORWARD: log.debug(_("Forward invitation for notification"), level=5) return MESSAGE_FORWARD else: return MESSAGE_PROCESSED return None def process_itip_reply(itip_event, policy, recipient_email, sender_email, receiving_user): """ Process an iTip REPLY message according to the given policy """ # if invitation policy is set to MANUAL, pass message along if policy & ACT_MANUAL: log.info(_("Pass reply for manual processing")) return MESSAGE_FORWARD # auto-update is enabled for this user if policy & ACT_UPDATE: try: sender_attendee = itip_event['xml'].get_attendee_by_email(sender_email) log.debug(_("Sender Attendee: %r") % (sender_attendee), level=8) except Exception as errmsg: log.error("Could not find envelope sender attendee: %r" % (errmsg)) return MESSAGE_FORWARD # find existing event in user's calendar # sets/checks lock to avoid concurrent wallace processes trying to update the same event simultaneously (existing, master) = find_existing_object(itip_event['uid'], itip_event['type'], itip_event['recurrence-id'], receiving_user, True) if existing: # compare sequence number to avoid outdated replies? if not itip_event['sequence'] == existing.get_sequence(): log.info(_("The iTip reply sequence (%r) doesn't match the referred object version (%r). Forwarding to Inbox.") % ( itip_event['sequence'], existing.get_sequence() )) remove_write_lock(existing._lock_key) return MESSAGE_FORWARD log.debug(_("Auto-updating %s %r on iTip REPLY") % (existing.type, existing.uid), level=8) updated_attendees = [] try: existing.set_attendee_participant_status(sender_email, sender_attendee.get_participant_status(), rsvp=False) existing_attendee = existing.get_attendee(sender_email) updated_attendees.append(existing_attendee) except Exception as errmsg: log.error("Could not find corresponding attende in organizer's copy: %r" % (errmsg)) # append delegated-from attendee ? if len(sender_attendee.get_delegated_from()) > 0: existing.add_attendee(sender_attendee) updated_attendees.append(sender_attendee) else: # TODO: accept new participant if ACT_ACCEPT ? remove_write_lock(existing._lock_key) return MESSAGE_FORWARD # append delegated-to attendee if len(sender_attendee.get_delegated_to()) > 0: try: delegatee_email = sender_attendee.get_delegated_to(True)[0] sender_delegatee = itip_event['xml'].get_attendee_by_email(delegatee_email) existing_delegatee = existing.find_attendee(delegatee_email) if not existing_delegatee: existing.add_attendee(sender_delegatee) log.debug(_("Add delegatee: %r") % (sender_delegatee.to_dict()), level=8) else: existing_delegatee.copy_from(sender_delegatee) log.debug(_("Update existing delegatee: %r") % (existing_delegatee.to_dict()), level=8) updated_attendees.append(sender_delegatee) # copy all parameters from replying attendee (e.g. delegated-to, role, etc.) existing_attendee.copy_from(sender_attendee) existing.update_attendees([existing_attendee]) log.debug(_("Update delegator: %r") % (existing_attendee.to_dict()), level=8) except Exception as errmsg: log.error("Could not find delegated-to attendee: %r" % (errmsg)) # update the organizer's copy of the object if update_object(existing, receiving_user, master): if policy & COND_NOTIFY: send_update_notification(existing, receiving_user, existing, True, sender_attendee, itip_event['xml'].get_comment()) # update all other attendee's copies if conf.get('wallace','invitationpolicy_autoupdate_other_attendees_on_reply'): propagate_changes_to_attendees_accounts(existing, updated_attendees) return MESSAGE_PROCESSED else: log.error(_("The object referred by this reply was not found in the user's folders. Forwarding to Inbox.")) return MESSAGE_FORWARD return None def process_itip_cancel(itip_event, policy, recipient_email, sender_email, receiving_user): """ Process an iTip CANCEL message according to the given policy """ # if invitation policy is set to MANUAL, pass message along if policy & ACT_MANUAL: log.info(_("Pass cancellation for manual processing")) return MESSAGE_FORWARD # auto-update the local copy if policy & ACT_UPDATE or policy & ACT_CANCEL_DELETE: # find existing object in user's folders (existing, master) = find_existing_object(itip_event['uid'], itip_event['type'], itip_event['recurrence-id'], receiving_user, True) remove_object = policy & ACT_CANCEL_DELETE if existing: # on this-and-future cancel requests, set the recurrence until date on the master event if itip_event['recurrence-id'] and master and itip_event['xml'].get_thisandfuture(): rrule = master.get_recurrence() rrule.set_count(0) rrule.set_until(existing.get_start() + datetime.timedelta(days=-1)) master.set_recurrence(rrule) existing.set_recurrence_id(existing.get_recurrence_id(), True) remove_object = False # delete the local copy if remove_object: # remove exception and register an exdate to the main event if master: log.debug(_("Remove cancelled %s instance %s from %r") % (existing.type, itip_event['recurrence-id'], existing.uid), level=8) master.add_exception_date(existing.get_start()) master.del_exception(existing) success = update_object(master, receiving_user) # delete main event else: success = delete_object(existing) # update the local copy with STATUS=CANCELLED else: log.debug(_("Update cancelled %s %r with STATUS=CANCELLED") % (existing.type, existing.uid), level=8) existing.set_status('CANCELLED') existing.set_transparency(True) success = update_object(existing, receiving_user, master) if success: # send cancellation notification if policy & COND_NOTIFY: sender = itip_event['xml'].get_organizer() comment = itip_event['xml'].get_comment() send_cancel_notification(existing, receiving_user, remove_object, sender, comment) return MESSAGE_PROCESSED else: log.error(_("The object referred by this cancel request was not found in the user's folders. Forwarding to Inbox.")) return MESSAGE_FORWARD return None def user_dn_from_email_address(email_address): """ Resolves the given email address to a Kolab user entity """ global auth if not auth: auth = Auth() auth.connect() # return cached value if email_address in user_dn_from_email_address.cache: return user_dn_from_email_address.cache[email_address] local_domains = auth.list_domains() if local_domains is not None: local_domains = list(set(local_domains.keys())) if not email_address.split('@')[1] in local_domains: user_dn_from_email_address.cache[email_address] = None return None log.debug(_("Checking if email address %r belongs to a local user") % (email_address), level=8) user_dn = auth.find_user_dn(email_address, True) if isinstance(user_dn, string_types): log.debug(_("User DN: %r") % (user_dn), level=8) else: log.debug(_("No user record(s) found for %r") % (email_address), level=8) # remember this lookup user_dn_from_email_address.cache[email_address] = user_dn return user_dn user_dn_from_email_address.cache = {} def get_matching_invitation_policies(receiving_user, sender_email, type_condition=COND_TYPE_ALL): # get user's kolabInvitationPolicy settings policies = receiving_user['kolabinvitationpolicy'] if 'kolabinvitationpolicy' in receiving_user else [] if policies and not isinstance(policies, list): policies = [policies] if len(policies) == 0: policies = conf.get_list('wallace', 'kolab_invitation_policy') # match policies agains the given sender_email matches = [] for p in policies: if ':' in p: (value, domain) = p.split(':', 1) else: value = p domain = '' if domain == '' or domain == '*' or str(sender_email).endswith(domain): value = value.upper() if value in policy_name_map: val = policy_name_map[value] # append if type condition matches if val & type_condition: matches.append(val &~ COND_TYPE_ALL) # add manual as default action if len(matches) == 0: matches.append(ACT_MANUAL) return matches def imap_proxy_auth(user_rec): """ Perform IMAP login using proxy authentication with admin credentials """ global imap mail_attribute = conf.get('cyrus-sasl', 'result_attribute') if mail_attribute is None: mail_attribute = 'mail' mail_attribute = mail_attribute.lower() if mail_attribute not in user_rec: log.error(_("User record doesn't have the mailbox attribute %r set" % (mail_attribute))) return False # do IMAP prox auth with the given user backend = conf.get('kolab', 'imap_backend') admin_login = conf.get(backend, 'admin_login') admin_password = conf.get(backend, 'admin_password') try: imap.disconnect() imap.connect(login=False) imap.login_plain(admin_login, admin_password, user_rec[mail_attribute]) except Exception as errmsg: log.error(_("IMAP proxy authentication failed: %r") % (errmsg)) return False return True def list_user_folders(user_rec, _type): """ Get a list of the given user's private calendar/tasks folders """ global imap # return cached list if '_imap_folders' in user_rec: return user_rec['_imap_folders'] result = [] if not imap_proxy_auth(user_rec): return result folders = imap.get_metadata('*', FOLDER_TYPE_ANNOTATION) log.debug( _("List %r folders for user %r: %r") % ( _type, user_rec['mail'], folders ), level=8 ) (ns_personal, ns_other, ns_shared) = imap.namespaces() _type = _type.encode('utf-8') _folders = {} # Filter the folders by type relevance for folder, metadata in folders.items(): key = ('/shared' + FOLDER_TYPE_ANNOTATION).encode('utf-8') if key in metadata: if metadata[key].startswith(_type): _folders[folder] = metadata key = ('/private' + FOLDER_TYPE_ANNOTATION).encode('utf-8') if key in metadata: if metadata[key].startswith(_type): _folders[folder] = metadata for folder, metadata in _folders.items(): folder_delegated = False # Exclude shared and other user's namespace # # First, test if this is another users folder if ns_other is not None and folder.startswith(ns_other): # If we have no delegated mailboxes, we can skip this entirely if '_delegated_mailboxes' not in user_rec: continue for _m in user_rec['_delegated_mailboxes']: if folder.startswith(ns_other + _m + '/'): folder_delegated = True if not folder_delegated: continue # TODO: list shared folders the user has write privileges ? if ns_shared is not None: if len([_ns for _ns in ns_shared if folder.startswith(_ns)]) > 0: continue key = ('/shared' + FOLDER_TYPE_ANNOTATION).encode('utf-8') if key in metadata: if metadata[key].startswith(_type): result.append(folder) key = ('/private' + FOLDER_TYPE_ANNOTATION).encode('utf-8') if key in metadata: if metadata[key].startswith(_type): result.append(folder) # store default folder in user record if metadata[key].endswith(b'.default'): user_rec['_default_folder'] = folder continue # store private and confidential folders in user record if metadata[key].endswith(b'.confidential'): if '_confidential_folder' not in user_rec: user_rec['_confidential_folder'] = folder continue if metadata[key].endswith(b'.private'): if '_private_folder' not in user_rec: user_rec['_private_folder'] = folder continue # cache with user record user_rec['_imap_folders'] = result return result def find_existing_object(uid, type, recurrence_id, user_rec, lock=False): """ Search user's private folders for the given object (by UID+type) """ global imap lock_key = None if lock: lock_key = get_lock_key(user_rec, uid) set_write_lock(lock_key) event = None master = None for folder in list_user_folders(user_rec, type): log.debug(_("Searching folder %r for %s %r") % (folder, type, uid), level=8) imap.imap.m.select(imap.folder_utf7(folder)) res, data = imap.imap.m.search(None, '(UNDELETED HEADER SUBJECT "%s")' % (uid)) for num in reversed(data[0].split()): res, data = imap.imap.m.fetch(num, '(UID RFC822)') try: msguid = re.search(r"\WUID (\d+)", data[0][0].decode('utf-8')).group(1) except Exception: log.error(_("No UID found in IMAP response: %r") % (data[0][0])) continue try: if type == 'task': - event = todo_from_message(message_from_string(data[0][1])) + event = todo_from_message(message_from_bytes(data[0][1])) else: - event = event_from_message(message_from_string(data[0][1])) + event = event_from_message(message_from_bytes(data[0][1])) # find instance in a recurring series if recurrence_id and (event.is_recurring() or event.has_exceptions() or event.get_recurrence_id()): master = event event = master.get_instance(recurrence_id) setattr(master, '_imap_folder', folder) setattr(master, '_msguid', msguid) # return master, even if instance is not found if not event and master.uid == uid: return (event, master) if event is not None: setattr(event, '_imap_folder', folder) setattr(event, '_lock_key', lock_key) setattr(event, '_msguid', msguid) except Exception: log.error(_("Failed to parse %s from message %s/%s: %s") % (type, folder, num, traceback.format_exc())) event = None master = None continue if event and event.uid == uid: return (event, master) if lock_key is not None: remove_write_lock(lock_key) return (event, master) def check_availability(itip_event, receiving_user): """ For the receiving user, determine if the event in question is in conflict. """ start = time.time() num_messages = 0 conflict = False # return previously detected conflict if '_conflicts' in itip_event: return not itip_event['_conflicts'] for folder in list_user_folders(receiving_user, 'event'): log.debug(_("Listing events from folder %r") % (folder), level=8) imap.imap.m.select(imap.folder_utf7(folder)) res, data = imap.imap.m.search(None, '(UNDELETED HEADER X-Kolab-Type "application/x-vnd.kolab.event")') num_messages += len(data[0].split()) for num in reversed(data[0].split()): event = None res, data = imap.imap.m.fetch(num, '(RFC822)') try: - event = event_from_message(message_from_string(data[0][1])) + event = event_from_message(message_from_bytes(data[0][1])) except Exception as errmsg: log.error(_("Failed to parse event from message %s/%s: %r") % (folder, num, errmsg)) continue if event and event.uid: conflict = check_event_conflict(event, itip_event) if conflict: log.info(_("Existing event %r conflicts with invitation %r") % (event.uid, itip_event['uid'])) break if conflict: break end = time.time() log.debug(_("start: %r, end: %r, total: %r, messages: %d") % (start, end, (end-start), num_messages), level=8) # remember the result of this check for further iterations itip_event['_conflicts'] = conflict return not conflict def set_write_lock(key, wait=True): """ Set a write-lock for the given key and wait if such a lock already exists """ if not os.path.isdir(mybasepath): os.makedirs(mybasepath) if not os.path.isdir(os.path.join(mybasepath, 'locks')): os.makedirs(os.path.join(mybasepath, 'locks')) filename = os.path.join(mybasepath, 'locks', key + '.lock') locktime = 0 if os.path.isfile(filename): locktime = os.path.getmtime(filename) # wait if file lock is in place while time.time() < locktime + 300: if not wait: return False log.debug(_("%r is locked, waiting...") % (key), level=8) time.sleep(0.5) locktime = os.path.getmtime(filename) if os.path.isfile(filename) else 0 # touch the file if os.path.isfile(filename): os.utime(filename, None) else: open(filename, 'w').close() # register active lock write_locks.append(key) return True def remove_write_lock(key, update=True): """ Remove the lock file for the given key """ global write_locks if key is not None: file = os.path.join(mybasepath, 'locks', key + '.lock') if os.path.isfile(file): os.remove(file) if update: write_locks = [k for k in write_locks if not k == key] def get_lock_key(user, uid): return hashlib.md5(("%s/%s" % (user['mail'], uid)).encode('utf-8')).hexdigest() def update_object(object, user_rec, master=None): """ Update the given object in IMAP (i.e. delete + append) """ success = False saveobj = object # updating a single instance only: use master event if object.get_recurrence_id() and master: saveobj = master if hasattr(saveobj, '_imap_folder'): if delete_object(saveobj): saveobj.set_lastmodified() # update last-modified timestamp success = store_object(object, user_rec, saveobj._imap_folder, master) # remove write lock for this event if hasattr(saveobj, '_lock_key') and saveobj._lock_key is not None: remove_write_lock(saveobj._lock_key) return success def store_object(object, user_rec, targetfolder=None, master=None): """ Append the given object to the user's default calendar/tasklist """ # find calendar folder to save object to if not specified if targetfolder is None: targetfolders = list_user_folders(user_rec, object.type) oc = object.get_classification() # use *.confidential/private folder for confidential/private invitations if oc == kolabformat.ClassConfidential and '_confidential_folder' in user_rec: targetfolder = user_rec['_confidential_folder'] elif oc == kolabformat.ClassPrivate and '_private_folder' in user_rec: targetfolder = user_rec['_private_folder'] # use *.default folder if exists elif '_default_folder' in user_rec: targetfolder = user_rec['_default_folder'] # fallback to any existing folder of specified type elif targetfolders is not None and len(targetfolders) > 0: targetfolder = targetfolders[0] if targetfolder is None: log.error(_("Failed to save %s: no target folder found for user %r") % (object.type, user_rec['mail'])) return False saveobj = object # updating a single instance only: add exception to master event if object.get_recurrence_id() and master: object.set_lastmodified() # update last-modified timestamp master.add_exception(object) saveobj = master log.debug(_("Save %s %r to user folder %r") % (saveobj.type, saveobj.uid, targetfolder), level=8) try: imap.imap.m.select(imap.folder_utf7(targetfolder)) result = imap.imap.m.append( imap.folder_utf7(targetfolder), None, None, saveobj.to_message(creator="Kolab Server ").as_string().encode('utf-8') ) return result except Exception as errmsg: log.error(_("Failed to save %s to user folder at %r: %r") % ( saveobj.type, targetfolder, errmsg )) return False def delete_object(existing): """ Removes the IMAP object with the given UID from a user's folder """ targetfolder = existing._imap_folder msguid = existing._msguid if hasattr(existing, '_msguid') else None try: imap.imap.m.select(imap.folder_utf7(targetfolder)) # delete by IMAP UID if msguid is not None: log.debug(_("Delete %s %r in %r by UID: %r") % ( existing.type, existing.uid, targetfolder, msguid ), level=8) imap.imap.m.uid('store', msguid, '+FLAGS', '(\\Deleted)') else: res, data = imap.imap.m.search(None, '(HEADER SUBJECT "%s")' % existing.uid) log.debug(_("Delete %s %r in %r: %r") % ( existing.type, existing.uid, targetfolder, data ), level=8) for num in data[0].split(): imap.imap.m.store(num, '+FLAGS', '(\\Deleted)') imap.imap.m.expunge() return True except Exception as errmsg: log.error(_("Failed to delete %s from folder %r: %r") % ( existing.type, targetfolder, errmsg )) return False def send_update_notification(object, receiving_user, old=None, reply=True, sender=None, comment=None): """ Send a (consolidated) notification about the current participant status to organizer """ global auth if sys.version_info.major >= 3: from email.mime.text import MIMEText from email.utils import formatdate from email.header import Header from email import charset else: from email.MIMEText import MIMEText from email.Utils import formatdate from email.header import Header from email import charset # encode unicode strings with quoted-printable charset.add_charset('utf-8', charset.SHORTEST, charset.QP) organizer = object.get_organizer() orgemail = organizer.email() orgname = organizer.name() itip_comment = None if comment is not None: comment = comment.strip() if sender is not None and not comment == '': itip_comment = _("%s commented: %s") % (_attendee_name(sender), comment) if reply: log.debug(_("Compose participation status summary for %s %r to user %r") % ( object.type, object.uid, receiving_user['mail'] ), level=8) auto_replies_expected = 0 auto_replies_received = 0 is_manual_reply = True partstats = {'ACCEPTED': [], 'TENTATIVE': [], 'DECLINED': [], 'DELEGATED': [], 'IN-PROCESS': [], 'COMPLETED': [], 'PENDING': []} for attendee in object.get_attendees(): parstat = attendee.get_participant_status(True) if parstat in partstats: partstats[parstat].append(attendee.get_displayname()) else: partstats['PENDING'].append(attendee.get_displayname()) # look-up kolabinvitationpolicy for this attendee if attendee.get_cutype() == kolabformat.CutypeResource: resource_dns = auth.find_resource(attendee.get_email()) if isinstance(resource_dns, list): attendee_dn = resource_dns[0] if len(resource_dns) > 0 else None else: attendee_dn = resource_dns else: attendee_dn = user_dn_from_email_address(attendee.get_email()) if attendee_dn: attendee_rec = auth.get_entry_attributes(None, attendee_dn, ['kolabinvitationpolicy']) if is_auto_reply(attendee_rec, orgemail, object.type): auto_replies_expected += 1 if not parstat == 'NEEDS-ACTION': auto_replies_received += 1 if sender is not None and sender.get_email() == attendee.get_email(): is_manual_reply = False # skip notification until we got replies from all automatically responding attendees if not is_manual_reply and auto_replies_received < auto_replies_expected: log.debug(_("Waiting for more automated replies (got %d of %d); skipping notification") % ( auto_replies_received, auto_replies_expected ), level=8) return # build notification message body roundup = '' if itip_comment is not None: roundup += "\n" + itip_comment for status,attendees in partstats.items(): if len(attendees) > 0: roundup += "\n" + participant_status_label(status) + ":\n\t" + "\n\t".join(attendees) + "\n" else: # build notification message body roundup = '' if itip_comment is not None: roundup += "\n" + itip_comment roundup += "\n" + _("Changes submitted by %s have been automatically applied.") % (orgname if orgname else orgemail) # list properties changed from previous version if old: diff = xmlutils.compute_diff(old.to_dict(), object.to_dict()) if len(diff) > 1: roundup += "\n" for change in diff: if not change['property'] in ['created','lastmodified-date','sequence']: new_value = xmlutils.property_to_string(change['property'], change['new']) if change['new'] else _("(removed)") if new_value: roundup += "\n- %s: %s" % (xmlutils.property_label(change['property']), new_value) # compose different notification texts for events/tasks if object.type == 'task': message_text = _(""" The assignment for '%(summary)s' has been updated in your tasklist. %(roundup)s """) % { 'summary': object.get_summary(), 'roundup': roundup } else: message_text = _(""" The event '%(summary)s' at %(start)s has been updated in your calendar. %(roundup)s """) % { 'summary': object.get_summary(), 'start': xmlutils.property_to_string('start', object.get_start()), 'roundup': roundup } if object.get_recurrence_id(): message_text += _("NOTE: This update only refers to this single occurrence!") + "\n" message_text += "\n" + _("*** This is an automated message. Please do not reply. ***") # compose mime message msg = MIMEText(utils.stripped_message(message_text), _charset='utf-8') msg['To'] = receiving_user['mail'] msg['Date'] = formatdate(localtime=True) msg['Subject'] = utils.str2unicode(_('"%s" has been updated') % (object.get_summary())) msg['From'] = Header(utils.str2unicode('%s' % orgname) if orgname else '') msg['From'].append("<%s>" % orgemail) seed = random.randint(0, 6) alarm_after = (seed * 10) + 60 log.debug(_("Set alarm to %s seconds") % (alarm_after), level=8) signal.alarm(alarm_after) result = modules._sendmail(orgemail, receiving_user['mail'], msg.as_string()) log.debug(_("Sent update notification to %r: %r") % (receiving_user['mail'], result), level=8) signal.alarm(0) def send_cancel_notification(object, receiving_user, deleted=False, sender=None, comment=None): """ Send a notification about event/task cancellation """ if sys.version_info.major >= 3: from email.mime.text import MIMEText from email.utils import formatdate from email.header import Header from email import charset else: from email.MIMEText import MIMEText from email.Utils import formatdate from email.header import Header from email import charset # encode unicode strings with quoted-printable charset.add_charset('utf-8', charset.SHORTEST, charset.QP) log.debug(_("Send cancellation notification for %s %r to user %r") % ( object.type, object.uid, receiving_user['mail'] ), level=8) organizer = object.get_organizer() orgemail = organizer.email() orgname = organizer.name() # compose different notification texts for events/tasks if object.type == 'task': message_text = _("The assignment for '%(summary)s' has been cancelled by %(organizer)s.") % { 'summary': object.get_summary(), 'organizer': orgname if orgname else orgemail } if deleted: message_text += " " + _("The copy in your tasklist has been removed accordingly.") else: message_text += " " + _("The copy in your tasklist has been marked as cancelled accordingly.") else: message_text = _("The event '%(summary)s' at %(start)s has been cancelled by %(organizer)s.") % { 'summary': object.get_summary(), 'start': xmlutils.property_to_string('start', object.get_start()), 'organizer': orgname if orgname else orgemail } if deleted: message_text += " " + _("The copy in your calendar has been removed accordingly.") else: message_text += " " + _("The copy in your calendar has been marked as cancelled accordingly.") if comment is not None: comment = comment.strip() if sender is not None and not comment == '': message_text += "\n" + _("%s commented: %s") % (_attendee_name(sender), comment) if object.get_recurrence_id(): message_text += "\n" + _("NOTE: This cancellation only refers to this single occurrence!") message_text += "\n\n" + _("*** This is an automated message. Please do not reply. ***") # compose mime message msg = MIMEText(utils.stripped_message(message_text), _charset='utf-8') msg['To'] = receiving_user['mail'] msg['Date'] = formatdate(localtime=True) msg['Subject'] = utils.str2unicode(_('"%s" has been cancelled') % (object.get_summary())) msg['From'] = Header(utils.str2unicode('%s' % orgname) if orgname else '') msg['From'].append("<%s>" % orgemail) seed = random.randint(0, 6) alarm_after = (seed * 10) + 60 log.debug(_("Set alarm to %s seconds") % (alarm_after), level=8) signal.alarm(alarm_after) result = modules._sendmail(orgemail, receiving_user['mail'], msg.as_string()) log.debug(_("Sent cancel notification to %r: %r") % (receiving_user['mail'], result), level=8) signal.alarm(0) def is_auto_reply(user, sender_email, type): accept_available = False accept_conflicts = False for policy in get_matching_invitation_policies(user, sender_email, object_type_conditons.get(type, COND_TYPE_EVENT)): if policy & (ACT_ACCEPT | ACT_REJECT | ACT_DELEGATE): if check_policy_condition(policy, True): accept_available = True if check_policy_condition(policy, False): accept_conflicts = True # we have both cases covered by a policy if accept_available and accept_conflicts: return True # manual action reached if policy & (ACT_MANUAL | ACT_SAVE_TO_FOLDER): return False return False def check_policy_condition(policy, available): condition_fulfilled = True if policy & (COND_IF_AVAILABLE | COND_IF_CONFLICT): condition_fulfilled = available if policy & COND_IF_CONFLICT: condition_fulfilled = not condition_fulfilled return condition_fulfilled def propagate_changes_to_attendees_accounts(object, updated_attendees=None): """ Find and update copies of this object in all attendee's personal folders """ recurrence_id = object.get_recurrence_id() for attendee in object.get_attendees(): attendee_user_dn = user_dn_from_email_address(attendee.get_email()) if attendee_user_dn: attendee_user = auth.get_entry_attributes(None, attendee_user_dn, ['*']) (attendee_object, master_object) = find_existing_object(object.uid, object.type, recurrence_id, attendee_user, True) # does IMAP authenticate if attendee_object: # find attendee's entry by one of its email addresses attendee_emails = auth.extract_recipient_addresses(attendee_user) for attendee_email in attendee_emails: try: attendee_entry = attendee_object.get_attendee_by_email(attendee_email) except: attendee_entry = None if attendee_entry: break # copy all attendees from master object (covers additions and removals) new_attendees = [] for a in object.get_attendees(): # keep my own entry intact if attendee_entry is not None and attendee_entry.get_email() == a.get_email(): new_attendees.append(attendee_entry) else: new_attendees.append(a) attendee_object.set_attendees(new_attendees) if updated_attendees and not recurrence_id: log.debug("Update Attendees %r for %s" % ([a.get_email()+':'+a.get_participant_status(True) for a in updated_attendees], attendee_user['mail']), level=8) attendee_object.update_attendees(updated_attendees, False) success = update_object(attendee_object, attendee_user, master_object) log.debug(_("Updated %s's copy of %r: %r") % (attendee_user['mail'], object.uid, success), level=8) else: log.debug(_("Attendee %s's copy of %r not found") % (attendee_user['mail'], object.uid), level=8) else: log.debug(_("Attendee %r not found in LDAP") % (attendee.get_email()), level=8) def invitation_response_text(type): footer = "\n\n" + _("*** This is an automated message. Please do not reply. ***") if type == 'task': return _("%(name)s has %(status)s your assignment for %(summary)s.") + footer else: return _("%(name)s has %(status)s your invitation for %(summary)s.") + footer def _attendee_name(attendee): # attendee here can be Attendee or ContactReference try: name = attendee.get_name() except Exception: name = attendee.name() if name == '': try: name = attendee.get_email() except Exception: name = attendee.email() return name diff --git a/wallace/module_resources.py b/wallace/module_resources.py index 0cef10c..4304137 100644 --- a/wallace/module_resources.py +++ b/wallace/module_resources.py @@ -1,1811 +1,1811 @@ # -*- coding: utf-8 -*- # pylint: disable=too-many-lines # Copyright 2010-2015 Kolab Systems AG (http://www.kolabsys.com) # # Jeroen van Meeuwen (Kolab Systems) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # import base64 import datetime import sys -from email import message_from_string +from email import message_from_bytes from email.parser import Parser from email.utils import formataddr from email.utils import getaddresses import os import random import re import signal from six import string_types import time import uuid from dateutil.tz import tzlocal from . import modules import kolabformat import pykolab from pykolab.auth import Auth from pykolab.conf import Conf from pykolab.imap import IMAP from pykolab.logger import LoggerAdapter from pykolab.itip import events_from_message from pykolab.itip import check_event_conflict from pykolab.translate import _ from pykolab.xml import to_dt from pykolab.xml import utils as xmlutils from pykolab.xml import event_from_message from pykolab.xml import participant_status_label # define some contstants used in the code below COND_NOTIFY = 256 ACT_MANUAL = 1 ACT_ACCEPT = 2 ACT_REJECT = 8 ACT_STORE = 16 ACT_ACCEPT_AND_NOTIFY = ACT_ACCEPT + COND_NOTIFY ACT_STORE_AND_NOTIFY = ACT_STORE + COND_NOTIFY # noqa: E241 policy_name_map = { 'ACT_MANUAL': ACT_MANUAL, # noqa: E241 'ACT_ACCEPT': ACT_ACCEPT, # noqa: E241 'ACT_REJECT': ACT_REJECT, # noqa: E241 'ACT_ACCEPT_AND_NOTIFY': ACT_ACCEPT_AND_NOTIFY, 'ACT_STORE_AND_NOTIFY': ACT_STORE_AND_NOTIFY } # pylint: disable=invalid-name log = pykolab.getLogger('pykolab.wallace/resources') extra_log_params = {'qid': '-'} log = LoggerAdapter(log, extra_log_params) conf = pykolab.getConf() mybasepath = '/var/spool/pykolab/wallace/resources/' auth = None imap = None def __init__(): modules.register('resources', execute, description=description(), heartbeat=heartbeat) def accept(filepath): new_filepath = os.path.join( mybasepath, 'ACCEPT', os.path.basename(filepath) ) cleanup() os.rename(filepath, new_filepath) filepath = new_filepath exec('modules.cb_action_ACCEPT(%r, %r)' % ('resources', filepath)) def description(): return """Resource management module.""" def cleanup(): global auth, imap, extra_log_params log.debug("cleanup(): %r, %r" % (auth, imap), level=8) extra_log_params['qid'] = '-' auth.disconnect() del auth # Disconnect IMAP or we lock the mailbox almost constantly imap.disconnect() del imap # pylint: disable=inconsistent-return-statements # pylint: disable=too-many-branches # pylint: disable=too-many-locals # pylint: disable=too-many-return-statements # pylint: disable=too-many-statements def execute(*args, **kw): global auth, imap, extra_log_params # TODO: Test for correct call. filepath = args[0] extra_log_params['qid'] = os.path.basename(filepath) # (re)set language to default pykolab.translate.setUserLanguage(conf.get('kolab', 'default_locale')) if not os.path.isdir(mybasepath): os.makedirs(mybasepath) for stage in ['incoming', 'ACCEPT', 'REJECT', 'HOLD', 'DEFER']: if not os.path.isdir(os.path.join(mybasepath, stage)): os.makedirs(os.path.join(mybasepath, stage)) log.debug(_("Resource Management called for %r, %r") % (args, kw), level=8) auth = Auth() imap = IMAP() if 'stage' in kw: log.debug( _("Issuing callback after processing to stage %s") % ( kw['stage'] ), level=8 ) log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8) if hasattr(modules, 'cb_action_%s' % (kw['stage'])): log.debug( _("Attempting to execute cb_action_%s()") % (kw['stage']), level=8 ) exec( 'modules.cb_action_%s(%r, %r)' % ( kw['stage'], 'resources', filepath ) ) return filepath else: # Move to incoming new_filepath = os.path.join( mybasepath, 'incoming', os.path.basename(filepath) ) if not filepath == new_filepath: log.debug("Renaming %r to %r" % (filepath, new_filepath)) os.rename(filepath, new_filepath) filepath = new_filepath # parse full message message = Parser().parse(open(filepath, 'r')) # invalid message, skip if not message.get('X-Kolab-To'): return filepath recipients = [address for displayname, address in getaddresses(message.get_all('X-Kolab-To'))] sender_email = [ address for displayname, address in getaddresses(message.get_all('X-Kolab-From')) ][0] any_itips = False any_resources = False possibly_any_resources = False reference_uid = None # An iTip message may contain multiple events. Later on, test if the message # is an iTip message by checking the length of this list. try: itip_events = events_from_message(message, ['REQUEST', 'REPLY', 'CANCEL']) # pylint: disable=broad-except except Exception as errmsg: log.error(_("Failed to parse iTip events from message: %r" % (errmsg))) itip_events = [] if not len(itip_events) > 0: log.info("Message is not an iTip message or does not contain any (valid) iTip.") else: any_itips = True log.debug( "iTip events attached to this message contain the following information: %r" % ( itip_events ), level=8 ) if any_itips: # See if any iTip actually allocates a resource. if (len([x['resources'] for x in itip_events if 'resources' in x]) > 0 or len([x['attendees'] for x in itip_events if 'attendees' in x]) > 0): possibly_any_resources = True if possibly_any_resources: auth.connect() for recipient in recipients: # extract reference UID from recipients like resource+UID@domain.org if re.match(r'.+\+[A-Za-z0-9=/-]+@', recipient): try: (prefix, host) = recipient.split('@') (local, uid) = prefix.split('+') reference_uid = base64.b64decode(uid, '-/') recipient = local + '@' + host # pylint: disable=broad-except except Exception: continue if not len(resource_record_from_email_address(recipient)) == 0: resource_recipient = recipient any_resources = True if any_resources: if not any_itips: log.debug( _("Not an iTip message, but sent to resource nonetheless. Reject message"), level=5 ) reject(filepath) return False else: # Continue. Resources and iTips. We like. pass else: if not any_itips: log.debug(_("No itips, no resources, pass along %r") % (filepath), level=5) return filepath else: log.debug(_("iTips, but no resources, pass along %r") % (filepath), level=5) return filepath # A simple list of merely resource entry IDs that hold any relevance to the # iTip events resource_dns = resource_records_from_itip_events(itip_events, resource_recipient) # check if resource attendees match the envelope recipient if len(resource_dns) == 0: log.info( _("No resource attendees matching envelope recipient %s, Reject message") % ( resource_recipient ) ) log.debug("%r" % (itip_events), level=8) reject(filepath) return False # Get the resource details, which includes details on the IMAP folder # This may append resource collection members to recource_dns resources = get_resource_records(resource_dns) log.debug(_("Resources: %r; %r") % (resource_dns, resources), level=8) imap.connect() done = False receiving_resource = resources[resource_dns[0]] for itip_event in itip_events: if itip_event['method'] == 'REPLY': done = True # find initial reservation referenced by the reply if reference_uid: (event, master) = find_existing_event( reference_uid, itip_event['recurrence-id'], receiving_resource ) log.debug( _("iTip REPLY to %r, %r; matches %r") % ( reference_uid, itip_event['recurrence-id'], type(event) ), level=8 ) if event: try: sender_attendee = itip_event['xml'].get_attendee_by_email(sender_email) owner_reply = sender_attendee.get_participant_status() log.debug( _("Sender Attendee: %r => %r") % (sender_attendee, owner_reply), level=8 ) # pylint: disable=broad-except except Exception as errmsg: log.error(_("Could not find envelope sender attendee: %r") % (errmsg)) continue # compare sequence number to avoid outdated replies if not itip_event['sequence'] == event.get_sequence(): log.info( _( "The iTip reply sequence (%r) doesn't match the " + "referred event version (%r). Ignoring." ) % ( itip_event['sequence'], event.get_sequence() ) ) continue # forward owner response comment comment = itip_event['xml'].get_comment() if comment: event.set_comment(str(comment)) _itip_event = dict(xml=event, uid=event.get_uid(), _master=master) _itip_event['recurrence-id'] = event.get_recurrence_id() if owner_reply == kolabformat.PartAccepted: event.set_status(kolabformat.StatusConfirmed) accept_reservation_request(_itip_event, receiving_resource, confirmed=True) elif owner_reply == kolabformat.PartDeclined: decline_reservation_request(_itip_event, receiving_resource) else: log.info( _( "Invalid response (%r) received from resource owner for event %r" ) % ( sender_attendee.get_participant_status(True), reference_uid ) ) else: log.info( _("Event referenced by this REPLY (%r) not found in resource calendar") % ( reference_uid ) ) else: log.info(_("No event reference found in this REPLY. Ignoring.")) # exit for-loop break # else: try: receiving_attendee = itip_event['xml'].get_attendee_by_email( receiving_resource['mail'] ) log.debug( _("Receiving Resource: %r; %r") % (receiving_resource, receiving_attendee), level=8 ) # pylint: disable=broad-except except Exception as errmsg: log.error(_("Could not find envelope attendee: %r") % (errmsg)) continue # ignore updates and cancellations to resource collections who already delegated the event att_delegated = (len(receiving_attendee.get_delegated_to()) > 0) att_nonpart = (receiving_attendee.get_role() == kolabformat.NonParticipant) att_rsvp = receiving_attendee.get_rsvp() if (att_delegated or att_nonpart) and not att_rsvp: done = True log.debug( _("Recipient %r is non-participant, ignoring message") % ( receiving_resource['mail'] ), level=8 ) # process CANCEL messages if not done and itip_event['method'] == "CANCEL": for resource in resource_dns: r_emails = [a.get_email() for a in itip_event['xml'].get_attendees()] _resource = resources[resource] if _resource['mail'] in r_emails and 'kolabtargetfolder' in _resource: (event, master) = find_existing_event( itip_event['uid'], itip_event['recurrence-id'], _resource ) if not event: continue # remove entire event if master is None: log.debug( _("Cancellation for entire event %r: deleting") % (itip_event['uid']), level=8 ) delete_resource_event( itip_event['uid'], resources[resource], event._msguid ) # just cancel one single occurrence: add exception with status=cancelled else: log.debug( _("Cancellation for a single occurrence %r of %r: updating...") % ( itip_event['recurrence-id'], itip_event['uid'] ), level=8 ) event.set_status('CANCELLED') event.set_transparency(True) _itip_event = dict(xml=event, uid=event.get_uid(), _master=master) _itip_event['recurrence-id'] = event.get_recurrence_id() save_resource_event(_itip_event, resources[resource]) done = True if done: os.unlink(filepath) cleanup() return # do the magic for the receiving attendee (available_resource, itip_event) = check_availability( itip_events, resource_dns, resources, receiving_attendee ) _reject = False resource = None original_resource = None # accept reservation if available_resource is not None: atts = [a.get_email() for a in itip_event['xml'].get_attendees()] if available_resource['mail'] in atts: # check if reservation was delegated if available_resource['mail'] != receiving_resource['mail']: if receiving_attendee.get_participant_status() == kolabformat.PartDelegated: original_resource = receiving_resource resource = available_resource else: # This must have been a resource collection originally. # We have inserted the reference to the original resource # record in 'memberof'. if 'memberof' in available_resource: original_resource = resources[available_resource['memberof']] atts = [a.get_email() for a in itip_event['xml'].get_attendees()] if original_resource['mail'] in atts: # # Delegate: # - delegator: the original resource collection # - delegatee: the target resource # itip_event['xml'].delegate( original_resource['mail'], available_resource['mail'], available_resource['cn'] ) # set delegator to NON-PARTICIPANT and RSVP=FALSE delegator = itip_event['xml'].get_attendee_by_email(original_resource['mail']) delegator.set_role(kolabformat.NonParticipant) delegator.set_rsvp(False) log.debug( _("Delegate invitation for resource collection %r to %r") % ( original_resource['mail'], available_resource['mail'] ), level=8 ) resource = available_resource # Look for ACT_REJECT policy if resource is not None: invitationpolicy = get_resource_invitationpolicy(resource) log.debug(_("Apply invitation policies %r") % (invitationpolicy), level=8) if invitationpolicy is not None: for policy in invitationpolicy: if policy & ACT_REJECT: _reject = True break if resource is not None and not _reject: log.debug( _("Accept invitation for individual resource %r / %r") % ( resource['dn'], resource['mail'] ), level=8 ) accept_reservation_request( itip_event, resource, original_resource, False, invitationpolicy ) else: resource = resources[resource_dns[0]] # this is the receiving resource record log.debug( _("Decline invitation for individual resource %r / %r") % ( resource['dn'], resource['mail'] ), level=8 ) decline_reservation_request(itip_event, resource) cleanup() os.unlink(filepath) def heartbeat(lastrun): global imap # run archival job every hour only now = int(time.time()) if lastrun == 0 or now - heartbeat._lastrun < 3600: return log.debug(_("module_resources.heartbeat(%d)") % (heartbeat._lastrun), level=8) # get a list of resource records from LDAP auth = Auth() auth.connect() resource_dns = auth.find_resource('*') # Remove referrals resource_dns = [dn for dn in resource_dns if dn is not None] # filter by resource_base_dn resource_base_dn = conf.get('ldap', 'resource_base_dn', None) if resource_base_dn is not None: resource_dns = [dn for dn in resource_dns if resource_base_dn in dn] if len(resource_dns) > 0: imap = IMAP() imap.connect() for resource_dn in resource_dns: resource_attrs = auth.get_entry_attributes(None, resource_dn, ['kolabtargetfolder']) if 'kolabtargetfolder' in resource_attrs: try: expunge_resource_calendar(resource_attrs['kolabtargetfolder']) # pylint: disable=broad-except except Exception as errmsg: log.error( _("Expunge resource calendar for %s (%s) failed: %r") % ( resource_dn, resource_attrs['kolabtargetfolder'], errmsg ) ) imap.disconnect() auth.disconnect() heartbeat._lastrun = now heartbeat._lastrun = 0 def expunge_resource_calendar(mailbox): """ Cleanup routine to remove events older than 100 days from the given resource calendar """ global imap days = int(conf.get('wallace', 'resource_calendar_expire_days')) now = datetime.datetime.now(tzlocal()) expire_date = now - datetime.timedelta(days=days) log.debug( _("Expunge events in resource folder %r older than %d days") % (mailbox, days), level=8 ) # might raise an exception, let that bubble targetfolder = imap.folder_quote(mailbox) imap.set_acl( targetfolder, conf.get(conf.get('kolab', 'imap_backend'), 'admin_login'), "lrswipkxtecda" ) imap.imap.m.select(targetfolder) typ, data = imap.imap.m.search(None, 'UNDELETED') for num in data[0].split(): log.debug( _("Fetching message ID %r from folder %r") % (num, mailbox), level=8 ) typ, data = imap.imap.m.fetch(num, '(RFC822)') try: - event = event_from_message(message_from_string(data[0][1])) + event = event_from_message(message_from_bytes(data[0][1])) # pylint: disable=broad-except except Exception as errmsg: log.error(_("Failed to parse event from message %s/%s: %r") % (mailbox, num, errmsg)) continue if event: dt_end = to_dt(event.get_end()) # consider recurring events and get real end date if event.is_recurring(): dt_end = to_dt(event.get_last_occurrence()) if dt_end is None: # skip if recurring forever continue if dt_end and dt_end < expire_date: age = now - dt_end log.debug( _("Flag event %s from message %s/%s as deleted (age = %d days)") % ( event.uid, mailbox, num, age.days ), level=8 ) imap.imap.m.store(num, '+FLAGS', '\\Deleted') imap.imap.m.expunge() def check_availability(itip_events, resource_dns, resources, receiving_attendee=None): """ For each resource, determine if any of the events in question are in conflict. """ # Store the (first) conflicting event(s) alongside the resource information. start = time.time() num_messages = 0 available_resource = None for resource in resources: # skip this for resource collections if 'kolabtargetfolder' not in resources[resource]: continue # sets the 'conflicting' flag and adds a list of conflicting events found try: num_messages += read_resource_calendar(resources[resource], itip_events) # pylint: disable=broad-except except Exception as e: log.error(_("Failed to read resource calendar for %r: %r") % (resource, e)) end = time.time() log.debug( _("start: %r, end: %r, total: %r, messages: %d") % ( start, end, (end - start), num_messages ), level=8 ) # For each resource (collections are first!) # check conflicts and either accept or decline the reservation request for resource in resource_dns: log.debug(_("Polling for resource %r") % (resource), level=8) if resource not in resources: log.debug(_("Resource %r has been popped from the list") % (resource), level=8) continue if 'conflicting_events' not in resources[resource]: log.debug(_("Resource is a collection"), level=8) # check if there are non-conflicting collection members conflicting_members = [ x for x in resources[resource]['uniquemember'] if resources[x]['conflict'] ] # found at least one non-conflicting member, remove the conflicting ones and continue if len(conflicting_members) < len(resources[resource]['uniquemember']): for member in conflicting_members: resources[resource]['uniquemember'] = [ x for x in resources[resource]['uniquemember'] if x != member ] del resources[member] log.debug(_("Removed conflicting resources from %r: (%r) => %r") % ( resource, conflicting_members, resources[resource]['uniquemember'] ), level=8) else: # TODO: shuffle existing bookings of collection members in order # to make one available for the requested time pass continue if len(resources[resource]['conflicting_events']) > 0: log.debug( _("Conflicting events: %r for resource %r") % ( resources[resource]['conflicting_events'], resource ), level=8 ) done = False # This is the event being conflicted with! for itip_event in itip_events: # do not re-assign single occurrences to another resource if itip_event['recurrence-id'] is not None: continue _eas = [a.get_email() for a in itip_event['xml'].get_attendees()] # Now we have the event that was conflicting if resources[resource]['mail'] in _eas: # this resource initially was delegated from a collection ? if receiving_attendee \ and receiving_attendee.get_email() == resources[resource]['mail'] \ and len(receiving_attendee.get_delegated_from()) > 0: for delegator in receiving_attendee.get_delegated_from(): collection_data = get_resource_collection(delegator.email()) if collection_data is not None: # check if another collection member is available (available_resource, dummy) = check_availability( itip_events, collection_data[0], collection_data[1] ) break if available_resource is not None: log.debug( _("Delegate to another resource collection member: %r to %r") % ( resources[resource]['mail'], available_resource['mail'] ), level=8 ) # set this new resource as delegate for the receiving_attendee itip_event['xml'].delegate( resources[resource]['mail'], available_resource['mail'], available_resource['cn'] ) # set delegator to NON-PARTICIPANT and RSVP=FALSE receiving_attendee.set_role(kolabformat.NonParticipant) receiving_attendee.set_rsvp(False) receiving_attendee.setDelegatedFrom([]) # remove existing_events as we now delegated back to the collection if len(resources[resource]['existing_events']) > 0: for existing in resources[resource]['existing_events']: delete_resource_event( existing.uid, resources[resource], existing._msguid ) done = True if done: break else: # No conflicts, go accept for itip_event in itip_events: # directly invited resource _eas = [a.get_email() for a in itip_event['xml'].get_attendees()] if resources[resource]['mail'] in _eas: available_resource = resources[resource] done = True else: # This must have been a resource collection originally. # We have inserted the reference to the original resource # record in 'memberof'. if 'memberof' in resources[resource]: original_resource = resources[resources[resource]['memberof']] # Randomly select a target resource from the resource collection. _selected = random.randint(0, (len(original_resource['uniquemember']) - 1)) available_resource = resources[original_resource['uniquemember'][_selected]] done = True if done: break # end for resource in resource_dns: return (available_resource, itip_event) def read_resource_calendar(resource_rec, itip_events): """ Read all booked events from the given resource's calendar and check for conflicts with the given list if itip events """ global imap resource_rec['conflict'] = False resource_rec['conflicting_events'] = [] resource_rec['existing_events'] = [] mailbox = resource_rec['kolabtargetfolder'] log.debug( _("Checking events in resource folder %r") % (mailbox), level=8 ) # set read ACLs for admin user imap.set_acl(mailbox, conf.get(conf.get('kolab', 'imap_backend'), 'admin_login'), "lrs") # might raise an exception, let that bubble imap.imap.m.select(imap.folder_quote(mailbox)) typ, data = imap.imap.m.search(None, 'UNDELETED') num_messages = len(data[0].split()) for num in data[0].split(): # For efficiency, makes the routine non-deterministic if resource_rec['conflict']: continue log.debug( _("Fetching message UID %r from folder %r") % (num, mailbox), level=8 ) typ, data = imap.imap.m.fetch(num, '(UID RFC822)') try: msguid = re.search(r"\WUID (\d+)", data[0][0].decode('utf-8')).group(1) # pylint: disable=broad-except except Exception: log.error(_("No UID found in IMAP response: %r") % (data[0][0])) continue try: - event = event_from_message(message_from_string(data[0][1])) + event = event_from_message(message_from_bytes(data[0][1])) # pylint: disable=broad-except except Exception as e: log.error(_("Failed to parse event from message %s/%s: %r") % (mailbox, num, e)) continue if event: for itip in itip_events: conflict = check_event_conflict(event, itip) if event.get_uid() == itip['uid']: setattr(event, '_msguid', msguid) if event.is_recurring() or itip['recurrence-id']: resource_rec['existing_master'] = event else: resource_rec['existing_events'].append(event) if conflict: log.info( _("Event %r conflicts with event %r") % ( itip['xml'].get_uid(), event.get_uid() ) ) resource_rec['conflicting_events'].append(event.get_uid()) resource_rec['conflict'] = True return num_messages def find_existing_event(uid, recurrence_id, resource_rec): """ Search the resources's calendar folder for the given event (by UID) """ global imap event = None master = None mailbox = resource_rec['kolabtargetfolder'] log.debug(_("Searching %r for event %r") % (mailbox, uid), level=8) try: imap.imap.m.select(imap.folder_quote(mailbox)) typ, data = imap.imap.m.search(None, '(UNDELETED HEADER SUBJECT "%s")' % (uid)) # pylint: disable=broad-except except Exception as errmsg: log.error(_("Failed to access resource calendar:: %r") % (errmsg)) return event for num in reversed(data[0].split()): typ, data = imap.imap.m.fetch(num, '(UID RFC822)') try: msguid = re.search(r"\WUID (\d+)", data[0][0].decode('utf-8')).group(1) # pylint: disable=broad-except except Exception: log.error(_("No UID found in IMAP response: %r") % (data[0][0])) continue try: - event = event_from_message(message_from_string(data[0][1])) + event = event_from_message(message_from_bytes(data[0][1])) # find instance in a recurring series if recurrence_id and (event.is_recurring() or event.has_exceptions()): master = event event = master.get_instance(recurrence_id) setattr(master, '_msguid', msguid) # return master, even if instance is not found if not event and master.uid == uid: return (event, master) # compare recurrence-id and skip to next message if not matching elif recurrence_id: if not xmlutils.dates_equal(recurrence_id, event.get_recurrence_id()): log.debug( _("Recurrence-ID not matching on message %s, skipping: %r != %r") % ( msguid, recurrence_id, event.get_recurrence_id() ), level=8 ) continue if event is not None: setattr(event, '_msguid', msguid) # pylint: disable=broad-except except Exception as errmsg: log.error(_("Failed to parse event from message %s/%s: %r") % (mailbox, num, errmsg)) event = None master = None continue if event and event.uid == uid: return (event, master) return (event, master) def accept_reservation_request( itip_event, resource, delegator=None, confirmed=False, invitationpolicy=None ): """ Accepts the given iTip event by booking it into the resource's calendar. Then, depending on the policy, set the attendee status of the given resource to ACCEPTED/TENTATIVE and send an iTip reply message to the organizer, or set the status to NEEDS-ACTION and don't send a reply to the organizer. """ owner = get_resource_owner(resource) confirmation_required = False do_send_response = True partstat = 'ACCEPTED' if not confirmed and owner: if invitationpolicy is None: invitationpolicy = get_resource_invitationpolicy(resource) log.debug(_("Apply invitation policies %r") % (invitationpolicy), level=8) if invitationpolicy is not None: for policy in invitationpolicy: if policy & ACT_MANUAL and owner['mail']: confirmation_required = True partstat = 'TENTATIVE' break if policy & ACT_STORE: partstat = 'NEEDS-ACTION' # Do not send an immediate response to the organizer do_send_response = False break itip_event['xml'].set_transparency(False) itip_event['xml'].set_attendee_participant_status( itip_event['xml'].get_attendee_by_email(resource['mail']), partstat ) saved = save_resource_event(itip_event, resource) log.debug( _("Adding event to %r: %r") % (resource['kolabtargetfolder'], saved), level=8 ) if saved and do_send_response: send_response(delegator['mail'] if delegator else resource['mail'], itip_event, owner) if owner and confirmation_required: send_owner_confirmation(resource, owner, itip_event) elif owner: send_owner_notification(resource, owner, itip_event, saved) def decline_reservation_request(itip_event, resource): """ Set the attendee status of the given resource to DECLINED and send an according iTip reply to the organizer. """ itip_event['xml'].set_attendee_participant_status( itip_event['xml'].get_attendee_by_email(resource['mail']), "DECLINED" ) # update master event if resource.get('existing_master') is not None or itip_event.get('_master') is not None: save_resource_event(itip_event, resource) # remove old copy of the reservation elif resource.get('existing_events', []) and len(resource['existing_events']) > 0: for existing in resource['existing_events']: delete_resource_event(existing.uid, resource, existing._msguid) # delete old event referenced by itip_event (from owner confirmation) elif hasattr(itip_event['xml'], '_msguid'): delete_resource_event(itip_event['xml'].uid, resource, itip_event['xml']._msguid) # send response and notification owner = get_resource_owner(resource) send_response(resource['mail'], itip_event, owner) if owner: send_owner_notification(resource, owner, itip_event, True) def save_resource_event(itip_event, resource): """ Append the given event object to the resource's calendar """ try: save_event = itip_event['xml'] # add exception to existing recurring main event if resource.get('existing_master') is not None: save_event = resource['existing_master'] save_event.add_exception(itip_event['xml']) elif itip_event.get('_master') is not None: save_event = itip_event['_master'] save_event.add_exception(itip_event['xml']) # remove old copy of the reservation (also sets ACLs) if 'existing_events' in resource and len(resource['existing_events']) > 0: for existing in resource['existing_events']: delete_resource_event(existing.uid, resource, existing._msguid) # delete old version referenced save_event elif hasattr(save_event, '_msguid'): delete_resource_event(save_event.uid, resource, save_event._msguid) else: imap.set_acl( resource['kolabtargetfolder'], conf.get(conf.get('kolab', 'imap_backend'), 'admin_login'), "lrswipkxtecda" ) # append new version result = imap.append( resource['kolabtargetfolder'], save_event.to_message(creator="Kolab Server ").as_string() ) return result # pylint: disable=broad-except except Exception as e: log.error(_("Failed to save event to resource calendar at %r: %r") % ( resource['kolabtargetfolder'], e )) return False def delete_resource_event(uid, resource, msguid=None): """ Removes the IMAP object with the given UID from a resource's calendar folder """ targetfolder = imap.folder_quote(resource['kolabtargetfolder']) try: imap.set_acl( targetfolder, conf.get(conf.get('kolab', 'imap_backend'), 'admin_login'), "lrswipkxtecda" ) imap.imap.m.select(targetfolder) # delete by IMAP UID if msguid is not None: log.debug(_("Delete resource calendar object from %r by UID %r") % ( targetfolder, msguid ), level=8) imap.imap.m.uid('store', msguid, '+FLAGS', '(\\Deleted)') else: typ, data = imap.imap.m.search(None, '(HEADER SUBJECT "%s")' % uid) log.debug(_("Delete resource calendar object %r in %r: %r") % ( uid, resource['kolabtargetfolder'], data ), level=8) for num in data[0].split(): imap.imap.m.store(num, '+FLAGS', '\\Deleted') imap.imap.m.expunge() return True # pylint: disable=broad-except except Exception as e: log.error(_("Failed to delete calendar object %r from folder %r: %r") % ( uid, targetfolder, e )) return False def reject(filepath): new_filepath = os.path.join( mybasepath, 'REJECT', os.path.basename(filepath) ) os.rename(filepath, new_filepath) filepath = new_filepath exec('modules.cb_action_REJECT(%r, %r)' % ('resources', filepath)) def resource_record_from_email_address(email_address): """ Resolves the given email address to a resource entity """ global auth if not auth: auth = Auth() auth.connect() resource_records = [] local_domains = auth.list_domains() if local_domains is not None: local_domains = list(set(local_domains.keys())) if not email_address.split('@')[1] in local_domains: return [] log.debug( _("Checking if email address %r belongs to a resource (collection)") % (email_address), level=8 ) resource_records = auth.find_resource(email_address) if isinstance(resource_records, list): if len(resource_records) > 0: log.debug(_("Resource record(s): %r") % (resource_records), level=8) else: log.debug(_("No resource (collection) records found for %r") % (email_address), level=8) elif isinstance(resource_records, string_types): resource_records = [resource_records] log.debug(_("Resource record: %r") % (resource_records), level=8) return resource_records def resource_records_from_itip_events(itip_events, recipient_email=None): """ Given a list of itip_events, determine which resources have been invited as attendees and/or resources. """ global auth if not auth: auth = Auth() auth.connect() resource_records = [] log.debug(_("Raw itip_events: %r") % (itip_events), level=8) attendees_raw = [] _lars = [ x for x in [ y['attendees'] for y in itip_events if 'attendees' in y and isinstance(y['attendees'], list) ] ] for list_attendees_raw in _lars: attendees_raw.extend(list_attendees_raw) _lars = [ y['attendees'] for y in itip_events if 'attendees' in y and isinstance(y['attendees'], string_types) ] for list_attendees_raw in _lars: attendees_raw.append(list_attendees_raw) log.debug(_("Raw set of attendees: %r") % (attendees_raw), level=8) # TODO: Resources are actually not implemented in the format. We reset this # list later. resources_raw = [] _lrrs = [x for x in [y['resources'] for y in itip_events if 'resource' in y]] for list_resources_raw in _lrrs: resources_raw.extend(list_resources_raw) log.debug(_("Raw set of resources: %r") % (resources_raw), level=8) # consider organizer (in REPLY messages), too organizers_raw = [ re.sub(r'\+[A-Za-z0-9=/-]+@', '@', str(y['organizer'])) for y in itip_events if 'organizer' in y ] log.debug(_("Raw set of organizers: %r") % (organizers_raw), level=8) # TODO: We expect the format of an attendee line to literally be: # # ATTENDEE:RSVP=TRUE;ROLE=REQ-PARTICIPANT;MAILTO:lydia.bossers@kolabsys.com # # which makes the attendees_raw contain: # # RSVP=TRUE;ROLE=REQ-PARTICIPANT;MAILTO:lydia.bossers@kolabsys.com # attendees = [x.split(':')[-1] for x in attendees_raw + organizers_raw] # Limit the attendee resources to the one that is actually invited # with the current message. Considering all invited resources would result in # duplicate responses from every iTip message sent to a resource. if recipient_email is not None: attendees = [a for a in attendees if a == recipient_email] for attendee in attendees: log.debug(_("Checking if attendee %r is a resource (collection)") % (attendee), level=8) _resource_records = auth.find_resource(attendee) if isinstance(_resource_records, list): if len(_resource_records) > 0: resource_records.extend(_resource_records) log.debug(_("Resource record(s): %r") % (_resource_records), level=8) else: log.debug(_("No resource (collection) records found for %r") % (attendee), level=8) elif isinstance(_resource_records, string_types): resource_records.append(_resource_records) log.debug(_("Resource record: %r") % (_resource_records), level=8) else: log.warning(_("Resource reservation made but no resource records found")) # Escape the non-implementation of the free-form, undefined RESOURCES # list(s) in iTip. if len(resource_records) == 0: # TODO: We don't know how to handle this yet! # We expect the format of an resource line to literally be: # RESOURCES:MAILTO:resource-car@kolabsys.com resources_raw = [] resources = [x.split(':')[-1] for x in resources_raw] # Limit the attendee resources to the one that is actually invited # with the current message. if recipient_email is not None: resources = [a for a in resources if a == recipient_email] for resource in resources: log.debug( _("Checking if resource %r is a resource (collection)") % (resource), level=8 ) _resource_records = auth.find_resource(resource) if isinstance(_resource_records, list): if len(_resource_records) > 0: resource_records.extend(_resource_records) log.debug(_("Resource record(s): %r") % (_resource_records), level=8) else: log.debug( _("No resource (collection) records found for %r") % (resource), level=8 ) elif isinstance(_resource_records, string_types): resource_records.append(_resource_records) log.debug(_("Resource record: %r") % (_resource_records), level=8) else: log.warning(_("Resource reservation made but no resource records found")) log.debug( _("The following resources are being referred to in the iTip: %r") % (resource_records), level=8 ) return resource_records def get_resource_records(resource_dns): """ Get the resource details, which includes details on the IMAP folder """ global auth resources = {} for resource_dn in list(set(resource_dns)): # Get the attributes for the record # See if it is a resource collection # If it is, expand to individual resources # If it is not, ... resource_attrs = auth.get_entry_attributes(None, resource_dn, ['*']) resource_attrs['dn'] = resource_dn parse_kolabinvitationpolicy(resource_attrs) if 'kolabsharedfolder' not in [x.lower() for x in resource_attrs['objectclass']]: if 'uniquemember' in resource_attrs: if not isinstance(resource_attrs['uniquemember'], list): resource_attrs['uniquemember'] = [resource_attrs['uniquemember']] resources[resource_dn] = resource_attrs for uniquemember in resource_attrs['uniquemember']: member_attrs = auth.get_entry_attributes( None, uniquemember, ['*'] ) if 'kolabsharedfolder' in [x.lower() for x in member_attrs['objectclass']]: member_attrs['dn'] = uniquemember parse_kolabinvitationpolicy(member_attrs, resource_attrs) resources[uniquemember] = member_attrs resources[uniquemember]['memberof'] = resource_dn if 'owner' not in member_attrs and 'owner' in resources[resource_dn]: resources[uniquemember]['owner'] = resources[resource_dn]['owner'] resource_dns.append(uniquemember) else: resources[resource_dn] = resource_attrs return resources def parse_kolabinvitationpolicy(attrs, parent=None): if 'kolabinvitationpolicy' in attrs: if not isinstance(attrs['kolabinvitationpolicy'], list): attrs['kolabinvitationpolicy'] = [attrs['kolabinvitationpolicy']] attrs['kolabinvitationpolicy'] = [ policy_name_map[p] for p in attrs['kolabinvitationpolicy'] if p in policy_name_map ] elif isinstance(parent, dict) and 'kolabinvitationpolicy' in parent: attrs['kolabinvitationpolicy'] = parent['kolabinvitationpolicy'] def get_resource_collection(email_address): """ Obtain a resource collection object from an email address. """ resource_dns = resource_record_from_email_address(email_address) if len(resource_dns) == 1: resource_attrs = auth.get_entry_attributes(None, resource_dns[0], ['objectclass']) if 'kolabsharedfolder' not in [x.lower() for x in resource_attrs['objectclass']]: resources = get_resource_records(resource_dns) return (resource_dns, resources) return None def get_resource_owner(resource): """ Get this resource's owner record """ global auth if not auth: auth = Auth() auth.connect() owners = [] if 'owner' in resource: if not isinstance(resource['owner'], list): owners = [resource['owner']] else: owners = resource['owner'] else: # get owner attribute from collection collections = auth.search_entry_by_attribute('uniquemember', resource['dn']) if not isinstance(collections, list): collections = [collections] for dn, collection in collections: if 'owner' in collection and isinstance(collection['owner'], list): owners += collection['owner'] elif 'owner' in collection: owners.append(collection['owner']) for dn in owners: owner = auth.get_entry_attributes(None, dn, ['cn', 'mail', 'telephoneNumber']) if owner is not None: return owner return None def get_resource_invitationpolicy(resource): """ Get this resource's kolabinvitationpolicy configuration """ global auth if 'kolabinvitationpolicy' not in resource or resource['kolabinvitationpolicy'] is None: if not auth: auth = Auth() auth.connect() # get kolabinvitationpolicy attribute from collection collections = auth.search_entry_by_attribute('uniquemember', resource['dn']) if not isinstance(collections, list): collections = [(collections['dn'], collections)] log.debug( _("Check collections %r for kolabinvitationpolicy attributes") % (collections), level=8 ) for dn, collection in collections: # ldap.search_entry_by_attribute() doesn't return the attributes lower-cased if 'kolabInvitationPolicy' in collection: collection['kolabinvitationpolicy'] = collection['kolabInvitationPolicy'] if 'kolabinvitationpolicy' in collection: parse_kolabinvitationpolicy(collection) resource['kolabinvitationpolicy'] = collection['kolabinvitationpolicy'] break return resource['kolabinvitationpolicy'] if 'kolabinvitationpolicy' in resource else None def send_response(from_address, itip_events, owner=None): """ Send the given iCal events as a valid iTip response to the organizer. In case the invited resource coolection was delegated to a concrete resource, this will send an additional DELEGATED response message. """ if isinstance(itip_events, dict): itip_events = [itip_events] for itip_event in itip_events: attendee = itip_event['xml'].get_attendee_by_email(from_address) participant_status = itip_event['xml'].get_ical_attendee_participant_status(attendee) # TODO: look-up event organizer in LDAP and change localization to its preferredlanguage message_text = reservation_response_text(participant_status, owner) subject_template = _("Reservation Request for %(summary)s was %(status)s") # Extra actions to take: send delegated reply if participant_status == "DELEGATED": delegatee = [ a for a in itip_event['xml'].get_attendees() if from_address in a.get_delegated_from(True) ][0] delegated_message_text = _(""" *** This is an automated response, please do not reply! *** Your reservation was delegated to "%s" which is available for the requested time. """) % (delegatee.get_name()) pykolab.itip.send_reply( from_address, itip_event, delegated_message_text, subject=subject_template ) # adjust some vars for the regular reply from the delegatee message_text = reservation_response_text(delegatee.get_participant_status(True), owner) from_address = delegatee.get_email() time.sleep(2) pykolab.itip.send_reply( from_address, itip_event, message_text, subject=subject_template ) def reservation_response_text(status, owner): message_text = _(""" *** This is an automated response, please do not reply! *** We hereby inform you that your reservation was %s. """) % (participant_status_label(status)) if owner: message_text += _( """ If you have questions about this reservation, please contact %s <%s> %s """ ) % ( owner['cn'], owner['mail'], owner['telephoneNumber'] if 'telephoneNumber' in owner else '' ) return message_text def send_owner_notification(resource, owner, itip_event, success=True): """ Send a reservation notification to the resource owner """ from pykolab import utils if sys.version_info.major >= 3: from email.mime.text import MIMEText from email.utils import formatdate else: from email.MIMEText import MIMEText from email.Utils import formatdate # encode unicode strings with quoted-printable from email import charset charset.add_charset('utf-8', charset.SHORTEST, charset.QP) notify = False status = itip_event['xml'].get_attendee_by_email(resource['mail']).get_participant_status(True) invitationpolicy = get_resource_invitationpolicy(resource) if invitationpolicy is not None: for policy in invitationpolicy: # TODO: distingish ACCEPTED / DECLINED status notifications? if policy & COND_NOTIFY and owner['mail']: notify = True break if notify or not success: log.debug( _("Sending booking notification for event %r to %r from %r") % ( itip_event['uid'], owner['mail'], resource['cn'] ), level=8 ) # change gettext language to the preferredlanguage setting of the resource owner if 'preferredlanguage' in owner: pykolab.translate.setUserLanguage(owner['preferredlanguage']) message_text = owner_notification_text(resource, owner, itip_event['xml'], success, status) msg = MIMEText(utils.stripped_message(message_text), _charset='utf-8') msg['To'] = owner['mail'] msg['From'] = resource['mail'] msg['Date'] = formatdate(localtime=True) if status == 'NEEDS-ACTION': msg['Subject'] = utils.str2unicode(_('New booking request for %s') % ( resource['cn'] )) else: msg['Subject'] = utils.str2unicode(_('Booking for %s has been %s') % ( resource['cn'], participant_status_label(status) if success else _('failed') )) seed = random.randint(0, 6) alarm_after = (seed * 10) + 60 log.debug(_("Set alarm to %s seconds") % (alarm_after), level=8) signal.alarm(alarm_after) result = modules._sendmail(resource['mail'], owner['mail'], msg.as_string()) log.debug(_("Owner notification was sent successfully: %r") % result, level=8) signal.alarm(0) def owner_notification_text(resource, owner, event, success, status): organizer = event.get_organizer() status = event.get_attendee_by_email(resource['mail']).get_participant_status(True) domain = resource['mail'].split('@')[1] url = conf.get('wallace', 'webmail_url') if success: if status == 'NEEDS-ACTION': message_text = _( """ The resource booking request is for %(resource)s by %(orgname)s <%(orgemail)s> for %(date)s. *** This is an automated message, sent to you as the resource owner. *** """ ) else: message_text = _( """ The resource booking for %(resource)s by %(orgname)s <%(orgemail)s> has been %(status)s for %(date)s. *** This is an automated message, sent to you as the resource owner. *** """ ) if url: message_text += ( "\n " + _("You can change the status via %(url)s") % { 'url': url } + '?_task=calendar' ) else: message_text = _( """ A reservation request for %(resource)s could not be processed automatically. Please contact %(orgname)s <%(orgemail)s> who requested this resource for %(date)s. Subject for the event: %(summary)s. *** This is an automated message, sent to you as the resource owner. *** """ ) return message_text % { 'resource': resource['cn'], 'summary': event.get_summary(), 'date': event.get_date_text(), 'status': participant_status_label(status), 'orgname': organizer.name(), 'orgemail': organizer.email(), 'domain': domain } def send_owner_confirmation(resource, owner, itip_event): """ Send a reservation request to the resource owner for manual confirmation (ACCEPT or DECLINE). This clones the given invtation with a new UID and setting the resource as organizer in order to receive the reply from the owner. """ uid = itip_event['uid'] event = itip_event['xml'] organizer = event.get_organizer() event_attendees = [ a.get_displayname() for a in event.get_attendees() if not a.get_cutype() == kolabformat.CutypeResource ] log.debug( _("Clone invitation for owner confirmation: %r from %r") % ( itip_event['uid'], event.get_organizer().email() ), level=8 ) # generate new UID and set the resource as organizer (mail, domain) = resource['mail'].split('@') event.set_uid(str(uuid.uuid4())) event.set_organizer(mail + '+' + base64.b64encode(uid, '-/') + '@' + domain, resource['cn']) itip_event['uid'] = event.get_uid() # add resource owner as (the sole) attendee event._attendees = [] event.add_attendee( owner['mail'], owner['cn'], rsvp=True, role=kolabformat.Required, participant_status=kolabformat.PartNeedsAction ) # flag this iTip message as confirmation type event.add_custom_property('X-Kolab-InvitationType', 'CONFIRMATION') message_text = _( """ A reservation request for %(resource)s requires your approval! Please either accept or decline this invitation without saving it to your calendar. The reservation request was sent from %(orgname)s <%(orgemail)s>. Subject: %(summary)s. Date: %(date)s Participants: %(attendees)s *** This is an automated message, please don't reply by email. *** """ ) % { 'resource': resource['cn'], 'orgname': organizer.name(), 'orgemail': organizer.email(), 'summary': event.get_summary(), 'date': event.get_date_text(), 'attendees': ",\n+ ".join(event_attendees) } pykolab.itip.send_request( owner['mail'], itip_event, message_text, subject=_('Booking request for %s requires confirmation') % (resource['cn']), direct=True ) diff --git a/wallace/modules.py b/wallace/modules.py index 56f8a09..831a80a 100644 --- a/wallace/modules.py +++ b/wallace/modules.py @@ -1,462 +1,461 @@ # -*- coding: utf-8 -*- # Copyright 2010-2019 Kolab Systems AG (http://www.kolabsys.com) # # Jeroen van Meeuwen (Kolab Systems) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # from __future__ import print_function from six import string_types import os import sys import time -from email import message_from_string from email.message import Message from email.mime.base import MIMEBase from email.mime.message import MIMEMessage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.parser import Parser from email.utils import COMMASPACE from email.utils import formatdate from email.utils import formataddr from email.utils import getaddresses from email.utils import parsedate_tz import smtplib import pykolab from pykolab import constants from pykolab.translate import _ log = pykolab.getLogger('pykolab.wallace/modules') extra_log_params = {'qid': '-'} log = pykolab.logger.LoggerAdapter(log, extra_log_params) conf = pykolab.getConf() modules = {} def initialize(): # We only want the base path modules_base_path = os.path.dirname(__file__) for modules_path, dirnames, filenames in os.walk(modules_base_path): if not modules_path == modules_base_path: continue for filename in filenames: if filename.startswith('module_') and filename.endswith('.py'): module_name = filename.replace('.py', '') name = module_name.replace('module_', '') # print("exec(\"from %s import __init__ as %s_register\")" % (module_name,name)) exec("from .%s import __init__ as %s_register" % (module_name, name)) exec("%s_register()" % (name)) for dirname in dirnames: if dirname != '__pycache__': register_group(modules_path, dirname) def list_modules(*args, **kw): """ List modules """ __modules = {} for module in modules: if isinstance(module, tuple): module_group, module = module __modules[module_group] = { module: modules[(module_group, module)] } else: __modules[module] = modules[module] for _module in sorted(__modules): if 'function' in __modules[_module]: # This is a top-level module if __modules[_module]['description'] is not None: print("%-25s - %s" % (_module.replace('_', '-'), __modules[_module]['description'])) else: print("%-25s" % (_module.replace('_', '-'))) for _module in sorted(__modules): if 'function' not in __modules[_module]: # This is a nested module print("\n" + _("Module Group: %s") % (_module) + "\n") for __module in sorted(__modules[_module]): if __modules[_module][__module]['description'] is not None: print( "%-4s%-21s - %s" % ( '', _module.replace('_', '-'), __modules[_module][__module]['description'] ) ) else: print("%-4s%-21s" % ('', __module.replace('_', '-'))) def execute(name, *args, **kw): if name not in modules: log.error(_("No such module %r in modules %r (1).") % (name, modules)) sys.exit(1) if 'function' not in modules[name] and 'group' not in modules[name]: log.error(_("No such module %r in modules %r (2).") % (name, modules)) sys.exit(1) try: return modules[name]['function'](*args, **kw) except Exception as errmsg: log.exception(_("Module %r - Unknown error occurred; %r") % (name, errmsg)) def heartbeat(name, *args, **kw): if name not in modules: log.warning(_("No such module %r in modules %r (1).") % (name, modules)) if 'heartbeat' in modules[name]: return modules[name]['heartbeat'](*args, **kw) def _sendmail(sender, recipients, msg): # NOTE: Use "127.0.0.1" here for IPv6 (see also the service # definition in master.cf). # Sanity check. We run into this if we end up reading an empty file, because sender ends up being an empty list if not sender or not recipients: log.warning(_("Sending email failed from %r to %r") % (sender, recipients)) return False sl = pykolab.logger.StderrToLogger(log) smtplib.stderr = sl smtp = smtplib.SMTP(timeout=15) if conf.debuglevel > 8: smtp.set_debuglevel(1) # Sender can't be a list if isinstance(sender, list): sender = sender[0] success = False attempt = 1 while not success and attempt <= 5: try: log.debug(_("Sending email via smtplib from %r, to %r (Attempt %r)") % (sender, recipients, attempt), level=8) smtp.connect("127.0.0.1", 10027) _response = smtp.sendmail(sender, recipients, msg) if len(_response) == 0: log.debug(_("SMTP sendmail OK"), level=8) else: log.debug(_("SMTP sendmail returned: %r") % (_response), level=8) smtp.quit() success = True break except smtplib.SMTPServerDisconnected as errmsg: log.error("SMTP Server Disconnected Error, %r" % (errmsg)) except smtplib.SMTPConnectError as errmsg: # DEFER log.error("SMTP Connect Error, %r" % (errmsg)) except smtplib.SMTPDataError as errmsg: # DEFER log.error("SMTP Data Error, %r" % (errmsg)) except smtplib.SMTPHeloError as errmsg: # DEFER log.error("SMTP HELO Error, %r" % (errmsg)) except smtplib.SMTPRecipientsRefused as errmsg: # REJECT, send NDR log.error("SMTP Recipient(s) Refused, %r" % (errmsg)) except smtplib.SMTPSenderRefused as errmsg: # REJECT, send NDR log.error("SMTP Sender Refused, %r" % (errmsg)) except Exception as errmsg: log.exception(_("smtplib - Unknown error occurred: %r") % (errmsg)) try: smtp.quit() except Exception as errmsg: log.error("smtplib quit() error - %r" % errmsg) time.sleep(10) attempt += 1 return success def cb_action_HOLD(module, filepath): global extra_log_params extra_log_params['qid'] = os.path.basename(filepath) log.info(_("Holding message in queue for manual review (%s by %s)") % (filepath, module)) def cb_action_DEFER(module, filepath): global extra_log_params extra_log_params['qid'] = os.path.basename(filepath) log.info(_("Deferring message in %s (by module %s)") % (filepath, module)) # parse message headers message = Parser().parse(open(filepath, 'r'), True) internal_time = parsedate_tz(message.__getitem__('Date')) internal_time = time.mktime(internal_time[:9]) + internal_time[9] now_time = time.time() delta = now_time - internal_time log.debug(_("The time when the message was sent: %r") % (internal_time), level=8) log.debug(_("The time now: %r") % (now_time), level=8) log.debug(_("The time delta: %r") % (delta), level=8) if delta > 432000: # TODO: Send NDR back to user log.debug(_("Message in file %s older then 5 days, deleting") % (filepath), level=8) os.unlink(filepath) # Alternative method is file age. #Date sent(/var/spool/pykolab/wallace/optout/DEFER/tmpIv7pDl): 'Thu, 08 Mar 2012 11:51:03 +0000' #(2012, 3, 8, 11, 51, 3, 0, 1, -1) # YYYY M D H m s weekday, yearday #log.debug(datetime.datetime(*), level=8) #import os #stat = os.stat(filepath) #fileage = datetime.datetime.fromtimestamp(stat.st_mtime) #now = datetime.datetime.now() #delta = now - fileage #print("file:", filepath, "fileage:", fileage, "now:", now, "delta(seconds):", delta.seconds) #if delta.seconds > 1800: ## TODO: Send NDR back to user #log.debug(_("Message in file %s older then 1800 seconds, deleting") % (filepath), level=8) #os.unlink(filepath) def cb_action_REJECT(module, filepath): global extra_log_params extra_log_params['qid'] = os.path.basename(filepath) log.info(_("Rejecting message in %s (by module %s)") % (filepath, module)) log.debug(_("Rejecting message in: %r") %(filepath), level=8) # parse message headers message = Parser().parse(open(filepath, 'r'), True) envelope_sender = getaddresses(message.get_all('From', [])) recipients = getaddresses(message.get_all('To', [])) + \ getaddresses(message.get_all('Cc', [])) + \ getaddresses(message.get_all('X-Kolab-To', [])) _recipients = [] for recipient in recipients: if not recipient[0] == '': _recipients.append('%s <%s>' % (recipient[0], recipient[1])) else: _recipients.append('%s' % (recipient[1])) # TODO: Find the preferredLanguage for the envelope_sender user. ndr_message_subject = "Undelivered Mail Returned to Sender" ndr_message_text = _("""This is the email system Wallace at %s. I'm sorry to inform you we could not deliver the attached message to the following recipients: - %s Your message is being delivered to any other recipients you may have sent your message to. There is no need to resend the message to those recipients. """) % ( constants.fqdn, "\n- ".join(_recipients) ) diagnostics = _("""X-Wallace-Module: %s X-Wallace-Result: REJECT """) % ( module ) msg = MIMEMultipart("report") msg['From'] = "MAILER-DAEMON@%s" % (constants.fqdn) msg['To'] = formataddr(envelope_sender[0]) msg['Date'] = formatdate(localtime=True) msg['Subject'] = ndr_message_subject msg.preamble = "This is a MIME-encapsulated message." part = MIMEText(ndr_message_text) part.add_header("Content-Description", "Notification") msg.attach(part) _diag_message = Message() _diag_message.set_payload(diagnostics) part = MIMEMessage(_diag_message, "delivery-status") part.add_header("Content-Description", "Delivery Report") msg.attach(part) # @TODO: here I'm not sure message will contain the whole body # when we used headersonly argument of Parser().parse() above # delete X-Kolab-* headers del message['X-Kolab-From'] del message['X-Kolab-To'] part = MIMEMessage(message) part.add_header("Content-Description", "Undelivered Message") msg.attach(part) result = _sendmail( "MAILER-DAEMON@%s" % (constants.fqdn), [formataddr(envelope_sender[0])], msg.as_string() ) log.debug(_("Rejection message was sent successfully: %r") % result) if result: os.unlink(filepath) else: log.debug(_("Message %r was not removed from spool") % filepath) def cb_action_ACCEPT(module, filepath): global extra_log_params extra_log_params['qid'] = os.path.basename(filepath) log.info(_("Accepting message in %s (by module %s)") % (filepath, module)) log.debug(_("Accepting message in: %r") %(filepath), level=8) # parse message headers message = Parser().parse(open(filepath, 'r'), True) messageid = message['message-id'] if 'message-id' in message else None sender = [formataddr(x) for x in getaddresses(message.get_all('X-Kolab-From', []))] recipients = [formataddr(x) for x in getaddresses(message.get_all('X-Kolab-To', []))] log.debug( _("Message-ID: %s, sender: %r, recipients: %r") % (messageid, sender, recipients), level=6 ) # delete X-Kolab-* headers del message['X-Kolab-From'] del message['X-Kolab-To'] log.debug(_("Removed X-Kolab- headers"), level=8) result = _sendmail( sender, recipients, # - Make sure we do not send this as binary. # - Second, strip NUL characters - I don't know where they # come from (TODO) # - Third, a character return is inserted somewhere. It # divides the body from the headers - and we don't like (TODO) # @TODO: check if we need Parser().parse() to load the whole message message.as_string() ) log.debug(_("Message was sent successfully: %r") % result) if result: os.unlink(filepath) else: log.debug(_("Message %r was not removed from spool") % filepath) def register_group(dirname, module): modules_base_path = os.path.join(os.path.dirname(__file__), module) modules[module] = {} for modules_path, dirnames, filenames in os.walk(modules_base_path): if not modules_path == modules_base_path: continue for filename in filenames: if filename.startswith('module_') and filename.endswith('.py'): module_name = filename.replace('.py','') name = module_name.replace('module_', '') # TODO: Error recovery from incomplete / incorrect modules. exec( "from .%s.%s import __init__ as %s_%s_register" % ( module, module_name, module, name ) ) exec("%s_%s_register()" % (module,name)) def register(name, func, group=None, description=None, aliases=[], heartbeat=None): if not group == None: module = "%s_%s" % (group,name) else: module = name if isinstance(aliases, string_types): aliases = [aliases] if module in modules: log.fatal(_("Module '%s' already registered") % (module)) sys.exit(1) if callable(func): if group == None: modules[name] = { 'function': func, 'description': description } else: modules[group][name] = { 'function': func, 'description': description } modules[module] = modules[group][name] modules[module]['group'] = group modules[module]['name'] = name for alias in aliases: modules[alias] = { 'function': func, 'description': _("Alias for %s") % (name) } if callable(heartbeat): modules[module]['heartbeat'] = heartbeat