;;; requires
;;; ########

(require 'gnus-score)
(require 'gnus-cite)
(require 'bbdb-gnus)
(require 'gnus-xmas)
(require 'spam)
(require 'pgg)
(require 'bbdb-pgp)
(require 'mailcrypt)

(when (run-on-home-machinep)
  (require 'smtpmail))

;;; basic configuration
;;; ###################

(setq user-full-name "Eric Knauel"
      user-mail-address "knauel@informatik.uni-tuebingen.de"
      message-from-style 'angels
      gnus-local-domain "informatik.uni-tuebingen.de")

(setq ek-wsi-addresses-rx
      (concat "knauel@informatik\\.uni-tuebingen\\.de\\|"
              "eric\\.knauel@informatik\\.uni-tuebingen\\.de"))
      
(setq ek-macnews-addresses-rx
      (concat "eric@macnews\\.de\\|"
              "ek@macnews\\.de\\|"
              "eric@knauel\\.org\\|"
              "e\\.knauel@macnews\\.de\\|"
              "ek@onx\\.de"))

(setq ek-all-addresses-rx 
      (concat ek-wsi-addresses-rx "\\|" ek-macnews-addresses-rx))

(setq gnus-ignored-from-addresses ek-all-addresses-rx
      message-dont-reply-to-names ek-all-addresses-rx)

;;; mail sources
;;; ############

(when (run-on-home-machinep)
  (setq gnus-select-method
        '(nnimap "knauel.org"
          (nnimap-address "abbruchco.de")
          (nnimap-server-port 993)
          (nnimap-stream ssl)
          (nnimap-authenticator cram-md5)))
  
  (setq gnus-secondary-select-methods
        '((nnimap "macnews.de"
                  (nnimap-address "webmail.macnews.de")
                  (nnimap-server-port 993)
                  (nnimap-stream ssl)
                  (nnimap-authenticator cram-md5))
          (nntp "news.gmane.org"
                (nntp-address "news.gmane.org")
                (nntp-port-number 119))
          (nntp "news.1und1.de"
                (nntp-address "news.1und1.de")
                (nntp-port-number 119))))

  (setq gnus-agent-synchronize-flags t))

(when (run-on-wsi-machinep)

  (setq mail-sources 
        '((file :path "~/mailsync/mailbox")
          (directory :path "~/mailsync/" :suffix ".spool")))
  
  (setq mail-source-delete-incoming t
        nnmail-directory (expand-file-name "~/Mail")
        nnml-directory (expand-file-name "~/Mail/nnml")
        nnfolder-directory (expand-file-name "~/Mail/nnfolder"))

  (setq gnus-select-method 
        '(nntp "newsserv.zdv.uni-tuebingen.de"
          (nntp-address "localhost")
          (nntp-port-number 5002)))

  (setq gnus-secondary-select-methods 
        '((nnml "")
          (nnfolder "")
          (nntp "news.gmane.org"
                (nntp-address "localhost")
                (nntp-port-number 5001)))))

;;; misc stuff
;;; ##########

(setq gnus-large-newsgroup 2000
      gnus-check-new-newsgroups nil
      gnus-read-active-file 'some
      gnus-nov-is-evil nil)

(setq gnus-interactive-exit t
      message-signature t)

(setq gnus-save-newsrc-file nil
      gnus-save-killed-list nil)

(setq mm-discouraged-alternatives 
      (append mm-discouraged-alternatives 
              '("text/html" "text/richtext")))

(add-to-list 'mm-text-html-renderer-alist 
             '(vilistextum mm-inline-render-with-file mm-links-remove-leading-blank
               "vilistextum" "-l" "-r" "-c" file "-"))

(add-to-list 'mm-text-html-washer-alist 
             '(vilistextum mm-inline-wash-with-file mm-links-remove-leading-blank
               "vilistextum" "-l" "-r" "-c" file "-"))

(setq mm-text-html-renderer 'links)

;;; sending mails
;;; #############

(when (run-on-home-machinep)
  
  (setq smtpmail-queue-mail nil
        smtpmail-debug-info t
        smtpmail-smtp-service 587
        message-sendmail-f-is-evil t
        mail-envelope-from "eric@knauel.org"

        message-send-mail-function 'smtpmail-send-it
        send-mail-function 'smtpmail-send-it

        smtpmail-smtp-server "abbruchco.de"
        smtpmail-local-domain "knauel.org")

  ;; A hack. Somehow LOGIN doesn't seem to work 
  ;; with abbruchco.de...
  (setq smtpmail-auth-supported '(cram-md5))

  (defvar ejk-knauel-org-password nil)

  (setq ejk-knauel-org-password
        (let ((password-file (expand-file-name "~/.gnupg/password.knauel.org")))
          (if (file-exists-p password-file)
              (with-temp-buffer
                (insert-file-contents password-file)
                (buffer-string)))))

  (setq smtpmail-auth-credentials 
        `(("abbruchco.de"
           587 "eric@knauel.org" ,ejk-knauel-org-password)))

  (setq smtpmail-starttls-credentials
        `(("abbruchco.de" 587
           ,(expand-file-name "~/.gnupg/ssl/self-signed.key")
           ,(expand-file-name "~/.gnupg/ssl/self-signed.cert")))))

(when (run-on-wsi-machinep)
  (setq send-mail-function 'message-send-mail-with-sendmail
        message-send-mail-function 'message-send-mail-with-sendmail))

(add-to-list 'message-syntax-checks '(sender . disabled))

(setq message-generate-headers-first t
      mime-editor/split-message nil)

;;; summary and article buffer
;;; ##########################

(setq gnus-visible-headers 
      (mapconcat 'regexp-quote
                 '("From:" "Newsgroups:" "Subject:" "Date:" 
                   "Organization:" "To:" "Cc:" "Followup-To" "Gnus-Warnings:"
                   "X-Sent:" "X-URL:" "User-Agent:" "X-Newsreader:"
                   "X-Mailer:" "Reply-To:" "X-Spam:" "X-Spam-Status:" "X-Now-Playing"
                   "X-Attachments" "X-Diagnostic")
                 "\\|"))

(setq gnus-group-line-format "%P  %(%~(pad-right 50)G%) unread: %-4N\n"
      gnus-summary-line-format "%U%R %d %*%5L%I %(%z%[%-23,23f%]%) %s\n"
      gnus-topic-line-format "%i[ %0{%(%n (new: %a)%)%} ]\n")

(setq gnus-auto-select-first t
      gnus-auto-select-next t
      gnus-fetch-old-headers 200

      gnus-thread-sort-functions 
      '((not gnus-thread-sort-by-date))

      gnus-article-sort-functions
      '((not gnus-article-sort-by-date))

      gnus-show-mime t
      gnus-show-mime-method 'gnus-article-preview-mime-message
      gnus-buttonized-mime-types '(".*/.*")
      message-forward-before-signature nil
      mm-inline-large-images nil
      gnus-article-date-lapsed-new-header t

      gnus-inews-mark-gcc-as-read t
      gnus-gcc-mark-as-read t)

;;; group parameters
;;; ################

(setq gnus-parameters
      '(("gmane\\..*"
          (spam-contents gnus-group-spam-classification-ham)
          (spam-process
           (gnus-group-spam-exit-processor-report-gmane)))))

;;; posting styles
;;; ##############

(setq gnus-posting-styles
      '(("INBOX"
         (from "Eric Knauel <eric@knauel.org>")
         (bcc "eric@knauel.org"))
        ("INBOX.wsi"
         (from "Eric Knauel <knauel@informatik.uni-tuebingen.de>")
         (bcc "knauel@informatik.uni-tuebingen.de"))))

;;; GnuPG
;;; #####

(mc-setversion "gpg")
(setq mc-passwd-timeout 600
      gpg-default-key-id "FD866533"
      mm-verify-option 'known
      mm-decrypt-option 'known)

(setq mml2015-use 'pgg)
(setq pgg-scheme 'gpg
      pgg-cache-passphrase t
      pgg-passphrase-cache-expiry 1800
      pgg-gpg-user-id "FD866533")

(setq mml-default-sign-method "pgpmime")

;;; backend
;;; #######

(setq nnfolder-nov-is-evil t
      nnmail-use-long-file-names t
      gnus-subscribe-newsgroup-method 'gnus-subscribe-topics)

;;; visual
;;; ######

(setq gnus-treat-emphasize t
      gnus-treat-buttonize t
      gnus-treat-buttonize-head 'head
      gnus-treat-unsplit-urls 'last
      gnus-treat-leading-whitespace 'head
      gnus-treat-highlight-citation t
      gnus-treat-highlight-signature t
      gnus-treat-date-lapsed 'head
      gnus-treat-strip-trailing-blank-lines t
      gnus-treat-strip-cr t
      gnus-treat-overstrike nil
      gnus-treat-display-x-face t
      gnus-treat-display-face t
      gnus-treat-display-smileys nil
      gnus-treat-x-pgp-sig 'head)

(setq gnus-visible-headers 
      (mapconcat 'regexp-quote
                 '("From:" "Newsgroups:" "Subject:" "Date:" 
                   "Organization:" "To:" "Cc:" "Followup-To" "Gnus-Warnings:"
                   "X-Sent:" "X-URL:" "User-Agent:" "X-Newsreader:"
                   "X-Mailer:" "Reply-To:" "X-Spam:" "X-Spam-Status:" "X-Now-Playing"
                   "X-Attachments" "X-Diagnostic")
                 "\\|"))

(setq gnus-group-sort-groups
      '(gnus-sort-by-rank gnus-sort-by-alphabet))

(setq gnus-group-line-format "%P  %(%~(pad-right 50)G%) unread: %-4N\n"
      gnus-topic-line-format "%i[ %0{%(%n (new: %a)%)%} ]\n")

;;; scoring
;;; #######

(setq gnus-use-scoring t
      gnus-save-score t
      gnus-summary-expunge-below -20
      gnus-score-followup-thread 10
      gnus-score-followup-article 10
      bbdb/gnus-score-default 5
      gnus-score-thread-simplify t
      gnus-use-adaptive-scoring nil)

(setq gnus-score-find-score-files-function
      '(gnus-score-find-bnews bbdb/gnus-score))

;;; archive (WSI)
;;; #############

(when (run-on-wsi-machinep)

  (setq gnus-message-archive-method
        '(nnfolder "archive"
          (nnfolder-directory "~/Mail/archive")
          (nnfolder-active-file "~/Mail/archive/active")
          (nnfolder-get-new-mail nil)
          (nnfolder-inhibit-expiry t)))

  (setq gnus-message-archive-group 
        '((if (string= gnus-newsgroup-name "nnml:mailbox")
              gnus-newsgroup-name
            "nnfolder+archive:mail.archive.2005")))

  (setq nnmail-expiry-target 
        (function
         (lambda (group-name)
           (if (string= group-name "nnml:mailbox")
               "nnfolder+archive:mail.archive.2005"
             'delete)))))

;;; BBDB
;;; ####

(bbdb-initialize 'gnus 'message)
(setq bbdb/gnus-summary-prefer-real-names t
      bbdb/gnus-summary-prefer-bbdb-data t
      bbdb-completion-type 'primary-or-name
      bbdb-use-pop-up nil
      bbdb/pgp-default-action nil)

(setq bbdb-auto-notes-alist 
      (quote (("Subject" (".*" last-subj 0 t))
              ("Organisation" (".*" organisation 0 t)) 
              ("Organization" (".*" organisation 0 t))
              ("X-Newsreader" (".*" mailer 0 t)) 
              ("X-Mailer" (".*" mailer 0 t))
              ("User-Agent" (".*" mailer 0 t))
              ("X-URL" (".*" url 0 t)))))

;;; setup bcc
;;; #########

(when (run-on-wsi-machinep)

  (add-hook 'message-header-setup-hook
            (function (lambda ()
                        (insert "X-EJK: wsi\n")
                        (insert "Bcc: eric@knauel.org\n")))))

;;; hooks
;;; #####

(add-hook 'gnus-summary-mode-hook 'mc-install-read-mode)
(add-hook 'message-mode-hook 'mc-install-write-mode)
(add-hook 'news-reply-mode-hook 'mc-install-write-mode)
(add-hook 'gnus-startup-hook 'bbdb-insinuate-gnus)
(add-hook 'message-setup-hook 'bbdb-define-all-aliases)
(add-hook 'bbdb-notice-hook 'bbdb-auto-notes-hook)
(add-hook 'message-header-setup-hook 'message-sort-headers)
(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)
(remove-hook 'gnus-mark-article-hook 'gnus-summary-mark-read-and-unread-as-read)
(add-hook 'gnus-mark-article-hook 'gnus-summary-mark-unread-as-read)

(gnus-start-date-timer)

;;; some hacks
;;; ##########

(defun ejk-view-article-as-charset ()
  (interactive)
  (message "View as iso-8859-(1), utf-(8)")
  (let ((answer (read-char)))
    (cond
     ((eq answer ?1)
      (gnus-summary-show-article-from-menu-as-charset-iso-8859-1))
     ((eq answer ?8)
      (gnus-summary-show-article-from-menu-as-charset-utf-8)))))

(define-key gnus-summary-mode-map "v" 'ejk-view-article-as-charset)

(defvar jochen-translate-map
  `((,(string 34 97)     . "ä")    ; umlaut a
    (,(string 34 111)    . "ö")    ; umlaut o
    (,(string 34 117)    . "ü")    ; umlaut u
    (,(string 34 65)     . "Ä")    ; umlaut A
    (,(string 34 79)     . "Ö")    ; umlaut O
    (,(string 34 85)     . "Ü")    ; umlaut U
    (,(string 34 115)    . "ß")    ; umlaut ss
    (,(string 97 51)    . "")
    (,(string 101 51)   . "")
    (,(string 105 51)   . "")
    (,(string 111 51)   . "")
    (,(string 117 51)   . ""))
  nil)
 
(defvar jochen-search-for
  (mapconcat (lambda (entry) 
               (car entry)) 
             jochen-translate-map "\\|"))
   
(defun cc-decode-jochen()
  (interactive)
  (let ((case-fold-search nil))
    (while (re-search-forward jochen-search-for (point-max) t)
      (replace-match
       (cdr (assoc (match-string 0) jochen-translate-map)) t t))))
 
(defun gnus-article-deuglify-jochen ()
  "Map LaTeX-like quotation of German Umlauts (i.e. \"a for ä) to corresponding iso-8859-1 characters"
  (interactive)
  (save-excursion
    (set-buffer gnus-article-buffer)
    (let ((buffer-read-only nil)
          (inhibit-point-motion-hooks nil))
      (cc-decode-jochen))))
 
(define-key gnus-summary-wash-map "j" 'gnus-article-deuglify-jochen)

(defun ek-message-insert-citation-line ()
  (when message-reply-headers
    (insert 
     "On " 
     (format-time-string 
      "%a %d %b %Y %R"
      (mail-header-parse-date (mail-header-date message-reply-headers)))
     ", " (mail-header-from message-reply-headers) 
     " writes:\n\n")))

(setq message-citation-line-function 'ek-message-insert-citation-line)

;; Local Variables:
;; no-byte-compile: t
;; End: