expand kwargs for initializing bot. add ssl and nickserv identify support

pull/1/head
Mallory Hancock 2017-10-10 11:56:10 -07:00
parent 27aa7a2e06
commit 285e580643
1 changed files with 28 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import imp import imp
import os import os
import ssl
import irc.bot import irc.bot
@ -16,18 +17,22 @@ class Message:
class Bot(irc.bot.SingleServerIRCBot): class Bot(irc.bot.SingleServerIRCBot):
def __init__(self, channels, nickname, server, port=6667, ops=[], plugin_dir='plugins'): def __init__(self, channels, nickname, server, **kwargs):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) self.set_kwargs(**kwargs)
if self.ssl_required:
factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
irc.bot.SingleServerIRCBot.__init__(self, [(server, self.port)], nickname, nickname, connect_factory=factory)
else:
irc.bot.SingleServerIRCBot.__init__(self, [(server, self.port)], nickname, nickname)
self.chanlist = channels self.chanlist = channels
self.bot_nick = nickname self.bot_nick = nickname
self.ops = ops
# load all plugins # load all plugins
plugins = [] plugins = []
for m in os.listdir(plugin_dir): for m in os.listdir(self.plugin_dir):
if m.endswith('.py'): if m.endswith('.py'):
name = m[:-3] name = m[:-3]
fp, pathname, description = imp.find_module(name, [plugin_dir]) fp, pathname, description = imp.find_module(name, [self.plugin_dir])
plugins.append(imp.load_module(name, fp, pathname, description)) plugins.append(imp.load_module(name, fp, pathname, description))
# gather all commands # gather all commands
@ -36,7 +41,25 @@ class Bot(irc.bot.SingleServerIRCBot):
for cmd in plugin.pinhook.plugin.cmds: for cmd in plugin.pinhook.plugin.cmds:
self.cmds[cmd['cmd']] = cmd['func'] self.cmds[cmd['cmd']] = cmd['func']
def set_kwargs(self, **kwargs):
kwarguments = {
'port': 6667,
'ops': [],
'plugin_dir': 'plugins',
'ssl_required': False,
'ns_pass': None,
'nickserv': 'NickServ',
}
for k, v in kwargs.items():
setattr(self, k, v)
for a in kwarguments:
if a not in kwargs:
setattr(self, a, kwarguments[a])
def on_welcome(self, c, e): def on_welcome(self, c, e):
if self.ns_pass:
c.privmsg(self.nickserv, 'identify {}'.format(self.ns_pass))
for channel in self.chanlist: for channel in self.chanlist:
c.join(channel) c.join(channel)