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: 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)) reactor.connectTCP('localhost', 6667, IDRTBotFactory(['#smb', '#linux'])) reactor.run()