Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.12 KB | None | 0 0
  1. import socket
  2. import httplib
  3. import io
  4. import re
  5. import os
  6. import sys
  7. import random
  8.  
  9. Host     = "irc.freenode.net"
  10. Channel  = "#bahahaha"
  11. Nick     = "omgnoes"
  12. Password = "hsdgfhkasdgfasdfasdfasdfhasdgf81t3495tg978t78t"
  13. Sender   = ""
  14. Text     = ""
  15. IRC      = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16. LoggedIn = []
  17.  
  18. def ToChannel(Message):
  19.     IRC.send("PRIVMSG " + Channel + " :" + str(Message) + "\r\n")
  20.        
  21. def ToServer(Message):
  22.     IRC.send(Message)
  23.    
  24. def UserExists(UserName):
  25.     if Text.find(UserName) != -1:
  26.         UserFile = file("IRC_ChannelUsers.txt", "r")
  27.         Users    = UserFile.read()
  28.         if Users.find(UserName) != -1:
  29.             return True
  30.     else:
  31.         return False
  32.            
  33. def HostMask(UserName):
  34.     MaskFile = file("IRC_ChannelHosts.txt", "r")
  35.     Masks    = MaskFile.read()
  36.     if Masks.find(UserName) != -1:
  37.         return True
  38.     else:
  39.         return False
  40.        
  41. def UserLoggedIn(UserName):
  42.     if LoggedIn.count(UserName) == 1:
  43.         return True
  44.     else:
  45.         return False
  46.    
  47. def Check(Message):
  48.     if Text.find(Message) != -1:
  49.         return True
  50.     else:
  51.         return False
  52.        
  53.        
  54. #Connect
  55. IRC.connect((Host, 6667))
  56. ToServer('USER py host servname : Python Bot\r\n')
  57. ToServer('NICK ' + str(Nick) + '\r\n')
  58.  
  59. #Main Loop
  60. while 1:
  61.     Text   = IRC.recv(1024)
  62.     Sender = Text[Text.find(':') : Text.find('!')].strip(':')
  63.     if not Text:
  64.         break
  65.  
  66.     #Server - stuff
  67.     if Check('Message of the Day'):
  68.         ToServer('JOIN '+ Channel +'\r\n')
  69.     if Check('PING'):
  70.         ToServer('PONG ' + Text.split() [1] + '\r\n')
  71.     if Check('KICK'):
  72.         Kicker = Sender
  73.         ToServer('JOIN '+ Channel +'\r\n')
  74.         ToServer('KICK '+ Channel + " " + Kicker + " do not kick me.\r\n")
  75.     if Check('This nickname is registered'):
  76.         ToServer("NS IDENTIFY " + str(Password) + "\r\n")
  77.     if Check("-o " + Nick):
  78.         ToServer("PRIVMSG ChanServ :op " + str(Channel) + " " + str(Nick) + "\r\n")
  79.        
  80.        
  81.     #Engine design reasoning
  82.     if Check(':!stl'):
  83.         ToServer('KICK ' + Channel + ' ' + Sender + '\r\n')
  84.        
  85.     #Process inputs
  86.     if Check(':!logout'):
  87.         if UserLoggedIn(Sender) == True:
  88.             UserT = Text.split(':!logout')
  89.             UserN = UserT[1].strip()
  90.             if UserLoggedIn(UserN) == True:
  91.                 ToChannel(UserN + " Succesfully logged out.")
  92.                 LoggedIn.remove(UserN)
  93.             else:
  94.                 if UserExists(UserN):
  95.                     ToChannel(UserN + " is not logged in.")
  96.         else:
  97.             ToChannel(Sender + " You're not authorized to do this.")
  98.            
  99.     if Check(':!login'):
  100.         if UserLoggedIn(Sender) == True:
  101.             ToChannel(Sender + " You're already logged in.")
  102.         else:
  103.             if UserExists(Sender) == True:
  104.                 if HostMask(Text.split()[0]) == True:
  105.                     ToChannel(str(Sender) + " You've succesfully logged in.")
  106.                     LoggedIn.append(Sender)
  107.                 else:
  108.                     ToChannel("Host masks do not match login criteria.")
  109.                     ToChannel("Your mask: " + Text.split()[0])
  110.             else:
  111.                 ToChannel(str(Sender) + " You're not special, you have no account.")
  112.                
  113.     if Check(':!logoutme'):
  114.         if UserLoggedIn(Sender) == True:
  115.             ToChannel(Sender + " Succesfully logged out.")
  116.             LoggedIn.remove(Sender)
  117.         else:
  118.             if UserExists(Sender) != True:
  119.                 ToChannel(str(Sender) + " You have no account, how can you logout?")
  120.             else:
  121.                 ToChannel(str(Sender) + " You're not logged in, how can you logout?")
  122.            
  123.     if Check(':!voice'):
  124.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  125.             voice = Text.split(':!voice')
  126.             voices = voice[1].strip()
  127.             ToServer('MODE '+ str(Channel) +' +v '+ str(voices) +'\r\n')
  128.         else:
  129.             ToChannel('You`re not authorized to do this.')
  130.  
  131.     if Check(':!devoice'):
  132.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  133.                 devoice = Text.split(':!devoice')
  134.                 devoices = devoice[1].strip()
  135.                 ToServer('MODE '+ str(Channel) +' -v '+ str(devoices) +'\r\n')
  136.         else:
  137.             ToChannel(str(Sender) + "You're not authorized to do this.")
  138.            
  139.     if Check(':!kick'):
  140.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  141.             TKick = Text.split(':!kick')
  142.             Kick  = TKick[1].strip()
  143.             if Kick != Nick:
  144.                 ToServer('KICK ' + str(Channel) + " " + str(Kick) + '\r\n')
  145.             else:
  146.                 ToChannel(Sender + " I cannot kick myself.")
  147.         else:
  148.             ToChannel(str(Sender) + " You're not authorized to do this.")
  149.                
  150.     if Check(':!help'):
  151.         HTemp = Text.split(':!help')
  152.         Help  = HTemp[1].strip()
  153.        
  154.         if Help == "voice":
  155.             ToChannel("To voice a user - syntax [ !voice nick ]")
  156.         if Help == "devoice":
  157.             ToChannel("To devoice a user - syntax [ !devoice nick ]")
  158.         if Help == "kick":
  159.             ToChannel("To kick a user - syntax [ !kick nick ]")
  160.         if Help == "login":
  161.             ToChannel("To login - syntax [ !login ]")      
  162.         if Help == "logout":
  163.             ToChannel("To logout - syntax [ !logout ]")
  164.         if Help == "deop":
  165.             ToChannel("To remove operator status from a user - syntax [ !deop nick ]")
  166.         if Help == "op":
  167.             ToChannel("To give operator status to a user - syntax [ !op nick ]")
  168.         if Help == "roulette":
  169.             ToChannel("Play a game of roulette - syntax [ !roulette ]")
  170.         if Help == "":
  171.             ToChannel("Commands[+], login, logout, voice, devoice, kick, roulette, deop, op "+
  172.                       "- For more specific information try help [command], example !help login ")
  173.  
  174.     if Check(':!deop'):
  175.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  176.             deop  = Text.split(':!deop')
  177.             deops = deop[1].strip()
  178.             if deops != Nick:
  179.                 ToServer('MODE '+ str(Channel) +' -o '+ str(deops) +'\r\n')
  180.             else:
  181.                 ToChannel('I cannot deop myself!')
  182.         else:
  183.             ToChannel('You`re not authorized to do this.')
  184.  
  185.     if Check(':!op'):
  186.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  187.             op  = Text.split(':!op')
  188.             ops = op[1].strip()
  189.             ToServer('MODE '+ str(Channel) +' +o '+ str(ops) +'\r\n')
  190.         else:
  191.             ToChannel('You`re not authorized to do this.')
  192.            
  193.     if Check(':!opme'):
  194.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  195.             ToServer('MODE '+ str(Channel) +' +o '+ str(Sender) +'\r\n')
  196.         else:
  197.             ToChannel('You`re not authorized to do this.')
  198.            
  199.     if Check(':!deopme'):
  200.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  201.             ToServer('MODE '+ str(Channel) +' -o '+ str(Sender) +'\r\n')
  202.         else:
  203.             ToChannel('You`re not authorized to do this.')
  204.            
  205.     if Check(":!adduser"):
  206.         if UserExists(Sender) == True and UserLoggedIn(Sender) == True:
  207.             UserTemp = Text.split(":!adduser")
  208.             UserData = UserTemp[1].strip()
  209.            
  210.             UserName = str(UserData[: UserData.find("~")  ]).replace(" ", "")
  211.             UserHost = str(UserData[  UserData.find("~") :]).replace(" ", "")
  212.            
  213.             if UserExists(UserName) != True and HostMask(":"+UserName+"!" + UserHost) != True:
  214.                 UserNFile = file("IRC_ChannelUsers.txt", "a")
  215.                 UserNFile.write(UserName)
  216.                 UserHFile = file("IRC_ChannelHosts.txt", "a")
  217.                 UserHFile.write(":" + UserName + "!" + UserHost)
  218.                 UserNFile.close()
  219.                 UserHFile.close()
  220.                 ToChannel("Added user: " + UserName + " to trusted users.")
  221.             else:
  222.                 ToChannel("Cannot add user: " + UserName + " user already exists.")
  223.         else:
  224.             ToChannel('You`re not authorized to do this.')
  225.            
  226.     if Check(":!deluser"):
  227.         ToChannel("Unimplemented.")
  228.        
  229.     if Check(":!commits") or Text.find("~CIA@") != -1:
  230.         os.system('cd ~/Kill--Field-Engine/ && git shortlog -s -n > ../IRCBot/git.log')
  231.         GitLog = open('git.log', 'r')
  232.         for Author in GitLog:
  233.             if(Author.find("unknown") == -1):
  234.                 ToChannel(str(str(Author).strip()).lstrip('0123456789').strip() + str(": ") + re.sub("\D", "", str(Author).strip()) +  str(" Commits."))
  235.        
  236.     # check code for other commands that do not exist
  237.     if Text.find(":!") != -1:
  238.         if Text.find(":!login"   ) == -1 and \
  239.            Text.find(":!logout"  ) == -1 and \
  240.            Text.find(":!logoutme") == -1 and \
  241.            Text.find(":!voice"   ) == -1 and \
  242.            Text.find(":!devoice" ) == -1 and \
  243.            Text.find(":!kick"    ) == -1 and \
  244.            Text.find(":!help"    ) == -1 and \
  245.            Text.find(":!deop"    ) == -1 and \
  246.            Text.find(":!op"      ) == -1 and \
  247.            Text.find(":!opme"    ) == -1 and \
  248.            Text.find(":!deopme"  ) == -1 and \
  249.            Text.find(":!adduser" ) == -1 and \
  250.            Text.find(":!stl"     ) == -1 and \
  251.            Text.find(":!deluser" ) == -1 and \
  252.            Text.find(":!commits" ) == -1:
  253.                ToChannel("Unrecognised command: " + str(Text.split(':!')[1].strip()))
  254.                
  255.     print(Text.strip())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement