1. Stephen Tanner
  2. Karma Cat

Commits

Stephen Tanner  committed cba052c

Added Combo Breaker plugin, fixed ignoring of usernames

  • Participants
  • Parent commits 227d672
  • Branches default

Comments (0)

Files changed (8)

File src/ACL.py

View file
 
 @author: stanner
 '''
+import sqlite3
 
 class ACL:
     '''
     before deciding to run the command
     '''
 
-    def __init__(self, owner, admins):
+    def __init__(self, owner):
         '''
         Constructor
         '''
         self.OWNER = owner
 
-        self.ADMINS = admins
+        self.ADMINS = []
 
         self.IGNORE = []
 
+        self.conn = sqlite3.connect('acl.db') #@UndefinedVariable
+
+        self.curs = self.conn.cursor()
+
+        self.__mkATable()
+        self.__mkITable()
+
+
+
 
     def getAdmins(self):
-        return self.ADMINS
+        self.curs.execute('SELECT nick from admin')
+        return self.curs.fetchall()
 
     def getOwner(self):
         return self.OWNER
 
-    def addAdmin(self, admin):
-        return self.ADMINS.append(admin)
+    def addAdmin(self, nick):
+        self.curs.execute('INSERT INTO admin values (?)', [nick])
+        self.conn.commit()
+        #return self.ADMINS.append(admin)
 
-    def delAdmin(self, admin):
-        return self.ADMINS.remove(admin)
+    def delAdmin(self, nick):
+        self.curs.execute('DELETE FROM admin WHERE nick = ?', [nick])
+        self.conn.commit()
+        #return self.ADMINS.remove(admin)
 
-    def getPermLvl(self, nick):
+    def getPermLvl(self, message):
 
-        if nick == self.OWNER:
+        self.ADMINS = self.getAdmins()
+        self.curs.execute('SELECT nick, uname, host from ignore')
+
+        self.IGNORE = self.curs.fetchall()
+        #print(message.uname)
+        #print("The ignore results: " + str([x[1] for x in self.IGNORE]))
+        if message.nick == self.OWNER:
             return 0
-        elif nick in self.ADMINS:
+        elif message.nick in self.ADMINS:
             return 1
-        elif nick in self.IGNORE:
+        elif message.nick in [x[0] for x in self.IGNORE]:
             return 9
+        elif message.uname in [x[1] for x in self.IGNORE]:
+            return 9
+        #elif message.host in self.IGNORE:
+            #return 9
         else:
             return 2
 
     def addIgnore(self, user):
-        return self.IGNORE.append(user)
+        self.curs.execute('INSERT INTO ignore values (?, ?, ?)', user)
+        self.conn.commit()
+#        return self.IGNORE.append(user)
 
-    def delIgnore(self, user):
-        return self.IGNORE.remove(user)
+    def delIgnore(self, nick):
+        self.curs.execute('DELETE FROM ignore WHERE nick = ?', [nick])
+        self.conn.commit()
+        #return self.IGNORE.remove(user)
 
     def getIgnore(self):
-        return self.IGNORE
+        self.curs.execute('SELECT nick, uname, host FROM ignore')
+        return self.curs.fetchall()
+        #return self.IGNORE
+
+    def __mkITable(self):
+        '''This method is used to make a new table'''
+        self.curs.execute(' CREATE TABLE IF NOT EXISTS ignore (nick TEXT PRIMARY KEY, uname TEXT, host TEXT)')
+        self.conn.commit()
+
+    def __mkATable(self):
+        '''This method is used to make a new table'''
+        self.curs.execute(' CREATE TABLE IF NOT EXISTS admin (nick TEXT PRIMARY KEY)')
+        self.conn.commit()

File src/Admin.py

View file
 
     def read(self, message):
         '''You are owner'''
-        if self.ACL.getPermLvl(message.nick) == 0 :
+        if self.ACL.getPermLvl(message) == 0 :
             try:
                 self.ownerCommands[message.message[0]](message)
             except KeyError:
                 pass
 
         '''You are an admin'''
-        if self.ACL.getPermLvl(message.nick) <= 1 :
+        if self.ACL.getPermLvl(message) <= 1 :
             try:
                 self.adminCommands[message.message[0]](message)
             except KeyError:
             pass
 
     def __getChans(self, message):
+        '''PM user a list of the chans the bot is in'''
         self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                     str(self.SERVER.chans))
 
     def __joinChan(self, message):
+        '''Have the bot join a chan'''
         if len(message.message) > 1:
             self.SERVER.joinChannel(message.message[1])
 
     def __partChan(self, messsage):
+        '''Have the bot leave a chan'''
         if len(messsage.message) > 1:
             self.SERVER.partChannel(messsage.message[1])
 
     def __getAdmin(self, message):
+        '''PM user a list of admins'''
         self.SERVER.sendPRIVMessage(message.nick + " :admins: " +
                                     str(self.ACL.getAdmins()))
 
     def __getIgnore(self, message):
+        '''PM user a list of ignored user(s)'''
         self.SERVER.sendPRIVMessage(message.nick + ' :ignored: ' +
                                     str(self.ACL.getIgnore()))
 
 
     def __muteServer(self, message):
+        '''Tell the bot to send no replies to the server'''
         self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                     'Cutting communications...')
         self.SERVER.mute()
 
     def __unmuteServer(self, message):
+        '''Tell the bot to allow replies to the server'''
         self.SERVER.unMute()
         self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                     'Establishing link...')
 
     def __addAdmin(self, message):
-
+        '''Add an admin to the admin list'''
         if len(message.message) > 1:
             self.ACL.addAdmin(message.message[1])
             self.SERVER.sendPRIVMessage(message.nick + " :" +
                                         " added to admins")
 
     def __delAdmin(self, message):
+        '''Remove an admin from the admin list'''
         if len(message.message) > 1:
             self.ACL.delAdmin(message.message[1])
             self.SERVER.sendPRIVMessage(message.nick + " :" +
                                         " removed from admins")
 
     def __die(self, message):
+        '''Tell the bot to die'''
         print('Master commanded me to quit')
         self.SERVER.unMute()
         self.SERVER.sendPRIVMessage(message.nick + " :" + " quitting...")
         sys.exit(3)
 
     def __addIgnore(self, message):
+        '''Add a user to the ignore list'''
         if len(message.message) > 1:
-            self.ACL.addIgnore(message.message[1])
+            self.ACL.addIgnore(self.SERVER.getWhois(message.message[1]))
             self.SERVER.sendPRIVMessage(message.message[1] + ' :' +
                                         'You have been added to the ignore list. Probably because you are annoying...')
 
     def __delIgnore(self, message):
+        '''Remove a user from the ignore list'''
         if len(message.message) > 1:
             self.ACL.delIgnore(message.message[1])
             self.SERVER.sendPRIVMessage(message.message[1] + ' :' +
 
     def mainHelp(self, message):
         helpstr = ['Meow Meow I am Karma Kat! ',
-                  '  Owner     = ' + str(self.ACL.OWNER),
-                  '  Admins    = ' + str(self.ACL.ADMINS),
-                  '  Ignored   = ' + str(self.ACL.IGNORE),
-                  '  Plugins   = admin, karma, url',
+                  '  Owner     = ' + str(self.ACL.getOwner()),
+                  '  Admins    = ' + str(self.ACL.getAdmins()),
+                  '  Ignored   = ' + str(self.ACL.getIgnore()),
+                  '  Plugins   = admin, karma, url, cccb',
                   '  More help - ,help <plugin name> to get the help page for a plugin']
         for hlp in helpstr:
             self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)
             self.mainHelp(message)
         else:
             if message.message[1] == 'admin':
-                helpstr = ['Admin Help Commands: add_admin, del_admin, die, list_admin, mute, and unmute',
-                           '  add_admin  - Add and admin',
-                           '  del_admin  - Delete an admin',
-                           '  die        - Kill the bot',
-                           '  get_admin  - PM a list of admins',
-                           '  mute       - Up-votes a user or word',
-                           '  unmute     - Down-votes a user or word', ]
+                cmds = list(self.ownerCommands.keys())
+                cmds.extend(list(self.adminCommands.keys()))
+                cmds.extend(list(self.anonCommands.keys()))
+
+                helpstr = ['Admin Help Commands: ' + str(cmds).replace(':', ''),
+                           '  ,add_admin  - ' + self.__addAdmin.__doc__,
+                           '  ,del_admin  - ' + self.__delAdmin.__doc__,
+                           '  ,die        - ' + self.__die.__doc__,
+                           '  ,get_admin  - ' + self.__getAdmin.__doc__,
+                           '  ,mute       - ' + self.__muteServer.__doc__,
+                           '  ,unmute     - ' + self.__unmuteServer.__doc__,
+                           '  ,join       - ' + self.__joinChan.__doc__,
+                           '  ,part       - ' + self.__partChan.__doc__,
+                           '  ,ignore     - ' + self.__addIgnore.__doc__,
+                           '  ,unignore   - ' + self.__delIgnore.__doc__,
+                           '  ,get_ignore - ' + self.__getIgnore.__doc__,
+                           '  ,get_chans  - ' + self.__getChans.__doc__ ]
 
                 for hlp in helpstr:
                     self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)

File src/Karma.py

View file
 
     def read(self, message):
         '''You are owner'''
-        if self.ACL.getPermLvl(message.nick) == 0 :
+        if self.ACL.getPermLvl(message) == 0 :
             try:
                 self.ownerCommands[message.message[0]](message)
             except KeyError:
                 pass
 
         '''You are admin'''
-        if self.ACL.getPermLvl(message.nick) <= 1 :
+        if self.ACL.getPermLvl(message) <= 1 :
             try:
                 self.adminCommands[message.message[0]](message)
             except KeyError:
             pass
 
     def __getUsers(self, message):
+        '''PM user a list of people who have voted or have karma'''
         self.curs.execute('SELECT uname from vote')
         names = self.curs.fetchall()
         self.SERVER.sendPRIVMessage(message.nick + " :" +
                                     "Known users: " + str(names))
 
     def __nukeKarma(self, message):
-        '''Nuke entire karma db
-        Then create a new table
-        '''
+        '''Nuke entire karma db Then create a new table'''
         self.curs.execute('DROP TABLE IF EXISTS vote')
         self.__mkKTable()
 
     def __nukeWords(self, message):
-        '''Nuke entire word db
-        Then create a new table
-        '''
+        '''Nuke entire word db Then create a new table'''
         self.curs.execute('DROP TABLE IF EXISTS vote2')
         self.__mkWTable()
 
     def __getWords(self, message):
-        self.curs.execute('SELECT word From vote2')
+        '''PM user a list of words with karma'''
+        self.curs.execute('SELECT word FROM vote2')
         words = self.curs.fetchall()
         self.SERVER.sendPRIVMessage(message.nick + " :" +
                                     "Known words: " + str(words))
 
     def __getKarma(self, message):
+        '''Get the karma score for the user or word'''
         '''This method gets the karma score for a word or a user'''
         if len(message.message) > 1:
             karma = ['']
             pass
 
     def __upVote(self, message):
-        '''This method will handle the logic for trying to upvote'''
+        '''Increase the karma score for a word or user by one'''
         if len(message.message) > 1:
-            self.curs.execute('SELECT * FROM vote WHERE uname = ?',
-                              message.nick)
+            self.curs.execute('SELECT uname FROM vote WHERE uname = ?',
+                              [message.nick])
             if len(self.curs.fetchall()) == 1 :
                 if self.__nickCheck(message) :
                     if self.__isAllowed(message):
                 self.__upVote(message)
 
     def __downVote(self, message):
-        '''This method will handle the logic for trying to downvote'''
+        '''Decease a karma score for a word or a user by one'''
         if len(message.message) > 1:
             self.curs.execute('SELECT * FROM vote WHERE uname = ?',
                               [message.nick])
 
     def __mkWTable(self):
         '''This method is used to make a new table'''
-        self.curs.execute(' CREATE TABLE IF NOT EXISTS vote2 \
-            (word TEXT PRIMARY KEY, up_vote INTEGER, down_vote INTEGER)')
+        self.curs.execute(' CREATE TABLE IF NOT EXISTS vote2 (word TEXT PRIMARY KEY, up_vote INTEGER, down_vote INTEGER)')
         self.conn.commit()
 
     def __wordCheck(self, message):
+        '''This method is used to see if a word is actually in the db already'''
         self.curs.execute('SELECT * FROM vote2 WHERE word = ?',
                           [message.message[1]])
         if len(self.curs.fetchall()) == 1 :
             return False
 
     def __newUser(self, nick):
+        '''Create a new user and add him to the db'''
         t = (nick, 0, 0, 0,
              datetime.datetime(1111, 1, 1, 0, 0, 0, 1).isoformat(' '))
         self.curs.execute('INSERT INTO vote values (?, ?, ?, ?, ?)', t)
         self.conn.commit()
 
     def __newWord(self, word):
+        '''Create a new word and add it to the db'''
         t = (word, 0, 0)
         self.curs.execute('INSERT INTO vote2 values (?, ?, ?)', t)
         self.conn.commit()
                 self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                             'Sorry but karma is meant to be public...')
                 return False
-            elif self.ACL.getPermLvl(message.nick) == 9:
+            elif self.ACL.getPermLvl(message) == 9:
                 self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                             'Sorry but you are on the naughty list...')
                 return False
                 self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                             'Sorry but karma is meant to be public...')
                 return False
-            elif self.ACL.getPermLvl(message.nick) == 9:
+            elif self.ACL.getPermLvl(message) == 9:
                 self.SERVER.sendPRIVMessage(message.nick + ' :' +
                                             'Sorry but you are on the naughty list...')
                 return False
             return False
 
     def __resetUser(self, message):
-        '''This method is to reset the karma score of a given user'''
+        '''Reset the karma score of a given user back to 0's'''
         if len(message.message) > 1:
             self.curs.execute('SELECT * FROM vote WHERE uname =?',
                               message.message[1])
             pass
 
     def __delWord(self, message):
-        '''This method is to delete a word from the BD'''
+        '''Delete a word from the db'''
         if len(message.message) > 1:
             self.curs.execute('DELETE FROM vote2 WHERE word = ?',
                               message.message[1])
 
     def help(self, message):
         if len(message.message) > 1 and message.message[1] == 'karma':
-            helpstr = ['Karma Help Commands: nuke_karma, nuke_words, reset <user>, del_word <word>, ++/<3 <user or word>, --/</3 <user or word>, karma <user or word>, and get_words',
-                       '  nuke_karma - Reset entire karma db',
-                       '  nuke_words - Reset entire word db',
-                       '  reset      - Resets the karma of the specified user to 0',
-                       '  del_word   - Deletes a single word from the table',
-                       '  ++/<3      - Up-votes a user or word',
-                       '  --/</3     - Down-votes a user or word',
-                       '  karma      - Gets the karma score for a user or word',
-                       '  get_words  - PM of the current list of words in the db',
-                       '  get_users  - PM of the registered users in the db']
+            cmds = list(self.ownerCommands.keys())
+            cmds.extend(list(self.adminCommands.keys()))
+            cmds.extend(list(self.anonCommands.keys()))
+            helpstr = ['Karma Help Commands: ' + str(cmds).replace(':', ''),
+                       '  nuke_karma - ' + self.__nukeKarma.__doc__,
+                       '  nuke_words - ' + self.__nukeWords.__doc__,
+                       '  reset      - ' + self.__resetUser.__doc__,
+                       '  del_word   - ' + self.__delWord.__doc__,
+                       '  ++ or <3   - ' + self.__upVote.__doc__,
+                       '  -- or </3  - ' + self.__downVote.__doc__,
+                       '  karma      - ' + self.__getKarma.__doc__,
+                       '  get_words  - ' + self.__getWords.__doc__,
+                       '  get_users  - ' + self.__getUsers.__doc__ ]
 
             for hlp in helpstr:
                 self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)

File src/Message.py

View file
 
         self.line = line
         self.nick = ""
-        self.uName = ""
+        self.uname = ""
         self.host = ""
         self.type = ""
         self.chan = ""
         senderInfo = line[0].partition('!')
         self.nick = senderInfo[0].lstrip(':')
         senderInfo = senderInfo[2].partition('@')
-        self.uName = senderInfo[0]
+        self.uname = senderInfo[0]
         self.host = senderInfo[2]
         self.type = line[1]
         self.chan = line[2]

File src/Server.py

View file
         nicks = [nick.lstrip('+@:') for nick in nicks[5:]]
         return nicks
 
+    def getWhois(self, nick):
+        self.sendMessage('WHOIS ' + nick)
+        info = self.getLine()
+        return [info[3], info[4], info[5]]
+

File src/URL.py

View file
 
 
     def read(self, message):
-        '''This whole method needs to be cleaned up and reimplemented'''
-
-        if message.message[0] == ':,sl' and len(message.message) == 1:
-            self.SERVER.sendPRIVMessage(message.chan + ' :' + self.__getLastURL())
-
-        elif message.message[0] == ':,sl' and len(message.message) > 1 :
-            if message.message[1].isdigit():
-                idx = int(message.message[1])
-                self.SERVER.sendPRIVMessage(message.chan + ' :' + self.__getURL(idx))
-        elif message.message[0] == ':,psl' and len(message.message) == 1:
-            self.SERVER.sendPRIVMessage(message.nick + ' :' + self.__getLastURL())
-        elif message.message[0] == ':,psl' and len(message.message) > 1 :
-            if message.message[1].isdigit():
-                idx = int(message.message[1])
-                self.SERVER.sendPRIVMessage(message.nick + ' :' + self.__getURL(idx))
-        elif message.message[0] == ':,help' and len(message.message) > 1 and message.message[1] == 'url':
-            self.help(message)
-        else:
+        '''Read the message from the server and proccess it'''
+        try:
+            self.knownCommands[message.message[0]](message)
+        except:
             pass
 
         for word in message.message:
             return "number out of bounds"
 
     def help(self, message):
+        if len(message.message) > 1 and message.message[1] == 'url':
+            cmds = list(self.knownCommands.keys())
+            helpstr = ['URL Help Commands: ' + str(cmds).replace(':', ''),
+                       '  sl  - ' + self.__shortLink.__doc__,
+                       '  psl - ' + self.__privateShortLink.__doc__,
+                       '  Note: {1...10} is an optional argument to get older links' ]
 
-        helpstr = ['URL Help Commands: sl, psl, sl {1...10}, and psl {1...10}',
-                   '  sl       - Replies to the chan with the most recent shortened link',
-                   '  psl      - Same as sl but sends a PM',
-                   '  {1...10} - Numbers 1-10 can be used to one of the previous links held in the queue']
-
-        for hlp in helpstr:
-            self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)
+            for hlp in helpstr:
+                self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)
 
     def __shortLink(self, message):
-        pass
+        '''Sends a shortened link to the chan.'''
+        if len(message.message) == 1:
+            self.SERVER.sendPRIVMessage(message.chan + ' :' + self.__getLastURL())
+
+        else :
+            if message.message[1].isdigit():
+                idx = int(message.message[1])
+                self.SERVER.sendPRIVMessage(message.chan + ' :' + self.__getURL(idx))
 
     def __privateShortLink(self, message):
