From 76c678dd6f1d51eed490b3df86943329c0b31bb0 Mon Sep 17 00:00:00 2001 From: HanzZ Date: Fri, 1 Apr 2011 23:54:29 +0200 Subject: [PATCH] Working conversation from legacy -> XMPP --- include/transport/abstractconversation.h | 53 +++++++++++++++++++ include/transport/conversationmanager.h | 61 ++++++++++++++++++++++ include/transport/rostermanager.h | 2 + include/transport/user.h | 4 ++ spectrum/src/main.cpp | 66 ++++++++++++++++++++++-- spectrum/src/spectrumconversation.cpp | 36 +++++++++++++ spectrum/src/spectrumconversation.h | 46 +++++++++++++++++ src/abstractconversation.cpp | 54 +++++++++++++++++++ src/conversationmanager.cpp | 48 +++++++++++++++++ src/rostermanager.cpp | 10 ++-- src/user.cpp | 3 ++ 11 files changed, 377 insertions(+), 6 deletions(-) create mode 100644 include/transport/abstractconversation.h create mode 100644 include/transport/conversationmanager.h create mode 100644 spectrum/src/spectrumconversation.cpp create mode 100644 spectrum/src/spectrumconversation.h create mode 100644 src/abstractconversation.cpp create mode 100644 src/conversationmanager.cpp diff --git a/include/transport/abstractconversation.h b/include/transport/abstractconversation.h new file mode 100644 index 00000000..ded4b9a2 --- /dev/null +++ b/include/transport/abstractconversation.h @@ -0,0 +1,53 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * 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 +#include +#include "transport/transport.h" + +#include "Swiften/Swiften.h" +#include "Swiften/Elements/Message.h" + +namespace Transport { + +class ConversationManager; + +class AbstractConversation { + public: + /// Constructor. + AbstractConversation(ConversationManager *conversationManager, const std::string &legacyName); + + /// Destructor + virtual ~AbstractConversation(); + + const std::string &getLegacyName() { return m_legacyName; } + + void handleMessage(boost::shared_ptr &message); + + virtual void sendMessage(boost::shared_ptr &message) = 0; + + private: + ConversationManager *m_conversationManager; + std::string m_legacyName; +}; + +} diff --git a/include/transport/conversationmanager.h b/include/transport/conversationmanager.h new file mode 100644 index 00000000..fd5a2e6a --- /dev/null +++ b/include/transport/conversationmanager.h @@ -0,0 +1,61 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * 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 +#include +#include +#include "Swiften/Swiften.h" + +namespace Transport { + +class AbstractConversation; +class User; +class Component; + +class ConversationManager { + public: + /// Creates new ConversationManager. + /// \param user User associated with this ConversationManager. + /// \param component Transport instance associated with this roster. + ConversationManager(User *user, Component *component); + + /// Destructor. + virtual ~ConversationManager(); + + /// Returns user associated with this manager. + /// \return User + User *getUser() { return m_user; } + + Component *getComponent() { return m_component; } + + void setConversation(AbstractConversation *conv); + + void unsetConversation(AbstractConversation *conv); + + private: + Component *m_component; + User *m_user; + + std::map m_convs; +}; + +} diff --git a/include/transport/rostermanager.h b/include/transport/rostermanager.h index ec4c37f4..47f29c06 100644 --- a/include/transport/rostermanager.h +++ b/include/transport/rostermanager.h @@ -54,6 +54,8 @@ class RosterManager { /// \param buddy AbstractBuddy. void unsetBuddy(AbstractBuddy *buddy); + AbstractBuddy *getBuddy(const std::string &name); + /// Returns user associated with this roster. /// \return User User *getUser() { return m_user; } diff --git a/include/transport/user.h b/include/transport/user.h index 2df8525f..fa6f19a1 100644 --- a/include/transport/user.h +++ b/include/transport/user.h @@ -30,6 +30,7 @@ namespace Transport { class Component; class RosterManager; +class ConversationManager; struct UserInfo; /// Represents online XMPP user. @@ -55,6 +56,8 @@ class User { RosterManager *getRosterManager() { return m_rosterManager; } + ConversationManager *getConversationManager() { return m_conversationManager; } + Component *getComponent() { return m_component; } @@ -77,6 +80,7 @@ class User { Swift::JID m_jid; Component *m_component; RosterManager *m_rosterManager; + ConversationManager *m_conversationManager; Swift::EntityCapsManager *m_entityCapsManager; Swift::PresenceOracle *m_presenceOracle; UserInfo m_userInfo; diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index 87c8fe6a..3bc32f95 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -13,6 +13,7 @@ #include "transport/rostermanager.h" #include "spectrumeventloop.h" #include "spectrumbuddy.h" +#include "spectrumconversation.h" #include "geventloop.h" #define Log(X, STRING) std::cout << "[SPECTRUM] " << X << " " << STRING << "\n"; @@ -109,8 +110,8 @@ static void NodeRemoved(PurpleBlistNode *node, void *data) { return; PurpleBuddy *buddy = (PurpleBuddy *) node; - PurpleAccount *account = purple_buddy_get_account(buddy); - User *user = (User *) account->ui_data; +// PurpleAccount *account = purple_buddy_get_account(buddy); +// User *user = (User *) account->ui_data; if (buddy->node.ui_data) { SpectrumBuddy *s_buddy = (SpectrumBuddy *) buddy->node.ui_data; s_buddy->removeBuddy(buddy); @@ -139,6 +140,63 @@ static PurpleBlistUiOps blistUiOps = NULL }; +static void conv_new(PurpleConversation *conv) { + PurpleAccount *account = purple_conversation_get_account(conv); + User *user = (User *) account->ui_data; + + if (!user) + return; + + std::string name = purple_conversation_get_name(conv); + size_t pos = name.find("/"); + if (pos != std::string::npos) + name.erase((int) pos, name.length() - (int) pos); + + SpectrumConversation *s_conv = new SpectrumConversation(user->getConversationManager(), name, conv); + conv->ui_data = s_conv; +} + +static void conv_destroy(PurpleConversation *conv) { + SpectrumConversation *s_conv = (SpectrumConversation *) conv->ui_data; + if (s_conv) { + delete s_conv; + } +} + +static void conv_write_im(PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime) { + SpectrumConversation *s_conv = (SpectrumConversation *) conv->ui_data; + if (!s_conv) + return; + + boost::shared_ptr msg(new Swift::Message()); + msg->setBody(message); + + s_conv->handleMessage(msg); +} + +static PurpleConversationUiOps conversation_ui_ops = +{ + conv_new, + conv_destroy, + NULL,//conv_write_chat, /* write_chat */ + conv_write_im, /* write_im */ + NULL,//conv_write_conv, /* write_conv */ + NULL,//conv_chat_add_users, /* chat_add_users */ + NULL,//conv_chat_rename_user, /* chat_rename_user */ + NULL,//conv_chat_remove_users, /* chat_remove_users */ + NULL,//pidgin_conv_chat_update_user, /* chat_update_user */ + NULL,//pidgin_conv_present_conversation, /* present */ + NULL,//pidgin_conv_has_focus, /* has_focus */ + NULL,//pidgin_conv_custom_smiley_add, /* custom_smiley_add */ + NULL,//pidgin_conv_custom_smiley_write, /* custom_smiley_write */ + NULL,//pidgin_conv_custom_smiley_close, /* custom_smiley_close */ + NULL,//pidgin_conv_send_confirm, /* send_confirm */ + NULL, + NULL, + NULL, + NULL +}; + static void transport_core_ui_init(void) { purple_blist_set_ui_ops(&blistUiOps); @@ -147,7 +205,7 @@ static void transport_core_ui_init(void) // purple_request_set_ui_ops(&requestUiOps); // purple_xfers_set_ui_ops(getXferUiOps()); // purple_connections_set_ui_ops(&conn_ui_ops); -// purple_conversations_set_ui_ops(&conversation_ui_ops); + purple_conversations_set_ui_ops(&conversation_ui_ops); // #ifndef WIN32 // purple_dnsquery_set_ui_ops(getDNSUiOps()); // #endif @@ -196,6 +254,8 @@ static bool initPurple(Config &cfg) { bool ret; purple_util_set_user_dir("./"); + remove("./accounts.xml"); + remove("./blist.xml"); // if (m_configuration.logAreas & LOG_AREA_PURPLE) purple_debug_set_ui_ops(&debugUiOps); diff --git a/spectrum/src/spectrumconversation.cpp b/spectrum/src/spectrumconversation.cpp new file mode 100644 index 00000000..ede6c5ed --- /dev/null +++ b/spectrum/src/spectrumconversation.cpp @@ -0,0 +1,36 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * 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 + */ + +#include "spectrumconversation.h" +#include "transport/user.h" + +#define Log(X, STRING) std::cout << "[SPECTRUM] " << X << " " << STRING << "\n"; + +SpectrumConversation::SpectrumConversation(ConversationManager *conversationManager, const std::string &legacyName, PurpleConversation *conv) : AbstractConversation(conversationManager, legacyName), m_conv(conv) { +} + +SpectrumConversation::~SpectrumConversation() { +} + +void SpectrumConversation::sendMessage(boost::shared_ptr &message) { + +} + + diff --git a/spectrum/src/spectrumconversation.h b/spectrum/src/spectrumconversation.h new file mode 100644 index 00000000..4fe8fc0d --- /dev/null +++ b/spectrum/src/spectrumconversation.h @@ -0,0 +1,46 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * 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 +#include "purple.h" +#include "account.h" +#include "glib.h" +#include +#include "transport/abstractconversation.h" +#include "transport/conversationmanager.h" + +using namespace Transport; + +// Wrapper for PurpleBuddy +class SpectrumConversation : public AbstractConversation { + public: + SpectrumConversation(ConversationManager *conversationManager, const std::string &legacyName, PurpleConversation *conv); + virtual ~SpectrumConversation(); + + PurpleConversation *getConversation() { return m_conv; } + + void sendMessage(boost::shared_ptr &message); + + private: + PurpleConversation *m_conv; +}; + diff --git a/src/abstractconversation.cpp b/src/abstractconversation.cpp new file mode 100644 index 00000000..fba69c7c --- /dev/null +++ b/src/abstractconversation.cpp @@ -0,0 +1,54 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * 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 + */ + +#include +#include "transport/abstractconversation.h" +#include "transport/conversationmanager.h" +#include "transport/user.h" +#include "transport/transport.h" +#include "transport/abstractbuddy.h" +#include "transport/rostermanager.h" + +namespace Transport { + +AbstractConversation::AbstractConversation(ConversationManager *conversationManager, const std::string &legacyName) : m_conversationManager(conversationManager) { + m_conversationManager->setConversation(this); + m_legacyName = legacyName; +} + +AbstractConversation::~AbstractConversation() { + m_conversationManager->unsetConversation(this); +} + +void AbstractConversation::handleMessage(boost::shared_ptr &message) { + message->setTo(m_conversationManager->getUser()->getJID().toBare()); + AbstractBuddy *buddy = m_conversationManager->getUser()->getRosterManager()->getBuddy(m_legacyName); + if (buddy) { + std::cout << m_legacyName << " 222222\n"; + message->setFrom(buddy->getJID()); + } + else { + std::cout << m_legacyName << " 1111111\n"; + // TODO: escape from and setFrom + } + m_conversationManager->getComponent()->getStanzaChannel()->sendMessage(message); +} + +} diff --git a/src/conversationmanager.cpp b/src/conversationmanager.cpp new file mode 100644 index 00000000..3aa76cad --- /dev/null +++ b/src/conversationmanager.cpp @@ -0,0 +1,48 @@ +/** + * XMPP - libpurple transport + * + * Copyright (C) 2009, Jan Kaluza + * + * 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 + */ + +#include "transport/conversationmanager.h" +#include "transport/abstractconversation.h" +#include "transport/usermanager.h" +#include "transport/abstractbuddy.h" +#include "transport/user.h" +#include "Swiften/Roster/SetRosterRequest.h" +#include "Swiften/Elements/RosterPayload.h" +#include "Swiften/Elements/RosterItemPayload.h" + +namespace Transport { + +ConversationManager::ConversationManager(User *user, Component *component){ + m_user = user; + m_component = component; +} + +ConversationManager::~ConversationManager() { +} + +void ConversationManager::setConversation(AbstractConversation *conv) { + m_convs[conv->getLegacyName()] = conv; +} + +void ConversationManager::unsetConversation(AbstractConversation *conv) { + m_convs.erase(conv->getLegacyName()); +} + +} \ No newline at end of file diff --git a/src/rostermanager.cpp b/src/rostermanager.cpp index 777c2d65..dc18158d 100644 --- a/src/rostermanager.cpp +++ b/src/rostermanager.cpp @@ -53,14 +53,14 @@ void RosterManager::sendBuddyRosterPush(AbstractBuddy *buddy) { payload->addItem(item); Swift::SetRosterRequest::ref request = Swift::SetRosterRequest::create(payload, m_component->getIQRouter(), m_user->getJID().toBare()); - request->onResponse.connect(boost::bind(&RosterManager::handleBuddyRosterPushResponse, this, _1, buddy->getSafeName())); + request->onResponse.connect(boost::bind(&RosterManager::handleBuddyRosterPushResponse, this, _1, buddy->getName())); request->send(); } void RosterManager::setBuddyCallback(AbstractBuddy *buddy) { m_setBuddyTimer->onTick.disconnect(boost::bind(&RosterManager::setBuddyCallback, this, buddy)); - m_buddies[buddy->getSafeName()] = buddy; + m_buddies[buddy->getName()] = buddy; onBuddySet(buddy); if (m_component->inServerMode()) { @@ -73,7 +73,7 @@ void RosterManager::setBuddyCallback(AbstractBuddy *buddy) { } void RosterManager::unsetBuddy(AbstractBuddy *buddy) { - m_buddies.erase(buddy->getSafeName()); + m_buddies.erase(buddy->getName()); onBuddyUnset(buddy); } @@ -83,4 +83,8 @@ void RosterManager::handleBuddyRosterPushResponse(Swift::ErrorPayload::ref error } } +AbstractBuddy *RosterManager::getBuddy(const std::string &name) { + return m_buddies[name]; +} + } \ No newline at end of file diff --git a/src/user.cpp b/src/user.cpp index daddfcd7..7b29fa82 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -22,6 +22,7 @@ #include "transport/transport.h" #include "transport/storagebackend.h" #include "transport/rostermanager.h" +#include "transport/conversationmanager.h" #include "Swiften/Swiften.h" namespace Transport { @@ -40,10 +41,12 @@ User::User(const Swift::JID &jid, UserInfo &userInfo, Component *component) { m_reconnectTimer->onTick.connect(boost::bind(&User::onConnectingTimeout, this)); m_rosterManager = new RosterManager(this, m_component); + m_conversationManager = new ConversationManager(this, m_component); } User::~User(){ delete m_rosterManager; + delete m_conversationManager; } const Swift::JID &User::getJID() {