;;
;; dot-gnus-mail.el --- Gnus mail-related stuff
;;

(require 'gnus-art)
(require 'mm-url)

;; Reading mail
;;
;; The mailing lists are auto-expired (i.e. read articles
;; are marked `expired' as well); probable-spam boxes
;; are total-expired (i.e. read == expired) and are
;; expired immediately, instead of after seven days.
;;
;; We use nnml for everything except for spam-storage groups,
;; which are nnmh to avoid the overhead of a huge overview file.

(defvar nix-mailing-list-directory "~/.Mail/"
  "The directory where mailing lists sit.")

(setq message-directory "~/Mail/spool/"
      mail-source-directory "~/Mail/spool/"
      mail-sources `((directory :path ,nix-mailing-list-directory ; We use procmail here, thanks
                                :suffix "")
                     (file :path "~/.Mailbox"))
      mail-source-delete-incoming t              ; These files are useless!
      mail-source-primary-source "/home/nix/.Mailbox" ; Keep getting mail from my mailbox
      mail-user-agent 'gnus-user-agent           ; With all the trimmings
      nnmail-message-id-cache-file "~/.Mail.msgid.cache" ; Message IDs (for duplicate detection) go here
      nnmail-treat-duplicates 'delete            ; Zap duplicate mails, thanks
      gnus-secondary-select-methods              ; We want to use a set of mail spools
      '((nnml ""
              (nnml-directory "/home/nix/Mail/spool/") ; Put our spool here
              (nnml-active-file "/home/nix/Mail/spool/active") ; This is the active file
              (nnml-newsgroups-file "/home/nix/Mail/spool/newsgroups") ; These are the group descriptions
              (nnml-use-compressed-files nil)    ; Don't read compressed email
              (nnml-get-new-mail t))             ; Get new mail in
        (nnmh ""
              (nnmh-directory "/home/nix/Mail/nnmh/") ; Put our NNMH spool here
              (nnmh-get-new-mail nil)))          ; This backend is for non-split groups only
      gnus-suppress-duplicates t                 ; Suppress duplicate articles
      gnus-save-duplicate-list t                 ; Remember duplicates
      gnus-gcc-mark-as-read t)                   ; Mark self-copied mail as read

;; We have to split mail correctly. We do this with a fancy split which looks at the
;; group name from the `X-Gnus-Mail-Source'; if it describes a file in the
;; `nix-mailing-list-directory', we file it in an nnml group of that name;
;; otherwise, we put it in the primary mailbox.

;; Unfortunately the :directory source does not indicate which *file* in a
;; directory is used to do the splitting. So we have to hack it to specify that.
;; Doubly unfortunately, no neat hack is possible; we cannot do advisements
;; here, we have to simply redefine the function.

(require 'mail-source)

(defun mail-source-fetch-directory (source callback)
  "Fetcher for directory sources.

Modified by Nix to tell split methods the name of the file being
grabbed from."
  (mail-source-bind (directory source)
    (mail-source-run-script
     prescript (format-spec-make ?t path) prescript-delay)
    (let ((found 0)
          (mail-source-prefix "directory:"))
      (dolist (file (directory-files
                     path t (concat (regexp-quote suffix) "$")))
        (when (and (file-regular-p file)
                   (funcall predicate file)
                   (let ((mail-source-string (concat mail-source-prefix file)))
                     (mail-source-movemail file mail-source-crash-box)))
          (incf found (mail-source-callback callback file))
          (mail-source-run-script postscript (format-spec-make ?t path))
          (mail-source-delete-crash-box)))
      found)))

;; Now split up the mail.

(setq nnmail-split-methods 'nnmail-split-fancy   ; Fancy mail splitting, please
      nnmail-split-fancy `(| ("x-gnus-mail-source"
                              ,(concat "directory:" nix-mailing-list-directory "\\(.*\\)")
                              "\\1")
                             "Mailbox")

;; MIME handling.
;;
;; Render horrible text/html invisible; display images.

      mm-discouraged-alternatives '("text/html") ; We *hates* HTML, we does
      mm-inline-large-images t                   ; All the clothes fit!
      mm-inline-text-html-with-w3m-keymap nil    ; I like w3m, but don't like its keymap
      message-default-charset 'utf-8             ; Don't nag us about charsets

;; Expire spam at once.

      nnmail-expiry-wait-function #'(lambda (group)
                                      (cond ((or (string-match "blockbox" group)
                                                 (string-match "spambox" group)
                                                 (string-match "spambox-verified" group))
                                             'immediate)
                                            (t 7))))

(add-hook 'nnmail-prepare-incoming-message-hook 'article-de-quoted-unreadable)

;; Convert Atom feeds to RSS on the fly.

(defadvice mm-url-insert (after convert-atom-to-rss nil)
  "Converts atom to RSS by calling xsltproc."
  (when (re-search-forward "xmlns=\"http://www.w3.org/.*/Atom\"" 
               nil t)
    (message "Converting Atom to RSS... ")
    (goto-char (point-min))
    (call-process-region (point-min) (point-max) 
             "xsltproc" 
             t t nil 
             (expand-file-name "~/lib/atom2rss.xsl") "-")
    (goto-char (point-min))
    (message "Converting Atom to RSS... done")))

(ad-activate 'mm-url-insert)

(provide 'dot-gnus-mail)