userioman.cpp 12 KB

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