Skip to content

Commit cf3677e

Browse files
committed
WIP
1 parent e55d3e1 commit cf3677e

29 files changed

+146
-551
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ option(ENABLE_SMSTOOLS3 "Build SMSTools3 plugin" ON)
4141
option(ENABLE_XMPP "Build XMPP plugin" ON)
4242
option(ENABLE_TWITTER "Build Twitter plugin" ON)
4343
option(ENABLE_WEBUI "Build Web UI" ON)
44-
44+
option(ENABLE_SLACK_FRONTEND "Build Slack frontend" OFF)
4545
option(ENABLE_DOCS "Build Docs" ON)
4646
# option(ENABLE_LOG "Build with logging using Log4cxx" ON)
4747
option(ENABLE_TESTS "Build Tests using CppUnit" OFF)

backends/libcommuni/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ add_executable(spectrum2_libcommuni_backend ${SRC})
1313

1414
if(NOT WIN32)
1515
if(ENABLE_QT4)
16-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport pthread)
16+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin pthread)
1717
else()
18-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport pthread)
18+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin pthread)
1919
endif()
2020
else()
2121
if(ENABLE_QT4)
22-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport)
22+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt4::QtNetwork Qt4::QtCore transport-plugin)
2323
else()
24-
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport)
24+
target_link_libraries(spectrum2_libcommuni_backend ${IRC_LIBRARY} Qt5::Network Qt5::Core transport-plugin)
2525
endif()
2626
endif()
2727

backends/swiften/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ file(GLOB SRC *.cpp)
33
add_executable(spectrum2_swiften_backend ${SRC})
44

