@@ -52,6 +52,16 @@ class AMIClient(object):
52
52
asterisk_start_regex = re .compile ('^Asterisk *Call *Manager/(?P<version>([0-9]+\.)*[0-9]+)' , re .IGNORECASE )
53
53
asterisk_line_regex = re .compile (b'\r \n ' , re .IGNORECASE | re .MULTILINE )
54
54
asterisk_pack_regex = re .compile (b'\r \n \r \n ' , re .IGNORECASE | re .MULTILINE )
55
+ # EventList start pattern
56
+ asterisk_start_list_regex = re .compile (
57
+ b'^Response: Success\r \n ActionID: \d+\r \n EventList: start' ,
58
+ re .IGNORECASE | re .MULTILINE
59
+ )
60
+ # EventList end pattern
61
+ asterisk_end_list_regex = re .compile (
62
+ b'EventList: Complete\r \n .+\r \n .+\r \n \r \n ' ,
63
+ re .IGNORECASE | re .MULTILINE
64
+ )
55
65
56
66
def __init__ (self , address = '127.0.0.1' , port = 5038 ,
57
67
encoding = 'utf-8' , encoding_errors = 'replace' ,
@@ -160,9 +170,20 @@ def _next_pack(self):
160
170
yield self ._decode_pack (pack )
161
171
break
162
172
while not self .finished .is_set ():
163
- while self .asterisk_pack_regex .search (data ):
173
+ is_list = self .asterisk_start_list_regex .match (data )
174
+ list_ends = self .asterisk_end_list_regex .search (data )
175
+ # If data has complete EventList even, get and yield it
176
+ if is_list and list_ends :
177
+ ending = list_ends .group ()
178
+ (pack , data ) = self .asterisk_end_list_regex .split (data , 1 )
179
+ yield self ._decode_pack (pack + ending )
180
+ # If data has Event divider CLRFCLRF and it is not EventList
181
+ # (has to starts with EventList start pattern
182
+ # get anf yield it
183
+ elif not is_list and self .asterisk_pack_regex .search (data ):
164
184
(pack , data ) = self .asterisk_pack_regex .split (data , 1 )
165
185
yield self ._decode_pack (pack )
186
+ # Read the data
166
187
recv = self ._socket .recv (self ._buffer_size )
167
188
if recv == b'' :
168
189
self .finished .set ()
0 commit comments