|
| 1 | +#-*- encoding: utf8 -*- |
| 2 | +'''SMS PDU encoding and decoding, including GSM-0338 character set. |
| 3 | +
|
| 4 | +Overview |
| 5 | +-------- |
| 6 | +
|
| 7 | +This library handles SMS-DELIVER and SMS-SUBMIT format PDUs, and includes |
| 8 | +full support for all data formats, flags and headers, and round-trips from |
| 9 | +PDU to object and back again. |
| 10 | +
|
| 11 | +It also includes convenience APIs for constructing new PDUs from text or |
| 12 | +data. |
| 13 | +
|
| 14 | +This library is very mature - it's been in production use for many years |
| 15 | +before the 1.0 release was made. It's also, as far as I'm aware, the most |
| 16 | +complete SMS PDU encoding and decoding library available. |
| 17 | +
|
| 18 | +The T39 functionality has been copied from the previous PyPI package with |
| 19 | +the same name as this library to provide some continuity. It is untested. |
| 20 | +
|
| 21 | +
|
| 22 | +PDU Interface |
| 23 | +------------- |
| 24 | +
|
| 25 | +Typical usage will involve the SMS_SUBMIT and SMS_DELIVER .fromPDU(), |
| 26 | +.toPDU() and .create() methods: |
| 27 | +
|
| 28 | +>>> from smspdu import SMS_SUBMIT |
| 29 | +>>> pdu = SMS_SUBMIT.create('sender', 'recipient', 'hello, world') |
| 30 | +>>> pdu.toPDU() |
| 31 | +'010010D0F2F2380D4F97DD7400000CE8329BFD6681EE6F399B0C' |
| 32 | +>>> pdu = smspdu.SMS_SUBMIT.fromPDU(_, 'sender') |
| 33 | +>>> pdu.user_data |
| 34 | +u'hello, world' |
| 35 | +
|
| 36 | +
|
| 37 | +Command-line Usage |
| 38 | +------------------ |
| 39 | +
|
| 40 | +To decode a PDU on the command-line (using python2.7+), use:: |
| 41 | +
|
| 42 | + % python -m smspdu 010010D0F2F2380D4F97DD7400000CE8329BFD6681EE6F399B0C |
| 43 | +
|
| 44 | + 010010D0F2F2380D4F97DD7400000CE8329BFD6681EE6F399B0C |
| 45 | + tp_mti = 1 (SMS-SUBMIT) |
| 46 | + sender = unknown |
| 47 | + tp_rd = 0 |
| 48 | + tp_vpf = 0 |
| 49 | + tp_vp = None |
| 50 | + tp_rp = 0 |
| 51 | + tp_udhi = 0 |
| 52 | + tp_srr = 0 |
| 53 | + tp_mr = 0 |
| 54 | + tp_al = 16 |
| 55 | + tp_toa = d0 (Alphanumeric; Unknown) |
| 56 | + (recipient) address = 'recipient' |
| 57 | + tp_pid = 0x00 (Normal Case) |
| 58 | + tp_dcs = 0x00 (Immedate Display, GSM-0338 Default Alphabet) |
| 59 | + tp_udl = 12 |
| 60 | + tp_ud = '\\xe82\\x9b\\xfdf\\x81\\xeeo9\\x9b\\x0c' |
| 61 | + datestamp = 11062712173200 |
| 62 | + user_data = u'hello, world' |
| 63 | + user_data_headers = [] |
| 64 | +
|
| 65 | +The first line re-displays the PDU with the various sections colourised. |
| 66 | +
|
| 67 | +Users of versions of Python 2.6 will need to run "python -m smspdu.pdu". |
| 68 | +
|
| 69 | +
|
| 70 | +SMS Text - Handling the Awesomeness of GSM 0338 |
| 71 | +------------------------------------------------ |
| 72 | +
|
| 73 | +First the basics; encoding some text: |
| 74 | +
|
| 75 | +>>> from smspdu import gsm0338 |
| 76 | +>>> c = gsm0338() |
| 77 | +>>> gsm_message = c.encode(u'test message') |
| 78 | +
|
| 79 | +And decoding that message: |
| 80 | +
|
| 81 | +>>> from smspdu import gsm0338 |
| 82 | +>>> c = gsm0338() |
| 83 | +>>> c.decode(gsm_message) |
| 84 | +u'test message' |
| 85 | +
|
| 86 | +The library also provides some functions for making text SMS-happy: |
| 87 | +
|
| 88 | +:func:`gsm0338_safe` |
| 89 | + A simplistic function which just replaces any characters in the unicode |
| 90 | + input. You should probably use :func:`attempt_encoding` instead since it |
| 91 | + tries to make the message appear the same. |
| 92 | +:func:`attempt_encoding` |
| 93 | + Attempt to encode the supplied text for SMS transmission in a single |
| 94 | + message. This will alter the message to replace accents and typography |
| 95 | + where necessary to reduce the per-character septet count. |
| 96 | +:func:`remove_accent` |
| 97 | + Used by :func:`attempt_encoding` to remove all accents from characters in |
| 98 | + the supplied text. |
| 99 | +:func:`remove_typography` |
| 100 | + Used by :func:`attempt_encoding` to replaced typograpically-correct |
| 101 | + punctuation with simplified GSM-0338 characters. |
| 102 | +:func:`decode_ascii_safe` |
| 103 | + Removes all non-printable, non-ASCII codes in the string. |
| 104 | +:func:`smpp_to_sms_data_coding` |
| 105 | + Attempt to convert the SMPP data coding scheme (SMPP v34) to a useful |
| 106 | + SMS PDU (GSM 03.38) data coding scheme. |
| 107 | +
|
| 108 | +Version History (in Brief) |
| 109 | +-------------------------- |
| 110 | +
|
| 111 | +- 1.0 the initial release based on mature internal ekit.com code |
| 112 | +
|
| 113 | +---- |
| 114 | +This code is copyright 2011 eKit.com Inc (http://www.ekit.com/) |
| 115 | +See the end of the source file for the license of use. |
| 116 | +
|
| 117 | +''' |
| 118 | + |
| 119 | +__version__ = '1.0' |
| 120 | + |
| 121 | +from .pdu import (SMS_SUBMIT, SMS_DELIVER, attempt_encoding, |
| 122 | + smpp_to_sms_data_coding, remove_accent, remove_typography, |
| 123 | + decode_ascii_safe) |
| 124 | +from .gsm0338 import Codec as gsm0338 |
| 125 | + |
| 126 | +def gsm0338_safe(message): |
| 127 | + '''Make the given unicode string gsm0338-safe by replacing out any |
| 128 | + characters not present in the above. |
| 129 | + ''' |
| 130 | + c = gsm0338() |
| 131 | + gsm_message = c.encode(message, 'replace') |
| 132 | + return c.decode(gsm_message)[0] |
| 133 | + |
| 134 | + |
| 135 | +# Copyright (c) 2011 eKit.com Inc (http://www.ekit.com/) |
| 136 | +# |
| 137 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 138 | +# of this software and associated documentation files (the "Software"), to deal |
| 139 | +# in the Software without restriction, including without limitation the rights |
| 140 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 141 | +# copies of the Software, and to permit persons to whom the Software is |
| 142 | +# furnished to do so, subject to the following conditions: |
| 143 | +# |
| 144 | +# The above copyright notice and this permission notice shall be included in |
| 145 | +# all copies or substantial portions of the Software. |
| 146 | +# |
| 147 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 148 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 149 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 150 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 151 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 152 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 153 | +# SOFTWARE. |
0 commit comments