55
if(NOT WIN32)
6-
target_link_libraries(spectrum2_swiften_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
6+
target_link_libraries(spectrum2_swiften_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
77
else()
8-
target_link_libraries(spectrum2_swiften_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
8+
target_link_libraries(spectrum2_swiften_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
99
endif()
1010

1111
install(TARGETS spectrum2_swiften_backend RUNTIME DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})

backends/swiften/main.cpp

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,36 +80,15 @@ class SwiftenPlugin : public NetworkPlugin, Swift::XMPPParserClient {
8080
Swift::XMPPParser *m_xmppParser;
8181
Swift::FullPayloadParserFactoryCollection m_collection2;
8282

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

9490
LOG4CXX_INFO(logger, "Starting the plugin.");
95-
}
96-
97-
// NetworkPlugin uses this method to send the data to networkplugin server
98-
void sendData(const std::string &string) {
99-
m_conn->write(Swift::createSafeByteArray(string));
100-
}
101-
102-
// This method has to call handleDataRead with all received data from network plugin server
103-
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data) {
104-
if (m_firstPing) {
105-
m_firstPing = false;
106-
NetworkPlugin::PluginConfig cfg;
107-
cfg.setRawXML(true);
108-
cfg.setNeedRegistration(false);
109-
sendConfig(cfg);
110-
}
111-
std::string d(data->begin(), data->end());
112-
handleDataRead(d);
91+
connect(host, std::to_string(port));
11392
}
11493

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

447426
Logging::initBackendLogging(cfg);
448427

449-
Swift::SimpleEventLoop eventLoop;
450-
loop_ = &eventLoop;
451-
np = new SwiftenPlugin(cfg, &eventLoop, host, port);
452-
loop_->run();
428+
np = new SwiftenPlugin(cfg, host, port);
453429

454430
return 0;
455431
}

backends/template/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ add_executable(spectrum2_template_backend ${SRC})
44

55
if(CMAKE_COMPILER_IS_GNUCXX)
66
if(NOT WIN32)
7-
target_link_libraries(spectrum2_template_backend transport pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
7+
target_link_libraries(spectrum2_template_backend transport-plugin pthread ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
88
else()
9-
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
9+
target_link_libraries(spectrum2_template_backend transport-plugin ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
1010
endif()
1111
else()
12-
target_link_libraries(spectrum2_template_backend transport ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
12+
target_link_libraries(spectrum2_template_backend transport-plugin ${PROTOBUF_LIBRARY} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
1313
endif()
1414

1515
#install(TARGETS spectrum2_template_backend RUNTIME DESTINATION bin)

backends/template/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include "transport/NetworkPlugin.h"
66
#include "transport/Logging.h"
77

8-
// Swiften
9-
#include "Swiften/Swiften.h"
10-
118
#ifndef _WIN32
129
// for signal handler
1310
#include "unistd.h"
@@ -17,6 +14,7 @@
1714
#endif
1815
// Boost
1916
#include <boost/algorithm/string.hpp>
17+
#include <boost/filesystem.hpp>
2018
using namespace boost::filesystem;
2119
using namespace boost::program_options;
2220
using namespace Transport;
@@ -60,9 +58,7 @@ int main (int argc, char* argv[]) {
6058

6159
Logging::initBackendLogging(cfg);
6260

63-
Swift::SimpleEventLoop eventLoop;
64-
Plugin p(cfg, &eventLoop, host, port);
65-
eventLoop.run();
61+
Plugin p(cfg, host, port);
6662

6763
return 0;
6864
}

backends/template/plugin.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,19 @@
44
#include "transport/NetworkPlugin.h"
55
#include "transport/Logging.h"
66

7-
// Swiften
8-
#include "Swiften/Swiften.h"
9-
107
// Boost
118
#include <boost/algorithm/string.hpp>
9+
#include <boost/filesystem.hpp>
1210
using namespace boost::filesystem;
1311
using namespace boost::program_options;
1412
using namespace Transport;
1513

1614
DEFINE_LOGGER(logger, "Backend Template");
1715

18-
Plugin::Plugin(Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port) : NetworkPlugin() {
16+
Plugin::Plugin(Config *config, const std::string &host, int port) : NetworkPlugin() {
1917
this->config = config;
20-
m_factories = new Swift::BoostNetworkFactories(loop);
21-
m_conn = m_factories->getConnectionFactory()->createConnection();
22-
m_conn->onDataRead.connect(boost::bind(&Plugin::_handleDataRead, this, _1));
23-
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));
24-
2518
LOG4CXX_INFO(logger, "Starting the plugin.");
26-
}
27-
28-
// NetworkPlugin uses this method to send the data to networkplugin server
29-
void Plugin::sendData(const std::string &string) {
30-
m_conn->write(Swift::createSafeByteArray(string));
31-
}
32-
33-
// This method has to call handleDataRead with all received data from network plugin server
34-
void Plugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data) {
35-
std::string d(data->begin(), data->end());
36-
handleDataRead(d);
19+
connect(host, std::to_string(port));
3720
}
3821

3922
void Plugin::handleLoginRequest(const std::string &user, const std::string &legacyName, const std::string &password, const std::map<std::string, std::string> &settings) {

backends/template/plugin.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#pragma once
22

3-
#include "Swiften/Swiften.h"
4-
53
#include "transport/Config.h"
64
#include "transport/NetworkPlugin.h"
75

86
class Plugin : public Transport::NetworkPlugin {
97
public:
10-
Plugin(Transport::Config *config, Swift::SimpleEventLoop *loop, const std::string &host, int port);
8+
Plugin(Transport::Config *config, const std::string &host, int port);
119

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

2523
private:
26-
// This method has to call handleDataRead with all received data from network plugin server
27-
void _handleDataRead(std::shared_ptr<Swift::SafeByteArray> data);
28-
29-
private:
30-
Swift::BoostNetworkFactories *m_factories;
31-
Swift::BoostIOServiceThread m_boostIOServiceThread;
32-
std::shared_ptr<Swift::Connection> m_conn;
3324
Transport::Config *config;
3425
};

backends/twitter/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ find_package(CURL)
88

99
if(CURL_FOUND)
1010
message(STATUS "Using curl ${CURL_VERSION_STRING}: ${CURL_INCLUDE_DIRS} ${CURL_LIBRARIES}")
11-
target_link_libraries(spectrum2_twitter_backend transport JsonCpp::JsonCpp ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
11+
target_link_libraries(spectrum2_twitter_backend transport-plugin ${CURL_LIBRARIES} ${Boost_LIBRARIES} ${SWIFTEN_LIBRARY} ${LOG4CXX_LIBRARIES})
1212
else()
1313
message(FATAL_ERROR "curl not found")
1414
endif()

backends/twitter/HTTPRequest.h

Lines changed: 0 additions & 37 deletions
This file was deleted.

backends/twitter/Requests/ProfileImageRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "ProfileImageRequest.h"
2-
#include "../HTTPRequest.h"
2+
#include "transport/HTTPRequest.h"
33
DEFINE_LOGGER(profileImageRequestLogger, "ProfileImageRequest")
44
void ProfileImageRequest::run()
55
{

backends/twitter/TwitterPlugin.cpp

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
#include "Requests/DestroyFriendRequest.h"
1111
#include "Requests/RetweetRequest.h"
1212
#include "Requests/ProfileImageRequest.h"
13-
#include "Swiften/StringCodecs/Hexify.h"
13+
14+
#include <boost/uuid/detail/sha1.hpp>
15+
16+
#include <memory>
1417

1518
DEFINE_LOGGER(logger, "Twitter Backend");
1619

1720
TwitterPlugin *np = NULL;
18-
Swift::SimpleEventLoop *loop_; // Event Loop
1921

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

39+
std::string get_sha1(const std::string& p_arg)
40+
{
41+
boost::uuids::detail::sha1 sha1;
42+
sha1.process_bytes(p_arg.data(), p_arg.size());
43+
unsigned hash[5] = {0};
44+
sha1.get_digest(hash);
45+
46+
// Back to string
47+
char buf[41] = {0};
48+
49+
for (int i = 0; i < 5; i++)
50+
{
51+
std::sprintf(buf + (i << 3), "%08x", hash[i]);
52+
}
53+
54+
return std::string(buf);
55+
}
56+
3757

38-
TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
58+
TwitterPlugin::TwitterPlugin(Config *config, StorageBackend *storagebackend, const std::string &host, int port) : NetworkPlugin()
3959
{
4060
this->config = config;
4161
this->storagebackend = storagebackend;
@@ -68,56 +88,24 @@ TwitterPlugin::TwitterPlugin(Config *config, Swift::SimpleEventLoop *loop, Stora
6888
OAUTH_SECRET = "twitter_oauth_secret";
6989
MODE = "mode";
7090

71-
m_factories = new Swift::BoostNetworkFactories(loop);
72-
m_conn = m_factories->getConnectionFactory()->createConnection();
73-
m_conn->onDataRead.connect(boost::bind(&TwitterPlugin::_handleDataRead, this, _1));
74-
m_conn->connect(Swift::HostAddressPort(*(Swift::HostAddress::fromString(host)), port));
75-
76-
tp = new ThreadPool(loop_, 10);
77-
7891
LOG4CXX_INFO(logger, "Fetch timeout is set to " << CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
79-
tweet_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
80-
message_timer = m_factories->getTimerFactory()->createTimer(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000));
81-
82-
tweet_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForTweets, this));
83-
//message_timer->onTick.connect(boost::bind(&TwitterPlugin::pollForDirectMessages, this));
92+
io = std::make_shared<boost::asio::io_service>();
93+
tweet_timer = std::make_shared<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));
94+
message_timer = std::make_shared<boost::asio::deadline_timer>(io, boost::posix_time::seconds(CONFIG_INT_DEFAULTED(config, "twitter.fetch_timeout", 90000)));
8495

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

8999

90100
LOG4CXX_INFO(logger, "Starting the plugin.");
101+
connect(host, std::to_string(port));
91102
}
92103

93104
TwitterPlugin::~TwitterPlugin()
94105
{
95106
delete storagebackend;
96107
std::set<std::string>::iterator it;
97108
for (it = onlineUsers.begin() ; it != onlineUsers.end() ; it++) delete userdb[*it].sessions;
98-
delete tp;
99-
}
100-
101-
// Send data to NetworkPlugin server
102-
void TwitterPlugin::sendData(const std::string &string)
103-
{
104-
m_conn->write(Swift::createSafeByteArray(string));
105-
}
106-
107-
// Receive date from the NetworkPlugin server and invoke the appropirate payload handler (implement in the NetworkPlugin class)
108-
void TwitterPlugin::_handleDataRead(std::shared_ptr<Swift::SafeByteArray> data)
109-
{
110-
if (m_firstPing) {
111-
m_firstPing = false;
112-
// Users can join the network without registering if we allow
113-
// one user to connect multiple IRC networks.
114-
NetworkPlugin::PluginConfig cfg;
115-
cfg.setNeedPassword(false);
116-
sendConfig(cfg);
117-
}
118-
119-
std::string d(data->begin(), data->end());
120-
handleDataRead(d);
121109
}
122110

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

656644
if(userdb[user].twitterMode == MULTIPLECONTACT) {
657645
std::string lastTweet = friends[i].getLastStatus().getTweet();
658-
//LOG4CXX_INFO(logger, user << " - " << SHA(friendAvatars[i]))
646+
647+
auto friendAvatarHash = get_sha1(friendAvatars[i]);
648+
649+
LOG4CXX_INFO(logger, user << " - " << friendAvatarHash);
650+
659651
handleBuddyChanged(user, friends[i].getScreenName(), friends[i].getUserName(), std::vector<std::string>(),
660-
pbnetwork::STATUS_ONLINE, lastTweet, Swift::Hexify::hexify(cryptoProvider->getSHA1Hash(Swift::createByteArray(friendAvatars[i]))));
652+
pbnetwork::STATUS_ONLINE, lastTweet, friendAvatarHash);
661653
}
662654
else if(userdb[user].twitterMode == CHATROOM)
663655
handleParticipantChanged(user, friends[i].getScreenName(), adminChatRoom, 0, pbnetwork::STATUS_ONLINE);
@@ -831,7 +823,7 @@ void TwitterPlugin::createFriendResponse(std::string &user, User &frnd, std::str
831823

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

0 commit comments

Comments
 (0)