Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # twisted imports
- from twisted.words.protocols import irc
- from twisted.internet import reactor, protocol
- from twisted.python import log
- # system imports
- import time, sys, os, random
- def parse(irc, user, channel, command):
- args=command.split(" ")
- try:
- if (args[0] in publiccommands) or (user==irc.master):
- commands[args[0]](irc,user,channel,args[1:])
- else:
- commands["denied"](irc,user,channel,args)
- except KeyError, e:
- commands["unknown"](irc,user,channel,args)
- def say(irc, user, channel, args):
- chann=channel if args[0]=="" else args[0]
- irc.msg(chann,' '.join(args[1:]))
- def me(irc, user, channel, args):
- chann=channel if args[0]=="" else args[0]
- irc.me(chann,' '.join(args[1:]))
- def givemaster(irc, user, channel, args):
- irc.master=args[0]
- irc.broadcast(irc.master+" is my new master.")
- def getmaster(irc, user, channel, args):
- if ' '.join(args)==irc.password:
- irc.master=user
- irc.broadcast(irc.master+" is my new master.")
- else:
- irc.msg(channel, "You are not my true master!")
- def who(irc, user, channel, args):
- for chann in sauerserver.keys():
- irc.msg(sauerserver[chann][0],"who")
- time.sleep(2)
- for chann in sauerserver.keys():
- irc.msg(channel,sauerserverresponse[sauerserver[chann][0]])
- def growl(irc, channel, atwho):
- irc.me(channel, "growls fiercely at "+atwho),
- def howlrandomly(irc):
- if irc.joined_channels:
- howl(irc,random.choice(irc.joined_channels))
- reactor.callLater(random.randint(3600,20000),howlrandomly,irc)
- def howl(irc, channel):
- irc.me(channel, "howls at the moon.")
- irc.msg(channel, "A"*random.randint(1,10)+"a"*random.randint(5,10)+"u"*random.randint(5,10)+"U"*random.randint(1,10)+"!"*random.randint(1,3))
- def listcmd(irc, user, channel, args):
- public=0
- if(len(args)>0):
- if(args[0]=="public"):
- public=1
- if(user!=irc.master):
- public=1
- if public:
- irc.msg(channel,"Public commands: "+", ".join(list(publiccommands)))
- irc.msg(channel,"I will also unspec you if you're in MH and you say \"hyperwulf unspec YOURCN\" when you are on the servers")
- else:
- irc.msg(channel,"Master commands: "+", ".join(commands.keys()))
- commands = {
- "hi": lambda irc, user, channel, args: irc.msg(channel, "hi " + user),
- "brag": lambda irc, user, channel, args: irc.msg(channel, "I am an awesome python bot, and my master is " + irc.master),
- "good": lambda irc, user, channel, args: args[0].startswith("boy") and irc.msg(channel, "woof"),
- "howl" :lambda irc, user, channel, args: howl(irc,channel),
- "growl":lambda irc, user, channel, args: growl(irc,channel,args[0]),
- "givemaster": givemaster,
- "getmaster": getmaster,
- "sauer": lambda irc, user, channel, args: irc.msg(channel,"@"+' '.join(args)),
- "clearbans": lambda irc, user, channel, args: irc.msg(channel,"@clearbans"),
- "takemaster": lambda irc, user, channel, args: irc.msg(channel,"@takemaster"),
- "who": who,
- "say": say,
- "me": me,
- "list": listcmd,
- #"run": lambda irc, user, channel, args: irc.msg(channel, os.popen(' '.join(args)).read()),
- #"search": lambda irc, user, channel, args: irc.msg(channel, os.popen("ssl/main.py search "+' '.join(args)).read()),
- "part": lambda irc, user, channel, args: irc.part(channel),
- "join": lambda irc, user, channel, args: irc.join(args[0]),
- "quit": lambda irc, user, channel, args: reactor.stop(),
- "denied": lambda irc, user, channel, args: irc.msg(channel, "Sorry " + user + ", I cannot do that. You are not my master, but "+irc.master+" is."),
- "unknown": lambda irc, user, channel, args: irc.msg(channel, user + ": I am not sure what you mean by \""+' '.join(args)+"\".")
- }
- publiccommands = (
- "takemaster","hi","brag","unknown","growl","howl","list",
- "clearbans","takemaster","who"
- )
- sauerserver = {
- "#hypertriangle" : ["hypertriangle", "pass",""],
- "#hellserver" : ["spamela", "pass",""]
- }
- sauerserverresponse = {
- }
- class LogBot(irc.IRCClient):
- """A logging IRC bot."""
- nickname = "hyperwulf"
- master = "SirAlex"
- password = "pass"
- def connectionMade(self):
- irc.IRCClient.connectionMade(self)
- self.joined_channels = []
- def connectionLost(self, reason):
- irc.IRCClient.connectionLost(self, reason)
- # callbacks for events
- def signedOn(self):
- """Called when bot has succesfully signed on to server."""
- self.join(self.factory.channel)
- self.msg(self.factory.channel,self.master+": here I am!")
- reactor.callLater(200,howlrandomly,self)
- def joined(self, channel):
- if channel not in self.joined_channels:
- self.joined_channels.append(channel)
- if channel in sauerserver.keys():
- self.msg(sauerserver[channel][0],"login "+sauerserver[channel][1])
- else:
- self.msg(channel,"Hmm, no sauerbraten server in here that I can login into.")
- def userJoined(self, user, channel):
- if random.randint(0,5)==0:
- growl(self,channel,user)
- def left(self, channel):
- if channel in self.joined_channels:
- self.joined_channels.remove(channel)
- def broadcast(self, msg):
- for channel in self.joined_channels:
- self.say(channel, msg)
- def privmsg(self, user, channel, msg):
- """This will get called when the bot receives a message."""
- user = user.split('!', 1)[0]
- #devour ppl
- if msg.find("fail")!=-1:
- self.me(channel,"devours "+user)
- #ignore sauerbraten bots
- if user in [x[0] for x in sauerserver.values()]:
- if channel == self.nickname:
- sauerserverresponse[user]=user+": "+msg[3:]
- return
- print msg
- #sauer server warnings
- if msg.endswith("Mastermode is now private (3)."):
- growl(self,channel,"master from the server")
- self.msg(channel,"Please do not keep mastermode on private unless you have to.")
- self.msg(channel,"Ideally keep it in locked, or /mastermode 2")
- self.msg(channel,"I will tell "+self.master+" about this.")
- if msg.find("disconnected because:")!=-1:
- if msg.find("banned")!=-1 or msg.find("private")!=-1:
- growl(self,channel,"master from the server")
- self.msg(channel,"I do not like the sound of that")
- self.msg(channel,"Please make sure you are not in private mode and you didn't kick for no reason")
- self.msg(channel,"I will tell "+self.master+" about this.")
- if msg.find("Blacklist")!=-1:
- growl(self,channel,"the blacklisted person")
- self.msg(channel,"You are not welcome here! I will tell "+self.master+" about this.")
- #unspec MH ppl
- if msg.find("MH")!=-1:
- if msg.find("hyperwulf unspec ")!=-1:
- print msg[msg.find("<")+1:msg.find(">")]
- self.say(channel,"@spectator 0 "+msg[msg.rfind(" ")+1:])
- self.say(channel,"woof")
- return
- # Check to see if they're sending me a private message
- if channel == self.nickname:
- parse(self, user, user, msg);
- # Directed at me?
- if msg.startswith(self.nickname + ":"):
- parse(self, user, channel, msg[(len(self.nickname)+1):].lstrip())
- 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))\
- pass
- # 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]
- if old_nick==self.master:
- self.master=new_nick
- self.broadcast(self.master+" is still my master.")
- # 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.
- """
- # the class of the protocol to build when new connection is made
- protocol = LogBot
- def __init__(self):
- self.channel = "#hypertriangle"
- 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__':
- # create factory protocol and application
- f = LogBotFactory()
- # connect factory to this host and port
- reactor.connectTCP("irc.quakenet.org", 6667, f)
- #reactor.connectTCP("irc.gamesurge.net", 6667, f)
- #reactor.connectTCP("irc.freenode.net", 6667, f)
- # run bot
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement