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.
58 #include "sipe-utils.h"
60 #include "http-conn.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_AUTODISCOVER_SUCCESS 1
151 #define SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE -1
152 #define SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE -2
153 #define SIPE_EWS_STATE_AVAILABILITY_SUCCESS 2
154 #define SIPE_EWS_STATE_OOF_SUCCESS 3
158 sipe_ews_cal_events_free(GSList
*cal_events
)
160 GSList
*entry
= cal_events
;
162 if (!cal_events
) return;
165 struct sipe_cal_event
*cal_event
= entry
->data
;
166 sipe_cal_event_free(cal_event
);
170 g_slist_free(cal_events
);
174 sipe_ews_free(struct sipe_ews
* ews
)
177 g_free(ews
->legacy_dn
);
179 g_free(ews
->auth
->domain
);
180 g_free(ews
->auth
->user
);
181 g_free(ews
->auth
->password
);
185 g_free(ews
->oof_url
);
186 g_free(ews
->oab_url
);
187 g_free(ews
->oof_state
);
188 g_free(ews
->oof_note
);
189 g_free(ews
->free_busy
);
190 g_free(ews
->working_hours_xml_str
);
192 sipe_ews_cal_events_free(ews
->cal_events
);
198 sipe_ews_get_oof_note(struct sipe_ews
*ews
)
200 time_t now
= time(NULL
);
202 if (!ews
|| !ews
->oof_state
) return NULL
;
204 if (sipe_strequal(ews
->oof_state
, "Enabled") ||
205 (sipe_strequal(ews
->oof_state
, "Scheduled") && now
>= ews
->oof_start
&& now
<= ews
->oof_end
))
207 return ews
->oof_note
;
216 sipe_ews_run_state_machine(struct sipe_ews
*ews
);
219 sipe_ews_process_avail_response(int return_code
,
224 struct sipe_ews
*ews
= data
;
226 purple_debug_info("sipe", "sipe_ews_process_avail_response: cb started.\n");
228 if(!sipe_strequal(ews
->as_url
, ews
->oof_url
)) { /* whether reuse conn */
229 http_conn_set_close(conn
);
230 ews
->http_conn
= NULL
;
233 if (return_code
== 200 && body
) {
234 const sipe_xml
*node
;
235 const sipe_xml
*resp
;
236 /** ref: [MS-OXWAVLS] */
237 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
239 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/ResponseMessage@ResponseClass="Success"
240 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/MergedFreeBusy
241 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/CalendarEventArray/CalendarEvent
242 Envelope/Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse/FreeBusyView/WorkingHours
244 resp
= sipe_xml_child(xml
, "Body/GetUserAvailabilityResponse/FreeBusyResponseArray/FreeBusyResponse");
245 if (!resp
) return; /* rather soap:Fault */
246 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
247 return; /* Error response */
251 g_free(ews
->free_busy
);
252 ews
->free_busy
= sipe_xml_data(sipe_xml_child(resp
, "FreeBusyView/MergedFreeBusy"));
255 node
= sipe_xml_child(resp
, "FreeBusyView/WorkingHours");
256 g_free(ews
->working_hours_xml_str
);
257 ews
->working_hours_xml_str
= sipe_xml_stringify(node
);
258 purple_debug_info("sipe", "sipe_ews_process_avail_response: ews->working_hours_xml_str:\n%s\n",
259 ews
->working_hours_xml_str
? ews
->working_hours_xml_str
: "");
261 sipe_ews_cal_events_free(ews
->cal_events
);
262 ews
->cal_events
= NULL
;
264 for (node
= sipe_xml_child(resp
, "FreeBusyView/CalendarEventArray/CalendarEvent");
266 node
= sipe_xml_twin(node
))
271 <StartTime>2009-12-07T13:30:00</StartTime>
272 <EndTime>2009-12-07T14:30:00</EndTime>
273 <BusyType>Busy</BusyType>
274 <CalendarEventDetails>
276 <Subject>Lunch</Subject>
277 <Location>Cafe</Location>
278 <IsMeeting>false</IsMeeting>
279 <IsRecurring>true</IsRecurring>
280 <IsException>false</IsException>
281 <IsReminderSet>true</IsReminderSet>
282 <IsPrivate>false</IsPrivate>
283 </CalendarEventDetails>
286 struct sipe_cal_event
*cal_event
= g_new0(struct sipe_cal_event
, 1);
287 ews
->cal_events
= g_slist_append(ews
->cal_events
, cal_event
);
289 tmp
= sipe_xml_data(sipe_xml_child(node
, "StartTime"));
290 cal_event
->start_time
= sipe_utils_str_to_time(tmp
);
293 tmp
= sipe_xml_data(sipe_xml_child(node
, "EndTime"));
294 cal_event
->end_time
= sipe_utils_str_to_time(tmp
);
297 tmp
= sipe_xml_data(sipe_xml_child(node
, "BusyType"));
298 if (sipe_strequal("Free", tmp
)) {
299 cal_event
->cal_status
= SIPE_CAL_FREE
;
300 } else if (sipe_strequal("Tentative", tmp
)) {
301 cal_event
->cal_status
= SIPE_CAL_TENTATIVE
;
302 } else if (sipe_strequal("Busy", tmp
)) {
303 cal_event
->cal_status
= SIPE_CAL_BUSY
;
304 } else if (sipe_strequal("OOF", tmp
)) {
305 cal_event
->cal_status
= SIPE_CAL_OOF
;
307 cal_event
->cal_status
= SIPE_CAL_NO_DATA
;
311 cal_event
->subject
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Subject"));
312 cal_event
->location
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/Location"));
314 tmp
= sipe_xml_data(sipe_xml_child(node
, "CalendarEventDetails/IsMeeting"));
315 cal_event
->is_meeting
= tmp
? sipe_strequal(tmp
, "true") : TRUE
;
321 ews
->state
= SIPE_EWS_STATE_AVAILABILITY_SUCCESS
;
322 sipe_ews_run_state_machine(ews
);
324 } else if (return_code
< 0) {
325 ews
->http_conn
= NULL
;
330 sipe_ews_process_oof_response(int return_code
,
335 struct sipe_ews
*ews
= data
;
337 purple_debug_info("sipe", "sipe_ews_process_oof_response: cb started.\n");
339 http_conn_set_close(conn
);
340 ews
->http_conn
= NULL
;
342 if (return_code
== 200 && body
) {
344 const sipe_xml
*resp
;
345 const sipe_xml
*xn_duration
;
346 /** ref: [MS-OXWOOF] */
347 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
348 /* Envelope/Body/GetUserOofSettingsResponse/ResponseMessage@ResponseClass="Success"
349 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/OofState=Enabled
350 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/StartTime
351 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/Duration/EndTime
352 * Envelope/Body/GetUserOofSettingsResponse/OofSettings/InternalReply/Message
354 resp
= sipe_xml_child(xml
, "Body/GetUserOofSettingsResponse");
355 if (!resp
) return; /* rather soap:Fault */
356 if (!sipe_strequal(sipe_xml_attribute(sipe_xml_child(resp
, "ResponseMessage"), "ResponseClass"), "Success")) {
357 return; /* Error response */
360 g_free(ews
->oof_state
);
361 ews
->oof_state
= sipe_xml_data(sipe_xml_child(resp
, "OofSettings/OofState"));
363 old_note
= ews
->oof_note
;
364 ews
->oof_note
= NULL
;
365 if (!sipe_strequal(ews
->oof_state
, "Disabled")) {
366 char *tmp
= sipe_xml_data(
367 sipe_xml_child(resp
, "OofSettings/InternalReply/Message"));
370 /* UTF-8 encoded BOM (0xEF 0xBB 0xBF) as a signature to mark the beginning of a UTF-8 file */
371 if (g_str_has_prefix(tmp
, "\xEF\xBB\xBF")) {
372 html
= g_strdup(tmp
+3);
374 html
= g_strdup(tmp
);
377 tmp
= g_strstrip(purple_markup_strip_html(html
));
379 ews
->oof_note
= g_markup_escape_text(tmp
, -1);
383 if (sipe_strequal(ews
->oof_state
, "Scheduled")
384 && (xn_duration
= sipe_xml_child(resp
, "OofSettings/Duration")))
386 char *tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "StartTime"));
387 ews
->oof_start
= sipe_utils_str_to_time(tmp
);
390 tmp
= sipe_xml_data(sipe_xml_child(xn_duration
, "EndTime"));
391 ews
->oof_end
= sipe_utils_str_to_time(tmp
);
395 if (!sipe_strequal(old_note
, ews
->oof_note
)) { /* oof note changed */
396 ews
->updated
= time(NULL
);
397 ews
->published
= FALSE
;
403 ews
->state
= SIPE_EWS_STATE_OOF_SUCCESS
;
404 sipe_ews_run_state_machine(ews
);
406 } else if (return_code
< 0) {
407 ews
->http_conn
= NULL
;
412 sipe_ews_process_autodiscover(int return_code
,
417 struct sipe_ews
*ews
= data
;
419 purple_debug_info("sipe", "sipe_ews_process_autodiscover: cb started.\n");
421 http_conn_set_close(conn
);
422 ews
->http_conn
= NULL
;
424 if (return_code
== 200 && body
) {
425 const sipe_xml
*node
;
426 /** ref: [MS-OXDSCLI] */
427 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
429 /* Autodiscover/Response/User/LegacyDN (trim()) */
430 ews
->legacy_dn
= sipe_xml_data(sipe_xml_child(xml
, "Response/User/LegacyDN"));
431 ews
->legacy_dn
= ews
->legacy_dn
? g_strstrip(ews
->legacy_dn
) : NULL
;
434 for (node
= sipe_xml_child(xml
, "Response/Account/Protocol");
436 node
= sipe_xml_twin(node
))
438 char *type
= sipe_xml_data(sipe_xml_child(node
, "Type"));
439 if (sipe_strequal("EXCH", type
)) {
440 ews
->as_url
= sipe_xml_data(sipe_xml_child(node
, "ASUrl"));
441 ews
->oof_url
= sipe_xml_data(sipe_xml_child(node
, "OOFUrl"));
442 ews
->oab_url
= sipe_xml_data(sipe_xml_child(node
, "OABUrl"));
444 purple_debug_info("sipe", "sipe_ews_process_autodiscover:as_url %s\n",
445 ews
->as_url
? ews
->as_url
: "");
446 purple_debug_info("sipe", "sipe_ews_process_autodiscover:oof_url %s\n",
447 ews
->oof_url
? ews
->oof_url
: "");
448 purple_debug_info("sipe", "sipe_ews_process_autodiscover:oab_url %s\n",
449 ews
->oab_url
? ews
->oab_url
: "");
461 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
462 sipe_ews_run_state_machine(ews
);
465 if (return_code
< 0) {
466 ews
->http_conn
= NULL
;
468 switch (ews
->auto_disco_method
) {
470 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
; break;
472 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
; break;
474 sipe_ews_run_state_machine(ews
);
479 sipe_ews_do_autodiscover(struct sipe_ews
*ews
,
480 const char* autodiscover_url
)
484 purple_debug_info("sipe", "sipe_ews_do_autodiscover: going autodiscover url=%s\n", autodiscover_url
? autodiscover_url
: "");
486 body
= g_strdup_printf(SIPE_EWS_AUTODISCOVER_REQUEST
, ews
->email
);
487 ews
->http_conn
= http_conn_create(
494 sipe_ews_process_autodiscover
,
500 sipe_ews_do_avail_request(struct sipe_ews
*ews
)
505 time_t now
= time(NULL
);
510 purple_debug_info("sipe", "sipe_ews_do_avail_request: going Availability req.\n");
512 now_tm
= gmtime(&now
);
513 /* start -1 day, 00:00:00 */
517 ews
->fb_start
= sipe_mktime_tz(now_tm
, "UTC");
518 ews
->fb_start
-= 24*60*60;
519 /* end = start + 4 days - 1 sec */
520 end
= ews
->fb_start
+ 4*(24*60*60) - 1;
522 start_str
= sipe_utils_time_to_str(ews
->fb_start
);
523 end_str
= sipe_utils_time_to_str(end
);
525 body
= g_strdup_printf(SIPE_EWS_USER_AVAILABILITY_REQUEST
, ews
->email
, start_str
, end_str
);
526 ews
->http_conn
= http_conn_create(
531 "text/xml; charset=UTF-8",
533 sipe_ews_process_avail_response
,
542 sipe_ews_do_oof_request(struct sipe_ews
*ews
)
546 const char *content_type
= "text/xml; charset=UTF-8";
548 purple_debug_info("sipe", "sipe_ews_do_oof_request: going OOF req.\n");
550 body
= g_strdup_printf(SIPE_EWS_USER_OOF_SETTINGS_REQUEST
, ews
->email
);
551 if (!ews
->http_conn
) {
552 ews
->http_conn
= http_conn_create(ews
->account
,
558 sipe_ews_process_oof_response
,
561 http_conn_post(ews
->http_conn
,
565 sipe_ews_process_oof_response
,
573 sipe_ews_run_state_machine(struct sipe_ews
*ews
)
575 switch (ews
->state
) {
576 case SIPE_EWS_STATE_NONE
:
578 char *maildomain
= strstr(ews
->email
, "@") + 1;
579 char *autodisc_url
= g_strdup_printf("https://Autodiscover.%s/Autodiscover/Autodiscover.xml", maildomain
);
581 ews
->auto_disco_method
= 1;
583 sipe_ews_do_autodiscover(ews
, autodisc_url
);
585 g_free(autodisc_url
);
588 case SIPE_EWS_STATE_AUTODISCOVER_1_FAILURE
:
590 char *maildomain
= strstr(ews
->email
, "@") + 1;
591 char *autodisc_url
= g_strdup_printf("https://%s/Autodiscover/Autodiscover.xml", maildomain
);
593 ews
->auto_disco_method
= 2;
595 sipe_ews_do_autodiscover(ews
, autodisc_url
);
597 g_free(autodisc_url
);
600 case SIPE_EWS_STATE_AUTODISCOVER_2_FAILURE
:
601 ews
->is_disabled
= TRUE
;
603 case SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
:
604 sipe_ews_do_avail_request(ews
);
606 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS
:
607 sipe_ews_do_oof_request(ews
);
609 case SIPE_EWS_STATE_OOF_SUCCESS
:
610 ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
611 ews
->is_updated
= TRUE
;
612 if (ews
->sip
->ocs2007
) {
614 publish_calendar_status_self(ews
->sip
);
617 send_presence_soap(ews
->sip
, TRUE
);
624 sipe_ews_update_calendar(struct sipe_account_data
*sip
)
626 //char *autodisc_srv = g_strdup_printf("_autodiscover._tcp.%s", maildomain);
628 purple_debug_info("sipe", "sipe_ews_update_calendar: started.\n");
633 sip
->ews
= g_new0(struct sipe_ews
, 1);
636 sip
->ews
->account
= sip
->account
;
637 sip
->ews
->email
= g_strdup(sip
->email
);
639 /* user specified a service URL? */
640 value
= purple_account_get_string(sip
->account
, "email_url", NULL
);
641 if (!is_empty(value
)) {
642 sip
->ews
->as_url
= g_strdup(value
);
643 sip
->ews
->oof_url
= g_strdup(value
);
644 sip
->ews
->state
= SIPE_EWS_STATE_AUTODISCOVER_SUCCESS
;
647 sip
->ews
->auth
= g_new0(HttpConnAuth
, 1);
648 sip
->ews
->auth
->use_negotiate
= purple_account_get_bool(sip
->account
, "krb5", FALSE
);
650 /* user specified email login? */
651 value
= purple_account_get_string(sip
->account
, "email_login", NULL
);
652 if (!is_empty(value
)) {
654 /* user specified email login domain? */
655 const char *tmp
= strstr(value
, "\\");
657 sip
->ews
->auth
->domain
= g_strndup(value
, tmp
- value
);
658 sip
->ews
->auth
->user
= g_strdup(tmp
+ 1);
660 sip
->ews
->auth
->user
= g_strdup(value
);
662 sip
->ews
->auth
->password
= g_strdup(purple_account_get_string(sip
->account
, "email_password", NULL
));
665 /* re-use SIPE credentials */
666 sip
->ews
->auth
->domain
= g_strdup(sip
->authdomain
);
667 sip
->ews
->auth
->user
= g_strdup(sip
->authuser
);
668 sip
->ews
->auth
->password
= g_strdup(sip
->password
);
672 if(sip
->ews
->is_disabled
) {
673 purple_debug_info("sipe", "sipe_ews_update_calendar: disabled, exiting.\n");
677 sipe_ews_run_state_machine(sip
->ews
);
679 purple_debug_info("sipe", "sipe_ews_update_calendar: finished.\n");