Whyblocked reminds you why you blocked someone.
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.

147 lines
4.3KB

  1. // CC-0, tastytea
  2. #include <iostream>
  3. #include <string>
  4. #include <experimental/filesystem>
  5. #include <basedir.h>
  6. #include <sqlite/connection.hpp>
  7. #include <sqlite/execute.hpp>
  8. #include <sqlite/query.hpp>
  9. #include <boost/shared_ptr.hpp>
  10. #include <version.hpp>
  11. using std::string;
  12. using std::cout;
  13. using std::cerr;
  14. using std::cin;
  15. namespace fs = std::experimental::filesystem;
  16. string get_filepath()
  17. {
  18. string filepath;
  19. xdgHandle xdg;
  20. xdgInitHandle(&xdg);
  21. filepath = xdgDataHome(&xdg);
  22. xdgWipeHandle(&xdg);
  23. filepath += "/whyblock";
  24. if (!fs::exists(filepath))
  25. {
  26. fs::create_directory(filepath);
  27. }
  28. filepath += "/database.sqlite";
  29. if (!fs::exists(filepath))
  30. {
  31. sqlite::connection con(filepath);
  32. sqlite::execute(con, "CREATE TABLE blocks(user TEXT PRIMARY KEY, blocked INTEGER, reason TEXT);", true);
  33. sqlite::execute(con, "CREATE TABLE urls(user TEXT, url TEXT);", true);
  34. }
  35. return filepath;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. try
  40. {
  41. sqlite::connection con(get_filepath());
  42. string answer;
  43. cout << "Would you like to add, remove or view records?\n";
  44. cout << "Or do you want to get the details of a record?\n";
  45. cout << "Type add, remove, view or details. Or just the first letter\n";
  46. cout << ": ";
  47. cin >> answer;
  48. switch (answer[0])
  49. {
  50. case 'a':
  51. case 'A':
  52. {
  53. cout << "ADD\n";
  54. break;
  55. }
  56. case 'r':
  57. case 'R':
  58. {
  59. cout << "REMOVE\n";
  60. break;
  61. }
  62. case 'v':
  63. case 'V':
  64. {
  65. sqlite::query q(con, "SELECT * FROM blocks;");
  66. boost::shared_ptr<sqlite::result> result = q.get_result();
  67. while(result->next_row())
  68. {
  69. if (result->get_int(1) == 1)
  70. {
  71. cout << " Blocked: ";
  72. }
  73. else
  74. {
  75. cout << "Silenced: ";
  76. }
  77. cout << result->get_string(0) << " because: ";
  78. cout << result->get_string(2) << '\n';
  79. }
  80. break;
  81. }
  82. case 'd':
  83. case 'D':
  84. {
  85. cout << "Which user?\n";
  86. cin >> answer;
  87. {
  88. sqlite::query q(con, "SELECT * FROM blocks WHERE user = \'" + answer + "\';");
  89. boost::shared_ptr<sqlite::result> result = q.get_result();
  90. result->next_row();
  91. cout << answer << " is ";
  92. if (result->get_row_count() == 0)
  93. {
  94. cout << "not in the database.\n";
  95. break;
  96. }
  97. if (result->get_int(1) == 1)
  98. {
  99. cout << "blocked, because: ";
  100. }
  101. else if (result->get_int(1) == 0)
  102. {
  103. cout << "silenced, because: ";
  104. }
  105. cout << result->get_string(2) << '\n';
  106. }
  107. {
  108. sqlite::query q(con, "SELECT * FROM urls WHERE user = \'" + answer + "\';");
  109. boost::shared_ptr<sqlite::result> result = q.get_result();
  110. while(result->next_row())
  111. {
  112. cout << result->get_string(1) << '\n';
  113. }
  114. }
  115. break;
  116. }
  117. default:
  118. cout << "Response not understood.\n";
  119. }
  120. // sqlite::execute ins(con, "INSERT INTO TEST VALUES(?, ?, ?);");
  121. // ins % sqlite::nil % "Hello";
  122. // ins();
  123. // sqlite::query q(con, "SELECT * FROM blocks;");
  124. // boost::shared_ptr<sqlite::result> result = q.get_result();
  125. // while(result->next_row())
  126. // {
  127. // std::cout << "ID: " << result->get_int(0) << "\n"
  128. // << "Name: " << result->get_string(1) << std::endl;
  129. // }
  130. }
  131. catch(const std::exception &e)
  132. {
  133. cerr << "An error occurred: " << e.what() << std::endl;
  134. return 1;
  135. }
  136. return 0;
  137. }