util.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. return s;
  57. }
  58. static QString escapeTextToRaw(QString s) {
  59. qDebug() << "Escaping text";
  60. if (s.contains("'")) {
  61. qDebug() << "Replacing single quote";
  62. s.replace("'", "'\"'\"'");
  63. }
  64. if (s.contains("\t")) {
  65. s.replace("\t", "\\t");
  66. }
  67. if (s.contains("🙋‍♀️")) {
  68. qDebug() << "Replacing woman raising hand emoji";
  69. s.replace("🙋‍♀️", ":woman raising hand:");
  70. }
  71. if (s.contains("❤️")) {
  72. qDebug() << "Replacing heart";
  73. s.replace("❤️", ":heart:");
  74. }
  75. return s.toUtf8().constData();
  76. }
  77. std::string getJsonString(std::string s) {
  78. Document d;
  79. d.Parse(s.c_str());
  80. StringBuffer buffer;
  81. PrettyWriter<StringBuffer> writer(buffer);
  82. d.Accept(writer);
  83. return buffer.GetString();
  84. }
  85. std::string createMessage(const char* data, std::string args = "") {
  86. StringBuffer s;
  87. Writer<StringBuffer> w(s);
  88. w.StartObject();
  89. w.Key("type");
  90. w.String("custom");
  91. w.Key("message");
  92. w.String(data);
  93. w.Key("args");
  94. w.String(args.c_str());
  95. w.EndObject();
  96. return s.GetString();
  97. }
  98. bool isOperation(const char* data) {
  99. Document d;
  100. d.Parse(data);
  101. return strcmp(d["type"].GetString(), "operation") == 0;
  102. }
  103. bool isUploadCompleteEvent(const char* event) {
  104. return strcmp(event, "File Transfer Complete") == 0;
  105. }
  106. bool isEvent(const char* data) {
  107. Document d;
  108. d.Parse(data);
  109. return strcmp(d["type"].GetString(), "event") == 0;
  110. }
  111. // TODO: This should be "message", no?
  112. bool isMessage(const char* data) {
  113. Document d;
  114. d.Parse(data);
  115. if (d.HasMember("message")) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. std::string createOperation(const char* op, std::vector<std::string> args) {
  122. StringBuffer s;
  123. Writer<StringBuffer, Document::EncodingType, ASCII<>> w(s);
  124. w.StartObject();
  125. w.Key("type");
  126. w.String("operation");
  127. w.Key("command");
  128. w.String(op);
  129. w.Key("args");
  130. w.StartArray();
  131. if (!args.empty()) {
  132. for (const auto& arg : args) {
  133. w.String(arg.c_str());
  134. }
  135. }
  136. w.EndArray();
  137. w.EndObject();
  138. return s.GetString();
  139. }
  140. std::string getOperation(const char* data) {
  141. Document d;
  142. d.Parse(data);
  143. if (d.HasMember("command")) {
  144. return d["command"].GetString();
  145. }
  146. return "";
  147. }
  148. QString getEvent(const char* data) {
  149. Document d;
  150. d.Parse(data);
  151. if (d.HasMember("event")) {
  152. return d["event"].GetString();
  153. }
  154. return "";
  155. }
  156. QString getMessage(const char* data) {
  157. Document d;
  158. d.Parse(data);
  159. if (d.HasMember("message")) {
  160. return d["message"].GetString();
  161. }
  162. return "";
  163. }
  164. QVector<QString> getShortArgs(const char* data) {
  165. Document d;
  166. d.Parse(data);
  167. QVector<QString> args{};
  168. if (d.HasMember("args")) {
  169. if (d["args"].IsArray()) {
  170. for (const auto& m : d["args"].GetArray()) {
  171. if (m.GetStringLength() < 100) {
  172. args.push_back(m.GetString());
  173. }
  174. }
  175. } else {
  176. for (const auto& m : d["args"].GetObject()) {
  177. QString arg = m.name.GetString();
  178. arg += ": ";
  179. arg += m.value.GetString();
  180. args.push_back(arg);
  181. }
  182. }
  183. }
  184. return args;
  185. }
  186. QVector<QString> getArgs(const char* data) {
  187. Document d;
  188. d.Parse(data);
  189. QVector<QString> args{};
  190. if (d.HasMember("args")) {
  191. for (const auto& m : d["args"].GetArray()) {
  192. args.push_back(m.GetString());
  193. }
  194. }
  195. return args;
  196. }
  197. CommandMap getArgMap(const char* data) {
  198. Document d;
  199. d.Parse(data);
  200. CommandMap cm{};
  201. if (d.HasMember("args")) {
  202. for (const auto& m : d["args"].GetObject()) {
  203. cm.emplace(std::stoi(m.name.GetString()), m.value.GetString());
  204. }
  205. }
  206. return cm;
  207. }
  208. std::string createMessage(const char* data,
  209. std::map<int, std::string> map = {}) {
  210. StringBuffer s;
  211. Writer<StringBuffer> w(s);
  212. w.StartObject();
  213. w.Key("type");
  214. w.String("custom");
  215. w.Key("message");
  216. w.String(data);
  217. w.Key("args");
  218. w.StartObject();
  219. if (!map.empty()) {
  220. for (const auto& [k, v] : map) {
  221. w.Key(std::to_string(k).c_str());
  222. w.String(v.c_str());
  223. }
  224. }
  225. w.EndObject();
  226. w.EndObject();
  227. return s.GetString();
  228. }
  229. std::string createMessage(const char* data, std::map<int, std::vector<std::string>> map = {}) {
  230. StringBuffer s;
  231. Writer<StringBuffer> w(s);
  232. w.StartObject();
  233. w.Key("type");
  234. w.String("custom");
  235. w.Key("message");
  236. w.String(data);
  237. w.Key("args");
  238. w.StartObject();
  239. if (!map.empty()) {
  240. for (const auto& [k, v] : map) {
  241. w.Key(std::to_string(k).c_str());
  242. if (!v.empty()) {
  243. w.StartArray();
  244. for (const auto& arg : v) {
  245. w.String(arg.c_str());
  246. }
  247. w.EndArray();
  248. }
  249. }
  250. }
  251. w.EndObject();
  252. w.EndObject();
  253. return s.GetString();
  254. }
  255. std::string rapidCreateMessage(const char* data,
  256. std::map<int, std::string> map = {}) {
  257. StringBuffer s;
  258. Writer<StringBuffer> w(s);
  259. w.StartObject();
  260. w.Key("type");
  261. w.String("custom");
  262. w.Key("message");
  263. w.String(data);
  264. w.Key("args");
  265. w.StartObject();
  266. if (!map.empty()) {
  267. for (const auto& [k, v] : map) {
  268. w.Key(std::to_string(k).c_str());
  269. w.String(v.c_str());
  270. }
  271. }
  272. w.EndObject();
  273. w.EndObject();
  274. return s.GetString();
  275. }
  276. bool isStartOperation(const char* data) {
  277. Document d;
  278. d.Parse(data);
  279. return strcmp(d["command"].GetString(), "start") == 0;
  280. }
  281. bool isStopOperation(const char* data) {
  282. Document d;
  283. d.Parse(data);
  284. return strcmp(d["command"].GetString(), "stop") == 0;
  285. }
  286. bool isNewSession(const char* data) {
  287. Document d;
  288. d.Parse(data);
  289. if (d.HasMember("message")) {
  290. return strcmp(d["message"].GetString(), "New Session") == 0;
  291. }
  292. return false;
  293. }
  294. bool serverWaitingForFile(const char* data) {
  295. Document d;
  296. d.Parse(data);
  297. if (d.HasMember("message")) {
  298. return strcmp(d["message"].GetString(), "File Ready") == 0;
  299. }
  300. return false;
  301. }
  302. std::string stringTupleVecToJson(
  303. std::vector<std::pair<std::string, std::string>> v) {
  304. json j{};
  305. for (const auto& row : v) {
  306. j[row.first] = row.second;
  307. }
  308. return j;
  309. }
  310. inline size_t findNullIndex(uint8_t* data) {
  311. size_t index = 0;
  312. while (data) {
  313. if (strcmp(const_cast<const char*>((char*)data), "\0") == 0) {
  314. break;
  315. }
  316. index++;
  317. data++;
  318. }
  319. return index;
  320. }
  321. }
  322. #endif // __UTIL_HPP__