Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Meatballbot vs 3.11 - Created by freecode
- #
- # Credits
- #
- # Justin Giorgi (rebel_kid)
- # Contribution 268 lines
- #
- # Lachlan Gilmore (chilli0) while mentored by Justin Giorgi (rebel_kid)
- # Contribution 41 lines
- #
- # Debuggers
- #
- # Justin Giorgi (rebel_kid)
- #
- #
- # Imports
- import irclib
- import MySQLdb as sql
- import md5
- import sys
- import time
- # Variables
- allfunctions = ['factoid', 'stats']
- permfunctions = ['factoid']
- servers = []
- identified = []
- starttime = time.time()
- version = "3.11"
- # 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()
- # Main channel message handler
- def pubmsg(server, event):
- # Global variable calls
- global functions
- # Message processing
- channel = event.target()
- source = event.source().split("!") [0]
- host = event.source().split("!") [1]
- message = event.arguments() [0]
- command = message.split(' ')
- # Find channel id
- cursor.execute("select id from channels where channame = %s", (channel))
- row = cursor.fetchone()
- chanid = row[0]
- # Parse message
- if message.startswith('!'):
- message = message.strip('!')
- if len(message) > 0:
- command = message.split(' ')
- command2 = []
- command2.append(command[0].lower())
- if len(command) > 1:
- command2.append(command[1].lower())
- if len(command) > 2:
- command2.append(command[2].lower())
- # Check if message asks for a function
- if command2[0] in allfunctions:
- functions(message, command, command2, channel, source, server, chanid, host)
- # Search for factoid
- else:
- 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] == command2[0]:
- server.privmsg(channel, row[1])
- notfound = False
- # Send factoid not found
- if notfound:
- output = "I couldn't find " + command2[0] + "."
- server.privmsg(channel, output)
- # Identify users by private message
- def privmsg(server, event):
- # Global variable calls
- global identified
- # 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 from channels where id = %s", (row[0]))
- row2 = cursor.fetchone()
- identified.append(host + '!' + row2[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.")
- # Locates the command requested
- def functions(message, command, command2, channel, source, server, chanid, host):
- # Global variable calls
- global identified
- # Check if user has permissions
- if command2[0] in permfunctions:
- # Checks for permissions
- try:
- cursor.execute("select id from users where chanid = %s and ircnick = %s", (chanid, source))
- row = cursor.fetchone()
- cursor.execute("select factoid from access where chanid = %s and nickid = %s", (chanid, nickid[0]))
- accessperms = cursor.fetchone()
- perm = True
- # Sends not authorized
- except:
- server.privmsg(channel, "You are not authorized to call that function.")
- perm = False
- if perm:
- # Check if user is logged in
- if host + '!' + channel in str(identified):
- # Find and call appropriate function
- if command2[0] == 'factoid':
- factoid(message, command, command2, channel, source, server, chanid, accessperms)
- # Send not authorized
- else:
- server.privmsg(channel, "You are not authorized to call that function.")
- else:
- if command2[0] == 'stats':
- stats(message, command, command2, channel, source, server, chanid)
- # Adds and removes factoids
- def factoid(message, command, command2, channel, source, server, chanid, accessperms):
- # Check is user has permissions
- if accessperms[0] == 1:
- # Checks if enough input is given
- if len(command2) > 2:
- # Get factoids
- if command2[1] == 'add':
- cursor.execute("select factoid from factoid where chanid = %s", (chanid))
- data = cursor.fetchall()
- # Check and send if factoid is already in the database
- for row in data:
- if command2[2] in row:
- server.privmsg(channel, command2[2] + " is already in the database.")
- add = False
- break
- # Add factoid to database
- else:
- add = True
- if add:
- cursor.execute("insert into factoid (chanid, factoid, fact) values (%s, %s, %s)", (chanid, command[2], ' '.join(command[3:])))
- server.privmsg(channel, command2[2] + " has been added to the database.")
- # Get factoids
- if command2[1] == 'delete':
- cursor.execute("select factoid from factoid where chanid = %s", (chanid))
- data = cursor.fetchall()
- # Delete factoid
- for row in data:
- if command2[2] in row:
- cursor.execute("delete from factoid where factoid = %s", (command[2]))
- server.privmsg(channel, command2[2] + " has been deleted from the database.")
- dele = True
- # Check if factoid is not in database
- else:
- dele = False
- if !dele:
- server.privmsg(channel, command2[2] + " is not in the database.")
- # Send not enough info
- else:
- server.privmsg(channel,"Not enough information.")
- # Send not authorized
- else:
- server.privmsg(channel, "You are not authorized to call that function.")
- def stats(message, command, command2, channel, source, server, chanid):
- # 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 on " + str(chans[0]) + " channels and " + str(nets[0]) + " networks. " + online)
- # Removes identified from list on part
- 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
- cursor.execute("select channame, netid from channels")
- data = cursor.fetchall()
- for row in data:
- net = servers[row[1] - 1]
- net.join(row[0])
- # Begin forever loop
- irc.process_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement