cmdman.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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", "status"};
  113. vector<string> cmdAllowAfterConnect = {"login", "signup", "disconnect"};
  114. /**
  115. * Fields used for status output.
  116. */
  117. string username, ip;
  118. unsigned int port;
  119. /**
  120. * Help strings and method prototypes for commands to be used by a user
  121. */
  122. /* execute command descriptions and methods go here */
  123. const string descHelp = "print available commands";
  124. CmdRet cmdHelp(vector<string> args);
  125. const string descStatus = "request basic status information from server";
  126. CmdRet cmdStatus(vector<string> args);
  127. const string descExtendedstatus = "request detailed status information from server about running transfers";
  128. CmdRet cmdExtendedstatus(vector<string> args);
  129. const string descDisconnect = "disconnect from server";
  130. CmdRet cmdDisconnect(vector<string> args);
  131. const string descPut = "upload file to server";
  132. CmdRet cmdPut(vector<string> args);
  133. const string descGet = "retrieve file from server";
  134. CmdRet cmdGet(vector<string> args);
  135. const string descList = "list names of files available on server";
  136. CmdRet cmdList(vector<string> args);
  137. const string descExtendedlist = "list files available on server with further information";
  138. CmdRet cmdExtendedlist(vector<string> args);
  139. const string descHead = "request the first few bytes of a file from the server";
  140. CmdRet cmdHead(vector<string> args);
  141. const string descDeletefile = "delete a file from the server";
  142. CmdRet cmdDeletefile(vector<string> args);
  143. const string descKeyfile = "set keyfile to use for encryption";
  144. CmdRet cmdKeyfile(vector<string> args);
  145. const string descClosekey = "stop using the previously selected keyfile";
  146. CmdRet cmdClosekey(vector<string> args);
  147. const string descNotifications = "request notifications from the server";
  148. CmdRet cmdNotifications(vector<string> args);
  149. const string descConnect = "connect to server";
  150. CmdRet cmdConnect(vector<string> args);
  151. const string descExit = "exit the application";
  152. CmdRet cmdExit(vector<string> args);
  153. const string descLogin = "login to the server";
  154. CmdRet cmdLogin(vector<string> args);
  155. const string descSignup = "sign up and login to the server";
  156. CmdRet cmdSignup(vector<string> args);
  157. const string descDeleteme = "delete the user you are currently logged in as (needs to be confirmed with the password)";
  158. CmdRet cmdDeleteme(vector<string> args);
  159. const string descQueue = "add a file that is already on the server to the queue for sending with the covert channel";
  160. CmdRet cmdQueue(vector<string> args);
  161. const string descDequeue = "remove a file from the queue for sending with the covert channel";
  162. CmdRet cmdDequeue(vector<string> args);
  163. /**
  164. * Method prototypes for commands used internally
  165. */
  166. /* internal execute commands */
  167. CmdRet cmdVersion(vector<string> args);
  168. CmdRet cmdPutdata(vector<string> args);
  169. CmdRet cmdGetdata(vector<string> args);
  170. CmdRet cmdListdata(vector<string> args);
  171. CmdRet cmdExtendedlistdata(vector<string> args);
  172. /**
  173. * Method prototypes for handling json responses
  174. */
  175. /* handle commands go here */
  176. CmdRet handleStatus(Json::Value);
  177. CmdRet handleExtendedstatus(Json::Value);
  178. CmdRet handleClose(Json::Value);
  179. CmdRet handlePut(Json::Value);
  180. CmdRet handleGet(Json::Value);
  181. CmdRet handleList(Json::Value);
  182. CmdRet handleExtendedlist(Json::Value);
  183. CmdRet handlePutdata(Json::Value);
  184. CmdRet handleGetdata(Json::Value);
  185. CmdRet handleListdata(Json::Value);
  186. CmdRet handleExtendedlistdata(Json::Value);
  187. CmdRet handleVersion(Json::Value);
  188. CmdRet handleLogin(Json::Value);
  189. CmdRet handleSignup(Json::Value);
  190. CmdRet handleHead(Json::Value);
  191. CmdRet handleDeletefile(Json::Value);
  192. CmdRet handleDeleteme(Json::Value);
  193. CmdRet handleQueue(Json::Value);
  194. CmdRet handleDequeue(Json::Value);
  195. CmdRet handleNotifications(Json::Value);
  196. };
  197. #endif