]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/talk.py
7 from .misc
import Object
10 def get_phonebook(self
, account
=None):
12 for a
in self
.accounts
.list():
13 if account
and a
== account
:
16 if not a
.is_talk_enabled():
23 def user_is_online(self
, sip_id
):
24 res
= self
.db
.get("SELECT 1 FROM location WHERE username = %s \
25 AND expires >= NOW() LIMIT 1", sip_id
)
32 def get_lines(self
, account
=None):
35 if account
and not account
.is_talk_enabled():
39 res
= self
.db
.query("SELECT contact AS location, expires, user_agent, socket \
40 FROM location WHERE domain = %s AND username = %s ORDER BY expires DESC",
41 "ipfire.org", account
.sip_id
)
43 res
= self
.db
.query("SELECT username, domain, contact AS location, \
44 expires, user_agent, socket FROM location ORDER BY username, expires DESC")
52 row
.account
= accounts_cache
[row
.username
]
54 row
.account
= accounts_cache
[row
.username
] = \
55 self
.accounts
.get_by_sip_id(row
.username
)
59 # Check if TLS is used
60 row
.tls_enabled
= row
.socket
.startswith("tls:")
62 # Cut off everything after ;
63 row
.location
, sep
, rest
= row
.location
.partition(";")
69 def _process_sip_uri(self
, sip_uri
):
70 sip_uri
, delimiter
, rest
= sip_uri
.partition(";")
72 m
= re
.match(r
"^sip:([0-9]{4})@ipfire\.org", sip_uri
)
76 # Remove trailing default port
77 if sip_uri
.endswith(":5060"):
78 sip_uri
= sip_uri
.replace(":5060", "")
82 def _process_cdr(self
, entries
, replace_sip_uris
=False):
88 row
["caller"] = self
._process
_sip
_uri
(row
.caller
)
91 row
["caller_account"] = accounts_cache
[row
.caller
]
93 row
["caller_account"] = accounts_cache
[row
.caller
] = \
94 self
.accounts
.get_by_sip_id(row
.caller
)
97 row
["called"] = self
._process
_sip
_uri
(row
.called
)
100 row
["called_account"] = accounts_cache
[row
.called
]
102 row
["called_account"] = accounts_cache
[row
.called
] = \
103 self
.accounts
.get_by_sip_id(row
.called
)
105 if not row
.called_account
:
106 row
["called_conference"] = self
.get_conference(row
.called
)
108 row
["called_conference"] = None
111 row
["time"] = datetime
.datetime
.fromutctimestamp(row
.time
)
119 def get_call_log(self
, account
, limit
=25):
121 res
= self
.db
.query("SELECT * FROM cdr WHERE (caller = %s OR called = %s) \
122 ORDER BY time DESC LIMIT %s", account
.sip_id
, account
.sip_id
, limit
)
124 res
= self
.db
.query("SELECT * FROM cdr ORDER BY time DESC LIMIT %s", limit
)
126 return self
._process
_cdr
(res
)
128 def get_channels(self
, account
=None):
130 for c
in a
.list_channels():
131 if account
and not account
.sip_id
in (c
.caller
, c
.callee
):
136 return sorted(channels
)
138 def get_channel(self
, channel_id
, account
=None):
139 channels
= self
.get_channels(account
=account
)
141 for channel
in channels
:
142 if channel
.id == channel_id
:
145 def get_ongoing_calls(self
, account
=None, sip_id
=None):
146 if account
and sip_id
is None:
147 sip_id
= account
.sip_id
149 # If the given account does not have a SIP ID,
150 # we not need to search for any active sessions
155 sip_id
= "sip:%s@%%" % sip_id
157 res
= self
.db
.query("SELECT from_uri AS caller, \
158 to_uri AS called, start_time AS time FROM dialog \
159 WHERE from_uri LIKE %s OR to_uri LIKE %s ORDER BY time",
162 res
= self
.db
.query("SELECT from_uri AS caller, to_uri AS called, \
163 start_time AS time FROM dialog ORDER BY time")
165 return self
._process
_cdr
(res
, replace_sip_uris
=True)
169 def get_favourite_contacts(self
, account
, limit
=6):
170 res
= self
.db
.query("SELECT src_user AS caller, dst_ouser AS called, \
171 COUNT(*) AS count FROM acc WHERE method = %s AND src_user = %s AND \
172 dst_ouser BETWEEN %s AND %s AND time >= NOW() - INTERVAL '1 year' \
173 GROUP BY caller, called ORDER BY count DESC LIMIT %s",
174 "INVITE", account
.sip_id
, "1000", "1999", limit
)
176 return self
._process
_cdr
(res
)
181 def conferences(self
):
184 for no
in range(1, 10):
185 conference
= Conference(self
.backend
, no
)
186 conferences
.append(conference
)
190 def get_conference(self
, id):
191 for c
in self
.conferences
:
192 if not c
.sip_id
== id:
198 class Conference(Object
):
199 def __init__(self
, backend
, no
):
200 Object
.__init
__(self
, backend
)
203 def __cmp__(self
, other
):
204 return cmp(self
.no
, other
.no
)
208 return "IPFire Conference Room %s" % self
.no
212 return "%s" % (9000 + self
.no
)
216 return "%s@ipfire.org" % self
.sip_id
219 def participants(self
):
220 if not hasattr(self
, "_participants"):
221 self
._participants
= self
.backend
.talk
.get_ongoing_calls(sip_id
=self
.sip_id
)
223 return self
._participants