File: __init__.py

package info (click to toggle)
python-smstrade 0.2.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 216 kB
  • sloc: python: 748; makefile: 10
file content (725 lines) | stat: -rw-r--r-- 23,501 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# -*- python -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014  Jan Dittberner

import six

from ._version import __version__

if six.PY2:
    from ConfigParser import SafeConfigParser, NoOptionError
else:
    from configparser import ConfigParser as SafeConfigParser, NoOptionError
from base64 import b16encode
from datetime import datetime
from locale import getpreferredencoding
from pprint import pformat
import appdirs
import binascii
import argparse
import codecs
import decimal
import logging
import os
import requests


#: default configuration files
CONFIGFILES = [
    os.path.join(appdirs.site_config_dir('smstrade'),
                 'smstrade.ini'),
    os.path.join(appdirs.user_config_dir('smstrade'),
                 'smstrade.ini'),
    'smstrade.ini']

#: default values for configuration
DEFAULTS = {
    'url': 'https://gateway.smstrade.de/',
    'balanceurl': 'https://gateway.smstrade.de/credits/',
    'route': 'basic',
    'debug': 'True',  # debug is set to True to avoid costs
    'enable-cost': 'False',
    'enable-message-id': 'False',
    'enable-count': 'False',
    'enable-delivery-reports': 'False',
    'enable-response': 'False',
    'enable-concat': 'False',  # concat is disabled to avoid costs
    'charset': 'ascii',
}

#: characters allowed in GSM 03.38
GSM0338_CHARS = (
    u'@£$¥èéùìòÇ\nØø\rÅå'
    u'Δ_ΦΓΛΩΠΨΣΘΞ' + six.unichr(27) + u'ÆæÉ'
    u' !"#¤%&\'()*+,-./'
    u'0123456789:;<=>?'
    u'¡ABCDEFGHIJKLMNO'
    u'PQRSTUVWXYZÄÖÑܧ'
    u'abcdefghijklmno'
    u'pqrstuvwxyzäöñüà'
)

#: characters allowed in GSM 03.38 that occupy two octets
GSM0338_TWO_OCTET_CHARS = u'€' + six.unichr(12) + r'[\]^{|}~'

# Message types defined in API documentation
#: Message type flash
MESSAGE_TYPE_FLASH = 'flash'
#: Message type unicode
MESSAGE_TYPE_UNICODE = 'unicode'
#: Message type binary
MESSAGE_TYPE_BINARY = 'binary'
#: Message type voice
MESSAGE_TYPE_VOICE = 'voice'

# Route names defined in API documentation
#: Route basic
ROUTE_BASIC = 'basic'
#: Route gold
ROUTE_GOLD = 'gold'
#: Route direct
ROUTE_DIRECT = 'direct'

# Status codes defined in API documentation
#: Status receiver number not valid
STATUS_INVALID_RECEIVER_NUMBER = 10
#: Status sender number not valid
STATUS_INVALID_SENDER_NUMBER = 20
#: Status message text not valid
STATUS_INVALID_MESSAGE_TEXT = 30
#: Status message type not valid
STATUS_INVALID_MESSAGE_TYPE = 31
#: Status SMS route not valid
STATUS_INVALID_SMS_ROUTE = 40
#: Status identification failed
STATUS_IDENTIFICATION_FAILED = 50
#: Status not enough balance in account
STATUS_NOT_ENOUGH_BALANCE = 60
#: Status network does not support the route
STATUS_NETWORK_NOT_SUPPORTED_BY_ROUTE = 70
#: Status feature is not possible by the route
STATUS_FEATURE_NOT_POSSIBLE_FOR_ROUTE = 71
#: Status handover to SMSC failed
STATUS_SMSC_HANDOVER_FAILED = 80
#: Status SMS has been sent successfully
STATUS_OK = 100

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)


class SMSTradeError(Exception):
    """
    Custom exception type.

    """


class SMSTradeAPIBase(object):
    """
    Base class for smstrade.eu API clients.

    """

    def __init__(self, config=None, section='smstrade'):
        """
        Initialize a new SMSTradeAPIBase instance.

        :param ConfigParser config:
            use the specified configuration instead of the default
            configuration

        :param str section:
            use the specified section in the configuration

        """
        if config:
            self.config = config
        else:
            self.config = SafeConfigParser(defaults=DEFAULTS)
            conffiles = self.config.read(CONFIGFILES)
            if not conffiles:
                log.warning((
                    u'none of the configuration files (%s) found, trying'
                    u' to continue with default values and command line'
                    u' arguments'), ", ".join(CONFIGFILES))
                self.config.add_section(section)
        self.key = None
        if self.config.has_section(section):
            try:
                self.key = self.config.get(section, 'key')
            except NoOptionError as e:
                log.warning(u"configuration is incomplete: %s", e)
        else:
            log.warning(u"configuration section %s does not exist.", section)

    def prepare_args(self, kwargs):
        """
        Take a dictionary with keyword arguments, usually parsed from the
        command line and set existing attributes of this instance.

        :param dict kwargs:
            dictionary of keyword arguments

        """
        for key, value in kwargs.items():
            if value is not None:
                setattr(self, key, value)


class SMSTradeAPI(SMSTradeAPIBase):
    """
    Abstraction of the `smstrade.eu <http://smstrade.eu>`_ http(s) mail sending
    API.

    """

    def __init__(self, config=None, section='smstrade'):
        """
        Initialize a new SMSTradeAPI instance.

        :param ConfigParser config:
            use the specified configuration instead of the default
            configuration

        :param str section:
            use the specified section in the configuration

        """
        super(SMSTradeAPI, self).__init__(config, section)

        self.response = None
        self.sender = None
        self.reference = None
        self.senddate = None
        self.messagetype = None
        self.udh = None
        if self.config.has_section(section):
            try:
                self.url = self.config.get(section, 'url')
                self.route = self.config.get(section, 'route')
                self.debug = self.config.getboolean(section, 'debug')
                self.cost = self.config.getboolean(section, 'enable-cost')
                self.message_id = self.config.getboolean(
                    section, 'enable-message-id')
                self.count = self.config.getboolean(section, 'enable-count')
                self.reports = self.config.getboolean(
                    section, 'enable-delivery-reports')
                self.response = self.config.getboolean(
                    section, 'enable-response')
                self.concat = self.config.getboolean(section, 'enable-concat')
                self.charset = self.config.get(section, 'charset')
                self.sender = self.config.get(section, 'from')
            except NoOptionError as e:
                log.warning(u"configuration is incomplete: %s", e)
        else:
            log.warning(u"configuration section %s does not exist.", section)
        log.debug(self.__dict__)

    def _handle_response_body(self, body):
        """
        Handle parsing of response body.

        :param str body:
            response body
        """
        log.info("response body:\n%s", body)
        lines = body.splitlines()
        try:
            retval = {
                'status': int(lines[0]),
            }
            if self.message_id:
                retval['message_id'] = lines[1]
            if self.cost:
                retval['cost'] = decimal.Decimal(lines[2])
            if self.count:
                retval['count'] = int(lines[3])
        except IndexError:
            raise SMSTradeError('malformed response')
        return retval

    def _add_optional_flags(self, request_params):
        if self.debug:
            request_params['debug'] = 1
        if self.cost:
            request_params['cost'] = 1
        if self.message_id:
            request_params['message_id'] = 1
        if self.count:
            request_params['count'] = 1
        if self.reports:
            request_params['dlr'] = 1
        if self.response and self.route == ROUTE_BASIC:
            request_params['response'] = 1
        return request_params

    def _add_optional_fields(self, request_params):
        if self.reference is not None:
            request_params['ref'] = self.reference
        if self.senddate is not None:
            request_params['senddate'] = self.senddate
        if self.messagetype is not None:
            request_params['messagetype'] = self.messagetype
        return request_params

    def _build_request_parameters(self, recipient):
        """
        Build the request parameter dictionary based on fields in this
        SMSTradeAPI instance.

        :param str recipient:
            recipient calling number

        """
        request_params = {
            'key': self.key,
            'to': recipient,
            'route': self.route,
        }
        if self.route in (ROUTE_GOLD, ROUTE_DIRECT):
            request_params['from'] = self.sender.encode(self.charset)
        if self.charset != 'ascii':
            request_params['charset'] = self.charset
        request_params = self._add_optional_flags(request_params)
        request_params = self._add_optional_fields(request_params)
        return request_params

    def _send_normal_message(self, recipient, text):
        """
        Send a normal SMS message to the given recipient.

        :param str recipient:
            recipient calling number

        :param unicode text:
            unicode SMS message text

        """
        request_params = self._build_request_parameters(recipient)
        request_params['message'] = text.encode(self.charset)
        if self.concat:
            request_params['concat'] = 1
        response = requests.post(self.url, data=request_params)
        response.raise_for_status()
        return self._handle_response_body(response.text)

    def _send_unicode_message(self, recipient, text):
        """
        Send a unicode SMS message to the given recipient.

        :param str recipient:
            recipient calling number

        :param unicode text:
            unicode SMS message text

        """
        request_params = self._build_request_parameters(recipient)
        request_params['message'] = b16encode(text.encode('utf_16_be'))
        response = requests.post(self.url, data=request_params)
        response.raise_for_status()
        return self._handle_response_body(response.text)

    def _send_binary_message(self, recipient, text):
        """
        Send a binary SMS message to the given recipient.

        :param str recipient:
            recipient calling number

        :param str text:
            hexadecimal representation of the binary data

        """
        request_params = self._build_request_parameters(recipient)
        request_params['message'] = text
        if self.udh is not None:
            request_params['udh'] = self.udh
        response = requests.post(self.url, data=request_params)
        response.raise_for_status()
        return self._handle_response_body(response.text)

    def _send_voice_message(self, recipient, text):
        """
        Send a voice SMS message to the given recipient.

        :param str recipient:
            recipient calling number

        :param unicode text:
            the message text

        """
        request_params = self._build_request_parameters(recipient)
        request_params['message'] = text.encode(self.charset)
        response = requests.post(self.url, data=request_params)
        response.raise_for_status()
        return self._handle_response_body(response.text)

    def _send_message(self, recipient, text):
        """
        Send an SMS to a single recipient.

        :param str recipient:
            recipient calling number

        :param str text:
            SMS message text

        """
        if ((self.messagetype is None) or
                (self.messagetype == MESSAGE_TYPE_FLASH)):
            return self._send_normal_message(recipient, text)
        elif self.messagetype == MESSAGE_TYPE_UNICODE:
            return self._send_unicode_message(recipient, text)
        elif self.messagetype == MESSAGE_TYPE_BINARY:
            return self._send_binary_message(recipient, text)
        elif self.messagetype == MESSAGE_TYPE_VOICE:
            return self._send_voice_message(recipient, text)
        else:
            raise SMSTradeError(u"unknown message type %s" %
                                self.messagetype)

    def _gsm0338_length(self, text):
        charcount = 0
        for char in text:
            if char in GSM0338_CHARS:
                charcount += 1
            elif char in GSM0338_TWO_OCTET_CHARS:
                charcount += 2
            else:
                raise SMSTradeError(
                    u"character %s is not allowed in GSM messages." % char)
        return charcount

    def _check_normal_message(self, text):
        """
        Perform a plausibility check on the given message text.

        :param str text:
            the message to check

        """
        charcount = self._gsm0338_length(text)
        if ((self.concat and charcount > 1530) or
                (not self.concat and charcount > 160)):
            message = "too many characters in message"
            if not self.concat and charcount <= 1530:
                message += ", you may try to use concat"
            raise SMSTradeError(message)
        try:
            text.encode(self.charset)
        except ValueError:
            raise SMSTradeError((
                "The message can not be encoded with the chosen character set"
                " %s") % self.charset)

    def _check_unicode_message(self, text):
        """
        Perform a plausibility check on the given unicode message text.

        :param str text:
            the message to check

        """
        for char in text:
            code = ord(char)
            if (code >= 0xd800 and code <= 0xdfff) or (code > 0xffff):
                raise SMSTradeError(
                    u"the message can not be represented in UCS2")
        if len(text) > 70:
            raise SMSTradeError(
                u"too many characters in message, unicode SMS may contain up"
                u" to 70 characters")

    def _check_binary_message(self, text):
        """
        Perform a plausibility check on the given binary message text.

        :param str text:
            the message to check

        """
        try:
            length = len(codecs.decode(six.b(text.lower()), 'hex_codec'))
            if length > 140:
                raise SMSTradeError(
                    u'too many bytes in message, binary messages may contain'
                    u' up to 140 bytes')
        except:
            raise SMSTradeError('message cannot be encoded as bytes')

    def _check_voice_message(self, text):
        """
        Perform a plausibility check on the give message intended to be sent as
        voice message.

        :param str text:
            the message to check

        """
        if self._gsm0338_length(text) > 160:
            raise SMSTradeError(u'too many GSM characters in message')

    def _check_message(self, text):
        if ((self.messagetype is None) or
                (self.messagetype == MESSAGE_TYPE_FLASH)):
            self._check_normal_message(text)
        elif self.messagetype == MESSAGE_TYPE_UNICODE:
            self._check_unicode_message(text)
        elif self.messagetype == MESSAGE_TYPE_BINARY:
            self._check_binary_message(text)
        elif self.messagetype == MESSAGE_TYPE_VOICE:
            self._check_voice_message(text)
        else:
            raise SMSTradeError(
                u"message type %s is unknown" % self.messagetype)

    def send_sms(self, to, text, **kwargs):
        """
        Send an SMS to recipients in the to parameter.

        :param list to:
            list of recipient calling numbers

        :param str text:
            SMS message text

        :param dict kwargs:
            keyword arguments that override values in the configuration files

        """
        self.prepare_args(kwargs)

        retval = {}
        for recipient in to:
            self._check_message(text)
            retval[recipient] = self._send_message(recipient, text)
        return retval


class SMSTradeBalanceAPI(SMSTradeAPIBase):
    """
    Abstraction of the `smstrade.eu <http://smstrade.eu>`_ http(s) account
    balance API.

    """

    def __init__(self, config=None, section='smstrade'):
        """
        Initialize a new SMSTradeBalanceAPI instance.

        :param ConfigParser config:
            use the specified configuration instead of the default
            configuration

        :param str section:
            use the specified section in the configuration

        """
        super(SMSTradeBalanceAPI, self).__init__(config, section)

        if self.config.has_section(section):
            self.url = self.config.get(section, 'balanceurl')
        else:
            log.warning(u"configuration section %s does not exist.", section)
        log.debug(self.__dict__)

    def get_balance(self, **kwargs):
        """
        Get the account balance from smstrade.eu and return it as a
        :pyclass:`decimal.Decimal` instance.

        :param dict kwargs:
            keyword arguments that override values in the configuration files

        """
        self.prepare_args(kwargs)
        if self.key is None:
            raise SMSTradeError(
                u'you need to define an API key either in a configuration'
                u' file or on the command line')
        response = requests.get(self.url, params={'key': self.key})
        response.raise_for_status()
        return decimal.Decimal(response.text)


def hexstr(value):
    """
    Check whether the given hexadecimal value can successfully be decoded to
    bytes.

    :param str value:
        string of hexadecimal representations of bytes

    """
    try:
        codecs.decode(six.b(value.lower()), 'hex_codec')
    except TypeError as te:
        raise argparse.ArgumentTypeError(te.message)
    except binascii.Error as be:
        raise argparse.ArgumentTypeError(be)
    return value


def secondssinceepoch(seconds):
    """
    Check whether the given value is a valid timestamp after the current
    timestamp.

    :param str seconds:
        string value that represents an int specifying a timestamp relative to
        the UNIX epoch

    """
    value = int(seconds)
    if datetime.fromtimestamp(value) < datetime.now():
        raise argparse.ArgumentTypeError(
            "timestamps in the past are not allowed.")
    return value


def send_sms(cmdline=None):
    """
    Send SMS from the command line.

    :param list cmdline:
        list of command line arguments
    """
    parser = argparse.ArgumentParser(
        description="Send one or more SMS via the smstrade.eu API")
    parser.add_argument(
        '-c', '--config', type=argparse.FileType('r'),
        help=(
            "alternative configuration instead of the default configuration"
            "files:\n {0}").format(", ".join(CONFIGFILES)))
    parser.add_argument(
        '-s', '--section', type=str, default='smstrade',
        help='use the specified section in the configuration file')
    parser.add_argument(
        '-k', '--key', type=str,
        help="personal identification code")
    parser.add_argument(
        '-f', '--from', dest='sender', type=str,
        help="source identifier (ignored for route basic)")
    parser.add_argument(
        '-r', '--route', choices=[ROUTE_BASIC, ROUTE_GOLD, ROUTE_DIRECT],
        help='SMS route')
    parser.add_argument(
        '-d', '--debug', action='store_const', const=True,
        help='activate debug mode')
    parser.add_argument(
        '--cost', action='store_const', const=True,
        help='enable output of sending costs')
    parser.add_argument(
        '-m', '--message-id', action='store_const', const=True,
        dest='message_id',
        help='enable output of message ids')
    parser.add_argument(
        '--count', action='store_const', const=True,
        help='enable output of message count')
    parser.add_argument(
        '--dlr', action='store_const', const=True,
        dest='reports',
        help='enable delivery reports')
    parser.add_argument(
        '--response', action='store_const', const=True,
        help='enable receiving of reply SMS (only for route basic)')
    parser.add_argument(
        '--ref', type=str, dest='reference',
        help='add an own reference for this message')
    parser.add_argument(
        '-l', '--concat', action='store_const', const=True,
        help='send as linked (longer) SMS')
    parser.add_argument(
        '--charset', choices=['UTF-8', 'ISO-8859-1', 'ISO-8859-15'],
        help=(
            "character set of the message (defaults to current locale's"
            " charset) converted to UTF-8 if necessary"))
    parser.add_argument(
        '--senddate', type=secondssinceepoch,
        help='send time delayed SMS at the given time (UNIX timestamp)')
    parser.add_argument(
        '--messagetype', choices=[
            MESSAGE_TYPE_FLASH, MESSAGE_TYPE_UNICODE, MESSAGE_TYPE_BINARY,
            MESSAGE_TYPE_VOICE],
        help='enable sending of flash, unicode, binary or voice messages')
    parser.add_argument(
        '--udh', type=hexstr,
        help='hexadecimal representation of bytes for a binary message')
    parser.add_argument(
        '--version', action='version', version='%(prog)s ' + __version__)
    parser.add_argument(
        'to',
        nargs='+', type=str, help="receiver of the SMS",)
    parser.add_argument(
        'smstext',
        type=six.text_type,
        help="the text content of the SMS message to send")

    if cmdline is not None:
        args = parser.parse_args(cmdline)
    else:
        args = parser.parse_args()

    config = None
    if args.config:
        config = SafeConfigParser(defaults=DEFAULTS)
        config.readfp(args.config)

    smstrade = SMSTradeAPI(config=config, section=args.section)
    to = args.to
    smstext = args.smstext
    del args.config
    del args.section
    del args.to
    del args.smstext
    try:
        retval = smstrade.send_sms(to, smstext, **args.__dict__)
        log.info("Return value:\n%s", pformat(retval))
    except SMSTradeError as e:
        log.error(e)


def account_balance(cmdline=None):
    """
    Get the smstrade.eu account balance from the command line.

    :param list cmdline:
        list of command line arguments
    """
    parser = argparse.ArgumentParser(
        description="Send one or more SMS via the smstrade.eu API")
    parser.add_argument(
        '-c', '--config', type=argparse.FileType('r'),
        help=(
            "alternative configuration instead of the default configuration"
            "files:\n {0}").format(", ".join(CONFIGFILES)))
    parser.add_argument(
        '-s', '--section', type=str, default='smstrade',
        help='use the specified section in the configuration file')
    parser.add_argument(
        '-k', '--key', type=str,
        help="personal identification code")

    if cmdline is not None:
        args = parser.parse_args(cmdline)
    else:
        args = parser.parse_args()

    config = None
    if args.config:
        config = SafeConfigParser(defaults=DEFAULTS)
        config.readfp(args.config)

    smstradebalance = SMSTradeBalanceAPI(
        config=config, section=args.section)
    del args.config
    del args.section
    try:
        balance = smstradebalance.get_balance(**args.__dict__)
        log.info(u"Your smstrade balance : %.03f €", balance)
    except SMSTradeError as e:
        log.error(e)