add enable/disable (#12) and enhanced help output

pull/74/head
Mallory Hancock 2019-09-18 09:27:41 -07:00
parent 0da04acc4e
commit 1aa17eebfb
1 changed files with 69 additions and 47 deletions

View File

@ -1,10 +1,11 @@
from collections import OrderedDict
from datetime import datetime, timezone from datetime import datetime, timezone
import imp import imp
import logging import logging
import os import os
import ssl import ssl
import time import time
import pinhook.plugin from . import plugin
import irc.bot import irc.bot
@ -32,13 +33,16 @@ class Bot(irc.bot.SingleServerIRCBot):
self.chanlist = channels self.chanlist = channels
self.bot_nick = nickname self.bot_nick = nickname
self.start_logging(self.log_level) self.start_logging(self.log_level)
self.output_message = pinhook.plugin.message self.output_message = plugin.message
self.output_action = pinhook.plugin.action self.output_action = plugin.action
self.internal_commands = { self.internal_commands = {
self.cmd_prefix + 'join': 'join a channel', 'join': 'join a channel',
self.cmd_prefix + 'quit': 'force the bot to quit', 'quit': 'force the bot to quit',
self.cmd_prefix + 'reload': 'force bot to reload all plugins' 'reload': 'force bot to reload all plugins',
'enable': 'enable a plugin',
'disable': 'disable a plugin'
} }
self.internal_commands = {self.cmd_prefix + k: v for k,v in self.internal_commands.items()}
self.load_plugins() self.load_plugins()
class Message: class Message:
@ -93,7 +97,7 @@ class Bot(irc.bot.SingleServerIRCBot):
def load_plugins(self): def load_plugins(self):
# clear plugin list to ensure no old plugins remain # clear plugin list to ensure no old plugins remain
self.logger.info('clearing plugin cache') self.logger.info('clearing plugin cache')
pinhook.plugin.clear_plugins() plugin.clear_plugins()
# ensure plugin folder exists # ensure plugin folder exists
self.logger.info('checking plugin directory') self.logger.info('checking plugin directory')
if not os.path.exists(self.plugin_dir): if not os.path.exists(self.plugin_dir):
@ -111,10 +115,10 @@ class Bot(irc.bot.SingleServerIRCBot):
self.logger.exception('could not load plugin') self.logger.exception('could not load plugin')
# gather all commands and listeners # gather all commands and listeners
if self.use_prefix_for_plugins: # use prefixes if needed if self.use_prefix_for_plugins: # use prefixes if needed
pinhook.plugin.cmds = {self.cmd_prefix + k: v for k,v in pinhook.plugin.cmds.items()} plugin.cmds = {self.cmd_prefix + k: v for k,v in plugin.cmds.items()}
for cmd in pinhook.plugin.cmds: for cmd in plugin.cmds:
self.logger.debug('adding command {}'.format(cmd)) self.logger.debug('adding command {}'.format(cmd))
for lstnr in pinhook.plugin.lstnrs: for lstnr in plugin.lstnrs:
self.logger.debug('adding listener {}'.format(lstnr)) self.logger.debug('adding listener {}'.format(lstnr))
def on_welcome(self, c, e): def on_welcome(self, c, e):
@ -134,15 +138,16 @@ class Bot(irc.bot.SingleServerIRCBot):
def on_action(self, c, e): def on_action(self, c, e):
self.process_event(c, e) self.process_event(c, e)
def call_help(self, op): def call_help(self, nick, op):
cmds = [i for i in pinhook.plugin.cmds if not pinhook.plugin.cmds[i].ops] cmds = {k:v.help_text for k,v in plugin.cmds.items() if not plugin.cmds[k].ops}
cmds.append(self.cmd_prefix + 'help') cmds.update({self.cmd_prefix + 'help': 'returns this output to private message'})
if op: if op:
cmds += [i for i in pinhook.plugin.cmds if pinhook.plugin.cmds[i].ops] cmds.update({k:v.help_text for k,v in plugin.cmds.items() if plugin.cmds[k].ops})
cmds += [i for i in self.internal_commands] cmds.update({k:v for k,v in self.internal_commands.items()})
helplist = sorted(cmds) helpout = OrderedDict(sorted(cmds.items()))
msg = ', '.join(helplist) for h in helpout:
return self.output_message('Available commands: {}'.format(msg)) self.connection.privmsg(nick, '{} -- {}'.format(h, helpout[h]))
return None
def call_internal_commands(self, channel, nick, cmd, text, arg, c): def call_internal_commands(self, channel, nick, cmd, text, arg, c):
output = None output = None
@ -159,23 +164,39 @@ class Bot(irc.bot.SingleServerIRCBot):
c.quit("See y'all later!") c.quit("See y'all later!")
quit() quit()
elif cmd == self.cmd_prefix + 'help': elif cmd == self.cmd_prefix + 'help':
output = self.call_help(op) self.call_help(nick, op)
elif cmd == self.cmd_prefix + 'reload' and op: elif cmd == self.cmd_prefix + 'reload' and op:
self.logger.info('reloading plugins per request of {}'.format(nick)) self.logger.info('reloading plugins per request of {}'.format(nick))
self.load_plugins() self.load_plugins()
output = self.output_message('Plugins reloaded') output = self.output_message('Plugins reloaded')
elif cmd == self.cmd_prefix + 'enable' and op:
if arg in plugin.plugins:
if plugin.plugins[arg].enabled:
output = self.output_message("{}: '{}' already enabled".format(nick, arg))
else:
plugin.plugins[arg].enable()
output = self.output_message("{}: '{}' enabled!".format(nick, arg))
else:
output = self.output_message("{}: '{}' not found".format(nick, arg))
elif cmd == self.cmd_prefix + 'disable' and op:
if arg in plugin.plugins:
if not plugin.plugins[arg].enabled:
output = self.output_message("{}: '{}' already disabled".format(nick, arg))
else:
plugin.plugins[arg].disable()
output = self.output_message("{}: '{}' disabled!".format(nick, arg))
return output return output
def call_plugins(self, privmsg, action, notice, chan, cmd, text, nick_list, nick, arg): def call_plugins(self, privmsg, action, notice, chan, cmd, text, nick_list, nick, arg):
output = None output = None
if cmd in pinhook.plugin.cmds: if cmd in plugin.cmds:
try: try:
if pinhook.plugin.cmds[cmd].ops and nick not in self.ops: if plugin.cmds[cmd].ops and nick not in self.ops:
if pinhook.plugin.cmds[cmd].ops_msg: if plugin.cmds[cmd].ops_msg:
output = self.output_message(pinhook.plugin.cmds[cmd].ops_msg) output = self.output_message(plugin.cmds[cmd].ops_msg)
else: elif plugin.cmds[cmd].enabled:
self.logger.debug('executing {}'.format(cmd)) self.logger.debug('executing {}'.format(cmd))
output = pinhook.plugin.cmds[cmd].run(self.Message( output = plugin.cmds[cmd].run(self.Message(
bot=self, bot=self,
channel=chan, channel=chan,
cmd=cmd, cmd=cmd,
@ -192,10 +213,11 @@ class Bot(irc.bot.SingleServerIRCBot):
except Exception: except Exception:
self.logger.exception('issue with command {}'.format(cmd)) self.logger.exception('issue with command {}'.format(cmd))
else: else:
for lstnr in pinhook.plugin.lstnrs: for lstnr in plugin.lstnrs:
if plugin.lstnrs[lstnr].enabled:
try: try:
self.logger.debug('whispering to listener: {}'.format(lstnr)) self.logger.debug('whispering to listener: {}'.format(lstnr))
listen_output = pinhook.plugin.lstnrs[lstnr](self.Message( listen_output = plugin.lstnrs[lstnr].run(self.Message(
bot=self, bot=self,
channel=chan, channel=chan,
text=text, text=text,
@ -258,14 +280,14 @@ class Bot(irc.bot.SingleServerIRCBot):
if not output.msg: if not output.msg:
return return
for msg in output.msg: for msg in output.msg:
if output.msg_type == pinhook.plugin.OutputType.Message: if output.msg_type == plugin.OutputType.Message:
self.logger.debug('output message: {}'.format(msg)) self.logger.debug('output message: {}'.format(msg))
try: try:
c.privmsg(chan, msg) c.privmsg(chan, msg)
except irc.client.MessageTooLong: except irc.client.MessageTooLong:
self.logger.error('output message too long: {}'.format(msg)) self.logger.error('output message too long: {}'.format(msg))
break break
elif output.msg_type == pinhook.plugin.OutputType.Action: elif output.msg_type == plugin.OutputType.Action:
self.logger.debug('output action: {}'.format(msg)) self.logger.debug('output action: {}'.format(msg))
try: try:
c.action(chan, msg) c.action(chan, msg)