Pārlūkot izejas kodu

presence: exported functions to kemi framework

Daniel-Constantin Mierla 8 gadi atpakaļ
vecāks
revīzija
da8690ddce

+ 1 - 1
src/modules/presence/bind_presence.c

@@ -65,7 +65,7 @@ int bind_presence(presence_api_t* api)
 	api->get_presentity= get_p_notify_body;
 	api->free_presentity= free_notify_body;
 	api->pres_auth_status= pres_auth_status;
-	api->handle_publish= handle_publish;
+	api->handle_publish= w_handle_publish;
 	api->handle_subscribe0= handle_subscribe0;
 	api->handle_subscribe= handle_subscribe;
 	return 0;

+ 132 - 66
src/modules/presence/presence.c

@@ -69,6 +69,7 @@
 #include "bind_presence.h"
 #include "notify.h"
 #include "../../core/mod_fix.h"
+#include "../../core/kemi.h"
 #include "../../core/timer_proc.h"
 #include "../../core/rpc.h"
 #include "../../core/rpc_lookup.h"
@@ -175,14 +176,14 @@ phtable_t* pres_htable=NULL;
 
 static cmd_export_t cmds[]=
 {
-	{"handle_publish",        (cmd_function)handle_publish,          0,
-		fixup_presence,0, REQUEST_ROUTE},
-	{"handle_publish",        (cmd_function)handle_publish,          1,
+	{"handle_publish",        (cmd_function)w_handle_publish,        0,
+		fixup_presence, 0, REQUEST_ROUTE},
+	{"handle_publish",        (cmd_function)w_handle_publish,        1,
 		fixup_presence, 0, REQUEST_ROUTE},
 	{"handle_subscribe",      (cmd_function)handle_subscribe0,       0,
-		fixup_subscribe,0, REQUEST_ROUTE},
+		fixup_subscribe, 0, REQUEST_ROUTE},
 	{"handle_subscribe",      (cmd_function)w_handle_subscribe,      1,
-		fixup_subscribe,0, REQUEST_ROUTE},
+		fixup_subscribe, 0, REQUEST_ROUTE},
 	{"pres_auth_status",      (cmd_function)w_pres_auth_status,      2,
 		fixup_pvar_pvar, fixup_free_pvar_pvar, REQUEST_ROUTE},
 	{"pres_refresh_watchers", (cmd_function)w_pres_refresh_watchers, 3,
@@ -191,7 +192,7 @@ static cmd_export_t cmds[]=
 		fixup_refresh_watchers, 0, ANY_ROUTE},
 	{"pres_update_watchers",  (cmd_function)w_pres_update_watchers,  2,
 		fixup_update_watchers, 0, ANY_ROUTE},
-        {"pres_has_subscribers",  (cmd_function)w_pres_has_subscribers,  2,
+	{"pres_has_subscribers",  (cmd_function)w_pres_has_subscribers,  2,
                 fixup_has_subscribers, 0, ANY_ROUTE},
  	{"bind_presence",         (cmd_function)bind_presence,           1,
 		0, 0, 0},
@@ -577,36 +578,21 @@ static int fixup_presence(void** param, int param_no)
 	pv_elem_t *model;
 	str s;
 
-	if(library_mode)
-	{
+	if(library_mode) {
 		LM_ERR("Bad config - you can not call 'handle_publish' function"
 				" (db_url not set)\n");
 		return -1;
 	}
-	if(param_no== 0)
+	if(param_no==0)
 		return 0;
 
-	if(*param)
-	{
-		s.s = (char*)(*param); s.len = strlen(s.s);
-		if(pv_parse_format(&s, &model)<0)
-		{
-			LM_ERR( "wrong format[%s]\n",(char*)(*param));
-			return E_UNSPEC;
-		}
-
-		*param = (void*)model;
-		return 0;
-	}
-	LM_ERR( "null format\n");
-	return E_UNSPEC;
+	return fixup_spve_null(param, 1);
 }
 
 static int fixup_subscribe(void** param, int param_no)
 {
 
-	if(library_mode)
-	{
+	if(library_mode) {
 		LM_ERR("Bad config - you can not call 'handle_subscribe' function"
 				" (db_url not set)\n");
 		return -1;
@@ -693,6 +679,16 @@ error:
 	return -1;
 }
 
+int ki_pres_refresh_watchers(sip_msg_t *msg, str *pres, str *event, int type)
+{
+	return pres_refresh_watchers(pres, event, type, NULL, NULL);
+}
+
+int ki_pres_refresh_watchers_file(sip_msg_t *msg, str *pres, str *event,
+		int type, str *file_uri, str *filename)
+{
+	return pres_refresh_watchers(pres, event, type, file_uri, filename);
+}
 
 int pres_update_status(subs_t subs, str reason, db_key_t* query_cols,
 		db_val_t* query_vals, int n_query_cols, subs_t** subs_array)
@@ -1625,60 +1621,46 @@ static int fixup_refresh_watchers(void** param, int param_no)
 
 
 /**
- * wrapper for update_watchers_status to use in config
+ * wrapper for update_watchers_status to use via kemi
  */
-static int w_pres_update_watchers(struct sip_msg *msg, char *puri,
-		char *pevent)
+static int ki_pres_update_watchers(struct sip_msg *msg, str *pres_uri,
+		str *event)
 {
-	str pres_uri;
-	str event;
 	pres_ev_t* ev;
 	struct sip_uri uri;
 	str* rules_doc = NULL;
 	int ret;
 
-	if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri)!=0)
-	{
-		LM_ERR("invalid uri parameter");
-		return -1;
-	}
-
-	if(fixup_get_svalue(msg, (gparam_p)pevent, &event)!=0)
-	{
-		LM_ERR("invalid uri parameter");
-		return -1;
-	}
-
-	ev = contains_event(&event, NULL);
+	ev = contains_event(event, NULL);
 	if(ev==NULL)
 	{
 		LM_ERR("event %.*s is not registered\n",
-				event.len, event.s);
+				event->len, event->s);
 		return -1;
 	}
 	if(ev->get_rules_doc==NULL)
 	{
 		LM_DBG("event  %.*s does not provide rules doc API\n",
-				event.len, event.s);
+				event->len, event->s);
 		return -1;
 	}
-	if(parse_uri(pres_uri.s, pres_uri.len, &uri)<0)
+	if(parse_uri(pres_uri->s, pres_uri->len, &uri)<0)
 	{
 		LM_ERR("failed to parse presentity uri [%.*s]\n",
-				pres_uri.len, pres_uri.s);
+				pres_uri->len, pres_uri->s);
 		return -1;
 	}
 	ret = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
 	if((ret < 0) || (rules_doc==NULL) || (rules_doc->s==NULL))
 	{
 		LM_DBG("no xcap rules doc found for presentity uri [%.*s]\n",
-				pres_uri.len, pres_uri.s);
+				pres_uri->len, pres_uri->s);
 		if(rules_doc != NULL)
 			pkg_free(rules_doc);
 		return -1;
 	}
 	ret = 1;
-	if(update_watchers_status(pres_uri, ev, rules_doc)<0)
+	if(update_watchers_status(*pres_uri, ev, rules_doc)<0)
 	{
 		LM_ERR("updating watchers in presence\n");
 		ret = -1;
@@ -1690,6 +1672,28 @@ static int w_pres_update_watchers(struct sip_msg *msg, char *puri,
 	return ret;
 }
 
+/**
+ * wrapper for update_watchers_status to use in config
+ */
+static int w_pres_update_watchers(struct sip_msg *msg, char *puri,
+		char *pevent)
+{
+	str pres_uri;
+	str event;
+
+	if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri)!=0)
+	{
+		LM_ERR("invalid uri parameter");
+		return -1;
+	}
+
+	if(fixup_get_svalue(msg, (gparam_p)pevent, &event)!=0)
+	{
+		LM_ERR("invalid uri parameter");
+		return -1;
+	}
+	return ki_pres_update_watchers(msg, &pres_uri, &event);
+}
 /**
  * fixup for w_pres_update_watchers
  */
@@ -1848,28 +1852,90 @@ static int fixup_has_subscribers(void** param, int param_no)
         return 0;
 }
 
-static int w_pres_has_subscribers(struct sip_msg* msg, char* _pres_uri, char* _event)
+static int ki_pres_has_subscribers(sip_msg_t *msg, str *pres_uri, str *wevent)
 {
-        str presentity_uri, watched_event;
-        pres_ev_t* ev;
+	pres_ev_t *ev;
 
-        if(fixup_get_svalue(msg, (gparam_p)_pres_uri, &presentity_uri)!=0)
-        {
-                LM_ERR("invalid presentity_uri parameter");
-                return -1;
-        }
+	ev = contains_event(wevent, NULL);
+	if(ev == NULL) {
+		LM_ERR("event is not registered\n");
+		return -1;
+	}
 
-        if(fixup_get_svalue(msg, (gparam_p)_event, &watched_event)!=0)
-        {
-                LM_ERR("invalid watched_event parameter");
-                return -1;
-        }
+	return get_subscribers_count(msg, *pres_uri, *wevent) > 0 ? 1 : -1;
+}
 
-	ev = contains_event(&watched_event, NULL);
-	if (ev == NULL) {
-		LM_ERR("event is not registered\n");
+static int w_pres_has_subscribers(sip_msg_t *msg, char *_pres_uri, char *_event)
+{
+	str presentity_uri, watched_event;
+
+	if(fixup_get_svalue(msg, (gparam_p)_pres_uri, &presentity_uri) != 0) {
+		LM_ERR("invalid presentity_uri parameter");
+		return -1;
+	}
+	if(fixup_get_svalue(msg, (gparam_p)_event, &watched_event) != 0) {
+		LM_ERR("invalid watched_event parameter");
 		return -1;
 	}
 
-	return get_subscribers_count(msg, presentity_uri, watched_event) > 0 ? 1 : -1;
+	return ki_pres_has_subscribers(msg, &presentity_uri, &watched_event);
 }
+
+/**
+ *
+ */
+/* clang-format off */
+static sr_kemi_t sr_kemi_presence_exports[] = {
+	{ str_init("presence"), str_init("handle_publish"),
+		SR_KEMIP_INT, ki_handle_publish,
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("handle_publish_uri"),
+		SR_KEMIP_INT, ki_handle_publish_uri,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("handle_subscribe"),
+		SR_KEMIP_INT, handle_subscribe0,
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("handle_subscribe_uri"),
+		SR_KEMIP_INT, handle_subscribe_uri,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("pres_refresh_watchers"),
+		SR_KEMIP_INT, ki_pres_refresh_watchers,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("pres_refresh_watchers_file"),
+		SR_KEMIP_INT, ki_pres_refresh_watchers_file,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
+			SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("pres_update_watchers"),
+		SR_KEMIP_INT, ki_pres_update_watchers,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("presence"), str_init("pres_has_subscribers"),
+		SR_KEMIP_INT, ki_pres_has_subscribers,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
+};
+/* clang-format on */
+
+/**
+ *
+ */
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
+{
+	sr_kemi_modules_add(sr_kemi_presence_exports);
+	return 0;
+}

+ 29 - 4
src/modules/presence/publish.c

@@ -38,6 +38,7 @@
 #include "../../core/lock_ops.h"
 #include "../../core/hashes.h"
 #include "../../core/strutils.h"
+#include "../../core/mod_fix.h"
 #include "../../lib/srdb1/db.h"
 #include "presence.h"
 #include "notify.h"
@@ -279,7 +280,7 @@ error:
  * PUBLISH request handling
  *
  */
-int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
+int ki_handle_publish_uri(struct sip_msg* msg, str* sender_uri)
 {
 	struct sip_uri puri;
 	str body;
@@ -450,11 +451,13 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
 		{
 			ERR_MEM(PKG_MEM_STR);
 		}
-		if(pv_printf(msg, (pv_elem_t*)sender_uri, buf, &buf_len)<0)
-		{
-			LM_ERR("cannot print the format\n");
+		if(sender_uri->len >= buf_len-1) {
+			LM_ERR("cannot use sender uri -- too long value\n");
 			goto error;
 		}
+		strncpy(buf, sender_uri->s, sender_uri->len);
+		buf_len = sender_uri->len;
+		buf[buf_len] = '\0';
 		if(parse_uri(buf, buf_len, &puri)!=0)
 		{
 			LM_ERR("bad sender SIP address!\n");
@@ -540,6 +543,28 @@ error:
 
 }
 
+int ki_handle_publish(struct sip_msg* msg)
+{
+	return ki_handle_publish_uri(msg, NULL);
+}
+
+/**
+ * PUBLISH request handling
+ *
+ */
+int w_handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
+{
+	str suri;
+
+	if (sender_uri!=NULL
+			&& fixup_get_svalue(msg, (gparam_t*)sender_uri, &suri) != 0) {
+		LM_ERR("invalid uri parameter\n");
+		return -1;
+	}
+
+	return ki_handle_publish_uri(msg, (sender_uri)?&suri:NULL);
+}
+
 int update_hard_presentity(str *pres_uri, pres_ev_t *event, str *file_uri, str *filename)
 {
 	int ret = -1, new_t, pidf_result;

+ 3 - 1
src/modules/presence/publish.h

@@ -44,7 +44,9 @@
 
 void msg_presentity_clean(unsigned int ticks,void *param);
 
-int handle_publish(struct sip_msg* msg, char* str1 ,char* str2);
+int w_handle_publish(struct sip_msg* msg, char* str1, char* str2);
+int ki_handle_publish(sip_msg_t* msg);
+int ki_handle_publish_uri(sip_msg_t* msg, str* suri);
 int update_hard_presentity(str *pres_uri, pres_ev_t *event, str *file_uri, str *filename);
 
 #endif

+ 22 - 7
src/modules/presence/subscribe.c

@@ -1019,24 +1019,39 @@ int handle_subscribe0(struct sip_msg* msg)
 			pfrom->parsed_uri.host);
 }
 
-int w_handle_subscribe(struct sip_msg* msg, char* watcher_uri)
+int w_handle_subscribe0(struct sip_msg* msg, char* p1, char *p2)
+{
+	return handle_subscribe0(msg);
+}
+
+int handle_subscribe_uri(struct sip_msg* msg, str* wuri)
 {
-	str wuri;
 	struct sip_uri parsed_wuri;
 
-	if (fixup_get_svalue(msg, (gparam_p)watcher_uri, &wuri) != 0)
+	if (parse_uri(wuri->s, wuri->len, &parsed_wuri) < 0)
 	{
-		LM_ERR("invalid uri parameter\n");
+		LM_ERR("failed to parse watcher URI\n");
 		return -1;
 	}
 
-	if (parse_uri(wuri.s, wuri.len, &parsed_wuri) < 0)
+	return handle_subscribe(msg, parsed_wuri.user, parsed_wuri.host);
+}
+
+int w_handle_subscribe(struct sip_msg* msg, char* watcher_uri, char* p2)
+{
+	str wuri;
+
+	if (fixup_get_svalue(msg, (gparam_p)watcher_uri, &wuri) != 0)
 	{
-		LM_ERR("failed to parse watcher URI\n");
+		LM_ERR("invalid uri parameter\n");
 		return -1;
 	}
+	return handle_subscribe_uri(msg, &wuri);
+}
 
-	return handle_subscribe(msg, parsed_wuri.user, parsed_wuri.host);
+int w_handle_subscribe1(struct sip_msg* msg, char* watcher_uri)
+{
+	return w_handle_subscribe(msg, watcher_uri, NULL);
 }
 
 int handle_subscribe(struct sip_msg* msg, str watcher_user, str watcher_domain)

+ 4 - 1
src/modules/presence/subscribe.h

@@ -95,7 +95,10 @@ void msg_active_watchers_clean(unsigned int ticks,void *param);
 void msg_watchers_clean(unsigned int ticks,void *param);
 
 int handle_subscribe0(struct sip_msg*);
-int w_handle_subscribe(struct sip_msg*, char *watcher_uri);
+int w_handle_subscribe0(struct sip_msg*, char*, char*);
+int w_handle_subscribe(struct sip_msg*, char *watcher_uri, char *p2);
+int w_handle_subscribe1(struct sip_msg*, char *watcher_uri);
+int handle_subscribe_uri(struct sip_msg*, str*);
 int handle_subscribe(struct sip_msg*, str watcher_user, str watcher_domain);
 
 void timer_db_update(unsigned int ticks,void *param);