3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011,2012, StatusNet, Inc.
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011,2012 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Check new notices with activity spam service.
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011,2012 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class ActivitySpamPlugin extends Plugin
49 const PLUGIN_VERSION = '2.0.0';
51 public $server = null;
52 public $hideSpam = false;
54 const REVIEWSPAM = 'ActivitySpamPlugin::REVIEWSPAM';
55 const TRAINSPAM = 'ActivitySpamPlugin::TRAINSPAM';
60 * @return boolean hook value; true means continue processing, false means stop.
64 $this->filter = new SpamFilter(common_config('activityspam', 'server'),
65 common_config('activityspam', 'consumerkey'),
66 common_config('activityspam', 'secret'));
68 $this->hideSpam = common_config('activityspam', 'hidespam');
70 // Let DB_DataObject find Spam_score
72 common_config_set('db', 'class_location',
73 common_config('db', 'class_location') .':'.dirname(__FILE__));
79 * Database schema setup
84 * @return boolean hook value; true means continue processing, false means stop.
87 function onCheckSchema()
89 $schema = Schema::get();
90 $schema->ensureTable('spam_score', Spam_score::schemaDef());
92 Spam_score::upgrade();
98 * When a notice is saved, check its spam score
100 * @param Notice $notice Notice that was just saved
102 * @return boolean hook value; true means continue processing, false means stop.
105 function onEndNoticeSave(Notice $notice)
109 $result = $this->filter->test($notice);
111 $score = Spam_score::saveNew($notice, $result);
113 $this->log(LOG_INFO, "Notice " . $notice->id . " has spam score " . $score->score);
115 } catch (Exception $e) {
117 $this->log(LOG_ERR, $e->getMessage());
123 function onNoticeDeleteRelated($notice) {
124 $score = Spam_score::getKV('notice_id', $notice->id);
125 if (!empty($score)) {
131 function onUserRightsCheck($profile, $right, &$result) {
133 case self::REVIEWSPAM:
134 case self::TRAINSPAM:
135 $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
142 function onGetSpamFilter(&$filter) {
143 $filter = $this->filter;
147 function onEndShowNoticeOptionItems($nli)
149 // FIXME: Cannot use type-hint NoticeListItem as NoticeListItemAdapter exists, too!
150 assert(is_object($nli));
152 $profile = Profile::current();
154 if (!empty($profile) && $profile->hasRight(self::TRAINSPAM)) {
156 $notice = $nli->getNotice();
157 $out = $nli->getOut();
159 if (!empty($notice)) {
161 $score = Spam_score::getKV('notice_id', $notice->id);
164 // If it's empty, we can train it.
165 $form = new TrainSpamForm($out, $notice);
167 } else if ($score->is_spam) {
168 $form = new TrainHamForm($out, $notice);
170 } else if (!$score->is_spam) {
171 $form = new TrainSpamForm($out, $notice);
181 * Map URLs to actions
183 * @param URLMapper $m path-to-action mapper
185 * @return boolean hook value; true means continue processing, false means stop.
188 public function onRouterInitialized(URLMapper $m)
190 $m->connect('main/train/spam',
191 ['action' => 'train',
192 'category' => 'spam']);
193 $m->connect('main/train/ham',
194 ['action' => 'train',
195 'category' => 'ham']);
196 $m->connect('main/spam',
197 ['action' => 'spam']);
201 function onEndShowStyles($action)
203 $action->element('style', null,
204 '.form-train-spam input.submit { background: url('.$this->path('icons/bullet_black.png').') no-repeat 0px 0px } ' . "\n" .
205 '.form-train-ham input.submit { background: url('.$this->path('icons/exclamation.png').') no-repeat 0px 0px } ');
209 function onEndPublicGroupNav(Menu $nav)
211 $user = common_current_user();
213 if (!empty($user) && $user->hasRight(self::REVIEWSPAM)) {
214 $nav->out->menuItem(common_local_url('spam'),
216 // TRANS: Menu item title in search group navigation panel.
217 _('Notices marked as spam'),
218 $nav->actionName == 'spam',
219 'nav_timeline_spam');
225 function onPluginVersion(array &$versions)
227 $versions[] = array('name' => 'ActivitySpam',
228 'version' => self::PLUGIN_VERSION,
229 'author' => 'Evan Prodromou',
230 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ActivitySpam',
232 _m('Test notices against the Activity Spam service.'));
236 function onEndNoticeInScope(Notice $notice, Profile $profile, &$bResult)
238 if ($this->hideSpam) {
241 $score = Spam_score::getKV('notice_id', $notice->id);
243 if (!empty($score) && $score->is_spam) {
244 if (empty($profile) ||
245 ($profile->id !== $notice->profile_id &&
246 !$profile->hasRight(self::REVIEWSPAM))) {
257 * Pre-cache our spam scores if needed.
259 function onEndNoticeListPrefill(array &$notices, array &$profiles, array $notice_ids, Profile $scoped=null) {
260 if ($this->hideSpam) {
261 Spam_score::multiGet('notice_id', $notice_ids);