From d8e60cea9507daf5485ee1627446497c7c1169f8 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Wed, 15 Feb 2012 21:13:14 +0100 Subject: [PATCH] Join the database from sms backend --- backends/smstools3/main.cpp | 76 +++++++++++++++++++++++++++++++++++++ spectrum/src/sample.cfg | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/backends/smstools3/main.cpp b/backends/smstools3/main.cpp index bc02c24a..919480ec 100644 --- a/backends/smstools3/main.cpp +++ b/backends/smstools3/main.cpp @@ -10,6 +10,10 @@ #include "transport/config.h" #include "transport/networkplugin.h" +#include "transport/sqlite3backend.h" +#include "transport/mysqlbackend.h" +#include "transport/pqxxbackend.h" +#include "transport/storagebackend.h" #include "Swiften/Swiften.h" #include #include "unistd.h" @@ -40,6 +44,7 @@ using namespace log4cxx; class SMSNetworkPlugin; SMSNetworkPlugin * np = NULL; +StorageBackend *storageBackend; class SMSNetworkPlugin : public NetworkPlugin { public: @@ -135,6 +140,18 @@ class SMSNetworkPlugin : public NetworkPlugin { } void handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password) { + UserInfo info; + if (!storageBackend->getUser(user, info)) { + handleDisconnected(user, 0, "Not registered user."); + return; + } + std::list roster; + storageBackend->getBuddies(info.id, roster); + + BOOST_FOREACH(BuddyInfo &b, roster) { + handleBuddyChanged(user, b.legacyName, b.alias, b.groups, pbnetwork::STATUS_ONLINE); + } + np->handleConnected(user); // std::vector groups; // groups.push_back("ZCode"); @@ -154,6 +171,14 @@ class SMSNetworkPlugin : public NetworkPlugin { void handleLeaveRoomRequest(const std::string &user, const std::string &room) { } + void handleBuddyUpdatedRequest(const std::string &user, const std::string &buddyName, const std::string &alias, const std::vector &groups) { + handleBuddyChanged(user, buddyName, alias, groups, pbnetwork::STATUS_ONLINE); + } + + void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector &groups) { + + } + private: @@ -255,6 +280,57 @@ int main (int argc, char* argv[]) { log4cxx::PropertyConfigurator::configure(p); } +#ifdef WITH_SQLITE + if (CONFIG_STRING(&config, "database.type") == "sqlite3") { + storageBackend = new SQLite3Backend(&config); + if (!storageBackend->connect()) { + std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; + return -1; + } + } +#else + if (CONFIG_STRING(&config, "database.type") == "sqlite3") { + std::cerr << "Spectrum2 is not compiled with mysql backend.\n"; + return -2; + } +#endif + +#ifdef WITH_MYSQL + if (CONFIG_STRING(&config, "database.type") == "mysql") { + storageBackend = new MySQLBackend(&config); + if (!storageBackend->connect()) { + std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; + return -1; + } + } +#else + if (CONFIG_STRING(&config, "database.type") == "mysql") { + std::cerr << "Spectrum2 is not compiled with mysql backend.\n"; + return -2; + } +#endif + +#ifdef WITH_PQXX + if (CONFIG_STRING(&config, "database.type") == "pqxx") { + storageBackend = new PQXXBackend(&config); + if (!storageBackend->connect()) { + std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; + return -1; + } + } +#else + if (CONFIG_STRING(&config, "database.type") == "pqxx") { + std::cerr << "Spectrum2 is not compiled with pqxx backend.\n"; + return -2; + } +#endif + + if (CONFIG_STRING(&config, "database.type") != "mysql" && CONFIG_STRING(&config, "database.type") != "sqlite3" + && CONFIG_STRING(&config, "database.type") != "pqxx" && CONFIG_STRING(&config, "database.type") != "none") { + std::cerr << "Unknown storage backend " << CONFIG_STRING(&config, "database.type") << "\n"; + return -2; + } + Swift::SimpleEventLoop eventLoop; loop_ = &eventLoop; np = new SMSNetworkPlugin(&config, &eventLoop, host, port); diff --git a/spectrum/src/sample.cfg b/spectrum/src/sample.cfg index 10850f05..85f88d37 100644 --- a/spectrum/src/sample.cfg +++ b/spectrum/src/sample.cfg @@ -33,6 +33,6 @@ 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 = none # or "none" without database backend +type = sqlite3 # or "none" without database backend database = test.sql prefix=icq