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