Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
irc: Move command parsing up to plugin level.
Browse files Browse the repository at this point in the history
  • Loading branch information
spladug committed Apr 10, 2015
1 parent bc93878 commit f5e5667
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions harold/plugins/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,8 @@ def connectionLost(self, *args, **kwargs):
self.factory.dispatcher.deregisterConsumer(self)

def privmsg(self, user, channel, msg):
split = msg.split()
if len(split) >= 2:
highlight = split[0].lower()
else:
highlight = ""

if not highlight.startswith(self.nickname):
return

command, args = (split[1].lower(), split[2:])
fn = self.plugin.commands.get(command)
if not fn:
return

sender_nick = user.partition('!')[0]

try:
fn(self, sender_nick, channel, *args)
except:
traceback.print_exc()
self.describe(channel, "just had a hiccup.")
self.factory.onMessageReceived(sender_nick, channel, msg)

def send_message(self, channel, message):
# get rid of any evil characters that might allow shenanigans
Expand Down Expand Up @@ -99,7 +80,6 @@ def buildProtocol(self, addr):
prot = protocol.ClientFactory.buildProtocol(self, addr)
prot.nickname = self.config.nick
prot.password = self.config.password
prot.plugin = self.plugin
prot.username = self.config.username
prot.userserv_password = self.config.userserv_password
return prot
Expand All @@ -110,6 +90,9 @@ def clientConnectionFailed(self, connector, reason):
def clientConnectionLost(self, connector, reason):
connector.connect()

def onMessageReceived(self, sender_nick, channel, msg):
self.plugin.onMessageReceived(sender_nick, channel, msg)


class MessageListener(ProtectedResource):
isLeaf = True
Expand Down Expand Up @@ -159,6 +142,27 @@ def __init__(self):
def register_command(self, handler):
self.commands[handler.__name__] = handler

def onMessageReceived(self, sender_nick, channel, msg):
split = msg.split()
if len(split) >= 2:
highlight = split[0].lower()
else:
highlight = ""

if not highlight.startswith(self.config.nick):
return

command, args = (split[1].lower(), split[2:])
fn = self.commands.get(command)
if not fn:
return

try:
fn(self.bot, sender_nick, channel, *args)
except:
traceback.print_exc()
self.bot.describe(channel, "just had a hiccup.")


def make_plugin(config, http=None):
irc_config = IrcConfig(config)
Expand Down

0 comments on commit f5e5667

Please sign in to comment.