/usr/share/pyshared/tracspamfilter/admin.py is in trac-spamfilter 0.2.1+svn6871-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2006 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://projects.edgewall.com/trac/.
import os
import urllib2
from pkg_resources import require, resource_filename, ResolutionError
from trac import __version__ as VERSION
from trac.core import *
from trac.util import format_datetime, pretty_timedelta, shorten_line, sorted
from trac.web import Request, HTTPNotFound
from trac.web.chrome import add_link, add_stylesheet, ITemplateProvider
from tracspamfilter.api import FilterSystem
from tracspamfilter.model import LogEntry
from tracspamfilter.filters.akismet import AkismetFilterStrategy
try:
from tracspamfilter.filters.bayes import BayesianFilterStrategy
except ImportError: # SpamBayes not installed
BayesianFilterStrategy = None
try: # Trac 0.11
from trac.admin import IAdminPanelProvider
except ImportError:
IAdminPanelProvider = None
try: # Trac 0.10 with WebAdmin plugin
require("TracWebAdmin")
from webadmin.web_ui import IAdminPageProvider
except (ResolutionError, ImportError):
IAdminPageProvider = None
class SpamFilterAdminPageProvider(Component):
"""Web administration panel for configuring and monitoring the spam
filtering system.
"""
implements(ITemplateProvider)
if IAdminPanelProvider:
implements(IAdminPanelProvider)
elif IAdminPageProvider:
implements(IAdminPageProvider)
MAX_PER_PAGE = 15
# IAdminPanelProvider methods
def get_admin_panels(self, req):
if req.perm.has_permission('SPAM_CONFIG'):
yield ('spamfilter', 'Spam Filtering', 'config', 'Configuration')
if req.perm.has_permission('SPAM_MONITOR'):
yield ('spamfilter', 'Spam Filtering', 'monitor', 'Monitoring')
def render_admin_panel(self, req, cat, page, path_info):
if page == 'config':
if req.method == 'POST':
if self._process_config_panel(req):
req.redirect(req.href.admin(cat, page))
data = self._render_config_panel(req, cat, page)
else:
if req.method == 'POST':
if self._process_monitoring_panel(req):
req.redirect(req.href.admin(cat, page,
page=req.args.get('page')))
if path_info:
data = self._render_monitoring_entry(req, cat, page, path_info)
page = 'entry'
else:
data = self._render_monitoring_panel(req, cat, page)
add_stylesheet(req, 'spamfilter/admin.css')
return 'admin_spam%s.html' % page, data
# IAdminPageProvider methods
get_admin_pages = get_admin_panels
def process_admin_request(self, req, cat, page, path_info):
template, data = self.render_admin_panel(req, cat, page, path_info)
# Kludges for ClearSilver
if 'entries' in data:
for idx, entry in enumerate(data['entries']):
data['entries'][idx] = _entry_to_hdf(req, entry)
elif 'entry' in data:
data['entry'] = _entry_to_hdf(req, data['entry'])
req.hdf['admin.spamfilter'] = data
return template.replace('.html', '.cs'), None
# ITemplateProvider
def get_htdocs_dirs(self):
"""Return the absolute path of a directory containing additional
static resources (such as images, style sheets, etc).
"""
return [('spamfilter', resource_filename(__name__, 'htdocs'))]
def get_templates_dirs(self):
"""Return the absolute path of the directory containing the provided
ClearSilver templates.
"""
return [resource_filename(__name__, 'templates')]
# Internal methods
def _render_config_panel(self, req, cat, page):
req.perm.assert_permission('SPAM_CONFIG')
filtersys = FilterSystem(self.env)
strategies = []
for strategy in filtersys.strategies:
info = {'name': strategy.__class__.__name__,
'karma_points': strategy.karma_points,
'karma_help': strategy.__class__.karma_points.__doc__}
strategies.append(info)
return {
'strategies': sorted(strategies, key=lambda x: x['name']),
'min_karma': filtersys.min_karma,
'logging_enabled': filtersys.logging_enabled,
'purge_age': filtersys.purge_age
}
def _process_config_panel(self, req):
req.perm.assert_permission('SPAM_CONFIG')
try:
min_karma = int(req.args.get('min_karma'))
self.config.set('spam-filter', 'min_karma', min_karma)
except ValueError:
pass
for strategy in FilterSystem(self.env).strategies:
option = strategy.__class__.karma_points
points = req.args.get(strategy.__class__.__name__ +
'_karmapoints')
if points is not None:
self.config.set(option.section, option.name, points)
logging_enabled = 'logging_enabled' in req.args
self.config.set('spam-filter', 'logging_enabled',
str(logging_enabled).lower())
if logging_enabled:
try:
purge_age = int(req.args.get('purge_age'))
self.config.set('spam-filter', 'purge_age', purge_age)
except ValueError:
pass
self.config.save()
return True
def _render_monitoring_panel(self, req, cat, page):
req.perm.assert_permission('SPAM_MONITOR')
try:
pagenum = int(req.args.get('page', 1)) - 1
except ValueError:
pagenum = 1
total = LogEntry.count(self.env)
offset = pagenum * self.MAX_PER_PAGE
entries = list(LogEntry.select(self.env, limit=self.MAX_PER_PAGE,
offset=offset))
if pagenum > 0:
add_link(req, 'prev', req.href.admin(cat, page, page=pagenum),
'Previous Page')
if offset + self.MAX_PER_PAGE < total:
add_link(req, 'next', req.href.admin(cat, page, page=pagenum+2),
'Next Page')
return {
'enabled': FilterSystem(self.env).logging_enabled,
'entries': entries,
'offset': offset + 1,
'page': pagenum + 1,
'total': total
}
def _render_monitoring_entry(self, req, cat, page, entry_id):
req.perm.assert_permission('SPAM_MONITOR')
entry = LogEntry.fetch(self.env, entry_id)
if not entry:
raise HTTPNotFound('Log entry not found')
previous = entry.get_previous()
if previous:
add_link(req, 'prev', req.href.admin(cat, page, previous.id),
'Log Entry %d' % previous.id)
add_link(req, 'up', req.href.admin(cat, page), 'Log Entry List')
next = entry.get_next()
if next:
add_link(req, 'next', req.href.admin(cat, page, next.id),
'Log Entry %d' % next.id)
return {'entry': entry}
def _process_monitoring_panel(self, req):
req.perm.assert_permission('SPAM_TRAIN')
filtersys = FilterSystem(self.env)
if 'markspam' in req.args or 'markham' in req.args:
spam = 'markspam' in req.args
for entry_id in req.args.getlist('sel'):
filtersys.train(req, entry_id, spam=spam)
elif 'delete' in req.args:
for entry_id in req.args.getlist('sel'):
LogEntry.delete(self.env, entry_id)
return True
def _entry_to_hdf(req, entry):
return {
'id': entry.id,
'time': format_datetime(entry.time),
'timedelta': pretty_timedelta(entry.time),
'path': entry.path,
'url': req.abs_href(entry.path),
'path_clipped': shorten_line(entry.path, 25),
'href': req.href(entry.path),
'admin_href': req.href.admin('spamfilter', 'monitor', entry.id),
'author': entry.author,
'author_clipped': shorten_line(entry.author, 25),
'ipnr': entry.ipnr,
'authenticated': entry.authenticated,
'headers': entry.headers,
'content': shorten_line(entry.content),
'full_content': entry.content,
'rejected': entry.rejected,
'karma': entry.karma, 'reasons': entry.reasons
}
class AkismetAdminPageProvider(Component):
"""Web administration panel for configuring the Akismet spam filter."""
if IAdminPanelProvider:
implements(IAdminPanelProvider)
elif IAdminPageProvider:
implements(IAdminPageProvider)
# IAdminPanelProvider methods
def get_admin_panels(self, req):
if req.perm.has_permission('SPAM_CONFIG'):
yield ('spamfilter', 'Spam Filtering', 'akismet', 'Akismet')
def render_admin_panel(self, req, cat, page, path_info):
req.perm.assert_permission('SPAM_CONFIG')
akismet = AkismetFilterStrategy(self.env)
data = {}
if req.method == 'POST':
if 'cancel' in req.args:
req.redirect(req.href.admin(cat, page))
api_url = req.args.get('api_url')
api_key = req.args.get('api_key')
try:
if not akismet._verify_key(req, api_url, api_key):
data['error'] = 'The API key is invalid'
else:
self.config.set('spam-filter', 'akismet_api_url', api_url)
self.config.set('spam-filter', 'akismet_api_key', api_key)
self.config.save()
req.redirect(req.href.admin(cat, page))
except urllib2.URLError, e:
data['error'] = e.reason[1]
else:
api_url = akismet.api_url
api_key = akismet.api_key
data.update({'api_key': api_key, 'api_url': api_url})
add_stylesheet(req, 'spamfilter/admin.css')
return 'admin_akismet.html', data
# IAdminPanelProvider methods
get_admin_pages = get_admin_panels
def process_admin_request(self, req, cat, page, path_info):
template, data = self.render_admin_panel(req, cat, page, path_info)
req.hdf['admin.akismet'] = data
return template.replace('.html', '.cs'), None
class BayesAdminPageProvider(Component):
"""Web administration panel for configuring the Bayes spam filter."""
if BayesianFilterStrategy:
if IAdminPanelProvider:
implements(IAdminPanelProvider)
elif IAdminPageProvider:
implements(IAdminPageProvider)
# IAdminPanelProvider methods
def get_admin_panels(self, req):
if req.perm.has_permission('SPAM_CONFIG'):
yield ('spamfilter', 'Spam Filtering', 'bayes', 'Bayes')
def render_admin_panel(self, req, cat, page, path_info):
req.perm.assert_permission('SPAM_CONFIG')
bayes = BayesianFilterStrategy(self.env)
hammie = bayes._get_hammie()
data = {}
if req.method == 'POST':
if 'train' in req.args:
bayes.train(None, None, req.args['content'],
spam='spam' in req.args['train'].lower())
req.redirect(req.href.admin(cat, page))
elif 'test' in req.args:
data['content'] = req.args['content']
try:
data['score'] = hammie.score(req.args['content'].encode('utf-8'))
except Exception, e:
self.log.warn('Bayes test failed: %s', e, exc_info=True)
data['error'] = unicode(e)
else:
if 'reset' in req.args:
self.log.info('Resetting SpamBayes training database')
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("DELETE FROM spamfilter_bayes")
db.commit()
try:
min_training = int(req.args['min_training'])
if min_training != bayes.min_training:
self.config.set('spam-filter', 'bayes_min_training',
min_training)
self.config.save()
except ValueError:
pass
req.redirect(req.href.admin(cat, page))
data.update({'min_training': bayes.min_training,
'nspam': hammie.bayes.nspam,
'nham': hammie.bayes.nham})
add_stylesheet(req, 'spamfilter/admin.css')
return 'admin_bayes.html', data
# IAdminPanelProvider methods
get_admin_pages = get_admin_panels
def process_admin_request(self, req, cat, page, path_info):
template, data = self.render_admin_panel(req, cat, page, path_info)
if 'score' in data:
data['score'] = '%.2f' % (data['score'] * 100)
req.hdf['admin.bayes'] = data
return template.replace('.html', '.cs'), None
|