buddy: add interface to iterate over buddies
[siplcs.git] / src / core / sipe-ews.c
blobbc6b8c6ca5c25549d5b9dda0af6285ef325d51dd
1 /**
2 * @file sipe-ews.c
4 * pidgin-sipe
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
25 /**
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.
47 #include <string.h>
48 #include <time.h>
50 #include <glib.h>
52 #include "sipe-backend.h"
53 #include "sipe-common.h"
54 #include "sipe-cal.h"
55 #include "sipe-core.h"
56 #include "sipe-core-private.h"
57 #include "sipe-ews.h"
58 #include "sipe-ews-autodiscover.h"
59 #include "sipe-http.h"
60 #include "sipe-utils.h"
61 #include "sipe-xml.h"
63 /**
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\">"\
70 "<Request>"\
71 "<EMailAddress>%s</EMailAddress>"\
72 "<AcceptableResponseSchema>"\
73 "http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"\
74 "</AcceptableResponseSchema>"\
75 "</Request>"\
76 "</Autodiscover>"
78 /**
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/\">"\
86 "<soap:Body>"\
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>"\
90 "</Mailbox>"\
91 "</GetUserOofSettingsRequest>"\
92 "</soap:Body>"\
93 "</soap:Envelope>"
95 /**
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\">"\
108 "<soap:Body>"\
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\">"\
112 "<Bias>0</Bias>"\
113 "<StandardTime>"\
114 "<Bias>0</Bias>"\
115 "<Time>00:00:00</Time>"\
116 "<DayOrder>0</DayOrder>"\
117 "<Month>0</Month>"\
118 "<DayOfWeek>Sunday</DayOfWeek>"\
119 "</StandardTime>"\
120 "<DaylightTime>"\
121 "<Bias>0</Bias>"\
122 "<Time>00:00:00</Time>"\
123 "<DayOrder>0</DayOrder>"\
124 "<Month>0</Month>"\
125 "<DayOfWeek>Sunday</DayOfWeek>"\
126 "</DaylightTime>"\
127 "</t:TimeZone>"\
128 "<MailboxDataArray>"\
129 "<t:MailboxData>"\
130 "<t:Email>"\
131 "<t:Address>%s</t:Address>"\
132 "</t:Email>"\
133 "<t:AttendeeType>Required</t:AttendeeType>"\
134 "<t:ExcludeConflicts>false</t:ExcludeConflicts>"\
135 "</t:MailboxData>"\
136 "</MailboxDataArray>"\
137 "<t:FreeBusyViewOptions>"\
138 "<t:TimeWindow>"\
139 "<t:StartTime>%s</t:StartTime>"\
140 "<t:EndTime>%s</t:EndTime>"\
141 "</t:TimeWindow>"\
142 "<t:MergedFreeBusyIntervalInMinutes>15</t:MergedFreeBusyIntervalInMinutes>"\
143 "<t:RequestedView>DetailedMerged</t:RequestedView>"\
144 "</t:FreeBusyViewOptions>"\
145 "</GetUserAvailabilityRequest>"\
146 "</soap:Body>"\
147 "</soap:Envelope>"
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
156 char *
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;
168 else
170 return NULL;
174 static void
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,
178 guint status,
179 SIPE_UNUSED_PARAMETER GSList *headers,
180 const gchar *body,
181 gpointer data)
183 struct sipe_calendar *cal = data;
185 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_avail_response: cb started.");
187 cal->request = NULL;
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 */
206 /* MergedFreeBusy */
207 g_free(cal->free_busy);
208 cal->free_busy = sipe_xml_data(sipe_xml_child(resp, "FreeBusyView/MergedFreeBusy"));
210 /* WorkingHours */
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;
219 /* CalendarEvents */
220 for (node = sipe_xml_child(resp, "FreeBusyView/CalendarEventArray/CalendarEvent");
221 node;
222 node = sipe_xml_twin(node))
224 char *tmp;
226 <CalendarEvent>
227 <StartTime>2009-12-07T13:30:00</StartTime>
228 <EndTime>2009-12-07T14:30:00</EndTime>
229 <BusyType>Busy</BusyType>
230 <CalendarEventDetails>
231 <ID>0000000...</ID>
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>
240 </CalendarEvent>
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);
247 g_free(tmp);
249 tmp = sipe_xml_data(sipe_xml_child(node, "EndTime"));
250 cal_event->end_time = sipe_utils_str_to_time(tmp);
251 g_free(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;
262 } else {
263 cal_event->cal_status = SIPE_CAL_NO_DATA;
265 g_free(tmp);
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;
272 g_free(tmp);
275 sipe_xml_free(xml);
277 cal->state = SIPE_EWS_STATE_AVAILABILITY_SUCCESS;
278 sipe_ews_run_state_machine(cal);
280 } else {
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,
287 guint status,
288 SIPE_UNUSED_PARAMETER GSList *headers,
289 const gchar *body,
290 gpointer data)
292 struct sipe_calendar *cal = data;
294 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_process_oof_response: cb started.");
296 cal->request = NULL;
298 if ((status == SIPE_HTTP_STATUS_OK) && body) {
299 char *old_note;
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"));
324 char *html;
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);
329 } else {
330 html = g_strdup(tmp);
332 g_free(tmp);
333 tmp = g_strstrip(sipe_backend_markup_strip_html(html));
334 g_free(html);
335 cal->oof_note = g_markup_escape_text(tmp, -1);
336 g_free(tmp);
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);
344 g_free(tmp);
346 tmp = sipe_xml_data(sipe_xml_child(xn_duration, "EndTime"));
347 cal->oof_end = sipe_utils_str_to_time(tmp);
348 g_free(tmp);
351 if (!sipe_strequal(old_note, cal->oof_note)) { /* oof note changed */
352 cal->updated = time(NULL);
353 cal->published = FALSE;
355 g_free(old_note);
357 sipe_xml_free(xml);
359 cal->state = SIPE_EWS_STATE_OOF_SUCCESS;
360 sipe_ews_run_state_machine(cal);
362 } else {
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)
370 if (cal->request) {
371 sipe_core_email_authentication(cal->sipe_private,
372 cal->request);
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)
380 if (cal->as_url) {
381 char *body;
382 time_t end;
383 time_t now = time(NULL);
384 char *start_str;
385 char *end_str;
386 struct tm *now_tm;
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 */
392 now_tm->tm_sec = 0;
393 now_tm->tm_min = 0;
394 now_tm->tm_hour = 0;
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,
405 cal->as_url,
406 NULL,
407 body,
408 "text/xml; charset=UTF-8",
409 sipe_ews_process_avail_response,
410 cal);
411 g_free(body);
412 g_free(start_str);
413 g_free(end_str);
415 sipe_ews_send_http_request(cal);
419 static void sipe_ews_do_oof_request(struct sipe_calendar *cal)
421 if (cal->oof_url) {
422 char *body;
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,
428 cal->as_url,
429 NULL,
430 body,
431 "text/xml; charset=UTF-8",
432 sipe_ews_process_oof_response,
433 cal);
434 g_free(body);
436 sipe_ews_send_http_request(cal);
440 static void
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;
447 break;
448 case SIPE_EWS_STATE_IDLE:
449 sipe_ews_do_avail_request(cal);
450 break;
451 case SIPE_EWS_STATE_AVAILABILITY_SUCCESS:
452 sipe_ews_do_oof_request(cal);
453 break;
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);
462 break;
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;
472 if (ews_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);
479 } else {
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,
501 cal);
502 } else {
503 sipe_ews_run_state_machine(cal);
504 SIPE_DEBUG_INFO_NOFORMAT("sipe_ews_update_calendar: finished.");
509 Local Variables:
510 mode: c
511 c-file-style: "bsd"
512 indent-tabs-mode: t
513 tab-width: 8
514 End: