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 accept_request(self): 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, "ACCEPTED") 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 accept a stored request, writing the 75 # response message to standard output. 76 77 if __name__ == "__main__": 78 try: 79 store_dir, user, uid, recurrenceid = (sys.argv[1:5] + [None])[:4] 80 except ValueError: 81 print >>sys.stderr, "Need a store directory, user URI, event UID and optional RECURRENCE-ID." 82 sys.exit(1) 83 84 store = imip_store.FileStore(store_dir) 85 fragment = store.get_event(user, uid, recurrenceid) 86 87 if not fragment: 88 print >>sys.stderr, "No such event:", uid, recurrenceid 89 sys.exit(1) 90 91 obj = Object(fragment) 92 handler = TestHandler(obj, user, Messenger()) 93 response = handler.accept_request() 94 95 if response: 96 store.dequeue_request(user, uid, recurrenceid) 97 print response 98 else: 99 sys.exit(1) 100 101 # vim: tabstop=4 expandtab shiftwidth=4