From f3e2db23e03914ec4b73e4d60d76ed3080441d4d Mon Sep 17 00:00:00 2001 From: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com> Date: Tue, 24 Mar 2009 16:27:00 -0400 Subject: [PATCH] Add functional url hook --- src/hooks/urlhook.cpp | 39 ++++++++++++++++++++++++++++++++++----- src/hooks/urlhook.h | 8 +++++++- src/sipvoiplink.cpp | 6 ++++-- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/hooks/urlhook.cpp b/src/hooks/urlhook.cpp index 5c8e8c41ad..e1d78c40c4 100644 --- a/src/hooks/urlhook.cpp +++ b/src/hooks/urlhook.cpp @@ -18,21 +18,50 @@ */ #include "urlhook.h" - #include <iostream> UrlHook::UrlHook () { } UrlHook::~UrlHook () { } -void UrlHook::addAction (pjsip_msg *msg, std::string field, std::string command){ +bool UrlHook::addAction (pjsip_msg *msg, std::string field, std::string command){ - std::string command_bg; + std::string command_bg, value, url; + pjsip_generic_string_hdr * hdr; + size_t pos; std::cout << "SIP field: " << field << " - command: " << command << std::endl;; - command_bg = command + "&"; + /* Get the URL in the SIP header */ + if ( (hdr = (pjsip_generic_string_hdr*)this->url_hook_fetch_header_value (msg, field)) != NULL) + { + value = hdr->hvalue.ptr; + if ( (pos=value.find ("\n")) != std::string::npos) { + url = value.substr (0, pos); + + /* Execute the command in the background to not block the application */ + command_bg = command + " " + url + "&" ; + /* Execute a system call */ + RUN_COMMAND (command_bg.c_str()); + + return true; + } + else + return false; + } + + return false; +} + +void* UrlHook::url_hook_fetch_header_value (pjsip_msg *msg, std::string field) { + + pj_str_t name; + + std::cout << "url hook fetch header value" << std::endl; - system (command_bg.c_str()); + /* Convert the field name into pjsip type */ + name = pj_str ((char*)field.c_str()); + /* Get the header value and convert into string*/ + return pjsip_msg_find_hdr_by_name (msg, &name, NULL); } diff --git a/src/hooks/urlhook.h b/src/hooks/urlhook.h index 74cd61045f..238f983345 100644 --- a/src/hooks/urlhook.h +++ b/src/hooks/urlhook.h @@ -24,6 +24,8 @@ #include <pjsip.h> +#define RUN_COMMAND(command) system(command); + class UrlHook { public: @@ -37,7 +39,11 @@ class UrlHook { */ ~UrlHook (); - void addAction (pjsip_msg *msg, std::string field, std::string command); + bool addAction (pjsip_msg *msg, std::string field, std::string command); + + private: + + void* url_hook_fetch_header_value (pjsip_msg *msg, std::string field); }; #endif // URL_HOOK_H diff --git a/src/sipvoiplink.cpp b/src/sipvoiplink.cpp index 21a97068a9..4d3944636a 100644 --- a/src/sipvoiplink.cpp +++ b/src/sipvoiplink.cpp @@ -1951,9 +1951,11 @@ void call_on_tsx_changed(pjsip_inv_session *inv, pjsip_transaction *tsx, pjsip_e } // URL HOOK // - urlhook->addAction (rdata->msg_info.msg, + if (!urlhook->addAction (rdata->msg_info.msg, Manager::instance().getConfigString (HOOKS, URLHOOK_SIP_FIELD), - Manager::instance().getConfigString (HOOKS, URLHOOK_COMMAND)); + Manager::instance().getConfigString (HOOKS, URLHOOK_COMMAND)) ) { + _debug ("URL hook failed\n"); + } // Generate a new call ID for the incoming call! id = Manager::instance().getNewCallID(); -- GitLab