Skip to content

Commit

Permalink
Merge pull request #1424 from kamailio/eschmidbauer/pua_json
Browse files Browse the repository at this point in the history
pua_json: new module to update presence using JSON data objects
  • Loading branch information
Emmanuel Schmidbauer committed Feb 8, 2018
2 parents 09291d8 + 81aa7c1 commit 02cb90c
Show file tree
Hide file tree
Showing 15 changed files with 842 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/modules/presence/bind_presence.h
Expand Up @@ -43,7 +43,7 @@ typedef int (*pres_handle_publish_t)(struct sip_msg* msg, char *str1, char* str2
typedef int (*pres_handle_subscribe0_t)(struct sip_msg* msg);
typedef int (*pres_handle_subscribe_t)(struct sip_msg* msg, str watcher_user, str watcher_domain);
typedef int (*pres_update_presentity_t)(str *event, str *realm, str *user, str *etag,
str *sender, str *body, int expires, int new_t);
str *sender, str *body, int expires, int new_t, int replace);
typedef int (*pres_refresh_watchers_t)(str *pres, str *event, int type);

typedef struct presence_api {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/presence/presence_dmq.c
Expand Up @@ -302,7 +302,7 @@ int pres_dmq_handle_msg(
switch(action) {
case PRES_DMQ_UPDATE_PRESENTITY:
if(update_presentity(NULL, presentity, &p_body, t_new, &sent_reply,
sphere, &cur_etag, &ruid)
sphere, &cur_etag, &ruid, 0)
< 0) {
goto error;
}
Expand Down
17 changes: 12 additions & 5 deletions src/modules/presence/presentity.c
Expand Up @@ -281,7 +281,7 @@ int check_if_dialog(str body, int *is_dialog, char **dialog_id)
doc = xmlParseMemory(body.s, body.len);
if(doc== NULL)
{
LM_ERR("failed to parse xml document\n");
LM_INFO("failed to parse xml document\n");
return -1;
}

Expand Down Expand Up @@ -556,7 +556,7 @@ int is_dialog_terminated(presentity_t* presentity)
}

int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
int new_t, int* sent_reply, char* sphere, str* etag_override, str* ruid)
int new_t, int* sent_reply, char* sphere, str* etag_override, str* ruid, int replace)
{
db_key_t query_cols[14], rquery_cols[2], update_keys[9], result_cols[7];
db_op_t query_ops[14], rquery_ops[2];
Expand Down Expand Up @@ -658,6 +658,9 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
}

LM_DBG("new htable record added\n");
if (presentity->expires == -1) {
replace = 1;
}

/* insert new record into database */
query_cols[n_query_cols] = &str_sender_col;
Expand Down Expand Up @@ -697,7 +700,7 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
query_vals[n_query_cols].val.str_val = p_ruid;
n_query_cols++;

if (presentity->expires != -1)
if (!replace)
{
/* A real PUBLISH */
query_cols[n_query_cols] = &str_expires_col;
Expand Down Expand Up @@ -748,6 +751,10 @@ int update_presentity(struct sip_msg* msg, presentity_t* presentity, str* body,
query_vals[n_query_cols].type = DB1_INT;
query_vals[n_query_cols].nul = 0;
query_vals[n_query_cols].val.int_val = -1;
if(presentity->expires != -1) {
query_vals[n_query_cols].val.int_val =
presentity->expires + (int)time(NULL);
}
n_query_cols++;

if (pa_dbf.use_table(pa_db, &presentity_table) < 0)
Expand Down Expand Up @@ -1918,7 +1925,7 @@ int delete_offline_presentities(str *pres_uri, pres_ev_t *event)

// used for API updates to the presentity table
int _api_update_presentity(str *event, str *realm, str *user, str *etag,
str *sender, str *body, int expires, int new_t)
str *sender, str *body, int expires, int new_t, int replace)
{
int ret;
presentity_t *pres = NULL;
Expand All @@ -1936,7 +1943,7 @@ int _api_update_presentity(str *event, str *realm, str *user, str *etag,
if(sphere_enable) {
sphere = extract_sphere(*body);
}
ret = update_presentity(NULL, pres, body, new_t, NULL, sphere, NULL, NULL);
ret = update_presentity(NULL, pres, body, new_t, NULL, sphere, NULL, NULL, replace);

if(pres)
pkg_free(pres);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/presence/presentity.h
Expand Up @@ -55,11 +55,11 @@ presentity_t* new_presentity( str* domain,str* user,int expires,

/* update presentity in database */
int update_presentity(struct sip_msg* msg,presentity_t* p,str* body,int t_new,
int* sent_reply, char* sphere, str* etag_override, str* ruid);
int* sent_reply, char* sphere, str* etag_override, str* ruid, int replace);

/* update presentity in database using API */
int _api_update_presentity(str *event, str *realm, str *user, str *etag,
str *sender, str *body, int expires, int reset);
str *sender, str *body, int expires, int new_t, int replace);

/* free memory */
void free_presentity(presentity_t *p);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/presence/publish.c
Expand Up @@ -494,7 +494,7 @@ int ki_handle_publish_uri(struct sip_msg* msg, str* sender_uri)
}

/* querry the database and update or insert */
if(update_presentity(msg, presentity, &body, etag_gen, &sent_reply, sphere, NULL, NULL) <0)
if(update_presentity(msg, presentity, &body, etag_gen, &sent_reply, sphere, NULL, NULL, 0) <0)
{
LM_ERR("when updating presentity\n");
goto error;
Expand Down Expand Up @@ -635,7 +635,7 @@ int update_hard_presentity(str *pres_uri, pres_ev_t *event, str *file_uri, str *
goto done;
}

if (update_presentity(NULL, pres, pidf_doc, new_t, NULL, sphere, NULL, NULL) < 0)
if (update_presentity(NULL, pres, pidf_doc, new_t, NULL, sphere, NULL, NULL, 0) < 0)
{
LM_ERR("updating presentity\n");
goto done;
Expand Down
17 changes: 17 additions & 0 deletions src/modules/pua_json/Makefile
@@ -0,0 +1,17 @@
#
# PUA_JSON module
#
#
# WARNING: do not run this directly, it should be run by the master Makefile

include ../../Makefile.defs

auto_gen=
NAME=pua_json.so

LIBS=-ljson-c
DEFS+=-I$(LOCALBASE)/include -I/usr/local/include $(shell pkg-config --cflags json-c)

DEFS+=-DKAMAILIO_MOD_INTERFACE

include ../../Makefile.modules
Empty file added src/modules/pua_json/README
Empty file.
134 changes: 134 additions & 0 deletions src/modules/pua_json/defs.h
@@ -0,0 +1,134 @@
/*
* PUA_JSON module
*
* Copyright (C) 2010-2014 2600Hz
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contributor(s):
* 2600Hz Team
* Emmanuel Schmidbauer <eschmidbauer@gmail.com>
*
*/

#ifndef _PUA_JSON_DEFS_H_
#define _PUA_JSON_DEFS_H_

#define BLF_MAX_DIALOGS 8
#define BLF_JSON_PRES "Presentity"
#define BLF_JSON_PRES_USER "Presentity-User"
#define BLF_JSON_PRES_REALM "Presentity-Realm"
#define BLF_JSON_FROM "From"
#define BLF_JSON_FROM_USER "From-User"
#define BLF_JSON_FROM_REALM "From-Realm"
#define BLF_JSON_FROM_URI "From-URI"
#define BLF_JSON_TO "To"
#define BLF_JSON_TO_USER "To-User"
#define BLF_JSON_TO_REALM "To-Realm"
#define BLF_JSON_TO_URI "To-URI"
#define BLF_JSON_CALLID "Call-ID"
#define BLF_JSON_TOTAG "To-Tag"
#define BLF_JSON_FROMTAG "From-Tag"
#define BLF_JSON_STATE "State"
#define BLF_JSON_USER "User"
#define BLF_JSON_QUEUE "Queue"
#define BLF_JSON_EXPIRES "Expires"
#define BLF_JSON_APP_NAME "App-Name"
#define BLF_JSON_APP_VERSION "App-Version"
#define BLF_JSON_NODE "Node"
#define BLF_JSON_SERVERID "Server-ID"
#define BLF_JSON_EVENT_CATEGORY "Event-Category"
#define BLF_JSON_EVENT_NAME "Event-Name"
#define BLF_JSON_TYPE "Type"
#define BLF_JSON_MSG_ID "Msg-ID"
#define BLF_JSON_DIRECTION "Direction"

#define BLF_JSON_CONTACT "Contact"
#define BLF_JSON_EVENT_PKG "Event-Package"
#define MWI_JSON_WAITING "Messages-Waiting"
#define MWI_JSON_VOICE_MESSAGE "MWI-Voice-Message"
#define MWI_JSON_NEW "Messages-New"
#define MWI_JSON_SAVED "Messages-Saved"
#define MWI_JSON_URGENT "Messages-Urgent"
#define MWI_JSON_URGENT_SAVED "Messages-Urgent-Saved"
#define MWI_JSON_ACCOUNT "Message-Account"
#define MWI_JSON_FROM "From"
#define MWI_JSON_TO "To"

#define TO_TAG_BUFFER_SIZE 128
#define FROM_TAG_BUFFER_SIZE 128
#define SENDER_BUFFER_SIZE 1024
#define DIALOGINFO_BODY_BUFFER_SIZE 8192
#define MWI_BODY_BUFFER_SIZE 2048
#define PRESENCE_BODY_BUFFER_SIZE 4096

#define MWI_BODY_VOICE_MESSAGE "Messages-Waiting: %.*s\r\nMessage-Account: %.*s\r\nVoice-Message: %.*s\r\n"
#define MWI_BODY_NO_VOICE_MESSAGE "Messages-Waiting: %.*s\r\nMessage-Account: %.*s\r\n"
#define MWI_BODY "Messages-Waiting: %.*s\r\nMessage-Account: %.*s\r\nVoice-Message: %.*s/%.*s (%.*s/%.*s)\r\n"
#define PRESENCE_BODY "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<presence xmlns=\"urn:ietf:params:xml:ns:pidf\" xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" xmlns:c=\"urn:ietf:params:xml:ns:pidf:cipid\" entity=\"%s\"> \
<tuple xmlns=\"urn:ietf:params:xml:ns:pidf\" id=\"%s\">\
<status>\
<basic>%s</basic>\
</status>\
</tuple>\
<note xmlns=\"urn:ietf:params:xml:ns:pidf\">%s</note>\
<dm:person xmlns:dm=\"urn:ietf:params:xml:ns:pidf:data-model\" xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" id=\"1\">\
<rpid:activities>%s</rpid:activities>\
<dm:note>%s</dm:note>\
</dm:person>\
</presence>"

#define DIALOGINFO_EMPTY_BODY "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"1\" state=\"full\" entity=\"%.*s\"> \
<dialog call-id=\"76001e23e09704ea9e1257ebea85e1f3\" direction=\"initiator\">\
<state>terminated</state>\
</dialog>\
</dialog-info>"

#define LOCAL_TAG "local-tag=\"%.*s\""
#define REMOTE_TAG "remote-tag=\"%.*s\""

#define DIALOGINFO_BODY "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"1\" state=\"full\" entity=\"%.*s\">\
<dialog id=\"%.*s\" call-id=\"%.*s\" %.*s %.*s direction=\"%.*s\">\
<state>%.*s</state>\
<local>\
<identity display=\"%.*s\">%.*s</identity>\
<target uri=\"%.*s\"/>\
</local>\
<remote>\
<identity display=\"%.*s\">%.*s</identity>\
<target uri=\"%.*s\"/>\
</remote>\
</dialog>\
</dialog-info>"

#define DIALOGINFO_BODY_2 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<dialog-info xmlns=\"urn:ietf:params:xml:ns:dialog-info\" version=\"1\" state=\"full\" entity=\"%.*s\">\
<dialog id=\"%.*s\" call-id=\"%.*s\" %.*s %.*s direction=\"%.*s\">\
<state>%.*s</state>\
<local>\
<identity display=\"%.*s\">%.*s</identity>\
</local>\
<remote>\
<identity display=\"%.*s\">%.*s</identity>\
</remote>\
</dialog>\
</dialog-info>"

#endif /* _PUA_JSON_DEFS_H_ */
4 changes: 4 additions & 0 deletions src/modules/pua_json/doc/Makefile
@@ -0,0 +1,4 @@
docs = json_pua.xml

docbook_dir = ../../../../doc/docbook
include $(docbook_dir)/Makefile.module
37 changes: 37 additions & 0 deletions src/modules/pua_json/doc/pua_json.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [

<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../../doc/docbook/entities.xml">
%docentities;

]>

<book xmlns:xi="http://www.w3.org/2001/XInclude">
<bookinfo>
<title>PUA_JSON Module</title>
<productname class="trade">&kamailioname;</productname>
<authorgroup>
<author>
<firstname>Emmanuel Schmidbauer</firstname>
<email>eschmidbauer@gmail.com</email>
</author>
<editor>
<firstname>Emmanuel</firstname>
<surname>Schmidbauer</surname>
<email>eschmidbauer@gmail.com</email>
</editor>
</authorgroup>
<copyright>
<year>2018</year>
<holder>VoIPxSWITCH</holder>
</copyright>
</bookinfo>
<toc></toc>

<xi:include href="pua_json_admin.xml"/>
<!-- <xi:include href="db_text_devel.xml"/> -->


</book>

0 comments on commit 02cb90c

Please sign in to comment.