cmdman.h 6.3 KB

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