6 * Copyright (C) 2010-12 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
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)
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
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.
68 /* for registry read */
70 #include "sipe-win32dep.h"
73 #include "http-conn.h"
74 #include "sipe-common.h"
75 #include "sipe-core.h"
76 #include "sipe-core-private.h"
78 #include "sipe-backend.h"
79 #include "sipe-utils.h"
82 #include "sipe-domino.h"
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"
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="
105 <viewentry position="77" unid="C3A77CC76EAA7D08802576FD0043D7D0" noteid="27B42" siblings="77">
106 <entrydata columnnumber="0" name="$134">
107 <datetime>20100423T103000,00Z</datetime>
109 <entrydata columnnumber="1" name="$149">
112 <entrydata columnnumber="2" name="$144">
113 <datetime>20100423T103000,00Z</datetime>
115 <entrydata columnnumber="3" name="$145">
118 <entrydata columnnumber="4" name="$146">
119 <datetime>20100423T120000,00Z</datetime>
121 <entrydata columnnumber="5" name="$147">
123 <text>G. S. ..I. L. T. Hall</text>
124 <text>Location: Auditorium - W. House</text>
125 <text>Chair: S. S.</text>
133 #define VIEWENTITY_START0_TIME "$134"
134 #define VIEWENTITY_START_TIME "$144"
135 #define VIEWENTITY_END_TIME "$146"
136 #define VIEWENTITY_TEXT_LIST "$147"
140 sipe_domino_get_slot_no(time_t fb_start
, time_t in
)
142 return (in
- fb_start
) / SIPE_FREE_BUSY_GRANULARITY_SEC
;
146 sipe_domino_get_free_busy(time_t fb_start
,
149 GSList
*entry
= cal_events
;
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');
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));
163 for (i
= start
; i
<= end
; i
++) {
164 res
[i
] = SIPE_CAL_BUSY
+ '0';
168 SIPE_DEBUG_INFO("sipe_domino_get_free_busy: res=\n%s", res
);
173 sipe_domino_process_calendar_response(int return_code
,
179 struct sipe_calendar
*cal
= data
;
180 const gchar
*content_type
= sipe_utils_nameval_find(headers
, "Content-Type");
182 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_process_calendar_response: cb started.");
184 http_conn_set_close(conn
);
185 cal
->http_conn
= NULL
;
187 if (content_type
&& !g_str_has_prefix(content_type
, "text/xml")) {
188 cal
->is_domino_disabled
= TRUE
;
189 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_process_calendar_response: not XML, disabling.");
193 if (return_code
== 200 && body
) {
194 struct sipe_core_private
*sipe_private
= cal
->sipe_private
;
195 const sipe_xml
*node
, *node2
, *node3
;
198 SIPE_DEBUG_INFO("sipe_domino_process_calendar_response: SUCCESS, ret=%d", return_code
);
199 xml
= sipe_xml_parse(body
, strlen(body
));
201 sipe_cal_events_free(cal
->cal_events
);
202 cal
->cal_events
= NULL
;
204 for (node
= sipe_xml_child(xml
, "viewentry");
206 node
= sipe_xml_twin(node
))
208 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
209 cal
->cal_events
= g_slist_append(cal
->cal_events
, cal_event
);
210 cal_event
->cal_status
= SIPE_CAL_BUSY
;
211 cal_event
->is_meeting
= TRUE
;
213 /* SIPE_DEBUG_INFO("viewentry unid=%s", sipe_xml_attribute(node, "unid")); */
216 for (node2
= sipe_xml_child(node
, "entrydata");
218 node2
= sipe_xml_twin(node2
))
220 const char *name
= sipe_xml_attribute(node2
, "name");
222 SIPE_DEBUG_INFO("\tentrydata name=%s", name
);
224 if (sipe_strequal(name
, VIEWENTITY_START0_TIME
) ||
225 sipe_strequal(name
, VIEWENTITY_START_TIME
) ||
226 sipe_strequal(name
, VIEWENTITY_END_TIME
))
228 char *tmp
= sipe_xml_data(sipe_xml_child(node2
, "datetime"));
229 time_t time_val
= sipe_utils_str_to_time(tmp
);
231 if (sipe_strequal(name
, VIEWENTITY_START_TIME
)) {
232 cal_event
->start_time
= time_val
;
233 } else if (sipe_strequal(name
, VIEWENTITY_END_TIME
)) {
234 cal_event
->end_time
= time_val
;
237 SIPE_DEBUG_INFO("\t\tdatetime=%s", asctime(gmtime(&time_val
)));
239 } else if (sipe_strequal(name
, VIEWENTITY_TEXT_LIST
)) {
243 for (node3
= sipe_xml_child(node2
, "textlist/text");
245 node3
= sipe_xml_twin(node3
))
247 char *tmp
= sipe_xml_data(node3
);
251 SIPE_DEBUG_INFO("\t\ttext=%s", tmp
);
253 cal_event
->subject
= g_strdup(tmp
);
254 SIPE_DEBUG_INFO("\t\t*Subj.=%s", tmp
);
256 /* plain English, don't localize! */
257 if (!g_ascii_strncasecmp(tmp
, "Location:", 9)) {
258 if (strlen(tmp
) > 9) {
259 cal_event
->location
= g_strdup(g_strstrip(tmp
+9));
260 SIPE_DEBUG_INFO("\t\t*Loc.=%s", cal_event
->location
);
262 /* Translators: (!) should be as in localized Lotus Notes to be able to extract meeting location */
263 } else if (g_str_has_prefix(tmp
, _("Location:"))) {
264 guint len
= strlen(_("Location:"));
265 if (strlen(tmp
) > len
) {
266 cal_event
->location
= g_strdup(g_strstrip(tmp
+len
));
267 SIPE_DEBUG_INFO("\t\t*Loc.=%s", cal_event
->location
);
279 /* creates FreeBusy from cal->cal_events */
280 g_free(cal
->free_busy
);
281 cal
->free_busy
= sipe_domino_get_free_busy(cal
->fb_start
, cal
->cal_events
);
283 /* update SIP server */
284 cal
->is_updated
= TRUE
;
285 sipe_cal_presence_publish(sipe_private
, TRUE
);
287 } else if (return_code
< 0) {
288 SIPE_DEBUG_INFO("sipe_domino_process_calendar_response: rather FAILURE, ret=%d", return_code
);
291 if (cal
->http_session
) {
292 http_conn_session_free(cal
->http_session
);
293 cal
->http_session
= NULL
;
297 /* Domino doesn't like '-' and ':' in ISO timestamps */
299 sipe_domino_time_to_str(time_t timestamp
)
303 res
= sipe_utils_time_to_str(timestamp
);
304 res
= sipe_utils_str_replace((tmp
= res
), "-", "");
306 res
= sipe_utils_str_replace((tmp
= res
), ":", "");
313 sipe_domino_do_calendar_request(struct sipe_calendar
*cal
)
315 if (cal
->domino_url
) {
319 time_t now
= time(NULL
);
324 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_do_calendar_request: going Calendar req.");
326 now_tm
= gmtime(&now
);
327 /* start -1 day, 00:00:00 */
331 cal
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
332 cal
->fb_start
-= 24*60*60;
333 /* end = start + 4 days - 1 sec */
334 end
= cal
->fb_start
+ SIPE_FREE_BUSY_PERIOD_SEC
- 1;
336 start_str
= sipe_domino_time_to_str(cal
->fb_start
);
337 end_str
= sipe_domino_time_to_str(end
);
339 url_req
= g_strdup_printf(SIPE_DOMINO_CALENDAR_REQUEST
, start_str
, end_str
);
343 url
= g_strconcat(cal
->domino_url
, url_req
, NULL
);
345 if (!cal
->http_conn
|| http_conn_is_closed(cal
->http_conn
)) {
346 cal
->http_conn
= http_conn_create(
347 (struct sipe_core_public
*) cal
->sipe_private
,
351 HTTP_CONN_NO_REDIRECT
,
354 NULL
, /* content-type */
357 sipe_domino_process_calendar_response
,
360 http_conn_send(cal
->http_conn
,
364 NULL
, /* content-type */
365 sipe_domino_process_calendar_response
,
373 sipe_domino_process_login_response(int return_code
,
375 SIPE_UNUSED_PARAMETER
const char *body
,
376 SIPE_UNUSED_PARAMETER GSList
*headers
,
380 struct sipe_calendar
*cal
= data
;
382 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_process_login_response: cb started.");
384 if (return_code
>= 200 && return_code
< 400) {
385 SIPE_DEBUG_INFO("sipe_domino_process_login_response: rather SUCCESS, ret=%d", return_code
);
388 sipe_domino_do_calendar_request(cal
);
390 } else if (return_code
< 0 || return_code
>= 400) {
391 SIPE_DEBUG_INFO("sipe_domino_process_login_response: rather FAILURE, ret=%d", return_code
);
394 /* cal->is_domino_disabled = TRUE; */
396 http_conn_set_close(conn
);
397 cal
->http_conn
= NULL
;
401 static gchar
*sipe_domino_uri_escape(const gchar
*string
)
405 if (!string
) return(NULL
);
406 if (!g_utf8_validate(string
, -1, NULL
)) return(NULL
);
408 #if GLIB_CHECK_VERSION(2,16,0)
409 escaped
= g_uri_escape_string(string
, NULL
, FALSE
);
411 /* loosely based on libpurple/util.c:purple_url_encode() */
413 GString
*buf
= g_string_new(NULL
);
416 gunichar c
= g_utf8_get_char(string
);
418 /* If the character is an ASCII character and is alphanumeric
419 * no need to escape */
421 (isalnum(c
) || c
== '-' || c
== '.' || c
== '_' || c
== '~')) {
422 g_string_append_c(buf
, c
);
424 gchar
*p
, utf_char
[6];
425 guint bytes
= g_unichar_to_utf8(c
, utf_char
);
428 while (bytes
-- > 0) {
429 g_string_append_printf(buf
,
435 string
= g_utf8_next_char(string
);
438 escaped
= g_string_free(buf
, FALSE
);
446 sipe_domino_do_login_request(struct sipe_calendar
*cal
)
448 if (cal
->domino_url
) {
450 const char *content_type
= "application/x-www-form-urlencoded";
451 char *login_url
= g_strconcat(cal
->domino_url
, "/?Login", NULL
);
455 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_do_login_request: going Login req.");
457 if (!cal
->auth
) return;
459 /* @TODO replace purple_url_encode() with non-purple equiv. */
460 user
= sipe_domino_uri_escape(cal
->email
);
461 password
= sipe_domino_uri_escape(cal
->auth
->password
);
463 body
= g_strdup_printf(SIPE_DOMINO_LOGIN_REQUEST
, user
, password
);
467 cal
->http_conn
= http_conn_create((struct sipe_core_public
*) cal
->sipe_private
,
471 HTTP_CONN_NO_REDIRECT
,
477 sipe_domino_process_login_response
,
485 MailFile=mail5\mhe111bm.nsf
486 MailServer=CN=MSGM2222/OU=srv/O=xxcom
488 Output values should be freed if requested.
491 sipe_domino_read_notes_ini(const char *filename_with_path
, char **mail_server
, char **mail_file
)
494 FILE *fp
= fopen(filename_with_path
, "r+");
497 while (fgets(rbuf
, sizeof (rbuf
), fp
)) {
498 char *prop
= "MailFile=";
499 guint prop_len
= strlen(prop
);
501 /* SIPE_DEBUG_INFO("\t%s (%"G_GSIZE_FORMAT")", rbuf, strlen(rbuf)); */
502 if (mail_file
&& !g_ascii_strncasecmp(rbuf
, prop
, prop_len
) && (strlen(rbuf
) > prop_len
)) {
503 *mail_file
= g_strdup(g_strstrip((rbuf
+prop_len
)));
506 prop
= "MailServer=";
507 prop_len
= strlen(prop
);
509 if (mail_server
&& !g_ascii_strncasecmp(rbuf
, prop
, prop_len
) && (strlen(rbuf
) > prop_len
)) {
510 *mail_server
= g_strdup(g_strstrip((rbuf
+prop_len
)));
515 SIPE_DEBUG_ERROR("sipe_domino_read_notes_ini(): could not open `%s': %s", filename_with_path
, g_strerror (errno
));
520 @param protocol Ex.: https
521 @param mail_server Ex.: CN=MSGM2222/OU=srv/O=xxcom
522 @param mail_file Ex.: mail5\mhe111bm.nsf
524 @return Ex.: https://msgm2222/mail5/mhe111bm.nsf
527 sipe_domino_compose_url(const char *protocol
, const char *mail_server
, const char *mail_file
)
530 char *tmp
, *tmp2
, *tmp3
;
532 g_return_val_if_fail(protocol
, NULL
);
533 g_return_val_if_fail(mail_server
, NULL
);
534 g_return_val_if_fail(mail_file
, NULL
);
536 /* mail_server: exptacting just common name */
537 if ((ptr
= strstr(mail_server
, "/"))) {
538 tmp
= g_strndup(mail_server
, (ptr
-mail_server
));
540 tmp
= g_strdup(mail_server
);
542 if ((!g_ascii_strncasecmp(tmp
, "CN=", 3))) {
543 tmp2
= g_strdup(tmp
+3);
545 tmp2
= g_strdup(tmp
);
548 tmp
= g_ascii_strdown(tmp2
, -1);
552 tmp3
= sipe_utils_str_replace(mail_file
, "\\", "/");
554 tmp2
= g_strconcat(protocol
, "://", tmp
, "/", tmp3
, NULL
);
562 sipe_domino_update_calendar(struct sipe_core_private
*sipe_private
)
564 struct sipe_calendar
* cal
;
566 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: started.");
568 sipe_cal_calendar_init(sipe_private
, NULL
);
570 /* check if URL is valid if provided */
571 cal
= sipe_private
->calendar
;
572 if (cal
&& !is_empty(cal
->domino_url
)) {
573 char *tmp
= g_ascii_strdown(cal
->domino_url
, -1);
574 if (!g_str_has_suffix(tmp
, ".nsf")) {
575 /* not valid Domino mail services URL */
576 cal
->is_domino_disabled
= TRUE
;
577 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: invalid Domino URI supplied, disabling.");
583 * Searches location of notes.ini in Registry, reads it, extracts mail server and mail file,
584 * composes HTTPS URL to Domino web, basing on that
586 if (cal
&& is_empty(cal
->domino_url
)) {
589 /* fine for Notes 8.5 too */
590 path
= wpurple_read_reg_expand_string(HKEY_CURRENT_USER
, "Software\\Lotus\\Notes\\8.0", "NotesIniPath");
591 if (is_empty(path
)) {
593 path
= wpurple_read_reg_expand_string(HKEY_CURRENT_USER
, "Software\\Lotus\\Notes\\7.0", "NotesIniPath");
594 if (is_empty(path
)) {
596 path
= wpurple_read_reg_expand_string(HKEY_CURRENT_USER
, "Software\\Lotus\\Notes\\6.0", "NotesIniPath");
597 if (is_empty(path
)) {
599 path
= wpurple_read_reg_expand_string(HKEY_CURRENT_USER
, "Software\\Lotus\\Notes\\5.0", "NotesIniPath");
603 SIPE_DEBUG_INFO("sipe_domino_update_calendar: notes.ini path:\n%s", path
? path
: "");
605 /* How to know location of notes.ini on *NIX ? */
610 char *mail_server
= NULL
;
611 char *mail_file
= NULL
;
613 sipe_domino_read_notes_ini(path
, &mail_server
, &mail_file
);
615 SIPE_DEBUG_INFO("sipe_domino_update_calendar: mail_server=%s", mail_server
? mail_server
: "");
616 SIPE_DEBUG_INFO("sipe_domino_update_calendar: mail_file=%s", mail_file
? mail_file
: "");
618 g_free(cal
->domino_url
);
619 cal
->domino_url
= sipe_domino_compose_url("https", mail_server
, mail_file
);
622 SIPE_DEBUG_INFO("sipe_domino_update_calendar: cal->domino_url=%s", cal
->domino_url
? cal
->domino_url
: "");
624 /* No domino_url, no path discovered, disabling */
625 cal
->is_domino_disabled
= TRUE
;
626 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: Domino URI hasn't been discovered, neither provided, disabling.");
633 if (cal
->http_session
) {
634 http_conn_session_free(cal
->http_session
);
636 cal
->http_session
= http_conn_session_create();
638 if (cal
->is_domino_disabled
) {
639 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: disabled, exiting.");
643 sipe_domino_do_login_request(cal
);
646 SIPE_DEBUG_INFO_NOFORMAT("sipe_domino_update_calendar: finished.");