Example #1
0
    def data_out(self, text=None, **kwargs):
        """
        Data Evennia -> User. A generic hook method for engine to call
        in order to send data through the websocket connection.

        Kwargs:
            oob (str or tuple): Supply an Out-of-Band instruction.
            raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
            nomarkup (bool): Clean out all ansi/html markers and tokens.

        """
        try:
            text = to_str(text if text else "", encoding=self.encoding)
        except Exception as e:
            self.sendLine(str(e))
        if "oob" in kwargs:
            for cmdname, args, okwargs in kwargs["oob"]:
                self.json_encode(cmdname, *args, **okwargs)

        raw = kwargs.get("raw", False)
        nomarkup = kwargs.get("nomarkup", False)
        if "prompt" in kwargs:
            self.sendLine("PRT" + parse_html(kwargs["prompt"], strip_ansi=nomarkup))
        if raw:
            self.sendLine("CMD" + text)
        else:
            self.sendLine("CMD" + parse_html(text, strip_ansi=nomarkup))
Example #2
0
 def test_parse_html(self):
     self.assertEqual("foo", text2html.parse_html("foo"))
     self.maxDiff = None
     self.assertEqual(
         """<span class="blink"><span class="bgcolor-006">Hello </span><span class="underline"><span class="err">W</span><span class="err">o</span><span class="err">r</span><span class="err">l</span><span class="err">d</span><span class="err">!<span class="bgcolor-002">!</span></span></span></span>""",
         text2html.parse_html(ansi.ANSI_BLINK + ansi.ANSI_BACK_CYAN +
                              "Hello " + ansi.ANSI_NORMAL +
                              ansi.ANSI_UNDERLINE + ansi.ANSI_RED + "W" +
                              ansi.ANSI_GREEN + "o" + ansi.ANSI_YELLOW +
                              "r" + ansi.ANSI_BLUE + "l" +
                              ansi.ANSI_MAGENTA + "d" + ansi.ANSI_CYAN +
                              "!" + ansi.ANSI_BACK_GREEN + "!"),
     )
Example #3
0
 def test_parse_html(self):
     self.assertEqual("foo", text2html.parse_html("foo"))
     self.maxDiff = None
     self.assertEqual(
         # TODO: note that the blink is currently *not* correctly aborted
         # with |n here! This is probably not possible to correctly handle
         # with regex - a stateful parser may be needed.
         # blink back-cyan normal underline red green yellow blue magenta cyan back-green
         text2html.parse_html("|^|[CHello|n|u|rW|go|yr|bl|md|c!|[G!"),
         '<span class="blink">'
         '<span class="bgcolor-006">Hello</span>'  # noqa
         '<span class="underline">'
         '<span class="color-009">W</span>'  # noqa
         '<span class="color-010">o</span>'
         '<span class="color-011">r</span>'
         '<span class="color-012">l</span>'
         '<span class="color-013">d</span>'
         '<span class="color-014">!'
         '<span class="bgcolor-002">!</span>'  # noqa
         '</span>'
         '</span>'
         '</span>')
Example #4
0
    def send_text(self, *args, **kwargs):
        """
        Send text data. This will pre-process the text for
        color-replacement, conversion to html etc.

        Args:
            text (str): Text to send.

        Kwargs:
            options (dict): Options-dict with the following keys understood:
                - raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
                - nocolor (bool): Clean out all color.
                - screenreader (bool): Use Screenreader mode.
                - send_prompt (bool): Send a prompt with parsed html

        """
        if args:
            args = list(args)
            text = args[0]
            if text is None:
                return
        else:
            return
        # just to be sure
        text = to_str(text)

        flags = self.protocol_flags

        options = kwargs.pop("options", {})
        raw = options.get("raw", flags.get("RAW", False))
        client_raw = options.get("client_raw", False)
        nocolor = options.get("nocolor", flags.get("NOCOLOR", False))
        screenreader = options.get("screenreader",
                                   flags.get("SCREENREADER", False))
        prompt = options.get("send_prompt", False)

        if screenreader:
            # screenreader mode cleans up output
            text = parse_ansi(text, strip_ansi=True, xterm256=False, mxp=False)
            text = _RE_SCREENREADER_REGEX.sub("", text)
        cmd = "prompt" if prompt else "text"
        if raw:
            if client_raw:
                args[0] = text
            else:
                args[0] = html.escape(text)  # escape html!
        else:
            args[0] = parse_html(text, strip_ansi=nocolor)

        # send to client on required form [cmdname, args, kwargs]
        self.sendLine(json.dumps([cmd, args, kwargs]))
Example #5
0
    def send_text(self, *args, **kwargs):
        """
        Send text data. This will pre-process the text for
        color-replacement, conversion to html etc.

        Args:
            text (str): Text to send.

        Kwargs:
            options (dict): Options-dict with the following keys understood:
                - raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
                - nocolor (bool): Remove all color.
                - screenreader (bool): Use Screenreader mode.
                - send_prompt (bool): Send a prompt with parsed html

        """
        if args:
            args = list(args)
            text = args[0]
            if text is None:
                return
        else:
            return

        flags = self.protocol_flags
        text = utils.to_str(text)

        options = kwargs.pop("options", {})
        raw = options.get("raw", flags.get("RAW", False))
        xterm256 = options.get("xterm256", flags.get('XTERM256', True))
        useansi = options.get("ansi", flags.get('ANSI', True))
        nocolor = options.get(
            "nocolor",
            flags.get("NOCOLOR") or not (xterm256 or useansi))
        screenreader = options.get("screenreader",
                                   flags.get("SCREENREADER", False))
        prompt = options.get("send_prompt", False)

        if screenreader:
            # screenreader mode cleans up output
            text = parse_ansi(text, strip_ansi=True, xterm256=False, mxp=False)
            text = _RE_SCREENREADER_REGEX.sub("", text)
        cmd = "prompt" if prompt else "text"
        if raw:
            args[0] = text
        else:
            args[0] = parse_html(text, strip_ansi=nocolor)

        # send to client on required form [cmdname, args, kwargs]
        self.client.lineSend(self.csessid, [cmd, args, kwargs])
Example #6
0
    def send_text(self, *args, **kwargs):
        """
        Send text data. This will pre-process the text for
        color-replacement, conversion to html etc.

        Args:
            text (str): Text to send.

        Kwargs:
            options (dict): Options-dict with the following keys understood:
                - raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
                - nocolor (bool): Remove all color.
                - screenreader (bool): Use Screenreader mode.
                - send_prompt (bool): Send a prompt with parsed html

        """
        if args:
            args = list(args)
            text = args[0]
            if text is None:
                return
        else:
            return

        flags = self.protocol_flags
        text = utils.to_str(text, force_string=True)

        options = kwargs.pop("options", {})
        raw = options.get("raw", flags.get("RAW", False))
        xterm256 = options.get("xterm256", flags.get('XTERM256', True))
        useansi = options.get("ansi", flags.get('ANSI', True))
        nocolor = options.get("nocolor", flags.get("NOCOLOR") or not (xterm256 or useansi))
        screenreader = options.get("screenreader", flags.get("SCREENREADER", False))
        prompt = options.get("send_prompt", False)

        if screenreader:
            # screenreader mode cleans up output
            text = parse_ansi(text, strip_ansi=True, xterm256=False, mxp=False)
            text = _RE_SCREENREADER_REGEX.sub("", text)
        cmd = "prompt" if prompt else "text"
        if raw:
            args[0] = text
        else:
            args[0] = parse_html(text, strip_ansi=nocolor)

        # send to client on required form [cmdname, args, kwargs]
        self.client.lineSend(self.csessid, [cmd, args, kwargs])
Example #7
0
    def send_text(self, *args, **kwargs):
        """
        Send text data. This will pre-process the text for
        color-replacement, conversion to html etc.

        Args:
            text (str): Text to send.

        Kwargs:
            options (dict): Options-dict with the following keys understood:
                - raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
                - nomarkup (bool): Clean out all ansi/html markers and tokens.
                - screenreader (bool): Use Screenreader mode.
                - send_prompt (bool): Send a prompt with parsed html

        """
        if args:
            args = list(args)
            text = args[0]
            if text is None:
                return
        else:
            return

        flags = self.protocol_flags
        text = to_str(text, force_string=True)

        options = kwargs.pop("options", {})
        raw = options.get("raw", flags.get("RAW", False))
        nomarkup = options.get("nomarkup", flags.get("NOMARKUP", False))
        screenreader = options.get("screenreader", flags.get("SCREENREADER", False))
        prompt = options.get("send_prompt", False)

        if screenreader:
            # screenreader mode cleans up output
            text = parse_ansi(text, strip_ansi=True, xterm256=False, mxp=False)
            text = _RE_SCREENREADER_REGEX.sub("", text)
        cmd = "prompt" if prompt else "text"
        if raw:
            args[0] = text
        else:
            args[0] = parse_html(text, strip_ansi=nomarkup)
        print "send_text:", cmd, args, kwargs

        # send to client on required form [cmdname, args, kwargs]
        self.sendLine(json.dumps([cmd, args, kwargs]))
Example #8
0
    def data_out(self, text=None, **kwargs):
        """
        Data Evennia -> Player access hook.

        webclient flags checked are
        raw=True - no parsing at all (leave ansi-to-html markers unparsed)
        nomarkup=True - clean out all ansi/html markers and tokens

        """
        # string handling is similar to telnet
        try:
            text = utils.to_str(text if text else "", encoding=self.encoding)
            raw = kwargs.get("raw", False)
            nomarkup = kwargs.get("nomarkup", False)
            if raw:
                self.client.lineSend(self.suid, text)
            else:
                self.client.lineSend(self.suid,
                                     parse_html(text, strip_ansi=nomarkup))
            return
        except Exception:
            logger.log_trace()
Example #9
0
    def data_out(self, text=None, **kwargs):
        """
        Data Evennia -> Player access hook.

        webclient flags checked are
        raw=True - no parsing at all (leave ansi-to-html markers unparsed)
        nomarkup=True - clean out all ansi/html markers and tokens

        """
        # string handling is similar to telnet
        try:
            text = utils.to_str(text if text else "", encoding=self.encoding)
            raw = kwargs.get("raw", False)
            nomarkup = kwargs.get("nomarkup", False)
            if raw:
                self.client.lineSend(self.suid, text)
            else:
                self.client.lineSend(self.suid,
                                     parse_html(text, strip_ansi=nomarkup))
            return
        except Exception:
            logger.log_trace()
Example #10
0
class WebSocketClient(Protocol, Session):
    """
    Implements the server-side of the Websocket connection.
    """

    def connectionMade(self):
        """
        This is called when the connection is first established.

        """
        client_address = self.transport.client
        self.init_session("websocket", client_address, self.factory.sessionhandler)
        # watch for dead links
        self.transport.setTcpKeepAlive(1)
        self.sessionhandler.connect(self)

    def disconnect(self, reason=None):
        """
        Generic hook for the engine to call in order to
        disconnect this protocol.

        Args:
            reason (str): Motivation for the disconnection.

        """
        if reason:
            self.data_out(text=reason)
        self.connectionLost(reason)

    def connectionLost(self, reason):
        """
        This is executed when the connection is lost for whatever
        reason. it can also be called directly, from the disconnect
        method

        Args:
            reason (str): Motivation for the lost connection.

        """
        self.sessionhandler.disconnect(self)
        self.transport.close()

    def dataReceived(self, string):
        """
        Method called when data is coming in over the websocket
        connection.

        Args:
            string (str): Type of data is identified by a 3-character
                prefix:
                    - "OOB" This is an Out-of-band instruction. If so,
                        the remaining string should be a json-packed
                        string on the form {oobfuncname: [args, ], ...}
                    - "CMD" plain text data, to be treated like a game
                        input command.
        """
        mode = string[:3]
        data = string[3:]

        if mode == "OOB":
            # an out-of-band command
            self.json_decode(data)
        elif mode == "CMD":
            # plain text input
            self.data_in(text=data)

    def sendLine(self, line):
        """
        Send data to client.

        Args:
            line (str): Text to send.

        """
        return self.transport.write(line)

    def json_decode(self, data):
        """
        Decodes incoming data from the client.

        Args:
            data (JSON): JSON object to unpack.

        Raises:
            Exception: If receiving a malform OOB request.

        Notes:
            [cmdname, [args],{kwargs}] -> cmdname *args **kwargs

        """
        try:
            cmdname, args, kwargs = json.loads(data)
        except Exception:
            log_trace("Websocket malformed OOB request: %s" % data)
            raise
        self.sessionhandler.data_in(self, oob=(cmdname, args, kwargs))

    def json_encode(self, cmdname, *args, **kwargs):
        """
        Encode OOB data for sending to client.

        Args:
            cmdname (str): OOB command name.
            args, kwargs (any): Arguments to oob command.

        Notes:
            cmdname *args -> cmdname [json array]
            cmdname **kwargs -> cmdname {json object}

        """
        cmdtuple = [cmdname, list(args), kwargs]
        self.sendLine("OOB" + json.dumps(cmdtuple))

    def data_in(self, text=None, **kwargs):
        """
        Data User > Evennia.

        Kwargs:
            text (str): Incoming text.
            kwargs (any): Options from protocol.

        """
        self.sessionhandler.data_in(self, text=text, **kwargs)

    def data_out(self, text=None, **kwargs):
        """
        Data Evennia -> User. A generic hook method for engine to call
        in order to send data through the websocket connection.

        Kwargs:
            oob (str or tuple): Supply an Out-of-Band instruction.
            raw (bool): No parsing at all (leave ansi-to-html markers unparsed).
            nomarkup (bool): Clean out all ansi/html markers and tokens.

        """
        try:
            text = to_str(text if text else "", encoding=self.encoding)
        except Exception, e:
            self.sendLine(str(e))
        if "oob" in kwargs:
            for cmdname, args, okwargs in kwargs["oob"]:
                self.json_encode(cmdname, *args, **okwargs)

        raw = kwargs.get("raw", False)
        nomarkup = kwargs.get("nomarkup", False)
        if "prompt" in kwargs:
            self.sendLine("PRT" + parse_html(kwargs["prompt"], strip_ansi=nomarkup))
        if raw:
            self.sendLine("CMD" + text)
        else:
            self.sendLine("CMD" + parse_html(text, strip_ansi=nomarkup))