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 os.path import split 25 import sys 26 27 # Find the modules. 28 29 try: 30 import imiptools 31 except ImportError: 32 parent = split(split(__file__)[0])[0] 33 if split(parent)[1] == "imip-agent": 34 sys.path.append(parent) 35 36 from codecs import getwriter 37 from imiptools.client import Client 38 from imiptools.data import get_window_end, Object 39 from imiptools.dates import get_default_timezone, to_utc_datetime 40 from imiptools.period import insert_period 41 from imiptools.profile import Preferences 42 from imip_store import FileStore, FilePublisher 43 44 def make_freebusy(store, publisher, preferences, user, participant, 45 store_and_publish, include_needs_action, reset_updated_list, verbose): 46 47 """ 48 Using the given 'store', 'publisher' and 'preferences', make free/busy 49 details for the records of the given 'user', generating details for 50 'participant' if not indicated as None; otherwise, generating free/busy 51 details concerning the given user. 52 53 If 'store_and_publish' is set, the stored details will be updated; 54 otherwise, the details will be written to standard output. 55 56 If 'include_needs_action' is set, details of objects whose participation 57 status is set to "NEEDS-ACTION" for the participant will be included in the 58 details. 59 60 If 'reset_updated_list' is set, all objects will be inspected for periods; 61 otherwise, only those in the stored free/busy providers file will be 62 inspected. 63 64 If 'verbose' is set, messages will be written to standard error. 65 """ 66 67 participant = participant or user 68 tzid = preferences.get("TZID") or get_default_timezone() 69 70 # Get the size of the free/busy window. 71 72 try: 73 window_size = int(preferences.get("window_size")) 74 except (TypeError, ValueError): 75 window_size = 100 76 window_end = get_window_end(tzid, window_size) 77 78 # Get identifiers for uncancelled events either from a list of events 79 # providing free/busy periods at the end of the given time window, or from 80 # a list of all events. 81 82 all_events = not reset_updated_list and store.get_freebusy_providers(user, window_end) 83 84 if not all_events: 85 all_events = store.get_all_events(user) 86 fb = [] 87 88 # With providers of additional periods, append to the existing collection. 89 90 else: 91 if user == participant: 92 fb = store.get_freebusy(user) 93 else: 94 fb = store.get_freebusy_for_other(user, participant) 95 96 # Obtain event objects. 97 98 objs = [] 99 for uid, recurrenceid in all_events: 100 if verbose: 101 print >>sys.stderr, uid, recurrenceid 102 event = store.get_event(user, uid, recurrenceid) 103 if event: 104 objs.append(Object(event)) 105 106 # Build a free/busy collection for the given user. 107 108 for obj in objs: 109 partstat = obj.get_participation_status(participant) 110 recurrenceids = not obj.get_recurrenceid() and store.get_recurrences(user, obj.get_uid()) 111 112 if obj.get_participation(partstat, include_needs_action): 113 for p in obj.get_active_periods(recurrenceids, tzid, window_end): 114 fbp = obj.get_freebusy_period(p, partstat == "ORG") 115 insert_period(fb, fbp) 116 117 # Store and publish the free/busy collection. 118 119 if store_and_publish: 120 if user == participant: 121 store.set_freebusy(user, fb) 122 123 if Client(user).is_sharing() and Client(user).is_publishing(): 124 publisher.set_freebusy(user, fb) 125 126 # Update the list of objects providing periods on future occasions. 127 128 store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), 129 [obj for obj in objs if obj.possibly_active_from(window_end, tzid)]) 130 else: 131 store.set_freebusy_for_other(user, fb, participant) 132 133 # Alternatively, just write the collection to standard output. 134 135 else: 136 f = getwriter("utf-8")(sys.stdout) 137 for item in fb: 138 print >>f, "\t".join(item.as_tuple(strings_only=True)) 139 140 # Main program. 141 142 if __name__ == "__main__": 143 144 # Interpret the command line arguments. 145 146 participants = [] 147 args = [] 148 store_dir = [] 149 publishing_dir = [] 150 preferences_dir = [] 151 ignored = [] 152 153 # Collect user details first, switching to other arguments when encountering 154 # switches. 155 156 l = participants 157 158 for arg in sys.argv[1:]: 159 if arg in ("-n", "-s", "-v", "-r"): 160 args.append(arg) 161 l = ignored 162 elif arg == "-S": 163 l = store_dir 164 elif arg == "-P": 165 l = publishing_dir 166 elif arg == "-p": 167 l = preferences_dir 168 else: 169 l.append(arg) 170 171 try: 172 user = participants[0] 173 except IndexError: 174 print >>sys.stderr, """\ 175 Usage: %s <user> [ <other user> ] <options> 176 177 Need a user and an optional participant (if different from the user), 178 along with the -s option if updating the store and the published details. 179 Specify -n to include objects with PARTSTAT of NEEDS-ACTION. 180 Specify -r to inspect all objects, not just those expected to provide details. 181 Specify -v for additional messages on standard error. 182 """ % split(sys.argv[0])[1] 183 sys.exit(1) 184 185 # Define any other participant of interest plus options. 186 187 participant = participants[1:] and participants[1] or None 188 store_and_publish = "-s" in args 189 include_needs_action = "-n" in args 190 reset_updated_list = "-r" in args 191 verbose = "-v" in args 192 193 # Override defaults if indicated. 194 195 store_dir = store_dir and store_dir[0] or None 196 publishing_dir = publishing_dir and publishing_dir[0] or None 197 preferences_dir = preferences_dir and preferences_dir[0] or None 198 199 # Obtain store-related objects. 200 201 store = FileStore(store_dir) 202 publisher = FilePublisher(publishing_dir) 203 preferences = Preferences(user, preferences_dir) 204 205 # Obtain a list of users for processing. 206 207 if user in ("*", "all"): 208 users = store.get_users() 209 else: 210 users = [user] 211 212 # Process the given users. 213 214 for user in users: 215 if verbose: 216 print >>sys.stderr, user 217 make_freebusy(store, publisher, preferences, user, participant, 218 store_and_publish, include_needs_action, reset_updated_list, verbose) 219 220 # vim: tabstop=4 expandtab shiftwidth=4