Beispiel #1
0
    def expand(self, text):
        """
    Looks at mud data and performs any gags.

    It returns the final text--even if there were no gags.

    @param text: the text to expand gags in
    @type  text: string

    @return: the (un)adjusted text
    @rtype: string
    """
        if len(text) > 0:
            # check for antigags first
            for mem in self._antigags.values():
                if mem.search(ansi.filter_ansi(text)):
                    return text

            # check for gags
            for mem in self._gags.values():
                if mem.search(ansi.filter_ansi(text)):
                    tokens = ansi.split_ansi_from_text(text)
                    tokens = [m for m in tokens if ansi.is_color_token(m)]
                    return "".join(tokens)

        return text
Beispiel #2
0
  def expand(self, text):
    """
    Looks at mud data and performs any gags.

    It returns the final text--even if there were no gags.

    @param text: the text to expand gags in
    @type  text: string

    @return: the (un)adjusted text
    @rtype: string
    """
    if len(text) > 0:
      # check for antigags first
      for mem in self._antigags.values():
        if mem.search(ansi.filter_ansi(text)):
          return text

      # check for gags
      for mem in self._gags.values():
        if mem.search(ansi.filter_ansi(text)):
          tokens = ansi.split_ansi_from_text(text)
          tokens = [m for m in tokens if ansi.is_color_token(m)]
          return "".join(tokens)

    return text 
Beispiel #3
0
  def expand(self, text):
    """
    Looks at mud data and performs any highlights.

    It returns the final text--even if there were no highlights.

    @param text: the input text
    @type  text: string

    @return: the finalized text--even if no highlights were expanded
    @rtype: string
    """
    if text:
      faketext = ansi.filter_ansi(text)
      textlist = ansi.split_ansi_from_text(text)
      hlist = self._highlights.keys()
      hlist.sort()
      for mem in hlist:
        miter = self._highlights[mem][2].finditer(faketext)
        for m in miter:
          # we need to loop for multiple instances of the highlight
          begin, end = m.span()
          hl = self._highlights[mem][1]
          textlist = self.highlight(textlist, begin, end - begin, hl)

      # here we sweep through the text string to update our current
      # color and leftover color attributes
      self._currcolor, self._colorleftover = ansi.figure_color(textlist, self._currcolor, self._colorleftover)

      text = "".join(textlist)

    return text
Beispiel #4
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)
Beispiel #5
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
Beispiel #6
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
Beispiel #7
0
def handle_recv_data(args):
    msg = args["message"]

    if type(msg) == types.StringType:
        msg = message.Message(msg, message.LTDATA)
    if not hasattr(msg, "data"):
        return
    line = msg.data
    ses = msg.session

    output.speak(filter_ansi(line), 0)
    return line
Beispiel #8
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
Beispiel #9
0
  def mudfilter(self, args):
    """
    mud_filter_hook function for filtering incoming data from the mud.
    """
    ses = args["session"]
    text = args["dataadj"]

    if self._config.get("ansicolor") == 0:
      return ansi.filter_ansi(text)
    else:
      if self._highlights.has_key(ses):
        return self._highlights[ses].expand(text)

    return text
Beispiel #10
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] = []
Beispiel #11
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] = []
Beispiel #12
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)
Beispiel #13
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
Beispiel #14
0
def handle_recv_data(args):
    """This function should be called when some datas are received."""
    data = args["data"]
    srapi.sayStringA(filter_ansi(data), 0)
    return data