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 name = CommandName(body);
BString args = CommandArgs(body); BString args = CommandArgs(body);
ChatCommand* cmd = _GetServer()->CommandById(name); ChatCommand* cmd = _GetServer()->CommandById(name, fLooper->GetInstance());
if (cmd == NULL) { if (cmd == NULL) {
_WarnUser(BString("That isn't a valid command. Try /help for a list.")); _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, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. 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. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Andrea Anzani, andrea.anzani@gmail.com * Andrea Anzani, andrea.anzani@gmail.com
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
* Jaidyn Levesque, jadedctrl@teknik.io
*/ */
#include "ProtocolLooper.h" #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 BString
ProtocolLooper::GetOwnId() 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, Andrea Anzani. All rights reserved.
* Copyright 2009-2011, Pier Luigi Fiorini. 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. * Distributed under the terms of the MIT License.
*/ */
#ifndef _PROTOCOL_LOOPER_H #ifndef _PROTOCOL_LOOPER_H
@ -13,6 +13,7 @@
#include <libsupport/KeyMap.h> #include <libsupport/KeyMap.h>
#include "CayaProtocol.h" #include "CayaProtocol.h"
#include "ChatCommand.h"
class Contact; class Contact;
class Conversation; class Conversation;
@ -47,6 +48,9 @@ public:
User* UserById(BString id); User* UserById(BString id);
void AddUser(User* user); void AddUser(User* user);
CommandMap Commands() const;
ChatCommand* CommandById(BString id);
BString GetOwnId(); BString GetOwnId();
void SetOwnId(BString user_id); void SetOwnId(BString user_id);
@ -64,6 +68,7 @@ private:
ChatMap fChatMap; ChatMap fChatMap;
RosterMap fRosterMap; RosterMap fRosterMap;
UserMap fUserMap; UserMap fUserMap;
CommandMap fCommands;
ConversationAccountItem* ConversationAccountItem*
fListItem; fListItem;

View File

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