index rss mastodon twitter github linkedin email
Álvaro Ramírez
sponsor

Álvaro Ramírez

08 October 2019 Spam blacklisting with Emacs org babel

Some email provider accept regular expressions to blacklist additional spam. My blacklist is long and tedious to update, but hey… Emacs org babel can simplify things here.

It's way easier to maintain a blacklist (with no regex) using an org table.

Blacklist

#+name: spam-entries
| .spammy                |
| dodgyfella@hotmail.com |
| henryzeespammer.com    |
| yumspam.com            |

and subsequently use org babel (elisp snippet) to generate the regex.

Regex gen

#+begin_src emacs-lisp :var rows=spam-entries
  (require 'dash)
  (require 's)

  (concat "^"
          (s-join "|"
                  (mapcar (lambda (entry)
                            (setq entry (regexp-quote
                                         (s-trim entry)))
                            (assert (s-present? entry))
                            (cond
                             ;; Blacklist email address: joe@spammer.spammy
                             ((s-contains-p "@" entry)
                              (format "(%s)" entry))
                             ;; Blacklist top-level domain: .spammy
                             ((s-starts-with-p "\\." entry)
                              (format "([^.]*%s)" entry))
                             ;; Blacklist domain: @spammer.spammy
                             (t
                              (format "(.*@%s)" entry))))
                          (-sort
                           'string<
                           (-map (lambda (row)
                                   (nth 0 row))
                                 rows))))
          "$")

#+end_src

#+RESULTS:
: ^([^.]*\.spammy)|(dodgyfella@hotmail\.com)|(.*@henryzeespammer\.com)|(.*@yumspam\.com)$

UPDATE: Tweaked elisp and regex (but not animation) also found John Bokma's post: Blacklisting domains with Postfix.

blacklist.png