core cleanup: move account fields out
[siplcs.git] / src / core / sipe-domino.c
blob323fea6bb74dce26f209d9df6c84edb8d1553a4f
1 /**
2 * @file sipe-domino.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 pier11 <pier11@operamail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /**
26 For communication with Lotus Domino groupware server.
28 Server requirements: Domino 5.0.2 and above with Web Access.
30 1) Tries to read user's notes.ini for mail database name.
31 Windows registry keys for notes.ini location:
32 HKEY_CURRENT_USER\Software\Lotus\Notes\6.0\NotesIniPath
34 2) Authenticates to server (HTTPS POST, plaintext login/password over SSL)
35 https://[domino_server]/[databasename].nsf/?Login
36 Content-Type=application/x-www-form-urlencoded
37 Username=[email]&Password=[password] (params are url-encoded)
38 Saves auth cookie.
39 Set-Cookie=DomAuthSessId=17D0428F7B9D57D4D0B064AE42FD21F9; path=/
41 3) Queries Calendar data (HTTPS GET, result is XML)
42 https://[domino_server]/[databasename].nsf/[viewname]?ReadViewEntries
43 https://[domino_server]/[databasename].nsf/($Calendar)?ReadViewEntries&KeyType=time&StartKey=20090805T000000Z&UntilKey=20090806T000000Z&Count=-1&TZType=UTC
44 Uses auth cookie.
45 Cookie=DomAuthSessId=17D0428F7B9D57D4D0B064AE42FD21F9
47 It is able to retrieve our Calendar information (Meetings schedule,
48 subject and location) from Lotus Domino for subsequent publishing.
50 Ref. for more implementation details:
51 https://sourceforge.net/tracker/?func=detail&aid=2945346&group_id=194563&atid=949934
53 Similar functionality for iCalendar/CalDAV/Google would be great to implement too.
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
60 #include <string.h>
61 #include <ctype.h>
62 #include <stdio.h>
63 #include <errno.h>
64 #include <time.h>
66 #include <glib.h>
68 /* for registry read */
69 #ifdef _WIN32
70 #include "sipe-win32dep.h"
71 #endif
73 #include "http-conn.h"
74 #include "sipe-common.h"
75 #include "sipe-core.h"
76 #include "sipe-core-private.h"
77 #include "sipe-nls.h"
78 #include "sipe-backend.h"
79 #include "sipe-utils.h"
80 #include "sipe-cal.h"
81 #include "sipe-xml.h"
82 #include "sipe-domino.h"
84 /**
85 * POST request for Login to Domino server
86 * @param email (%s) Should be URL-encoded. Ex.: alice@cosmo.local
87 * @param password (%s) Should be URL-encoded.
89 #define SIPE_DOMINO_LOGIN_REQUEST \
90 "Username=%s&Password=%s"
92 /**
93 * GET request to Domino server
94 * to obtain our Calendar information.
95 * @param start_time (%s) Ex.: 20090805T000000Z
96 * @param end_time (%s) Ex.: 20090806T000000Z
98 #define SIPE_DOMINO_CALENDAR_REQUEST \
99 "/($Calendar)?ReadViewEntries&KeyType=time&StartKey=%s&UntilKey=%s&Count=-1&TZType=UTC"
102 <?xml version="1.0" encoding="UTF-8"?>
103 <viewentries timestamp="20100416T112140,02Z" toplevelentries="77" rangeentries="
104 1000">
105 <viewentry position="77" unid="C3A77CC76EAA7D08802576FD0043D7D0" noteid="27B42" siblings="77">
106 <entrydata columnnumber="0" name="$134">
107 <datetime>20100423T103000,00Z</datetime>
108 </entrydata>
109 <entrydata columnnumber="1" name="$149">
110 <number>158</number>
111 </entrydata>
112 <entrydata columnnumber="2" name="$144">
113 <datetime>20100423T103000,00Z</datetime>
114 </entrydata>
115 <entrydata columnnumber="3" name="$145">
116 <text>-</text>
117 </entrydata>
118 <entrydata columnnumber="4" name="$146">
119 <datetime>20100423T120000,00Z</datetime>
120 </entrydata>
121 <entrydata columnnumber="5" name="$147">
122 <textlist>
123 <text>G. S. ..I. L. T. Hall</text>
124 <text>Location: Auditorium - W. House</text>
125 <text>Chair: S. S.</text>
126 </textlist>
127 </entrydata>
128 </viewentry>
129 <viewentry .........
130 </viewentries>
133 #define VIEWENTITY_START0_TIME "$134"
134 #define VIEWENTITY_START_TIME "$144"
135 #define VIEWENTITY_END_TIME "$146"
136 #define VIEWENTITY_TEXT_LIST "$147"
139 static int
140 sipe_domino_get_slot_no(time_t fb_start, time_t in)
142 return (in - fb_start) / SIPE_FREE_BUSY_GRANULARITY_SEC;
145 static char *
146 sipe_domino_get_free_busy(time_t fb_start,
147 GSList *cal_events)
149 GSList *entry = cal_events;
150 char *res;
152 if (!cal_events) return NULL;
154 res = g_strnfill(SIPE_FREE_BUSY_PERIOD_SEC / SIPE_FREE_BUSY_GRANULARITY_SEC,
155 SIPE_CAL_FREE + '0');
157 while (entry) {
158 struct sipe_cal_event *cal_event = entry->data;
159 int start = sipe_domino_get_slot_no(fb_start, cal_event->start_time);
160 int end = sipe_domino_get_slot_no(fb_start, (cal_event->end_time - 1));
161 int i;
163 for (i = start; i <= end; i++) {
164 res[i] = SIPE_CAL_BUSY + '0';
166 entry = entry->next;
168 SIPE_DEBUG_INFO("sipe_domino_get_free_busy: res=\n%s", res);
169 return res;
172 static void
173 sipe_domino_process_calendar_response(int return_code,
174 const char *body,
175 const char *content_type,
176 HttpConn *conn,
177 void *data)
179 struct sipe_calendar *cal = data;
181 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_process_calendar_response: cb started.");
183 http_conn_set_close(conn);
184 cal->http_conn = NULL;
186 if (content_type && !g_str_has_prefix(content_type, "text/xml")) {
187 cal->is_domino_disabled = TRUE;
188 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_process_calendar_response: not XML, disabling.");
189 return;
192 if (return_code == 200 && body) {
193 struct sipe_core_private *sipe_private = cal->sipe_private;
194 const sipe_xml *node, *node2, *node3;
195 sipe_xml *xml;
197 SIPE_DEBUG_INFO("sipe_domino_process_calendar_response: SUCCESS, ret=%d", return_code);
198 xml = sipe_xml_parse(body, strlen(body));
200 sipe_cal_events_free(cal->cal_events);
201 cal->cal_events = NULL;
202 /* viewentry */
203 for (node = sipe_xml_child(xml, "viewentry");
204 node;
205 node = sipe_xml_twin(node))
207 struct sipe_cal_event *cal_event = g_new0(struct sipe_cal_event, 1);
208 cal->cal_events = g_slist_append(cal->cal_events, cal_event);
209 cal_event->cal_status = SIPE_CAL_BUSY;
210 cal_event->is_meeting = TRUE;
212 /* SIPE_DEBUG_INFO("viewentry unid=%s", sipe_xml_attribute(node, "unid")); */
214 /* entrydata */
215 for (node2 = sipe_xml_child(node, "entrydata");
216 node2;
217 node2 = sipe_xml_twin(node2))
219 const char *name = sipe_xml_attribute(node2, "name");
221 SIPE_DEBUG_INFO("\tentrydata name=%s", name);
223 if (sipe_strequal(name, VIEWENTITY_START0_TIME) ||
224 sipe_strequal(name, VIEWENTITY_START_TIME) ||
225 sipe_strequal(name, VIEWENTITY_END_TIME))
227 char *tmp = sipe_xml_data(sipe_xml_child(node2, "datetime"));
228 time_t time_val = sipe_utils_str_to_time(tmp);
230 if (sipe_strequal(name, VIEWENTITY_START_TIME)) {
231 cal_event->start_time = time_val;
232 } else if (sipe_strequal(name, VIEWENTITY_END_TIME)) {
233 cal_event->end_time = time_val;
236 SIPE_DEBUG_INFO("\t\tdatetime=%s", asctime(gmtime(&time_val)));
237 g_free(tmp);
238 } else if (sipe_strequal(name, VIEWENTITY_TEXT_LIST)) {
239 int i = 0;
241 /* test */
242 for (node3 = sipe_xml_child(node2, "textlist/text");
243 node3;
244 node3 = sipe_xml_twin(node3))
246 char *tmp = sipe_xml_data(node3);
248 if (!tmp) continue;
250 SIPE_DEBUG_INFO("\t\ttext=%s", tmp);
251 if (i == 0) {
252 cal_event->subject = g_strdup(tmp);
253 SIPE_DEBUG_INFO("\t\t*Subj.=%s", tmp);
254 } else {
255 /* plain English, don't localize! */
256 if (!g_ascii_strncasecmp(tmp, "Location:", 9)) {
257 if (strlen(tmp) > 9) {
258 cal_event->location = g_strdup(g_strstrip(tmp+9));
259 SIPE_DEBUG_INFO("\t\t*Loc.=%s", cal_event->location);
261 /* Translators: (!) should be as in localized Lotus Notes to be able to extract meeting location */
262 } else if (g_str_has_prefix(tmp, _("Location:"))) {
263 guint len = strlen(_("Location:"));
264 if (strlen(tmp) > len) {
265 cal_event->location = g_strdup(g_strstrip(tmp+len));
266 SIPE_DEBUG_INFO("\t\t*Loc.=%s", cal_event->location);
270 i++;
271 g_free(tmp);
276 sipe_xml_free(xml);
278 /* creates FreeBusy from cal->cal_events */
279 g_free(cal->free_busy);
280 cal->free_busy = sipe_domino_get_free_busy(cal->fb_start, cal->cal_events);
282 /* update SIP server */
283 cal->is_updated = TRUE;
284 sipe_cal_presence_publish(sipe_private, TRUE);
286 } else if (return_code < 0) {
287 SIPE_DEBUG_INFO("sipe_domino_process_calendar_response: rather FAILURE, ret=%d", return_code);
290 if (cal->http_session) {
291 http_conn_session_free(cal->http_session);
292 cal->http_session = NULL;
296 /* Domino doesn't like '-' and ':' in ISO timestamps */
297 static gchar *
298 sipe_domino_time_to_str(time_t timestamp)
300 char *res, *tmp;
302 res = sipe_utils_time_to_str(timestamp);
303 res = sipe_utils_str_replace((tmp = res), "-", "");
304 g_free(tmp);
305 res = sipe_utils_str_replace((tmp = res), ":", "");
306 g_free(tmp);
308 return res;
311 static void
312 sipe_domino_do_calendar_request(struct sipe_calendar *cal)
314 if (cal->domino_url) {
315 char *url_req;
316 char *url;
317 time_t end;
318 time_t now = time(NULL);
319 char *start_str;
320 char *end_str;
321 struct tm *now_tm;
323 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_do_calendar_request: going Calendar req.");
325 now_tm = gmtime(&now);
326 /* start -1 day, 00:00:00 */
327 now_tm->tm_sec = 0;
328 now_tm->tm_min = 0;
329 now_tm->tm_hour = 0;
330 cal->fb_start = sipe_mktime_tz(now_tm, "UTC");
331 cal->fb_start -= 24*60*60;
332 /* end = start + 4 days - 1 sec */
333 end = cal->fb_start + SIPE_FREE_BUSY_PERIOD_SEC - 1;
335 start_str = sipe_domino_time_to_str(cal->fb_start);
336 end_str = sipe_domino_time_to_str(end);
338 url_req = g_strdup_printf(SIPE_DOMINO_CALENDAR_REQUEST, start_str, end_str);
339 g_free(start_str);
340 g_free(end_str);
342 url = g_strconcat(cal->domino_url, url_req, NULL);
343 g_free(url_req);
344 if (!cal->http_conn || http_conn_is_closed(cal->http_conn)) {
345 cal->http_conn = http_conn_create(
346 (struct sipe_core_public *) cal->sipe_private,
347 cal->http_session,
348 HTTP_CONN_GET,
349 HTTP_CONN_SSL,
350 HTTP_CONN_NO_REDIRECT,
351 url,
352 NULL, /* body */
353 NULL, /* content-type */
354 NULL,
355 cal->auth,
356 sipe_domino_process_calendar_response,
357 cal);
358 } else {
359 http_conn_send(cal->http_conn,
360 HTTP_CONN_GET,
361 url,
362 NULL, /* body */
363 NULL, /* content-type */
364 sipe_domino_process_calendar_response,
365 cal);
367 g_free(url);
371 static void
372 sipe_domino_process_login_response(int return_code,
373 /* temporary? */
374 SIPE_UNUSED_PARAMETER const char *body,
375 SIPE_UNUSED_PARAMETER const char *content_type,
376 HttpConn *conn,
377 void *data)
379 struct sipe_calendar *cal = data;
381 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_process_login_response: cb started.");
383 if (return_code >= 200 && return_code < 400) {
384 SIPE_DEBUG_INFO("sipe_domino_process_login_response: rather SUCCESS, ret=%d", return_code);
386 /* next query */
387 sipe_domino_do_calendar_request(cal);
389 } else if (return_code < 0 || return_code >= 400) {
390 SIPE_DEBUG_INFO("sipe_domino_process_login_response: rather FAILURE, ret=%d", return_code);
392 /* stop here */
393 /* cal->is_domino_disabled = TRUE; */
395 http_conn_set_close(conn);
396 cal->http_conn = NULL;
400 static gchar *sipe_domino_uri_escape(const gchar *string)
402 gchar *escaped;
404 if (!string) return(NULL);
405 if (!g_utf8_validate(string, -1, NULL)) return(NULL);
407 #if GLIB_CHECK_VERSION(2,16,0)
408 escaped = g_uri_escape_string(string, NULL, FALSE);
409 #else
410 /* loosely based on libpurple/util.c:purple_url_encode() */
412 GString *buf = g_string_new(NULL);
414 while (*string) {
415 gunichar c = g_utf8_get_char(string);
417 /* If the character is an ASCII character and is alphanumeric
418 * no need to escape */
419 if (c < 128 &&
420 (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')) {
421 g_string_append_c(buf, c);
422 } else {
423 gchar *p, utf_char[6];
424 guint bytes = g_unichar_to_utf8(c, utf_char);
426 p = utf_char;
427 while (bytes-- > 0) {
428 g_string_append_printf(buf,
429 "%%%02X",
430 *p++ & 0xff);
434 string = g_utf8_next_char(string);
437 escaped = g_string_free(buf, FALSE);
439 #endif
441 return(escaped);
444 static void
445 sipe_domino_do_login_request(struct sipe_calendar *cal)
447 if (cal->domino_url) {
448 char *body;
449 const char *content_type = "application/x-www-form-urlencoded";
450 char *login_url = g_strconcat(cal->domino_url, "/?Login", NULL);
451 char *user;
452 char *password;
454 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_do_login_request: going Login req.");
456 if (!cal->auth) return;
458 /* @TODO replace purple_url_encode() with non-purple equiv. */
459 user = sipe_domino_uri_escape(cal->email);
460 password = sipe_domino_uri_escape(cal->auth->password);
462 body = g_strdup_printf(SIPE_DOMINO_LOGIN_REQUEST, user, password);
463 g_free(user);
464 g_free(password);
466 cal->http_conn = http_conn_create((struct sipe_core_public *) cal->sipe_private,
467 cal->http_session,
468 HTTP_CONN_POST,
469 HTTP_CONN_SSL,
470 HTTP_CONN_NO_REDIRECT,
471 login_url,
472 body,
473 content_type,
474 NULL,
475 cal->auth,
476 sipe_domino_process_login_response,
477 cal);
478 g_free(login_url);
479 g_free(body);
483 /* in notes.ni
484 MailFile=mail5\mhe111bm.nsf
485 MailServer=CN=MSGM2222/OU=srv/O=xxcom
487 Output values should be freed if requested.
489 static void
490 sipe_domino_read_notes_ini(const char *filename_with_path, char **mail_server, char **mail_file)
492 char rbuf[256];
493 FILE *fp = fopen(filename_with_path, "r+");
495 if (fp) {
496 while (fgets(rbuf, sizeof (rbuf), fp)) {
497 char *prop = "MailFile=";
498 guint prop_len = strlen(prop);
500 /* SIPE_DEBUG_INFO("\t%s (%"G_GSIZE_FORMAT")", rbuf, strlen(rbuf)); */
501 if (mail_file && !g_ascii_strncasecmp(rbuf, prop, prop_len) && (strlen(rbuf) > prop_len)) {
502 *mail_file = g_strdup(g_strstrip((rbuf+prop_len)));
505 prop = "MailServer=";
506 prop_len = strlen(prop);
508 if (mail_server && !g_ascii_strncasecmp(rbuf, prop, prop_len) && (strlen(rbuf) > prop_len)) {
509 *mail_server = g_strdup(g_strstrip((rbuf+prop_len)));
512 fclose(fp);
513 } else {
514 SIPE_DEBUG_ERROR("sipe_domino_read_notes_ini(): could not open `%s': %s", filename_with_path, g_strerror (errno));
519 @param protocol Ex.: https
520 @param mail_server Ex.: CN=MSGM2222/OU=srv/O=xxcom
521 @param mail_file Ex.: mail5\mhe111bm.nsf
523 @return Ex.: https://msgm2222/mail5/mhe111bm.nsf
525 static char *
526 sipe_domino_compose_url(const char *protocol, const char *mail_server, const char *mail_file)
528 const char *ptr;
529 char *tmp, *tmp2, *tmp3;
531 g_return_val_if_fail(protocol, NULL);
532 g_return_val_if_fail(mail_server, NULL);
533 g_return_val_if_fail(mail_file, NULL);
535 /* mail_server: exptacting just common name */
536 if ((ptr = strstr(mail_server, "/"))) {
537 tmp = g_strndup(mail_server, (ptr-mail_server));
538 } else {
539 tmp = g_strdup(mail_server);
541 if ((!g_ascii_strncasecmp(tmp, "CN=", 3))) {
542 tmp2 = g_strdup(tmp+3);
543 } else {
544 tmp2 = g_strdup(tmp);
546 g_free(tmp);
547 tmp = g_ascii_strdown(tmp2, -1);
548 g_free(tmp2);
550 /* mail_file */
551 tmp3 = sipe_utils_str_replace(mail_file, "\\", "/");
553 tmp2 = g_strconcat(protocol, "://", tmp, "/", tmp3, NULL);
554 g_free(tmp);
555 g_free(tmp3);
557 return tmp2;
560 void
561 sipe_domino_update_calendar(struct sipe_core_private *sipe_private)
563 struct sipe_calendar* cal;
565 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: started.");
567 sipe_cal_calendar_init(sipe_private, NULL);
569 /* check if URL is valid if provided */
570 cal = sipe_private->calendar;
571 if (cal && !is_empty(cal->domino_url)) {
572 char *tmp = g_ascii_strdown(cal->domino_url, -1);
573 if (!g_str_has_suffix(tmp, ".nsf")) {
574 /* not valid Domino mail services URL */
575 cal->is_domino_disabled = TRUE;
576 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: invalid Domino URI supplied, disabling.");
578 g_free(tmp);
581 /* Autodiscovery.
582 * Searches location of notes.ini in Registry, reads it, extracts mail server and mail file,
583 * composes HTTPS URL to Domino web, basing on that
585 if (cal && is_empty(cal->domino_url)) {
586 char *path = NULL;
587 #ifdef _WIN32
588 /* fine for Notes 8.5 too */
589 path = wpurple_read_reg_expand_string(HKEY_CURRENT_USER, "Software\\Lotus\\Notes\\8.0", "NotesIniPath");
590 if (is_empty(path)) {
591 g_free(path);
592 path = wpurple_read_reg_expand_string(HKEY_CURRENT_USER, "Software\\Lotus\\Notes\\7.0", "NotesIniPath");
593 if (is_empty(path)) {
594 g_free(path);
595 path = wpurple_read_reg_expand_string(HKEY_CURRENT_USER, "Software\\Lotus\\Notes\\6.0", "NotesIniPath");
596 if (is_empty(path)) {
597 g_free(path);
598 path = wpurple_read_reg_expand_string(HKEY_CURRENT_USER, "Software\\Lotus\\Notes\\5.0", "NotesIniPath");
602 SIPE_DEBUG_INFO("sipe_domino_update_calendar: notes.ini path:\n%s", path ? path : "");
603 #else
604 /* How to know location of notes.ini on *NIX ? */
605 #endif
607 /* get server url */
608 if (path) {
609 char *mail_server = NULL;
610 char *mail_file = NULL;
612 sipe_domino_read_notes_ini(path, &mail_server, &mail_file);
613 g_free(path);
614 SIPE_DEBUG_INFO("sipe_domino_update_calendar: mail_server=%s", mail_server ? mail_server : "");
615 SIPE_DEBUG_INFO("sipe_domino_update_calendar: mail_file=%s", mail_file ? mail_file : "");
617 g_free(cal->domino_url);
618 cal->domino_url = sipe_domino_compose_url("https", mail_server, mail_file);
619 g_free(mail_server);
620 g_free(mail_file);
621 SIPE_DEBUG_INFO("sipe_domino_update_calendar: cal->domino_url=%s", cal->domino_url ? cal->domino_url : "");
622 } else {
623 /* No domino_url, no path discovered, disabling */
624 cal->is_domino_disabled = TRUE;
625 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: Domino URI hasn't been discovered, neither provided, disabling.");
629 if (cal) {
631 /* create session */
632 if (cal->http_session) {
633 http_conn_session_free(cal->http_session);
635 cal->http_session = http_conn_session_create();
637 if (cal->is_domino_disabled) {
638 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: disabled, exiting.");
639 return;
642 sipe_domino_do_login_request(cal);
645 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: finished.");
649 Local Variables:
650 mode: c
651 c-file-style: "bsd"
652 indent-tabs-mode: t
653 tab-width: 8
654 End: