1.1 --- a/imiptools/__init__.py Mon Feb 02 18:05:31 2015 +0100
1.2 +++ b/imiptools/__init__.py Mon Feb 02 18:16:49 2015 +0100
1.3 @@ -21,9 +21,11 @@
1.4
1.5 from email import message_from_file
1.6 from imiptools.content import handle_itip_part
1.7 -from imiptools.data import get_addresses, get_uri
1.8 +from imiptools.data import get_addresses, get_uri, make_freebusy, to_part
1.9 +from imiptools.dates import get_timestamp
1.10 from imiptools.mail import Messenger
1.11 from imiptools.profile import Preferences
1.12 +import imip_store
1.13 import sys
1.14
1.15 # Postfix exit codes.
1.16 @@ -120,7 +122,16 @@
1.17 # Reply using any outgoing parts in a new message.
1.18
1.19 if outgoing_parts:
1.20 +
1.21 + # Obtain free/busy details, if configured to do so.
1.22 +
1.23 + fb = self.can_provide_freebusy(handlers) and self.get_freebusy_for_recipient(recipient)
1.24 +
1.25 for outgoing_recipient, parts in outgoing_parts.items():
1.26 +
1.27 + # Bundle free/busy messages, if configured to do so.
1.28 +
1.29 + if fb: parts.append(fb)
1.30 message = self.messenger.make_outgoing_message(parts, [outgoing_recipient])
1.31
1.32 if "-d" in sys.argv:
1.33 @@ -169,6 +180,40 @@
1.34 elif self.lmtp_socket:
1.35 self.messenger.sendmail(recipient, msg.as_string(), lmtp_socket=self.lmtp_socket)
1.36
1.37 + def can_provide_freebusy(self, handlers):
1.38 +
1.39 + "Test for any free/busy information produced by 'handlers'."
1.40 +
1.41 + fbhandler = handlers.get("VFREEBUSY")
1.42 + if fbhandler:
1.43 + fbmethods = fbhandler.get_outgoing_methods()
1.44 + return not "REPLY" in fbmethods and not "PUBLISH" in fbmethods
1.45 + else:
1.46 + return False
1.47 +
1.48 + def get_freebusy_for_recipient(self, recipient):
1.49 +
1.50 + """
1.51 + Return a list of responses containing free/busy information for the
1.52 + given 'recipient'.
1.53 + """
1.54 +
1.55 + organiser = get_uri(recipient)
1.56 + preferences = Preferences(organiser)
1.57 +
1.58 + if preferences.get("freebusy_sharing") == "share" and \
1.59 + preferences.get("freebusy_bundling") == "always":
1.60 +
1.61 + # Invent a unique identifier.
1.62 +
1.63 + utcnow = get_timestamp()
1.64 + uid = "imip-agent-%s-%s" % (utcnow, recipient)
1.65 +
1.66 + freebusy = imip_store.FileStore().get_freebusy(organiser)
1.67 + return to_part("PUBLISH", [make_freebusy(freebusy, uid, organiser)])
1.68 +
1.69 + return None
1.70 +
1.71 def process_args(self, args, stream):
1.72
1.73 """