summaryrefslogtreecommitdiff
path: root/plugins/chatbot.py
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/chatbot.py')
-rw-r--r--plugins/chatbot.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/plugins/chatbot.py b/plugins/chatbot.py
new file mode 100644
index 0000000..e26928d
--- /dev/null
+++ b/plugins/chatbot.py
@@ -0,0 +1,71 @@
+import re
+import random
+import types
+import net.mapserv as mapserv
+from utils import extends
+import chat
+
+
+__all__ = [ 'PLUGIN', 'init', 'answer', 'add_command', 'remove_command' ]
+
+
+PLUGIN = {
+ 'name': 'chatbot',
+ 'requires': (),
+ 'blocks': (),
+}
+
+commands = {}
+
+
+def answer_info(nick, message, is_whisper, match):
+ if is_whisper:
+ chat.send_whisper(nick, "This is an experimental bot.")
+
+def answer_random(nick, message, is_whisper, answers):
+ resp = random.choice(answers)
+ if is_whisper:
+ chat.send_whisper(nick, resp)
+ else:
+ mapserv.cmsg_chat_message(resp)
+
+def answer(nick, message, is_whisper):
+ try:
+ for regex, action in commands.iteritems():
+ match = regex.match(message)
+ if match:
+ if isinstance(action, types.ListType):
+ answer_random(nick, message, is_whisper, action)
+ elif isinstance(action, types.FunctionType):
+ action(nick, message, is_whisper, match)
+ else:
+ raise ValueError("must be either list or function")
+ except:
+ answer_random(nick, message, is_whisper, action)
+
+@extends('smsg_being_chat')
+def being_chat(data):
+ idx = data.message.find(' : ')
+ if idx > -1:
+ nick = data.message[:idx]
+ message = data.message[idx + 3:]
+ answer(nick, message, False)
+
+
+@extends('smsg_whisper')
+def got_whisper(data):
+ nick, message = data.nick, data.message
+ answer(nick, message, True)
+
+
+def add_command(cmd, action):
+ cmd_re = re.compile(cmd)
+ commands[cmd_re] = action
+
+def remove_command(cmd):
+ cmd_re = re.compile(cmd)
+ commands.remove(cmd_re)
+
+def init(config):
+ add_command('!info', answer_info)
+ add_command('!random', ['asd', 'Ciao!'])