From 1769d40d113b34a5560abfbe8e998c273d7710dc Mon Sep 17 00:00:00 2001 From: Lynesth Date: Wed, 31 Jul 2019 23:12:44 +1100 Subject: [PATCH 1/2] Function to parse short_message from deliver_sm --- smpplib/pdu.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/smpplib/pdu.py b/smpplib/pdu.py index 21712a7..3f8baba 100644 --- a/smpplib/pdu.py +++ b/smpplib/pdu.py @@ -22,6 +22,7 @@ """PDU module""" +import re import struct from smpplib import command_codes, consts @@ -48,6 +49,7 @@ class PDU(object): command = None status = None _sequence = None + dlr_regex = None def __init__(self, client=default_client(), **kwargs): """Singleton dummy client will be used if omitted""" @@ -104,6 +106,22 @@ def get_status_desc(self, status=None): return desc + @staticmethod + def set_delivey_receipt_regex(regex): + """To replace the regex used if SMSC uses a different format""" + PDU.dlr_regex = re.compile(regex, re.IGNORECASE) + + def parse_deliver_receipt(self): + """Parses the short_message property to retrieve deliver_sm infos""" + + if self.command != 'deliver_sm': + raise ValueError("Invalid PDU command: %s", self.command) + + if PDU.dlr_regex is None: + PDU.dlr_regex = re.compile(rb'^id:(?P\S+)\s+sub:(?P\S+)\s+dlvrd:(?P\S+)\s+submit date:(?P\S+)\s+done date:(?P\S+)\s+stat:(?P\S+)\s+err:(?P\S+)\s+Text:(?P.*)$', re.IGNORECASE) + + return PDU.dlr_regex.match(self.short_message).groupdict() + def parse(self, data): """Parse raw PDU""" From b961c52fa91e7579cfb44badff3b7fa1ca6cddb7 Mon Sep 17 00:00:00 2001 From: Lynesth Date: Wed, 31 Jul 2019 23:51:02 +1100 Subject: [PATCH 2/2] Fix for Python 2.7 --- smpplib/pdu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smpplib/pdu.py b/smpplib/pdu.py index 3f8baba..709057b 100644 --- a/smpplib/pdu.py +++ b/smpplib/pdu.py @@ -118,7 +118,7 @@ def parse_deliver_receipt(self): raise ValueError("Invalid PDU command: %s", self.command) if PDU.dlr_regex is None: - PDU.dlr_regex = re.compile(rb'^id:(?P\S+)\s+sub:(?P\S+)\s+dlvrd:(?P\S+)\s+submit date:(?P\S+)\s+done date:(?P\S+)\s+stat:(?P\S+)\s+err:(?P\S+)\s+Text:(?P.*)$', re.IGNORECASE) + PDU.dlr_regex = re.compile(br'^id:(?P\S+)\s+sub:(?P\S+)\s+dlvrd:(?P\S+)\s+submit date:(?P\S+)\s+done date:(?P\S+)\s+stat:(?P\S+)\s+err:(?P\S+)\s+Text:(?P.*)$', re.IGNORECASE) return PDU.dlr_regex.match(self.short_message).groupdict()