# Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ An example IRC log bot - logs a channel's events to a file. If someone says the bot's name in the channel followed by a ':', e.g. logbot: hello! the bot will reply: foo: I am a log bot Run this script with two arguments, the channel name the bot should connect to, and file to log to, e.g.: $ python ircLogBot.py test test.log will log channel #test to the file 'test.log'. """ # twisted imports from twisted.words.protocols import irc from twisted.internet import reactor, protocol from twisted.python import log # system imports import time, sys class LogBot(irc.IRCClient): """A logging IRC bot.""" nickname = "twistbot" def __init(self): self.j = 0 self.listChannels = [] # print(listChannels) def connectionMade(self): irc.IRCClient.connectionMade(self) self.j = 0 self.listChannels = ["#testing", # one channel only "#testing,#test", #two channels, separated by a comma "#testing, #test" #two channel, separated by a comma and a space ] print(self.listChannels) print("[connected at %s]" % time.asctime(time.localtime(time.time()))) def channelListStart(self, params): print "%s" % (params) def gotChannelList(self, channel, usersCount, topic): print "%s\t%d\t%s" % (channel, usersCount, topic) def channelListEnd(self, params): print "%s" % (params) def connectionLost(self, reason): irc.IRCClient.connectionLost(self, reason) print("[disconnected at %s]" % (time.asctime(time.localtime(time.time())))) def signedOn(self): """ Called when bot has succesfully signed on to server. """ self.join(self.factory.channel) def joined(self, channel): """ This will get called when the bot joins the channel. """ # self.logger.log("[I have joined %s]" % channel) print("[I have joined %s]" % channel) def privmsg(self, user, channel, msg): """ This will get called when the bot receives a message. """ user = user.split('!', 1)[0] # self.logger.log("<%s> %s" % (user, msg)) # print("<%s> %s" % (user, msg)) # Check to see if they're sending me a private message if channel == self.nickname: msg = "It isn't nice to whisper! Play nice with the group." self.msg(user, msg) return # Otherwise check to see if it is a message directed at me if msg.startswith(self.nickname + ":"): msg = "%s: I am a log bot" % user self.msg(channel, msg) # self.logger.log("<%s> %s" % (self.nickname, msg)) # print("<%s> %s" % (self.nickname, msg)) def action(self, user, channel, msg): """ This will get called when the bot sees someone do an action. """ user = user.split('!', 1)[0] # self.logger.log("* %s %s" % (user, msg)) print("* %s %s" % (user, msg)) print("I've got an action message, now I'll send LIST to server") #sending a channel list print(self.listChannels[self.j]) self.channelList(self.listChannels[self.j]) self.j += 1 # irc callbacks def irc_NICK(self, prefix, params): """ Called when an IRC user changes their nickname. """ old_nick = prefix.split('!')[0] new_nick = params[0] # self.logger.log("%s is now known as %s" % (old_nick, new_nick)) print("%s is now known as %s" % (old_nick, new_nick)) def irc_unknown(self, prefix, command, params): print "In irc_unknown:" print "prefix : %s" % ( prefix ) print "command : %s" % ( command ) print "params : %s" % ( params ) # For fun, override the method that determines how a nickname is changed on # collisions. The default method appends an underscore. def alterCollidedNick(self, nickname): """ Generate an altered version of a nickname that caused a collision in an effort to create an unused related name for subsequent registration. """ return nickname + '^' class LogBotFactory(protocol.ClientFactory): """ A factory for LogBots. A new protocol instance will be created each time we connect to the server. """ def __init__(self, channel): self.channel = channel def buildProtocol(self, addr): p = LogBot() p.factory = self return p def clientConnectionLost(self, connector, reason): """If we get disconnected, reconnect to server.""" connector.connect() def clientConnectionFailed(self, connector, reason): print("connection failed:", reason) reactor.stop() if __name__ == '__main__': # initialize logging # log.startLogging(sys.stdout) # create factory protocol and application f = LogBotFactory("test") # connect factory to this host and port reactor.connectTCP("irc.dal.net", 6667, f) # run bot reactor.run()