-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move stuff around, seperate main, commands, and Commander.
- Loading branch information
Showing
4 changed files
with
136 additions
and
106 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |