Per-protocol commands, precedence over default commands

Fixes #10
This commit is contained in:
Jaidyn Ann 2021-06-15 00:59:00 -05:00
parent 7d72b5152c
commit dd77f246f8
5 changed files with 43 additions and 8 deletions

View File

@ -94,7 +94,7 @@ Conversation::ImMessage(BMessage* msg)
BString name = CommandName(body);
BString args = CommandArgs(body);
ChatCommand* cmd = _GetServer()->CommandById(name);
ChatCommand* cmd = _GetServer()->CommandById(name, fLooper->GetInstance());
if (cmd == NULL) {
_WarnUser(BString("That isn't a valid command. Try /help for a list."));

View File

@ -1,12 +1,13 @@
/*
* Copyright 2021, Jaidyn Levesque. All rights reserved.
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
* Copyright 2021, Jaidyn Levesque. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Andrea Anzani, andrea.anzani@gmail.com
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
* Jaidyn Levesque, jadedctrl@teknik.io
*/
#include "ProtocolLooper.h"
@ -142,6 +143,20 @@ ProtocolLooper::AddUser(User* user)
}
CommandMap
ProtocolLooper::Commands() const
{
return fCommands;
}
ChatCommand*
ProtocolLooper::CommandById(BString id)
{
return fCommands.ValueFor(id);
}
BString
ProtocolLooper::GetOwnId()
{

View File

@ -1,7 +1,7 @@
/*
* Copyright 2021, Jaidyn Levesque. All rights reserved.
* Copyright 2009-2011, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
* Copyright 2021, Jaidyn Levesque. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _PROTOCOL_LOOPER_H
@ -13,6 +13,7 @@
#include <libsupport/KeyMap.h>
#include "CayaProtocol.h"
#include "ChatCommand.h"
class Contact;
class Conversation;
@ -47,6 +48,9 @@ public:
User* UserById(BString id);
void AddUser(User* user);
CommandMap Commands() const;
ChatCommand* CommandById(BString id);
BString GetOwnId();
void SetOwnId(BString user_id);
@ -64,6 +68,7 @@ private:
ChatMap fChatMap;
RosterMap fRosterMap;
UserMap fUserMap;
CommandMap fCommands;
ConversationAccountItem*
fListItem;

View File

@ -117,13 +117,14 @@ Server::Filter(BMessage* message, BHandler **target)
{
BString body;
BString cmd_name = message->FindString("misc_str");
int64 instance = message->FindInt64("instance");
Conversation* chat = _EnsureConversation(message);
if (chat == NULL)
break;
if (cmd_name.IsEmpty() == false) {
ChatCommand* cmd = CommandById(cmd_name);
ChatCommand* cmd = CommandById(cmd_name, instance);
if (cmd == NULL)
body = "-- That command doesn't exist. Try '/help' for a "
"list.\n";
@ -682,7 +683,6 @@ UserMap
Server::Users() const
{
UserMap users;
for (int i = 0; i < fAccounts.CountItems(); i++) {
ProtocolLooper* fruitLoop = fLoopers.ValueFor(fAccounts.ValueAt(i));
if (fruitLoop == NULL) continue;
@ -757,14 +757,29 @@ Server::AddConversation(Conversation* chat, int64 instance)
CommandMap
Server::Commands()
{
CommandMap commands = fCommands;
for (int i = 0; i < fAccounts.CountItems(); i++) {
ProtocolLooper* fruitLoop = fLoopers.ValueFor(fAccounts.ValueAt(i));
if (fruitLoop == NULL) continue;
CommandMap cmds = fruitLoop->Commands();
for (int i = 0; i < cmds.CountItems(); i++)
commands.AddItem(cmds.KeyAt(i), cmds.ValueAt(i));
}
return fCommands;
}
ChatCommand*
Server::CommandById(BString id)
Server::CommandById(BString id, int64 instance)
{
return fCommands.ValueFor(id);
ProtocolLooper* looper = fLoopers.ValueFor(instance);
ChatCommand* result = NULL;
if (looper != NULL)
result = looper->CommandById(id);
if (result == NULL)
result = fCommands.ValueFor(id);
return result;
}

View File

@ -60,7 +60,7 @@ public:
void AddConversation(Conversation* chat, int64 instance);
CommandMap Commands();
ChatCommand* CommandById(BString id);
ChatCommand* CommandById(BString id, int64 instance);
private:
ProtocolLooper* _LooperFromMessage(BMessage* message);