From 64325607f9b4ed72e4c95b49c9dec59cb8929747 Mon Sep 17 00:00:00 2001 From: Kirill Apanovich Date: Mon, 26 Nov 2018 23:01:18 +0400 Subject: [PATCH] Fix EventList events handling --- asterisk/ami/client.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/asterisk/ami/client.py b/asterisk/ami/client.py index 5c7154a..b1758b6 100644 --- a/asterisk/ami/client.py +++ b/asterisk/ami/client.py @@ -52,6 +52,16 @@ class AMIClient(object): asterisk_start_regex = re.compile('^Asterisk *Call *Manager/(?P([0-9]+\.)*[0-9]+)', re.IGNORECASE) asterisk_line_regex = re.compile(b'\r\n', re.IGNORECASE | re.MULTILINE) asterisk_pack_regex = re.compile(b'\r\n\r\n', re.IGNORECASE | re.MULTILINE) + # EventList start pattern + asterisk_start_list_regex = re.compile( + b'^Response: Success\r\nActionID: \d+\r\nEventList: start', + re.IGNORECASE | re.MULTILINE + ) + # EventList end pattern + asterisk_end_list_regex = re.compile( + b'EventList: Complete\r\n.+\r\n.+\r\n\r\n', + re.IGNORECASE | re.MULTILINE + ) def __init__(self, address='127.0.0.1', port=5038, encoding='utf-8', encoding_errors='replace', @@ -160,9 +170,20 @@ def _next_pack(self): yield self._decode_pack(pack) break while not self.finished.is_set(): - while self.asterisk_pack_regex.search(data): + is_list = self.asterisk_start_list_regex.match(data) + list_ends = self.asterisk_end_list_regex.search(data) + # If data has complete EventList even, get and yield it + if is_list and list_ends: + ending = list_ends.group() + (pack, data) = self.asterisk_end_list_regex.split(data, 1) + yield self._decode_pack(pack + ending) + # If data has Event divider CLRFCLRF and it is not EventList + # (has to starts with EventList start pattern + # get anf yield it + elif not is_list and self.asterisk_pack_regex.search(data): (pack, data) = self.asterisk_pack_regex.split(data, 1) yield self._decode_pack(pack) + # Read the data recv = self._socket.recv(self._buffer_size) if recv == b'': self.finished.set()