6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010, 2009 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 Exchange 2007/2010 Web Server/Web Services:
28 1) Autodiscover (HTTPS POST request). With redirect support. XML content.
29 1.1) DNS SRV record _autodiscover._tcp.<domain> may also be resolved.
30 2) Availability Web service (SOAP = HTTPS POST + XML) call.
31 3) Out of Office (OOF) Web Service (SOAP = HTTPS POST + XML) call.
32 4) Web server authentication required - NTLM and/or Negotiate (Kerberos).
34 Note: ews - EWS stands for Exchange Web Services.
36 It will be able to retrieve our Calendar information (FreeBusy, WorkingHours,
37 Meetings Subject and Location, Is_Meeting) as well as our Out of Office (OOF) note
38 from Exchange Web Services for subsequent publishing.
40 Ref. for more implementation details:
41 http://sourceforge.net/projects/sipe/forums/forum/688535/topic/3403462
43 Similar functionality for Lotus Notes/Domino, iCalendar/CalDAV/Google would
44 be great to implement too.
52 #include "sipe-backend.h"
53 #include "sipe-common.h"
55 #include "sipe-core.h"
56 #include "sipe-core-private.h"
58 #include "sipe-http.h"
59 #include "sipe-utils.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 3
153 #define SIPE_EWS_STATE_AVAILABILITY_FAILURE -3
154 #define SIPE_EWS_STATE_OOF_SUCCESS 4
155 #define SIPE_EWS_STATE_OOF_FAILURE -4
158 sipe_ews_get_oof_note(struct sipe_calendar
*cal
)
160 time_t now
= time(NULL
);
162 if (!cal
|| !cal
->oof_state
) return NULL
;
164 if (sipe_strequal(cal
->oof_state
, "Enabled") ||
165 (sipe_strequal(cal
->oof_state
, "Scheduled") && now
>= cal
->oof_start
&& now
<= cal
->oof_end
))
167 return cal
->oof_note
;
176 sipe_ews_run_state_machine(struct sipe_calendar
*cal
);
178 static void sipe_ews_process_avail_response(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
180 SIPE_UNUSED_PARAMETER GSList
*headers
,
184 struct sipe_calendar
*cal
= data
;
186 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_avail_response: cb started.");
190 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
191 const sipe_xml
*node
;
192 const sipe_xml
*resp
;
193 /** ref: [MS-OXWAVLS] */
194 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
196 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
197 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
198 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
199 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
201 resp
= sipe_xml_child(xml
, "Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse");
202 if (!resp
) return; /* rather soap:Fault */
203 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
204 return; /* Error response */
208 g_free(cal
->free_busy
);
209 cal
->free_busy
= sipe_xml_data(sipe_xml_child(resp
, "FreeBusyView/MergedFreeBusy"));
212 node
= sipe_xml_child(resp
, "FreeBusyView/WorkingHours");
213 g_free(cal
->working_hours_xml_str
);
214 cal
->working_hours_xml_str
= sipe_xml_stringify(node
);
215 SIPE_DEBUG_INFO("sipe_ews_process_avail_response: cal->working_hours_xml_str:\n%s",
216 cal
->working_hours_xml_str
? cal
->working_hours_xml_str
: "");
218 sipe_cal_events_free(cal
->cal_events
);
219 cal
->cal_events
= NULL
;
221 for (node
= sipe_xml_child(resp
, "FreeBusyView/CalendarEventArray/CalendarEvent");
223 node
= sipe_xml_twin(node
))
228 <StartTime>2009-12-07T13:30:00</StartTime>
229 <EndTime>2009-12-07T14:30:00</EndTime>
230 <BusyType>Busy</BusyType>
231 <CalendarEventDetails>
233 <Subject>Lunch</Subject>
234 <Location>Cafe</Location>
235 <IsMeeting>false</IsMeeting>
236 <IsRecurring>true</IsRecurring>
237 <IsException>false</IsException>
238 <IsReminderSet>true</IsReminderSet>
239 <IsPrivate>false</IsPrivate>
240 </CalendarEventDetails>
243 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
244 cal
->cal_events
= g_slist_append(cal
->cal_events
, cal_event
);
246 tmp
= sipe_xml_data(sipe_xml_child(node
, "StartTime"));
247 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
250 tmp
= sipe_xml_data(sipe_xml_child(node
, "EndTime"));
251 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
254 tmp
= sipe_xml_data(sipe_xml_child(node
, "BusyType"));
255 if (sipe_strequal("Free", tmp
)) {
256 cal_event
->cal_status
= SIPE_CAL_FREE
;
257 } else if (sipe_strequal("Tentative", tmp
)) {
258 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
259 } else if (sipe_strequal("Busy", tmp
)) {
260 cal_event
->cal_status
= SIPE_CAL_BUSY
;
261 } else if (sipe_strequal("OOF", tmp
)) {
262 cal_event
->cal_status
= SIPE_CAL_OOF
;
264 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
268 cal_event
->subject
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Subject"));
269 cal_event
->location
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Location"));
271 tmp
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/IsMeeting"));
272 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
278 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
279 sipe_ews_run_state_machine(cal
);
282 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_FAILURE
;
283 sipe_ews_run_state_machine(cal
);
287 static void sipe_ews_process_oof_response(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
289 SIPE_UNUSED_PARAMETER GSList
*headers
,
293 struct sipe_calendar
*cal
= data
;
295 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_oof_response: cb started.");
299 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
301 const sipe_xml
*resp
;
302 const sipe_xml
*xn_duration
;
303 /** ref: [MS-OXWOOF] */
304 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
305 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
306 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
307 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
308 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
309 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
311 resp
= sipe_xml_child(xml
, "Body/GetUserOofSettingsResponse");
312 if (!resp
) return; /* rather soap:Fault */
313 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
314 return; /* Error response */
317 g_free(cal
->oof_state
);
318 cal
->oof_state
= sipe_xml_data(sipe_xml_child(resp
, "OofSettings/OofState"));
320 old_note
= cal
->oof_note
;
321 cal
->oof_note
= NULL
;
322 if (!sipe_strequal(cal
->oof_state
, "Disabled")) {
323 char *tmp
= sipe_xml_data(
324 sipe_xml_child(resp
, "OofSettings/InternalReply/Message"));
327 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
328 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
329 html
= g_strdup(tmp
+3);
331 html
= g_strdup(tmp
);
334 tmp
= g_strstrip(sipe_backend_markup_strip_html(html
));
336 cal
->oof_note
= g_markup_escape_text(tmp
, -1);
340 if (sipe_strequal(cal
->oof_state
, "Scheduled")
341 && (xn_duration
= sipe_xml_child(resp
, "OofSettings/Duration")))
343 char *tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "StartTime"));
344 cal
->oof_start
= sipe_utils_str_to_time(tmp
);
347 tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "EndTime"));
348 cal
->oof_end
= sipe_utils_str_to_time(tmp
);
352 if (!sipe_strequal(old_note
, cal
->oof_note
)) { /* oof note changed */
353 cal
->updated
= time(NULL
);
354 cal
->published
= FALSE
;
360 cal
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
361 sipe_ews_run_state_machine(cal
);
364 cal
->state
= SIPE_EWS_STATE_OOF_FAILURE
;
365 sipe_ews_run_state_machine(cal
);
369 static void sipe_ews_process_autodiscover(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
371 SIPE_UNUSED_PARAMETER GSList
*headers
,
375 struct sipe_calendar
*cal
= data
;
377 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_autodiscover: cb started.");
381 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
382 const sipe_xml
*node
;
383 /** ref: [MS-OXDSCLI] */
384 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
386 /* Autodiscover/Response/User/LegacyDN (trim()) */
387 cal
->legacy_dn
= sipe_xml_data(sipe_xml_child(xml
, "Response/User/LegacyDN"));
388 cal
->legacy_dn
= cal
->legacy_dn
? g_strstrip(cal
->legacy_dn
) : NULL
;
391 for (node
= sipe_xml_child(xml
, "Response/Account/Protocol");
393 node
= sipe_xml_twin(node
))
395 char *type
= sipe_xml_data(sipe_xml_child(node
, "Type"));
396 if (sipe_strequal("EXCH", type
)) {
397 cal
->as_url
= sipe_xml_data(sipe_xml_child(node
, "ASUrl"));
398 cal
->oof_url
= sipe_xml_data(sipe_xml_child(node
, "OOFUrl"));
399 cal
->oab_url
= sipe_xml_data(sipe_xml_child(node
, "OABUrl"));
401 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:as_url %s",
402 cal
->as_url
? cal
->as_url
: "");
403 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oof_url %s",
404 cal
->oof_url
? cal
->oof_url
: "");
405 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oab_url %s",
406 cal
->oab_url
? cal
->oab_url
: "");
418 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
419 sipe_ews_run_state_machine(cal
);
422 switch (cal
->auto_disco_method
) {
424 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
; break;
426 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
; break;
428 sipe_ews_run_state_machine(cal
);
432 static void sipe_ews_send_http_request(struct sipe_calendar
*cal
)
434 sipe_cal_http_authentication(cal
);
435 sipe_http_request_allow_redirect(cal
->request
);
436 sipe_http_request_ready(cal
->request
);
439 static void sipe_ews_do_autodiscover(struct sipe_calendar
*cal
,
440 const gchar
*autodiscover_url
)
444 SIPE_DEBUG_INFO("sipe_ews_do_autodiscover: going autodiscover url=%s",
445 autodiscover_url
? autodiscover_url
: "");
447 body
= g_strdup_printf(SIPE_EWS_AUTODISCOVER_REQUEST
, cal
->email
);
448 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
453 sipe_ews_process_autodiscover
,
457 sipe_ews_send_http_request(cal
);
460 static void sipe_ews_do_avail_request(struct sipe_calendar
*cal
)
465 time_t now
= time(NULL
);
470 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_avail_request: going Availability req.");
472 now_tm
= gmtime(&now
);
473 /* start -1 day, 00:00:00 */
477 cal
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
478 cal
->fb_start
-= 24*60*60;
479 /* end = start + 4 days - 1 sec */
480 end
= cal
->fb_start
+ SIPE_FREE_BUSY_PERIOD_SEC
- 1;
482 start_str
= sipe_utils_time_to_str(cal
->fb_start
);
483 end_str
= sipe_utils_time_to_str(end
);
485 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, cal
->email
, start_str
, end_str
);
486 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
490 "text/xml; charset=UTF-8",
491 sipe_ews_process_avail_response
,
497 sipe_ews_send_http_request(cal
);
501 static void sipe_ews_do_oof_request(struct sipe_calendar
*cal
)
506 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_oof_request: going OOF req.");
508 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, cal
->email
);
509 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
513 "text/xml; charset=UTF-8",
514 sipe_ews_process_oof_response
,
518 sipe_ews_send_http_request(cal
);
523 sipe_ews_run_state_machine(struct sipe_calendar
*cal
)
525 switch (cal
->state
) {
526 case SIPE_EWS_STATE_NONE
:
528 char *maildomain
= strstr(cal
->email
, "@") + 1;
529 char *autodisc_url
= g_strdup_printf("https://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
531 cal
->auto_disco_method
= 1;
533 sipe_ews_do_autodiscover(cal
, autodisc_url
);
535 g_free(autodisc_url
);
538 case SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
:
540 char *maildomain
= strstr(cal
->email
, "@") + 1;
541 char *autodisc_url
= g_strdup_printf("https://%s/Autodiscover/Autodiscover.xml", maildomain
);
543 cal
->auto_disco_method
= 2;
545 sipe_ews_do_autodiscover(cal
, autodisc_url
);
547 g_free(autodisc_url
);
550 case SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
:
551 case SIPE_EWS_STATE_AVAILABILITY_FAILURE
:
552 case SIPE_EWS_STATE_OOF_FAILURE
:
553 cal
->is_ews_disabled
= TRUE
;
555 case SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
:
556 sipe_ews_do_avail_request(cal
);
558 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
559 sipe_ews_do_oof_request(cal
);
561 case SIPE_EWS_STATE_OOF_SUCCESS
:
563 struct sipe_core_private
*sipe_private
= cal
->sipe_private
;
565 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
566 cal
->is_updated
= TRUE
;
567 sipe_cal_presence_publish(sipe_private
, TRUE
);
574 sipe_ews_update_calendar(struct sipe_core_private
*sipe_private
)
576 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
579 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: started.");
581 if (sipe_cal_calendar_init(sipe_private
, &has_url
)) {
583 sipe_private
->calendar
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
587 if (sipe_private
->calendar
->is_ews_disabled
) {
588 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: disabled, exiting.");
592 sipe_ews_run_state_machine(sipe_private
->calendar
);
594 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: finished.");