Advertisement
Guest User

python bot

a guest
Apr 7th, 2013
1,702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. import socket, ssl, sys, urllib2, urllib
  2. import time, random, os
  3. from time import clock
  4.  
  5. irc = socket.socket()
  6.  
  7. sslEnabled = True #Change to false if you don't want to use SSL
  8. if sslEnabled == True:
  9.         irc = ssl.wrap_socket(irc)
  10.    
  11. ircServer = "server" #IRC Server your bots are going to be connecting to
  12. ircChannel = "channel" #IRC Channel your bots are going to be connecting to
  13. ircChannelPassword = "channelpassword" #Leave blank if no password
  14. ircSSLPort = 6697 #If you're using SSL (enabled by default) this is the port it connects to
  15. ircPort = 6667 #If you're using raw connection, (not enabled by default) this is the port it uses
  16. ircNick = "nickname" + str(random.randint(10000, 999999)) #Nicknames your bots are going to have in the channel
  17. ircCKey = "." #The command key for your bots to listen to commands
  18. ircPass = "serverpassword" #Leave blank if no password
  19. authList = "authenticationtousebot" #Person authenticated to use the bots
  20.  
  21. def rawSend(data): #Send raw data to server
  22.     irc.send(data + "\r\n")
  23.  
  24. def ircConnect():
  25.     global irc
  26.         try:
  27.         irc = socket.socket()
  28.         if sslEnabled == True:
  29.             irc = ""
  30.             irc = socket.socket()
  31.             irc = ssl.wrap_socket(irc)
  32.             irc.connect((ircServer, ircSSLPort))
  33.         else:
  34.             irc.connect((ircServer, ircPort))
  35.     except:
  36.         print("lel1231")
  37.         ircConnect()
  38.                    
  39. def ircMessage(msg): #Send a message to the IRC channel
  40.     rawSend("PRIVMSG " + ircChannel + " :" + msg + "\r\n")
  41.  
  42. def ircRegister():
  43.     rawSend("USER " + ircNick + " 0 * " + ":" + ircNick + "\r\n")
  44.  
  45. def ircSendNick():
  46.     rawSend("NICK " + ircNick + "\r\n")
  47.  
  48. def ircJoin():
  49.     if ircChannelPassword == "":
  50.         rawSend("JOIN " + ircChannel + "\r\n")
  51.     else:
  52.                 rawSend("JOIN " + ircChannel + " " + ircChannelPassword + "\r\n")
  53.  
  54. def ircPassword():
  55.     if ircPassword != "":
  56.                 rawSend("PASS " + ircPass + "\r\n")
  57.                 #print("CLIENT: No password being sent")
  58.  
  59. def Initialize(): #Define connecting
  60.     ircConnect()
  61.     ircPassword()
  62.     ircRegister()
  63.     ircSendNick()
  64.     ircJoin()
  65.  
  66. try:
  67.     Initialize() #Running Initialize Function
  68. except socket.error:
  69.     Initialize() #Incase of socket errors
  70.  
  71. while True:
  72.     try:
  73.         data = irc.recv(1024)
  74.         print(data)
  75.     except socket.timeout:
  76.         ircConnect()
  77.     except socket.error:
  78.         ircConnect()
  79.  
  80.     secureData = data.split(" ")
  81.  
  82.     if ":" and "!" and "@" and "PRIVMSG" and "#" in data:
  83.         userName = data.split(" ")
  84.         userName = userName[0]
  85.  
  86.     if "PING" in data:
  87.         rawSend("PONG")
  88.  
  89.  
  90. #Commands:
  91.  
  92.     if ircCKey + "ping" in data:
  93.         ircMessage("pong")
  94.    
  95.     if ircCKey + "lookup" in data and secureData[0] in authList: #Hostname to IP
  96.         hostname = data.split(" ")
  97.         try:
  98.             hostname = hostname[4]
  99.             hostname = hostname.strip("\r\n")
  100.             ip = socket.gethostbyname(hostname)
  101.             ircMessage(ip + ", " + hostname)
  102.         except:
  103.             ircMessage("Failed to lookup: " + hostname)
  104.    
  105.     if ircCKey + "isup" in data and secureData[0] in authList: #Is a website up
  106.         hostname = data.split(" ")
  107.         try:
  108.             hostname = hostname[4]
  109.             hostname = hostname.strip("\r\n")
  110.             request = urllib2.urlopen("http://isup.me/" + hostname)
  111.             source = request.read()
  112.             if "s just you." in source:
  113.                 ircMessage(hostname + " is up from here.")
  114.             else:
  115.                 ircMessage(hostname + " is down from here.")
  116.         except:
  117.             ircMessage("Failed to check if: " + hostname + " is up.")
  118.  
  119.     if ircCKey + "die" in data and secureData[0] in authList: #Die
  120.         ircMessage("Cya!")
  121.             irc.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement