]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/talk.py
7 from misc
import Object
11 db_args
= self
.backend
.db
._db
_args
13 "database" : "kamailio",
16 self
._db
= database
.Connection(**db_args
)
22 def get_phonebook(self
, account
=None):
24 for a
in self
.accounts
.list():
25 if account
and a
== account
:
28 if not a
.is_talk_enabled():
35 def user_is_online(self
, sip_id
):
36 res
= self
.db
.get("SELECT 1 FROM location WHERE username = %s \
37 AND expires >= NOW() LIMIT 1", sip_id
)
44 def get_lines(self
, account
=None):
47 if account
and not account
.is_talk_enabled():
51 res
= self
.db
.query("SELECT contact AS location, expires, user_agent, socket \
52 FROM location WHERE domain = %s AND username = %s ORDER BY expires DESC",
53 "ipfire.org", account
.sip_id
)
55 res
= self
.db
.query("SELECT username, domain, contact AS location, \
56 expires, user_agent, socket FROM location ORDER BY username, expires DESC")
64 row
.account
= accounts_cache
[row
.username
]
66 row
.account
= accounts_cache
[row
.username
] = \
67 self
.accounts
.get_by_sip_id(row
.username
)
71 # Check if TLS is used
72 row
.tls_enabled
= row
.socket
.startswith("tls:")
74 # Cut off everything after ;
75 row
.location
, sep
, rest
= row
.location
.partition(";")
81 def _process_sip_uri(self
, sip_uri
):
82 sip_uri
, delimiter
, rest
= sip_uri
.partition(";")
84 m
= re
.match(r
"^sip:([0-9]{4})@ipfire\.org", sip_uri
)
88 # Remove trailing default port
89 if sip_uri
.endswith(":5060"):
90 sip_uri
= sip_uri
.replace(":5060", "")
94 def _process_cdr(self
, entries
, replace_sip_uris
=False):
100 row
["caller"] = self
._process
_sip
_uri
(row
.caller
)
103 row
["caller_account"] = accounts_cache
[row
.caller
]
105 row
["caller_account"] = accounts_cache
[row
.caller
] = \
106 self
.accounts
.get_by_sip_id(row
.caller
)
109 row
["called"] = self
._process
_sip
_uri
(row
.called
)
112 row
["called_account"] = accounts_cache
[row
.called
]
114 row
["called_account"] = accounts_cache
[row
.called
] = \
115 self
.accounts
.get_by_sip_id(row
.called
)
117 if not row
.called_account
:
118 row
["called_conference"] = self
.get_conference(row
.called
)
120 row
["called_conference"] = None
123 row
["time"] = datetime
.datetime
.fromutctimestamp(row
.time
)
131 def get_call_log(self
, account
, limit
=25):
133 res
= self
.db
.query("SELECT * FROM cdr WHERE (caller = %s OR called = %s) \
134 ORDER BY time DESC LIMIT %s", account
.sip_id
, account
.sip_id
, limit
)
136 res
= self
.db
.query("SELECT * FROM cdr ORDER BY time DESC LIMIT %s", limit
)
138 return self
._process
_cdr
(res
)
140 def get_ongoing_calls(self
, account
=None, sip_id
=None):
141 if account
and sip_id
is None:
142 sip_id
= account
.sip_id
144 # If the given account does not have a SIP ID,
145 # we not need to search for any active sessions
150 sip_id
= "sip:%s@%%" % sip_id
152 res
= self
.db
.query("SELECT from_uri AS caller, \
153 to_uri AS called, start_time AS time FROM dialog \
154 WHERE from_uri LIKE %s OR to_uri LIKE %s ORDER BY time",
157 res
= self
.db
.query("SELECT from_uri AS caller, to_uri AS called, \
158 start_time AS time FROM dialog ORDER BY time")
160 return self
._process
_cdr
(res
, replace_sip_uris
=True)
162 def initiate_call(self
, caller
, called
, when
=None):
163 self
.db
.execute("INSERT INTO dialout(caller, called, not_before) \
164 VALUES(%s, %s, %s)", caller
, called
, when
)
168 def get_favourite_contacts(self
, account
, limit
=6):
169 res
= self
.db
.query("SELECT src_user AS caller, dst_ouser AS called, \
170 COUNT(*) AS count FROM acc WHERE method = %s AND src_user = %s AND \
171 dst_ouser BETWEEN %s AND %s AND time >= NOW() - INTERVAL '1 year' \
172 GROUP BY caller, called ORDER BY count DESC LIMIT %s",
173 "INVITE", account
.sip_id
, "1000", "1999", limit
)
175 return self
._process
_cdr
(res
)
180 def conferences(self
):
183 for no
in range(1, 10):
184 conference
= Conference(self
.backend
, no
)
185 conferences
.append(conference
)
189 def get_conference(self
, id):
190 for c
in self
.conferences
:
191 if not c
.sip_id
== id:
197 class Conference(Object
):
198 def __init__(self
, backend
, no
):
199 Object
.__init
__(self
, backend
)
202 def __cmp__(self
, other
):
203 return cmp(self
.no
, other
.no
)
207 return "IPFire Conference Room %s" % self
.no
211 return "%s" % (9000 + self
.no
)
215 return "%s@ipfire.org" % self
.sip_id
218 def participants(self
):
219 if not hasattr(self
, "_participants"):
220 self
._participants
= self
.backend
.talk
.get_ongoing_calls(sip_id
=self
.sip_id
)
222 return self
._participants
227 participants
= [p
.caller
for p
in self
.participants
]
229 for invitee
in self
.backend
.talk
.get_phonebook():
230 if not invitee
.sip_id
:
233 if invitee
.sip_id
in participants
:
236 invitees
.append(invitee
)