def train(self, req, log_id, spam=True): environ = {} for name, value in req.environ.items(): if not name.startswith('HTTP_'): environ[name] = value entry = LogEntry.fetch(self.env, log_id) if entry: self.log.debug('Marking as %s: %r submitted by "%s"', spam and 'spam' or 'ham', shorten_line(entry.content), entry.author) fakeenv = environ.copy() for header in entry.headers.splitlines(): name, value = header.split(':', 1) if name == 'Cookie': # breaks SimpleCookie somehow continue cgi_name = 'HTTP_%s' % name.strip().replace('-', '_').upper() fakeenv[cgi_name] = value.strip() fakeenv['REQUEST_METHOD'] = 'POST' fakeenv['PATH_INFO'] = entry.path fakeenv['wsgi.input'] = StringIO('') fakeenv['REMOTE_ADDR'] = entry.ipnr if entry.authenticated: fakeenv['REMOTE_USER'] = entry.author for strategy in self.strategies: strategy.train(Request(fakeenv, None), entry.author or 'anonymous', entry.content, spam=spam) entry.update(rejected=spam)
def _render_monitoring_entry(self, req, cat, page, entry_id): req.perm.require('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 %(id)s", id=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 %(id)s", id=next.id)) return {'entry': entry, 'train_only': self.train_only}
def delete(self, req, ids, stats): if not isinstance(ids, list): ids = [ids] if stats: for log_id in ids: entry = LogEntry.fetch(self.env, log_id) if entry: type = "spam" if entry.rejected else "ham" status = "delete" for strategy in self.strategies: count = entry.findreasonvalue(get_strategy_name(strategy)) if count: spamstatus = count < 0 self._record_action(status, type, ("ok" if spamstatus == entry.rejected else "error"), strategy, 0) else: self._record_action(status, type, '', strategy, 0) self._record_action(status, type, '', '', 0) LogEntry.delete(self.env, ids)
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 train(self, req, ids, spam=True, delete=False): environ = {} for name, value in req.environ.items(): if not name.startswith('HTTP_'): environ[name] = value if not isinstance(ids, list): ids = [ids] for log_id in ids: start = time.time() entry = LogEntry.fetch(self.env, log_id) if entry: extint = "trainint" self.log.debug('Marking as %s: %r submitted by "%s"', spam and 'spam' or 'ham', shorten_line(entry.content), entry.author) fakeenv = environ.copy() for header in entry.headers.splitlines(): name, value = header.split(':', 1) if name == 'Cookie': # breaks SimpleCookie somehow continue cgi_name = 'HTTP_%s' % name.strip().replace('-', '_').upper() fakeenv[cgi_name] = value.strip() fakeenv['REQUEST_METHOD'] = 'POST' fakeenv['PATH_INFO'] = entry.path fakeenv['wsgi.input'] = StringIO('') fakeenv['REMOTE_ADDR'] = entry.ipnr if entry.authenticated: fakeenv['REMOTE_USER'] = entry.author type = "spam" if spam else "ham" for strategy in self.strategies: status = "trainskip" if (self.use_external and self.train_external) or not strategy.is_external(): tim = time.time() extint = "trainext" res = strategy.train(Request(fakeenv, None), entry.author or 'anonymous', entry.content, entry.ipnr, spam=spam) tim = time.time()-tim if tim > 3: self.log.warn('Training %s took %d seconds to ' 'complete.', strategy, tim) if res == -1: status = "trainerror" elif res == -2: status = "traincond" elif res > 0: status = "train" count = entry.findreasonvalue(get_strategy_name(strategy)) if count: spamstatus = count < 0 self._record_action(status, type, ("ok" if spamstatus == spam else "error"), strategy, tim) else: self._record_action(status, type, '', strategy, tim) self._record_action(extint, type, ("ok" if entry.rejected == spam else "error"), '', time.time()-start) entry.update(rejected=spam) if delete: self.delete(req, log_id, True)