silencespammer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @copyright 2013 StatusNet, Inc.
  19. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  20. */
  21. define('INSTALLDIR', dirname(__DIR__, 3));
  22. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  23. $shortoptions = 'i:n:a';
  24. $longoptions = array('id=', 'nickname=', 'all');
  25. $helptext = <<<END_OF_SILENCESPAMMER_HELP
  26. silencespammer.php [options]
  27. Users who post a lot of spam get silenced
  28. -i --id ID of user to test and silence
  29. -n --nickname nickname of the user to test and silence
  30. -a --all All users
  31. END_OF_SILENCESPAMMER_HELP;
  32. require_once INSTALLDIR.'/scripts/commandline.inc';
  33. function testAllUsers($filter, $minimum, $percent)
  34. {
  35. $found = false;
  36. $offset = 0;
  37. $limit = 1000;
  38. do {
  39. $user = new User();
  40. $user->orderBy('created');
  41. $user->limit($offset, $limit);
  42. $found = $user->find();
  43. if ($found) {
  44. while ($user->fetch()) {
  45. try {
  46. silencespammer($filter, $user, $minimum, $percent);
  47. } catch (Exception $e) {
  48. printfnq("ERROR testing user %s\n: %s", $user->nickname, $e->getMessage());
  49. }
  50. }
  51. $offset += $found;
  52. }
  53. } while ($found > 0);
  54. }
  55. function silencespammer($filter, $user, $minimum, $percent)
  56. {
  57. printfnq("Testing user %s\n", $user->nickname);
  58. $profile = Profile::getKV('id', $user->id);
  59. if ($profile->isSilenced()) {
  60. printfnq("Already silenced %s\n", $user->nickname);
  61. return;
  62. }
  63. $cnt = $profile->noticeCount();
  64. if ($cnt < $minimum) {
  65. printfnq("Only %d notices posted (minimum %d); skipping\n", $cnt, $minimum);
  66. return;
  67. }
  68. $ss = new Spam_score();
  69. $ss->query(sprintf(
  70. <<<'END'
  71. SELECT COUNT(*) AS spam_count
  72. FROM notice INNER JOIN spam_score ON notice.id = spam_score.notice_id
  73. WHERE notice.profile_id = %d AND spam_score.is_spam IS TRUE;
  74. END,
  75. $profile->getID()
  76. ));
  77. while ($ss->fetch()) {
  78. $spam_count = $ss->spam_count;
  79. }
  80. $spam_percent = ($spam_count * 100.0 / $cnt);
  81. if ($spam_percent > $percent) {
  82. printfnq("Silencing user %s (%d/%d = %0.2f%% spam)\n", $user->nickname, $spam_count, $cnt, $spam_percent);
  83. try {
  84. $profile->silence();
  85. } catch (Exception $e) {
  86. printfnq("Error: %s", $e->getMessage());
  87. }
  88. }
  89. }
  90. try {
  91. $filter = null;
  92. $minimum = 5;
  93. $percent = 80;
  94. Event::handle('GetSpamFilter', array(&$filter));
  95. if (empty($filter)) {
  96. throw new Exception(_("No spam filter."));
  97. }
  98. if (have_option('a', 'all')) {
  99. testAllUsers($filter, $minimum, $percent);
  100. } else {
  101. $user = getUser();
  102. silencespammer($filter, $user, $minimum, $percent);
  103. }
  104. } catch (Exception $e) {
  105. print $e->getMessage()."\n";
  106. exit(1);
  107. }