userioman.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "../include/userioman.h"
  2. #include <boost/algorithm/string.hpp>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <vector>
  6. #include <readline/history.h>
  7. #include <readline/readline.h>
  8. using boost::asio::buffer;
  9. UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring) {};
  10. UserIoMan::UserIoMan(char *ipcstring, bool usessl) : IoMan(ipcstring, usessl) {
  11. /* setup json stuff */
  12. Json::CharReaderBuilder rbuilder;
  13. wbuilder.settings_["indentation"] = "";
  14. reader = rbuilder.newCharReader();
  15. /* initialize print command map */
  16. printmap["error"] = &UserIoMan::printError;
  17. printmap["connect"] = &UserIoMan::printConnect;
  18. printmap["help"] = &UserIoMan::printHelp;
  19. printmap["status"] = &UserIoMan::printStatus;
  20. printmap["disconnect"] = &UserIoMan::printDisconnect;
  21. printmap["put"] = &UserIoMan::printPut;
  22. printmap["get"] = &UserIoMan::printGet;
  23. printmap["list"] = &UserIoMan::printList;
  24. printmap["version"] = &UserIoMan::printVersion;
  25. printmap["login"] = &UserIoMan::printLogin;
  26. printmap["signup"] = &UserIoMan::printSignup;
  27. printmap["putdata"] = &UserIoMan::printPutdata;
  28. printmap["getdata"] = &UserIoMan::printGetdata;
  29. printmap["head"] = &UserIoMan::printHead;
  30. printmap["deleteme"] = &UserIoMan::printDeleteme;
  31. printmap["keyfile"] = &UserIoMan::printKeyfile;
  32. printmap["closekey"] = &UserIoMan::printClosekey;
  33. }
  34. UserIoMan::~UserIoMan() { delete reader; }
  35. void UserIoMan::printMessage(std::string msg, OutMsgType type) {
  36. Json::Value root;
  37. msgmutex.lock();
  38. switch (type) {
  39. case normal:
  40. case error: {
  41. // this should never happen outside of development
  42. if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root, &jsonerror)) {
  43. printMessage(string(__PRETTY_FUNCTION__) + " couldnt parse json data: " + jsonerror, debug);
  44. } else {
  45. printJson(root);
  46. }
  47. break;
  48. }
  49. //~ case error: {
  50. //~ std::cout << msg << std::endl;
  51. //~ break;
  52. //~ }
  53. case debug: {
  54. std::cerr << msg << std::endl;
  55. break;
  56. }
  57. }
  58. rl_redisplay();
  59. msgmutex.unlock();
  60. }
  61. void UserIoMan::printWelcomeMessage() {
  62. std::cout << "please login by entering \"login <username> <password>\" \nor "
  63. "sign up and log in with \"signup <username> <password>\""
  64. << std::endl;
  65. }
  66. std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
  67. // currently not in use:
  68. //~ std::string UserIoMan::getUserPrompt() { return "User: "; }
  69. //~ std::string UserIoMan::getPassPrompt() { return "Pass: "; }
  70. void UserIoMan::printJson(Json::Value root) {
  71. map<string, void (UserIoMan::*)(Json::Value)>::iterator it = printmap.find(root["command"].asString());
  72. if (it == printmap.end()) {
  73. // this should never happen outside of development
  74. printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nensure code is implemented.", debug);
  75. return;
  76. }
  77. (this->*(printmap[root["command"].asString()]))(root);
  78. }
  79. void UserIoMan::printError(Json::Value root) { std::cout << "Error: " << root["error"].asString() << std::endl; }
  80. void UserIoMan::printConnect(Json::Value root) {
  81. if (!root["accept"].asBool()) {
  82. std::cout << "Couldnt connect to " << root["address"].asString() << ":" << root["port"].asUInt() << std::endl
  83. << "Reason: " << root["error"].asString() << std::endl;
  84. }
  85. }
  86. void UserIoMan::printHelp(Json::Value root) {
  87. std::cout << "Available commands are: " << std::endl;
  88. for (Json::Value i : root["names"])
  89. std::cout << i.asString() << std::endl;
  90. }
  91. void UserIoMan::printStatus(Json::Value root) { std::cout << "Server reports status: " << root["response"].asString() << std::endl; }
  92. void UserIoMan::printDisconnect(Json::Value root) {
  93. if (!root["accept"].asBool()) {
  94. std::cout << "Disconnect failed." << std::endl;
  95. } else {
  96. std::cout << "Disconnect successful." << std::endl;
  97. }
  98. }
  99. void UserIoMan::printPut(Json::Value root) {
  100. if (!root["accept"].asBool()) {
  101. if (root.isMember("file")) {
  102. std::cout << "Upload request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
  103. } else {
  104. std::cout << "Upload request failed: " << root["error"].asString() << std::endl;
  105. }
  106. } else
  107. std::cout << "Begin uploading file " << root["file"].asString() << std::endl;
  108. }
  109. void UserIoMan::printGet(Json::Value root) {
  110. if (!root["accept"].asBool()) {
  111. if (root.isMember("file")) {
  112. std::cout << "Download request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
  113. } else {
  114. std::cout << "Download request failed: " << root["error"].asString() << std::endl;
  115. }
  116. } else
  117. std::cout << "Begin downloading file " << root["file"].asString() << std::endl;
  118. }
  119. void UserIoMan::printList(Json::Value root) {
  120. if (!root["accept"].asBool()) {
  121. std::cout << "Listing files failed: " << root["error"].asString() << std::endl;
  122. } else {
  123. std::cout << "Listing files stored on server: " << std::endl;
  124. for (Json::Value i : root["names"])
  125. std::cout << i.asString() << std::endl;
  126. std::cout << "End of list." << std::endl;
  127. }
  128. }
  129. void UserIoMan::printVersion(Json::Value root) {
  130. if (!root["accept"].asBool()) {
  131. std::cout << "Version check failed. Server reports " << root["serverversion"].asString() << " but client is " << root["clientversion"].asString()
  132. << std::endl;
  133. } else
  134. std::cout << "Version check ok." << std::endl;
  135. }
  136. void UserIoMan::printLogin(Json::Value root) {
  137. if (!root["accept"].asBool()) {
  138. std::cout << "Login failed: " << root["error"].asString() << std::endl;
  139. } else
  140. std::cout << "Login ok." << std::endl;
  141. }
  142. void UserIoMan::printSignup(Json::Value root) {
  143. if (!root["accept"].asBool()) {
  144. std::cout << "Signup failed: " << root["error"].asString() << std::endl;
  145. } else
  146. std::cout << "Signup ok. You are now logged in." << std::endl;
  147. }
  148. void UserIoMan::printDeleteme(Json::Value root) {
  149. if (!root["accept"].asBool()) {
  150. std::cout << "User deletion failed: " << root["error"].asString() << std::endl;
  151. } else
  152. std::cout << "User deletion ok. You are now disconnected from the server." << std::endl;
  153. }
  154. void UserIoMan::printPutdata(Json::Value root) {}
  155. void UserIoMan::printGetdata(Json::Value root) {}
  156. void UserIoMan::printListdata(Json::Value root) {}
  157. void UserIoMan::printHead(Json::Value root) {
  158. if (!root["accept"].asBool())
  159. std::cout << "Request of the first four bytes failed, server reports: " << root["error"].asString() << std::endl;
  160. else
  161. std::cout << "First four bytes of file " << root["file"].asString() << " are: " << root["data"].asString() << std::endl;
  162. }
  163. void UserIoMan::printKeyfile(Json::Value root) {
  164. if (!root["accept"].asBool())
  165. std::cout << "Couldnt select keyfile " << root["file"].asString() << ": " << root["error"].asString() << std::endl;
  166. else
  167. std::cout << "Using keyfile " << root["file"].asString() << std::endl;
  168. }
  169. void UserIoMan::printClosekey(Json::Value root) {
  170. if (!root["accept"].asBool())
  171. std::cout << "Failed to close key: " << root["error"].asString() << std::endl;
  172. else
  173. std::cout << "Key closed." << std::endl;
  174. }