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.
51 /* for xmlnode_get_descendant */
52 #include "sipe-utils.h"
59 * Autodiscover request for Exchange Web Services
60 * @param email (%s) Ex.: alice@cosmo.local
62 #define SIPE_EWS_AUTODISCOVER_REQUEST \
63 "<?xml version=\"1.0\"?>"\
64 "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\">"\
66 "<EMailAddress>%s</EMailAddress>"\
67 "<AcceptableResponseSchema>"\
68 "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"\
69 "</AcceptableResponseSchema>"\
74 * GetUserOofSettingsRequest SOAP request to Exchange Web Services
75 * to obtain our Out-of-office (OOF) information.
76 * @param email (%s) Ex.: alice@cosmo.local
78 #define SIPE_EWS_USER_OOF_SETTINGS_REQUEST \
79 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
80 "<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/\">"\
82 "<GetUserOofSettingsRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">"\
83 "<Mailbox xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
84 "<Address>%s</Address>"\
86 "</GetUserOofSettingsRequest>"\
91 * GetUserAvailabilityRequest SOAP request to Exchange Web Services
92 * to obtain our Availability (FreeBusy, WorkingHours, Meetings) information.
93 * @param email (%s) Ex.: alice@cosmo.local
94 * @param start_time (%s) Ex.: 2009-12-06T00:00:00
95 * @param end_time (%s) Ex.: 2009-12-09T23:59:59
97 #define SIPE_EWS_USER_AVAILABILITY_REQUEST \
98 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"\
99 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""\
100 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""\
101 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""\
102 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
104 "<GetUserAvailabilityRequest xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\""\
105 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
106 "<t:TimeZone xmlns=\"http://schemas.microsoft.com/exchange/services/2006/types\">"\
110 "<Time>00:00:00</Time>"\
111 "<DayOrder>0</DayOrder>"\
113 "<DayOfWeek>Sunday</DayOfWeek>"\
117 "<Time>00:00:00</Time>"\
118 "<DayOrder>0</DayOrder>"\
120 "<DayOfWeek>Sunday</DayOfWeek>"\
123 "<MailboxDataArray>"\
126 "<t:Address>%s</t:Address>"\
128 "<t:AttendeeType>Required</t:AttendeeType>"\
129 "<t:ExcludeConflicts>false</t:ExcludeConflicts>"\
131 "</MailboxDataArray>"\
132 "<t:FreeBusyViewOptions>"\
134 "<t:StartTime>%s</t:StartTime>"\
135 "<t:EndTime>%s</t:EndTime>"\
137 "<t:MergedFreeBusyIntervalInMinutes>15</t:MergedFreeBusyIntervalInMinutes>"\
138 "<t:RequestedView>DetailedMerged</t:RequestedView>"\
139 "</t:FreeBusyViewOptions>"\
140 "</GetUserAvailabilityRequest>"\
144 #define SIPE_EWS_STATE_NONE 0
145 #define SIPE_EWS_STATE_AUTODISCOVER_SUCCESS 1
146 #define SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE -1
147 #define SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE -2
148 #define SIPE_EWS_STATE_AVAILABILITY_SUCCESS 2
149 #define SIPE_EWS_STATE_OOF_SUCCESS 3
153 sipe_ews_cal_events_free(GSList
*cal_events
)
155 GSList
*entry
= cal_events
;
157 if (!cal_events
) return;
160 struct sipe_cal_event
*cal_event
= entry
->data
;
161 sipe_cal_event_free(cal_event
);
165 g_slist_free(cal_events
);
169 sipe_ews_free(struct sipe_ews
* ews
)
172 g_free(ews
->legacy_dn
);
174 g_free(ews
->auth
->domain
);
175 g_free(ews
->auth
->user
);
176 g_free(ews
->auth
->password
);
180 g_free(ews
->oof_url
);
181 g_free(ews
->oab_url
);
182 g_free(ews
->oof_state
);
183 g_free(ews
->oof_note
);
184 g_free(ews
->free_busy
);
185 g_free(ews
->working_hours_xml_str
);
187 sipe_ews_cal_events_free(ews
->cal_events
);
193 sipe_ews_get_oof_note(struct sipe_ews
*ews
)
195 time_t now
= time(NULL
);
197 if (!ews
|| !ews
->oof_state
) return NULL
;
199 if (sipe_strequal(ews
->oof_state
, "Enabled") ||
200 (sipe_strequal(ews
->oof_state
, "Scheduled") && now
>= ews
->oof_start
&& now
<= ews
->oof_end
))
202 return ews
->oof_note
;
211 sipe_ews_run_state_machine(struct sipe_ews
*ews
);
214 sipe_ews_process_avail_response(int return_code
,
219 struct sipe_ews
*ews
= data
;
221 purple_debug_info("sipe", "sipe_ews_process_avail_response: cb started.\n");
223 if(!sipe_strequal(ews
->as_url
, ews
->oof_url
)) { /* whether reuse conn */
224 http_conn_set_close(conn
);
225 ews
->http_conn
= NULL
;
228 if (return_code
== 200 && body
) {
231 /** ref: [MS-OXWAVLS] */
232 xmlnode
*xml
= xmlnode_from_str(body
, strlen(body
));
234 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
235 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
236 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
237 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
239 resp
= xmlnode_get_descendant(xml
, "Body", "GetUserAvailabilityResponse", "FreeBusyResponseArray", "FreeBusyResponse", NULL
);
240 if (!resp
) return; /* rather soap:Fault */
241 if (!sipe_strequal(xmlnode_get_attrib(xmlnode_get_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
242 return; /* Error response */
246 g_free(ews
->free_busy
);
247 ews
->free_busy
= xmlnode_get_data(xmlnode_get_descendant(resp
, "FreeBusyView", "MergedFreeBusy", NULL
));
250 node
= xmlnode_get_descendant(resp
, "FreeBusyView", "WorkingHours", NULL
);
251 g_free(ews
->working_hours_xml_str
);
252 ews
->working_hours_xml_str
= xmlnode_to_str(node
, NULL
);
253 purple_debug_info("sipe", "sipe_ews_process_avail_response: ews->working_hours_xml_str:\n%s\n",
254 ews
->working_hours_xml_str
? ews
->working_hours_xml_str
: "");
256 sipe_ews_cal_events_free(ews
->cal_events
);
257 ews
->cal_events
= NULL
;
259 for (node
= xmlnode_get_descendant(resp
, "FreeBusyView", "CalendarEventArray", "CalendarEvent", NULL
);
261 node
= xmlnode_get_next_twin(node
))
266 <StartTime>2009-12-07T13:30:00</StartTime>
267 <EndTime>2009-12-07T14:30:00</EndTime>
268 <BusyType>Busy</BusyType>
269 <CalendarEventDetails>
271 <Subject>Lunch</Subject>
272 <Location>Cafe</Location>
273 <IsMeeting>false</IsMeeting>
274 <IsRecurring>true</IsRecurring>
275 <IsException>false</IsException>
276 <IsReminderSet>true</IsReminderSet>
277 <IsPrivate>false</IsPrivate>
278 </CalendarEventDetails>
281 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
282 ews
->cal_events
= g_slist_append(ews
->cal_events
, cal_event
);
284 tmp
= xmlnode_get_data(xmlnode_get_child(node
, "StartTime"));
285 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
288 tmp
= xmlnode_get_data(xmlnode_get_child(node
, "EndTime"));
289 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
292 tmp
= xmlnode_get_data(xmlnode_get_child(node
, "BusyType"));
293 if (sipe_strequal("Free", tmp
)) {
294 cal_event
->cal_status
= SIPE_CAL_FREE
;
295 } else if (sipe_strequal("Tentative", tmp
)) {
296 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
297 } else if (sipe_strequal("Busy", tmp
)) {
298 cal_event
->cal_status
= SIPE_CAL_BUSY
;
299 } else if (sipe_strequal("OOF", tmp
)) {
300 cal_event
->cal_status
= SIPE_CAL_OOF
;
302 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
306 cal_event
->subject
= xmlnode_get_data(xmlnode_get_descendant(node
, "CalendarEventDetails", "Subject", NULL
));
307 cal_event
->location
= xmlnode_get_data(xmlnode_get_descendant(node
, "CalendarEventDetails", "Location", NULL
));
309 tmp
= xmlnode_get_data(xmlnode_get_descendant(node
, "CalendarEventDetails", "IsMeeting", NULL
));
310 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
316 ews
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
317 sipe_ews_run_state_machine(ews
);
319 } else if (return_code
< 0) {
320 ews
->http_conn
= NULL
;
325 sipe_ews_process_oof_response(int return_code
,
330 struct sipe_ews
*ews
= data
;
332 purple_debug_info("sipe", "sipe_ews_process_oof_response: cb started.\n");
334 http_conn_set_close(conn
);
335 ews
->http_conn
= NULL
;
337 if (return_code
== 200 && body
) {
340 xmlnode
*xn_duration
;
341 /** ref: [MS-OXWOOF] */
342 xmlnode
*xml
= xmlnode_from_str(body
, strlen(body
));
343 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
344 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
345 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
346 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
347 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
349 resp
= xmlnode_get_descendant(xml
, "Body", "GetUserOofSettingsResponse", NULL
);
350 if (!resp
) return; /* rather soap:Fault */
351 if (!sipe_strequal(xmlnode_get_attrib(xmlnode_get_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
352 return; /* Error response */
355 g_free(ews
->oof_state
);
356 ews
->oof_state
= xmlnode_get_data(xmlnode_get_descendant(resp
, "OofSettings", "OofState", NULL
));
358 old_note
= ews
->oof_note
;
359 ews
->oof_note
= NULL
;
360 if (!sipe_strequal(ews
->oof_state
, "Disabled")) {
361 char *tmp
= xmlnode_get_data(
362 xmlnode_get_descendant(resp
, "OofSettings", "InternalReply", "Message", NULL
));
365 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
366 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
367 html
= g_strdup(tmp
+3);
369 html
= g_strdup(tmp
);
372 tmp
= g_strstrip(purple_markup_strip_html(html
));
374 ews
->oof_note
= g_markup_escape_text(tmp
, -1);
378 if (sipe_strequal(ews
->oof_state
, "Scheduled")
379 && (xn_duration
= xmlnode_get_descendant(resp
, "OofSettings", "Duration", NULL
)))
381 char *tmp
= xmlnode_get_data(xmlnode_get_child(xn_duration
, "StartTime"));
382 ews
->oof_start
= sipe_utils_str_to_time(tmp
);
385 tmp
= xmlnode_get_data(xmlnode_get_child(xn_duration
, "EndTime"));
386 ews
->oof_end
= sipe_utils_str_to_time(tmp
);
390 if (!sipe_strequal(old_note
, ews
->oof_note
)) { /* oof note changed */
391 ews
->updated
= time(NULL
);
392 ews
->published
= FALSE
;
398 ews
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
399 sipe_ews_run_state_machine(ews
);
401 } else if (return_code
< 0) {
402 ews
->http_conn
= NULL
;
407 sipe_ews_process_autodiscover(int return_code
,
412 struct sipe_ews
*ews
= data
;
414 purple_debug_info("sipe", "sipe_ews_process_autodiscover: cb started.\n");
416 http_conn_set_close(conn
);
417 ews
->http_conn
= NULL
;
419 if (return_code
== 200 && body
) {
421 /** ref: [MS-OXDSCLI] */
422 xmlnode
*xml
= xmlnode_from_str(body
, strlen(body
));
424 /* Autodiscover/Response/User/LegacyDN (trim()) */
425 ews
->legacy_dn
= xmlnode_get_data(xmlnode_get_descendant(xml
, "Response", "User", "LegacyDN", NULL
));
426 ews
->legacy_dn
= ews
->legacy_dn
? g_strstrip(ews
->legacy_dn
) : NULL
;
429 for (node
= xmlnode_get_descendant(xml
, "Response", "Account", "Protocol", NULL
);
431 node
= xmlnode_get_next_twin(node
))
433 char *type
= xmlnode_get_data(xmlnode_get_child(node
, "Type"));
434 if (sipe_strequal("EXCH", type
)) {
435 ews
->as_url
= xmlnode_get_data(xmlnode_get_child(node
, "ASUrl"));
436 ews
->oof_url
= xmlnode_get_data(xmlnode_get_child(node
, "OOFUrl"));
437 ews
->oab_url
= xmlnode_get_data(xmlnode_get_child(node
, "OABUrl"));
439 purple_debug_info("sipe", "sipe_ews_process_autodiscover:as_url %s\n",
440 ews
->as_url
? ews
->as_url
: "");
441 purple_debug_info("sipe", "sipe_ews_process_autodiscover:oof_url %s\n",
442 ews
->oof_url
? ews
->oof_url
: "");
443 purple_debug_info("sipe", "sipe_ews_process_autodiscover:oab_url %s\n",
444 ews
->oab_url
? ews
->oab_url
: "");
456 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
457 sipe_ews_run_state_machine(ews
);
460 if (return_code
< 0) {
461 ews
->http_conn
= NULL
;
463 switch (ews
->auto_disco_method
) {
465 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
; break;
467 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
; break;
469 sipe_ews_run_state_machine(ews
);
474 sipe_ews_do_autodiscover(struct sipe_ews
*ews
,
475 const char* autodiscover_url
)
479 purple_debug_info("sipe", "sipe_ews_do_autodiscover: going autodiscover url=%s\n", autodiscover_url
? autodiscover_url
: "");
481 body
= g_strdup_printf(SIPE_EWS_AUTODISCOVER_REQUEST
, ews
->email
);
482 ews
->http_conn
= http_conn_create(
489 sipe_ews_process_autodiscover
,
495 sipe_ews_do_avail_request(struct sipe_ews
*ews
)
500 time_t now
= time(NULL
);
505 purple_debug_info("sipe", "sipe_ews_do_avail_request: going Availability req.\n");
507 now_tm
= gmtime(&now
);
508 /* start -1 day, 00:00:00 */
512 ews
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
513 ews
->fb_start
-= 24*60*60;
514 /* end = start + 4 days - 1 sec */
515 end
= ews
->fb_start
+ 4*(24*60*60) - 1;
517 start_str
= sipe_utils_time_to_str(ews
->fb_start
);
518 end_str
= sipe_utils_time_to_str(end
);
520 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, ews
->email
, start_str
, end_str
);
521 ews
->http_conn
= http_conn_create(
526 "text/xml; charset=UTF-8",
528 sipe_ews_process_avail_response
,
537 sipe_ews_do_oof_request(struct sipe_ews
*ews
)
541 const char *content_type
= "text/xml; charset=UTF-8";
543 purple_debug_info("sipe", "sipe_ews_do_oof_request: going OOF req.\n");
545 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, ews
->email
);
546 if (!ews
->http_conn
) {
547 ews
->http_conn
= http_conn_create(ews
->account
,
553 sipe_ews_process_oof_response
,
556 http_conn_post(ews
->http_conn
,
560 sipe_ews_process_oof_response
,
568 sipe_ews_run_state_machine(struct sipe_ews
*ews
)
570 switch (ews
->state
) {
571 case SIPE_EWS_STATE_NONE
:
573 char *maildomain
= strstr(ews
->email
, "@") + 1;
574 char *autodisc_url
= g_strdup_printf("https://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
576 ews
->auto_disco_method
= 1;
578 sipe_ews_do_autodiscover(ews
, autodisc_url
);
580 g_free(autodisc_url
);
583 case SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
:
585 char *maildomain
= strstr(ews
->email
, "@") + 1;
586 char *autodisc_url
= g_strdup_printf("https://%s/Autodiscover/Autodiscover.xml", maildomain
);
588 ews
->auto_disco_method
= 2;
590 sipe_ews_do_autodiscover(ews
, autodisc_url
);
592 g_free(autodisc_url
);
595 case SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
:
596 ews
->is_disabled
= TRUE
;
598 case SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
:
599 sipe_ews_do_avail_request(ews
);
601 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
602 sipe_ews_do_oof_request(ews
);
604 case SIPE_EWS_STATE_OOF_SUCCESS
:
605 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
606 ews
->is_updated
= TRUE
;
607 if (ews
->sip
->ocs2007
) {
609 publish_calendar_status_self(ews
->sip
);
612 send_presence_soap(ews
->sip
, TRUE
);
619 sipe_ews_update_calendar(struct sipe_account_data
*sip
)
621 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
623 purple_debug_info("sipe", "sipe_ews_update_calendar: started.\n");
628 sip
->ews
= g_new0(struct sipe_ews
, 1);
631 sip
->ews
->account
= sip
->account
;
632 sip
->ews
->email
= g_strdup(sip
->email
);
634 /* user specified a service URL? */
635 value
= purple_account_get_string(sip
->account
, "email_url", NULL
);
636 if (!is_empty(value
)) {
637 sip
->ews
->as_url
= g_strdup(value
);
638 sip
->ews
->oof_url
= g_strdup(value
);
639 sip
->ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
642 sip
->ews
->auth
= g_new0(HttpConnAuth
, 1);
643 sip
->ews
->auth
->use_negotiate
= purple_account_get_bool(sip
->account
, "krb5", FALSE
);
645 /* user specified email login? */
646 value
= purple_account_get_string(sip
->account
, "email_login", NULL
);
647 if (!is_empty(value
)) {
649 /* user specified email login domain? */
650 const char *tmp
= strstr(value
, "\\");
652 sip
->ews
->auth
->domain
= g_strndup(value
, tmp
- value
);
653 sip
->ews
->auth
->user
= g_strdup(tmp
+ 1);
655 sip
->ews
->auth
->user
= g_strdup(value
);
657 sip
->ews
->auth
->password
= g_strdup(purple_account_get_string(sip
->account
, "email_password", NULL
));
660 /* re-use SIPE credentials */
661 sip
->ews
->auth
->domain
= g_strdup(sip
->authdomain
);
662 sip
->ews
->auth
->user
= g_strdup(sip
->authuser
);
663 sip
->ews
->auth
->password
= g_strdup(sip
->password
);
667 if(sip
->ews
->is_disabled
) {
668 purple_debug_info("sipe", "sipe_ews_update_calendar: disabled, exiting.\n");
672 sipe_ews_run_state_machine(sip
->ews
);
674 purple_debug_info("sipe", "sipe_ews_update_calendar: finished.\n");