From f755fa298bfb573f88b59cc0b7d27c9311bcc67d Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Sun, 27 Jun 2021 17:33:09 -0500 Subject: [PATCH] (purple) Disconnection of accounts, quit server w/o Cardie --- protocols/purple/PurpleApp.cpp | 31 ++++++++++++++++++++++++++--- protocols/purple/PurpleApp.h | 3 ++- protocols/purple/PurpleMessages.h | 8 ++++++-- protocols/purple/PurpleProtocol.cpp | 21 +++++++++++++------ protocols/purple/PurpleProtocol.h | 4 +++- 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/protocols/purple/PurpleApp.cpp b/protocols/purple/PurpleApp.cpp index e1a0be7..b4eaba7 100644 --- a/protocols/purple/PurpleApp.cpp +++ b/protocols/purple/PurpleApp.cpp @@ -26,7 +26,9 @@ #include #include +#include +#include #include #include "Purple.h" @@ -51,7 +53,8 @@ PurpleApp::PurpleApp() std::cerr << "libpurple initialization failed. Please report!\n"; _GetProtocolsInfo(); - fGRunner = new BMessageRunner(this, new BMessage(G_MAIN_LOOP), 100000, -1); + new BMessageRunner(this, new BMessage(G_MAIN_LOOP), 100000, -1); + new BMessageRunner(this, new BMessage(CHECK_APP), 60000000, -1); } @@ -81,14 +84,13 @@ PurpleApp::MessageReceived(BMessage* msg) break; } - case PURPLE_LOAD_ACCOUNT: + case PURPLE_CONNECT_ACCOUNT: { _ParseCardieSettings(msg); break; } case PURPLE_REGISTER_THREAD: { - msg->PrintToStream(); BString accName = msg->FindString("account_name"); BString username = fAccounts.ValueFor(accName); int64 thread; @@ -98,6 +100,29 @@ PurpleApp::MessageReceived(BMessage* msg) fAccountThreads.AddItem(username, thread); break; } + case PURPLE_REQUEST_DISCONNECT: + { + PurpleAccount* account = _AccountFromMessage(msg); + if (account == NULL) + return; + + BString account_name = msg->FindString("account_name"); + const char* username = purple_account_get_username(account); + fAccountThreads.RemoveItemFor(BString(username)); + fAccounts.RemoveItemFor(account_name); + + purple_account_disconnect(account); + + if (fAccountThreads.CountItems() == 0 || fAccounts.CountItems() == 0) + Quit(); + } + case CHECK_APP: + { + BRoster roster; + if (roster.IsRunning(APP_SIGNATURE) == false) + Quit(); + break; + } case G_MAIN_LOOP: g_main_context_iteration(g_main_loop_get_context(fGloop), false); break; diff --git a/protocols/purple/PurpleApp.h b/protocols/purple/PurpleApp.h index 0962d95..462cc97 100644 --- a/protocols/purple/PurpleApp.h +++ b/protocols/purple/PurpleApp.h @@ -34,6 +34,7 @@ typedef KeyMap Accounts; // Cardie username → Purple usernam typedef KeyMap AccountThreads; // Purple username → Thread const uint32 G_MAIN_LOOP = 'GLml'; +const uint32 CHECK_APP = 'Paca'; #define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) @@ -60,6 +61,7 @@ int main(int argc, char** argv); class PurpleApp : public BApplication { public: PurpleApp(); + virtual void MessageReceived(BMessage* msg); void SendMessage(thread_id thread, BMessage msg); void SendMessage(PurpleAccount* account, BMessage msg); @@ -79,7 +81,6 @@ private: BObjectList fProtocols; GMainLoop* fGloop; - BMessageRunner* fGRunner; }; diff --git a/protocols/purple/PurpleMessages.h b/protocols/purple/PurpleMessages.h index b1a02a6..bcffb47 100644 --- a/protocols/purple/PurpleMessages.h +++ b/protocols/purple/PurpleMessages.h @@ -44,7 +44,7 @@ enum purple_message { * Just the account's settings message from Cardie's end. * It's the server's job to tie the Cardie account name * to the PurpleAccount. */ - PURPLE_LOAD_ACCOUNT = 'PAla', + PURPLE_CONNECT_ACCOUNT = 'PAla', /*! Associate account with thread →Server * Makes the server associate the given account with @@ -52,7 +52,11 @@ enum purple_message { * messages related to the account will be sent to this * thread. * Requires: String account_name, int64 thread_id */ - PURPLE_REGISTER_THREAD = 'PArl' + PURPLE_REGISTER_THREAD = 'PArl', + + /*! Disconnect add-on's account →Server + * Requires: String account_name */ + PURPLE_REQUEST_DISCONNECT = 'Axwx' }; #endif // _PURPLE_MESSAGES_H diff --git a/protocols/purple/PurpleProtocol.cpp b/protocols/purple/PurpleProtocol.cpp index 99138a6..da9e025 100644 --- a/protocols/purple/PurpleProtocol.cpp +++ b/protocols/purple/PurpleProtocol.cpp @@ -19,6 +19,8 @@ #include "PurpleProtocol.h" +#include + #include #include @@ -117,11 +119,8 @@ status_t connect_thread(void* data) { PurpleProtocol* protocol = (PurpleProtocol*)data; - - while (true) { - BMessage msg = receive_message(); - protocol->SendMessage(new BMessage(msg)); - } + while (true) + protocol->SendMessage(new BMessage(receive_message())); } @@ -147,6 +146,12 @@ PurpleProtocol::PurpleProtocol(BString name, BString id, BMessage settings) } +PurpleProtocol::~PurpleProtocol() +{ + Shutdown(); +} + + status_t PurpleProtocol::Init(ChatProtocolMessengerInterface* interface) { @@ -158,6 +163,10 @@ PurpleProtocol::Init(ChatProtocolMessengerInterface* interface) status_t PurpleProtocol::Shutdown() { + BMessage* disconnect = new BMessage(PURPLE_REQUEST_DISCONNECT); + _SendPrplMessage(disconnect); + + kill_thread(fBirdThread); return B_OK; } @@ -174,7 +183,7 @@ PurpleProtocol::UpdateSettings(BMessage* msg) { ensure_app(); fPrplMessenger = new BMessenger(PURPLE_SIGNATURE); - msg->what = PURPLE_LOAD_ACCOUNT; + msg->what = PURPLE_CONNECT_ACCOUNT; _SendPrplMessage(msg); thread_id thread = spawn_thread(connect_thread, "bird_superiority", diff --git a/protocols/purple/PurpleProtocol.h b/protocols/purple/PurpleProtocol.h index 7b55299..b7d3afe 100644 --- a/protocols/purple/PurpleProtocol.h +++ b/protocols/purple/PurpleProtocol.h @@ -44,6 +44,8 @@ public: PurpleProtocol(BString name, BString id, BMessage settings); + ~PurpleProtocol(); + // ChatProtocol inheritance virtual status_t Init(ChatProtocolMessengerInterface* interface); virtual status_t Shutdown(); @@ -81,7 +83,7 @@ private: ChatProtocolMessengerInterface* fMessenger; BMessenger* fPrplMessenger; - thread_id fServerThread; + thread_id fBirdThread; BString fName; BPath fAddOnPath;