Skip to content

Commit

Permalink
Move stuff around, seperate main, commands, and Commander.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon committed Jul 15, 2011
1 parent 80464a3 commit 1d94c1f
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 106 deletions.
106 changes: 0 additions & 106 deletions hamper/Commander.py

This file was deleted.

72 changes: 72 additions & 0 deletions hamper/commander.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sys
import re

from twisted.words.protocols import irc
from twisted.internet import protocol, reactor

class Commander(irc.IRCClient):

commands = []

def _get_nickname(self):
return self.factory.nickname

nickname = property(_get_nickname)

def signedOn(self):
self.join(self.factory.channel)
print "Signed on as %s." % (self.nickname,)

def joined(self, channel):
print "Joined %s." % (channel,)

def privmsg(self, user, channel, msg):
"""On message received (from channel or user)."""

if not user:
# Ignore server messages
return

directed = msg.startswith(self.nickname)
# This monster of a regex extracts msg and target from a message, where
# the target may not be there.
target, msg = re.match(
r'^(?:([a-z_\-\[\]\\^{}|`][a-z0-9_\-\[\]\\^{}|`]*)[:,] )? *(.*)$',
msg).groups()

print user, target, msg

for cmd in Commander.commands:
if cmd.regex.match(msg):
if directed or (not cmd.onlyDirected):
cmd(self, user, target, msg)

def connectionLost(self, reason):
reactor.stop()

def say(self, msg):
self.msg(self.factory.channel, msg)


class CommanderFactory(protocol.ClientFactory):

protocol = Commander

def __init__(self, channel, nickname='Hamper'):
self.channel = channel
self.nickname = nickname

def clientConnectionLost(self, connector, reason):
print "Lost connection (%s)." % (reason)

def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)


def registerCommand(Command):
"""Register a command with the Commander. To be used as a decorator."""
options = re.I if not Command.caseSensitive else None
Command.regex = re.compile(Command.regex, options)

Commander.commands.append(Command())
print 'registered', Command
41 changes: 41 additions & 0 deletions hamper/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from hamper.commander import registerCommand


class Command(object):
"""Base class for a simple command."""

onlyDirected = True
caseSensitive = False
regex = ''

def __call__(self, commander, sender, target, message):
pass


@registerCommand
class FriendlyCommand(Command):

regex = 'hi'

def __call__(self, commander, sender, target, message):
commander.say('Hello {0}'.format(sender))


@registerCommand
class QuitCommand(Command):

regex = 'go away'

def __call__(self, commander, sender, target, message):
commander.say('Bye!')
commander.quit()


@registerCommand
class OmgPonies(Command):

regex = r'.*pon(y|ies).*'
onlyDirected = False

def __call__(self, commander, sender, target, message):
commander.say('OMG PONIES!!!')
23 changes: 23 additions & 0 deletions hamper/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys

from twisted.words.protocols import irc
from twisted.internet import protocol, reactor

from hamper import commands
from hamper.commander import CommanderFactory


if __name__ == '__main__':
if len(sys.argv) > 1:
chan = sys.argv
else:
chan = 'hamper'

if len(sys.argv) > 2:
nickname = sys.argv[2]
else:
nickname = 'hamper'

reactor.connectTCP('irc.freenode.net', 6667,
CommanderFactory('#' + chan, nickname))
reactor.run()

0 comments on commit 1d94c1f

Please sign in to comment.