Advertisement
Guest User

Untitled

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