Filtering bots with erc

Published 2014-09-21

It's easy to get annoyed with bots on IRC.

Introduction

Much chatter happens on IRC at work. The channel where my group hangs is the primary conduit of information, the other being the dreaded Outlook. The channel is, however, plagued with bots, and I wanted a way to filter them and their prattle.

Resolution

I use erc, and although it has an ignore list, I wanted something that was specific to a channel. Here's the result:

(defun private/erc-filter-bot (channel bots)
  "Filter messages to/from bots.
CHANNEL is the name of the channel to watch.
BOTS are a list of bots (nicks) to filter."
  (message "Filtering %s channel for %s bots" channel bots)
  (let ((bot-list `()))
    (dolist (bot bots)
      (push (format "%s:" bot) bot-list)
      (push (format "<%s>" bot) bot-list))
    (message "Bot list %s" bot-list)
    (add-hook 'erc-insert-pre-hook (function (lambda (msg)
                                               (when (string-match (buffer-name) channel)
                                                 (dolist (bot-indicator bot-list)
                                                   (when (string-match bot-indicator msg)
                                                     (message "Filtering bot: %s" msg)
                                                     (setq erc-insert-this nil)))))))))

(jwp/erc-filter-bot "#emacs" '("foo" "bar" "baz"))
(jwp/erc-filter-bot "#lisp" '("foo" "bar" "baz"))

This code requires lexical binding.