userioman.cpp 12 KB

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