]>
Commit | Line | Data |
---|---|---|
66862195 MT |
1 | #!/usr/bin/python |
2 | ||
917434b8 MT |
3 | import ipaddress |
4 | import logging | |
77431b9c | 5 | import re |
9f05796c | 6 | import time |
77431b9c | 7 | |
5ac74b02 | 8 | from . import database |
66862195 | 9 | |
11347e46 | 10 | from .misc import Object |
13e89ccc MT |
11 | from .decorators import * |
12 | ||
13 | class Freeswitch(Object): | |
14 | @lazy_property | |
15 | def db(self): | |
16 | credentials = { | |
17 | "host" : self.settings.get("freeswitch_database_host"), | |
18 | "database" : self.settings.get("freeswitch_database_name", "freeswitch"), | |
19 | "user" : self.settings.get("freeswitch_database_user"), | |
20 | "password" : self.settings.get("freeswitch_database_password"), | |
21 | } | |
22 | ||
23 | return database.Connection(**credentials) | |
24 | ||
917434b8 MT |
25 | def get_sip_registrations(self, sip_uri): |
26 | logging.debug("Fetching SIP registrations for %s" % sip_uri) | |
27 | ||
28 | user, delim, domain = sip_uri.partition("@") | |
29 | ||
30 | res = self.db.query("SELECT * FROM sip_registrations \ | |
31 | WHERE sip_user = %s AND sip_host = %s AND expires >= EXTRACT(epoch FROM CURRENT_TIMESTAMP) \ | |
32 | ORDER BY contact", user, domain) | |
33 | ||
34 | for row in res: | |
35 | yield SIPRegistration(self, data=row) | |
36 | ||
9f05796c MT |
37 | def get_sip_channels(self, account): |
38 | res = self.db.query("SELECT * FROM channels \ | |
39 | WHERE (direction = %s AND cid_num = %s) OR \ | |
8476e80f MT |
40 | (direction = %s AND (callee_num = %s OR callee_num = ANY(%s))) \ |
41 | ORDER BY created_epoch", | |
42 | "inbound", account.sip_id, "outbound", account.sip_id, | |
43 | account._all_telephone_numbers) | |
9f05796c | 44 | |
9c50164e MT |
45 | channels = [] |
46 | ||
9f05796c | 47 | for row in res: |
9c50164e MT |
48 | c = Channel(self, data=row) |
49 | channels.append(c) | |
50 | ||
51 | return channels | |
9f05796c | 52 | |
bdaf6b46 | 53 | def get_cdr_by_account(self, account, date=None, limit=None): |
525c01f7 | 54 | res = self.db.query("SELECT * FROM cdr \ |
8476e80f MT |
55 | WHERE ((caller_id_number = ANY(%s) AND bleg_uuid IS NOT NULL) \ |
56 | OR (destination_number = ANY(%s) AND bleg_uuid IS NULL)) \ | |
bdaf6b46 | 57 | AND (%s IS NULL OR start_stamp::date = %s) \ |
8476e80f MT |
58 | ORDER BY end_stamp DESC LIMIT %s", account._all_telephone_numbers, |
59 | account._all_telephone_numbers, date, date, limit) | |
525c01f7 MT |
60 | |
61 | for row in res: | |
62 | yield CDR(self, data=row) | |
63 | ||
68ece434 MT |
64 | def get_call_by_uuid(self, uuid): |
65 | res = self.db.get("SELECT * FROM cdr \ | |
66 | WHERE uuid = %s", uuid) | |
67 | ||
68 | if res: | |
69 | return CDR(self, data=res) | |
70 | ||
917434b8 MT |
71 | |
72 | class SIPRegistration(object): | |
73 | def __init__(self, freeswitch, data): | |
74 | self.freeswitch = freeswitch | |
75 | self.data = data | |
76 | ||
77 | @lazy_property | |
78 | def protocol(self): | |
79 | m = re.match(r"Registered\(([A-Z]+)(\-NAT)?\)", self.data.status) | |
80 | ||
81 | if m: | |
82 | return m.group(1) | |
83 | ||
84 | @property | |
85 | def network_ip(self): | |
86 | return ipaddress.ip_address(self.data.network_ip) | |
87 | ||
88 | @property | |
89 | def network_port(self): | |
90 | return self.data.network_port | |
91 | ||
92 | @property | |
93 | def user_agent(self): | |
94 | return self.data.user_agent | |
95 | ||
96 | def is_reachable(self): | |
97 | return self.data.ping_status == "Reachable" | |
98 | ||
99 | @lazy_property | |
100 | def latency(self): | |
b78ec69b | 101 | if self.is_reachable() and self.data.ping_time: |
917434b8 MT |
102 | return self.data.ping_time / 1000.0 |
103 | ||
104 | ||
9f05796c MT |
105 | class Channel(object): |
106 | def __init__(self, freeswitch, data): | |
107 | self.freeswitch = freeswitch | |
108 | self.data = data | |
66862195 | 109 | |
9f05796c MT |
110 | @property |
111 | def backend(self): | |
112 | return self.freeswitch.backend | |
13e89ccc | 113 | |
9f05796c MT |
114 | @property |
115 | def uuid(self): | |
116 | return self.data.uuid | |
66862195 | 117 | |
9f05796c MT |
118 | @property |
119 | def direction(self): | |
120 | return self.data.direction | |
121 | ||
122 | @lazy_property | |
123 | def caller(self): | |
124 | return self.backend.accounts.get_by_sip_id(self.caller_number) | |
125 | ||
126 | @property | |
127 | def caller_name(self): | |
128 | return self.data.cid_name | |
129 | ||
130 | @property | |
131 | def caller_number(self): | |
132 | return self.data.cid_num | |
133 | ||
134 | @lazy_property | |
135 | def callee(self): | |
136 | return self.backend.accounts.get_by_sip_id(self.callee_number) | |
66862195 | 137 | |
9f05796c MT |
138 | @property |
139 | def callee_name(self): | |
140 | return self.data.callee_name | |
141 | ||
142 | @property | |
143 | def callee_number(self): | |
144 | return self.data.callee_num | |
145 | ||
146 | @property | |
147 | def called_number(self): | |
148 | return self.data.dest | |
149 | ||
150 | @property | |
151 | def application(self): | |
152 | return self.data.application | |
66862195 | 153 | |
9f05796c MT |
154 | @property |
155 | def application_data(self): | |
156 | return self.data.application_data | |
66862195 | 157 | |
9f05796c MT |
158 | @property |
159 | def duration(self): | |
160 | return time.time() - self.data.created_epoch | |
66862195 | 161 | |
9f05796c MT |
162 | @property |
163 | def codec(self): | |
164 | # We always assume a symmetric codec | |
165 | s = [ | |
e3dfabc1 | 166 | self.data.write_codec, |
9f05796c MT |
167 | ] |
168 | ||
e3dfabc1 MT |
169 | if self.data.write_rate: |
170 | s.append("%.0f kHz" % (int(self.data.write_rate) / 1000.0)) | |
171 | ||
9f05796c MT |
172 | if self.data.write_bit_rate == "0": |
173 | s.append("VBR") | |
174 | else: | |
175 | s.append("%.0f kBit/s" % (int(self.data.write_bit_rate) / 1000.0)) | |
66862195 | 176 | |
9f05796c MT |
177 | return " ".join(s) |
178 | ||
fe55357f MT |
179 | def is_secure(self): |
180 | if self.data.secure: | |
181 | return True | |
182 | ||
183 | return False | |
184 | ||
9f05796c MT |
185 | @property |
186 | def secure(self): | |
fe55357f MT |
187 | try: |
188 | transport_protocol, key_negotiation, cipher_suite = self.data.secure.split(":") | |
189 | except: | |
190 | return | |
191 | ||
192 | return "%s: %s" % (key_negotiation.upper(), cipher_suite.replace("_", "-")) | |
9f05796c MT |
193 | |
194 | ||
525c01f7 MT |
195 | class CDR(object): |
196 | def __init__(self, freeswitch, data): | |
197 | self.freeswitch = freeswitch | |
198 | self.data = data | |
199 | ||
200 | @property | |
201 | def backend(self): | |
202 | return self.freeswitch.backend | |
203 | ||
68ece434 MT |
204 | @property |
205 | def db(self): | |
206 | return self.freeswitch.db | |
207 | ||
208 | @property | |
209 | def uuid(self): | |
210 | return self.data.uuid | |
211 | ||
212 | @lazy_property | |
213 | def bleg(self): | |
214 | if self.data.bleg_uuid: | |
215 | return self.freeswitch.get_call_by_uuid(self.data.bleg_uuid) | |
216 | ||
217 | # If we are the bleg, we need to search for one where UUID is the bleg | |
218 | res = self.db.get("SELECT * FROM cdr WHERE bleg_uuid = %s", self.uuid) | |
219 | ||
220 | if res: | |
221 | return CDR(self.freeswitch, data=res) | |
222 | ||
525c01f7 MT |
223 | @property |
224 | def direction(self): | |
225 | if self.data.bleg_uuid: | |
226 | return "inbound" | |
227 | ||
228 | return "outbound" | |
229 | ||
230 | @lazy_property | |
231 | def caller(self): | |
232 | return self.backend.accounts.get_by_phone_number(self.data.caller_id_number) | |
233 | ||
234 | @property | |
235 | def caller_number(self): | |
236 | return self.data.caller_id_number | |
237 | ||
238 | @lazy_property | |
239 | def callee(self): | |
240 | return self.backend.accounts.get_by_phone_number(self.data.destination_number) | |
241 | ||
242 | @property | |
243 | def callee_number(self): | |
244 | return self.data.destination_number | |
245 | ||
246 | @property | |
247 | def time_start(self): | |
248 | return self.data.start_stamp | |
249 | ||
250 | @property | |
251 | def time_answered(self): | |
252 | return self.data.answer_stamp | |
253 | ||
254 | @property | |
255 | def duration(self): | |
256 | return self.data.duration | |
257 | ||
258 | @property | |
259 | def codec(self): | |
260 | return self.data.write_codec | |
261 | ||
262 | @property | |
263 | def user_agent(self): | |
68ece434 MT |
264 | if self.data.user_agent: |
265 | return self.data.user_agent.replace("_", " ") | |
525c01f7 | 266 | |
354445ed MT |
267 | @property |
268 | def size(self): | |
269 | return sum((self.data.rtp_audio_in_raw_bytes or 0, self.data.rtp_audio_out_raw_bytes or 0)) | |
270 | ||
271 | @property | |
272 | def mos(self): | |
273 | return self.data.rtp_audio_in_mos | |
274 | ||
525c01f7 | 275 | |
9f05796c MT |
276 | class Talk(Object): |
277 | def init(self): | |
278 | # Connect to FreeSWITCH | |
279 | self.freeswitch = Freeswitch(self.backend) | |
66862195 | 280 | |
66862195 MT |
281 | # Conferences |
282 | ||
283 | @property | |
284 | def conferences(self): | |
285 | conferences = [] | |
286 | ||
287 | for no in range(1, 10): | |
288 | conference = Conference(self.backend, no) | |
289 | conferences.append(conference) | |
290 | ||
291 | return conferences | |
292 | ||
293 | def get_conference(self, id): | |
294 | for c in self.conferences: | |
295 | if not c.sip_id == id: | |
296 | continue | |
297 | ||
298 | return c | |
299 | ||
300 | ||
301 | class Conference(Object): | |
302 | def __init__(self, backend, no): | |
303 | Object.__init__(self, backend) | |
304 | self.no = no | |
305 | ||
306 | def __cmp__(self, other): | |
307 | return cmp(self.no, other.no) | |
308 | ||
309 | @property | |
310 | def name(self): | |
311 | return "IPFire Conference Room %s" % self.no | |
312 | ||
313 | @property | |
314 | def sip_id(self): | |
315 | return "%s" % (9000 + self.no) | |
316 | ||
317 | @property | |
318 | def sip_url(self): | |
319 | return "%s@ipfire.org" % self.sip_id | |
320 | ||
321 | @property | |
322 | def participants(self): | |
323 | if not hasattr(self, "_participants"): | |
324 | self._participants = self.backend.talk.get_ongoing_calls(sip_id=self.sip_id) | |
325 | ||
326 | return self._participants |