Initial support for PurpleAccount sharing
This commit is contained in:
parent
6aa3e63520
commit
1e8eb7076f
12 changed files with 53 additions and 79 deletions
|
@ -54,16 +54,20 @@ ServerFromClientSession::~ServerFromClientSession() {
|
|||
void ServerFromClientSession::handlePasswordValid(const std::string &user) {
|
||||
if (user != JID(user_, getLocalJID().getDomain()).toString())
|
||||
return;
|
||||
getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
|
||||
authenticated_ = true;
|
||||
getXMPPLayer()->resetParser();
|
||||
if (!isInitialized()) {
|
||||
getXMPPLayer()->writeElement(boost::shared_ptr<AuthSuccess>(new AuthSuccess()));
|
||||
authenticated_ = true;
|
||||
getXMPPLayer()->resetParser();
|
||||
}
|
||||
}
|
||||
|
||||
void ServerFromClientSession::handlePasswordInvalid(const std::string &user) {
|
||||
if (user != JID(user_, getLocalJID().getDomain()).toString() || authenticated_)
|
||||
return;
|
||||
getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
|
||||
finishSession(AuthenticationFailedError);
|
||||
if (!isInitialized()) {
|
||||
getXMPPLayer()->writeElement(boost::shared_ptr<AuthFailure>(new AuthFailure));
|
||||
finishSession(AuthenticationFailedError);
|
||||
}
|
||||
}
|
||||
|
||||
void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element) {
|
||||
|
|
|
@ -107,7 +107,7 @@ void ServerStanzaChannel::handleSessionFinished(const boost::optional<Session::S
|
|||
removeSession(session);
|
||||
|
||||
Swift::Presence::ref presence = Swift::Presence::create();
|
||||
presence->setFrom(JID(session->getUser(), session->getLocalJID().getDomain()).toString());
|
||||
presence->setFrom(session->getRemoteJID());
|
||||
presence->setType(Swift::Presence::Unavailable);
|
||||
onPresenceReceived(presence);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ namespace Swift {
|
|||
SimpleUserRegistry::SimpleUserRegistry() {
|
||||
}
|
||||
|
||||
bool SimpleUserRegistry::isValidUserPassword(const JID& user, const SafeByteArray& password) const {
|
||||
bool SimpleUserRegistry::isValidUserPassword(const JID& user, const SafeByteArray& password) {
|
||||
std::map<JID,SafeByteArray>::const_iterator i = users.find(user);
|
||||
return i != users.end() ? i->second == password : false;
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Swift {
|
|||
public:
|
||||
SimpleUserRegistry();
|
||||
|
||||
virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) const;
|
||||
virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password);
|
||||
void addUser(const JID& user, const std::string& password);
|
||||
|
||||
private:
|
||||
|
|
|
@ -17,7 +17,7 @@ namespace Swift {
|
|||
public:
|
||||
virtual ~UserRegistry();
|
||||
|
||||
virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) const = 0;
|
||||
virtual bool isValidUserPassword(const JID& user, const SafeByteArray& password) = 0;
|
||||
|
||||
boost::signal<void (const std::string &user)> onPasswordValid;
|
||||
boost::signal<void (const std::string &user)> onPasswordInvalid;
|
||||
|
|
|
@ -51,6 +51,7 @@ namespace Transport {
|
|||
class DiscoInfoResponder;
|
||||
class DiscoItemsResponder;
|
||||
class Factory;
|
||||
class UserRegistry;
|
||||
|
||||
/// Represents one transport instance.
|
||||
|
||||
|
@ -71,7 +72,7 @@ namespace Transport {
|
|||
/// - service.port
|
||||
/// - service.server_mode
|
||||
/// \param factory Transport Abstract factory used to create basic transport structures.
|
||||
Component(Swift::EventLoop *loop, Config *config, Factory *factory);
|
||||
Component(Swift::EventLoop *loop, Config *config, Factory *factory, Transport::UserRegistry *userRegistry = NULL);
|
||||
|
||||
/// Component destructor.
|
||||
~Component();
|
||||
|
@ -98,14 +99,6 @@ namespace Transport {
|
|||
/// \return True if the component is in server mode.
|
||||
bool inServerMode() { return m_server != NULL; }
|
||||
|
||||
/// Returns user password from internal UserRegistry.
|
||||
|
||||
/// In server mode, the password user used for login can be obtained by
|
||||
/// this method.
|
||||
/// \param barejid User's bare JID.
|
||||
/// \return User's password.
|
||||
const std::string &getUserRegistryPassword(const std::string &barejid);
|
||||
|
||||
/// Connects the Jabber server.
|
||||
|
||||
void start();
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
#include "Swiften/Swiften.h"
|
||||
#include "transport/userregistry.h"
|
||||
|
||||
namespace Transport {
|
||||
|
||||
|
@ -41,7 +42,7 @@ class UserManager {
|
|||
/// Creates new UserManager.
|
||||
/// \param component Component which's presence will be handled
|
||||
/// \param storageBackend Storage backend used to fetch UserInfos
|
||||
UserManager(Component *component, StorageBackend *storageBackend = NULL);
|
||||
UserManager(Component *component, UserRegistry *userRegistry, StorageBackend *storageBackend = NULL);
|
||||
|
||||
/// Destroys UserManager.
|
||||
~UserManager();
|
||||
|
@ -68,6 +69,23 @@ class UserManager {
|
|||
/// \param user removed User class
|
||||
boost::signal<void (User *user)> onUserDestroyed;
|
||||
|
||||
bool isUserConnected(const std::string &barejid) const {
|
||||
return m_users.find(barejid) != m_users.end();
|
||||
}
|
||||
|
||||
void connectUser(const Swift::JID &user) {
|
||||
if (m_users.find(user.toBare().toString()) != m_users.end()) {
|
||||
m_userRegistry->onPasswordValid(user);
|
||||
}
|
||||
else {
|
||||
Swift::Presence::ref response = Swift::Presence::create();
|
||||
response->setTo(m_component->getJID());
|
||||
response->setFrom(user);
|
||||
response->setType(Swift::Presence::Available);
|
||||
m_component->onUserPresenceReceived(response);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void handlePresence(Swift::Presence::ref presence);
|
||||
void handleMessageReceived(Swift::Message::ref message);
|
||||
|
@ -83,6 +101,7 @@ class UserManager {
|
|||
Component *m_component;
|
||||
StorageBackend *m_storageBackend;
|
||||
StorageResponder *m_storageResponder;
|
||||
UserRegistry *m_userRegistry;
|
||||
friend class RosterResponder;
|
||||
};
|
||||
|
||||
|
|
|
@ -52,8 +52,10 @@ int main(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
UserRegistry userRegistry(&config);
|
||||
|
||||
Swift::SimpleEventLoop eventLoop;
|
||||
Component transport(&eventLoop, &config, NULL);
|
||||
Component transport(&eventLoop, &config, NULL, &userRegistry);
|
||||
Logger logger(&transport);
|
||||
|
||||
StorageBackend *storageBackend = NULL;
|
||||
|
@ -66,7 +68,7 @@ int main(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
|
||||
UserManager userManager(&transport, storageBackend);
|
||||
UserManager userManager(&transport, &userRegistry, storageBackend);
|
||||
if (storageBackend) {
|
||||
UserRegistration userRegistration(&transport, &userManager, storageBackend);
|
||||
logger.setUserRegistration(&userRegistration);
|
||||
|
|
|
@ -35,7 +35,6 @@
|
|||
#include "Swiften/Elements/StreamError.h"
|
||||
#include "Swiften/Network/BoostConnectionServer.h"
|
||||
#include "Swiften/Elements/AttentionPayload.h"
|
||||
#include "Swiften/Elements/XHTMLIMPayload.h"
|
||||
#include "pbnetwork.pb.h"
|
||||
#include "sys/wait.h"
|
||||
#include "sys/signal.h"
|
||||
|
@ -392,10 +391,6 @@ void NetworkPluginServer::handleConvMessagePayload(const std::string &data, bool
|
|||
msg->setBody(payload.message());
|
||||
}
|
||||
|
||||
if (!payload.xhtml().empty()) {
|
||||
msg->addPayload(boost::make_shared<Swift::XHTMLIMPayload>(payload.xhtml()));
|
||||
}
|
||||
|
||||
NetworkConversation *conv = (NetworkConversation *) user->getConversationManager()->getConversation(payload.buddyname());
|
||||
if (!conv) {
|
||||
conv = new NetworkConversation(user->getConversationManager(), payload.buddyname());
|
||||
|
@ -650,7 +645,7 @@ void NetworkPluginServer::handleUserDestroyed(User *user) {
|
|||
}
|
||||
|
||||
void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost::shared_ptr<Swift::Message> &msg) {
|
||||
// handle ChatState
|
||||
|
||||
boost::shared_ptr<Swift::ChatState> statePayload = msg->getPayload<Swift::ChatState>();
|
||||
if (statePayload) {
|
||||
pbnetwork::WrapperMessage_Type type = pbnetwork::WrapperMessage_Type_TYPE_BUDDY_CHANGED;
|
||||
|
@ -682,7 +677,6 @@ void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost
|
|||
}
|
||||
}
|
||||
|
||||
// Handle attention requests
|
||||
boost::shared_ptr<Swift::AttentionPayload> attentionPayload = msg->getPayload<Swift::AttentionPayload>();
|
||||
if (attentionPayload) {
|
||||
pbnetwork::ConversationMessage m;
|
||||
|
@ -699,20 +693,13 @@ void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost
|
|||
send(c->connection, message);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
std::string xhtml;
|
||||
boost::shared_ptr<Swift::XHTMLIMPayload> xhtmlPayload = msg->getPayload<Swift::XHTMLIMPayload>();
|
||||
if (xhtmlPayload) {
|
||||
xhtml = xhtmlPayload->getBody();
|
||||
}
|
||||
|
||||
// Send normal message
|
||||
if (!msg->getBody().empty() || !xhtml.empty()) {
|
||||
if (!msg->getBody().empty()) {
|
||||
pbnetwork::ConversationMessage m;
|
||||
m.set_username(conv->getConversationManager()->getUser()->getJID().toBare());
|
||||
m.set_buddyname(conv->getLegacyName());
|
||||
m.set_message(msg->getBody());
|
||||
m.set_xhtml(xhtml);
|
||||
|
||||
std::string message;
|
||||
m.SerializeToString(&message);
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <boost/bind.hpp>
|
||||
#include "transport/storagebackend.h"
|
||||
#include "transport/factory.h"
|
||||
#include "transport/userregistry.h"
|
||||
#include "discoinforesponder.h"
|
||||
#include "discoitemsresponder.h"
|
||||
#include "storageparser.h"
|
||||
|
@ -46,36 +47,7 @@ namespace Transport {
|
|||
static LoggerPtr logger = Logger::getLogger("Component");
|
||||
static LoggerPtr logger_xml = Logger::getLogger("Component.XML");
|
||||
|
||||
class MyUserRegistry : public Swift::UserRegistry {
|
||||
public:
|
||||
MyUserRegistry(Component *c, Config *cfg) {component = c; config = cfg;}
|
||||
~MyUserRegistry() {}
|
||||
bool isValidUserPassword(const JID& user, const Swift::SafeByteArray& password) const {
|
||||
if (!CONFIG_STRING(config, "service.admin_username").empty() && user.getNode() == CONFIG_STRING(config, "service.admin_username")) {
|
||||
LOG4CXX_INFO(logger, "Admin is trying to login");
|
||||
if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
|
||||
onPasswordValid(user);
|
||||
}
|
||||
else {
|
||||
onPasswordInvalid(user);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
users[user.toBare().toString()] = Swift::safeByteArrayToString(password);
|
||||
Swift::Presence::ref response = Swift::Presence::create();
|
||||
response->setTo(component->getJID());
|
||||
response->setFrom(user);
|
||||
response->setType(Swift::Presence::Available);
|
||||
component->onUserPresenceReceived(response);
|
||||
return true;
|
||||
}
|
||||
mutable std::map<std::string, std::string> users;
|
||||
mutable Component *component;
|
||||
mutable Config *config;
|
||||
};
|
||||
|
||||
Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory) {
|
||||
Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory, Transport::UserRegistry *userRegistry) {
|
||||
m_component = NULL;
|
||||
m_userRegistry = NULL;
|
||||
m_server = NULL;
|
||||
|
@ -83,6 +55,7 @@ Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory) {
|
|||
m_config = config;
|
||||
m_factory = factory;
|
||||
m_loop = loop;
|
||||
m_userRegistry = userRegistry;
|
||||
|
||||
if (CONFIG_STRING(m_config, "logging.config").empty()) {
|
||||
LoggerPtr root = Logger::getRootLogger();
|
||||
|
@ -101,7 +74,6 @@ Component::Component(Swift::EventLoop *loop, Config *config, Factory *factory) {
|
|||
|
||||
if (CONFIG_BOOL(m_config, "service.server_mode")) {
|
||||
LOG4CXX_INFO(logger, "Creating component in server mode on port " << CONFIG_INT(m_config, "service.port"));
|
||||
m_userRegistry = new MyUserRegistry(this, m_config);
|
||||
m_server = new Swift::Server(loop, m_factories, m_userRegistry, m_jid, CONFIG_INT(m_config, "service.port"));
|
||||
if (!CONFIG_STRING(m_config, "service.cert").empty()) {
|
||||
LOG4CXX_INFO(logger, "Using PKCS#12 certificate " << CONFIG_STRING(m_config, "service.cert"));
|
||||
|
@ -165,16 +137,9 @@ Component::~Component() {
|
|||
delete m_component;
|
||||
if (m_server)
|
||||
delete m_server;
|
||||
if (m_userRegistry)
|
||||
delete m_userRegistry;
|
||||
delete m_factories;
|
||||
}
|
||||
|
||||
const std::string &Component::getUserRegistryPassword(const std::string &barejid) {
|
||||
MyUserRegistry *registry = dynamic_cast<MyUserRegistry *>(m_userRegistry);
|
||||
return registry->users[barejid];
|
||||
}
|
||||
|
||||
Swift::StanzaChannel *Component::getStanzaChannel() {
|
||||
return m_stanzaChannel;
|
||||
}
|
||||
|
|
|
@ -67,8 +67,7 @@ const Swift::JID &User::getJID() {
|
|||
}
|
||||
|
||||
void User::handlePresence(Swift::Presence::ref presence) {
|
||||
Swift::Presence::ref highest = m_presenceOracle->getHighestPriorityPresence(m_jid.toBare());
|
||||
|
||||
std::cout << "PRESENCE " << presence->getFrom().toString() << "\n";
|
||||
if (!m_connected) {
|
||||
// we are not connected to legacy network, so we should do it when disco#info arrive :)
|
||||
if (m_readyForConnect == false) {
|
||||
|
@ -114,6 +113,7 @@ void User::handlePresence(Swift::Presence::ref presence) {
|
|||
return;
|
||||
}
|
||||
|
||||
Swift::Presence::ref highest = m_presenceOracle->getHighestPriorityPresence(m_jid.toBare());
|
||||
if (highest) {
|
||||
highest->setTo(presence->getFrom().toBare());
|
||||
highest->setFrom(m_component->getJID());
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "transport/storagebackend.h"
|
||||
#include "transport/conversationmanager.h"
|
||||
#include "transport/rostermanager.h"
|
||||
#include "transport/userregistry.h"
|
||||
#include "storageresponder.h"
|
||||
#include "log4cxx/logger.h"
|
||||
|
||||
|
@ -33,12 +34,13 @@ namespace Transport {
|
|||
|
||||
static LoggerPtr logger = Logger::getLogger("UserManager");
|
||||
|
||||
UserManager::UserManager(Component *component, StorageBackend *storageBackend) {
|
||||
UserManager::UserManager(Component *component, UserRegistry *userRegistry, StorageBackend *storageBackend) {
|
||||
m_cachedUser = NULL;
|
||||
m_onlineBuddies = 0;
|
||||
m_component = component;
|
||||
m_storageBackend = storageBackend;
|
||||
m_storageResponder = NULL;
|
||||
m_userRegistry = userRegistry;
|
||||
|
||||
if (m_storageBackend) {
|
||||
m_storageResponder = new StorageResponder(component->getIQRouter(), m_storageBackend, this);
|
||||
|
@ -48,6 +50,8 @@ UserManager::UserManager(Component *component, StorageBackend *storageBackend) {
|
|||
component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
|
||||
m_component->getStanzaChannel()->onMessageReceived.connect(bind(&UserManager::handleMessageReceived, this, _1));
|
||||
m_component->getStanzaChannel()->onPresenceReceived.connect(bind(&UserManager::handleGeneralPresenceReceived, this, _1));
|
||||
|
||||
m_userRegistry->onConnectUser.connect(bind(&UserManager::connectUser, this, _1));
|
||||
// component->onDiscoInfoResponse.connect(bind(&UserManager::handleDiscoInfoResponse, this, _1, _2, _3));
|
||||
}
|
||||
|
||||
|
@ -131,7 +135,7 @@ void UserManager::handlePresence(Swift::Presence::ref presence) {
|
|||
registered = true;
|
||||
}
|
||||
}
|
||||
res.password = m_component->getUserRegistryPassword(userkey);
|
||||
res.password = m_userRegistry->getUserPassword(userkey);
|
||||
}
|
||||
|
||||
if (!registered) {
|
||||
|
|
Loading…
Add table
Reference in a new issue