core cleanup: move out ocs2007 code
[siplcs.git] / src / core / sipe-cal.c
blob5f0cb62398e6efd25e7e944aa785a8546cd38e56
1 /**
2 * @file sipe-cal.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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 "http-conn.h"
36 #include "sipe-backend.h"
37 #include "sipe-buddy.h"
38 #include "sipe-core.h"
39 #include "sipe-core-private.h"
40 #include "sipe-cal.h"
41 #include "sipe-nls.h"
42 #include "sipe-ocs2007.h"
43 #include "sipe-schedule.h"
44 #include "sipe-utils.h"
45 #include "sipe-xml.h"
46 #include "sipe.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 GSList *entry = cal_events;
166 if (!cal_events) return;
168 while (entry) {
169 struct sipe_cal_event *cal_event = entry->data;
170 sipe_cal_event_free(cal_event);
171 entry = entry->next;
174 g_slist_free(cal_events);
177 void
178 sipe_cal_calendar_free(struct sipe_calendar *cal)
180 g_free(cal->email);
181 g_free(cal->legacy_dn);
182 if (cal->auth) {
183 g_free(cal->auth->domain);
184 g_free(cal->auth->user);
185 g_free(cal->auth->password);
187 g_free(cal->auth);
188 g_free(cal->as_url);
189 g_free(cal->oof_url);
190 g_free(cal->oab_url);
191 g_free(cal->domino_url);
192 g_free(cal->oof_state);
193 g_free(cal->oof_note);
194 g_free(cal->free_busy);
195 g_free(cal->working_hours_xml_str);
197 sipe_cal_events_free(cal->cal_events);
199 if (cal->http_conn) {
200 http_conn_free(cal->http_conn);
203 if (cal->http_session) {
204 http_conn_session_free(cal->http_session);
207 g_free(cal);
210 gboolean
211 sipe_cal_calendar_init(struct sipe_core_private *sipe_private,
212 gboolean *has_url)
214 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
215 if (!sip->cal) {
216 const char *value;
218 sip->cal = g_new0(struct sipe_calendar, 1);
219 sip->cal->sipe_private = sipe_private;
221 sip->cal->email = g_strdup(sip->email);
223 /* user specified a service URL? */
224 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_URL);
225 if (has_url) *has_url = !is_empty(value);
226 if (!is_empty(value)) {
227 sip->cal->as_url = g_strdup(value);
228 sip->cal->oof_url = g_strdup(value);
229 sip->cal->domino_url = g_strdup(value);
232 sip->cal->auth = g_new0(HttpConnAuth, 1);
233 sip->cal->auth->use_negotiate = SIPE_CORE_PUBLIC_FLAG_IS(KRB5);
235 /* user specified email login? */
236 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_LOGIN);
237 if (!is_empty(value)) {
239 /* user specified email login domain? */
240 const char *tmp = strstr(value, "\\");
241 if (tmp) {
242 sip->cal->auth->domain = g_strndup(value, tmp - value);
243 sip->cal->auth->user = g_strdup(tmp + 1);
244 } else {
245 sip->cal->auth->user = g_strdup(value);
247 sip->cal->auth->password = g_strdup(sipe_backend_setting(SIPE_CORE_PUBLIC,
248 SIPE_SETTING_EMAIL_PASSWORD));
250 } else {
251 /* re-use SIPE credentials */
252 sip->cal->auth->domain = g_strdup(sip->authdomain);
253 sip->cal->auth->user = g_strdup(sip->authuser);
254 sip->cal->auth->password = g_strdup(sip->password);
256 return TRUE;
258 return FALSE;
262 char *
263 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
265 GString* str = g_string_new(NULL);
266 const char *status = "";
268 switch(cal_event->cal_status) {
269 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
270 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
271 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
272 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
273 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
276 g_string_append_printf(str, "\t%s: %s", "start_time",
277 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
278 g_string_append_printf(str, "\t%s: %s", "end_time ",
279 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
280 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
281 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
282 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
283 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
285 return g_string_free(str, FALSE);
288 char *
289 sipe_cal_event_hash(struct sipe_cal_event* event)
291 /* no end_time as it dos not get published */
292 /* no cal_status as it can change on publication */
293 return g_strdup_printf("<%d><%s><%s><%d>",
294 (int)event->start_time,
295 event->subject ? event->subject : "",
296 event->location ? event->location : "",
297 event->is_meeting);
300 #define ENVIRONMENT_TIMEZONE "TZ"
302 static gchar *
303 sipe_switch_tz(const char *tz)
305 gchar *tz_orig;
307 tz_orig = g_strdup(g_getenv(ENVIRONMENT_TIMEZONE));
308 g_setenv(ENVIRONMENT_TIMEZONE, tz, TRUE);
309 tzset();
310 return(tz_orig);
313 static void
314 sipe_reset_tz(gchar *tz_orig)
316 if (tz_orig) {
317 g_setenv(ENVIRONMENT_TIMEZONE, tz_orig, TRUE);
318 g_free(tz_orig);
319 } else {
320 g_unsetenv(ENVIRONMENT_TIMEZONE);
322 tzset();
326 * Converts struct tm to Epoch time_t considering timezone.
328 * @param tz as defined for TZ environment variable.
330 * Reference: see timegm(3) - Linux man page
332 time_t
333 sipe_mktime_tz(struct tm *tm,
334 const char* tz)
336 time_t ret;
337 gchar *tz_orig;
339 tz_orig = sipe_switch_tz(tz);
340 ret = mktime(tm);
341 sipe_reset_tz(tz_orig);
343 return ret;
347 * Converts Epoch time_t to struct tm considering timezone.
349 * @param tz as defined for TZ environment variable.
351 * Reference: see timegm(3) - Linux man page
353 static struct tm *
354 sipe_localtime_tz(const time_t *time,
355 const char* tz)
357 struct tm *ret;
358 gchar *tz_orig;
360 tz_orig = sipe_switch_tz(tz);
361 ret = localtime(time);
362 sipe_reset_tz(tz_orig);
364 return ret;
367 void
368 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
370 if (!wh) return;
372 g_free(wh->std.time);
373 g_free(wh->std.day_of_week);
374 g_free(wh->std.year);
376 g_free(wh->dst.time);
377 g_free(wh->dst.day_of_week);
378 g_free(wh->dst.year);
380 g_free(wh->days_of_week);
381 g_free(wh->tz);
382 g_free(wh->tz_std);
383 g_free(wh->tz_dst);
384 g_free(wh);
388 * Returns time_t of daylight savings time start/end
389 * in the provided timezone or otherwise
390 * (time_t)-1 if no daylight savings time.
392 static time_t
393 sipe_cal_get_std_dst_time(time_t now,
394 int bias,
395 struct sipe_cal_std_dst* std_dst,
396 struct sipe_cal_std_dst* dst_std)
398 struct tm switch_tm;
399 time_t res = TIME_NULL;
400 struct tm *gm_now_tm;
401 gchar **time_arr;
403 if (std_dst->month == 0) return TIME_NULL;
405 gm_now_tm = gmtime(&now);
406 time_arr = g_strsplit(std_dst->time, ":", 0);
408 switch_tm.tm_sec = atoi(time_arr[2]);
409 switch_tm.tm_min = atoi(time_arr[1]);
410 switch_tm.tm_hour = atoi(time_arr[0]);
411 g_strfreev(time_arr);
412 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
413 switch_tm.tm_mon = std_dst->month - 1;
414 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
415 switch_tm.tm_isdst = 0;
416 /* to set tm_wday */
417 res = sipe_mktime_tz(&switch_tm, "UTC");
419 /* if not dynamic, calculate right tm_mday */
420 if (!std_dst->year) {
421 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
422 int needed_month;
423 /* get first desired wday in the month */
424 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
425 switch_tm.tm_mday = 1 + delta;
426 /* try nth order */
427 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
428 needed_month = switch_tm.tm_mon;
429 /* to set settle date if ahead of allowed month dates */
430 res = sipe_mktime_tz(&switch_tm, "UTC");
431 if (needed_month != switch_tm.tm_mon) {
432 /* moving 1 week back to stay within required month */
433 switch_tm.tm_mday -= 7;
434 /* to fix date again */
435 res = sipe_mktime_tz(&switch_tm, "UTC");
438 /* note: bias is taken from "switch to" structure */
439 return res + (bias + dst_std->bias)*60;
442 static void
443 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
444 struct sipe_cal_std_dst *std_dst)
446 const sipe_xml *node;
447 gchar *tmp;
449 if (!xn_std_dst_time) return;
450 if (!std_dst) return;
452 <StandardTime>
453 <Bias>0</Bias>
454 <Time>02:00:00</Time>
455 <DayOrder>1</DayOrder>
456 <Month>11</Month>
457 <DayOfWeek>Sunday</DayOfWeek>
458 </StandardTime>
461 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
462 std_dst->bias = atoi(tmp = sipe_xml_data(node));
463 g_free(tmp);
466 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
467 std_dst->time = sipe_xml_data(node);
470 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
471 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
472 g_free(tmp);
475 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
476 std_dst->month = atoi(tmp = sipe_xml_data(node));
477 g_free(tmp);
480 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
481 std_dst->day_of_week = sipe_xml_data(node);
484 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
485 std_dst->year = sipe_xml_data(node);
489 void
490 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
491 struct sipe_buddy *buddy)
493 const sipe_xml *xn_bias;
494 const sipe_xml *xn_timezone;
495 const sipe_xml *xn_working_period;
496 const sipe_xml *xn_standard_time;
497 const sipe_xml *xn_daylight_time;
498 gchar *tmp;
499 time_t now = time(NULL);
500 struct sipe_cal_std_dst* std;
501 struct sipe_cal_std_dst* dst;
503 if (!xn_working_hours) return;
505 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
506 <TimeZone>
507 <Bias>480</Bias>
509 </TimeZone>
510 <WorkingPeriodArray>
511 <WorkingPeriod>
512 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
513 <StartTimeInMinutes>600</StartTimeInMinutes>
514 <EndTimeInMinutes>1140</EndTimeInMinutes>
515 </WorkingPeriod>
516 </WorkingPeriodArray>
517 </WorkingHours>
519 sipe_cal_free_working_hours(buddy->cal_working_hours);
520 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
522 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
523 xn_bias = sipe_xml_child(xn_timezone, "Bias");
524 if (xn_bias) {
525 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
526 g_free(tmp);
529 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
530 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
532 std = &((*buddy->cal_working_hours).std);
533 dst = &((*buddy->cal_working_hours).dst);
534 sipe_cal_parse_std_dst(xn_standard_time, std);
535 sipe_cal_parse_std_dst(xn_daylight_time, dst);
537 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
538 if (xn_working_period) {
539 /* NOTE: this can be NULL! */
540 buddy->cal_working_hours->days_of_week =
541 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
543 buddy->cal_working_hours->start_time =
544 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
545 g_free(tmp);
547 buddy->cal_working_hours->end_time =
548 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
549 g_free(tmp);
552 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
553 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
555 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
556 buddy->cal_working_hours->tz =
557 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
558 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
559 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
561 buddy->cal_working_hours->dst.month,
562 buddy->cal_working_hours->dst.day_order,
563 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
564 buddy->cal_working_hours->dst.time,
566 buddy->cal_working_hours->std.month,
567 buddy->cal_working_hours->std.day_order,
568 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
569 buddy->cal_working_hours->std.time
571 /* TST8 */
572 buddy->cal_working_hours->tz_std =
573 g_strdup_printf("TST%d",
574 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
575 /* TDT7 */
576 buddy->cal_working_hours->tz_dst =
577 g_strdup_printf("TDT%d",
578 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
581 struct sipe_cal_event*
582 sipe_cal_get_event(GSList *cal_events,
583 time_t time_in_question)
585 GSList *entry = cal_events;
586 struct sipe_cal_event* cal_event;
587 struct sipe_cal_event* res = NULL;
589 if (!cal_events || !IS(time_in_question)) return NULL;
591 while (entry) {
592 cal_event = entry->data;
593 /* event is in the past or in the future */
594 if (cal_event->start_time > time_in_question ||
595 cal_event->end_time <= time_in_question)
597 entry = entry->next;
598 continue;
601 if (!res) {
602 res = cal_event;
603 } else {
604 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
605 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
606 if (res_status < cal_status) {
607 res = cal_event;
610 entry = entry->next;
612 return res;
615 static int
616 sipe_cal_get_status0(const gchar *free_busy,
617 time_t cal_start,
618 int granularity,
619 time_t time_in_question,
620 int *index)
622 int res = SIPE_CAL_NO_DATA;
623 int shift;
624 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
626 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
628 shift = (time_in_question - cal_start) / (granularity*60);
629 if (index) {
630 *index = shift;
633 res = free_busy[shift] - '0';
635 return res;
639 * Returns time when current calendar state started
641 static time_t
642 sipe_cal_get_since_time(const gchar *free_busy,
643 time_t calStart,
644 int granularity,
645 int index,
646 int current_state)
648 int i;
650 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
652 for (i = index; i >= 0; i--) {
653 int temp_status = free_busy[i] - '0';
655 if (current_state != temp_status) {
656 return calStart + (i + 1)*granularity*60;
659 if (i == 0) return calStart;
662 return 0;
664 static char*
665 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
668 sipe_cal_get_status(struct sipe_buddy *buddy,
669 time_t time_in_question,
670 time_t *since)
672 time_t cal_start;
673 const char* free_busy;
674 int ret = SIPE_CAL_NO_DATA;
675 time_t state_since;
676 int index;
678 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
679 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
680 buddy ? (buddy->name ? buddy->name : "") : "");
681 return SIPE_CAL_NO_DATA;
684 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
685 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy->name);
686 return SIPE_CAL_NO_DATA;
688 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy);
690 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
692 ret = sipe_cal_get_status0(free_busy,
693 cal_start,
694 buddy->cal_granularity,
695 time_in_question,
696 &index);
697 state_since = sipe_cal_get_since_time(free_busy,
698 cal_start,
699 buddy->cal_granularity,
700 index,
701 ret);
703 if (since) *since = state_since;
704 return ret;
707 static time_t
708 sipe_cal_get_switch_time(const gchar *free_busy,
709 time_t calStart,
710 int granularity,
711 int index,
712 int current_state,
713 int *to_state)
715 size_t i;
716 time_t ret = TIME_NULL;
718 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
719 *to_state = SIPE_CAL_NO_DATA;
720 return ret;
723 for (i = index + 1; i < strlen(free_busy); i++) {
724 int temp_status = free_busy[i] - '0';
726 if (current_state != temp_status) {
727 *to_state = temp_status;
728 return calStart + i*granularity*60;
732 return ret;
735 static const char*
736 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
737 time_t time_in_question)
739 time_t dst_switch_time = (*wh).dst.switch_time;
740 time_t std_switch_time = (*wh).std.switch_time;
741 gboolean is_dst = FALSE;
743 /* No daylight savings */
744 if (dst_switch_time == TIME_NULL) {
745 return wh->tz_std;
748 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
749 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
750 is_dst = TRUE;
752 } else { /* South hemisphere - Australia */
753 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
754 is_dst = TRUE;
758 if (is_dst) {
759 return wh->tz_dst;
760 } else {
761 return wh->tz_std;
765 static time_t
766 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
767 const int shift_minutes,
768 const char *tz)
770 sample_today_tm->tm_sec = 0;
771 sample_today_tm->tm_min = shift_minutes % 60;
772 sample_today_tm->tm_hour = shift_minutes / 60;
774 return sipe_mktime_tz(sample_today_tm, tz);
778 * Returns work day start and end in Epoch time
779 * considering the initial values are provided
780 * in contact's local time zone.
782 static void
783 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
784 time_t *start,
785 time_t *end,
786 time_t *next_start)
788 time_t now = time(NULL);
789 const char *tz = sipe_cal_get_tz(wh, now);
790 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
792 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday]))) {
793 /* not a work day */
794 *start = TIME_NULL;
795 *end = TIME_NULL;
796 *next_start = TIME_NULL;
797 return;
800 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
802 if (now < *end) {
803 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
804 *next_start = TIME_NULL;
805 } else { /* calculate start of tomorrow's work day if any */
806 time_t tom = now + 24*60*60;
807 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
809 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday]))) {
810 /* not a work day */
811 *next_start = TIME_NULL;
814 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
815 *start = TIME_NULL;
819 static int
820 sipe_cal_is_in_work_hours(const time_t time_in_question,
821 const time_t start,
822 const time_t end)
824 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
828 * Returns time closest to now. Choses only from times ahead of now.
829 * Returns TIME_NULL otherwise.
831 static time_t
832 sipe_cal_get_until(const time_t now,
833 const time_t switch_time,
834 const time_t start,
835 const time_t end,
836 const time_t next_start)
838 time_t ret = TIME_NULL;
839 int min_diff = now - ret;
841 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
842 min_diff = switch_time - now;
843 ret = switch_time;
845 if (IS(start) && start > now && (start - now) < min_diff) {
846 min_diff = start - now;
847 ret = start;
849 if (IS(end) && end > now && (end - now) < min_diff) {
850 min_diff = end - now;
851 ret = end;
853 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
854 min_diff = next_start - now;
855 ret = next_start;
857 return ret;
860 static char*
861 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
863 /* do lazy decode if necessary */
864 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
865 gsize cal_dec64_len;
866 guchar *cal_dec64;
867 gsize i;
868 int j = 0;
870 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
872 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
874 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
875 00, Free (Fr)
876 01, Tentative (Te)
877 10, Busy (Bu)
878 11, Out of facility (Oo)
880 http://msdn.microsoft.com/en-us/library/aa566048.aspx
881 0 Free
882 1 Tentative
883 2 Busy
884 3 Out of Office (OOF)
885 4 No data
887 for (i = 0; i < cal_dec64_len; i++) {
888 #define TWO_BIT_MASK 0x03
889 char tmp = cal_dec64[i];
890 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
891 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
892 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
893 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
895 buddy->cal_free_busy[j++] = '\0';
896 g_free(cal_dec64);
899 return buddy->cal_free_busy;
902 char *
903 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
905 guint i = 0;
906 guint j = 0;
907 guint shift_factor = 0;
908 guint len, res_len;
909 guchar *res;
910 gchar *res_base64;
912 if (!freebusy_hex) return NULL;
914 len = strlen(freebusy_hex);
915 res_len = len / 4 + 1;
916 res = g_malloc0(res_len);
917 while (i < len) {
918 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
919 shift_factor += 2;
920 if (shift_factor == 8) {
921 shift_factor = 0;
922 j++;
926 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
927 g_free(res);
928 return res_base64;
931 char *
932 sipe_cal_get_description(struct sipe_buddy *buddy)
934 time_t cal_start;
935 time_t cal_end;
936 int current_cal_state;
937 time_t now = time(NULL);
938 time_t start = TIME_NULL;
939 time_t end = TIME_NULL;
940 time_t next_start = TIME_NULL;
941 time_t switch_time;
942 int to_state = SIPE_CAL_NO_DATA;
943 time_t until = TIME_NULL;
944 int index = 0;
945 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
946 const char *free_busy;
947 const char *cal_states[] = {_("Free"),
948 _("Tentative"),
949 _("Busy"),
950 _("Out of office"),
951 _("No data")};
953 if (buddy->cal_granularity != 15) {
954 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy->cal_granularity);
955 return NULL;
958 /* to lazy load if needed */
959 free_busy = sipe_cal_get_free_busy(buddy);
960 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy ? free_busy : "");
962 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
963 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: no calendar data, exiting");
964 return NULL;
967 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
968 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
970 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
971 if (current_cal_state == SIPE_CAL_NO_DATA) {
972 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: calendar is undefined for present moment, exiting.");
973 return NULL;
976 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
978 SIPE_DEBUG_INFO_NOFORMAT("\n* Calendar *");
979 if (buddy->cal_working_hours) {
980 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
982 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy->cal_working_hours, now));
983 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
984 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "");
985 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
986 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "");
987 SIPE_DEBUG_INFO("Remote now time : %s",
988 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
989 SIPE_DEBUG_INFO("Remote start time : %s",
990 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "");
991 SIPE_DEBUG_INFO("Remote end time : %s",
992 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "");
993 SIPE_DEBUG_INFO("Rem. next_start time: %s",
994 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "");
995 SIPE_DEBUG_INFO("Remote switch time : %s",
996 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "");
997 } else {
998 SIPE_DEBUG_INFO("Local now time : %s",
999 asctime(localtime(&now)));
1000 SIPE_DEBUG_INFO("Local switch time : %s",
1001 IS(switch_time) ? asctime(localtime(&switch_time)) : "");
1003 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
1004 SIPE_DEBUG_INFO("current cal state : %s", cal_states[current_cal_state]);
1005 SIPE_DEBUG_INFO("switch cal state : %s", cal_states[to_state] );
1007 /* Calendar: string calculations */
1010 ALGORITHM (don't delete)
1011 (c)2009,2010 pier11 <pier11@operamail.com>
1013 SOD = Start of Work Day
1014 EOD = End of Work Day
1015 NSOD = Start of tomorrow's Work Day
1016 SW = Calendar status switch time
1018 if current_cal_state == Free
1019 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
1020 else
1021 until = SW
1023 if (!until && (cal_period_end > now + 8H))
1024 until = cal_period_end
1026 if (!until)
1027 return "Currently %", current_cal_state
1029 if (until - now > 8H)
1030 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
1031 return "Outside of working hours for next 8 hours"
1032 else
1033 return "%s for next 8 hours", current_cal_state
1035 if (current_cal_state == Free)
1036 if (work_hours && until !in work_hours(now))
1037 "Not working"
1038 else
1039 "%s", current_cal_state
1040 " until %.2d:%.2d", until
1041 else
1042 "Currently %", current_cal_state
1043 if (work_hours && until !in work_hours(until))
1044 ". Outside of working hours at at %.2d:%.2d", until
1045 else
1046 ". %s at %.2d:%.2d", to_state, until
1049 if (current_cal_state < 1) { /* Free */
1050 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
1051 } else {
1052 until = switch_time;
1055 if (!IS(until) && (cal_end - now > 8*60*60))
1056 until = cal_end;
1058 if (!IS(until)) {
1059 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1062 if (until - now > 8*60*60) {
1063 /* Free & outside work hours */
1064 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1065 return g_strdup(_("Outside of working hours for next 8 hours"));
1066 } else {
1067 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
1071 if (current_cal_state < 1) { /* Free */
1072 const char *tmp;
1073 struct tm *until_tm = localtime(&until);
1075 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1076 tmp = _("Not working");
1077 } else {
1078 tmp = cal_states[current_cal_state];
1080 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
1081 } else { /* Tentative or Busy or OOF */
1082 char *tmp;
1083 char *res;
1084 struct tm *until_tm = localtime(&until);
1086 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1087 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
1088 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
1089 tmp, until_tm->tm_hour, until_tm->tm_min);
1090 g_free(tmp);
1091 return res;
1092 } else {
1093 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
1094 g_free(tmp);
1095 return res;
1098 /* End of - Calendar: string calculations */
1101 #define UPDATE_CALENDAR_INTERVAL 30*60 /* 30 min */
1103 void sipe_core_update_calendar(struct sipe_core_public *sipe_public)
1105 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: started.");
1107 /* Do in parallel.
1108 * If failed, the branch will be disabled for subsequent calls.
1109 * Can't rely that user turned the functionality on in account settings.
1111 sipe_ews_update_calendar(SIPE_CORE_PRIVATE);
1112 #ifdef _WIN32
1113 /* @TODO: UNIX integration missing */
1114 sipe_domino_update_calendar(SIPE_CORE_PRIVATE);
1115 #endif
1117 /* schedule repeat */
1118 sipe_schedule_seconds(SIPE_CORE_PRIVATE,
1119 "<+update-calendar>",
1120 NULL,
1121 UPDATE_CALENDAR_INTERVAL,
1122 (sipe_schedule_action)sipe_core_update_calendar,
1123 NULL);
1125 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: finished.");
1128 void sipe_cal_presence_publish(struct sipe_core_private *sipe_private,
1129 gboolean do_publish_calendar)
1131 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1132 if (do_publish_calendar)
1133 sipe_ocs2007_presence_publish(sipe_private, NULL);
1134 else
1135 sipe_ocs2007_category_publish(sipe_private);
1136 } else {
1137 /* sipe.h */
1138 send_presence_soap(sipe_private, do_publish_calendar);
1142 void sipe_cal_delayed_calendar_update(struct sipe_core_private *sipe_private)
1144 #define UPDATE_CALENDAR_DELAY 1*60 /* 1 min */
1146 sipe_schedule_seconds(sipe_private,
1147 "<+update-calendar>",
1148 NULL,
1149 UPDATE_CALENDAR_DELAY,
1150 (sipe_schedule_action) sipe_core_update_calendar,
1151 NULL);
1155 Local Variables:
1156 mode: c
1157 c-file-style: "bsd"
1158 indent-tabs-mode: t
1159 tab-width: 8
1160 End: