userioman.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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["deletefile"] = &UserIoMan::printDeletefile;
  31. printmap["deleteme"] = &UserIoMan::printDeleteme;
  32. printmap["keyfile"] = &UserIoMan::printKeyfile;
  33. printmap["closekey"] = &UserIoMan::printClosekey;
  34. }
  35. UserIoMan::~UserIoMan() { delete reader; }
  36. void UserIoMan::printMessage(std::string msg, OutMsgType type) {
  37. Json::Value root;
  38. msgmutex.lock();
  39. switch (type) {
  40. case normal:
  41. case error: {
  42. // this should never happen outside of development
  43. if (!reader->parse(msg.c_str(), msg.c_str() + msg.size(), &root, &jsonerror)) {
  44. printMessage(string(__PRETTY_FUNCTION__) + " couldnt parse json data: " + jsonerror, debug);
  45. } else {
  46. printJson(root);
  47. }
  48. break;
  49. }
  50. //~ case error: {
  51. //~ std::cout << msg << std::endl;
  52. //~ break;
  53. //~ }
  54. case debug: {
  55. std::cerr << msg << std::endl;
  56. break;
  57. }
  58. }
  59. rl_redisplay();
  60. msgmutex.unlock();
  61. }
  62. void UserIoMan::printWelcomeMessage() {
  63. std::cout << "please login by entering \"login <username> <password>\" \nor "
  64. "sign up and log in with \"signup <username> <password>\""
  65. << std::endl;
  66. }
  67. std::string UserIoMan::getCmdPrompt() { return "ccats> "; }
  68. // currently not in use:
  69. //~ std::string UserIoMan::getUserPrompt() { return "User: "; }
  70. //~ std::string UserIoMan::getPassPrompt() { return "Pass: "; }
  71. void UserIoMan::printJson(Json::Value root) {
  72. map<string, void (UserIoMan::*)(Json::Value)>::iterator it = printmap.find(root["command"].asString());
  73. if (it == printmap.end()) {
  74. // this should never happen outside of development
  75. printMessage(string(__PRETTY_FUNCTION__) + " unknown command \"" + root["command"].asString() + "\".\nensure code is implemented.", debug);
  76. return;
  77. }
  78. (this->*(printmap[root["command"].asString()]))(root);
  79. }
  80. void UserIoMan::printError(Json::Value root) { std::cout << "Error: " << root["error"].asString() << std::endl; }
  81. void UserIoMan::printConnect(Json::Value root) {
  82. if (!root["accept"].asBool()) {
  83. std::cout << "Couldnt connect to " << root["address"].asString() << ":" << root["port"].asUInt() << std::endl
  84. << "Reason: " << root["error"].asString() << std::endl;
  85. }
  86. }
  87. void UserIoMan::printHelp(Json::Value root) {
  88. std::cout << "Available commands are: " << std::endl;
  89. for (Json::Value i : root["names"])
  90. std::cout << i.asString() << std::endl;
  91. }
  92. void UserIoMan::printStatus(Json::Value root) { std::cout << "Server reports status: " << root["response"].asString() << std::endl; }
  93. void UserIoMan::printDisconnect(Json::Value root) {
  94. if (!root["accept"].asBool()) {
  95. std::cout << "Disconnect failed." << std::endl;
  96. } else {
  97. std::cout << "Disconnect successful." << std::endl;
  98. }
  99. }
  100. void UserIoMan::printPut(Json::Value root) {
  101. if (!root["accept"].asBool()) {
  102. if (root.isMember("file")) {
  103. std::cout << "Upload request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
  104. } else {
  105. std::cout << "Upload request failed: " << root["error"].asString() << std::endl;
  106. }
  107. } else
  108. std::cout << "Begin uploading file " << root["file"].asString() << std::endl;
  109. }
  110. void UserIoMan::printGet(Json::Value root) {
  111. if (!root["accept"].asBool()) {
  112. if (root.isMember("file")) {
  113. std::cout << "Download request for file " << root["file"].asString() << " failed: " << root["error"].asString() << std::endl;
  114. } else {
  115. std::cout << "Download request failed: " << root["error"].asString() << std::endl;
  116. }
  117. } else
  118. std::cout << "Begin downloading file " << root["file"].asString() << std::endl;
  119. }
  120. void UserIoMan::printList(Json::Value root) {
  121. if (!root["accept"].asBool()) {
  122. std::cout << "Listing files failed: " << root["error"].asString() << std::endl;
  123. } else {
  124. std::cout << "Listing files stored on server: " << std::endl;
  125. for (Json::Value i : root["names"])
  126. std::cout << i.asString() << std::endl;
  127. std::cout << "End of list." << std::endl;
  128. }
  129. }
  130. void UserIoMan::printVersion(Json::Value root) {
  131. if (!root["accept"].asBool()) {
  132. std::cout << "Version check failed. Server reports " << root["serverversion"].asString() << " but client is " << root["clientversion"].asString()
  133. << std::endl;
  134. } else
  135. std::cout << "Version check ok." << std::endl;
  136. }
  137. void UserIoMan::printLogin(Json::Value root) {
  138. if (!root["accept"].asBool()) {
  139. std::cout << "Login failed: " << root["error"].asString() << std::endl;
  140. } else
  141. std::cout << "Login ok." << std::endl;
  142. }
  143. void UserIoMan::printSignup(Json::Value root) {
  144. if (!root["accept"].asBool()) {
  145. std::cout << "Signup failed: " << root["error"].asString() << std::endl;
  146. } else
  147. std::cout << "Signup ok. You are now logged in." << std::endl;
  148. }
  149. void UserIoMan::printDeleteme(Json::Value root) {
  150. if (!root["accept"].asBool()) {
  151. std::cout << "User deletion failed: " << root["error"].asString() << std::endl;
  152. } else
  153. std::cout << "User deletion ok. You are now disconnected from the server." << std::endl;
  154. }
  155. void UserIoMan::printPutdata(Json::Value root) {}
  156. void UserIoMan::printGetdata(Json::Value root) {}
  157. void UserIoMan::printListdata(Json::Value root) {}
  158. void UserIoMan::printHead(Json::Value root) {
  159. if (!root["accept"].asBool())
  160. std::cout << "Request of the first four bytes failed. " << root["error"].asString() << std::endl;
  161. else
  162. std::cout << "First four bytes of file " << root["file"].asString() << " are: " << root["data"].asString() << std::endl;
  163. }
  164. void UserIoMan::printDeletefile(Json::Value root) {
  165. if (!root["accept"].asBool())
  166. std::cout << "Deletion of file " << root["file"] << " failed. " << root["error"].asString() << std::endl;
  167. else
  168. std::cout << "File " << root["file"] << " deleted succesfully" << std::endl;
  169. }
  170. void UserIoMan::printKeyfile(Json::Value root) {
  171. if (!root["accept"].asBool())
  172. std::cout << "Couldnt select keyfile " << root["file"].asString() << ": " << root["error"].asString() << std::endl;
  173. else
  174. std::cout << "Using keyfile " << root["file"].asString() << std::endl;
  175. }
  176. void UserIoMan::printClosekey(Json::Value root) {
  177. if (!root["accept"].asBool())
  178. std::cout << "Failed to close key: " << root["error"].asString() << std::endl;
  179. else
  180. std::cout << "Key closed." << std::endl;
  181. }