Comments + some methods renamed
This commit is contained in:
parent
88865c1234
commit
49e5cd3ac4
7 changed files with 107 additions and 62 deletions
|
@ -26,72 +26,109 @@
|
|||
|
||||
#include "Swiften/Swiften.h"
|
||||
|
||||
typedef enum { SPECTRUM_BUDDY_NO_FLAG = 0,
|
||||
SPECTRUM_BUDDY_JID_ESCAPING = 2,
|
||||
SPECTRUM_BUDDY_IGNORE = 4
|
||||
} SpectrumBuddyFlag;
|
||||
|
||||
namespace Transport {
|
||||
|
||||
class RosterManager;
|
||||
|
||||
// Wrapper for PurpleBuddy.
|
||||
typedef enum { BUDDY_NO_FLAG = 0,
|
||||
BUDDY_JID_ESCAPING = 2,
|
||||
BUDDY_IGNORE = 4
|
||||
} BuddyFlag;
|
||||
|
||||
/// Represents one legacy network Buddy.
|
||||
class AbstractBuddy {
|
||||
public:
|
||||
/// Constructor.
|
||||
AbstractBuddy(long id);
|
||||
|
||||
/// Destructor
|
||||
virtual ~AbstractBuddy();
|
||||
|
||||
// Sets/gets ID used to identify this buddy for example by storage backend.
|
||||
void setId(long id);
|
||||
long getId();
|
||||
/// Sets unique ID used to identify this buddy by StorageBackend. This is set
|
||||
/// by RosterStorage class once the buddy is stored into database or when the
|
||||
/// buddy is loaded from database.
|
||||
/// You should not need to set this ID manually.
|
||||
/// \param id ID
|
||||
void setID(long id);
|
||||
|
||||
// Returns full JID.
|
||||
/// Returns unique ID used to identify this buddy by StorageBackend.
|
||||
/// \see AbstractBuddy::setID(long)
|
||||
/// \return ID
|
||||
long getID();
|
||||
|
||||
/// Returns full JID of this buddy.
|
||||
/// \param hostname hostname used as domain in returned JID
|
||||
/// \return full JID of this buddy
|
||||
Swift::JID getJID(const std::string &hostname);
|
||||
|
||||
// Generates whole <presence> stanza without "to" attribute. That attribute
|
||||
// has to be added manually.
|
||||
// only_new - if the stanza is the same as previous generated one, returns NULL.
|
||||
/// Generates whole Presennce stanza with current status/show for this buddy.
|
||||
/// Presence stanza does not containt "to" attribute, it has to be added manually.
|
||||
/// \param features features used in returned stanza
|
||||
/// \param only_new if True, this function returns Presence stanza only if it's different
|
||||
/// than the previously generated one.
|
||||
/// \return Presence stanza or NULL.
|
||||
Swift::Presence::ref generatePresenceStanza(int features, bool only_new = false);
|
||||
|
||||
// Sets online/offline state information.
|
||||
/// Marks this buddy as available.
|
||||
void setOnline();
|
||||
|
||||
/// Marks this buddy as offline.
|
||||
void setOffline();
|
||||
|
||||
// Returns true if online.
|
||||
/// Returns true if this buddy is marked as available/online.
|
||||
/// \return true if this buddy is marked as available/online.
|
||||
bool isOnline();
|
||||
|
||||
// Sets/gets current subscription.
|
||||
// TODO: rewrite me to use SpectrumSubscriptionType!
|
||||
/// Sets current subscription.
|
||||
/// \param subscription "to", "from", "both", "ask"
|
||||
void setSubscription(const std::string &subscription);
|
||||
|
||||
/// Returns current subscription
|
||||
/// \return subscription "to", "from", "both", "ask"
|
||||
const std::string &getSubscription();
|
||||
|
||||
// Sets SpectrumBuddyFlags.
|
||||
void setFlags(int flags);
|
||||
/// Sets this buddy's flags.
|
||||
/// \param flags flags
|
||||
void setFlags(BuddyFlag flags);
|
||||
|
||||
// Returns flags.
|
||||
int getFlags();
|
||||
/// Returns this buddy's flags.
|
||||
/// \param flags flags
|
||||
BuddyFlag getFlags();
|
||||
|
||||
/// Sets RosterManager associated with this buddy.
|
||||
/// \param rosterManager RosterManager
|
||||
void setRosterManager(RosterManager *rosterManager) { m_rosterManager = rosterManager; }
|
||||
|
||||
/// Returns RosterManager associated with this buddy
|
||||
/// \return rosterManager
|
||||
RosterManager *getRosterManager() { return m_rosterManager; }
|
||||
|
||||
// Returns buddy's name (so for example UIN for ICQ, JID for XMPP...).
|
||||
/// Returns legacy network username which does not contain unsafe characters,
|
||||
/// so it can be used in JIDs.
|
||||
std::string getSafeName();
|
||||
|
||||
/// Returns legacy network username of this buddy. (for example UIN for ICQ,
|
||||
/// JID for Jabber, ...).
|
||||
/// \return legacy network username
|
||||
virtual std::string getName() = 0;
|
||||
|
||||
// Returns buddy's alias (nickname).
|
||||
/// Returns alias (nickname) of this buddy.
|
||||
/// \return alias (nickname)
|
||||
virtual std::string getAlias() = 0;
|
||||
|
||||
// Returns buddy's group name.
|
||||
/// Returns list of groups this buddy is in.
|
||||
/// \return groups
|
||||
virtual std::vector<std::string> getGroups() = 0;
|
||||
|
||||
// Returns name which doesn't contain unsafe characters, so it can be used.
|
||||
// in JIDs.
|
||||
virtual std::string getSafeName() = 0;
|
||||
|
||||
// Stores current status in `status` and current status message in `statusMessage`.
|
||||
// Returns true if data can be stored.
|
||||
/// Returns current legacy network status and statuMessage of this buddy.
|
||||
/// \param status current status/show is stored here
|
||||
/// \param statusMessage current status message is stored here
|
||||
/// \return true if status was stored successfully
|
||||
virtual bool getStatus(Swift::StatusShow &status, std::string &statusMessage) = 0;
|
||||
|
||||
// Returns SHA-1 hash of buddy icon (avatar) or empty string if there is no avatar.
|
||||
/// Returns SHA-1 hash of buddy icon (avatar) or empty string if there is no avatar
|
||||
/// for this buddy.
|
||||
/// \return avatar hash or empty string.
|
||||
virtual std::string getIconHash() = 0;
|
||||
|
||||
private:
|
||||
|
@ -99,7 +136,7 @@ class AbstractBuddy {
|
|||
bool m_online;
|
||||
std::string m_subscription;
|
||||
Swift::Presence::ref m_lastPresence;
|
||||
int m_flags;
|
||||
BuddyFlag m_flags;
|
||||
RosterManager *m_rosterManager;
|
||||
};
|
||||
|
||||
|
|
|
@ -82,8 +82,8 @@ class Logger
|
|||
void handleUserDestroyed(User *user);
|
||||
|
||||
// RosterManager
|
||||
void handleBuddyAdded(AbstractBuddy *buddy);
|
||||
void handleBuddyRemoved(AbstractBuddy *buddy);
|
||||
void handleBuddySet(AbstractBuddy *buddy);
|
||||
void handleBuddyUnset(AbstractBuddy *buddy);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
using namespace Transport;
|
||||
|
||||
Logger *_logger;
|
||||
|
||||
static gboolean nodaemon = FALSE;
|
||||
static gchar *logfile = NULL;
|
||||
static gchar *lock_file = NULL;
|
||||
|
@ -65,9 +67,9 @@ static void buddyListNewNode(PurpleBlistNode *node) {
|
|||
else {
|
||||
buddy->node.ui_data = (void *) new SpectrumBuddy(-1, buddy);
|
||||
SpectrumBuddy *s_buddy = (SpectrumBuddy *) buddy->node.ui_data;
|
||||
s_buddy->setFlags(SPECTRUM_BUDDY_JID_ESCAPING);
|
||||
s_buddy->setFlags(BUDDY_JID_ESCAPING);
|
||||
|
||||
user->getRosterManager()->addBuddy(s_buddy);
|
||||
user->getRosterManager()->setBuddy(s_buddy);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -83,7 +85,7 @@ static void NodeRemoved(PurpleBlistNode *node, void *data) {
|
|||
s_buddy->removeBuddy(buddy);
|
||||
buddy->node.ui_data = NULL;
|
||||
if (s_buddy->getBuddiesCount() == 0) {
|
||||
user->getRosterManager()->removeBuddy(s_buddy);
|
||||
user->getRosterManager()->unsetBuddy(s_buddy);
|
||||
delete s_buddy;
|
||||
}
|
||||
}
|
||||
|
@ -296,6 +298,7 @@ static void handleUserCreated(User *user, UserManager *userManager, Config *conf
|
|||
user->setData(account);
|
||||
|
||||
user->onReadyToConnect.connect(boost::bind(&handleUserReadyToConnect, user));
|
||||
_logger->setRosterManager(user->getRosterManager());
|
||||
|
||||
// Transport::instance()->protocol()->onPurpleAccountCreated(m_account);
|
||||
|
||||
|
@ -403,6 +406,7 @@ int main(int argc, char **argv) {
|
|||
SpectrumEventLoop eventLoop;
|
||||
Component transport(&eventLoop, &config);
|
||||
Logger logger(&transport);
|
||||
_logger = &logger;
|
||||
|
||||
SQLite3Backend sql(&config);
|
||||
logger.setStorageBackend(&sql);
|
||||
|
|
|
@ -108,20 +108,3 @@ std::vector<std::string> SpectrumBuddy::getGroups() {
|
|||
return groups;
|
||||
}
|
||||
|
||||
std::string SpectrumBuddy::getSafeName() {
|
||||
std::string name = getName();
|
||||
// Transport::instance()->protocol()->prepareUsername(name, purple_buddy_get_account(m_buddy));
|
||||
if (getFlags() & SPECTRUM_BUDDY_JID_ESCAPING) {
|
||||
// name = JID::escapeNode(name);
|
||||
}
|
||||
else {
|
||||
if (name.find_last_of("@") != std::string::npos) {
|
||||
name.replace(name.find_last_of("@"), 1, "%");
|
||||
}
|
||||
}
|
||||
if (name.empty()) {
|
||||
Log("SpectrumBuddy::getSafeName", "Name is EMPTY! Previous was " << getName() << ".");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,6 @@ class SpectrumBuddy : public AbstractBuddy {
|
|||
bool getStatus(Swift::StatusShow &status, std::string &statusMessage);
|
||||
std::string getIconHash();
|
||||
std::vector<std::string> getGroups();
|
||||
std::string getSafeName();
|
||||
|
||||
void addBuddy(PurpleBuddy *buddy) { m_buddies.push_back(buddy); }
|
||||
void removeBuddy(PurpleBuddy *buddy) { m_buddies.remove(buddy); }
|
||||
|
|
|
@ -22,25 +22,25 @@
|
|||
|
||||
namespace Transport {
|
||||
|
||||
AbstractBuddy::AbstractBuddy(long id) : m_id(id), m_online(false), m_subscription("ask"), m_flags(0) {
|
||||
AbstractBuddy::AbstractBuddy(long id) : m_id(id), m_online(false), m_subscription("ask"), m_flags(BUDDY_NO_FLAG) {
|
||||
}
|
||||
|
||||
AbstractBuddy::~AbstractBuddy() {
|
||||
}
|
||||
|
||||
void AbstractBuddy::setId(long id) {
|
||||
void AbstractBuddy::setID(long id) {
|
||||
m_id = id;
|
||||
}
|
||||
|
||||
long AbstractBuddy::getId() {
|
||||
long AbstractBuddy::getID() {
|
||||
return m_id;
|
||||
}
|
||||
|
||||
void AbstractBuddy::setFlags(int flags) {
|
||||
void AbstractBuddy::setFlags(BuddyFlag flags) {
|
||||
m_flags = flags;
|
||||
}
|
||||
|
||||
int AbstractBuddy::getFlags() {
|
||||
BuddyFlag AbstractBuddy::getFlags() {
|
||||
return m_flags;
|
||||
}
|
||||
|
||||
|
@ -110,4 +110,21 @@ Swift::Presence::ref AbstractBuddy::generatePresenceStanza(int features, bool on
|
|||
return presence;
|
||||
}
|
||||
|
||||
std::string AbstractBuddy::getSafeName() {
|
||||
std::string name = getName();
|
||||
// Transport::instance()->protocol()->prepareUsername(name, purple_buddy_get_account(m_buddy));
|
||||
if (getFlags() & BUDDY_JID_ESCAPING) {
|
||||
// name = JID::escapeNode(name);
|
||||
}
|
||||
else {
|
||||
if (name.find_last_of("@") != std::string::npos) {
|
||||
name.replace(name.find_last_of("@"), 1, "%");
|
||||
}
|
||||
}
|
||||
if (name.empty()) {
|
||||
// Log("SpectrumBuddy::getSafeName", "Name is EMPTY! Previous was " << getName() << ".");
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -57,6 +57,11 @@ void Logger::setUserManager(UserManager *userManager) {
|
|||
userManager->onUserDestroyed.connect(bind(&Logger::handleUserDestroyed, this, _1));
|
||||
}
|
||||
|
||||
void Logger::setRosterManager(RosterManager *rosterManager) {
|
||||
rosterManager->onBuddySet.connect(bind(&Logger::handleBuddySet, this, _1));
|
||||
rosterManager->onBuddyUnset.connect(bind(&Logger::handleBuddyUnset, this, _1));
|
||||
}
|
||||
|
||||
void Logger::handleConnected() {
|
||||
std::cout << "[COMPONENT] Connected to Jabber Server!\n";
|
||||
}
|
||||
|
@ -106,11 +111,11 @@ void Logger::handleUserDestroyed(User *user) {
|
|||
std::cout << "[USERMANAGER] User \"" << user->getJID().toBare().toString() << "\" (UIN: \"" << user->getUserInfo().uin << "\") disconnected and User class is going to be destroyed\n";
|
||||
}
|
||||
|
||||
void Logger::handleBuddyAdded(AbstractBuddy *buddy) {
|
||||
void Logger::handleBuddySet(AbstractBuddy *buddy) {
|
||||
std::cout << "[ROSTERMANAGER] \"" << buddy->getRosterManager()->getUser()->getJID().toBare().toString() << "\": Buddy \"" << buddy->getSafeName() << "\" (ALIAS: \"" << buddy->getAlias() << "\") has been bound with this user's roster.\n";
|
||||
}
|
||||
|
||||
void Logger::handleBuddyRemoved(AbstractBuddy *buddy) {
|
||||
void Logger::handleBuddyUnset(AbstractBuddy *buddy) {
|
||||
std::cout << "[ROSTERMANAGER] \"" << buddy->getRosterManager()->getUser()->getJID().toBare().toString() << "\": Buddy \"" << buddy->getSafeName() << "\" (ALIAS: \"" << buddy->getAlias() << "\") has been unbound with this user's roster.\n";
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue