;;; thanos-mu4e.el --- mu4e configuration            -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Thanos Apollo

;; Author: Thanos Apollo <public@thanosapollo.com>
;; Keywords: extensions

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

;; This program 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 General Public License for more details.

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

;;; Commentary:
;; ╭━━━━┳╮╱╱╱╱╱╱╱╱╱╱╱╱╱╱╭━━━╮╱╱╱╱╱╭╮╭╮╱╱╱╱╱╱╱╱╭━━━╮
;; ┃╭╮╭╮┃┃╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃╭━╮┃╱╱╱╱╱┃┃┃┃╱╱╱╱╱╱╱╱┃╭━━╯
;; ╰╯┃┃╰┫╰━┳━━┳━╮╭━━┳━━╮┃┃╱┃┣━━┳━━┫┃┃┃╭━━╮╱╱╱╱┃╰━━┳╮╭┳━━┳━━┳━━╮
;; ╱╱┃┃╱┃╭╮┃╭╮┃╭╮┫╭╮┃━━┫┃╰━╯┃╭╮┃╭╮┃┃┃┃┃╭╮┃╭━━╮┃╭━━┫╰╯┃╭╮┃╭━┫━━┫
;; ╱╱┃┃╱┃┃┃┃╭╮┃┃┃┃╰╯┣━━┃┃╭━╮┃╰╯┃╰╯┃╰┫╰┫╰╯┃╰━━╯┃╰━━┫┃┃┃╭╮┃╰━╋━━┃
;; ╱╱╰╯╱╰╯╰┻╯╰┻╯╰┻━━┻━━╯╰╯╱╰┫╭━┻━━┻━┻━┻━━╯╱╱╱╱╰━━━┻┻┻┻╯╰┻━━┻━━╯
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱┃┃
;; ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╰╯
;;; Code:

(require 'smtpmail)


(when is-zeus
  (add-to-list 'load-path "/usr/share/emacs/site-lisp/mu4e/"))

(require 'mu4e)

(when is-zeus (setf mu4e-update-interval (* 10 60)))

(setf mu4e-get-mail-command "mbsync -a")

(defun set-mu4e-context (context-name full-name mail-address signature)
  "Return a mu4e context named CONTEXT-NAME with :match-func matching
    folder name CONTEXT-NAME in Maildir. The context's `user-mail-address',
    `user-full-name' and `mu4e-compose-signature'`smtpmail-smpt-server' is set to MAIL-ADDRESS
    FULL-NAME SIGNATURE and SERVER respectively.
    Special folders are set to context specific folders."
  (let ((dir-name (concat "/" context-name)))
    (make-mu4e-context
     :name context-name
     ;; we match based on the maildir of the message
     :match-func
     `(lambda (msg)
	(when msg
	  (string-match-p
	   ,(concat "^" dir-name)
	   (mu4e-message-field msg :maildir))))
     :vars
     `((user-mail-address    . ,mail-address)
       (user-full-name       . ,full-name)
       (mu4e-sent-folder     . ,(concat dir-name "/Sent"))
       (mu4e-drafts-folder   . ,(concat dir-name "/Drafts"))
       (mu4e-trash-folder    . ,(concat dir-name "/Trash"))
       (mu4e-trash-folder    . ,(concat dir-name "/Starred"))
       (mu4e-refile-folder   . ,(concat dir-name "/Archive"))
       (mu4e-compose-signature . ,signature)))))
;;Fixing duplicate UID errors when using mbsync and mu4e
(setf mu4e-change-filenames-when-moving t)

(setf mu4e-maildir-shortcuts
      '(("/Public/Inbox" . ?I)
	("/Inbox" . ?i)
	("/Sent" . ?s)
	("/Emacs/dev" . ?e)))

(setf mu4e-contexts
      (list
       (make-mu4e-context
	:name "Fastmail"
	:match-func
	(lambda (msg)
	  (when msg
	    (string-prefix-p "/" (mu4e-message-field msg :maildir))))
	:vars '((user-mail-address . "thanosapollo@fastmail.com")
		(user-full-name    . "Thanos Apollo")
		(mu4e-drafts-folder  . "/Drafts")
		(mu4e-sent-folder  . "/Sent")
		(mu4e-refile-folder  . "/Archive")
		(mu4e-trash-folder  . "/Trash")))
       (make-mu4e-context
	:name "Public"
	:match-func
	(lambda (msg)
	  (when msg
	    (string-prefix-p "/" (mu4e-message-field msg :maildir))))
	:vars '((user-mail-address . "public@thanosapollo.com")
		(user-full-name    . "Thanos Apollo")
		(mu4e-drafts-folder  . "/Drafts")
		(mu4e-sent-folder  . "/Sent")
		(mu4e-refile-folder  . "/Archive")
		(mu4e-trash-folder  . "/Trash")))))

(setf message-send-mail-function 'smtpmail-send-it
      smtpmail-smtp-server "smtp.fastmail.com"
      smtpmail-smtp-service 465
      smtpmail-stream-type 'ssl
      mu4e-compose-signature "Thanos Apollo\nhttps://thanosapollo.com"
      mu4e-compose-context-policy 'ask
      mu4e-compose-format-flowed t)

(setf mu4e-view-actions
      (delete-dups
       (append
	'(("gapply git patches" . mu4e-action-git-apply-patch)
	  ("mgit am patch" . mu4e-action-git-apply-mbox)
	  ("bb4 am patch" . mu4e-action-git-apply-b4)
	  ("ssetup reword list with b4" . mu4e-action-setup-reword-b4)
	  ("crun checkpatch script" . my-mu4e-action-run-check-patch)
	  ("MCheck if merged" . my-mu4e-action-check-if-merged)))))

(setf mu4e-view-actions
      (delete-dups
       (append
	'(("gapply git patches" . mu4e-action-git-apply-patch)
	  ("mgit am patch" . mu4e-action-git-apply-mbox)
	  ("bb4 am patch" . mu4e-action-git-apply-b4)
	  ("ssetup reword list with b4" . mu4e-action-setup-reword-b4)
	  ("crun checkpatch script" . my-mu4e-action-run-check-patch)
	  ("MCheck if merged" . my-mu4e-action-check-if-merged)))))

(require 'mu4e-alert)
(mu4e-alert-enable-mode-line-display)

(define-key thanos/applications-map (kbd "m") 'mu4e)

(provide 'thanos-mu4e)
;;; thanos-mu4e.el ends here