Skip to content

Commit

Permalink
presence: add $subs pv for subscription access
Browse files Browse the repository at this point in the history
after handle_subscribe there is no safe way in script to obtain the presently
because re-subscription RURI is ip of kamailio and To-Uri may not contain
the presently.

$subs(uri) will allow access to this value

added as a pv to allow extension to get more properties from subscription
  • Loading branch information
lazedo committed Mar 30, 2015
1 parent 202506e commit 97f1909
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
34 changes: 34 additions & 0 deletions modules/presence/doc/presence_admin.xml
Expand Up @@ -1129,6 +1129,40 @@ pres_update_watchers("sip:test@kamailio.org", "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>
Expand Down
7 changes: 6 additions & 1 deletion modules/presence/presence.c
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions modules/presence/presence.h
Expand Up @@ -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 changes: 51 additions & 0 deletions modules/presence/subscribe.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 97f1909

Please sign in to comment.