ucs: move GetUserPhoto operation to buddy
[siplcs.git] / src / core / sipe-core.c
blob42c80a171b761d8291372a35a37ac8060f230408
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-media.h"
90 #include "sipe-mime.h"
91 #include "sipe-nls.h"
92 #include "sipe-ocs2007.h"
93 #include "sipe-schedule.h"
94 #include "sipe-session.h"
95 #include "sipe-status.h"
96 #include "sipe-subscriptions.h"
97 #include "sipe-svc.h"
98 #include "sipe-ucs.h"
99 #include "sipe-utils.h"
100 #include "sipe-webticket.h"
102 #ifdef PACKAGE_GIT_COMMIT
103 #define SIPE_CORE_VERSION PACKAGE_VERSION " (git commit " PACKAGE_GIT_COMMIT " / "
104 #else
105 #define SIPE_CORE_VERSION PACKAGE_VERSION " ("
106 #endif
108 /* translate config.h options to strings */
109 static const gchar * const sipe_core_build_options[] = {
111 /* Authentication */
112 #ifdef HAVE_SSPI
113 "SSPI",
114 #elif defined(HAVE_GSSAPI_ONLY)
115 "GSSAPI",
116 #ifndef HAVE_GSSAPI_PASSWORD_SUPPORT
117 "SSO only",
118 #endif
119 #else
120 #ifdef HAVE_GSSAPI_GSSAPI_H
121 "Kerberos 5 & NTLM",
122 #else
123 "NTLM",
124 #endif
125 #endif
127 /* Media */
128 #ifdef HAVE_VV
129 "Voice & Video",
130 #ifdef HAVE_SRTP
131 "SRTP",
132 #endif
133 #ifdef HAVE_XDATA
134 "Lync FT",
135 #endif
136 #endif
138 /* Development */
139 #ifdef HAVE_VALGRIND
140 "Valgrind",
141 #endif
143 NULL
147 /* locale_dir is unused if ENABLE_NLS is not defined */
148 void sipe_core_init(SIPE_UNUSED_PARAMETER const char *locale_dir)
150 srand(time(NULL));
151 sip_sec_init();
153 #ifdef ENABLE_NLS
154 SIPE_DEBUG_INFO("bindtextdomain = %s",
155 bindtextdomain(PACKAGE_NAME, locale_dir));
156 SIPE_DEBUG_INFO("bind_textdomain_codeset = %s",
157 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8"));
158 textdomain(PACKAGE_NAME);
159 #endif
160 /* Initialization for crypto backend (production mode) */
161 sipe_crypto_init(TRUE);
162 sipe_mime_init();
163 sipe_status_init();
166 void sipe_core_destroy(void)
168 sipe_chat_destroy();
169 sipe_status_shutdown();
170 sipe_mime_shutdown();
171 sipe_crypto_shutdown();
172 sip_sec_destroy();
175 gchar *sipe_core_about(void)
177 gchar *options = g_strjoinv(" / ", (gchar **) sipe_core_build_options);
178 gchar *about = g_strdup_printf(
180 * Non-translatable parts, like markup, are hard-coded
181 * into the format string. This requires more translatable
182 * texts but it makes the translations less error prone.
184 "<b><font size=\"+1\">SIPE " SIPE_CORE_VERSION "%s) </font></b><br/>"
185 "<br/>"
186 /* 1 */ "%s:<br/>"
187 " - Skype for Business<br/>"
188 " - Microsoft Office 365<br/>"
189 " - Microsoft Business Productivity Online Suite (BPOS)<br/>"
190 " - Microsoft Lync Server<br/>"
191 " - Microsoft Office Communications Server 2007 R2<br/>"
192 " - Microsoft Office Communications Server 2007<br/>"
193 " - Microsoft Live Communications Server 2005<br/>"
194 " - Microsoft Live Communications Server 2003<br/>"
195 " - Reuters Messaging<br/>"
196 "<br/>"
197 /* 2 */ "%s: <a href=\"" PACKAGE_URL "\">" PACKAGE_URL "</a><br/>"
198 /* 3,4 */ "%s: <a href=\"http://sourceforge.net/p/sipe/discussion/688534/\">%s</a><br/>"
199 /* 5,6 */ "%s: <a href=\"" PACKAGE_BUGREPORT "\">%s</a><br/>"
200 /* 7 */ "%s: <a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.com</a><br/>"
201 /* 8 */ "%s: GPLv2+<br/>"
202 "<br/>"
203 /* 9 (REMOVED) */
204 /* 10,11 */ "%s<a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.com</a>%s.<br/>"
205 "<br/>"
206 /* 12 */ "<b>%s:</b><br/>"
207 " - Stefan Becker<br/>"
208 " - Jakub Adam<br/>"
209 " - Jochen De Smet (retired, Miranda port)<br/>"
210 " - Michael Lamb (retired, Adium port)<br/>"
211 " - Anibal Avelar (retired)<br/>"
212 " - Gabriel Burt (retired)<br/>"
213 " - pier11 (retired)<br/>"
214 " - Tomáš Hrabčík (retired)<br/>"
215 "<br/>"
216 /* 13 */ "%s<br/>"
217 , options,
218 /* The next 13 texts make up the SIPE about note text */
219 /* About note, part 1/13: introduction */
220 _("A third-party plugin implementing extended version of SIP/SIMPLE used by various products"),
221 /* About note, part 2/13: home page URL (label) */
222 _("Home Page"),
223 /* About note, part 3/13: support forum URL (label) */
224 _("Support"),
225 /* About note, part 4/13: support forum name (hyperlink text) */
226 _("Help Forum"),
227 /* About note, part 5/13: bug tracker URL (label) */
228 _("Report Problems"),
229 /* About note, part 6/13: bug tracker URL (hyperlink text) */
230 _("Bug Tracker"),
231 /* About note, part 7/13: translation service URL (label) */
232 _("Translations"),
233 /* About note, part 8/13: license type (label) */
234 _("License"),
235 /* About note, part 9/13: (REMOVED) */
236 /* About note, part 10/13: translation request, text before Transifex.com URL */
237 /* append a space if text is not empty */
238 _("Please help us to translate SIPE to your native language here at "),
239 /* About note, part 11/13: translation request, text after Transifex.com URL */
240 /* start with a space if text is not empty */
241 _(" using convenient web interface"),
242 /* About note, part 12/13: author list (header) */
243 _("Authors"),
244 /* About note, part 13/13: Localization credit */
245 /* PLEASE NOTE: do *NOT* simply translate the english original */
246 /* but write something similar to the following sentence: */
247 /* "Localization for <language name> (<language code>): <name>" */
248 _("Original texts in English (en): SIPE developers")
250 g_free(options);
251 return(about);
254 struct sipe_core_public *sipe_core_allocate(const gchar *signin_name,
255 gboolean sso,
256 const gchar *login_account,
257 const gchar *password,
258 const gchar *email,
259 const gchar *email_url,
260 const gchar **errmsg)
262 struct sipe_core_private *sipe_private;
263 gchar **user_domain;
265 SIPE_DEBUG_INFO("sipe_core_allocate: SIPE version " SIPE_CORE_VERSION " signin_name '%s'", signin_name);
267 /* ensure that sign-in name doesn't contain invalid characters */
268 if (strpbrk(signin_name, "\t\v\r\n") != NULL) {
269 *errmsg = _("SIP Exchange user name contains invalid characters");
270 return NULL;
273 /* ensure that sign-in name format is name@domain */
274 if (!strchr(signin_name, '@') ||
275 g_str_has_prefix(signin_name, "@") ||
276 g_str_has_suffix(signin_name, "@")) {
277 *errmsg = _("User name should be a valid SIP URI\nExample: user@company.com");
278 return NULL;
282 /* ensure that Password is valid when SSO is not selected */
283 if (!sso && is_empty(password)) {
284 *errmsg = _("Password is required when Single Sign-On is not enabled");
285 return NULL;
288 /* ensure that email format is name@domain (if provided) */
289 if (!is_empty(email) &&
290 (!strchr(email, '@') ||
291 g_str_has_prefix(email, "@") ||
292 g_str_has_suffix(email, "@")))
294 *errmsg = _("Email address should be valid if provided\nExample: user@company.com");
295 return NULL;
298 /* ensure that user name doesn't contain spaces */
299 user_domain = g_strsplit(signin_name, "@", 2);
300 SIPE_DEBUG_INFO("sipe_core_allocate: user '%s' domain '%s'", user_domain[0], user_domain[1]);
301 if (strchr(user_domain[0], ' ') != NULL) {
302 g_strfreev(user_domain);
303 *errmsg = _("SIP Exchange user name contains whitespace");
304 return NULL;
307 /* ensure that email_url is in proper format if enabled (if provided).
308 * Example (Exchange): https://server.company.com/EWS/Exchange.asmx
309 * Example (Domino) : https://[domino_server]/[mail_database_name].nsf
311 if (!is_empty(email_url)) {
312 char *tmp = g_ascii_strdown(email_url, -1);
313 if (!g_str_has_prefix(tmp, "https://"))
315 g_free(tmp);
316 g_strfreev(user_domain);
317 *errmsg = _("Email services URL should be valid if provided\n"
318 "Example: https://exchange.corp.com/EWS/Exchange.asmx\n"
319 "Example: https://domino.corp.com/maildatabase.nsf");
320 return NULL;
322 g_free(tmp);
325 /* re-use sign-in name if login is empty */
326 if (is_empty(login_account))
327 login_account = signin_name;
329 sipe_private = g_new0(struct sipe_core_private, 1);
330 SIPE_CORE_PRIVATE_FLAG_UNSET(SUBSCRIBED_BUDDIES);
331 SIPE_CORE_PRIVATE_FLAG_UNSET(INITIAL_PUBLISH);
332 SIPE_CORE_PRIVATE_FLAG_UNSET(SSO);
333 if (sso)
334 SIPE_CORE_PRIVATE_FLAG_SET(SSO);
335 sipe_private->username = g_strdup(signin_name);
336 sipe_private->email = is_empty(email) ? g_strdup(signin_name) : g_strdup(email);
337 sipe_private->authuser = sso ? NULL : g_strdup(login_account);
338 sipe_private->password = sso ? NULL : g_strdup(password);
339 sipe_private->public.sip_name = g_strdup(user_domain[0]);
340 sipe_private->public.sip_domain = g_strdup(user_domain[1]);
341 g_strfreev(user_domain);
343 sipe_group_init(sipe_private);
344 sipe_buddy_init(sipe_private);
345 sipe_private->our_publications = g_hash_table_new_full(g_str_hash, g_str_equal,
346 g_free, (GDestroyNotify)g_hash_table_destroy);
347 sipe_subscriptions_init(sipe_private);
348 sipe_ews_autodiscover_init(sipe_private);
349 sipe_status_set_activity(sipe_private, SIPE_ACTIVITY_UNSET);
351 sipe_private->media_calls = g_hash_table_new_full(g_str_hash, g_str_equal,
352 g_free, NULL);
353 sipe_private->access_numbers = g_hash_table_new_full(g_str_hash, g_str_equal,
354 g_free, g_free);
356 return((struct sipe_core_public *)sipe_private);
359 void sipe_core_backend_initialized(struct sipe_core_private *sipe_private,
360 guint authentication)
362 const gchar *value;
364 sipe_private->authentication_type = authentication;
366 /* user specified email login? */
367 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_LOGIN);
368 if (!is_empty(value)) {
369 sipe_private->email_authuser = g_strdup(value);
370 sipe_private->email_password = g_strdup(sipe_backend_setting(SIPE_CORE_PUBLIC,
371 SIPE_SETTING_EMAIL_PASSWORD));
375 void sipe_core_connection_cleanup(struct sipe_core_private *sipe_private)
377 g_free(sipe_private->epid);
378 sipe_private->epid = NULL;
380 sipe_http_free(sipe_private);
381 sip_transport_disconnect(sipe_private);
383 sipe_schedule_cancel_all(sipe_private);
385 if (sipe_private->allowed_events)
386 sipe_utils_slist_free_full(sipe_private->allowed_events, g_free);
388 sipe_ocs2007_free(sipe_private);
390 sipe_core_buddy_menu_free(SIPE_CORE_PUBLIC);
392 if (sipe_private->contact)
393 g_free(sipe_private->contact);
394 sipe_private->contact = NULL;
395 if (sipe_private->register_callid)
396 g_free(sipe_private->register_callid);
397 sipe_private->register_callid = NULL;
399 if (sipe_private->focus_factory_uri)
400 g_free(sipe_private->focus_factory_uri);
401 sipe_private->focus_factory_uri = NULL;
403 sipe_groupchat_free(sipe_private);
406 void sipe_core_deallocate(struct sipe_core_public *sipe_public)
408 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
410 #ifdef HAVE_VV
411 sipe_media_handle_going_offline(sipe_private);
412 #endif
414 /* leave all conversations */
415 if (sipe_private->sessions) {
416 GSList *entry;
417 while ((entry = sipe_private->sessions) != NULL) {
418 sipe_session_close(sipe_private, entry->data);
422 sipe_conf_cancel_unaccepted(sipe_private, NULL);
424 if (sipe_private->csta) {
425 sip_csta_close(sipe_private);
428 /* pending service requests must be cancelled first */
429 sipe_svc_free(sipe_private);
430 sipe_webticket_free(sipe_private);
431 sipe_ucs_free(sipe_private);
433 if (sipe_backend_connection_is_valid(SIPE_CORE_PUBLIC)) {
434 sipe_subscriptions_unsubscribe(sipe_private);
435 sip_transport_deregister(sipe_private);
438 sipe_core_connection_cleanup(sipe_private);
439 sipe_ews_autodiscover_free(sipe_private);
440 sipe_cal_calendar_free(sipe_private->calendar);
441 sipe_certificate_free(sipe_private);
443 g_free(sipe_private->public.sip_name);
444 g_free(sipe_private->public.sip_domain);
445 g_free(sipe_private->username);
446 g_free(sipe_private->email_password);
447 g_free(sipe_private->email_authuser);
448 g_free(sipe_private->email);
449 g_free(sipe_private->password);
450 g_free(sipe_private->authuser);
451 g_free(sipe_private->status);
452 g_free(sipe_private->note);
453 g_free(sipe_private->ocs2005_user_states);
455 sipe_buddy_free(sipe_private);
456 g_hash_table_destroy(sipe_private->our_publications);
457 g_hash_table_destroy(sipe_private->user_state_publications);
458 g_hash_table_destroy(sipe_private->media_calls);
459 sipe_subscriptions_destroy(sipe_private);
460 sipe_group_free(sipe_private);
462 if (sipe_private->our_publication_keys)
463 sipe_utils_slist_free_full(sipe_private->our_publication_keys, g_free);
465 #ifdef HAVE_VV
466 g_free(sipe_private->test_call_bot_uri);
467 g_free(sipe_private->uc_line_uri);
468 g_free(sipe_private->mras_uri);
469 g_free(sipe_private->media_relay_username);
470 g_free(sipe_private->media_relay_password);
471 sipe_media_relay_list_free(sipe_private->media_relays);
472 #endif
474 g_free(sipe_private->persistentChatPool_uri);
475 g_free(sipe_private->addressbook_uri);
476 g_free(sipe_private->dlx_uri);
477 sipe_utils_slist_free_full(sipe_private->conf_mcu_types, g_free);
478 g_hash_table_destroy(sipe_private->access_numbers);
479 g_free(sipe_private);
482 void sipe_core_email_authentication(struct sipe_core_private *sipe_private,
483 struct sipe_http_request *request)
485 if (sipe_private->email_authuser) {
486 sipe_http_request_authentication(request,
487 sipe_private->email_authuser,
488 sipe_private->email_password);
493 Local Variables:
494 mode: c
495 c-file-style: "bsd"
496 indent-tabs-mode: t
497 tab-width: 8
498 End: