ChatCommand.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
  3. * All rights reserved. Distributed under the terms of the MIT license.
  4. */
  5. #ifndef CHAT_COMMAND_H
  6. #define CHAT_COMMAND_H
  7. #include <Archivable.h>
  8. #include <Message.h>
  9. #include <String.h>
  10. #include <libsupport/KeyMap.h>
  11. #include <libsupport/List.h>
  12. class Conversation;
  13. class User;
  14. typedef KeyMap<BString, User*> UserMap;
  15. enum cmd_arg_type
  16. {
  17. CMD_ROOM_PARTICIPANT = 'CArp', // "user_id" in message
  18. CMD_KNOWN_USER = 'CAku', // "user_id" in message
  19. CMD_ANY_USER = 'CAau', // "user_id" in message
  20. CMD_BODY_STRING = 'CAbs', // "body" in message
  21. CMD_MISC_STRING = 'CAms' // "misc_str" in message
  22. };
  23. class ChatCommand : public BArchivable {
  24. public:
  25. ChatCommand(const char* name, BMessage msg, bool toProtocol,
  26. List<int32> argTypes);
  27. ChatCommand(BMessage* data);
  28. status_t Archive(BMessage* data, bool deep=true);
  29. ChatCommand* Instantiate(BMessage* data);
  30. const char* GetName() { return fName.String(); }
  31. void SetDesc(const char* description);
  32. const char* GetDesc() { return fDescription.String(); }
  33. bool Parse(BString args, BString* errorMsg, Conversation* chat);
  34. private:
  35. bool _ProcessArgs(BString args, BMessage* msg, BString* errorMsg,
  36. Conversation* chat);
  37. User* _FindUser(BString idOrName, UserMap users);
  38. bool _Send(BMessage* msg, Conversation* chat);
  39. BString fName;
  40. BString fDescription;
  41. BMessage fMessage;
  42. bool fToProto;
  43. List<int32> fArgTypes;
  44. };
  45. typedef KeyMap<BString, ChatCommand*> CommandMap;
  46. #endif // CHAT_COMMAND_H