(purple) Disconnection of accounts, quit server w/o Cardie

This commit is contained in:
Jaidyn Ann 2021-06-27 17:33:09 -05:00
parent 588b32b9c3
commit f755fa298b
5 changed files with 54 additions and 13 deletions

View File

@ -26,7 +26,9 @@
#include <libpurple/purple.h>
#include <MessageRunner.h>
#include <Roster.h>
#include <Cardie.h>
#include <ChatProtocolMessages.h>
#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;

View File

@ -34,6 +34,7 @@ typedef KeyMap<BString, BString> Accounts; // Cardie username → Purple usernam
typedef KeyMap<BString, thread_id> 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<ProtocolInfo> fProtocols;
GMainLoop* fGloop;
BMessageRunner* fGRunner;
};

View File

@ -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

View File

@ -19,6 +19,8 @@
#include "PurpleProtocol.h"
#include <iostream>
#include <Application.h>
#include <Roster.h>
@ -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",

View File

@ -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;