1 #!/usr/bin/env python 2 3 """ 4 Construct free/busy records for a user, either recording that user's own 5 availability schedule or the schedule of another user (using details provided 6 when scheduling events with that user). 7 8 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 9 10 This program is free software; you can redistribute it and/or modify it under 11 the terms of the GNU General Public License as published by the Free Software 12 Foundation; either version 3 of the License, or (at your option) any later 13 version. 14 15 This program is distributed in the hope that it will be useful, but WITHOUT 16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 18 details. 19 20 You should have received a copy of the GNU General Public License along with 21 this program. If not, see <http://www.gnu.org/licenses/>. 22 """ 23 24 from imiptools.data import get_window_end, Object 25 from imiptools.dates import get_default_timezone 26 from imiptools.period import FreeBusyPeriod, is_replaced 27 from imiptools.profile import Preferences 28 from imip_store import FileStore, FilePublisher 29 import sys 30 31 def get_periods(fb, obj, tzid, window_end, only_organiser, recurrenceids): 32 33 """ 34 Update free/busy details 'fb' with the actual periods associated with the 35 event 'obj'. 36 """ 37 38 recurrenceid = obj.get_recurrenceid() 39 40 for p in obj.get_periods(tzid, window_end): 41 if recurrenceid or not is_replaced(p, recurrenceids, tzid): 42 fb.append(FreeBusyPeriod( 43 p.get_start_point(), 44 p.get_end_point(), 45 obj.get_value("UID"), 46 only_organiser and "ORG" or obj.get_value("TRANSP") or "OPAQUE", 47 recurrenceid, 48 obj.get_value("SUMMARY"), 49 obj.get_value("ORGANIZER") 50 )) 51 52 # Main program. 53 54 if __name__ == "__main__": 55 56 # Interpret the command line arguments. 57 58 try: 59 user = sys.argv[1] 60 args = sys.argv[2:] 61 participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user 62 store_and_publish = "-s" in args 63 include_needs_action = "-n" in args 64 verbose = "-v" in args 65 except IndexError: 66 print >>sys.stderr, """\ 67 Need a user and an optional participant (if different from the user), 68 along with the -s option if updating the store and the published details. 69 """ 70 sys.exit(1) 71 72 preferences = Preferences(user) 73 tzid = preferences.get("TZID") or get_default_timezone() 74 75 # Get the size of the free/busy window. 76 77 try: 78 window_size = int(preferences.get("window_size")) 79 except (TypeError, ValueError): 80 window_size = 100 81 window_end = get_window_end(tzid, window_size) 82 83 store = FileStore() 84 publisher = FilePublisher() 85 86 # Get all identifiers for events. 87 88 uids = store.get_events(user) 89 90 all_events = set() 91 for uid in uids: 92 all_events.add((uid, None)) 93 all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) 94 95 # Filter out cancelled events. 96 97 cancelled = store.get_cancellations(user) or [] 98 all_events.difference_update(cancelled) 99 100 # Obtain event objects. 101 102 objs = [] 103 for uid, recurrenceid in all_events: 104 if verbose: 105 print >>sys.stderr, uid, recurrenceid 106 event = store.get_event(user, uid, recurrenceid) 107 if event: 108 objs.append(Object(event)) 109 110 # Build a free/busy collection for the given user. 111 112 fb = [] 113 for obj in objs: 114 attendees = obj.get_value_map("ATTENDEE") 115 organiser = obj.get_value("ORGANIZER") 116 recurrenceids = store.get_recurrences(user, obj.get_value("UID")) 117 118 for attendee, attendee_attr in attendees.items(): 119 120 # Only consider events where the stated participant actually attends. 121 122 if attendee == participant: 123 partstat = attendee_attr.get("PARTSTAT", "NEEDS-ACTION") 124 125 if partstat not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ 126 include_needs_action and partstat == "NEEDS-ACTION": 127 128 get_periods(fb, obj, tzid, window_end, False, recurrenceids) 129 130 break 131 132 # Where not attending, retain the affected periods and mark them as 133 # organising periods. 134 135 else: 136 if organiser == participant: 137 get_periods(fb, obj, tzid, window_end, True, recurrenceids) 138 139 fb.sort() 140 141 # Store and publish the free/busy collection. 142 143 if store_and_publish: 144 if user == participant: 145 store.set_freebusy(user, fb) 146 publisher.set_freebusy(user, fb) 147 else: 148 store.set_freebusy_for_other(user, fb, participant) 149 else: 150 for item in fb: 151 print "\t".join(item.as_tuple(strings_only=True)) 152 153 # vim: tabstop=4 expandtab shiftwidth=4