6 * Copyright (C) 2010 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 "http-conn.h"
53 #include "sipe-backend.h"
54 #include "sipe-common.h"
56 #include "sipe-core.h"
58 #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
);
179 sipe_ews_process_avail_response(int return_code
,
181 SIPE_UNUSED_PARAMETER
const char *content_type
,
185 struct sipe_calendar
*cal
= data
;
187 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_avail_response: cb started.");
189 if(!sipe_strequal(cal
->as_url
, cal
->oof_url
)) { /* whether reuse conn */
190 http_conn_set_close(conn
);
191 cal
->http_conn
= NULL
;
194 if (return_code
== 200 && body
) {
195 const sipe_xml
*node
;
196 const sipe_xml
*resp
;
197 /** ref: [MS-OXWAVLS] */
198 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
200 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
201 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
202 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
203 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
205 resp
= sipe_xml_child(xml
, "Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse");
206 if (!resp
) return; /* rather soap:Fault */
207 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
208 return; /* Error response */
212 g_free(cal
->free_busy
);
213 cal
->free_busy
= sipe_xml_data(sipe_xml_child(resp
, "FreeBusyView/MergedFreeBusy"));
216 node
= sipe_xml_child(resp
, "FreeBusyView/WorkingHours");
217 g_free(cal
->working_hours_xml_str
);
218 cal
->working_hours_xml_str
= sipe_xml_stringify(node
);
219 SIPE_DEBUG_INFO("sipe_ews_process_avail_response: cal->working_hours_xml_str:\n%s",
220 cal
->working_hours_xml_str
? cal
->working_hours_xml_str
: "");
222 sipe_cal_events_free(cal
->cal_events
);
223 cal
->cal_events
= NULL
;
225 for (node
= sipe_xml_child(resp
, "FreeBusyView/CalendarEventArray/CalendarEvent");
227 node
= sipe_xml_twin(node
))
232 <StartTime>2009-12-07T13:30:00</StartTime>
233 <EndTime>2009-12-07T14:30:00</EndTime>
234 <BusyType>Busy</BusyType>
235 <CalendarEventDetails>
237 <Subject>Lunch</Subject>
238 <Location>Cafe</Location>
239 <IsMeeting>false</IsMeeting>
240 <IsRecurring>true</IsRecurring>
241 <IsException>false</IsException>
242 <IsReminderSet>true</IsReminderSet>
243 <IsPrivate>false</IsPrivate>
244 </CalendarEventDetails>
247 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
248 cal
->cal_events
= g_slist_append(cal
->cal_events
, cal_event
);
250 tmp
= sipe_xml_data(sipe_xml_child(node
, "StartTime"));
251 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
254 tmp
= sipe_xml_data(sipe_xml_child(node
, "EndTime"));
255 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
258 tmp
= sipe_xml_data(sipe_xml_child(node
, "BusyType"));
259 if (sipe_strequal("Free", tmp
)) {
260 cal_event
->cal_status
= SIPE_CAL_FREE
;
261 } else if (sipe_strequal("Tentative", tmp
)) {
262 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
263 } else if (sipe_strequal("Busy", tmp
)) {
264 cal_event
->cal_status
= SIPE_CAL_BUSY
;
265 } else if (sipe_strequal("OOF", tmp
)) {
266 cal_event
->cal_status
= SIPE_CAL_OOF
;
268 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
272 cal_event
->subject
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Subject"));
273 cal_event
->location
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Location"));
275 tmp
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/IsMeeting"));
276 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
282 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
283 sipe_ews_run_state_machine(cal
);
286 if (return_code
< 0) {
287 cal
->http_conn
= NULL
;
289 cal
->state
= SIPE_EWS_STATE_AVAILABILITY_FAILURE
;
290 sipe_ews_run_state_machine(cal
);
295 sipe_ews_process_oof_response(int return_code
,
297 SIPE_UNUSED_PARAMETER
const char *content_type
,
301 struct sipe_calendar
*cal
= data
;
303 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_oof_response: cb started.");
305 http_conn_set_close(conn
);
306 cal
->http_conn
= NULL
;
308 if (return_code
== 200 && body
) {
310 const sipe_xml
*resp
;
311 const sipe_xml
*xn_duration
;
312 /** ref: [MS-OXWOOF] */
313 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
314 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
315 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
316 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
317 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
318 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
320 resp
= sipe_xml_child(xml
, "Body/GetUserOofSettingsResponse");
321 if (!resp
) return; /* rather soap:Fault */
322 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
323 return; /* Error response */
326 g_free(cal
->oof_state
);
327 cal
->oof_state
= sipe_xml_data(sipe_xml_child(resp
, "OofSettings/OofState"));
329 old_note
= cal
->oof_note
;
330 cal
->oof_note
= NULL
;
331 if (!sipe_strequal(cal
->oof_state
, "Disabled")) {
332 char *tmp
= sipe_xml_data(
333 sipe_xml_child(resp
, "OofSettings/InternalReply/Message"));
336 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
337 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
338 html
= g_strdup(tmp
+3);
340 html
= g_strdup(tmp
);
343 tmp
= g_strstrip(sipe_backend_markup_strip_html(html
));
345 cal
->oof_note
= g_markup_escape_text(tmp
, -1);
349 if (sipe_strequal(cal
->oof_state
, "Scheduled")
350 && (xn_duration
= sipe_xml_child(resp
, "OofSettings/Duration")))
352 char *tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "StartTime"));
353 cal
->oof_start
= sipe_utils_str_to_time(tmp
);
356 tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "EndTime"));
357 cal
->oof_end
= sipe_utils_str_to_time(tmp
);
361 if (!sipe_strequal(old_note
, cal
->oof_note
)) { /* oof note changed */
362 cal
->updated
= time(NULL
);
363 cal
->published
= FALSE
;
369 cal
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
370 sipe_ews_run_state_machine(cal
);
373 if (return_code
< 0) {
374 cal
->http_conn
= NULL
;
376 cal
->state
= SIPE_EWS_STATE_OOF_FAILURE
;
377 sipe_ews_run_state_machine(cal
);
382 sipe_ews_process_autodiscover(int return_code
,
384 SIPE_UNUSED_PARAMETER
const char *content_type
,
388 struct sipe_calendar
*cal
= data
;
390 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_autodiscover: cb started.");
392 http_conn_set_close(conn
);
393 cal
->http_conn
= NULL
;
395 if (return_code
== 200 && body
) {
396 const sipe_xml
*node
;
397 /** ref: [MS-OXDSCLI] */
398 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
400 /* Autodiscover/Response/User/LegacyDN (trim()) */
401 cal
->legacy_dn
= sipe_xml_data(sipe_xml_child(xml
, "Response/User/LegacyDN"));
402 cal
->legacy_dn
= cal
->legacy_dn
? g_strstrip(cal
->legacy_dn
) : NULL
;
405 for (node
= sipe_xml_child(xml
, "Response/Account/Protocol");
407 node
= sipe_xml_twin(node
))
409 char *type
= sipe_xml_data(sipe_xml_child(node
, "Type"));
410 if (sipe_strequal("EXCH", type
)) {
411 cal
->as_url
= sipe_xml_data(sipe_xml_child(node
, "ASUrl"));
412 cal
->oof_url
= sipe_xml_data(sipe_xml_child(node
, "OOFUrl"));
413 cal
->oab_url
= sipe_xml_data(sipe_xml_child(node
, "OABUrl"));
415 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:as_url %s",
416 cal
->as_url
? cal
->as_url
: "");
417 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oof_url %s",
418 cal
->oof_url
? cal
->oof_url
: "");
419 SIPE_DEBUG_INFO("sipe_ews_process_autodiscover:oab_url %s",
420 cal
->oab_url
? cal
->oab_url
: "");
432 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
433 sipe_ews_run_state_machine(cal
);
436 if (return_code
< 0) {
437 cal
->http_conn
= NULL
;
439 switch (cal
->auto_disco_method
) {
441 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
; break;
443 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
; break;
445 sipe_ews_run_state_machine(cal
);
450 sipe_ews_do_autodiscover(struct sipe_calendar
*cal
,
451 const char* autodiscover_url
)
455 SIPE_DEBUG_INFO("sipe_ews_do_autodiscover: going autodiscover url=%s", autodiscover_url
? autodiscover_url
: "");
457 body
= g_strdup_printf(SIPE_EWS_AUTODISCOVER_REQUEST
, cal
->email
);
458 cal
->http_conn
= http_conn_create(
460 NULL
, /* HttpSession */
463 HTTP_CONN_ALLOW_REDIRECT
,
468 sipe_ews_process_autodiscover
,
474 sipe_ews_do_avail_request(struct sipe_calendar
*cal
)
479 time_t now
= time(NULL
);
484 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_avail_request: going Availability req.");
486 now_tm
= gmtime(&now
);
487 /* start -1 day, 00:00:00 */
491 cal
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
492 cal
->fb_start
-= 24*60*60;
493 /* end = start + 4 days - 1 sec */
494 end
= cal
->fb_start
+ SIPE_FREE_BUSY_PERIOD_SEC
- 1;
496 start_str
= sipe_utils_time_to_str(cal
->fb_start
);
497 end_str
= sipe_utils_time_to_str(end
);
499 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, cal
->email
, start_str
, end_str
);
500 cal
->http_conn
= http_conn_create(
502 NULL
, /* HttpSession */
505 HTTP_CONN_ALLOW_REDIRECT
,
508 "text/xml; charset=UTF-8",
510 sipe_ews_process_avail_response
,
519 sipe_ews_do_oof_request(struct sipe_calendar
*cal
)
523 const char *content_type
= "text/xml; charset=UTF-8";
525 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_do_oof_request: going OOF req.");
527 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, cal
->email
);
528 if (!cal
->http_conn
|| http_conn_is_closed(cal
->http_conn
)) {
529 cal
->http_conn
= http_conn_create(cal
->sip
->public,
530 NULL
, /* HttpSession */
533 HTTP_CONN_ALLOW_REDIRECT
,
538 sipe_ews_process_oof_response
,
541 http_conn_send(cal
->http_conn
,
546 sipe_ews_process_oof_response
,
554 sipe_ews_run_state_machine(struct sipe_calendar
*cal
)
556 switch (cal
->state
) {
557 case SIPE_EWS_STATE_NONE
:
559 char *maildomain
= strstr(cal
->email
, "@") + 1;
560 char *autodisc_url
= g_strdup_printf("https://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
562 cal
->auto_disco_method
= 1;
564 sipe_ews_do_autodiscover(cal
, autodisc_url
);
566 g_free(autodisc_url
);
569 case SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
:
571 char *maildomain
= strstr(cal
->email
, "@") + 1;
572 char *autodisc_url
= g_strdup_printf("https://%s/Autodiscover/Autodiscover.xml", maildomain
);
574 cal
->auto_disco_method
= 2;
576 sipe_ews_do_autodiscover(cal
, autodisc_url
);
578 g_free(autodisc_url
);
581 case SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
:
582 case SIPE_EWS_STATE_AVAILABILITY_FAILURE
:
583 case SIPE_EWS_STATE_OOF_FAILURE
:
584 cal
->is_ews_disabled
= TRUE
;
586 case SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
:
587 sipe_ews_do_avail_request(cal
);
589 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
590 sipe_ews_do_oof_request(cal
);
592 case SIPE_EWS_STATE_OOF_SUCCESS
:
593 cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
594 cal
->is_updated
= TRUE
;
595 if (cal
->sip
->ocs2007
) {
597 publish_calendar_status_self(cal
->sip
->private,
601 send_presence_soap(cal
->sip
, TRUE
);
608 sipe_ews_update_calendar(struct sipe_account_data
*sip
)
610 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
613 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: started.");
615 if (sipe_cal_calendar_init(sip
, &has_url
)) {
617 sip
->cal
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
621 if (sip
->cal
->is_ews_disabled
) {
622 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: disabled, exiting.");
626 sipe_ews_run_state_machine(sip
->cal
);
628 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: finished.");