From dd77f246f8f3e02a6da92ad1eda45e2a554634e0 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Tue, 15 Jun 2021 00:59:00 -0500 Subject: [PATCH] Per-protocol commands, precedence over default commands Fixes #10 --- application/Conversation.cpp | 2 +- application/ProtocolLooper.cpp | 17 ++++++++++++++++- application/ProtocolLooper.h | 7 ++++++- application/Server.cpp | 23 +++++++++++++++++++---- application/Server.h | 2 +- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/application/Conversation.cpp b/application/Conversation.cpp index feb4949..e481ce3 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -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.")); diff --git a/application/ProtocolLooper.cpp b/application/ProtocolLooper.cpp index bd19c0f..c7cdaca 100644 --- a/application/ProtocolLooper.cpp +++ b/application/ProtocolLooper.cpp @@ -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() { diff --git a/application/ProtocolLooper.h b/application/ProtocolLooper.h index c7f9020..9f4f69e 100644 --- a/application/ProtocolLooper.h +++ b/application/ProtocolLooper.h @@ -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 #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; diff --git a/application/Server.cpp b/application/Server.cpp index 36ab0a0..366b693 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -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; } diff --git a/application/Server.h b/application/Server.h index 55cccb2..3e76c51 100644 --- a/application/Server.h +++ b/application/Server.h @@ -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);