<commit_before>"""Notifier that sends messages through XMPP/JABBER"""

import logging

from notifiers.base_notifier import Notifier
import xmpp


_logger = logging.getLogger(__name__)


class XMPPNotifier(Notifier):
    """Notifier class to work with xmpp"""

    def __init__(self, config):
        """Override init to check settings"""
        self.xmpp_jid = config['xmpp_jid']
        self.xmpp_password = config['<PASSWORD>']
        self.xmpp_recipient = config['xmpp_recipient']
        self.xmpp_send_test = config.get('xmpp_send_test', False)
        super(XMPPNotifier, self).__init__(config)

    def check_requirements(self):
        """Log in to xmpp server and check credentials"""
        jid = xmpp.protocol.JID(self.xmpp_jid)
        try:
            cl = xmpp.Client(jid.getDomain(),debug=[])
            if not cl.connect():
                raise Exception("Connect failed.")

            if not cl.auth(jid.getNode(), self.xmpp_password, resource=jid.getResource()):
                raise Exception("Authentication failed.")

            if self.xmpp_send_test:
                if not cl.send(xmpp.protocol.Message(self.xmpp_recipient, 'Kimsufi Crawler: Initial Test Message')):
                    raise Exception("Failed to send message to %s" % (self.xmpp_recipient, ))
                _logger.info("XMPP sent: [%s] %s" % (self.xmpp_recipient, 'Kimsufi Crawler: Initial Test Message'))

        except Exception as ex:
            _logger.error("Cannot connect/auth to XMPP Server. "
                          "Correct your config and try again. Error details:")
            _logger.error(ex)
            raise
        _logger.info("XMPP connected.")

    def notify(self, title, text, url=False):
        """Send XMPP notification"""
        body = text + ' - ' + url
        jid = xmpp.protocol.JID(self.xmpp_jid)
        cl = xmpp.Client(jid.getDomain(),debug=[])
        if not cl.connect():
            raise Exception("Connect failed.")

        if not cl.auth(jid.getNode(), self.xmpp_password, resource=jid.getResource()):
            raise Exception("Authentication failed.")

        if not cl.send(xmpp.protocol.Message(self.xmpp_recipient, body)):
            raise Exception("Failed to send message to %s" % (self.xmpp_recipient, ))
        _logger.info("XMPP sent: [%s] %s" % (self.xmpp_recipient, body))<commit_msg>Support multiple recipients in XMPP<commit_after>"""Notifier that sends messages through XMPP/JABBER"""

import logging

from notifiers.base_notifier import Notifier
import xmpp


_logger = logging.getLogger(__name__)


class XMPPNotifier(Notifier):
    """Notifier class to work with xmpp"""

    def __init__(self, config):
        """Override init to check settings"""
        self.xmpp_jid = config['xmpp_jid']
        self.xmpp_password = config['<PASSWORD>']
        if not isinstance(config['xmpp_recipient'], list):
            self.xmpp_recipient = [ config['xmpp_recipient'] ]
        else:
            self.xmpp_recipient = config['xmpp_recipient']
        self.xmpp_send_test = config.get('xmpp_send_test', False)
        super(XMPPNotifier, self).__init__(config)

    def check_requirements(self):
        """Log in to xmpp server and check credentials"""
        jid = xmpp.protocol.JID(self.xmpp_jid)
        try:
            cl = xmpp.Client(jid.getDomain(),debug=[])
            if not cl.connect():
                raise Exception("Connect failed.")

            if not cl.auth(jid.getNode(), self.xmpp_password, resource=jid.getResource()):
                raise Exception("Authentication failed.")

            if self.xmpp_send_test:
                for recipient in self.xmpp_recipient:
                    if not cl.send(xmpp.protocol.Message(recipient, 'Kimsufi Crawler: Initial Test Message')):
                        raise Exception("Failed to send message to %s" % (recipient, ))
                    _logger.info("XMPP sent: [%s] %s" % (recipient, 'Kimsufi Crawler: Initial Test Message'))

        except Exception as ex:
            _logger.error("Cannot connect/auth to XMPP Server. "
                          "Correct your config and try again. Error details:")
            _logger.error(ex)
            raise
        _logger.info("XMPP connected.")

    def notify(self, title, text, url=False):
        """Send XMPP notification"""
        body = text + ' - ' + url
        jid = xmpp.protocol.JID(self.xmpp_jid)
        cl = xmpp.Client(jid.getDomain(),debug=[])
        if not cl.connect():
            raise Exception("Connect failed.")

        if not cl.auth(jid.getNode(), self.xmpp_password, resource=jid.getResource()):
            raise Exception("Authentication failed.")

        for recipient in self.xmpp_recipient:
            if not cl.send(xmpp.protocol.Message(recipient, body)):
                _logger.error("Failed to send message to %s" % (recipient, ))
            else:
                _logger.info("XMPP sent: [%s] %s" % (recipient, body))
{'fraction_non_alphanumeric': 0.11894184168012924, 'fraction_numerical': 0.0, 'mean_word_length': 2.461215932914046, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '633635', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1493, 'n_words': 354}