silencespammer.php 3.4 KB

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