57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
import argparse
|
|
|
|
from twisted.words.protocols import irc
|
|
from twisted.internet import protocol, reactor
|
|
|
|
|
|
class IDRTBot(irc.IRCClient):
|
|
@property
|
|
def nickname(self):
|
|
return self.factory.nickname
|
|
|
|
def signedOn(self):
|
|
for c in self.factory.channels:
|
|
self.join(c)
|
|
print('signed on as {}.'.format(self.nickname))
|
|
|
|
def joined(self, channel):
|
|
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, '')
|
|
self.msg(channel, 'echo: {}'.format(msg))
|
|
|
|
|
|
class IDRTBotFactory(protocol.ClientFactory):
|
|
protocol = IDRTBot
|
|
|
|
def __init__(self, channels, nickname='idrt'):
|
|
self.channels = channels
|
|
self.nickname = nickname
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
print('lost connection({}), reconnecting.'.format(reason))
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
print('could not connect: {}'.format(reason))
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('-H', '--host', type=str, default='localhost')
|
|
parser.add_argument('-p', '--port', type=int, default=6667)
|
|
parser.add_argument('channels', nargs='+')
|
|
|
|
args = parser.parse_args()
|
|
|
|
reactor.connectTCP(
|
|
args.host, int(args.port),
|
|
IDRTBotFactory('#{}'.format(c) for c in args.channels)
|
|
)
|
|
reactor.run()
|