(purple) Register per-protocol commands

This commit is contained in:
Jaidyn Ann 2021-07-05 23:56:59 -05:00
parent d9051766d9
commit 91c7e44b44
5 changed files with 77 additions and 9 deletions

View File

@ -109,6 +109,8 @@ PurpleApp::MessageReceived(BMessage* msg)
|| msg->FindInt64("thread_id", &thread) != B_OK) || msg->FindInt64("thread_id", &thread) != B_OK)
break; break;
fAccountThreads.AddItem(username, thread); fAccountThreads.AddItem(username, thread);
SendMessage(thread, _GetCommands(_AccountFromMessage(msg)));
break; break;
} }
case PURPLE_REQUEST_DISCONNECT: case PURPLE_REQUEST_DISCONNECT:
@ -563,6 +565,42 @@ PurpleApp::_GetRoomTemplate(PurplePluginProtocolInfo* info)
} }
BMessage
PurpleApp::_GetCommands(PurpleAccount* account)
{
PurpleConversation* conv = purple_conversation_new(PURPLE_CONV_TYPE_ANY,
account, NULL);
BMessage cmdMsgs(PURPLE_REGISTER_COMMANDS);
GList* cmds = purple_cmd_list(conv);
for (int i = 0; cmds != NULL; cmds = cmds->next) {
const char* cmd_name = (const char*)cmds->data;
if (cmd_name == NULL) break;;
BMessage cmdMsg;
cmdMsg.AddString("class", "ChatCommand");
cmdMsg.AddString("_name", cmd_name);
cmdMsg.AddBool("_proto", true);
cmdMsg.AddInt32("_argtype", 0);
BString helpString;
GList* helps = purple_cmd_help(NULL, cmd_name);
for (int j = 0; helps != NULL; helps = helps->next)
helpString << (const char*)helps->data;
cmdMsg.AddString("_desc", helpString);
BMessage cmdActionMsg(IM_MESSAGE);
cmdActionMsg.AddInt32("im_what", PURPLE_CHAT_COMMAND);
cmdActionMsg.AddString("cmd_name", cmd_name);
cmdMsg.AddMessage("_msg", &cmdActionMsg);
cmdMsgs.AddMessage("command", &cmdMsg);
}
return cmdMsgs;
}
void void
PurpleApp::_ParseAccountTemplate(BMessage* settings) PurpleApp::_ParseAccountTemplate(BMessage* settings)
{ {

View File

@ -89,6 +89,7 @@ private:
BMessage _GetAccountTemplate(PurplePluginProtocolInfo* info); BMessage _GetAccountTemplate(PurplePluginProtocolInfo* info);
BMessage _GetRoomTemplate(PurplePluginProtocolInfo* info); BMessage _GetRoomTemplate(PurplePluginProtocolInfo* info);
BMessage _GetCommands(PurpleAccount* account);
void _ParseAccountTemplate(BMessage* settings); void _ParseAccountTemplate(BMessage* settings);
GHashTable* _ParseRoomTemplate(BMessage* msg); GHashTable* _ParseRoomTemplate(BMessage* msg);

View File

@ -56,7 +56,15 @@ enum purple_message {
/*! Disconnect add-on's account →Server /*! Disconnect add-on's account →Server
Requires: String account_name */ Requires: String account_name */
PURPLE_REQUEST_DISCONNECT = 'Axwx' PURPLE_REQUEST_DISCONNECT = 'Axwx',
/*! Register chat commands with proto →Protocol */
PURPLE_REGISTER_COMMANDS = 'Scmd',
/*! User has typed a command, process! →Server
Forwarded from Cardie.
Requires: String chat_id, String cmd_name, String misc_str */
PURPLE_CHAT_COMMAND = 'Pcmd'
}; };
#endif // _PURPLE_MESSAGES_H #endif // _PURPLE_MESSAGES_H

View File

@ -121,8 +121,16 @@ status_t
connect_thread(void* data) connect_thread(void* data)
{ {
PurpleProtocol* protocol = (PurpleProtocol*)data; PurpleProtocol* protocol = (PurpleProtocol*)data;
while (true) while (true) {
protocol->SendMessage(new BMessage(receive_message())); BMessage* msg = new BMessage(receive_message());
switch (msg->what) {
case PURPLE_REGISTER_COMMANDS:
protocol->Process(msg);
break;
default:
protocol->SendMessage(msg);
}
}
} }
@ -172,13 +180,25 @@ PurpleProtocol::Shutdown()
return B_OK; return B_OK;
} }
status_t status_t
PurpleProtocol::Process(BMessage* msg) PurpleProtocol::Process(BMessage* msg)
{ {
if (msg->what == IM_MESSAGE) { switch(msg->what)
_SendPrplMessage(msg); {
return B_OK; case IM_MESSAGE:
_SendPrplMessage(msg);
return B_OK;
case PURPLE_REGISTER_COMMANDS:
{
BMessage cmd;
for (int i = 0; msg->FindMessage("command", i, &cmd) == B_OK; i++)
fCommands.AddItem(new BMessage(cmd));
BMessage* reload = new BMessage(IM_MESSAGE);
reload->AddInt32("im_what", IM_PROTOCOL_RELOAD_COMMANDS);
SendMessage(reload);
break;
}
} }
return B_ERROR; return B_ERROR;
} }
@ -192,7 +212,7 @@ PurpleProtocol::UpdateSettings(BMessage* msg)
msg->what = PURPLE_CONNECT_ACCOUNT; msg->what = PURPLE_CONNECT_ACCOUNT;
_SendPrplMessage(msg); _SendPrplMessage(msg);
thread_id thread = spawn_thread(connect_thread, "bird_superiority", thread_id thread = spawn_thread(connect_thread, "fly_away_little_bird",
B_NORMAL_PRIORITY, (void*)this); B_NORMAL_PRIORITY, (void*)this);
if (thread < B_OK) if (thread < B_OK)
@ -225,7 +245,7 @@ PurpleProtocol::SettingsTemplate(const char* name)
BObjectList<BMessage> BObjectList<BMessage>
PurpleProtocol::Commands() PurpleProtocol::Commands()
{ {
return BObjectList<BMessage>(); return fCommands;
} }

View File

@ -93,6 +93,7 @@ private:
BString fSignature; BString fSignature;
BString fFriendlySignature; BString fFriendlySignature;
BMessage fTemplates; BMessage fTemplates;
BObjectList<BMessage> fCommands;
}; };