appshare: prevent multiple accept dialogs
[siplcs.git] / src / core / sipe-chat.c
blob8c54d97eeac9e5424fccc8e4457a7aac8c51c62f
1 /**
2 * @file sipe-chat.c
4 * pidgin-sipe
6 * Copyright (C) 2009-2016 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipmsg.h"
35 #include "sip-transport.h"
36 #include "sipe-backend.h"
37 #include "sipe-chat.h"
38 #include "sipe-conf.h"
39 #include "sipe-core.h"
40 #include "sipe-core-private.h"
41 #include "sipe-dialog.h"
42 #include "sipe-groupchat.h"
43 #include "sipe-im.h"
44 #include "sipe-nls.h"
45 #include "sipe-schedule.h"
46 #include "sipe-session.h"
47 #include "sipe-user.h"
48 #include "sipe-utils.h"
49 #include "sipe-xml.h"
51 /**
52 * Invite @who to chat
54 * @param sipe_private SIPE core private data
55 * @param session SIPE session for chat
56 * @param who URI whom to invite to chat.
58 static void
59 sipe_invite_to_chat(struct sipe_core_private *sipe_private,
60 struct sip_session *session,
61 const gchar *who);
63 static GList *chat_sessions = NULL;
65 struct sipe_chat_session *sipe_chat_create_session(enum sipe_chat_type type,
66 const gchar *id,
67 const gchar *title)
69 struct sipe_chat_session *session = g_new0(struct sipe_chat_session, 1);
70 if (id)
71 session->id = g_strdup(id);
72 session->title = g_strdup(title);
73 session->type = type;
74 chat_sessions = g_list_prepend(chat_sessions, session);
75 return(session);
78 void sipe_chat_remove_session(struct sipe_chat_session *session)
80 chat_sessions = g_list_remove(chat_sessions, session);
81 sipe_backend_chat_session_destroy(session->backend);
82 g_free(session->title);
83 g_free(session->id);
84 g_free(session->join_url);
85 g_free(session->dial_in_conf_id);
86 g_free(session->organizer);
88 if (session->appshare_ask_ctx) {
89 sipe_user_close_ask(session->appshare_ask_ctx);
92 g_free(session);
95 void sipe_chat_destroy(void)
97 while (chat_sessions) {
98 struct sipe_chat_session *chat_session = chat_sessions->data;
99 SIPE_DEBUG_INFO("sipe_chat_destroy: '%s' (%s)",
100 chat_session->title, chat_session->id);
101 sipe_chat_remove_session(chat_session);
105 const gchar *sipe_core_chat_id(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
106 struct sipe_chat_session *chat_session)
108 return(chat_session->id);
111 void sipe_core_chat_invite(struct sipe_core_public *sipe_public,
112 struct sipe_chat_session *chat_session,
113 const char *name)
115 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
117 SIPE_DEBUG_INFO("sipe_core_chat_create: who '%s'", name);
119 switch (chat_session->type) {
120 case SIPE_CHAT_TYPE_MULTIPARTY:
121 case SIPE_CHAT_TYPE_CONFERENCE:
123 struct sip_session *session = sipe_session_find_chat(sipe_private,
124 chat_session);
126 if (session) {
127 gchar *uri = sip_uri(name);
128 sipe_invite_to_chat(sipe_private, session, uri);
129 g_free(uri);
132 break;
133 case SIPE_CHAT_TYPE_GROUPCHAT:
134 /* @TODO */
135 SIPE_DEBUG_INFO_NOFORMAT("GROUP CHAT: INVITE NOT IMPLEMENTED!");
136 break;
137 default:
138 break;
142 void sipe_core_chat_rejoin(struct sipe_core_public *sipe_public,
143 struct sipe_chat_session *chat_session)
145 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
147 SIPE_DEBUG_INFO("sipe_core_chat_rejoin: '%s'", chat_session->title);
149 switch (chat_session->type) {
150 case SIPE_CHAT_TYPE_MULTIPARTY:
152 struct sip_session *session = sipe_session_add_chat(sipe_private,
153 chat_session,
154 TRUE,
155 NULL);
156 gchar *self = sip_uri_self(sipe_private);
158 sipe_invite_to_chat(sipe_private, session, self);
159 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
160 chat_session->backend,
161 self,
162 chat_session->title);
163 g_free(self);
165 break;
166 case SIPE_CHAT_TYPE_CONFERENCE:
167 sipe_conf_create(sipe_private, chat_session, NULL);
168 break;
169 case SIPE_CHAT_TYPE_GROUPCHAT:
170 sipe_groupchat_rejoin(sipe_private, chat_session);
171 break;
172 default:
173 break;
177 void sipe_core_chat_leave(struct sipe_core_public *sipe_public,
178 struct sipe_chat_session *chat_session)
180 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
182 SIPE_DEBUG_INFO("sipe_core_chat_leave: '%s'", chat_session->title);
184 switch (chat_session->type) {
185 case SIPE_CHAT_TYPE_MULTIPARTY:
186 case SIPE_CHAT_TYPE_CONFERENCE:
188 struct sip_session *session = sipe_session_find_chat(sipe_private,
189 chat_session);
191 if (session) {
192 sipe_session_close(sipe_private, session);
195 break;
196 case SIPE_CHAT_TYPE_GROUPCHAT:
197 sipe_groupchat_leave(sipe_private, chat_session);
198 break;
199 default:
200 break;
204 void sipe_core_chat_send(struct sipe_core_public *sipe_public,
205 struct sipe_chat_session *chat_session,
206 const char *what)
208 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
210 SIPE_DEBUG_INFO("sipe_core_chat_send: '%s' to '%s'",
211 what, chat_session->title);
213 switch (chat_session->type) {
214 case SIPE_CHAT_TYPE_MULTIPARTY:
215 case SIPE_CHAT_TYPE_CONFERENCE:
217 struct sip_session *session = sipe_session_find_chat(sipe_private,
218 chat_session);
220 if (session) {
221 sipe_session_enqueue_message(session,
222 what,
223 NULL);
224 sipe_im_process_queue(sipe_private, session);
227 break;
228 case SIPE_CHAT_TYPE_GROUPCHAT:
229 sipe_groupchat_send(sipe_private, chat_session, what);
230 break;
231 default:
232 break;
236 gchar *
237 sipe_chat_get_name(void)
240 * A non-volatile ID counter.
241 * Should survive connection drop & reconnect.
243 static guint chat_seq = 0;
245 /* Generate next ID */
246 gchar *chat_name = g_strdup_printf(_("Chat #%d"), ++chat_seq);
247 SIPE_DEBUG_INFO("sipe_chat_get_name: added new: %s", chat_name);
249 return chat_name;
252 static void
253 sipe_refer(struct sipe_core_private *sipe_private,
254 struct sip_session *session,
255 const gchar *who)
257 gchar *hdr;
258 gchar *contact;
259 struct sip_dialog *dialog = sipe_dialog_find(session,
260 session->chat_session->id);
261 const char *ourtag = dialog && dialog->ourtag ? dialog->ourtag : NULL;
263 contact = get_contact(sipe_private);
264 hdr = g_strdup_printf(
265 "Contact: %s\r\n"
266 "Refer-to: <%s>\r\n"
267 "Referred-By: <sip:%s>%s%s;epid=%s\r\n"
268 "Require: com.microsoft.rtc-multiparty\r\n",
269 contact,
270 who,
271 sipe_private->username,
272 ourtag ? ";tag=" : "",
273 ourtag ? ourtag : "",
274 sip_transport_epid(sipe_private));
276 sip_transport_request(sipe_private,
277 "REFER",
278 session->chat_session->id,
279 session->chat_session->id,
280 hdr,
281 NULL,
282 dialog,
283 NULL);
285 g_free(hdr);
286 g_free(contact);
289 static gboolean
290 sipe_is_election_finished(struct sip_session *session)
292 gboolean res = TRUE;
294 SIPE_DIALOG_FOREACH {
295 if (dialog->election_vote == 0) {
296 res = FALSE;
297 break;
299 } SIPE_DIALOG_FOREACH_END;
301 if (res) {
302 session->is_voting_in_progress = FALSE;
304 return res;
307 static gboolean
308 process_info_response(struct sipe_core_private *sipe_private,
309 struct sipmsg *msg,
310 struct transaction *trans);
312 static void
313 sipe_send_election_set_rm(struct sipe_core_private *sipe_private,
314 struct sip_dialog *dialog)
316 const gchar *hdr = "Content-Type: application/x-ms-mim\r\n";
318 gchar *body = g_strdup_printf(
319 "<?xml version=\"1.0\"?>\r\n"
320 "<action xmlns=\"http://schemas.microsoft.com/sip/multiparty/\">"
321 "<SetRM uri=\"sip:%s\"/></action>\r\n",
322 sipe_private->username);
324 sip_transport_info(sipe_private,
325 hdr,
326 body,
327 dialog,
328 process_info_response);
330 g_free(body);
333 void
334 sipe_process_pending_invite_queue(struct sipe_core_private *sipe_private,
335 struct sip_session *session)
337 gchar *invitee;
338 GSList *entry = session->pending_invite_queue;
340 while (entry) {
341 invitee = entry->data;
342 sipe_invite_to_chat(sipe_private, session, invitee);
343 entry = session->pending_invite_queue = g_slist_remove(session->pending_invite_queue, invitee);
344 g_free(invitee);
348 void sipe_chat_set_roster_manager(struct sip_session *session,
349 const gchar *roster_manager)
351 struct sipe_chat_session *chat_session = session->chat_session;
353 g_free(chat_session->id);
354 chat_session->id = NULL;
355 if (roster_manager)
356 chat_session->id = g_strdup(roster_manager);
359 static void
360 sipe_election_result(struct sipe_core_private *sipe_private,
361 void *sess)
363 struct sip_session *session = (struct sip_session *)sess;
364 const gchar *rival = NULL;
366 if (session->chat_session->id) {
367 SIPE_DEBUG_INFO(
368 "sipe_election_result: RM has already been elected in the meantime. It is %s",
369 session->chat_session->id);
370 return;
373 session->is_voting_in_progress = FALSE;
375 SIPE_DIALOG_FOREACH {
376 if (dialog->election_vote < 0) {
377 rival = dialog->with;
378 break;
380 } SIPE_DIALOG_FOREACH_END;
382 if (rival) {
383 SIPE_DEBUG_INFO("sipe_election_result: we loose RM election to %s", rival);
384 } else {
385 gchar *self = sip_uri_self(sipe_private);
387 SIPE_DEBUG_INFO_NOFORMAT("sipe_election_result: we have won RM election!");
389 sipe_chat_set_roster_manager(session, self);
390 g_free(self);
392 SIPE_DIALOG_FOREACH {
393 /* send SetRM to each chat participant*/
394 sipe_send_election_set_rm(sipe_private, dialog);
395 } SIPE_DIALOG_FOREACH_END;
397 session->bid = 0;
399 sipe_process_pending_invite_queue(sipe_private, session);
402 static gboolean
403 process_info_response(struct sipe_core_private *sipe_private,
404 struct sipmsg *msg,
405 SIPE_UNUSED_PARAMETER struct transaction *trans)
407 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
408 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
409 struct sip_dialog *dialog;
410 struct sip_session *session;
412 session = sipe_session_find_chat_by_callid(sipe_private, callid);
413 if (!session) {
414 SIPE_DEBUG_INFO("process_info_response: failed find dialog for callid %s, exiting.", callid);
415 return FALSE;
418 if (msg->response == 200 && g_str_has_prefix(contenttype, "application/x-ms-mim")) {
419 sipe_xml *xn_action = sipe_xml_parse(msg->body, msg->bodylen);
420 const sipe_xml *xn_request_rm_response = sipe_xml_child(xn_action, "RequestRMResponse");
421 const sipe_xml *xn_set_rm_response = sipe_xml_child(xn_action, "SetRMResponse");
423 if (xn_request_rm_response) {
424 const char *with = sipe_xml_attribute(xn_request_rm_response, "uri");
425 const char *allow = sipe_xml_attribute(xn_request_rm_response, "allow");
427 dialog = sipe_dialog_find(session, with);
428 if (!dialog) {
429 SIPE_DEBUG_INFO("process_info_response: failed find dialog for %s, exiting.", with);
430 sipe_xml_free(xn_action);
431 return FALSE;
434 if (allow && !g_ascii_strcasecmp(allow, "true")) {
435 SIPE_DEBUG_INFO("process_info_response: %s has voted PRO", with);
436 dialog->election_vote = 1;
437 } else if (allow && !g_ascii_strcasecmp(allow, "false")) {
438 SIPE_DEBUG_INFO("process_info_response: %s has voted CONTRA", with);
439 dialog->election_vote = -1;
442 if (sipe_is_election_finished(session)) {
443 sipe_election_result(sipe_private,
444 session);
447 } else if (xn_set_rm_response) {
450 sipe_xml_free(xn_action);
454 return TRUE;
457 static void
458 sipe_send_election_request_rm(struct sipe_core_private *sipe_private,
459 struct sip_dialog *dialog,
460 int bid)
462 const gchar *hdr = "Content-Type: application/x-ms-mim\r\n";
464 gchar *body = g_strdup_printf(
465 "<?xml version=\"1.0\"?>\r\n"
466 "<action xmlns=\"http://schemas.microsoft.com/sip/multiparty/\">"
467 "<RequestRM uri=\"sip:%s\" bid=\"%d\"/></action>\r\n",
468 sipe_private->username, bid);
470 sip_transport_info(sipe_private,
471 hdr,
472 body,
473 dialog,
474 process_info_response);
476 g_free(body);
479 static void
480 sipe_election_start(struct sipe_core_private *sipe_private,
481 struct sip_session *session)
483 if (session->is_voting_in_progress) {
484 SIPE_DEBUG_INFO_NOFORMAT("sipe_election_start: other election is in progress, exiting.");
485 return;
486 } else {
487 session->is_voting_in_progress = TRUE;
489 session->bid = rand();
491 SIPE_DEBUG_INFO("sipe_election_start: RM election has initiated. Our bid=%d", session->bid);
493 SIPE_DIALOG_FOREACH {
494 /* reset election_vote for each chat participant */
495 dialog->election_vote = 0;
497 /* send RequestRM to each chat participant*/
498 sipe_send_election_request_rm(sipe_private, dialog, session->bid);
499 } SIPE_DIALOG_FOREACH_END;
501 sipe_schedule_seconds(sipe_private,
502 "<+election-result>",
503 session,
505 sipe_election_result,
506 NULL);
509 static void
510 sipe_invite_to_chat(struct sipe_core_private *sipe_private,
511 struct sip_session *session,
512 const gchar *who)
514 /* a conference */
515 if (session->chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)
517 sipe_invite_conf(sipe_private, session, who);
519 else /* a multi-party chat */
521 gchar *self = sip_uri_self(sipe_private);
522 if (session->chat_session->id) {
523 if (sipe_strcase_equal(session->chat_session->id, self)) {
524 sipe_im_invite(sipe_private, session, who, NULL, NULL, NULL, FALSE);
525 } else {
526 sipe_refer(sipe_private, session, who);
528 } else {
529 SIPE_DEBUG_INFO_NOFORMAT("sipe_invite_to_chat: no RM available");
531 session->pending_invite_queue = sipe_utils_slist_insert_unique_sorted(session->pending_invite_queue,
532 g_strdup(who),
533 (GCompareFunc)strcmp,
534 g_free);
535 sipe_election_start(sipe_private, session);
537 g_free(self);
542 Local Variables:
543 mode: c
544 c-file-style: "bsd"
545 indent-tabs-mode: t
546 tab-width: 8
547 End: