C++11 library providing functionality common to all adapters.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
11 KiB

5 months ago
  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #include "utils.hh"
  4. #include <pEp/identity_list.h>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace pEp;
  8. namespace pEp {
  9. namespace Utils {
  10. std::vector<::pEp_identity *> to_cxx(const ::identity_list &idl)
  11. {
  12. vector<pEp_identity *> ret{};
  13. for (const ::identity_list *curr = &idl; curr != nullptr; curr = curr->next) {
  14. if (curr->ident) {
  15. ret.push_back(curr->ident);
  16. }
  17. }
  18. return ret;
  19. }
  20. string to_string(const ::pEp_identity *const ident, bool full, int indent)
  21. {
  22. stringstream builder;
  23. if (ident != nullptr) {
  24. if (full) {
  25. builder << endl;
  26. builder << std::string(indent, '\t') << "{" << endl;
  27. indent++;
  28. builder << std::string(indent, '\t') << "address: '"
  29. << (ident->address != nullptr ? ident->address : "NULL") << "'" << endl;
  30. builder << std::string(indent, '\t') << "user_id: '"
  31. << (ident->user_id != nullptr ? ident->user_id : "NULL") << "'" << endl;
  32. builder << std::string(indent, '\t') << "username: '"
  33. << (ident->username != nullptr ? ident->username : "NULL") << "'"
  34. << endl;
  35. builder << std::string(indent, '\t') << "fpr: '"
  36. << (ident->fpr != nullptr ? ident->fpr : "NULL") << "'" << endl;
  37. builder << std::string(indent, '\t') << "comm_type: " << ident->comm_type << endl;
  38. builder << std::string(indent, '\t') << "lang: '"
  39. << static_cast<string>(ident->lang) << "'" << endl;
  40. builder << std::string(indent, '\t') << "me: " << ident->me << endl;
  41. builder << std::string(indent, '\t') << "major_ver: " << ident->major_ver << endl;
  42. builder << std::string(indent, '\t') << "minor_ver: " << ident->minor_ver << endl;
  43. builder << std::string(indent, '\t') << "enc_format: " << ident->enc_format
  44. << endl;
  45. builder << std::string(indent, '\t') << "flags: " << ident->flags << endl;
  46. indent--;
  47. builder << std::string(indent, '\t') << "}";
  48. } else {
  49. builder << std::string(indent, '\t') << "{ '"
  50. << (ident->address != nullptr ? ident->address : "NULL") << "' / '"
  51. << (ident->user_id != nullptr ? ident->user_id : "NULL") << "' / '"
  52. << (ident->username != nullptr ? ident->username : "NULL") << "' / '"
  53. << (ident->fpr != nullptr ? ident->fpr : "NULL") << "' }";
  54. }
  55. } else {
  56. builder << "NULL";
  57. }
  58. return builder.str();
  59. }
  60. std::string to_string(const ::bloblist_t *const blob, bool full, int indent)
  61. {
  62. stringstream builder;
  63. if (blob != nullptr) {
  64. builder << endl;
  65. builder << std::string(indent, '\t') << "[" << endl;
  66. indent++;
  67. for (const ::bloblist_t *curr = blob; curr != nullptr; curr = curr->next) {
  68. if (full) {
  69. builder << std::string(indent, '\t') << "{" << endl;
  70. indent++;
  71. builder << std::string(indent, '\t') << "mime_type: '"
  72. << (curr->mime_type != nullptr ? std::string(curr->mime_type) : "NULL")
  73. << "'" << endl;
  74. builder << std::string(indent, '\t') << "filename: '"
  75. << (curr->filename != nullptr ? curr->filename : "NULL") << "'"
  76. << endl;
  77. builder << std::string(indent, '\t') << "size: " << curr->size << endl;
  78. builder << std::string(indent, '\t') << "value: '"
  79. << (curr->value != nullptr
  80. ? Utils::tldr(std::string(curr->value), 300)
  81. : "NULL")
  82. << "'" << endl;
  83. indent--;
  84. builder << std::string(indent, '\t') << "}" << endl;
  85. } else {
  86. builder << std::string(indent, '\t');
  87. builder << "{ '"
  88. << (curr->mime_type != nullptr ? std::string(curr->mime_type)
  89. : "NULL");
  90. builder << "' / '" << (curr->filename != nullptr ? curr->filename : "NULL");
  91. builder << "' / '" << curr->size << "'";
  92. builder << " }" << endl;
  93. }
  94. }
  95. indent--;
  96. builder << std::string(indent, '\t') << "]" << endl;
  97. } else {
  98. builder << "NULL";
  99. }
  100. return builder.str();
  101. }
  102. std::string to_string(const ::stringpair_list_t *const spl, bool full, int indent)
  103. {
  104. stringstream builder;
  105. if (spl != nullptr) {
  106. builder << endl;
  107. builder << std::string(indent, '\t') << "[" << endl;
  108. indent++;
  109. for (const ::stringpair_list_t *curr = spl; curr != nullptr; curr = curr->next) {
  110. builder << std::string(indent, '\t') << "{ '";
  111. if (curr->value != nullptr) {
  112. builder << (curr->value->key ? curr->value->key : "NULL");
  113. builder << "' : '";
  114. builder << (curr->value->value ? curr->value->value : "NULL");
  115. }
  116. builder << "' }" << endl;
  117. }
  118. indent--;
  119. builder << std::string(indent, '\t') << "]" << endl;
  120. } else {
  121. builder << "NULL";
  122. }
  123. return builder.str();
  124. }
  125. std::string to_string(const ::message *const msg, bool full, int indent)
  126. {
  127. stringstream builder;
  128. if (msg != nullptr) {
  129. builder << endl;
  130. builder << std::string(indent, '\t') << "{" << endl;
  131. indent++;
  132. builder << std::string(indent, '\t') << "from: "
  133. << (msg->from != nullptr ? to_string(msg->from, full, indent) : "NULL")
  134. << endl;
  135. builder << std::string(indent, '\t') << "to: "
  136. << (msg->to != nullptr ? to_string(msg->to, full, indent) : "NULL") << endl;
  137. builder << std::string(indent, '\t') << "shortmsg: '"
  138. << (msg->shortmsg != nullptr ? msg->shortmsg : "NULL") << "'" << endl;
  139. builder << std::string(indent, '\t') << "longmsg: '"
  140. << (msg->longmsg != nullptr ? msg->longmsg : "NULL") << "'" << endl;
  141. builder << std::string(indent, '\t') << "enc_format: " << msg->enc_format << endl;
  142. builder << std::string(indent, '\t')
  143. << "dir: " << (msg->dir == 0 ? "incomming" : "outgoing") << endl;
  144. builder << std::string(indent, '\t') << "id: '"
  145. << (msg->id != nullptr ? msg->id : "NULL") << "'" << endl;
  146. builder << std::string(indent, '\t') << "opt_fields: "
  147. << (msg->opt_fields ? to_string(msg->opt_fields, full, indent) : "NULL")
  148. << endl;
  149. builder << std::string(indent, '\t') << "attachments: "
  150. << (msg->attachments ? to_string(msg->attachments, full, indent) : "NULL")
  151. << endl;
  152. indent--;
  153. builder << std::string(indent, '\t') << "}" << endl;
  154. } else {
  155. builder << "NULL";
  156. }
  157. return builder.str();
  158. }
  159. std::string to_string(const ::identity_list *const idl, bool full, int indent)
  160. {
  161. stringstream builder;
  162. if (idl != nullptr) {
  163. builder << endl;
  164. builder << std::string(indent, '\t') << "[" << endl;
  165. indent++;
  166. for (const ::identity_list *curr = idl; curr != nullptr; curr = curr->next) {
  167. builder << to_string(curr->ident, full, indent) << endl;
  168. }
  169. indent--;
  170. builder << std::string(indent, '\t') << "]";
  171. } else {
  172. builder << "NULL";
  173. }
  174. return builder.str();
  175. }
  176. string to_string(const ::pEp_member *const member, bool full, int indent)
  177. {
  178. stringstream builder;
  179. if (member != nullptr) {
  180. builder << std::string(indent, '\t') << "{" << endl;
  181. indent++;
  182. builder << std::string(indent, '\t')
  183. << "ident: " << to_string(member->ident, full, indent) << endl;
  184. builder << std::string(indent, '\t') << "joined: " << member->joined << endl;
  185. indent--;
  186. builder << std::string(indent, '\t') << "}";
  187. } else {
  188. builder << "NULL";
  189. }
  190. return builder.str();
  191. }
  192. string to_string(const ::member_list *const mbl, bool full, int indent)
  193. {
  194. stringstream builder;
  195. if (mbl != nullptr) {
  196. builder << endl;
  197. builder << std::string(indent, '\t') << "[" << endl;
  198. indent++;
  199. for (const member_list *curr = mbl; curr != nullptr; curr = curr->next) {
  200. builder << to_string(curr->member, full, indent) << endl;
  201. }
  202. indent--;
  203. builder << std::string(indent, '\t') << "]";
  204. } else {
  205. builder << "NULL";
  206. }
  207. return builder.str();
  208. }
  209. string to_string(const ::pEp_group *const group, bool full, int indent)
  210. {
  211. stringstream builder;
  212. if (group != nullptr) {
  213. builder << endl;
  214. builder << std::string(indent, '\t') << "{" << endl;
  215. indent++;
  216. builder << std::string(indent, '\t')
  217. << "group_identity: " << to_string(group->group_identity, full, indent)
  218. << endl;
  219. builder << std::string(indent, '\t')
  220. << "manager: " << to_string(group->manager, full, indent) << endl;
  221. builder << std::string(indent, '\t') << "active: " << group->active << endl;
  222. builder << std::string(indent, '\t')
  223. << "members: " << to_string(group->members, full, indent) << endl;
  224. indent--;
  225. builder << std::string(indent, '\t') << "]";
  226. } else {
  227. builder << "NULL";
  228. }
  229. return builder.str();
  230. }
  231. // TODO: Move to std_utils
  232. std::string readKey()
  233. {
  234. std::string ret;
  235. std::cin >> ret;
  236. return ret;
  237. }
  238. } // namespace Utils
  239. } // namespace pEp