Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. import socket, ssl, base64, sys
  2.  
  3. # Fill in the values needed. Start with: bad-robot:~ Dennis$ python bot.py
  4. server = "irc.freenode.org"         # Server
  5. port = 6669                             # Port
  6. usessl = True                           # SSL
  7. channel = "#verbose"                    # Channel
  8.  
  9. botnick = "Bad-Robot"                   # Bot nick
  10. username = "yourusername"               # IRC Server Auth Username
  11. password = "yourpassword"               # IRC Server Auth Password, base64
  12. quitmsg = "GRRRRR FINE, I'LL LEAVE!"    # IRC Leave message
  13. prefix = "."                            # Prefix for commands, using a "." makes ".help", using "bot " makes "bot help"
  14. owner = "FLX"                       # Used in some commands to describe the bot owner
  15. botowner = "flx"                        # This is the username it responds to on admin commands
  16.  
  17. def joinchan(chan): # This function is used to join channels.
  18.     print "Connecting to " + chan
  19.     ircsock.send("JOIN %s\n" % (chan))
  20.     ircsock.send("PRIVMSG %s :%s reporting!\n" % (channel,botnick))
  21.     print "Connected to "+channel+"!"
  22.  
  23. # Bot commands
  24. def commands(nick,user,channel,message):
  25.     # Says that it"s awesome
  26.     if message.find(prefix+"awesome")!=-1:
  27.         ircsock.send("PRIVMSG %s :%s: this is awesome!\r\n" % (channel,nick))
  28.    
  29.     # Help command, you can use it to list other commands
  30.     elif message.find(prefix+"help")!=-1:
  31.         ircsock.send("PRIVMSG %s :%s: My other command is awesome.\r\n" % (channel,nick))
  32.    
  33.     # Check if user is considered an admin 
  34.     elif message.find(prefix+"admin")!=-1:
  35.         if user==botowner:
  36.             ircsock.send("PRIVMSG %s :hey %s, you're my owner!\r\n" % (channel,nick))
  37.         else:
  38.             ircsock.send("PRIVMSG %s :%s: you're not my daddy!\r\n" % (channel,nick))
  39.    
  40.     # When someone mentions bad robot, it will respond     
  41.     elif message.find("bad robot")!=-1:
  42.         ircsock.send("PRIVMSG %s :%s: I'm %s, an irc bot. My owner is %s\r\n" % (channel,nick,botnick,owner))
  43.    
  44.     # Responds to hey BOTNICK, example: hey Bad-Robot  
  45.     elif message.find(":hey "+ botnick)!=-1:
  46.         ircsock.send("PRIVMSG %s :hey %s, what's up?\r\n" % (channel,nick))
  47.    
  48. # Create connection
  49. if usessl:
  50.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket
  51.     s.connect((server, port)) # Connect to the server
  52.     ircsock = ssl.wrap_socket(s) # Wrap existing socket in ssl
  53. else:
  54.     ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket
  55.     ircsock.connect((server, port)) # Connect to the server
  56.    
  57. # Start Login Process
  58. passwordDecrypted = base64.decodestring(password) # Decode password from base64
  59. ircsock.send("PASS %s\n" % (passwordDecrypted)) # Supply password
  60. ircsock.send("NICK %s\n" % (botnick)) # Here we actually assign the nick to the bot
  61. ircsock.send("USER %s %s %s :%s\n" % (username,username,server,botnick)) # User authentication
  62.  
  63. while 1: # Loop loop loop!
  64.     ircmsg = ircsock.recv(2048) # Receive data from server
  65.     ircmsg = ircmsg.strip("\n\r") # Removing unnecessary linebreaks.
  66.     print(ircmsg) # Print what"s coming from the server
  67.  
  68.     # Create command handler
  69.     if ircmsg.find(" PRIVMSG ")!=-1: # This needs to be done more secure with a split that looks at the full msg
  70.         nick=ircmsg.split("!")[0][1:]
  71.         channel=ircmsg.split(" PRIVMSG ")[-1].split(" :")[0]
  72.         user=ircmsg.split("!")[-1].split("@")[0]
  73.         commands(nick,user,channel,ircmsg)
  74.  
  75.     if ircmsg.find(prefix+"leave") != -1: # Look for leave and exit bot
  76.         user=ircmsg.split("!")[-1].split("@")[0] # Grab username (not nick) of user asking to exit bot
  77.         if user==botowner: # See if user has enough rights to quit bot
  78.             ircsock.send("QUIT :%s\n" % (quitmsg)) # Disconnect from IRC
  79.             ircsock.close() # Close irc socket
  80.             if usessl:
  81.                 s.close() # Close unencrypted irc socket
  82.             break # Exit loop, thus exiting the program
  83.         else:
  84.             ircsock.send("PRIVMSG %s :%s: you can't shut me down!\r\n" % (channel,nick))
  85.    
  86.     if ircmsg.find("001 "+botnick) != -1: # Join channel when welcome message prints
  87.         joinchan(channel)
  88.  
  89.     if ircmsg.find("PING :") != -1: # If the server pings us then we"ve got to respond!
  90.         ircsock.send("PONG :pingis\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement