weblog d’un abbe


Ready to assassinate spam

Filed under: Experiments, Fun, Hacking — Tags: , , , , , — abbe @ 2136

Since a week or two, I noticed I suddenly started to get many spams (at the rate of around 10/day) delivered to my gmail inbox. I reported them every time, but that is of not much use. So tired of reporting spams, I started to create filters (in gmail) based on From address, ‘Subject’ etc. to delete them as soon they arrive, and I noticed that some of the ham mails also got deleted due to my filters. So, to get rid of this problem yesternight I installed SpamAssassin (which I’m afraid of installing earlier because I heard of its complex configuration) on my box integrated with postfix instance on my Gentoo GNU/Linux. The installation (thanks to portage) is very easy and integration with postfix is also quite easy and well documented on its wiki. And now, the best part, my SpamAssassin today recognized those mails as spam, which gmail‘s spam filters delivered into my inbox :).

Following is a Gnus hack to report spam as ham and ham as spam :

(defun my-gnus-mark-spam()
  (gnus-summary-save-in-pipe "spamc -L spam")
  ;; substitute "nnmaildir:spam" with the name of group holding "spam"
  (gnus-summary-move-article nil "nnmaildir:spam"))

(defun my-gnus-mark-ham()
  (gnus-summary-save-in-pipe "spamc -L ham")
  (gnus-summary-respool-article nil))

;; in summary mode use "B s" and "B h" keys to mark a mail as spam and ham
;; respectively for SpamAssassin to learn, and to also move to "spam" maildir
;; or respool mail accordingly
(define-key gnus-summary-mode-map (kbd "B s") 'my-gnus-mark-spam)
(define-key gnus-summary-mode-map (kbd "B h") 'my-gnus-mark-ham)

I could have used Gnus built-in spam filtering, but Gnus is not the only MUA I use ;) . Anyways, happy assassinating spam…;)


A Gnus Hack

Filed under: Hacking — Tags: , , , , — abbe @ 1317
;; Add to ~/.gnus
(defcustom my-inline-pgp-mails-list
  "List of email address which only accept inline-PGP signed mails"
  :type '(repeat string))

(defun my-list-all-recipients()
  "Lists all recipients in the current buffer"
  (let ((list-of-recipients)
        (list-of-emails '()))
    (setq list-of-recipients (split-string (concat (message-fetch-field "to") ","
                                     (message-fetch-field "bcc") ","
                                     (message-fetch-field "cc")) ","))
    (dolist (recipient list-of-recipients)
      (when (string-match "\\([[:alnum:].-]+@[[:alnum:].-]*\\)" recipient)
        (add-to-list 'list-of-emails (match-string 1 recipient))))

(defun my-sign-mail()
  "Sends a PGP signed mail depending on whether recipient allows PGP/MIME signed mails"
  (let ((recipients (my-list-all-recipients))
        (message-signed nil))
    (dolist (email my-inline-pgp-mails-list)
      (when (member email recipients)
        (message "Recipient found as %s" email)
        (setq message-signed t)
        (return t)))
    (unless message-signed

(add-hook 'message-send-hook 'my-sign-mail)

Above Emacs Lisp code allows one to send PGP signed messages, in the way preferred by recipient. e.g. There are some lists which don’t allow multipart/signed message, so in order to post to those lists, one needs to send inline PGP signed messages. This piece of code takes care of all that. All you need to do is to specify email addresses which don’t acceptmultipart/signed messages using M-x customize-variable my-inline-pgp-mails-list. Happy Gnusing… ;)

P.S. The code is also pasted at paste.lisp.org.

Create a free website or blog at WordPress.com.