forked from hreeder/botbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botbot.py
103 lines (79 loc) · 3.46 KB
/
botbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import commands
import hooks
import logging
import pydle
import sys
import webhooks
from configparser import ConfigParser
from pydle.async import EventLoop
from tornado.httpserver import HTTPServer
logger = logging.getLogger("BotBot-Bot")
class BotBot(pydle.Client):
def __init__(self, config, fallback_nicknames=[], username=None, realname=None, **kwargs):
super(BotBot, self).__init__(config['IRC']['nick'],
fallback_nicknames=fallback_nicknames,
username=username,
realname=realname,
**kwargs)
self.join_channels = config['IRC']['channel'].split()
self.trigger = config['IRC']['trigger']
self.config = config
self.commands = {}
self.pm_commands = {}
self.channel_hooks = []
self.ignored_users = set()
self.event_loop = EventLoop()
def register_command(self, command, action, type="channel"):
if type == "pm":
self.pm_commands[command] = action
else:
self.commands[command] = action
def register_hook(self, channel_hook):
self.channel_hooks.append(channel_hook)
def on_connect(self):
for channel in self.join_channels:
self.join(channel)
def on_channel_message(self, channel, sender, message):
if message.startswith(self.trigger):
message = message[len(self.trigger):]
command = message.split()[0]
args = message.split()[1:]
logger.debug("Command: " + command)
logger.debug("Args: " + str(args))
if command in self.commands.keys() and sender not in self.ignored_users:
self.commands[command](self, channel, sender, args)
return
for chan_hook in self.channel_hooks:
chan_hook(self, channel, sender, message)
def on_private_message(self, sender, message):
command = message.split()[0]
args = message.split()[1:]
if command in self.pm_commands.keys():
self.pm_commands[command](self, sender, args)
if __name__ == "__main__":
config = ConfigParser()
# Attempt to read the specified config file, else read a default config.ini
try:
config.read(sys.argv[1])
except IndexError:
config.read("config.ini")
if config['System'].getboolean('debug'):
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
client = BotBot(config)
for command in commands.commands:
if command not in config['System']['command_blacklist'].split(" "):
client.register_command(command, commands.commands[command])
for command in commands.pm_commands:
if command not in config['System']['pm_command_blacklist'].split(" "):
client.register_command(command, commands.pm_commands[command], type="pm")
for hook in hooks.hooks:
if hook.__module__.split(".")[1] not in config['System']['hook_blacklist'].split(" "):
client.register_hook(hook)
botbot_webhooks_app = webhooks.app
botbot_webhooks_app._ctx = client
http_server = HTTPServer(botbot_webhooks_app, io_loop=client.event_loop.io_loop)
http_server.listen(config['Webhooks']['port'], config['Webhooks']['host'])
client.connect(config['IRC']['host'], int(config['IRC']['port']), tls=config.getboolean('IRC', 'tls'))
client.handle_forever()