Commit 1c4fd526 authored by Gabriel Margiani's avatar Gabriel Margiani

add allways to phonebook if needed.

parent dab806af
......@@ -65,8 +65,8 @@ void p3::call::onCallState(pj::OnCallStateParam& prm) {
phone.start_ringing();
ringing = true;
}
state = p3::callState::INCOMMING;
phone.get_event_hook().run_hook("incomming", p3id, get_nr());
state = p3::callState::INCOMING;
phone.get_event_hook().run_hook("incoming", p3id, get_nr());
}
break;
case PJSIP_INV_STATE_NULL:
......
......@@ -31,7 +31,7 @@ namespace p3 {
enum class callState: char {
NONE = 'X',
CALLING = 'c',
INCOMMING = '*',
INCOMING = '*',
RUNNING = ' ',
HOLDING = '&'
};
......
......@@ -22,11 +22,17 @@
#include "eventhook.h"
p3::eventHook::eventHook(std::string c, phonebook& p) : command(c), book(p) {
p3::eventHook::eventHook(config& c, phonebook& p) : conf(c), book(p), command(conf.get_string("hook:command")) {
signal(SIGCHLD, SIG_IGN);
}
void p3::eventHook::run_hook(std::string event, int cid, std::string nr) {
// TODO: Think of a better place for this
if (conf.get_bool("phonebook:auto_add_unanswered")) {
book.add(nr);
}
int pid = fork();
if (pid == 0) {
execl(
......
......@@ -22,6 +22,7 @@
#include <string>
#include "phonebook.h"
#include "config.h"
namespace p3 {
......@@ -38,13 +39,14 @@ namespace p3 {
class eventHook {
std::string command;
config& conf;
phonebook& book;
std::string command;
public:
explicit eventHook(std::string c, phonebook& p);
explicit eventHook(config& c, phonebook& p);
void run_hook(std::string event, int call_id, std::string nr);
};
......
......@@ -70,7 +70,7 @@ void p3::phonebook::add(const std::string& nr) {
if (nr.empty() || index.count(nr)) return;
std::regex rnm("\"(.*)\"");
std::regex rnr("sip:(.*)@(.*)>?");
std::regex rnr("<sip:(.*)@(.*)>");
std::smatch match;
std::string name("");
......@@ -87,6 +87,8 @@ void p3::phonebook::add(const std::string& nr) {
}
}
if (index.count(num)) return; // number already there
data[id_count] = {num, name, ""};
index[num] = id_count;
if (!name.empty()) index[name] = id_count;
......@@ -98,7 +100,7 @@ void p3::phonebook::add(const std::string& nr) {
);
if (f) {
f << name << "\t< " << num << "\t<" << std::endl;
f << name << "\t> " << num << "\t>" << std::endl;
}
}
......@@ -133,7 +135,7 @@ std::string p3::phonebook::uri_by_nr(const std::string& nr) {
int id = index.at(nr);
return "\"" + data[id][1] +
"\" <sip:" + nr + "@" +
conf->get_string("phnebook:default_uri") +
conf->get_string("phonebook:default_uri") +
">";
} catch (std::out_of_range& e) {
return "<sip:" + nr + "@" +
......
......@@ -36,6 +36,7 @@ std::map<std::string, std::string> p3::server::default_config {
{"phonebook:default_uri", "3phone.com"},
{"phonebook:filename", "~/.3phone/phonebook"},
{"phonebook:auto_add", "false"},
{"phonebook:auto_add_unanswered", "false"},
{"hook:command", "~/.3phone/hook"},
{"phone:ringtone", "/usr/share/3phone/rt.wav"},
{"phone:holdmusic", "none"},
......@@ -46,7 +47,7 @@ p3::server::server() :
conf(std::string(std::getenv("HOME")) + "/.3phonerc", p3::server::default_config),
connection(p3::mQueue::DefaultId, true),
book(),
hook(conf.get_string("hook:command"), book),
hook(conf, book),
phone(conf, hook)
{
p3::phonebook::set_config(conf);
......@@ -191,6 +192,9 @@ void p3::clientHandler::handle_call() {
p3::mQueueMessage n = connection.receive(listenTimeout);
std::string nr = book.make_uri(n.get_value());
phone.call(nr);
if (conf.get_bool("phonebook:auto_add")) {
book.add(nr);
}
connection.send(p3::protocol::OK, "Call OK");
}
......@@ -205,7 +209,7 @@ void p3::clientHandler::handle_hangup_all() {
}
void p3::clientHandler::handle_answer() {
int id = get_call_id("Call to answer (id)", p3::callState::INCOMMING);
int id = get_call_id("Call to answer (id)", p3::callState::INCOMING);
phone.answer(id);
if (conf.get_bool("phonebook:auto_add")) {
book.add(phone.get_nr_by_id(id));
......
......@@ -219,7 +219,7 @@ void p3::sipphone::answer(int id) {
pj::CallOpParam o;
o.statusCode = PJSIP_SC_OK;
try {
get_call_by_id(id, p3::callState::INCOMMING)->answer(o);
get_call_by_id(id, p3::callState::INCOMING)->answer(o);
} catch (pj::Error &e) {
throw p3::perror("sipphone:pjanswer", e.info());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment