Commit 4c9833ad authored by Gabriel Margiani's avatar Gabriel Margiani

reverse number lookup implemented.

parent ae3d891d
......@@ -21,15 +21,28 @@
#include <sstream>
#include <regex>
#include <boost/algorithm/string.hpp>
#include <cstdio>
#include "phonebook.h"
p3::config* p3::phonebook::conf = nullptr;
p3::phonebook::phonebook() : id_count(0) {
p3::phonebook::phonebook() : id_count(0), add_stop(false) {
add_thread = new std::thread(&p3::phonebook::add_worker, this);
}
p3::phonebook::~phonebook() {
add_mutex.lock();
add_stop = true;
add_mutex.unlock();
add_condition.notify_all();
if (add_thread->joinable()) {
add_thread->join();
}
}
void p3::phonebook::load() {
std::lock_guard<std::recursive_mutex> g(data_mutex);
data.clear();
index.clear();
id_count = 0;
......@@ -66,7 +79,88 @@ void p3::phonebook::set_config(p3::config& c) {
conf = &c;
}
void p3::phonebook::add_worker() {
while (true) {
{
std::unique_lock<std::mutex> g(add_mutex);
if (add_numbers.size()) {
std::string nr = add_numbers.front();
add_numbers.pop();
g.unlock();
add_number(nr);
}
}
std::unique_lock<std::mutex> g(add_mutex);
if (add_stop) return;
add_condition.wait(g);
if (add_stop) return;
}
}
void p3::phonebook::add_number(const std::string& nr) {
std::unique_lock<std::recursive_mutex> g(data_mutex);
if (nr.empty() || index.count(nr)) return;
g.unlock();
std::string com = conf->get_string("phonebook:reverse_lookup_command");
if (com.empty() || com == "none") return;
com += " " + nr;
std::cout << "Looking up name for '" << nr << "' using '"
<< com << "'" << std::endl;
FILE* pipe = popen(com.c_str(), "r");
if (!pipe) {
std::cout << "Failed to run '" << com
<< "' to reverse lookup '" << nr << "'" << std::endl;
return;
}
std::string name = "";
try {
char buffer[128];
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
name += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
name = std::regex_replace(name,std::regex("\\s+"), " ");
boost::trim(name);
std::cout << "Got name for '" << nr << "': '" << name << "'" << std::endl;
g.lock();
data[id_count] = {nr, name, ""};
index[nr] = id_count;
if (!name.empty()) index[name] = id_count;
id_count++;
std::ofstream f(
conf->get_string("phonebook:filename"),
std::ofstream::out | std::ofstream::app
);
if (f) {
f << name << "\t> " << nr << "\t>" << std::endl;
}
}
void p3::phonebook::shedule_add(const std::string& nr) {
{
std::lock_guard<std::mutex> g(add_mutex);
add_numbers.push(nr);
}
add_condition.notify_all();
}
void p3::phonebook::add(const std::string& nr) {
std::lock_guard<std::recursive_mutex> g(data_mutex);
if (nr.empty() || index.count(nr)) return;
std::regex rnm("\"(.*)\"");
......@@ -76,20 +170,31 @@ void p3::phonebook::add(const std::string& nr) {
std::string name("");
std::string num(nr);
bool non_voip = false;
if (std::regex_search(nr, match, rnm) && match.size() > 1) {
name = match.str(1);
boost::trim(name);
}
if (std::regex_search(nr, match, rnr) && match.size() > 2) {
if (match.str(2) == conf->get_string("phonebook:default_uri") ||
match.str(2) == conf->get_string("phonebook:default_uri_alias")) {
num = match.str(1);
non_voip = true;
} else {
num = "<sip:" + match.str(1) + "@" + match.str(2) + ">";
}
normalize(num);
}
if (index.count(num)) return; // number already there
if (non_voip && name.empty() &&
conf->get_string("phonebook:reverse_lookup_command") != "none") {
shedule_add(num);
return;
}
data[id_count] = {num, name, ""};
index[num] = id_count;
if (!name.empty()) index[name] = id_count;
......@@ -111,6 +216,7 @@ void p3::phonebook::normalize(std::string& s) {
}
std::string p3::phonebook::make_uri(std::string nr) {
std::lock_guard<std::recursive_mutex> g(data_mutex);
normalize(nr);
try {
int id = index.at(nr);
......@@ -132,6 +238,7 @@ std::string p3::phonebook::make_uri(std::string nr) {
}
std::string p3::phonebook::uri_by_nr(const std::string& nr) {
std::lock_guard<std::recursive_mutex> g(data_mutex);
try {
int id = index.at(nr);
return "\"" + data[id][1] +
......@@ -146,6 +253,7 @@ std::string p3::phonebook::uri_by_nr(const std::string& nr) {
}
std::string p3::phonebook::address_by_nr(const std::string& nr) {
std::lock_guard<std::recursive_mutex> g(data_mutex);
try {
int id = index.at(nr);
return data[id][1] +
......@@ -157,6 +265,7 @@ std::string p3::phonebook::address_by_nr(const std::string& nr) {
}
std::string p3::phonebook::find_nr(std::string s) {
std::lock_guard<std::recursive_mutex> g(data_mutex);
normalize(s);
try {
int id = index.at(s);
......@@ -168,6 +277,7 @@ std::string p3::phonebook::find_nr(std::string s) {
}
std::string p3::phonebook::slug_by_nr(const std::string& nr) {
std::lock_guard<std::recursive_mutex> g(data_mutex);
try {
int id = index.at(nr);
return data[id][2];
......
......@@ -22,6 +22,11 @@
#include <string>
#include <map>
#include <array>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include "config.h"
......@@ -44,13 +49,29 @@ namespace p3 {
int id_count;
std::map<int, std::array<std::string, 3> > data;
std::map<std::string, int> index;
std::recursive_mutex data_mutex;
static void normalize(std::string& s);
std::thread* add_thread;
std::queue<std::string> add_numbers;
bool add_stop;
std::mutex add_mutex;
std::condition_variable add_condition;
void add_worker();
void add_number(const std::string& nr);
void shedule_add(const std::string& nr);
public:
explicit phonebook();
phonebook(const phonebook&) = delete;
~phonebook();
static void set_config(config& conf);
// Load the phonebook from disc
......
......@@ -38,6 +38,7 @@ std::map<std::string, std::string> p3::server::default_config {
{"phonebook:filename", "~/.3phone/phonebook"},
{"phonebook:auto_add", "false"},
{"phonebook:auto_add_unanswered", "false"},
{"phonebook:reverse_lookup_command", "none"},
{"hook:command", "~/.3phone/hook"},
{"phone:ringtone", "/usr/share/3phone/rt.wav"},
{"phone:holdmusic", "none"},
......
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