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