Advertisement
Guest User

Untitled

a guest
May 16th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.55 KB | None | 0 0
  1. # twisted imports
  2. from twisted.words.protocols import irc
  3. from twisted.internet import reactor, protocol
  4. from twisted.python import log
  5.  
  6. # system imports
  7. import time, sys, os, random
  8.  
  9. def parse(irc, user, channel, command):
  10.     args=command.split(" ")
  11.     try:
  12.         if (args[0] in publiccommands) or (user==irc.master):
  13.             commands[args[0]](irc,user,channel,args[1:])
  14.         else:
  15.             commands["denied"](irc,user,channel,args)
  16.     except KeyError, e:
  17.         commands["unknown"](irc,user,channel,args)
  18.    
  19. def say(irc, user, channel, args):
  20.     chann=channel if args[0]=="" else args[0]
  21.     irc.msg(chann,' '.join(args[1:]))
  22.  
  23. def me(irc, user, channel, args):
  24.     chann=channel if args[0]=="" else args[0]
  25.     irc.me(chann,' '.join(args[1:]))
  26.  
  27. def givemaster(irc, user, channel, args):
  28.     irc.master=args[0]
  29.     irc.broadcast(irc.master+" is my new master.")
  30.  
  31. def getmaster(irc, user, channel, args):
  32.     if ' '.join(args)==irc.password:
  33.         irc.master=user
  34.         irc.broadcast(irc.master+" is my new master.")
  35.     else:
  36.         irc.msg(channel, "You are not my true master!")
  37.  
  38. def who(irc, user, channel, args):
  39.     for chann in sauerserver.keys():
  40.         irc.msg(sauerserver[chann][0],"who")
  41.     time.sleep(2)
  42.     for chann in sauerserver.keys():
  43.         irc.msg(channel,sauerserverresponse[sauerserver[chann][0]])
  44.  
  45. def growl(irc, channel, atwho):
  46.     irc.me(channel, "growls fiercely at "+atwho),
  47.  
  48. def howlrandomly(irc):
  49.     if irc.joined_channels:
  50.         howl(irc,random.choice(irc.joined_channels))
  51.     reactor.callLater(random.randint(3600,20000),howlrandomly,irc)
  52.  
  53. def howl(irc, channel):
  54.     irc.me(channel, "howls at the moon.")
  55.     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))
  56.  
  57. def listcmd(irc, user, channel, args):
  58.     public=0
  59.    
  60.     if(len(args)>0):
  61.         if(args[0]=="public"):
  62.             public=1
  63.    
  64.     if(user!=irc.master):
  65.         public=1
  66.    
  67.     if public:
  68.         irc.msg(channel,"Public commands: "+", ".join(list(publiccommands)))
  69.         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")
  70.     else:
  71.         irc.msg(channel,"Master commands: "+", ".join(commands.keys()))
  72.  
  73. commands = {
  74.     "hi":   lambda irc, user, channel, args: irc.msg(channel, "hi " + user),
  75.     "brag": lambda irc, user, channel, args: irc.msg(channel, "I am an awesome python bot, and my master is " + irc.master),
  76.     "good": lambda irc, user, channel, args: args[0].startswith("boy") and irc.msg(channel, "woof"),
  77.    
  78.     "howl" :lambda irc, user, channel, args: howl(irc,channel),
  79.     "growl":lambda irc, user, channel, args: growl(irc,channel,args[0]),
  80.    
  81.     "givemaster": givemaster,
  82.     "getmaster":  getmaster,
  83.    
  84.     "sauer":      lambda irc, user, channel, args: irc.msg(channel,"@"+' '.join(args)),
  85.     "clearbans":  lambda irc, user, channel, args: irc.msg(channel,"@clearbans"),
  86.     "takemaster": lambda irc, user, channel, args: irc.msg(channel,"@takemaster"),
  87.     "who":        who,
  88.    
  89.     "say":  say,
  90.     "me":   me,
  91.    
  92.     "list": listcmd,
  93.    
  94.     #"run":    lambda irc, user, channel, args: irc.msg(channel, os.popen(' '.join(args)).read()),
  95.     #"search": lambda irc, user, channel, args: irc.msg(channel, os.popen("ssl/main.py search "+' '.join(args)).read()),
  96.    
  97.     "part": lambda irc, user, channel, args: irc.part(channel),
  98.     "join": lambda irc, user, channel, args: irc.join(args[0]),
  99.     "quit": lambda irc, user, channel, args: reactor.stop(),
  100.        
  101.     "denied":  lambda irc, user, channel, args: irc.msg(channel, "Sorry " + user + ", I cannot do that. You are not my master, but "+irc.master+" is."),
  102.     "unknown": lambda irc, user, channel, args: irc.msg(channel, user + ": I am not sure what you mean by \""+' '.join(args)+"\".")
  103. }
  104.  
  105. publiccommands = (
  106.     "takemaster","hi","brag","unknown","growl","howl","list",
  107.     "clearbans","takemaster","who"
  108. )
  109.  
  110. sauerserver = {
  111.     "#hypertriangle" : ["hypertriangle", "pass",""],
  112.     "#hellserver" : ["spamela", "pass",""]
  113. }
  114.  
  115. sauerserverresponse = {
  116. }
  117.  
  118. class LogBot(irc.IRCClient):
  119.     """A logging IRC bot."""
  120.  
  121.     nickname = "hyperwulf"
  122.     master = "SirAlex"
  123.     password = "pass"
  124.  
  125.     def connectionMade(self):
  126.         irc.IRCClient.connectionMade(self)
  127.         self.joined_channels = []
  128.  
  129.     def connectionLost(self, reason):
  130.         irc.IRCClient.connectionLost(self, reason)
  131.  
  132.  
  133.     # callbacks for events
  134.  
  135.     def signedOn(self):
  136.         """Called when bot has succesfully signed on to server."""
  137.         self.join(self.factory.channel)
  138.         self.msg(self.factory.channel,self.master+": here I am!")
  139.         reactor.callLater(200,howlrandomly,self)
  140.  
  141.     def joined(self, channel):
  142.         if channel not in self.joined_channels:
  143.             self.joined_channels.append(channel)
  144.        
  145.         if channel in sauerserver.keys():
  146.             self.msg(sauerserver[channel][0],"login "+sauerserver[channel][1])
  147.         else:
  148.             self.msg(channel,"Hmm, no sauerbraten server in here that I can login into.")
  149.    
  150.     def userJoined(self, user, channel):
  151.         if random.randint(0,5)==0:
  152.             growl(self,channel,user)
  153.    
  154.     def left(self, channel):
  155.         if channel in self.joined_channels:
  156.             self.joined_channels.remove(channel)
  157.  
  158.     def broadcast(self, msg):
  159.         for channel in self.joined_channels:
  160.             self.say(channel, msg)
  161.  
  162.     def privmsg(self, user, channel, msg):
  163.         """This will get called when the bot receives a message."""
  164.         user = user.split('!', 1)[0]
  165.        
  166.         #devour ppl
  167.         if msg.find("fail")!=-1:
  168.             self.me(channel,"devours "+user)
  169.        
  170.         #ignore sauerbraten bots
  171.         if user in [x[0] for x in sauerserver.values()]:
  172.             if channel == self.nickname:
  173.                 sauerserverresponse[user]=user+": "+msg[3:]
  174.                 return
  175.             print msg
  176.            
  177.             #sauer server warnings
  178.             if msg.endswith("Mastermode is now private (3)."):
  179.                 growl(self,channel,"master from the server")
  180.                 self.msg(channel,"Please do not keep mastermode on private unless you have to.")
  181.                 self.msg(channel,"Ideally keep it in locked, or /mastermode 2")
  182.                 self.msg(channel,"I will tell "+self.master+" about this.")
  183.            
  184.             if msg.find("disconnected because:")!=-1:
  185.                 if msg.find("banned")!=-1 or msg.find("private")!=-1:
  186.                     growl(self,channel,"master from the server")
  187.                     self.msg(channel,"I do not like the sound of that")
  188.                     self.msg(channel,"Please make sure you are not in private mode and you didn't kick for no reason")
  189.                     self.msg(channel,"I will tell "+self.master+" about this.")
  190.            
  191.             if msg.find("Blacklist")!=-1:
  192.                 growl(self,channel,"the blacklisted person")
  193.                 self.msg(channel,"You are not welcome here! I will tell "+self.master+" about this.")
  194.            
  195.             #unspec MH ppl
  196.             if msg.find("MH")!=-1:
  197.                 if msg.find("hyperwulf unspec ")!=-1:
  198.                     print msg[msg.find("<")+1:msg.find(">")]
  199.                     self.say(channel,"@spectator 0 "+msg[msg.rfind(" ")+1:])
  200.                     self.say(channel,"woof")
  201.                
  202.             return
  203.        
  204.         # Check to see if they're sending me a private message
  205.         if channel == self.nickname:
  206.             parse(self, user, user, msg);
  207.        
  208.         # Directed at me?
  209.         if msg.startswith(self.nickname + ":"):
  210.             parse(self, user, channel, msg[(len(self.nickname)+1):].lstrip())
  211.  
  212.     def action(self, user, channel, msg):
  213.         """This will get called when the bot sees someone do an action."""
  214.         #user = user.split('!', 1)[0]
  215.         #self.logger.log("* %s %s" % (user, msg))\
  216.         pass
  217.  
  218.     # irc callbacks
  219.  
  220.     def irc_NICK(self, prefix, params):
  221.         """Called when an IRC user changes their nickname."""
  222.         old_nick = prefix.split('!')[0]
  223.         new_nick = params[0]
  224.         if old_nick==self.master:
  225.             self.master=new_nick
  226.             self.broadcast(self.master+" is still my master.")
  227.  
  228.  
  229.     # For fun, override the method that determines how a nickname is changed on
  230.     # collisions. The default method appends an underscore.
  231.     def alterCollidedNick(self, nickname):
  232.         """
  233.         Generate an altered version of a nickname that caused a collision in an
  234.         effort to create an unused related name for subsequent registration.
  235.         """
  236.         return nickname + '_'
  237.  
  238.  
  239.  
  240. class LogBotFactory(protocol.ClientFactory):
  241.     """A factory for LogBots.
  242.  
  243.     A new protocol instance will be created each time we connect to the server.
  244.     """
  245.  
  246.     # the class of the protocol to build when new connection is made
  247.     protocol = LogBot
  248.  
  249.     def __init__(self):
  250.         self.channel = "#hypertriangle"
  251.  
  252.     def clientConnectionLost(self, connector, reason):
  253.         """If we get disconnected, reconnect to server."""
  254.         connector.connect()
  255.  
  256.     def clientConnectionFailed(self, connector, reason):
  257.         print "connection failed:", reason
  258.         reactor.stop()
  259.  
  260.  
  261. if __name__ == '__main__':
  262.     # create factory protocol and application
  263.     f = LogBotFactory()
  264.  
  265.     # connect factory to this host and port
  266.     reactor.connectTCP("irc.quakenet.org", 6667, f)
  267.     #reactor.connectTCP("irc.gamesurge.net", 6667, f)
  268.     #reactor.connectTCP("irc.freenode.net", 6667, f)
  269.    
  270.     # run bot
  271.     reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement