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