2 * Copyright (C) 2006 Voice Sistem S.R.L.
4 * This file is part of Kamailio, a free SIP server.
6 * Kamailio is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version
11 * Kamailio is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * \defgroup presence Presence :: A generic implementation of the SIP event package (PUBLISH, SUBSCRIBE, NOTIFY)
24 * The Kamailio presence module is a generic module for SIP event packages, which is much more than presence.
25 * It is extensible by developing other modules that use the internal developer API.
33 * \brief Kamailio presence module :: Core
41 #include <sys/types.h>
47 #include "../../lib/srdb1/db.h"
48 #include "../../core/sr_module.h"
49 #include "../../core/dprint.h"
50 #include "../../core/error.h"
51 #include "../../core/ut.h"
52 #include "../../core/parser/parse_to.h"
53 #include "../../core/parser/parse_uri.h"
54 #include "../../core/parser/parse_content.h"
55 #include "../../core/parser/parse_from.h"
56 #include "../../core/mem/mem.h"
57 #include "../../core/mem/shm_mem.h"
58 #include "../../core/usr_avp.h"
59 #include "../../core/rand/kam_rand.h"
60 #include "../../modules/tm/tm_load.h"
61 #include "../../modules/sl/sl.h"
62 #include "../../core/pt.h"
63 #include "../../core/hashes.h"
64 #include "../pua/hash.h"
67 #include "subscribe.h"
68 #include "event_list.h"
69 #include "bind_presence.h"
71 #include "presence_dmq.h"
72 #include "../../core/mod_fix.h"
73 #include "../../core/kemi.h"
74 #include "../../core/timer_proc.h"
75 #include "../../core/rpc.h"
76 #include "../../core/rpc_lookup.h"
80 #define S_TABLE_VERSION 3
81 #define P_TABLE_VERSION 5
82 #define ACTWATCH_TABLE_VERSION 12
84 static int pres_clean_period = 100;
85 static int pres_db_update_period = 100;
86 int pres_local_log_level = L_INFO;
88 static char *pres_log_facility_str =
89 0; /*!< Syslog: log facility that is used */
90 int pres_local_log_facility;
92 /* database connection */
93 db1_con_t *pa_db = NULL;
95 str presentity_table = str_init("presentity");
96 str active_watchers_table = str_init("active_watchers");
97 str watchers_table = str_init("watchers");
99 int pres_fetch_rows = 500;
100 static int pres_library_mode = 0;
101 str pres_server_address = {0, 0};
102 evlist_t *pres_evlist = NULL;
103 int pres_subs_remove_match = 0;
104 int _pres_subs_mode = 1;
105 static int pres_timer_mode = 1;
107 int pres_uri_match = 0;
108 /* sip uri match function pointer */
109 sip_uri_match_f presence_sip_uri_match;
110 static int sip_uri_case_sensitive_match(str *s1, str *s2);
111 static int sip_uri_case_insensitive_match(str *s1, str *s2);
115 /* SL API structure */
118 /** module functions */
120 static int mod_init(void);
121 static int child_init(int);
122 static void destroy(void);
123 int stored_pres_info(struct sip_msg *msg, char *pres_uri, char *s);
124 static int fixup_presence(void **param, int param_no);
125 static int fixup_subscribe(void **param, int param_no);
126 static int update_pw_dialogs(
127 subs_t *subs, unsigned int hash_code, subs_t **subs_array);
128 static int w_pres_auth_status(struct sip_msg *_msg, char *_sp1, char *_sp2);
129 static int w_pres_refresh_watchers(
130 struct sip_msg *msg, char *puri, char *pevent, char *ptype);
131 static int w_pres_refresh_watchers5(struct sip_msg *msg, char *puri,
132 char *pevent, char *ptype, char *furi, char *fname);
133 static int w_pres_update_watchers(
134 struct sip_msg *msg, char *puri, char *pevent);
135 static int fixup_refresh_watchers(void **param, int param_no);
136 static int fixup_update_watchers(void **param, int param_no);
137 static int presence_init_rpc(void);
139 static int w_pres_has_subscribers(struct sip_msg *_msg, char *_sp1, char *_sp2);
140 static int fixup_has_subscribers(void **param, int param_no);
142 int pres_counter = 0;
144 char pres_prefix = 'a';
145 int pres_startup_time = 0;
146 str pres_db_url = {0, 0};
147 int pres_expires_offset = 0;
148 int pres_cseq_offset = 0;
149 uint32_t pres_min_expires = 0;
150 int pres_min_expires_action = 1;
151 uint32_t pres_max_expires = 3600;
152 int shtable_size = 9;
153 shtable_t subs_htable = NULL;
154 int pres_subs_dbmode = WRITE_BACK;
155 int pres_sphere_enable = 0;
156 int pres_timeout_rm_subs = 1;
157 int pres_send_fast_notify = 1;
158 int publ_cache_mode = PS_PCACHE_HYBRID;
159 int pres_waitn_time = 5;
160 int pres_notifier_poll_rate = 10;
161 int pres_notifier_processes = 1;
162 int pres_force_delete = 0;
163 int pres_startup_mode = 1;
164 str pres_xavp_cfg = {0};
165 int pres_retrieve_order = 0;
166 str pres_retrieve_order_by = str_init("priority");
167 int pres_enable_dmq = 0;
168 int pres_delete_same_subs = 0;
170 int pres_db_table_lock_type = 1;
171 db_locking_t pres_db_table_lock = DB_LOCKING_WRITE;
173 int *pres_notifier_id = NULL;
175 int phtable_size = 9;
176 phtable_t *pres_htable = NULL;
180 /* clang-format off */
181 static cmd_export_t cmds[]=
183 {"handle_publish", (cmd_function)w_handle_publish, 0,
184 fixup_presence, 0, REQUEST_ROUTE},
185 {"handle_publish", (cmd_function)w_handle_publish, 1,
186 fixup_presence, 0, REQUEST_ROUTE},
187 {"handle_subscribe", (cmd_function)handle_subscribe0, 0,
188 fixup_subscribe, 0, REQUEST_ROUTE},
189 {"handle_subscribe", (cmd_function)w_handle_subscribe, 1,
190 fixup_subscribe, 0, REQUEST_ROUTE},
191 {"pres_auth_status", (cmd_function)w_pres_auth_status, 2,
192 fixup_spve_spve, fixup_free_spve_spve, REQUEST_ROUTE},
193 {"pres_refresh_watchers", (cmd_function)w_pres_refresh_watchers, 3,
194 fixup_refresh_watchers, 0, ANY_ROUTE},
195 {"pres_refresh_watchers", (cmd_function)w_pres_refresh_watchers5,5,
196 fixup_refresh_watchers, 0, ANY_ROUTE},
197 {"pres_update_watchers", (cmd_function)w_pres_update_watchers, 2,
198 fixup_update_watchers, 0, ANY_ROUTE},
199 {"pres_has_subscribers", (cmd_function)w_pres_has_subscribers, 2,
200 fixup_has_subscribers, 0, ANY_ROUTE},
201 {"bind_presence", (cmd_function)bind_presence, 1,
205 /* clang-format on */
207 /* clang-format off */
208 static param_export_t params[]={
209 { "db_url", PARAM_STR, &pres_db_url},
210 { "presentity_table", PARAM_STR, &presentity_table},
211 { "active_watchers_table", PARAM_STR, &active_watchers_table},
212 { "watchers_table", PARAM_STR, &watchers_table},
213 { "clean_period", INT_PARAM, &pres_clean_period },
214 { "db_update_period", INT_PARAM, &pres_db_update_period },
215 { "waitn_time", INT_PARAM, &pres_waitn_time },
216 { "notifier_poll_rate", INT_PARAM, &pres_notifier_poll_rate },
217 { "notifier_processes", INT_PARAM, &pres_notifier_processes },
218 { "force_delete", INT_PARAM, &pres_force_delete },
219 { "startup_mode", INT_PARAM, &pres_startup_mode },
220 { "expires_offset", INT_PARAM, &pres_expires_offset },
221 { "max_expires", INT_PARAM, &pres_max_expires },
222 { "min_expires", INT_PARAM, &pres_min_expires },
223 { "min_expires_action", INT_PARAM, &pres_min_expires_action },
224 { "server_address", PARAM_STR, &pres_server_address},
225 { "subs_htable_size", INT_PARAM, &shtable_size},
226 { "pres_htable_size", INT_PARAM, &phtable_size},
227 { "subs_db_mode", INT_PARAM, &pres_subs_dbmode},
228 { "publ_cache", INT_PARAM, &publ_cache_mode},
229 { "enable_sphere_check", INT_PARAM, &pres_sphere_enable},
230 { "timeout_rm_subs", INT_PARAM, &pres_timeout_rm_subs},
231 { "send_fast_notify", INT_PARAM, &pres_send_fast_notify},
232 { "fetch_rows", INT_PARAM, &pres_fetch_rows},
233 { "db_table_lock_type", INT_PARAM, &pres_db_table_lock_type},
234 { "local_log_level", PARAM_INT, &pres_local_log_level},
235 { "local_log_facility", PARAM_STR, &pres_log_facility_str},
236 { "subs_remove_match", PARAM_INT, &pres_subs_remove_match},
237 { "xavp_cfg", PARAM_STR, &pres_xavp_cfg},
238 { "retrieve_order", PARAM_INT, &pres_retrieve_order},
239 { "retrieve_order_by", PARAM_STR, &pres_retrieve_order_by},
240 { "sip_uri_match", PARAM_INT, &pres_uri_match},
241 { "cseq_offset", PARAM_INT, &pres_cseq_offset},
242 { "enable_dmq", PARAM_INT, &pres_enable_dmq},
243 { "pres_subs_mode", PARAM_INT, &_pres_subs_mode},
244 { "delete_same_subs", PARAM_INT, &pres_delete_same_subs},
245 { "timer_mode", PARAM_INT, &pres_timer_mode},
249 /* clang-format on */
251 /* clang-format off */
252 static pv_export_t pres_mod_pvs[] = {
253 { {"subs", (sizeof("subs")-1)}, PVT_OTHER,
254 pv_get_subscription, 0, pv_parse_subscription_name, 0, 0, 0},
255 { {"notify_reply", (sizeof("notify_reply")-1)}, PVT_OTHER,
256 pv_get_notify_reply, 0, pv_parse_notify_reply_var_name, 0, 0, 0},
257 { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
259 /* clang-format on */
261 /** module exports */
262 /* clang-format off */
263 struct module_exports exports= {
264 "presence", /* module name */
265 DEFAULT_DLFLAGS, /* dlopen flags */
266 cmds, /* exported functions */
267 params, /* exported parameters */
268 0, /* RPC method exports */
269 pres_mod_pvs, /* exported pseudo-variables */
270 0, /* response handling function */
271 mod_init, /* module initialization function */
272 child_init, /* per-child init function */
273 destroy /* module destroy function */
275 /* clang-format on */
278 * init module function
280 static int mod_init(void)
282 if(pres_uri_match == 1) {
283 presence_sip_uri_match = sip_uri_case_insensitive_match;
285 presence_sip_uri_match = sip_uri_case_sensitive_match;
288 if(presence_init_rpc() != 0) {
289 LM_ERR("failed to register RPC commands\n");
293 LM_DBG("db_url=%s (len=%d addr=%p)\n", ZSW(pres_db_url.s), pres_db_url.len,
296 if(pres_db_url.s == NULL || pres_db_url.len == 0) {
297 LM_DBG("db url is not set - switch to library mode\n");
298 pres_library_mode = 1;
301 pres_evlist = init_evlist();
303 LM_ERR("unsuccessful initialize event list\n");
307 if(pres_library_mode == 1) {
308 LM_DBG("Presence module used for API library purpose only\n");
312 if(sruid_init(&pres_sruid, '-', "pres", SRUID_INC) < 0) {
316 if(pres_expires_offset < 0) {
317 pres_expires_offset = 0;
320 if(pres_max_expires <= 0) {
321 pres_max_expires = 3600;
324 if(pres_min_expires > pres_max_expires) {
325 pres_min_expires = pres_max_expires;
328 if(pres_min_expires_action < 1 || pres_min_expires_action > 2) {
329 LM_ERR("min_expires_action must be 1 = RFC 6665/3261 Reply 423, 2 = "
330 "force min_expires value\n");
334 if(pres_server_address.s == NULL || pres_server_address.len==0) {
335 LM_DBG("server_address parameter not set in configuration file\n");
338 /* bind the SL API */
339 if(sl_load_api(&slb) != 0) {
340 LM_ERR("cannot bind to SL API\n");
344 /* load all TM stuff */
345 if(load_tm_api(&tmb) == -1) {
346 LM_ERR("Can't load tm functions. Module TM not loaded?\n");
350 if(pres_db_url.s == NULL) {
351 LM_ERR("database url not set!\n");
355 /* binding to database module */
356 if(db_bind_mod(&pres_db_url, &pa_dbf)) {
357 LM_ERR("Database module not found\n");
361 if(!DB_CAPABILITY(pa_dbf, DB_CAP_ALL)) {
362 LM_ERR("Database module does not implement all functions"
363 " needed by presence module\n");
367 pa_db = pa_dbf.init(&pres_db_url);
369 LM_ERR("Connection to database failed\n");
373 /*verify table versions */
374 if((db_check_table_version(
375 &pa_dbf, pa_db, &presentity_table, P_TABLE_VERSION)
377 || (db_check_table_version(
378 &pa_dbf, pa_db, &watchers_table, S_TABLE_VERSION)
380 DB_TABLE_VERSION_ERROR(presentity_table);
384 if(pres_subs_dbmode != NO_DB
385 && db_check_table_version(&pa_dbf, pa_db, &active_watchers_table,
386 ACTWATCH_TABLE_VERSION)
388 DB_TABLE_VERSION_ERROR(active_watchers_table);
392 if(pres_subs_dbmode != DB_ONLY) {
396 shtable_size = 1 << shtable_size;
398 subs_htable = new_shtable(shtable_size);
399 if(subs_htable == NULL) {
400 LM_ERR(" initializing subscribe hash table\n");
403 if(restore_db_subs() < 0) {
404 LM_ERR("restoring subscribe info from database\n");
409 if(publ_cache_mode==PS_PCACHE_HYBRID) {
413 phtable_size = 1 << phtable_size;
415 pres_htable = new_phtable();
416 if(pres_htable == NULL) {
417 LM_ERR("initializing presentity hash table\n");
421 if(pres_htable_restore() < 0) {
422 LM_ERR("filling in presentity hash table from database\n");
427 pres_startup_time = (int)time(NULL);
428 if(pres_clean_period > 0) {
429 if(pres_timer_mode==0) {
430 register_timer(msg_presentity_clean, 0, pres_clean_period);
431 register_timer(msg_watchers_clean, 0, pres_clean_period);
433 sr_wtimer_add(msg_presentity_clean, 0, pres_clean_period);
434 sr_wtimer_add(msg_watchers_clean, 0, pres_clean_period);
438 if(pres_db_update_period > 0) {
439 if(pres_timer_mode==0) {
440 register_timer(timer_db_update, 0, pres_db_update_period);
442 sr_wtimer_add(timer_db_update, 0, pres_db_update_period);
446 if(pres_waitn_time <= 0) {
450 if(pres_notifier_poll_rate <= 0) {
451 pres_notifier_poll_rate = 10;
454 if(pres_notifier_processes < 0 || pres_subs_dbmode != DB_ONLY) {
455 pres_notifier_processes = 0;
458 if(pres_notifier_processes > 0) {
459 if((pres_notifier_id =
460 shm_malloc(sizeof(int) * pres_notifier_processes))
462 LM_ERR("allocating shared memory\n");
466 register_basic_timers(pres_notifier_processes);
469 if(pres_force_delete > 0)
470 pres_force_delete = 1;
472 if(pres_log_facility_str) {
473 int tmp = str2facility(pres_log_facility_str);
476 pres_local_log_facility = tmp;
478 LM_ERR("invalid log facility configured\n");
482 pres_local_log_facility = cfg_get(core, core_cfg, log_facility);
485 if(pres_db_table_lock_type != 1) {
486 pres_db_table_lock = DB_LOCKING_NONE;
492 goto_on_notify_reply = route_lookup(&event_rt, "presence:notify-reply");
493 if(goto_on_notify_reply >= 0 && event_rt.rlist[goto_on_notify_reply] == 0)
494 goto_on_notify_reply = -1; /* disable */
496 if(pres_enable_dmq > 0 && pres_dmq_initialize() != 0) {
497 LM_ERR("failed to initialize dmq integration\n");
510 * Initialize children
512 static int child_init(int rank)
514 if(rank == PROC_INIT || rank == PROC_TCP_MAIN) {
520 if(pres_library_mode) {
524 if(sruid_init(&pres_sruid, '-', "pres", SRUID_INC) < 0) {
528 if(rank == PROC_MAIN) {
531 for(i = 0; i < pres_notifier_processes; i++) {
533 snprintf(tmp, 21, "PRESENCE NOTIFIER %d", i);
534 pres_notifier_id[i] = i;
536 if(fork_basic_utimer(PROC_TIMER, tmp, 1, pres_timer_send_notify,
537 &pres_notifier_id[i], 1000000 / pres_notifier_poll_rate)
539 LM_ERR("Failed to start PRESENCE NOTIFIER %d\n", i);
547 if(pa_dbf.init == 0) {
548 LM_CRIT("child_init: database not bound\n");
551 /* Do not pool the connections where possible when running notifier
553 if(pres_notifier_processes > 0 && pa_dbf.init2)
554 pa_db = pa_dbf.init2(&pres_db_url, DB_POOLING_NONE);
556 pa_db = pa_dbf.init(&pres_db_url);
558 LM_ERR("child %d: unsuccessful connecting to database\n", rank);
562 if(pa_dbf.use_table(pa_db, &presentity_table) < 0) {
563 LM_ERR("child %d:unsuccessful use_table presentity_table\n", rank);
567 if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
568 LM_ERR("child %d:unsuccessful use_table active_watchers_table\n", rank);
572 if(pa_dbf.use_table(pa_db, &watchers_table) < 0) {
573 LM_ERR("child %d:unsuccessful use_table watchers_table\n", rank);
577 LM_DBG("child %d: Database connection opened successfully\n", rank);
586 static void destroy(void)
588 if(subs_htable && pres_subs_dbmode == WRITE_BACK) {
589 /* open database connection */
590 pa_db = pa_dbf.init(&pres_db_url);
592 LM_ERR("mod_destroy: unsuccessful connecting to database\n");
594 timer_db_update(0, 0);
598 destroy_shtable(subs_htable, shtable_size);
603 if(pa_db && pa_dbf.close)
606 if(pres_notifier_id != NULL)
607 shm_free(pres_notifier_id);
612 static int fixup_presence(void **param, int param_no)
614 if(pres_library_mode) {
615 LM_ERR("Bad config - you can not call 'handle_publish' function"
616 " (db_url not set)\n");
622 return fixup_spve_null(param, 1);
625 static int fixup_subscribe(void **param, int param_no)
628 if(pres_library_mode) {
629 LM_ERR("Bad config - you can not call 'handle_subscribe' function"
630 " (db_url not set)\n");
634 return fixup_spve_null(param, 1);
639 int pres_refresh_watchers(
640 str *pres, str *event, int type, str *file_uri, str *filename)
644 str *rules_doc = NULL;
647 ev = contains_event(event, NULL);
649 LM_ERR("wrong event parameter\n");
654 /* if a request to refresh watchers authorization */
655 if(ev->get_rules_doc == NULL) {
656 LM_ERR("wrong request for a refresh watchers authorization status"
657 "for an event that does not require authorization\n");
661 if(parse_uri(pres->s, pres->len, &uri) < 0) {
662 LM_ERR("parsing uri [%.*s]\n", pres->len, pres->s);
666 result = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
667 if(result < 0 || rules_doc == NULL || rules_doc->s == NULL) {
668 LM_ERR("no rules doc found for the user\n");
672 if(update_watchers_status(pres, ev, rules_doc) < 0) {
673 LM_ERR("failed to update watchers\n");
677 pkg_free(rules_doc->s);
683 if(update_hard_presentity(pres, ev, file_uri, filename) < 0) {
684 LM_ERR("updating hard presentity\n");
689 /* if a request to refresh notified info */
690 if(query_db_notify(pres, ev, NULL) < 0) {
691 LM_ERR("sending Notify requests\n");
700 pkg_free(rules_doc->s);
706 int _api_pres_refresh_watchers(str *pres, str *event, int type)
708 return pres_refresh_watchers(pres, event, type, NULL, NULL);
711 int ki_pres_refresh_watchers(sip_msg_t *msg, str *pres, str *event, int type)
713 return pres_refresh_watchers(pres, event, type, NULL, NULL);
716 int ki_pres_refresh_watchers_file(sip_msg_t *msg, str *pres, str *event,
717 int type, str *file_uri, str *filename)
719 return pres_refresh_watchers(pres, event, type, file_uri, filename);
722 int pres_update_status(subs_t *subs, str reason, db_key_t *query_cols,
723 db_val_t *query_vals, int n_query_cols, subs_t **subs_array)
725 db_key_t update_cols[5];
726 db_val_t update_vals[5];
727 int n_update_cols = 0;
728 int u_status_col, u_reason_col, q_wuser_col, q_wdomain_col;
730 query_cols[q_wuser_col = n_query_cols] = &str_watcher_username_col;
731 query_vals[n_query_cols].nul = 0;
732 query_vals[n_query_cols].type = DB1_STR;
735 query_cols[q_wdomain_col = n_query_cols] = &str_watcher_domain_col;
736 query_vals[n_query_cols].nul = 0;
737 query_vals[n_query_cols].type = DB1_STR;
740 update_cols[u_status_col = n_update_cols] = &str_status_col;
741 update_vals[u_status_col].nul = 0;
742 update_vals[u_status_col].type = DB1_INT;
745 update_cols[u_reason_col = n_update_cols] = &str_reason_col;
746 update_vals[u_reason_col].nul = 0;
747 update_vals[u_reason_col].type = DB1_STR;
750 status = subs->status;
751 if(subs->event->get_auth_status(subs) < 0) {
752 LM_ERR("getting status from rules document\n");
755 LM_DBG("subs.status= %d\n", subs->status);
756 if(get_status_str(subs->status) == NULL) {
757 LM_ERR("wrong status: %d\n", subs->status);
761 if(subs->status != status || reason.len != subs->reason.len
762 || (reason.s && subs->reason.s
763 && strncmp(reason.s, subs->reason.s, reason.len))) {
764 /* update in watchers_table */
765 query_vals[q_wuser_col].val.str_val = subs->watcher_user;
766 query_vals[q_wdomain_col].val.str_val = subs->watcher_domain;
768 update_vals[u_status_col].val.int_val = subs->status;
769 update_vals[u_reason_col].val.str_val = subs->reason;
771 if(pa_dbf.use_table(pa_db, &watchers_table) < 0) {
772 LM_ERR("in use_table\n");
776 if(pa_dbf.update(pa_db, query_cols, 0, query_vals, update_cols,
777 update_vals, n_query_cols, n_update_cols)
779 LM_ERR("in sql update\n");
782 /* save in the list all affected dialogs */
783 /* if status switches to terminated -> delete dialog */
784 if(update_pw_dialogs(subs, subs->db_flag, subs_array) < 0) {
785 LM_ERR("extracting dialogs from [watcher]=%.*s@%.*s to"
786 " [presentity]=%.*s\n",
787 subs->watcher_user.len, subs->watcher_user.s,
788 subs->watcher_domain.len, subs->watcher_domain.s,
789 subs->pres_uri.len, subs->pres_uri.s);
796 int pres_db_delete_status(subs_t *s)
798 int n_query_cols = 0;
799 db_key_t query_cols[5];
800 db_val_t query_vals[5];
802 if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
803 LM_ERR("sql use table failed\n");
807 query_cols[n_query_cols] = &str_event_col;
808 query_vals[n_query_cols].nul = 0;
809 query_vals[n_query_cols].type = DB1_STR;
810 query_vals[n_query_cols].val.str_val = s->event->name;
813 query_cols[n_query_cols] = &str_presentity_uri_col;
814 query_vals[n_query_cols].nul = 0;
815 query_vals[n_query_cols].type = DB1_STR;
816 query_vals[n_query_cols].val.str_val = s->pres_uri;
819 query_cols[n_query_cols] = &str_watcher_username_col;
820 query_vals[n_query_cols].nul = 0;
821 query_vals[n_query_cols].type = DB1_STR;
822 query_vals[n_query_cols].val.str_val = s->watcher_user;
825 query_cols[n_query_cols] = &str_watcher_domain_col;
826 query_vals[n_query_cols].nul = 0;
827 query_vals[n_query_cols].type = DB1_STR;
828 query_vals[n_query_cols].val.str_val = s->watcher_domain;
831 if(pa_dbf.delete(pa_db, query_cols, 0, query_vals, n_query_cols) < 0) {
832 LM_ERR("sql delete failed\n");
838 int update_watchers_status(str *pres_uri, pres_ev_t *ev, str *rules_doc)
841 db_key_t query_cols[6], result_cols[5];
842 db_val_t query_vals[6];
843 int n_result_cols = 0, n_query_cols = 0;
844 db1_res_t *result = NULL;
848 str w_user, w_domain, reason = {0, 0};
850 int status_col, w_user_col, w_domain_col, reason_col;
851 subs_t *subs_array = NULL, *s;
852 unsigned int hash_code;
864 ws_t *ws_list = NULL;
868 if(ev->content_type.s == NULL) {
869 ev = contains_event(&ev->name, NULL);
871 LM_ERR("wrong event parameter\n");
876 memset(&subs, 0, sizeof(subs_t));
877 subs.pres_uri = *pres_uri;
879 subs.auth_rules_doc = rules_doc;
881 /* update in watchers_table */
882 query_cols[n_query_cols] = &str_presentity_uri_col;
883 query_vals[n_query_cols].nul = 0;
884 query_vals[n_query_cols].type = DB1_STR;
885 query_vals[n_query_cols].val.str_val = *pres_uri;
888 query_cols[n_query_cols] = &str_event_col;
889 query_vals[n_query_cols].nul = 0;
890 query_vals[n_query_cols].type = DB1_STR;
891 query_vals[n_query_cols].val.str_val = ev->name;
894 result_cols[status_col = n_result_cols++] = &str_status_col;
895 result_cols[reason_col = n_result_cols++] = &str_reason_col;
896 result_cols[w_user_col = n_result_cols++] = &str_watcher_username_col;
897 result_cols[w_domain_col = n_result_cols++] = &str_watcher_domain_col;
899 if(pa_dbf.use_table(pa_db, &watchers_table) < 0) {
900 LM_ERR("in use_table\n");
904 if(pa_dbf.query(pa_db, query_cols, 0, query_vals, result_cols, n_query_cols,
905 n_result_cols, 0, &result)
907 LM_ERR("in sql query\n");
918 LM_DBG("found %d record-uri in watchers_table\n", result->n);
919 hash_code = core_case_hash(pres_uri, &ev->name, shtable_size);
920 subs.db_flag = hash_code;
922 /* must do a copy as sphere_check requires database queries */
923 if(pres_sphere_enable) {
925 ws_list = (ws_t *)pkg_malloc(n * sizeof(ws_t));
926 if(ws_list == NULL) {
927 LM_ERR("No more private memory\n");
930 memset(ws_list, 0, n * sizeof(ws_t));
932 for(i = 0; i < result->n; i++) {
933 row = &result->rows[i];
934 row_vals = ROW_VALUES(row);
936 status = row_vals[status_col].val.int_val;
938 reason.s = (char *)row_vals[reason_col].val.string_val;
939 reason.len = reason.s ? strlen(reason.s) : 0;
941 w_user.s = (char *)row_vals[w_user_col].val.string_val;
942 w_user.len = strlen(w_user.s);
944 w_domain.s = (char *)row_vals[w_domain_col].val.string_val;
945 w_domain.len = strlen(w_domain.s);
948 ws_list[i].reason.s =
949 (char *)pkg_malloc(reason.len * sizeof(char));
950 if(ws_list[i].reason.s == NULL) {
951 LM_ERR("No more private memory\n");
954 memcpy(ws_list[i].reason.s, reason.s, reason.len);
955 ws_list[i].reason.len = reason.len;
957 ws_list[i].reason.s = NULL;
959 ws_list[i].w_user.s = (char *)pkg_malloc(w_user.len * sizeof(char));
960 if(ws_list[i].w_user.s == NULL) {
961 LM_ERR("No more private memory\n");
964 memcpy(ws_list[i].w_user.s, w_user.s, w_user.len);
965 ws_list[i].w_user.len = w_user.len;
967 ws_list[i].w_domain.s =
968 (char *)pkg_malloc(w_domain.len * sizeof(char));
969 if(ws_list[i].w_domain.s == NULL) {
970 LM_ERR("No more private memory\n");
973 memcpy(ws_list[i].w_domain.s, w_domain.s, w_domain.len);
974 ws_list[i].w_domain.len = w_domain.len;
976 ws_list[i].status = status;
979 pa_dbf.free_result(pa_db, result);
982 for(i = 0; i < n; i++) {
983 subs.watcher_user = ws_list[i].w_user;
984 subs.watcher_domain = ws_list[i].w_domain;
985 subs.status = ws_list[i].status;
986 memset(&subs.reason, 0, sizeof(str));
988 if(pres_update_status(&subs, reason, query_cols, query_vals,
989 n_query_cols, &subs_array)
991 LM_ERR("failed to update watcher status\n");
996 for(i = 0; i < n; i++) {
997 pkg_free(ws_list[i].w_user.s);
998 pkg_free(ws_list[i].w_domain.s);
999 if(ws_list[i].reason.s)
1000 pkg_free(ws_list[i].reason.s);
1008 for(i = 0; i < result->n; i++) {
1009 row = &result->rows[i];
1010 row_vals = ROW_VALUES(row);
1012 status = row_vals[status_col].val.int_val;
1014 reason.s = (char *)row_vals[reason_col].val.string_val;
1015 reason.len = reason.s ? strlen(reason.s) : 0;
1017 w_user.s = (char *)row_vals[w_user_col].val.string_val;
1018 w_user.len = strlen(w_user.s);
1020 w_domain.s = (char *)row_vals[w_domain_col].val.string_val;
1021 w_domain.len = strlen(w_domain.s);
1023 subs.watcher_user = w_user;
1024 subs.watcher_domain = w_domain;
1025 subs.status = status;
1026 memset(&subs.reason, 0, sizeof(str));
1028 if(pres_update_status(&subs, reason, query_cols, query_vals,
1029 n_query_cols, &subs_array)
1031 LM_ERR("failed to update watcher status\n");
1036 pa_dbf.free_result(pa_db, result);
1041 if(pres_notifier_processes == 0) {
1045 if(notify(s, NULL, NULL, 0, 0) < 0) {
1046 LM_ERR("sending Notify request\n");
1050 /* delete from database also */
1051 if(s->status == TERMINATED_STATUS) {
1052 if(pres_db_delete_status(s) < 0) {
1053 LM_ERR("failed to delete terminated "
1054 "dialog from database\n");
1063 free_subs_list(subs_array, PKG_MEM_TYPE, 0);
1068 pa_dbf.free_result(pa_db, result);
1069 free_subs_list(subs_array, PKG_MEM_TYPE, 0);
1071 for(i = 0; i < n; i++) {
1072 if(ws_list[i].w_user.s)
1073 pkg_free(ws_list[i].w_user.s);
1074 if(ws_list[i].w_domain.s)
1075 pkg_free(ws_list[i].w_domain.s);
1076 if(ws_list[i].reason.s)
1077 pkg_free(ws_list[i].reason.s);
1084 /********************************************************************************/
1086 static int update_pw_dialogs_dbonlymode(subs_t *subs, subs_t **subs_array)
1088 db_key_t query_cols[5], db_cols[3];
1089 db_val_t query_vals[5], db_vals[3];
1090 db_key_t result_cols[26];
1091 int n_query_cols = 0, n_result_cols = 0, n_update_cols = 0;
1092 int event_col, pres_uri_col, watcher_user_col, watcher_domain_col;
1093 int r_pres_uri_col, r_to_user_col, r_to_domain_col;
1094 int r_from_user_col, r_from_domain_col, r_callid_col;
1095 int r_to_tag_col, r_from_tag_col, r_sockinfo_col;
1096 int r_event_id_col, r_local_contact_col, r_contact_col;
1097 int r_record_route_col, r_reason_col;
1098 int r_event_col, r_local_cseq_col, r_remote_cseq_col;
1099 int r_status_col, r_version_col;
1100 int r_expires_col, r_watcher_user_col, r_watcher_domain_col;
1101 int r_flags_col, r_user_agent_col;
1102 db1_res_t *result = NULL;
1110 LM_ERR("null database connection\n");
1114 if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
1115 LM_ERR("use table failed\n");
1119 query_cols[event_col = n_query_cols] = &str_event_col;
1120 query_vals[event_col].nul = 0;
1121 query_vals[event_col].type = DB1_STR;
1122 query_vals[event_col].val.str_val = subs->event->name;
1125 query_cols[pres_uri_col = n_query_cols] = &str_presentity_uri_col;
1126 query_vals[pres_uri_col].nul = 0;
1127 query_vals[pres_uri_col].type = DB1_STR;
1128 query_vals[pres_uri_col].val.str_val = subs->pres_uri;
1131 query_cols[watcher_user_col = n_query_cols] = &str_watcher_username_col;
1132 query_vals[watcher_user_col].nul = 0;
1133 query_vals[watcher_user_col].type = DB1_STR;
1134 query_vals[watcher_user_col].val.str_val = subs->watcher_user;
1137 query_cols[watcher_domain_col = n_query_cols] = &str_watcher_domain_col;
1138 query_vals[watcher_domain_col].nul = 0;
1139 query_vals[watcher_domain_col].type = DB1_STR;
1140 query_vals[watcher_domain_col].val.str_val = subs->watcher_domain;
1144 result_cols[r_to_user_col = n_result_cols++] = &str_to_user_col;
1145 result_cols[r_to_domain_col = n_result_cols++] = &str_to_domain_col;
1146 result_cols[r_from_user_col = n_result_cols++] = &str_from_user_col;
1147 result_cols[r_from_domain_col = n_result_cols++] = &str_from_domain_col;
1148 result_cols[r_watcher_user_col = n_result_cols++] =
1149 &str_watcher_username_col;
1150 result_cols[r_watcher_domain_col = n_result_cols++] =
1151 &str_watcher_domain_col;
1152 result_cols[r_callid_col = n_result_cols++] = &str_callid_col;
1153 result_cols[r_to_tag_col = n_result_cols++] = &str_to_tag_col;
1154 result_cols[r_from_tag_col = n_result_cols++] = &str_from_tag_col;
1155 result_cols[r_sockinfo_col = n_result_cols++] = &str_socket_info_col;
1156 result_cols[r_event_id_col = n_result_cols++] = &str_event_id_col;
1157 result_cols[r_local_contact_col = n_result_cols++] = &str_local_contact_col;
1158 result_cols[r_record_route_col = n_result_cols++] = &str_record_route_col;
1159 result_cols[r_reason_col = n_result_cols++] = &str_reason_col;
1160 result_cols[r_local_cseq_col = n_result_cols++] = &str_local_cseq_col;
1161 result_cols[r_version_col = n_result_cols++] = &str_version_col;
1162 result_cols[r_expires_col = n_result_cols++] = &str_expires_col;
1163 result_cols[r_event_col = n_result_cols++] = &str_event_col;
1164 result_cols[r_pres_uri_col = n_result_cols++] = &str_presentity_uri_col;
1165 result_cols[r_contact_col = n_result_cols++] = &str_contact_col;
1167 /* these ones are unused for some reason !!! */
1168 result_cols[r_remote_cseq_col = n_result_cols++] = &str_remote_cseq_col;
1169 result_cols[r_status_col = n_result_cols++] = &str_status_col;
1170 /*********************************************/
1172 result_cols[r_flags_col = n_result_cols++] = &str_flags_col;
1173 result_cols[r_user_agent_col = n_result_cols++] = &str_user_agent_col;
1175 if(pa_dbf.query(pa_db, query_cols, 0, query_vals, result_cols, n_query_cols,
1176 n_result_cols, 0, &result)
1178 LM_ERR("Can't query db\n");
1180 pa_dbf.free_result(pa_db, result);
1187 nr_rows = RES_ROW_N(result);
1189 LM_DBG("found %d matching dialogs\n", nr_rows);
1192 pa_dbf.free_result(pa_db, result);
1196 rows = RES_ROWS(result);
1197 /* get the results and fill in return data structure */
1198 for(loop = 0; loop < nr_rows; loop++) {
1199 row_vals = ROW_VALUES(&rows[loop]);
1201 memset(&s, 0, sizeof(subs_t));
1202 s.status = subs->status;
1204 s.reason.s = subs->reason.s;
1205 s.reason.len = s.reason.s ? strlen(s.reason.s) : 0; //>>>>>>>>>>
1207 s.pres_uri.s = (char *)row_vals[r_pres_uri_col].val.string_val;
1208 s.pres_uri.len = s.pres_uri.s ? strlen(s.pres_uri.s) : 0;
1210 s.to_user.s = (char *)row_vals[r_to_user_col].val.string_val;
1211 s.to_user.len = s.to_user.s ? strlen(s.to_user.s) : 0;
1213 s.to_domain.s = (char *)row_vals[r_to_domain_col].val.string_val;
1214 s.to_domain.len = s.to_domain.s ? strlen(s.to_domain.s) : 0;
1216 s.from_user.s = (char *)row_vals[r_from_user_col].val.string_val;
1217 s.from_user.len = s.from_user.s ? strlen(s.from_user.s) : 0;
1219 s.from_domain.s = (char *)row_vals[r_from_domain_col].val.string_val;
1220 s.from_domain.len = s.from_domain.s ? strlen(s.from_domain.s) : 0;
1222 s.watcher_user.s = (char *)row_vals[r_watcher_user_col].val.string_val;
1223 s.watcher_user.len = s.watcher_user.s ? strlen(s.watcher_user.s) : 0;
1225 s.watcher_domain.s =
1226 (char *)row_vals[r_watcher_domain_col].val.string_val;
1227 s.watcher_domain.len =
1228 s.watcher_domain.s ? strlen(s.watcher_domain.s) : 0;
1230 s.event_id.s = (char *)row_vals[r_event_id_col].val.string_val;
1231 s.event_id.len = (s.event_id.s) ? strlen(s.event_id.s) : 0;
1233 s.to_tag.s = (char *)row_vals[r_to_tag_col].val.string_val;
1234 s.to_tag.len = s.to_tag.s ? strlen(s.to_tag.s) : 0;
1236 s.from_tag.s = (char *)row_vals[r_from_tag_col].val.string_val;
1237 s.from_tag.len = s.from_tag.s ? strlen(s.from_tag.s) : 0;
1239 s.callid.s = (char *)row_vals[r_callid_col].val.string_val;
1240 s.callid.len = s.callid.s ? strlen(s.callid.s) : 0;
1242 s.record_route.s = (char *)row_vals[r_record_route_col].val.string_val;
1243 s.record_route.len = (s.record_route.s) ? strlen(s.record_route.s) : 0;
1245 s.contact.s = (char *)row_vals[r_contact_col].val.string_val;
1246 s.contact.len = s.contact.s ? strlen(s.contact.s) : 0;
1248 s.sockinfo_str.s = (char *)row_vals[r_sockinfo_col].val.string_val;
1249 s.sockinfo_str.len = s.sockinfo_str.s ? strlen(s.sockinfo_str.s) : 0;
1252 (char *)row_vals[r_local_contact_col].val.string_val;
1253 s.local_contact.len = s.local_contact.s ? strlen(s.local_contact.s) : 0;
1255 ev_sname.s = (char *)row_vals[r_event_col].val.string_val;
1256 ev_sname.len = ev_sname.s ? strlen(ev_sname.s) : 0;
1258 s.event = contains_event(&ev_sname, NULL);
1260 if(s.event == NULL) {
1261 LM_ERR("event not found and set to NULL\n");
1264 s.local_cseq = row_vals[r_local_cseq_col].val.int_val;
1266 s.expires = row_vals[r_expires_col].val.int_val;
1268 if(s.expires > (int)time(NULL) + pres_expires_offset)
1269 s.expires -= (int)time(NULL);
1273 s.version = row_vals[r_version_col].val.int_val;
1275 s.flags = row_vals[r_flags_col].val.int_val;
1276 s.user_agent.s = (char *)row_vals[r_user_agent_col].val.string_val;
1277 s.user_agent.len = (s.user_agent.s) ? strlen(s.user_agent.s) : 0;
1280 cs = mem_copy_subs(&s, PKG_MEM_TYPE);
1282 LM_ERR("while copying subs_t structure\n");
1283 /* tidy up and return */
1284 pa_dbf.free_result(pa_db, result);
1288 cs->next = (*subs_array);
1294 pa_dbf.free_result(pa_db, result);
1296 if(pres_notifier_processes == 0 && subs->status == TERMINATED_STATUS) {
1297 /* delete the records */
1298 if(pa_dbf.delete(pa_db, query_cols, 0, query_vals, n_query_cols) < 0) {
1299 LM_ERR("sql delete failed\n");
1306 /* otherwise we update the records */
1307 db_cols[n_update_cols] = &str_status_col;
1308 db_vals[n_update_cols].type = DB1_INT;
1309 db_vals[n_update_cols].nul = 0;
1310 db_vals[n_update_cols].val.int_val = subs->status;
1313 db_cols[n_update_cols] = &str_reason_col;
1314 db_vals[n_update_cols].type = DB1_STR;
1315 db_vals[n_update_cols].nul = 0;
1316 db_vals[n_update_cols].val.str_val = subs->reason;
1319 db_cols[n_update_cols] = &str_updated_col;
1320 db_vals[n_update_cols].type = DB1_INT;
1321 db_vals[n_update_cols].nul = 0;
1322 if(subs->callid.len == 0 || subs->from_tag.len == 0) {
1323 db_vals[n_update_cols].val.int_val =
1324 (int)((kam_rand() / (KAM_RAND_MAX + 1.0))
1325 * (pres_waitn_time * pres_notifier_poll_rate
1326 * pres_notifier_processes));
1328 db_vals[n_update_cols].val.int_val =
1329 core_case_hash(&subs->callid, &subs->from_tag, 0)
1330 % (pres_waitn_time * pres_notifier_poll_rate
1331 * pres_notifier_processes);
1335 if(pa_dbf.update(pa_db, query_cols, 0, query_vals, db_cols, db_vals,
1336 n_query_cols, n_update_cols)
1338 LM_ERR("DB update failed\n");
1345 /********************************************************************************/
1347 static int update_pw_dialogs(
1348 subs_t *subs, unsigned int hash_code, subs_t **subs_array)
1350 subs_t *s, *ps, *cs;
1355 if(pres_subs_dbmode == DB_ONLY) {
1356 return (update_pw_dialogs_dbonlymode(subs, subs_array));
1359 lock_get(&subs_htable[hash_code].lock);
1361 ps = subs_htable[hash_code].entries;
1363 while(ps && ps->next) {
1366 if(s->event == subs->event && s->pres_uri.len == subs->pres_uri.len
1367 && s->watcher_user.len == subs->watcher_user.len
1368 && s->watcher_domain.len == subs->watcher_domain.len
1369 && presence_sip_uri_match(&s->pres_uri, &subs->pres_uri) == 0
1370 && presence_sip_uri_match(&s->watcher_user, &subs->watcher_user)
1372 && presence_sip_uri_match(
1373 &s->watcher_domain, &subs->watcher_domain)
1376 s->status = subs->status;
1377 s->reason = subs->reason;
1378 s->db_flag = UPDATEDB_FLAG;
1380 cs = mem_copy_subs(s, PKG_MEM_TYPE);
1382 LM_ERR("copying subs_t structure\n");
1383 lock_release(&subs_htable[hash_code].lock);
1387 cs->expires -= (int)time(NULL);
1388 cs->next = (*subs_array);
1390 if(subs->status == TERMINATED_STATUS) {
1392 shm_free(s->contact.s);
1394 LM_DBG(" deleted terminated dialog from hash table\n");
1403 LM_DBG("found %d matching dialogs\n", i);
1404 lock_release(&subs_htable[hash_code].lock);
1409 static int w_pres_auth_status(struct sip_msg *_msg, char *_sp1, char *_sp2)
1411 str watcher_uri, presentity_uri;
1413 if(fixup_get_svalue(_msg, (gparam_t *)_sp1, &watcher_uri) != 0) {
1414 LM_ERR("invalid watcher uri parameter");
1418 if(fixup_get_svalue(_msg, (gparam_t *)_sp2, &presentity_uri) != 0) {
1419 LM_ERR("invalid presentity uri parameter");
1423 if(watcher_uri.len == 0 || watcher_uri.s == NULL) {
1424 LM_ERR("missing watcher uri\n");
1428 if(presentity_uri.len == 0 || presentity_uri.s == NULL) {
1429 LM_DBG("missing presentity uri\n");
1433 return pres_auth_status(_msg, watcher_uri, presentity_uri);
1436 int ki_pres_auth_status(sip_msg_t *msg, str *watcher_uri, str *presentity_uri)
1438 if(watcher_uri == NULL || presentity_uri == NULL) {
1439 LM_ERR("invalid parameters\n");
1442 return pres_auth_status(msg, *watcher_uri, *presentity_uri);
1445 int pres_auth_status(struct sip_msg *msg, str watcher_uri, str presentity_uri)
1450 str *rules_doc = NULL;
1454 event.s = "presence";
1457 ev = contains_event(&event, NULL);
1459 LM_ERR("event is not registered\n");
1462 if(ev->get_rules_doc == NULL) {
1463 LM_DBG("event does not require authorization");
1464 return ACTIVE_STATUS;
1466 if(parse_uri(presentity_uri.s, presentity_uri.len, &uri) < 0) {
1467 LM_ERR("failed to parse presentity uri\n");
1470 res = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
1471 if((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
1472 LM_DBG("no xcap rules doc found for presentity uri\n");
1473 return PENDING_STATUS;
1476 if(parse_uri(watcher_uri.s, watcher_uri.len, &uri) < 0) {
1477 LM_ERR("failed to parse watcher uri\n");
1481 subs.watcher_user = uri.user;
1482 subs.watcher_domain = uri.host;
1483 subs.pres_uri = presentity_uri;
1484 subs.auth_rules_doc = rules_doc;
1485 if(ev->get_auth_status(&subs) < 0) {
1486 LM_ERR("getting status from rules document\n");
1489 LM_DBG("auth status of watcher <%.*s> on presentity <%.*s> is %d\n",
1490 watcher_uri.len, watcher_uri.s, presentity_uri.len,
1491 presentity_uri.s, subs.status);
1492 pkg_free(rules_doc->s);
1493 pkg_free(rules_doc);
1494 if((subs.reason.len == 12)
1495 && (strncmp(subs.reason.s, "polite-block", 12) == 0))
1496 return POLITE_BLOCK_STATUS;
1500 pkg_free(rules_doc->s);
1501 pkg_free(rules_doc);
1506 * wrapper for pres_refresh_watchers to use in config
1508 static int w_pres_refresh_watchers(
1509 struct sip_msg *msg, char *puri, char *pevent, char *ptype)
1515 if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri) != 0) {
1516 LM_ERR("invalid uri parameter");
1520 if(fixup_get_svalue(msg, (gparam_p)pevent, &event) != 0) {
1521 LM_ERR("invalid uri parameter");
1525 if(fixup_get_ivalue(msg, (gparam_p)ptype, &refresh_type) != 0) {
1526 LM_ERR("no type value\n");
1530 if(refresh_type == 2) {
1531 LM_ERR("Wrong number of parameters for type 2\n");
1535 if(pres_refresh_watchers(&pres_uri, &event, refresh_type, NULL, NULL) < 0)
1541 static int w_pres_refresh_watchers5(struct sip_msg *msg, char *puri,
1542 char *pevent, char *ptype, char *furi, char *fname)
1544 str pres_uri, event, file_uri, filename;
1547 if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri) != 0) {
1548 LM_ERR("invalid uri parameter");
1552 if(fixup_get_svalue(msg, (gparam_p)pevent, &event) != 0) {
1553 LM_ERR("invalid event parameter");
1557 if(fixup_get_ivalue(msg, (gparam_p)ptype, &refresh_type) != 0) {
1558 LM_ERR("no type value\n");
1562 if(fixup_get_svalue(msg, (gparam_p)furi, &file_uri) != 0) {
1563 LM_ERR("invalid file uri parameter");
1567 if(fixup_get_svalue(msg, (gparam_p)fname, &filename) != 0) {
1568 LM_ERR("invalid filename parameter");
1572 if(refresh_type != 2) {
1573 LM_ERR("Wrong number of parameters for type %d\n", refresh_type);
1577 if(pres_refresh_watchers(
1578 &pres_uri, &event, refresh_type, &file_uri, &filename)
1586 * fixup for w_pres_refresh_watchers
1588 static int fixup_refresh_watchers(void **param, int param_no)
1591 return fixup_spve_null(param, 1);
1592 } else if(param_no == 2) {
1593 return fixup_spve_null(param, 1);
1594 } else if(param_no == 3) {
1595 return fixup_igp_null(param, 1);
1596 } else if(param_no == 4) {
1597 return fixup_spve_null(param, 1);
1598 } else if(param_no == 5) {
1599 return fixup_spve_null(param, 1);
1607 * wrapper for update_watchers_status to use via kemi
1609 static int ki_pres_update_watchers(
1610 struct sip_msg *msg, str *pres_uri, str *event)
1614 str *rules_doc = NULL;
1617 ev = contains_event(event, NULL);
1619 LM_ERR("event %.*s is not registered\n", event->len, event->s);
1622 if(ev->get_rules_doc == NULL) {
1623 LM_DBG("event %.*s does not provide rules doc API\n", event->len,
1627 if(parse_uri(pres_uri->s, pres_uri->len, &uri) < 0) {
1628 LM_ERR("failed to parse presentity uri [%.*s]\n", pres_uri->len,
1632 ret = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
1633 if((ret < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
1634 LM_DBG("no xcap rules doc found for presentity uri [%.*s]\n",
1635 pres_uri->len, pres_uri->s);
1636 if(rules_doc != NULL)
1637 pkg_free(rules_doc);
1641 if(update_watchers_status(pres_uri, ev, rules_doc) < 0) {
1642 LM_ERR("updating watchers in presence\n");
1646 pkg_free(rules_doc->s);
1647 pkg_free(rules_doc);
1653 * wrapper for update_watchers_status to use in config
1655 static int w_pres_update_watchers(struct sip_msg *msg, char *puri, char *pevent)
1660 if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri) != 0) {
1661 LM_ERR("invalid uri parameter");
1665 if(fixup_get_svalue(msg, (gparam_p)pevent, &event) != 0) {
1666 LM_ERR("invalid uri parameter");
1669 return ki_pres_update_watchers(msg, &pres_uri, &event);
1672 * fixup for w_pres_update_watchers
1674 static int fixup_update_watchers(void **param, int param_no)
1677 return fixup_spve_null(param, 1);
1678 } else if(param_no == 2) {
1679 return fixup_spve_null(param, 1);
1685 * rpc cmd: presence.refreshWatchers
1688 * \<refresh_type> // can be: = 0 -> watchers autentification type or
1689 * != 0 -> publish type //
1691 void rpc_presence_refresh_watchers(rpc_t *rpc, void *ctx)
1693 str pres_uri = {0, 0};
1695 str file_uri = {0, 0};
1696 str filename = {0, 0};
1697 unsigned int refresh_type;
1700 LM_DBG("initiation refresh of watchers\n");
1702 pn = rpc->scan(ctx, "SSu*SS", &pres_uri, &event, &refresh_type, &file_uri,
1705 rpc->fault(ctx, 500, "Not enough parameters");
1709 if(pres_uri.s == NULL || pres_uri.len == 0) {
1710 LM_ERR("empty uri\n");
1711 rpc->fault(ctx, 500, "Empty presentity URI");
1715 if(event.s == NULL || event.len == 0) {
1716 LM_ERR("empty event parameter\n");
1717 rpc->fault(ctx, 500, "Empty event parameter");
1720 LM_DBG("event '%.*s'\n", event.len, event.s);
1722 if(refresh_type == 2) {
1724 LM_ERR("empty file uri or name parameters\n");
1725 rpc->fault(ctx, 500, "No file uri or name parameters");
1728 if(file_uri.s == NULL || file_uri.len == 0) {
1729 LM_ERR("empty file uri parameter\n");
1730 rpc->fault(ctx, 500, "Empty file uri parameter");
1734 if(filename.s == NULL || filename.len == 0) {
1735 LM_ERR("empty file name parameter\n");
1736 rpc->fault(ctx, 500, "Empty file name parameter");
1741 if(pres_refresh_watchers(&pres_uri, &event, refresh_type,
1742 file_uri.len ? &file_uri : NULL, filename.len ? &filename : NULL)
1744 rpc->fault(ctx, 500, "Execution failed");
1749 static const char *rpc_presence_refresh_watchers_doc[2] = {
1750 "Trigger refresh of watchers", 0};
1753 * rpc cmd: presence.updateWatchers
1757 void rpc_presence_update_watchers(rpc_t *rpc, void *ctx)
1759 str pres_uri = {0, 0};
1763 LM_DBG("init update of watchers\n");
1765 pn = rpc->scan(ctx, "SS", &pres_uri, &event);
1767 rpc->fault(ctx, 500, "Not enough parameters");
1771 if(pres_uri.s == NULL || pres_uri.len == 0) {
1772 LM_ERR("empty uri\n");
1773 rpc->fault(ctx, 500, "Empty presentity URI");
1777 if(event.s == NULL || event.len == 0) {
1778 LM_ERR("empty event parameter\n");
1779 rpc->fault(ctx, 500, "Empty event parameter");
1782 LM_DBG("uri '%.*s' - event '%.*s'\n", pres_uri.len, pres_uri.s,
1783 event.len, event.s);
1785 if(ki_pres_update_watchers(NULL, &pres_uri, &event)<0) {
1786 rpc->fault(ctx, 500, "Processing error");
1791 static const char *rpc_presence_update_watchers_doc[2] = {
1792 "Trigger update of watchers", 0};
1795 void rpc_presence_cleanup(rpc_t *rpc, void *c)
1797 LM_DBG("rpc_presence_cleanup:start\n");
1799 (void)msg_watchers_clean(0, 0);
1800 (void)msg_presentity_clean(0, 0);
1801 (void)timer_db_update(0, 0);
1803 rpc->rpl_printf(c, "Reload OK");
1807 static const char *rpc_presence_cleanup_doc[2] = {
1808 "Manually triggers the cleanup functions for the active_watchers, "
1809 "presentity, and watchers tables.",
1812 rpc_export_t presence_rpc[] = {
1813 {"presence.cleanup", rpc_presence_cleanup, rpc_presence_cleanup_doc, 0},
1814 {"presence.refreshWatchers", rpc_presence_refresh_watchers,
1815 rpc_presence_refresh_watchers_doc, 0},
1816 {"presence.updateWatchers", rpc_presence_update_watchers,
1817 rpc_presence_update_watchers_doc, 0},
1820 static int presence_init_rpc(void)
1822 if(rpc_register_array(presence_rpc) != 0) {
1823 LM_ERR("failed to register RPC commands\n");
1829 static int sip_uri_case_sensitive_match(str *s1, str *s2)
1832 LM_ERR("null pointer (s1) in sip_uri_match\n");
1836 LM_ERR("null pointer (s2) in sip_uri_match\n");
1839 return strncmp(s1->s, s2->s, s2->len);
1842 static int sip_uri_case_insensitive_match(str *s1, str *s2)
1845 LM_ERR("null pointer (s1) in sip_uri_match\n");
1849 LM_ERR("null pointer (s2) in sip_uri_match\n");
1852 return strncasecmp(s1->s, s2->s, s2->len);
1855 static int fixup_has_subscribers(void **param, int param_no)
1858 return fixup_spve_null(param, 1);
1859 } else if(param_no == 2) {
1860 return fixup_spve_null(param, 1);
1866 static int ki_pres_has_subscribers(sip_msg_t *msg, str *pres_uri, str *wevent)
1870 ev = contains_event(wevent, NULL);
1872 LM_ERR("event is not registered\n");
1876 return get_subscribers_count(msg, *pres_uri, *wevent) > 0 ? 1 : -1;
1879 static int w_pres_has_subscribers(sip_msg_t *msg, char *_pres_uri, char *_event)
1881 str presentity_uri, watched_event;
1883 if(fixup_get_svalue(msg, (gparam_p)_pres_uri, &presentity_uri) != 0) {
1884 LM_ERR("invalid presentity_uri parameter");
1887 if(fixup_get_svalue(msg, (gparam_p)_event, &watched_event) != 0) {
1888 LM_ERR("invalid watched_event parameter");
1892 return ki_pres_has_subscribers(msg, &presentity_uri, &watched_event);
1898 /* clang-format off */
1899 static sr_kemi_t sr_kemi_presence_exports[] = {
1900 { str_init("presence"), str_init("handle_publish"),
1901 SR_KEMIP_INT, ki_handle_publish,
1902 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1903 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1905 { str_init("presence"), str_init("handle_publish_uri"),
1906 SR_KEMIP_INT, ki_handle_publish_uri,
1907 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1908 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1910 { str_init("presence"), str_init("handle_subscribe"),
1911 SR_KEMIP_INT, handle_subscribe0,
1912 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1913 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1915 { str_init("presence"), str_init("handle_subscribe_uri"),
1916 SR_KEMIP_INT, handle_subscribe_uri,
1917 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1918 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1920 { str_init("presence"), str_init("pres_refresh_watchers"),
1921 SR_KEMIP_INT, ki_pres_refresh_watchers,
1922 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1923 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1925 { str_init("presence"), str_init("pres_refresh_watchers_file"),
1926 SR_KEMIP_INT, ki_pres_refresh_watchers_file,
1927 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1928 SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE }
1930 { str_init("presence"), str_init("pres_update_watchers"),
1931 SR_KEMIP_INT, ki_pres_update_watchers,
1932 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1933 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1935 { str_init("presence"), str_init("pres_has_subscribers"),
1936 SR_KEMIP_INT, ki_pres_has_subscribers,
1937 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1938 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1940 { str_init("presence"), str_init("pres_auth_status"),
1941 SR_KEMIP_INT, ki_pres_auth_status,
1942 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1943 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1946 { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1948 /* clang-format on */
1953 int mod_register(char *path, int *dlflags, void *p1, void *p2)
1955 sr_kemi_modules_add(sr_kemi_presence_exports);