errant

tom morton

Dvelopment Snippet of NCS (ES addon)

Back to Pastebin
import difflib
from collections import deque
from __future__ import division
from urllib import FancyURLopener
from configobj import ConfigObj
import es
import cfglib
import esamlib
import installib
import playerlib
import services
# loglib is used if available
try: 
    import loglib
except ImportError:
    # no loglib!
    pass
 
# "CONSTANTS"
SUCCESS = 1
 
# CONFIGURATION
 
cfg = cfglib.AddonCFG(es.getAddonPath("nochatspam") + "/cfg/addon.cfg")
advertise     = cfg.cvar("ncs_announce",     1, "Announce NCS on round start etc.")
adminlist     = cfg.cvar("ncs_admins",     1, "Sets NCS admins if auth provider is *not* used (sep. with ;)")
 
#
# End of Cfg setup
#
 
# AUTH SERVICE CFG
 
# try to get an auth provider
# first our fake auth class... :)
# for use if an auth provider is not loaded
class FakeAuth(object):
    def registerCapability (self, name,level):
        # WE CAN IGNORE THIS
        pass
 
    def isUseridAuthorized (self, uid, cap):
        return self.isIdAuthorized(self.getOfflineIdentifier(uid),cap):
 
    def getOfflineIdentifier (self, uid):
        try:
            return es.getplayersteamid(uid)
        except:
            return None
 
    def isIdAuthorized (self, steam, cap):
        if steam in adminlist.split(';'):
            return 1
        return 0
# check if the server has an auth provider loaded
if services.isRegistered('auth'):
        # great! grab the auth instance
        auth = services.use('auth') 
else:
        # otherwise use our fake auth class
        auth = FakeAuth()
# register our capability
auth.registerCapability('ncs_admin',1)
#
#   End of Auth provider setup
#
 
 
# the addon
class NCS(object):
    version = "2"
    needUpdate = 0
    tillUpdateChecks = 5
    players = {}
    def log(self,lvl,msg):
        pass
    def reloadFiles(self):
        pass
    def update(self):
        pass
    # the say watcher...    
    def watcher(self,userid,msg,teamonly):
        '''
            The main processing filter of NCS2
        '''
        if userid in 
        return userid,msg,teamonly
 
    def player_activate(self,ev):
        '''
            Plater Activate event :: add new player to local DB
        '''
        sid = playerlib.uniqueid(ev['userid'])
        if sid != 'BOT':
            if sid not in self.players:
                # new player!
                self.players[sid] = PlayerClass()
            else:
                self.players[sid].checkForBan()
            self.players[sid].lastjoin = int(time.time())
    def round_start(self,ev):
        if cfg.
 
this = NCS()
 
# download classes
class Remote(FancyURLopener):
    version = "ESPython/2.5 NCS/2.0"
    def updateSpammerLists(self):
        '''
            Updates the known spammer gray & black lists
        '''
        pass
    def updateDefinitions(self):
        '''
            Download the latest spam definitions
        '''
        pass
    def addNewString(self,string):
        '''
            Download the latest spam definitions
        '''
        pass
    def addNewBan(self,steam):
        '''
            Download the latest spam definitions
        '''
        pass
    def checkForUpdate():
        '''
            Check if the NCS file has been updated
        '''
        if this.needUpdate == 0:
            addon = esamlib.getAddon('nochatspam')
            if addon.currentversion != this.version:
                this.needUpdate = 1
    def updateAddon():
        '''
            Update the NCS addon
        '''
        pass
web = Remote()
 
 
heursiticMatchLimit = 0.3
messageHistoryLength = 20
class PlayerClass(object):
    def __init__(self):
        self.playerDeque = deque()
        self.spamCollections = []
 
    def hueristicStrings(self,new):
        matches = []
        compare = difflib.SequenceMatcher(None)
        compare.set_seq2(new)
        for strItem in self.playerDeque:
            compare.set_seq1(strItem)
            if compare.quick_ratio() > 0.6:
                # quite close
                matches.append(strItem)
        if float(len(self.playerDeque) / len(matches)) > heursiticMatchLimit:
            # possible spam
            matches.append(strItem)
 
    def heavyProcessing(self):
        # does the heavier analysis...
        pass
 
# EVENTS
def load():
    # first things first.. set it to check 4 update on next map load
    this.tillUpdateChecks = 0
    # execute the cfg file
    cfg.execute()
 
def es_map_start(ev):
    # dl some stuff and update the lists etc.
    # check if we should do it this map start...
    if this.tillUpdateChecks < 1:
        # reset this value
        this.tillUpdateChecks = 5
        # try to update definitions
        this.log(0,"NCS: Attempting to update definition files (server may pause on slow connections)")
        if web.updateSpammerList() == SUCCESS or web.updateDefinitions() == SUCCESS:
            # if updated then reload the files
            this.log(1,"NCS: Retrieved new definitions, reloading files")
            this.reloadFiles()
        # check for script updates
        this.log(0,"NCS: Checking for script updates (server may pause on slow connections)")
        web.checkForUpdate()
        if this.needUpdate == 1:
            # needs update....
            if this.autoUpdate == 1:
                this.log(0, "NCS: NEW UPDATE AVAILABLE. Updating Automatically")
                web.updateAddon()
                this.log(0, "NCS: Update completed successfully")
            else:
                this.log(0, "NCS: NEW UPDATE AVAILABLE. Manual update required") 
    else:
        # otherwise just dec the variable
        this.tillUpdateChecks -= 1