silencespammer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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("SELECT count(*) as spam_count ".
  70. "FROM notice join spam_score on notice.id = spam_score.notice_id ".
  71. 'WHERE notice.profile_id = %d AND spam_score.is_spam = TRUE', $profile->id));
  72. while ($ss->fetch()) {
  73. $spam_count = $ss->spam_count;
  74. }
  75. $spam_percent = ($spam_count * 100.0 / $cnt);
  76. if ($spam_percent > $percent) {
  77. printfnq("Silencing user %s (%d/%d = %0.2f%% spam)\n", $user->nickname, $spam_count, $cnt, $spam_percent);
  78. try {
  79. $profile->silence();
  80. } catch (Exception $e) {
  81. printfnq("Error: %s", $e->getMessage());
  82. }
  83. }
  84. }
  85. try {
  86. $filter = null;
  87. $minimum = 5;
  88. $percent = 80;
  89. Event::handle('GetSpamFilter', array(&$filter));
  90. if (empty($filter)) {
  91. throw new Exception(_("No spam filter."));
  92. }
  93. if (have_option('a', 'all')) {
  94. testAllUsers($filter, $minimum, $percent);
  95. } else {
  96. $user = getUser();
  97. silencespammer($filter, $user, $minimum, $percent);
  98. }
  99. } catch (Exception $e) {
  100. print $e->getMessage()."\n";
  101. exit(1);
  102. }