<commit_before># Copyright (c) 2014 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import asyncio
from socket import getfqdn

from pysasl import SASLAuth

from .exceptions import *  # NOQA

from .core import PymapError
from .parsing.primitives import List, Number
from .parsing.specials import FetchAttribute, DateTime
from .parsing.command import CommandAuth, CommandNonAuth, CommandSelect
from .parsing.response import *  # NOQA
from .parsing.response.code import *  # NOQA
from .parsing.response.specials import *  # NOQA

__all__ = ['CloseConnection', 'ConnectionState']

fqdn = getfqdn().encode('ascii')


class CloseConnection(PymapError):
    """Raised when the connection should be closed immediately after sending
    the provided response.

    :param response: The response to send before closing the connection.
    :type response: :class:`~pymap.parsing.response.Response`

    """

    def __init__(self, response):
        super().__init__()
        self.response = response


class ConnectionState(object):

    flags_attr = FetchAttribute(b'FLAGS')

    def __init__(self, transport, backend):
        super().__init__()
        self.transport = transport
        self.backend = backend
        self.auth = SASLAuth([b'PLAIN'])
        self.session = None
        self.selected = None
        self.expunge_buffer = set()
        self.before_exists = 0
        self.before_recent = 0
        self.capability = Capability([b'AUTH=%b' % mech.name
                                      for mech in self.auth])

    async def do_greeting(self):
        return ResponseOk(b'*', b'Server ready ' + fqdn, self.capability)

    async def do_authenticate(self, cmd, result):
        if not result:
            return ResponseNo(cmd.tag, b'Invalid authentication mechanism.')
        self.session = user = await self.backend(result)
        if user:
            return ResponseOk(cmd.tag, b'Authentication successful.')
        return ResponseNo(cmd.tag, b'Invalid authentication credentials.')

    async def do_capability(self, cmd):
        response = ResponseOk(cmd.tag, b'Capabilities listed.')
        response.add_data(self.capability.to_response())
        return response
<commit_msg>reflect change in pysasl usage<commit_after># Copyright (c) 2014 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import asyncio
from socket import getfqdn

from pysasl import SASLAuth

from .exceptions import *  # NOQA

from .core import PymapError
from .parsing.primitives import List, Number
from .parsing.specials import FetchAttribute, DateTime
from .parsing.command import CommandAuth, CommandNonAuth, CommandSelect
from .parsing.response import *  # NOQA
from .parsing.response.code import *  # NOQA
from .parsing.response.specials import *  # NOQA

__all__ = ['CloseConnection', 'ConnectionState']

fqdn = getfqdn().encode('ascii')


class CloseConnection(PymapError):
    """Raised when the connection should be closed immediately after sending
    the provided response.

    :param response: The response to send before closing the connection.
    :type response: :class:`~pymap.parsing.response.Response`

    """

    def __init__(self, response):
        super().__init__()
        self.response = response


class ConnectionState(object):

    flags_attr = FetchAttribute(b'FLAGS')

    def __init__(self, transport, backend):
        super().__init__()
        self.transport = transport
        self.backend = backend
        self.auth = SASLAuth([b'PLAIN'])
        self.session = None
        self.selected = None
        self.expunge_buffer = set()
        self.before_exists = 0
        self.before_recent = 0
        self.capability = Capability([b'AUTH=%b' % mech.name
                                      for mech in self.auth.server_mechanisms])

    async def do_greeting(self):
        return ResponseOk(b'*', b'Server ready ' + fqdn, self.capability)

    async def do_authenticate(self, cmd, result):
        if not result:
            return ResponseNo(cmd.tag, b'Invalid authentication mechanism.')
        self.session = user = await self.backend(result)
        if user:
            return ResponseOk(cmd.tag, b'Authentication successful.')
        return ResponseNo(cmd.tag, b'Invalid authentication credentials.')

    async def do_capability(self, cmd):
        response = ResponseOk(cmd.tag, b'Capabilities listed.')
        response.add_data(self.capability.to_response())
        return response

{'fraction_non_alphanumeric': 0.0804881816777383, 'fraction_numerical': 0.0018538544724239147, 'mean_word_length': 3.4709944751381214, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4461530', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1693, 'n_words': 668}