Hackery

2006-06-16 08:14 pm
mdlbear: (hacker glider)
[personal profile] mdlbear

Spent some time this morning filling in the remaining holes in my Java git blob classes -- specifically the part that conses up a header into a byte array. I didn't need it for the stream code, since it was more efficient to write it into the stream piecewise.

Spent most of the afternoon hacking Emacs lisp to finally solve a problem with gnus automatic mail-foldering that's been bothering me for a long time. You see, gnus (the Emacs mail/news reader) lets you match a series of regular expressions against your mail headers in order to decide what folder it belongs in. A lot of mail from mailing lists contains strings like "[mumble]" -- a tag in square brackets. Turns out our new spam-filtering appliance does that, too. Trouble is, gnus insists that the thing you match look like a word, meaning it has to begin and end with an alphanumeric.

This is what's called a "delimited search" in Emacs, and most of the time it's what you want when you're matching keywords in email headers. Not this time, though. But this being Emacs, after all, there's a way around the problem: write a hook function (nnmail-split-hook) that rewrites the subject header to replace "[" and "]" with "x_" and "_x" respectively. (It took me a while to figure those out, too. They make the rewritten tag begin and end with a letter, but also surround the original tag with non-letters for the sake of the many older expressions that just match the word. Big win all around.

After getting the basic code to work, using the constant "[spam]", it was time to parametrize it with the list of expected tags. This being elisp, it took a while to get right. I had quite a lot of trouble getting regexp substitution to work, probably because of some stupid misunderstanding that I was too lazy to clear up. I ended up doing a mapcar down the list of tags, concatenating up the appropriate match and replacement strings for the substitute. Somewhat crude baroque, but it worked.

(setq subject-tags '( "worm infected" "spam"))

(add-hook 'nnmail-split-hook 'my-nnmail-split-hook)
(defun my-nnmail-split-hook () 
  "Hook function to be called before splitting mail.  Its main function
   is to make it possible to match subject tags like [this] when doing
   nnmail-split-fancy.  It does this by replacing the brackets with x_
   and _x; this allows matching either the bare word or the bracketed tag.
   It relies on the fact that _ is not a word constituent.

   Uses a variable called subject-tags that contains a list of strings,
   one for each recognized tag.
  "
  (mapcar (function (lambda (tag)
                      (save-excursion
                        (and (re-search-forward "^Subject:" nil t)
                             (perform-replace (concat "[" tag "]")
                                              (concat "x_" tag "_x")
                                              nil nil nil 1)))))
                    subject-tags)
  )

Now you know why God wrote in Lisp.

Date: 2006-06-17 04:08 am (UTC)
kayshapero: (Default)
From: [personal profile] kayshapero
He likes lots of irritating silly parentheses? (snerk)

You folks coming to Worldcon?

Date: 2006-06-17 08:01 pm (UTC)
kayshapero: (Default)
From: [personal profile] kayshapero
With any luck. I don't expect to be able to drag Nicolai there, given that he has to work that week, but I'm planning on coming.

Most Popular Tags

Style Credit

Page generated 2026-01-03 06:12 am
Powered by Dreamwidth Studios