-        pass
+        '''PM a shortened link to user'''
+        if message.message[0] == ':,psl' and len(message.message) == 1:
+            self.SERVER.sendPRIVMessage(message.nick + ' :' + self.__getLastURL())
+        else:
+            if message.message[1].isdigit():
+                idx = int(message.message[1])
+                self.SERVER.sendPRIVMessage(message.nick + ' :' + self.__getURL(idx))
 

File src/cccb.py

View file
+'''
+Created on Jul 14, 2011
+
+@author: stanner
+'''
+from Plugin import Plugin
+import socket
+
+class ComboBreaker(Plugin):
+    '''
+    classdocs
+    '''
+
+
+    def __init__(self, acl, server):
+        '''
+        Constructor
+        '''
+        super(ComboBreaker, self).__init__(acl, server)
+
+        self.knownCommands = {':,help': self.help}
+
+        self.prevMsg = None
+
+        self.heatingUp = 0
+
+    def read(self, message):
+
+        try:
+            self.knownCommands[message.message[0]](message)
+            #print("commands")
+        except KeyError:
+            pass
+
+        if message.message == self.prevMsg:
+            '''This means that the previous message is the same as the current one'''
+            self.heatingUp = self.heatingUp + 1
+            #print(self.heatingUp)
+            self.prevMsg = message.message
+        else:
+            if self.heatingUp > 1:
+                cmbo = ""
+                for i in range(self.heatingUp):
+                    cmbo = cmbo + "C-"
+                cmbo = cmbo + "COMBO BREAKER!"
+                self.SERVER.sendPRIVMessage(message.chan + " :" + cmbo)
+                self.heatingUp = 0
+                self.prevMsg = message.message
+            else:
+                self.prevMsg = message.message
+
+
+
+    def help(self, message):
+        if len(message.message) > 1 and message.message[1] == 'cccb':
+            #print("Shit Shit")
+            helpstr = ['ComboBreaker Help Commands: ',
+                       '  none - When the same message is sent 3 times or more, a combo is created.' ]
+
+            for hlp in helpstr:
+                self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)
+
+        if len(message.message) > 1 and message.message[1] == 'cccp':
+            #print("Shit Shit")
+            helpstr = ['CCCP Help Commands: ',
+                       '  none - nuke now inbound towards ' + self.__getIP(message.host)]
+
+            for hlp in helpstr:
+                self.SERVER.sendPRIVMessage(message.nick + ' :' + hlp)
+
+
+    def __getIP(self, host):
+        try:
+            return socket.gethostbyname(host)
+        except:
+            return host

