Browse code

presence: exported functions to kemi framework

Daniel-Constantin Mierla authored on 29/04/2017 06:31:15
Showing 6 changed files
... ...
@@ -65,7 +65,7 @@ int bind_presence(presence_api_t* api)
65 65
 	api->get_presentity= get_p_notify_body;
66 66
 	api->free_presentity= free_notify_body;
67 67
 	api->pres_auth_status= pres_auth_status;
68
-	api->handle_publish= handle_publish;
68
+	api->handle_publish= w_handle_publish;
69 69
 	api->handle_subscribe0= handle_subscribe0;
70 70
 	api->handle_subscribe= handle_subscribe;
71 71
 	return 0;
... ...
@@ -69,6 +69,7 @@
69 69
 #include "bind_presence.h"
70 70
 #include "notify.h"
71 71
 #include "../../core/mod_fix.h"
72
+#include "../../core/kemi.h"
72 73
 #include "../../core/timer_proc.h"
73 74
 #include "../../core/rpc.h"
74 75
 #include "../../core/rpc_lookup.h"
... ...
@@ -175,14 +176,14 @@ phtable_t* pres_htable=NULL;
175 176
 
176 177
 static cmd_export_t cmds[]=
177 178
 {
178
-	{"handle_publish",        (cmd_function)handle_publish,          0,
179
-		fixup_presence,0, REQUEST_ROUTE},
180
-	{"handle_publish",        (cmd_function)handle_publish,          1,
179
+	{"handle_publish",        (cmd_function)w_handle_publish,        0,
180
+		fixup_presence, 0, REQUEST_ROUTE},
181
+	{"handle_publish",        (cmd_function)w_handle_publish,        1,
181 182
 		fixup_presence, 0, REQUEST_ROUTE},
182 183
 	{"handle_subscribe",      (cmd_function)handle_subscribe0,       0,
183
-		fixup_subscribe,0, REQUEST_ROUTE},
184
+		fixup_subscribe, 0, REQUEST_ROUTE},
184 185
 	{"handle_subscribe",      (cmd_function)w_handle_subscribe,      1,
185
-		fixup_subscribe,0, REQUEST_ROUTE},
186
+		fixup_subscribe, 0, REQUEST_ROUTE},
186 187
 	{"pres_auth_status",      (cmd_function)w_pres_auth_status,      2,
187 188
 		fixup_pvar_pvar, fixup_free_pvar_pvar, REQUEST_ROUTE},
188 189
 	{"pres_refresh_watchers", (cmd_function)w_pres_refresh_watchers, 3,
... ...
@@ -191,7 +192,7 @@ static cmd_export_t cmds[]=
191 192
 		fixup_refresh_watchers, 0, ANY_ROUTE},
192 193
 	{"pres_update_watchers",  (cmd_function)w_pres_update_watchers,  2,
193 194
 		fixup_update_watchers, 0, ANY_ROUTE},
194
-        {"pres_has_subscribers",  (cmd_function)w_pres_has_subscribers,  2,
195
+	{"pres_has_subscribers",  (cmd_function)w_pres_has_subscribers,  2,
195 196
                 fixup_has_subscribers, 0, ANY_ROUTE},
196 197
  	{"bind_presence",         (cmd_function)bind_presence,           1,
197 198
 		0, 0, 0},
... ...
@@ -577,36 +578,21 @@ static int fixup_presence(void** param, int param_no)
577 578
 	pv_elem_t *model;
578 579
 	str s;
579 580
 
580
-	if(library_mode)
581
-	{
581
+	if(library_mode) {
582 582
 		LM_ERR("Bad config - you can not call 'handle_publish' function"
583 583
 				" (db_url not set)\n");
584 584
 		return -1;
585 585
 	}
586
-	if(param_no== 0)
586
+	if(param_no==0)
587 587
 		return 0;
588 588
 
589
-	if(*param)
590
-	{
591
-		s.s = (char*)(*param); s.len = strlen(s.s);
592
-		if(pv_parse_format(&s, &model)<0)
593
-		{
594
-			LM_ERR( "wrong format[%s]\n",(char*)(*param));
595
-			return E_UNSPEC;
596
-		}
597
-
598
-		*param = (void*)model;
599
-		return 0;
600
-	}
601
-	LM_ERR( "null format\n");
602
-	return E_UNSPEC;
589
+	return fixup_spve_null(param, 1);
603 590
 }
604 591
 
605 592
 static int fixup_subscribe(void** param, int param_no)
606 593
 {
607 594
 
608
-	if(library_mode)
609
-	{
595
+	if(library_mode) {
610 596
 		LM_ERR("Bad config - you can not call 'handle_subscribe' function"
611 597
 				" (db_url not set)\n");
612 598
 		return -1;
... ...
@@ -693,6 +679,16 @@ error:
693 679
 	return -1;
694 680
 }
695 681
 
682
+int ki_pres_refresh_watchers(sip_msg_t *msg, str *pres, str *event, int type)
683
+{
684
+	return pres_refresh_watchers(pres, event, type, NULL, NULL);
685
+}
686
+
687
+int ki_pres_refresh_watchers_file(sip_msg_t *msg, str *pres, str *event,
688
+		int type, str *file_uri, str *filename)
689
+{
690
+	return pres_refresh_watchers(pres, event, type, file_uri, filename);
691
+}
696 692
 
697 693
 int pres_update_status(subs_t subs, str reason, db_key_t* query_cols,
698 694
 		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)
1625 1621
 
1626 1622
 
1627 1623
 /**
1628
- * wrapper for update_watchers_status to use in config
1624
+ * wrapper for update_watchers_status to use via kemi
1629 1625
  */
1630
-static int w_pres_update_watchers(struct sip_msg *msg, char *puri,
1631
-		char *pevent)
1626
+static int ki_pres_update_watchers(struct sip_msg *msg, str *pres_uri,
1627
+		str *event)
1632 1628
 {
1633
-	str pres_uri;
1634
-	str event;
1635 1629
 	pres_ev_t* ev;
1636 1630
 	struct sip_uri uri;
1637 1631
 	str* rules_doc = NULL;
1638 1632
 	int ret;
1639 1633
 
1640
-	if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri)!=0)
1641
-	{
1642
-		LM_ERR("invalid uri parameter");
1643
-		return -1;
1644
-	}
1645
-
1646
-	if(fixup_get_svalue(msg, (gparam_p)pevent, &event)!=0)
1647
-	{
1648
-		LM_ERR("invalid uri parameter");
1649
-		return -1;
1650
-	}
1651
-
1652
-	ev = contains_event(&event, NULL);
1634
+	ev = contains_event(event, NULL);
1653 1635
 	if(ev==NULL)
1654 1636
 	{
1655 1637
 		LM_ERR("event %.*s is not registered\n",
1656
-				event.len, event.s);
1638
+				event->len, event->s);
1657 1639
 		return -1;
1658 1640
 	}
1659 1641
 	if(ev->get_rules_doc==NULL)
1660 1642
 	{
1661 1643
 		LM_DBG("event  %.*s does not provide rules doc API\n",
1662
-				event.len, event.s);
1644
+				event->len, event->s);
1663 1645
 		return -1;
1664 1646
 	}
1665
-	if(parse_uri(pres_uri.s, pres_uri.len, &uri)<0)
1647
+	if(parse_uri(pres_uri->s, pres_uri->len, &uri)<0)
1666 1648
 	{
1667 1649
 		LM_ERR("failed to parse presentity uri [%.*s]\n",
1668
-				pres_uri.len, pres_uri.s);
1650
+				pres_uri->len, pres_uri->s);
1669 1651
 		return -1;
1670 1652
 	}
1671 1653
 	ret = ev->get_rules_doc(&uri.user, &uri.host, &rules_doc);
1672 1654
 	if((ret < 0) || (rules_doc==NULL) || (rules_doc->s==NULL))
1673 1655
 	{
1674 1656
 		LM_DBG("no xcap rules doc found for presentity uri [%.*s]\n",
1675
-				pres_uri.len, pres_uri.s);
1657
+				pres_uri->len, pres_uri->s);
1676 1658
 		if(rules_doc != NULL)
1677 1659
 			pkg_free(rules_doc);
1678 1660
 		return -1;
1679 1661
 	}
1680 1662
 	ret = 1;
1681
-	if(update_watchers_status(pres_uri, ev, rules_doc)<0)
1663
+	if(update_watchers_status(*pres_uri, ev, rules_doc)<0)
1682 1664
 	{
1683 1665
 		LM_ERR("updating watchers in presence\n");
1684 1666
 		ret = -1;
... ...
@@ -1690,6 +1672,28 @@ static int w_pres_update_watchers(struct sip_msg *msg, char *puri,
1690 1672
 	return ret;
1691 1673
 }
1692 1674
 
1675
+/**
1676
+ * wrapper for update_watchers_status to use in config
1677
+ */
1678
+static int w_pres_update_watchers(struct sip_msg *msg, char *puri,
1679
+		char *pevent)
1680
+{
1681
+	str pres_uri;
1682
+	str event;
1683
+
1684
+	if(fixup_get_svalue(msg, (gparam_p)puri, &pres_uri)!=0)
1685
+	{
1686
+		LM_ERR("invalid uri parameter");
1687
+		return -1;
1688
+	}
1689
+
1690
+	if(fixup_get_svalue(msg, (gparam_p)pevent, &event)!=0)
1691
+	{
1692
+		LM_ERR("invalid uri parameter");
1693
+		return -1;
1694
+	}
1695
+	return ki_pres_update_watchers(msg, &pres_uri, &event);
1696
+}
1693 1697
 /**
1694 1698
  * fixup for w_pres_update_watchers
1695 1699
  */
... ...
@@ -1848,28 +1852,90 @@ static int fixup_has_subscribers(void** param, int param_no)
1848 1852
         return 0;
1849 1853
 }
1850 1854
 
1851
-static int w_pres_has_subscribers(struct sip_msg* msg, char* _pres_uri, char* _event)
1855
+static int ki_pres_has_subscribers(sip_msg_t *msg, str *pres_uri, str *wevent)
1852 1856
 {
1853
-        str presentity_uri, watched_event;
1854
-        pres_ev_t* ev;
1857
+	pres_ev_t *ev;
1855 1858
 
1856
-        if(fixup_get_svalue(msg, (gparam_p)_pres_uri, &presentity_uri)!=0)
1857
-        {
1858
-                LM_ERR("invalid presentity_uri parameter");
1859
-                return -1;
1860
-        }
1859
+	ev = contains_event(wevent, NULL);
1860
+	if(ev == NULL) {
1861
+		LM_ERR("event is not registered\n");
1862
+		return -1;
1863
+	}
1861 1864
 
1862
-        if(fixup_get_svalue(msg, (gparam_p)_event, &watched_event)!=0)
1863
-        {
1864
-                LM_ERR("invalid watched_event parameter");
1865
-                return -1;
1866
-        }
1865
+	return get_subscribers_count(msg, *pres_uri, *wevent) > 0 ? 1 : -1;
1866
+}
1867 1867
 
