diff --git a/aprslib/parsing/message.py b/aprslib/parsing/message.py index 3995629..e06ef88 100644 --- a/aprslib/parsing/message.py +++ b/aprslib/parsing/message.py @@ -72,25 +72,99 @@ def parse_message(body): else: logger.debug("Packet is just a regular message") parsed.update({'format': 'message'}) + has_matched = False + # APRS supports two different message formats: + # - the standard format which is described in 'aprs101.pdf': + # http://www.aprs.org/doc/APRS101.PDF + # - an addendum from 1999 which introduces a new format: + # http://www.aprs.org/aprs11/replyacks.txt + # + # A message (ack/rej as well as a standard msg text body) can either have: + # - no message number at all + # - a message number in the old format (1..5 characters / digits) + # - a message number in the new format (2 characters / digits) without trailing 'ack msg no' + # - a message number in the new format with trailing 'free ack msg no' (2 characters / digits) + + # + # ack / rej parser + # + # search for: ack/rej new format with msgNo present and ackMsgNo present + if not has_matched: + match = re.search(r"^(ack|rej)([A-Za-z0-9]{2})}([A-Za-z0-9]{2})$", body) + if match: + parsed.update({ + 'response': match.group(1), + 'msgNo': match.group(2), + 'ackMsgNo': match.group(3) + }) + if "message_text" in parsed: + parsed.pop("message_text") + has_matched = True + + # search for: ack/rej new format with msgNo present and ackMsgNo not present + if not has_matched: + match = re.search(r"^(ack|rej)([A-Za-z0-9]{2})}$", body) + if match: + parsed.update({ + 'response': match.group(1), + 'msgNo': match.group(2), + }) + if "message_text" in parsed: + parsed.pop("message_text") + has_matched = True + + # search for: ack/rej standard format as per aprs101.pdf chapter 14 match = re.search(r"^(ack|rej)([A-Za-z0-9]{1,5})$", body) if match: parsed.update({ 'response': match.group(1), 'msgNo': match.group(2), }) - else: - body = body[0:70] + if "message_text" in parsed: + parsed.pop("message_text") + has_matched = True - match = re.search(r"{([A-Za-z0-9]{1,5})$", body) + # + # regular message body parser + # + if not has_matched: + # new message format: http://www.aprs.org/aprs11/replyacks.txt + # search for: msgText/msgNo/ackMsgNo all present + match = re.search(r"{([A-Za-z0-9]{2})}([A-Za-z0-9]{2})$", body) if match: msgNo = match.group(1) - body = body[:len(body) - 1 - len(msgNo)] - + ackMsgNo = match.group(2) + body = body[:len(body) - 3 - len(msgNo) - len(ackMsgNo)] parsed.update({'msgNo': msgNo}) + parsed.update({'ackMsgNo': ackMsgNo}) + has_matched = True + parsed.update({'message_text': body[0:67].strip()}) - parsed.update({'message_text': body.strip(' ')}) + # new message format: http://www.aprs.org/aprs11/replyacks.txt + # search for: msgText/msgNo present and ackMsgNo not present + if not has_matched: + match = re.search(r"{([A-Za-z0-9]{2})}$", body) + if match: + msgNo = match.group(1) + body = body[:len(body) - 2 - len(msgNo)] + parsed.update({'msgNo': msgNo}) + has_matched = True + body = body[0:67].strip() + parsed.update({'message_text': body[0:67].strip()}) - break + # old message format - see aprs101.pdf. + # search for: msgNo present + if not has_matched: + match = re.search(r"{([A-Za-z0-9]{1,5})$", body) + if match: + msgNo = match.group(1) + body = body[:len(body) - 1 - len(msgNo)] + parsed.update({'msgNo': msgNo}) + has_matched = True + parsed.update({'message_text': body[0:67].strip()}) + + # break free from the eternal 'while' + break return ('', parsed)