From 6a23a76f8d7ad6dc0c32660eb2cb3aea895b84a5 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 18 Nov 2016 17:42:55 +0100 Subject: [PATCH] TT#2370 add internal interface for incoming sms Change-Id: I8e994e9bde930e2756b7492afcb7760181fac858 --- lib/NGCP/Panel/Controller/InternalSms.pm | 80 ++++++++++++++++++++++++ lib/NGCP/Panel/Controller/Root.pm | 1 + ngcp_panel.conf | 1 + 3 files changed, 82 insertions(+) create mode 100644 lib/NGCP/Panel/Controller/InternalSms.pm diff --git a/lib/NGCP/Panel/Controller/InternalSms.pm b/lib/NGCP/Panel/Controller/InternalSms.pm new file mode 100644 index 0000000000..2c93d700df --- /dev/null +++ b/lib/NGCP/Panel/Controller/InternalSms.pm @@ -0,0 +1,80 @@ +package NGCP::Panel::Controller::InternalSms; +use NGCP::Panel::Utils::Generic qw(:all); +use Sipwise::Base; + + +use parent 'Catalyst::Controller'; + +#sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) { +sub auto { + my ($self, $c) = @_; + $c->log->debug(__PACKAGE__ . '::auto'); + #NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c); + return 1; +} + +sub list :Chained('/') :PathPart('internalsms') :CaptureArgs(0) { + # my ($self, $c) = @_; + return; +} + +sub receive :Chained('list') :PathPart('receive') :Args(0) { + my ($self, $c) = @_; + + my $from = $c->req->params->{from} // ""; + my $to = $c->req->params->{to} // ""; + my $text = $c->req->params->{text} // ""; + my $token = $c->req->params->{auth_token} // ""; + + unless ($from && $to && $text && $token) { + $c->log->error("Missing one param of: from ($from), to ($to), text ($text), auth_token ($token)."); + $c->detach('/denied_page'); + } + + unless ($c->config->{sms}{api_token} && $c->config->{sms}{api_token} eq $token) { + $c->log->error("Token mismatch (sent: $token)."); + $c->detach('/denied_page'); + } + + $to =~ s/^\+//; + $from =~ s/^\+//; + + try { + my $schema = $c->model('DB'); + $schema->txn_do(sub { + my $prov_dbalias = $c->model('DB')->resultset('voip_dbaliases') + ->search_rs({ + 'me.username' => $to, + },{ + join => { subscriber => 'voip_subscriber' } + })->first; + + unless ($prov_dbalias) { + $c->log->warn("No corresponding subscriber for incoming number ($to) found."); + $c->log->debug("from: $from, to: $to, text: $text"); + die "no_subscriber_found"; + } + + my $created_item = $c->model('DB')->resultset('sms_journal')->create({ + subscriber_id => $prov_dbalias->subscriber_id, + direction => "in", + caller => $from, + callee => $to, + text => $text, + }); + }); + } catch($e) { + $c->log->error("Failed to store received SMS message."); + $c->log->debug($e); + } + + $c->response->code(200); + $c->response->body(""); + return; +} + +__PACKAGE__->meta->make_immutable; + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index 7af178a772..2dddab326a 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -57,6 +57,7 @@ sub auto :Private { or $c->req->uri->path =~ m|^/pbx/directory/.+| or $c->req->uri->path =~ m|^/recoverwebpassword/?$| or $c->req->uri->path =~ m|^/resetwebpassword/?$| + or $c->req->uri->path =~ m|^/internalsms/receive/?$| ) { $c->log->debug("*** Root::auto skip authn, grant access to " . $c->request->path); return 1; diff --git a/ngcp_panel.conf b/ngcp_panel.conf index eabb4dfb36..a46bbdc111 100644 --- a/ngcp_panel.conf +++ b/ngcp_panel.conf @@ -394,4 +394,5 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{ path /cgi-bin/sendsms user unset pass unset + api_token abc123