paul@585 | 1 | #!/usr/bin/env python |
paul@585 | 2 | |
paul@585 | 3 | """ |
paul@585 | 4 | A handler to help with testing. |
paul@585 | 5 | |
paul@585 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@585 | 7 | |
paul@585 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@585 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@585 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@585 | 11 | version. |
paul@585 | 12 | |
paul@585 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@585 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@585 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@585 | 16 | details. |
paul@585 | 17 | |
paul@585 | 18 | You should have received a copy of the GNU General Public License along with |
paul@585 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@585 | 20 | """ |
paul@585 | 21 | |
paul@601 | 22 | from imiptools.client import ClientForObject |
paul@667 | 23 | from imiptools.data import Object, get_address, parse_object |
paul@585 | 24 | from imiptools.mail import Messenger |
paul@585 | 25 | import imip_store |
paul@585 | 26 | import sys |
paul@585 | 27 | |
paul@601 | 28 | class TestClient(ClientForObject): |
paul@585 | 29 | |
paul@585 | 30 | """ |
paul@585 | 31 | A content handler for use in testing, as opposed to operating within the |
paul@585 | 32 | mail processing pipeline. |
paul@585 | 33 | """ |
paul@585 | 34 | |
paul@585 | 35 | # Action methods. |
paul@585 | 36 | |
paul@587 | 37 | def handle_request(self, accept): |
paul@585 | 38 | |
paul@585 | 39 | """ |
paul@585 | 40 | Process the current request for the current user. Return whether any |
paul@585 | 41 | action was taken. |
paul@585 | 42 | """ |
paul@585 | 43 | |
paul@585 | 44 | # Reply only on behalf of this user. |
paul@585 | 45 | |
paul@587 | 46 | attendee_attr = self.update_participation(self.obj, accept and "ACCEPTED" or "DECLINED") |
paul@585 | 47 | |
paul@585 | 48 | if not attendee_attr: |
paul@585 | 49 | return None |
paul@585 | 50 | |
paul@601 | 51 | # NOTE: This is a simpler form of the code in imipweb.client. |
paul@585 | 52 | |
paul@585 | 53 | organiser = get_address(self.obj.get_value("ORGANIZER")) |
paul@585 | 54 | |
paul@585 | 55 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@585 | 56 | self.update_dtstamp() |
paul@585 | 57 | self.set_sequence(False) |
paul@585 | 58 | |
paul@585 | 59 | message = self.messenger.make_outgoing_message( |
paul@585 | 60 | [self.obj.to_part("REPLY")], |
paul@585 | 61 | [organiser], |
paul@585 | 62 | outgoing_bcc=get_address(self.user) |
paul@585 | 63 | ) |
paul@585 | 64 | |
paul@585 | 65 | return message.as_string() |
paul@585 | 66 | |
paul@587 | 67 | # A simple main program that attempts to handle a stored request, writing the |
paul@585 | 68 | # response message to standard output. |
paul@585 | 69 | |
paul@585 | 70 | if __name__ == "__main__": |
paul@585 | 71 | try: |
paul@667 | 72 | minargs = 3; maxargs = 5 |
paul@667 | 73 | accept, store_dir, user, uid, recurrenceid = (sys.argv[1:maxargs+1] + ([None] * (maxargs - minargs)))[:maxargs] |
paul@585 | 74 | except ValueError: |
paul@587 | 75 | print >>sys.stderr, "Need 'accept' or 'decline', a store directory, user URI, event UID and optional RECURRENCE-ID." |
paul@593 | 76 | print >>sys.stderr, "The RECURRENCE-ID must be in the form employed by the store." |
paul@667 | 77 | print >>sys.stderr |
paul@667 | 78 | print >>sys.stderr, "Alternatively, omit the UID and RECURRENCE-ID and provide event-only details on standard input" |
paul@667 | 79 | print >>sys.stderr, "to force the script to handle an event not already present in the store." |
paul@585 | 80 | sys.exit(1) |
paul@585 | 81 | |
paul@585 | 82 | store = imip_store.FileStore(store_dir) |
paul@667 | 83 | |
paul@667 | 84 | if uid is not None: |
paul@667 | 85 | fragment = store.get_event(user, uid, recurrenceid) |
paul@585 | 86 | |
paul@667 | 87 | if not fragment: |
paul@667 | 88 | print >>sys.stderr, "No such event:", uid, recurrenceid |
paul@667 | 89 | sys.exit(1) |
paul@667 | 90 | else: |
paul@667 | 91 | fragment = parse_object(sys.stdin, "utf-8") |
paul@585 | 92 | |
paul@585 | 93 | obj = Object(fragment) |
paul@667 | 94 | handler = TestClient(obj, user, Messenger(), store) |
paul@587 | 95 | response = handler.handle_request(accept == "accept") |
paul@585 | 96 | |
paul@585 | 97 | if response: |
paul@667 | 98 | if uid is not None: |
paul@667 | 99 | store.dequeue_request(user, uid, recurrenceid) |
paul@585 | 100 | print response |
paul@585 | 101 | else: |
paul@585 | 102 | sys.exit(1) |
paul@585 | 103 | |
paul@585 | 104 | # vim: tabstop=4 expandtab shiftwidth=4 |