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-ews-autodiscover.h"
59 #include "sipe-http.h"
60 #include "sipe-utils.h"
64 * Autodiscover request for Exchange Web Services
65 * @param email (%s) Ex.: alice@cosmo.local
67 #define SIPE_EWS_AUTODISCOVER_REQUEST \
68 "<?xml version=\"1.0\"?>"\
69 "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">"\
71 "<EMailAddress>%s</EMailAddress>"\
72 "<AcceptableResponseSchema>"\
73 "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"\
74 "</AcceptableResponseSchema>"\
79 * GetUserOofSettingsRequest SOAP request to Exchange Web Services
80 * to obtain our Out-of-office (OOF) information.
81 * @param email (%s) Ex.: alice@cosmo.local
83 #define SIPE_EWS_USER_OOF_SETTINGS_REQUEST \
84 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
85 "<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/\">"\
87 "<GetUserOofSettingsRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">"\
88 "<Mailbox xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
89 "<Address>%s</Address>"\
91 "</GetUserOofSettingsRequest>"\
96 * GetUserAvailabilityRequest SOAP request to Exchange Web Services
97 * to obtain our Availability (FreeBusy, WorkingHours, Meetings) information.
98 * @param email (%s) Ex.: alice@cosmo.local
99 * @param start_time (%s) Ex.: 2009-12-06T00:00:00
100 * @param end_time (%s) Ex.: 2009-12-09T23:59:59
102 #define SIPE_EWS_USER_AVAILABILITY_REQUEST \
103 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
104 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""\
105 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""\
106 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""\
107 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
109 "<GetUserAvailabilityRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\""\
110 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
111 "<t:TimeZone xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
115 "<Time>00:00:00</Time>"\
116 "<DayOrder>0</DayOrder>"\
118 "<DayOfWeek>Sunday</DayOfWeek>"\
122 "<Time>00:00:00</Time>"\
123 "<DayOrder>0</DayOrder>"\
125 "<DayOfWeek>Sunday</DayOfWeek>"\
128 "<MailboxDataArray>"\
131 "<t:Address>%s</t:Address>"\
133 "<t:AttendeeType>Required</t:AttendeeType>"\
134 "<t:ExcludeConflicts>false</t:ExcludeConflicts>"\
136 "</MailboxDataArray>"\
137 "<t:FreeBusyViewOptions>"\
139 "<t:StartTime>%s</t:StartTime>"\
140 "<t:EndTime>%s</t:EndTime>"\
142 "<t:MergedFreeBusyIntervalInMinutes>15</t:MergedFreeBusyIntervalInMinutes>"\
143 "<t:RequestedView>DetailedMerged</t:RequestedView>"\
144 "</t:FreeBusyViewOptions>"\
145 "</GetUserAvailabilityRequest>"\
149 #define SIPE_EWS_STATE_NONE 0
150 #define SIPE_EWS_STATE_IDLE 1
151 #define SIPE_EWS_STATE_AVAILABILITY_SUCCESS 2
152 #define SIPE_EWS_STATE_AVAILABILITY_FAILURE -2
153 #define SIPE_EWS_STATE_OOF_SUCCESS 3
154 #define SIPE_EWS_STATE_OOF_FAILURE -3
157 sipe_ews_get_oof_note(struct sipe_calendar
*cal
)
159 time_t now
= time(NULL
);
161 if (!cal
|| !cal
->oof_state
) return NULL
;
163 if (sipe_strequal(cal
->oof_state
, "Enabled") ||
164 (sipe_strequal(cal
->oof_state
, "Scheduled") && now
>= cal
->oof_start
&& now
<= cal
->oof_end
))
166 return cal
->oof_note
;
175 sipe_ews_run_state_machine(struct sipe_calendar
*cal
);
177 static void sipe_ews_process_avail_response(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
179 SIPE_UNUSED_PARAMETER GSList
*headers
,
183 struct sipe_calendar
*cal
= data
;
185 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_avail_response: cb started.");
189 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
190 const sipe_xml
*node
;
191 const sipe_xml
*resp
;
192 /** ref: [MS-OXWAVLS] */
193 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
195 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
196 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
197 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
198 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
200 resp
= sipe_xml_child(xml
, "Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse");
201 if (!resp
) return; /* rather soap:Fault */
202 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
203 return; /* Error response */
207 g_free(cal
->free_busy
);
208 cal
->free_busy
= sipe_xml_data(sipe_xml_child(resp
, "FreeBusyView/MergedFreeBusy"));
211 node
= sipe_xml_child(resp
, "FreeBusyView/WorkingHours");
212 g_free(cal
->working_hours_xml_str
);
213 cal
->working_hours_xml_str
= sipe_xml_stringify(node
);
214 SIPE_DEBUG_INFO("sipe_ews_process_avail_response: cal->working_hours_xml_str:\n%s",
215 cal
->working_hours_xml_str
? cal
->working_hours_xml_str
: "");
217 sipe_cal_events_free(cal
->cal_events
);
218 cal
->cal_events
= NULL
;
220 for (node
= sipe_xml_child(resp
, "FreeBusyView/CalendarEventArray/CalendarEvent");
222 node
= sipe_xml_twin(node
))
227 <StartTime>2009-12-07T13:30:00</StartTime>
228 <EndTime>2009-12-07T14:30:00</EndTime>
229 <BusyType>Busy</BusyType>
230 <CalendarEventDetails>
232 <Subject>Lunch</Subject>
233 <Location>Cafe</Location>
234 <IsMeeting>false</IsMeeting>
235 <IsRecurring>true</IsRecurring>
236 <IsException>false</IsException>
237 <IsReminderSet>true</IsReminderSet>
238 <IsPrivate>false</IsPrivate>
239 </CalendarEventDetails>
242 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
243 cal
->cal_events
= g_slist_append(cal
->cal_events
, cal_event
);
245 tmp
= sipe_xml_data(sipe_xml_child(node
, "StartTime"));
246 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
249 tmp
= sipe_xml_data(sipe_xml_child(node
, "EndTime"));
250 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
253 tmp
= sipe_xml_data(sipe_xml_child(node
, "BusyType"));
254 if (sipe_strequal("Free", tmp
)) {
255 cal_event
->cal_status
= SIPE_CAL_FREE
;
256 } else if (sipe_strequal("Tentative", tmp
)) {
257 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
258 } else if (sipe_strequal("Busy", tmp
)) {
259 cal_event
->cal_status
= SIPE_CAL_BUSY
;
260 } else if (sipe_strequal("OOF", tmp
)) {
261 cal_event
->cal_status
= SIPE_CAL_OOF
;
263 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
267 cal_event
->subject
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Subject"));
268 cal_event
->location
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Location"));
270 tmp
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/IsMeeting"));
271 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
277 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
278 sipe_ews_run_state_machine(cal
);
281 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_FAILURE
;
282 sipe_ews_run_state_machine(cal
);
286 static void sipe_ews_process_oof_response(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
288 SIPE_UNUSED_PARAMETER GSList
*headers
,
292 struct sipe_calendar
*cal
= data
;
294 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_oof_response: cb started.");
298 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
300 const sipe_xml
*resp
;
301 const sipe_xml
*xn_duration
;
302 /** ref: [MS-OXWOOF] */
303 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
304 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
305 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
306 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
307 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
308 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
310 resp
= sipe_xml_child(xml
, "Body/GetUserOofSettingsResponse");
311 if (!resp
) return; /* rather soap:Fault */
312 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
313 return; /* Error response */
316 g_free(cal
->oof_state
);
317 cal
->oof_state
= sipe_xml_data(sipe_xml_child(resp
, "OofSettings/OofState"));
319 old_note
= cal
->oof_note
;
320 cal
->oof_note
= NULL
;
321 if (!sipe_strequal(cal
->oof_state
, "Disabled")) {
322 char *tmp
= sipe_xml_data(
323 sipe_xml_child(resp
, "OofSettings/InternalReply/Message"));
326 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
327 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
328 html
= g_strdup(tmp
+3);
330 html
= g_strdup(tmp
);
333 tmp
= g_strstrip(sipe_backend_markup_strip_html(html
));
335 cal
->oof_note
= g_markup_escape_text(tmp
, -1);
339 if (sipe_strequal(cal
->oof_state
, "Scheduled")
340 && (xn_duration
= sipe_xml_child(resp
, "OofSettings/Duration")))
342 char *tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "StartTime"));
343 cal
->oof_start
= sipe_utils_str_to_time(tmp
);
346 tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "EndTime"));
347 cal
->oof_end
= sipe_utils_str_to_time(tmp
);
351 if (!sipe_strequal(old_note
, cal
->oof_note
)) { /* oof note changed */
352 cal
->updated
= time(NULL
);
353 cal
->published
= FALSE
;
359 cal
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
360 sipe_ews_run_state_machine(cal
);
363 cal
->state
= SIPE_EWS_STATE_OOF_FAILURE
;
364 sipe_ews_run_state_machine(cal
);
368 static void sipe_ews_send_http_request(struct sipe_calendar
*cal
)
371 sipe_core_email_authentication(cal
->sipe_private
,
373 sipe_http_request_allow_redirect(cal
->request
);
374 sipe_http_request_ready(cal
->request
);
378 static void sipe_ews_do_avail_request(struct sipe_calendar
*cal
)
383 time_t now
= time(NULL
);
388 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_avail_request: going Availability req.");
390 now_tm
= gmtime(&now
);
391 /* start -1 day, 00:00:00 */
395 cal
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
396 cal
->fb_start
-= 24*60*60;
397 /* end = start + 4 days - 1 sec */
398 end
= cal
->fb_start
+ SIPE_FREE_BUSY_PERIOD_SEC
- 1;
400 start_str
= sipe_utils_time_to_str(cal
->fb_start
);
401 end_str
= sipe_utils_time_to_str(end
);
403 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, cal
->email
, start_str
, end_str
);
404 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
408 "text/xml; charset=UTF-8",
409 sipe_ews_process_avail_response
,
415 sipe_ews_send_http_request(cal
);
419 static void sipe_ews_do_oof_request(struct sipe_calendar
*cal
)
424 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_oof_request: going OOF req.");
426 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, cal
->email
);
427 cal
->request
= sipe_http_request_post(cal
->sipe_private
,
431 "text/xml; charset=UTF-8",
432 sipe_ews_process_oof_response
,
436 sipe_ews_send_http_request(cal
);
441 sipe_ews_run_state_machine(struct sipe_calendar
*cal
)
443 switch (cal
->state
) {
444 case SIPE_EWS_STATE_AVAILABILITY_FAILURE
:
445 case SIPE_EWS_STATE_OOF_FAILURE
:
446 cal
->is_ews_disabled
= TRUE
;
448 case SIPE_EWS_STATE_IDLE
:
449 sipe_ews_do_avail_request(cal
);
451 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
452 sipe_ews_do_oof_request(cal
);
454 case SIPE_EWS_STATE_OOF_SUCCESS
:
456 struct sipe_core_private
*sipe_private
= cal
->sipe_private
;
458 cal
->state
= SIPE_EWS_STATE_IDLE
;
459 cal
->is_updated
= TRUE
;
460 sipe_cal_presence_publish(sipe_private
, TRUE
);
466 static void sipe_calendar_ews_autodiscover_cb(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
467 const struct sipe_ews_autodiscover_data
*ews_data
,
468 gpointer callback_data
)
470 struct sipe_calendar
*cal
= callback_data
;
473 cal
->as_url
= g_strdup(ews_data
->as_url
);
474 cal
->legacy_dn
= g_strdup(ews_data
->legacy_dn
);
475 cal
->oab_url
= g_strdup(ews_data
->oab_url
);
476 cal
->oof_url
= g_strdup(ews_data
->oof_url
);
477 cal
->state
= SIPE_EWS_STATE_IDLE
;
478 sipe_ews_run_state_machine(cal
);
480 SIPE_DEBUG_INFO_NOFORMAT("sipe_calendar_ews_autodiscover_cb: EWS disabled");
481 cal
->is_ews_disabled
= TRUE
;
485 void sipe_ews_update_calendar(struct sipe_core_private
*sipe_private
)
487 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
488 struct sipe_calendar
*cal
;
490 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: started.");
492 sipe_cal_calendar_init(sipe_private
);
493 cal
= sipe_private
->calendar
;
495 if (cal
->is_ews_disabled
) {
496 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: disabled, exiting.");
497 } else if (!cal
->as_url
&& !cal
->ews_autodiscover_triggered
) {
498 cal
->ews_autodiscover_triggered
= TRUE
;
499 sipe_ews_autodiscover_start(sipe_private
,
500 sipe_calendar_ews_autodiscover_cb
,
503 sipe_ews_run_state_machine(cal
);
504 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: finished.");