]>
git.ipfire.org Git - ipfire.org.git/blob - webapp/backend/talk.py
8 from misc
import Object
12 db_args
= self
.backend
.db
._db
_args
14 "database" : "kamailio",
17 self
._db
= database
.Connection(**db_args
)
23 def connect_to_asterisk(self
):
24 hostname
= self
.settings
.get("asterisk_hostname")
25 username
= self
.settings
.get("asterisk_username")
26 password
= self
.settings
.get("asterisk_password")
28 return asterisk
.AsteriskManager(
29 hostname
, username
=username
, password
=password
32 def get_phonebook(self
, account
=None):
34 for a
in self
.accounts
.list():
35 if account
and a
== account
:
38 if not a
.is_talk_enabled():
45 def user_is_online(self
, sip_id
):
46 res
= self
.db
.get("SELECT 1 FROM location WHERE username = %s \
47 AND expires >= NOW() LIMIT 1", sip_id
)
54 def get_lines(self
, account
=None):
57 if account
and not account
.is_talk_enabled():
61 res
= self
.db
.query("SELECT contact AS location, expires, user_agent, socket \
62 FROM location WHERE domain = %s AND username = %s ORDER BY expires DESC",
63 "ipfire.org", account
.sip_id
)
65 res
= self
.db
.query("SELECT username, domain, contact AS location, \
66 expires, user_agent, socket FROM location ORDER BY username, expires DESC")
74 row
.account
= accounts_cache
[row
.username
]
76 row
.account
= accounts_cache
[row
.username
] = \
77 self
.accounts
.get_by_sip_id(row
.username
)
81 # Check if TLS is used
82 row
.tls_enabled
= row
.socket
.startswith("tls:")
84 # Cut off everything after ;
85 row
.location
, sep
, rest
= row
.location
.partition(";")
91 def _process_sip_uri(self
, sip_uri
):
92 sip_uri
, delimiter
, rest
= sip_uri
.partition(";")
94 m
= re
.match(r
"^sip:([0-9]{4})@ipfire\.org", sip_uri
)
98 # Remove trailing default port
99 if sip_uri
.endswith(":5060"):
100 sip_uri
= sip_uri
.replace(":5060", "")
104 def _process_cdr(self
, entries
, replace_sip_uris
=False):
110 row
["caller"] = self
._process
_sip
_uri
(row
.caller
)
113 row
["caller_account"] = accounts_cache
[row
.caller
]
115 row
["caller_account"] = accounts_cache
[row
.caller
] = \
116 self
.accounts
.get_by_sip_id(row
.caller
)
119 row
["called"] = self
._process
_sip
_uri
(row
.called
)
122 row
["called_account"] = accounts_cache
[row
.called
]
124 row
["called_account"] = accounts_cache
[row
.called
] = \
125 self
.accounts
.get_by_sip_id(row
.called
)
127 if not row
.called_account
:
128 row
["called_conference"] = self
.get_conference(row
.called
)
130 row
["called_conference"] = None
133 row
["time"] = datetime
.datetime
.fromutctimestamp(row
.time
)
141 def get_call_log(self
, account
, limit
=25):
143 res
= self
.db
.query("SELECT * FROM cdr WHERE (caller = %s OR called = %s) \
144 ORDER BY time DESC LIMIT %s", account
.sip_id
, account
.sip_id
, limit
)
146 res
= self
.db
.query("SELECT * FROM cdr ORDER BY time DESC LIMIT %s", limit
)
148 return self
._process
_cdr
(res
)
150 def get_channels(self
, account
=None):
151 a
= self
.connect_to_asterisk()
154 for c
in a
.list_channels():
155 if account
and not account
.sip_id
in (c
.caller
, c
.callee
):
160 return sorted(channels
)
162 def get_channel(self
, channel_id
, account
=None):
163 channels
= self
.get_channels(account
=account
)
165 for channel
in channels
:
166 if channel
.id == channel_id
:
169 def get_ongoing_calls(self
, account
=None, sip_id
=None):
170 if account
and sip_id
is None:
171 sip_id
= account
.sip_id
173 # If the given account does not have a SIP ID,
174 # we not need to search for any active sessions
179 sip_id
= "sip:%s@%%" % sip_id
181 res
= self
.db
.query("SELECT from_uri AS caller, \
182 to_uri AS called, start_time AS time FROM dialog \
183 WHERE from_uri LIKE %s OR to_uri LIKE %s ORDER BY time",
186 res
= self
.db
.query("SELECT from_uri AS caller, to_uri AS called, \
187 start_time AS time FROM dialog ORDER BY time")
189 return self
._process
_cdr
(res
, replace_sip_uris
=True)
193 def get_favourite_contacts(self
, account
, limit
=6):
194 res
= self
.db
.query("SELECT src_user AS caller, dst_ouser AS called, \
195 COUNT(*) AS count FROM acc WHERE method = %s AND src_user = %s AND \
196 dst_ouser BETWEEN %s AND %s AND time >= NOW() - INTERVAL '1 year' \
197 GROUP BY caller, called ORDER BY count DESC LIMIT %s",
198 "INVITE", account
.sip_id
, "1000", "1999", limit
)
200 return self
._process
_cdr
(res
)
205 def conferences(self
):
208 for no
in range(1, 10):
209 conference
= Conference(self
.backend
, no
)
210 conferences
.append(conference
)
214 def get_conference(self
, id):
215 for c
in self
.conferences
:
216 if not c
.sip_id
== id:
222 class Conference(Object
):
223 def __init__(self
, backend
, no
):
224 Object
.__init
__(self
, backend
)
227 def __cmp__(self
, other
):
228 return cmp(self
.no
, other
.no
)
232 return "IPFire Conference Room %s" % self
.no
236 return "%s" % (9000 + self
.no
)
240 return "%s@ipfire.org" % self
.sip_id
243 def participants(self
):
244 if not hasattr(self
, "_participants"):
245 self
._participants
= self
.backend
.talk
.get_ongoing_calls(sip_id
=self
.sip_id
)
247 return self
._participants