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;
106 int pres_uri_match = 0;
107 /* sip uri match function pointer */
108 sip_uri_match_f presence_sip_uri_match;
109 static int sip_uri_case_sensitive_match(str *s1, str *s2);
110 static int sip_uri_case_insensitive_match(str *s1, str *s2);
114 /* SL API structure */
117 /** module functions */
119 static int mod_init(void);
120 static int child_init(int);
121 static void destroy(void);
122 int stored_pres_info(struct sip_msg *msg, char *pres_uri, char *s);
123 static int fixup_presence(void **param, int param_no);
124 static int fixup_subscribe(void **param, int param_no);
125 static int update_pw_dialogs(
126 subs_t *subs, unsigned int hash_code, subs_t **subs_array);
127 static int w_pres_auth_status(struct sip_msg *_msg, char *_sp1, char *_sp2);
128 static int w_pres_refresh_watchers(
129 struct sip_msg *msg, char *puri, char *pevent, char *ptype);
130 static int w_pres_refresh_watchers5(struct sip_msg *msg, char *puri,
131 char *pevent, char *ptype, char *furi, char *fname);
132 static int w_pres_update_watchers(
133 struct sip_msg *msg, char *puri, char *pevent);
134 static int fixup_refresh_watchers(void **param, int param_no);
135 static int fixup_update_watchers(void **param, int param_no);
136 static int presence_init_rpc(void);
138 static int w_pres_has_subscribers(struct sip_msg *_msg, char *_sp1, char *_sp2);
139 static int fixup_has_subscribers(void **param, int param_no);
141 int pres_counter = 0;
143 char pres_prefix = 'a';
144 int pres_startup_time = 0;
145 str pres_db_url = {0, 0};
146 int pres_expires_offset = 0;
147 int pres_cseq_offset = 0;
148 uint32_t pres_min_expires = 0;
149 int pres_min_expires_action = 1;
150 uint32_t pres_max_expires = 3600;
151 int shtable_size = 9;
152 shtable_t subs_htable = NULL;
153 int pres_subs_dbmode = WRITE_BACK;
154 int pres_sphere_enable = 0;
155 int pres_timeout_rm_subs = 1;
156 int pres_send_fast_notify = 1;
157 int publ_cache_enabled = 1;
158 int pres_waitn_time = 5;
159 int pres_notifier_poll_rate = 10;
160 int pres_notifier_processes = 1;
161 int pres_force_delete = 0;
162 int pres_startup_mode = 1;
163 str pres_xavp_cfg = {0};
164 int pres_retrieve_order = 0;
165 str pres_retrieve_order_by = str_init("priority");
166 int pres_enable_dmq = 0;
167 int pres_delete_same_subs = 0;
169 int pres_db_table_lock_type = 1;
170 db_locking_t pres_db_table_lock = DB_LOCKING_WRITE;
172 int *pres_notifier_id = NULL;
174 int phtable_size = 9;
175 phtable_t *pres_htable = NULL;
179 /* clang-format off */
180 static cmd_export_t cmds[]=
182 {"handle_publish", (cmd_function)w_handle_publish, 0,
183 fixup_presence, 0, REQUEST_ROUTE},
184 {"handle_publish", (cmd_function)w_handle_publish, 1,
185 fixup_presence, 0, REQUEST_ROUTE},
186 {"handle_subscribe", (cmd_function)handle_subscribe0, 0,
187 fixup_subscribe, 0, REQUEST_ROUTE},
188 {"handle_subscribe", (cmd_function)w_handle_subscribe, 1,
189 fixup_subscribe, 0, REQUEST_ROUTE},
190 {"pres_auth_status", (cmd_function)w_pres_auth_status, 2,
191 fixup_spve_spve, fixup_free_spve_spve, REQUEST_ROUTE},
192 {"pres_refresh_watchers", (cmd_function)w_pres_refresh_watchers, 3,
193 fixup_refresh_watchers, 0, ANY_ROUTE},
194 {"pres_refresh_watchers", (cmd_function)w_pres_refresh_watchers5,5,
195 fixup_refresh_watchers, 0, ANY_ROUTE},
196 {"pres_update_watchers", (cmd_function)w_pres_update_watchers, 2,
197 fixup_update_watchers, 0, ANY_ROUTE},
198 {"pres_has_subscribers", (cmd_function)w_pres_has_subscribers, 2,
199 fixup_has_subscribers, 0, ANY_ROUTE},
200 {"bind_presence", (cmd_function)bind_presence, 1,
204 /* clang-format on */
206 /* clang-format off */
207 static param_export_t params[]={
208 { "db_url", PARAM_STR, &pres_db_url},
209 { "presentity_table", PARAM_STR, &presentity_table},
210 { "active_watchers_table", PARAM_STR, &active_watchers_table},
211 { "watchers_table", PARAM_STR, &watchers_table},
212 { "clean_period", INT_PARAM, &pres_clean_period },
213 { "db_update_period", INT_PARAM, &pres_db_update_period },
214 { "waitn_time", INT_PARAM, &pres_waitn_time },
215 { "notifier_poll_rate", INT_PARAM, &pres_notifier_poll_rate },
216 { "notifier_processes", INT_PARAM, &pres_notifier_processes },
217 { "force_delete", INT_PARAM, &pres_force_delete },
218 { "startup_mode", INT_PARAM, &pres_startup_mode },
219 { "expires_offset", INT_PARAM, &pres_expires_offset },
220 { "max_expires", INT_PARAM, &pres_max_expires },
221 { "min_expires", INT_PARAM, &pres_min_expires },
222 { "min_expires_action", INT_PARAM, &pres_min_expires_action },
223 { "server_address", PARAM_STR, &pres_server_address},
224 { "subs_htable_size", INT_PARAM, &shtable_size},
225 { "pres_htable_size", INT_PARAM, &phtable_size},
226 { "subs_db_mode", INT_PARAM, &pres_subs_dbmode},
227 { "publ_cache", INT_PARAM, &publ_cache_enabled},
228 { "enable_sphere_check", INT_PARAM, &pres_sphere_enable},
229 { "timeout_rm_subs", INT_PARAM, &pres_timeout_rm_subs},
230 { "send_fast_notify", INT_PARAM, &pres_send_fast_notify},
231 { "fetch_rows", INT_PARAM, &pres_fetch_rows},
232 { "db_table_lock_type", INT_PARAM, &pres_db_table_lock_type},
233 { "local_log_level", PARAM_INT, &pres_local_log_level},
234 { "local_log_facility", PARAM_STR, &pres_log_facility_str},
235 { "subs_remove_match", PARAM_INT, &pres_subs_remove_match},
236 { "xavp_cfg", PARAM_STR, &pres_xavp_cfg},
237 { "retrieve_order", PARAM_INT, &pres_retrieve_order},
238 { "retrieve_order_by", PARAM_STR, &pres_retrieve_order_by},
239 { "sip_uri_match", PARAM_INT, &pres_uri_match},
240 { "cseq_offset", PARAM_INT, &pres_cseq_offset},
241 { "enable_dmq", PARAM_INT, &pres_enable_dmq},
242 { "pres_subs_mode", PARAM_INT, &_pres_subs_mode},
243 { "delete_same_subs", PARAM_INT, &pres_delete_same_subs},
246 /* clang-format on */
248 /* clang-format off */
249 static pv_export_t pres_mod_pvs[] = {
250 { {"subs", (sizeof("subs")-1)}, PVT_OTHER,
251 pv_get_subscription, 0, pv_parse_subscription_name, 0, 0, 0},
252 { {"notify_reply", (sizeof("notify_reply")-1)}, PVT_OTHER,
253 pv_get_notify_reply, 0, pv_parse_notify_reply_var_name, 0, 0, 0},
254 { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
256 /* clang-format on */
258 /** module exports */
259 /* clang-format off */
260 struct module_exports exports= {
261 "presence", /* module name */
262 DEFAULT_DLFLAGS, /* dlopen flags */
263 cmds, /* exported functions */
264 params, /* exported parameters */
265 0, /* RPC method exports */
266 pres_mod_pvs, /* exported pseudo-variables */
267 0, /* response handling function */
268 mod_init, /* module initialization function */
269 child_init, /* per-child init function */
270 destroy /* module destroy function */
272 /* clang-format on */
275 * init module function
277 static int mod_init(void)
279 if(pres_uri_match == 1) {
280 presence_sip_uri_match = sip_uri_case_insensitive_match;
282 presence_sip_uri_match = sip_uri_case_sensitive_match;
285 if(presence_init_rpc() != 0) {
286 LM_ERR("failed to register RPC commands\n");
290 LM_DBG("db_url=%s (len=%d addr=%p)\n", ZSW(pres_db_url.s), pres_db_url.len,
293 if(pres_db_url.s == NULL || pres_db_url.len == 0) {
294 LM_DBG("db url is not set - switch to library mode\n");
295 pres_library_mode = 1;
298 pres_evlist = init_evlist();
300 LM_ERR("unsuccessful initialize event list\n");
304 if(pres_library_mode == 1) {
305 LM_DBG("Presence module used for API library purpose only\n");
309 if(sruid_init(&pres_sruid, '-', "pres", SRUID_INC) < 0) {
313 if(pres_expires_offset < 0) {
314 pres_expires_offset = 0;
317 if(pres_max_expires <= 0) {
318 pres_max_expires = 3600;
321 if(pres_min_expires > pres_max_expires) {
322 pres_min_expires = pres_max_expires;
325 if(pres_min_expires_action < 1 || pres_min_expires_action > 2) {
326 LM_ERR("min_expires_action must be 1 = RFC 6665/3261 Reply 423, 2 = "
327 "force min_expires value\n");
331 if(pres_server_address.s == NULL || pres_server_address.len==0) {
332 LM_DBG("server_address parameter not set in configuration file\n");
335 /* bind the SL API */
336 if(sl_load_api(&slb) != 0) {
337 LM_ERR("cannot bind to SL API\n");
341 /* load all TM stuff */
342 if(load_tm_api(&tmb) == -1) {
343 LM_ERR("Can't load tm functions. Module TM not loaded?\n");
347 if(pres_db_url.s == NULL) {
348 LM_ERR("database url not set!\n");
352 /* binding to database module */
353 if(db_bind_mod(&pres_db_url, &pa_dbf)) {
354 LM_ERR("Database module not found\n");
358 if(!DB_CAPABILITY(pa_dbf, DB_CAP_ALL)) {
359 LM_ERR("Database module does not implement all functions"
360 " needed by presence module\n");
364 pa_db = pa_dbf.init(&pres_db_url);
366 LM_ERR("Connection to database failed\n");
370 /*verify table versions */
371 if((db_check_table_version(
372 &pa_dbf, pa_db, &presentity_table, P_TABLE_VERSION)
374 || (db_check_table_version(
375 &pa_dbf, pa_db, &watchers_table, S_TABLE_VERSION)
377 DB_TABLE_VERSION_ERROR(presentity_table);
381 if(pres_subs_dbmode != NO_DB
382 && db_check_table_version(&pa_dbf, pa_db, &active_watchers_table,
383 ACTWATCH_TABLE_VERSION)
385 DB_TABLE_VERSION_ERROR(active_watchers_table);
389 if(pres_subs_dbmode != DB_ONLY) {
393 shtable_size = 1 << shtable_size;
395 subs_htable = new_shtable(shtable_size);
396 if(subs_htable == NULL) {
397 LM_ERR(" initializing subscribe hash table\n");
400 if(restore_db_subs() < 0) {
401 LM_ERR("restoring subscribe info from database\n");
406 if(publ_cache_enabled) {
410 phtable_size = 1 << phtable_size;
412 pres_htable = new_phtable();
413 if(pres_htable == NULL) {
414 LM_ERR("initializing presentity hash table\n");
418 if(pres_htable_restore() < 0) {
419 LM_ERR("filling in presentity hash table from database\n");
424 pres_startup_time = (int)time(NULL);
425 if(pres_clean_period > 0) {
426 register_timer(msg_presentity_clean, 0, pres_clean_period);
427 register_timer(msg_watchers_clean, 0, pres_clean_period);
430 if(pres_db_update_period > 0) {
431 register_timer(timer_db_update, 0, pres_db_update_period);
434 if(pres_waitn_time <= 0) {
438 if(pres_notifier_poll_rate <= 0) {
439 pres_notifier_poll_rate = 10;
442 if(pres_notifier_processes < 0 || pres_subs_dbmode != DB_ONLY) {
443 pres_notifier_processes = 0;
446 if(pres_notifier_processes > 0) {
447 if((pres_notifier_id =
448 shm_malloc(sizeof(int) * pres_notifier_processes))
450 LM_ERR("allocating shared memory\n");
454 register_basic_timers(pres_notifier_processes);
457 if(pres_force_delete > 0)
458 pres_force_delete = 1;
460 if(pres_log_facility_str) {
461 int tmp = str2facility(pres_log_facility_str);
464 pres_local_log_facility = tmp;
466 LM_ERR("invalid log facility configured\n");
470 pres_local_log_facility = cfg_get(core, core_cfg, log_facility);
473 if(pres_db_table_lock_type != 1) {
474 pres_db_table_lock = DB_LOCKING_NONE;
480 goto_on_notify_reply = route_lookup(&event_rt, "presence:notify-reply");
481 if(goto_on_notify_reply >= 0 && event_rt.rlist[goto_on_notify_reply] == 0)
482 goto_on_notify_reply = -1; /* disable */
484 if(pres_enable_dmq > 0 && pres_dmq_initialize() != 0) {
485 LM_ERR("failed to initialize dmq integration\n");
498 * Initialize children
500 static int child_init(int rank)
502 if(rank == PROC_INIT || rank == PROC_TCP_MAIN) {
508 if(pres_library_mode) {
512 if(sruid_init(&pres_sruid, '-', "pres", SRUID_INC) < 0) {
516 if(rank == PROC_MAIN) {
519 for(i = 0; i < pres_notifier_processes; i++) {
521 snprintf(tmp, 21, "PRESENCE NOTIFIER %d", i);
522 pres_notifier_id[i] = i;
524 if(fork_basic_utimer(PROC_TIMER, tmp, 1, pres_timer_send_notify,
525 &pres_notifier_id[i], 1000000 / pres_notifier_poll_rate)
527 LM_ERR("Failed to start PRESENCE NOTIFIER %d\n", i);
535 if(pa_dbf.init == 0) {
536 LM_CRIT("child_init: database not bound\n");
539 /* Do not pool the connections where possible when running notifier
541 if(pres_notifier_processes > 0 && pa_dbf.init2)
542 pa_db = pa_dbf.init2(&pres_db_url, DB_POOLING_NONE);
544 pa_db = pa_dbf.init(&pres_db_url);
546 LM_ERR("child %d: unsuccessful connecting to database\n", rank);
550 if(pa_dbf.use_table(pa_db, &presentity_table) < 0) {
551 LM_ERR("child %d:unsuccessful use_table presentity_table\n", rank);
555 if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
556 LM_ERR("child %d:unsuccessful use_table active_watchers_table\n", rank);
560 if(pa_dbf.use_table(pa_db, &watchers_table) < 0) {
561 LM_ERR("child %d:unsuccessful use_table watchers_table\n", rank);
565 LM_DBG("child %d: Database connection opened successfully\n", rank);
574 static void destroy(void)
576 if(subs_htable && pres_subs_dbmode == WRITE_BACK) {
577 /* open database connection */
578 pa_db = pa_dbf.init(&pres_db_url);
580 LM_ERR("mod_destroy: unsuccessful connecting to database\n");
582 timer_db_update(0, 0);
586 destroy_shtable(subs_htable, shtable_size);
591 if(pa_db && pa_dbf.close)
594 if(pres_notifier_id != NULL)
595 shm_free(pres_notifier_id);
600 static int fixup_presence(void **param, int param_no)
602 if(pres_library_mode) {
603 LM_ERR("Bad config - you can not call 'handle_publish' function"
604 " (db_url not set)\n");
610 return fixup_spve_null(param, 1);
613 static int fixup_subscribe(void **param, int param_no)
616 if(pres_library_mode) {
617 LM_ERR("Bad config - you can not call 'handle_subscribe' function"
618 " (db_url not set)\n");
622 return fixup_spve_null(param, 1);
627 int pres_refresh_watchers(
628 str *pres, str *event, int type, str *file_uri, str *filename)
632 str *rules_doc = NULL;
635 ev = contains_event(event, NULL);
637 LM_ERR("wrong event parameter\n");
642 /* if a request to refresh watchers authorization */
643 if(ev->get_rules_doc == NULL) {
644 LM_ERR("wrong request for a refresh watchers authorization status"
645 "for an event that does not require authorization\n");
649 if(parse_uri(pres->s, pres->len, &uri) < 0) {
650 LM_ERR("parsing uri [%.*s]\n", pres->len, pres->s);
654 result = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
655 if(result < 0 || rules_doc == NULL || rules_doc->s == NULL) {
656 LM_ERR("no rules doc found for the user\n");
660 if(update_watchers_status(pres, ev, rules_doc) < 0) {
661 LM_ERR("failed to update watchers\n");
665 pkg_free(rules_doc->s);
671 if(update_hard_presentity(pres, ev, file_uri, filename) < 0) {
672 LM_ERR("updating hard presentity\n");
677 /* if a request to refresh notified info */
678 if(query_db_notify(pres, ev, NULL) < 0) {
679 LM_ERR("sending Notify requests\n");
688 pkg_free(rules_doc->s);
694 int _api_pres_refresh_watchers(str *pres, str *event, int type)
696 return pres_refresh_watchers(pres, event, type, NULL, NULL);
699 int ki_pres_refresh_watchers(sip_msg_t *msg, str *pres, str *event, int type)
701 return pres_refresh_watchers(pres, event, type, NULL, NULL);
704 int ki_pres_refresh_watchers_file(sip_msg_t *msg, str *pres, str *event,
705 int type, str *file_uri, str *filename)
707 return pres_refresh_watchers(pres, event, type, file_uri, filename);
710 int pres_update_status(subs_t *subs, str reason, db_key_t *query_cols,
711 db_val_t *query_vals, int n_query_cols, subs_t **subs_array)
713 db_key_t update_cols[5];
714 db_val_t update_vals[5];
715 int n_update_cols = 0;
716 int u_status_col, u_reason_col, q_wuser_col, q_wdomain_col;
718 query_cols[q_wuser_col = n_query_cols] = &str_watcher_username_col;
719 query_vals[n_query_cols].nul = 0;
720 query_vals[n_query_cols].type = DB1_STR;
723 query_cols[q_wdomain_col = n_query_cols] = &str_watcher_domain_col;
724 query_vals[n_query_cols].nul = 0;
725 query_vals[n_query_cols].type = DB1_STR;
728 update_cols[u_status_col = n_update_cols] = &str_status_col;
729 update_vals[u_status_col].nul = 0;
730 update_vals[u_status_col].type = DB1_INT;
733 update_cols[u_reason_col = n_update_cols] = &str_reason_col;
734 update_vals[u_reason_col].nul = 0;
735 update_vals[u_reason_col].type = DB1_STR;
738 status = subs->status;
739 if(subs->event->get_auth_status(subs) < 0) {
740 LM_ERR("getting status from rules document\n");
743 LM_DBG("subs.status= %d\n", subs->status);
744 if(get_status_str(subs->status) == NULL) {
745 LM_ERR("wrong status: %d\n", subs->status);
749 if(subs->status != status || reason.len != subs->reason.len
750 || (reason.s && subs->reason.s
751 && strncmp(reason.s, subs->reason.s, reason.len))) {
752 /* update in watchers_table */
753 query_vals[q_wuser_col].val.str_val = subs->watcher_user;
754 query_vals[q_wdomain_col].val.str_val = subs->watcher_domain;
756 update_vals[u_status_col].val.int_val = subs->status;
757 update_vals[u_reason_col].val.str_val = subs->reason;
759 if(pa_dbf.use_table(pa_db, &watchers_table) < 0) {
760 LM_ERR("in use_table\n");
764 if(pa_dbf.update(pa_db, query_cols, 0, query_vals, update_cols,
765 update_vals, n_query_cols, n_update_cols)
767 LM_ERR("in sql update\n");
770 /* save in the list all affected dialogs */
771 /* if status switches to terminated -> delete dialog */
772 if(update_pw_dialogs(subs, subs->db_flag, subs_array) < 0) {
773 LM_ERR("extracting dialogs from [watcher]=%.*s@%.*s to"
774 " [presentity]=%.*s\n",
775 subs->watcher_user.len, subs->watcher_user.s,
776 subs->watcher_domain.len, subs->watcher_domain.s,
777 subs->pres_uri.len, subs->pres_uri.s);
784 int pres_db_delete_status(subs_t *s)
786 int n_query_cols = 0;
787 db_key_t query_cols[5];
788 db_val_t query_vals[5];
790 if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
791 LM_ERR("sql use table failed\n");
795 query_cols[n_query_cols] = &str_event_col;
796 query_vals[n_query_cols].nul = 0;
797 query_vals[n_query_cols].type = DB1_STR;
798 query_vals[n_query_cols].val.str_val = s->event->name;
801 query_cols[n_query_cols] = &str_presentity_uri_col;
802 query_vals[n_query_cols].nul = 0;
803 query_vals[n_query_cols].type = DB1_STR;
804 query_vals[n_query_cols].val.str_val = s->pres_uri;
807 query_cols[n_query_cols] = &str_watcher_username_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->watcher_user;
813 query_cols[n_query_cols] = &str_watcher_domain_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->watcher_domain;
819 if(pa_dbf.delete(pa_db, query_cols, 0, query_vals, n_query_cols) < 0) {
820 LM_ERR("sql delete failed\n");
826 int update_watchers_status(str *pres_uri, pres_ev_t *ev, str *rules_doc)
829 db_key_t query_cols[6], result_cols[5];
830 db_val_t query_vals[6];
831 int n_result_cols = 0, n_query_cols = 0;
832 db1_res_t *result = NULL;
836 str w_user, w_domain, reason = {0, 0};
838 int status_col, w_user_col, w_domain_col, reason_col;
839 subs_t *subs_array = NULL, *s;
840 unsigned int hash_code;
852 ws_t *ws_list = NULL;
856 if(ev->content_type.s == NULL) {
857 ev = contains_event(&ev->name, NULL);
859 LM_ERR("wrong event parameter\n");
864 memset(&subs, 0, sizeof(subs_t));
865 subs.pres_uri = *pres_uri;
867 subs.auth_rules_doc = rules_doc;
869 /* update in watchers_table */
870 query_cols[n_query_cols] = &str_presentity_uri_col;
871 query_vals[n_query_cols].nul = 0;
872 query_vals[n_query_cols].type = DB1_STR;
873 query_vals[n_query_cols].val.str_val = *pres_uri;
876 query_cols[n_query_cols] = &str_event_col;
877 query_vals[n_query_cols].nul = 0;
878 query_vals[n_query_cols].type = DB1_STR;
879 query_vals[n_query_cols].val.str_val = ev->name;
882 result_cols[status_col = n_result_cols++] = &str_status_col;
883 result_cols[reason_col = n_result_cols++] = &str_reason_col;
884 result_cols[w_user_col = n_result_cols++] = &str_watcher_username_col;
885 result_cols[w_domain_col = n_result_cols++] = &str_watcher_domain_col;
887 if(pa_dbf.use_table(pa_db, &watchers_table) < 0) {
888 LM_ERR("in use_table\n");
892 if(pa_dbf.query(pa_db, query_cols, 0, query_vals, result_cols, n_query_cols,
893 n_result_cols, 0, &result)
895 LM_ERR("in sql query\n");
906 LM_DBG("found %d record-uri in watchers_table\n", result->n);
907 hash_code = core_case_hash(pres_uri, &ev->name, shtable_size);
908 subs.db_flag = hash_code;
910 /* must do a copy as sphere_check requires database queries */
911 if(pres_sphere_enable) {
913 ws_list = (ws_t *)pkg_malloc(n * sizeof(ws_t));
914 if(ws_list == NULL) {
915 LM_ERR("No more private memory\n");
918 memset(ws_list, 0, n * sizeof(ws_t));
920 for(i = 0; i < result->n; i++) {
921 row = &result->rows[i];
922 row_vals = ROW_VALUES(row);
924 status = row_vals[status_col].val.int_val;
926 reason.s = (char *)row_vals[reason_col].val.string_val;
927 reason.len = reason.s ? strlen(reason.s) : 0;
929 w_user.s = (char *)row_vals[w_user_col].val.string_val;
930 w_user.len = strlen(w_user.s);
932 w_domain.s = (char *)row_vals[w_domain_col].val.string_val;
933 w_domain.len = strlen(w_domain.s);
936 ws_list[i].reason.s =
937 (char *)pkg_malloc(reason.len * sizeof(char));
938 if(ws_list[i].reason.s == NULL) {
939 LM_ERR("No more private memory\n");
942 memcpy(ws_list[i].reason.s, reason.s, reason.len);
943 ws_list[i].reason.len = reason.len;
945 ws_list[i].reason.s = NULL;
947 ws_list[i].w_user.s = (char *)pkg_malloc(w_user.len * sizeof(char));
948 if(ws_list[i].w_user.s == NULL) {
949 LM_ERR("No more private memory\n");
952 memcpy(ws_list[i].w_user.s, w_user.s, w_user.len);
953 ws_list[i].w_user.len = w_user.len;
955 ws_list[i].w_domain.s =
956 (char *)pkg_malloc(w_domain.len * sizeof(char));
957 if(ws_list[i].w_domain.s == NULL) {
958 LM_ERR("No more private memory\n");
961 memcpy(ws_list[i].w_domain.s, w_domain.s, w_domain.len);
962 ws_list[i].w_domain.len = w_domain.len;
964 ws_list[i].status = status;
967 pa_dbf.free_result(pa_db, result);
970 for(i = 0; i < n; i++) {
971 subs.watcher_user = ws_list[i].w_user;
972 subs.watcher_domain = ws_list[i].w_domain;
973 subs.status = ws_list[i].status;
974 memset(&subs.reason, 0, sizeof(str));
976 if(pres_update_status(&subs, reason, query_cols, query_vals,
977 n_query_cols, &subs_array)
979 LM_ERR("failed to update watcher status\n");
984 for(i = 0; i < n; i++) {
985 pkg_free(ws_list[i].w_user.s);
986 pkg_free(ws_list[i].w_domain.s);
987 if(ws_list[i].reason.s)
988 pkg_free(ws_list[i].reason.s);
996 for(i = 0; i < result->n; i++) {
997 row = &result->rows[i];
998 row_vals = ROW_VALUES(row);
1000 status = row_vals[status_col].val.int_val;
1002 reason.s = (char *)row_vals[reason_col].val.string_val;
1003 reason.len = reason.s ? strlen(reason.s) : 0;
1005 w_user.s = (char *)row_vals[w_user_col].val.string_val;
1006 w_user.len = strlen(w_user.s);
1008 w_domain.s = (char *)row_vals[w_domain_col].val.string_val;
1009 w_domain.len = strlen(w_domain.s);
1011 subs.watcher_user = w_user;
1012 subs.watcher_domain = w_domain;
1013 subs.status = status;
1014 memset(&subs.reason, 0, sizeof(str));
1016 if(pres_update_status(&subs, reason, query_cols, query_vals,
1017 n_query_cols, &subs_array)
1019 LM_ERR("failed to update watcher status\n");
1024 pa_dbf.free_result(pa_db, result);
1029 if(pres_notifier_processes == 0) {
1033 if(notify(s, NULL, NULL, 0, 0) < 0) {
1034 LM_ERR("sending Notify request\n");
1038 /* delete from database also */
1039 if(s->status == TERMINATED_STATUS) {
1040 if(pres_db_delete_status(s) < 0) {
1041 LM_ERR("failed to delete terminated "
1042 "dialog from database\n");
1051 free_subs_list(subs_array, PKG_MEM_TYPE, 0);
1056 pa_dbf.free_result(pa_db, result);
1057 free_subs_list(subs_array, PKG_MEM_TYPE, 0);
1059 for(i = 0; i < n; i++) {
1060 if(ws_list[i].w_user.s)
1061 pkg_free(ws_list[i].w_user.s);
1062 if(ws_list[i].w_domain.s)
1063 pkg_free(ws_list[i].w_domain.s);
1064 if(ws_list[i].reason.s)
1065 pkg_free(ws_list[i].reason.s);
1072 /********************************************************************************/
1074 static int update_pw_dialogs_dbonlymode(subs_t *subs, subs_t **subs_array)
1076 db_key_t query_cols[5], db_cols[3];
1077 db_val_t query_vals[5], db_vals[3];
1078 db_key_t result_cols[26];
1079 int n_query_cols = 0, n_result_cols = 0, n_update_cols = 0;
1080 int event_col, pres_uri_col, watcher_user_col, watcher_domain_col;
1081 int r_pres_uri_col, r_to_user_col, r_to_domain_col;
1082 int r_from_user_col, r_from_domain_col, r_callid_col;
1083 int r_to_tag_col, r_from_tag_col, r_sockinfo_col;
1084 int r_event_id_col, r_local_contact_col, r_contact_col;
1085 int r_record_route_col, r_reason_col;
1086 int r_event_col, r_local_cseq_col, r_remote_cseq_col;
1087 int r_status_col, r_version_col;
1088 int r_expires_col, r_watcher_user_col, r_watcher_domain_col;
1089 int r_flags_col, r_user_agent_col;
1090 db1_res_t *result = NULL;
1098 LM_ERR("null database connection\n");
1102 if(pa_dbf.use_table(pa_db, &active_watchers_table) < 0) {
1103 LM_ERR("use table failed\n");
1107 query_cols[event_col = n_query_cols] = &str_event_col;
1108 query_vals[event_col].nul = 0;
1109 query_vals[event_col].type = DB1_STR;
1110 query_vals[event_col].val.str_val = subs->event->name;
1113 query_cols[pres_uri_col = n_query_cols] = &str_presentity_uri_col;
1114 query_vals[pres_uri_col].nul = 0;
1115 query_vals[pres_uri_col].type = DB1_STR;
1116 query_vals[pres_uri_col].val.str_val = subs->pres_uri;
1119 query_cols[watcher_user_col = n_query_cols] = &str_watcher_username_col;
1120 query_vals[watcher_user_col].nul = 0;
1121 query_vals[watcher_user_col].type = DB1_STR;
1122 query_vals[watcher_user_col].val.str_val = subs->watcher_user;
1125 query_cols[watcher_domain_col = n_query_cols] = &str_watcher_domain_col;
1126 query_vals[watcher_domain_col].nul = 0;
1127 query_vals[watcher_domain_col].type = DB1_STR;
1128 query_vals[watcher_domain_col].val.str_val = subs->watcher_domain;
1132 result_cols[r_to_user_col = n_result_cols++] = &str_to_user_col;
1133 result_cols[r_to_domain_col = n_result_cols++] = &str_to_domain_col;
1134 result_cols[r_from_user_col = n_result_cols++] = &str_from_user_col;
1135 result_cols[r_from_domain_col = n_result_cols++] = &str_from_domain_col;
1136 result_cols[r_watcher_user_col = n_result_cols++] =
1137 &str_watcher_username_col;
1138 result_cols[r_watcher_domain_col = n_result_cols++] =
1139 &str_watcher_domain_col;
1140 result_cols[r_callid_col = n_result_cols++] = &str_callid_col;
1141 result_cols[r_to_tag_col = n_result_cols++] = &str_to_tag_col;
1142 result_cols[r_from_tag_col = n_result_cols++] = &str_from_tag_col;
1143 result_cols[r_sockinfo_col = n_result_cols++] = &str_socket_info_col;
1144 result_cols[r_event_id_col = n_result_cols++] = &str_event_id_col;
1145 result_cols[r_local_contact_col = n_result_cols++] = &str_local_contact_col;
1146 result_cols[r_record_route_col = n_result_cols++] = &str_record_route_col;
1147 result_cols[r_reason_col = n_result_cols++] = &str_reason_col;
1148 result_cols[r_local_cseq_col = n_result_cols++] = &str_local_cseq_col;
1149 result_cols[r_version_col = n_result_cols++] = &str_version_col;
1150 result_cols[r_expires_col = n_result_cols++] = &str_expires_col;
1151 result_cols[r_event_col = n_result_cols++] = &str_event_col;
1152 result_cols[r_pres_uri_col = n_result_cols++] = &str_presentity_uri_col;
1153 result_cols[r_contact_col = n_result_cols++] = &str_contact_col;
1155 /* these ones are unused for some reason !!! */
1156 result_cols[r_remote_cseq_col = n_result_cols++] = &str_remote_cseq_col;
1157 result_cols[r_status_col = n_result_cols++] = &str_status_col;
1158 /*********************************************/
1160 result_cols[r_flags_col = n_result_cols++] = &str_flags_col;
1161 result_cols[r_user_agent_col = n_result_cols++] = &str_user_agent_col;
1163 if(pa_dbf.query(pa_db, query_cols, 0, query_vals, result_cols, n_query_cols,
1164 n_result_cols, 0, &result)
1166 LM_ERR("Can't query db\n");
1168 pa_dbf.free_result(pa_db, result);
1175 nr_rows = RES_ROW_N(result);
1177 LM_DBG("found %d matching dialogs\n", nr_rows);
1180 pa_dbf.free_result(pa_db, result);
1184 rows = RES_ROWS(result);
1185 /* get the results and fill in return data structure */
1186 for(loop = 0; loop < nr_rows; loop++) {
1187 row_vals = ROW_VALUES(&rows[loop]);
1189 memset(&s, 0, sizeof(subs_t));
1190 s.status = subs->status;
1192 s.reason.s = subs->reason.s;
1193 s.reason.len = s.reason.s ? strlen(s.reason.s) : 0; //>>>>>>>>>>
1195 s.pres_uri.s = (char *)row_vals[r_pres_uri_col].val.string_val;
1196 s.pres_uri.len = s.pres_uri.s ? strlen(s.pres_uri.s) : 0;
1198 s.to_user.s = (char *)row_vals[r_to_user_col].val.string_val;
1199 s.to_user.len = s.to_user.s ? strlen(s.to_user.s) : 0;
1201 s.to_domain.s = (char *)row_vals[r_to_domain_col].val.string_val;
1202 s.to_domain.len = s.to_domain.s ? strlen(s.to_domain.s) : 0;
1204 s.from_user.s = (char *)row_vals[r_from_user_col].val.string_val;
1205 s.from_user.len = s.from_user.s ? strlen(s.from_user.s) : 0;
1207 s.from_domain.s = (char *)row_vals[r_from_domain_col].val.string_val;
1208 s.from_domain.len = s.from_domain.s ? strlen(s.from_domain.s) : 0;
1210 s.watcher_user.s = (char *)row_vals[r_watcher_user_col].val.string_val;
1211 s.watcher_user.len = s.watcher_user.s ? strlen(s.watcher_user.s) : 0;
1213 s.watcher_domain.s =
1214 (char *)row_vals[r_watcher_domain_col].val.string_val;
1215 s.watcher_domain.len =
1216 s.watcher_domain.s ? strlen(s.watcher_domain.s) : 0;
1218 s.event_id.s = (char *)row_vals[r_event_id_col].val.string_val;
1219 s.event_id.len = (s.event_id.s) ? strlen(s.event_id.s) : 0;
1221 s.to_tag.s = (char *)row_vals[r_to_tag_col].val.string_val;
1222 s.to_tag.len = s.to_tag.s ? strlen(s.to_tag.s) : 0;
1224 s.from_tag.s = (char *)row_vals[r_from_tag_col].val.string_val;
1225 s.from_tag.len = s.from_tag.s ? strlen(s.from_tag.s) : 0;
1227 s.callid.s = (char *)row_vals[r_callid_col].val.string_val;
1228 s.callid.len = s.callid.s ? strlen(s.callid.s) : 0;
1230 s.record_route.s = (char *)row_vals[r_record_route_col].val.string_val;
1231 s.record_route.len = (s.record_route.s) ? strlen(s.record_route.s) : 0;
1233 s.contact.s = (char *)row_vals[r_contact_col].val.string_val;
1234 s.contact.len = s.contact.s ? strlen(s.contact.s) : 0;
1236 s.sockinfo_str.s = (char *)row_vals[r_sockinfo_col].val.string_val;
1237 s.sockinfo_str.len = s.sockinfo_str.s ? strlen(s.sockinfo_str.s) : 0;
1240 (char *)row_vals[r_local_contact_col].val.string_val;
1241 s.local_contact.len = s.local_contact.s ? strlen(s.local_contact.s) : 0;
1243 ev_sname.s = (char *)row_vals[r_event_col].val.string_val;
1244 ev_sname.len = ev_sname.s ? strlen(ev_sname.s) : 0;
1246 s.event = contains_event(&ev_sname, NULL);
1248 if(s.event == NULL) {
1249 LM_ERR("event not found and set to NULL\n");
1252 s.local_cseq = row_vals[r_local_cseq_col].val.int_val;
1254 s.expires = row_vals[r_expires_col].val.int_val;
1256 if(s.expires > (int)time(NULL) + pres_expires_offset)
1257 s.expires -= (int)time(NULL);
1261 s.version = row_vals[r_version_col].val.int_val;
1263 s.flags = row_vals[r_flags_col].val.int_val;
1264 s.user_agent.s = (char *)row_vals[r_user_agent_col].val.string_val;
1265 s.user_agent.len = (s.user_agent.s) ? strlen(s.user_agent.s) : 0;
1268 cs = mem_copy_subs(&s, PKG_MEM_TYPE);
1270 LM_ERR("while copying subs_t structure\n");
1271 /* tidy up and return */
1272 pa_dbf.free_result(pa_db, result);
1276 cs->next = (*subs_array);
1282 pa_dbf.free_result(pa_db, result);
1284 if(pres_notifier_processes == 0 && subs->status == TERMINATED_STATUS) {
1285 /* delete the records */
1286 if(pa_dbf.delete(pa_db, query_cols, 0, query_vals, n_query_cols) < 0) {
1287 LM_ERR("sql delete failed\n");
1294 /* otherwise we update the records */
1295 db_cols[n_update_cols] = &str_status_col;
1296 db_vals[n_update_cols].type = DB1_INT;
1297 db_vals[n_update_cols].nul = 0;
1298 db_vals[n_update_cols].val.int_val = subs->status;
1301 db_cols[n_update_cols] = &str_reason_col;
1302 db_vals[n_update_cols].type = DB1_STR;
1303 db_vals[n_update_cols].nul = 0;
1304 db_vals[n_update_cols].val.str_val = subs->reason;
1307 db_cols[n_update_cols] = &str_updated_col;
1308 db_vals[n_update_cols].type = DB1_INT;
1309 db_vals[n_update_cols].nul = 0;
1310 if(subs->callid.len == 0 || subs->from_tag.len == 0) {
1311 db_vals[n_update_cols].val.int_val =
1312 (int)((kam_rand() / (KAM_RAND_MAX + 1.0))
1313 * (pres_waitn_time * pres_notifier_poll_rate
1314 * pres_notifier_processes));
1316 db_vals[n_update_cols].val.int_val =
1317 core_case_hash(&subs->callid, &subs->from_tag, 0)
1318 % (pres_waitn_time * pres_notifier_poll_rate
1319 * pres_notifier_processes);
1323 if(pa_dbf.update(pa_db, query_cols, 0, query_vals, db_cols, db_vals,
1324 n_query_cols, n_update_cols)
1326 LM_ERR("DB update failed\n");
1333 /********************************************************************************/
1335 static int update_pw_dialogs(
1336 subs_t *subs, unsigned int hash_code, subs_t **subs_array)
1338 subs_t *s, *ps, *cs;
1343 if(pres_subs_dbmode == DB_ONLY) {
1344 return (update_pw_dialogs_dbonlymode(subs, subs_array));
1347 lock_get(&subs_htable[hash_code].lock);
1349 ps = subs_htable[hash_code].entries;
1351 while(ps && ps->next) {
1354 if(s->event == subs->event && s->pres_uri.len == subs->pres_uri.len
1355 && s->watcher_user.len == subs->watcher_user.len
1356 && s->watcher_domain.len == subs->watcher_domain.len
1357 && presence_sip_uri_match(&s->pres_uri, &subs->pres_uri) == 0
1358 && presence_sip_uri_match(&s->watcher_user, &subs->watcher_user)
1360 && presence_sip_uri_match(
1361 &s->watcher_domain, &subs->watcher_domain)
1364 s->status = subs->status;
1365 s->reason = subs->reason;
1366 s->db_flag = UPDATEDB_FLAG;
1368 cs = mem_copy_subs(s, PKG_MEM_TYPE);
1370 LM_ERR("copying subs_t structure\n");
1371 lock_release(&subs_htable[hash_code].lock);
1375 cs->expires -= (int)time(NULL);
1376 cs->next = (*subs_array);
1378 if(subs->status == TERMINATED_STATUS) {
1380 shm_free(s->contact.s);
1382 LM_DBG(" deleted terminated dialog from hash table\n");
1391 LM_DBG("found %d matching dialogs\n", i);
1392 lock_release(&subs_htable[hash_code].lock);
1397 static int w_pres_auth_status(struct sip_msg *_msg, char *_sp1, char *_sp2)
1399 str watcher_uri, presentity_uri;
1401 if(fixup_get_svalue(_msg, (gparam_t *)_sp1, &watcher_uri) != 0) {
1402 LM_ERR("invalid watcher uri parameter");
1406 if(fixup_get_svalue(_msg, (gparam_t *)_sp2, &presentity_uri) != 0) {
1407 LM_ERR("invalid presentity uri parameter");
1411 if(watcher_uri.len == 0 || watcher_uri.s == NULL) {
1412 LM_ERR("missing watcher uri\n");
1416 if(presentity_uri.len == 0 || presentity_uri.s == NULL) {
1417 LM_DBG("missing presentity uri\n");
1421 return pres_auth_status(_msg, watcher_uri, presentity_uri);
1424 int ki_pres_auth_status(sip_msg_t *msg, str *watcher_uri, str *presentity_uri)
1426 if(watcher_uri == NULL || presentity_uri == NULL) {
1427 LM_ERR("invalid parameters\n");
1430 return pres_auth_status(msg, *watcher_uri, *presentity_uri);
1433 int pres_auth_status(struct sip_msg *msg, str watcher_uri, str presentity_uri)
1438 str *rules_doc = NULL;
1442 event.s = "presence";
1445 ev = contains_event(&event, NULL);
1447 LM_ERR("event is not registered\n");
1450 if(ev->get_rules_doc == NULL) {
1451 LM_DBG("event does not require authorization");
1452 return ACTIVE_STATUS;
1454 if(parse_uri(presentity_uri.s, presentity_uri.len, &uri) < 0) {
1455 LM_ERR("failed to parse presentity uri\n");
1458 res = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
1459 if((res < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
1460 LM_DBG("no xcap rules doc found for presentity uri\n");
1461 return PENDING_STATUS;
1464 if(parse_uri(watcher_uri.s, watcher_uri.len, &uri) < 0) {
1465 LM_ERR("failed to parse watcher uri\n");
1469 subs.watcher_user = uri.user;
1470 subs.watcher_domain = uri.host;
1471 subs.pres_uri = presentity_uri;
1472 subs.auth_rules_doc = rules_doc;
1473 if(ev->get_auth_status(&subs) < 0) {
1474 LM_ERR("getting status from rules document\n");
1477 LM_DBG("auth status of watcher <%.*s> on presentity <%.*s> is %d\n",
1478 watcher_uri.len, watcher_uri.s, presentity_uri.len,
1479 presentity_uri.s, subs.status);
1480 pkg_free(rules_doc->s);
1481 pkg_free(rules_doc);
1482 if((subs.reason.len == 12)
1483 && (strncmp(subs.reason.s, "polite-block", 12) == 0))
1484 return POLITE_BLOCK_STATUS;
1488 pkg_free(rules_doc->s);
1489 pkg_free(rules_doc);
1494 * wrapper for pres_refresh_watchers to use in config
1496 static int w_pres_refresh_watchers(
1497 struct sip_msg *msg, char *puri, char *pevent, char *ptype)
1503 if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri) != 0) {
1504 LM_ERR("invalid uri parameter");
1508 if(fixup_get_svalue(msg, (gparam_p)pevent, &event) != 0) {
1509 LM_ERR("invalid uri parameter");
1513 if(fixup_get_ivalue(msg, (gparam_p)ptype, &refresh_type) != 0) {
1514 LM_ERR("no type value\n");
1518 if(refresh_type == 2) {
1519 LM_ERR("Wrong number of parameters for type 2\n");
1523 if(pres_refresh_watchers(&pres_uri, &event, refresh_type, NULL, NULL) < 0)
1529 static int w_pres_refresh_watchers5(struct sip_msg *msg, char *puri,
1530 char *pevent, char *ptype, char *furi, char *fname)
1532 str pres_uri, event, file_uri, filename;
1535 if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri) != 0) {
1536 LM_ERR("invalid uri parameter");
1540 if(fixup_get_svalue(msg, (gparam_p)pevent, &event) != 0) {
1541 LM_ERR("invalid event parameter");
1545 if(fixup_get_ivalue(msg, (gparam_p)ptype, &refresh_type) != 0) {
1546 LM_ERR("no type value\n");
1550 if(fixup_get_svalue(msg, (gparam_p)furi, &file_uri) != 0) {
1551 LM_ERR("invalid file uri parameter");
1555 if(fixup_get_svalue(msg, (gparam_p)fname, &filename) != 0) {
1556 LM_ERR("invalid filename parameter");
1560 if(refresh_type != 2) {
1561 LM_ERR("Wrong number of parameters for type %d\n", refresh_type);
1565 if(pres_refresh_watchers(
1566 &pres_uri, &event, refresh_type, &file_uri, &filename)
1574 * fixup for w_pres_refresh_watchers
1576 static int fixup_refresh_watchers(void **param, int param_no)
1579 return fixup_spve_null(param, 1);
1580 } else if(param_no == 2) {
1581 return fixup_spve_null(param, 1);
1582 } else if(param_no == 3) {
1583 return fixup_igp_null(param, 1);
1584 } else if(param_no == 4) {
1585 return fixup_spve_null(param, 1);
1586 } else if(param_no == 5) {
1587 return fixup_spve_null(param, 1);
1595 * wrapper for update_watchers_status to use via kemi
1597 static int ki_pres_update_watchers(
1598 struct sip_msg *msg, str *pres_uri, str *event)
1602 str *rules_doc = NULL;
1605 ev = contains_event(event, NULL);
1607 LM_ERR("event %.*s is not registered\n", event->len, event->s);
1610 if(ev->get_rules_doc == NULL) {
1611 LM_DBG("event %.*s does not provide rules doc API\n", event->len,
1615 if(parse_uri(pres_uri->s, pres_uri->len, &uri) < 0) {
1616 LM_ERR("failed to parse presentity uri [%.*s]\n", pres_uri->len,
1620 ret = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
1621 if((ret < 0) || (rules_doc == NULL) || (rules_doc->s == NULL)) {
1622 LM_DBG("no xcap rules doc found for presentity uri [%.*s]\n",
1623 pres_uri->len, pres_uri->s);
1624 if(rules_doc != NULL)
1625 pkg_free(rules_doc);
1629 if(update_watchers_status(pres_uri, ev, rules_doc) < 0) {
1630 LM_ERR("updating watchers in presence\n");
1634 pkg_free(rules_doc->s);
1635 pkg_free(rules_doc);
1641 * wrapper for update_watchers_status to use in config
1643 static int w_pres_update_watchers(struct sip_msg *msg, char *puri, char *pevent)
1648 if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri) != 0) {
1649 LM_ERR("invalid uri parameter");
1653 if(fixup_get_svalue(msg, (gparam_p)pevent, &event) != 0) {
1654 LM_ERR("invalid uri parameter");
1657 return ki_pres_update_watchers(msg, &pres_uri, &event);
1660 * fixup for w_pres_update_watchers
1662 static int fixup_update_watchers(void **param, int param_no)
1665 return fixup_spve_null(param, 1);
1666 } else if(param_no == 2) {
1667 return fixup_spve_null(param, 1);
1673 * rpc cmd: presence.refreshWatchers
1676 * \<refresh_type> // can be: = 0 -> watchers autentification type or
1677 * != 0 -> publish type //
1679 void rpc_presence_refresh_watchers(rpc_t *rpc, void *ctx)
1681 str pres_uri = {0, 0};
1683 str file_uri = {0, 0};
1684 str filename = {0, 0};
1685 unsigned int refresh_type;
1688 LM_DBG("initiation refresh of watchers\n");
1690 pn = rpc->scan(ctx, "SSu*SS", &pres_uri, &event, &refresh_type, &file_uri,
1693 rpc->fault(ctx, 500, "Not enough parameters");
1697 if(pres_uri.s == NULL || pres_uri.len == 0) {
1698 LM_ERR("empty uri\n");
1699 rpc->fault(ctx, 500, "Empty presentity URI");
1703 if(event.s == NULL || event.len == 0) {
1704 LM_ERR("empty event parameter\n");
1705 rpc->fault(ctx, 500, "Empty event parameter");
1708 LM_DBG("event '%.*s'\n", event.len, event.s);
1710 if(refresh_type == 2) {
1712 LM_ERR("empty file uri or name parameters\n");
1713 rpc->fault(ctx, 500, "No file uri or name parameters");
1716 if(file_uri.s == NULL || file_uri.len == 0) {
1717 LM_ERR("empty file uri parameter\n");
1718 rpc->fault(ctx, 500, "Empty file uri parameter");
1722 if(filename.s == NULL || filename.len == 0) {
1723 LM_ERR("empty file name parameter\n");
1724 rpc->fault(ctx, 500, "Empty file name parameter");
1729 if(pres_refresh_watchers(&pres_uri, &event, refresh_type,
1730 file_uri.len ? &file_uri : NULL, filename.len ? &filename : NULL)
1732 rpc->fault(ctx, 500, "Execution failed");
1737 static const char *rpc_presence_refresh_watchers_doc[2] = {
1738 "Trigger refresh of watchers", 0};
1741 * rpc cmd: presence.updateWatchers
1745 void rpc_presence_update_watchers(rpc_t *rpc, void *ctx)
1747 str pres_uri = {0, 0};
1751 LM_DBG("init update of watchers\n");
1753 pn = rpc->scan(ctx, "SS", &pres_uri, &event);
1755 rpc->fault(ctx, 500, "Not enough parameters");
1759 if(pres_uri.s == NULL || pres_uri.len == 0) {
1760 LM_ERR("empty uri\n");
1761 rpc->fault(ctx, 500, "Empty presentity URI");
1765 if(event.s == NULL || event.len == 0) {
1766 LM_ERR("empty event parameter\n");
1767 rpc->fault(ctx, 500, "Empty event parameter");
1770 LM_DBG("uri '%.*s' - event '%.*s'\n", pres_uri.len, pres_uri.s,
1771 event.len, event.s);
1773 if(ki_pres_update_watchers(NULL, &pres_uri, &event)<0) {
1774 rpc->fault(ctx, 500, "Processing error");
1779 static const char *rpc_presence_update_watchers_doc[2] = {
1780 "Trigger update of watchers", 0};
1783 void rpc_presence_cleanup(rpc_t *rpc, void *c)
1785 LM_DBG("rpc_presence_cleanup:start\n");
1787 (void)msg_watchers_clean(0, 0);
1788 (void)msg_presentity_clean(0, 0);
1789 (void)timer_db_update(0, 0);
1791 rpc->rpl_printf(c, "Reload OK");
1795 static const char *rpc_presence_cleanup_doc[2] = {
1796 "Manually triggers the cleanup functions for the active_watchers, "
1797 "presentity, and watchers tables.",
1800 rpc_export_t presence_rpc[] = {
1801 {"presence.cleanup", rpc_presence_cleanup, rpc_presence_cleanup_doc, 0},
1802 {"presence.refreshWatchers", rpc_presence_refresh_watchers,
1803 rpc_presence_refresh_watchers_doc, 0},
1804 {"presence.updateWatchers", rpc_presence_update_watchers,
1805 rpc_presence_update_watchers_doc, 0},
1808 static int presence_init_rpc(void)
1810 if(rpc_register_array(presence_rpc) != 0) {
1811 LM_ERR("failed to register RPC commands\n");
1817 static int sip_uri_case_sensitive_match(str *s1, str *s2)
1820 LM_ERR("null pointer (s1) in sip_uri_match\n");
1824 LM_ERR("null pointer (s2) in sip_uri_match\n");
1827 return strncmp(s1->s, s2->s, s2->len);
1830 static int sip_uri_case_insensitive_match(str *s1, str *s2)
1833 LM_ERR("null pointer (s1) in sip_uri_match\n");
1837 LM_ERR("null pointer (s2) in sip_uri_match\n");
1840 return strncasecmp(s1->s, s2->s, s2->len);
1843 static int fixup_has_subscribers(void **param, int param_no)
1846 return fixup_spve_null(param, 1);
1847 } else if(param_no == 2) {
1848 return fixup_spve_null(param, 1);
1854 static int ki_pres_has_subscribers(sip_msg_t *msg, str *pres_uri, str *wevent)
1858 ev = contains_event(wevent, NULL);
1860 LM_ERR("event is not registered\n");
1864 return get_subscribers_count(msg, *pres_uri, *wevent) > 0 ? 1 : -1;
1867 static int w_pres_has_subscribers(sip_msg_t *msg, char *_pres_uri, char *_event)
1869 str presentity_uri, watched_event;
1871 if(fixup_get_svalue(msg, (gparam_p)_pres_uri, &presentity_uri) != 0) {
1872 LM_ERR("invalid presentity_uri parameter");
1875 if(fixup_get_svalue(msg, (gparam_p)_event, &watched_event) != 0) {
1876 LM_ERR("invalid watched_event parameter");
1880 return ki_pres_has_subscribers(msg, &presentity_uri, &watched_event);
1886 /* clang-format off */
1887 static sr_kemi_t sr_kemi_presence_exports[] = {
1888 { str_init("presence"), str_init("handle_publish"),
1889 SR_KEMIP_INT, ki_handle_publish,
1890 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1891 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1893 { str_init("presence"), str_init("handle_publish_uri"),
1894 SR_KEMIP_INT, ki_handle_publish_uri,
1895 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1896 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1898 { str_init("presence"), str_init("handle_subscribe"),
1899 SR_KEMIP_INT, handle_subscribe0,
1900 { SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1901 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1903 { str_init("presence"), str_init("handle_subscribe_uri"),
1904 SR_KEMIP_INT, handle_subscribe_uri,
1905 { SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1906 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1908 { str_init("presence"), str_init("pres_refresh_watchers"),
1909 SR_KEMIP_INT, ki_pres_refresh_watchers,
1910 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1911 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1913 { str_init("presence"), str_init("pres_refresh_watchers_file"),
1914 SR_KEMIP_INT, ki_pres_refresh_watchers_file,
1915 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1916 SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE }
1918 { str_init("presence"), str_init("pres_update_watchers"),
1919 SR_KEMIP_INT, ki_pres_update_watchers,
1920 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1921 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1923 { str_init("presence"), str_init("pres_has_subscribers"),
1924 SR_KEMIP_INT, ki_pres_has_subscribers,
1925 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1926 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1928 { str_init("presence"), str_init("pres_auth_status"),
1929 SR_KEMIP_INT, ki_pres_auth_status,
1930 { SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1931 SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1934 { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1936 /* clang-format on */
1941 int mod_register(char *path, int *dlflags, void *p1, void *p2)
1943 sr_kemi_modules_add(sr_kemi_presence_exports);