;;; ycp-gnus.el -- My config for email etc -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My config for email etc. Covers gnus, bbdb, message mode etc.

;;; Code:


(my-setq-from-local user-mail-address user-full-name)
(setq auth-sources '("~/.authinfo.gpg"))

;;; `mm-encode'
(my-package mm-encode
  (setq mm-encrypt-option nil   ; use 'guided if you need more control
        mm-sign-option nil))  ; same

(my-package mm-decode
  (setq mm-discouraged-alternatives '("text/html" "text/richtext")))

;;; `mml-sec'
(my-package mml-sec
  (setq mml-secure-openpgp-encrypt-to-self t
        mml-secure-openpgp-sign-with-sender t
        mml-secure-smime-encrypt-to-self t
        mml-secure-smime-sign-with-sender t))

;;; `message'
(my-package message
  (setq mail-user-agent 'message-user-agent
        message-elide-ellipsis "\n> [... %l lines elided]\n"
        compose-mail-user-agent-warnings nil
        message-mail-user-agent t         ; use `mail-user-agent'
        message-citation-line-function #'message-insert-formatted-citation-line
        message-ignored-cited-headers ""  ; default is "." for all headers
        message-confirm-send nil
        message-kill-buffer-on-exit t
        message-wide-reply-confirm-recipients nil
        message-citation-line-format
	      "On %a %Y-%m-%d %H:%M:%S %z, %N wrote:\n")
  (add-hook 'message-setup-hook #'message-sort-headers)
  (require 'my-gnus)
  (my-keybind message-mode-map
    "M-n" #'my-message-after-next-cited
    "M-p" #'my-message-before-previous-cited
    "C-c M-e" #'my-message-elide-remaining)
  )

(my-package smtpmail
  (my-setq-from-local smtpmail-default-smtp-server
                      smtpmail-smtp-server smtpmail-stream-type)
  (setq smtpmail-smtp-service 587
        smtpmail-queue-mail nil))

;;; `sendmail' (mail transfer agent)
(setq send-mail-function 'smtpmail-send-it)

;;; gnus
(my-package gnus
  (setq gnus-select-method '(nnnil ""))
  (setq gnus-group-line-format "%M%S%p%P%5y:%B%(%G%)
")
  (my-setq-from-local gnus-secondary-select-methods)
  (setq gnus-agent t)
  (setq mail-user-agent 'gnus-user-agent)
  (dolist (mode '(gnus-group-mode-hook
                  gnus-summary-mode-hook
                  gnus-browse-mode-hook))
    (add-hook mode #'hl-line-mode))
  (require 'my-gnus)
  (require 'my-buffer)
  (my-setq-from-local my-gnus-inbox-group
                      my-gnus-group-alist)
  (my-keybind global-map
    "C-c n i" #'my-gnus-open-inbox
    "C-c n n" #'my-gnus-start
    "C-c n u" #'gnus-group-get-new-news)
  (my-server-timer my-gnus-new-news-timer nil 300
                   'my-gnus-group-get-new-news-quietly)
  ;; https://superuser.com/questions/519685/gnus-get-rid-of-mail-and-news-folders
  ;; this also fixes issues with presumably nonexisting
  ;; nndraft-directory causing
  ;; (wrong-type-argument stringp nndraft-directory)
  ;; which may require a restart of gnus to fix
  (setq message-directory "~/.emacs.d/mail/")
  (setq gnus-directory "~/.emacs.d/news/")
  (setq nnfolder-directory "~/.emacs.d/mail/archive")
  (setq nndraft-directory "~/.emacs.d/mail/drafts/")
  )

(my-configure
  (:delay 10)
  (org-link-set-parameters "gnus" :follow #'my-org-open-gnus-link))

(my-package gnus-dired
  (add-hook 'dired-mode-hook #'turn-on-gnus-dired-mode))

(my-package gnus-msg
  (setq gnus-gcc-mark-as-read t)
  (setq gnus-message-replysign t)
  (my-setq-from-local gnus-posting-styles)
  (my-override mm-display-external)
  (add-hook 'gnus-message-setup-hook #'message-remove-blank-cited-lines)
  (add-hook 'gnus-message-setup-hook #'my-message-remove-trailing-cited-lines)
  )

;; checking sources
(my-package gnus-start
  (setq gnus-check-new-newsgroups 'ask-server)
  (setq gnus-read-active-file 'some)
  (setq gnus-use-dribble-file t)
  (setq gnus-always-read-dribble-file t))

(my-package gnus-search
  (setq gnus-search-use-parsed-queries t))

(my-package gnus-win
  (setq gnus-use-full-window nil))

(my-package gnus-topic
  (require 'my-gnus)
  (my-keybind gnus-topic-mode-map
    "u" #'my-gnus-topic-up
    "<RET>" #'my-gnus-topic-select-group)
  )

(my-package gnus-group
  (require 'my-gnus)
  (my-keybind gnus-group-mode-map
    "n" #'next-line
    "p" #'previous-line
    "m" #'my-gnus-group-compose
    "M-&" nil
    "<RET>" #'my-gnus-topic-select-group
    "q" #'bury-buffer)
  (add-hook 'gnus-group-mode-hook 'gnus-topic-mode)
  )

(my-package gnus-sum
  (require 'my-gnus)
  (setq gnus-ignored-from-addresses user-full-name)
  (my-keybind gnus-summary-mode-map
    "n" #'my-gnus-summary-next-article-like-mu4e
    "p" #'my-gnus-summary-prev-article-like-mu4e
    "q" #'my-gnus-summary-exit-like-mu4e
    "g" #'gnus-group-get-new-news-this-group
    "M-u" nil
    "M-&" nil
    "M" #'my-gnus-move-article-like-mu4e
    "a" #'my-gnus-archive-article-like-mu4e
    "c" #'org-capture
    "t" #'my-org-capture-todo
    "m" #'gnus-summary-post-news
    "k" #'my-gnus-trash-article-like-mu4e
    "r" #'gnus-article-reply-with-original
    "R" #'gnus-article-wide-reply-with-original
    "." #'gnus-summary-show-raw-article)
  (setq gnus-sum-thread-tree-root ""
        gnus-sum-thread-tree-single-leaf "└>"
        gnus-sum-thread-tree-leaf-with-other "├>"
        gnus-sum-thread-tree-indent " ")
  (setq gnus-summary-line-format "%*%U%R%z %d %(%-20,20f%) %B%S
")
  (setq gnus-thread-sort-functions
        '(gnus-thread-sort-by-most-recent-date))
  (setq gnus-summary-next-group-on-exit nil)
  )

(my-package gnus-art
  (my-keybind gnus-article-mode-map
    "w" #'my-copy-url-at-point))

(my-package nnrss
  (:delay 60)
  (setq nnrss-use-local t))

(my-package gnus-art
  (setq gnus-inhibit-images t)
  (setq gnus-treat-display-smileys nil)
  (setq gnus-article-x-face-too-ugly ".*")
  ;; shows a radio button in a multipart article to choose which part
  ;; to display
  (setq gnus-buttonized-mime-types '("multipart/alternative"))
  (setq gnus-visible-headers
        "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^In-Reply-To"))
;;; gnus-desktop-notify
(my-package gnus-desktop-notify
  (:delay 30)
  (gnus-desktop-notify-mode)
  (setq gnus-desktop-notify-groups 'gnus-desktop-notify-explicit)
  (setq gnus-desktop-notify-uncollapsed-levels 2)
  )

(my-package gnus-demon
  (gnus-demon-add-scanmail))

;;; bbdb
(my-package bbdb
  (:delay 5)
  (bbdb-initialize 'gnus 'message)
  (bbdb-mua-auto-update-init 'gnus 'message)
  (setq bbdb-dedicated-window t)
  (setq bbdb-message-all-addresses t)
  (setq bbdb-mua-pop-up-window-size .15)
  (setq bbdb-mua-pop-up nil)
  (setq bbdb-new-mails-primary nil)
  (setq bbdb-ignore-redundant-mails t)
  (setq bbdb-mail-user-agent 'gnus-user-agent)
  (setq bbdb-update-records-p 'create)
  (setq bbdb-message-clean-mail-function 'my-bbdb-clean-mail)
  (my-setq-from-local bbdb-message-ignore-mail-re)
  (setq bbdb-add-name nil)
  (require 'my-bbdb)
  (my-keybind bbdb-mode-map "C-c C-c" #'my-bbdb-done)
  (setq bbdb-phone-style nil
        bbdb-default-country nil)
  (my-override bbdb-read-record)
  (my-override bbdb-create)
  (my-keybind global-map
    "C-c b a" #'my-bbdb-all
    "C-c b b" #'bbdb
    "C-c b c" #'bbdb-create)
  )

(my-package bbdb-anniv (:delay 60))

;; bbdb-vcard
(my-package bbdb-vcard
  (:delay 60)
  (bbdb-vcard-default-keybindings)
  (my-setq-from-local bbdb-vcard-default-dir))

(my-package my-url-rewrite
  (:delay 60))

(my-package traclicker
  (:delay 60)
  (my-setq-from-local tracli-senders tracli-maildirs)
  (my-server-timer my-tracli-timer nil 3600 #'tracli-scan)
  (setq tracli-url-rewriter 'my-url-strip-drw-tracking-params))

(provide 'ycp-gnus)