Fix #193: Pidgin Status changes stop working (III)
[siplcs.git] / src / core / sipe-cal.c
blob8ccc8afe51a67e6d2c44dd3babc212e6f3b71f09
1 /**
2 * @file sipe-cal.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 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 g_free(cal->email);
173 g_free(cal->legacy_dn);
174 g_free(cal->auth_domain);
175 g_free(cal->auth_user);
176 g_free(cal->password);
177 g_free(cal->as_url);
178 g_free(cal->oof_url);
179 g_free(cal->oab_url);
180 g_free(cal->domino_url);
181 g_free(cal->oof_state);
182 g_free(cal->oof_note);
183 g_free(cal->free_busy);
184 g_free(cal->working_hours_xml_str);
186 sipe_cal_events_free(cal->cal_events);
188 if (cal->request)
189 sipe_http_request_cancel(cal->request);
190 sipe_http_session_close(cal->session);
192 g_free(cal);
195 gboolean
196 sipe_cal_calendar_init(struct sipe_core_private *sipe_private,
197 gboolean *has_url)
199 if (!sipe_private->calendar) {
200 struct sipe_calendar *cal;
201 const char *value;
203 sipe_private->calendar = cal = g_new0(struct sipe_calendar, 1);
204 cal->sipe_private = sipe_private;
206 cal->email = g_strdup(sipe_private->email);
208 /* user specified a service URL? */
209 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_URL);
210 if (has_url) *has_url = !is_empty(value);
211 if (!is_empty(value)) {
212 cal->as_url = g_strdup(value);
213 cal->oof_url = g_strdup(value);
214 cal->domino_url = g_strdup(value);
217 /* user specified email login? */
218 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_LOGIN);
219 if (!is_empty(value)) {
220 /* Allowed domain-account separators are / or \ */
221 gchar **domain_user = g_strsplit_set(value, "/\\", 2);
222 gboolean has_domain = domain_user[1] != NULL;
224 cal->auth_domain = has_domain ? g_strdup(domain_user[0]) : NULL;
225 cal->auth_user = g_strdup(domain_user[has_domain ? 1 : 0]);
226 cal->password = g_strdup(sipe_backend_setting(SIPE_CORE_PUBLIC,
227 SIPE_SETTING_EMAIL_PASSWORD));
228 g_strfreev(domain_user);
230 return TRUE;
232 return FALSE;
236 char *
237 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
239 GString* str = g_string_new(NULL);
240 const char *status = "";
242 switch(cal_event->cal_status) {
243 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
244 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
245 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
246 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
247 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
250 g_string_append_printf(str, "\t%s: %s", "start_time",
251 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
252 g_string_append_printf(str, "\t%s: %s", "end_time ",
253 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
254 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
255 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
256 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
257 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
259 return g_string_free(str, FALSE);
262 char *
263 sipe_cal_event_hash(struct sipe_cal_event* event)
265 /* no end_time as it dos not get published */
266 /* no cal_status as it can change on publication */
267 return g_strdup_printf("<%d><%s><%s><%d>",
268 (int)event->start_time,
269 event->subject ? event->subject : "",
270 event->location ? event->location : "",
271 event->is_meeting);
274 #define ENVIRONMENT_TIMEZONE "TZ"
276 static gchar *
277 sipe_switch_tz(const char *tz)
279 gchar *tz_orig;
281 tz_orig = g_strdup(g_getenv(ENVIRONMENT_TIMEZONE));
282 g_setenv(ENVIRONMENT_TIMEZONE, tz, TRUE);
283 tzset();
284 return(tz_orig);
287 static void
288 sipe_reset_tz(gchar *tz_orig)
290 if (tz_orig) {
291 g_setenv(ENVIRONMENT_TIMEZONE, tz_orig, TRUE);
292 g_free(tz_orig);
293 } else {
294 g_unsetenv(ENVIRONMENT_TIMEZONE);
296 tzset();
300 * Converts struct tm to Epoch time_t considering timezone.
302 * @param tz as defined for TZ environment variable.
304 * Reference: see timegm(3) - Linux man page
306 time_t
307 sipe_mktime_tz(struct tm *tm,
308 const char* tz)
310 time_t ret;
311 gchar *tz_orig;
313 tz_orig = sipe_switch_tz(tz);
314 ret = mktime(tm);
315 sipe_reset_tz(tz_orig);
317 return ret;
321 * Converts Epoch time_t to struct tm considering timezone.
323 * @param tz as defined for TZ environment variable.
325 * Reference: see timegm(3) - Linux man page
327 static struct tm *
328 sipe_localtime_tz(const time_t *time,
329 const char* tz)
331 struct tm *ret;
332 gchar *tz_orig;
334 tz_orig = sipe_switch_tz(tz);
335 ret = localtime(time);
336 sipe_reset_tz(tz_orig);
338 return ret;
341 void
342 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
344 if (!wh) return;
346 g_free(wh->std.time);
347 g_free(wh->std.day_of_week);
348 g_free(wh->std.year);
350 g_free(wh->dst.time);
351 g_free(wh->dst.day_of_week);
352 g_free(wh->dst.year);
354 g_free(wh->days_of_week);
355 g_free(wh->tz);
356 g_free(wh->tz_std);
357 g_free(wh->tz_dst);
358 g_free(wh);
362 * Returns time_t of daylight savings time start/end
363 * in the provided timezone or otherwise
364 * (time_t)-1 if no daylight savings time.
366 static time_t
367 sipe_cal_get_std_dst_time(time_t now,
368 int bias,
369 struct sipe_cal_std_dst* std_dst,
370 struct sipe_cal_std_dst* dst_std)
372 struct tm switch_tm;
373 time_t res = TIME_NULL;
374 struct tm *gm_now_tm;
375 gchar **time_arr;
377 if (std_dst->month == 0) return TIME_NULL;
379 gm_now_tm = gmtime(&now);
380 time_arr = g_strsplit(std_dst->time, ":", 0);
382 switch_tm.tm_sec = atoi(time_arr[2]);
383 switch_tm.tm_min = atoi(time_arr[1]);
384 switch_tm.tm_hour = atoi(time_arr[0]);
385 g_strfreev(time_arr);
386 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
387 switch_tm.tm_mon = std_dst->month - 1;
388 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
389 switch_tm.tm_isdst = 0;
390 /* to set tm_wday */
391 res = sipe_mktime_tz(&switch_tm, "UTC");
393 /* if not dynamic, calculate right tm_mday */
394 if (!std_dst->year) {
395 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
396 int needed_month;
397 /* get first desired wday in the month */
398 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
399 switch_tm.tm_mday = 1 + delta;
400 /* try nth order */
401 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
402 needed_month = switch_tm.tm_mon;
403 /* to set settle date if ahead of allowed month dates */
404 res = sipe_mktime_tz(&switch_tm, "UTC");
405 if (needed_month != switch_tm.tm_mon) {
406 /* moving 1 week back to stay within required month */
407 switch_tm.tm_mday -= 7;
408 /* to fix date again */
409 res = sipe_mktime_tz(&switch_tm, "UTC");
412 /* note: bias is taken from "switch to" structure */
413 return res + (bias + dst_std->bias)*60;
416 static void
417 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
418 struct sipe_cal_std_dst *std_dst)
420 const sipe_xml *node;
421 gchar *tmp;
423 if (!xn_std_dst_time) return;
424 if (!std_dst) return;
426 <StandardTime>
427 <Bias>0</Bias>
428 <Time>02:00:00</Time>
429 <DayOrder>1</DayOrder>
430 <Month>11</Month>
431 <Year>2009</Year>
432 <DayOfWeek>Sunday</DayOfWeek>
433 </StandardTime>
436 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
437 std_dst->bias = atoi(tmp = sipe_xml_data(node));
438 g_free(tmp);
441 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
442 std_dst->time = sipe_xml_data(node);
445 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
446 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
447 g_free(tmp);
450 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
451 std_dst->month = atoi(tmp = sipe_xml_data(node));
452 g_free(tmp);
455 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
456 std_dst->day_of_week = sipe_xml_data(node);
459 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
460 std_dst->year = sipe_xml_data(node);
464 void
465 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
466 struct sipe_buddy *buddy)
468 const sipe_xml *xn_bias;
469 const sipe_xml *xn_timezone;
470 const sipe_xml *xn_working_period;
471 const sipe_xml *xn_standard_time;
472 const sipe_xml *xn_daylight_time;
473 gchar *tmp;
474 time_t now = time(NULL);
475 struct sipe_cal_std_dst* std;
476 struct sipe_cal_std_dst* dst;
478 if (!xn_working_hours) return;
480 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
481 <TimeZone>
482 <Bias>480</Bias>
484 </TimeZone>
485 <WorkingPeriodArray>
486 <WorkingPeriod>
487 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
488 <StartTimeInMinutes>600</StartTimeInMinutes>
489 <EndTimeInMinutes>1140</EndTimeInMinutes>
490 </WorkingPeriod>
491 </WorkingPeriodArray>
492 </WorkingHours>
494 sipe_cal_free_working_hours(buddy->cal_working_hours);
495 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
497 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
498 xn_bias = sipe_xml_child(xn_timezone, "Bias");
499 if (xn_bias) {
500 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
501 g_free(tmp);
504 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
505 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
507 std = &((*buddy->cal_working_hours).std);
508 dst = &((*buddy->cal_working_hours).dst);
509 sipe_cal_parse_std_dst(xn_standard_time, std);
510 sipe_cal_parse_std_dst(xn_daylight_time, dst);
512 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
513 if (xn_working_period) {
514 /* NOTE: this can be NULL! */
515 buddy->cal_working_hours->days_of_week =
516 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
518 buddy->cal_working_hours->start_time =
519 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
520 g_free(tmp);
522 buddy->cal_working_hours->end_time =
523 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
524 g_free(tmp);
527 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
528 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
530 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
531 buddy->cal_working_hours->tz =
532 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
533 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
534 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
536 buddy->cal_working_hours->dst.month,
537 buddy->cal_working_hours->dst.day_order,
538 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
539 buddy->cal_working_hours->dst.time,
541 buddy->cal_working_hours->std.month,
542 buddy->cal_working_hours->std.day_order,
543 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
544 buddy->cal_working_hours->std.time
546 /* TST8 */
547 buddy->cal_working_hours->tz_std =
548 g_strdup_printf("TST%d",
549 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
550 /* TDT7 */
551 buddy->cal_working_hours->tz_dst =
552 g_strdup_printf("TDT%d",
553 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
556 struct sipe_cal_event*
557 sipe_cal_get_event(GSList *cal_events,
558 time_t time_in_question)
560 GSList *entry = cal_events;
561 struct sipe_cal_event* cal_event;
562 struct sipe_cal_event* res = NULL;
564 if (!cal_events || !IS(time_in_question)) return NULL;
566 while (entry) {
567 cal_event = entry->data;
568 /* event is in the past or in the future */
569 if (cal_event->start_time > time_in_question ||
570 cal_event->end_time <= time_in_question)
572 entry = entry->next;
573 continue;
576 if (!res) {
577 res = cal_event;
578 } else {
579 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
580 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
581 if (res_status < cal_status) {
582 res = cal_event;
585 entry = entry->next;
587 return res;
590 static int
591 sipe_cal_get_status0(const gchar *free_busy,
592 time_t cal_start,
593 int granularity,
594 time_t time_in_question,
595 int *index)
597 int res = SIPE_CAL_NO_DATA;
598 int shift;
599 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
601 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
603 shift = (time_in_question - cal_start) / (granularity*60);
604 if (index) {
605 *index = shift;
608 res = free_busy[shift] - '0';
610 return res;
614 * Returns time when current calendar state started
616 static time_t
617 sipe_cal_get_since_time(const gchar *free_busy,
618 time_t calStart,
619 int granularity,
620 int index,
621 int current_state)
623 int i;
625 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
627 for (i = index; i >= 0; i--) {
628 int temp_status = free_busy[i] - '0';
630 if (current_state != temp_status) {
631 return calStart + (i + 1)*granularity*60;
634 if (i == 0) return calStart;
637 return 0;
639 static char*
640 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
643 sipe_cal_get_status(struct sipe_buddy *buddy,
644 time_t time_in_question,
645 time_t *since)
647 time_t cal_start;
648 const char* free_busy;
649 int ret = SIPE_CAL_NO_DATA;
650 time_t state_since;
651 int index = -1;
653 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
654 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
655 buddy ? (buddy->name ? buddy->name : "") : "");
656 return SIPE_CAL_NO_DATA;
659 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
660 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy->name);
661 return SIPE_CAL_NO_DATA;
663 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy);
665 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
667 ret = sipe_cal_get_status0(free_busy,
668 cal_start,
669 buddy->cal_granularity,
670 time_in_question,
671 &index);
672 state_since = sipe_cal_get_since_time(free_busy,
673 cal_start,
674 buddy->cal_granularity,
675 index,
676 ret);
678 if (since) *since = state_since;
679 return ret;
682 static time_t
683 sipe_cal_get_switch_time(const gchar *free_busy,
684 time_t calStart,
685 int granularity,
686 int index,
687 int current_state,
688 int *to_state)
690 size_t i;
691 time_t ret = TIME_NULL;
693 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
694 *to_state = SIPE_CAL_NO_DATA;
695 return ret;
698 for (i = index + 1; i < strlen(free_busy); i++) {
699 int temp_status = free_busy[i] - '0';
701 if (current_state != temp_status) {
702 *to_state = temp_status;
703 return calStart + i*granularity*60;
707 return ret;
710 static const char*
711 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
712 time_t time_in_question)
714 time_t dst_switch_time = (*wh).dst.switch_time;
715 time_t std_switch_time = (*wh).std.switch_time;
716 gboolean is_dst = FALSE;
718 /* No daylight savings */
719 if (dst_switch_time == TIME_NULL) {
720 return wh->tz_std;
723 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
724 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
725 is_dst = TRUE;
727 } else { /* South hemisphere - Australia */
728 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
729 is_dst = TRUE;
733 if (is_dst) {
734 return wh->tz_dst;
735 } else {
736 return wh->tz_std;
740 static time_t
741 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
742 const int shift_minutes,
743 const char *tz)
745 sample_today_tm->tm_sec = 0;
746 sample_today_tm->tm_min = shift_minutes % 60;
747 sample_today_tm->tm_hour = shift_minutes / 60;
749 return sipe_mktime_tz(sample_today_tm, tz);
753 * Returns work day start and end in Epoch time
754 * considering the initial values are provided
755 * in contact's local time zone.
757 static void
758 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
759 time_t *start,
760 time_t *end,
761 time_t *next_start)
763 time_t now = time(NULL);
764 const char *tz = sipe_cal_get_tz(wh, now);
765 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
767 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday]))) {
768 /* not a work day */
769 *start = TIME_NULL;
770 *end = TIME_NULL;
771 *next_start = TIME_NULL;
772 return;
775 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
777 if (now < *end) {
778 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
779 *next_start = TIME_NULL;
780 } else { /* calculate start of tomorrow's work day if any */
781 time_t tom = now + 24*60*60;
782 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
784 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday]))) {
785 /* not a work day */
786 *next_start = TIME_NULL;
789 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
790 *start = TIME_NULL;
794 static int
795 sipe_cal_is_in_work_hours(const time_t time_in_question,
796 const time_t start,
797 const time_t end)
799 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
803 * Returns time closest to now. Choses only from times ahead of now.
804 * Returns TIME_NULL otherwise.
806 static time_t
807 sipe_cal_get_until(const time_t now,
808 const time_t switch_time,
809 const time_t start,
810 const time_t end,
811 const time_t next_start)
813 time_t ret = TIME_NULL;
814 int min_diff = now - ret;
816 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
817 min_diff = switch_time - now;
818 ret = switch_time;
820 if (IS(start) && start > now && (start - now) < min_diff) {
821 min_diff = start - now;
822 ret = start;
824 if (IS(end) && end > now && (end - now) < min_diff) {
825 min_diff = end - now;
826 ret = end;
828 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
829 min_diff = next_start - now;
830 ret = next_start;
832 return ret;
835 static char*
836 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
838 /* do lazy decode if necessary */
839 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
840 gsize cal_dec64_len;
841 guchar *cal_dec64;
842 gsize i;
843 int j = 0;
845 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
847 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
849 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
850 00, Free (Fr)
851 01, Tentative (Te)
852 10, Busy (Bu)
853 11, Out of facility (Oo)
855 http://msdn.microsoft.com/en-us/library/aa566048.aspx
856 0 Free
857 1 Tentative
858 2 Busy
859 3 Out of Office (OOF)
860 4 No data
862 for (i = 0; i < cal_dec64_len; i++) {
863 #define TWO_BIT_MASK 0x03
864 char tmp = cal_dec64[i];
865 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
866 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
867 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
868 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
870 buddy->cal_free_busy[j++] = '\0';
871 g_free(cal_dec64);
874 return buddy->cal_free_busy;
877 char *
878 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
880 guint i = 0;
881 guint j = 0;
882 guint shift_factor = 0;
883 guint len, res_len;
884 guchar *res;
885 gchar *res_base64;
887 if (!freebusy_hex) return NULL;
889 len = strlen(freebusy_hex);
890 res_len = len / 4 + 1;
891 res = g_malloc0(res_len);
892 while (i < len) {
893 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
894 shift_factor += 2;
895 if (shift_factor == 8) {
896 shift_factor = 0;
897 j++;
901 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
902 g_free(res);
903 return res_base64;
906 char *
907 sipe_cal_get_description(struct sipe_buddy *buddy)
909 time_t cal_start;
910 time_t cal_end;
911 int current_cal_state;
912 time_t now = time(NULL);
913 time_t start = TIME_NULL;
914 time_t end = TIME_NULL;
915 time_t next_start = TIME_NULL;
916 time_t switch_time;
917 int to_state = SIPE_CAL_NO_DATA;
918 time_t until = TIME_NULL;
919 int index = 0;
920 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
921 const char *free_busy;
922 const char *cal_states[] = {_("Free"),
923 _("Tentative"),
924 _("Busy"),
925 _("Out of office"),
926 _("No data")};
928 if (buddy->cal_granularity != 15) {
929 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy->cal_granularity);
930 return NULL;
933 /* to lazy load if needed */
934 free_busy = sipe_cal_get_free_busy(buddy);
935 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy ? free_busy : "");
937 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
938 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: no calendar data, exiting");
939 return NULL;
942 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
943 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
945 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
946 if (current_cal_state == SIPE_CAL_NO_DATA) {
947 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: calendar is undefined for present moment, exiting.");
948 return NULL;
951 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
953 SIPE_DEBUG_INFO_NOFORMAT("\n* Calendar *");
954 if (buddy->cal_working_hours) {
955 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
957 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy->cal_working_hours, now));
958 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
959 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "");
960 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
961 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "");
962 SIPE_DEBUG_INFO("Remote now time : %s",
963 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
964 SIPE_DEBUG_INFO("Remote start time : %s",
965 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "");
966 SIPE_DEBUG_INFO("Remote end time : %s",
967 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "");
968 SIPE_DEBUG_INFO("Rem. next_start time: %s",
969 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "");
970 SIPE_DEBUG_INFO("Remote switch time : %s",
971 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "");
972 } else {
973 SIPE_DEBUG_INFO("Local now time : %s",
974 asctime(localtime(&now)));
975 SIPE_DEBUG_INFO("Local switch time : %s",
976 IS(switch_time) ? asctime(localtime(&switch_time)) : "");
978 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
979 SIPE_DEBUG_INFO("current cal state : %s", cal_states[current_cal_state]);
980 SIPE_DEBUG_INFO("switch cal state : %s", cal_states[to_state] );
982 /* Calendar: string calculations */
985 ALGORITHM (don't delete)
986 (c)2009,2010 pier11 <pier11@operamail.com>
988 SOD = Start of Work Day
989 EOD = End of Work Day
990 NSOD = Start of tomorrow's Work Day
991 SW = Calendar status switch time
993 if current_cal_state == Free
994 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
995 else
996 until = SW
998 if (!until && (cal_period_end > now + 8H))
999 until = cal_period_end
1001 if (!until)
1002 return "Currently %", current_cal_state
1004 if (until - now > 8H)
1005 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
1006 return "Outside of working hours for next 8 hours"
1007 else
1008 return "%s for next 8 hours", current_cal_state
1010 if (current_cal_state == Free)
1011 if (work_hours && until !in work_hours(now))
1012 "Not working"
1013 else
1014 "%s", current_cal_state
1015 " until %.2d:%.2d", until
1016 else
1017 "Currently %", current_cal_state
1018 if (work_hours && until !in work_hours(until))
1019 ". Outside of working hours at at %.2d:%.2d", until
1020 else
1021 ". %s at %.2d:%.2d", to_state, until
1024 if (current_cal_state < 1) { /* Free */
1025 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
1026 } else {
1027 until = switch_time;
1030 if (!IS(until) && (cal_end - now > 8*60*60))
1031 until = cal_end;
1033 if (!IS(until)) {
1034 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1037 if (until - now > 8*60*60) {
1038 /* Free & outside work hours */
1039 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1040 return g_strdup(_("Outside of working hours for next 8 hours"));
1041 } else {
1042 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
1046 if (current_cal_state < 1) { /* Free */
1047 const char *tmp;
1048 struct tm *until_tm = localtime(&until);
1050 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1051 tmp = _("Not working");
1052 } else {
1053 tmp = cal_states[current_cal_state];
1055 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
1056 } else { /* Tentative or Busy or OOF */
1057 char *tmp;
1058 char *res;
1059 struct tm *until_tm = localtime(&until);
1061 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1062 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
1063 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
1064 tmp, until_tm->tm_hour, until_tm->tm_min);
1065 g_free(tmp);
1066 return res;
1067 } else {
1068 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
1069 g_free(tmp);
1070 return res;
1073 /* End of - Calendar: string calculations */
1076 #define UPDATE_CALENDAR_INTERVAL 30*60 /* 30 min */
1078 void sipe_core_update_calendar(struct sipe_core_public *sipe_public)
1080 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: started.");
1082 /* Do in parallel.
1083 * If failed, the branch will be disabled for subsequent calls.
1084 * Can't rely that user turned the functionality on in account settings.
1086 sipe_ews_update_calendar(SIPE_CORE_PRIVATE);
1087 #ifdef _WIN32
1088 /* @TODO: UNIX integration missing */
1089 sipe_domino_update_calendar(SIPE_CORE_PRIVATE);
1090 #endif
1092 /* schedule repeat */
1093 sipe_schedule_seconds(SIPE_CORE_PRIVATE,
1094 "<+update-calendar>",
1095 NULL,
1096 UPDATE_CALENDAR_INTERVAL,
1097 (sipe_schedule_action)sipe_core_update_calendar,
1098 NULL);
1100 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: finished.");
1103 void sipe_cal_presence_publish(struct sipe_core_private *sipe_private,
1104 gboolean do_publish_calendar)
1106 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1107 if (do_publish_calendar)
1108 sipe_ocs2007_presence_publish(sipe_private, NULL);
1109 else
1110 sipe_ocs2007_category_publish(sipe_private);
1111 } else {
1112 sipe_ocs2005_presence_publish(sipe_private,
1113 do_publish_calendar);
1117 void sipe_cal_delayed_calendar_update(struct sipe_core_private *sipe_private)
1119 #define UPDATE_CALENDAR_DELAY 1*60 /* 1 min */
1121 /* only start periodic calendar updating if user hasn't disabled it */
1122 if (!SIPE_CORE_PUBLIC_FLAG_IS(DONT_PUBLISH))
1123 sipe_schedule_seconds(sipe_private,
1124 "<+update-calendar>",
1125 NULL,
1126 UPDATE_CALENDAR_DELAY,
1127 (sipe_schedule_action) sipe_core_update_calendar,
1128 NULL);
1131 void sipe_cal_http_authentication(struct sipe_calendar *cal)
1133 if (cal->auth_user) {
1134 sipe_http_request_authentication(cal->request,
1135 cal->auth_domain,
1136 cal->auth_user,
1137 cal->password);
1142 Local Variables:
1143 mode: c
1144 c-file-style: "bsd"
1145 indent-tabs-mode: t
1146 tab-width: 8
1147 End: