core cleanup: move out email & user_states field
[siplcs.git] / src / core / sipe-cal.c
blob3fe6d4ec6801ee1f9f91425e6d39309f2b95bfe0
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-ocs2005.h"
43 #include "sipe-ocs2007.h"
44 #include "sipe-schedule.h"
45 #include "sipe-utils.h"
46 #include "sipe-xml.h"
47 #include "sipe.h"
49 /* Calendar backends */
50 #ifdef _WIN32
51 #include "sipe-domino.h"
52 #endif
53 #include "sipe-ews.h"
55 #define TIME_NULL (time_t)-1
56 #define IS(time) (time != TIME_NULL)
59 http://msdn.microsoft.com/en-us/library/aa565001.aspx
61 <?xml version="1.0"?>
62 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
63 <TimeZone>
64 <Bias>480</Bias>
65 <StandardTime>
66 <Bias>0</Bias>
67 <Time>02:00:00</Time>
68 <DayOrder>1</DayOrder>
69 <Month>11</Month>
70 <DayOfWeek>Sunday</DayOfWeek>
71 </StandardTime>
72 <DaylightTime>
73 <Bias>-60</Bias>
74 <Time>02:00:00</Time>
75 <DayOrder>2</DayOrder>
76 <Month>3</Month>
77 <DayOfWeek>Sunday</DayOfWeek>
78 </DaylightTime>
79 </TimeZone>
80 <WorkingPeriodArray>
81 <WorkingPeriod>
82 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
83 <StartTimeInMinutes>600</StartTimeInMinutes>
84 <EndTimeInMinutes>1140</EndTimeInMinutes>
85 </WorkingPeriod>
86 </WorkingPeriodArray>
87 </WorkingHours>
89 Desc:
90 <StandardTime>
91 <Bias>int</Bias>
92 <Time>string</Time>
93 <DayOrder>short</DayOrder>
94 <Month>short</Month>
95 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
96 <Year>string</Year>
97 </StandardTime>
100 struct sipe_cal_std_dst {
101 int bias; /* Ex.: -60 */
102 gchar *time; /* hh:mm:ss, 02:00:00 */
103 int day_order; /* 1..5 */
104 int month; /* 1..12 */
105 gchar *day_of_week; /* Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday */
106 gchar *year; /* YYYY */
108 time_t switch_time;
111 struct sipe_cal_working_hours {
112 int bias; /* Ex.: 480 */
113 struct sipe_cal_std_dst std; /* StandardTime */
114 struct sipe_cal_std_dst dst; /* DaylightTime */
115 gchar *days_of_week; /* Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday separated by space */
116 int start_time; /* 0...1440 */
117 int end_time; /* 0...1440 */
119 gchar *tz; /* aggregated timezone string as in TZ environment variable.
120 Ex.: TST+8TDT+7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
121 /** separate simple strings for Windows platform as the proper TZ does not work there.
122 * anyway, dynamic timezones would't work with just TZ
124 gchar *tz_std; /* Ex.: TST8 */
125 gchar *tz_dst; /* Ex.: TDT7 */
128 /* not for translation, a part of XML Schema definitions */
129 static const char *wday_names[] = {"Sunday",
130 "Monday",
131 "Tuesday",
132 "Wednesday",
133 "Thursday",
134 "Friday",
135 "Saturday"};
136 static int
137 sipe_cal_get_wday(char *wday_name)
139 int i;
141 if (!wday_name) return -1;
143 for (i = 0; i < 7; i++) {
144 if (sipe_strequal(wday_names[i], wday_name)) {
145 return i;
149 return -1;
152 void
153 sipe_cal_event_free(struct sipe_cal_event* cal_event)
155 if (!cal_event) return;
157 g_free(cal_event->subject);
158 g_free(cal_event->location);
159 g_free(cal_event);
162 void
163 sipe_cal_events_free(GSList *cal_events)
165 GSList *entry = cal_events;
167 if (!cal_events) return;
169 while (entry) {
170 struct sipe_cal_event *cal_event = entry->data;
171 sipe_cal_event_free(cal_event);
172 entry = entry->next;
175 g_slist_free(cal_events);
178 void
179 sipe_cal_calendar_free(struct sipe_calendar *cal)
181 g_free(cal->email);
182 g_free(cal->legacy_dn);
183 if (cal->auth) {
184 g_free(cal->auth->domain);
185 g_free(cal->auth->user);
186 g_free(cal->auth->password);
188 g_free(cal->auth);
189 g_free(cal->as_url);
190 g_free(cal->oof_url);
191 g_free(cal->oab_url);
192 g_free(cal->domino_url);
193 g_free(cal->oof_state);
194 g_free(cal->oof_note);
195 g_free(cal->free_busy);
196 g_free(cal->working_hours_xml_str);
198 sipe_cal_events_free(cal->cal_events);
200 if (cal->http_conn) {
201 http_conn_free(cal->http_conn);
204 if (cal->http_session) {
205 http_conn_session_free(cal->http_session);
208 g_free(cal);
211 gboolean
212 sipe_cal_calendar_init(struct sipe_core_private *sipe_private,
213 gboolean *has_url)
215 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
216 struct sipe_calendar* cal = sipe_private->calendar;
217 if (!cal) {
218 const char *value;
220 cal = g_new0(struct sipe_calendar, 1);
221 cal->sipe_private = sipe_private;
223 cal->email = g_strdup(sipe_private->email);
225 /* user specified a service URL? */
226 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_URL);
227 if (has_url) *has_url = !is_empty(value);
228 if (!is_empty(value)) {
229 cal->as_url = g_strdup(value);
230 cal->oof_url = g_strdup(value);
231 cal->domino_url = g_strdup(value);
234 cal->auth = g_new0(HttpConnAuth, 1);
235 cal->auth->use_negotiate = SIPE_CORE_PUBLIC_FLAG_IS(KRB5);
237 /* user specified email login? */
238 value = sipe_backend_setting(SIPE_CORE_PUBLIC, SIPE_SETTING_EMAIL_LOGIN);
239 if (!is_empty(value)) {
241 /* user specified email login domain? */
242 const char *tmp = strstr(value, "\\");
243 if (tmp) {
244 cal->auth->domain = g_strndup(value, tmp - value);
245 cal->auth->user = g_strdup(tmp + 1);
246 } else {
247 cal->auth->user = g_strdup(value);
249 cal->auth->password = g_strdup(sipe_backend_setting(SIPE_CORE_PUBLIC,
250 SIPE_SETTING_EMAIL_PASSWORD));
252 } else {
253 /* re-use SIPE credentials */
254 cal->auth->domain = g_strdup(sip->authdomain);
255 cal->auth->user = g_strdup(sip->authuser);
256 cal->auth->password = g_strdup(sip->password);
258 return TRUE;
260 return FALSE;
264 char *
265 sipe_cal_event_describe(struct sipe_cal_event* cal_event)
267 GString* str = g_string_new(NULL);
268 const char *status = "";
270 switch(cal_event->cal_status) {
271 case SIPE_CAL_FREE: status = "SIPE_CAL_FREE"; break;
272 case SIPE_CAL_TENTATIVE: status = "SIPE_CAL_TENTATIVE"; break;
273 case SIPE_CAL_BUSY: status = "SIPE_CAL_BUSY"; break;
274 case SIPE_CAL_OOF: status = "SIPE_CAL_OOF"; break;
275 case SIPE_CAL_NO_DATA: status = "SIPE_CAL_NO_DATA"; break;
278 g_string_append_printf(str, "\t%s: %s", "start_time",
279 IS(cal_event->start_time) ? asctime(localtime(&cal_event->start_time)) : "\n");
280 g_string_append_printf(str, "\t%s: %s", "end_time ",
281 IS(cal_event->end_time) ? asctime(localtime(&cal_event->end_time)) : "\n");
282 g_string_append_printf(str, "\t%s: %s\n", "cal_status", status);
283 g_string_append_printf(str, "\t%s: %s\n", "subject ", cal_event->subject ? cal_event->subject : "");
284 g_string_append_printf(str, "\t%s: %s\n", "location ", cal_event->location ? cal_event->location : "");
285 g_string_append_printf(str, "\t%s: %s\n", "is_meeting", cal_event->is_meeting ? "TRUE" : "FALSE");
287 return g_string_free(str, FALSE);
290 char *
291 sipe_cal_event_hash(struct sipe_cal_event* event)
293 /* no end_time as it dos not get published */
294 /* no cal_status as it can change on publication */
295 return g_strdup_printf("<%d><%s><%s><%d>",
296 (int)event->start_time,
297 event->subject ? event->subject : "",
298 event->location ? event->location : "",
299 event->is_meeting);
302 #define ENVIRONMENT_TIMEZONE "TZ"
304 static gchar *
305 sipe_switch_tz(const char *tz)
307 gchar *tz_orig;
309 tz_orig = g_strdup(g_getenv(ENVIRONMENT_TIMEZONE));
310 g_setenv(ENVIRONMENT_TIMEZONE, tz, TRUE);
311 tzset();
312 return(tz_orig);
315 static void
316 sipe_reset_tz(gchar *tz_orig)
318 if (tz_orig) {
319 g_setenv(ENVIRONMENT_TIMEZONE, tz_orig, TRUE);
320 g_free(tz_orig);
321 } else {
322 g_unsetenv(ENVIRONMENT_TIMEZONE);
324 tzset();
328 * Converts struct tm to Epoch time_t considering timezone.
330 * @param tz as defined for TZ environment variable.
332 * Reference: see timegm(3) - Linux man page
334 time_t
335 sipe_mktime_tz(struct tm *tm,
336 const char* tz)
338 time_t ret;
339 gchar *tz_orig;
341 tz_orig = sipe_switch_tz(tz);
342 ret = mktime(tm);
343 sipe_reset_tz(tz_orig);
345 return ret;
349 * Converts Epoch time_t to struct tm considering timezone.
351 * @param tz as defined for TZ environment variable.
353 * Reference: see timegm(3) - Linux man page
355 static struct tm *
356 sipe_localtime_tz(const time_t *time,
357 const char* tz)
359 struct tm *ret;
360 gchar *tz_orig;
362 tz_orig = sipe_switch_tz(tz);
363 ret = localtime(time);
364 sipe_reset_tz(tz_orig);
366 return ret;
369 void
370 sipe_cal_free_working_hours(struct sipe_cal_working_hours *wh)
372 if (!wh) return;
374 g_free(wh->std.time);
375 g_free(wh->std.day_of_week);
376 g_free(wh->std.year);
378 g_free(wh->dst.time);
379 g_free(wh->dst.day_of_week);
380 g_free(wh->dst.year);
382 g_free(wh->days_of_week);
383 g_free(wh->tz);
384 g_free(wh->tz_std);
385 g_free(wh->tz_dst);
386 g_free(wh);
390 * Returns time_t of daylight savings time start/end
391 * in the provided timezone or otherwise
392 * (time_t)-1 if no daylight savings time.
394 static time_t
395 sipe_cal_get_std_dst_time(time_t now,
396 int bias,
397 struct sipe_cal_std_dst* std_dst,
398 struct sipe_cal_std_dst* dst_std)
400 struct tm switch_tm;
401 time_t res = TIME_NULL;
402 struct tm *gm_now_tm;
403 gchar **time_arr;
405 if (std_dst->month == 0) return TIME_NULL;
407 gm_now_tm = gmtime(&now);
408 time_arr = g_strsplit(std_dst->time, ":", 0);
410 switch_tm.tm_sec = atoi(time_arr[2]);
411 switch_tm.tm_min = atoi(time_arr[1]);
412 switch_tm.tm_hour = atoi(time_arr[0]);
413 g_strfreev(time_arr);
414 switch_tm.tm_mday = std_dst->year ? std_dst->day_order : 1 /* to adjust later */ ;
415 switch_tm.tm_mon = std_dst->month - 1;
416 switch_tm.tm_year = std_dst->year ? atoi(std_dst->year) - 1900 : gm_now_tm->tm_year;
417 switch_tm.tm_isdst = 0;
418 /* to set tm_wday */
419 res = sipe_mktime_tz(&switch_tm, "UTC");
421 /* if not dynamic, calculate right tm_mday */
422 if (!std_dst->year) {
423 int switch_wday = sipe_cal_get_wday(std_dst->day_of_week);
424 int needed_month;
425 /* get first desired wday in the month */
426 int delta = switch_wday >= switch_tm.tm_wday ? (switch_wday - switch_tm.tm_wday) : (switch_wday + 7 - switch_tm.tm_wday);
427 switch_tm.tm_mday = 1 + delta;
428 /* try nth order */
429 switch_tm.tm_mday += (std_dst->day_order - 1) * 7;
430 needed_month = switch_tm.tm_mon;
431 /* to set settle date if ahead of allowed month dates */
432 res = sipe_mktime_tz(&switch_tm, "UTC");
433 if (needed_month != switch_tm.tm_mon) {
434 /* moving 1 week back to stay within required month */
435 switch_tm.tm_mday -= 7;
436 /* to fix date again */
437 res = sipe_mktime_tz(&switch_tm, "UTC");
440 /* note: bias is taken from "switch to" structure */
441 return res + (bias + dst_std->bias)*60;
444 static void
445 sipe_cal_parse_std_dst(const sipe_xml *xn_std_dst_time,
446 struct sipe_cal_std_dst *std_dst)
448 const sipe_xml *node;
449 gchar *tmp;
451 if (!xn_std_dst_time) return;
452 if (!std_dst) return;
454 <StandardTime>
455 <Bias>0</Bias>
456 <Time>02:00:00</Time>
457 <DayOrder>1</DayOrder>
458 <Month>11</Month>
459 <DayOfWeek>Sunday</DayOfWeek>
460 </StandardTime>
463 if ((node = sipe_xml_child(xn_std_dst_time, "Bias"))) {
464 std_dst->bias = atoi(tmp = sipe_xml_data(node));
465 g_free(tmp);
468 if ((node = sipe_xml_child(xn_std_dst_time, "Time"))) {
469 std_dst->time = sipe_xml_data(node);
472 if ((node = sipe_xml_child(xn_std_dst_time, "DayOrder"))) {
473 std_dst->day_order = atoi(tmp = sipe_xml_data(node));
474 g_free(tmp);
477 if ((node = sipe_xml_child(xn_std_dst_time, "Month"))) {
478 std_dst->month = atoi(tmp = sipe_xml_data(node));
479 g_free(tmp);
482 if ((node = sipe_xml_child(xn_std_dst_time, "DayOfWeek"))) {
483 std_dst->day_of_week = sipe_xml_data(node);
486 if ((node = sipe_xml_child(xn_std_dst_time, "Year"))) {
487 std_dst->year = sipe_xml_data(node);
491 void
492 sipe_cal_parse_working_hours(const sipe_xml *xn_working_hours,
493 struct sipe_buddy *buddy)
495 const sipe_xml *xn_bias;
496 const sipe_xml *xn_timezone;
497 const sipe_xml *xn_working_period;
498 const sipe_xml *xn_standard_time;
499 const sipe_xml *xn_daylight_time;
500 gchar *tmp;
501 time_t now = time(NULL);
502 struct sipe_cal_std_dst* std;
503 struct sipe_cal_std_dst* dst;
505 if (!xn_working_hours) return;
507 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
508 <TimeZone>
509 <Bias>480</Bias>
511 </TimeZone>
512 <WorkingPeriodArray>
513 <WorkingPeriod>
514 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
515 <StartTimeInMinutes>600</StartTimeInMinutes>
516 <EndTimeInMinutes>1140</EndTimeInMinutes>
517 </WorkingPeriod>
518 </WorkingPeriodArray>
519 </WorkingHours>
521 sipe_cal_free_working_hours(buddy->cal_working_hours);
522 buddy->cal_working_hours = g_new0(struct sipe_cal_working_hours, 1);
524 xn_timezone = sipe_xml_child(xn_working_hours, "TimeZone");
525 xn_bias = sipe_xml_child(xn_timezone, "Bias");
526 if (xn_bias) {
527 buddy->cal_working_hours->bias = atoi(tmp = sipe_xml_data(xn_bias));
528 g_free(tmp);
531 xn_standard_time = sipe_xml_child(xn_timezone, "StandardTime");
532 xn_daylight_time = sipe_xml_child(xn_timezone, "DaylightTime");
534 std = &((*buddy->cal_working_hours).std);
535 dst = &((*buddy->cal_working_hours).dst);
536 sipe_cal_parse_std_dst(xn_standard_time, std);
537 sipe_cal_parse_std_dst(xn_daylight_time, dst);
539 xn_working_period = sipe_xml_child(xn_working_hours, "WorkingPeriodArray/WorkingPeriod");
540 if (xn_working_period) {
541 /* NOTE: this can be NULL! */
542 buddy->cal_working_hours->days_of_week =
543 sipe_xml_data(sipe_xml_child(xn_working_period, "DayOfWeek"));
545 buddy->cal_working_hours->start_time =
546 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "StartTimeInMinutes")));
547 g_free(tmp);
549 buddy->cal_working_hours->end_time =
550 atoi(tmp = sipe_xml_data(sipe_xml_child(xn_working_period, "EndTimeInMinutes")));
551 g_free(tmp);
554 std->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, std, dst);
555 dst->switch_time = sipe_cal_get_std_dst_time(now, buddy->cal_working_hours->bias, dst, std);
557 /* TST8TDT7,M3.2.0/02:00:00,M11.1.0/02:00:00 */
558 buddy->cal_working_hours->tz =
559 g_strdup_printf("TST%dTDT%d,M%d.%d.%d/%s,M%d.%d.%d/%s",
560 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60,
561 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60,
563 buddy->cal_working_hours->dst.month,
564 buddy->cal_working_hours->dst.day_order,
565 sipe_cal_get_wday(buddy->cal_working_hours->dst.day_of_week),
566 buddy->cal_working_hours->dst.time,
568 buddy->cal_working_hours->std.month,
569 buddy->cal_working_hours->std.day_order,
570 sipe_cal_get_wday(buddy->cal_working_hours->std.day_of_week),
571 buddy->cal_working_hours->std.time
573 /* TST8 */
574 buddy->cal_working_hours->tz_std =
575 g_strdup_printf("TST%d",
576 (buddy->cal_working_hours->bias + buddy->cal_working_hours->std.bias) / 60);
577 /* TDT7 */
578 buddy->cal_working_hours->tz_dst =
579 g_strdup_printf("TDT%d",
580 (buddy->cal_working_hours->bias + buddy->cal_working_hours->dst.bias) / 60);
583 struct sipe_cal_event*
584 sipe_cal_get_event(GSList *cal_events,
585 time_t time_in_question)
587 GSList *entry = cal_events;
588 struct sipe_cal_event* cal_event;
589 struct sipe_cal_event* res = NULL;
591 if (!cal_events || !IS(time_in_question)) return NULL;
593 while (entry) {
594 cal_event = entry->data;
595 /* event is in the past or in the future */
596 if (cal_event->start_time > time_in_question ||
597 cal_event->end_time <= time_in_question)
599 entry = entry->next;
600 continue;
603 if (!res) {
604 res = cal_event;
605 } else {
606 int res_status = (res->cal_status == SIPE_CAL_NO_DATA) ? -1 : res->cal_status;
607 int cal_status = (cal_event->cal_status == SIPE_CAL_NO_DATA) ? -1 : cal_event->cal_status;
608 if (res_status < cal_status) {
609 res = cal_event;
612 entry = entry->next;
614 return res;
617 static int
618 sipe_cal_get_status0(const gchar *free_busy,
619 time_t cal_start,
620 int granularity,
621 time_t time_in_question,
622 int *index)
624 int res = SIPE_CAL_NO_DATA;
625 int shift;
626 time_t cal_end = cal_start + strlen(free_busy)*granularity*60 - 1;
628 if (!(time_in_question >= cal_start && time_in_question <= cal_end)) return res;
630 shift = (time_in_question - cal_start) / (granularity*60);
631 if (index) {
632 *index = shift;
635 res = free_busy[shift] - '0';
637 return res;
641 * Returns time when current calendar state started
643 static time_t
644 sipe_cal_get_since_time(const gchar *free_busy,
645 time_t calStart,
646 int granularity,
647 int index,
648 int current_state)
650 int i;
652 if ((index < 0) || ((size_t)(index + 1) > strlen(free_busy))) return 0;
654 for (i = index; i >= 0; i--) {
655 int temp_status = free_busy[i] - '0';
657 if (current_state != temp_status) {
658 return calStart + (i + 1)*granularity*60;
661 if (i == 0) return calStart;
664 return 0;
666 static char*
667 sipe_cal_get_free_busy(struct sipe_buddy *buddy);
670 sipe_cal_get_status(struct sipe_buddy *buddy,
671 time_t time_in_question,
672 time_t *since)
674 time_t cal_start;
675 const char* free_busy;
676 int ret = SIPE_CAL_NO_DATA;
677 time_t state_since;
678 int index;
680 if (!buddy || !buddy->cal_start_time || !buddy->cal_granularity) {
681 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data1 for %s, exiting",
682 buddy ? (buddy->name ? buddy->name : "") : "");
683 return SIPE_CAL_NO_DATA;
686 if (!(free_busy = sipe_cal_get_free_busy(buddy))) {
687 SIPE_DEBUG_INFO("sipe_cal_get_status: no calendar data2 for %s, exiting", buddy->name);
688 return SIPE_CAL_NO_DATA;
690 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy);
692 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
694 ret = sipe_cal_get_status0(free_busy,
695 cal_start,
696 buddy->cal_granularity,
697 time_in_question,
698 &index);
699 state_since = sipe_cal_get_since_time(free_busy,
700 cal_start,
701 buddy->cal_granularity,
702 index,
703 ret);
705 if (since) *since = state_since;
706 return ret;
709 static time_t
710 sipe_cal_get_switch_time(const gchar *free_busy,
711 time_t calStart,
712 int granularity,
713 int index,
714 int current_state,
715 int *to_state)
717 size_t i;
718 time_t ret = TIME_NULL;
720 if ((index < 0) || ((size_t) (index + 1) > strlen(free_busy))) {
721 *to_state = SIPE_CAL_NO_DATA;
722 return ret;
725 for (i = index + 1; i < strlen(free_busy); i++) {
726 int temp_status = free_busy[i] - '0';
728 if (current_state != temp_status) {
729 *to_state = temp_status;
730 return calStart + i*granularity*60;
734 return ret;
737 static const char*
738 sipe_cal_get_tz(struct sipe_cal_working_hours *wh,
739 time_t time_in_question)
741 time_t dst_switch_time = (*wh).dst.switch_time;
742 time_t std_switch_time = (*wh).std.switch_time;
743 gboolean is_dst = FALSE;
745 /* No daylight savings */
746 if (dst_switch_time == TIME_NULL) {
747 return wh->tz_std;
750 if (dst_switch_time < std_switch_time) { /* North hemosphere - Europe, US */
751 if (time_in_question >= dst_switch_time && time_in_question < std_switch_time) {
752 is_dst = TRUE;
754 } else { /* South hemisphere - Australia */
755 if (time_in_question >= dst_switch_time || time_in_question < std_switch_time) {
756 is_dst = TRUE;
760 if (is_dst) {
761 return wh->tz_dst;
762 } else {
763 return wh->tz_std;
767 static time_t
768 sipe_cal_mktime_of_day(struct tm *sample_today_tm,
769 const int shift_minutes,
770 const char *tz)
772 sample_today_tm->tm_sec = 0;
773 sample_today_tm->tm_min = shift_minutes % 60;
774 sample_today_tm->tm_hour = shift_minutes / 60;
776 return sipe_mktime_tz(sample_today_tm, tz);
780 * Returns work day start and end in Epoch time
781 * considering the initial values are provided
782 * in contact's local time zone.
784 static void
785 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours *wh,
786 time_t *start,
787 time_t *end,
788 time_t *next_start)
790 time_t now = time(NULL);
791 const char *tz = sipe_cal_get_tz(wh, now);
792 struct tm *remote_now_tm = sipe_localtime_tz(&now, tz);
794 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_now_tm->tm_wday]))) {
795 /* not a work day */
796 *start = TIME_NULL;
797 *end = TIME_NULL;
798 *next_start = TIME_NULL;
799 return;
802 *end = sipe_cal_mktime_of_day(remote_now_tm, wh->end_time, tz);
804 if (now < *end) {
805 *start = sipe_cal_mktime_of_day(remote_now_tm, wh->start_time, tz);
806 *next_start = TIME_NULL;
807 } else { /* calculate start of tomorrow's work day if any */
808 time_t tom = now + 24*60*60;
809 struct tm *remote_tom_tm = sipe_localtime_tz(&tom, sipe_cal_get_tz(wh, tom));
811 if (!(wh->days_of_week && strstr(wh->days_of_week, wday_names[remote_tom_tm->tm_wday]))) {
812 /* not a work day */
813 *next_start = TIME_NULL;
816 *next_start = sipe_cal_mktime_of_day(remote_tom_tm, wh->start_time, sipe_cal_get_tz(wh, tom));
817 *start = TIME_NULL;
821 static int
822 sipe_cal_is_in_work_hours(const time_t time_in_question,
823 const time_t start,
824 const time_t end)
826 return !((time_in_question >= end) || (IS(start) && time_in_question < start));
830 * Returns time closest to now. Choses only from times ahead of now.
831 * Returns TIME_NULL otherwise.
833 static time_t
834 sipe_cal_get_until(const time_t now,
835 const time_t switch_time,
836 const time_t start,
837 const time_t end,
838 const time_t next_start)
840 time_t ret = TIME_NULL;
841 int min_diff = now - ret;
843 if (IS(switch_time) && switch_time > now && (switch_time - now) < min_diff) {
844 min_diff = switch_time - now;
845 ret = switch_time;
847 if (IS(start) && start > now && (start - now) < min_diff) {
848 min_diff = start - now;
849 ret = start;
851 if (IS(end) && end > now && (end - now) < min_diff) {
852 min_diff = end - now;
853 ret = end;
855 if (IS(next_start) && next_start > now && (next_start - now) < min_diff) {
856 min_diff = next_start - now;
857 ret = next_start;
859 return ret;
862 static char*
863 sipe_cal_get_free_busy(struct sipe_buddy *buddy)
865 /* do lazy decode if necessary */
866 if (!buddy->cal_free_busy && buddy->cal_free_busy_base64) {
867 gsize cal_dec64_len;
868 guchar *cal_dec64;
869 gsize i;
870 int j = 0;
872 cal_dec64 = g_base64_decode(buddy->cal_free_busy_base64, &cal_dec64_len);
874 buddy->cal_free_busy = g_malloc0(cal_dec64_len * 4 + 1);
876 http://msdn.microsoft.com/en-us/library/dd941537%28office.13%29.aspx
877 00, Free (Fr)
878 01, Tentative (Te)
879 10, Busy (Bu)
880 11, Out of facility (Oo)
882 http://msdn.microsoft.com/en-us/library/aa566048.aspx
883 0 Free
884 1 Tentative
885 2 Busy
886 3 Out of Office (OOF)
887 4 No data
889 for (i = 0; i < cal_dec64_len; i++) {
890 #define TWO_BIT_MASK 0x03
891 char tmp = cal_dec64[i];
892 buddy->cal_free_busy[j++] = (tmp & TWO_BIT_MASK) + '0';
893 buddy->cal_free_busy[j++] = ((tmp >> 2) & TWO_BIT_MASK) + '0';
894 buddy->cal_free_busy[j++] = ((tmp >> 4) & TWO_BIT_MASK) + '0';
895 buddy->cal_free_busy[j++] = ((tmp >> 6) & TWO_BIT_MASK) + '0';
897 buddy->cal_free_busy[j++] = '\0';
898 g_free(cal_dec64);
901 return buddy->cal_free_busy;
904 char *
905 sipe_cal_get_freebusy_base64(const char* freebusy_hex)
907 guint i = 0;
908 guint j = 0;
909 guint shift_factor = 0;
910 guint len, res_len;
911 guchar *res;
912 gchar *res_base64;
914 if (!freebusy_hex) return NULL;
916 len = strlen(freebusy_hex);
917 res_len = len / 4 + 1;
918 res = g_malloc0(res_len);
919 while (i < len) {
920 res[j] |= (freebusy_hex[i++] - '0') << shift_factor;
921 shift_factor += 2;
922 if (shift_factor == 8) {
923 shift_factor = 0;
924 j++;
928 res_base64 = g_base64_encode(res, shift_factor ? res_len : res_len - 1);
929 g_free(res);
930 return res_base64;
933 char *
934 sipe_cal_get_description(struct sipe_buddy *buddy)
936 time_t cal_start;
937 time_t cal_end;
938 int current_cal_state;
939 time_t now = time(NULL);
940 time_t start = TIME_NULL;
941 time_t end = TIME_NULL;
942 time_t next_start = TIME_NULL;
943 time_t switch_time;
944 int to_state = SIPE_CAL_NO_DATA;
945 time_t until = TIME_NULL;
946 int index = 0;
947 gboolean has_working_hours = (buddy->cal_working_hours != NULL);
948 const char *free_busy;
949 const char *cal_states[] = {_("Free"),
950 _("Tentative"),
951 _("Busy"),
952 _("Out of office"),
953 _("No data")};
955 if (buddy->cal_granularity != 15) {
956 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy->cal_granularity);
957 return NULL;
960 /* to lazy load if needed */
961 free_busy = sipe_cal_get_free_busy(buddy);
962 SIPE_DEBUG_INFO("sipe_cal_get_description: buddy->cal_free_busy=\n%s", free_busy ? free_busy : "");
964 if (!buddy->cal_free_busy || !buddy->cal_granularity || !buddy->cal_start_time) {
965 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: no calendar data, exiting");
966 return NULL;
969 cal_start = sipe_utils_str_to_time(buddy->cal_start_time);
970 cal_end = cal_start + 60 * (buddy->cal_granularity) * strlen(buddy->cal_free_busy);
972 current_cal_state = sipe_cal_get_status0(free_busy, cal_start, buddy->cal_granularity, time(NULL), &index);
973 if (current_cal_state == SIPE_CAL_NO_DATA) {
974 SIPE_DEBUG_INFO_NOFORMAT("sipe_cal_get_description: calendar is undefined for present moment, exiting.");
975 return NULL;
978 switch_time = sipe_cal_get_switch_time(free_busy, cal_start, buddy->cal_granularity, index, current_cal_state, &to_state);
980 SIPE_DEBUG_INFO_NOFORMAT("\n* Calendar *");
981 if (buddy->cal_working_hours) {
982 sipe_cal_get_today_work_hours(buddy->cal_working_hours, &start, &end, &next_start);
984 SIPE_DEBUG_INFO("Remote now timezone : %s", sipe_cal_get_tz(buddy->cal_working_hours, now));
985 SIPE_DEBUG_INFO("std.switch_time(GMT): %s",
986 IS((*buddy->cal_working_hours).std.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).std.switch_time))) : "");
987 SIPE_DEBUG_INFO("dst.switch_time(GMT): %s",
988 IS((*buddy->cal_working_hours).dst.switch_time) ? asctime(gmtime(&((*buddy->cal_working_hours).dst.switch_time))) : "");
989 SIPE_DEBUG_INFO("Remote now time : %s",
990 asctime(sipe_localtime_tz(&now, sipe_cal_get_tz(buddy->cal_working_hours, now))));
991 SIPE_DEBUG_INFO("Remote start time : %s",
992 IS(start) ? asctime(sipe_localtime_tz(&start, sipe_cal_get_tz(buddy->cal_working_hours, start))) : "");
993 SIPE_DEBUG_INFO("Remote end time : %s",
994 IS(end) ? asctime(sipe_localtime_tz(&end, sipe_cal_get_tz(buddy->cal_working_hours, end))) : "");
995 SIPE_DEBUG_INFO("Rem. next_start time: %s",
996 IS(next_start) ? asctime(sipe_localtime_tz(&next_start, sipe_cal_get_tz(buddy->cal_working_hours, next_start))) : "");
997 SIPE_DEBUG_INFO("Remote switch time : %s",
998 IS(switch_time) ? asctime(sipe_localtime_tz(&switch_time, sipe_cal_get_tz(buddy->cal_working_hours, switch_time))) : "");
999 } else {
1000 SIPE_DEBUG_INFO("Local now time : %s",
1001 asctime(localtime(&now)));
1002 SIPE_DEBUG_INFO("Local switch time : %s",
1003 IS(switch_time) ? asctime(localtime(&switch_time)) : "");
1005 SIPE_DEBUG_INFO("Calendar End (GMT) : %s", asctime(gmtime(&cal_end)));
1006 SIPE_DEBUG_INFO("current cal state : %s", cal_states[current_cal_state]);
1007 SIPE_DEBUG_INFO("switch cal state : %s", cal_states[to_state] );
1009 /* Calendar: string calculations */
1012 ALGORITHM (don't delete)
1013 (c)2009,2010 pier11 <pier11@operamail.com>
1015 SOD = Start of Work Day
1016 EOD = End of Work Day
1017 NSOD = Start of tomorrow's Work Day
1018 SW = Calendar status switch time
1020 if current_cal_state == Free
1021 until = min_t of SOD, EOD, NSOD, SW (min_t(x) = min(x-now) where x>now only)
1022 else
1023 until = SW
1025 if (!until && (cal_period_end > now + 8H))
1026 until = cal_period_end
1028 if (!until)
1029 return "Currently %", current_cal_state
1031 if (until - now > 8H)
1032 if (current_cal_state == Free && (work_hours && !in work_hours(now)))
1033 return "Outside of working hours for next 8 hours"
1034 else
1035 return "%s for next 8 hours", current_cal_state
1037 if (current_cal_state == Free)
1038 if (work_hours && until !in work_hours(now))
1039 "Not working"
1040 else
1041 "%s", current_cal_state
1042 " until %.2d:%.2d", until
1043 else
1044 "Currently %", current_cal_state
1045 if (work_hours && until !in work_hours(until))
1046 ". Outside of working hours at at %.2d:%.2d", until
1047 else
1048 ". %s at %.2d:%.2d", to_state, until
1051 if (current_cal_state < 1) { /* Free */
1052 until = sipe_cal_get_until(now, switch_time, start, end, next_start);
1053 } else {
1054 until = switch_time;
1057 if (!IS(until) && (cal_end - now > 8*60*60))
1058 until = cal_end;
1060 if (!IS(until)) {
1061 return g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1064 if (until - now > 8*60*60) {
1065 /* Free & outside work hours */
1066 if (current_cal_state < 1 && has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1067 return g_strdup(_("Outside of working hours for next 8 hours"));
1068 } else {
1069 return g_strdup_printf(_("%s for next 8 hours"), cal_states[current_cal_state]);
1073 if (current_cal_state < 1) { /* Free */
1074 const char *tmp;
1075 struct tm *until_tm = localtime(&until);
1077 if (has_working_hours && !sipe_cal_is_in_work_hours(now, start, end)) {
1078 tmp = _("Not working");
1079 } else {
1080 tmp = cal_states[current_cal_state];
1082 return g_strdup_printf(_("%s until %.2d:%.2d"), tmp, until_tm->tm_hour, until_tm->tm_min);
1083 } else { /* Tentative or Busy or OOF */
1084 char *tmp;
1085 char *res;
1086 struct tm *until_tm = localtime(&until);
1088 tmp = g_strdup_printf(_("Currently %s"), cal_states[current_cal_state]);
1089 if (has_working_hours && !sipe_cal_is_in_work_hours(until, start, end)) {
1090 res = g_strdup_printf(_("%s. Outside of working hours at %.2d:%.2d"),
1091 tmp, until_tm->tm_hour, until_tm->tm_min);
1092 g_free(tmp);
1093 return res;
1094 } else {
1095 res = g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp, cal_states[to_state], until_tm->tm_hour, until_tm->tm_min);
1096 g_free(tmp);
1097 return res;
1100 /* End of - Calendar: string calculations */
1103 #define UPDATE_CALENDAR_INTERVAL 30*60 /* 30 min */
1105 void sipe_core_update_calendar(struct sipe_core_public *sipe_public)
1107 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: started.");
1109 /* Do in parallel.
1110 * If failed, the branch will be disabled for subsequent calls.
1111 * Can't rely that user turned the functionality on in account settings.
1113 sipe_ews_update_calendar(SIPE_CORE_PRIVATE);
1114 #ifdef _WIN32
1115 /* @TODO: UNIX integration missing */
1116 sipe_domino_update_calendar(SIPE_CORE_PRIVATE);
1117 #endif
1119 /* schedule repeat */
1120 sipe_schedule_seconds(SIPE_CORE_PRIVATE,
1121 "<+update-calendar>",
1122 NULL,
1123 UPDATE_CALENDAR_INTERVAL,
1124 (sipe_schedule_action)sipe_core_update_calendar,
1125 NULL);
1127 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_update_calendar: finished.");
1130 void sipe_cal_presence_publish(struct sipe_core_private *sipe_private,
1131 gboolean do_publish_calendar)
1133 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1134 if (do_publish_calendar)
1135 sipe_ocs2007_presence_publish(sipe_private, NULL);
1136 else
1137 sipe_ocs2007_category_publish(sipe_private);
1138 } else {
1139 sipe_ocs2005_presence_publish(sipe_private,
1140 do_publish_calendar);
1144 void sipe_cal_delayed_calendar_update(struct sipe_core_private *sipe_private)
1146 #define UPDATE_CALENDAR_DELAY 1*60 /* 1 min */
1148 sipe_schedule_seconds(sipe_private,
1149 "<+update-calendar>",
1150 NULL,
1151 UPDATE_CALENDAR_DELAY,
1152 (sipe_schedule_action) sipe_core_update_calendar,
1153 NULL);
1157 Local Variables:
1158 mode: c
1159 c-file-style: "bsd"
1160 indent-tabs-mode: t
1161 tab-width: 8
1162 End: