Browse Source

add presence query & notify & sync

3.22
lazedo 10 years ago
parent
commit
5c15f139fa
7 changed files with 195 additions and 0 deletions
  1. +33
    -0
      kamailio/default.cfg
  2. +15
    -0
      kamailio/defs.cfg
  3. +3
    -0
      kamailio/kamailio.cfg
  4. +4
    -0
      kamailio/local.cfg
  5. +32
    -0
      kamailio/presence_notify_sync-role.cfg
  6. +65
    -0
      kamailio/presence_query-role.cfg
  7. +43
    -0
      kamailio/presence_sync-role.cfg

+ 33
- 0
kamailio/default.cfg View File

@ -86,6 +86,9 @@ mpath="/usr/lib64/kamailio/modules/"
######## Kamailio control connector module ########
loadmodule "ctl.so"
######## set params at runtime
loadmodule "cfg_rpc.so"
######## Kamailio core extensions module ########
loadmodule "kex.so"
@ -219,6 +222,15 @@ include_file "registrar-sync-role.cfg"
#!ifdef FAST_PICKUP-ROLE
include_file "fast-pickup-role.cfg"
#!endif
#!ifdef PRESENCE_SYNC-ROLE
include_file "presence_sync-role.cfg"
#!endif
#!ifdef PRESENCE_QUERY-ROLE
include_file "presence_query-role.cfg"
#!endif
#!ifdef PRESENCE_NOTIFY_SYNC-ROLE
include_file "presence_notify_sync-role.cfg"
#!endif
####### Permissions module ##########
loadmodule "permissions.so"
@ -791,6 +803,20 @@ event_route[kazoo:mod-init]
#!endif
#!ifdef PRESENCE_SYNC-ROLE
$var(payload) = "{ 'exchange' : 'presence' , 'type' : 'topic', 'queue' : 'PRESENCE-QUEUE-MY_HOSTNAME', 'routing' : 'sync', 'auto_delete' : 1, 'durable' : 0, 'no_ack' : 1, 'wait_for_consumer_ack' : 0 }";
kazoo_subscribe("$var(payload)");
#!endif
#!ifdef PRESENCE_QUERY-ROLE
$var(payload) = "{ 'exchange' : 'omnipresence' , 'type' : 'topic', 'routing' : 'presence.search_req.*', 'federate' : 1 }";
kazoo_subscribe("$var(payload)");
#!endif
}
event_route[kazoo:consumer-event]
@ -808,5 +834,12 @@ event_route[kazoo:consumer-event-connection-closed]
xlog("L_INFO","connection to $(kzE{kz.json,host}) closed");
}
#!ifdef PRESENCE_NOTIFY_SYNC-ROLE
event_route[tm:local-request]
{
route(PRESENCE_LOCAL_NOTIFY);
}
#!endif
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab

+ 15
- 0
kamailio/defs.cfg View File

@ -0,0 +1,15 @@
## NOTE: DO NOT CHANGE THIS FILE, EDIT local.cfg ##
####### defs ########
#!ifndef KAZOO_DB_URL
#!substdef "!KAZOO_DB_URL!text:///etc/kazoo/kamailio/dbtext!g"
#!endif
#!ifndef MAX_WHILE_LOOPS
#!substdef "!MAX_WHILE_LOOPS!500!g"
#!endif
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab

+ 3
- 0
kamailio/kamailio.cfg View File

@ -26,6 +26,9 @@
####### Local Configuration ########
include_file "local.cfg"
####### defaults not configured in local ########
include_file "defs.cfg"
####### Default Configuration ######
include_file "default.cfg"


+ 4
- 0
kamailio/local.cfg View File

@ -26,6 +26,10 @@ debug = L_INFO
# # #!trydef PUSHER-ROLE
# # #!trydef REGISTRAR_SYNC-ROLE
# # #!trydef PRESENCE_QUERY-ROLE
# # #!trydef PRESENCE_SYNC-ROLE
# # #!trydef PRESENCE_NOTIFY_SYNC-ROLE
################################################################################
## SERVER INFORMATION
################################################################################


+ 32
- 0
kamailio/presence_notify_sync-role.cfg View File

@ -0,0 +1,32 @@
######## Presence notify sync ########
## send notify sync back to kazoo
kazoo.presence_notify = 0 descr "enable/disable sending notify callback to omnipresence"
route[PRESENCE_LOCAL_NOTIFY]
{
if($rm != "NOTIFY" || @cfg_get.kazoo.presence_notify != 1) {
return;
}
$avp(notify_body) = $rb;
$avp(notify_event) = $hdr(Event);
t_on_reply("PRESENCE_NOTIFY_REPLY");
t_on_failure("PRESENCE_NOTIFY_FAULT");
}
onreply_route[PRESENCE_NOTIFY_REPLY]
{
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "notify", "Event-Package" : "$avp(notify_event)", "Call-ID" : "$ci", "From" : "$fu", "To" : "$tu", "Body" : "$(avp(notify_body){s.escape.common})", "Sequence" : $cs, "Reply" : $T_reply_code }';
$var(rk) = "notify." + $(td{kz.encode}) + "." + $(tU{kz.encode});
kazoo_publish("omnipresence", "$var(rk)", $var(amqp_payload_request));
xlog("L_INFO", "$ci|log|sent notify callback for event $avp(notify_event) : $tu");
}
failure_route[PRESENCE_NOTIFY_FAULT]
{
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "notify", "Event-Package" : "$avp(notify_event)", "Call-ID" : "$ci", "From" : "$fu", "To" : "$tu", "Body" : "$(avp(notify_body){s.escape.common})", "Sequence" : $cs, "Reply" : $T_reply_code }';
$var(rk) = "notify." + $(td{kz.encode}) + "." + $(tU{kz.encode});
kazoo_publish("omnipresence", "$var(rk)", $var(amqp_payload_request));
xlog("L_INFO", "$ci|log|sent notify callback error $T_reply_code for event $avp(notify_event) : $tu");
}

+ 65
- 0
kamailio/presence_query-role.cfg View File

@ -0,0 +1,65 @@
######## Presence query server module ########
####### SQL OPS module ##########
#!ifndef SQLOPS-LOADED
loadmodule "sqlops.so"
modparam("sqlops","sqlcon", "cb=>KAZOO_DB_URL")
#!trydef SQLOPS-LOADED
#!endif
event_route[kazoo:consumer-event-presence-search-req]
{
xlog("L_INFO", "received presence search_req $kzE");
$var(Queue) = $(kzE{kz.json,Server-ID});
$var(Event) = $(kzE{kz.json,Event-Package});
$var(Domain) = $(kzE{kz.json,Realm});
$var(Username) = $(kzE{kz.json,Username});
$var(Now) = $TS;
$var(Items) = "";
$var(Query) = $_s(select * from presentity where domain = "$var(Domain)");
if($var(Event) != "") {
$var(Query) = $var(Query) + $_s( and event = "$var(Event)");
}
if($var(Username) != "") {
$var(Query) = $var(Query) + $_s( and username = "$var(Username)");
}
$var(Query) = $var(Query) + " order by username, event, id";
xlog("L_INFO", "$ci| QUERY $var(Query)");
$var(rows) = 0;
if (sql_xquery("cb", "$var(Query)", "ra") == 1)
{
while($xavp(ra) != $null) {
$var(Username) = $xavp(ra=>username);
$var(Sep2)="";
$var(Evt)="";
while($xavp(ra) != $null && $var(Username) == $xavp(ra=>username))
{
$var(Event) = $xavp(ra=>event);
$var(Sep3)="";
$var(Sub)="";
while($xavp(ra) != $null && $var(Username) == $xavp(ra=>username) && $var(Event) == $xavp(ra=>event))
{
$var(Sub) = $var(Sub) + $_s($var(Sep3)"$xavp(ra=>id)" : {"etag" : "$xavp(ra=>etag)", "body" : "$(xavp(ra=>body){s.escape.common})" });
$var(Sep3)=", ";
pv_unset("$xavp(ra)");
}
$var(Evt) = $var(Evt) + $var(Sep2) + $_s("$var(Event)") + " : { " + $var(Sub) + "}";
$var(Sep2)=", ";
}
$var(Usr) = $_s("$var(Username)") + " : { " + $var(Evt) + " }";
## xlog("L_INFO", "$ci| RESULT \"Subscriptions\" : { $var(Usr) }");
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "search_partial_resp", "Msg-ID" : "$(kzE{kz.json,Msg-ID})", "Subscriptions" : { $var(Usr) } }';
kazoo_publish("targeted", "$var(Queue)", $var(amqp_payload_request));
$var(rows) = $var(rows) + 1;
}
}
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "search_resp", "Msg-ID" : "$(kzE{kz.json,Msg-ID})" }';
kazoo_publish("targeted", "$var(Queue)", $var(amqp_payload_request));
xlog("L_INFO", "$ci| sent $var(rows) search_partial_resp in result of search_req");
}
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab

+ 43
- 0
kamailio/presence_sync-role.cfg View File

@ -0,0 +1,43 @@
######## Presence sync server module ########
####### SQL OPS module ##########
#!ifndef SQLOPS-LOADED
loadmodule "sqlops.so"
modparam("sqlops","sqlcon", "cb=>KAZOO_DB_URL")
#!trydef SQLOPS-LOADED
#!endif
event_route[kazoo:consumer-event-presence-sync]
{
xlog("L_INFO", "received SYNC $kzE");
$var(Queue) = $(kzE{kz.json,Server-ID});
$var(Now) = $TS;
######## SEND BACK START ########
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "sync", "Action" : "Start" }';
kazoo_publish("targeted", "$var(Queue)", $var(amqp_payload_request));
if (sql_xquery("cb", "select * from active_watchers where expires > $var(Now)", "ra") == 1)
{
while($xavp(ra) != $null)
{
$var(runloop) = 1;
while($xavp(ra) != $null && $var(runloop) < MAX_WHILE_LOOPS ) {
xlog("L_INFO", "[id, presentity, watcher] = [$xavp(ra=>id), $xavp(ra=>presentity_uri), $xavp(ra=>watcher_username)]");
$var(Expires) = $xavp(ra=>expires) - $var(Now);
xlog("L_INFO", "EXPIRES $var(Expires) , $xavp(ra=>expires) , $var(Now)");
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "subscription", "Event-Package" : "$xavp(ra=>event)", "Expires" : $var(Expires), "Server-ID" : "BLF-MY_HOSTNAME" , "Contact" : "$xavp(ra=>contact)", "Call-ID" : "$xavp(ra=>callid)", "From" : "sip:$xavp(ra=>watcher_username)@$xavp(ra=>watcher_domain)", "User" : "$xavp(ra=>presentity_uri)" }"';
kazoo_publish("targeted", "$var(Queue)", $var(amqp_payload_request));
pv_unset("$xavp(ra)");
$var(runloop) = $var(runloop) + 1;
}
}
}
######## SEND BACK END ########
$var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "sync", "Action" : "End" }';
kazoo_publish("targeted", "$var(Queue)", $var(amqp_payload_request));
}
# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab

Loading…
Cancel
Save