2012-04-01 11:29:24 +02:00
|
|
|
|
;; mu4e-send.el -- part of mu4e, the mu mail user agent for emacs
|
2011-09-18 13:39:36 +02:00
|
|
|
|
;;
|
2012-03-31 13:22:06 +02:00
|
|
|
|
;; Copyright (C) 2011-2012 Dirk-Jan C. Binnema
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
;; Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
|
;; Maintainer: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
;;
|
|
|
|
|
;; GNU Emacs 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.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; In this file, various functions to compose/send messages, piggybacking on
|
2011-10-10 07:38:14 +02:00
|
|
|
|
;; gnus' message mode
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
;;; Code:
|
2012-04-09 15:34:52 +02:00
|
|
|
|
;; we use some stuff from gnus..
|
|
|
|
|
(require 'cl)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2012-04-09 15:34:52 +02:00
|
|
|
|
(require 'mu4e-utils)
|
|
|
|
|
(require 'mu4e-vars)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
(require 'message)
|
|
|
|
|
(require 'mail-parse)
|
2011-12-29 00:25:52 +01:00
|
|
|
|
(require 'smtpmail)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
;; internal variables / constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defconst mu4e-send-draft-name "*mu4e-draft*"
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Name for draft messages.")
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-user-agent ()
|
2012-01-07 13:27:00 +01:00
|
|
|
|
"Return the User-Agent string for mu4e. This is either the value
|
2012-04-01 11:29:24 +02:00
|
|
|
|
of `mu4e-user-agent', or, if not set, a string based on the versions
|
|
|
|
|
of mu4e and emacs."
|
2011-12-13 08:07:38 +01:00
|
|
|
|
(or mu4e-user-agent
|
2012-01-07 13:27:00 +01:00
|
|
|
|
(format "mu4e %s; emacs %s" mu4e-mu-version emacs-version)))
|
2011-12-20 20:40:16 +01:00
|
|
|
|
|
|
|
|
|
(defun mu4e-send-cite-original (msg)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Cite the body text of MSG, with a \"On %s, %s wrote:\"
|
|
|
|
|
line (with the %s's replaced with the date of MSG and the name
|
|
|
|
|
or e-mail address of its sender (or 'someone' if nothing
|
|
|
|
|
else)), followed of the quoted body of MSG, constructed by by
|
2011-12-20 20:40:16 +01:00
|
|
|
|
prepending `mu4e-send-citation-prefix' to each line. If there is
|
2011-09-18 13:39:36 +02:00
|
|
|
|
no body in MSG, return nil."
|
|
|
|
|
(let* ((from (plist-get msg :from))
|
2011-12-19 22:54:08 +01:00
|
|
|
|
(body (mu4e-body-text msg)))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
(when body
|
|
|
|
|
(concat
|
|
|
|
|
(format "On %s, %s wrote:"
|
|
|
|
|
(format-time-string "%c" (plist-get msg :date))
|
|
|
|
|
(if (and from (car from)) ;; a list ((<name> . <email>))
|
|
|
|
|
(or (caar from) (cdar from) "someone")
|
|
|
|
|
"someone"))
|
|
|
|
|
"\n\n"
|
2012-03-20 23:03:20 +01:00
|
|
|
|
(replace-regexp-in-string "^"
|
|
|
|
|
mu4e-send-citation-prefix body)))))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-recipients-remove (lst email-to-remove)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Remove the recipient with EMAIL from the recipient list (of form
|
|
|
|
|
'( (\"A\" . \"a@example.com\") (\"B\" . \"B@example.com\"))."
|
|
|
|
|
(remove-if
|
|
|
|
|
(lambda (name-email)
|
|
|
|
|
(let ((email (cdr name-email)))
|
|
|
|
|
(when email (string= email-to-remove (downcase email))))) lst))
|
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-recipients-to-string (lst)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Convert a recipient list (of form '( (\"A\"
|
|
|
|
|
. \"a@example.com\") (\"B\" . \"B@example.com\") (nil
|
|
|
|
|
. \"c@example.com\")) into a string of form \"A <@aexample.com>, B
|
|
|
|
|
<b@example.com>, c@example.com\."
|
2011-11-09 07:55:39 +01:00
|
|
|
|
(when lst
|
|
|
|
|
(mapconcat
|
|
|
|
|
(lambda (recip)
|
|
|
|
|
(let ((name (car recip)) (email (cdr recip)))
|
|
|
|
|
(if name
|
|
|
|
|
(format "%s <%s>" name email)
|
|
|
|
|
(format "%s" email)))) lst ", ")))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-header (hdr val)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Return a header line of the form HDR: VAL\n. If VAL is nil,
|
|
|
|
|
return nil."
|
|
|
|
|
(when val (format "%s: %s\n" hdr val)))
|
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-references-create (msg)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Construct the value of the References: header based on MSG as a
|
|
|
|
|
comma-separated string. Normally, this the concatenation of the
|
|
|
|
|
existing References (which may be empty) and the message-id. If the
|
|
|
|
|
message-id is empty, returns the old References. If both are empty,
|
|
|
|
|
return nil."
|
|
|
|
|
(let ((refs (plist-get msg :references))
|
2011-10-02 20:35:03 +02:00
|
|
|
|
(old-msgid (plist-get msg :message-id)))
|
|
|
|
|
(when old-msgid
|
|
|
|
|
(setq refs (append refs (list old-msgid)))
|
|
|
|
|
(mapconcat
|
|
|
|
|
(lambda (msgid) (format "<%s>" msgid))
|
|
|
|
|
refs ","))))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-to-create (msg)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Construct the To: header for a reply-message based on some
|
2011-11-09 07:55:39 +01:00
|
|
|
|
message MSG. This takes the Reply-To address of MSG if it exist, or
|
2011-11-09 07:35:24 +01:00
|
|
|
|
the From:-address otherwise. The result is either nil or a string
|
2011-11-09 07:55:39 +01:00
|
|
|
|
which can be used for the To:-field. Note, when it's present,
|
|
|
|
|
Reply-To contains a string of one or more addresses,
|
|
|
|
|
comma-separated."
|
|
|
|
|
(or
|
|
|
|
|
(plist-get msg :reply-to)
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-recipients-to-string (plist-get msg :from))))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-cc-create (msg reply-all)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Get the list of Cc-addresses for the reply to MSG. If REPLY-ALL
|
2011-11-09 07:35:24 +01:00
|
|
|
|
is nil this is simply empty, otherwise it is the old CC-list
|
|
|
|
|
together with the old TO-list, minus `user-mail-address'. The
|
|
|
|
|
result of this function is either nil or a string to be used for
|
|
|
|
|
the Cc: field."
|
2012-02-19 10:05:29 +01:00
|
|
|
|
(when reply-all
|
|
|
|
|
(let ((cc-lst
|
|
|
|
|
(mu4e-send-recipients-remove ;; remove myself from cc
|
|
|
|
|
(append (plist-get msg :cc) (plist-get msg :to))
|
|
|
|
|
user-mail-address)))
|
|
|
|
|
(mu4e-send-recipients-to-string cc-lst))))
|
2011-11-09 07:35:24 +01:00
|
|
|
|
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-from-create ()
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Construct a value for the From:-field of the reply to MSG,
|
|
|
|
|
based on `user-full-name' and `user-mail-address'; if the latter is
|
|
|
|
|
nil, function returns nil."
|
|
|
|
|
(when user-mail-address
|
|
|
|
|
(if user-full-name
|
|
|
|
|
(format "%s <%s>" user-full-name user-mail-address)
|
|
|
|
|
(format "%s" user-mail-address))))
|
|
|
|
|
|
2012-03-31 16:26:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun mu4e-insert-mail-header-separator ()
|
|
|
|
|
"Insert `mail-header-separator' in the first empty line of the
|
|
|
|
|
message. message-mode needs this line to know where the headers end
|
|
|
|
|
and the body starts. Note, in `mu4e-edit-mode, we use
|
|
|
|
|
`before-save-hook' and `after-save-hook' to ensure that this
|
|
|
|
|
separator is never written to file. Also see
|
|
|
|
|
`mu4e-remove-mail-header-separator'."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
;; search for the first empty line
|
|
|
|
|
(if (search-forward-regexp (concat "^$"))
|
|
|
|
|
(replace-match
|
|
|
|
|
(propertize mail-header-separator 'read-only t 'intangible t))
|
|
|
|
|
;; no empty line? then append one
|
2012-04-09 15:34:52 +02:00
|
|
|
|
(progn
|
2012-03-31 16:26:36 +02:00
|
|
|
|
(goto-char (point-max))
|
|
|
|
|
(insert (concat "\n" mail-header-separator "\n"))))))
|
|
|
|
|
|
|
|
|
|
(defun mu4e-remove-mail-header-separator ()
|
|
|
|
|
"Remove `mail-header-separator; we do this before saving a
|
|
|
|
|
file (and restore it afterwardds), to ensure that the separator
|
|
|
|
|
never hits the disk. Also see `mu4e-insert-mail-header-separator."
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
;; remove the --text follows this line-- separator
|
|
|
|
|
(when (search-forward-regexp (concat "^" mail-header-separator))
|
|
|
|
|
(replace-match ""))))
|
|
|
|
|
|
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-create-reply (msg)
|
2011-10-02 20:35:03 +02:00
|
|
|
|
"Create a draft message as a reply to MSG.
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
A reply message has fields:
|
|
|
|
|
From: - see `mu-msg-from-create'
|
2011-12-20 20:40:16 +01:00
|
|
|
|
To: - see `mu4e-send-to-create'
|
|
|
|
|
Cc: - see `mu4e-send-cc-create'
|
|
|
|
|
Subject: - `mu4e-send-reply-prefix' + subject of MSG
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
then, the following fields, normally hidden from user:
|
|
|
|
|
Reply-To: - if `mail-reply-to' has been set
|
2011-12-20 20:40:16 +01:00
|
|
|
|
References: - see `mu4e-send-references-create'
|
2011-09-18 13:39:36 +02:00
|
|
|
|
In-Reply-To: - message-id of MSG
|
2011-12-20 20:40:16 +01:00
|
|
|
|
User-Agent - see `mu4e-send-user-agent'
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
And finally, the cited body of MSG, as per `mu4e-send-cite-original'."
|
2011-10-02 20:35:03 +02:00
|
|
|
|
(let* ((recipnum (+ (length (plist-get msg :to))
|
|
|
|
|
(length (plist-get msg :cc))))
|
|
|
|
|
(reply-all (when (> recipnum 1)
|
|
|
|
|
(yes-or-no-p
|
|
|
|
|
(format "Reply to all ~%d recipients? "
|
2011-12-17 10:31:36 +01:00
|
|
|
|
(+ recipnum)))))
|
|
|
|
|
(old-msgid (plist-get msg :message-id))
|
2012-02-08 21:17:00 +01:00
|
|
|
|
(subject (or (plist-get msg :subject) "")))
|
2011-10-02 20:35:03 +02:00
|
|
|
|
(concat
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "From" (or (mu4e-send-from-create) ""))
|
2011-10-02 20:35:03 +02:00
|
|
|
|
(when (boundp 'mail-reply-to)
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "Reply-To" mail-reply-to))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "To" (or (mu4e-send-to-create msg) ""))
|
|
|
|
|
(mu4e-send-header "Cc" (mu4e-send-cc-create msg reply-all))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "User-agent" (mu4e-send-user-agent))
|
|
|
|
|
(mu4e-send-header "References" (mu4e-send-references-create msg))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-17 10:31:36 +01:00
|
|
|
|
(when old-msgid
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "In-reply-to" (format "<%s>" old-msgid)))
|
|
|
|
|
(mu4e-send-header "Subject"
|
2012-02-08 21:17:00 +01:00
|
|
|
|
(concat
|
|
|
|
|
;; if there's no Re: yet, prepend it
|
|
|
|
|
(if (string-match (concat "^" mu4e-send-reply-prefix) subject)
|
* make it configurable what to do with sent messages (part 2), and document it:
By default, @t{mu4e} puts a copy of any messages you sent in the folder you
set for @code{mu4e-sent-folder}. In some case, this may not be what you want -
for example, when using GMail+@abbr{IMAP} (but @emph{not} with
GMail+@abbr{POP3}), this interferes with GMail's handling of the sent messages
folder, and you may end up with duplicate messages. For this, since @t{mu4e}
0.9.8.3, there is the variable @code{mu4e-sent-messages-behavior}, which takes
a symbol. The default is @code{'sent} which, as stated causes the message to
be copied to your sent-messages folder. Other possible values are
@code{'trash} (so the sent message is copied to the trash-folder
(@code{mu4e-trash-folder}), and @code{'delete} to simply discard the message
altogether.
2012-03-13 19:13:30 +01:00
|
|
|
|
"" mu4e-send-reply-prefix)
|
2012-02-08 21:17:00 +01:00
|
|
|
|
subject))
|
2011-10-10 07:38:14 +02:00
|
|
|
|
"\n\n"
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-cite-original msg))))
|
2011-09-30 07:37:47 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-create-forward (msg)
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Create a draft forward message for MSG.
|
|
|
|
|
|
|
|
|
|
A forward message has fields:
|
2011-12-20 20:40:16 +01:00
|
|
|
|
From: - see `mu4e-send-from-create'
|
2011-09-18 13:39:36 +02:00
|
|
|
|
To: - empty
|
2011-12-20 20:40:16 +01:00
|
|
|
|
Subject: - `mu4e-send-forward-prefix' + subject of MSG
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
then, the following fields, normally hidden from user:
|
|
|
|
|
Reply-To: - if `mail-reply-to' has been set
|
2011-12-20 20:40:16 +01:00
|
|
|
|
References: - see `mu4e-send-references-create'
|
|
|
|
|
User-Agent - see `mu4e-send-user-agent'
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
And finally, the cited body of MSG, as per `mu4e-send-cite-original'."
|
2012-02-09 20:20:36 +01:00
|
|
|
|
(let ((subject (or (plist-get msg :subject) "")))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
(concat
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "From" (or (mu4e-send-from-create) ""))
|
2012-02-09 20:20:36 +01:00
|
|
|
|
(when (boundp 'mail-reply-to)
|
|
|
|
|
(mu4e-send-header "Reply-To" mail-reply-to))
|
* make it configurable what to do with sent messages (part 2), and document it:
By default, @t{mu4e} puts a copy of any messages you sent in the folder you
set for @code{mu4e-sent-folder}. In some case, this may not be what you want -
for example, when using GMail+@abbr{IMAP} (but @emph{not} with
GMail+@abbr{POP3}), this interferes with GMail's handling of the sent messages
folder, and you may end up with duplicate messages. For this, since @t{mu4e}
0.9.8.3, there is the variable @code{mu4e-sent-messages-behavior}, which takes
a symbol. The default is @code{'sent} which, as stated causes the message to
be copied to your sent-messages folder. Other possible values are
@code{'trash} (so the sent message is copied to the trash-folder
(@code{mu4e-trash-folder}), and @code{'delete} to simply discard the message
altogether.
2012-03-13 19:13:30 +01:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "To" "")
|
|
|
|
|
(mu4e-send-header "User-agent" (mu4e-send-user-agent))
|
|
|
|
|
(mu4e-send-header "References" (mu4e-send-references-create msg))
|
2012-02-09 20:20:36 +01:00
|
|
|
|
(mu4e-send-header "Subject"
|
|
|
|
|
(concat
|
|
|
|
|
;; if there's no Re: yet, prepend it
|
|
|
|
|
(if (string-match (concat "^" mu4e-send-forward-prefix) subject)
|
* make it configurable what to do with sent messages (part 2), and document it:
By default, @t{mu4e} puts a copy of any messages you sent in the folder you
set for @code{mu4e-sent-folder}. In some case, this may not be what you want -
for example, when using GMail+@abbr{IMAP} (but @emph{not} with
GMail+@abbr{POP3}), this interferes with GMail's handling of the sent messages
folder, and you may end up with duplicate messages. For this, since @t{mu4e}
0.9.8.3, there is the variable @code{mu4e-sent-messages-behavior}, which takes
a symbol. The default is @code{'sent} which, as stated causes the message to
be copied to your sent-messages folder. Other possible values are
@code{'trash} (so the sent message is copied to the trash-folder
(@code{mu4e-trash-folder}), and @code{'delete} to simply discard the message
altogether.
2012-03-13 19:13:30 +01:00
|
|
|
|
"" mu4e-send-forward-prefix)
|
2012-02-09 20:20:36 +01:00
|
|
|
|
subject))
|
2011-10-10 07:38:14 +02:00
|
|
|
|
"\n\n"
|
2012-02-09 20:20:36 +01:00
|
|
|
|
(mu4e-send-cite-original msg))))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-create-new ()
|
2011-09-18 13:39:36 +02:00
|
|
|
|
"Create a new message.
|
|
|
|
|
|
|
|
|
|
A new draft message has fields:
|
|
|
|
|
From: - see `mu-msg-from-create'
|
|
|
|
|
To: - empty
|
|
|
|
|
Subject: - empty
|
|
|
|
|
|
|
|
|
|
then, the following fields, normally hidden from user:
|
|
|
|
|
Reply-To: - if `mail-reply-to' has been set
|
2012-03-31 16:26:36 +02:00
|
|
|
|
User-Agent - see `mu4e-send-user-agent'."
|
2011-09-18 13:39:36 +02:00
|
|
|
|
(concat
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "From" (or (mu4e-send-from-create) ""))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
(when (boundp 'mail-reply-to)
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(mu4e-send-header "Reply-To" mail-reply-to))
|
|
|
|
|
(mu4e-send-header "To" "")
|
|
|
|
|
(mu4e-send-header "User-agent" (mu4e-send-user-agent))
|
|
|
|
|
(mu4e-send-header "Subject" "")
|
2012-03-31 16:26:36 +02:00
|
|
|
|
"\n"))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(defun mu4e-send-open-draft (compose-type &optional msg)
|
2011-10-02 20:35:03 +02:00
|
|
|
|
"Open a draft file for a new message, creating it if it does not
|
|
|
|
|
already exist, and optionally fill it with STR. Function also adds
|
|
|
|
|
the new message to the database. When the draft message is added to
|
2011-12-13 08:07:38 +01:00
|
|
|
|
the database, `mu4e-path-docid-map' will be updated, so that we can
|
2011-11-05 09:26:24 +01:00
|
|
|
|
use the new docid. Returns the full path to the new message."
|
|
|
|
|
(let* ((hostname
|
2011-11-09 07:55:39 +01:00
|
|
|
|
(downcase
|
2011-11-05 09:26:24 +01:00
|
|
|
|
(save-match-data
|
|
|
|
|
(substring system-name
|
|
|
|
|
(string-match "^[^.]+" system-name) (match-end 0)))))
|
|
|
|
|
(draft
|
2011-12-13 08:07:38 +01:00
|
|
|
|
(concat mu4e-maildir mu4e-drafts-folder "/cur/"
|
2012-04-06 00:28:58 +02:00
|
|
|
|
;; 'D': rarely used, but hey, it's available
|
|
|
|
|
;; 'S': because we're looking at the draft as we speak
|
|
|
|
|
(format "%s-%x%x.%s:2,DS"
|
2011-11-05 09:26:24 +01:00
|
|
|
|
(format-time-string "%Y%m%d" (current-time))
|
|
|
|
|
(emacs-pid) (random t) hostname)))
|
|
|
|
|
(str (case compose-type
|
2011-12-20 20:40:16 +01:00
|
|
|
|
(reply (mu4e-send-create-reply msg))
|
|
|
|
|
(forward (mu4e-send-create-forward msg))
|
|
|
|
|
(new (mu4e-send-create-new))
|
2011-11-05 09:26:24 +01:00
|
|
|
|
(t (error "unsupported compose-type %S" compose-type)))))
|
2011-10-02 20:35:03 +02:00
|
|
|
|
(when str
|
2012-03-27 20:15:47 +02:00
|
|
|
|
(with-current-buffer (find-file-noselect draft)
|
|
|
|
|
(insert str)))
|
|
|
|
|
draft)) ;; return the draft buffer file
|
2011-10-02 20:35:03 +02:00
|
|
|
|
|
2012-03-28 00:47:52 +02:00
|
|
|
|
|
|
|
|
|
(define-derived-mode mu4e-edit-mode message-mode "mu4e:edit"
|
|
|
|
|
"Major mode for the mu4e main screen.
|
|
|
|
|
\\{message-mode-map}."
|
|
|
|
|
(let ((message-hidden-headers
|
|
|
|
|
`("^References:" "^Face:" "^X-Face:" "^X-Draft-From:"
|
|
|
|
|
"^User-agent:")))
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(use-local-map mu4e-edit-mode-map)
|
2012-03-30 08:15:52 +02:00
|
|
|
|
|
|
|
|
|
(message-hide-headers)
|
2012-03-31 16:26:36 +02:00
|
|
|
|
|
|
|
|
|
(make-local-variable 'before-save-hook)
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(make-local-variable 'after-save-hook)
|
2012-03-30 08:15:52 +02:00
|
|
|
|
|
2012-04-02 18:59:16 +02:00
|
|
|
|
(make-local-variable 'message-default-charset)
|
|
|
|
|
|
|
|
|
|
;; if the default charset is not set, use UTF-8
|
|
|
|
|
(unless message-default-charset
|
|
|
|
|
(setq message-default-charset 'utf-8))
|
|
|
|
|
|
|
|
|
|
|
2012-03-31 16:26:36 +02:00
|
|
|
|
;; hack-hack-hack... just before saving, we remove the
|
|
|
|
|
;; mail-header-separator; just after saving we restore it; thus, the
|
|
|
|
|
;; separator should never appear on disk
|
|
|
|
|
(add-hook 'before-save-hook 'mu4e-remove-mail-header-separator)
|
|
|
|
|
(add-hook 'after-save-hook
|
|
|
|
|
(lambda ()
|
2012-04-01 20:36:13 +02:00
|
|
|
|
(mu4e-set-buffer-name)
|
2012-03-31 16:26:36 +02:00
|
|
|
|
(mu4e-insert-mail-header-separator)
|
|
|
|
|
(set-buffer-modified-p nil)))
|
|
|
|
|
|
2012-03-28 17:58:43 +02:00
|
|
|
|
;; update the db when the file is saved...]
|
|
|
|
|
(add-hook 'after-save-hook
|
2012-03-31 16:26:36 +02:00
|
|
|
|
(lambda()
|
|
|
|
|
(mu4e-proc-add (buffer-file-name) mu4e-drafts-folder))))
|
2012-03-28 17:58:43 +02:00
|
|
|
|
|
|
|
|
|
;; notify the backend that a message has been sent. The backend will respond
|
2012-03-30 08:15:52 +02:00
|
|
|
|
;; with (:sent ...) sexp, which is handled in `mu4e-send-compose-handler'.
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(add-hook 'message-sent-hook
|
|
|
|
|
(lambda ()
|
2012-03-31 16:39:47 +02:00
|
|
|
|
(set-buffer-modified-p t)
|
2012-03-30 08:15:52 +02:00
|
|
|
|
(basic-save-buffer)
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(mu4e-proc-sent (buffer-file-name) mu4e-drafts-folder)))
|
2012-03-30 08:15:52 +02:00
|
|
|
|
|
2012-03-28 17:58:43 +02:00
|
|
|
|
;; register the function; this function will be called when the '(:sent...)'
|
|
|
|
|
;; message is received (see mu4e-proc.el) with parameters docid and path
|
|
|
|
|
(setq mu4e-proc-sent-func 'mu4e-sent-handler)
|
|
|
|
|
|
|
|
|
|
;; set the default directory to the user's home dir; this is probably more
|
|
|
|
|
;; useful e.g. when finding an attachment file the directory the current
|
|
|
|
|
;; mail files lives in...
|
2012-03-31 16:26:36 +02:00
|
|
|
|
(setq default-directory (expand-file-name "~/")))
|
2012-03-28 00:47:52 +02:00
|
|
|
|
|
|
|
|
|
|
2012-04-01 20:36:13 +02:00
|
|
|
|
(defconst mu4e-send-buffer-max-name-length 20
|
|
|
|
|
"Maximum length of the mu4e-send-buffer-name.")
|
|
|
|
|
|
|
|
|
|
(defun mu4e-set-buffer-name (&optional compose-type)
|
|
|
|
|
"Set some user-friendly buffer name based on the compose type."
|
|
|
|
|
(let* ((subj (message-field-value "subject"))
|
|
|
|
|
(subj (unless (and subj (string-match "^[:blank:]*$" subj)) subj))
|
|
|
|
|
(str (or subj
|
|
|
|
|
(case compose-type
|
|
|
|
|
(reply "*reply*")
|
|
|
|
|
(forward "*forward*")
|
|
|
|
|
(otherwise "*draft*")))))
|
|
|
|
|
(rename-buffer (generate-new-buffer-name
|
|
|
|
|
(truncate-string-to-width str
|
|
|
|
|
mu4e-send-buffer-max-name-length
|
|
|
|
|
nil nil t)))))
|
|
|
|
|
|
2012-03-28 00:47:52 +02:00
|
|
|
|
|
2012-01-14 11:55:50 +01:00
|
|
|
|
(defun mu4e-send-compose-handler (compose-type &optional original-msg includes)
|
2012-04-01 20:36:13 +02:00
|
|
|
|
"Create a new draft message, or open an existing one.
|
2011-10-02 20:35:03 +02:00
|
|
|
|
|
|
|
|
|
COMPOSE-TYPE determines the kind of message to compose and is a
|
|
|
|
|
symbol, either `reply', `forward', `edit', `new'. `edit' is for
|
|
|
|
|
editing existing messages.
|
|
|
|
|
|
|
|
|
|
When COMPOSE-TYPE is `reply' or `forward', MSG should be a message
|
|
|
|
|
plist. If COMPOSE-TYPE is `new', MSG should be nil.
|
|
|
|
|
|
2012-01-14 11:55:50 +01:00
|
|
|
|
Optionally (when forwarding, replying) ORIGINAL-MSG is the original
|
|
|
|
|
message we will forward / reply to.
|
|
|
|
|
|
|
|
|
|
Optionally (when forwarding) INCLUDES contains a list of
|
|
|
|
|
(:file-name <filename> :mime-type <mime-type> :disposition <disposition>)
|
|
|
|
|
for the attachements to include; file-name refers to
|
|
|
|
|
a file which our backend has conveniently saved for us (as a
|
|
|
|
|
tempfile).
|
|
|
|
|
|
2011-10-02 20:35:03 +02:00
|
|
|
|
The name of the draft folder is constructed from the concatenation
|
2011-12-13 08:07:38 +01:00
|
|
|
|
of `mu4e-maildir' and `mu4e-drafts-folder' (therefore, these must be
|
2011-10-02 20:35:03 +02:00
|
|
|
|
set).
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
|
|
|
|
The message file name is a unique name determined by
|
2011-12-20 20:40:16 +01:00
|
|
|
|
`mu4e-send-draft-file-name'.
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-12-20 20:40:16 +01:00
|
|
|
|
The initial STR would be created from either `mu4e-send-create-reply',
|
|
|
|
|
ar`mu4e-send-create-forward' or `mu4e-send-create-new'. The editing buffer is
|
2011-09-18 13:39:36 +02:00
|
|
|
|
using Gnus' `message-mode'."
|
2012-04-01 20:36:13 +02:00
|
|
|
|
(unless mu4e-maildir (error "mu4e-maildir not set"))
|
|
|
|
|
(unless mu4e-drafts-folder (error "mu4e-drafts-folder not set"))
|
|
|
|
|
(let ((draft
|
|
|
|
|
(if (member compose-type '(reply forward new))
|
|
|
|
|
(mu4e-send-open-draft compose-type original-msg)
|
|
|
|
|
(if (eq compose-type 'edit)
|
|
|
|
|
(plist-get original-msg :path)
|
|
|
|
|
(error "unsupported compose-type %S" compose-type)))))
|
|
|
|
|
|
|
|
|
|
(find-file draft)
|
|
|
|
|
(mu4e-edit-mode)
|
|
|
|
|
|
|
|
|
|
;; insert mail-header-separator, which is needed by message mode to separate
|
|
|
|
|
;; headers and body. will be removed before saving to disk
|
|
|
|
|
(mu4e-insert-mail-header-separator)
|
|
|
|
|
|
|
|
|
|
;; include files -- e.g. when forwarding a message with attachments,
|
|
|
|
|
;; we take those from the original.
|
|
|
|
|
(save-excursion
|
|
|
|
|
(goto-char (point-max)) ;; put attachments at the end
|
|
|
|
|
(dolist (att includes)
|
|
|
|
|
(mml-attach-file
|
|
|
|
|
(plist-get att :file-name) (plist-get att :mime-type))))
|
|
|
|
|
|
|
|
|
|
;; include the message header if it's set; but not when editing an existing
|
|
|
|
|
;; message
|
|
|
|
|
(unless (eq compose-type 'edit)
|
|
|
|
|
(when message-signature
|
|
|
|
|
(message-insert-signature)))
|
|
|
|
|
|
|
|
|
|
(if (member compose-type '(new forward))
|
|
|
|
|
(message-goto-to)
|
|
|
|
|
(message-goto-body))
|
|
|
|
|
|
|
|
|
|
(mu4e-set-buffer-name compose-type)
|
|
|
|
|
|
|
|
|
|
;; buffer is not user-modified yet
|
|
|
|
|
(set-buffer-modified-p nil)))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
* make it configurable what to do with sent messages (part 2), and document it:
By default, @t{mu4e} puts a copy of any messages you sent in the folder you
set for @code{mu4e-sent-folder}. In some case, this may not be what you want -
for example, when using GMail+@abbr{IMAP} (but @emph{not} with
GMail+@abbr{POP3}), this interferes with GMail's handling of the sent messages
folder, and you may end up with duplicate messages. For this, since @t{mu4e}
0.9.8.3, there is the variable @code{mu4e-sent-messages-behavior}, which takes
a symbol. The default is @code{'sent} which, as stated causes the message to
be copied to your sent-messages folder. Other possible values are
@code{'trash} (so the sent message is copied to the trash-folder
(@code{mu4e-trash-folder}), and @code{'delete} to simply discard the message
altogether.
2012-03-13 19:13:30 +01:00
|
|
|
|
|
2012-03-13 07:28:25 +01:00
|
|
|
|
|
2012-03-27 20:15:47 +02:00
|
|
|
|
(defun mu4e-sent-handler (docid path)
|
|
|
|
|
"Handler function, called with DOCID and PATH for the just-sent
|
|
|
|
|
message."
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(with-current-buffer (find-file-noselect path)
|
|
|
|
|
;; for Forward ('Passed') and Replied messages, try to set the appropriate
|
|
|
|
|
;; flag at the message forwarded or replied-to
|
|
|
|
|
(mu4e-send-set-parent-flag docid path)
|
|
|
|
|
;; handle the draft -- should it be moved to the send folder, or elsewhere?
|
|
|
|
|
(mu4e-send-save-copy-maybe docid path)
|
|
|
|
|
;; now, get rid of the buffer
|
|
|
|
|
(kill-buffer)))
|
2012-03-27 20:15:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun mu4e-send-save-copy-maybe (docid path)
|
|
|
|
|
"Handler function, called with DOCID and PATH for the just-sent
|
2012-03-28 17:58:43 +02:00
|
|
|
|
message, which will move it to the sent-folder or elsewhere,
|
|
|
|
|
depending on the value of `mu4e-sent-messages-behavior'.
|
|
|
|
|
|
|
|
|
|
Function assumes that it's executed in the context of the message
|
|
|
|
|
buffer."
|
2012-03-30 08:15:52 +02:00
|
|
|
|
;; first, what to do with the draft message in PATH?
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(if (eq mu4e-sent-messages-behavior 'delete)
|
2012-03-31 16:26:36 +02:00
|
|
|
|
(mu4e-proc-remove-msg docid) ;; remove it
|
2012-03-28 17:58:43 +02:00
|
|
|
|
;; otherwise,
|
|
|
|
|
(progn ;; prepare the message for saving
|
2012-03-31 16:26:36 +02:00
|
|
|
|
(basic-save-buffer)
|
|
|
|
|
;; now either move it to trash or to sent
|
|
|
|
|
(if (eq mu4e-sent-messages-behavior 'trash)
|
|
|
|
|
(mu4e-proc-move-msg docid mu4e-trash-folder "+T-D+S")
|
|
|
|
|
(mu4e-proc-move-msg docid mu4e-sent-folder "-T-D+S")))))
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2012-03-27 20:15:47 +02:00
|
|
|
|
(defun mu4e-send-set-parent-flag (docid path)
|
|
|
|
|
"Set the 'replied' \"R\" flag on messages we replied to, and the
|
|
|
|
|
'passed' \"F\" flag on message we have forwarded.
|
2011-09-18 13:39:36 +02:00
|
|
|
|
|
2011-10-02 20:35:03 +02:00
|
|
|
|
If a message has a 'in-reply-to' header, it is considered a reply
|
|
|
|
|
to the message with the corresponding message id. If it does not
|
|
|
|
|
have an 'in-reply-to' header, but does have a 'references' header,
|
|
|
|
|
it is considered to be a forward message for the message
|
|
|
|
|
corresponding with the /last/ message-id in the references header.
|
2011-09-30 07:37:47 +02:00
|
|
|
|
|
2011-10-02 20:35:03 +02:00
|
|
|
|
Now, if the message has been determined to be either a forwarded
|
|
|
|
|
message or a reply, we instruct the server to update that message
|
|
|
|
|
with resp. the 'P' (passed) flag for a forwarded message, or the
|
2012-03-28 17:58:43 +02:00
|
|
|
|
'R' flag for a replied message.
|
|
|
|
|
|
|
|
|
|
Function assumes that it's executed in the context of the message
|
|
|
|
|
buffer.
|
|
|
|
|
"
|
|
|
|
|
(let ((in-reply-to (message-fetch-field "in-reply-to"))
|
|
|
|
|
(forwarded-from)
|
2012-03-30 08:15:52 +02:00
|
|
|
|
(references (message-fetch-field "references")))
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(unless in-reply-to
|
|
|
|
|
(when references
|
|
|
|
|
(with-temp-buffer ;; inspired by `message-shorten-references'.
|
|
|
|
|
(insert references)
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(let ((refs))
|
|
|
|
|
(while (re-search-forward "<[^ <]+@[^ <]+>" nil t)
|
|
|
|
|
(push (match-string 0) refs))
|
2012-03-31 13:22:06 +02:00
|
|
|
|
;; the last will be the first
|
2012-03-28 17:58:43 +02:00
|
|
|
|
(setq forwarded-from (first refs))))))
|
|
|
|
|
;; remove the <>
|
|
|
|
|
(when (and in-reply-to (string-match "<\\(.*\\)>" in-reply-to))
|
|
|
|
|
(mu4e-proc-flag (match-string 1 in-reply-to) "+R"))
|
|
|
|
|
(when (and forwarded-from (string-match "<\\(.*\\)>" forwarded-from))
|
|
|
|
|
(mu4e-proc-flag (match-string 1 forwarded-from) "+P"))))
|
2011-10-03 08:13:58 +02:00
|
|
|
|
|
2011-12-13 08:07:38 +01:00
|
|
|
|
(provide 'mu4e-send)
|