Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Meatballbot vs 3.11 - Created by freecode
- #
- # Coders
- #
- # Justin Giorgi (rebel_kid)
- # Contribution framework, factoid
- #
- # Lachlan Gilmore (chilli0) while mentored by Justin Giorgi (rebel_kid)
- # Contribution stats
- #
- # Debuggers
- #
- # Justin Giorgi (rebel_kid)
- # Lachlan Gilmore (chilli0)
- #
- #
- # Imports
- import irclib
- import MySQLdb as sql
- import md5
- import sys
- import time
- # Variables
- normfunctions = ['stats']
- permfunctions = ['factoid']
- servers = []
- identified = []
- starttime = time.time()
- version = "3.12"
- debug = False
- call = '!'
- # MySQL Connection
- host = 'localhost'
- user = 'freecoders'
- passwd = 'tropicalbreezesmoothy'
- db = 'meatballbotbeta'
- try:
- conn = sql.connect(host = host, user = user, passwd = passwd, db = db)
- except sql.Error, e:
- print "Error %d: %s" % (e.args[0], e.args[1])
- cursor = conn.cursor()
- # In Channel message handler
- def pubmsg(server, event):
- # Message processing
- message = event.arguments() [0]
- # Checks if message is a request to the bot
- if message.startswith(call):
- # Gets channel id and finishes message processing
- channel = event.target()
- cursor.execute("select id from channels where channame = %s", (channel))
- row = cursor.fetchone()
- chanid = row[0]
- command = message.split(' ')
- # Checks if message requests a normal function
- if command[0].strip(call).lower() in normfunctions:
- normfunction(server, channel, command)
- # Checks if message requests a permissioned function
- elif command[0].strip(call).lower() in permfunctions:
- permfunction(server, event, message, command, chanid, channel)
- # Sends request to factoid processor
- else:
- findfactoid(server, command, chanid, channel)
- # Normal function handler
- def normfunction(server, channel, command):
- # Calls the stats function
- if command[0].strip(call).lower() == 'stats':
- stats(server, channel)
- # Provides bot statistics
- def stats(server, channel):
- # Global variable calls
- global starttime
- global version
- # Variables
- days = int((time.time() - starttime) / 86400)
- hours = int(((time.time() - starttime) - (days * 86400)) / 3600)
- minutes = int(((time.time() - starttime) - (days * 86400 + hours * 3600)) / 60)
- seconds = int(time.time() - starttime) - (days * 86400 + hours * 3600 + minutes * 60)
- # Time processing
- if (time.time() - starttime) / 60 <= 1:
- online = "I have been online for " + str(seconds) + " seconds."
- elif (time.time() - starttime) / 3600 <= 1:
- online = "I have been online for " + str(minutes) + " minutes and " + str(seconds) + " seconds."
- elif (time.time() - starttime) / 86400 <= 1:
- online = "I have been online for " + str(hours) + " hours " + str(minutes) + " minutes and " + str(seconds) + " seconds."
- else:
- online = "I have been online for " + str(days) + " days " + str(hours) + " hours " + str(minutes) + " minutes and " + str(seconds) + " seconds."
- # Channel and network counting
- cursor.execute("select count(*) from channels");
- chans = cursor.fetchone()
- cursor.execute("select count(*) from networks");
- nets = cursor.fetchone()
- # Stats declaration
- server.privmsg(channel, "I'm MeatBallBot version: " + version + ". I am currently running in " + str(chans[0]) + " channels on " + str(nets[0]) + " networks. " + online)
- # Permissioned function handler
- def permfunction(server, event, message, command, chanid, channel):
- # Global variable calls
- global call
- # Variables
- host = event.source().split("!") [1]
- source = event.source().split("!") [0]
- # Checks for permissions
- access = getperms(channel, chanid, host)
- if access == 0:
- server.privmsg(channel, "You are not authorized to call that function. Error: 101")
- else:
- # Calls factoid function
- if command[0].strip(call).lower() == 'factoid':
- if access[5] == 1:
- if len(command) > 1:
- factoid(server, command, chanid)
- else:
- server.privmsg(channel, "Not enough information.")
- else:
- server.privmsg(channel, "You are not authorized to call that function. Error: 102")
- # Gets users permissions
- def getperms(channel, chanid, host):
- # Global variable calls
- global identified
- # Check permissions
- if len(identified) > 0:
- for item in identified:
- if host + '!' + str(chanid) in item:
- splitem = item.split('!')
- nick = splitem[0]
- cursor.execute("select id from users where chanid = %s and ircnick = %s", (chanid, nick))
- row = cursor.fetchone()
- cursor.execute("select * from access where chanid = %s and nickid = %s", (chanid, row[0]))
- accessperms = cursor.fetchone()
- break
- # Declares no permissions found
- else:
- accessperms = 0
- else:
- accessperms = 0
- # Returns permissions
- return accessperms
- # Adds and deletes factoids
- def factoid(server, command, chanid):
- # Checks if request is to add a factoid
- if command[1].lower() == 'add':
- # Verifies required data has been inputed
- if len(command) > 3:
- # Checks if factoid already exists
- cursor.execute("select factoid from factoid where chanid = %s", (chanid))
- data = cursor.fetchall()
- found = False
- for row in data:
- if command[2].lower() == row[0]:
- server.privmsg(channel, command[2].lower() + " is already in the database.")
- found = True
- break
- else:
- found = False
- # Adds a factoid to the database
- if not found:
- cursor.execute("insert into factoid (chanid, factoid, fact) values (%s, %s, %s)", (chanid, command[2].lower(), ' '.join(command[3:])))
- server.privmsg(channel, command[2].lower() + " has been added to the database.")
- # Sends not enough data
- else:
- server.privmsg(channel, "Not enough information to add factoid.")
- # Checks if request is to delete a factoid
- elif command[1].lower() == 'delete':
- # Checks if required data has been input
- if len(command) == 3:
- # Checks if factoid exists
- cursor.execute("select factoid from factoid where chanid = %s", (chanid))
- data = cursor.fetchall()
- found = False
- for row in data:
- # Deletes factoid from the database
- if command[2].lower() == row[0]:
- found = True
- cursor.execute("delete from factoid where factoid = %s and chanid = %s", (command[2].lower(), chanid))
- server.privmsg(channel, command[2].lower() + " has been deleted from the database.")
- break
- else:
- found = False
- # Sends factoid not found
- if not found:
- server.privmsg(channel, command[2].lower() + " is not in the database.")
- # Sends not enough/too much data
- if len(command) < 3:
- server.privmsg(channel, "Not enough information to delete factoid.")
- else:
- server.privmsg(channel, "Too much information to delete factoid.")
- else:
- server.privmsg(channel, "I'm sorry that is not a valid function.")
- # Factoid handler
- def findfactoid(server, command, chanid, channel):
- # Global variable calls
- global call
- # Gets factoid data
- cursor.execute("select factoid, fact from factoid where chanid = %s", (chanid))
- data = cursor.fetchall()
- notfound = True
- # Send factoid
- for row in data:
- if row[0] == command[0].strip(call).lower():
- server.privmsg(channel, row[1])
- notfound = False
- # Sends factoid not found
- if notfound:
- server.privmsg(channel, "I couldn't find " + command[0].strip(call).lower() + ".")
- def privmsg(server, event):
- # Global variable calls
- global identified
- global servers
- # Message processing
- source = event.source().split("!") [0]
- message = event.arguments() [0]
- command = message.split(' ')
- host = event.source().split("!") [1]
- # Get user informaion
- cursor.execute("select chanid, ircpass from users where ircnick = %s", (source))
- data = cursor.fetchall()
- send = True
- # Hash password
- hashpass = md5.new(command[1]).digest()
- # Check user and pass against database
- for row in data:
- # If password is correct log in for channel
- if hashpass == row[1]:
- cursor.execute("select channame, netid from channels where id = %s", (row[0]))
- row2 = cursor.fetchone()
- if server == servers[row2[1] - 1]:
- identified.append(source + '!' + host + '!' + str(row[0]))
- server.privmsg(source, "You are now logged in for " + row2[0])
- send = False
- # Send failed login
- if send:
- server.privmsg(source, "I could not log you in.")
- def part(server, event):
- # Global variable calls
- global identified
- # Message processing
- channel = event.target()
- host = event.source().split("!") [1]
- # Check if user is logged in and log out
- index = 0
- try:
- while index < len(identified):
- if host + '!' + channel == identified[index]:
- del identified[index]
- index = index + 1
- else:
- index = index + 1
- except:
- pass
- # Removes identified from list on quit
- def quit(server, event):
- # Global variable calls
- global identified
- # Message Processing
- channel = event.target()
- host = event.source().split("!") [1]
- # Check if user is logged in and log out
- index = 0
- try:
- while index < len(identified):
- if host + '!' + channel == identified[index]:
- del identified[index]
- index = index + 1
- else:
- index = index + 1
- except:
- pass
- # IRC Connection
- network = 'irc.freenode.net'
- port = 6667
- nick = 'MeatBallBot'
- password = 'uniden'
- channel = '#freecode'
- irc = irclib.IRC()
- # Add event handlers
- irc.add_global_handler('pubmsg', pubmsg)
- irc.add_global_handler('privmsg', privmsg)
- irc.add_global_handler('part', part)
- irc.add_global_handler('quit', quit)
- # IRC network joins
- cursor.execute("select netaddy from networks")
- data = cursor.fetchall()
- index = 1
- for row in data:
- intname = "server" + str(index)
- intname = irc.server()
- servers.append(intname.connect(row[0], port, nick, password))
- index = index + 1
- # IRC channel joins
- if not debug:
- cursor.execute("select channame, netid from channels")
- data = cursor.fetchall()
- for row in data:
- net = servers[row[1] - 1]
- net.join(row[0])
- if debug:
- net = servers[0]
- net.join("#freecode")
- # Begin forever loop
- irc.process_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement