cmdman.h 6.5 KB

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