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
35 #include "sipe-backend.h"
36 #include "sipe-buddy.h"
37 #include "sipe-core.h"
38 #include "sipe-core-private.h"
40 #include "sipe-http.h"
42 #include "sipe-ocs2005.h"
43 #include "sipe-ocs2007.h"
44 #include "sipe-schedule.h"
45 #include "sipe-utils.h"
48 /* Calendar backends */
50 #include "sipe-domino.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
61 <WorkingHours xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
67 <DayOrder>1</DayOrder>
69 <DayOfWeek>Sunday</DayOfWeek>
74 <DayOrder>2</DayOrder>
76 <DayOfWeek>Sunday</DayOfWeek>
81 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
82 <StartTimeInMinutes>600</StartTimeInMinutes>
83 <EndTimeInMinutes>1140</EndTimeInMinutes>
92 <DayOrder>short</DayOrder>
94 <DayOfWeek>Sunday or Monday or Tuesday or Wednesday or Thursday or Friday or Saturday</DayOfWeek>
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 */
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",
136 sipe_cal_get_wday(char *wday_name
)
140 if (!wday_name
) return -1;
142 for (i
= 0; i
< 7; i
++) {
143 if (sipe_strequal(wday_names
[i
], wday_name
)) {
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
);
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
);
170 sipe_cal_calendar_free(struct sipe_calendar
*cal
)
173 g_free(cal
->legacy_dn
);
174 g_free(cal
->auth_domain
);
175 g_free(cal
->auth_user
);
176 g_free(cal
->password
);
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
);
189 sipe_http_request_cancel(cal
->request
);
190 sipe_http_session_close(cal
->session
);
196 sipe_cal_calendar_init(struct sipe_core_private
*sipe_private
,
199 if (!sipe_private
->calendar
) {
200 struct sipe_calendar
*cal
;
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
);
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
);
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
: "",
274 #define ENVIRONMENT_TIMEZONE "TZ"
277 sipe_switch_tz(const char *tz
)
281 tz_orig
= g_strdup(g_getenv(ENVIRONMENT_TIMEZONE
));
282 g_setenv(ENVIRONMENT_TIMEZONE
, tz
, TRUE
);
288 sipe_reset_tz(gchar
*tz_orig
)
291 g_setenv(ENVIRONMENT_TIMEZONE
, tz_orig
, TRUE
);
294 g_unsetenv(ENVIRONMENT_TIMEZONE
);
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
307 sipe_mktime_tz(struct tm
*tm
,
313 tz_orig
= sipe_switch_tz(tz
);
315 sipe_reset_tz(tz_orig
);
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
328 sipe_localtime_tz(const time_t *time
,
334 tz_orig
= sipe_switch_tz(tz
);
335 ret
= localtime(time
);
336 sipe_reset_tz(tz_orig
);
342 sipe_cal_free_working_hours(struct sipe_cal_working_hours
*wh
)
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
);
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.
367 sipe_cal_get_std_dst_time(time_t now
,
369 struct sipe_cal_std_dst
* std_dst
,
370 struct sipe_cal_std_dst
* dst_std
)
373 time_t res
= TIME_NULL
;
374 struct tm
*gm_now_tm
;
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;
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
);
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
;
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;
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
;
423 if (!xn_std_dst_time
) return;
424 if (!std_dst
) return;
428 <Time>02:00:00</Time>
429 <DayOrder>1</DayOrder>
432 <DayOfWeek>Sunday</DayOfWeek>
436 if ((node
= sipe_xml_child(xn_std_dst_time
, "Bias"))) {
437 std_dst
->bias
= atoi(tmp
= sipe_xml_data(node
));
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
));
450 if ((node
= sipe_xml_child(xn_std_dst_time
, "Month"))) {
451 std_dst
->month
= atoi(tmp
= sipe_xml_data(node
));
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
);
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
;
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">
487 <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
488 <StartTimeInMinutes>600</StartTimeInMinutes>
489 <EndTimeInMinutes>1140</EndTimeInMinutes>
491 </WorkingPeriodArray>
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");
500 buddy
->cal_working_hours
->bias
= atoi(tmp
= sipe_xml_data(xn_bias
));
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")));
522 buddy
->cal_working_hours
->end_time
=
523 atoi(tmp
= sipe_xml_data(sipe_xml_child(xn_working_period
, "EndTimeInMinutes")));
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
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);
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
;
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
)
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
) {
591 sipe_cal_get_status0(const gchar
*free_busy
,
594 time_t time_in_question
,
597 int res
= SIPE_CAL_NO_DATA
;
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);
608 res
= free_busy
[shift
] - '0';
614 * Returns time when current calendar state started
617 sipe_cal_get_since_time(const gchar
*free_busy
,
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
;
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
,
648 const char* free_busy
;
649 int ret
= SIPE_CAL_NO_DATA
;
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
,
669 buddy
->cal_granularity
,
672 state_since
= sipe_cal_get_since_time(free_busy
,
674 buddy
->cal_granularity
,
678 if (since
) *since
= state_since
;
683 sipe_cal_get_switch_time(const gchar
*free_busy
,
691 time_t ret
= TIME_NULL
;
693 if ((index
< 0) || ((size_t) (index
+ 1) > strlen(free_busy
))) {
694 *to_state
= SIPE_CAL_NO_DATA
;
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;
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
) {
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
) {
727 } else { /* South hemisphere - Australia */
728 if (time_in_question
>= dst_switch_time
|| time_in_question
< std_switch_time
) {
741 sipe_cal_mktime_of_day(struct tm
*sample_today_tm
,
742 const int shift_minutes
,
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.
758 sipe_cal_get_today_work_hours(struct sipe_cal_working_hours
*wh
,
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
]))) {
771 *next_start
= TIME_NULL
;
775 *end
= sipe_cal_mktime_of_day(remote_now_tm
, wh
->end_time
, tz
);
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
]))) {
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
));
795 sipe_cal_is_in_work_hours(const time_t time_in_question
,
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.
807 sipe_cal_get_until(const time_t now
,
808 const time_t switch_time
,
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
;
820 if (IS(start
) && start
> now
&& (start
- now
) < min_diff
) {
821 min_diff
= start
- now
;
824 if (IS(end
) && end
> now
&& (end
- now
) < min_diff
) {
825 min_diff
= end
- now
;
828 if (IS(next_start
) && next_start
> now
&& (next_start
- now
) < min_diff
) {
829 min_diff
= next_start
- now
;
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
) {
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
853 11, Out of facility (Oo)
855 http://msdn.microsoft.com/en-us/library/aa566048.aspx
859 3 Out of Office (OOF)
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';
874 return buddy
->cal_free_busy
;
878 sipe_cal_get_freebusy_base64(const char* freebusy_hex
)
882 guint shift_factor
= 0;
887 if (!freebusy_hex
) return NULL
;
889 len
= strlen(freebusy_hex
);
890 res_len
= len
/ 4 + 1;
891 res
= g_malloc0(res_len
);
893 res
[j
] |= (freebusy_hex
[i
++] - '0') << shift_factor
;
895 if (shift_factor
== 8) {
901 res_base64
= g_base64_encode(res
, shift_factor
? res_len
: res_len
- 1);
907 sipe_cal_get_description(struct sipe_buddy
*buddy
)
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
;
917 int to_state
= SIPE_CAL_NO_DATA
;
918 time_t until
= TIME_NULL
;
920 gboolean has_working_hours
= (buddy
->cal_working_hours
!= NULL
);
921 const char *free_busy
;
922 const char *cal_states
[] = {_("Free"),
928 if (buddy
->cal_granularity
!= 15) {
929 SIPE_DEBUG_INFO("sipe_cal_get_description: granularity %d is unsupported, exiting.", buddy
->cal_granularity
);
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");
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.");
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
))) : "");
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)
998 if (!until && (cal_period_end > now + 8H))
999 until = cal_period_end
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"
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))
1014 "%s", current_cal_state
1015 " until %.2d:%.2d", until
1017 "Currently %", current_cal_state
1018 if (work_hours && until !in work_hours(until))
1019 ". Outside of working hours at at %.2d:%.2d", until
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
);
1027 until
= switch_time
;
1030 if (!IS(until
) && (cal_end
- now
> 8*60*60))
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"));
1042 return g_strdup_printf(_("%s for next 8 hours"), cal_states
[current_cal_state
]);
1046 if (current_cal_state
< 1) { /* Free */
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");
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 */
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
);
1068 res
= g_strdup_printf(_("%s. %s at %.2d:%.2d"), tmp
, cal_states
[to_state
], until_tm
->tm_hour
, until_tm
->tm_min
);
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.");
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
);
1088 /* @TODO: UNIX integration missing */
1089 sipe_domino_update_calendar(SIPE_CORE_PRIVATE
);
1092 /* schedule repeat */
1093 sipe_schedule_seconds(SIPE_CORE_PRIVATE
,
1094 "<+update-calendar>",
1096 UPDATE_CALENDAR_INTERVAL
,
1097 (sipe_schedule_action
)sipe_core_update_calendar
,
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
);
1110 sipe_ocs2007_category_publish(sipe_private
);
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>",
1126 UPDATE_CALENDAR_DELAY
,
1127 (sipe_schedule_action
) sipe_core_update_calendar
,
1131 void sipe_cal_http_authentication(struct sipe_calendar
*cal
)
1133 if (cal
->auth_user
) {
1134 sipe_http_request_authentication(cal
->request
,