1 # Copyright (c) 2010 Google Inc. All rights reserved. 2 # 3 # Redistribution and use in source and binary forms, with or without 4 # modification, are permitted provided that the following conditions are 5 # met: 6 # 7 # * Redistributions of source code must retain the above copyright 8 # notice, this list of conditions and the following disclaimer. 9 # * Redistributions in binary form must reproduce the above 10 # copyright notice, this list of conditions and the following disclaimer 11 # in the documentation and/or other materials provided with the 12 # distribution. 13 # * Neither the name of Google Inc. nor the names of its 14 # contributors may be used to endorse or promote products derived from 15 # this software without specific prior written permission. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 from webkitpy.tool.bot import irc_command 30 31 from webkitpy.common.net.irc.ircbot import IRCBotDelegate 32 from webkitpy.common.thread.threadedmessagequeue import ThreadedMessageQueue 33 34 35 class _IRCThreadTearoff(IRCBotDelegate): 36 def __init__(self, password, message_queue, wakeup_event): 37 self._password = password 38 self._message_queue = message_queue 39 self._wakeup_event = wakeup_event 40 41 # IRCBotDelegate methods 42 43 def irc_message_received(self, nick, message): 44 self._message_queue.post([nick, message]) 45 self._wakeup_event.set() 46 47 def irc_nickname(self): 48 return "sheriffbot" 49 50 def irc_password(self): 51 return self._password 52 53 54 class SheriffIRCBot(object): 55 def __init__(self, tool, sheriff): 56 self._tool = tool 57 self._sheriff = sheriff 58 self._message_queue = ThreadedMessageQueue() 59 60 def irc_delegate(self): 61 return _IRCThreadTearoff(self._tool.irc_password, 62 self._message_queue, 63 self._tool.wakeup_event) 64 65 def process_message(self, message): 66 (nick, request) = message 67 tokenized_request = request.strip().split(" ") 68 if not tokenized_request: 69 return 70 command = irc_command.commands.get(tokenized_request[0]) 71 args = tokenized_request[1:] 72 if not command: 73 # Give the peoples someone to talk with. 74 command = irc_command.Eliza 75 args = tokenized_request 76 response = command().execute(nick, args, self._tool, self._sheriff) 77 if response: 78 self._tool.irc().post(response) 79 80 def process_pending_messages(self): 81 (messages, is_running) = self._message_queue.take_all() 82 for message in messages: 83 self.process_message(message) 84