Emacs BBDB magic: Greeting people with nicknames

| emacs

I use Gnus to read my mail within the Emacs text editor. One of the
advantages of using a mail client that's infinitely programmable is
that you can add all sorts of little tweaks to it. Gnus can be
integrated with Emacs' Big Brother Database (BBDB), a semi-structured
text database in which I store all sorts of weird notes. This little
hack takes the nick field of the database and automatically inserts a
greeting. If someone signs himself as Mikong, I should call him that
instead of Joseph Michael. Similarly, I sign my messages as Sacha, not
Sandra Jean. This little tidbit makes it easier to remember to call
people by their nicknames.

(defun sacha/gnus-add-nick-to-message ()
  "Inserts \"Hello, NICK!\" in messages based on the recipient's nick field."
  (interactive)
  (save-excursion
    (let ((bbdb-get-addresses-headers (list (assoc 'recipients bbdb-get-addresses-headers)))
          nicks)
      (setq nicks
            (delq nil
                  (mapcar (lambda (rec) (bbdb-record-getprop rec 'nick))
                          (bbdb-update-records
                           (bbdb-get-addresses nil gnus-ignored-from-addresses 'gnus-fetch-field)
                           nil
                           nil))))
      (goto-char (point-min))
      (when (and nicks
                 (re-search-forward "--text follows this line--" nil t))
        (forward-line 1)
        (insert "Hello, "
                (mapconcat 'identity nicks ", ")
                "!\n\n")))))

(defadvice gnus-post-news (after sacha activate)
  (sacha/gnus-add-nick-to-message))
You can comment with Disqus or you can e-mail me at sacha@sachachua.com.