perplexing error

David Clymer <[email protected]>
Newsgroups gmane.comp.games.mud.client.lyntin
Message-ID <1066538574.1533.5.camel@hulk>
My mud has a limit on the number of consecutive times you can enter the
same command, and kicks you if you go over that limit. So, i'm trying to
write a module to prevent spam-kicks. i've written a class, manager, and
hook to do this (hook and init command code below):

def handle_spam(args):
  """
  Chccks to see if the text has been spammed to the spam limit, and if
so, sends a delimiter to the mud
                                                                                                                             
  @param text: text to check for spam
  @type text: string
  """
  ses = args["session"]
  text = args["data"]
  sm = exported.get_manager("spam")
  exported.write_message("this is what i'm seeing: " + text) #debug
  if sm.isActivated(ses):
    exported.write_message("spam limits are active.") #debug
    if sm.isSpam(ses, text):
      exported.write_message("'%s' is spam." % text) #debug
      if sm.isAtLimit(ses, text):
        exported.write_message("we are at the limit." % text) #debug
        exported.write_mud_data(sm.getDelimiter(ses), ses)
                                                                                                                             
commands_dict = {}
                                                                                                                             
def spamlimit_cmd(ses, args, input):
  """
  this just turns stuff on for testing purposes right now. its still
ugly                                                                                                                             
  category: commands
  """
  sm = exported.get_manager("spam")
  sm.activate(ses)
  exported.write_message("spam limits activated for session %s" % ses)
                                                                                                                             

commands_dict["spamlimit"] = (spamlimit_cmd, "text=
quiet:boolean=false")


 the problem is this: when i enter the same command 11 times in a row, i
get the error pasted below.

WARNING: Unhandled error encountered (14 out of 20).
engine: unhandled error in engine.
Traceback (most recent call last):
  File "/usr/lib/python2.2/site-packages/lyntin/engine.py", line 663, in
runengine
    e.execute()
  File "/usr/lib/python2.2/site-packages/lyntin/event.py", line 134, in
execute
    exported.lyntin_command(self._input, internal=self._internal,
session=self._ses)
  File "/usr/lib/python2.2/site-packages/lyntin/exported.py", line 70,
in lyntin_command
    get_engine().handleUserData(text, internal)
  File "/usr/lib/python2.2/site-packages/lyntin/engine.py", line 454, in
handleUserData
    session.handleUserData(mem, internal)
  File "/usr/lib/python2.2/site-packages/lyntin/session.py", line 367,
in handleUserData
    self.writeSocket(input + "\n")
  File "/usr/lib/python2.2/site-packages/lyntin/session.py", line 262,
in writeSocket
    exported.hook_spam("to_mud_hook", {"session": self, "data": line,
"tag": tag})
  File "/usr/lib/python2.2/site-packages/lyntin/exported.py", line 542,
in hook_spam
    output = mem(argmap)
  File "/home/david/.lyntin/modules/spam.py", line 264, in handle_spam
    commands_dict = {}
TypeError: not all arguments converted


What i find confusing is that the line: "commands_dict = {}" is not even
in the handle_spam function. I realize this may be more of a python
question than a lyntin question, but this is my first real foray into
either one, so its hard for me to tell if where exactly the problem
lies.

if anyone has any suggestions, i'm all ears.

-davidc



-----------[ full module code (if you're interested) ]--------------

"""
I'm not sure what this will do exactly yet.
Something to do with spam-kick prevention
"""

import string
from lyntin import manager, utils, exported
from lyntin.modules import modutils

class SpamData:
  def __init__(self):
    self._spam = ''
    self._ignore = []
    self._spamlimit = 20
    self._activated = 1 
    self._delimiter = 'spam'
    self._spamcount = 0
 
  def reset(self):
    """
    Reset spam and spamcount
    """
    self._spamcount = 0
    self._spam = ''

  def setLimit(self, limit):
    """
    @param limit: maximum consecutive times the same text can be spammed to the mud
    @type limit: integer
    """
    self._spamlimit = limit

  def getLimit(self):
    """
    @return: maximum consecutive times the same text can be spammed to the mud
    @rtype: integer 
    """
    return self._spamlimit

  def activate(self):
    """
    Activate spam-kick prevention
    """  
    self._activated = 1 

  def deactivate(self):
    """
    Deactivate spam-kick prevention
    """  
    self._activated = 0 

  def isActivated(self):
    """
    @return: whether spam-limiting is active or not
    @rtype: boolean
    """
    return self._activated

  def addIgnore(self, text):
    """
    Adds a text that will not be spam-limited
    
    @param text: the text to be ignored
    @type  text: string
    """
    self._ignore.append(text)

  def removeIgnore(self, text):
    """
    Adds a string that can be spammed to the mud with no intervention
    
    @param text: the text that will no longer be ignored 
    @type  text: string
    """
    self._ignore.remove(text)
  
  def isIgnored(self, text):
    """
    Checks to see if a given command is to be ignored and returns
    true if it is, false if it is not

    @param text: the text to check for
    @type text: string

    @return: indication of whether the given text is ignored or not
    @rtype: boolean
    """
    for item in self._ignore:
      if item == text:
        return 1 
    return 0 

  def setSpam(self, text):
    """
    Set the spam text
    
    @param text: text to be watched
    @type text: string
    """
    self._spam = text

  def getSpam(self):
    """
    Get the spam text
    
    @return: text to be watched
    @rtype: string
    """
    return self._spam

  def isSpam(self, text):
    """
    returns true if specified text is spam, false if not

    @param text: text to be checked for spam
    @type text: string

    @return: whether or not the text is spam
    @rtype: boolean
    """
    if text == self._spam:
      self.incrementSpamcount()
      return 1
    else:
      self._spam = text
      self._spamcount = 0
      return 0 
  
  def setDelimiter(self, text):
    """
    Set the delimiter used for interrupting spam

    @param text: spam delimiter
    @type text: string
    """
    self._delimiter = text

  def getDelimiter(self):
    """
    Get the delimiter used for interrupting spam

    @return: spam delimiter
    @rtype: string
    """
    return self._delimiter

  def incrementSpamcount(self):
    """
    Add one to the spam count
    """
    self._spamcount = self._spamcount + 1

  def decrementSpamcount(self):
    """
    Add negative one to the spam count
    """
    self._spamcount = self._spamcount - 1
    if self._spamcount < 0:
      self._spamcount = 0

  def isAtLimit(self, text):
    """
    returns true if specified text has been sent to the mud too many times, false otherwise
    
    @param text: text to check for spam
    @type text: string

    @return: whether the text has been over-spammed
    @rtype: boolean
    """
    if text == self._spam and not self.isIgnored(text):
      self._spamcount = self._spamcount + 1
      if not self._spamcount < self._spamlimit:
        return 1 
    return 0 


class SpamManager(manager.Manager):
  def __init__(self):
    self._spam = {}

  def addIgnore(self, ses, text):
    if not self._spam.has_key(ses):
      self._spam[ses] = SpamData()
    self._spam[ses].addIgnore(text)

  def removeIgnore(self, ses, text):
    if self._spam.has_key(ses):
      self._spam[ses].removeIgnore(text)

  def setLimit(self, ses, limit):
    if not self._spam.has_key(ses):
      self._spam[ses] = SpamData()
    self._spam[ses].setLimit(limit)

  def getLimit(self, ses):
    if self._spam.has_key(ses):
      return self._spam[ses].getLimit()

  def isAtLimit(self, ses, text):
    if self._spam.has_key(ses):
      return self._spam[ses].isAtLimit(text)

  def setSpam(self, ses, text):
    if not self._spam.has_key(ses):
      self._spam[ses] = SpamData()
    self._spam[ses].setSpam(text)

  def getSpam(self, ses):
    if self._spam.has_key(ses):
      return self._spam[ses].getSpam()

  def isSpam(self, ses, text):
    if self._spam.has_key(ses):
      return self._spam[ses].isSpam(text)

  def setDelimiter(self, ses, text):
    if not self._spam.has_key(ses):
      self._spam[ses] = SpamData()
    self._spam[ses].setDelimiter(text)

  def getDelimiter(self, ses):
    if self._spam.has_key(ses):
      return self._spam[ses].getDelimiter()

  def activate(self, ses):
    if not self._spam.has_key(ses):
      self._spam[ses] = SpamData()
    self._spam[ses].activate()

  def deactivate(self, ses):
    if self._spam.has_key(ses):
      self._spam[ses].deactivate()

  def isActivated(self, ses):
    if self._spam.has_key(ses):
      return self._spam[ses].isActivated()



def handle_spam(args):
  """
  Chccks to see if the text has been spammed to the spam limit, and if so, sends a delimiter to the mud

  @param text: text to check for spam
  @type text: string
  """
  ses = args["session"]
  text = args["data"]
  sm = exported.get_manager("spam")
  exported.write_message("this is what i'm seeing: " + text) #debug
  if sm.isActivated(ses):
    exported.write_message("spam limits are active.") #debug
    if sm.isSpam(ses, text):
      exported.write_message("'%s' is spam." % text) #debug
      if sm.isAtLimit(ses, text):
        exported.write_message("we are at the limit." % text) #debug
        exported.write_mud_data(sm.getDelimiter(ses), ses)
   
commands_dict = {}

def spamlimit_cmd(ses, args, input):
  """
    
  category: commands
  """
  sm = exported.get_manager("spam")
  sm.activate(ses)
  exported.write_message("spam limits activated for session %s" % ses)

commands_dict["spamlimit"] = (spamlimit_cmd, "text= quiet:boolean=false")


sm = None

def load():
  """ Initializes the module by binding all the commands."""
  global dm
  modutils.load_commands(commands_dict)
  sm = SpamManager()
  exported.add_manager("spam", sm)
  exported.hook_register("to_mud_hook", handle_spam)


def unload():
  """ Unloads the module by calling any unload/unbind functions."""
  global sm
  modutils.unload_commands(commands_dict.keys())
  exported.remove_manager("spam")
  exported.hook_unregister("to_mud_hook", handle_spam)

# Local variables:
# mode:python
# py-indent-offset:2
# tab-width:2
# End:




-------------------------------------------------------
This SF.net email sponsored by: Enterprise Linux Forum Conference & Expo
The Event For Linux Datacenter Solutions & Strategies in The Enterprise 
Linux in the Boardroom; in the Front Office; & in the Server Room 
http://www.enterpriselinuxforum.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.