purple: remove purple >= 2.7.0 flagging
[siplcs.git] / src / core / sipe-cal.c
blobdf8eb3adb6694d2993f266b9c4e670acac30cbd9
1 /**
2 * @file sipe-cal.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2016 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 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 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 #include <glib.h>
35 #include "sipe-backend.h"
36 #include "sipe-buddy.h"
37 #include "sipe-core.h"
38 #include "sipe-core-private.h"
39 #include "sipe-cal.h"
40 #include "sipe-http.h"
41 #include "sipe-nls.h"
42 #include "sipe-ocs2005.h"
43 #include "sipe-ocs2007.h"
44 #include "sipe-schedule.h"
45 #include "sipe-utils.h"
46 #include "sipe-xml.h"
48 /* Calendar backends */
49 #ifdef _WIN32
50 #include "sipe-domino.h"
51 #endif
52 #include "sipe-ews.h"
54 #define TIME_NULL (time_t)-1
55 #define IS(time) (time != TIME_NULL)
58 http://msdn.microsoft.com/en-us/library/aa565001.aspx
60 <?xml version="1.0"?>
61 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
62 <TimeZone>
63 <Bias>480</Bias>
64 <StandardTime>
65 <Bias>0</Bias>
66 <Time>02:00:00</Time>
67 <DayOrder>1</DayOrder>
68 <Month>11</Month>
69 <DayOfWeek>Sunday</DayOfWeek>
70 </StandardTime>
71 <DaylightTime>
72 <Bias>-60</Bias>
73 <Time>02:00:00</Time>
74 <DayOrder>2</DayOrder>
75 <Month>3</Month>
76 <DayOfWeek>Sunday</DayOfWeek>
77 </DaylightTime>
78 </TimeZone>
79 <WorkingPeriodArray>
80 <WorkingPeriod>
81 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
82 <StartTimeInMinutes>600</StartTimeInMinutes>
83 <EndTimeInMinutes>1140</EndTimeInMinutes>
84 </WorkingPeriod>
85 </WorkingPeriodArray>
86 </WorkingHours>
88 Desc:
89 <StandardTime>
90 <Bias>int</Bias>
91 <Time>string</Time>
92 <DayOrder>short</DayOrder>
93 <Month>short</Month>
94 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
95 <Year>string</Year>
96 </StandardTime>
99 struct sipe_cal_std_dst {
100 int bias; /* Ex.: -60 */
101 gchar *time; /* hh:mm:ss, 02:00:00 */
102 int day_order; /* 1..5 */
103 int month; /* 1..12 */
104 gchar *day_of_week; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
105 gchar *year; /* YYYY */
107 time_t switch_time;
110 struct sipe_cal_working_hours {
111 int bias; /* Ex.: 480 */
112 struct sipe_cal_std_dst std; /* StandardTime */
113 struct sipe_cal_std_dst dst; /* DaylightTime */
114 gchar *days_of_week; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
115 int start_time; /* 0...1440 */
116 int end_time; /* 0...1440 */
118 gchar *tz; /* aggregated timezone string as in TZ environment variable.
119 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
120 /** separate simple strings for Windows platform as the proper TZ does not work there.
121 * anyway, dynamic timezones would't work with just TZ
123 gchar *tz_std; /* Ex.: TST8 */
124 gchar *tz_dst; /* Ex.: TDT7 */
127 /* not for translation, a part of XML Schema definitions */
128 static const char *wday_names[] = {"Sunday",
129 "Monday",
130 "Tuesday",
131 "Wednesday",
132 "Thursday",
133 "Friday",
134 "Saturday"};
135 static int
136 sipe_cal_get_wday(char *wday_name)
138 int i;
140 if (!wday_name) return -1;
142 for (i = 0; i < 7; i++) {
143 if (sipe_strequal(wday_names[i], wday_name)) {
144 return i;
148 return -1;
151 void
152 sipe_cal_event_free(struct sipe_cal_event* cal_event)
154 if (!cal_event) return;
156 g_free(cal_event->subject);
157 g_free(cal_event->location);
158 g_free(cal_event);
161 void
162 sipe_cal_events_free(GSList *cal_events)
164 if (!cal_events) return;
165 sipe_utils_slist_free_full(cal_events,
166 (GDestroyNotify) sipe_cal_event_free);
169 void
170 sipe_cal_calendar_free(struct sipe_calendar *cal)
172 if (cal) {
173 g_free(cal->email);
174 g_free(cal->legacy_dn);
175 g_free(cal->as_url);
176 g_free(cal->oof_url);
177 g_free(cal->oab_url);
178 g_free(cal->domino_url);
179 g_free(cal->oof_state);
180 g_free(cal->oof_note);
181 g_free(cal->free_busy);
182 g_free(cal->working_hours_xml_str);
184 sipe_cal_events_free(cal->cal_events);
186 if (cal->request)
187 sipe_http_request_cancel(cal->request);
188 sipe_http_session_close(cal->session);
190 g_free(cal);
194 void
195 sipe_cal_calendar_init(struct sipe_core_private *sipe_private)
197 if (!sipe_private->calendar) {
198 struct sipe_calendar *cal;
199 const char *value;
201 sipe_private->calendar = cal = g_new0(struct sipe_calendar, 1);
202 cal->sipe_private = sipe_private;
204 cal->email = g_strdup(sipe_private->email);
206 /* user specified a service URL? */
207 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_URL);
208 if (!is_empty(value)) {
209 cal->as_url = g_strdup(value);
210 cal->oof_url = g_strdup(value);
211 cal->domino_url = g_strdup(value);
214 return;
218 void
219 sipe_cal_event_debug(const struct sipe_cal_event *cal_event,
220 const gchar *label)
222 GString* str = g_string_new(NULL);
223 const gchar *status = "";
225 switch(cal_event->cal_status) {
226 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
227 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
228 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
229 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
230 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
233 g_string_append_printf(str, "\tstart_time: %s\n",
234 IS(cal_event->start_time) ? sipe_utils_time_to_debug_str(localtime(&cal_event->start_time)) : "");
235 g_string_append_printf(str, "\tend_time : %s\n",
236 IS(cal_event->end_time) ? sipe_utils_time_to_debug_str(localtime(&cal_event->end_time)) : "");
237 g_string_append_printf(str, "\tcal_status: %s\n", status);
238 g_string_append_printf(str, "\tsubject : %s\n", cal_event->subject ? cal_event->subject : "");
239 g_string_append_printf(str, "\tlocation : %s\n", cal_event->location ? cal_event->location : "");
240 /* last line must have no line break */
241 g_string_append_printf(str, "\tis_meeting: %s", cal_event->is_meeting ? "TRUE" : "FALSE");
243 SIPE_DEBUG_INFO("%s%s", label, str->str);
244 g_string_free(str, TRUE);
247 char *
248 sipe_cal_event_hash(struct sipe_cal_event* event)
250 /* no end_time as it dos not get published */
251 /* no cal_status as it can change on publication */
252 return g_strdup_printf("<%d><%s><%s><%d>",
253 (int)event->start_time,
254 event->subject ? event->subject : "",
255 event->location ? event->location : "",
256 event->is_meeting);
259 #define ENVIRONMENT_TIMEZONE "TZ"
261 static gchar *
262 sipe_switch_tz(const char *tz)
264 gchar *tz_orig;
266 tz_orig = g_strdup(g_getenv(ENVIRONMENT_TIMEZONE));
267 g_setenv(ENVIRONMENT_TIMEZONE, tz, TRUE);
268 tzset();
269 return(tz_orig);
272 static void
273 sipe_reset_tz(gchar *tz_orig)
275 if (tz_orig) {
276 g_setenv(ENVIRONMENT_TIMEZONE, tz_orig, TRUE);
277 g_free(tz_orig);
278 } else {
279 g_unsetenv(ENVIRONMENT_TIMEZONE);
281 tzset();
285 * Converts struct tm to Epoch time_t considering timezone.
287 * @param tz as defined for TZ environment variable.
289 * Reference: see timegm(3) - Linux man page
291 time_t
292 sipe_mktime_tz(struct tm *tm,
293 const char* tz)
295 time_t ret;
296 gchar *tz_orig;
298 tz_orig = sipe_switch_tz(tz);
299 ret = mktime(tm);
300 sipe_reset_tz(tz_orig);
302 return ret;
306 * Converts Epoch time_t to struct tm considering timezone.
308 * @param tz as defined for TZ environment variable.
310 * Reference: see timegm(3) - Linux man page
312 static struct tm *
313 sipe_localtime_tz(const time_t *time,
314 const char* tz)
316 struct tm *ret;
317 gchar *tz_orig;
319 tz_orig = sipe_switch_tz(tz);
320 ret = localtime(time);
321 sipe_reset_tz(tz_orig);
323 return ret;
326 void
327 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
329 if (!wh) return;
331 g_free(wh->std.time);
332 g_free(wh->std.day_of_week);
333 g_free(wh->std.year);
335 g_free(wh->dst.time);
336 g_free(wh->dst.day_of_week);
337 g_free(wh->dst.year);
339 g_free(wh->days_of_week);
340 g_free(wh->tz);
341 g_free(wh->tz_std);
342 g_free(wh->tz_dst);
343 g_free(wh);
347 * Returns time_t of daylight savings time start/end
348 * in the provided timezone or otherwise
349 * (time_t)-1 if no daylight savings time.
351 static time_t
352 sipe_cal_get_std_dst_time(time_t now,
353 int bias,
354 struct sipe_cal_std_dst* std_dst,
355 struct sipe_cal_std_dst* dst_std)
357 struct tm switch_tm;
358 time_t res = TIME_NULL;
359 struct tm *gm_now_tm;
360 gchar **time_arr;
362 if (std_dst->month == 0) return TIME_NULL;
364 gm_now_tm = gmtime(&now);
365 time_arr = g_strsplit(std_dst->time, ":", 0);
367 switch_tm.tm_sec = atoi(time_arr[2]);
368 switch_tm.tm_min = atoi(time_arr[1]);
369 switch_tm.tm_hour = atoi(time_arr[0]);
370 g_strfreev(time_arr);
371 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
372 switch_tm.tm_mon = std_dst->month - 1;
373 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
374 switch_tm.tm_isdst = 0;
375 /* to set tm_wday */
376 res = sipe_mktime_tz(&switch_tm, "UTC");
378 /* if not dynamic, calculate right tm_mday */
379 if (!std_dst->year) {
380 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
381 int needed_month;
382 /* get first desired wday in the month */
383 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
384 switch_tm.tm_mday = 1 + delta;
385 /* try nth order */
386 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
387 needed_month = switch_tm.tm_mon;
388 /* to set settle date if ahead of allowed month dates */
389 res = sipe_mktime_tz(&switch_tm, "UTC");
390 if (needed_month != switch_tm.tm_mon) {
391 /* moving 1 week back to stay within required month */
392 switch_tm.tm_mday -= 7;
393 /* to fix date again */
394 res = sipe_mktime_tz(&switch_tm, "UTC");
397 /* note: bias is taken from "switch to" structure */
398 return res + (bias + dst_std->bias)*60;
401 static void
402 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
403 struct sipe_cal_std_dst *std_dst)
405 const sipe_xml *node;
406 gchar *tmp;
408 if (!xn_std_dst_time) return;
409 if (!std_dst) return;
411 <StandardTime>
412 <Bias>0</Bias>
413 <Time>02:00:00</Time>
414 <DayOrder>1</DayOrder>
415 <Month>11</Month>
416 <Year>2009</Year>
417 <DayOfWeek>Sunday</DayOfWeek>
418 </StandardTime>
421 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
422 std_dst->bias = atoi(tmp = sipe_xml_data(node));
423 g_free(tmp);
426 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
427 std_dst->time = sipe_xml_data(node);
430 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
431 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
432 g_free(tmp);
435 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
436 std_dst->month = atoi(tmp = sipe_xml_data(node));
437 g_free(tmp);
440 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
441 std_dst->day_of_week = sipe_xml_data(node);
444 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
445 std_dst->year = sipe_xml_data(node);
449 void
450 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
451 struct sipe_buddy *buddy)
453 const sipe_xml *xn_bias;
454 const sipe_xml *xn_timezone;
455 const sipe_xml *xn_working_period;
456 const sipe_xml *xn_standard_time;
457 const sipe_xml *xn_daylight_time;
458 gchar *tmp;
459 time_t now = time(NULL);
460 struct sipe_cal_std_dst* std;
461 struct sipe_cal_std_dst* dst;
463 if (!xn_working_hours) return;
465 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
466 <TimeZone>
467 <Bias>480</Bias>
469 </TimeZone>
470 <WorkingPeriodArray>
471 <WorkingPeriod>
472 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
473 <StartTimeInMinutes>600</StartTimeInMinutes>
474 <EndTimeInMinutes>1140</EndTimeInMinutes>
475 </WorkingPeriod>
476 </WorkingPeriodArray>
477 </WorkingHours>
479 sipe_cal_free_working_hours(buddy->cal_working_hours);
480 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
482 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
483 xn_bias = sipe_xml_child(xn_timezone, "Bias");
484 if (xn_bias) {
485 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
486 g_free(tmp);
489 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
490 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
492 std = &((*buddy->cal_working_hours).std);
493 dst = &((*buddy->cal_working_hours).dst);
494 sipe_cal_parse_std_dst(xn_standard_time, std);
495 sipe_cal_parse_std_dst(xn_daylight_time, dst);
497 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
498 if (xn_working_period) {
499 /* NOTE: this can be NULL! */
500 buddy->cal_working_hours->days_of_week =
501 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
503 buddy->cal_working_hours->start_time =
504 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
505 g_free(tmp);
507 buddy->cal_working_hours->end_time =
508 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
509 g_free(tmp);
512 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
513 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
515 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
516 buddy->cal_working_hours->tz =
517 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
518 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
519 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
521 buddy->cal_working_hours->dst.month,
522 buddy->cal_working_hours->dst.day_order,
523 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
524 buddy->cal_working_hours->dst.time,
526 buddy->cal_working_hours->std.month,
527 buddy->cal_working_hours->std.day_order,
528 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
529 buddy->cal_working_hours->std.time
531 /* TST8 */
532 buddy->cal_working_hours->tz_std =
533 g_strdup_printf("TST%d",
534 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
535 /* TDT7 */
536 buddy->cal_working_hours->tz_dst =
537 g_strdup_printf("TDT%d",
538 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
541 struct sipe_cal_event*
542 sipe_cal_get_event(GSList *cal_events,
543 time_t time_in_question)
545 GSList *entry = cal_events;
546 struct sipe_cal_event* cal_event;
547 struct sipe_cal_event* res = NULL;
549 if (!cal_events || !IS(time_in_question)) return NULL;
551 while (entry) {
552 cal_event = entry->data;
553 /* event is in the past or in the future */
554 if (cal_event->start_time > time_in_question ||
555 cal_event->end_time <= time_in_question)
557 entry = entry->next;
558 continue;
561 if (!res) {
562 res = cal_event;
563 } else {
564 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
565 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
566 if (res_status < cal_status) {
567 res = cal_event;
570 entry = entry->next;
572 return res;
575 static int
576 sipe_cal_get_status0(const gchar *free_busy,
577 time_t cal_start,
578 int granularity,
579 time_t time_in_question,
580 int *index)
582 int res = SIPE_CAL_NO_DATA;
583 int shift;
584 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
586 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
588 shift = (time_in_question - cal_start) / (granularity*60);
589 if (index) {
590 *index = shift;
593 res = free_busy[shift] - '0';
595 return res;
599 * Returns time when current calendar state started
601 static time_t
602 sipe_cal_get_since_time(const gchar *free_busy,
603 time_t calStart,
604 int granularity,
605 int index,
606 int current_state)
608 int i;
610 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
612 for (i = index; i >= 0; i--) {
613 int temp_status = free_busy[i] - '0';
615 if (current_state != temp_status) {
616 return calStart + (i + 1)*granularity*60;
620 return calStart;
623 static char*
624 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
627 sipe_cal_get_status(struct sipe_buddy *buddy,
628 time_t time_in_question,
629 time_t *since)
631 time_t cal_start;
632 const char* free_busy;
633 int ret = SIPE_CAL_NO_DATA;
634 time_t state_since;
635 int index = -1;
637 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
638 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
639 buddy ? (buddy->name ? buddy->name : "") : "");
640 return SIPE_CAL_NO_DATA;
643 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
644 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy->name);
645 return SIPE_CAL_NO_DATA;
647 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy);
649 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
651 ret = sipe_cal_get_status0(free_busy,
652 cal_start,
653 buddy->cal_granularity,
654 time_in_question,
655 &index);
656 state_since = sipe_cal_get_since_time(free_busy,
657 cal_start,
658 buddy->cal_granularity,
659 index,
660 ret);
662 if (since) *since = state_since;
663 return ret;
666 static time_t
667 sipe_cal_get_switch_time(const gchar *free_busy,
668 time_t calStart,
669 int granularity,
670 int index,
671 int current_state,
672 int *to_state)
674 size_t i;
675 time_t ret = TIME_NULL;
677 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
678 *to_state = SIPE_CAL_NO_DATA;
679 return ret;
682 for (i = index + 1; i < strlen(free_busy); i++) {
683 int temp_status = free_busy[i] - '0';
685 if (current_state != temp_status) {
686 *to_state = temp_status;
687 return calStart + i*granularity*60;
691 return ret;
694 static const char*
695 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
696 time_t time_in_question)
698 time_t dst_switch_time = (*wh).dst.switch_time;
699 time_t std_switch_time = (*wh).std.switch_time;
700 gboolean is_dst = FALSE;
702 /* No daylight savings */
703 if (dst_switch_time == TIME_NULL) {
704 return wh->tz_std;
707 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
708 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
709 is_dst = TRUE;
711 } else { /* South hemisphere - Australia */
712 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
713 is_dst = TRUE;
717 if (is_dst) {
718 return wh->tz_dst;
719 } else {
720 return wh->tz_std;
724 static time_t
725 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
726 const int shift_minutes,
727 const char *tz)
729 sample_today_tm->tm_sec = 0;
730 sample_today_tm->tm_min = shift_minutes % 60;
731 sample_today_tm->tm_hour = shift_minutes / 60;
733 return sipe_mktime_tz(sample_today_tm, tz);
737 * Returns work day start and end in Epoch time
738 * considering the initial values are provided
739 * in contact's local time zone.
741 static void
742 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
743 time_t *start,
744 time_t *end,
745 time_t *next_start)
747 time_t now = time(NULL);
748 const char *tz = sipe_cal_get_tz(wh, now);
749 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
751 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday]))) {
752 /* not a work day */
753 *start = TIME_NULL;
754 *end = TIME_NULL;
755 *next_start = TIME_NULL;
756 return;
759 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
761 if (now < *end) {
762 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
763 *next_start = TIME_NULL;
764 } else { /* calculate start of tomorrow's work day if any */
765 time_t tom = now + 24*60*60;
766 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
768 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday]))) {
769 /* not a work day */
770 *next_start = TIME_NULL;
773 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
774 *start = TIME_NULL;
778 static int
779 sipe_cal_is_in_work_hours(const time_t time_in_question,
780 const time_t start,
781 const time_t end)
783 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
787 * Returns time closest to now. Choses only from times ahead of now.
788 * Returns TIME_NULL otherwise.
790 static time_t
791 sipe_cal_get_until(const time_t now,
792 const time_t switch_time,
793 const time_t start,
794 const time_t end,
795 const time_t next_start)
797 time_t ret = TIME_NULL;
798 int min_diff = now - ret;
800 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
801 min_diff = switch_time - now;
802 ret = switch_time;
804 if (IS(start) && start > now && (start - now) < min_diff) {
805 min_diff = start - now;
806 ret = start;
808 if (IS(end) && end > now && (end - now) < min_diff) {
809 min_diff = end - now;
810 ret = end;
812 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
813 min_diff = next_start - now;
814 ret = next_start;
816 return ret;
819 static char*
820 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
822 /* do lazy decode if necessary */
823 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
824 gsize cal_dec64_len;
825 guchar *cal_dec64;
826 gsize i;
827 int j = 0;
829 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
831 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
833 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
834 00, Free (Fr)
835 01, Tentative (Te)
836 10, Busy (Bu)
837 11, Out of facility (Oo)
839 http://msdn.microsoft.com/en-us/library/aa566048.aspx
840 0 Free
841 1 Tentative
842 2 Busy
843 3 Out of Office (OOF)
844 4 No data
846 for (i = 0; i < cal_dec64_len; i++) {
847 #define TWO_BIT_MASK 0x03
848 char tmp = cal_dec64[i];
849 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
850 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
851 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
852 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
854 buddy->cal_free_busy[j++] = '\0';
855 g_free(cal_dec64);
858 return buddy->cal_free_busy;
861 char *
862 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
864 guint i = 0;
865 guint j = 0;
866 guint shift_factor = 0;
867 guint len, res_len;
868 guchar *res;
869 gchar *res_base64;
871 if (!freebusy_hex) return NULL;
873 len = strlen(freebusy_hex);
874 res_len = len / 4 + 1;
875 res = g_malloc0(res_len);
876 while (i < len) {
877 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
878 shift_factor += 2;
879 if (shift_factor == 8) {
880 shift_factor = 0;
881 j++;
885 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
886 g_free(res);
887 return res_base64;
890 char *
891 sipe_cal_get_description(struct sipe_buddy *buddy)
893 time_t cal_start;
894 time_t cal_end;
895 int current_cal_state;
896 time_t now = time(NULL);
897 time_t start = TIME_NULL;
898 time_t end = TIME_NULL;
899 time_t next_start = TIME_NULL;
900 time_t switch_time;
901 int to_state = SIPE_CAL_NO_DATA;
902 time_t until = TIME_NULL;
903 int index = 0;
904 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
905 const char *free_busy;
906 const char *cal_states[] = {_("Free"),
907 _("Tentative"),
908 _("Busy"),
909 _("Out of office"),
910 _("No data")};
912 if (buddy->cal_granularity != 15) {
913 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy->cal_granularity);
914 return NULL;
917 /* to lazy load if needed */
918 free_busy = sipe_cal_get_free_busy(buddy);
919 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy ? free_busy : "");
921 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
922 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: no calendar data, exiting");
923 return NULL;
926 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
927 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
929 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
930 if (current_cal_state == SIPE_CAL_NO_DATA) {
931 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: calendar is undefined for present moment, exiting.");
932 return NULL;
935 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
937 SIPE_DEBUG_INFO_NOFORMAT("\n* Calendar *");
938 if (buddy->cal_working_hours) {
939 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
941 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy->cal_working_hours, now));
942 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
943 IS((*buddy->cal_working_hours).std.switch_time) ? sipe_utils_time_to_debug_str(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "");
944 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
945 IS((*buddy->cal_working_hours).dst.switch_time) ? sipe_utils_time_to_debug_str(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "");
946 SIPE_DEBUG_INFO("Remote now time : %s",
947 sipe_utils_time_to_debug_str(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
948 SIPE_DEBUG_INFO("Remote start time : %s",
949 IS(start) ? sipe_utils_time_to_debug_str(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "");
950 SIPE_DEBUG_INFO("Remote end time : %s",
951 IS(end) ? sipe_utils_time_to_debug_str(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "");
952 SIPE_DEBUG_INFO("Rem. next_start time: %s",
953 IS(next_start) ? sipe_utils_time_to_debug_str(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "");
954 SIPE_DEBUG_INFO("Remote switch time : %s",
955 IS(switch_time) ? sipe_utils_time_to_debug_str(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "");
956 } else {
957 SIPE_DEBUG_INFO("Local now time : %s",
958 sipe_utils_time_to_debug_str(localtime(&now)));
959 SIPE_DEBUG_INFO("Local switch time : %s",
960 IS(switch_time) ? sipe_utils_time_to_debug_str(localtime(&switch_time)) : "");
962 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", sipe_utils_time_to_debug_str(gmtime(&cal_end)));
963 SIPE_DEBUG_INFO("current cal state : %s", cal_states[current_cal_state]);
964 SIPE_DEBUG_INFO("switch cal state : %s", cal_states[to_state] );
966 /* Calendar: string calculations */
969 ALGORITHM (don't delete)
970 (c)2009,2010 pier11 <pier11@operamail.com>
972 SOD = Start of Work Day
973 EOD = End of Work Day
974 NSOD = Start of tomorrow's Work Day
975 SW = Calendar status switch time
977 if current_cal_state == Free
978 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
979 else
980 until = SW
982 if (!until && (cal_period_end > now + 8H))
983 until = cal_period_end
985 if (!until)
986 return "Currently %", current_cal_state
988 if (until - now > 8H)
989 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
990 return "Outside of working hours for next 8 hours"
991 else
992 return "%s for next 8 hours", current_cal_state
994 if (current_cal_state == Free)
995 if (work_hours && until !in work_hours(now))
996 "Not working"
997 else
998 "%s", current_cal_state
999 " until %.2d:%.2d", until
1000 else
1001 "Currently %", current_cal_state
1002 if (work_hours && until !in work_hours(until))
1003 ". Outside of working hours at at %.2d:%.2d", until
1004 else
1005 ". %s at %.2d:%.2d", to_state, until
1008 if (current_cal_state < 1) { /* Free */
1009 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
1010 } else {
1011 until = switch_time;
1014 if (!IS(until) && (cal_end - now > 8*60*60))
1015 until = cal_end;
1017 if (!IS(until)) {
1018 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1021 if (until - now > 8*60*60) {
1022 /* Free & outside work hours */
1023 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1024 return g_strdup(_("Outside of working hours for next 8 hours"));
1025 } else {
1026 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
1030 if (current_cal_state < 1) { /* Free */
1031 const char *tmp;
1032 struct tm *until_tm = localtime(&until);
1034 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1035 tmp = _("Not working");
1036 } else {
1037 tmp = cal_states[current_cal_state];
1039 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
1040 } else { /* Tentative or Busy or OOF */
1041 char *tmp;
1042 char *res;
1043 struct tm *until_tm = localtime(&until);
1045 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1046 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
1047 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
1048 tmp, until_tm->tm_hour, until_tm->tm_min);
1049 g_free(tmp);
1050 return res;
1051 } else {
1052 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
1053 g_free(tmp);
1054 return res;
1057 /* End of - Calendar: string calculations */
1060 #define UPDATE_CALENDAR_INTERVAL (15*60) /* 15 min, default granularity for Exchange */
1061 #define UPDATE_CALENDAR_OFFSET 30 /* 30 seconds before next interval starts */
1063 void sipe_core_update_calendar(struct sipe_core_public *sipe_public)
1065 time_t now, offset;
1067 SIPE_LOG_INFO_NOFORMAT("sipe_core_update_calendar: started.");
1069 /* Do in parallel.
1070 * If failed, the branch will be disabled for subsequent calls.
1071 * Can't rely that user turned the functionality on in account settings.
1073 sipe_ews_update_calendar(SIPE_CORE_PRIVATE);
1074 #ifdef _WIN32
1075 /* @TODO: UNIX integration missing */
1076 sipe_domino_update_calendar(SIPE_CORE_PRIVATE);
1077 #endif
1079 /* how long, in seconds, until the next calendar interval starts? */
1080 now = time(NULL);
1081 offset = (now / UPDATE_CALENDAR_INTERVAL + 1) * UPDATE_CALENDAR_INTERVAL - now;
1083 /* ensure that the update after the initial one is not too soon */
1084 if (offset <= (UPDATE_CALENDAR_INTERVAL / 2))
1085 offset += UPDATE_CALENDAR_INTERVAL;
1087 /* schedule next update before a new calendar interval starts */
1088 sipe_schedule_seconds(SIPE_CORE_PRIVATE,
1089 "<+update-calendar>",
1090 NULL,
1091 offset - UPDATE_CALENDAR_OFFSET,
1092 (sipe_schedule_action)sipe_core_update_calendar,
1093 NULL);
1095 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: finished.");
1098 void sipe_cal_presence_publish(struct sipe_core_private *sipe_private,
1099 gboolean do_publish_calendar)
1101 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1102 if (do_publish_calendar)
1103 sipe_ocs2007_presence_publish(sipe_private, NULL);
1104 else
1105 sipe_ocs2007_category_publish(sipe_private, FALSE);
1106 } else {
1107 sipe_ocs2005_presence_publish(sipe_private,
1108 do_publish_calendar);
1112 void sipe_cal_delayed_calendar_update(struct sipe_core_private *sipe_private)
1114 #define UPDATE_CALENDAR_DELAY 1*60 /* 1 min */
1116 /* only start periodic calendar updating if user hasn't disabled it */
1117 if (!SIPE_CORE_PUBLIC_FLAG_IS(DONT_PUBLISH))
1118 sipe_schedule_seconds(sipe_private,
1119 "<+update-calendar>",
1120 NULL,
1121 UPDATE_CALENDAR_DELAY,
1122 (sipe_schedule_action) sipe_core_update_calendar,
1123 NULL);
1127 Local Variables:
1128 mode: c
1129 c-file-style: "bsd"
1130 indent-tabs-mode: t
1131 tab-width: 8
1132 End: