cmdman.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef CMDMAN_H
  2. #define CMDMAN_H
  3. #include "fileman.h"
  4. #include <json/json.h>
  5. #include <map>
  6. #include <string>
  7. #include <vector>
  8. using std::map;
  9. using std::string;
  10. using std::vector;
  11. /**
  12. * @class CmdMan
  13. *
  14. * A class that provides handling of user provided commands as well as
  15. * commands as provided by json responses from a server.
  16. */
  17. class CmdMan {
  18. public:
  19. /**
  20. * Flags for type of message returned in CmdRet.
  21. * print - print something to the user
  22. * send - send something to the server
  23. * error - an error occured, do not send to the server
  24. * close - terminate the connection
  25. * seton - contextually change state, used for version check and login
  26. */
  27. enum rettype { none = 0, print = (1 << 1), send = (1 << 2), error = (1 << 3), close = (1 << 4), connect = (1 << 5), exit = (1 << 6) };
  28. /**
  29. * Response to user or command input
  30. *
  31. * msg is to be handled depending on type
  32. * if nextcommand isnt an empty string it should be handled;
  33. */
  34. struct CmdRet {
  35. unsigned int type;
  36. string nextcommand;
  37. Json::Value msg;
  38. };
  39. /**
  40. * Constructor and destructor
  41. */
  42. CmdMan(FileMan &fm, void (*dpf)(string));
  43. ~CmdMan();
  44. /**
  45. * Executes a user provided command with optional arguments
  46. */
  47. CmdRet execute(string cmd, vector<string> args);
  48. /**
  49. * Handles a server provided response json
  50. */
  51. CmdRet handle(Json::Value root);
  52. /**
  53. * Internal json reader
  54. */
  55. Json::CharReader *reader;
  56. void stateSetConnectionOk();
  57. protected:
  58. /**
  59. * State used to internally format received json to allow easy handling using
  60. * handlemap and to disallow the use of certain commands when logged in/not
  61. * logged in.
  62. */
  63. enum state { connectionpossible, versionpossible, doversion, loginpossible, dologin, dosignup, normal, disconnecttoexit, disconnecttoexitearly };
  64. state currentState;
  65. private:
  66. /**
  67. * internal json writer and error string member
  68. */
  69. Json::StreamWriterBuilder wbuilder;
  70. string jsonerror;
  71. /**
  72. * FileMan instance used to file commands
  73. */
  74. FileMan &fileman;
  75. void (*debugprintfunc)(string);
  76. /**
  77. * Maps containing pointers to the appropriate member functions for executing
  78. * or handling commands
  79. */
  80. map<string, CmdRet (CmdMan::*)(vector<string>)> execmap;
  81. map<string, CmdRet (CmdMan::*)(Json::Value)> handlemap;
  82. /**
  83. * Map containing help strings to show to a user
  84. */
  85. map<string, string> helpmap;
  86. /**
  87. * Vectors containing command strings that should either be usable in any
  88. * state or after connecting but not logging in
  89. */
  90. vector<string> cmdAllowAlways = {"help", "keyfile", "closekey", "exit"};
  91. vector<string> cmdAllowAfterConnect = {"login", "signup", "disconnect"};
  92. /**
  93. * Help strings and method prototypes for commands to be used by a user
  94. */
  95. /* execute command descriptions and methods go here */
  96. const string descHelp = "print available commands";
  97. CmdRet cmdHelp(vector<string> args);
  98. const string descStatus = "request basic status information from server";
  99. CmdRet cmdStatus(vector<string> args);
  100. const string descExtendedstatus = "request detailed status information from server about running transfers";
  101. CmdRet cmdExtendedstatus(vector<string> args);
  102. const string descDisconnect = "disconnect from server";
  103. CmdRet cmdDisconnect(vector<string> args);
  104. const string descPut = "upload file to server";
  105. CmdRet cmdPut(vector<string> args);
  106. const string descGet = "retrieve file from server";
  107. CmdRet cmdGet(vector<string> args);
  108. const string descList = "list names of files available on server";
  109. CmdRet cmdList(vector<string> args);
  110. const string descExtendedlist = "list files available on server with further information";
  111. CmdRet cmdExtendedlist(vector<string> args);
  112. const string descHead = "request the first few bytes of a file from the server";
  113. CmdRet cmdHead(vector<string> args);
  114. const string descDeletefile = "delete a file from the server";
  115. CmdRet cmdDeletefile(vector<string> args);
  116. const string descKeyfile = "set keyfile to use for encryption";
  117. CmdRet cmdKeyfile(vector<string> args);
  118. const string descClosekey = "stop using the previously selected keyfile";
  119. CmdRet cmdClosekey(vector<string> args);
  120. const string descNotifications = "request notifications from the server";
  121. CmdRet cmdNotifications(vector<string> args);
  122. const string descConnect = "connect to server";
  123. CmdRet cmdConnect(vector<string> args);
  124. const string descExit = "exit the application";
  125. CmdRet cmdExit(vector<string> args);
  126. const string descLogin = "login to the server";
  127. CmdRet cmdLogin(vector<string> args);
  128. const string descSignup = "sign up and login to the server";
  129. CmdRet cmdSignup(vector<string> args);
  130. const string descDeleteme = "delete the user you are currently logged in as (needs to be confirmed with the password)";
  131. CmdRet cmdDeleteme(vector<string> args);
  132. const string descQueue = "add a file that is already on the server to the queue for sending with the covert channel";
  133. CmdRet cmdQueue(vector<string> args);
  134. const string descDequeue = "remove a file from the queue for sending with the covert channel";
  135. CmdRet cmdDequeue(vector<string> args);
  136. /**
  137. * Method prototypes for commands used internally
  138. */
  139. /* internal execute commands */
  140. CmdRet cmdVersion(vector<string> args);
  141. CmdRet cmdPutdata(vector<string> args);
  142. CmdRet cmdGetdata(vector<string> args);
  143. CmdRet cmdListdata(vector<string> args);
  144. CmdRet cmdExtendedlistdata(vector<string> args);
  145. /**
  146. * Method prototypes for handling json responses
  147. */
  148. /* handle commands go here */
  149. CmdRet handleStatus(Json::Value);
  150. CmdRet handleExtendedstatus(Json::Value);
  151. CmdRet handleClose(Json::Value);
  152. CmdRet handlePut(Json::Value);
  153. CmdRet handleGet(Json::Value);
  154. CmdRet handleList(Json::Value);
  155. CmdRet handleExtendedlist(Json::Value);
  156. CmdRet handlePutdata(Json::Value);
  157. CmdRet handleGetdata(Json::Value);
  158. CmdRet handleListdata(Json::Value);
  159. CmdRet handleExtendedlistdata(Json::Value);
  160. CmdRet handleVersion(Json::Value);
  161. CmdRet handleLogin(Json::Value);
  162. CmdRet handleSignup(Json::Value);
  163. CmdRet handleHead(Json::Value);
  164. CmdRet handleDeletefile(Json::Value);
  165. CmdRet handleDeleteme(Json::Value);
  166. CmdRet handleQueue(Json::Value);
  167. CmdRet handleDequeue(Json::Value);
  168. CmdRet handleNotifications(Json::Value);
  169. };
  170. #endif