Skip to content

Instantly share code, notes, and snippets.

@sachac
Last active August 29, 2015 14:01
Show Gist options
  • Save sachac/9aae1e34bb6f4d6d1299 to your computer and use it in GitHub Desktop.
Save sachac/9aae1e34bb6f4d6d1299 to your computer and use it in GitHub Desktop.
Gnus BBDB Salutations
;; This version is for BBDBv3 - thanks, Thomas!
(defvar wicked/gnus-nick-threshold 5 "*Number of people to stop greeting individually. Nil means always greet individually.") ;; (1)
(defvar wicked/bbdb-hello-string "Hello, %s!" "Format string for hello. Example: \"Hello, %s!\"")
(defvar wicked/bbdb-hello-all-string "Hello, all!" "String for hello when there are many people. Example: \"Hello, all!\"")
(defvar wicked/bbdb-nick-field 'nick "Symbol name for nickname field in BBDB.")
(defvar wicked/bbdb-salutation-field 'hello "Symbol name for salutation field in BBDB.")
(defun wicked/gnus-add-nick-to-message ()
"Inserts \"Hello, NICK!\" in messages based on the recipient's nick field."
(interactive)
(let ((recipients (bbdb-get-address-components))
recipient nicks rec net salutations)
(goto-char (point-min))
(when (re-search-forward "--text follows this line--" nil t)
(forward-line 1)
(if (and wicked/gnus-nick-threshold
(>= (length recipients) wicked/gnus-nick-threshold))
(insert wicked/bbdb-hello-all-string "\n\n") ;; (3)
(while recipients
(setq recipient (car recipients))
(setq net (nth 1 recipient))
(setq rec (car (bbdb-search (bbdb-records) nil nil net)))
(cond
((null rec) ;; (4)
(add-to-list 'nicks (car recipient)))
((bbdb-record-xfield rec wicked/bbdb-salutation-field) ;; (5)
(add-to-list 'salutations
(bbdb-record-xfield rec wicked/bbdb-salutation-field)))
((bbdb-record-xfield rec wicked/bbdb-nick-field) ;; (6)
(add-to-list 'nicks
(bbdb-record-xfield rec wicked/bbdb-nick-field)))
(t
(add-to-list 'nicks
(car (split-string (bbdb-record-name rec)))))) ;; (7)
(setq recipients (cdr recipients))))
(when nicks ;; (8)
(insert (format wicked/bbdb-hello-string
(mapconcat 'identity (nreverse nicks) ", "))
" "))
(when salutations ;; (9)
(insert (mapconcat 'identity salutations " ")))
(when (or nicks salutations)
(insert "\n\n")))))
(defadvice gnus-post-news (after wicked/bbdb activate)
"Insert nicknames or custom salutations."
(wicked/gnus-add-nick-to-message))
(defadvice gnus-msg-mail (after wicked/bbdb activate)
"Insert nicknames or custom salutations."
(wicked/gnus-add-nick-to-message))
(defadvice gnus-summary-reply (after wicked/bbdb activate)
"Insert nicknames or custom salutations."
(wicked/gnus-add-nick-to-message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment