/usr/share/pyshared/messaging/sms/deliver.py is in python-messaging 0.5.11+debian-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # see LICENSE
"""Classes for processing received SMS"""
from datetime import datetime, timedelta
from messaging.utils import (swap, swap_number, encode_bytes, debug,
unpack_msg, unpack_msg2, to_array)
from messaging.sms import consts
from messaging.sms.base import SmsBase
from messaging.sms.udh import UserDataHeader
class SmsDeliver(SmsBase):
"""I am a delivered SMS in your Inbox"""
def __init__(self, pdu, strict=True):
super(SmsDeliver, self).__init__()
self._pdu = None
self._strict = strict
self.date = None
self.mtype = None
self.sr = None
self.pdu = pdu
@property
def data(self):
"""
Returns a dict populated with the SMS attributes
It mimics the old API to ease the port to the new API
"""
ret = {
'text': self.text,
'pid': self.pid,
'dcs': self.dcs,
'csca': self.csca,
'number': self.number,
'type': self.type,
'date': self.date,
'fmt': self.fmt,
'sr': self.sr,
}
if self.udh is not None:
if self.udh.concat is not None:
ret.update({
'ref': self.udh.concat.ref,
'cnt': self.udh.concat.cnt,
'seq': self.udh.concat.seq,
})
return ret
def _set_pdu(self, pdu):
if not self._strict and len(pdu) % 2:
# if not strict and PDU-length is odd, remove the last character
# and make it even. See the discussion of this bug at
# http://github.com/pmarti/python-messaging/issues#issue/7
pdu = pdu[:-1]
if len(pdu) % 2:
raise ValueError("Can not decode an odd-length pdu")
# XXX: Should we keep the original PDU or the modified one?
self._pdu = pdu
data = to_array(self._pdu)
# Service centre address
smscl = data.pop(0)
if smscl > 0:
smscertype = data.pop(0)
smscl -= 1
self.csca = swap_number(encode_bytes(data[:smscl]))
if (smscertype >> 4) & 0x07 == consts.INTERNATIONAL:
self.csca = '+%s' % self.csca
data = data[smscl:]
else:
self.csca = None
# 1 byte(octet) == 2 char
# Message type TP-MTI bits 0,1
# More messages to send/deliver bit 2
# Status report request indicated bit 5
# User Data Header Indicator bit 6
# Reply path set bit 7
try:
self.mtype = data.pop(0)
except TypeError:
raise ValueError("Decoding this type of SMS is not supported yet")
mtype = self.mtype & 0x03
if mtype == 0x02:
return self._decode_status_report_pdu(data)
if mtype == 0x01:
raise ValueError("Cannot decode a SmsSubmitReport message yet")
sndlen = data.pop(0)
if sndlen % 2:
sndlen += 1
sndlen = int(sndlen / 2.0)
sndtype = (data.pop(0) >> 4) & 0x07
if sndtype == consts.ALPHANUMERIC:
# coded according to 3GPP TS 23.038 [9] GSM 7-bit default alphabet
sender = unpack_msg2(data[:sndlen]).decode("gsm0338")
else:
# Extract phone number of sender
sender = swap_number(encode_bytes(data[:sndlen]))
if sndtype == consts.INTERNATIONAL:
sender = '+%s' % sender
self.number = sender
data = data[sndlen:]
# 1 byte TP-PID (Protocol IDentifier)
self.pid = data.pop(0)
# 1 byte TP-DCS (Data Coding Scheme)
self.dcs = data.pop(0)
if self.dcs & (0x04 | 0x08) == 0:
self.fmt = 0x00
elif self.dcs & 0x04:
self.fmt = 0x04
elif self.dcs & 0x08:
self.fmt = 0x08
datestr = ''
# Get date stamp (sender's local time)
date = list(encode_bytes(data[:6]))
for n in range(1, len(date), 2):
date[n - 1], date[n] = date[n], date[n - 1]
data = data[6:]
# Get sender's offset from GMT (TS 23.040 TP-SCTS)
tz = data.pop(0)
offset = ((tz & 0x07) * 10 + ((tz & 0xf0) >> 4)) * 15
if (tz & 0x08):
offset = offset * -1
# 02/08/26 19:37:41
datestr = "%s%s/%s%s/%s%s %s%s:%s%s:%s%s" % tuple(date)
outputfmt = '%y/%m/%d %H:%M:%S'
sndlocaltime = datetime.strptime(datestr, outputfmt)
sndoffset = timedelta(minutes=offset)
# date as UTC
self.date = sndlocaltime - sndoffset
self._process_message(data)
def _process_message(self, data):
# Now get message body
msgl = data.pop(0)
msg = encode_bytes(data[:msgl])
# check for header
headlen = ud_len = 0
if self.mtype & 0x40: # UDHI present
ud_len = data.pop(0)
self.udh = UserDataHeader.from_bytes(data[:ud_len])
headlen = (ud_len + 1) * 8
if self.fmt == 0x00:
while headlen % 7:
headlen += 1
headlen /= 7
headlen = int(headlen)
if self.fmt == 0x00:
# XXX: Use unpack_msg2
data = data[ud_len:].tolist()
#self.text = unpack_msg2(data).decode("gsm0338")
self.text = unpack_msg(msg)[headlen:msgl].decode("gsm0338")
elif self.fmt == 0x04:
self.text = data[ud_len:].tostring()
elif self.fmt == 0x08:
data = data[ud_len:].tolist()
_bytes = [int("%02X%02X" % (data[i], data[i + 1]), 16)
for i in range(0, len(data), 2)]
self.text = u''.join(list(map(unichr, _bytes)))
pdu = property(lambda self: self._pdu, _set_pdu)
def _decode_status_report_pdu(self, data):
self.udh = UserDataHeader.from_status_report_ref(data.pop(0))
sndlen = data.pop(0)
if sndlen % 2:
sndlen += 1
sndlen = int(sndlen / 2.0)
sndtype = data.pop(0)
recipient = swap_number(encode_bytes(data[:sndlen]))
if (sndtype >> 4) & 0x07 == consts.INTERNATIONAL:
recipient = '+%s' % recipient
data = data[sndlen:]
date = swap(list(encode_bytes(data[:7])))
try:
scts_str = "%s%s/%s%s/%s%s %s%s:%s%s:%s%s" % tuple(date[0:12])
self.date = datetime.strptime(scts_str, "%y/%m/%d %H:%M:%S")
except (ValueError, TypeError):
scts_str = ''
debug('Could not decode scts: %s' % date)
data = data[7:]
date = swap(list(encode_bytes(data[:7])))
try:
dt_str = "%s%s/%s%s/%s%s %s%s:%s%s:%s%s" % tuple(date[0:12])
dt = datetime.strptime(dt_str, "%y/%m/%d %H:%M:%S")
except (ValueError, TypeError):
dt_str = ''
dt = None
debug('Could not decode date: %s' % date)
data = data[7:]
msg_l = [recipient, scts_str]
try:
status = data.pop(0)
except IndexError:
# Yes it is entirely possible that a status report comes
# with no status at all! I'm faking for now the values and
# set it to SR-UNKNOWN as that's all we can do
_status = None
status = 0x1
sender = 'SR-UNKNOWN'
msg_l.append(dt_str)
else:
_status = status
if status == 0x00:
msg_l.append(dt_str)
else:
msg_l.append('')
if status == 0x00:
sender = "SR-OK"
elif status == 0x1:
sender = "SR-UNKNOWN"
elif status == 0x30:
sender = "SR-STORED"
else:
sender = "SR-UNKNOWN"
self.number = sender
self.text = "|".join(msg_l)
self.fmt = 0x08 # UCS2
self.type = 0x03 # status report
self.sr = {
'recipient': recipient,
'scts': self.date,
'dt': dt,
'status': _status
}
|