Working accounts sharing :)

This commit is contained in:
HanzZ 2011-06-28 21:57:49 +02:00
parent 1e8eb7076f
commit 9477e3f838
7 changed files with 98 additions and 13 deletions

View file

@ -109,7 +109,12 @@ void ServerFromClientSession::handleElement(boost::shared_ptr<Element> element)
}
else if (IQ* iq = dynamic_cast<IQ*>(element.get())) {
if (boost::shared_ptr<ResourceBind> resourceBind = iq->getPayload<ResourceBind>()) {
setRemoteJID(JID(user_, getLocalJID().getDomain(), resourceBind->getResource()));
std::string bucket = "abcdefghijklmnopqrstuvwxyz";
std::string uuid;
for (int i = 0; i < 10; i++) {
uuid += bucket[rand() % bucket.size()];
}
setRemoteJID(JID(user_, getLocalJID().getDomain(), uuid));
boost::shared_ptr<ResourceBind> resultResourceBind(new ResourceBind());
resultResourceBind->setJID(getRemoteJID());
getXMPPLayer()->writeElement(IQ::createResult(JID(), iq->getID(), resultResourceBind));

View file

@ -84,6 +84,8 @@ class RosterManager {
void sendBuddyRosterPush(Buddy *buddy);
void sendCurrentPresences(const Swift::JID &to);
private:
void setBuddyCallback(Buddy *buddy);

View file

@ -73,18 +73,7 @@ class UserManager {
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);
}
}
void connectUser(const Swift::JID &user);
private:
void handlePresence(Swift::Presence::ref presence);

View file

@ -0,0 +1,63 @@
/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#pragma once
#include <string>
#include <map>
#include "Swiften/Swiften.h"
#include "Swiften/Server/UserRegistry.h"
#include "transport/config.h"
namespace Transport {
class UserRegistry : public Swift::UserRegistry {
public:
UserRegistry(Config *cfg) {config = cfg;}
~UserRegistry() {}
bool isValidUserPassword(const Swift::JID& user, const Swift::SafeByteArray& password) {
if (!CONFIG_STRING(config, "service.admin_username").empty() && user.getNode() == CONFIG_STRING(config, "service.admin_username")) {
if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
onPasswordValid(user);
}
else {
onPasswordInvalid(user);
}
return true;
}
users[user.toBare().toString()] = Swift::safeByteArrayToString(password);
onConnectUser(user);
return true;
}
const std::string &getUserPassword(const std::string &barejid) {
return users[barejid];
}
boost::signal<void (const Swift::JID &user)> onConnectUser;
mutable std::map<std::string, std::string> users;
mutable Config *config;
};
}

View file

@ -322,4 +322,15 @@ Swift::RosterPayload::ref RosterManager::generateRosterPayload() {
return payload;
}
void RosterManager::sendCurrentPresences(const Swift::JID &to) {
for (std::map<std::string, Buddy *>::const_iterator it = m_buddies.begin(); it != m_buddies.end(); it++) {
Buddy *buddy = (*it).second;
Swift::Presence::ref presence = buddy->generatePresenceStanza(255);
if (presence) {
presence->setTo(to);
m_component->getStanzaChannel()->sendPresence(presence);
}
}
}
}

View file

@ -72,6 +72,7 @@ bool RosterResponder::handleGetRequest(const Swift::JID& from, const Swift::JID&
LOG4CXX_WARN(logger, from.toBare().toString() << ": User is not logged in");
}
sendResponse(from, id, user->getRosterManager()->generateRosterPayload());
user->getRosterManager()->sendCurrentPresences(from);
return true;
}

View file

@ -250,4 +250,18 @@ void UserManager::handleSubscription(Swift::Presence::ref presence) {
// }
}
void UserManager::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);
}
}
}