media: also fill zero ports of active candidates
[siplcs.git] / src / core / sipe-chat.c
blob356cec37dd5b8741343cfe81be4a873cb7d4a439
1 /**
2 * @file sipe-chat.c
4 * pidgin-sipe
6 * Copyright (C) 2009-2015 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-utils.h"
48 #include "sipe-xml.h"
50 /**
51 * Invite @who to chat
53 * @param sipe_private SIPE core private data
54 * @param session SIPE session for chat
55 * @param who URI whom to invite to chat.
57 static void
58 sipe_invite_to_chat(struct sipe_core_private *sipe_private,
59 struct sip_session *session,
60 const gchar *who);
62 static GList *chat_sessions = NULL;
64 struct sipe_chat_session *sipe_chat_create_session(enum sipe_chat_type type,
65 const gchar *id,
66 const gchar *title)
68 struct sipe_chat_session *session = g_new0(struct sipe_chat_session, 1);
69 if (id)
70 session->id = g_strdup(id);
71 session->title = g_strdup(title);
72 session->type = type;
73 chat_sessions = g_list_prepend(chat_sessions, session);
74 return(session);
77 void sipe_chat_remove_session(struct sipe_chat_session *session)
79 chat_sessions = g_list_remove(chat_sessions, session);
80 sipe_backend_chat_session_destroy(session->backend);
81 g_free(session->title);
82 g_free(session->id);
83 g_free(session);
86 void sipe_chat_destroy(void)
88 while (chat_sessions) {
89 struct sipe_chat_session *chat_session = chat_sessions->data;
90 SIPE_DEBUG_INFO("sipe_chat_destroy: '%s' (%s)",
91 chat_session->title, chat_session->id);
92 sipe_chat_remove_session(chat_session);
96 const gchar *sipe_core_chat_id(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
97 struct sipe_chat_session *chat_session)
99 return(chat_session->id);
102 void sipe_core_chat_invite(struct sipe_core_public *sipe_public,
103 struct sipe_chat_session *chat_session,
104 const char *name)
106 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
108 SIPE_DEBUG_INFO("sipe_core_chat_create: who '%s'", name);
110 switch (chat_session->type) {
111 case SIPE_CHAT_TYPE_MULTIPARTY:
112 case SIPE_CHAT_TYPE_CONFERENCE:
114 struct sip_session *session = sipe_session_find_chat(sipe_private,
115 chat_session);
117 if (session) {
118 gchar *uri = sip_uri(name);
119 sipe_invite_to_chat(sipe_private, session, uri);
120 g_free(uri);
123 break;
124 case SIPE_CHAT_TYPE_GROUPCHAT:
125 /* @TODO */
126 SIPE_DEBUG_INFO_NOFORMAT("GROUP CHAT: INVITE NOT IMPLEMENTED!");
127 break;
128 default:
129 break;
133 void sipe_core_chat_rejoin(struct sipe_core_public *sipe_public,
134 struct sipe_chat_session *chat_session)
136 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
138 SIPE_DEBUG_INFO("sipe_core_chat_rejoin: '%s'", chat_session->title);
140 switch (chat_session->type) {
141 case SIPE_CHAT_TYPE_MULTIPARTY:
143 struct sip_session *session = sipe_session_add_chat(sipe_private,
144 chat_session,
145 TRUE,
146 NULL);
147 gchar *self = sip_uri_self(sipe_private);
149 sipe_invite_to_chat(sipe_private, session, self);
150 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
151 chat_session->backend,
152 self,
153 chat_session->title);
154 g_free(self);
156 break;
157 case SIPE_CHAT_TYPE_CONFERENCE:
158 sipe_conf_create(sipe_private, chat_session, NULL);
159 break;
160 case SIPE_CHAT_TYPE_GROUPCHAT:
161 sipe_groupchat_rejoin(sipe_private, chat_session);
162 break;
163 default:
164 break;
168 void sipe_core_chat_leave(struct sipe_core_public *sipe_public,
169 struct sipe_chat_session *chat_session)
171 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
173 SIPE_DEBUG_INFO("sipe_core_chat_leave: '%s'", chat_session->title);
175 switch (chat_session->type) {
176 case SIPE_CHAT_TYPE_MULTIPARTY:
177 case SIPE_CHAT_TYPE_CONFERENCE:
179 struct sip_session *session = sipe_session_find_chat(sipe_private,
180 chat_session);
182 if (session) {
183 sipe_session_close(sipe_private, session);
186 break;
187 case SIPE_CHAT_TYPE_GROUPCHAT:
188 sipe_groupchat_leave(sipe_private, chat_session);
189 break;
190 default:
191 break;
195 void sipe_core_chat_send(struct sipe_core_public *sipe_public,
196 struct sipe_chat_session *chat_session,
197 const char *what)
199 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
201 SIPE_DEBUG_INFO("sipe_core_chat_send: '%s' to '%s'",
202 what, chat_session->title);
204 switch (chat_session->type) {
205 case SIPE_CHAT_TYPE_MULTIPARTY:
206 case SIPE_CHAT_TYPE_CONFERENCE:
208 struct sip_session *session = sipe_session_find_chat(sipe_private,
209 chat_session);
211 if (session) {
212 sipe_session_enqueue_message(session,
213 what,
214 NULL);
215 sipe_im_process_queue(sipe_private, session);
218 break;
219 case SIPE_CHAT_TYPE_GROUPCHAT:
220 sipe_groupchat_send(sipe_private, chat_session, what);
221 break;
222 default:
223 break;
227 gchar *
228 sipe_chat_get_name(void)
231 * A non-volatile ID counter.
232 * Should survive connection drop & reconnect.
234 static guint chat_seq = 0;
236 /* Generate next ID */
237 gchar *chat_name = g_strdup_printf(_("Chat #%d"), ++chat_seq);
238 SIPE_DEBUG_INFO("sipe_chat_get_name: added new: %s", chat_name);
240 return chat_name;
243 static void
244 sipe_refer(struct sipe_core_private *sipe_private,
245 struct sip_session *session,
246 const gchar *who)
248 gchar *hdr;
249 gchar *contact;
250 gchar *epid = get_epid(sipe_private);
251 struct sip_dialog *dialog = sipe_dialog_find(session,
252 session->chat_session->id);
253 const char *ourtag = dialog && dialog->ourtag ? dialog->ourtag : NULL;
255 contact = get_contact(sipe_private);
256 hdr = g_strdup_printf(
257 "Contact: %s\r\n"
258 "Refer-to: <%s>\r\n"
259 "Referred-By: <sip:%s>%s%s;epid=%s\r\n"
260 "Require: com.microsoft.rtc-multiparty\r\n",
261 contact,
262 who,
263 sipe_private->username,
264 ourtag ? ";tag=" : "",
265 ourtag ? ourtag : "",
266 epid);
267 g_free(epid);
269 sip_transport_request(sipe_private,
270 "REFER",
271 session->chat_session->id,
272 session->chat_session->id,
273 hdr,
274 NULL,
275 dialog,
276 NULL);
278 g_free(hdr);
279 g_free(contact);
282 static gboolean
283 sipe_is_election_finished(struct sip_session *session)
285 gboolean res = TRUE;
287 SIPE_DIALOG_FOREACH {
288 if (dialog->election_vote == 0) {
289 res = FALSE;
290 break;
292 } SIPE_DIALOG_FOREACH_END;
294 if (res) {
295 session->is_voting_in_progress = FALSE;
297 return res;
300 static gboolean
301 process_info_response(struct sipe_core_private *sipe_private,
302 struct sipmsg *msg,
303 struct transaction *trans);
305 static void
306 sipe_send_election_set_rm(struct sipe_core_private *sipe_private,
307 struct sip_dialog *dialog)
309 const gchar *hdr = "Content-Type: application/x-ms-mim\r\n";
311 gchar *body = g_strdup_printf(
312 "<?xml version=\"1.0\"?>\r\n"
313 "<action xmlns=\"http://schemas.microsoft.com/sip/multiparty/\">"
314 "<SetRM uri=\"sip:%s\"/></action>\r\n",
315 sipe_private->username);
317 sip_transport_info(sipe_private,
318 hdr,
319 body,
320 dialog,
321 process_info_response);
323 g_free(body);
326 void
327 sipe_process_pending_invite_queue(struct sipe_core_private *sipe_private,
328 struct sip_session *session)
330 gchar *invitee;
331 GSList *entry = session->pending_invite_queue;
333 while (entry) {
334 invitee = entry->data;
335 sipe_invite_to_chat(sipe_private, session, invitee);
336 entry = session->pending_invite_queue = g_slist_remove(session->pending_invite_queue, invitee);
337 g_free(invitee);
341 void sipe_chat_set_roster_manager(struct sip_session *session,
342 const gchar *roster_manager)
344 struct sipe_chat_session *chat_session = session->chat_session;
346 g_free(chat_session->id);
347 chat_session->id = NULL;
348 if (roster_manager)
349 chat_session->id = g_strdup(roster_manager);
352 static void
353 sipe_election_result(struct sipe_core_private *sipe_private,
354 void *sess)
356 struct sip_session *session = (struct sip_session *)sess;
357 const gchar *rival = NULL;
359 if (session->chat_session->id) {
360 SIPE_DEBUG_INFO(
361 "sipe_election_result: RM has already been elected in the meantime. It is %s",
362 session->chat_session->id);
363 return;
366 session->is_voting_in_progress = FALSE;
368 SIPE_DIALOG_FOREACH {
369 if (dialog->election_vote < 0) {
370 rival = dialog->with;
371 break;
373 } SIPE_DIALOG_FOREACH_END;
375 if (rival) {
376 SIPE_DEBUG_INFO("sipe_election_result: we loose RM election to %s", rival);
377 } else {
378 gchar *self = sip_uri_self(sipe_private);
380 SIPE_DEBUG_INFO_NOFORMAT("sipe_election_result: we have won RM election!");
382 sipe_chat_set_roster_manager(session, self);
383 g_free(self);
385 SIPE_DIALOG_FOREACH {
386 /* send SetRM to each chat participant*/
387 sipe_send_election_set_rm(sipe_private, dialog);
388 } SIPE_DIALOG_FOREACH_END;
390 session->bid = 0;
392 sipe_process_pending_invite_queue(sipe_private, session);
395 static gboolean
396 process_info_response(struct sipe_core_private *sipe_private,
397 struct sipmsg *msg,
398 SIPE_UNUSED_PARAMETER struct transaction *trans)
400 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
401 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
402 struct sip_dialog *dialog;
403 struct sip_session *session;
405 session = sipe_session_find_chat_by_callid(sipe_private, callid);
406 if (!session) {
407 SIPE_DEBUG_INFO("process_info_response: failed find dialog for callid %s, exiting.", callid);
408 return FALSE;
411 if (msg->response == 200 && g_str_has_prefix(contenttype, "application/x-ms-mim")) {
412 sipe_xml *xn_action = sipe_xml_parse(msg->body, msg->bodylen);
413 const sipe_xml *xn_request_rm_response = sipe_xml_child(xn_action, "RequestRMResponse");
414 const sipe_xml *xn_set_rm_response = sipe_xml_child(xn_action, "SetRMResponse");
416 if (xn_request_rm_response) {
417 const char *with = sipe_xml_attribute(xn_request_rm_response, "uri");
418 const char *allow = sipe_xml_attribute(xn_request_rm_response, "allow");
420 dialog = sipe_dialog_find(session, with);
421 if (!dialog) {
422 SIPE_DEBUG_INFO("process_info_response: failed find dialog for %s, exiting.", with);
423 sipe_xml_free(xn_action);
424 return FALSE;
427 if (allow && !g_ascii_strcasecmp(allow, "true")) {
428 SIPE_DEBUG_INFO("process_info_response: %s has voted PRO", with);
429 dialog->election_vote = 1;
430 } else if (allow && !g_ascii_strcasecmp(allow, "false")) {
431 SIPE_DEBUG_INFO("process_info_response: %s has voted CONTRA", with);
432 dialog->election_vote = -1;
435 if (sipe_is_election_finished(session)) {
436 sipe_election_result(sipe_private,
437 session);
440 } else if (xn_set_rm_response) {
443 sipe_xml_free(xn_action);
447 return TRUE;
450 static void
451 sipe_send_election_request_rm(struct sipe_core_private *sipe_private,
452 struct sip_dialog *dialog,
453 int bid)
455 const gchar *hdr = "Content-Type: application/x-ms-mim\r\n";
457 gchar *body = g_strdup_printf(
458 "<?xml version=\"1.0\"?>\r\n"
459 "<action xmlns=\"http://schemas.microsoft.com/sip/multiparty/\">"
460 "<RequestRM uri=\"sip:%s\" bid=\"%d\"/></action>\r\n",
461 sipe_private->username, bid);
463 sip_transport_info(sipe_private,
464 hdr,
465 body,
466 dialog,
467 process_info_response);
469 g_free(body);
472 static void
473 sipe_election_start(struct sipe_core_private *sipe_private,
474 struct sip_session *session)
476 if (session->is_voting_in_progress) {
477 SIPE_DEBUG_INFO_NOFORMAT("sipe_election_start: other election is in progress, exiting.");
478 return;
479 } else {
480 session->is_voting_in_progress = TRUE;
482 session->bid = rand();
484 SIPE_DEBUG_INFO("sipe_election_start: RM election has initiated. Our bid=%d", session->bid);
486 SIPE_DIALOG_FOREACH {
487 /* reset election_vote for each chat participant */
488 dialog->election_vote = 0;
490 /* send RequestRM to each chat participant*/
491 sipe_send_election_request_rm(sipe_private, dialog, session->bid);
492 } SIPE_DIALOG_FOREACH_END;
494 sipe_schedule_seconds(sipe_private,
495 "<+election-result>",
496 session,
498 sipe_election_result,
499 NULL);
502 static void
503 sipe_invite_to_chat(struct sipe_core_private *sipe_private,
504 struct sip_session *session,
505 const gchar *who)
507 /* a conference */
508 if (session->chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)
510 sipe_invite_conf(sipe_private, session, who);
512 else /* a multi-party chat */
514 gchar *self = sip_uri_self(sipe_private);
515 if (session->chat_session->id) {
516 if (sipe_strcase_equal(session->chat_session->id, self)) {
517 sipe_im_invite(sipe_private, session, who, NULL, NULL, NULL, FALSE);
518 } else {
519 sipe_refer(sipe_private, session, who);
521 } else {
522 SIPE_DEBUG_INFO_NOFORMAT("sipe_invite_to_chat: no RM available");
524 session->pending_invite_queue = sipe_utils_slist_insert_unique_sorted(session->pending_invite_queue,
525 g_strdup(who),
526 (GCompareFunc)strcmp,
527 g_free);
528 sipe_election_start(sipe_private, session);
530 g_free(self);
535 Local Variables:
536 mode: c
537 c-file-style: "bsd"
538 indent-tabs-mode: t
539 tab-width: 8
540 End: