util.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #ifndef UTIL_HPP
  2. #define UTIL_HPP
  3. #include <string>
  4. #include <charconv>
  5. #include <utility>
  6. #include <vector>
  7. #include <QDebug>
  8. #include <QVector>
  9. #include "rapidjson/writer.h"
  10. #include "rapidjson/prettywriter.h"
  11. #include "rapidjson/pointer.h"
  12. #include "rapidjson/stringbuffer.h"
  13. #include "rapidjson/document.h"
  14. #include "json.hpp"
  15. namespace Kontainer {
  16. /** Reverse Iterator */
  17. template <class T>
  18. class ReverseIterator {
  19. T& _obj;
  20. public:
  21. ReverseIterator(T &obj) : _obj(obj) {}
  22. auto begin() {return _obj.rbegin();}
  23. auto end() {return _obj.rend();}
  24. };
  25. }
  26. namespace {
  27. using namespace rapidjson;
  28. using json = nlohmann::json;
  29. typedef std::string KOperation;
  30. typedef std::vector<std::pair<std::string, std::string>> TupVec;
  31. typedef std::vector<std::map<int, std::string>> MapVec;
  32. typedef std::vector<std::string> StdStringVec;
  33. typedef std::map<int, std::string> CommandMap;
  34. struct KSession {
  35. int id;
  36. int fd;
  37. int status;
  38. };
  39. static QString escapeText(QString s) {
  40. qDebug() << "Escaping text";
  41. if (s.contains("'")) {
  42. qDebug() << "Replacing single quote";
  43. s.replace("'", "'\"'\"'");
  44. }
  45. if (s.contains("\t")) {
  46. s.replace("\t", "\\t");
  47. }
  48. // if (s.contains("🙋‍♀️")) {
  49. // qDebug() << "Replacing woman raising hand emoji";
  50. // s.replace("🙋‍♀️", ":woman raising hand:");
  51. // }
  52. // if (s.contains("❤️")) {
  53. // qDebug() << "Replacing heart";
  54. // s.replace("❤️", ":heart:");
  55. // }
  56. // if (s.contains("🔗")) {
  57. // qDebug() << "Replacing link";
  58. // s.replace("🔗", ":link:");
  59. // }
  60. // if (s.contains("⬆️")) {
  61. // qDebug() << "Replacing arrow";
  62. // s.replace("⬆️", ":arrow_up:");
  63. // }
  64. return s;
  65. }
  66. static QString escapeTextToRaw(QString s) {
  67. return escapeText(s).toUtf8().constData();
  68. }
  69. std::string getJsonString(std::string s) {
  70. Document d;
  71. d.Parse(s.c_str());
  72. StringBuffer buffer;
  73. PrettyWriter<StringBuffer> writer(buffer);
  74. d.Accept(writer);
  75. return buffer.GetString();
  76. }
  77. std::string createMessage(const char* data, std::string args = "") {
  78. StringBuffer s;
  79. Writer<StringBuffer> w(s);
  80. w.StartObject();
  81. w.Key("type");
  82. w.String("custom");
  83. w.Key("message");
  84. w.String(data);
  85. w.Key("args");
  86. w.String(args.c_str());
  87. w.EndObject();
  88. return s.GetString();
  89. }
  90. bool isOperation(const char* data) {
  91. Document d;
  92. d.Parse(data);
  93. return strcmp(d["type"].GetString(), "operation") == 0;
  94. }
  95. bool isUploadCompleteEvent(const char* event) {
  96. return strcmp(event, "File Transfer Complete") == 0;
  97. }
  98. bool isEvent(const char* data) {
  99. Document d;
  100. d.Parse(data);
  101. return strcmp(d["type"].GetString(), "event") == 0;
  102. }
  103. // TODO: This should be "message", no?
  104. bool isMessage(const char* data) {
  105. Document d;
  106. d.Parse(data);
  107. if (d.HasMember("message")) {
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. std::string createOperation(const char* op, std::vector<std::string> args) {
  114. StringBuffer s;
  115. Writer<StringBuffer, Document::EncodingType, ASCII<>> w(s);
  116. w.StartObject();
  117. w.Key("type");
  118. w.String("operation");
  119. w.Key("command");
  120. w.String(op);
  121. w.Key("args");
  122. w.StartArray();
  123. if (!args.empty()) {
  124. for (const auto& arg : args) {
  125. w.String(arg.c_str());
  126. }
  127. }
  128. w.EndArray();
  129. w.EndObject();
  130. return s.GetString();
  131. }
  132. std::string getOperation(const char* data) {
  133. Document d;
  134. d.Parse(data);
  135. if (d.HasMember("command")) {
  136. return d["command"].GetString();
  137. }
  138. return "";
  139. }
  140. QString getEvent(const char* data) {
  141. Document d;
  142. d.Parse(data);
  143. if (d.HasMember("event")) {
  144. return d["event"].GetString();
  145. }
  146. return "";
  147. }
  148. QString getMessage(const char* data) {
  149. Document d;
  150. d.Parse(data);
  151. if (d.HasMember("message")) {
  152. return d["message"].GetString();
  153. }
  154. return "";
  155. }
  156. QVector<QString> getShortArgs(const char* data) {
  157. Document d;
  158. d.Parse(data);
  159. QVector<QString> args{};
  160. if (d.HasMember("args")) {
  161. if (d["args"].IsArray()) {
  162. for (const auto& m : d["args"].GetArray()) {
  163. if (m.GetStringLength() < 100) {
  164. args.push_back(m.GetString());
  165. }
  166. }
  167. } else {
  168. for (const auto& m : d["args"].GetObject()) {
  169. QString arg = m.name.GetString();
  170. arg += ": ";
  171. arg += m.value.GetString();
  172. args.push_back(arg);
  173. }
  174. }
  175. }
  176. return args;
  177. }
  178. QVector<QString> getArgs(const char* data) {
  179. Document d;
  180. d.Parse(data);
  181. QVector<QString> args{};
  182. if (d.HasMember("args")) {
  183. for (const auto& m : d["args"].GetArray()) {
  184. args.push_back(m.GetString());
  185. }
  186. }
  187. return args;
  188. }
  189. CommandMap getArgMap(const char* data) {
  190. Document d;
  191. d.Parse(data);
  192. CommandMap cm{};
  193. if (d.HasMember("args")) {
  194. for (const auto& m : d["args"].GetObject()) {
  195. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  196. }
  197. }
  198. return cm;
  199. }
  200. std::string createMessage(const char* data,
  201. std::map<int, std::string> map = {}) {
  202. StringBuffer s;
  203. Writer<StringBuffer> w(s);
  204. w.StartObject();
  205. w.Key("type");
  206. w.String("custom");
  207. w.Key("message");
  208. w.String(data);
  209. w.Key("args");
  210. w.StartObject();
  211. if (!map.empty()) {
  212. for (const auto& [k, v] : map) {
  213. w.Key(std::to_string(k).c_str());
  214. w.String(v.c_str());
  215. }
  216. }
  217. w.EndObject();
  218. w.EndObject();
  219. return s.GetString();
  220. }
  221. std::string createMessage(const char* data, std::map<int, std::vector<std::string>> map = {}) {
  222. StringBuffer s;
  223. Writer<StringBuffer> w(s);
  224. w.StartObject();
  225. w.Key("type");
  226. w.String("custom");
  227. w.Key("message");
  228. w.String(data);
  229. w.Key("args");
  230. w.StartObject();
  231. if (!map.empty()) {
  232. for (const auto& [k, v] : map) {
  233. w.Key(std::to_string(k).c_str());
  234. if (!v.empty()) {
  235. w.StartArray();
  236. for (const auto& arg : v) {
  237. w.String(arg.c_str());
  238. }
  239. w.EndArray();
  240. }
  241. }
  242. }
  243. w.EndObject();
  244. w.EndObject();
  245. return s.GetString();
  246. }
  247. std::string rapidCreateMessage(const char* data,
  248. std::map<int, std::string> map = {}) {
  249. StringBuffer s;
  250. Writer<StringBuffer> w(s);
  251. w.StartObject();
  252. w.Key("type");
  253. w.String("custom");
  254. w.Key("message");
  255. w.String(data);
  256. w.Key("args");
  257. w.StartObject();
  258. if (!map.empty()) {
  259. for (const auto& [k, v] : map) {
  260. w.Key(std::to_string(k).c_str());
  261. w.String(v.c_str());
  262. }
  263. }
  264. w.EndObject();
  265. w.EndObject();
  266. return s.GetString();
  267. }
  268. bool isStartOperation(const char* data) {
  269. Document d;
  270. d.Parse(data);
  271. return strcmp(d["command"].GetString(), "start") == 0;
  272. }
  273. bool isStopOperation(const char* data) {
  274. Document d;
  275. d.Parse(data);
  276. return strcmp(d["command"].GetString(), "stop") == 0;
  277. }
  278. bool isNewSession(const char* data) {
  279. Document d;
  280. d.Parse(data);
  281. if (d.HasMember("message")) {
  282. return strcmp(d["message"].GetString(), "New Session") == 0;
  283. }
  284. return false;
  285. }
  286. bool serverWaitingForFile(const char* data) {
  287. Document d;
  288. d.Parse(data);
  289. if (d.HasMember("message")) {
  290. return strcmp(d["message"].GetString(), "File Ready") == 0;
  291. }
  292. return false;
  293. }
  294. std::string stringTupleVecToJson(
  295. std::vector<std::pair<std::string, std::string>> v) {
  296. json j{};
  297. for (const auto& row : v) {
  298. j[row.first] = row.second;
  299. }
  300. return j;
  301. }
  302. inline size_t findNullIndex(uint8_t* data) {
  303. size_t index = 0;
  304. while (data) {
  305. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  306. break;
  307. }
  308. index++;
  309. data++;
  310. }
  311. return index;
  312. }
  313. }
  314. #endif // __UTIL_HPP__