<commit_before># -*- coding: utf-8 -*- line endings: unix -*-
#------------------------------------------------------------------------------
#   miniboa.py
#   Copyright 2009 <NAME>
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain a
#   copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
# Changes made by pR0Ps.CM[at]gmail[dot]com on 18/07/2012
# -Updated for use with Python 3.x
# -Repackaged into a single file to simplify distribution
# -Other misc fixes and changes
#
# Report any bugs in this implementation to me (email above)
#------------------------------------------------------------------------------

#import logging
import socket
import select
import sys
import re
import time


# Don't want to use the global logging without configuration -WH
from ..core.logs import get_logger
logging = get_logger("miniboa")


#---[ Telnet Notes ]-----------------------------------------------------------
# (See RFC 854 for more information)
#
# Negotiating a Local Option
# --------------------------
#
# Side A begins with:
#
#    "IAC WILL/WONT XX"   Meaning "I would like to [use|not use] option XX."
#
# Side B replies with either:
#
#    "IAC DO XX"     Meaning "OK, you may use option XX."
#    "IAC DONT XX"   Meaning "No, you cannot use option XX."
#
#
# Negotiating a Remote Option
# ----------------------------
#
# Side A begins with:
#
#    "IAC DO/DONT XX"  Meaning "I would like YOU to [use|not use] option XX."
#
# Side B replies with either:
#
#    "IAC WILL XX"   Meaning "I will begin using option XX"
#    "IAC WONT XX"   Meaning "I will not begin using option XX"
#
#
# The syntax is designed so that if both parties receive simultaneous requests
# for the same option, each will see the other's request as a positive
# acknowledgement of it's own.
#
# If a party receives a request to enter a mode that it is already in, the
# request should not be acknowledged.


#--[ Global Constants ]--------------------------------------------------------

UNKNOWN = -1
## Cap sockets to 512 on Windows because winsock can only process 512 at time
## Cap sockets to 1000 on Linux because you can only have 1024 file descriptors
MAX_CONNECTIONS = 512 if sys.platform == 'win32' else 1000
PARA_BREAK = re.compile(r"(\n\s*\n)", re.MULTILINE)

ANSI_CODES = (
    ('^k', '\x1b[22;30m'),    # black
    ('^K', '\x1b[1;30m'),     # bright black (grey)
    ('^r', '\x1b[22;31m'),    # red
    ('^R', '\x1b[1;31m'),     # bright red
    ('^g', '\x1b[22;32m'),    # green
    ('^G', '\x1b[1;32m'),     # bright green
    ('^y', '\x1b[22;33m'),    # yellow
    ('^Y', '\x1b[1;33m'),     # bright yellow
    ('^b', '\x1b[22;34m'),    # blue
    ('^B', '\x1b[1;34m'),     # bright blue
    ('^m', '\x1b[22;35m'),    # magenta
    ('^M', '\x1b[1;35m'),     # bright magenta
    ('^c', '\x1b[22;36m'),    # cyan
    ('^C', '\x1b[1;36m'),     # bright cyan
    ('^w', '\x1b[22;37m'),    # white
    ('^W', '\x1b[1;37m'),     # bright white
    ('^0', '\x1b[40m'),       # black background
    ('^1', '\x1b[41m'),       # red background
    ('^2', '\x1b[42m'),       # green background
    ('^3', '\x1b[43m'),       # yellow background
    ('^4', '\x1b[44m'),       # blue background
    ('^5', '\x1b[45m'),       # magenta background
    ('^6', '\x1b[46m'),       # cyan background
    ('^d', '\x1b[39m'),       # default (should be white on black)
    ('^I', '\x1b[7m'),        # inverse text on
    ('^i', '\x1b[27m'),       # inverse text off
    ('^~', '\x1b[0m'),        # reset all
    ('^U', '\x1b[4m'),        # underline on
    ('^u', '\x1b[24m'),       # underline off
    ('^!', '\x1b[1m'),        # bold on
    ('^.', '\x1b[22m'),       # bold off
    ('^s', '\x1b[2J'),        # clear screen
    ('^l', '\x1b[2K'),        # clear to end of line
)

#--[ Telnet Commands ]---------------------------------------------------------

SE      = chr(240)      # End of subnegotiation parameters
NOP     = chr(241)      # No operation
DATMK   = chr(242)      # Data stream portion of a sync.
BREAK   = chr(243)      # NVT Character BRK
IP      = chr(244)      # Interrupt Process
AO      = chr(245)      # Abort Output
AYT     = chr(246)      # Are you there
EC      = chr(247)      # Erase Character
EL      = chr(248)      # Erase Line
GA      = chr(249)      # The Go Ahead Signal
SB      = chr(250)      # Sub-option to follow
WILL    = chr(251)      # Will; request or confirm option begin
WONT    = chr(252)      # Wont; deny option request
DO      = chr(253)      # Do = Request or confirm remote option
DONT    = chr(254)      # Don't = Demand or confirm option halt
IAC     = chr(255)      # Interpret as Command
SEND    = chr(  1)      # Sub-process negotiation SEND command
IS      = chr(  0)      # Sub-process negotiation IS command

#--[ Telnet Options ]----------------------------------------------------------

BINARY  = chr(  0)      # Transmit Binary
ECHO    = chr(  1)      # Echo characters back to sender
RECON   = chr(  2)      # Reconnection
SGA     = chr(  3)      # Suppress Go-Ahead
TTYPE   = chr( 24)      # Terminal Type
NAWS    = chr( 31)      # Negotiate About Window Size
LINEMO  = chr( 34)      # Line Mode

#--[ Connection Lost ]---------------------------------------------------------

class ConnectionLost(Exception):
    """
    Custom exception to signal a lost connection to the Telnet Server.
    """

#--[ Xterm-style client formatting ]-------------------------------------------


def strip_caret_codes(text):
    """
    Strip out any caret codes from a string.
    """
    ## temporarily escape out ^^
    text = text.replace('^^', '\x00')
    for token, foo in ANSI_CODES:
        text = text.replace(token, '')
    return text.replace('\x00', '^')


def colorize(text, ansi=True):
    """
    If the client wants ansi, replace the tokens with ansi sequences --
    otherwise, simply strip them out.
    """
    if ansi:
        text = text.replace('^^', '\x00')
        for token, code in ANSI_CODES:
            text = text.replace(token, code)
        text = text.replace('\x00', '^')
    else:
        text = strip_caret_codes(text)
    return text


def word_wrap(text, columns=80, indent=4, padding=2):
    """
    Given a block of text, breaks into a list of lines wrapped to
    length.
    """
    paragraphs = PARA_BREAK.split(text)
    lines = []
    columns -= padding
    for para in paragraphs:
        if para.isspace():
            continue
        line = ' ' * indent
        for word in para.split():<commit_msg>Miniboa ANSI Codes Update<commit_after># -*- coding: utf-8 -*- line endings: unix -*-
#------------------------------------------------------------------------------
#   miniboa.py
#   Copyright 2009 <NAME>
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain a
#   copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.
#------------------------------------------------------------------------------
#
#------------------------------------------------------------------------------
# Changes made by pR0Ps.CM[at]gmail[dot]com on 18/07/2012
# -Updated for use with Python 3.x
# -Repackaged into a single file to simplify distribution
# -Other misc fixes and changes
#
# Report any bugs in this implementation to me (email above)
#------------------------------------------------------------------------------

#import logging
import socket
import select
import sys
import re
import time


# Don't want to use the global logging without configuration -WH
from ..core.logs import get_logger
logging = get_logger("miniboa")


#---[ Telnet Notes ]-----------------------------------------------------------
# (See RFC 854 for more information)
#
# Negotiating a Local Option
# --------------------------
#
# Side A begins with:
#
#    "IAC WILL/WONT XX"   Meaning "I would like to [use|not use] option XX."
#
# Side B replies with either:
#
#    "IAC DO XX"     Meaning "OK, you may use option XX."
#    "IAC DONT XX"   Meaning "No, you cannot use option XX."
#
#
# Negotiating a Remote Option
# ----------------------------
#
# Side A begins with:
#
#    "IAC DO/DONT XX"  Meaning "I would like YOU to [use|not use] option XX."
#
# Side B replies with either:
#
#    "IAC WILL XX"   Meaning "I will begin using option XX"
#    "IAC WONT XX"   Meaning "I will not begin using option XX"
#
#
# The syntax is designed so that if both parties receive simultaneous requests
# for the same option, each will see the other's request as a positive
# acknowledgement of it's own.
#
# If a party receives a request to enter a mode that it is already in, the
# request should not be acknowledged.


#--[ Global Constants ]--------------------------------------------------------

UNKNOWN = -1
## Cap sockets to 512 on Windows because winsock can only process 512 at time
## Cap sockets to 1000 on Linux because you can only have 1024 file descriptors
MAX_CONNECTIONS = 512 if sys.platform == 'win32' else 1000
PARA_BREAK = re.compile(r"(\n\s*\n)", re.MULTILINE)

ANSI_CODES = {
    '^k': '\x1b[22;30m',    # black
    '^K': '\x1b[1;30m',     # bright black (grey)
    '^r': '\x1b[22;31m',    # red
    '^R': '\x1b[1;31m',     # bright red
    '^g': '\x1b[22;32m',    # green
    '^G': '\x1b[1;32m',     # bright green
    '^y': '\x1b[22;33m',    # yellow
    '^Y': '\x1b[1;33m',     # bright yellow
    '^b': '\x1b[22;34m',    # blue
    '^B': '\x1b[1;34m',     # bright blue
    '^m': '\x1b[22;35m',    # magenta
    '^M': '\x1b[1;35m',     # bright magenta
    '^c': '\x1b[22;36m',    # cyan
    '^C': '\x1b[1;36m',     # bright cyan
    '^w': '\x1b[22;37m',    # white
    '^W': '\x1b[1;37m',     # bright white
    '^0': '\x1b[40m',       # black background
    '^1': '\x1b[41m',       # red background
    '^2': '\x1b[42m',       # green background
    '^3': '\x1b[43m',       # yellow background
    '^4': '\x1b[44m',       # blue background
    '^5': '\x1b[45m',       # magenta background
    '^6': '\x1b[46m',       # cyan background
    '^d': '\x1b[39m',       # default (should be white on black)
    '^I': '\x1b[7m',        # inverse text on
    '^i': '\x1b[27m',       # inverse text off
    '^~': '\x1b[0m',        # reset all
    '^U': '\x1b[4m',        # underline on
    '^u': '\x1b[24m',       # underline off
    '^!': '\x1b[1m',        # bold on
    '^.': '\x1b[22m',       # bold off
    '^s': '\x1b[2J',        # clear screen
    '^l': '\x1b[2K',        # clear to end of line
}

#--[ Telnet Commands ]---------------------------------------------------------

SE      = chr(240)      # End of subnegotiation parameters
NOP     = chr(241)      # No operation
DATMK   = chr(242)      # Data stream portion of a sync.
BREAK   = chr(243)      # NVT Character BRK
IP      = chr(244)      # Interrupt Process
AO      = chr(245)      # Abort Output
AYT     = chr(246)      # Are you there
EC      = chr(247)      # Erase Character
EL      = chr(248)      # Erase Line
GA      = chr(249)      # The Go Ahead Signal
SB      = chr(250)      # Sub-option to follow
WILL    = chr(251)      # Will; request or confirm option begin
WONT    = chr(252)      # Wont; deny option request
DO      = chr(253)      # Do = Request or confirm remote option
DONT    = chr(254)      # Don't = Demand or confirm option halt
IAC     = chr(255)      # Interpret as Command
SEND    = chr(  1)      # Sub-process negotiation SEND command
IS      = chr(  0)      # Sub-process negotiation IS command

#--[ Telnet Options ]----------------------------------------------------------

BINARY  = chr(  0)      # Transmit Binary
ECHO    = chr(  1)      # Echo characters back to sender
RECON   = chr(  2)      # Reconnection
SGA     = chr(  3)      # Suppress Go-Ahead
TTYPE   = chr( 24)      # Terminal Type
NAWS    = chr( 31)      # Negotiate About Window Size
LINEMO  = chr( 34)      # Line Mode

#--[ Connection Lost ]---------------------------------------------------------

class ConnectionLost(Exception):
    """
    Custom exception to signal a lost connection to the Telnet Server.
    """

#--[ Xterm-style client formatting ]-------------------------------------------


def strip_caret_codes(text):
    """
    Strip out any caret codes from a string.
    """
    ## temporarily escape out ^^
    text = text.replace('^^', '\x00')
    for token, foo in ANSI_CODES.items():
        text = text.replace(token, '')
    return text.replace('\x00', '^')


def colorize(text, ansi=True):
    """
    If the client wants ansi, replace the tokens with ansi sequences --
    otherwise, simply strip them out.
    """
    if ansi:
        text = text.replace('^^', '\x00')
        for token, code in ANSI_CODES.items():
            text = text.replace(token, code)
        text = text.replace('\x00', '^')
    else:
        text = strip_caret_codes(text)
    return text


def word_wrap(text, columns=80, indent=4, padding=2):
    """
    Given a block of text, breaks into a list of lines wrapped to
    length.
    """
    paragraphs = PARA_BREAK.split(text)
    lines = []
    columns -= padding
    for para in paragraphs:
        if para.isspace():
            continue
        line = ' ' * indent
        for word in para.split():
{'fraction_non_alphanumeric': 0.22269383135257498, 'fraction_numerical': 0.034238822863610636, 'mean_word_length': 1.7894632991318074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2185825', 'n_tokens_mistral': 4631, 'n_tokens_neox': 4279, 'n_words': 1486}