From 57963cf18ec01b4998765f292001cd207e0cc2b1 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Wed, 15 Feb 2012 22:31:08 +0100 Subject: [PATCH] working smstools3 with basic pairing --- backends/smstools3/main.cpp | 48 +++++++++++++++++++++++++++++++------ spectrum/src/sample.cfg | 12 +++++++--- src/mysqlbackend.cpp | 10 +++++--- src/sqlite3backend.cpp | 2 ++ 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/backends/smstools3/main.cpp b/backends/smstools3/main.cpp index 919480ec..892a37f9 100644 --- a/backends/smstools3/main.cpp +++ b/backends/smstools3/main.cpp @@ -42,6 +42,8 @@ using namespace Transport; using namespace log4cxx; +#define INTERNAL_USER "/sms@backend@internal@user" + class SMSNetworkPlugin; SMSNetworkPlugin * np = NULL; StorageBackend *storageBackend; @@ -52,6 +54,7 @@ class SMSNetworkPlugin : public NetworkPlugin { Swift::BoostIOServiceThread m_boostIOServiceThread; boost::shared_ptr m_conn; Swift::Timer::ref m_timer; + int m_internalUser; SMSNetworkPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() { this->config = config; @@ -65,6 +68,13 @@ class SMSNetworkPlugin : public NetworkPlugin { m_timer = m_factories->getTimerFactory()->createTimer(5000); m_timer->onTick.connect(boost::bind(&SMSNetworkPlugin::handleSMSDir, this)); m_timer->start(); + + UserInfo info; + info.jid = INTERNAL_USER; + info.password = ""; + storageBackend->setUser(info); + storageBackend->getUser(INTERNAL_USER, info); + m_internalUser = info.id; } @@ -78,21 +88,32 @@ class SMSNetworkPlugin : public NetworkPlugin { str.assign((std::istreambuf_iterator(t)), std::istreambuf_iterator()); - std::string to = ""; + std::string from = ""; std::string msg = ""; while(str.find("\n") != std::string::npos) { std::string line = str.substr(0, str.find("\n")); - if (line.find("To: ") == 0) { - to = line.substr(strlen("To: ")); + if (line.find("From: ") == 0) { + from = line.substr(strlen("From: ")); } else if (line.empty()) { - msg = str; + msg = str.substr(1); break; } str = str.substr(str.find("\n") + 1); } - std::cout << "INCOMING SMS '" << to << "' '" << msg << "'\n"; + std::list roster; + storageBackend->getBuddies(m_internalUser, roster); + + std::string to; + BOOST_FOREACH(BuddyInfo &b, roster) { + if (b.legacyName == from) { + to = b.alias; + } + } + + handleMessage(to, from, msg); + std::cout << "INCOMING SMS '" << from << "' '" << to << "' '" << msg << "'\n"; } @@ -104,7 +125,7 @@ class SMSNetworkPlugin : public NetworkPlugin { try { if (is_regular(itr->path())) { handleSMS(itr->path().string()); -// remove(itr->path()); + remove(itr->path()); } } catch (const filesystem_error& ex) { @@ -162,7 +183,20 @@ class SMSNetworkPlugin : public NetworkPlugin { } void handleMessageSendRequest(const std::string &user, const std::string &legacyName, const std::string &message, const std::string &xhtml = "") { - sendSMS(legacyName, message); + std::string n = legacyName; + if (n.find("+") == 0) { + n = n.substr(1); + } + + BuddyInfo info; + info.legacyName = n; + info.alias = user; + info.id = -1; + info.subscription = "both"; + info.flags = 0; + storageBackend->addBuddy(m_internalUser, info); + + sendSMS(n, message); } void handleJoinRoomRequest(const std::string &user, const std::string &room, const std::string &nickname, const std::string &password) { diff --git a/spectrum/src/sample.cfg b/spectrum/src/sample.cfg index 85f88d37..cc57d216 100644 --- a/spectrum/src/sample.cfg +++ b/spectrum/src/sample.cfg @@ -33,6 +33,12 @@ incoming_dir=/var/spool/sms/incoming #backend_config=/home/hanzz/code/libtransport/spectrum/src/backend-logging.cfg # log4cxx/log4j logging configuration file for backends [database] -type = sqlite3 # or "none" without database backend -database = test.sql -prefix=icq +#type = sqlite3 # or "none" without database backend +#database = test.sql +#prefix=icq +type = mysql # or "none" without database backend....................................................................................................................... +database = test +prefix= +user=root +password=yourrootsqlpassword +#encryption_key=hanzzik diff --git a/src/mysqlbackend.cpp b/src/mysqlbackend.cpp index 4bc2eec4..1af53c3a 100644 --- a/src/mysqlbackend.cpp +++ b/src/mysqlbackend.cpp @@ -470,7 +470,7 @@ long MySQLBackend::addBuddy(long userId, const BuddyInfo &buddyInfo) { long id = (long) mysql_insert_id(&m_conn); // INSERT OR REPLACE INTO " + m_prefix + "buddies_settings (user_id, buddy_id, var, type, value) VALUES (?, ?, ?, ?, ?) - if (!buddyInfo.settings.find("icon_hash")->second.s.empty()) { + if (buddyInfo.settings.find("icon_hash") != buddyInfo.settings.end() && !buddyInfo.settings.find("icon_hash")->second.s.empty()) { *m_updateBuddySetting << userId << id << buddyInfo.settings.find("icon_hash")->first << (int) TYPE_STRING << buddyInfo.settings.find("icon_hash")->second.s << buddyInfo.settings.find("icon_hash")->second.s; EXEC(m_updateBuddySetting, addBuddy(userId, buddyInfo)); } @@ -597,6 +597,10 @@ void MySQLBackend::getUserSetting(long id, const std::string &variable, int &typ else { *m_getUserSetting >> type >> value; } + + while (m_getUserSetting->fetch() == 0) { + + } } void MySQLBackend::updateUserSetting(long id, const std::string &variable, const std::string &value) { @@ -606,11 +610,11 @@ void MySQLBackend::updateUserSetting(long id, const std::string &variable, const } void MySQLBackend::beginTransaction() { - exec("START TRANSACTION;"); + //exec("START TRANSACTION;"); } void MySQLBackend::commitTransaction() { - exec("COMMIT;"); + //exec("COMMIT;"); } } diff --git a/src/sqlite3backend.cpp b/src/sqlite3backend.cpp index ad03bf23..ea869275 100644 --- a/src/sqlite3backend.cpp +++ b/src/sqlite3backend.cpp @@ -111,6 +111,8 @@ bool SQLite3Backend::connect() { return false; } + sqlite3_busy_timeout(m_db, 1500); + if (createDatabase() == false) return false;