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(), 44 p.get_end(), 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 p.get_tzid() 51 )) 52 53 # Main program. 54 55 if __name__ == "__main__": 56 57 # Interpret the command line arguments. 58 59 try: 60 user = sys.argv[1] 61 args = sys.argv[2:] 62 participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user 63 store_and_publish = "-s" in args 64 include_needs_action = "-n" in args 65 verbose = "-v" in args 66 except IndexError: 67 print >>sys.stderr, """\ 68 Need a user and an optional participant (if different from the user), 69 along with the -s option if updating the store and the published details. 70 """ 71 sys.exit(1) 72 73 preferences = Preferences(user) 74 tzid = preferences.get("TZID") or get_default_timezone() 75 76 # Get the size of the free/busy window. 77 78 try: 79 window_size = int(preferences.get("window_size")) 80 except (TypeError, ValueError): 81 window_size = 100 82 window_end = get_window_end(tzid, window_size) 83 84 store = FileStore() 85 publisher = FilePublisher() 86 87 # Get all identifiers for events. 88 89 uids = store.get_events(user) 90 91 all_events = set() 92 for uid in uids: 93 all_events.add((uid, None)) 94 all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) 95 96 # Filter out cancelled events. 97 98 cancelled = store.get_cancellations(user) or [] 99 all_events.difference_update(cancelled) 100 101 # Obtain event objects. 102 103 objs = [] 104 for uid, recurrenceid in all_events: 105 if verbose: 106 print >>sys.stderr, uid, recurrenceid 107 event = store.get_event(user, uid, recurrenceid) 108 if event: 109 objs.append(Object(event)) 110 111 # Build a free/busy collection for the given user. 112 113 fb = [] 114 for obj in objs: 115 attendees = obj.get_value_map("ATTENDEE") 116 organiser = obj.get_value("ORGANIZER") 117 recurrenceids = store.get_recurrences(user, obj.get_value("UID")) 118 119 for attendee, attendee_attr in attendees.items(): 120 121 # Only consider events where the stated participant actually attends. 122 123 if attendee == participant: 124 partstat = attendee_attr.get("PARTSTAT", "NEEDS-ACTION") 125 126 if partstat not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ 127 include_needs_action and partstat == "NEEDS-ACTION": 128 129 get_periods(fb, obj, tzid, window_end, False, recurrenceids) 130 131 break 132 133 # Where not attending, retain the affected periods and mark them as 134 # organising periods. 135 136 else: 137 if organiser == participant: 138 get_periods(fb, obj, tzid, window_end, True, recurrenceids) 139 140 fb.sort() 141 142 # Store and publish the free/busy collection. 143 144 if store_and_publish: 145 if user == participant: 146 store.set_freebusy(user, fb) 147 publisher.set_freebusy(user, fb) 148 else: 149 store.set_freebusy_for_other(user, fb, participant) 150 else: 151 for item in fb: 152 print "\t".join(item.as_tuple(strings_only=True)) 153 154 # vim: tabstop=4 expandtab shiftwidth=4