Advertisement
Guest User

Untitled

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