Example #1
0
    def mudfilter(self, text):
        colorline = utils.filter_cm(text)
        nocolorline = ansi.filter_ansi(colorline)
        self._raw = colorline

        #exported.write_message("EventClassifier: DEBUG: " +
        #      "callbacks {0}".format(self.__callbacks))
        #exported.write_message("EventClassifier: DEBUG: " +
        #      "events {0}".format(self.__events))

        self._gag = False
        self._current = None
        #i = 0
        #j = 0
        #pattern = ""
        for t, list_r in self.__events.items():
            for r in list_r:
                match = r.search(nocolorline)
                #pattern = pattern + "e{0} ".format(i)
                #i = i + 1
                if match:
                    self._current = t
                    for c in self.__callbacks[t]:
                        #exported.write_message(u"EventClassifier: DEBUG: callback for {0}".format(t))
                        #pattern = pattern + "c{0} ".format(j)
                        #j = j + 1
                        c.on_event(t, match)
Example #2
0
  def checkActions(self, text):
    """
    Checks to see if text triggered any actions.  Any resulting 
    actions will get added as an InputEvent to the queue.

    @param text: the data coming from the mud to check for triggers
    @type  text: string
    """
    # FIXME - make sure this works even when lines are broken up.

    actionlist = self._actionlist
    if not actionlist:
      actionlist = [x for x in list(self._actions.values()) if x[6] not in self._disabled]
      actionlist.sort(key = lambda x:x[3])
      self._actionlist = actionlist

    colorline = utils.filter_cm(text)
    nocolorline = ansi.filter_ansi(colorline)

    # go through all the lines in the data and see if we have
    # any matches
    for (action, actioncompiled, response, color, priority, onetime, tag) in actionlist:
      if color:
        match = actioncompiled.search(colorline)
        line = colorline
      else:
        match = actioncompiled.search(nocolorline)
        line = nocolorline

      if match:
        # for every match we figure out what the expanded response
        # is and add it as an InputEvent in the queue.  the reason
        # we do a series of separate events rather than one big
        # event with ; separators is due to possible issues with 
        # braces and such in malformed responses.

        # get variables from the action
        actionvars = get_ordered_vars(action)

        # fill in values for all the variables in the match
        varvals = {}
        for i in range(len(actionvars)):
          varvals[actionvars[i]] = match.group(i+1)

        # add special variables
        varvals['a'] = line.replace(';', '_')
            
        # fill in response variables from those that
        # matched on the trigger
        response = utils.expand_vars(response, varvals)

        # event.InputEvent(response, internal=1, ses=self._ses).enqueue()
        try:
          exported.lyntin_command(response, internal=1, session=self._ses)
        except:
          exported.write_traceback()

        if onetime and action in self._actions:
          del self._actions[action]
          self._actionlist = None           # invalidate the list
Example #3
0
    def mudfilter(self, text):
        colorline = utils.filter_cm(text)
        nocolorline = ansi.filter_ansi(colorline)
        finalline = nocolorline.strip()

        if self._look_room is not None:
            self.mudfilter_looking(finalline)
        if self._expected_room is not None:
            self.mudfilter_moving(finalline)
        return text
Example #4
0
  def addToDataBuffer(self, text):
    """
    Adds data to the buffer by thinking about everything
    in terms of lines.
    
    @param text: the text to add to the buffer
    @type  text: string
    """
    text = ansi.filter_ansi(utils.filter_cm(text))
    lines = text.splitlines(1)

    for mem in lines:
      if len(self._databuffer) == 0 or self._databuffer[-1].endswith("\n"):
        self._databuffer.append(mem)
      else:
        self._databuffer[-1] += mem

    if len(self._databuffer) > self._databuffersize:
      self._databuffer[:-self._databuffersize] = []
Example #5
0
    def addToDataBuffer(self, text):
        """
    Adds data to the buffer by thinking about everything
    in terms of lines.
    
    @param text: the text to add to the buffer
    @type  text: string
    """
        text = ansi.filter_ansi(utils.filter_cm(text))
        lines = text.splitlines(1)

        for mem in lines:
            if len(self._databuffer) == 0 or self._databuffer[-1].endswith(
                    "\n"):
                self._databuffer.append(mem)
            else:
                self._databuffer[-1] += mem

        if len(self._databuffer) > self._databuffersize:
            self._databuffer[:-self._databuffersize] = []
Example #6
0
  def log(self, input):
    """
    Logs text to a file instance self._logfile and optionally
    filters ansi according to self._strip_ansi.

    @param input: the string to log to the logfile for this session
    @type  input: string
    """
    if self._logfile == None:
      return

    try:
      if self._strip_ansi == 1:
        input = ansi.filter_ansi(input)

      text = utils.filter_cm(input)
      text = text.replace("\n", os.linesep)
      self._logfile.write(text)
      self._logfile.flush()
    except:
      self._logfile = None
      exported.write_traceback("Logfile cannot be written to.", self._session)
Example #7
0
  def mudfilter(self, args):
    ses = args["session"]
    text = args["dataadj"]

    colorline = utils.filter_cm(text)
    nocolorline = ansi.filter_ansi(colorline)

    if nocolorline == "\n":
      return text
    #if self._enabled:
    self._event_classifier.mudfilter(text)

    if self._event_classifier.is_gagged():
      text = ""

    with open('nocolor.log', 'ab') as f:
      try:
        f.write(ansi.filter_ansi(text).encode("utf-8"))
      except TypeError:
        exported.write_message("sowmud.mudfilter: ERROR: unexpected return type by submodule: {0}".format(type(text)))
        text = ""
    self._proxy.send(text)
    return text
Example #8
0
  def checkActions(self, text):
    """
    Checks to see if text triggered any actions.  Any resulting 
    actions will get added as an InputEvent to the queue.

    @param text: the data coming from the mud to check for triggers
    @type  text: string
    """
    # FIXME - make sure this works even when lines are broken up.

    actionlist = self._actionlist
    if not actionlist:
      actionlist = filter(lambda x: not self._disabled.has_key(x[6]),
                          self._actions.values())
      actionlist.sort(key=lambda i: i[3])
      self._actionlist = actionlist

    colorline = utils.filter_cm(text)
    nocolorline = ansi.filter_ansi(colorline)

    # go through all the lines in the data and see if we have
    # any matches
    for (action, actioncompiled, response, color, priority, onetime, tag) in actionlist:
      if color:
        match = actioncompiled.search(colorline)
        line = colorline
      else:
        match = actioncompiled.search(nocolorline)
        line = nocolorline

      if match:
        # for every match we figure out what the expanded response
        # is and add it as an InputEvent in the queue.  the reason
        # we do a series of separate events rather than one big
        # event with ; separators is due to possible issues with 
        # braces and such in malformed responses.

        # fill in values for all the variables in the match
        varvals = {}
        if match.lastindex is not None:
          if action.startswith('r['):
            for i in xrange(match.lastindex):
              varvals[str(i+1)] = match.group(i+1)
          else:
            # get variables from the action
            actionvars = get_ordered_vars(action)
            for i in xrange(len(actionvars)):
              varvals[actionvars[i]] = match.group(i+1)

        # add special variables
        varvals['a'] = line.replace(';', '_')
            
        # fill in response variables from those that
        # matched on the trigger
        response = utils.expand_vars(response, varvals)

        # event.InputEvent(response, internal=1, ses=self._ses).enqueue()
        try:
          exported.lyntin_command(response, internal=1, session=self._ses)
        except:
          exported.write_traceback()

        if onetime and self._actions.has_key(action):
          del self._actions[action]
          self._actionlist = None           # invalidate the list