Advertisement
Guest User

Untitled

a guest
May 9th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.12 KB | None | 0 0
  1. # Meatballbot vs 3.10 - Created by freecode
  2. #
  3. # Credits
  4. #
  5. #   Justin Giorgi (rebel_kid)
  6. #       Contribution 255 lines
  7. #
  8. # Debuggers
  9. #
  10. #   Justin Giorgi (rebel_kid)
  11. #
  12. #
  13. # Imports
  14. import irclib
  15. import MySQLdb as sql
  16. import md5
  17. import sys
  18.  
  19. # Variables
  20. allfunctions = ['factoid', 'quit']
  21. servers = []
  22. identified = []
  23.  
  24. # MySQL Connection
  25. host = 'localhost'
  26. user = 'freecoders'
  27. passwd = 'tropicalbreezesmoothy'
  28. db = 'meatballbotbeta'
  29. try:
  30.     conn = sql.connect(host = host, user = user, passwd = passwd, db = db)
  31. except sql.Error, e:
  32.     print "Error %d: %s" % (e.args[0], e.args[1])
  33. cursor = conn.cursor()
  34.  
  35. # Main channel message handler
  36. def pubmsg(server, event):
  37.  
  38.     # Global variable calls
  39.     global functions
  40.    
  41.     # Message processing
  42.     channel = event.target()
  43.     source  = event.source().split("!") [0]
  44.     host = event.source().split("!") [1]
  45.     message = event.arguments() [0]
  46.     command = message.split(' ')
  47.    
  48.     # Find channel id
  49.     cursor.execute("select id from channels where channame = %s", (channel))
  50.     table = cursor.fetchone()
  51.     chanid = table[0]
  52.    
  53.     # Parse message
  54.     if message.startswith('!'):
  55.         message = message.strip('!')
  56.         if len(message) > 0:
  57.             command = message.split(' ')
  58.             command2 = []
  59.             command2.append(command[0].lower())
  60.             if len(command) > 1:
  61.                 command2.append(command[1].lower())
  62.             if len(command) > 2:
  63.                 command2.append(command[2].lower())
  64.                
  65.             # Check if message asks for a function
  66.             if command2[0] in allfunctions:
  67.                 functions(message, command, command2, channel, source, server, chanid, host)
  68.                
  69.             # Search for factoid   
  70.             else:
  71.                 cursor.execute("select factoid, fact from factoid where chanid = %s", (chanid))
  72.                 table = cursor.fetchall()
  73.                 notfound = True
  74.                
  75.                 # Send factoid
  76.                 for item in table:
  77.                     if item[0] == command2[0]:
  78.                         server.privmsg(channel, item[1])
  79.                         notfound = False
  80.                        
  81.                 # Send factoid not found
  82.                 if notfound:
  83.                     output = "I couldn't find " + command2[0] + "."
  84.                     server.privmsg(channel, output)
  85.  
  86. # Identify users by private message
  87. def privmsg(server, event):
  88.  
  89.     # Global variable calls
  90.     global identified
  91.    
  92.     # Message processing
  93.     source  = event.source().split("!") [0]
  94.     message = event.arguments() [0]
  95.     command = message.split(' ')
  96.     host = event.source().split("!") [1]
  97.    
  98.     # Get user informaion
  99.     cursor.execute("select chanid, ircpass from users where ircnick = %s", (source))
  100.     access = cursor.fetchall()
  101.     send = True
  102.    
  103.     # Hash password
  104.     hashpass = md5.new(command[1]).digest()
  105.    
  106.     # Check user and pass against database
  107.     for data in access:
  108.        
  109.         # If password is correct log in for channel
  110.         if hashpass == data[1]:
  111.             cursor.execute("select channame from channels where id = %s", (data[0]))
  112.             findid = cursor.fetchone()
  113.             identified.append(host + '!' + findid[0])
  114.             server.privmsg(source, "You are now logged in for " + findid[0])
  115.             send = False
  116.    
  117.     # Send failed login    
  118.     if send:
  119.         server.privmsg(source, "I could not log you in.")
  120.  
  121. # Locates the command requested
  122. def functions(message, command, command2, channel, source, server, chanid, host):
  123.  
  124.     # Global variable calls
  125.     global identified
  126.    
  127.     # Check if user has permissions
  128.     try:
  129.         cursor.execute("select id from users where chanid = %s and ircnick = %s", (chanid, source))
  130.         nickid = cursor.fetchone()
  131.         cursor.execute("select factoid from access where chanid = %s and nickid = %s", (chanid, nickid[0]))
  132.         accessperms = cursor.fetchone()
  133.        
  134.         # Check if user is logged in
  135.         if host + '!' + channel in str(identified):
  136.  
  137.             # Find and call appropriate function
  138.             if command2[0] == 'factoid':
  139.                 factoid(message, command, command2, command3, channel, source, server, chanid, accessperms)
  140.        
  141.         # Send not authorized
  142.         else:
  143.             server.privmsg(channel, "You are not authorized to call that function.")
  144.    
  145.     # Send not authorized  
  146.     except:
  147.         server.privmsg(channel, "You are not authorized to call that function.")
  148. # Adds and removes factoids
  149. def factoid(message, command, command2, command3, channel, source, server, chanid, accessperms):
  150.  
  151.     # Check is user has permissions
  152.     try:
  153.         if accessperms[0] == 1:
  154.            
  155.             # Get factoids
  156.             if command2[1] == 'add':
  157.                 cursor.execute("select factoid from factoid where chanid = %s", (chanid))
  158.                 allfacts = cursor.fetchall()
  159.        
  160.                 # Check and send if factoid is already in the database     
  161.                 if command2 in str(allfacts):
  162.                     server.privmsg(channel, command[2] + " is already in the database.")
  163.        
  164.                 # Add factoid to database      
  165.                 else:
  166.                     cursor.execute("insert into factoid (chanid, factoid, fact) values (%s, %s, %s)", (chanid, command[2], ' '.join(command[3:])))
  167.                     server.privmsg(channel, command2[2] + " has been added to the database.")
  168.    
  169.             # Get factoids
  170.             if command2[1] == 'delete':
  171.                 cursor.execute("select factoid from factoid where chanid = %s", (chanid))
  172.                 allfacts = cursor.fetchall()
  173.        
  174.                 # Delete factoid
  175.                 if command3 in str(allfacts):
  176.                     cursor.execute("delete from factoid where factoid = %s", (command[2]))
  177.                     server.privmsg(channel, command2[2] + " has been deleted from the database.")
  178.        
  179.                 # Check if factoid is not in database
  180.                 else:
  181.                     server.privmsg(channel, command2[2] + " is not in the database.")
  182.    
  183.         # Send not authorized
  184.         else:
  185.             server.privmsg(channel, "You are not authorized to call that function.")
  186.    
  187.     # Send not enough info
  188.     except:
  189.         server.privmsg(channel, "Not enough information to process request.")
  190.        
  191. # Removes identified from list on part
  192. def part(server, event):
  193.  
  194.     # Global variable calls
  195.     global identified
  196.    
  197.     # Message processing
  198.     channel = event.target()
  199.     host  = event.source().split("!") [1]
  200.    
  201.     # Check if user is logged in and log out
  202.     index = 0
  203.     try:
  204.         while index < len(identified):
  205.             if host + '!' + channel == identified[index]:
  206.                 del identified[index]
  207.                 index = index + 1
  208.             else:
  209.                 index = index + 1
  210.     except:
  211.         pass
  212.            
  213. # Removes identified from list on quit
  214. def quit(server, event):
  215.  
  216.     # Global variable calls
  217.     global identified
  218.    
  219.     # Message Processing
  220.     channel = event.target()
  221.     host  = event.source().split("!") [1]
  222.    
  223.     # Check if user is logged in and log out
  224.     index = 0
  225.     try:
  226.         while index < len(identified):
  227.             if host + '!' + channel == identified[index]:
  228.                 del identified[index]
  229.                 index = index + 1
  230.             else:
  231.                 index = index + 1
  232.     except:
  233.         pass
  234.            
  235. # IRC Connection
  236. network = 'irc.freenode.net'
  237. port = 6667
  238. nick = 'MeatBallBotSQLBETA'
  239. password = 'uniden'
  240. channel = '#freecode'
  241. irc = irclib.IRC()
  242.  
  243. # Add event handlers
  244. irc.add_global_handler('pubmsg', pubmsg)
  245. irc.add_global_handler('privmsg', privmsg)
  246. irc.add_global_handler('part', part)
  247. irc.add_global_handler('quit', quit)
  248.  
  249. # IRC network joins
  250. cursor.execute("select netaddy from networks")
  251. nets = cursor.fetchall()
  252. index = 1
  253. for net in nets:
  254.     intname = "server" + str(index)
  255.     intname = irc.server()
  256.     servers.append(intname.connect(net[0], port, nick, password))
  257.     index = index + 1
  258.    
  259. # IRC channel joins
  260. cursor.execute("select channame, netid from channels")
  261. allchans = cursor.fetchall()
  262. for chan in allchans:
  263.     net = servers[chan[1] - 1]
  264.     net.join(chan[0])
  265.  
  266. # Begin forever loop
  267. irc.process_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement