paul@1375 | 1 | #!/usr/bin/env python |
paul@1375 | 2 | |
paul@1375 | 3 | from email import message_from_file |
paul@1387 | 4 | from imiptools import get_handlers, parse_args |
paul@1375 | 5 | from imiptools.config import settings |
paul@1387 | 6 | from imiptools.content import get_objects_from_itip, handle_calendar_data, \ |
paul@1387 | 7 | handle_calendar_object, have_itip_part, \ |
paul@1387 | 8 | parse_itip_part |
paul@1387 | 9 | from imiptools.data import get_address, get_main_period, get_recurrence_periods, get_value, parse_object |
paul@1375 | 10 | from imiptools.dates import get_datetime_item, get_time, to_timezone |
paul@1375 | 11 | from imiptools.editing import EditingClient, PeriodError |
paul@1387 | 12 | from imiptools.handlers import person, person_outgoing |
paul@1375 | 13 | from imiptools.mail import Messenger |
paul@1375 | 14 | from imiptools.stores import get_journal, get_store |
paul@1375 | 15 | from imiptools.utils import decode_part, message_as_string |
paul@1387 | 16 | import sys, os |
paul@1375 | 17 | |
paul@1375 | 18 | # User interface functions. |
paul@1375 | 19 | |
paul@1375 | 20 | echo = False |
paul@1375 | 21 | |
paul@1375 | 22 | def read_input(label): |
paul@1375 | 23 | s = raw_input(label).strip() |
paul@1375 | 24 | if echo: |
paul@1375 | 25 | print s |
paul@1375 | 26 | return s |
paul@1375 | 27 | |
paul@1375 | 28 | def input_with_default(label, default): |
paul@1375 | 29 | return read_input(label % default) or default |
paul@1375 | 30 | |
paul@1375 | 31 | def print_title(text): |
paul@1375 | 32 | print text |
paul@1375 | 33 | print len(text) * "-" |
paul@1375 | 34 | |
paul@1375 | 35 | def write(s, filename): |
paul@1375 | 36 | f = filename and open(filename, "w") or None |
paul@1375 | 37 | try: |
paul@1375 | 38 | print >>(f or sys.stdout), s |
paul@1375 | 39 | finally: |
paul@1375 | 40 | if f: |
paul@1375 | 41 | f.close() |
paul@1375 | 42 | |
paul@1375 | 43 | # Interpret an input file containing a calendar resource. |
paul@1375 | 44 | |
paul@1387 | 45 | def get_itip_from_message(filename): |
paul@1375 | 46 | |
paul@1387 | 47 | "Return iTIP details provided by 'filename'." |
paul@1375 | 48 | |
paul@1375 | 49 | f = open(filename) |
paul@1375 | 50 | try: |
paul@1375 | 51 | msg = message_from_file(f) |
paul@1375 | 52 | finally: |
paul@1375 | 53 | f.close() |
paul@1375 | 54 | |
paul@1387 | 55 | all_itip = [] |
paul@1375 | 56 | |
paul@1375 | 57 | for part in msg.walk(): |
paul@1387 | 58 | if have_itip_part(part): |
paul@1387 | 59 | all_itip.append(parse_itip_part(part)) |
paul@1375 | 60 | |
paul@1387 | 61 | return all_itip |
paul@1387 | 62 | |
paul@1387 | 63 | def get_itip_from_data(filename, charset): |
paul@1375 | 64 | |
paul@1387 | 65 | "Return objects provided by 'filename'." |
paul@1375 | 66 | |
paul@1387 | 67 | f = open(filename) |
paul@1387 | 68 | try: |
paul@1387 | 69 | itip = parse_object(f, charset, "VCALENDAR") |
paul@1387 | 70 | finally: |
paul@1387 | 71 | f.close() |
paul@1387 | 72 | |
paul@1387 | 73 | return [itip] |
paul@1375 | 74 | |
paul@1375 | 75 | def show_objects(objects, user, store): |
paul@1375 | 76 | |
paul@1375 | 77 | """ |
paul@1375 | 78 | Show details of 'objects', accessed by the given 'user' in the given |
paul@1375 | 79 | 'store'. |
paul@1375 | 80 | """ |
paul@1375 | 81 | |
paul@1387 | 82 | print |
paul@1387 | 83 | print_title("Objects") |
paul@1387 | 84 | print |
paul@1387 | 85 | |
paul@1375 | 86 | for index, obj in enumerate(objects): |
paul@1375 | 87 | recurrenceid = obj.get_recurrenceid() |
paul@1375 | 88 | recurrence_label = recurrenceid and " %s" % recurrenceid or "" |
paul@1375 | 89 | print "(%d) Summary: %s (%s%s)" % (index, obj.get_value("SUMMARY"), obj.get_uid(), recurrence_label) |
paul@1375 | 90 | |
paul@1387 | 91 | def show_requests(user, store): |
paul@1387 | 92 | |
paul@1387 | 93 | "Show requests available to the given 'user' in the given 'store'." |
paul@1387 | 94 | |
paul@1387 | 95 | requests = store.get_requests(user) |
paul@1387 | 96 | |
paul@1387 | 97 | print |
paul@1387 | 98 | print_title("Requests") |
paul@1387 | 99 | print |
paul@1387 | 100 | |
paul@1387 | 101 | if not requests: |
paul@1387 | 102 | print "No requests are pending." |
paul@1387 | 103 | return |
paul@1387 | 104 | |
paul@1387 | 105 | for index, (uid, recurrenceid) in enumerate(requests): |
paul@1387 | 106 | obj = store.get_event(user, uid, recurrenceid) |
paul@1387 | 107 | recurrence_label = recurrenceid and " %s" % recurrenceid or "" |
paul@1387 | 108 | print "(%d) Summary: %s (%s%s)" % (index, obj.get_value("SUMMARY"), obj.get_uid(), recurrence_label) |
paul@1387 | 109 | |
paul@1375 | 110 | def show_attendee(attendee_item, index): |
paul@1375 | 111 | |
paul@1375 | 112 | "Show the 'attendee_item' (value and attributes) at 'index'." |
paul@1375 | 113 | |
paul@1375 | 114 | attendee, attr = attendee_item |
paul@1375 | 115 | partstat = attr.get("PARTSTAT") |
paul@1375 | 116 | print "(%d) %s%s" % (index, attendee, partstat and " (%s)" % partstat or "") |
paul@1375 | 117 | |
paul@1375 | 118 | def show_attendees_raw(attendee_map): |
paul@1375 | 119 | |
paul@1375 | 120 | "Show the 'attendee_map' in a simple raw form." |
paul@1375 | 121 | |
paul@1375 | 122 | for attendee, attr in attendee_map.items(): |
paul@1375 | 123 | print attendee |
paul@1375 | 124 | |
paul@1375 | 125 | def show_periods(periods, errors=None): |
paul@1375 | 126 | |
paul@1375 | 127 | "Show 'periods' with any indicated 'errors'." |
paul@1375 | 128 | |
paul@1375 | 129 | main = get_main_period(periods) |
paul@1375 | 130 | if main: |
paul@1375 | 131 | show_period(main, 0, errors) |
paul@1375 | 132 | |
paul@1375 | 133 | recurrences = get_recurrence_periods(periods) |
paul@1375 | 134 | if recurrences: |
paul@1375 | 135 | print |
paul@1375 | 136 | print_title("Recurrences") |
paul@1375 | 137 | for index, p in enumerate(recurrences): |
paul@1375 | 138 | show_period(p, index + 1, errors) |
paul@1375 | 139 | |
paul@1375 | 140 | def show_period(p, index, errors=None): |
paul@1375 | 141 | |
paul@1375 | 142 | "Show period 'p' at 'index' with any indicated 'errors'." |
paul@1375 | 143 | |
paul@1375 | 144 | errors = errors and errors.get(index) |
paul@1375 | 145 | if p.replacement: |
paul@1375 | 146 | if p.cancelled: |
paul@1375 | 147 | label = "Cancelled" |
paul@1375 | 148 | else: |
paul@1375 | 149 | label = "Replaced" |
paul@1375 | 150 | else: |
paul@1375 | 151 | if p.new_replacement: |
paul@1375 | 152 | label = "To replace" |
paul@1375 | 153 | elif p.recurrenceid: |
paul@1375 | 154 | label = "Retained" |
paul@1375 | 155 | else: |
paul@1375 | 156 | label = "New" |
paul@1375 | 157 | |
paul@1375 | 158 | error_label = errors and " (errors: %s)" % ", ".join(errors) or "" |
paul@1375 | 159 | print "(%d) %s%s:" % (index, label, error_label), p.get_start(), p.get_end(), p.origin |
paul@1375 | 160 | |
paul@1375 | 161 | def show_periods_raw(periods): |
paul@1375 | 162 | |
paul@1375 | 163 | "Show 'periods' in a simple raw form." |
paul@1375 | 164 | |
paul@1375 | 165 | periods = periods[:] |
paul@1375 | 166 | periods.sort() |
paul@1375 | 167 | map(show_period_raw, periods) |
paul@1375 | 168 | |
paul@1375 | 169 | def show_period_raw(p): |
paul@1375 | 170 | |
paul@1375 | 171 | "Show period 'p' in a simple raw form." |
paul@1375 | 172 | |
paul@1375 | 173 | print p.get_start(), p.get_end(), p.origin |
paul@1375 | 174 | |
paul@1375 | 175 | def show_attendee_changes(new, modified, unmodified, removed): |
paul@1375 | 176 | |
paul@1375 | 177 | "Show 'new', 'modified', 'unmodified' and 'removed' periods." |
paul@1375 | 178 | |
paul@1375 | 179 | print |
paul@1375 | 180 | print_title("Changes to attendees") |
paul@1375 | 181 | print |
paul@1375 | 182 | print "New:" |
paul@1375 | 183 | show_attendees_raw(new) |
paul@1375 | 184 | print |
paul@1375 | 185 | print "Modified:" |
paul@1375 | 186 | show_attendees_raw(modified) |
paul@1375 | 187 | print |
paul@1375 | 188 | print "Unmodified:" |
paul@1375 | 189 | show_attendees_raw(unmodified) |
paul@1375 | 190 | print |
paul@1375 | 191 | print "Removed:" |
paul@1375 | 192 | show_attendees_raw(removed) |
paul@1375 | 193 | |
paul@1375 | 194 | def show_period_classification(new, replaced, retained, cancelled, obsolete): |
paul@1375 | 195 | |
paul@1375 | 196 | "Show 'new', 'replaced', 'retained', 'cancelled' and 'obsolete' periods." |
paul@1375 | 197 | |
paul@1375 | 198 | print |
paul@1375 | 199 | print_title("Period classification") |
paul@1375 | 200 | print |
paul@1375 | 201 | print "New:" |
paul@1375 | 202 | show_periods_raw(new) |
paul@1375 | 203 | print |
paul@1375 | 204 | print "Replaced:" |
paul@1375 | 205 | show_periods_raw(replaced) |
paul@1375 | 206 | print |
paul@1375 | 207 | print "Retained:" |
paul@1375 | 208 | show_periods_raw(retained) |
paul@1375 | 209 | print |
paul@1375 | 210 | print "Cancelled:" |
paul@1375 | 211 | show_periods_raw(cancelled) |
paul@1375 | 212 | print |
paul@1375 | 213 | print "Obsolete:" |
paul@1375 | 214 | show_periods_raw(obsolete) |
paul@1375 | 215 | |
paul@1375 | 216 | def show_changes(modified, unmodified, removed): |
paul@1375 | 217 | |
paul@1375 | 218 | "Show 'modified', 'unmodified' and 'removed' periods." |
paul@1375 | 219 | |
paul@1375 | 220 | print |
paul@1375 | 221 | print_title("Changes to periods") |
paul@1375 | 222 | print |
paul@1375 | 223 | print "Modified:" |
paul@1375 | 224 | show_periods_raw(modified) |
paul@1375 | 225 | print |
paul@1375 | 226 | print "Unmodified:" |
paul@1375 | 227 | show_periods_raw(unmodified) |
paul@1375 | 228 | print |
paul@1375 | 229 | print "Removed:" |
paul@1375 | 230 | show_periods_raw(removed) |
paul@1375 | 231 | |
paul@1375 | 232 | def show_attendee_operations(to_invite, to_cancel, to_modify): |
paul@1375 | 233 | |
paul@1375 | 234 | "Show attendees 'to_invite', 'to_cancel' and 'to_modify'." |
paul@1375 | 235 | |
paul@1375 | 236 | print |
paul@1375 | 237 | print_title("Attendee update operations") |
paul@1375 | 238 | print |
paul@1375 | 239 | print "To invite:" |
paul@1375 | 240 | show_attendees_raw(to_invite) |
paul@1375 | 241 | print |
paul@1375 | 242 | print "To cancel:" |
paul@1375 | 243 | show_attendees_raw(to_cancel) |
paul@1375 | 244 | print |
paul@1375 | 245 | print "To modify:" |
paul@1375 | 246 | show_attendees_raw(to_modify) |
paul@1375 | 247 | |
paul@1375 | 248 | def show_period_operations(to_unschedule, to_reschedule, to_add, to_exclude, to_set, |
paul@1375 | 249 | all_unscheduled, all_rescheduled): |
paul@1375 | 250 | |
paul@1375 | 251 | """ |
paul@1375 | 252 | Show operations for periods 'to_unschedule', 'to_reschedule', 'to_add', |
paul@1375 | 253 | 'to_exclude' and 'to_set' (for updating other calendar participants), and |
paul@1375 | 254 | for periods 'all_unscheduled' and 'all_rescheduled' (for publishing event |
paul@1375 | 255 | state). |
paul@1375 | 256 | """ |
paul@1375 | 257 | |
paul@1375 | 258 | print |
paul@1375 | 259 | print_title("Period update and publishing operations") |
paul@1375 | 260 | print |
paul@1375 | 261 | print "Unschedule:" |
paul@1375 | 262 | show_periods_raw(to_unschedule) |
paul@1375 | 263 | print |
paul@1375 | 264 | print "Reschedule:" |
paul@1375 | 265 | show_periods_raw(to_reschedule) |
paul@1375 | 266 | print |
paul@1375 | 267 | print "Added:" |
paul@1375 | 268 | show_periods_raw(to_add) |
paul@1375 | 269 | print |
paul@1375 | 270 | print "Excluded:" |
paul@1375 | 271 | show_periods_raw(to_exclude) |
paul@1375 | 272 | print |
paul@1375 | 273 | print "Set in object:" |
paul@1375 | 274 | show_periods_raw(to_set) |
paul@1375 | 275 | print |
paul@1375 | 276 | print "All unscheduled:" |
paul@1375 | 277 | show_periods_raw(all_unscheduled) |
paul@1375 | 278 | print |
paul@1375 | 279 | print "All rescheduled:" |
paul@1375 | 280 | show_periods_raw(all_rescheduled) |
paul@1375 | 281 | |
paul@1375 | 282 | class TextClient(EditingClient): |
paul@1375 | 283 | |
paul@1375 | 284 | "Simple client with textual output." |
paul@1375 | 285 | |
paul@1375 | 286 | def new_object(self): |
paul@1375 | 287 | |
paul@1375 | 288 | "Create a new object with the current time." |
paul@1375 | 289 | |
paul@1375 | 290 | utcnow = get_time() |
paul@1375 | 291 | now = to_timezone(utcnow, self.get_tzid()) |
paul@1375 | 292 | obj = EditingClient.new_object(self, "VEVENT") |
paul@1375 | 293 | obj.set_value("SUMMARY", "New event") |
paul@1375 | 294 | obj["DTSTART"] = [get_datetime_item(now)] |
paul@1375 | 295 | obj["DTEND"] = [get_datetime_item(now)] |
paul@1375 | 296 | return obj |
paul@1375 | 297 | |
paul@1375 | 298 | # Editing methods involving interaction. |
paul@1375 | 299 | |
paul@1375 | 300 | def edit_attendee(self, index): |
paul@1375 | 301 | |
paul@1375 | 302 | "Edit the attendee at 'index'." |
paul@1375 | 303 | |
paul@1375 | 304 | t = self.can_edit_attendee(index) |
paul@1375 | 305 | if t: |
paul@1375 | 306 | attendees = self.state.get("attendees") |
paul@1375 | 307 | attendee, attr = t |
paul@1375 | 308 | del attendees[attendee] |
paul@1375 | 309 | attendee = input_with_default("Attendee (%s)? ", attendee) |
paul@1375 | 310 | attendees[attendee] = attr |
paul@1375 | 311 | |
paul@1375 | 312 | def edit_period(self, index, args=None): |
paul@1375 | 313 | period = self.can_edit_period(index) |
paul@1375 | 314 | if period: |
paul@1375 | 315 | edit_period(period, args) |
paul@1375 | 316 | period.cancelled = False |
paul@1375 | 317 | |
paul@1375 | 318 | # Sort the periods after this change. |
paul@1375 | 319 | |
paul@1375 | 320 | periods = self.state.get("periods") |
paul@1375 | 321 | periods.sort() |
paul@1375 | 322 | |
paul@1375 | 323 | def edit_summary(self, summary=None): |
paul@1375 | 324 | if self.can_edit_properties(): |
paul@1375 | 325 | if not summary: |
paul@1375 | 326 | summary = input_with_default("Summary (%s)? ", self.state.get("summary")) |
paul@1375 | 327 | self.state.set("summary", summary) |
paul@1375 | 328 | |
paul@1375 | 329 | def finish(self): |
paul@1375 | 330 | try: |
paul@1375 | 331 | EditingClient.finish(self) |
paul@1375 | 332 | except PeriodError: |
paul@1375 | 333 | print "Errors exist in the periods." |
paul@1375 | 334 | return |
paul@1375 | 335 | |
paul@1375 | 336 | # Diagnostic methods. |
paul@1375 | 337 | |
paul@1375 | 338 | def show_period_classification(self): |
paul@1375 | 339 | try: |
paul@1375 | 340 | new, replaced, retained, cancelled, obsolete = self.classify_periods() |
paul@1375 | 341 | show_period_classification(new, replaced, retained, cancelled, obsolete) |
paul@1375 | 342 | except PeriodError: |
paul@1375 | 343 | print |
paul@1375 | 344 | print "Errors exist in the periods." |
paul@1375 | 345 | |
paul@1375 | 346 | def show_changes(self): |
paul@1375 | 347 | try: |
paul@1375 | 348 | modified, unmodified, removed = self.classify_period_changes() |
paul@1375 | 349 | show_changes(modified, unmodified, removed) |
paul@1375 | 350 | except PeriodError: |
paul@1375 | 351 | print "Errors exist in the periods." |
paul@1375 | 352 | |
paul@1375 | 353 | is_changed = self.properties_changed() |
paul@1375 | 354 | if is_changed: |
paul@1375 | 355 | print |
paul@1375 | 356 | print "Properties changed:", ", ".join(is_changed) |
paul@1375 | 357 | new, modified, unmodified, removed = self.classify_attendee_changes() |
paul@1375 | 358 | show_attendee_changes(new, modified, unmodified, removed) |
paul@1375 | 359 | |
paul@1375 | 360 | def show_operations(self): |
paul@1375 | 361 | is_changed = self.properties_changed() |
paul@1375 | 362 | |
paul@1375 | 363 | try: |
paul@1375 | 364 | to_unschedule, to_reschedule, to_add, to_exclude, to_set, \ |
paul@1375 | 365 | all_unscheduled, all_rescheduled = self.classify_period_operations() |
paul@1375 | 366 | show_period_operations(to_unschedule, to_reschedule, to_add, |
paul@1375 | 367 | to_exclude, to_set, |
paul@1375 | 368 | all_unscheduled, all_rescheduled) |
paul@1375 | 369 | except PeriodError: |
paul@1375 | 370 | print "Errors exist in the periods." |
paul@1375 | 371 | |
paul@1375 | 372 | to_invite, to_cancel, to_modify = self.classify_attendee_operations() |
paul@1375 | 373 | show_attendee_operations(to_invite, to_cancel, to_modify) |
paul@1375 | 374 | |
paul@1375 | 375 | # Output methods. |
paul@1375 | 376 | |
paul@1375 | 377 | def show_message(self, message, plain=False, filename=None): |
paul@1375 | 378 | if plain: |
paul@1375 | 379 | decode_part(message) |
paul@1375 | 380 | write(message_as_string(message), filename) |
paul@1375 | 381 | |
paul@1375 | 382 | def show_cancel_message(self, plain=False, filename=None): |
paul@1375 | 383 | |
paul@1375 | 384 | "Show the cancel message for uninvited attendees." |
paul@1375 | 385 | |
paul@1375 | 386 | message = self.prepare_cancel_message() |
paul@1375 | 387 | if message: |
paul@1375 | 388 | self.show_message(message, plain, filename) |
paul@1375 | 389 | |
paul@1375 | 390 | def show_publish_message(self, plain=False, filename=None): |
paul@1375 | 391 | |
paul@1375 | 392 | "Show the publishing message for the updated event." |
paul@1375 | 393 | |
paul@1375 | 394 | message = self.prepare_publish_message() |
paul@1375 | 395 | self.show_message(message, plain, filename) |
paul@1375 | 396 | |
paul@1375 | 397 | def show_update_message(self, plain=False, filename=None): |
paul@1375 | 398 | |
paul@1375 | 399 | "Show the update message for the updated event." |
paul@1375 | 400 | |
paul@1375 | 401 | message = self.prepare_update_message() |
paul@1375 | 402 | if message: |
paul@1375 | 403 | self.show_message(message, plain, filename) |
paul@1375 | 404 | |
paul@1375 | 405 | # General display methods. |
paul@1375 | 406 | |
paul@1375 | 407 | def show_object(self): |
paul@1375 | 408 | print |
paul@1387 | 409 | print_title("Object details") |
paul@1387 | 410 | print |
paul@1375 | 411 | print "Summary:", self.state.get("summary") |
paul@1375 | 412 | print |
paul@1375 | 413 | print "Organiser:", self.state.get("organiser") |
paul@1375 | 414 | self.show_attendees() |
paul@1375 | 415 | self.show_periods() |
paul@1375 | 416 | self.show_suggested_attendees() |
paul@1375 | 417 | self.show_suggested_periods() |
paul@1375 | 418 | self.show_conflicting_periods() |
paul@1375 | 419 | |
paul@1375 | 420 | def show_attendees(self): |
paul@1375 | 421 | print |
paul@1375 | 422 | print_title("Attendees") |
paul@1375 | 423 | attendees = self.state.get("attendees") |
paul@1375 | 424 | for index, attendee_item in enumerate(attendees.items()): |
paul@1375 | 425 | show_attendee(attendee_item, index) |
paul@1375 | 426 | |
paul@1375 | 427 | def show_periods(self): |
paul@1375 | 428 | print |
paul@1375 | 429 | print_title("Periods") |
paul@1375 | 430 | show_periods(self.state.get("periods"), self.state.get("period_errors")) |
paul@1375 | 431 | |
paul@1375 | 432 | def show_suggested_attendees(self): |
paul@1375 | 433 | current_attendee = None |
paul@1375 | 434 | for index, (attendee, suggested_item) in enumerate(self.state.get("suggested_attendees")): |
paul@1375 | 435 | if attendee != current_attendee: |
paul@1375 | 436 | print |
paul@1375 | 437 | print_title("Attendees suggested by %s" % attendee) |
paul@1375 | 438 | current_attendee = attendee |
paul@1375 | 439 | show_attendee(suggested_item, index) |
paul@1375 | 440 | |
paul@1375 | 441 | def show_suggested_periods(self): |
paul@1375 | 442 | periods = self.state.get("suggested_periods") |
paul@1375 | 443 | current_attendee = None |
paul@1375 | 444 | index = 0 |
paul@1375 | 445 | for attendee, period, operation in periods: |
paul@1375 | 446 | if attendee != current_attendee: |
paul@1375 | 447 | print |
paul@1375 | 448 | print_title("Periods suggested by %s" % attendee) |
paul@1375 | 449 | current_attendee = attendee |
paul@1375 | 450 | show_period(period, index) |
paul@1375 | 451 | print " %s" % (operation == "add" and "Add this period" or "Remove this period") |
paul@1375 | 452 | index += 1 |
paul@1375 | 453 | |
paul@1375 | 454 | def show_conflicting_periods(self): |
paul@1375 | 455 | conflicts = self.get_conflicting_periods() |
paul@1375 | 456 | if not conflicts: |
paul@1375 | 457 | return |
paul@1375 | 458 | print |
paul@1375 | 459 | print_title("Conflicting periods") |
paul@1375 | 460 | |
paul@1375 | 461 | conflicts = list(conflicts) |
paul@1375 | 462 | conflicts.sort() |
paul@1375 | 463 | |
paul@1375 | 464 | for p in conflicts: |
paul@1375 | 465 | print p.summary, p.uid, p.get_start(), p.get_end() |
paul@1375 | 466 | |
paul@1375 | 467 | # Interaction functions. |
paul@1375 | 468 | |
paul@1375 | 469 | def expand_arg(args): |
paul@1375 | 470 | if args[0] and args[0][1:].isdigit(): |
paul@1375 | 471 | args[:1] = [args[0][0], args[0][1:]] |
paul@1375 | 472 | |
paul@1375 | 473 | def get_filename_arg(cmd): |
paul@1375 | 474 | return (cmd.split()[1:] or [None])[0] |
paul@1375 | 475 | |
paul@1375 | 476 | def next_arg(args): |
paul@1375 | 477 | if args: |
paul@1375 | 478 | arg = args[0] |
paul@1375 | 479 | del args[0] |
paul@1375 | 480 | return arg |
paul@1375 | 481 | return None |
paul@1375 | 482 | |
paul@1375 | 483 | def edit_period(period, args=None): |
paul@1375 | 484 | |
paul@1375 | 485 | "Edit the given 'period'." |
paul@1375 | 486 | |
paul@1375 | 487 | print "Editing start (%s)" % period.get_start() |
paul@1375 | 488 | edit_date(period.start, args) |
paul@1375 | 489 | print "Editing end (%s)" % period.get_end() |
paul@1375 | 490 | edit_date(period.end, args) |
paul@1375 | 491 | |
paul@1375 | 492 | def edit_date(date, args=None): |
paul@1375 | 493 | |
paul@1375 | 494 | "Edit the given 'date' object attributes." |
paul@1375 | 495 | |
paul@1375 | 496 | date.date = next_arg(args) or input_with_default("Date (%s)? ", date.date) |
paul@1375 | 497 | date.hour = next_arg(args) or input_with_default("Hour (%s)? ", date.hour) |
paul@1375 | 498 | date.minute = next_arg(args) or input_with_default("Minute (%s)? ", date.minute) |
paul@1375 | 499 | date.second = next_arg(args) or input_with_default("Second (%s)? ", date.second) |
paul@1375 | 500 | date.tzid = next_arg(args) or input_with_default("Time zone (%s)? ", date.tzid) |
paul@1375 | 501 | date.reset() |
paul@1375 | 502 | |
paul@1375 | 503 | def select_object(cl, objects): |
paul@1375 | 504 | print |
paul@1387 | 505 | |
paul@1387 | 506 | if objects: |
paul@1387 | 507 | label = "Select object number or (n)ew object or (q)uit> " |
paul@1387 | 508 | else: |
paul@1387 | 509 | label = "Select (n)ew object or (q)uit> " |
paul@1387 | 510 | |
paul@1375 | 511 | while True: |
paul@1375 | 512 | try: |
paul@1387 | 513 | cmd = read_input(label) |
paul@1375 | 514 | except EOFError: |
paul@1375 | 515 | return None |
paul@1375 | 516 | |
paul@1375 | 517 | if cmd.isdigit(): |
paul@1375 | 518 | index = int(cmd) |
paul@1375 | 519 | if 0 <= index < len(objects): |
paul@1375 | 520 | obj = objects[index] |
paul@1375 | 521 | return cl.load_object(obj.get_uid(), obj.get_recurrenceid()) |
paul@1387 | 522 | |
paul@1375 | 523 | elif cmd in ("n", "new"): |
paul@1375 | 524 | return cl.new_object() |
paul@1375 | 525 | elif cmd in ("q", "quit", "exit"): |
paul@1375 | 526 | return None |
paul@1375 | 527 | |
paul@1387 | 528 | def show_commands(): |
paul@1375 | 529 | print |
paul@1375 | 530 | print_title("Editing commands") |
paul@1375 | 531 | print |
paul@1375 | 532 | print """\ |
paul@1375 | 533 | a [ <uri> ] |
paul@1375 | 534 | attendee [ <uri> ] |
paul@1375 | 535 | Add attendee |
paul@1375 | 536 | |
paul@1375 | 537 | A, attend, attendance |
paul@1375 | 538 | Change attendance/participation |
paul@1375 | 539 | |
paul@1375 | 540 | a<digit> |
paul@1375 | 541 | attendee <digit> |
paul@1375 | 542 | Select attendee from list |
paul@1375 | 543 | |
paul@1375 | 544 | as<digit> |
paul@1375 | 545 | Add suggested attendee from list |
paul@1375 | 546 | |
paul@1375 | 547 | f, finish |
paul@1375 | 548 | Finish editing, confirming changes, proceeding to messaging |
paul@1375 | 549 | |
paul@1375 | 550 | h, help, ? |
paul@1375 | 551 | Show this help message |
paul@1375 | 552 | |
paul@1375 | 553 | l, list, show |
paul@1375 | 554 | List/show all event details |
paul@1375 | 555 | |
paul@1375 | 556 | p, period |
paul@1375 | 557 | Add new period |
paul@1375 | 558 | |
paul@1375 | 559 | p<digit> |
paul@1375 | 560 | period <digit> |
paul@1375 | 561 | Select period from list |
paul@1375 | 562 | |
paul@1375 | 563 | ps<digit> |
paul@1375 | 564 | Add or remove suggested period from list |
paul@1375 | 565 | |
paul@1375 | 566 | q, quit, exit |
paul@1375 | 567 | Exit/quit this program |
paul@1375 | 568 | |
paul@1375 | 569 | r, reload, reset, restart |
paul@1375 | 570 | Reset event periods (return to editing mode, if already finished) |
paul@1375 | 571 | |
paul@1375 | 572 | s, summary |
paul@1375 | 573 | Set event summary |
paul@1375 | 574 | """ |
paul@1375 | 575 | |
paul@1387 | 576 | print_title("Messaging commands") |
paul@1387 | 577 | print |
paul@1387 | 578 | print """\ |
paul@1387 | 579 | S, send |
paul@1387 | 580 | Send messages to recipients and to self, if appropriate |
paul@1387 | 581 | """ |
paul@1387 | 582 | |
paul@1375 | 583 | print_title("Diagnostic commands") |
paul@1375 | 584 | print |
paul@1375 | 585 | print """\ |
paul@1375 | 586 | c, class, classification |
paul@1375 | 587 | Show period classification |
paul@1375 | 588 | |
paul@1375 | 589 | C, changes |
paul@1375 | 590 | Show changes made by editing |
paul@1375 | 591 | |
paul@1375 | 592 | o, ops, operations |
paul@1375 | 593 | Show update operations |
paul@1375 | 594 | |
paul@1375 | 595 | RECURRENCE-ID [ <filename> ] |
paul@1375 | 596 | Show event recurrence identifier, writing to <filename> if specified |
paul@1375 | 597 | |
paul@1375 | 598 | UID [ <filename> ] |
paul@1375 | 599 | Show event unique identifier, writing to <filename> if specified |
paul@1375 | 600 | """ |
paul@1375 | 601 | |
paul@1387 | 602 | print_title("Message inspection commands") |
paul@1375 | 603 | print |
paul@1375 | 604 | print """\ |
paul@1375 | 605 | P [ <filename> ] |
paul@1375 | 606 | publish [ <filename> ] |
paul@1375 | 607 | Show publishing message, writing to <filename> if specified |
paul@1375 | 608 | |
paul@1375 | 609 | R [ <filename> ] |
paul@1375 | 610 | remove [ <filename> ] |
paul@1375 | 611 | cancel [ <filename> ] |
paul@1375 | 612 | Show cancellation message sent to uninvited/removed recipients, writing to |
paul@1375 | 613 | <filename> if specified |
paul@1375 | 614 | |
paul@1375 | 615 | U [ <filename> ] |
paul@1375 | 616 | update [ <filename> ] |
paul@1375 | 617 | Show update message, writing to <filename> if specified |
paul@1375 | 618 | """ |
paul@1375 | 619 | |
paul@1387 | 620 | def edit_object(cl, obj, handle_outgoing=False): |
paul@1375 | 621 | cl.show_object() |
paul@1375 | 622 | print |
paul@1375 | 623 | |
paul@1375 | 624 | try: |
paul@1375 | 625 | while True: |
paul@1375 | 626 | role = cl.is_organiser() and "Organiser" or "Attendee" |
paul@1375 | 627 | status = cl.state.get("finished") and " (editing complete)" or "" |
paul@1375 | 628 | |
paul@1375 | 629 | cmd = read_input("%s%s> " % (role, status)) |
paul@1375 | 630 | |
paul@1375 | 631 | args = cmd.split() |
paul@1375 | 632 | |
paul@1375 | 633 | if not args or not args[0]: |
paul@1375 | 634 | continue |
paul@1375 | 635 | |
paul@1375 | 636 | # Check the status of the periods. |
paul@1375 | 637 | |
paul@1375 | 638 | if cmd in ("c", "class", "classification"): |
paul@1375 | 639 | cl.show_period_classification() |
paul@1375 | 640 | print |
paul@1375 | 641 | |
paul@1375 | 642 | elif cmd in ("C", "changes"): |
paul@1375 | 643 | cl.show_changes() |
paul@1375 | 644 | print |
paul@1375 | 645 | |
paul@1375 | 646 | # Finish editing. |
paul@1375 | 647 | |
paul@1375 | 648 | elif cmd in ("f", "finish"): |
paul@1375 | 649 | cl.finish() |
paul@1375 | 650 | |
paul@1375 | 651 | # Help. |
paul@1375 | 652 | |
paul@1375 | 653 | elif cmd in ("h", "?", "help"): |
paul@1387 | 654 | show_commands() |
paul@1375 | 655 | |
paul@1375 | 656 | # Show object details. |
paul@1375 | 657 | |
paul@1375 | 658 | elif cmd in ("l", "list", "show"): |
paul@1375 | 659 | cl.show_object() |
paul@1375 | 660 | print |
paul@1375 | 661 | |
paul@1375 | 662 | # Show the operations. |
paul@1375 | 663 | |
paul@1375 | 664 | elif cmd in ("o", "ops", "operations"): |
paul@1375 | 665 | cl.show_operations() |
paul@1375 | 666 | print |
paul@1375 | 667 | |
paul@1375 | 668 | # Quit or exit. |
paul@1375 | 669 | |
paul@1375 | 670 | elif cmd in ("q", "quit", "exit"): |
paul@1375 | 671 | break |
paul@1375 | 672 | |
paul@1375 | 673 | # Restart editing. |
paul@1375 | 674 | |
paul@1375 | 675 | elif cmd in ("r", "reload", "reset", "restart"): |
paul@1375 | 676 | obj = cl.load_object(obj.get_uid(), obj.get_recurrenceid()) |
paul@1375 | 677 | if not obj: |
paul@1375 | 678 | obj = cl.new_object() |
paul@1375 | 679 | cl.reset() |
paul@1375 | 680 | cl.show_object() |
paul@1375 | 681 | print |
paul@1375 | 682 | |
paul@1375 | 683 | # Show UID details. |
paul@1375 | 684 | |
paul@1375 | 685 | elif args[0] == "UID": |
paul@1375 | 686 | filename = get_filename_arg(cmd) |
paul@1375 | 687 | write(obj.get_uid(), filename) |
paul@1375 | 688 | |
paul@1375 | 689 | elif args[0] == "RECURRENCE-ID": |
paul@1375 | 690 | filename = get_filename_arg(cmd) |
paul@1375 | 691 | write(obj.get_recurrenceid() or "", filename) |
paul@1375 | 692 | |
paul@1375 | 693 | # Post-editing operations. |
paul@1375 | 694 | |
paul@1375 | 695 | elif cl.state.get("finished"): |
paul@1375 | 696 | |
paul@1375 | 697 | # Show messages. |
paul@1375 | 698 | |
paul@1375 | 699 | if args[0] in ("P", "publish"): |
paul@1375 | 700 | filename = get_filename_arg(cmd) |
paul@1375 | 701 | cl.show_publish_message(plain=not filename, filename=filename) |
paul@1375 | 702 | |
paul@1375 | 703 | elif args[0] in ("R", "remove", "cancel"): |
paul@1375 | 704 | filename = get_filename_arg(cmd) |
paul@1375 | 705 | cl.show_cancel_message(plain=not filename, filename=filename) |
paul@1375 | 706 | |
paul@1375 | 707 | elif args[0] in ("U", "update"): |
paul@1375 | 708 | filename = get_filename_arg(cmd) |
paul@1375 | 709 | cl.show_update_message(plain=not filename, filename=filename) |
paul@1375 | 710 | |
paul@1387 | 711 | # Definitive finishing action. |
paul@1387 | 712 | |
paul@1387 | 713 | elif args[0] in ("S", "send"): |
paul@1387 | 714 | |
paul@1387 | 715 | # Send update and cancellation messages. |
paul@1387 | 716 | |
paul@1387 | 717 | did_send = False |
paul@1387 | 718 | |
paul@1387 | 719 | message = cl.prepare_update_message() |
paul@1387 | 720 | if message: |
paul@1387 | 721 | cl.send_message(message, cl.get_recipients()) |
paul@1387 | 722 | did_send = True |
paul@1387 | 723 | |
paul@1387 | 724 | to_cancel = cl.state.get("attendees_to_cancel") |
paul@1387 | 725 | if to_cancel: |
paul@1387 | 726 | message = cl.prepare_cancel_message() |
paul@1387 | 727 | if message: |
paul@1387 | 728 | cl.send_message(message, to_cancel) |
paul@1387 | 729 | did_send = True |
paul@1387 | 730 | |
paul@1387 | 731 | # Process the object using the person outgoing handler. |
paul@1387 | 732 | |
paul@1387 | 733 | if handle_outgoing: |
paul@1387 | 734 | |
paul@1387 | 735 | # Handle the parent object plus any rescheduled periods. |
paul@1387 | 736 | |
paul@1387 | 737 | unscheduled_objects, rescheduled_objects, added_objects = \ |
paul@1387 | 738 | cl.get_publish_objects() |
paul@1387 | 739 | |
paul@1387 | 740 | handlers = get_handlers(cl, person_outgoing.handlers, |
paul@1387 | 741 | [get_address(cl.user)]) |
paul@1387 | 742 | |
paul@1387 | 743 | handle_calendar_object(cl.obj, handlers, "PUBLISH") |
paul@1387 | 744 | for o in unscheduled_objects: |
paul@1387 | 745 | handle_calendar_object(o, handlers, "CANCEL") |
paul@1387 | 746 | for o in rescheduled_objects: |
paul@1387 | 747 | handle_calendar_object(o, handlers, "PUBLISH") |
paul@1387 | 748 | for o in added_objects: |
paul@1387 | 749 | handle_calendar_object(o, handlers, "ADD") |
paul@1387 | 750 | |
paul@1387 | 751 | # Otherwise, send a message to self with the event details. |
paul@1387 | 752 | |
paul@1387 | 753 | else: |
paul@1387 | 754 | message = cl.prepare_publish_message() |
paul@1387 | 755 | if message: |
paul@1387 | 756 | cl.send_message_to_self(message) |
paul@1387 | 757 | did_send = True |
paul@1387 | 758 | |
paul@1387 | 759 | # Exit if sending occurred. |
paul@1387 | 760 | |
paul@1387 | 761 | if did_send: |
paul@1387 | 762 | break |
paul@1387 | 763 | else: |
paul@1387 | 764 | print "No messages sent. Try making edits or exit manually." |
paul@1387 | 765 | |
paul@1375 | 766 | # Editing operations. |
paul@1375 | 767 | |
paul@1375 | 768 | elif not cl.state.get("finished"): |
paul@1375 | 769 | |
paul@1375 | 770 | # Expand short-form arguments. |
paul@1375 | 771 | |
paul@1375 | 772 | expand_arg(args) |
paul@1375 | 773 | |
paul@1375 | 774 | # Add or edit attendee. |
paul@1375 | 775 | |
paul@1375 | 776 | if args[0] in ("a", "attendee"): |
paul@1375 | 777 | |
paul@1375 | 778 | args = args[1:] |
paul@1375 | 779 | value = next_arg(args) |
paul@1375 | 780 | |
paul@1375 | 781 | if value and value.isdigit(): |
paul@1375 | 782 | index = int(value) |
paul@1375 | 783 | else: |
paul@1375 | 784 | try: |
paul@1375 | 785 | index = cl.find_attendee(value) |
paul@1375 | 786 | except ValueError: |
paul@1375 | 787 | index = None |
paul@1375 | 788 | |
paul@1375 | 789 | # Add an attendee. |
paul@1375 | 790 | |
paul@1375 | 791 | if index is None: |
paul@1375 | 792 | cl.add_attendee(value) |
paul@1375 | 793 | if not value: |
paul@1375 | 794 | cl.edit_attendee(-1) |
paul@1375 | 795 | |
paul@1375 | 796 | # Edit attendee (using index). |
paul@1375 | 797 | |
paul@1375 | 798 | else: |
paul@1375 | 799 | attendee_item = cl.can_remove_attendee(index) |
paul@1375 | 800 | if attendee_item: |
paul@1375 | 801 | while True: |
paul@1375 | 802 | show_attendee(attendee_item, index) |
paul@1375 | 803 | |
paul@1375 | 804 | # Obtain a command from any arguments. |
paul@1375 | 805 | |
paul@1375 | 806 | cmd = next_arg(args) |
paul@1375 | 807 | if not cmd: |
paul@1375 | 808 | cmd = read_input(" (e)dit, (r)emove (or return)> ") |
paul@1375 | 809 | if cmd in ("e", "edit"): |
paul@1375 | 810 | cl.edit_attendee(index) |
paul@1375 | 811 | elif cmd in ("r", "remove"): |
paul@1375 | 812 | cl.remove_attendees([index]) |
paul@1375 | 813 | elif not cmd: |
paul@1375 | 814 | pass |
paul@1375 | 815 | else: |
paul@1375 | 816 | continue |
paul@1375 | 817 | break |
paul@1375 | 818 | |
paul@1375 | 819 | cl.show_attendees() |
paul@1375 | 820 | print |
paul@1375 | 821 | |
paul@1375 | 822 | # Add suggested attendee (using index). |
paul@1375 | 823 | |
paul@1375 | 824 | elif args[0] in ("as", "attendee-suggested", "suggested-attendee"): |
paul@1375 | 825 | try: |
paul@1375 | 826 | index = int(args[1]) |
paul@1375 | 827 | cl.add_suggested_attendee(index) |
paul@1375 | 828 | except ValueError: |
paul@1375 | 829 | pass |
paul@1375 | 830 | cl.show_attendees() |
paul@1375 | 831 | print |
paul@1375 | 832 | |
paul@1375 | 833 | # Edit attendance. |
paul@1375 | 834 | |
paul@1375 | 835 | elif args[0] in ("A", "attend", "attendance"): |
paul@1375 | 836 | |
paul@1375 | 837 | args = args[1:] |
paul@1375 | 838 | |
paul@1375 | 839 | if not cl.is_attendee() and cl.is_organiser(): |
paul@1375 | 840 | cl.add_attendee(cl.user) |
paul@1375 | 841 | |
paul@1375 | 842 | # NOTE: Support delegation. |
paul@1375 | 843 | |
paul@1375 | 844 | if cl.can_edit_attendance(): |
paul@1375 | 845 | while True: |
paul@1375 | 846 | |
paul@1375 | 847 | # Obtain a command from any arguments. |
paul@1375 | 848 | |
paul@1375 | 849 | cmd = next_arg(args) |
paul@1375 | 850 | if not cmd: |
paul@1375 | 851 | cmd = read_input(" (a)ccept, (d)ecline, (t)entative (or return)> ") |
paul@1375 | 852 | if cmd in ("a", "accept", "accepted", "attend"): |
paul@1375 | 853 | cl.edit_attendance("ACCEPTED") |
paul@1375 | 854 | elif cmd in ("d", "decline", "declined"): |
paul@1375 | 855 | cl.edit_attendance("DECLINED") |
paul@1375 | 856 | elif cmd in ("t", "tentative"): |
paul@1375 | 857 | cl.edit_attendance("TENTATIVE") |
paul@1375 | 858 | elif not cmd: |
paul@1375 | 859 | pass |
paul@1375 | 860 | else: |
paul@1375 | 861 | continue |
paul@1375 | 862 | break |
paul@1375 | 863 | |
paul@1375 | 864 | cl.show_attendees() |
paul@1375 | 865 | print |
paul@1375 | 866 | |
paul@1375 | 867 | # Add or edit period. |
paul@1375 | 868 | |
paul@1375 | 869 | elif args[0] in ("p", "period"): |
paul@1375 | 870 | |
paul@1375 | 871 | args = args[1:] |
paul@1375 | 872 | value = next_arg(args) |
paul@1375 | 873 | |
paul@1375 | 874 | if value and value.isdigit(): |
paul@1375 | 875 | index = int(value) |
paul@1375 | 876 | else: |
paul@1375 | 877 | index = None |
paul@1375 | 878 | |
paul@1375 | 879 | # Add a new period. |
paul@1375 | 880 | |
paul@1375 | 881 | if index is None: |
paul@1375 | 882 | cl.add_period() |
paul@1375 | 883 | cl.edit_period(-1) |
paul@1375 | 884 | |
paul@1375 | 885 | # Edit period (using index). |
paul@1375 | 886 | |
paul@1375 | 887 | else: |
paul@1375 | 888 | period = cl.can_edit_period(index) |
paul@1375 | 889 | if period: |
paul@1375 | 890 | while True: |
paul@1375 | 891 | show_period_raw(period) |
paul@1375 | 892 | |
paul@1375 | 893 | # Obtain a command from any arguments. |
paul@1375 | 894 | |
paul@1375 | 895 | cmd = next_arg(args) |
paul@1375 | 896 | if not cmd: |
paul@1375 | 897 | cmd = read_input(" (e)dit, (c)ancel, (u)ncancel (or return)> ") |
paul@1375 | 898 | if cmd in ("e", "edit"): |
paul@1375 | 899 | cl.edit_period(index, args) |
paul@1375 | 900 | elif cmd in ("c", "cancel"): |
paul@1375 | 901 | cl.cancel_periods([index]) |
paul@1375 | 902 | elif cmd in ("u", "uncancel", "restore"): |
paul@1375 | 903 | cl.cancel_periods([index], False) |
paul@1375 | 904 | elif not cmd: |
paul@1375 | 905 | pass |
paul@1375 | 906 | else: |
paul@1375 | 907 | continue |
paul@1375 | 908 | break |
paul@1375 | 909 | |
paul@1375 | 910 | cl.show_periods() |
paul@1375 | 911 | print |
paul@1375 | 912 | |
paul@1375 | 913 | # Apply suggested period (using index). |
paul@1375 | 914 | |
paul@1375 | 915 | elif args[0] in ("ps", "period-suggested", "suggested-period"): |
paul@1375 | 916 | try: |
paul@1375 | 917 | index = int(args[1]) |
paul@1375 | 918 | cl.apply_suggested_period(index) |
paul@1375 | 919 | except ValueError: |
paul@1375 | 920 | pass |
paul@1375 | 921 | cl.show_periods() |
paul@1375 | 922 | print |
paul@1375 | 923 | |
paul@1375 | 924 | # Set the summary. |
paul@1375 | 925 | |
paul@1375 | 926 | elif args[0] in ("s", "summary"): |
paul@1387 | 927 | t = cmd.split(None, 1) |
paul@1387 | 928 | cl.edit_summary(len(t) > 1 and t[1] or None) |
paul@1375 | 929 | cl.show_object() |
paul@1375 | 930 | print |
paul@1375 | 931 | |
paul@1375 | 932 | except EOFError: |
paul@1375 | 933 | return |
paul@1375 | 934 | |
paul@1375 | 935 | def main(args): |
paul@1375 | 936 | global echo |
paul@1375 | 937 | |
paul@1387 | 938 | if "--help" in args: |
paul@1387 | 939 | show_help(os.path.split(sys.argv[0])[-1]) |
paul@1387 | 940 | return |
paul@1387 | 941 | |
paul@1375 | 942 | # Parse command line arguments using the standard options plus some extra |
paul@1375 | 943 | # options. |
paul@1375 | 944 | |
paul@1375 | 945 | args = parse_args(args, { |
paul@1387 | 946 | "--calendar-data" : ("calendar_data", False), |
paul@1387 | 947 | "--charset" : ("charset", "utf-8"), |
paul@1375 | 948 | "--echo" : ("echo", False), |
paul@1375 | 949 | "-f" : ("filename", None), |
paul@1387 | 950 | "--handle-data" : ("handle_data", False), |
paul@1375 | 951 | "--suppress-bcc" : ("suppress_bcc", False), |
paul@1375 | 952 | "-u" : ("user", None), |
paul@1375 | 953 | }) |
paul@1375 | 954 | |
paul@1387 | 955 | charset = args["charset"] |
paul@1387 | 956 | calendar_data = args["calendar_data"] |
paul@1375 | 957 | echo = args["echo"] |
paul@1375 | 958 | filename = args["filename"] |
paul@1387 | 959 | handle_data = args["handle_data"] |
paul@1375 | 960 | sender = (args["senders"] or [None])[0] |
paul@1375 | 961 | suppress_bcc = args["suppress_bcc"] |
paul@1375 | 962 | user = args["user"] |
paul@1375 | 963 | |
paul@1375 | 964 | # Determine the user and sender identities. |
paul@1375 | 965 | |
paul@1375 | 966 | if sender and not user: |
paul@1375 | 967 | user = get_uri(sender) |
paul@1375 | 968 | elif user and not sender: |
paul@1375 | 969 | sender = get_address(user) |
paul@1375 | 970 | elif not sender and not user: |
paul@1375 | 971 | print >>sys.stderr, "A sender or a user must be specified." |
paul@1375 | 972 | sys.exit(1) |
paul@1375 | 973 | |
paul@1375 | 974 | # Open a store. |
paul@1375 | 975 | |
paul@1375 | 976 | store_type = args.get("store_type") |
paul@1375 | 977 | store_dir = args.get("store_dir") |
paul@1375 | 978 | preferences_dir = args.get("preferences_dir") |
paul@1375 | 979 | |
paul@1375 | 980 | store = get_store(store_type, store_dir) |
paul@1375 | 981 | journal = None |
paul@1375 | 982 | |
paul@1375 | 983 | # Open a messenger for the user. |
paul@1375 | 984 | |
paul@1375 | 985 | messenger = Messenger(sender=sender, suppress_bcc=suppress_bcc) |
paul@1375 | 986 | |
paul@1375 | 987 | # Open a client for the user. |
paul@1375 | 988 | |
paul@1375 | 989 | cl = TextClient(user, messenger, store, journal, preferences_dir) |
paul@1375 | 990 | |
paul@1375 | 991 | # Read any input resource. |
paul@1375 | 992 | |
paul@1375 | 993 | if filename: |
paul@1387 | 994 | if calendar_data: |
paul@1387 | 995 | all_itip = get_itip_from_data(filename, charset) |
paul@1387 | 996 | else: |
paul@1387 | 997 | all_itip = get_itip_from_message(filename) |
paul@1387 | 998 | |
paul@1387 | 999 | objects = [] |
paul@1387 | 1000 | |
paul@1387 | 1001 | # Process the objects using the person handler. |
paul@1387 | 1002 | |
paul@1387 | 1003 | if handle_data: |
paul@1387 | 1004 | for itip in all_itip: |
paul@1387 | 1005 | objects += handle_calendar_data(itip, get_handlers(cl, person.handlers, None)) |
paul@1387 | 1006 | |
paul@1387 | 1007 | # Or just obtain objects from the data. |
paul@1387 | 1008 | |
paul@1387 | 1009 | else: |
paul@1387 | 1010 | for itip in all_itip: |
paul@1387 | 1011 | objects += get_objects_from_itip(itip, ["VEVENT"]) |
paul@1375 | 1012 | |
paul@1375 | 1013 | # Choose an object to edit. |
paul@1375 | 1014 | |
paul@1375 | 1015 | show_objects(objects, user, store) |
paul@1375 | 1016 | obj = select_object(cl, objects) |
paul@1375 | 1017 | |
paul@1375 | 1018 | # Exit without any object. |
paul@1375 | 1019 | |
paul@1375 | 1020 | if not obj: |
paul@1387 | 1021 | print >>sys.stderr, "No object loaded." |
paul@1375 | 1022 | sys.exit(1) |
paul@1375 | 1023 | |
paul@1375 | 1024 | # Or create a new object. |
paul@1375 | 1025 | |
paul@1375 | 1026 | else: |
paul@1375 | 1027 | obj = cl.new_object() |
paul@1375 | 1028 | |
paul@1375 | 1029 | # Edit the object. |
paul@1375 | 1030 | |
paul@1387 | 1031 | edit_object(cl, obj, handle_outgoing=handle_data) |
paul@1387 | 1032 | |
paul@1387 | 1033 | def show_help(progname): |
paul@1387 | 1034 | print >>sys.stderr, help_text % progname |
paul@1387 | 1035 | |
paul@1387 | 1036 | help_text = """\ |
paul@1387 | 1037 | Usage: %s -s <sender> | -u <user> \\ |
paul@1387 | 1038 | [ -f <filename> ] \\ |
paul@1387 | 1039 | [ --calendar-data --charset ] \\ |
paul@1387 | 1040 | [ --handle-data ] \\ |
paul@1387 | 1041 | [ -T <store type ] [ -S <store directory> ] \\ |
paul@1387 | 1042 | [ -p <preferences directory> ] \\ |
paul@1387 | 1043 | [ --echo ] |
paul@1387 | 1044 | |
paul@1387 | 1045 | Identity options: |
paul@1387 | 1046 | |
paul@1387 | 1047 | -s Indicate the user by specifying a sender address |
paul@1387 | 1048 | -u Indicate the user by specifying their URI |
paul@1387 | 1049 | |
paul@1387 | 1050 | Input options: |
paul@1387 | 1051 | |
paul@1387 | 1052 | -f Indicates a filename containing a MIME-encoded message or calendar object |
paul@1387 | 1053 | |
paul@1387 | 1054 | --calendar-data Indicates that the specified file contains a calendar object |
paul@1387 | 1055 | as opposed to a mail message |
paul@1387 | 1056 | --charset Specifies the character encoding used by a calendar object |
paul@1387 | 1057 | description |
paul@1387 | 1058 | |
paul@1387 | 1059 | Processing options: |
paul@1387 | 1060 | |
paul@1387 | 1061 | --handle-data Cause the input to be handled and stored in the configured |
paul@1387 | 1062 | data store |
paul@1387 | 1063 | |
paul@1387 | 1064 | Configuration options (overriding configured defaults): |
paul@1387 | 1065 | |
paul@1387 | 1066 | -p Indicates the location of user preference directories |
paul@1387 | 1067 | -S Indicates the location of the calendar data store containing user storage |
paul@1387 | 1068 | directories |
paul@1387 | 1069 | -T Indicates the store type (the configured value if omitted) |
paul@1387 | 1070 | |
paul@1387 | 1071 | Output options: |
paul@1387 | 1072 | |
paul@1387 | 1073 | --echo Echo received input, useful if consuming input from the |
paul@1387 | 1074 | standard input stream and producing a log of the program's |
paul@1387 | 1075 | activity |
paul@1387 | 1076 | """ |
paul@1375 | 1077 | |
paul@1375 | 1078 | if __name__ == "__main__": |
paul@1375 | 1079 | main(sys.argv[1:]) |
paul@1375 | 1080 | |
paul@1375 | 1081 | # vim: tabstop=4 expandtab shiftwidth=4 |