cmdman.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * Help strings and method prototypes for commands to be used by a user
  88. */
  89. /* execute command descriptions and methods go here */
  90. const string descHelp = "print available commands";
  91. CmdRet cmdHelp(vector<string> args);
  92. const string descStatus = "request basic status information from server";
  93. CmdRet cmdStatus(vector<string> args);
  94. const string descExtendedstatus = "request detailed status information from server with running transfers";
  95. CmdRet cmdExtendedstatus(vector<string> args);
  96. const string descDisconnect = "disconnect from server";
  97. CmdRet cmdDisconnect(vector<string> args);
  98. const string descPut = "upload file to server and add to queue";
  99. CmdRet cmdPut(vector<string> args);
  100. const string descGet = "retrieve file from server";
  101. CmdRet cmdGet(vector<string> args);
  102. const string descList = "list files available on server";
  103. CmdRet cmdList(vector<string> args);
  104. const string descHead = "request the first four bytes of a file from the server";
  105. CmdRet cmdHead(vector<string> args);
  106. const string descDeletefile = "delete a file from the server";
  107. CmdRet cmdDeletefile(vector<string> args);
  108. const string descKeyfile = "set keyfile to use";
  109. CmdRet cmdKeyfile(vector<string> args);
  110. const string descClosekey = "stop using the previously selected keyfile";
  111. CmdRet cmdClosekey(vector<string> args);
  112. const string descNotifications = "request notifications from the server";
  113. CmdRet cmdNotifications(vector<string> args);
  114. const string descConnect = "connect to server";
  115. CmdRet cmdConnect(vector<string> args);
  116. const string descExit = "exit the application";
  117. CmdRet cmdExit(vector<string> args);
  118. const string descLogin = "login to the server";
  119. CmdRet cmdLogin(vector<string> args);
  120. const string descSignup = "sign up and login to the server";
  121. CmdRet cmdSignup(vector<string> args);
  122. const string descDeleteme = "delete the user you are currently logged in as "
  123. "(needs to be confirmed with the password)";
  124. CmdRet cmdDeleteme(vector<string> args);
  125. const string descQueue = "add a file that is already on the server to the queue for sending with the covert channel";
  126. CmdRet cmdQueue(vector<string> args);
  127. const string descDequeue = "remove a file from the queue for sending with the covert channel";
  128. CmdRet cmdDequeue(vector<string> args);
  129. /**
  130. * Method prototypes for commands used internally
  131. */
  132. /* internal execute commands */
  133. CmdRet cmdVersion(vector<string> args);
  134. CmdRet cmdPutdata(vector<string> args);
  135. CmdRet cmdGetdata(vector<string> args);
  136. CmdRet cmdListdata(vector<string> args);
  137. /**
  138. * Method prototypes for handling json responses
  139. */
  140. /* handle commands go here */
  141. CmdRet handleStatus(Json::Value);
  142. CmdRet handleExtendedstatus(Json::Value);
  143. CmdRet handleClose(Json::Value);
  144. CmdRet handlePut(Json::Value);
  145. CmdRet handleGet(Json::Value);
  146. CmdRet handleList(Json::Value);
  147. CmdRet handlePutdata(Json::Value);
  148. CmdRet handleGetdata(Json::Value);
  149. CmdRet handleListdata(Json::Value);
  150. CmdRet handleVersion(Json::Value);
  151. CmdRet handleLogin(Json::Value);
  152. CmdRet handleSignup(Json::Value);
  153. CmdRet handleHead(Json::Value);
  154. CmdRet handleDeletefile(Json::Value);
  155. CmdRet handleDeleteme(Json::Value);
  156. CmdRet handleQueue(Json::Value);
  157. CmdRet handleDequeue(Json::Value);
  158. CmdRet handleNotifications(Json::Value);
  159. };
  160. #endif