anonymized the classes

This commit is contained in:
Stephen McQuay 2013-02-23 17:44:57 -08:00
parent 637a56da8c
commit 533f10a841

20
tbot.py
View File

@ -1,10 +1,11 @@
import argparse
import re
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor
class IDRTBot(irc.IRCClient):
class IRCBot(irc.IRCClient):
@property
def nickname(self):
return self.factory.nickname
@ -18,22 +19,21 @@ class IDRTBot(irc.IRCClient):
print('joined {}'.format(channel))
def privmsg(self, user, channel, msg):
print('user: {} | channel: {} | msg: {}'.format(user, channel, msg))
username = user.split('!', 1)[0]
if channel == self.nickname:
print('got private message')
self.msg(username, "whisper back ...")
if self.nickname in msg:
msg = msg.replace(self.nickname, '')
msg = self.factory.nick_stripper.sub('', msg)
self.msg(channel, 'echo: {}'.format(msg))
class IDRTBotFactory(protocol.ClientFactory):
protocol = IDRTBot
class IRCBotFactory(protocol.ClientFactory):
protocol = IRCBot
def __init__(self, channels, nickname='idrt'):
def __init__(self, channels, nickname='bot'):
self.channels = channels
self.nickname = nickname
self.nick_stripper = re.compile(r'{}\s*?\:?'.format(self.nickname))
def clientConnectionLost(self, connector, reason):
print('lost connection({}), reconnecting.'.format(reason))
@ -45,12 +45,16 @@ class IDRTBotFactory(protocol.ClientFactory):
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host', type=str, default='localhost')
parser.add_argument('-p', '--port', type=int, default=6667)
parser.add_argument('-n', '--nick', type=str, default='bot')
parser.add_argument('channels', nargs='+')
args = parser.parse_args()
reactor.connectTCP(
args.host, int(args.port),
IDRTBotFactory('#{}'.format(c) for c in args.channels)
IRCBotFactory(
('#{}'.format(c) for c in args.channels),
nickname=args.nick
)
)
reactor.run()