core cleanup: separate code for sipe_backend_account_status_and_note()
[siplcs.git] / src / core / sipe-core.c
blob27f62b8144f71dee74faeab937eea7416d58341a
1 /**
2 * @file sipe-core.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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>
30 #include <glib.h>
32 #include "sipe-common.h"
33 #include "http-conn.h" /* sipe-cal.h requires this */
34 #include "sip-csta.h"
35 #include "sip-sec.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
38 #include "sipe-buddy.h"
39 #include "sipe-cal.h"
40 #include "sipe-certificate.h"
41 #include "sipe-chat.h"
42 #include "sipe-conf.h"
43 #include "sipe-core.h"
44 #include "sipe-core-private.h"
45 #include "sipe-crypt.h"
46 #include "sipe-group.h"
47 #include "sipe-groupchat.h"
48 #include "sipe-media.h"
49 #include "sipe-mime.h"
50 #include "sipe-nls.h"
51 #include "sipe-ocs2007.h"
52 #include "sipe-schedule.h"
53 #include "sipe-session.h"
54 #include "sipe-subscriptions.h"
55 #include "sipe-svc.h"
56 #include "sipe-utils.h"
57 #include "sipe.h"
59 /* locale_dir is unused if ENABLE_NLS is not defined */
60 void sipe_core_init(SIPE_UNUSED_PARAMETER const char *locale_dir)
62 srand(time(NULL));
63 sip_sec_init();
65 #ifdef ENABLE_NLS
66 SIPE_DEBUG_INFO("bindtextdomain = %s",
67 bindtextdomain(PACKAGE_NAME, locale_dir));
68 SIPE_DEBUG_INFO("bind_textdomain_codeset = %s",
69 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8"));
70 textdomain(PACKAGE_NAME);
71 #endif
72 /* Initialization for crypto backend (production mode) */
73 sipe_crypto_init(TRUE);
74 sipe_mime_init();
77 void sipe_core_destroy(void)
79 sipe_chat_destroy();
80 sipe_mime_shutdown();
81 sipe_crypto_shutdown();
82 sip_sec_destroy();
85 gchar *sipe_core_about(void)
87 return g_strdup_printf(
89 * Non-translatable parts, like markup, are hard-coded
90 * into the format string. This requires more translatable
91 * texts but it makes the translations less error prone.
93 "<b><font size=\"+1\">SIPE " PACKAGE_VERSION " </font></b><br/>"
94 "<br/>"
95 /* 1 */ "%s:<br/>"
96 "<li> - MS Office Communications Server 2007 R2</li><br/>"
97 "<li> - MS Office Communications Server 2007</li><br/>"
98 "<li> - MS Live Communications Server 2005</li><br/>"
99 "<li> - MS Live Communications Server 2003</li><br/>"
100 "<li> - Reuters Messaging</li><br/>"
101 "<br/>"
102 /* 2 */ "%s: <a href=\"" PACKAGE_URL "\">" PACKAGE_URL "</a><br/>"
103 /* 3,4 */ "%s: <a href=\"http://sourceforge.net/projects/sipe/forums/forum/688534\">%s</a><br/>"
104 /* 5,6 */ "%s: <a href=\"" PACKAGE_BUGREPORT "\">%s</a><br/>"
105 /* 7 */ "%s: <a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.net</a><br/>"
106 /* 8 */ "%s: GPLv2+<br/>"
107 "<br/>"
108 /* 9 */ "%s:<br/>"
109 " - CERN<br/>"
110 " - Reuters Messaging network<br/>"
111 " - Deutsche Bank<br/>"
112 " - Merrill Lynch<br/>"
113 " - Wachovia<br/>"
114 " - Intel<br/>"
115 " - Nokia<br/>"
116 " - HP<br/>"
117 " - Symantec<br/>"
118 " - Accenture<br/>"
119 " - Capgemini<br/>"
120 " - Siemens<br/>"
121 " - Alcatel-Lucent<br/>"
122 " - BT<br/>"
123 "<br/>"
124 /* 10,11 */ "%s<a href=\"" SIPE_TRANSLATIONS_URL "\">Transifex.net</a>%s.<br/>"
125 "<br/>"
126 /* 12 */ "<b>%s:</b><br/>"
127 " - Anibal Avelar<br/>"
128 " - Gabriel Burt<br/>"
129 " - Stefan Becker<br/>"
130 " - pier11<br/>"
131 " - Jakub Adam<br/>"
132 " - Tomáš Hrabčík<br/>"
133 "<br/>"
134 /* 13 */ "%s<br/>"
136 /* The next 13 texts make up the SIPE about note text */
137 /* About note, part 1/13: introduction */
138 _("A third-party plugin implementing extended version of SIP/SIMPLE used by various products"),
139 /* About note, part 2/13: home page URL (label) */
140 _("Home"),
141 /* About note, part 3/13: support forum URL (label) */
142 _("Support"),
143 /* About note, part 4/13: support forum name (hyperlink text) */
144 _("Help Forum"),
145 /* About note, part 5/13: bug tracker URL (label) */
146 _("Report Problems"),
147 /* About note, part 6/13: bug tracker URL (hyperlink text) */
148 _("Bug Tracker"),
149 /* About note, part 7/13: translation service URL (label) */
150 _("Translations"),
151 /* About note, part 8/13: license type (label) */
152 _("License"),
153 /* About note, part 9/13: known users */
154 _("We support users in such organizations as"),
155 /* About note, part 10/13: translation request, text before Transifex.net URL */
156 /* append a space if text is not empty */
157 _("Please help us to translate SIPE to your native language here at "),
158 /* About note, part 11/13: translation request, text after Transifex.net URL */
159 /* start with a space if text is not empty */
160 _(" using convenient web interface"),
161 /* About note, part 12/13: author list (header) */
162 _("Authors"),
163 /* About note, part 13/13: Localization credit */
164 /* PLEASE NOTE: do *NOT* simply translate the english original */
165 /* but write something similar to the following sentence: */
166 /* "Localization for <language name> (<language code>): <name>" */
167 _("Original texts in English (en): SIPE developers")
171 static guint sipe_ht_hash_nick(const char *nick)
173 char *lc = g_utf8_strdown(nick, -1);
174 guint bucket = g_str_hash(lc);
175 g_free(lc);
177 return bucket;
180 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
182 char *nick1_norm = NULL;
183 char *nick2_norm = NULL;
184 gboolean equal;
186 if (nick1 == NULL && nick2 == NULL) return TRUE;
187 if (nick1 == NULL || nick2 == NULL ||
188 !g_utf8_validate(nick1, -1, NULL) ||
189 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
191 nick1_norm = g_utf8_casefold(nick1, -1);
192 nick2_norm = g_utf8_casefold(nick2, -1);
193 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
194 g_free(nick2_norm);
195 g_free(nick1_norm);
197 return equal;
200 struct sipe_core_public *sipe_core_allocate(const gchar *signin_name,
201 const gchar *login_domain,
202 const gchar *login_account,
203 const gchar *password,
204 const gchar *email,
205 const gchar *email_url,
206 const gchar **errmsg)
208 struct sipe_core_private *sipe_private;
209 struct sipe_account_data *sip;
210 gchar **user_domain;
212 SIPE_DEBUG_INFO("sipe_core_allocate: signin_name '%s'", signin_name);
214 /* ensure that sign-in name doesn't contain invalid characters */
215 if (strpbrk(signin_name, "\t\v\r\n") != NULL) {
216 *errmsg = _("SIP Exchange user name contains invalid characters");
217 return NULL;
220 /* ensure that sign-in name format is name@domain */
221 if (!strchr(signin_name, '@') ||
222 g_str_has_prefix(signin_name, "@") ||
223 g_str_has_suffix(signin_name, "@")) {
224 *errmsg = _("User name should be a valid SIP URI\nExample: user@company.com");
225 return NULL;
228 /* ensure that email format is name@domain (if provided) */
229 if (!is_empty(email) &&
230 (!strchr(email, '@') ||
231 g_str_has_prefix(email, "@") ||
232 g_str_has_suffix(email, "@")))
234 *errmsg = _("Email address should be valid if provided\nExample: user@company.com");
235 return NULL;
238 /* ensure that user name doesn't contain spaces */
239 user_domain = g_strsplit(signin_name, "@", 2);
240 SIPE_DEBUG_INFO("sipe_core_allocate: user '%s' domain '%s'", user_domain[0], user_domain[1]);
241 if (strchr(user_domain[0], ' ') != NULL) {
242 g_strfreev(user_domain);
243 *errmsg = _("SIP Exchange user name contains whitespace");
244 return NULL;
247 /* ensure that email_url is in proper format if enabled (if provided).
248 * Example (Exchange): https://server.company.com/EWS/Exchange.asmx
249 * Example (Domino) : https://[domino_server]/[mail_database_name].nsf
251 if (!is_empty(email_url)) {
252 char *tmp = g_ascii_strdown(email_url, -1);
253 if (!g_str_has_prefix(tmp, "https://"))
255 g_free(tmp);
256 g_strfreev(user_domain);
257 *errmsg = _("Email services URL should be valid if provided\n"
258 "Example: https://exchange.corp.com/EWS/Exchange.asmx\n"
259 "Example: https://domino.corp.com/maildatabase.nsf");
260 return NULL;
262 g_free(tmp);
265 sipe_private = g_new0(struct sipe_core_private, 1);
266 sipe_private->temporary = sip = g_new0(struct sipe_account_data, 1);
267 sip->subscribed_buddies = FALSE;
268 sip->initial_state_published = FALSE;
269 sipe_private->username = g_strdup(signin_name);
270 sip->email = is_empty(email) ? g_strdup(signin_name) : g_strdup(email);
271 sip->authdomain = is_empty(login_domain) ? NULL : g_strdup(login_domain);
272 sip->authuser = is_empty(login_account) ? NULL : g_strdup(login_account);
273 sip->password = g_strdup(password);
274 sipe_private->public.sip_name = g_strdup(user_domain[0]);
275 sipe_private->public.sip_domain = g_strdup(user_domain[1]);
276 g_strfreev(user_domain);
278 sipe_private->buddies = g_hash_table_new((GHashFunc)sipe_ht_hash_nick, (GEqualFunc)sipe_ht_equals_nick);
279 sip->our_publications = g_hash_table_new_full(g_str_hash, g_str_equal,
280 g_free, (GDestroyNotify)g_hash_table_destroy);
281 sipe_subscriptions_init(sipe_private);
282 sipe_set_unknown_status(sipe_private);
284 return((struct sipe_core_public *)sipe_private);
287 void sipe_core_connection_cleanup(struct sipe_core_private *sipe_private)
289 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
291 g_free(sipe_private->epid);
292 sipe_private->epid = NULL;
294 sip_transport_disconnect(sipe_private);
296 sipe_schedule_cancel_all(sipe_private);
298 if (sip->allow_events) {
299 GSList *entry = sip->allow_events;
300 while (entry) {
301 g_free(entry->data);
302 entry = entry->next;
305 g_slist_free(sip->allow_events);
307 sipe_ocs2007_free(sipe_private);
309 sipe_blist_menu_free_containers(sipe_private);
311 if (sipe_private->contact)
312 g_free(sipe_private->contact);
313 sipe_private->contact = NULL;
314 if (sip->regcallid)
315 g_free(sip->regcallid);
316 sip->regcallid = NULL;
318 if (sipe_private->focus_factory_uri)
319 g_free(sipe_private->focus_factory_uri);
320 sipe_private->focus_factory_uri = NULL;
322 if (sip->cal) {
323 sipe_cal_calendar_free(sip->cal);
325 sip->cal = NULL;
327 sipe_groupchat_free(sipe_private);
330 void sipe_core_deallocate(struct sipe_core_public *sipe_public)
332 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
333 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
335 #ifdef HAVE_VV
336 if (sipe_private->media_call) {
337 sipe_media_handle_going_offline(sipe_private->media_call);
339 #endif
341 /* leave all conversations */
342 if (sipe_private->sessions) {
343 GSList *entry;
344 while ((entry = sipe_private->sessions) != NULL) {
345 sipe_session_close(sipe_private, entry->data);
349 sipe_conf_cancel_unaccepted(sipe_private, NULL);
351 if (sip->csta) {
352 sip_csta_close(sipe_private);
355 sipe_certificate_free(sipe_private);
356 sipe_svc_free(sipe_private);
358 if (sipe_backend_connection_is_valid(SIPE_CORE_PUBLIC)) {
359 sipe_subscriptions_unsubscribe(sipe_private);
360 sip_transport_deregister(sipe_private);
363 sipe_core_connection_cleanup(sipe_private);
364 g_free(sipe_private->public.sip_name);
365 g_free(sipe_private->public.sip_domain);
366 g_free(sipe_private->username);
367 g_free(sip->email);
368 g_free(sip->password);
369 g_free(sip->authdomain);
370 g_free(sip->authuser);
371 g_free(sip->status);
372 g_free(sip->note);
373 g_free(sip->user_states);
375 sipe_buddy_free_all(sipe_private);
376 g_hash_table_destroy(sipe_private->buddies);
377 g_hash_table_destroy(sip->our_publications);
378 g_hash_table_destroy(sip->user_state_publications);
379 sipe_subscriptions_destroy(sipe_private);
381 if (sipe_private->groups) {
382 GSList *entry = sipe_private->groups;
383 while (entry) {
384 struct sipe_group *group = entry->data;
385 g_free(group->name);
386 g_free(group);
387 entry = entry->next;
390 g_slist_free(sipe_private->groups);
392 if (sip->our_publication_keys) {
393 GSList *entry = sip->our_publication_keys;
394 while (entry) {
395 g_free(entry->data);
396 entry = entry->next;
399 g_slist_free(sip->our_publication_keys);
401 #ifdef HAVE_VV
402 g_free(sipe_private->mras_uri);
403 g_free(sipe_private->media_relay_username);
404 g_free(sipe_private->media_relay_password);
405 sipe_media_relay_list_free(sipe_private->media_relays);
406 #endif
408 g_free(sip);
409 g_free(sipe_private);
413 Local Variables:
414 mode: c
415 c-file-style: "bsd"
416 indent-tabs-mode: t
417 tab-width: 8
418 End: