core cleanup: move out is_oof_note flag
[siplcs.git] / src / core / sipe-ocs2005.c
blob2a522876f922f75c775610ee3657e3789ab31d6d
1 /**
2 * @file sipe-ocs2005.c
4 * pidgin-sipe
6 * Copyright (C) 2011 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * OCS2005 specific code
28 #include <time.h>
30 #include <glib.h>
32 #include "sipe-common.h"
33 #include "http-conn.h" /* sipe-cal.h requires this */
34 #include "sip-soap.h"
35 #include "sipe-backend.h"
36 #include "sipe-buddy.h"
37 #include "sipe-cal.h"
38 #include "sipe-core.h"
39 #include "sipe-core-private.h"
40 #include "sipe-ews.h"
41 #include "sipe-ocs2005.h"
42 #include "sipe-ocs2007.h"
43 #include "sipe-schedule.h"
44 #include "sipe-status.h"
45 #include "sipe-utils.h"
46 #include "sipe-xml.h"
47 #include "sipe.h"
49 /**
50 * 2005-style Activity and Availability.
52 * [MS-SIP] 2.2.1
54 * @param activity 2005 aggregated activity. Ex.: 600
55 * @param availablity 2005 aggregated availablity. Ex.: 300
57 * The values define the starting point of a range
59 #define SIPE_OCS2005_ACTIVITY_UNKNOWN 0
60 #define SIPE_OCS2005_ACTIVITY_AWAY 100
61 #define SIPE_OCS2005_ACTIVITY_LUNCH 150
62 #define SIPE_OCS2005_ACTIVITY_IDLE 200
63 #define SIPE_OCS2005_ACTIVITY_BRB 300
64 #define SIPE_OCS2005_ACTIVITY_AVAILABLE 400 /* user is active */
65 #define SIPE_OCS2005_ACTIVITY_ON_PHONE 500 /* user is participating in a communcation session */
66 #define SIPE_OCS2005_ACTIVITY_BUSY 600
67 #define SIPE_OCS2005_ACTIVITY_AWAY2 700
68 #define SIPE_OCS2005_ACTIVITY_AVAILABLE2 800
70 #define SIPE_OCS2005_AVAILABILITY_OFFLINE 0
71 #define SIPE_OCS2005_AVAILABILITY_MAYBE 100
72 #define SIPE_OCS2005_AVAILABILITY_ONLINE 300
73 static guint sipe_ocs2005_activity_from_status(struct sipe_core_private *sipe_private)
75 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
76 const gchar *status = sip->status;
78 if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_AWAY))) {
79 return(SIPE_OCS2005_ACTIVITY_AWAY);
80 /*} else if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_LUNCH))) {
81 return(SIPE_OCS2005_ACTIVITY_LUNCH); */
82 } else if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_BRB))) {
83 return(SIPE_OCS2005_ACTIVITY_BRB);
84 } else if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_AVAILABLE))) {
85 return(SIPE_OCS2005_ACTIVITY_AVAILABLE);
86 /*} else if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_ON_PHONE))) {
87 return(SIPE_OCS2005_ACTIVITY_ON_PHONE); */
88 } else if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_BUSY)) ||
89 sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_DND))) {
90 return(SIPE_OCS2005_ACTIVITY_BUSY);
91 } else if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_INVISIBLE)) ||
92 sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_OFFLINE))) {
93 return(SIPE_OCS2005_ACTIVITY_AWAY);
94 } else {
95 return(SIPE_OCS2005_ACTIVITY_AVAILABLE);
99 static guint sipe_ocs2005_availability_from_status(struct sipe_core_private *sipe_private)
101 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
102 const gchar *status = sip->status;
104 if (sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_INVISIBLE)) ||
105 sipe_strequal(status, sipe_backend_activity_to_token(SIPE_ACTIVITY_OFFLINE)))
106 return(SIPE_OCS2005_AVAILABILITY_OFFLINE);
107 else
108 return(SIPE_OCS2005_AVAILABILITY_ONLINE);
111 const gchar *sipe_ocs2005_status_from_activity_availability(guint activity,
112 guint availability)
114 guint type;
116 if (availability < SIPE_OCS2005_AVAILABILITY_MAYBE) {
117 type = SIPE_ACTIVITY_OFFLINE;
118 } else if (activity < SIPE_OCS2005_ACTIVITY_LUNCH) {
119 type = SIPE_ACTIVITY_AWAY;
120 } else if (activity < SIPE_OCS2005_ACTIVITY_IDLE) {
121 //type = SIPE_ACTIVITY_LUNCH;
122 type = SIPE_ACTIVITY_AWAY;
123 } else if (activity < SIPE_OCS2005_ACTIVITY_BRB) {
124 //type = SIPE_ACTIVITY_IDLE;
125 type = SIPE_ACTIVITY_AWAY;
126 } else if (activity < SIPE_OCS2005_ACTIVITY_AVAILABLE) {
127 type = SIPE_ACTIVITY_BRB;
128 } else if (activity < SIPE_OCS2005_ACTIVITY_ON_PHONE) {
129 type = SIPE_ACTIVITY_AVAILABLE;
130 } else if (activity < SIPE_OCS2005_ACTIVITY_BUSY) {
131 //type = SIPE_ACTIVITY_ON_PHONE;
132 type = SIPE_ACTIVITY_BUSY;
133 } else if (activity < SIPE_OCS2005_ACTIVITY_AWAY2) {
134 type = SIPE_ACTIVITY_BUSY;
135 } else if (activity < SIPE_OCS2005_ACTIVITY_AVAILABLE2) {
136 type = SIPE_ACTIVITY_AWAY;
137 } else {
138 type = SIPE_ACTIVITY_AVAILABLE;
141 return(sipe_backend_activity_to_token(type));
144 const gchar *sipe_ocs2005_activity_description(guint activity)
146 if ((activity >= SIPE_OCS2005_ACTIVITY_LUNCH) &&
147 (activity < SIPE_OCS2005_ACTIVITY_IDLE)) {
148 return(sipe_core_activity_description(SIPE_ACTIVITY_LUNCH));
149 } else if ((activity >= SIPE_OCS2005_ACTIVITY_IDLE) &&
150 (activity < SIPE_OCS2005_ACTIVITY_BRB)) {
151 return(sipe_core_activity_description(SIPE_ACTIVITY_INACTIVE));
152 } else if ((activity >= SIPE_OCS2005_ACTIVITY_ON_PHONE) &&
153 (activity < SIPE_OCS2005_ACTIVITY_BUSY)) {
154 return(sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE));
155 } else {
156 return(NULL);
160 void sipe_ocs2005_user_info_has_updated(struct sipe_core_private *sipe_private,
161 const sipe_xml *xn_userinfo)
163 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
164 const sipe_xml *xn_states;
166 g_free(sip->user_states);
167 sip->user_states = NULL;
168 if ((xn_states = sipe_xml_child(xn_userinfo, "states")) != NULL) {
169 gchar *orig = sip->user_states = sipe_xml_stringify(xn_states);
171 /* this is a hack-around to remove added newline after inner element,
172 * state in this case, where it shouldn't be.
173 * After several use of sipe_xml_stringify, amount of added newlines
174 * grows significantly.
176 if (orig) {
177 gchar c, *stripped = orig;
178 while ((c = *orig++)) {
179 if ((c != '\n') /* && (c != '\r') */) {
180 *stripped++ = c;
183 *stripped = '\0';
187 /* Publish initial state if not yet.
188 * Assuming this happens on initial responce to self subscription
189 * so we've already updated our UserInfo.
191 if (!sip->initial_state_published) {
192 sipe_ocs2005_presence_publish(sipe_private, FALSE);
193 /* dalayed run */
194 sipe_cal_delayed_calendar_update(sipe_private);
198 static gboolean sipe_is_user_available(struct sipe_core_private *sipe_private)
200 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
201 return(sipe_strequal(sip->status,
202 sipe_backend_activity_to_token(SIPE_ACTIVITY_AVAILABLE)));
207 * OCS2005 presence XML messages
209 * Calendar publication entry
211 * @param legacy_dn (%s) Ex.: /o=EXCHANGE/ou=BTUK02/cn=Recipients/cn=AHHBTT
212 * @param fb_start_time_str (%s) Ex.: 2009-12-06T17:15:00Z
213 * @param free_busy_base64 (%s) Ex.: AAAAAAAAAAAAAAAAA......
215 #define SIPE_SOAP_SET_PRESENCE_CALENDAR \
216 "<calendarInfo xmlns=\"http://schemas.microsoft.com/2002/09/sip/presence\" mailboxId=\"%s\" startTime=\"%s\" granularity=\"PT15M\">%s</calendarInfo>"
219 * Note publication entry
221 * @param note (%s) Ex.: Working from home
223 #define SIPE_SOAP_SET_PRESENCE_NOTE_XML "<note>%s</note>"
226 * Note's OOF publication entry
228 #define SIPE_SOAP_SET_PRESENCE_OOF_XML "<oof></oof>"
231 * States publication entry for User State
233 * @param avail (%d) Availability 2007-style. Ex.: 9500
234 * @param since_time_str (%s) Ex.: 2010-01-13T10:30:05Z
235 * @param device_id (%s) epid. Ex.: 4c77e6ec72
236 * @param activity_token (%s) Ex.: do-not-disturb
238 #define SIPE_SOAP_SET_PRESENCE_STATES \
239 "<states>"\
240 "<state avail=\"%d\" since=\"%s\" validWith=\"any-device\" deviceId=\"%s\" set=\"manual\" xsi:type=\"userState\">%s</state>"\
241 "</states>"
244 * Presentity publication entry.
246 * @param uri (%s) SIP URI without 'sip:' prefix. Ex.: fox@atlanta.local
247 * @param aggr_availability (%d) Ex.: 300
248 * @param aggr_activity (%d) Ex.: 600
249 * @param host_name (%s) Uppercased. Ex.: ATLANTA
250 * @param note_xml_str (%s) XML string as SIPE_SOAP_SET_PRESENCE_NOTE_XML
251 * @param oof_xml_str (%s) XML string as SIPE_SOAP_SET_PRESENCE_OOF_XML
252 * @param states_xml_str (%s) XML string as SIPE_SOAP_SET_PRESENCE_STATES
253 * @param calendar_info_xml_str (%s) XML string as SIPE_SOAP_SET_PRESENCE_CALENDAR
254 * @param device_id (%s) epid. Ex.: 4c77e6ec72
255 * @param since_time_str (%s) Ex.: 2010-01-13T10:30:05Z
256 * @param since_time_str (%s) Ex.: 2010-01-13T10:30:05Z
257 * @param user_input (%s) active, idle
259 #define SIPE_SOAP_SET_PRESENCE \
260 "<s:Envelope" \
261 " xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"" \
262 " xmlns:m=\"http://schemas.microsoft.com/winrtc/2002/11/sip\"" \
263 ">" \
264 "<s:Body>" \
265 "<m:setPresence>" \
266 "<m:presentity xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" m:uri=\"sip:%s\">"\
267 "<m:availability m:aggregate=\"%d\"/>"\
268 "<m:activity m:aggregate=\"%d\"/>"\
269 "<deviceName xmlns=\"http://schemas.microsoft.com/2002/09/sip/presence\" name=\"%s\"/>"\
270 "<rtc:devicedata xmlns:rtc=\"http://schemas.microsoft.com/winrtc/2002/11/sip\" namespace=\"rtcService\">"\
271 "<![CDATA[<caps><renders_gif/><renders_isf/></caps>]]></rtc:devicedata>"\
272 "<userInfo xmlns=\"http://schemas.microsoft.com/2002/09/sip/presence\">"\
273 "%s%s" \
274 "%s" \
275 "</userInfo>"\
276 "%s" \
277 "<device xmlns=\"http://schemas.microsoft.com/2002/09/sip/presence\" deviceId=\"%s\" since=\"%s\" >"\
278 "<userInput since=\"%s\" >%s</userInput>"\
279 "</device>"\
280 "</m:presentity>" \
281 "</m:setPresence>"\
282 "</s:Body>" \
283 "</s:Envelope>"
285 static void send_presence_soap(struct sipe_core_private *sipe_private,
286 gboolean do_publish_calendar,
287 gboolean do_reset_status)
289 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
290 struct sipe_calendar* cal = sip->cal;
291 gchar *body;
292 gchar *tmp;
293 gchar *tmp2 = NULL;
294 gchar *res_note = NULL;
295 gchar *res_oof = NULL;
296 const gchar *note_pub = NULL;
297 gchar *states = NULL;
298 gchar *calendar_data = NULL;
299 gchar *epid = get_epid(sipe_private);
300 gchar *from = sip_uri_self(sipe_private);
301 time_t now = time(NULL);
302 gchar *since_time_str = sipe_utils_time_to_str(now);
303 const gchar *oof_note = cal ? sipe_ews_get_oof_note(cal) : NULL;
304 const char *user_input;
305 gboolean pub_oof = cal && oof_note && (!sip->note || cal->updated > sip->note_since);
307 if (oof_note && sip->note) {
308 SIPE_DEBUG_INFO("cal->oof_start : %s", asctime(localtime(&(cal->oof_start))));
309 SIPE_DEBUG_INFO("sip->note_since : %s", asctime(localtime(&(sip->note_since))));
312 SIPE_DEBUG_INFO("sip->note : %s", sip->note ? sip->note : "");
314 if (!sip->initial_state_published ||
315 do_reset_status)
316 sipe_status_set_activity(sipe_private, SIPE_ACTIVITY_AVAILABLE);
318 /* Note */
319 if (pub_oof) {
320 note_pub = oof_note;
321 res_oof = SIPE_SOAP_SET_PRESENCE_OOF_XML;
322 cal->published = TRUE;
323 } else if (sip->note) {
324 if (SIPE_CORE_PRIVATE_FLAG_IS(OOF_NOTE) &&
325 !oof_note) { /* stale OOF note, as it's not present in cal already */
326 g_free(sip->note);
327 sip->note = NULL;
328 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
329 sip->note_since = 0;
330 } else {
331 note_pub = sip->note;
332 res_oof = SIPE_CORE_PRIVATE_FLAG_IS(OOF_NOTE) ? SIPE_SOAP_SET_PRESENCE_OOF_XML : "";
336 if (note_pub)
338 /* to protocol internal plain text format */
339 tmp = sipe_backend_markup_strip_html(note_pub);
340 res_note = g_markup_printf_escaped(SIPE_SOAP_SET_PRESENCE_NOTE_XML, tmp);
341 g_free(tmp);
344 /* User State */
345 if (!do_reset_status) {
346 if (sipe_status_changed_by_user(sipe_private) && !do_publish_calendar && sip->initial_state_published)
348 const gchar *activity_token;
349 int avail_2007 = sipe_ocs2007_availability_from_status(sip->status,
350 &activity_token);
352 states = g_strdup_printf(SIPE_SOAP_SET_PRESENCE_STATES,
353 avail_2007,
354 since_time_str,
355 epid,
356 activity_token);
358 else /* preserve existing publication */
360 if (sip->user_states) {
361 states = g_strdup(sip->user_states);
364 } else {
365 /* do nothing - then User state will be erased */
367 sip->initial_state_published = TRUE;
369 /* CalendarInfo */
370 if (cal && (!is_empty(cal->legacy_dn) || !is_empty(cal->email)) && cal->fb_start && !is_empty(cal->free_busy))
372 char *fb_start_str = sipe_utils_time_to_str(cal->fb_start);
373 char *free_busy_base64 = sipe_cal_get_freebusy_base64(cal->free_busy);
374 calendar_data = g_strdup_printf(SIPE_SOAP_SET_PRESENCE_CALENDAR,
375 !is_empty(cal->legacy_dn) ? cal->legacy_dn : cal->email,
376 fb_start_str,
377 free_busy_base64);
378 g_free(fb_start_str);
379 g_free(free_busy_base64);
382 user_input = (sipe_status_changed_by_user(sipe_private) ||
383 sipe_is_user_available(sipe_private)) ?
384 "active" : "idle";
386 /* generate XML */
387 body = g_strdup_printf(SIPE_SOAP_SET_PRESENCE,
388 sipe_private->username,
389 sipe_ocs2005_availability_from_status(sipe_private),
390 sipe_ocs2005_activity_from_status(sipe_private),
391 (tmp = g_ascii_strup(g_get_host_name(), -1)),
392 res_note ? res_note : "",
393 res_oof ? res_oof : "",
394 states ? states : "",
395 calendar_data ? calendar_data : "",
396 epid,
397 since_time_str,
398 since_time_str,
399 user_input);
400 g_free(tmp);
401 g_free(tmp2);
402 g_free(res_note);
403 g_free(states);
404 g_free(calendar_data);
405 g_free(since_time_str);
406 g_free(epid);
408 sip_soap_raw_request_cb(sipe_private, from, body, NULL, NULL);
410 g_free(body);
413 void sipe_ocs2005_presence_publish(struct sipe_core_private *sipe_private,
414 gboolean do_publish_calendar)
416 return send_presence_soap(sipe_private, do_publish_calendar, FALSE);
419 void sipe_ocs2005_reset_status(struct sipe_core_private *sipe_private)
421 return send_presence_soap(sipe_private, FALSE, TRUE);
424 void sipe_ocs2005_apply_calendar_status(struct sipe_core_private *sipe_private,
425 struct sipe_buddy *sbuddy,
426 const char *status_id)
428 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
429 time_t cal_avail_since;
430 int cal_status = sipe_cal_get_status(sbuddy, time(NULL), &cal_avail_since);
431 int avail;
432 gchar *self_uri;
434 if (!sbuddy) return;
436 if (cal_status < SIPE_CAL_NO_DATA) {
437 SIPE_DEBUG_INFO("sipe_apply_calendar_status: cal_status : %d for %s", cal_status, sbuddy->name);
438 SIPE_DEBUG_INFO("sipe_apply_calendar_status: cal_avail_since : %s", asctime(localtime(&cal_avail_since)));
441 /* scheduled Cal update call */
442 if (!status_id) {
443 status_id = sbuddy->last_non_cal_status_id;
444 g_free(sbuddy->activity);
445 sbuddy->activity = g_strdup(sbuddy->last_non_cal_activity);
448 if (!status_id) {
449 SIPE_DEBUG_INFO("sipe_apply_calendar_status: status_id is NULL for %s, exiting.",
450 sbuddy->name ? sbuddy->name : "" );
451 return;
454 /* adjust to calendar status */
455 if (cal_status != SIPE_CAL_NO_DATA) {
456 SIPE_DEBUG_INFO("sipe_apply_calendar_status: user_avail_since: %s", asctime(localtime(&sbuddy->user_avail_since)));
458 if ((cal_status == SIPE_CAL_BUSY) &&
459 (cal_avail_since > sbuddy->user_avail_since) &&
460 sipe_ocs2007_status_is_busy(status_id)) {
461 status_id = sipe_backend_activity_to_token(SIPE_ACTIVITY_BUSY);
462 g_free(sbuddy->activity);
463 sbuddy->activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_IN_MEETING));
465 avail = sipe_ocs2007_availability_from_status(status_id, NULL);
467 SIPE_DEBUG_INFO("sipe_apply_calendar_status: activity_since : %s", asctime(localtime(&sbuddy->activity_since)));
468 if (cal_avail_since > sbuddy->activity_since) {
469 if ((cal_status == SIPE_CAL_OOF) &&
470 sipe_ocs2007_availability_is_away2(avail)) {
471 g_free(sbuddy->activity);
472 sbuddy->activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_OOF));
477 /* then set status_id actually */
478 SIPE_DEBUG_INFO("sipe_apply_calendar_status: to %s for %s", status_id, sbuddy->name ? sbuddy->name : "" );
479 sipe_backend_buddy_set_status(SIPE_CORE_PUBLIC, sbuddy->name, status_id);
481 /* set our account state to the one in roaming (including calendar info) */
482 self_uri = sip_uri_self(sipe_private);
483 if (sip->initial_state_published && sipe_strcase_equal(sbuddy->name, self_uri)) {
484 if (sipe_strequal(status_id, sipe_backend_activity_to_token(SIPE_ACTIVITY_OFFLINE))) {
485 /* do not let offline status switch us off */
486 status_id = sipe_backend_activity_to_token(SIPE_ACTIVITY_INVISIBLE);
489 sipe_status_and_note(sipe_private, status_id);
491 g_free(self_uri);
494 static void update_calendar_status_cb(SIPE_UNUSED_PARAMETER char *name,
495 struct sipe_buddy *sbuddy,
496 struct sipe_core_private *sipe_private)
498 sipe_ocs2005_apply_calendar_status(sipe_private, sbuddy, NULL);
502 * Updates contact's status
503 * based on their calendar information.
505 static void update_calendar_status(struct sipe_core_private *sipe_private,
506 SIPE_UNUSED_PARAMETER void *unused)
508 SIPE_DEBUG_INFO_NOFORMAT("update_calendar_status() started.");
509 g_hash_table_foreach(sipe_private->buddies,
510 (GHFunc)update_calendar_status_cb,
511 sipe_private);
513 /* repeat scheduling */
514 sipe_ocs2005_schedule_status_update(sipe_private,
515 time(NULL) + 3 * 60 /* 3 min */);
519 * Schedules process of contacts' status update
520 * based on their calendar information.
521 * Should be scheduled to the beginning of every
522 * 15 min interval, like:
523 * 13:00, 13:15, 13:30, 13:45, etc.
525 void sipe_ocs2005_schedule_status_update(struct sipe_core_private *sipe_private,
526 time_t calculate_from)
528 #define SCHEDULE_INTERVAL 15 * 60 /* 15 min */
530 /* start of the beginning of closest 15 min interval. */
531 time_t next_start = (calculate_from / SCHEDULE_INTERVAL + 1) * SCHEDULE_INTERVAL;
533 SIPE_DEBUG_INFO("sipe_ocs2005_schedule_status_update: calculate_from time: %s",
534 asctime(localtime(&calculate_from)));
535 SIPE_DEBUG_INFO("sipe_ocs2005_schedule_status_update: next start time : %s",
536 asctime(localtime(&next_start)));
538 sipe_schedule_seconds(sipe_private,
539 "<+2005-cal-status>",
540 NULL,
541 next_start - time(NULL),
542 update_calendar_status,
543 NULL);
547 Local Variables:
548 mode: c
549 c-file-style: "bsd"
550 indent-tabs-mode: t
551 tab-width: 8
552 End: