From 7d72b5152c7f9ffbaf2de02a5b74abf251975d79 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Tue, 15 Jun 2021 00:45:51 -0500 Subject: [PATCH] Add '/help' command --- application/CayaMessages.h | 5 ++++- application/CayaUtils.cpp | 5 +++-- application/Conversation.cpp | 5 ++++- application/Server.cpp | 42 +++++++++++++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/application/CayaMessages.h b/application/CayaMessages.h index 8b6d2e3..c1da0ad 100644 --- a/application/CayaMessages.h +++ b/application/CayaMessages.h @@ -44,7 +44,10 @@ const uint32 CAYA_MOVE_UP = 'CYmu'; //! Select the downward conversation const uint32 CAYA_MOVE_DOWN = 'CYmd'; -//! Select the downward conversation +//! Disable a given account const uint32 CAYA_DISABLE_ACCOUNT = 'CYda'; +//! Request a "help" message +const uint32 CAYA_REQUEST_HELP = 'CYhm'; + #endif // _CAYA_MESSAGES_H diff --git a/application/CayaUtils.cpp b/application/CayaUtils.cpp index 2ad63fa..e32adb6 100644 --- a/application/CayaUtils.cpp +++ b/application/CayaUtils.cpp @@ -61,8 +61,9 @@ BString CommandArgs(BString line) { BString remove("/"); - remove << CommandName(line) << " "; - return line.RemoveFirst(remove); + remove << CommandName(line) << ""; + line.RemoveFirst(remove); + return line.Trim(); } diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 0272c3b..feb4949 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -97,7 +97,7 @@ Conversation::ImMessage(BMessage* msg) ChatCommand* cmd = _GetServer()->CommandById(name); if (cmd == NULL) { - _WarnUser(BString("That isn't a valid command.")); + _WarnUser(BString("That isn't a valid command. Try /help for a list.")); break; } @@ -370,6 +370,9 @@ Conversation::_LogChatMessage(BMessage* msg) BString id = msg->FindString("user_id"); BString body = msg->FindString("body"); + if (id.IsEmpty() == true) + return; + // Binary logs // TODO: Don't hardcode 21, expose maximum as a setting BStringList users, bodies; diff --git a/application/Server.cpp b/application/Server.cpp index 2b3b127..36ab0a0 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -109,11 +109,46 @@ Server::Filter(BMessage* message, BHandler **target) result = B_SKIP_MESSAGE; break; } - RemoveProtocolLooper(instance); break; } + case CAYA_REQUEST_HELP: + { + BString body; + BString cmd_name = message->FindString("misc_str"); + Conversation* chat = _EnsureConversation(message); + + if (chat == NULL) + break; + + if (cmd_name.IsEmpty() == false) { + ChatCommand* cmd = CommandById(cmd_name); + if (cmd == NULL) + body = "-- That command doesn't exist. Try '/help' for a " + "list.\n"; + else { + body = "** "; + body << cmd->GetName() << " ― " << cmd->GetDesc() << "\n"; + } + } + else { + body << "** Commands: "; + for (int i = 0; i < fCommands.CountItems(); i++) { + ChatCommand* cmd = fCommands.ValueAt(i); + if (i > 0) body << ", "; + body << cmd->GetName(); + } + body << "\n"; + } + BMessage* help = new BMessage(IM_MESSAGE); + help->AddInt32("im_what", IM_MESSAGE_RECEIVED); + help->AddString("body", body); + help->AddInt64("when", 0); + chat->ImMessage(help); + break; + } + default: // Dispatch not handled messages to main window break; @@ -911,6 +946,11 @@ Server::_InitDefaultCommands() ChatCommand* invite = new ChatCommand("invite", inviteMsg, true, knownUser); invite->SetDesc("Invite a user to the current room."); fCommands.AddItem("invite", invite); + + BMessage helpMsg(CAYA_REQUEST_HELP); + ChatCommand* help = new ChatCommand("help", helpMsg, false, List()); + help->SetDesc("List all current commands, or get help for certain command."); + fCommands.AddItem("help", help); }