Skip to content

Instantly share code, notes, and snippets.

@komputerwiz
Last active November 2, 2022 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save komputerwiz/508868054c76afaf56b32f971eaffdc0 to your computer and use it in GitHub Desktop.
Save komputerwiz/508868054c76afaf56b32f971eaffdc0 to your computer and use it in GitHub Desktop.
afew filter for classifying spam using bogofilter
"""
A filter for [afew](https://afew.readthedocs.io/) that classifies spam
messages using bogofilter.
I couldn't figure out how to get bogofilter to integrate nicely with
offlineimap/notmuch/afew, so I decided to write it myself. It was
surprisingly easy, and perhaps this could inspire others to write their
own custom filters. If functionality grows more complex, I might
package this into a pip module via the `afew.filter` entry point.
If any of the creators of afew are reading this and think it might be
worth integrating, please feel free to do so.
I'd be happy to hear your comments and suggestions. Enjoy!
NOTE: Requires Python 3.5+
# Installation and Usage
1. Refer to the [bogofilter](https://bogofilter.sourceforge.io/)
documentation for info on setting up and training bogofilter.
2. Place this file in your afew config directory, e.g.,
`~/.config/afew/BogoFilter.py`.
3. Configure the filter in `~/.config/afew/config`. The following
config tells bogofilter to use XDG dirs instead of ~/.bogofilter:
[BogoFilter]
config_file = /home/user/.config/bogofilter/bogofilter.cf
bogofilter_dir = /home/user/.local/share/bogofilter/
4. Read less spam.
# MIT License
Copyright (c) 2021 Matthew Barry <matthew@komputerwiz.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from enum import Enum, unique
from pathlib import Path
import subprocess
from afew.filters.BaseFilter import Filter
from afew.FilterRegistry import register_filter
@unique
class Classification(Enum):
SPAM = 0
HAM = 1
UNSURE = 2
ERROR = 3
@register_filter
class BogoFilter(Filter):
message = 'Passing messages through bogofilter'
command = ['bogofilter']
spam_tag = 'spam'
ham_tag = None
unsure_tag = None
def __init__(self, database, config_file = None, bogofilter_dir = None, **kwargs):
super().__init__(database, **kwargs)
if config_file is not None and Path(config_file).is_file():
self.command += ['-c', config_file]
if bogofilter_dir is not None and Path(bogofilter_dir).is_dir():
self.command += ['-d', bogofilter_dir]
def handle_message(self, message):
if not self._tag_blacklist.intersection(message.get_tags()):
classification = self._classify_message(message)
if Classification.SPAM in classification and self.spam_tag is not None:
self.add_tags(message, self.spam_tag)
if Classification.HAM in classification and self.ham_tag is not None:
self.add_tags(message, self.ham_tag)
if Classification.UNSURE in classification and self.unsure_tag is not None:
self.add_tags(message, self.unsure_tag)
def _classify_message(self, message):
return {self._classify_file(f) for f in message.get_filenames() if Path(f).is_file()}
def _classify_file(self, input_file):
result = subprocess.run(self.command + ['-I', input_file])
return Classification(result.returncode)
@agenbite
Copy link

agenbite commented Nov 2, 2022

Hi! Thanks for this, I've been using it for a while and it rocks! However, I get too much mail classified as "unsure", and I'd like to have it tagged as spam and leave it for a later revision. For that, I'd need to be able to establish a set of tags for those cases (i.e. +bogounsure +spam -new). I tried to modify your script in order to do that, but I don't seem to know how to do it. I guess my main blocker is that the base filter does not care for anything else than tags defined under that precise keyword ("tags=..."). Do you have an idea of how to proceed from here? Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment