]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/talk.py
70a4172437f3d5bbbaec69a4442c06be55e67de0
5 from misc
import Object
9 db_args
= self
.backend
.db
._db
_args
14 self
._db
= database
.Connection(**db_args
)
20 def get_phonebook(self
, account
=None):
22 for a
in self
.accounts
.list():
23 if account
and a
== account
:
26 if not a
.is_talk_enabled():
33 def user_is_online(self
, sip_id
):
34 res
= self
.db
.get("SELECT 1 FROM lines WHERE username = %s \
35 AND expires >= NOW() LIMIT 1", sip_id
)
43 res
= self
.db
.query("SELECT * FROM accounts WHERE enabled = TRUE \
44 ORDER BY description, account")
48 def get_lines(self
, account
):
49 if not account
.is_talk_enabled():
52 res
= self
.db
.query("SELECT location, expires, user_agent FROM lines \
53 WHERE username = %s ORDER BY expires DESC", account
.sip_id
)
57 # Check if TLS is used
58 row
.tls_enabled
= row
.location
.startswith("sip/sips:") or \
59 "transport=TLS" in row
.location
61 # Strip a leading sip/
62 if row
.location
.startswith("sip/"):
63 row
.location
= row
.location
[4:]
65 # Cut off everything after ;
66 row
.location
, sep
, rest
= row
.location
.partition(";")
72 def _process_cdr(self
, entries
):
78 row
.caller_account
= accounts_cache
[row
.caller
]
80 row
.caller_account
= accounts_cache
[row
.caller
] = \
81 self
.accounts
.get_by_sip_id(row
.caller
)
84 row
.called_account
= accounts_cache
[row
.called
]
86 row
.called_account
= accounts_cache
[row
.called
] = \
87 self
.accounts
.get_by_sip_id(row
.called
)
89 if not row
.called_account
:
90 row
.called_conference
= self
.get_conference(row
.called
)
92 row
.called_conference
= None
98 def get_call_log(self
, account
, limit
=25):
99 res
= self
.db
.query("SELECT * FROM cdr \
100 WHERE ((direction = 'incoming' AND called = %s) OR (direction = 'outgoing' AND caller = %s)) \
101 AND (status = 'answered' OR \
102 (status = 'ringing' AND (reason = 'hangup' OR reason = 'Request Terminated'))) \
103 AND NOT (status = 'outgoing' AND reason = 'pickup') \
104 AND ended = TRUE ORDER BY time DESC LIMIT %s", account
.sip_id
, account
.sip_id
, limit
)
106 return self
._process
_cdr
(res
)
108 def get_ongoing_calls(self
, account
=None, sip_id
=None):
109 if account
and sip_id
is None:
110 sip_id
= account
.sip_id
113 res
= self
.db
.query("SELECT * FROM cdr \
114 WHERE direction = 'incoming' AND (caller = %s OR called = %s) \
115 AND status = 'answered' AND NOT ended \
116 ORDER BY time", sip_id
, sip_id
)
118 res
= self
.db
.query("SELECT * FROM cdr \
119 WHERE direction = 'incoming' AND \
120 status = 'answered' AND NOT ended \
123 return self
._process
_cdr
(res
)
125 def initiate_call(self
, caller
, called
, when
=None):
126 self
.db
.execute("INSERT INTO dialout(caller, called, not_before) \
127 VALUES(%s, %s, %s)", caller
, called
, when
)
131 def get_favourite_contacts(self
, account
, limit
=6):
132 ignored_numbers
= ["9999", account
.sip_id
, ""]
134 res
= self
.db
.query("SELECT called, %s AS caller, COUNT(*) AS count FROM cdr \
135 WHERE direction = 'outgoing' AND caller = %s AND status = 'answered' \
136 AND NOT called = ANY(%s) GROUP BY called ORDER BY count DESC LIMIT %s",
137 account
.sip_id
, account
.sip_id
, ignored_numbers
, limit
)
139 return self
._process
_cdr
(res
)
144 def conferences(self
):
147 for no
in range(1, 10):
148 conference
= Conference(self
.backend
, no
)
149 conferences
.append(conference
)
153 def get_conference(self
, id):
154 for c
in self
.conferences
:
155 if not c
.sip_id
== id:
161 class Conference(Object
):
162 def __init__(self
, backend
, no
):
163 Object
.__init
__(self
, backend
)
166 def __cmp__(self
, other
):
167 return cmp(self
.no
, other
.no
)
171 return "IPFire Conference Room %s" % self
.no
175 return "%s" % (9000 + self
.no
)
179 return "%s@ipfire.org" % self
.sip_id
182 def participants(self
):
183 if not hasattr(self
, "_participants"):
184 self
._participants
= self
.backend
.talk
.get_ongoing_calls(sip_id
=self
.sip_id
)
186 return self
._participants
191 participants
= [p
.caller
for p
in self
.participants
]
193 for invitee
in self
.backend
.talk
.get_phonebook():
194 if invitee
.sip_id
in participants
:
197 invitees
.append(invitee
)