6 * Copyright (C) 2010, 2009 pier11 <pier11@operamail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 For communication with Exchange 2007/2010 Web Server/Web Services:
27 1) Autodiscover (HTTPS POST request). With redirect support. XML content.
28 1.1) DNS SRV record _autodiscover._tcp.<domain> may also be resolved.
29 2) Availability Web service (SOAP = HTTPS POST + XML) call.
30 3) Out of Office (OOF) Web Service (SOAP = HTTPS POST + XML) call.
31 4) Web server authentication required - NTLM and/or Negotiate (Kerberos).
33 Note: ews - EWS stands for Exchange Web Services.
35 It will be able to retrieve our Calendar information (FreeBusy, WorkingHours,
36 Meetings Subject and Location, Is_Meeting) as well as our Out of Office (OOF) note
37 from Exchange Web Services for subsequent publishing.
39 Ref. for more implementation details:
40 http://sourceforge.net/projects/sipe/forums/forum/688535/topic/3403462
42 Similar functionality for Lotus Notes/Domino, iCalendar/CalDAV/Google would
43 be great to implement too.
54 #include "sipe-backend.h"
57 #include "sipe-utils.h"
59 #include "http-conn.h"
63 * Autodiscover request for Exchange Web Services
64 * @param email (%s) Ex.: alice@cosmo.local
66 #define SIPE_EWS_AUTODISCOVER_REQUEST \
67 "<?xml version=\"1.0\"?>"\
68 "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">"\
70 "<EMailAddress>%s</EMailAddress>"\
71 "<AcceptableResponseSchema>"\
72 "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"\
73 "</AcceptableResponseSchema>"\
78 * GetUserOofSettingsRequest SOAP request to Exchange Web Services
79 * to obtain our Out-of-office (OOF) information.
80 * @param email (%s) Ex.: alice@cosmo.local
82 #define SIPE_EWS_USER_OOF_SETTINGS_REQUEST \
83 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
84 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"\
86 "<GetUserOofSettingsRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">"\
87 "<Mailbox xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
88 "<Address>%s</Address>"\
90 "</GetUserOofSettingsRequest>"\
95 * GetUserAvailabilityRequest SOAP request to Exchange Web Services
96 * to obtain our Availability (FreeBusy, WorkingHours, Meetings) information.
97 * @param email (%s) Ex.: alice@cosmo.local
98 * @param start_time (%s) Ex.: 2009-12-06T00:00:00
99 * @param end_time (%s) Ex.: 2009-12-09T23:59:59
101 #define SIPE_EWS_USER_AVAILABILITY_REQUEST \
102 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
103 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""\
104 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""\
105 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""\
106 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
108 "<GetUserAvailabilityRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\""\
109 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
110 "<t:TimeZone xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
114 "<Time>00:00:00</Time>"\
115 "<DayOrder>0</DayOrder>"\
117 "<DayOfWeek>Sunday</DayOfWeek>"\
121 "<Time>00:00:00</Time>"\
122 "<DayOrder>0</DayOrder>"\
124 "<DayOfWeek>Sunday</DayOfWeek>"\
127 "<MailboxDataArray>"\
130 "<t:Address>%s</t:Address>"\
132 "<t:AttendeeType>Required</t:AttendeeType>"\
133 "<t:ExcludeConflicts>false</t:ExcludeConflicts>"\
135 "</MailboxDataArray>"\
136 "<t:FreeBusyViewOptions>"\
138 "<t:StartTime>%s</t:StartTime>"\
139 "<t:EndTime>%s</t:EndTime>"\
141 "<t:MergedFreeBusyIntervalInMinutes>15</t:MergedFreeBusyIntervalInMinutes>"\
142 "<t:RequestedView>DetailedMerged</t:RequestedView>"\
143 "</t:FreeBusyViewOptions>"\
144 "</GetUserAvailabilityRequest>"\
148 #define SIPE_EWS_STATE_NONE 0
149 #define SIPE_EWS_STATE_AUTODISCOVER_SUCCESS 1
150 #define SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE -1
151 #define SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE -2
152 #define SIPE_EWS_STATE_AVAILABILITY_SUCCESS 2
153 #define SIPE_EWS_STATE_OOF_SUCCESS 3
157 sipe_ews_cal_events_free(GSList
*cal_events
)
159 GSList
*entry
= cal_events
;
161 if (!cal_events
) return;
164 struct sipe_cal_event
*cal_event
= entry
->data
;
165 sipe_cal_event_free(cal_event
);
169 g_slist_free(cal_events
);
173 sipe_ews_free(struct sipe_ews
* ews
)
176 g_free(ews
->legacy_dn
);
178 g_free(ews
->auth
->domain
);
179 g_free(ews
->auth
->user
);
180 g_free(ews
->auth
->password
);
184 g_free(ews
->oof_url
);
185 g_free(ews
->oab_url
);
186 g_free(ews
->oof_state
);
187 g_free(ews
->oof_note
);
188 g_free(ews
->free_busy
);
189 g_free(ews
->working_hours_xml_str
);
191 sipe_ews_cal_events_free(ews
->cal_events
);
197 sipe_ews_get_oof_note(struct sipe_ews
*ews
)
199 time_t now
= time(NULL
);
201 if (!ews
|| !ews
->oof_state
) return NULL
;
203 if (sipe_strequal(ews
->oof_state
, "Enabled") ||
204 (sipe_strequal(ews
->oof_state
, "Scheduled") && now
>= ews
->oof_start
&& now
<= ews
->oof_end
))
206 return ews
->oof_note
;
215 sipe_ews_run_state_machine(struct sipe_ews
*ews
);
218 sipe_ews_process_avail_response(int return_code
,
223 struct sipe_ews
*ews
= data
;
225 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_avail_response: cb started.");
227 if(!sipe_strequal(ews
->as_url
, ews
->oof_url
)) { /* whether reuse conn */
228 http_conn_set_close(conn
);
229 ews
->http_conn
= NULL
;
232 if (return_code
== 200 && body
) {
233 const sipe_xml
*node
;
234 const sipe_xml
*resp
;
235 /** ref: [MS-OXWAVLS] */
236 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
238 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
239 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
240 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
241 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
243 resp
= sipe_xml_child(xml
, "Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse");
244 if (!resp
) return; /* rather soap:Fault */
245 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
246 return; /* Error response */
250 g_free(ews
->free_busy
);
251 ews
->free_busy
= sipe_xml_data(sipe_xml_child(resp
, "FreeBusyView/MergedFreeBusy"));
254 node
= sipe_xml_child(resp
, "FreeBusyView/WorkingHours");
255 g_free(ews
->working_hours_xml_str
);
256 ews
->working_hours_xml_str
= sipe_xml_stringify(node
);
257 SIPE_DEBUG_INFO("sipe_ews_process_avail_response: ews->working_hours_xml_str:\n%s",
258 ews
->working_hours_xml_str
? ews
->working_hours_xml_str
: "");
260 sipe_ews_cal_events_free(ews
->cal_events
);
261 ews
->cal_events
= NULL
;
263 for (node
= sipe_xml_child(resp
, "FreeBusyView/CalendarEventArray/CalendarEvent");
265 node
= sipe_xml_twin(node
))
270 <StartTime>2009-12-07T13:30:00</StartTime>
271 <EndTime>2009-12-07T14:30:00</EndTime>
272 <BusyType>Busy</BusyType>
273 <CalendarEventDetails>
275 <Subject>Lunch</Subject>
276 <Location>Cafe</Location>
277 <IsMeeting>false</IsMeeting>
278 <IsRecurring>true</IsRecurring>
279 <IsException>false</IsException>
280 <IsReminderSet>true</IsReminderSet>
281 <IsPrivate>false</IsPrivate>
282 </CalendarEventDetails>
285 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
286 ews
->cal_events
= g_slist_append(ews
->cal_events
, cal_event
);
288 tmp
= sipe_xml_data(sipe_xml_child(node
, "StartTime"));
289 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
292 tmp
= sipe_xml_data(sipe_xml_child(node
, "EndTime"));
293 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
296 tmp
= sipe_xml_data(sipe_xml_child(node
, "BusyType"));
297 if (sipe_strequal("Free", tmp
)) {
298 cal_event
->cal_status
= SIPE_CAL_FREE
;
299 } else if (sipe_strequal("Tentative", tmp
)) {
300 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
301 } else if (sipe_strequal("Busy", tmp
)) {
302 cal_event
->cal_status
= SIPE_CAL_BUSY
;
303 } else if (sipe_strequal("OOF", tmp
)) {
304 cal_event
->cal_status
= SIPE_CAL_OOF
;
306 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
310 cal_event
->subject
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Subject"));
311 cal_event
->location
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Location"));
313 tmp
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/IsMeeting"));
314 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
320 ews
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
321 sipe_ews_run_state_machine(ews
);
323 } else if (return_code
< 0) {
324 ews
->http_conn
= NULL
;
329 sipe_ews_process_oof_response(int return_code
,
334 struct sipe_ews
*ews
= data
;
336 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_oof_response: cb started.");
338 http_conn_set_close(conn
);
339 ews
->http_conn
= NULL
;
341 if (return_code
== 200 && body
) {
343 const sipe_xml
*resp
;
344 const sipe_xml
*xn_duration
;
345 /** ref: [MS-OXWOOF] */
346 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
347 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
348 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
349 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
350 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
351 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
353 resp
= sipe_xml_child(xml
, "Body/GetUserOofSettingsResponse");
354 if (!resp
) return; /* rather soap:Fault */
355 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
356 return; /* Error response */
359 g_free(ews
->oof_state
);
360 ews
->oof_state
= sipe_xml_data(sipe_xml_child(resp
, "OofSettings/OofState"));
362 old_note
= ews
->oof_note
;
363 ews
->oof_note
= NULL
;
364 if (!sipe_strequal(ews
->oof_state
, "Disabled")) {
365 char *tmp
= sipe_xml_data(
366 sipe_xml_child(resp
, "OofSettings/InternalReply/Message"));
369 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
370 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
371 html
= g_strdup(tmp
+3);
373 html
= g_strdup(tmp
);
376 tmp
= g_strstrip(sipe_backend_markup_strip_html(html
));
378 ews
->oof_note
= g_markup_escape_text(tmp
, -1);
382 if (sipe_strequal(ews
->oof_state
, "Scheduled")
383 && (xn_duration
= sipe_xml_child(resp
, "OofSettings/Duration")))
385 char *tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "StartTime"));
386 ews
->oof_start
= sipe_utils_str_to_time(tmp
);
389 tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "EndTime"));
390 ews
->oof_end
= sipe_utils_str_to_time(tmp
);
394 if (!sipe_strequal(old_note
, ews
->oof_note
)) { /* oof note changed */
395 ews
->updated
= time(NULL
);
396 ews
->published
= FALSE
;
402 ews
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
403 sipe_ews_run_state_machine(ews
);
405 } else if (return_code
< 0) {
406 ews
->http_conn
= NULL
;
411 sipe_ews_process_autodiscover(int return_code
,
416 struct sipe_ews
*ews
= data
;
418 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_autodiscover: cb started.");
420 http_conn_set_close(conn
);
421 ews
->http_conn
= NULL
;
423 if (return_code
== 200 && body
) {
424 const sipe_xml
*node
;
425 /** ref: [MS-OXDSCLI] */
426 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
428 /* Autodiscover/Response/User/LegacyDN (trim()) */
429 ews
->legacy_dn
= sipe_xml_data(sipe_xml_child(xml
, "Response/User/LegacyDN"));
430 ews
->legacy_dn
= ews
->legacy_dn
? g_strstrip(ews
->legacy_dn
) : NULL
;
433 for (node
= sipe_xml_child(xml
, "Response/Account/Protocol");
435 node
= sipe_xml_twin(node
))
437 char *type
= sipe_xml_data(sipe_xml_child(node
, "Type"));
438 if (sipe_strequal("EXCH", type
)) {
439 ews
->as_url
= sipe_xml_data(sipe_xml_child(node
, "ASUrl"));
440 ews
->oof_url
= sipe_xml_data(sipe_xml_child(node
, "OOFUrl"));
441 ews
->oab_url
= sipe_xml_data(sipe_xml_child(node
, "OABUrl"));
443 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:as_url %s",
444 ews
->as_url
? ews
->as_url
: "");
445 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oof_url %s",
446 ews
->oof_url
? ews
->oof_url
: "");
447 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oab_url %s",
448 ews
->oab_url
? ews
->oab_url
: "");
460 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
461 sipe_ews_run_state_machine(ews
);
464 if (return_code
< 0) {
465 ews
->http_conn
= NULL
;
467 switch (ews
->auto_disco_method
) {
469 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
; break;
471 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
; break;
473 sipe_ews_run_state_machine(ews
);
478 sipe_ews_do_autodiscover(struct sipe_ews
*ews
,
479 const char* autodiscover_url
)
483 SIPE_DEBUG_INFO("sipe_ews_do_autodiscover: going autodiscover url=%s", autodiscover_url
? autodiscover_url
: "");
485 body
= g_strdup_printf(SIPE_EWS_AUTODISCOVER_REQUEST
, ews
->email
);
486 ews
->http_conn
= http_conn_create(
493 sipe_ews_process_autodiscover
,
499 sipe_ews_do_avail_request(struct sipe_ews
*ews
)
504 time_t now
= time(NULL
);
509 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_avail_request: going Availability req.");
511 now_tm
= gmtime(&now
);
512 /* start -1 day, 00:00:00 */
516 ews
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
517 ews
->fb_start
-= 24*60*60;
518 /* end = start + 4 days - 1 sec */
519 end
= ews
->fb_start
+ 4*(24*60*60) - 1;
521 start_str
= sipe_utils_time_to_str(ews
->fb_start
);
522 end_str
= sipe_utils_time_to_str(end
);
524 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, ews
->email
, start_str
, end_str
);
525 ews
->http_conn
= http_conn_create(
530 "text/xml; charset=UTF-8",
532 sipe_ews_process_avail_response
,
541 sipe_ews_do_oof_request(struct sipe_ews
*ews
)
545 const char *content_type
= "text/xml; charset=UTF-8";
547 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_oof_request: going OOF req.");
549 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, ews
->email
);
550 if (!ews
->http_conn
) {
551 ews
->http_conn
= http_conn_create(ews
->account
,
557 sipe_ews_process_oof_response
,
560 http_conn_post(ews
->http_conn
,
564 sipe_ews_process_oof_response
,
572 sipe_ews_run_state_machine(struct sipe_ews
*ews
)
574 switch (ews
->state
) {
575 case SIPE_EWS_STATE_NONE
:
577 char *maildomain
= strstr(ews
->email
, "@") + 1;
578 char *autodisc_url
= g_strdup_printf("https://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
580 ews
->auto_disco_method
= 1;
582 sipe_ews_do_autodiscover(ews
, autodisc_url
);
584 g_free(autodisc_url
);
587 case SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
:
589 char *maildomain
= strstr(ews
->email
, "@") + 1;
590 char *autodisc_url
= g_strdup_printf("https://%s/Autodiscover/Autodiscover.xml", maildomain
);
592 ews
->auto_disco_method
= 2;
594 sipe_ews_do_autodiscover(ews
, autodisc_url
);
596 g_free(autodisc_url
);
599 case SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
:
600 ews
->is_disabled
= TRUE
;
602 case SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
:
603 sipe_ews_do_avail_request(ews
);
605 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
606 sipe_ews_do_oof_request(ews
);
608 case SIPE_EWS_STATE_OOF_SUCCESS
:
609 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
610 ews
->is_updated
= TRUE
;
611 if (ews
->sip
->ocs2007
) {
613 publish_calendar_status_self(ews
->sip
);
616 send_presence_soap(ews
->sip
, TRUE
);
623 sipe_ews_update_calendar(struct sipe_account_data
*sip
)
625 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
627 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: started.");
632 sip
->ews
= g_new0(struct sipe_ews
, 1);
635 sip
->ews
->account
= sip
->account
;
636 sip
->ews
->email
= g_strdup(sip
->email
);
638 /* user specified a service URL? */
639 value
= purple_account_get_string(sip
->account
, "email_url", NULL
);
640 if (!is_empty(value
)) {
641 sip
->ews
->as_url
= g_strdup(value
);
642 sip
->ews
->oof_url
= g_strdup(value
);
643 sip
->ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
646 sip
->ews
->auth
= g_new0(HttpConnAuth
, 1);
647 sip
->ews
->auth
->use_negotiate
= purple_account_get_bool(sip
->account
, "krb5", FALSE
);
649 /* user specified email login? */
650 value
= purple_account_get_string(sip
->account
, "email_login", NULL
);
651 if (!is_empty(value
)) {
653 /* user specified email login domain? */
654 const char *tmp
= strstr(value
, "\\");
656 sip
->ews
->auth
->domain
= g_strndup(value
, tmp
- value
);
657 sip
->ews
->auth
->user
= g_strdup(tmp
+ 1);
659 sip
->ews
->auth
->user
= g_strdup(value
);
661 sip
->ews
->auth
->password
= g_strdup(purple_account_get_string(sip
->account
, "email_password", NULL
));
664 /* re-use SIPE credentials */
665 sip
->ews
->auth
->domain
= g_strdup(sip
->authdomain
);
666 sip
->ews
->auth
->user
= g_strdup(sip
->authuser
);
667 sip
->ews
->auth
->password
= g_strdup(sip
->password
);
671 if(sip
->ews
->is_disabled
) {
672 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: disabled, exiting.");
676 sipe_ews_run_state_machine(sip
->ews
);
678 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: finished.");