Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalyster committed Mar 30, 2023
1 parent e55d3e1 commit cf3677e
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 551 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ option(ENABLE_SMSTOOLS3 "Build SMSTools3 plugin" ON)
option(ENABLE_XMPP "Build XMPP plugin" ON)
option(ENABLE_TWITTER "Build Twitter plugin" ON)
option(ENABLE_WEBUI "Build Web UI" ON)

option(ENABLE_SLACK_FRONTEND "Build Slack frontend" OFF)
option(ENABLE_DOCS "Build Docs" ON)
# option(ENABLE_LOG "Build with logging using Log4cxx" ON)
option(ENABLE_TESTS "Build Tests using CppUnit" OFF)
Expand Down
8 changes: 4 additions & 4 deletions backends/libcommuni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ add_executable(spectrum2_libcommuni_backend ${SRC})

if(NOT WIN32)
if(ENABLE_QT4)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport pthread)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin pthread)
else()
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport pthread)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin pthread)
endif()
else()
if(ENABLE_QT4)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin)
else()
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport)
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin)
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions backends/swiften/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ file(GLOB SRC *.cpp)
add_executable(spectrum2_swiften_backend ${SRC})

if(NOT WIN32)
target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_swiften_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
else()
target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_swiften_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
endif()

install(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})
30 changes: 3 additions & 27 deletions backends/swiften/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,15 @@ class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient {
Swift::XMPPParser *m_xmppParser;
Swift::FullPayloadParserFactoryCollection m_collection2;

SwiftenPlugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
SwiftenPlugin(Config *config, const std::string &host, int port) : NetworkPlugin() {
this->config = config;
m_firstPing = true;
m_factories = new Swift::BoostNetworkFactories(loop);
m_conn = m_factories->getConnectionFactory()->createConnection();
m_conn->onDataRead.connect(boost::bind(&SwiftenPlugin::_handleDataRead, this, _1));
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));
serializer = new Swift::XMPPSerializer(&collection, Swift::ClientStreamType, false);
m_xmppParser = new Swift::XMPPParser(this, &m_collection2, m_factories->getXMLParserFactory());
m_xmppParser->parse("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='localhost' version='1.0'>");

LOG4CXX_INFO(logger, "Starting the plugin.");
}

// NetworkPlugin uses this method to send the data to networkplugin server
void sendData(const std::string &string) {
m_conn->write(Swift::createSafeByteArray(string));
}

// This method has to call handleDataRead with all received data from network plugin server
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data) {
if (m_firstPing) {
m_firstPing = false;
NetworkPlugin::PluginConfig cfg;
cfg.setRawXML(true);
cfg.setNeedRegistration(false);
sendConfig(cfg);
}
std::string d(data->begin(), data->end());
handleDataRead(d);
connect(host, std::to_string(port));
}

void handleStreamStart(const Swift::ProtocolHeader&) {}
Expand Down Expand Up @@ -446,10 +425,7 @@ int main (int argc, char* argv[]) {

Logging::initBackendLogging(cfg);

Swift::SimpleEventLoop eventLoop;
loop_ = &eventLoop;
np = new SwiftenPlugin(cfg, &eventLoop, host, port);
loop_->run();
np = new SwiftenPlugin(cfg, host, port);

return 0;
}
6 changes: 3 additions & 3 deletions backends/template/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ add_executable(spectrum2_template_backend ${SRC})

if(CMAKE_COMPILER_IS_GNUCXX)
if(NOT WIN32)
target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_template_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
else()
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_template_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
endif()
else()
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_template_backend transport-plugin ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
endif()

#install(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin)
8 changes: 2 additions & 6 deletions backends/template/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include "transport/NetworkPlugin.h"
#include "transport/Logging.h"

// Swiften
#include "Swiften/Swiften.h"

#ifndef _WIN32
// for signal handler
#include "unistd.h"
Expand All @@ -17,6 +14,7 @@
#endif
// Boost
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;
Expand Down Expand Up @@ -60,9 +58,7 @@ int main (int argc, char* argv[]) {

Logging::initBackendLogging(cfg);

Swift::SimpleEventLoop eventLoop;
Plugin p(cfg, &eventLoop, host, port);
eventLoop.run();
Plugin p(cfg, host, port);

return 0;
}
23 changes: 3 additions & 20 deletions backends/template/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,19 @@
#include "transport/NetworkPlugin.h"
#include "transport/Logging.h"

// Swiften
#include "Swiften/Swiften.h"

// Boost
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace boost::program_options;
using namespace Transport;

DEFINE_LOGGER(logger, "Backend Template");

Plugin::Plugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
Plugin::Plugin(Config *config, const std::string &host, int port) : NetworkPlugin() {
this->config = config;
m_factories = new Swift::BoostNetworkFactories(loop);
m_conn = m_factories->getConnectionFactory()->createConnection();
m_conn->onDataRead.connect(boost::bind(&Plugin::_handleDataRead, this, _1));
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));

LOG4CXX_INFO(logger, "Starting the plugin.");
}

// NetworkPlugin uses this method to send the data to networkplugin server
void Plugin::sendData(const std::string &string) {
m_conn->write(Swift::createSafeByteArray(string));
}

// This method has to call handleDataRead with all received data from network plugin server
void Plugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data) {
std::string d(data->begin(), data->end());
handleDataRead(d);
connect(host, std::to_string(port));
}

void Plugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map<std::string, std::string> &settings) {
Expand Down
11 changes: 1 addition & 10 deletions backends/template/plugin.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#pragma once

#include "Swiften/Swiften.h"

#include "transport/Config.h"
#include "transport/NetworkPlugin.h"

class Plugin : public Transport::NetworkPlugin {
public:
Plugin(Transport::Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port);
Plugin(Transport::Config *config, const std::string &host, int port);

// NetworkPlugin uses this method to send the data to networkplugin server
void sendData(const std::string &string);
Expand All @@ -23,12 +21,5 @@ class Plugin : public Transport::NetworkPlugin {
void handleBuddyRemovedRequest(const std::string &user, const std::string &buddyName, const std::vector<std::string> &groups);

private:
// This method has to call handleDataRead with all received data from network plugin server
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data);

private:
Swift::BoostNetworkFactories *m_factories;
Swift::BoostIOServiceThread m_boostIOServiceThread;
std::shared_ptr<Swift::Connection> m_conn;
Transport::Config *config;
};
2 changes: 1 addition & 1 deletion backends/twitter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ find_package(CURL)

if(CURL_FOUND)
message(STATUS "Using curl ${CURL_VERSION_STRING}: ${CURL_INCLUDE_DIRS} ${CURL_LIBRARIES}")
target_link_libraries(spectrum2_twitter_backend transport JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
target_link_libraries(spectrum2_twitter_backend transport-plugin ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
else()
message(FATAL_ERROR "curl not found")
endif()
Expand Down
37 changes: 0 additions & 37 deletions backends/twitter/HTTPRequest.h

This file was deleted.

2 changes: 1 addition & 1 deletion backends/twitter/Requests/ProfileImageRequest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ProfileImageRequest.h"
#include "../HTTPRequest.h"
#include "transport/HTTPRequest.h"
DEFINE_LOGGER(profileImageRequestLogger, "ProfileImageRequest")
void ProfileImageRequest::run()
{
Expand Down
80 changes: 36 additions & 44 deletions backends/twitter/TwitterPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
#include "Requests/DestroyFriendRequest.h"
#include "Requests/RetweetRequest.h"
#include "Requests/ProfileImageRequest.h"
#include "Swiften/StringCodecs/Hexify.h"

#include <boost/uuid/detail/sha1.hpp>

#include <memory>

DEFINE_LOGGER(logger, "Twitter Backend");

TwitterPlugin *np = NULL;
Swift::SimpleEventLoop *loop_; // Event Loop

const std::string OLD_APP_KEY = "PCWAdQpyyR12ezp2fVwEhw";
const std::string OLD_APP_SECRET = "EveLmCXJIg2R7BTCpm6OWV8YyX49nI0pxnYXh7JMvDg";
Expand All @@ -34,8 +36,26 @@ static int cmp(std::string a, std::string b)
return 1;
}

std::string get_sha1(const std::string& p_arg)
{
boost::uuids::detail::sha1 sha1;
sha1.process_bytes(p_arg.data(), p_arg.size());
unsigned hash[5] = {0};
sha1.get_digest(hash);

// Back to string
char buf[41] = {0};

for (int i = 0; i < 5; i++)
{
std::sprintf(buf + (i << 3), "%08x", hash[i]);
}

return std::string(buf);
}


TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
TwitterPlugin::TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
{
this->config = config;
this->storagebackend = storagebackend;
Expand Down Expand Up @@ -68,56 +88,24 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
OAUTH_SECRET = "twitter_oauth_secret";
MODE = "mode";

m_factories = new Swift::BoostNetworkFactories(loop);
m_conn = m_factories->getConnectionFactory()->createConnection();
m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));

tp = new ThreadPool(loop_, 10);

LOG4CXX_INFO(logger, "Fetch timeout is set to " << CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
tweet_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
message_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));

tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
//message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
io = std::make_shared<boost::asio::io_service>();
tweet_timer = std::make_shared<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));
message_timer = std::make_shared<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));

tweet_timer->start();
message_timer->start();
cryptoProvider = std::shared_ptr<Swift::CryptoProvider>(Swift::PlatformCryptoProvider::create());
tweet_timer->async_wait(boost::bind(&TwitterPlugin::pollForTweets, this));
//message_timer->async_wait(boost::bind(&TwitterPlugin::pollForDirectMessages, this));


LOG4CXX_INFO(logger, "Starting the plugin.");
connect(host, std::to_string(port));
}

TwitterPlugin::~TwitterPlugin()
{
delete storagebackend;
std::set<std::string>::iterator it;
for (it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
delete tp;
}

// Send data to NetworkPlugin server
void TwitterPlugin::sendData(const std::string &string)
{
m_conn->write(Swift::createSafeByteArray(string));
}

// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
void TwitterPlugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data)
{
if (m_firstPing) {
m_firstPing = false;
// Users can join the network without registering if we allow
// one user to connect multiple IRC networks.
NetworkPlugin::PluginConfig cfg;
cfg.setNeedPassword(false);
sendConfig(cfg);
}

std::string d(data->begin(), data->end());
handleDataRead(d);
}

// User trying to login into his twitter account
Expand Down Expand Up @@ -655,9 +643,13 @@ void TwitterPlugin::populateRoster(std::string &user, std::vector<User> &friends

if(userdb[user].twitterMode == MULTIPLECONTACT) {
std::string lastTweet = friends[i].getLastStatus().getTweet();
//LOG4CXX_INFO(logger, user << " - " << SHA(friendAvatars[i]))

auto friendAvatarHash = get_sha1(friendAvatars[i]);

LOG4CXX_INFO(logger, user << " - " << friendAvatarHash);

handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(),
pbnetwork::STATUS_ONLINE, lastTweet, Swift::Hexify::hexify(cryptoProvider->getSHA1Hash(Swift::createByteArray(friendAvatars[i]))));
pbnetwork::STATUS_ONLINE, lastTweet, friendAvatarHash);
}
else if(userdb[user].twitterMode == CHATROOM)
handleParticipantChanged(user, friends[i].getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
Expand Down Expand Up @@ -831,7 +823,7 @@ void TwitterPlugin::createFriendResponse(std::string &user, User &frnd, std::str

LOG4CXX_INFO(logger, user << " - " << frnd.getScreenName() << ", " << frnd.getProfileImgURL());
if(userdb[user].twitterMode == MULTIPLECONTACT) {
handleBuddyChanged(user, frnd.getScreenName(), frnd.getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE, "", Swift::byteArrayToString(cryptoProvider->getSHA1Hash(Swift::createByteArray(img))));
handleBuddyChanged(user, frnd.getScreenName(), frnd.getUserName(), std::vector<std::string>(), pbnetwork::STATUS_ONLINE, "", get_sha1(img));
} else if(userdb[user].twitterMode == CHATROOM) {
handleParticipantChanged(user, frnd.getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
}
Expand Down
Loading

0 comments on commit cf3677e

Please sign in to comment.