From 03309d313c4522b217646422f12831454302e185 Mon Sep 17 00:00:00 2001 From: lazedo Date: Wed, 4 Feb 2015 02:57:06 +0000 Subject: [PATCH] KAZOO-3265 MESSAGE-ROLE --- kamailio/default.cfg | 47 ++++++++++++++++++++++++++----- kamailio/local.cfg | 1 + kamailio/message-role.cfg | 58 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 kamailio/message-role.cfg diff --git a/kamailio/default.cfg b/kamailio/default.cfg index 04cb12e..9a9bd90 100644 --- a/kamailio/default.cfg +++ b/kamailio/default.cfg @@ -174,6 +174,9 @@ include_file "registrar-role.cfg" #!ifdef PRESENCE-ROLE include_file "presence-role.cfg" #!endif +#!ifdef MESSAGE-ROLE +include_file "message-role.cfg" +#!endif #!ifdef NAT-TRAVERSAL-ROLE include_file "nat-traversal-role.cfg" #!endif @@ -322,14 +325,44 @@ route[HANDLE_NOTIFY] route[HANDLE_MESSAGE] { - if (is_method("MESSAGE")) { - if (isflagset(FLAG_INTERNALLY_SOURCED)) { - route(INTERNAL_TO_EXTERNAL_RELAY); - } else { - sl_send_reply("202", "delivered to /dev/null"); - exit; + #!ifdef MESSAGE-ROLE + + if (is_method("MESSAGE")) { + xlog("L_INFO", "$ci|MESSAGE from $fu to $tu"); + if (isflagset(FLAG_INTERNALLY_SOURCED) || src_ip == myself) { + xlog("L_INFO", "$ci| routing MESSAGE to external from $fu to $tu"); + route(INTERNAL_TO_EXTERNAL_RELAY); + #!ifdef TRAFFIC-FILTER-ROLE + } else if (!isflagset(FLAG_TRUSTED_SOURCE) + && $rd =~ "[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}\.[0-9]{1,3}") { + xlog("L_WARN", "$ci|end|dropping MESSAGE request with IP domain"); + #!endif + } else { + xlog("L_WARN", "$ci|end|MESSAGE $(hdr(Content-Type))"); + if( $hdr(Content-Type) == "application/im-iscomposing+xml" ) { + xlog("L_WARN", "$ci|end|dropping MESSAGE application/im-iscomposing+xml"); + sl_send_reply("200", "OK"); + } else { + route(DISPATCHER_FIND_ROUTES); + route(EXTERNAL_TO_INTERNAL_RELAY); + } + } + exit(); } - } + + #!else + + if (is_method("MESSAGE")) { + if (isflagset(FLAG_INTERNALLY_SOURCED)) { + route(INTERNAL_TO_EXTERNAL_RELAY); + } else { + sl_send_reply("202", "delivered to /dev/null"); + exit; + } + } + + #!endif + } route[HANDLE_IN_DIALOG_REQUESTS] diff --git a/kamailio/local.cfg b/kamailio/local.cfg index 69b1000..f06e814 100644 --- a/kamailio/local.cfg +++ b/kamailio/local.cfg @@ -11,6 +11,7 @@ debug = L_INFO #!trydef NAT-TRAVERSAL-ROLE #!trydef REGISTRAR-ROLE #!trydef PRESENCE-ROLE +###!trydef MESSAGE-ROLE ## Disabled Roles - remove all but the last '#' to enable # # #!trydef TRAFFIC-FILTER-ROLE diff --git a/kamailio/message-role.cfg b/kamailio/message-role.cfg new file mode 100644 index 0000000..a8d97ee --- /dev/null +++ b/kamailio/message-role.cfg @@ -0,0 +1,58 @@ +modparam("htable", "htable", "msg=>size=32;autoexpire=60;") + + +event_route[kazoo:consumer-event-message-route] +{ + $var(uri_username) = ""; + kazoo_json($kzE, "Endpoints[0].To-Username", "$var(uri_username)"); + $var(x) = $(kzE{kz.json,Endpoints[0].To-Username}); + xlog("L_INFO", "received message route for $(kzE{kz.json,Endpoints[0].To-DID})"); + if( $var(uri_username) != "" ) { + $var(from_uri) = "sip:" + $(kzE{kz.json,Caller-ID-Number}) + "@" + $(kzE{kz.json,Endpoints[0].To-Realm}); + $var(to_uri) = "sip:" + $(kzE{kz.json,Endpoints[0].To-Username}) + "@" + $(kzE{kz.json,Endpoints[0].To-Realm}); + } else { + $var(from_uri) = "sip:" + $(kzE{kz.json,Caller-ID-Number}) + $(kzE{kz.json,Endpoints[0].To-Realm}); + $var(to_uri) = $(kzE{kz.json,Endpoints[0].Route}); + } + $sht(msg=>$(kzE{kz.json,Call-ID})) = $kzE; + $uac_req(method)="MESSAGE"; + $uac_req(body)= $(kzE{kz.json,Body}); + $uac_req(hdrs)="Content-Type: text/plain\r\n"; + $uac_req(turi) = $var(to_uri); + $uac_req(ruri) = $var(to_uri); + $uac_req(furi) = $var(from_uri); + $uac_req(ouri) = "sip:MY_IP_ADDRESS:5060"; + $uac_req(callid) = $(kzE{kz.json,Call-ID}); + xlog("L_INFO", "sending message from $var(from_uri) to $var(to_uri) "); + uac_req_send(); + +} + +route[MESSAGE_REPLY] +{ + if( $(sht(msg=>$ci)) == $null) { + exit(); + } + + if($T_reply_code != 200 && $T_reply_code != 202) { + $var(Result) = "Failure"; + } else { + $var(Result) = "Success"; + } + + + + $var(Payload) = '{ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "' + $(sht(msg=>$ci){kz.json,Call-ID}) + '", "Message-ID" : "' + $(sht(msg=>$ci){kz.json,Message-ID}) +'" , "Delivery-Result-Code" : "sip:" + $T_reply_code + '" , "Msg-ID" : "' + $(sht(msg=>$ci){kz.json,Msg-ID}) + '" , "Status" : "' + $var(Result) + '"}'; + + $var(RoutingKey) = $(sht(msg=>$ci){kz.json,Server-ID}); + $var(exchange) = "targeted"; + if($var(RoutingKey) == "") { + $var(exchange) = "sms"; + $var(RoutingKey) = "message.delivery." + $(sht(msg=>$ci){kz.json,Call-ID}{kz.encode}); + } + xlog("L_INFO", "sending delivery message for $ci"); + kazoo_publish($var(exchange), $var(RoutingKey), $var(Payload)); +} + + +# vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab