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