<commit_before>"""
Copyright 2015 <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.
"""
from Logger import Logger
from ProfileLookup import ProfileLookup
from RedisConn import RedisConn


class SaslSenderDomainPolicy:
    """
    This class provides saslsender domain rate limiting
    """
    key = 'sasl_sender'
    prefix = 'SaslSenderPolicy_'
    quota = {}

    def __init__(self, parsed_config):
        self.parsed_config = parsed_config
        self.Enforce = parsed_config.getboolean('SaslSenderDomainPolicy', 'Enforce')
        self.RejectMessage = parsed_config.get('SaslSenderDomainPolicy', 'RejectMessage')
        self.ProfileLookupObj = ProfileLookup.create_profile_lookup('SaslSenderDomainPolicy', parsed_config)
        self.ProfileCacheTTL = parsed_config.getint('SaslSenderDomainPolicy', 'ProfileCacheTime')
        for i in parsed_config.items('SaslSenderDomainPolicy-Profiles'):
            limits = i[1].split(',')
            profile = i[0].lower()
            SaslSenderDomainPolicy.quota[profile] = (int(limits[0]), int(limits[1]))
        self.value = self.profile = self.error = None

    def check_quota(self, message, redis_pipe):
        self.error = False
        try:
            self.value = message.data[self.key].split('@')[1].lower()
            self.profile = self.ProfileLookupObj.lookup(self.value, self.ProfileCacheTTL)
            RedisConn.LUA_CALL_CHECK_LIMIT(keys=[SaslSenderDomainPolicy.prefix + self.value],
                                           args=[SaslSenderDomainPolicy.quota[self.profile][0]], client=redis_pipe)
        except IndexError:
            self.error = True
            self.message = message
            RedisConn.LUA_CALL_DO_NOTHING_SLAVE(keys=[], args=[], client=redis_pipe)<commit_msg>Bugfix in key in sasl sender domain ratelimiting<commit_after>"""
Copyright 2015 <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.
"""
from Logger import Logger
from ProfileLookup import ProfileLookup
from RedisConn import RedisConn


class SaslSenderDomainPolicy:
    """
    This class provides saslsender domain rate limiting
    """
    key = 'sasl_username'
    prefix = 'SaslSenderPolicy_'
    quota = {}

    def __init__(self, parsed_config):
        self.parsed_config = parsed_config
        self.Enforce = parsed_config.getboolean('SaslSenderDomainPolicy', 'Enforce')
        self.RejectMessage = parsed_config.get('SaslSenderDomainPolicy', 'RejectMessage')
        self.ProfileLookupObj = ProfileLookup.create_profile_lookup('SaslSenderDomainPolicy', parsed_config)
        self.ProfileCacheTTL = parsed_config.getint('SaslSenderDomainPolicy', 'ProfileCacheTime')
        for i in parsed_config.items('SaslSenderDomainPolicy-Profiles'):
            limits = i[1].split(',')
            profile = i[0].lower()
            SaslSenderDomainPolicy.quota[profile] = (int(limits[0]), int(limits[1]))
        self.value = self.profile = self.error = None

    def check_quota(self, message, redis_pipe):
        self.error = False
        try:
            self.value = message.data[self.key].split('@')[1].lower()
            self.profile = self.ProfileLookupObj.lookup(self.value, self.ProfileCacheTTL)
            RedisConn.LUA_CALL_CHECK_LIMIT(keys=[SaslSenderDomainPolicy.prefix + self.value],
                                           args=[SaslSenderDomainPolicy.quota[self.profile][0]], client=redis_pipe)
        except IndexError:
            self.error = True
            self.message = message
            RedisConn.LUA_CALL_DO_NOTHING_SLAVE(keys=[], args=[], client=redis_pipe)
{'fraction_non_alphanumeric': 0.09298206774407793, 'fraction_numerical': 0.006198804516271862, 'mean_word_length': 3.394941634241245, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4312779', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1252, 'n_words': 336}