From b0d2249ebcec3f7794b3204f217eced4bf957ade Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:12:39 +0100 Subject: [PATCH 01/18] Added Windows support to retroshare-android-service --- .../src/retroshare-android-service.pro | 31 ++++++++++++++++++- retroshare-android-service/src/service.cpp | 10 +++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/retroshare-android-service/src/retroshare-android-service.pro b/retroshare-android-service/src/retroshare-android-service.pro index c2caa12a0..e45067f81 100644 --- a/retroshare-android-service/src/retroshare-android-service.pro +++ b/retroshare-android-service/src/retroshare-android-service.pro @@ -6,11 +6,40 @@ QT += core network QT -= gui CONFIG += c++11 -CONFIG += dll +android-g++:CONFIG += dll android-g++:TEMPLATE = lib !android-g++:TEMPLATE = app +win32 { + OBJECTS_DIR = temp/obj + + LIBS_DIR = $$PWD/../../libs/lib + LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a + LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a + + for(lib, LIB_DIR):LIBS += -L"$$lib" + for(bin, BIN_DIR):LIBS += -L"$$bin" + + + LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 + LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 + LIBS += -lwinmm + + DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T + + DEPENDPATH += . $$INC_DIR + INCLUDEPATH += . $$INC_DIR + + greaterThan(QT_MAJOR_VERSION, 4) { + # Qt 5 + RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src + } else { + # Qt 4 + QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src + } +} + SOURCES += service.cpp DEPENDPATH *= ../../libresapi/src diff --git a/retroshare-android-service/src/service.cpp b/retroshare-android-service/src/service.cpp index d3759dc61..afe73bfc3 100644 --- a/retroshare-android-service/src/service.cpp +++ b/retroshare-android-service/src/service.cpp @@ -39,11 +39,19 @@ int main(int argc, char *argv[]) #endif QCoreApplication a(argc, argv); + + RsInit::InitRsConfig(); + ApiServer api; RsControlModule ctrl_mod(argc, argv, api.getStateTokenServer(), &api, true); api.addResourceHandler("control", dynamic_cast(&ctrl_mod), &resource_api::RsControlModule::handleRequest); - QString sockPath = QString::fromStdString(RsAccounts::ConfigDirectory()); +#ifdef QT_DEBUG + QString sockPath = "RS/"; +#else + QString sockPath = QCoreApplication::applicationDirPath(); +#endif + sockPath.append("/libresapi.sock"); qDebug() << "Listening on:" << sockPath; ApiServerLocal apiServerLocal(&api, sockPath); (void) apiServerLocal; From cb041341143edaf6d5b9321d2f9208cfb857a7eb Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:15:47 +0100 Subject: [PATCH 02/18] Added Windows support to libresapi when working with retroshare_android_service --- libresapi/src/libresapi.pro | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index f310198fb..f1a3aa8d5 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -10,6 +10,40 @@ DESTDIR = lib INCLUDEPATH += ../../libretroshare/src +retroshare_android_service { + win32 { + OBJECTS_DIR = temp/obj + + LIBS_DIR = $$PWD/../../libs/lib + LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a + LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a + + for(lib, LIB_DIR):LIBS += -L"$$lib" + for(bin, BIN_DIR):LIBS += -L"$$bin" + + + LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 + LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 + LIBS += -lwinmm + + DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T + + DEPENDPATH += . $$INC_DIR + INCLUDEPATH += . $$INC_DIR + + greaterThan(QT_MAJOR_VERSION, 4) { + # Qt 5 + RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src + } else { + # Qt 4 + QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src + } + } + + DEPENDPATH += . ../../libretroshare/src/ + INCLUDEPATH += ../../libretroshare/src/ +} + libresapihttpserver { CONFIG += libmicrohttpd From ae95b49f8a1a235f8fe64632915c48b99f5d6849 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:20:53 +0100 Subject: [PATCH 03/18] Added SettingsHandler to handle response to settings requests --- libresapi/src/api/ApiServer.cpp | 10 +- libresapi/src/api/SettingsHandler.cpp | 182 ++++++++++++++++++++++++++ libresapi/src/api/SettingsHandler.h | 59 +++++++++ libresapi/src/libresapi.pro | 6 +- 4 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 libresapi/src/api/SettingsHandler.cpp create mode 100644 libresapi/src/api/SettingsHandler.h diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index b9606a003..593e9386a 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -17,6 +17,8 @@ #include "ChannelsHandler.h" #include "StatsHandler.h" +#include "SettingsHandler.h" + /* data types in json http://json.org/ string (utf-8 unicode) @@ -236,7 +238,8 @@ public: mChatHandler(sts, ifaces.mNotify, ifaces.mMsgs, ifaces.mPeers, ifaces.mIdentity, &mPeersHandler), mApiPluginHandler(sts, ifaces), mChannelsHandler(ifaces.mGxsChannels), - mStatsHandler() + mStatsHandler(), + mSettingsHandler(sts) { // the dynamic cast is to not confuse the addResourceHandler template like this: // addResourceHandler(derived class, parent class) @@ -262,7 +265,9 @@ public: &ChannelsHandler::handleRequest); router.addResourceHandler("stats", dynamic_cast(&mStatsHandler), &StatsHandler::handleRequest); - } + router.addResourceHandler("settings", dynamic_cast(&mSettingsHandler), + &SettingsHandler::handleRequest); + } PeersHandler mPeersHandler; IdentityHandler mIdentityHandler; @@ -274,6 +279,7 @@ public: ApiPluginHandler mApiPluginHandler; ChannelsHandler mChannelsHandler; StatsHandler mStatsHandler; + SettingsHandler mSettingsHandler; }; ApiServer::ApiServer(): diff --git a/libresapi/src/api/SettingsHandler.cpp b/libresapi/src/api/SettingsHandler.cpp new file mode 100644 index 000000000..6e1aa75da --- /dev/null +++ b/libresapi/src/api/SettingsHandler.cpp @@ -0,0 +1,182 @@ +#include "SettingsHandler.h" + +#include + +#include + +namespace resource_api +{ + #define SETTINGS_FILE (QString::fromUtf8(RsAccounts::AccountDirectory().c_str()) + "/Sonet.conf") + + SettingsHandler::SettingsHandler(StateTokenServer *sts, const QString settingsGroup) : + QSettings(SETTINGS_FILE, QSettings::IniFormat), + mStateTokenServer(sts), + mMtx("SettingsHandler Mutex"), + mStateToken(sts->getNewToken()) + { + RsPeerId sPreferedId; + m_bValid = RsAccounts::GetPreferredAccountId(sPreferedId); + + if (!settingsGroup.isEmpty()) + beginGroup(settingsGroup); + + addResourceHandler("*", this, &SettingsHandler::handleSettingsRequest); + addResourceHandler("get_advanced_mode", this, &SettingsHandler::handleGetAdvancedMode); + addResourceHandler("set_advanced_mode", this, &SettingsHandler::handleSetAdvancedMode); + addResourceHandler("get_flickable_grid_mode", this, &SettingsHandler::handleGetFlickableGridMode); + addResourceHandler("set_flickable_grid_mode", this, &SettingsHandler::handleSetFlickableGridMode); + addResourceHandler("get_auto_login", this, &SettingsHandler::handleGetAutoLogin); + addResourceHandler("set_auto_login", this, &SettingsHandler::handleSetAutoLogin); + } + + SettingsHandler::~SettingsHandler() + { + sync(); + } + + void SettingsHandler::handleSettingsRequest(Request &/*req*/, Response &resp) + { + + } + + void SettingsHandler::handleGetAdvancedMode(Request &/*req*/, Response &resp) + { + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + bool advanced_mode = valueFromGroup("General", "Advanced", false).toBool(); + resp.mDataStream << makeKeyValueReference("advanced_mode", advanced_mode); + resp.setOk(); + sync(); + } + + void SettingsHandler::handleSetAdvancedMode(Request &req, Response &resp) + { + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + bool advanced_mode; + req.mStream << makeKeyValueReference("advanced_mode", advanced_mode); + setValueToGroup("General", "Advanced", advanced_mode); + resp.setOk(); + sync(); + } + + void SettingsHandler::handleGetFlickableGridMode(Request &/*req*/, Response &resp) + { + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + bool flickable_grid_mode = valueFromGroup("General", "FlickableGrid", false).toBool(); + resp.mDataStream << makeKeyValueReference("flickable_grid_mode", flickable_grid_mode); + resp.setOk(); + sync(); + } + + void SettingsHandler::handleSetFlickableGridMode(Request &req, Response &resp) + { + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + bool flickable_grid_mode; + req.mStream << makeKeyValueReference("flickable_grid_mode", flickable_grid_mode); + setValueToGroup("General", "FlickableGrid", flickable_grid_mode); + + resp.setOk(); + sync(); + } + + void SettingsHandler::handleGetAutoLogin(Request &/*req*/, Response &resp) + { + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + bool autoLogin = RsInit::getAutoLogin();; + resp.mDataStream << makeKeyValueReference("auto_login", autoLogin); + resp.setOk(); + sync(); + } + + void SettingsHandler::handleSetAutoLogin(Request &req, Response &resp) + { + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + bool autoLogin; + req.mStream << makeKeyValueReference("auto_login", autoLogin); + RsInit::setAutoLogin(autoLogin); + + resp.setOk(); + sync(); + } + + QVariant SettingsHandler::value(const QString &key, const QVariant &defaultVal) const + { + if (m_bValid == false) + { + return defaultVal.isNull() ? defaultValue(key) : defaultVal; + } + return QSettings::value(key, defaultVal.isNull() ? defaultValue(key) : defaultVal); + } + + void SettingsHandler::setValue(const QString &key, const QVariant &val) + { + if (m_bValid == false) + { + std::cerr << "RSettings::setValue() Calling on invalid object, key = " << key.toStdString() << std::endl; + return; + } + if (val == defaultValue(key)) + QSettings::remove(key); + else if (val != value(key)) + QSettings::setValue(key, val); + } + + QVariant SettingsHandler::valueFromGroup(const QString &group, const QString &key, const QVariant &defaultVal) + { + beginGroup(group); + QVariant val = value(key, defaultVal); + endGroup(); + + return val; + } + + void SettingsHandler::setValueToGroup(const QString &group, const QString &key, const QVariant &val) + { + beginGroup(group); + setValue(key, val); + endGroup(); + } + + void SettingsHandler::setDefault(const QString &key, const QVariant &val) + { + _defaults.insert(key, val); + } + + QVariant SettingsHandler::defaultValue(const QString &key) const + { + if (_defaults.contains(key)) + return _defaults.value(key); + return QVariant(); + } + + void SettingsHandler::reset() + { + /* Static method, so we have to create a QSettings object. */ + QSettings settings(SETTINGS_FILE, QSettings::IniFormat); + settings.clear(); + } +} // namespace resource_api + diff --git a/libresapi/src/api/SettingsHandler.h b/libresapi/src/api/SettingsHandler.h new file mode 100644 index 000000000..b4c21fae2 --- /dev/null +++ b/libresapi/src/api/SettingsHandler.h @@ -0,0 +1,59 @@ +#ifndef SETTINGSHANDLER_H +#define SETTINGSHANDLER_H + +#include + +#include + +#include "ResourceRouter.h" +#include "StateTokenServer.h" + +/* Reimplemented class RSettings*/ +namespace resource_api +{ + class SettingsHandler : public ResourceRouter, public QSettings + { + public: + SettingsHandler(StateTokenServer* sts, const QString group = QString()); + ~SettingsHandler(); + + static void reset(); + + QVariant value(const QString &key, + const QVariant &defaultVal = QVariant()) const; + + void setValue(const QString &key, const QVariant &val); + + QVariant valueFromGroup(const QString &group, const QString &key, + const QVariant &defaultVal = QVariant()); + void setValueToGroup(const QString &group, const QString &key, + const QVariant &val); + + protected: + void setDefault(const QString &key, const QVariant &val); + QVariant defaultValue(const QString &key) const; + + bool m_bValid; + + private: + void handleSettingsRequest(Request& req, Response& resp); + + void handleGetAdvancedMode(Request& req, Response& resp); + void handleSetAdvancedMode(Request& req, Response& resp); + + void handleGetFlickableGridMode(Request& req, Response& resp); + void handleSetFlickableGridMode(Request& req, Response& resp); + + void handleGetAutoLogin(Request& req, Response& resp); + void handleSetAutoLogin(Request& req, Response& resp); + + QHash _defaults; + + StateTokenServer* mStateTokenServer; + + RsMutex mMtx; + StateToken mStateToken; // mutex protected + }; +} // namespace resource_api + +#endif // SETTINGSHANDLER_H diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index f1a3aa8d5..31de982b3 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -182,7 +182,8 @@ SOURCES += \ util/ContentTypes.cpp \ api/ApiPluginHandler.cpp \ api/ChannelsHandler.cpp \ - api/StatsHandler.cpp + api/StatsHandler.cpp \ + api/SettingsHandler.cpp HEADERS += \ api/ApiServer.h \ @@ -209,7 +210,8 @@ HEADERS += \ util/ContentTypes.h \ api/ApiPluginHandler.h \ api/ChannelsHandler.h \ - api/StatsHandler.h + api/StatsHandler.h \ + api/SettingsHandler.h libresapilocalserver { CONFIG *= qt From 39967b7ce7e62b0acaf17b96d5a32d3b91506df4 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:23:28 +0100 Subject: [PATCH 04/18] Added name of requests to be used by GUI --- libresapi/src/api/ApiServer.cpp | 3 +++ libresapi/src/api/ApiTypes.h | 3 +++ libresapi/src/api/ResourceRouter.cpp | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index 593e9386a..6efadeaf6 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -368,6 +368,9 @@ std::string ApiServer::handleRequest(Request &request) if(data.isRawData()) return data.getRawData(); + if(!resp.mCallbackName.empty()) + outstream << resource_api::makeKeyValueReference("callback_name", resp.mCallbackName); + outstream << resource_api::makeKeyValue("debug_msg", debugString.str()); outstream << resource_api::makeKeyValueReference("returncode", returncode); if(!resp.mStateToken.isNull()) diff --git a/libresapi/src/api/ApiTypes.h b/libresapi/src/api/ApiTypes.h index c443eb170..6d6d5c746 100644 --- a/libresapi/src/api/ApiTypes.h +++ b/libresapi/src/api/ApiTypes.h @@ -252,6 +252,9 @@ public: StateToken mStateToken; + //Just for GUI benefit + std::string mCallbackName; + // the result StreamBase& mDataStream; diff --git a/libresapi/src/api/ResourceRouter.cpp b/libresapi/src/api/ResourceRouter.cpp index a13201a27..48050cdb6 100644 --- a/libresapi/src/api/ResourceRouter.cpp +++ b/libresapi/src/api/ResourceRouter.cpp @@ -37,6 +37,13 @@ ResponseTask* ResourceRouter::handleRequest(Request& req, Response& resp) if(vit->first == req.mPath.top()) { req.mPath.pop(); + + //Just for GUI benefit + std::string callbackName; + req.mStream << makeKeyValueReference("callback_name", callbackName); + resp.mCallbackName = callbackName; + // + return vit->second->handleRequest(req, resp); } } From 28d37c2968b305a2163a11d3efd1690a3ad495d0 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:25:26 +0100 Subject: [PATCH 05/18] Added password storing to RsControlModule for GUI benefits --- libresapi/src/api/RsControlModule.cpp | 135 ++++++++++++++++++++++++-- libresapi/src/api/RsControlModule.h | 10 +- 2 files changed, 135 insertions(+), 10 deletions(-) diff --git a/libresapi/src/api/RsControlModule.cpp b/libresapi/src/api/RsControlModule.cpp index 9430df170..228bb691a 100644 --- a/libresapi/src/api/RsControlModule.cpp +++ b/libresapi/src/api/RsControlModule.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "api/ApiServer.h" #include "api/Operators.h" @@ -301,6 +302,7 @@ void RsControlModule::handlePassword(Request &req, Response &resp) mPassword = passwd; mWantPassword = false; mStateTokenServer->replaceToken(mStateToken); + mFixedPassword = passwd; } resp.mDataStream @@ -425,17 +427,17 @@ void RsControlModule::handleCreateLocation(Request &req, Response &resp) RsPeerId ssl_id; std::string err_string; // give the password to the password callback - { - RsStackMutex stack(mDataMtx); // ********** LOCKED ********** - mFixedPassword = pgp_password; - } + { + RsStackMutex stack(mDataMtx); // ********** LOCKED ********** + mFixedPassword = pgp_password; + } bool ssl_ok = RsAccounts::GenerateSSLCertificate(pgp_id, "", ssl_name, "", hidden_port!=0, ssl_password, ssl_id, err_string); // clear fixed password to restore normal password operation - { - RsStackMutex stack(mDataMtx); // ********** LOCKED ********** - mFixedPassword = ""; - } +// { +// RsStackMutex stack(mDataMtx); // ********** LOCKED ********** +// mFixedPassword = ""; +// } if (ssl_ok) { @@ -456,6 +458,123 @@ void RsControlModule::handleCreateLocation(Request &req, Response &resp) resp.setFail("could not create a new location. Error: "+err_string); } +class SignatureEventData +{ + public: + SignatureEventData(const void *_data,int32_t _len,unsigned int _signlen, std::string _reason) + { + // We need a new memory chnk because there's no guarranty _sign nor _signlen are not in the stack + + sign = (unsigned char *)rs_malloc(_signlen); + + if(!sign) + { + signlen = NULL; + signature_result = SELF_SIGNATURE_RESULT_FAILED; + return; + } + + signlen = new unsigned int; + *signlen = _signlen; + signature_result = SELF_SIGNATURE_RESULT_PENDING; + data = rs_malloc(_len); + + if(!data) + { + len = 0; + return; + } + len = _len; + memcpy(data,_data,len); + reason = _reason; + } + + ~SignatureEventData() + { + free(sign); + delete signlen; + free(data); + } + + void performSignature() + { + if(rsPeers->gpgSignData(data,len,sign,signlen,reason)) + signature_result = SELF_SIGNATURE_RESULT_SUCCESS; + else + signature_result = SELF_SIGNATURE_RESULT_FAILED; + } + + void *data; + uint32_t len; + unsigned char *sign; + unsigned int *signlen; + int signature_result; // 0=pending, 1=done, 2=failed/cancelled. + std::string reason; +}; + +bool RsControlModule::askForDeferredSelfSignature(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen,int& signature_result, std::string reason /*=""*/) +{ + + { + RsStackMutex stack(mDataMtx); + std::cerr << "NotifyTxt:: deferred signature event requeted. " << std::endl; + + // Look into the queue + + Sha1CheckSum chksum = RsDirUtil::sha1sum((uint8_t*)data,len); + + std::map::iterator it = _deferred_signature_queue.find(chksum.toStdString()); + signature_result = SELF_SIGNATURE_RESULT_PENDING; + + if(it != _deferred_signature_queue.end()) + { + signature_result = it->second->signature_result; + + if(it->second->signature_result != SELF_SIGNATURE_RESULT_PENDING) // found it. Copy the result, and remove from the queue. + { + // We should check for the exact data match, for the sake of being totally secure. + // + std::cerr << "Found into queue: returning it" << std::endl; + + memcpy(sign,it->second->sign,*it->second->signlen); + *signlen = *(it->second->signlen); + + delete it->second; + _deferred_signature_queue.erase(it); + } + + return true; // already registered, but not done yet. + } + + // Not found. Store in the queue and emit a signal. + // + std::cerr << "NotifyTxt:: deferred signature event requeted. Pushing into queue" << std::endl; + + SignatureEventData *edta = new SignatureEventData(data,len,*signlen, reason); + + _deferred_signature_queue[chksum.toStdString()] = edta; + } + handleSignatureEvent(); + return true; +} + +void RsControlModule::handleSignatureEvent() +{ + std::cerr << "NotifyTxt:: performing a deferred signature in the main GUI thread." << std::endl; + + static bool working = false ; + + if(!working) + { + working = true ; + + for(std::map::const_iterator it(_deferred_signature_queue.begin());it!=_deferred_signature_queue.end();++it) + it->second->performSignature() ; + + working = false ; + } +} + void RsControlModule::setRunState(RunState s, std::string errstr) { RsStackMutex stack(mDataMtx); // ********** LOCKED ********** diff --git a/libresapi/src/api/RsControlModule.h b/libresapi/src/api/RsControlModule.h index d3c8ad18b..338027e15 100644 --- a/libresapi/src/api/RsControlModule.h +++ b/libresapi/src/api/RsControlModule.h @@ -9,6 +9,8 @@ namespace resource_api{ class StateTokenServer; class ApiServer; +class SignatureEventData; + // resource api module to control accounts, startup and shutdown of retroshare // - this module handles everything, no things are required from outside // - exception: users of this module have to create an api server and register this module @@ -31,8 +33,9 @@ public: // returns true if the process should terminate bool processShouldExit(); - // from NotifyClient - virtual bool askForPassword(const std::string &title, const std::string& key_details, bool prev_is_bad , std::string& password,bool& canceled); + // from NotifyClient + virtual bool askForPassword(const std::string &title, const std::string& key_details, bool prev_is_bad , std::string& password,bool& canceled) override; + virtual bool askForDeferredSelfSignature(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen,int& signature_result, std::string reason = "") override; protected: // from RsThread @@ -84,6 +87,9 @@ private: // we store the password in this variable, it has higher priority than the normal password variable // it is also to avoid a lock, when we make a synchronous call into librs, like in ssl cert generation std::string mFixedPassword; + + void handleSignatureEvent(); + std::map _deferred_signature_queue ; }; } // namespace resource_api From cb32f304a98e9ca953b4ec5c478730fb905d042a Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:28:31 +0100 Subject: [PATCH 06/18] Added states handling to PeersHandler --- libresapi/src/api/PeersHandler.cpp | 226 ++++++++++++++++++++++++++++- libresapi/src/api/PeersHandler.h | 6 + 2 files changed, 228 insertions(+), 4 deletions(-) diff --git a/libresapi/src/api/PeersHandler.cpp b/libresapi/src/api/PeersHandler.cpp index 0f35e59e9..d77a39651 100644 --- a/libresapi/src/api/PeersHandler.cpp +++ b/libresapi/src/api/PeersHandler.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -12,6 +13,12 @@ namespace resource_api { +#define PEER_STATE_ONLINE 1 +#define PEER_STATE_BUSY 2 +#define PEER_STATE_AWAY 3 +#define PEER_STATE_AVAILABLE 4 +#define PEER_STATE_INACTIVE 5 +#define PEER_STATE_OFFLINE 6 // todo: groups, add friend, remove friend, permissions void peerDetailsToStream(StreamBase& stream, RsPeerDetails& details) @@ -21,7 +28,50 @@ void peerDetailsToStream(StreamBase& stream, RsPeerDetails& details) << makeKeyValueReference("name", details.name) << makeKeyValueReference("location", details.location) << makeKeyValueReference("pgp_id", details.gpg_id) - ; + << makeKeyValueReference("pgp_id", details.gpg_id); + + if(details.state & RS_PEER_STATE_CONNECTED) + { + std::list statusInfo; + rsStatus->getStatusList(statusInfo); + + std::string state_string; + std::list::iterator it; + for (it = statusInfo.begin(); it != statusInfo.end(); ++it) + { + if (it->id == details.id) + { + switch (it->status) + { + case RS_STATUS_INACTIVE: + state_string = "inactive"; + break; + + case RS_STATUS_ONLINE: + state_string = "online"; + break; + + case RS_STATUS_AWAY: + state_string = "away"; + break; + + case RS_STATUS_BUSY: + state_string = "busy"; + break; + default: + state_string = "undefined"; + break; + } + break; + } + } + stream << makeKeyValueReference("state_string", state_string); + } + else + { + std::string state_string = "undefined"; + stream << makeKeyValueReference("state_string", state_string); + } } bool peerInfoToStream(StreamBase& stream, RsPeerDetails& details, RsPeers* peers, std::list& grpInfo, bool have_avatar) @@ -29,7 +79,9 @@ bool peerInfoToStream(StreamBase& stream, RsPeerDetails& details, RsPeers* peers bool ok = true; peerDetailsToStream(stream, details); stream << makeKeyValue("is_online", peers->isOnline(details.id)) - << makeKeyValue("chat_id", ChatId(details.id).toStdString()); + << makeKeyValue("chat_id", ChatId(details.id).toStdString()) + << makeKeyValue("custom_state_string", rsMsgs->getCustomStateString(details.id)); + std::string avatar_address = "/"+details.id.toStdString()+"/avatar_image"; @@ -63,7 +115,11 @@ PeersHandler::PeersHandler(StateTokenServer* sts, RsNotify* notify, RsPeers *pee mNotify->registerNotifyClient(this); mStateTokenServer->registerTickClient(this); addResourceHandler("*", this, &PeersHandler::handleWildcard); - addResourceHandler("examine_cert", this, &PeersHandler::handleExamineCert); + addResourceHandler("get_state_string", this, &PeersHandler::handleGetStateString); + addResourceHandler("set_state_string", this, &PeersHandler::handleSetStateString); + addResourceHandler("get_custom_state_string", this, &PeersHandler::handleGetCustomStateString); + addResourceHandler("set_custom_state_string", this, &PeersHandler::handleSetCustomStateString); + addResourceHandler("examine_cert", this, &PeersHandler::handleExamineCert); } PeersHandler::~PeersHandler() @@ -121,6 +177,83 @@ static bool have_avatar(RsMsgs* msgs, const RsPeerId& id) return size != 0; } +void PeersHandler::handleGetStateString(Request& req, Response& resp) +{ + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + std::string state_string; + StatusInfo statusInfo; + if (rsStatus->getOwnStatus(statusInfo)) + { + if(statusInfo.status == RS_STATUS_ONLINE) + state_string = "online"; + else if(statusInfo.status == RS_STATUS_BUSY) + state_string = "busy"; + else if(statusInfo.status == RS_STATUS_AWAY) + state_string = "away"; + else if(statusInfo.status == RS_STATUS_INACTIVE) + state_string = "inactive"; + else + state_string = "undefined"; + } + else + state_string = "undefined"; + + resp.mDataStream << makeKeyValueReference("state_string", state_string); + resp.setOk(); +} + +void PeersHandler::handleSetStateString(Request& req, Response& resp) +{ + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + std::string state_string; + req.mStream << makeKeyValueReference("state_string", state_string); + + uint32_t status; + if(state_string == "online") + status = RS_STATUS_ONLINE; + else if(state_string == "busy") + status = RS_STATUS_BUSY; + else if(state_string == "away") + status = RS_STATUS_AWAY; + + rsStatus->sendStatus(RsPeerId(), status); + resp.setOk(); +} + +void PeersHandler::handleGetCustomStateString(Request& req, Response& resp) +{ + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + std::string custom_state_string = rsMsgs->getCustomStateString(); + resp.mDataStream << makeKeyValueReference("custom_state_string", custom_state_string); + resp.setOk(); +} + +void PeersHandler::handleSetCustomStateString(Request& req, Response& resp) +{ + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + + std::string custom_state_string; + req.mStream << makeKeyValueReference("custom_state_string", custom_state_string); + + rsMsgs->setCustomStateString(custom_state_string); + resp.setOk(); +} + void PeersHandler::handleWildcard(Request &req, Response &resp) { bool ok = false; @@ -176,6 +309,9 @@ void PeersHandler::handleWildcard(Request &req, Response &resp) RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ unread_msgs = mUnreadMsgsCounts; } + std::list statusInfo; + rsStatus->getStatusList(statusInfo); + // list all peers ok = true; std::list identities; @@ -216,6 +352,11 @@ void PeersHandler::handleWildcard(Request &req, Response &resp) StreamBase& locationStream = itemStream.getStreamToMember("locations"); // mark as list (in case list is empty) locationStream.getStreamToMember(); + + int bestPeerState = 0; + unsigned int bestRSState = 0; + std::string bestCustomStateString; + for(std::vector::iterator vit = detailsVec.begin(); vit != detailsVec.end(); ++vit) { if(vit->gpg_id == *lit) @@ -226,8 +367,85 @@ void PeersHandler::handleWildcard(Request &req, Response &resp) unread = unread_msgs.find(vit->id)->second; stream << makeKeyValueReference("unread_msgs", unread); peerInfoToStream(stream,*vit, mRsPeers, grpInfo, have_avatar(mRsMsgs, vit->id)); - } + + + /* Custom state string */ + std::string customStateString; + if (vit->state & RS_PEER_STATE_CONNECTED) + { + customStateString = rsMsgs->getCustomStateString(vit->id); + } + + int peerState = 0; + + if (vit->state & RS_PEER_STATE_CONNECTED) + { + // get the status info for this ssl id + int rsState = 0; + std::list::iterator it; + for (it = statusInfo.begin(); it != statusInfo.end(); ++it) + { + if (it->id == vit->id) + { + rsState = it->status; + switch (rsState) + { + case RS_STATUS_INACTIVE: + peerState = PEER_STATE_INACTIVE; + break; + + case RS_STATUS_ONLINE: + peerState = PEER_STATE_ONLINE; + break; + + case RS_STATUS_AWAY: + peerState = PEER_STATE_AWAY; + break; + + case RS_STATUS_BUSY: + peerState = PEER_STATE_BUSY; + break; + } + + /* find the best ssl contact for the gpg item */ + if (bestPeerState == 0 || peerState < bestPeerState) + { + bestPeerState = peerState; + bestRSState = rsState; + bestCustomStateString = customStateString; + } + else if (peerState == bestPeerState) + { + /* equal state */ + if (bestCustomStateString.empty() && !customStateString.empty()) + { + bestPeerState = peerState; + bestRSState = rsState; + bestCustomStateString = customStateString; + } + } + break; + } + } + } + } } + itemStream << makeKeyValue("custom_state_string", bestCustomStateString); + + std::string state_string; + + if(bestRSState == RS_STATUS_ONLINE) + state_string = "online"; + else if(bestRSState == RS_STATUS_BUSY) + state_string = "busy"; + else if(bestRSState == RS_STATUS_AWAY) + state_string = "away"; + else if(bestRSState == RS_STATUS_INACTIVE) + state_string = "inactive"; + else + state_string = "undefined"; + + itemStream << makeKeyValue("state_string", state_string); } resp.mStateToken = getCurrentStateToken(); } diff --git a/libresapi/src/api/PeersHandler.h b/libresapi/src/api/PeersHandler.h index dfcc9e10c..a2eab706b 100644 --- a/libresapi/src/api/PeersHandler.h +++ b/libresapi/src/api/PeersHandler.h @@ -35,6 +35,12 @@ private: void handleWildcard(Request& req, Response& resp); void handleExamineCert(Request& req, Response& resp); + void handleGetStateString(Request& req, Response& resp); + void handleSetStateString(Request& req, Response& resp); + + void handleGetCustomStateString(Request& req, Response& resp); + void handleSetCustomStateString(Request& req, Response& resp); + // a helper which ensures proper mutex locking StateToken getCurrentStateToken(); From 194878a3585bda4c53862269e7862c39bd901945 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:31:13 +0100 Subject: [PATCH 07/18] Added functions for handling own and not own identities to IdentityHandler --- libresapi/src/api/IdentityHandler.cpp | 115 +++++++++++++++++++++++++- libresapi/src/api/IdentityHandler.h | 3 + 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/libresapi/src/api/IdentityHandler.cpp b/libresapi/src/api/IdentityHandler.cpp index 4d3f0ff80..9f01442df 100644 --- a/libresapi/src/api/IdentityHandler.cpp +++ b/libresapi/src/api/IdentityHandler.cpp @@ -101,9 +101,12 @@ IdentityHandler::IdentityHandler(StateTokenServer *sts, RsNotify *notify, RsIden { mNotify->registerNotifyClient(this); - addResourceHandler("*", this, &IdentityHandler::handleWildcard); - addResourceHandler("own", this, &IdentityHandler::handleOwn); - addResourceHandler("create_identity", this, &IdentityHandler::handleCreateIdentity); + addResourceHandler("*", this, &IdentityHandler::handleWildcard); + //addResourceHandler("own", this, &IdentityHandler::handleOwn); + addResourceHandler("own", this, &IdentityHandler::handleOwnIdsRequest); + addResourceHandler("notown", this, &IdentityHandler::handleNotOwnIdsRequest); + + addResourceHandler("create_identity", this, &IdentityHandler::handleCreateIdentity); } IdentityHandler::~IdentityHandler() @@ -172,6 +175,112 @@ void IdentityHandler::handleWildcard(Request & /*req*/, Response &resp) else resp.setFail(); } + +void IdentityHandler::handleNotOwnIdsRequest(Request & /*req*/, Response &resp) +{ + bool ok = true; + + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; + uint32_t token; + mRsIdentity->getTokenService()->requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts); + + time_t start = time(NULL); + while((mRsIdentity->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(mRsIdentity->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10))) + ) + { +#ifdef WINDOWS_SYS + Sleep(500); +#else + usleep(500*1000); +#endif + } + + if(mRsIdentity->getTokenService()->requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + { + std::vector grps; + ok &= mRsIdentity->getGroupData(token, grps); + for(std::vector::iterator vit = grps.begin(); vit != grps.end(); vit++) + { + RsGxsIdGroup& grp = *vit; + //electron: not very happy about this, i think the flags should stay hidden in rsidentities + if(!(grp.mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) && grp.mIsAContact) + { + bool pgp_linked = (grp.mMeta.mGroupFlags & RSGXSID_GROUPFLAG_REALID_kept_for_compatibility ) ; + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("id", grp.mMeta.mGroupId) /// @deprecated using "id" as key can cause problems in some JS based languages like Qml @see gxs_id instead + << makeKeyValueReference("gxs_id", grp.mMeta.mGroupId) + << makeKeyValueReference("pgp_id",grp.mPgpId ) + << makeKeyValueReference("name", grp.mMeta.mGroupName) + << makeKeyValueReference("pgp_linked", pgp_linked); + } + } + } + else ok = false; + + if(ok) resp.setOk(); + else resp.setFail(); +} + +void IdentityHandler::handleOwnIdsRequest(Request & /*req*/, Response &resp) +{ + bool ok = true; + + { + RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStateToken; + } + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; + uint32_t token; + mRsIdentity->getTokenService()->requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts); + + time_t start = time(NULL); + while((mRsIdentity->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(mRsIdentity->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10))) + ) + { +#ifdef WINDOWS_SYS + Sleep(500); +#else + usleep(500*1000); +#endif + } + + if(mRsIdentity->getTokenService()->requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + { + std::vector grps; + ok &= mRsIdentity->getGroupData(token, grps); + for(std::vector::iterator vit = grps.begin(); vit != grps.end(); vit++) + { + RsGxsIdGroup& grp = *vit; + //electron: not very happy about this, i think the flags should stay hidden in rsidentities + if(vit->mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) + { + bool pgp_linked = (grp.mMeta.mGroupFlags & RSGXSID_GROUPFLAG_REALID_kept_for_compatibility ) ; + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("own_gxs_id", grp.mMeta.mGroupId) + << makeKeyValueReference("pgp_id",grp.mPgpId ) + << makeKeyValueReference("name", grp.mMeta.mGroupName) + << makeKeyValueReference("pgp_linked", pgp_linked); + } + } + + } + else + ok = false; + + if(ok) resp.setOk(); + else resp.setFail(); +} + ResponseTask* IdentityHandler::handleOwn(Request & /* req */, Response &resp) { StateToken state; diff --git a/libresapi/src/api/IdentityHandler.h b/libresapi/src/api/IdentityHandler.h index 5e5d67f13..1b54f5438 100644 --- a/libresapi/src/api/IdentityHandler.h +++ b/libresapi/src/api/IdentityHandler.h @@ -23,6 +23,9 @@ public: private: void handleWildcard(Request& req, Response& resp); + void handleNotOwnIdsRequest(Request& req, Response& resp); + void handleOwnIdsRequest(Request& req, Response& resp); + ResponseTask *handleOwn(Request& req, Response& resp); ResponseTask *handleCreateIdentity(Request& req, Response& resp); From 8624c9374de788394df09b5acb8913019302adf0 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 25 Mar 2017 18:33:35 +0100 Subject: [PATCH 08/18] Added functions for hadling particular lobbies types to ChatHandler --- libresapi/src/api/ChatHandler.cpp | 107 ++++++++++++++++++++++++++++-- libresapi/src/api/ChatHandler.h | 4 ++ 2 files changed, 106 insertions(+), 5 deletions(-) diff --git a/libresapi/src/api/ChatHandler.cpp b/libresapi/src/api/ChatHandler.cpp index 4335389df..f50eaae9e 100644 --- a/libresapi/src/api/ChatHandler.cpp +++ b/libresapi/src/api/ChatHandler.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -158,6 +159,10 @@ ChatHandler::ChatHandler(StateTokenServer *sts, RsNotify *notify, RsMsgs *msgs, addResourceHandler("initiate_distant_chat", this, &ChatHandler::handleInitiateDistantChatConnexion); addResourceHandler("distant_chat_status", this, &ChatHandler::handleDistantChatStatus); addResourceHandler("close_distant_chat", this, &ChatHandler::handleCloseDistantChatConnexion); + + addResourceHandler("private_lobbies", this, &ChatHandler::handlePrivateLobbies); + addResourceHandler("subscribed_public_lobbies", this, &ChatHandler::handleSubscribedPublicLobbies); + addResourceHandler("unsubscribed_public_lobbies", this, &ChatHandler::handleUnsubscribedPublicLobbies); } ChatHandler::~ChatHandler() @@ -928,15 +933,19 @@ void ChatHandler::handleMessages(Request &req, Response &resp) * doing it? */ tick(); + std::string chat_id; + req.mStream << makeKeyValueReference("chat_id", chat_id); + ChatId id(chat_id); + { RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ - ChatId id(req.mPath.top()); + // make response a list resp.mDataStream.getStreamToMember(); if(id.isNotSet()) { - resp.setFail("\""+req.mPath.top()+"\" is not a valid chat id"); - return; + resp.setFail("\""+chat_id+"\" is not a valid chat id"); + return; } std::map >::iterator mit = mMsgs.find(id); if(mit == mMsgs.end()) @@ -973,10 +982,14 @@ void ChatHandler::handleSendMessage(Request &req, Response &resp) void ChatHandler::handleMarkChatAsRead(Request &req, Response &resp) { RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ - ChatId id(req.mPath.top()); + + std::string chat_id; + req.mStream << makeKeyValueReference("chat_id", chat_id); + ChatId id(chat_id); + if(id.isNotSet()) { - resp.setFail("\""+req.mPath.top()+"\" is not a valid chat id"); + resp.setFail("\""+chat_id+"\" is not a valid chat id"); return; } std::map >::iterator mit = mMsgs.find(id); @@ -1197,4 +1210,88 @@ void ChatHandler::handleCloseDistantChatConnexion(Request& req, Response& resp) else resp.setFail("Failed to close distant chat"); } +void ChatHandler::handlePrivateLobbies(Request &req, Response &resp) +{ + tick(); + + { + RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ + resp.mDataStream.getStreamToMember(); + for(std::vector::iterator vit = mLobbies.begin(); vit != mLobbies.end(); ++vit) + { + if(!vit->is_private) + continue; + uint32_t unread_msgs = 0; + ChatId chat_id(vit->id); + std::map >::iterator mit = mMsgs.find(chat_id); + if(mit != mMsgs.end()) + { + std::list& msgs = mit->second; + for(std::list::iterator lit = msgs.begin(); lit != msgs.end(); ++lit) + if(!lit->read) + unread_msgs++; + } + resp.mDataStream.getStreamToMember() << *vit << makeKeyValueReference("unread_msg_count", unread_msgs); + } + resp.mStateToken = mLobbiesStateToken; + } + resp.setOk(); +} + +void ChatHandler::handleSubscribedPublicLobbies(Request &req, Response &resp) +{ + tick(); + + { + RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ + resp.mDataStream.getStreamToMember(); + for(std::vector::iterator vit = mLobbies.begin(); vit != mLobbies.end(); ++vit) + { + if(!vit->subscribed || vit->is_private || vit->is_broadcast) + continue; + uint32_t unread_msgs = 0; + ChatId chat_id(vit->id); + std::map >::iterator mit = mMsgs.find(chat_id); + if(mit != mMsgs.end()) + { + std::list& msgs = mit->second; + for(std::list::iterator lit = msgs.begin(); lit != msgs.end(); ++lit) + if(!lit->read) + unread_msgs++; + } + resp.mDataStream.getStreamToMember() << *vit << makeKeyValueReference("unread_msg_count", unread_msgs); + } + resp.mStateToken = mLobbiesStateToken; + } + resp.setOk(); +} + +void ChatHandler::handleUnsubscribedPublicLobbies(Request &req, Response &resp) +{ + tick(); + + { + RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ + resp.mDataStream.getStreamToMember(); + for(std::vector::iterator vit = mLobbies.begin(); vit != mLobbies.end(); ++vit) + { + if(vit->subscribed || vit->is_private) + continue; + uint32_t unread_msgs = 0; + ChatId chat_id(vit->id); + std::map >::iterator mit = mMsgs.find(chat_id); + if(mit != mMsgs.end()) + { + std::list& msgs = mit->second; + for(std::list::iterator lit = msgs.begin(); lit != msgs.end(); ++lit) + if(!lit->read) + unread_msgs++; + } + resp.mDataStream.getStreamToMember() << *vit << makeKeyValueReference("unread_msg_count", unread_msgs); + } + resp.mStateToken = mLobbiesStateToken; + } + resp.setOk(); +} + } // namespace resource_api diff --git a/libresapi/src/api/ChatHandler.h b/libresapi/src/api/ChatHandler.h index dc075c77f..056629c06 100644 --- a/libresapi/src/api/ChatHandler.h +++ b/libresapi/src/api/ChatHandler.h @@ -133,6 +133,10 @@ private: void handleDistantChatStatus(Request& req, Response& resp); void handleCloseDistantChatConnexion(Request& req, Response& resp); + void handlePrivateLobbies(Request &req, Response &resp); + void handleSubscribedPublicLobbies(Request &req, Response &resp); + void handleUnsubscribedPublicLobbies(Request &req, Response &resp); + void getPlainText(const std::string& in, std::string &out, std::vector &links); // last parameter is only used for lobbies! void locked_storeTypingInfo(const ChatId& chat_id, std::string status, RsGxsId lobby_gxs_id = RsGxsId()); From bef75408da0d4c58e996f02b6ac34daca0672110 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sun, 26 Mar 2017 15:11:07 +0200 Subject: [PATCH 09/18] Fixed building on Travis CI --- libresapi/src/libresapi.pro | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 31de982b3..33c4f726c 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -3,7 +3,8 @@ TEMPLATE = lib CONFIG += staticlib CONFIG += create_prl -CONFIG -= qt +CONFIG += qt +QT += core TARGET = resapi TARGET_PRL = libresapi DESTDIR = lib From c33107c1cc4bc17cf154c33039690e6047eff72a Mon Sep 17 00:00:00 2001 From: Konrad Date: Sun, 26 Mar 2017 19:12:04 +0200 Subject: [PATCH 10/18] Fixed: backward comptiblity --- libresapi/src/api/ChatHandler.cpp | 14 ++++---------- libresapi/src/api/IdentityHandler.cpp | 7 ++++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/libresapi/src/api/ChatHandler.cpp b/libresapi/src/api/ChatHandler.cpp index f50eaae9e..68f0bd356 100644 --- a/libresapi/src/api/ChatHandler.cpp +++ b/libresapi/src/api/ChatHandler.cpp @@ -933,18 +933,15 @@ void ChatHandler::handleMessages(Request &req, Response &resp) * doing it? */ tick(); - std::string chat_id; - req.mStream << makeKeyValueReference("chat_id", chat_id); - ChatId id(chat_id); - { RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ + ChatId id(req.mPath.top()); // make response a list resp.mDataStream.getStreamToMember(); if(id.isNotSet()) { - resp.setFail("\""+chat_id+"\" is not a valid chat id"); + resp.setFail("\""+req.mPath.top()+"\" is not a valid chat id"); return; } std::map >::iterator mit = mMsgs.find(id); @@ -982,14 +979,11 @@ void ChatHandler::handleSendMessage(Request &req, Response &resp) void ChatHandler::handleMarkChatAsRead(Request &req, Response &resp) { RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ - - std::string chat_id; - req.mStream << makeKeyValueReference("chat_id", chat_id); - ChatId id(chat_id); + ChatId id(req.mPath.top()); if(id.isNotSet()) { - resp.setFail("\""+chat_id+"\" is not a valid chat id"); + resp.setFail("\""+req.mPath.top()+"\" is not a valid chat id"); return; } std::map >::iterator mit = mMsgs.find(id); diff --git a/libresapi/src/api/IdentityHandler.cpp b/libresapi/src/api/IdentityHandler.cpp index 9f01442df..1a43211ea 100644 --- a/libresapi/src/api/IdentityHandler.cpp +++ b/libresapi/src/api/IdentityHandler.cpp @@ -102,9 +102,10 @@ IdentityHandler::IdentityHandler(StateTokenServer *sts, RsNotify *notify, RsIden mNotify->registerNotifyClient(this); addResourceHandler("*", this, &IdentityHandler::handleWildcard); - //addResourceHandler("own", this, &IdentityHandler::handleOwn); - addResourceHandler("own", this, &IdentityHandler::handleOwnIdsRequest); - addResourceHandler("notown", this, &IdentityHandler::handleNotOwnIdsRequest); + addResourceHandler("own", this, &IdentityHandler::handleOwn); + + addResourceHandler("own_ids", this, &IdentityHandler::handleOwnIdsRequest); + addResourceHandler("notown_ids", this, &IdentityHandler::handleNotOwnIdsRequest); addResourceHandler("create_identity", this, &IdentityHandler::handleCreateIdentity); } From 4031e493fba5c275e3b9d7c89630e5bdc3da56bd Mon Sep 17 00:00:00 2001 From: Konrad Date: Mon, 27 Mar 2017 16:11:04 +0200 Subject: [PATCH 11/18] Deleted: unused stateTokens --- libresapi/src/api/PeersHandler.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libresapi/src/api/PeersHandler.cpp b/libresapi/src/api/PeersHandler.cpp index d77a39651..b8fdc913d 100644 --- a/libresapi/src/api/PeersHandler.cpp +++ b/libresapi/src/api/PeersHandler.cpp @@ -181,7 +181,6 @@ void PeersHandler::handleGetStateString(Request& req, Response& resp) { { RS_STACK_MUTEX(mMtx); - resp.mStateToken = mStateToken; } std::string state_string; @@ -210,7 +209,6 @@ void PeersHandler::handleSetStateString(Request& req, Response& resp) { { RS_STACK_MUTEX(mMtx); - resp.mStateToken = mStateToken; } std::string state_string; @@ -232,7 +230,6 @@ void PeersHandler::handleGetCustomStateString(Request& req, Response& resp) { { RS_STACK_MUTEX(mMtx); - resp.mStateToken = mStateToken; } std::string custom_state_string = rsMsgs->getCustomStateString(); @@ -244,7 +241,6 @@ void PeersHandler::handleSetCustomStateString(Request& req, Response& resp) { { RS_STACK_MUTEX(mMtx); - resp.mStateToken = mStateToken; } std::string custom_state_string; From 569458a1c1847f42dca8556e92a9275af1a13a4b Mon Sep 17 00:00:00 2001 From: Konrad Date: Tue, 28 Mar 2017 11:18:27 +0200 Subject: [PATCH 12/18] Changed: Qt dependecies are optional --- libresapi/src/api/ApiServer.cpp | 15 +++++++-- libresapi/src/libresapi.pro | 55 ++++++++++++++++++--------------- retroshare.pri | 6 ++++ 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index 6efadeaf6..327dd6260 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -17,7 +17,9 @@ #include "ChannelsHandler.h" #include "StatsHandler.h" -#include "SettingsHandler.h" +#ifdef LIBRESAPI_QT + #include "SettingsHandler.h" +#endif /* data types in json http://json.org/ @@ -238,8 +240,10 @@ public: mChatHandler(sts, ifaces.mNotify, ifaces.mMsgs, ifaces.mPeers, ifaces.mIdentity, &mPeersHandler), mApiPluginHandler(sts, ifaces), mChannelsHandler(ifaces.mGxsChannels), - mStatsHandler(), - mSettingsHandler(sts) + mStatsHandler() +#ifdef LIBRESAPI_QT + ,mSettingsHandler(sts) +#endif { // the dynamic cast is to not confuse the addResourceHandler template like this: // addResourceHandler(derived class, parent class) @@ -265,8 +269,10 @@ public: &ChannelsHandler::handleRequest); router.addResourceHandler("stats", dynamic_cast(&mStatsHandler), &StatsHandler::handleRequest); +#ifdef LIBRESAPI_QT router.addResourceHandler("settings", dynamic_cast(&mSettingsHandler), &SettingsHandler::handleRequest); +#endif } PeersHandler mPeersHandler; @@ -279,7 +285,10 @@ public: ApiPluginHandler mApiPluginHandler; ChannelsHandler mChannelsHandler; StatsHandler mStatsHandler; + +#ifdef LIBRESAPI_QT SettingsHandler mSettingsHandler; +#endif }; ApiServer::ApiServer(): diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 33c4f726c..e548ea9d0 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -3,8 +3,7 @@ TEMPLATE = lib CONFIG += staticlib CONFIG += create_prl -CONFIG += qt -QT += core +CONFIG -= qt TARGET = resapi TARGET_PRL = libresapi DESTDIR = lib @@ -13,32 +12,32 @@ INCLUDEPATH += ../../libretroshare/src retroshare_android_service { win32 { - OBJECTS_DIR = temp/obj + OBJECTS_DIR = temp/obj - LIBS_DIR = $$PWD/../../libs/lib - LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a + LIBS_DIR = $$PWD/../../libs/lib + LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a + LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" + for(lib, LIB_DIR):LIBS += -L"$$lib" + for(bin, BIN_DIR):LIBS += -L"$$bin" - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm + LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 + LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 + LIBS += -lwinmm - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T + DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR + DEPENDPATH += . $$INC_DIR + INCLUDEPATH += . $$INC_DIR - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src - } else { - # Qt 4 - QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src - } + greaterThan(QT_MAJOR_VERSION, 4) { + # Qt 5 + RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src + } else { + # Qt 4 + QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src + } } DEPENDPATH += . ../../libretroshare/src/ @@ -183,8 +182,7 @@ SOURCES += \ util/ContentTypes.cpp \ api/ApiPluginHandler.cpp \ api/ChannelsHandler.cpp \ - api/StatsHandler.cpp \ - api/SettingsHandler.cpp + api/StatsHandler.cpp HEADERS += \ api/ApiServer.h \ @@ -211,8 +209,7 @@ HEADERS += \ util/ContentTypes.h \ api/ApiPluginHandler.h \ api/ChannelsHandler.h \ - api/StatsHandler.h \ - api/SettingsHandler.h + api/StatsHandler.h libresapilocalserver { CONFIG *= qt @@ -220,3 +217,11 @@ libresapilocalserver { SOURCES *= api/ApiServerLocal.cpp HEADERS *= api/ApiServerLocal.h } + +qt_dependencies { + CONFIG *= qt + QT *= core + + SOURCES += api/SettingsHandler.cpp + HEADERS += api/SettingsHandler.h +} diff --git a/retroshare.pri b/retroshare.pri index 498131aea..e27348311 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -29,6 +29,11 @@ retroshare_qml_app:CONFIG -= no_retroshare_qml_app CONFIG *= no_libresapilocalserver libresapilocalserver:CONFIG -= no_libresapilocalserver +# To enable Qt dependencies in libresapi append the following +# assignation to qmake command line "CONFIG+=qt_dependencies" +CONFIG *= no_qt_dependencies +qt_dependencies:CONFIG -= no_qt_dependencies + # To disable libresapi via HTTP (based on libmicrohttpd) append the following # assignation to qmake command line "CONFIG+=no_libresapihttpserver" CONFIG *= libresapihttpserver @@ -155,6 +160,7 @@ unfinished { wikipoos:DEFINES *= RS_USE_WIKI rs_gxs:DEFINES *= RS_ENABLE_GXS libresapilocalserver:DEFINES *= LIBRESAPI_LOCAL_SERVER +qt_dependencies:DEFINES *= LIBRESAPI_QT libresapihttpserver:DEFINES *= ENABLE_WEBUI sqlcipher:DEFINES -= NO_SQLCIPHER no_sqlcipher:DEFINES *= NO_SQLCIPHER From efd6ce3ed518af7462689ed467fda66cdf6c0831 Mon Sep 17 00:00:00 2001 From: Konrad Date: Tue, 28 Mar 2017 11:19:50 +0200 Subject: [PATCH 13/18] Changed: Simplified asking for deferred self signature --- libresapi/src/api/RsControlModule.cpp | 115 ++------------------------ libresapi/src/api/RsControlModule.h | 5 -- 2 files changed, 6 insertions(+), 114 deletions(-) diff --git a/libresapi/src/api/RsControlModule.cpp b/libresapi/src/api/RsControlModule.cpp index 228bb691a..595509f09 100644 --- a/libresapi/src/api/RsControlModule.cpp +++ b/libresapi/src/api/RsControlModule.cpp @@ -458,120 +458,17 @@ void RsControlModule::handleCreateLocation(Request &req, Response &resp) resp.setFail("could not create a new location. Error: "+err_string); } -class SignatureEventData -{ - public: - SignatureEventData(const void *_data,int32_t _len,unsigned int _signlen, std::string _reason) - { - // We need a new memory chnk because there's no guarranty _sign nor _signlen are not in the stack - - sign = (unsigned char *)rs_malloc(_signlen); - - if(!sign) - { - signlen = NULL; - signature_result = SELF_SIGNATURE_RESULT_FAILED; - return; - } - - signlen = new unsigned int; - *signlen = _signlen; - signature_result = SELF_SIGNATURE_RESULT_PENDING; - data = rs_malloc(_len); - - if(!data) - { - len = 0; - return; - } - len = _len; - memcpy(data,_data,len); - reason = _reason; - } - - ~SignatureEventData() - { - free(sign); - delete signlen; - free(data); - } - - void performSignature() - { - if(rsPeers->gpgSignData(data,len,sign,signlen,reason)) - signature_result = SELF_SIGNATURE_RESULT_SUCCESS; - else - signature_result = SELF_SIGNATURE_RESULT_FAILED; - } - - void *data; - uint32_t len; - unsigned char *sign; - unsigned int *signlen; - int signature_result; // 0=pending, 1=done, 2=failed/cancelled. - std::string reason; -}; - bool RsControlModule::askForDeferredSelfSignature(const void *data, const uint32_t len, unsigned char *sign, unsigned int *signlen,int& signature_result, std::string reason /*=""*/) { - + if(rsPeers->gpgSignData(data,len,sign,signlen,reason)) { - RsStackMutex stack(mDataMtx); - std::cerr << "NotifyTxt:: deferred signature event requeted. " << std::endl; - - // Look into the queue - - Sha1CheckSum chksum = RsDirUtil::sha1sum((uint8_t*)data,len); - - std::map::iterator it = _deferred_signature_queue.find(chksum.toStdString()); - signature_result = SELF_SIGNATURE_RESULT_PENDING; - - if(it != _deferred_signature_queue.end()) - { - signature_result = it->second->signature_result; - - if(it->second->signature_result != SELF_SIGNATURE_RESULT_PENDING) // found it. Copy the result, and remove from the queue. - { - // We should check for the exact data match, for the sake of being totally secure. - // - std::cerr << "Found into queue: returning it" << std::endl; - - memcpy(sign,it->second->sign,*it->second->signlen); - *signlen = *(it->second->signlen); - - delete it->second; - _deferred_signature_queue.erase(it); - } - - return true; // already registered, but not done yet. - } - - // Not found. Store in the queue and emit a signal. - // - std::cerr << "NotifyTxt:: deferred signature event requeted. Pushing into queue" << std::endl; - - SignatureEventData *edta = new SignatureEventData(data,len,*signlen, reason); - - _deferred_signature_queue[chksum.toStdString()] = edta; + signature_result = SELF_SIGNATURE_RESULT_SUCCESS; + return true; } - handleSignatureEvent(); - return true; -} - -void RsControlModule::handleSignatureEvent() -{ - std::cerr << "NotifyTxt:: performing a deferred signature in the main GUI thread." << std::endl; - - static bool working = false ; - - if(!working) + else { - working = true ; - - for(std::map::const_iterator it(_deferred_signature_queue.begin());it!=_deferred_signature_queue.end();++it) - it->second->performSignature() ; - - working = false ; + signature_result = SELF_SIGNATURE_RESULT_FAILED; + return false; } } diff --git a/libresapi/src/api/RsControlModule.h b/libresapi/src/api/RsControlModule.h index 338027e15..1323651fc 100644 --- a/libresapi/src/api/RsControlModule.h +++ b/libresapi/src/api/RsControlModule.h @@ -9,8 +9,6 @@ namespace resource_api{ class StateTokenServer; class ApiServer; -class SignatureEventData; - // resource api module to control accounts, startup and shutdown of retroshare // - this module handles everything, no things are required from outside // - exception: users of this module have to create an api server and register this module @@ -87,9 +85,6 @@ private: // we store the password in this variable, it has higher priority than the normal password variable // it is also to avoid a lock, when we make a synchronous call into librs, like in ssl cert generation std::string mFixedPassword; - - void handleSignatureEvent(); - std::map _deferred_signature_queue ; }; } // namespace resource_api From 6e530027f8fec09479446316a1bbce92ac749e9d Mon Sep 17 00:00:00 2001 From: Konrad Date: Tue, 28 Mar 2017 11:20:51 +0200 Subject: [PATCH 14/18] Added: tokens in PeersHandler --- libresapi/src/api/PeersHandler.cpp | 37 +++++++++++++++++++++--------- libresapi/src/api/PeersHandler.h | 5 ++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/libresapi/src/api/PeersHandler.cpp b/libresapi/src/api/PeersHandler.cpp index b8fdc913d..f48beb2f6 100644 --- a/libresapi/src/api/PeersHandler.cpp +++ b/libresapi/src/api/PeersHandler.cpp @@ -116,9 +116,9 @@ PeersHandler::PeersHandler(StateTokenServer* sts, RsNotify* notify, RsPeers *pee mStateTokenServer->registerTickClient(this); addResourceHandler("*", this, &PeersHandler::handleWildcard); addResourceHandler("get_state_string", this, &PeersHandler::handleGetStateString); - addResourceHandler("set_state_string", this, &PeersHandler::handleSetStateString); - addResourceHandler("get_custom_state_string", this, &PeersHandler::handleGetCustomStateString); - addResourceHandler("set_custom_state_string", this, &PeersHandler::handleSetCustomStateString); + addResourceHandler("set_state_string", this, &PeersHandler::handleSetStateString); + addResourceHandler("get_custom_state_string", this, &PeersHandler::handleGetCustomStateString); + addResourceHandler("set_custom_state_string", this, &PeersHandler::handleSetCustomStateString); addResourceHandler("examine_cert", this, &PeersHandler::handleExamineCert); } @@ -156,6 +156,27 @@ void PeersHandler::tick() mStateTokenServer->discardToken(mStateToken); mStateToken = mStateTokenServer->getNewToken(); } + + StatusInfo statusInfo; + rsStatus->getOwnStatus(statusInfo); + if(statusInfo.status != status) + { + status = statusInfo.status; + + RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ + mStateTokenServer->discardToken(mStringStateToken); + mStringStateToken = mStateTokenServer->getNewToken(); + } + + std::string custom_state = rsMsgs->getCustomStateString(); + if(custom_state != custom_state_string) + { + custom_state_string = custom_state; + + RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ + mStateTokenServer->discardToken(mCustomStateToken); + mCustomStateToken = mStateTokenServer->getNewToken(); + } } void PeersHandler::notifyUnreadMsgCountChanged(const RsPeerId &peer, uint32_t count) @@ -181,6 +202,7 @@ void PeersHandler::handleGetStateString(Request& req, Response& resp) { { RS_STACK_MUTEX(mMtx); + resp.mStateToken = mStringStateToken; } std::string state_string; @@ -207,10 +229,6 @@ void PeersHandler::handleGetStateString(Request& req, Response& resp) void PeersHandler::handleSetStateString(Request& req, Response& resp) { - { - RS_STACK_MUTEX(mMtx); - } - std::string state_string; req.mStream << makeKeyValueReference("state_string", state_string); @@ -230,6 +248,7 @@ void PeersHandler::handleGetCustomStateString(Request& req, Response& resp) { { RS_STACK_MUTEX(mMtx); + resp.mStateToken = mCustomStateToken; } std::string custom_state_string = rsMsgs->getCustomStateString(); @@ -239,10 +258,6 @@ void PeersHandler::handleGetCustomStateString(Request& req, Response& resp) void PeersHandler::handleSetCustomStateString(Request& req, Response& resp) { - { - RS_STACK_MUTEX(mMtx); - } - std::string custom_state_string; req.mStream << makeKeyValueReference("custom_state_string", custom_state_string); diff --git a/libresapi/src/api/PeersHandler.h b/libresapi/src/api/PeersHandler.h index a2eab706b..15d231c10 100644 --- a/libresapi/src/api/PeersHandler.h +++ b/libresapi/src/api/PeersHandler.h @@ -50,9 +50,14 @@ private: RsMsgs* mRsMsgs; // required for avatar data std::list mOnlinePeers; + uint32_t status; + std::string custom_state_string; RsMutex mMtx; StateToken mStateToken; // mutex protected + StateToken mStringStateToken; // mutex protected + StateToken mCustomStateToken; // mutex protected + std::map mUnreadMsgsCounts; }; } // namespace resource_api From 22956be00f1fd9a79dd6b95295eeda762847df51 Mon Sep 17 00:00:00 2001 From: Konrad Date: Tue, 28 Mar 2017 11:24:16 +0200 Subject: [PATCH 15/18] Deleted: functions for hadling particular lobbies types in ChatHandler --- libresapi/src/api/ChatHandler.cpp | 88 ------------------------------- libresapi/src/api/ChatHandler.h | 4 -- 2 files changed, 92 deletions(-) diff --git a/libresapi/src/api/ChatHandler.cpp b/libresapi/src/api/ChatHandler.cpp index 68f0bd356..511993a29 100644 --- a/libresapi/src/api/ChatHandler.cpp +++ b/libresapi/src/api/ChatHandler.cpp @@ -159,10 +159,6 @@ ChatHandler::ChatHandler(StateTokenServer *sts, RsNotify *notify, RsMsgs *msgs, addResourceHandler("initiate_distant_chat", this, &ChatHandler::handleInitiateDistantChatConnexion); addResourceHandler("distant_chat_status", this, &ChatHandler::handleDistantChatStatus); addResourceHandler("close_distant_chat", this, &ChatHandler::handleCloseDistantChatConnexion); - - addResourceHandler("private_lobbies", this, &ChatHandler::handlePrivateLobbies); - addResourceHandler("subscribed_public_lobbies", this, &ChatHandler::handleSubscribedPublicLobbies); - addResourceHandler("unsubscribed_public_lobbies", this, &ChatHandler::handleUnsubscribedPublicLobbies); } ChatHandler::~ChatHandler() @@ -1204,88 +1200,4 @@ void ChatHandler::handleCloseDistantChatConnexion(Request& req, Response& resp) else resp.setFail("Failed to close distant chat"); } -void ChatHandler::handlePrivateLobbies(Request &req, Response &resp) -{ - tick(); - - { - RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ - resp.mDataStream.getStreamToMember(); - for(std::vector::iterator vit = mLobbies.begin(); vit != mLobbies.end(); ++vit) - { - if(!vit->is_private) - continue; - uint32_t unread_msgs = 0; - ChatId chat_id(vit->id); - std::map >::iterator mit = mMsgs.find(chat_id); - if(mit != mMsgs.end()) - { - std::list& msgs = mit->second; - for(std::list::iterator lit = msgs.begin(); lit != msgs.end(); ++lit) - if(!lit->read) - unread_msgs++; - } - resp.mDataStream.getStreamToMember() << *vit << makeKeyValueReference("unread_msg_count", unread_msgs); - } - resp.mStateToken = mLobbiesStateToken; - } - resp.setOk(); -} - -void ChatHandler::handleSubscribedPublicLobbies(Request &req, Response &resp) -{ - tick(); - - { - RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ - resp.mDataStream.getStreamToMember(); - for(std::vector::iterator vit = mLobbies.begin(); vit != mLobbies.end(); ++vit) - { - if(!vit->subscribed || vit->is_private || vit->is_broadcast) - continue; - uint32_t unread_msgs = 0; - ChatId chat_id(vit->id); - std::map >::iterator mit = mMsgs.find(chat_id); - if(mit != mMsgs.end()) - { - std::list& msgs = mit->second; - for(std::list::iterator lit = msgs.begin(); lit != msgs.end(); ++lit) - if(!lit->read) - unread_msgs++; - } - resp.mDataStream.getStreamToMember() << *vit << makeKeyValueReference("unread_msg_count", unread_msgs); - } - resp.mStateToken = mLobbiesStateToken; - } - resp.setOk(); -} - -void ChatHandler::handleUnsubscribedPublicLobbies(Request &req, Response &resp) -{ - tick(); - - { - RS_STACK_MUTEX(mMtx); /********** LOCKED **********/ - resp.mDataStream.getStreamToMember(); - for(std::vector::iterator vit = mLobbies.begin(); vit != mLobbies.end(); ++vit) - { - if(vit->subscribed || vit->is_private) - continue; - uint32_t unread_msgs = 0; - ChatId chat_id(vit->id); - std::map >::iterator mit = mMsgs.find(chat_id); - if(mit != mMsgs.end()) - { - std::list& msgs = mit->second; - for(std::list::iterator lit = msgs.begin(); lit != msgs.end(); ++lit) - if(!lit->read) - unread_msgs++; - } - resp.mDataStream.getStreamToMember() << *vit << makeKeyValueReference("unread_msg_count", unread_msgs); - } - resp.mStateToken = mLobbiesStateToken; - } - resp.setOk(); -} - } // namespace resource_api diff --git a/libresapi/src/api/ChatHandler.h b/libresapi/src/api/ChatHandler.h index 056629c06..dc075c77f 100644 --- a/libresapi/src/api/ChatHandler.h +++ b/libresapi/src/api/ChatHandler.h @@ -133,10 +133,6 @@ private: void handleDistantChatStatus(Request& req, Response& resp); void handleCloseDistantChatConnexion(Request& req, Response& resp); - void handlePrivateLobbies(Request &req, Response &resp); - void handleSubscribedPublicLobbies(Request &req, Response &resp); - void handleUnsubscribedPublicLobbies(Request &req, Response &resp); - void getPlainText(const std::string& in, std::string &out, std::vector &links); // last parameter is only used for lobbies! void locked_storeTypingInfo(const ChatId& chat_id, std::string status, RsGxsId lobby_gxs_id = RsGxsId()); From fbc1bb4e823209ad534f75552f06264fd4b69a9d Mon Sep 17 00:00:00 2001 From: Konrad Date: Wed, 29 Mar 2017 16:06:39 +0200 Subject: [PATCH 16/18] Added: function for handling autosubscribing lobbies --- libresapi/src/api/ChatHandler.cpp | 10 ++++++++++ libresapi/src/api/ChatHandler.h | 1 + 2 files changed, 11 insertions(+) diff --git a/libresapi/src/api/ChatHandler.cpp b/libresapi/src/api/ChatHandler.cpp index 511993a29..f576f8601 100644 --- a/libresapi/src/api/ChatHandler.cpp +++ b/libresapi/src/api/ChatHandler.cpp @@ -147,6 +147,7 @@ ChatHandler::ChatHandler(StateTokenServer *sts, RsNotify *notify, RsMsgs *msgs, addResourceHandler("lobbies", this, &ChatHandler::handleLobbies); addResourceHandler("subscribe_lobby", this, &ChatHandler::handleSubscribeLobby); addResourceHandler("unsubscribe_lobby", this, &ChatHandler::handleUnsubscribeLobby); + addResourceHandler("autosubscribe_lobby", this, &ChatHandler::handleAutoSubsribeLobby); addResourceHandler("clear_lobby", this, &ChatHandler::handleClearLobby); addResourceHandler("lobby_participants", this, &ChatHandler::handleLobbyParticipants); addResourceHandler("messages", this, &ChatHandler::handleMessages); @@ -890,6 +891,15 @@ void ChatHandler::handleUnsubscribeLobby(Request &req, Response &resp) resp.setOk(); } +void ChatHandler::handleAutoSubsribeLobby(Request& req, Response& resp) +{ + ChatLobbyId chatId = 0; + bool autosubsribe; + req.mStream << makeKeyValueReference("chatid", chatId) << makeKeyValueReference("autosubsribe", autosubsribe); + mRsMsgs->setLobbyAutoSubscribe(chatId, autosubsribe); + resp.setOk(); +} + void ChatHandler::handleClearLobby(Request &req, Response &resp) { ChatLobbyId id = 0; diff --git a/libresapi/src/api/ChatHandler.h b/libresapi/src/api/ChatHandler.h index dc075c77f..3463cee57 100644 --- a/libresapi/src/api/ChatHandler.h +++ b/libresapi/src/api/ChatHandler.h @@ -120,6 +120,7 @@ private: void handleLobbies(Request& req, Response& resp); void handleSubscribeLobby(Request& req, Response& resp); void handleUnsubscribeLobby(Request& req, Response& resp); + void handleAutoSubsribeLobby(Request& req, Response& resp); void handleClearLobby(Request& req, Response& resp); ResponseTask* handleLobbyParticipants(Request& req, Response& resp); void handleMessages(Request& req, Response& resp); From 8543525c77f6c54f44ef58221c0f07b3c6cee69c Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 1 Apr 2017 14:28:55 +0200 Subject: [PATCH 17/18] Fixed: Linker problem --- .../src/retroshare-android-service.pro | 20 +++++++++---------- retroshare-android-service/src/service.cpp | 2 -- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/retroshare-android-service/src/retroshare-android-service.pro b/retroshare-android-service/src/retroshare-android-service.pro index e45067f81..54843d34f 100644 --- a/retroshare-android-service/src/retroshare-android-service.pro +++ b/retroshare-android-service/src/retroshare-android-service.pro @@ -11,6 +11,16 @@ android-g++:CONFIG += dll android-g++:TEMPLATE = lib !android-g++:TEMPLATE = app +DEPENDPATH *= ../../libresapi/src +INCLUDEPATH *= ../../libresapi/src +PRE_TARGETDEPS *= ../../libresapi/src/lib/libresapi.a +LIBS *= ../../libresapi/src/lib/libresapi.a + +DEPENDPATH *= ../../libretroshare/src +INCLUDEPATH *= ../../libretroshare/src +PRE_TARGETDEPS *= ../../libretroshare/src/lib/libretroshare.a +LIBS *= ../../libretroshare/src/lib/libretroshare.a + win32 { OBJECTS_DIR = temp/obj @@ -41,13 +51,3 @@ win32 { } SOURCES += service.cpp - -DEPENDPATH *= ../../libresapi/src -INCLUDEPATH *= ../../libresapi/src -PRE_TARGETDEPS *= ../../libresapi/src/lib/libresapi.a -LIBS *= ../../libresapi/src/lib/libresapi.a - -DEPENDPATH *= ../../libretroshare/src -INCLUDEPATH *= ../../libretroshare/src -PRE_TARGETDEPS *= ../../libretroshare/src/lib/libretroshare.a -LIBS *= ../../libretroshare/src/lib/libretroshare.a diff --git a/retroshare-android-service/src/service.cpp b/retroshare-android-service/src/service.cpp index afe73bfc3..c822e8718 100644 --- a/retroshare-android-service/src/service.cpp +++ b/retroshare-android-service/src/service.cpp @@ -40,8 +40,6 @@ int main(int argc, char *argv[]) QCoreApplication a(argc, argv); - RsInit::InitRsConfig(); - ApiServer api; RsControlModule ctrl_mod(argc, argv, api.getStateTokenServer(), &api, true); api.addResourceHandler("control", dynamic_cast(&ctrl_mod), &resource_api::RsControlModule::handleRequest); From 6e65fcd6acf95e32a2ecde450abe67248888a660 Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 1 Apr 2017 14:32:49 +0200 Subject: [PATCH 18/18] Removed: Unused #include statement --- retroshare-android-service/src/service.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/retroshare-android-service/src/service.cpp b/retroshare-android-service/src/service.cpp index c822e8718..84ec62301 100644 --- a/retroshare-android-service/src/service.cpp +++ b/retroshare-android-service/src/service.cpp @@ -24,12 +24,10 @@ # include "util/androiddebug.h" #endif -#include "retroshare/rsinit.h" #include "api/ApiServer.h" #include "api/ApiServerLocal.h" #include "api/RsControlModule.h" - using namespace resource_api; int main(int argc, char *argv[])