imip-agent

imiptools/handlers/person.py

163:9ea2f5646a4e
2015-01-23 Paul Boddie Added a note about gathering other people's free/busy details for subsequent scheduling purposes.
     1 #!/usr/bin/env python     2      3 """     4 Handlers for a person for whom scheduling is performed.     5      6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from email.mime.text import MIMEText    23 from imiptools.config import MANAGER_PATH, MANAGER_URL    24 from imiptools.content import Handler, get_address, get_uri, to_part, uri_dict, uri_items    25 from imiptools.handlers.common import CommonFreebusy    26 from socket import gethostname    27 from vCalendar import to_node    28     29 def get_manager_url():    30     url_base = MANAGER_URL or "http://%s/" % gethostname()    31     return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/"))    32     33 def get_object_url(uid):    34     return "%s/%s" % (get_manager_url().rstrip("/"), uid)    35     36 class PersonHandler(Handler):    37     38     "Handling mechanisms specific to people."    39     40     def _record_and_deliver(self, objtype, from_organiser=True, queue=False, cancel=False):    41     42         oa = self.require_organiser_and_attendees(from_organiser)    43         if not oa:    44             return False    45     46         (organiser, organiser_attr), attendees = organiser_item, attendees = oa    47     48         # Validate the organiser or attendee, ignoring spoofed requests.    49     50         if not self.validate_identities(from_organiser and [organiser_item] or attendees.items()):    51             return False    52     53         # Handle notifications and invitations.    54     55         if from_organiser:    56     57             # Process each attendee separately.    58     59             for attendee, attendee_attr in attendees.items():    60     61                 if not self.have_new_object(attendee, objtype):    62                     continue    63     64                 # Store the object and queue any request.    65     66                 self.store.set_event(attendee, self.uid, to_node(    67                     {objtype : [(self.details, {})]}    68                     ))    69     70                 if queue:    71                     self.store.queue_request(attendee, self.uid)    72                 elif cancel:    73                     self.store.cancel_event(attendee, self.uid)    74     75         # As organiser, update attendance.    76     77         else:    78             obj = self.get_object(organiser, objtype)    79     80             if obj and self.have_new_object(organiser, objtype, obj):    81     82                 # Get attendee details in a usable form.    83     84                 attendee_map = uri_dict(self.get_value_map("ATTENDEE"))    85     86                 for attendee, attendee_attr in attendees.items():    87     88                     # Update attendance in the loaded object.    89     90                     attendee_map[attendee] = attendee_attr    91     92                     # NOTE: Record other attendee free/busy information.    93     94                 # Set the new details and store the object.    95     96                 obj["ATTENDEE"] = attendee_map.items()    97     98                 self.store.set_event(organiser, self.uid, to_node(    99                     {objtype : [(obj, {})]}   100                     ))   101    102         return True   103    104     def _record_freebusy(self, from_organiser=True):   105    106         "Record free/busy information for the received information."   107    108         freebusy = []   109    110         for value in self.get_values("FREEBUSY") or []:   111             if not isinstance(value, list):   112                 value = [value]   113             for v in value:   114                 try:   115                     start, end = v.split("/", 1)   116                     freebusy.append((start, end))   117                 except ValueError:   118                     pass   119    120         for sender, sender_attr in uri_items(self.get_items(from_organiser and "ORGANIZER" or "ATTENDEE")):   121             for recipient in self.recipients:   122                 self.store.set_freebusy_for_other(get_uri(recipient), freebusy, sender)   123    124     def wrap(self, method, text, from_organiser=True, link=True):   125    126         "Wrap any valid message and pass it on to the recipient."   127    128         texts = []   129         texts.append(text)   130         if link:   131             texts.append("If your mail program cannot handle this "   132                          "message, you may view the details here:\n\n%s" %   133                          get_object_url(self.uid))   134    135         return method, MIMEText("\n".join(texts))   136    137 class Event(PersonHandler):   138    139     "An event handler."   140    141     def add(self):   142    143         # NOTE: Queue a suggested modification to any active event.   144    145         # The message is now wrapped and passed on to the recipient.   146    147         return "ADD", MIMEText("An addition to an event has been received.")   148    149     def cancel(self):   150    151         "Queue a cancellation of any active event."   152    153         self._record_and_deliver("VEVENT", from_organiser=True, queue=False, cancel=True)   154         return self.wrap("CANCEL", "A cancellation has been received.", from_organiser=True, link=True)   155    156     def counter(self):   157    158         # NOTE: Queue a suggested modification to any active event.   159    160         # The message is now wrapped and passed on to the recipient.   161    162         return "COUNTER", MIMEText("A counter proposal has been received.")   163    164     def declinecounter(self):   165    166         # NOTE: Queue a suggested modification to any active event.   167    168         # The message is now wrapped and passed on to the recipient.   169    170         return "DECLINECOUNTER", MIMEText("A declining counter proposal has been received.")   171    172     def publish(self):   173    174         "Register details of any relevant event."   175    176         self._record_and_deliver("VEVENT", from_organiser=True, queue=False)   177         return self.wrap("PUBLISH", "Details of an event have been received.", from_organiser=True, link=True)   178    179     def refresh(self):   180    181         "Update details of any active event."   182    183         self._record_and_deliver("VEVENT", from_organiser=True, queue=False)   184         return self.wrap("REFRESH", "An event update has been received.", from_organiser=True, link=True)   185    186     def reply(self):   187    188         "Record replies and notify the recipient."   189    190         self._record_and_deliver("VEVENT", from_organiser=False, queue=False)   191         return self.wrap("REPLY", "A reply has been received.", from_organiser=False, link=True)   192    193     def request(self):   194    195         "Hold requests and notify the recipient."   196    197         self._record_and_deliver("VEVENT", from_organiser=True, queue=True)   198    199         # The message is now wrapped and passed on to the recipient.   200    201         return "REQUEST", MIMEText("A request has been queued and can be viewed here: %s" % get_object_url(self.uid))   202    203 class Freebusy(PersonHandler, CommonFreebusy):   204    205     "A free/busy handler."   206    207     def publish(self):   208    209         "Register free/busy information."   210    211         # NOTE: This could be configured to not produce a message.   212    213         self._record_freebusy(from_organiser=True)   214         return self.wrap("PUBLISH", "A free/busy update has been received.", from_organiser=True, link=False)   215    216     def reply(self):   217    218         "Record replies and notify the recipient."   219    220         # NOTE: This could be configured to not produce a message.   221    222         self._record_freebusy(from_organiser=False)   223         return self.wrap("REPLY", "A reply has been received.", from_organiser=False, link=False)   224    225     def request(self):   226    227         """   228         Respond to a request by preparing a reply containing free/busy   229         information for each indicated attendee.   230         """   231    232         # NOTE: This should be subject to policy/preferences.   233    234         return CommonFreebusy.request(self)   235    236 class Journal(PersonHandler):   237    238     "A journal entry handler."   239    240     def add(self):   241    242         # NOTE: Queue a suggested modification to any active entry.   243    244         # The message is now wrapped and passed on to the recipient.   245    246         return "ADD", MIMEText("An addition to a journal entry has been received.")   247    248     def cancel(self):   249    250         # NOTE: Queue a suggested modification to any active entry.   251    252         # The message is now wrapped and passed on to the recipient.   253    254         return "CANCEL", MIMEText("A cancellation has been received.")   255    256     def publish(self):   257    258         # NOTE: Register details of any relevant entry.   259    260         # The message is now wrapped and passed on to the recipient.   261    262         self._record_and_deliver("VJOURNAL", from_organiser=True, queue=False)   263         return self.wrap("PUBLISH", "Details of a journal entry have been received.", from_organiser=True, link=False)   264    265 class Todo(PersonHandler):   266    267     "A to-do item handler."   268    269     def add(self):   270    271         # NOTE: Queue a suggested modification to any active item.   272    273         # The message is now wrapped and passed on to the recipient.   274    275         return "ADD", MIMEText("An addition to an item has been received.")   276    277     def cancel(self):   278    279         # NOTE: Queue a suggested modification to any active item.   280    281         # The message is now wrapped and passed on to the recipient.   282    283         return "CANCEL", MIMEText("A cancellation has been received.")   284    285     def counter(self):   286    287         # NOTE: Queue a suggested modification to any active item.   288    289         # The message is now wrapped and passed on to the recipient.   290    291         return "COUNTER", MIMEText("A counter proposal has been received.")   292    293     def declinecounter(self):   294    295         # NOTE: Queue a suggested modification to any active item.   296    297         # The message is now wrapped and passed on to the recipient.   298    299         return "DECLINECOUNTER", MIMEText("A declining counter proposal has been received.")   300    301     def publish(self):   302    303         "Register details of any relevant item."   304    305         self._record_and_deliver("VTODO", from_organiser=True, queue=False)   306         return self.wrap("PUBLISH", "Details of an item have been received.", from_organiser=True, link=True)   307    308     def refresh(self):   309    310         "Update details of any active item."   311    312         self._record_and_deliver("VTODO", from_organiser=True, queue=False)   313         return self.wrap("REFRESH", "An item update has been received.", from_organiser=True, link=True)   314    315     def reply(self):   316    317         "Record replies and notify the recipient."   318    319         self._record_and_deliver("VTODO", from_organiser=False, queue=False)   320         return self.wrap("REPLY", "A reply has been received.", from_organiser=False, link=True)   321    322     def request(self):   323    324         "Hold requests and notify the recipient."   325    326         self._record_and_deliver("VTODO", from_organiser=True, queue=True)   327    328         # The message is now wrapped and passed on to the recipient.   329    330         return "REQUEST", MIMEText("A request has been queued.")   331    332 # Handler registry.   333    334 handlers = [   335     ("VFREEBUSY",   Freebusy),   336     ("VEVENT",      Event),   337     ("VTODO",       Todo),   338     ("VJOURNAL",    Journal),   339     ]   340    341 # vim: tabstop=4 expandtab shiftwidth=4