def sendsms(self, phone, msgs):
        """
        Sends SMS using Uninett's mail-to-SMS gateway.

        :param phone: The phone number the messages are to be dispatched to.
        :param msgs: A list of message strings, ordered by descending severity.
                     Each message is a tuple with ID, text and severity of the
                     message.

        :returns: A five-tuple containing these values:
                  - The formatted SMS.
                  - A list of IDs of sent messages.
                  - A list of IDs of ignored messages.
                  - A boolean which is true for success and false for failure.
                  - An integer which is the sending ID, if available or 0
                    otherwise.
        """
        (sms, sent, ignored) = self.formatsms(msgs)

        try:
            message = EmailMessage(subject="sms {}".format(phone),
                                   to=[self.mailaddr],
                                   body=sms)
            message.send(fail_silently=False)
        except smtplib.SMTPException as error:
            raise DispatcherError("SMTP error: %s" % error)

        result = True
        smsid = 0
        return sms, sent, ignored, result, smsid
示例#2
0
    def sendsms(self, phone, msgs):
        """
        Send SMS using Gammu.

        Arguments:
            ``phone'' is the phone number the messages are to be dispatched to.
            ``msgs'' is a list of messages ordered with the most severe first.
            Each message is a tuple with ID, text and severity of the message.

        Returns five values:
            The formatted SMS.
            A list of IDs of sent messages.
            A list of IDs of ignored messages.
            A boolean which is true for success and false for failure.
            An integer which is the sending ID if available or 0 otherwise.
        """

        # Format SMS
        (sms, sent, ignored) = self.formatsms(msgs)
        sms = decode_sms_to_unicode(sms)

        # We got a python-gammu binding :-)
        sm = gammu.StateMachine()

        try:
            # Typically ~root/.gammurc or ~$NAV_USER/.gammurc
            sm.ReadConfig()
        except IOError as error:
            raise PermanentDispatcherError(error)

        try:
            # Fails if e.g. phone is not connected
            # See http://www.gammu.org/wiki/index.php?title=Gammu:Error_Codes
            # for complete list of errors fetched here
            sm.Init()
        except gammu.GSMError as error:
            raise PermanentDispatcherError(
                "GSM %s error %d: %s" %
                (error.args[0]['Where'], error.args[0]['Code'],
                 error.args[0]['Text']))

        message = {'Text': sms, 'SMSC': {'Location': 1}, 'Number': phone}

        try:
            # Tested with:
            # - Nokia 6610, Tekram IRmate 410U and Gammu 1.07.00
            # - Sony Ericsson K310, USB cable, Gammu 1.06.00, python-gammu 0.13
            smsid = sm.SendSMS(message)
        except gammu.GSMError as error:
            raise DispatcherError(
                "GSM %s error %d: %s" %
                (error.args[0]['Where'], error.args[0]['Code'],
                 error.args[0]['Text']))

        if isinstance(smsid, int):
            result = True
        else:
            result = False

        return sms, sent, ignored, result, smsid
示例#3
0
    def __init__(self, config):
        # Call mother's init
        Dispatcher.__init__(self)

        # Get config
        try:
            # Result of "dispatch" attempts
            self.result = config['result']
        except KeyError as error:
            raise DispatcherError("Config option not found: %s" % error)

        if self.result.lower() not in self.OPTIONS:
            raise DispatcherError("Invalid value %r for option 'result'. "
                                  "Must be one of: " + ", ".join(self.OPTIONS),
                                  self.result)
        else:
            self.result = self.result.lower()
    def __init__(self, config):
        """Constructor"""
        super(UninettMailDispatcher, self).__init__()

        try:
            # Mail address for gateway
            self.mailaddr = config['mailaddr']
        except KeyError as error:
            raise DispatcherError("Config option not found: %s" % error)
示例#5
0
    def __init__(self, config):
        """Constructor."""

        # Call mother's init
        Dispatcher.__init__(self)

        # Get config
        try:
            # Remote address for gateway
            self.url = config['url']
        except KeyError as error:
            raise DispatcherError("Config option not found: %s" % error)
示例#6
0
    def __init__(self, config):
        """Constructor."""

        # Call mother's init
        Dispatcher.__init__(self)

        # Get config
        try:
            # Remote address for gateway
            self.url = config['url']
            # Username for WebService
            self.username = config['username']
            # Password for WebService
            self.password = config['password']
            # Our phonenumber
            self.sender = config['sender']
        except KeyError as error:
            raise DispatcherError("Config option not found: %s" % error)

        # Initiate connector to Boost
        try:
            self.service = SOAPProxy(self.url)
        except Exception as error:
            raise DispatcherError("Failed to initialize SOAPProxy: %s" % error)
示例#7
0
    def sendsms(self, phone, msgs):
        """
        Send SMS using UNINETT's mail-to-SMS gateway.

        Arguments:
            ``phone'' is the phone number the messages are to be dispatched to.
            ``msgs'' is a list of messages ordered with the most severe first.
            Each message is a tuple with ID, text and severity of the message.

        Returns five values:
            The formatted SMS.
            A list of IDs of sent messages.
            A list of IDs of ignored messages.
            A boolean which is true for success and false for failure.
            An integer which is the sending ID if available or 0 otherwise.
        """

        # NOTE: This dispatcher should be made a general
        # SMS-via-mail-dispatcher if there is any wish for it.
        # This includes supporting various formats for the mail.

        # Format SMS
        (sms, sent, ignored) = self.formatsms(msgs)

        # Send SMS
        sender = "%s@%s" % (pwd.getpwuid(os.getuid())[0], socket.gethostname())
        headers = "From: %s\r\nTo: %s\r\nSubject: sms %s\r\n\r\n" % (
            sender, self.mailaddr, phone)
        message = headers + sms

        try:
            server = smtplib.SMTP('localhost')
            result = server.sendmail(sender, self.mailaddr, message)
            server.quit()
        except smtplib.SMTPException as error:
            raise DispatcherError("SMTP error: %s" % error)

        if len(result) == 0:
            # No errors
            result = True
        else:
            # If anything failed the SMTPException above should handle it
            result = False
        smsid = 0

        return (sms, sent, ignored, result, smsid)
示例#8
0
    def sendsms(self, phone, msgs):
        """Log SMS message and report pre-configured result."""

        # Format SMS
        (sms, sent_count, ignored_count) = self.formatsms(msgs)
        _logger.info("SMS to %s: %s", phone, sms)
        smsid = 0
        result = True

        if self.result == self.RESULT_FAIL:
            result = False
            _logger.info("Returning failed status")
        if self.result == self.RESULT_SUCCESS:
            _logger.info("Returning success status")
        elif self.result == self.RESULT_ERROR:
            _logger.info("Raising DispatcherError")
            raise DispatcherError("Failed, because I was configured to.")
        elif self.result == self.RESULT_PERMANENT:
            _logger.info("Raising PermanentDispatcherError")
            raise PermanentDispatcherError(
                "Failed permanently, because I was configured to.")

        return (sms, sent_count, ignored_count, result, smsid)
示例#9
0
This dispatcher sends SMS via Boost Communications' WebService (SOAP). Requires
a username and password to send SMS using the SOAP interface.

Contact http://www.boostcom.no/ to setup a contract.
The remote service is called ExternalSender.

Depends on SOAPpy/python-soappy.

"""

from nav.smsd.dispatcher import Dispatcher, DispatcherError

try:
    from SOAPpy import SOAPProxy
except ImportError as ie:
    raise DispatcherError('SOAPpy not installed or misconfigured.')


class BoostDispatcher(Dispatcher):
    """The smsd dispatcher for Boost Communications' External Sender."""
    def __init__(self, config):
        """Constructor."""

        # Call mother's init
        Dispatcher.__init__(self)

        # Get config
        try:
            # Remote address for gateway
            self.url = config['url']
            # Username for WebService