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