File src/karma_kat.py

View file
 from Admin import Admin
 from Karma import Karma
 from URL import URL
+from cccb import ComboBreaker
 #import os
 
 
         #Pets Owner
         self.OWNER = 'btreecat'
 
-        self.ADMINS = [self.OWNER, 'stanner']
+        #self.ADMINS = [self.OWNER, 'stanner']
 
-        self.ACL = ACL(self.OWNER, self.ADMINS)
+        self.ACL = ACL(self.OWNER)
 
-        self.server = Server(self.configDic)
+        self.SERVER = Server(self.configDic)
 
-        self.admin = Admin(self.ACL, self.server)
-        self.karma = Karma(self.ACL, self.server)
-        self.url = URL(self.ACL, self.server)
+        #create the plugins
+        self.admin = Admin(self.ACL, self.SERVER)
+        self.karma = Karma(self.ACL, self.SERVER)
+        self.url = URL(self.ACL, self.SERVER)
+        self.cccb = ComboBreaker(self.ACL, self.SERVER)
+        self.plugins = [self.admin, self.karma, self.url, self.cccb]
 
-        self.plugins = [self.admin, self.karma, self.url]
 
-
-        self.server.connectToServer()
-        self.parser = Parser(self.server)
+        self.SERVER.connectToServer()
+        self.parser = Parser(self.SERVER)
         self.__registerPlugins()
         self.parser.parseEngine()