;; 13-bbdb.el --- Big Brother DataBase (BBDB)

;; Copyright (C) 2007 - 2020 Steve Youngs

;;     Author: Steve Youngs <steve@sxemacs.org>
;; Maintainer: Steve Youngs <steve@sxemacs.org>
;;    Created: <2007-12-02>
;; Time-stamp: <Thursday Apr  9, 2020 18:51:53 steve>
;;   Download: <https://downloads.sxemacs.org/SYinits>
;;   HTMLised: <https://www.sxemacs.org/SYinits/13-bbdb.html>
;;   Git Repo: git clone https://git.sxemacs.org/syinit
;;   Keywords: init, compile

;; This file is part of SYinit

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;;
;; 3. Neither the name of the author nor the names of any contributors
;;    may be used to endorse or promote products derived from this
;;    software without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; Commentary:
;;
;;  BBDB settings.
;;

;;; Credits:
;;
;;   The HTML version of this file was created with Hrvoje Niksic's
;;   htmlize.el which is part of the XEmacs "text-modes" package.
;;

;;; Todo:
;;
;;     

;;; Code:
;:*======================
;:* Some standard stuff first
(require 'bbdb)
(require 'bbdb-pgp)

;:*=======================
;:* Make sure gnus stuff is loaded
(load-file (expand-file-name ".gnus" (user-home-directory)))

(bbdb-initialize 'gnus 'message 'w3 'sendmail)
(bbdb-insinuate-gnus)
(bbdb-insinuate-message)
(bbdb-insinuate-sc)
(bbdb-insinuate-sendmail)
(bbdb-insinuate-w3)

;; BBDB/PGP stuff
(setq 
 bbdb/pgp-method 'mml-pgpmime
 bbdb/pgp-default-action 'sign
 bbdb/pgp-quiet t)

(defun sy-bbdb-gnus-auto-notes-hook (record)
  "Runs `bbdb-auto-notes-hook' on the original article.
This is so a header doesn't have to be visible for BBDB to notice
any changes.
Kudos to Robert Fenk <fenk@users.sf.net> for this."
  (save-excursion
    (set-buffer (get-buffer gnus-article-buffer))
    (set-buffer gnus-original-article-buffer)
    (goto-char (point-min))
    (bbdb-auto-notes-hook record)))

(autoload 'gnus-convert-face-to-png "gnus-fun")

;;; FIXME: Not ideal, it currently puts cface at one end of the name,
;;; and the xface at the other.  I'd prefer both glyphs on the same
;;; side (preferrably left)
(defun sy-bbdb-display-cx-face ()
  "Search for face properties and display the faces.
This is from Alex Shroeder."
  (let ((inhibit-read-only t); edit the BBDB buffer
        (all-records bbdb-records)
        cface xface record start ext)
    (while all-records
      (setq record (caar all-records)
            cface (bbdb-record-getprop record 'cface)
            xface (bbdb-record-getprop record 'face)
            start (marker-position (nth 2 (car all-records))))
      (when (or cface xface)
        (setq ext (extent-at start)))
      (when cface
        (set-extent-begin-glyph
         ext
         (make-glyph
          (list (vector 'png :data (gnus-convert-face-to-png cface))))))
      (when xface
        (set-extent-end-glyph
         ext
         (make-glyph
          (list (vector 'xface :data (concat "X-Face: " xface)
                        :foreground "black"
                        :background "white")))))
      (setq all-records (cddr all-records)))))

(setq 
 bbdb-always-add-addresses t
 bbdb-canonicalize-redundant-nets-p t
 bbdb-canonicalize-net-hook
 (lambda (addr)
   (cond 
    ((string-match 
      "\\`\\([^0-9]+\\)\\(-dated-[^@]+\\|-[0-9]+\\|\\+[^@]+\\.[^@]+\\)\\(@.*\\)\\'"
      addr)
     (concat (substring addr (match-beginning 1) (match-end 1))
             (substring addr (match-beginning 3) (match-end 3))))
    (t addr)))
 bbdb-complete-name-allow-cycling t
 bbdb-completion-display-record t
 bbdb-completion-type 'primary-or-name
 bbdb-default-area-code 7
 bbdb-dial-local-prefix nil
 bbdb-display-layout 'multi-line
 bbdb-dwim-net-address-allow-redundancy t
 bbdb-electric-p nil
 bbdb-new-nets-always-primary 'never
 bbdb-north-american-phone-numbers-p nil
 bbdb-notice-hook 'sy-bbdb-gnus-auto-notes-hook
 bbdb-offer-save 'auto
 bbdb-pop-up-display-layout 'multi-line
 bbdb-pop-up-target-lines 7
 bbdb-quiet-about-name-mismatches nil
 bbdb-use-pop-up nil
 bbdb/gnus-score-default 25
 bbdb/gnus-summary-in-bbdb-format-letter "X"
 bbdb/mail-auto-create-p nil
 bbdb/news-auto-create-p nil)

;:*=======================
;:* All my email addresses, well the important ones anyway.
(let ((emails '("steve@steveyoungs.com"
                "steve@thereadinglamp.net"
                "steve@bastard.steveyoungs.com"
                "steve@bastard.no-ip.org"
                "steve@sxemacs.org"
                "steve@sywriting.com"
                "sryoungs@iinet.net.au"
                "steve.r.youngs@gmail.com"
                ;; The following aren't mine, but I'm using
                ;; `bbdb-user-mail-names' in
                ;; `gnus-ignored-from-addresses.  It's just easier.
                "post@gwene.org")))
  (setq bbdb-user-mail-names (regexp-opt emails t)))

;:*=======================
;:* Update some stuff automatically
(setq bbdb-auto-notes-alist
      '(("Organization"
         (".*" company 0 'replace))
        ("Newsgroups"
         ("[^,]+" newsgroups 0))
        ("Subject"
         (".*" last-subj 0 'replace))
        ("User-Agent" 
         (".*" mailer 0 'replace))
        ("X-Mailer" 
         (".*" mailer 0 'replace))
        ("X-Newsreader" 
         (".*" mailer 0 'replace))
        ("X-Attribution"
         (".*" attribution 0 'replace))
        ("X-Now-Playing"
         (".*" music 0 'replace))
        ("X-Face" 
         (".+" face 0 'replace))
        ("Face" 
         (".+" cface 0 'replace))))

;:*=======================
;:* mail aliases
(autoload 'bbdb-define-all-aliases "bbdb-com" 
  "Hook mail alias feature of BBDB into message-mode." t)

;:*=======================
;:* Hooks
(add-hook 'bbdb-notice-hook 'bbdb-auto-notes-hook)
(add-hook 'bbdb-change-hook 'bbdb-timestamp-hook) 
(add-hook 'bbdb-create-hook 'bbdb-creation-date-hook)
(add-hook 'bbdb-list-hook 'sy-bbdb-display-cx-face)
(add-hook 'message-setup-hook 'bbdb-define-all-aliases)
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*
(message "bbdb initialised")

Created with SXEmacs Valid XHTML 1.0 Transitional!
Copyright © 2020 Steve Youngs
Verbatim copying and distribution is permitted in any medium, providing this notice is preserved.
Last modified: Wed Apr 15 18:14:43 AEST 2020