1868
-	ev = contains_event(&watched_event, NULL);
1869
-	if (ev == NULL) {
1870
-		LM_ERR("event is not registered\n");
1868
+static int w_pres_has_subscribers(sip_msg_t *msg, char *_pres_uri, char *_event)
1869
+{
1870
+	str presentity_uri, watched_event;
1871
+
1872
+	if(fixup_get_svalue(msg, (gparam_p)_pres_uri, &presentity_uri) != 0) {
1873
+		LM_ERR("invalid presentity_uri parameter");
1874
+		return -1;
1875
+	}
1876
+	if(fixup_get_svalue(msg, (gparam_p)_event, &watched_event) != 0) {
1877
+		LM_ERR("invalid watched_event parameter");
1871 1878
 		return -1;
1872 1879
 	}
1873 1880
 
1874
-	return get_subscribers_count(msg, presentity_uri, watched_event) > 0 ? 1 : -1;
1881
+	return ki_pres_has_subscribers(msg, &presentity_uri, &watched_event);
1875 1882
 }
1883
+
1884
+/**
1885
+ *
1886
+ */
1887
+/* clang-format off */
1888
+static sr_kemi_t sr_kemi_presence_exports[] = {
1889
+	{ str_init("presence"), str_init("handle_publish"),
1890
+		SR_KEMIP_INT, ki_handle_publish,
1891
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1892
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1893
+	},
1894
+	{ str_init("presence"), str_init("handle_publish_uri"),
1895
+		SR_KEMIP_INT, ki_handle_publish_uri,
1896
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1897
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1898
+	},
1899
+	{ str_init("presence"), str_init("handle_subscribe"),
1900
+		SR_KEMIP_INT, handle_subscribe0,
1901
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1902
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1903
+	},
1904
+	{ str_init("presence"), str_init("handle_subscribe_uri"),
1905
+		SR_KEMIP_INT, handle_subscribe_uri,
1906
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1907
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1908
+	},
1909
+	{ str_init("presence"), str_init("pres_refresh_watchers"),
1910
+		SR_KEMIP_INT, ki_pres_refresh_watchers,
1911
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1912
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1913
+	},
1914
+	{ str_init("presence"), str_init("pres_refresh_watchers_file"),
1915
+		SR_KEMIP_INT, ki_pres_refresh_watchers_file,
1916
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1917
+			SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE }
1918
+	},
1919
+	{ str_init("presence"), str_init("pres_update_watchers"),
1920
+		SR_KEMIP_INT, ki_pres_update_watchers,
1921
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1922
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1923
+	},
1924
+	{ str_init("presence"), str_init("pres_has_subscribers"),
1925
+		SR_KEMIP_INT, ki_pres_has_subscribers,
1926
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1927
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1928
+	},
1929
+
1930
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1931
+};
1932
+/* clang-format on */
1933
+
1934
+/**
1935
+ *
1936
+ */
1937
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
1938
+{
1939
+	sr_kemi_modules_add(sr_kemi_presence_exports);
1940
+	return 0;
1941
+}
1876 1942
\ No newline at end of file
... ...
@@ -38,6 +38,7 @@
38 38
 #include "../../core/lock_ops.h"
39 39
 #include "../../core/hashes.h"
40 40
 #include "../../core/strutils.h"
41
+#include "../../core/mod_fix.h"
41 42
 #include "../../lib/srdb1/db.h"
42 43
 #include "presence.h"
43 44
 #include "notify.h"
... ...
@@ -279,7 +280,7 @@ error:
279 280
  * PUBLISH request handling
280 281
  *
281 282
  */
282
-int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
283
+int ki_handle_publish_uri(struct sip_msg* msg, str* sender_uri)
283 284
 {
284 285
 	struct sip_uri puri;
285 286
 	str body;
... ...
@@ -450,11 +451,13 @@ int handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
450 451
 		{
451 452
 			ERR_MEM(PKG_MEM_STR);
452 453
 		}
453
-		if(pv_printf(msg, (pv_elem_t*)sender_uri, buf, &buf_len)<0)
454
-		{
455
-			LM_ERR("cannot print the format\n");
454
+		if(sender_uri->len >= buf_len-1) {
455
+			LM_ERR("cannot use sender uri -- too long value\n");
456 456
 			goto error;
457 457
 		}
458
+		strncpy(buf, sender_uri->s, sender_uri->len);
459
+		buf_len = sender_uri->len;
460
+		buf[buf_len] = '\0';
458 461
 		if(parse_uri(buf, buf_len, &puri)!=0)
459 462
 		{
460 463
 			LM_ERR("bad sender SIP address!\n");
... ...
@@ -540,6 +543,28 @@ error:
540 543
 
541 544
 }
542 545
 
546
+int ki_handle_publish(struct sip_msg* msg)
547
+{
548
+	return ki_handle_publish_uri(msg, NULL);
549
+}
550
+
551
+/**
552
+ * PUBLISH request handling
553
+ *
554
+ */
555
+int w_handle_publish(struct sip_msg* msg, char* sender_uri, char* str2)
556
+{
557
+	str suri;
558
+
559
+	if (sender_uri!=NULL
560
+			&& fixup_get_svalue(msg, (gparam_t*)sender_uri, &suri) != 0) {
561
+		LM_ERR("invalid uri parameter\n");
562
+		return -1;
563
+	}
564
+
565
+	return ki_handle_publish_uri(msg, (sender_uri)?&suri:NULL);
566
+}
567
+
543 568
 int update_hard_presentity(str *pres_uri, pres_ev_t *event, str *file_uri, str *filename)
544 569
 {
545 570
 	int ret = -1, new_t, pidf_result;
... ...
@@ -44,7 +44,9 @@
44 44
 
45 45
 void msg_presentity_clean(unsigned int ticks,void *param);
46 46
 
47
-int handle_publish(struct sip_msg* msg, char* str1 ,char* str2);
47
+int w_handle_publish(struct sip_msg* msg, char* str1, char* str2);
48
+int ki_handle_publish(sip_msg_t* msg);
49
+int ki_handle_publish_uri(sip_msg_t* msg, str* suri);
48 50
 int update_hard_presentity(str *pres_uri, pres_ev_t *event, str *file_uri, str *filename);
49 51
 
50 52
 #endif
... ...
@@ -1019,24 +1019,39 @@ int handle_subscribe0(struct sip_msg* msg)
1019 1019
 			pfrom->parsed_uri.host);
1020 1020
 }
1021 1021
 
1022
-int w_handle_subscribe(struct sip_msg* msg, char* watcher_uri)
1022
+int w_handle_subscribe0(struct sip_msg* msg, char* p1, char *p2)
1023
+{
1024
+	return handle_subscribe0(msg);
1025
+}
1026
+
1027
+int handle_subscribe_uri(struct sip_msg* msg, str* wuri)
1023 1028
 {
1024
-	str wuri;
1025 1029
 	struct sip_uri parsed_wuri;
1026 1030
 
1027
-	if (fixup_get_svalue(msg, (gparam_p)watcher_uri, &wuri) != 0)
1031
+	if (parse_uri(wuri->s, wuri->len, &parsed_wuri) < 0)
1028 1032
 	{
1029
-		LM_ERR("invalid uri parameter\n");
1033
+		LM_ERR("failed to parse watcher URI\n");
1030 1034
 		return -1;
1031 1035
 	}
1032 1036
 
1033
-	if (parse_uri(wuri.s, wuri.len, &parsed_wuri) < 0)
1037
+	return handle_subscribe(msg, parsed_wuri.user, parsed_wuri.host);
1038
+}
1039
+
1040
+int w_handle_subscribe(struct sip_msg* msg, char* watcher_uri, char* p2)
1041
+{
1042
+	str wuri;
1043
+
1044
+	if (fixup_get_svalue(msg, (gparam_p)watcher_uri, &wuri) != 0)
1034 1045
 	{
1035
-		LM_ERR("failed to parse watcher URI\n");
1046
+		LM_ERR("invalid uri parameter\n");
1036 1047
 		return -1;
1037 1048
 	}
1049
+	return handle_subscribe_uri(msg, &wuri);
1050
+}
1038 1051
 
1039
-	return handle_subscribe(msg, parsed_wuri.user, parsed_wuri.host);
1052
+int w_handle_subscribe1(struct sip_msg* msg, char* watcher_uri)
1053
+{
1054
+	return w_handle_subscribe(msg, watcher_uri, NULL);
1040 1055
 }
1041 1056
 
1042 1057
 int handle_subscribe(struct sip_msg* msg, str watcher_user, str watcher_domain)
... ...
@@ -95,7 +95,10 @@ void msg_active_watchers_clean(unsigned int ticks,void *param);
95 95
 void msg_watchers_clean(unsigned int ticks,void *param);
96 96
 
97 97
 int handle_subscribe0(struct sip_msg*);
98
-int w_handle_subscribe(struct sip_msg*, char *watcher_uri);
98
+int w_handle_subscribe0(struct sip_msg*, char*, char*);
99
+int w_handle_subscribe(struct sip_msg*, char *watcher_uri, char *p2);
100
+int w_handle_subscribe1(struct sip_msg*, char *watcher_uri);
101
+int handle_subscribe_uri(struct sip_msg*, str*);
99 102
 int handle_subscribe(struct sip_msg*, str watcher_user, str watcher_domain);
100 103
 
101 104
 void timer_db_update(unsigned int ticks,void *param);