-
Notifications
You must be signed in to change notification settings - Fork 2
/
CallHistory.py
250 lines (221 loc) · 9.21 KB
/
CallHistory.py
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
#!/usr/bin/env python3
import datetime
import csv
import re
import subprocess
import collections
import operator
class AsteriskCallHistory():
"""docstring for AsteriskCallHistory"""
def __init__(self, configfile):
# Global Variables
self.configuration = configfile
self.timezone = 3
def getCallHistory(self, limit):
self.contacts = self.readContacts('cidname')
with open(self.configuration['options']['callDataFile'], newline='') as f:
reader = csv.reader(f)
asteriskCalls = list(reader)
asteriskCalls.reverse() # Read log botton up
# if limit:
# asteriskCalls = asteriskCalls[:limit]
callHistory = []
callHistoryExternal = []
callHistoryInternal = []
for callLog in asteriskCalls:
callFromType = "unknown"
callToType = "unknown"
accountcode = callLog[0]
log_from = callLog[1]
log_to = callLog[2]
log_dcontext = callLog[3]
log_callerid = next(iter(re.findall(r'\"(.*)\"', callLog[4])), '')
log_channel = re.findall(r'.*\/([1-9A-z_\s]+)-*.*', callLog[5])[0]
log_dstchannel = callLog[5]
log_function = '{0}({1})'.format(callLog[7], callLog[8])
log_time = callLog[9]
log_time = datetime.datetime.strptime(callLog[9], '%Y-%m-%d %H:%M:%S') # change it!
log_time += datetime.timedelta(hours=self.timezone)
log_time = log_time.strftime("%H:%M:%S %d-%m-%Y")
log_duration = str(datetime.timedelta(seconds=int(callLog[13])))
log_callStatus = callLog[14]
log_result = callLog[7] + ' ' + callLog[8]
if self.isInListType(log_to, self.configuration['options']['internalNumbers']):
callToType = "internal"
else:
callToType = "external"
if self.isInListType(log_from, self.configuration['options']['internalNumbers']):
callFromType = "internal"
else:
callFromType = "external"
if "Dial" in log_result:
log_result = ''
elif "block" in log_dcontext.lower():
log_callStatus = 'BLOCKED'
log_result = ''
elif "VoiceMail" in log_result:
log_callStatus = 'VOICEMAIL'
log_result = ''
elif "hangup" in log_result.lower():
log_callStatus = 'HANGUP'
log_result = ''
elif "telemarket" in log_result:
log_callStatus = 'BLOCKED'
log_result = ''
elif "WaitExten" in log_result:
log_callStatus = 'BLOCKED'
log_result = ''
else:
log_callStatus = 'Unknown'
log_from_id = self.getCallerID(log_from)
log_to_id = self.getCallerID(log_to)
if log_callStatus == "ANSWERED":
callStatusColor = "#030"
elif log_callStatus == "NO ANSWER":
callStatusColor = "#321"
elif log_callStatus == "BUSY":
callStatusColor = "#122"
elif log_callStatus == "FAILED":
callStatusColor = "#330000"
elif log_callStatus == "HANGUP":
callStatusColor = "#200"
elif log_callStatus == "VOICEMAIL":
callStatusColor = "#060"
elif log_callStatus == "BLOCKED":
callStatusColor = "#004"
else:
callStatusColor = ""
if callFromType == "internal" and callToType == "internal":
callType = "internal"
elif callFromType == "external" or callToType == "external":
callType = "external"
callHistoryTmp = ({
"callFrom": log_from,
"callFromID": log_from_id,
"callCallerID": log_callerid,
"callFromType": callFromType,
"callTo": log_to,
"callToID": log_to_id,
"callToType": callToType,
"callDuration": log_duration,
"callDate": log_time,
"callFunction": log_function,
"callChannel": log_channel,
"callStatus": log_callStatus,
"callResult": log_result,
"callColor": callStatusColor,
"callType": callType,
})
if len(callHistory) > 1:
if callHistory[-1]['callDate'] == callHistoryTmp['callDate']:
callHistory.pop()
callHistory.append(callHistoryTmp)
if callFromType == "internal" and callToType == "internal":
callHistoryInternal.append(callHistoryTmp)
elif callFromType == "external" or callToType == "external":
callHistoryExternal.append(callHistoryTmp)
if limit:
callHistoryExternal = callHistoryExternal[:limit]
callHistoryInternal = callHistoryInternal[:limit]
return callHistoryExternal, callHistoryInternal, callHistoryTmp
def getCurrentStatus(self):
self.contacts = self.readContacts('cidname')
currentCalls = []
cmd = "asterisk -vvvvvrx 'core show channels concise'"
out = subprocess.getoutput(cmd)
for row in out.split('\n'):
m = re.search(r'(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)!(.*)$', row)
if m:
currentCalls.append({
"Channel": m.group(1),
"Context": m.group(2),
"Extension": m.group(3),
"ExtensionName": self.getCallerID(m.group(3)),
"Prio": m.group(4),
"State": m.group(5),
"Application": m.group(6),
"Data": m.group(7),
"CallerID": m.group(8),
"CallerIDName": self.getCallerID(m.group(8)),
"Duration": str(datetime.timedelta(seconds=int(m.group(12)))),
"Accountcode": m.group(10),
"PeerAccount": m.group(9),
"BridgedTo": m.group(13),
"???": m.group(11),
"????": m.group(14)
})
return currentCalls
def isInListType(self, numberToCheck, listCallRegExp):
inListType = False
for callRegExp in listCallRegExp:
if callRegExp[0] == "_":
callRegExp = callRegExp.replace("_", "")
callRegExp = callRegExp.replace(".", "+")
callRegExp = callRegExp.replace("X", "\d") + "$"
pattern = re.compile(callRegExp)
if pattern.match(numberToCheck):
inListType = True
break
elif callRegExp == numberToCheck:
inListType = True
break
else:
inListType = False
return inListType
def getVoiceMails(self):
mailboxes = []
cmd = "asterisk -rx 'voicemail show users'"
out = subprocess.getoutput(cmd)
for row in out.split('\n'):
if len(row) >= 55:
mailboxes.append({
"context": row[0:10].strip(),
"mbox": row[10:16].strip(),
"user": row[16:42].strip(),
"zone": row[42:53].strip(),
"newMsg": row[53:].strip()
})
mailboxes.pop(0)
return mailboxes
def getPeers(self):
peers = []
cmd = "asterisk -rx 'sip show peers'"
out = subprocess.getoutput(cmd)
for row in out.split('\n'):
if len(row) >= 105:
peers.append({
"user": row[0:25].strip(),
"host": row[25:65].strip(),
"port": row[84:93].strip(),
"status": row[93:105].strip()
})
peers.pop(0)
return peers
def getContacts(self, table):
self.contacts = self.readContacts(table)
return self.contacts
def delContact(self, table, number):
cmd = "asterisk -rx 'database del {table} {number}'".format(table=table, number=number)
out = subprocess.getoutput(cmd)
return "done"
def addContact(self, table, number, name):
cmd = "asterisk -rx 'database put {table} {number} \"{name}\"'".format(table=table, number=number, name=name)
out = subprocess.getoutput(cmd)
return "done"
def readContacts(self, table):
contacts = {}
cmd = "asterisk -rx 'database show {table}'".format(table=table)
out = subprocess.getoutput(cmd)
for row in out.split('\n'):
m = re.search(r'/.+/([^\s]+)\s+:\s(.*)', row)
if m:
number = m.group(1).strip()
name = m.group(2).strip()
contacts[number] = name
oContacts = collections.OrderedDict(sorted(contacts.items(), key=operator.itemgetter(1)))
return oContacts
def getCallerID(self, number):
callerID = number
if number in self.contacts:
callerID = self.contacts[number]
return callerID