userioman.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include "../include/userioman.h"
  2. #include <iostream>
  3. #include <readline/readline.h>
  4. #include <vector>
  5. UserIoMan::UserIoMan(bool usessl, const char *certfile, bool beverbose) : IoMan(usessl, certfile) {
  6. /* setup json stuff */
  7. Json::CharReaderBuilder rbuilder;
  8. wbuilder.settings_["indentation"] = "";
  9. reader = rbuilder.newCharReader();
  10. verbose = beverbose;
  11. /* initialize print command map */
  12. printmap["error"] = &UserIoMan::printError;
  13. printmap["connect"] = &UserIoMan::printConnect;
  14. printmap["help"] = &UserIoMan::printHelp;
  15. printmap["extendedstatus"] = &UserIoMan::printExtendedstatus;
  16. printmap["status"] = &UserIoMan::printStatus;
  17. printmap["disconnect"] = &UserIoMan::printDisconnect;
  18. printmap["put"] = &UserIoMan::printPut;
  19. printmap["get"] = &UserIoMan::printGet;
  20. printmap["list"] = &UserIoMan::printList;
  21. printmap["extendedlist"] = &UserIoMan::printExtendedlist;
  22. printmap["version"] = &UserIoMan::printVersion;
  23. printmap["login"] = &UserIoMan::printLogin;
  24. printmap["signup"] = &UserIoMan::printSignup;
  25. printmap["putdata"] = &UserIoMan::printPutdata;
  26. printmap["getdata"] = &UserIoMan::printGetdata;
  27. printmap["head"] = &UserIoMan::printHead;
  28. printmap["deletefile"] = &UserIoMan::printDeletefile;
  29. printmap["deleteme"] = &UserIoMan::printDeleteme;
  30. printmap["queue"] = &UserIoMan::printQueue;
  31. printmap["dequeue"] = &UserIoMan::printDequeue;
  32. printmap["keyfile"] = &UserIoMan::printKeyfile;
  33. printmap["closekey"] = &UserIoMan::printClosekey;
  34. printmap["notifications"] = &UserIoMan::printNotifications;
  35. }
  36. UserIoMan::~UserIoMan() { delete reader; }
  37. void UserIoMan::printMessage(std::string msg, OutMsgType type) {
  38. Json::Value root;
  39. msgmutex.lock();
  40. char *savedline = rl_copy_text(0, rl_end);
  41. int savedpoint = rl_point;
  42. rl_set_prompt("");
  43. rl_replace_line("", 0);
  44. rl_redisplay();
  45. switch (type) {
  46. case normal:
  47. case error: {
  48. // this should never happen outside of development
  49. if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root, &jsonerror)) {
  50. printMessage(string(__PRETTY_FUNCTION__) + " couldnt parse json data: " + jsonerror, debug);
  51. } else {
  52. printJson(root);
  53. }
  54. break;
  55. }
  56. case debug: {
  57. if (verbose)
  58. std::cerr << msg << std::endl;
  59. break;
  60. }
  61. }
  62. rl_set_prompt(getCmdPrompt().c_str());
  63. rl_replace_line(savedline, 0);
  64. rl_point = savedpoint;
  65. rl_redisplay();
  66. free(savedline);
  67. msgmutex.unlock();
  68. }
  69. void UserIoMan::printWelcomeMessage() {
  70. std::cout << std::endl
  71. << "Please connect to a server via \"connect <ip> <port>\", then" << std::endl
  72. << "login by entering \"login <username> <password>\"" << std::endl
  73. << "or sign up and log in with \"signup <username> <password>\"." << std::endl
  74. << std::endl;
  75. }
  76. std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
  77. void UserIoMan::printJson(Json::Value root) {
  78. map<string, void (UserIoMan::*)(Json::Value)>::iterator it = printmap.find(root["command"].asString());
  79. if (it == printmap.end()) {
  80. // this should never happen outside of development
  81. printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nEnsure code is implemented.", debug);
  82. return;
  83. }
  84. (this->*(printmap[root["command"].asString()]))(root);
  85. }
  86. void UserIoMan::printError(Json::Value root) { std::cout << "Error: " << root["error"].asString() << std::endl; }
  87. void UserIoMan::printConnect(Json::Value root) {
  88. if (!root["accept"].asBool()) {
  89. std::cout << "Couldnt connect to " << root["address"].asString() << ":" << root["port"].asUInt() << std::endl
  90. << "Reason: " << root["error"].asString() << std::endl;
  91. }
  92. }
  93. void UserIoMan::printHelp(Json::Value root) {
  94. std::cout << "Available commands are: " << std::endl;
  95. for (Json::Value i : root["names"])
  96. std::cout << i.asString() << std::endl;
  97. }
  98. void UserIoMan::printStatus(Json::Value root) { std::cout << "Server reports status: " << root["response"].asString() << std::endl; }
  99. void UserIoMan::printExtendedstatus(Json::Value root) {
  100. if (!root["accept"].asBool()) {
  101. std::cout << "Listing transfers failed. Server reports: " << root["error"].asString() << std::endl;
  102. } else {
  103. if (!root["transfersclientserver"].empty()) {
  104. std::cout << std::endl << "Transfers between clients and server:" << std::endl;
  105. /*
  106. * EXAMPLE:
  107. * type progress file
  108. * download 99% foobar.txt
  109. * upload 1% baz.html
  110. */
  111. std::cout << "type progress file" << std::endl;
  112. for (Json::Value val : root["transfersclientserver"]) {
  113. std::string type = val["upload"].asBool() ? "upload" : "download";
  114. std::string progress = std::to_string(val["progress"].asInt());
  115. std::string file = val["file"].asString();
  116. char output[21];
  117. std::snprintf(output, 21, "%-8s %7.7s%% ", type.c_str(), progress.c_str());
  118. std::cout << output << file << std::endl;
  119. }
  120. }
  121. if (!root["transfersserverserver"].empty()) {
  122. std::cout << std::endl << "Transfers between different servers:" << std::endl;
  123. /*
  124. * EXAMPLE:
  125. * type progress method bytes/sec file
  126. * download 99% method0000001 9000.01 foobar.txt
  127. * queued 1% urgent field 42.00 baz.html
  128. *
  129. * Too long method strings get truncated, unexpectedly high speeds are shown (with less pretty format), e.g.:
  130. *
  131. * download 95% too long stri 340282346638528859811704183484516925440.00 filename.zip
  132. */
  133. std::cout << "type progress method bytes/sec file" << std::endl;
  134. for (Json::Value val : root["transfersserverserver"]) {
  135. std::string type = val["type"].asString();
  136. std::string progress = std::to_string(val["progress"].asInt());
  137. std::string method = val["method"].asString();
  138. float speed = val["speed"].asFloat();
  139. std::string file = val["file"].asString();
  140. // size of 80 is just enough for maximum possible float value to fit as string
  141. char output[80];
  142. std::snprintf(output, 80, "%-8s %7.7s%% %-13.13s %15.2f ", type.c_str(), progress.c_str(), method.c_str(), speed);
  143. std::cout << output << file << std::endl;
  144. }
  145. }
  146. if (root["transfersclientserver"].empty() && root["transfersserverserver"].empty()) {
  147. std::cout << "No transfers running." << std::endl;
  148. } else {
  149. std::cout << std::endl;
  150. }
  151. }
  152. }
  153. void UserIoMan::printDisconnect(Json::Value root) {
  154. if (!root["accept"].asBool()) {
  155. std::cout << "Disconnect failed." << std::endl;
  156. } else {
  157. std::cout << "Disconnect successful." << std::endl;
  158. }
  159. }
  160. void UserIoMan::printPut(Json::Value root) {
  161. if (!root["accept"].asBool()) {
  162. if (root.isMember("file")) {
  163. std::cout << "Upload request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
  164. } else {
  165. std::cout << "Upload request failed: " << root["error"].asString() << std::endl;
  166. }
  167. } else
  168. std::cout << "Begin uploading file " << root["file"].asString() << std::endl;
  169. }
  170. void UserIoMan::printGet(Json::Value root) {
  171. if (!root["accept"].asBool()) {
  172. if (root.isMember("file")) {
  173. std::cout << "Download request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
  174. } else {
  175. std::cout << "Download request failed: " << root["error"].asString() << std::endl;
  176. }
  177. } else
  178. std::cout << "Begin downloading file " << root["file"].asString() << std::endl;
  179. }
  180. void UserIoMan::printList(Json::Value root) {
  181. if (!root["accept"].asBool()) {
  182. std::cout << "Listing files failed: " << root["error"].asString() << std::endl;
  183. } else {
  184. std::cout << "Listing files stored on server: " << std::endl;
  185. for (Json::Value i : root["names"])
  186. std::cout << i.asString() << std::endl;
  187. std::cout << "End of list." << std::endl;
  188. }
  189. }
  190. void UserIoMan::printExtendedlist(Json::Value root) {
  191. if (!root["accept"].asBool()) {
  192. std::cout << "Listing files failed: " << root["error"].asString() << std::endl;
  193. } else {
  194. if (!root["files"].empty()) {
  195. std::cout << "Files stored on server: " << std::endl;
  196. /*
  197. * EXAMPLE:
  198. * size (kBytes) decryptable file
  199. * 9000.01 yes foo.txt
  200. * 42.00 no baz.html
  201. * 0.02 plaintext bar.zip
  202. */
  203. std::cout << "size (kBytes) decryptable file" << std::endl;
  204. for (Json::Value val : root["files"]) {
  205. float size = val["size"].asFloat();
  206. std::string encrypted = val["encrypted"].asString();
  207. std::string decryptable;
  208. if (encrypted == "decryptable") {
  209. decryptable = "\033[32myes\033[0m "; // "yes" in green
  210. } else if (encrypted == "undecryptable") {
  211. decryptable = "\033[31mno\033[0m "; // "no" in red
  212. } else if (encrypted == "unknown") {
  213. decryptable = "unknown ";
  214. } else {
  215. decryptable = "plaintext ";
  216. }
  217. std::string progress = std::to_string(val["progress"].asInt());
  218. std::string file = val["name"].asString();
  219. char sizeString[44];
  220. std::snprintf(sizeString, 44, "%13.2f ", size);
  221. std::cout << sizeString << decryptable << file << std::endl;
  222. }
  223. } else {
  224. std::cout << "No files stored on server." << std::endl;
  225. }
  226. }
  227. }
  228. void UserIoMan::printVersion(Json::Value root) {
  229. if (!root["accept"].asBool()) {
  230. std::cout << "Version check failed. Server reports version " << root["serverversion"].asString() << " but client is "
  231. << root["clientversion"].asString() << std::endl;
  232. } else
  233. std::cout << "Version check ok." << std::endl;
  234. }
  235. void UserIoMan::printLogin(Json::Value root) {
  236. if (!root["accept"].asBool()) {
  237. std::cout << "Login failed: " << root["error"].asString() << std::endl;
  238. } else
  239. std::cout << "Login ok." << std::endl;
  240. }
  241. void UserIoMan::printSignup(Json::Value root) {
  242. if (!root["accept"].asBool()) {
  243. std::cout << "Signup failed: " << root["error"].asString() << std::endl;
  244. } else
  245. std::cout << "Signup ok. You are now logged in." << std::endl;
  246. }
  247. void UserIoMan::printDeleteme(Json::Value root) {
  248. if (!root["accept"].asBool()) {
  249. std::cout << "User deletion failed: " << root["error"].asString() << std::endl;
  250. } else
  251. std::cout << "User deletion ok. You are now disconnected from the server." << std::endl;
  252. }
  253. void UserIoMan::printPutdata(Json::Value root) {}
  254. void UserIoMan::printGetdata(Json::Value root) {}
  255. void UserIoMan::printListdata(Json::Value root) {}
  256. void UserIoMan::printHead(Json::Value root) {
  257. if (!root["accept"].asBool())
  258. std::cout << "Request of the first few bytes failed. " << root["error"].asString() << std::endl;
  259. else
  260. std::cout << "First few bytes of file " << root["file"].asString() << " are: " << root["data"].asString() << std::endl;
  261. }
  262. void UserIoMan::printDeletefile(Json::Value root) {
  263. if (!root["accept"].asBool())
  264. std::cout << "Deletion of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
  265. else
  266. std::cout << "File " << root["file"] << " deleted succesfully" << std::endl;
  267. }
  268. void UserIoMan::printKeyfile(Json::Value root) {
  269. if (!root["accept"].asBool())
  270. std::cout << "Couldnt select keyfile " << root["file"].asString() << ": " << root["error"].asString() << std::endl;
  271. else
  272. std::cout << "Using keyfile " << root["file"].asString() << std::endl;
  273. }
  274. void UserIoMan::printClosekey(Json::Value root) {
  275. if (!root["accept"].asBool())
  276. std::cout << "Failed to close key: " << root["error"].asString() << std::endl;
  277. else
  278. std::cout << "Key closed." << std::endl;
  279. }
  280. void UserIoMan::printQueue(Json::Value root) {
  281. if (!root["accept"].asBool())
  282. std::cout << "Queueing of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
  283. else
  284. std::cout << "File " << root["file"] << " queued succesfully." << std::endl;
  285. }
  286. void UserIoMan::printDequeue(Json::Value root) {
  287. if (!root["accept"].asBool())
  288. std::cout << "Dequeueing of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
  289. else
  290. std::cout << "File " << root["file"] << " dequeued succesfully." << std::endl;
  291. }
  292. void UserIoMan::printNotifications(Json::Value root) {
  293. if (!root["accept"].asBool()) {
  294. std::cout << "Failed to get notifications: " << root["error"].asString() << std::endl;
  295. } else {
  296. std::cout << "New notifications:" << std::endl;
  297. for (Json::Value i : root["messages"])
  298. std::cout << "\033[94m" << i.asString() << std::endl;
  299. std::cout << "\033[0m " << std::endl;
  300. }
  301. }