appshare: process incoming invite
[siplcs.git] / src / core / sipe-core.c
blob317b509de0858c835064f15e688e11785c95325c
1 /**
2 * @file sipe-core.c
4 * pidgin-sipe
6 * Copyright (C) 2010-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 * Some notes on the history of this project/code/copyrights:
25 * - the project is called SIPE, but originally the code was only written
26 * for the libpurple framework, i.e. Pidgin. Hence the package name is
27 * "pidgin-sipe".
29 * - in the beginning almost all of the code was located in a module
30 * called "sipe.c". During the effort to remove the libpurple
31 * dependencies from the SIPE core, thousands of lines of code got
32 * shifted out of sipe.c, mostly to newly created modules and sipe.c
33 * ceased to exist.
35 * - it would have been tedious to track down the original author or
36 * copyright and preserve them for each line of code that was moved.
37 * Therefore the new modules started with a fresh copyright notice
38 * (like the one above).
40 * - the original copyright notices from sipe.c have been moved to this
41 * file (see below) and *MUST* be preserved!
43 * - if necessary the author of a line of code in question can still be
44 * reconstructed from the git repository information.
45 * See also "man git-blame"
47 * - if you think your copyright should be restored for a piece of code,
48 * then please contact the SIPE project to fix the source files ASAP.
50 *------------------- Copyright notices from "sipe.c" ---------------
51 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
52 * Copyright (C) 2009-10 pier11 <pier11@operamail.com>
53 * Copyright (C) 2008 Novell, Inc.
54 * Copyright (C) 2007-09 Anibal Avelar <debianmx@gmail.com>
55 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
57 * ***
58 * Thanks to Google's Summer of Code Program and the helpful mentors
59 * ***
60 *------------------- Copyright notices from "sipe.c" ---------------
63 #ifdef HAVE_CONFIG_H
64 #include "config.h"
65 #endif
67 #include <stdlib.h>
68 #include <string.h>
70 #include <glib.h>
72 #include "sipe-common.h"
73 #include "sip-csta.h"
74 #include "sip-sec.h"
75 #include "sip-transport.h"
76 #include "sipe-backend.h"
77 #include "sipe-buddy.h"
78 #include "sipe-cal.h"
79 #include "sipe-certificate.h"
80 #include "sipe-chat.h"
81 #include "sipe-conf.h"
82 #include "sipe-core.h"
83 #include "sipe-core-private.h"
84 #include "sipe-crypt.h"
85 #include "sipe-ews-autodiscover.h"
86 #include "sipe-group.h"
87 #include "sipe-groupchat.h"
88 #include "sipe-http.h"
89 #include "sipe-lync-autodiscover.h"
90 #include "sipe-media.h"
91 #include "sipe-mime.h"
92 #include "sipe-nls.h"
93 #include "sipe-ocs2007.h"
94 #include "sipe-schedule.h"
95 #include "sipe-session.h"
96 #include "sipe-status.h"
97 #include "sipe-subscriptions.h"
98 #include "sipe-svc.h"
99 #include "sipe-ucs.h"
100 #include "sipe-utils.h"
101 #include "sipe-webticket.h"
103 #ifdef PACKAGE_GIT_COMMIT
104 #define SIPE_CORE_VERSION PACKAGE_VERSION " (git commit " PACKAGE_GIT_COMMIT " / "
105 #else
106 #define SIPE_CORE_VERSION PACKAGE_VERSION " ("
107 #endif
109 /* translate config.h options to strings */
110 static const gchar * const sipe_core_build_options[] = {
112 /* Authentication */
113 #ifdef HAVE_SSPI
114 "SSPI",
115 #elif defined(HAVE_GSSAPI_ONLY)
116 "GSSAPI",
117 #ifndef HAVE_GSSAPI_PASSWORD_SUPPORT
118 "SSO only",
119 #endif
120 #else
121 #ifdef HAVE_GSSAPI_GSSAPI_H
122 "Kerberos 5 & NTLM",
123 #else
124 "NTLM",
125 #endif
126 #endif
128 /* Media */
129 #ifdef HAVE_VV
130 "Voice & Video",
131 #ifdef HAVE_SRTP
132 "SRTP",
133 #endif
134 #ifdef HAVE_XDATA
135 "Lync FT",
136 #endif
137 #endif
139 /* Development */
140 #ifdef HAVE_VALGRIND
141 "Valgrind",
142 #endif
144 NULL
148 /* locale_dir is unused if ENABLE_NLS is not defined */
149 void sipe_core_init(SIPE_UNUSED_PARAMETER const char *locale_dir)
151 srand(time(NULL));
152 sip_sec_init();
154 #ifdef ENABLE_NLS
155 SIPE_DEBUG_INFO("bindtextdomain = %s",
156 bindtextdomain(PACKAGE_NAME, locale_dir));
157 SIPE_DEBUG_INFO("bind_textdomain_codeset = %s",
158 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8"));
159 textdomain(PACKAGE_NAME);
160 #endif
161 /* Initialization for crypto backend (production mode) */
162 sipe_crypto_init(TRUE);
163 sipe_mime_init();
164 sipe_status_init();
167 void sipe_core_destroy(void)
169 sipe_chat_destroy();
170 sipe_status_shutdown();
171 sipe_mime_shutdown();
172 sipe_crypto_shutdown();
173 sip_sec_destroy();
176 gchar *sipe_core_about(void)
178 gchar *options = g_strjoinv(" / ", (gchar **) sipe_core_build_options);
179 gchar *about = g_strdup_printf(
181 * Non-translatable parts, like markup, are hard-coded
182 * into the format string. This requires more translatable
183 * texts but it makes the translations less error prone.
185 "<b><font size=\"+1\">SIPE " SIPE_CORE_VERSION "%s) </font></b><br/>"
186 "<br/>"
187 /* 1 */ "%s:<br/>"
188 " - Skype for Business<br/>"
189 " - Microsoft Office 365<br/>"
190 " - Microsoft Business Productivity Online Suite (BPOS)<br/>"
191 " - Microsoft Lync Server<br/>"
192 " - Microsoft Office Communications Server 2007 R2<br/>"
193 " - Microsoft Office Communications Server 2007<br/>"
194 " - Microsoft Live Communications Server 2005<br/>"
195 " - Microsoft Live Communications Server 2003<br/>"
196 " - Reuters Messaging<br/>"
197 "<br/>"
198 /* 2 */ "%s: <a href=\"" PACKAGE_URL "\">" PACKAGE_URL "</a><br/>"
199 /* 3,4 */ "%s: <a href=\"http://sourceforge.net/p/sipe/discussion/688534/\">%s</a><br/>"
200 /* 5,6 */ "%s: <a href=\"" PACKAGE_BUGREPORT "\">%s</a><br/>"
201 /* 7 */ "%s: <a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.com</a><br/>"
202 /* 8 */ "%s: GPLv2+<br/>"
203 "<br/>"
204 /* 9 (REMOVED) */
205 /* 10,11 */ "%s<a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.com</a>%s.<br/>"
206 "<br/>"
207 /* 12 */ "<b>%s:</b><br/>"
208 " - Stefan Becker<br/>"
209 " - Jakub Adam<br/>"
210 " - Jochen De Smet (retired, Miranda port)<br/>"
211 " - Michael Lamb (retired, Adium port)<br/>"
212 " - Anibal Avelar (retired)<br/>"
213 " - Gabriel Burt (retired)<br/>"
214 " - pier11 (retired)<br/>"
215 " - Tomáš Hrabčík (retired)<br/>"
216 "<br/>"
217 /* 13 */ "%s<br/>"
218 , options,
219 /* The next 13 texts make up the SIPE about note text */
220 /* About note, part 1/13: introduction */
221 _("A third-party plugin implementing extended version of SIP/SIMPLE used by various products"),
222 /* About note, part 2/13: home page URL (label) */
223 _("Home Page"),
224 /* About note, part 3/13: support forum URL (label) */
225 _("Support"),
226 /* About note, part 4/13: support forum name (hyperlink text) */
227 _("Help Forum"),
228 /* About note, part 5/13: bug tracker URL (label) */
229 _("Report Problems"),
230 /* About note, part 6/13: bug tracker URL (hyperlink text) */
231 _("Bug Tracker"),
232 /* About note, part 7/13: translation service URL (label) */
233 _("Translations"),
234 /* About note, part 8/13: license type (label) */
235 _("License"),
236 /* About note, part 9/13: (REMOVED) */
237 /* About note, part 10/13: translation request, text before Transifex.com URL */
238 /* append a space if text is not empty */
239 _("Please help us to translate SIPE to your native language here at "),
240 /* About note, part 11/13: translation request, text after Transifex.com URL */
241 /* start with a space if text is not empty */
242 _(" using convenient web interface"),
243 /* About note, part 12/13: author list (header) */
244 _("Authors"),
245 /* About note, part 13/13: Localization credit */
246 /* PLEASE NOTE: do *NOT* simply translate the english original */
247 /* but write something similar to the following sentence: */
248 /* "Localization for <language name> (<language code>): <name>" */
249 _("Original texts in English (en): SIPE developers")
251 g_free(options);
252 return(about);
255 struct sipe_core_public *sipe_core_allocate(const gchar *signin_name,
256 gboolean sso,
257 const gchar *login_account,
258 const gchar *password,
259 const gchar *email,
260 const gchar *email_url,
261 const gchar **errmsg)
263 struct sipe_core_private *sipe_private;
264 gchar **user_domain;
265 gchar *options = g_strjoinv(" / ", (gchar **) sipe_core_build_options);
267 SIPE_LOG_INFO("sipe_core_allocate: SIPE version " SIPE_CORE_VERSION "%s)", options);
268 g_free(options);
269 SIPE_DEBUG_INFO("sipe_core_allocate: signin_name '%s'", signin_name);
271 /* ensure that sign-in name doesn't contain invalid characters */
272 if (strpbrk(signin_name, "\t\v\r\n") != NULL) {
273 *errmsg = _("SIP Exchange user name contains invalid characters");
274 return NULL;
277 /* ensure that sign-in name format is name@domain */
278 if (!strchr(signin_name, '@') ||
279 g_str_has_prefix(signin_name, "@") ||
280 g_str_has_suffix(signin_name, "@")) {
281 *errmsg = _("User name should be a valid SIP URI\nExample: user@company.com");
282 return NULL;
286 /* ensure that Password is valid when SSO is not selected */
287 if (!sso && is_empty(password)) {
288 *errmsg = _("Password is required when Single Sign-On is not enabled");
289 return NULL;
292 /* ensure that email format is name@domain (if provided) */
293 if (!is_empty(email) &&
294 (!strchr(email, '@') ||
295 g_str_has_prefix(email, "@") ||
296 g_str_has_suffix(email, "@")))
298 *errmsg = _("Email address should be valid if provided\nExample: user@company.com");
299 return NULL;
302 /* ensure that user name doesn't contain spaces */
303 user_domain = g_strsplit(signin_name, "@", 2);
304 SIPE_DEBUG_INFO("sipe_core_allocate: user '%s' domain '%s'", user_domain[0], user_domain[1]);
305 if (strchr(user_domain[0], ' ') != NULL) {
306 g_strfreev(user_domain);
307 *errmsg = _("SIP Exchange user name contains whitespace");
308 return NULL;
311 /* ensure that email_url is in proper format if enabled (if provided).
312 * Example (Exchange): https://server.company.com/EWS/Exchange.asmx
313 * Example (Domino) : https://[domino_server]/[mail_database_name].nsf
315 if (!is_empty(email_url)) {
316 char *tmp = g_ascii_strdown(email_url, -1);
317 if (!g_str_has_prefix(tmp, "https://"))
319 g_free(tmp);
320 g_strfreev(user_domain);
321 *errmsg = _("Email services URL should be valid if provided\n"
322 "Example: https://exchange.corp.com/EWS/Exchange.asmx\n"
323 "Example: https://domino.corp.com/maildatabase.nsf");
324 return NULL;
326 g_free(tmp);
329 /* re-use sign-in name if login is empty */
330 if (is_empty(login_account))
331 login_account = signin_name;
333 sipe_private = g_new0(struct sipe_core_private, 1);
334 SIPE_CORE_PRIVATE_FLAG_UNSET(SUBSCRIBED_BUDDIES);
335 SIPE_CORE_PRIVATE_FLAG_UNSET(INITIAL_PUBLISH);
336 SIPE_CORE_PRIVATE_FLAG_UNSET(SSO);
337 if (sso)
338 SIPE_CORE_PRIVATE_FLAG_SET(SSO);
339 sipe_private->username = g_strdup(signin_name);
340 sipe_private->email = is_empty(email) ? g_strdup(signin_name) : g_strdup(email);
341 sipe_private->authuser = sso ? NULL : g_strdup(login_account);
342 sipe_private->password = sso ? NULL : g_strdup(password);
343 sipe_private->public.sip_name = g_strdup(user_domain[0]);
344 sipe_private->public.sip_domain = g_strdup(user_domain[1]);
345 g_strfreev(user_domain);
347 sipe_group_init(sipe_private);
348 sipe_buddy_init(sipe_private);
349 sipe_private->our_publications = g_hash_table_new_full(g_str_hash, g_str_equal,
350 g_free, (GDestroyNotify)g_hash_table_destroy);
351 sipe_subscriptions_init(sipe_private);
352 sipe_lync_autodiscover_init(sipe_private);
353 sipe_ews_autodiscover_init(sipe_private);
354 sipe_status_set_activity(sipe_private, SIPE_ACTIVITY_UNSET);
356 sipe_private->media_calls = g_hash_table_new_full(g_str_hash, g_str_equal,
357 g_free, NULL);
358 sipe_private->access_numbers = g_hash_table_new_full(g_str_hash, g_str_equal,
359 g_free, g_free);
361 return((struct sipe_core_public *)sipe_private);
364 void sipe_core_backend_initialized(struct sipe_core_private *sipe_private,
365 guint authentication)
367 const gchar *value;
369 sipe_private->authentication_type = authentication;
371 /* user specified email login? */
372 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_LOGIN);
373 if (!is_empty(value)) {
374 sipe_private->email_authuser = g_strdup(value);
375 sipe_private->email_password = g_strdup(sipe_backend_setting(SIPE_CORE_PUBLIC,
376 SIPE_SETTING_EMAIL_PASSWORD));
380 void sipe_core_connection_cleanup(struct sipe_core_private *sipe_private)
382 sipe_http_free(sipe_private);
383 sip_transport_disconnect(sipe_private);
385 sipe_schedule_cancel_all(sipe_private);
387 if (sipe_private->allowed_events)
388 sipe_utils_slist_free_full(sipe_private->allowed_events, g_free);
390 sipe_ocs2007_free(sipe_private);
392 sipe_core_buddy_menu_free(SIPE_CORE_PUBLIC);
394 if (sipe_private->contact)
395 g_free(sipe_private->contact);
396 sipe_private->contact = NULL;
397 if (sipe_private->register_callid)
398 g_free(sipe_private->register_callid);
399 sipe_private->register_callid = NULL;
401 if (sipe_private->focus_factory_uri)
402 g_free(sipe_private->focus_factory_uri);
403 sipe_private->focus_factory_uri = NULL;
405 sipe_groupchat_free(sipe_private);
407 while (sipe_private->lync_autodiscover_servers)
408 sipe_private->lync_autodiscover_servers =
409 sipe_lync_autodiscover_pop(sipe_private->lync_autodiscover_servers);
412 void sipe_core_deallocate(struct sipe_core_public *sipe_public)
414 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
416 #ifdef HAVE_VV
417 sipe_media_handle_going_offline(sipe_private);
418 #endif
420 /* leave all conversations */
421 if (sipe_private->sessions) {
422 GSList *entry;
423 while ((entry = sipe_private->sessions) != NULL) {
424 sipe_session_close(sipe_private, entry->data);
428 sipe_conf_cancel_unaccepted(sipe_private, NULL);
430 if (sipe_private->csta) {
431 sip_csta_close(sipe_private);
434 /* pending service requests must be cancelled first */
435 sipe_svc_free(sipe_private);
436 sipe_webticket_free(sipe_private);
437 sipe_ucs_free(sipe_private);
438 sipe_lync_autodiscover_free(sipe_private);
440 if (sipe_backend_connection_is_valid(SIPE_CORE_PUBLIC)) {
441 sipe_subscriptions_unsubscribe(sipe_private);
442 sip_transport_deregister(sipe_private);
445 sipe_core_connection_cleanup(sipe_private);
446 sipe_ews_autodiscover_free(sipe_private);
447 sipe_cal_calendar_free(sipe_private->calendar);
448 sipe_certificate_free(sipe_private);
450 g_free(sipe_private->public.sip_name);
451 g_free(sipe_private->public.sip_domain);
452 g_free(sipe_private->username);
453 g_free(sipe_private->email_password);
454 g_free(sipe_private->email_authuser);
455 g_free(sipe_private->email);
456 g_free(sipe_private->password);
457 g_free(sipe_private->authuser);
458 g_free(sipe_private->status);
459 g_free(sipe_private->note);
460 g_free(sipe_private->ocs2005_user_states);
462 sipe_buddy_free(sipe_private);
463 g_hash_table_destroy(sipe_private->our_publications);
464 g_hash_table_destroy(sipe_private->user_state_publications);
465 g_hash_table_destroy(sipe_private->media_calls);
466 sipe_subscriptions_destroy(sipe_private);
467 sipe_group_free(sipe_private);
469 if (sipe_private->our_publication_keys)
470 sipe_utils_slist_free_full(sipe_private->our_publication_keys, g_free);
472 #ifdef HAVE_VV
473 g_free(sipe_private->test_call_bot_uri);
474 g_free(sipe_private->uc_line_uri);
475 g_free(sipe_private->mras_uri);
476 g_free(sipe_private->media_relay_username);
477 g_free(sipe_private->media_relay_password);
478 sipe_media_relay_list_free(sipe_private->media_relays);
479 #endif
481 g_free(sipe_private->persistentChatPool_uri);
482 g_free(sipe_private->addressbook_uri);
483 g_free(sipe_private->dlx_uri);
484 sipe_utils_slist_free_full(sipe_private->conf_mcu_types, g_free);
485 g_hash_table_destroy(sipe_private->access_numbers);
486 g_free(sipe_private);
489 void sipe_core_email_authentication(struct sipe_core_private *sipe_private,
490 struct sipe_http_request *request)
492 if (sipe_private->email_authuser) {
493 sipe_http_request_authentication(request,
494 sipe_private->email_authuser,
495 sipe_private->email_password);
500 Local Variables:
501 mode: c
502 c-file-style: "bsd"
503 indent-tabs-mode: t
504 tab-width: 8
505 End: