utils: add g_slist_free_full() replacement
[siplcs.git] / src / core / sipe-core.c
blob2a8b59b84b29e93770d06c935b946ab24ed539de
1 /**
2 * @file sipe-core.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 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-group.h"
86 #include "sipe-groupchat.h"
87 #include "sipe-http.h"
88 #include "sipe-media.h"
89 #include "sipe-mime.h"
90 #include "sipe-nls.h"
91 #include "sipe-ocs2007.h"
92 #include "sipe-schedule.h"
93 #include "sipe-session.h"
94 #include "sipe-status.h"
95 #include "sipe-subscriptions.h"
96 #include "sipe-svc.h"
97 #include "sipe-utils.h"
98 #include "sipe-webticket.h"
100 #ifdef PACKAGE_GIT_COMMIT
101 #define SIPE_CORE_VERSION PACKAGE_VERSION " (git commit " PACKAGE_GIT_COMMIT ")"
102 #else
103 #define SIPE_CORE_VERSION PACKAGE_VERSION
104 #endif
106 /* locale_dir is unused if ENABLE_NLS is not defined */
107 void sipe_core_init(SIPE_UNUSED_PARAMETER const char *locale_dir)
109 srand(time(NULL));
110 sip_sec_init();
112 #ifdef ENABLE_NLS
113 SIPE_DEBUG_INFO("bindtextdomain = %s",
114 bindtextdomain(PACKAGE_NAME, locale_dir));
115 SIPE_DEBUG_INFO("bind_textdomain_codeset = %s",
116 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8"));
117 textdomain(PACKAGE_NAME);
118 #endif
119 /* Initialization for crypto backend (production mode) */
120 sipe_crypto_init(TRUE);
121 sipe_mime_init();
122 sipe_status_init();
125 void sipe_core_destroy(void)
127 sipe_chat_destroy();
128 sipe_status_shutdown();
129 sipe_mime_shutdown();
130 sipe_crypto_shutdown();
131 sip_sec_destroy();
134 gchar *sipe_core_about(void)
136 return g_strdup_printf(
138 * Non-translatable parts, like markup, are hard-coded
139 * into the format string. This requires more translatable
140 * texts but it makes the translations less error prone.
142 "<b><font size=\"+1\">SIPE " SIPE_CORE_VERSION " </font></b><br/>"
143 "<br/>"
144 /* 1 */ "%s:<br/>"
145 "<li> - Microsoft Office 365</li><br/>"
146 "<li> - Microsoft Business Productivity Online Suite (BPOS)</li><br/>"
147 "<li> - Microsoft Lync Server</li><br/>"
148 "<li> - Microsoft Office Communications Server 2007 R2</li><br/>"
149 "<li> - Microsoft Office Communications Server 2007</li><br/>"
150 "<li> - Microsoft Live Communications Server 2005</li><br/>"
151 "<li> - Microsoft Live Communications Server 2003</li><br/>"
152 "<li> - Reuters Messaging</li><br/>"
153 "<br/>"
154 /* 2 */ "%s: <a href=\"" PACKAGE_URL "\">" PACKAGE_URL "</a><br/>"
155 /* 3,4 */ "%s: <a href=\"http://sourceforge.net/p/sipe/discussion/688534/\">%s</a><br/>"
156 /* 5,6 */ "%s: <a href=\"" PACKAGE_BUGREPORT "\">%s</a><br/>"
157 /* 7 */ "%s: <a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.net</a><br/>"
158 /* 8 */ "%s: GPLv2+<br/>"
159 "<br/>"
160 /* 9 */ "%s:<br/>"
161 " - CERN<br/>"
162 " - Reuters Messaging network<br/>"
163 " - Deutsche Bank<br/>"
164 " - Merrill Lynch<br/>"
165 " - Wachovia<br/>"
166 " - Intel<br/>"
167 " - Nokia<br/>"
168 " - HP<br/>"
169 " - Symantec<br/>"
170 " - Accenture<br/>"
171 " - Capgemini<br/>"
172 " - Siemens<br/>"
173 " - Alcatel-Lucent<br/>"
174 " - BT<br/>"
175 "<br/>"
176 /* 10,11 */ "%s<a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.net</a>%s.<br/>"
177 "<br/>"
178 /* 12 */ "<b>%s:</b><br/>"
179 " - Anibal Avelar<br/>"
180 " - Gabriel Burt<br/>"
181 " - Stefan Becker<br/>"
182 " - pier11<br/>"
183 " - Jakub Adam<br/>"
184 " - Tomáš Hrabčík<br/>"
185 "<br/>"
186 /* 13 */ "%s<br/>"
188 /* The next 13 texts make up the SIPE about note text */
189 /* About note, part 1/13: introduction */
190 _("A third-party plugin implementing extended version of SIP/SIMPLE used by various products"),
191 /* About note, part 2/13: home page URL (label) */
192 _("Home Page"),
193 /* About note, part 3/13: support forum URL (label) */
194 _("Support"),
195 /* About note, part 4/13: support forum name (hyperlink text) */
196 _("Help Forum"),
197 /* About note, part 5/13: bug tracker URL (label) */
198 _("Report Problems"),
199 /* About note, part 6/13: bug tracker URL (hyperlink text) */
200 _("Bug Tracker"),
201 /* About note, part 7/13: translation service URL (label) */
202 _("Translations"),
203 /* About note, part 8/13: license type (label) */
204 _("License"),
205 /* About note, part 9/13: known users */
206 _("We support users in such organizations as"),
207 /* About note, part 10/13: translation request, text before Transifex.net URL */
208 /* append a space if text is not empty */
209 _("Please help us to translate SIPE to your native language here at "),
210 /* About note, part 11/13: translation request, text after Transifex.net URL */
211 /* start with a space if text is not empty */
212 _(" using convenient web interface"),
213 /* About note, part 12/13: author list (header) */
214 _("Authors"),
215 /* About note, part 13/13: Localization credit */
216 /* PLEASE NOTE: do *NOT* simply translate the english original */
217 /* but write something similar to the following sentence: */
218 /* "Localization for <language name> (<language code>): <name>" */
219 _("Original texts in English (en): SIPE developers")
223 static guint sipe_ht_hash_nick(const char *nick)
225 char *lc = g_utf8_strdown(nick, -1);
226 guint bucket = g_str_hash(lc);
227 g_free(lc);
229 return bucket;
232 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
234 char *nick1_norm = NULL;
235 char *nick2_norm = NULL;
236 gboolean equal;
238 if (nick1 == NULL && nick2 == NULL) return TRUE;
239 if (nick1 == NULL || nick2 == NULL ||
240 !g_utf8_validate(nick1, -1, NULL) ||
241 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
243 nick1_norm = g_utf8_casefold(nick1, -1);
244 nick2_norm = g_utf8_casefold(nick2, -1);
245 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
246 g_free(nick2_norm);
247 g_free(nick1_norm);
249 return equal;
252 struct sipe_core_public *sipe_core_allocate(const gchar *signin_name,
253 gboolean sso,
254 const gchar *login_domain,
255 const gchar *login_account,
256 const gchar *password,
257 const gchar *email,
258 const gchar *email_url,
259 const gchar **errmsg)
261 struct sipe_core_private *sipe_private;
262 gchar **user_domain;
264 SIPE_DEBUG_INFO("sipe_core_allocate: SIPE version " SIPE_CORE_VERSION " signin_name '%s'", signin_name);
266 /* ensure that sign-in name doesn't contain invalid characters */
267 if (strpbrk(signin_name, "\t\v\r\n") != NULL) {
268 *errmsg = _("SIP Exchange user name contains invalid characters");
269 return NULL;
272 /* ensure that sign-in name format is name@domain */
273 if (!strchr(signin_name, '@') ||
274 g_str_has_prefix(signin_name, "@") ||
275 g_str_has_suffix(signin_name, "@")) {
276 *errmsg = _("User name should be a valid SIP URI\nExample: user@company.com");
277 return NULL;
280 /* ensure that Login & Password are valid when SSO is not selected */
281 if (!sso && (is_empty(login_account) || is_empty(password))) {
282 *errmsg = _("Login and password are required when Single Sign-On is not enabled");
283 return NULL;
286 /* ensure that email format is name@domain (if provided) */
287 if (!is_empty(email) &&
288 (!strchr(email, '@') ||
289 g_str_has_prefix(email, "@") ||
290 g_str_has_suffix(email, "@")))
292 *errmsg = _("Email address should be valid if provided\nExample: user@company.com");
293 return NULL;
296 /* ensure that user name doesn't contain spaces */
297 user_domain = g_strsplit(signin_name, "@", 2);
298 SIPE_DEBUG_INFO("sipe_core_allocate: user '%s' domain '%s'", user_domain[0], user_domain[1]);
299 if (strchr(user_domain[0], ' ') != NULL) {
300 g_strfreev(user_domain);
301 *errmsg = _("SIP Exchange user name contains whitespace");
302 return NULL;
305 /* ensure that email_url is in proper format if enabled (if provided).
306 * Example (Exchange): https://server.company.com/EWS/Exchange.asmx
307 * Example (Domino) : https://[domino_server]/[mail_database_name].nsf
309 if (!is_empty(email_url)) {
310 char *tmp = g_ascii_strdown(email_url, -1);
311 if (!g_str_has_prefix(tmp, "https://"))
313 g_free(tmp);
314 g_strfreev(user_domain);
315 *errmsg = _("Email services URL should be valid if provided\n"
316 "Example: https://exchange.corp.com/EWS/Exchange.asmx\n"
317 "Example: https://domino.corp.com/maildatabase.nsf");
318 return NULL;
320 g_free(tmp);
323 sipe_private = g_new0(struct sipe_core_private, 1);
324 SIPE_CORE_PRIVATE_FLAG_UNSET(SUBSCRIBED_BUDDIES);
325 SIPE_CORE_PRIVATE_FLAG_UNSET(INITIAL_PUBLISH);
326 SIPE_CORE_PRIVATE_FLAG_UNSET(SSO);
327 if (sso)
328 SIPE_CORE_PRIVATE_FLAG_SET(SSO);
329 sipe_private->username = g_strdup(signin_name);
330 sipe_private->email = is_empty(email) ? g_strdup(signin_name) : g_strdup(email);
331 sipe_private->authdomain = sso ? NULL : g_strdup(login_domain);
332 sipe_private->authuser = sso ? NULL : g_strdup(login_account);
333 sipe_private->password = sso ? NULL : g_strdup(password);
334 sipe_private->public.sip_name = g_strdup(user_domain[0]);
335 sipe_private->public.sip_domain = g_strdup(user_domain[1]);
336 g_strfreev(user_domain);
338 sipe_private->buddies = g_hash_table_new((GHashFunc)sipe_ht_hash_nick, (GEqualFunc)sipe_ht_equals_nick);
339 sipe_private->our_publications = g_hash_table_new_full(g_str_hash, g_str_equal,
340 g_free, (GDestroyNotify)g_hash_table_destroy);
341 sipe_subscriptions_init(sipe_private);
342 sipe_status_set_activity(sipe_private, SIPE_ACTIVITY_UNSET);
344 return((struct sipe_core_public *)sipe_private);
347 void sipe_core_connection_cleanup(struct sipe_core_private *sipe_private)
349 g_free(sipe_private->epid);
350 sipe_private->epid = NULL;
352 sipe_http_free(sipe_private);
353 sip_transport_disconnect(sipe_private);
355 sipe_schedule_cancel_all(sipe_private);
357 if (sipe_private->allowed_events)
358 sipe_utils_slist_free_full(sipe_private->allowed_events, g_free);
360 sipe_ocs2007_free(sipe_private);
362 sipe_core_buddy_menu_free(SIPE_CORE_PUBLIC);
364 if (sipe_private->contact)
365 g_free(sipe_private->contact);
366 sipe_private->contact = NULL;
367 if (sipe_private->register_callid)
368 g_free(sipe_private->register_callid);
369 sipe_private->register_callid = NULL;
371 if (sipe_private->focus_factory_uri)
372 g_free(sipe_private->focus_factory_uri);
373 sipe_private->focus_factory_uri = NULL;
375 if (sipe_private->calendar) {
376 sipe_cal_calendar_free(sipe_private->calendar);
378 sipe_private->calendar = NULL;
380 sipe_groupchat_free(sipe_private);
383 void sipe_core_deallocate(struct sipe_core_public *sipe_public)
385 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
387 #ifdef HAVE_VV
388 if (sipe_private->media_call) {
389 sipe_media_handle_going_offline(sipe_private->media_call);
391 #endif
393 /* leave all conversations */
394 if (sipe_private->sessions) {
395 GSList *entry;
396 while ((entry = sipe_private->sessions) != NULL) {
397 sipe_session_close(sipe_private, entry->data);
401 sipe_conf_cancel_unaccepted(sipe_private, NULL);
403 if (sipe_private->csta) {
404 sip_csta_close(sipe_private);
407 /* pending service requests must be cancelled first */
408 sipe_svc_free(sipe_private);
409 sipe_webticket_free(sipe_private);
411 if (sipe_backend_connection_is_valid(SIPE_CORE_PUBLIC)) {
412 sipe_subscriptions_unsubscribe(sipe_private);
413 sip_transport_deregister(sipe_private);
416 sipe_core_connection_cleanup(sipe_private);
417 sipe_certificate_free(sipe_private);
419 g_free(sipe_private->public.sip_name);
420 g_free(sipe_private->public.sip_domain);
421 g_free(sipe_private->username);
422 g_free(sipe_private->email);
423 g_free(sipe_private->password);
424 g_free(sipe_private->authdomain);
425 g_free(sipe_private->authuser);
426 g_free(sipe_private->status);
427 g_free(sipe_private->note);
428 g_free(sipe_private->ocs2005_user_states);
430 sipe_buddy_free_all(sipe_private);
431 g_hash_table_destroy(sipe_private->buddies);
432 g_hash_table_destroy(sipe_private->our_publications);
433 g_hash_table_destroy(sipe_private->user_state_publications);
434 sipe_subscriptions_destroy(sipe_private);
436 if (sipe_private->groups) {
437 GSList *entry;
438 while ((entry = sipe_private->groups) != NULL)
439 sipe_group_free(sipe_private, entry->data);
442 if (sipe_private->our_publication_keys)
443 sipe_utils_slist_free_full(sipe_private->our_publication_keys, g_free);
445 #ifdef HAVE_VV
446 g_free(sipe_private->test_call_bot_uri);
447 g_free(sipe_private->mras_uri);
448 g_free(sipe_private->media_relay_username);
449 g_free(sipe_private->media_relay_password);
450 sipe_media_relay_list_free(sipe_private->media_relays);
451 #endif
453 g_free(sipe_private->addressbook_uri);
454 g_free(sipe_private->dlx_uri);
455 g_free(sipe_private);
459 Local Variables:
460 mode: c
461 c-file-style: "bsd"
462 indent-tabs-mode: t
463 tab-width: 8
464 End: