Răsfoiți Sursa

Merge pull request #118 from kamailio/lazedo/presence-subs

presence: add $subs pv for subscription access
lazedo 10 ani în urmă
părinte
comite
08acf091a4

+ 34 - 0
modules/presence/doc/presence_admin.xml

@@ -1129,6 +1129,40 @@ pres_update_watchers("sip:[email protected]", "presence");
 	</section>
 </section>
 
+<section>
+	<title>Pseudo Variables</title>
+		
+		<section>
+			<title><varname>$subs(attr)</varname></title>
+			<para>
+				Access the attributes of handled subscription. 
+				It must be used after a call of
+				<quote>handle_subscription())</quote>.
+			</para>
+			<para>
+			The <quote>attr</quote> can be:
+			</para>
+			<itemizedlist>
+				<listitem>
+				<para><emphasis>uri</emphasis> - subscription presentity uri
+				</para>
+				</listitem>	  
+			</itemizedlist>
+
+			<example>
+			<title><function moreinfo="none">$subs(name)</function> usage</title>
+<programlisting format="linespecific">
+...
+if(handle_subscription())
+{
+  xlog("presentity=$subs(uri)\n");
+}
+...
+				 </programlisting>
+			</example>
+		</section>
+</section>
+
 <section>
 	<title>Installation</title>
 	<para>

+ 6 - 1
modules/presence/presence.c

@@ -228,6 +228,11 @@ static mi_export_t mi_cmds[] = {
 	{  0,                0,                     0,  0,  0}
 };
 
+static pv_export_t pres_mod_pvs[] = {
+	{{"subs", (sizeof("subs")-1)}, PVT_OTHER, pv_get_subscription, 0, pv_parse_subscription_name, 0, 0, 0},
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
+};
+
 /** module exports */
 struct module_exports exports= {
 	"presence",			/* module name */
@@ -236,7 +241,7 @@ struct module_exports exports= {
 	params,				/* exported parameters */
 	0,					/* exported statistics */
 	mi_cmds,   			/* exported MI functions */
-	0,					/* exported pseudo-variables */
+	pres_mod_pvs,		/* exported pseudo-variables */
 	0,					/* extra processes */
 	mod_init,			/* module initialization function */
 	0,   				/* response handling function */

+ 3 - 0
modules/presence/presence.h

@@ -102,4 +102,7 @@ int pres_auth_status(struct sip_msg* msg, str watcher_uri, str presentity_uri);
 typedef int (*sip_uri_match_f) (str* s1, str* s2);
 extern sip_uri_match_f presence_sip_uri_match;
 
+int pv_get_subscription(struct sip_msg *msg, pv_param_t *param,	pv_value_t *res);
+int pv_parse_subscription_name(pv_spec_p sp, str *in);
+
 #endif /* PA_MOD_H */

+ 51 - 0
modules/presence/subscribe.c

@@ -755,6 +755,45 @@ void msg_watchers_clean(unsigned int ticks,void *param)
 		LM_ERR("cleaning pending subscriptions\n");
 }
 
+char* last_presentity = NULL;
+
+int pv_parse_subscription_name(pv_spec_p sp, str *in)
+{
+	if(sp==NULL || in==NULL || in->len<=0)
+		return -1;
+
+	switch(in->len)
+	{
+		case 3:
+			if(strncmp(in->s, "uri", 3)==0) {
+				sp->pvp.pvn.u.isname.name.n = 1;
+			} else {
+				goto error;
+			};
+			break;
+		default:
+			goto error;
+	}
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
+	sp->pvp.pvn.u.isname.type = 0;
+
+	return 0;
+
+error:
+	LM_ERR("unknown PV subscription name %.*s\n", in->len, in->s);
+	return -1;
+}
+
+int pv_get_subscription(struct sip_msg *msg, pv_param_t *param,	pv_value_t *res)
+{
+	if(param->pvn.u.isname.name.n==1) /* presentity */
+		return last_presentity == NULL ? pv_get_null(msg, param, res) : pv_get_strzval(msg, param, res, last_presentity);
+
+	LM_ERR("unknown specifier\n");
+	return pv_get_null(msg, param, res);
+
+}
+
 int handle_subscribe0(struct sip_msg* msg)
 {
 	struct to_body *pfrom;
@@ -809,6 +848,11 @@ int handle_subscribe(struct sip_msg* msg, str watcher_user, str watcher_domain)
 	str reply_str;
 	int sent_reply= 0;
 
+	if(last_presentity) {
+		pkg_free(last_presentity);
+		last_presentity = NULL;
+	}
+
 	/* ??? rename to avoid collisions with other symbols */
 	counter++;
 
@@ -906,6 +950,13 @@ int handle_subscribe(struct sip_msg* msg, str watcher_user, str watcher_domain)
 		}
 		reason= subs.reason;
 	}
+
+	if(subs.pres_uri.len > 0 && subs.pres_uri.s) {
+		last_presentity= (char*)pkg_malloc((subs.pres_uri.len+1) * sizeof(char));
+		strncpy(last_presentity, subs.pres_uri.s, subs.pres_uri.len);
+		last_presentity[subs.pres_uri.len] = '\0';
+	}
+		
 	/* mark that the received event is a SUBSCRIBE message */
 	subs.recv_event = PRES_SUBSCRIBE_RECV;