Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Meatballbot vs 3.10 - Created by freecode
- #
- # Credits
- #
- # Justin Giorgi (rebel_kid)
- # Contribution 255 lines
- #
- # Debuggers
- #
- # Justin Giorgi (rebel_kid)
- #
- #
- # Imports
- import irclib
- import MySQLdb as sql
- import md5
- import sys
- # Variables
- allfunctions = ['factoid', 'quit']
- servers = []
- identified = []
- # 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))
- table = cursor.fetchone()
- chanid = table[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))
- table = cursor.fetchall()
- notfound = True
- # Send factoid
- for item in table:
- if item[0] == command2[0]:
- server.privmsg(channel, item[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))
- access = cursor.fetchall()
- send = True
- # Hash password
- hashpass = md5.new(command[1]).digest()
- # Check user and pass against database
- for data in access:
- # If password is correct log in for channel
- if hashpass == data[1]:
- cursor.execute("select channame from channels where id = %s", (data[0]))
- findid = cursor.fetchone()
- identified.append(host + '!' + findid[0])
- server.privmsg(source, "You are now logged in for " + findid[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
- try:
- cursor.execute("select id from users where chanid = %s and ircnick = %s", (chanid, source))
- nickid = cursor.fetchone()
- cursor.execute("select factoid from access where chanid = %s and nickid = %s", (chanid, nickid[0]))
- accessperms = cursor.fetchone()
- # 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, command3, channel, source, server, chanid, accessperms)
- # Send not authorized
- else:
- server.privmsg(channel, "You are not authorized to call that function.")
- # Send not authorized
- except:
- server.privmsg(channel, "You are not authorized to call that function.")
- # Adds and removes factoids
- def factoid(message, command, command2, command3, channel, source, server, chanid, accessperms):
- # Check is user has permissions
- try:
- if accessperms[0] == 1:
- # Get factoids
- if command2[1] == 'add':
- cursor.execute("select factoid from factoid where chanid = %s", (chanid))
- allfacts = cursor.fetchall()
- # Check and send if factoid is already in the database
- if command2 in str(allfacts):
- server.privmsg(channel, command[2] + " is already in the database.")
- # Add factoid to database
- else:
- 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))
- allfacts = cursor.fetchall()
- # Delete factoid
- if command3 in str(allfacts):
- cursor.execute("delete from factoid where factoid = %s", (command[2]))
- server.privmsg(channel, command2[2] + " has been deleted from the database.")
- # Check if factoid is not in database
- else:
- server.privmsg(channel, command2[2] + " is not in the database.")
- # Send not authorized
- else:
- server.privmsg(channel, "You are not authorized to call that function.")
- # Send not enough info
- except:
- server.privmsg(channel, "Not enough information to process request.")
- # 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 = 'MeatBallBotSQLBETA'
- 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")
- nets = cursor.fetchall()
- index = 1
- for net in nets:
- intname = "server" + str(index)
- intname = irc.server()
- servers.append(intname.connect(net[0], port, nick, password))
- index = index + 1
- # IRC channel joins
- cursor.execute("select channame, netid from channels")
- allchans = cursor.fetchall()
- for chan in allchans:
- net = servers[chan[1] - 1]
- net.join(chan[0])
- # Begin forever loop
- irc.process_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement