mh-comp.el 52.6 KB
Newer Older
Bill Wohler's avatar
Bill Wohler committed
1
;;; mh-comp.el --- MH-E functions for composing and sending messages
Richard M. Stallman's avatar
Richard M. Stallman committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1993, 1995, 1997, 2000-2020 Free Software Foundation,
4
;; Inc.
Stefan Monnier's avatar
Stefan Monnier committed
5

Bill Wohler's avatar
Bill Wohler committed
6
;; Author: Bill Wohler <wohler@newt.com>
Stefan Monnier's avatar
Stefan Monnier committed
7
;; Keywords: mail
Bill Wohler's avatar
Bill Wohler committed
8
;; See: mh-e.el
Richard M. Stallman's avatar
Richard M. Stallman committed
9

10
;; This file is part of GNU Emacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
11

12
;; GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
16

Karl Heuer's avatar
Karl Heuer committed
17
;; GNU Emacs is distributed in the hope that it will be useful,
Richard M. Stallman's avatar
Richard M. Stallman committed
18 19 20 21 22
;; 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
23
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26

;;; Commentary:

Bill Wohler's avatar
Bill Wohler committed
27 28 29 30
;; This file includes the functions in the MH-Folder maps that get us
;; into MH-Letter mode, as well the functions in the MH-Letter mode
;; that are used to send the mail. Other that those, functions that
;; are needed in mh-letter.el should be found there.
Richard M. Stallman's avatar
Richard M. Stallman committed
31

Karl Heuer's avatar
Karl Heuer committed
32 33
;;; Change Log:

Richard M. Stallman's avatar
Richard M. Stallman committed
34 35
;;; Code:

36
(require 'mh-e)
Bill Wohler's avatar
Bill Wohler committed
37 38
(require 'mh-gnus)                      ;needed because mh-gnus.el not compiled
(require 'mh-scan)
39

Bill Wohler's avatar
Bill Wohler committed
40
(require 'sendmail)
Bill Wohler's avatar
Bill Wohler committed
41

Bill Wohler's avatar
Bill Wohler committed
42 43
(autoload 'easy-menu-add "easymenu")
(autoload 'mml-insert-tag "mml")
Richard M. Stallman's avatar
Richard M. Stallman committed
44

Bill Wohler's avatar
Bill Wohler committed
45 46


Bill Wohler's avatar
Bill Wohler committed
47
;;; Site Customization
Karl Heuer's avatar
Karl Heuer committed
48 49 50 51 52

(defvar mh-send-prog "send"
  "Name of the MH send program.
Some sites need to change this because of a name conflict.")

53 54 55 56 57 58 59 60 61
(defvar mh-send-uses-spost-flag nil
  "Non-nil means \"send\" uses \"spost\" to submit messages.

If the value of \"postproc:\" is \"spost\", you may need to set
this variable to t to tell MH-E to avoid using features of
\"post\" that are not supported by \"spost\". You'll know that
you'll need to do this if sending mail fails with an error of
\"spost: -msgid unknown\".")

Bill Wohler's avatar
Bill Wohler committed
62 63
(defvar mh-redist-background nil
  "If non-nil redist will be done in background like send.
64 65
This allows transaction log to be visible if -watch, -verbose or
-snoop are used.")
Karl Heuer's avatar
Karl Heuer committed
66

Bill Wohler's avatar
Bill Wohler committed
67 68


Bill Wohler's avatar
Bill Wohler committed
69
;;; Variables
Richard M. Stallman's avatar
Richard M. Stallman committed
70 71 72

(defvar mh-comp-formfile "components"
  "Name of file to be used as a skeleton for composing messages.
73 74 75 76 77

Default is \"components\".

If not an absolute file name, the file is searched for first in the
user's MH directory, then in the system MH lib directory.")
Richard M. Stallman's avatar
Richard M. Stallman committed
78

79 80 81 82 83 84 85 86
(defvar mh-dist-formfile "distcomps"
  "Name of file to be used as a skeleton for redistributing messages.

Default is \"distcomps\".

If not an absolute file name, the file is searched for first in the
user's MH directory, then in the system MH lib directory.")

Karl Heuer's avatar
Karl Heuer committed
87 88
(defvar mh-repl-formfile "replcomps"
  "Name of file to be used as a skeleton for replying to messages.
89 90 91 92 93

Default is \"replcomps\".

If not an absolute file name, the file is searched for first in the
user's MH directory, then in the system MH lib directory.")
Karl Heuer's avatar
Karl Heuer committed
94

Karl Heuer's avatar
Karl Heuer committed
95
(defvar mh-repl-group-formfile "replgroupcomps"
Bill Wohler's avatar
Bill Wohler committed
96
  "Name of file to be used as a skeleton for replying to messages.
97

Bill Wohler's avatar
Bill Wohler committed
98
Default is \"replgroupcomps\".
99 100

This file is used to form replies to the sender and all recipients of
101
a message. Only used if (mh-variant-p \\='nmh) is non-nil.
102 103
If not an absolute file name, the file is searched for first in the
user's MH directory, then in the system MH lib directory.")
Bill Wohler's avatar
Bill Wohler committed
104

Richard M. Stallman's avatar
Richard M. Stallman committed
105
(defvar mh-rejected-letter-start
Bill Wohler's avatar
Bill Wohler committed
106
  (format "^%s$"
Bill Wohler's avatar
Bill Wohler committed
107 108
          (regexp-opt
           '("Content-Type: message/rfc822" ;MIME MDN
Bill Wohler's avatar
Bill Wohler committed
109
             "------ This is a copy of the message, including all the headers. ------";from exim
Bill Wohler's avatar
Bill Wohler committed
110
             "--- Below this line is a copy of the message."; from qmail
Bill Wohler's avatar
Bill Wohler committed
111 112 113 114 115 116 117 118 119
             "   ----- Unsent message follows -----" ;from sendmail V5
             " --------Unsent Message below:" ; from sendmail at BU
             "   ----- Original message follows -----" ;from sendmail V8
             "------- Unsent Draft"     ;from MH itself
             "----------  Original Message  ----------" ;from zmailer
             "  --- The unsent message follows ---" ;from AIX mail system
             "    Your message follows:" ;from MMDF-II
             "Content-Description: Returned Content" ;1993 KJ sendmail
             ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
120 121

(defvar mh-new-draft-cleaned-headers
Karl Heuer's avatar
Karl Heuer committed
122
  "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
123 124
  "Regexp of header lines to remove before offering a message as a new draft\\<mh-folder-mode-map>.
Used by the \\[mh-edit-again] and \\[mh-extract-rejected-mail] commands.")
Richard M. Stallman's avatar
Richard M. Stallman committed
125

126 127 128 129
(defvar mh-letter-mode-syntax-table
  (let ((syntax-table (make-syntax-table text-mode-syntax-table)))
    (modify-syntax-entry ?% "." syntax-table)
    syntax-table)
Bill Wohler's avatar
Bill Wohler committed
130
  "Syntax table used by MH-E while in MH-Letter mode.")
Richard M. Stallman's avatar
Richard M. Stallman committed
131

132
(defvar mh-regexp-in-field-syntax-table nil
133
  "Specify a syntax table for `mh-regexp-in-field-p' to use.")
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

(defvar mh-fcc-syntax-table
  (let ((syntax-table (make-syntax-table text-mode-syntax-table)))
    (modify-syntax-entry ?+ "w" syntax-table)
    (modify-syntax-entry ?/ "w" syntax-table)
    syntax-table)
  "Syntax table used by MH-E while searching an Fcc field.")

(defvar mh-addr-syntax-table
  (let ((syntax-table (make-syntax-table text-mode-syntax-table)))
    (modify-syntax-entry ?! "w" syntax-table)
    (modify-syntax-entry ?# "w" syntax-table)
    (modify-syntax-entry ?$ "w" syntax-table)
    (modify-syntax-entry ?% "w" syntax-table)
    (modify-syntax-entry ?& "w" syntax-table)
    (modify-syntax-entry ?' "w" syntax-table)
    (modify-syntax-entry ?* "w" syntax-table)
    (modify-syntax-entry ?+ "w" syntax-table)
    (modify-syntax-entry ?- "w" syntax-table)
    (modify-syntax-entry ?/ "w" syntax-table)
    (modify-syntax-entry ?= "w" syntax-table)
    (modify-syntax-entry ?? "w" syntax-table)
    (modify-syntax-entry ?^ "w" syntax-table)
    (modify-syntax-entry ?_ "w" syntax-table)
    (modify-syntax-entry ?` "w" syntax-table)
    (modify-syntax-entry ?{ "w" syntax-table)
    (modify-syntax-entry ?| "w" syntax-table)
    (modify-syntax-entry ?} "w" syntax-table)
    (modify-syntax-entry ?~ "w" syntax-table)
    (modify-syntax-entry ?. "w" syntax-table)
    (modify-syntax-entry ?@ "w" syntax-table)
    syntax-table)
  "Syntax table used by MH-E while searching an address field.")

168
(defvar mh-send-args ""
Bill Wohler's avatar
Bill Wohler committed
169 170 171 172 173 174 175
  "Extra args to pass to \"send\" command.")

(defvar mh-annotate-char nil
  "Character to use to annotate `mh-sent-from-msg'.")

(defvar mh-annotate-field nil
  "Field name for message annotation.")
Richard M. Stallman's avatar
Richard M. Stallman committed
176

Stephen Gildea's avatar
Stephen Gildea committed
177 178 179 180
(defvar mh-annotate-list nil
  "Messages annotated, either a sequence name or a list of message numbers.
This variable can be used by `mh-annotate-msg-hook'.")

Bill Wohler's avatar
Bill Wohler committed
181
(defvar mh-insert-auto-fields-done-local nil
Bill Wohler's avatar
Bill Wohler committed
182
  "Buffer-local variable set when `mh-insert-auto-fields' called successfully.")
Bill Wohler's avatar
Bill Wohler committed
183 184
(make-variable-buffer-local 'mh-insert-auto-fields-done-local)

Bill Wohler's avatar
Bill Wohler committed
185 186 187 188


;;; MH-E Entry Points

Richard M. Stallman's avatar
Richard M. Stallman committed
189 190
;;;###autoload
(defun mh-smail ()
191
  "Compose a message with the MH mail system.
Bill Wohler's avatar
Bill Wohler committed
192
See `mh-send' for more details on composing mail."
Richard M. Stallman's avatar
Richard M. Stallman committed
193 194 195 196
  (interactive)
  (mh-find-path)
  (call-interactively 'mh-send))

197 198 199 200 201 202 203 204
;;;###autoload
(defun mh-smail-other-window ()
  "Compose a message with the MH mail system in other window.
See `mh-send' for more details on composing mail."
  (interactive)
  (mh-find-path)
  (call-interactively 'mh-send-other-window))

Bill Wohler's avatar
Bill Wohler committed
205 206 207 208 209 210 211 212 213 214 215 216
(defun mh-send-other-window (to cc subject)
  "Compose a message in another window.

See `mh-send' for more information and a description of how the
TO, CC, and SUBJECT arguments are used."
  (interactive (list
                (mh-interactive-read-address "To: ")
                (mh-interactive-read-address "Cc: ")
                (mh-interactive-read-string "Subject: ")))
  (let ((pop-up-windows t))
    (mh-send-sub to cc subject (current-window-configuration))))

Bill Wohler's avatar
Bill Wohler committed
217
(defvar mh-error-if-no-draft nil)       ;raise error over using old draft
Karl Heuer's avatar
Karl Heuer committed
218 219

;;;###autoload
Stefan Monnier's avatar
Stefan Monnier committed
220
(defun mh-smail-batch (&optional to subject _other-headers &rest _ignored)
221 222
  "Compose a message with the MH mail system.

223 224 225
This function does not prompt the user for any header fields, and
thus is suitable for use by programs that want to create a mail
buffer. Users should use \\[mh-smail] to compose mail.
Bill Wohler's avatar
Bill Wohler committed
226

227
Optional arguments for setting certain fields include TO,
228 229 230 231
SUBJECT, and OTHER-HEADERS. Additional arguments are IGNORED.

This function remains for Emacs 21 compatibility. New
applications should use `mh-user-agent-compose'."
Karl Heuer's avatar
Karl Heuer committed
232 233
  (mh-find-path)
  (let ((mh-error-if-no-draft t))
234
    (mh-send (or to "") "" (or subject ""))))
Karl Heuer's avatar
Karl Heuer committed
235

236 237 238 239 240
;;;###autoload
(define-mail-user-agent 'mh-e-user-agent
  'mh-user-agent-compose 'mh-send-letter 'mh-fully-kill-draft
  'mh-before-send-letter-hook)

Bill Wohler's avatar
Bill Wohler committed
241
;;;###autoload
Stefan Monnier's avatar
Stefan Monnier committed
242
(defun mh-user-agent-compose (&optional to subject other-headers &rest _ignored)
Bill Wohler's avatar
Bill Wohler committed
243
  "Set up mail composition draft with the MH mail system.
244 245 246 247
This is the `mail-user-agent' entry point to MH-E. This function
conforms to the contract specified by `define-mail-user-agent'
which means that this function should accept the same arguments
as `compose-mail'.
Bill Wohler's avatar
Bill Wohler committed
248 249 250 251

The optional arguments TO and SUBJECT specify recipients and the
initial Subject field, respectively.

252 253 254
OTHER-HEADERS is an alist specifying additional header fields.
Elements look like (HEADER . VALUE) where both HEADER and VALUE
are strings.
Bill Wohler's avatar
Bill Wohler committed
255

Stefan Monnier's avatar
Stefan Monnier committed
256
Any additional arguments are IGNORED."
Bill Wohler's avatar
Bill Wohler committed
257 258 259 260 261
  (mh-find-path)
  (let ((mh-error-if-no-draft t))
    (mh-send to "" subject)
    (while other-headers
      (mh-insert-fields (concat (car (car other-headers)) ":")
Bill Wohler's avatar
Bill Wohler committed
262
                        (cdr (car other-headers)))
Bill Wohler's avatar
Bill Wohler committed
263
      (setq other-headers (cdr other-headers)))))
Karl Heuer's avatar
Karl Heuer committed
264

Stefan Monnier's avatar
Stefan Monnier committed
265
(defvar sendmail-coding-system)
Bill Wohler's avatar
Bill Wohler committed
266 267 268 269 270 271 272

;;;###autoload
(defun mh-send-letter (&optional arg)
  "Save draft and send message.

When you are all through editing a message, you send it with this
command. You can give a prefix argument ARG to monitor the first stage
273
of the delivery; this output can be found in a buffer called \"*MH-E
Bill Wohler's avatar
Bill Wohler committed
274 275 276 277 278 279
Mail Delivery*\".

The hook `mh-before-send-letter-hook' is run at the beginning of
this command. For example, if you want to check your spelling in
your message before sending, add the function `ispell-message'.

280 281 282 283 284 285 286
Unless `mh-insert-auto-fields' had previously been called
manually, the function `mh-insert-auto-fields' is called to
insert fields based upon the recipients. If fields are added, you
are given a chance to see and to confirm these fields before the
message is actually sent. You can do away with this confirmation
by turning off the option `mh-auto-fields-prompt-flag'.

Bill Wohler's avatar
Bill Wohler committed
287
In case the MH \"send\" program is installed under a different name,
288 289 290 291
use `mh-send-prog' to tell MH-E the name.

The hook `mh-annotate-msg-hook' is run after annotating the
message and scan line."
Bill Wohler's avatar
Bill Wohler committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  (interactive "P")
  (run-hooks 'mh-before-send-letter-hook)
  (if (and (mh-insert-auto-fields t)
           mh-auto-fields-prompt-flag
           (goto-char (point-min)))
      (if (not (y-or-n-p "Auto fields inserted, send? "))
          (error "Send aborted")))
  (cond ((mh-mh-directive-present-p)
         (mh-mh-to-mime))
        ((or (mh-mml-tag-present-p) (not (mh-ascii-buffer-p)))
         (mh-mml-to-mime)))
  (save-buffer)
  (message "Sending...")
  (let ((draft-buffer (current-buffer))
        (file-name buffer-file-name)
        (config mh-previous-window-config)
308
        ;; FIXME this is subtly different to select-message-coding-system.
Bill Wohler's avatar
Bill Wohler committed
309
        (coding-system-for-write
310 311 312 313 314 315 316 317 318 319 320 321
         (if (fboundp 'select-message-coding-system)
             (select-message-coding-system) ; Emacs has this since at least 21.1
           (if (and (local-variable-p 'buffer-file-coding-system
                                      (current-buffer)) ;XEmacs needs two args
                    ;; We're not sure why, but buffer-file-coding-system
                    ;; tends to get set to undecided-unix.
                    (not (memq buffer-file-coding-system
                               '(undecided undecided-unix undecided-dos))))
               buffer-file-coding-system
             (or (and (boundp 'sendmail-coding-system) sendmail-coding-system)
                 (and (default-boundp 'buffer-file-coding-system)
                      (default-value 'buffer-file-coding-system))
322
                 'utf-8)))))
323 324 325 326 327 328
    ;; Older versions of spost do not support -msgid and -mime.
    (unless mh-send-uses-spost-flag
      ;; Adding a Message-ID field looks good, makes it easier to search for
      ;; message in your +outbox, and best of all doesn't break threading for
      ;; the recipient if you reply to a message in your +outbox.
      (setq mh-send-args (concat "-msgid " mh-send-args))
329
      ;; The default Bcc encapsulation will make a MIME message unreadable.
330 331 332 333 334
      ;; With nmh use the -mime arg to prevent this.
      (if (and (mh-variant-p 'nmh)
               (mh-goto-header-field "Bcc:")
               (mh-goto-header-field "Content-Type:"))
          (setq mh-send-args (concat "-mime " mh-send-args))))
Bill Wohler's avatar
Bill Wohler committed
335 336 337
    (cond (arg
           (pop-to-buffer mh-mail-delivery-buffer)
           (erase-buffer)
338 339 340
           (mh-exec-cmd-output mh-send-prog t
                               "-nodraftfolder" "-watch" "-nopush"
                               (split-string mh-send-args) file-name)
Bill Wohler's avatar
Bill Wohler committed
341 342 343 344
           (goto-char (point-max))      ; show the interesting part
           (recenter -1)
           (set-buffer draft-buffer))   ; for annotation below
          (t
345 346
           (mh-exec-cmd-daemon mh-send-prog nil
                               "-nodraftfolder" "-noverbose"
347
                               (split-string mh-send-args) file-name)))
Bill Wohler's avatar
Bill Wohler committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    (if mh-annotate-char
        (mh-annotate-msg mh-sent-from-msg
                         mh-sent-from-folder
                         mh-annotate-char
                         "-component" mh-annotate-field
                         "-text" (format "\"%s %s\""
                                         (mh-get-header-field "To:")
                                         (mh-get-header-field "Cc:"))))

    (cond ((or (not arg)
               (y-or-n-p "Kill draft buffer? "))
           (kill-buffer draft-buffer)
           (if config
               (set-window-configuration config))))
    (if arg
        (message "Sending...done")
      (message "Sending...backgrounded"))))

;;;###autoload
(defun mh-fully-kill-draft ()
  "Quit editing and delete draft message.

If for some reason you are not happy with the draft, you can use
this command to kill the draft buffer and delete the draft
message. Use the command \\[kill-buffer] if you don't want to
delete the draft message."
  (interactive)
  (if (y-or-n-p "Kill draft message? ")
      (let ((config mh-previous-window-config))
        (if (file-exists-p buffer-file-name)
            (delete-file buffer-file-name))
        (set-buffer-modified-p nil)
        (kill-buffer (buffer-name))
        (message "")
        (if config
            (set-window-configuration config)))
    (error "Message not killed")))



;;; MH-Folder Commands

;; Alphabetical.

Bill Wohler's avatar
Bill Wohler committed
392
;;;###mh-autoload
393 394 395
(defun mh-edit-again (message)
  "Edit a MESSAGE to send it again.

396 397 398 399 400 401 402 403
If you don't complete a draft for one reason or another, and if
the draft buffer is no longer available, you can pick your draft
up again with this command. If you don't use a draft folder, your
last \"draft\" file will be used. If you use draft folders,
you'll need to visit the draft folder with \"\\[mh-visit-folder]
drafts <RET>\", use \\[mh-next-undeleted-msg] to move to the
appropriate message, and then use \\[mh-edit-again] to prepare
the message for editing.
404

405 406
This command can also be used to take messages that were sent to
you and to send them to more people.
407

408 409 410
Don't use this command to re-edit a message from a Mailer-Daemon
who complained that your mail wasn't posted for some reason or
another (see `mh-extract-rejected-mail').
411 412

The default message is the current message.
Bill Wohler's avatar
Bill Wohler committed
413 414

See also `mh-send'."
Richard M. Stallman's avatar
Richard M. Stallman committed
415 416
  (interactive (list (mh-get-msg-num t)))
  (let* ((from-folder mh-current-folder)
Bill Wohler's avatar
Bill Wohler committed
417
         (config (current-window-configuration))
418
         (components-file (mh-bare-components mh-comp-formfile))
Bill Wohler's avatar
Bill Wohler committed
419 420
         (draft
          (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
421 422 423
                 (pop-to-buffer (find-file-noselect (mh-msg-filename message))
                                t)
                 (rename-buffer (format "draft-%d" message))
Bill Wohler's avatar
Bill Wohler committed
424 425 426 427 428 429 430
                 ;; Make buffer writable...
                 (setq buffer-read-only nil)
                 ;; If buffer was being used to display the message reinsert
                 ;; from file...
                 (when (eq major-mode 'mh-show-mode)
                   (erase-buffer)
                   (insert-file-contents buffer-file-name))
Bill Wohler's avatar
Bill Wohler committed
431 432
                 (buffer-name))
                (t
433
                 (mh-read-draft "clean-up" (mh-msg-filename message) nil)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
434
    (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
Bill Wohler's avatar
Bill Wohler committed
435
    (mh-insert-header-separator)
436
    ;; Merge in components
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    (mh-mapc
     (function
      (lambda (header-field)
        (let ((field (car header-field))
              (value (cdr header-field))
              (case-fold-search t))
          (cond
           ;; Address field
           ((string-match field "^To$\\|^Cc$\\|^From$")
            (cond
             ((not (mh-goto-header-field (concat field ":")))
              ;; Header field does not exist, add it
              (mh-goto-header-end 0)
              (insert field ": " value "\n"))
             ((string-equal value "")
              ;; Header field already exists and no value
              )
             (t
              ;; Header field exists and we have a value
              (let (address mailbox (alias (mh-alias-expand value)))
                (and alias
                     (setq address (ietf-drums-parse-address alias))
                     (setq mailbox (car address)))
                ;; XXX - Need to parse all addresses out of field
                (if (and
                     (not (mh-regexp-in-field-p
                           (concat "\\b" (regexp-quote value) "\\b") field))
                     mailbox
                     (not (mh-regexp-in-field-p
                           (concat "\\b" (regexp-quote mailbox) "\\b") field)))
                    (insert " " value ","))
                ))))
           ((string-match field "^Fcc$")
            ;; Folder reference
            (mh-modify-header-field field value))
           ;; Text field, that's an easy case
           (t
            (mh-modify-header-field field value))))))
475 476
     (mh-components-to-list components-file))
    (delete-file components-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
477
    (goto-char (point-min))
Karl Heuer's avatar
Karl Heuer committed
478
    (save-buffer)
479 480
    (mh-compose-and-send-mail
     draft "" from-folder nil nil nil nil nil nil config)
Bill Wohler's avatar
Bill Wohler committed
481 482
    (mh-letter-mode-message)
    (mh-letter-adjust-point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497
(defun mh-extract-header-field ()
  "Extract field name and field value from the field at point.
Returns a list of field name and value (which may be null)."
  (let ((end (save-excursion (mh-header-field-end)
                             (point))))
    (if (looking-at mh-letter-header-field-regexp)
        (save-excursion
          (goto-char (match-end 1))
          (forward-char 1)
          (skip-chars-forward " \t")
          (cons (match-string-no-properties 1) (buffer-substring-no-properties (point) end))))))


(defun mh-components-to-list (components)
498
  "Convert the COMPONENTS file to a list of field names and values."
499 500 501 502 503 504 505 506 507 508 509 510 511
  (with-current-buffer (get-buffer-create mh-temp-buffer)
    (erase-buffer)
    (insert-file-contents components)
    (goto-char (point-min))
    (let
        ((header-fields nil))
      (while (mh-in-header-p)
        (setq header-fields (append header-fields (list (mh-extract-header-field))))
        (mh-header-field-end)
        (forward-char 1)
        )
      header-fields)))

Bill Wohler's avatar
Bill Wohler committed
512
;;;###mh-autoload
513 514 515
(defun mh-extract-rejected-mail (message)
  "Edit a MESSAGE that was returned by the mail system.

516 517 518 519
This command prepares the message for editing by removing the
Mailer-Daemon envelope and unneeded header fields. Fix whatever
addressing problem you had, and send the message again with
\\[mh-send-letter].
520 521

The default message is the current message.
Bill Wohler's avatar
Bill Wohler committed
522 523

See also `mh-send'."
Richard M. Stallman's avatar
Richard M. Stallman committed
524 525
  (interactive (list (mh-get-msg-num t)))
  (let ((from-folder mh-current-folder)
Bill Wohler's avatar
Bill Wohler committed
526
        (config (current-window-configuration))
527
        (draft (mh-read-draft "extraction" (mh-msg-filename message) nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
528 529
    (goto-char (point-min))
    (cond ((re-search-forward mh-rejected-letter-start nil t)
Bill Wohler's avatar
Bill Wohler committed
530 531 532 533
           (skip-chars-forward " \t\n")
           (delete-region (point-min) (point))
           (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
          (t
Bill Wohler's avatar
Bill Wohler committed
534
           (message "Does not appear to be a rejected letter")))
Bill Wohler's avatar
Bill Wohler committed
535
    (mh-insert-header-separator)
Richard M. Stallman's avatar
Richard M. Stallman committed
536
    (goto-char (point-min))
Karl Heuer's avatar
Karl Heuer committed
537
    (save-buffer)
538
    (mh-compose-and-send-mail draft "" from-folder message
Bill Wohler's avatar
Bill Wohler committed
539 540 541 542
                              (mh-get-header-field "To:")
                              (mh-get-header-field "From:")
                              (mh-get-header-field "Cc:")
                              nil nil config)
Bill Wohler's avatar
Bill Wohler committed
543
    (mh-letter-mode-message)))
Richard M. Stallman's avatar
Richard M. Stallman committed
544

Bill Wohler's avatar
Bill Wohler committed
545
;;;###mh-autoload
Bill Wohler's avatar
Bill Wohler committed
546
(defun mh-forward (to cc &optional range)
547
  "Forward message.
Bill Wohler's avatar
Bill Wohler committed
548

549 550 551
You are prompted for the TO and CC recipients. You are given a
draft to edit that looks like it would if you had run the MH
command \"forw\". You can then add some text.
Bill Wohler's avatar
Bill Wohler committed
552

553 554 555 556
You can forward several messages by using a RANGE. All of the
messages in the range are inserted into your draft. Check the
documentation of `mh-interactive-range' to see how RANGE is read
in interactive use.
557

558 559 560 561
The hook `mh-forward-hook' is called on the draft.

See also `mh-compose-forward-as-mime-flag',
`mh-forward-subject-format', and `mh-send'."
Bill Wohler's avatar
Bill Wohler committed
562 563 564
  (interactive (list (mh-interactive-read-address "To: ")
                     (mh-interactive-read-address "Cc: ")
                     (mh-interactive-range "Forward")))
Richard M. Stallman's avatar
Richard M. Stallman committed
565
  (let* ((folder mh-current-folder)
Bill Wohler's avatar
Bill Wohler committed
566
         (msgs (mh-range-to-msg-list range))
Bill Wohler's avatar
Bill Wohler committed
567 568 569 570 571
         (config (current-window-configuration))
         (fwd-msg-file (mh-msg-filename (car msgs) folder))
         ;; forw always leaves file in "draft" since it doesn't have -draft
         (draft-name (expand-file-name "draft" mh-user-path))
         (draft (cond ((or (not (file-exists-p draft-name))
572
                           (y-or-n-p "The file draft exists; discard it? "))
Bill Wohler's avatar
Bill Wohler committed
573 574 575 576
                       (mh-exec-cmd "forw" "-build"
                                    (if (and (mh-variant-p 'nmh)
                                             mh-compose-forward-as-mime-flag)
                                        "-mime")
Bill Wohler's avatar
Bill Wohler committed
577 578
                                    mh-current-folder
                                    (mh-coalesce-msg-list msgs))
Bill Wohler's avatar
Bill Wohler committed
579 580 581 582 583 584
                       (prog1
                           (mh-read-draft "" draft-name t)
                         (mh-insert-fields "To:" to "Cc:" cc)
                         (save-buffer)))
                      (t
                       (mh-read-draft "" draft-name nil)))))
Karl Heuer's avatar
Karl Heuer committed
585
    (let (orig-from
Bill Wohler's avatar
Bill Wohler committed
586
          orig-subject)
587
      (with-current-buffer (get-buffer-create mh-temp-buffer)
Bill Wohler's avatar
Bill Wohler committed
588 589 590 591
        (erase-buffer)
        (insert-file-contents fwd-msg-file)
        (setq orig-from (mh-get-header-field "From:"))
        (setq orig-subject (mh-get-header-field "Subject:")))
Richard M. Stallman's avatar
Richard M. Stallman committed
592
      (let ((forw-subject
Bill Wohler's avatar
Bill Wohler committed
593
             (mh-forwarded-letter-subject orig-from orig-subject)))
Bill Wohler's avatar
Bill Wohler committed
594 595
        (mh-insert-fields "Subject:" forw-subject)
        (goto-char (point-min))
596 597 598 599 600 601 602
        ;; Set the local value of mh-mail-header-separator according to what is
        ;; present in the buffer...
        (set (make-local-variable 'mh-mail-header-separator)
             (save-excursion
               (goto-char (mh-mail-header-end))
               (buffer-substring-no-properties (point) (mh-line-end-position))))
        (set (make-local-variable 'mail-header-separator) mh-mail-header-separator) ;override sendmail.el
603 604
        ;; If using MML, translate MH-style directive
        (if (equal mh-compose-insertion 'mml)
Bill Wohler's avatar
Bill Wohler committed
605
            (save-excursion
Bill Wohler's avatar
Bill Wohler committed
606
              (goto-char (mh-mail-header-end))
Bill Wohler's avatar
Bill Wohler committed
607 608
              (while
                  (re-search-forward
609
                   "^#forw \\[\\([^]]+\\)\\] \\(\\+\\S-+\\) \\(.*\\)$"
Bill Wohler's avatar
Bill Wohler committed
610 611 612 613 614 615 616 617 618 619 620 621
                   (point-max) t)
                (let ((description (if (equal (match-string 1)
                                              "forwarded messages")
                                       "forwarded message %d"
                                     (match-string 1)))
                      (msgs (split-string (match-string 3)))
                      (i 0))
                  (beginning-of-line)
                  (delete-region (point) (progn (forward-line 1) (point)))
                  (dolist (msg msgs)
                    (setq i (1+ i))
                    (mh-mml-forward-message (format description i)
622 623 624
                                            folder msg)
                    ;; Was inserted before us, move to end of file to preserve order
                    (goto-char (point-max)))))))
Paul Eggert's avatar
Paul Eggert committed
625
        ;; Position just before forwarded message.
Bill Wohler's avatar
Bill Wohler committed
626 627
        (if (re-search-forward "^------- Forwarded Message" nil t)
            (forward-line -1)
Bill Wohler's avatar
Bill Wohler committed
628
          (goto-char (mh-mail-header-end))
Bill Wohler's avatar
Bill Wohler committed
629 630 631
          (forward-line 1))
        (delete-other-windows)
        (mh-add-msgs-to-seq msgs 'forwarded t)
Bill Wohler's avatar
Bill Wohler committed
632
        (mh-compose-and-send-mail draft "" folder msgs
Bill Wohler's avatar
Bill Wohler committed
633 634 635
                                  to forw-subject cc
                                  mh-note-forw "Forwarded:"
                                  config)
Bill Wohler's avatar
Bill Wohler committed
636
        (mh-letter-mode-message)
Bill Wohler's avatar
Bill Wohler committed
637 638
        (mh-letter-adjust-point)
        (run-hooks 'mh-forward-hook)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
639 640

(defun mh-forwarded-letter-subject (from subject)
Bill Wohler's avatar
Bill Wohler committed
641 642
  "Return a Subject suitable for a forwarded message.
Original message has headers FROM and SUBJECT."
Richard M. Stallman's avatar
Richard M. Stallman committed
643
  (let ((addr-start (string-match "<" from))
Bill Wohler's avatar
Bill Wohler committed
644
        (comment (string-match "(" from)))
Richard M. Stallman's avatar
Richard M. Stallman committed
645
    (cond ((and addr-start (> addr-start 0))
Bill Wohler's avatar
Bill Wohler committed
646 647 648 649 650
           ;; Full Name <luser@host>
           (setq from (substring from 0 (1- addr-start))))
          (comment
           ;; luser@host (Full Name)
           (setq from (substring from (1+ comment) (1- (length from)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
651 652
  (format mh-forward-subject-format from subject))

653
;;;###mh-autoload
654
(defun mh-redistribute (to cc identity &optional message)
655
  "Redistribute a message.
Karl Heuer's avatar
Karl Heuer committed
656

657 658 659 660
This command is similar in function to forwarding mail, but it
does not allow you to edit the message, nor does it add your name
to the \"From\" header field. It appears to the recipient as if
the message had come from the original sender. When you run this
661 662 663
command, you are prompted for the TO and CC recipients. You are
also prompted for the sending IDENTITY to use. The default
MESSAGE is the current message.
Richard M. Stallman's avatar
Richard M. Stallman committed
664

665
Also investigate the command \\[mh-edit-again] for another way to
666
redistribute messages.
667

668 669 670 671
See also `mh-redist-full-contents-flag'.

The hook `mh-annotate-msg-hook' is run after annotating the
message and scan line."
Richard M. Stallman's avatar
Richard M. Stallman committed
672
  (interactive (list (mh-read-address "Redist-To: ")
Bill Wohler's avatar
Bill Wohler committed
673
                     (mh-read-address "Redist-Cc: ")
674 675 676
                     (if mh-identity-list
                         (mh-select-identity mh-identity-default)
                       nil)
Bill Wohler's avatar
Bill Wohler committed
677
                     (mh-get-msg-num t)))
678 679
  (or message
      (setq message (mh-get-msg-num t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
680 681
  (save-window-excursion
    (let ((folder mh-current-folder)
Bill Wohler's avatar
Bill Wohler committed
682
          (draft (mh-read-draft "redistribution"
683 684
                                (if mh-redist-full-contents-flag
                                    (mh-msg-filename message)
Bill Wohler's avatar
Bill Wohler committed
685
                                  nil)
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
                                nil))
          (from (mh-identity-field identity "From"))
          (fcc  (mh-identity-field identity "Fcc"))
          (bcc  (mh-identity-field identity "Bcc"))
          comp-fcc comp-to comp-cc comp-bcc)
      (if mh-redist-full-contents-flag
          (mh-clean-msg-header
           (point-min)
           "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Date:\\|^Resent-.*:"
           nil))
      ;; Read fields from the distcomps file and put them in our
      ;; draft. For "To", "Cc", "Bcc", and "Fcc", multiple headers are
      ;; combined into a single header with comma-separated entries.
      ;; For "From", the first value wins, with the identity's "From"
      ;; trumping anything in the distcomps file.
      (let ((components-file (mh-bare-components mh-dist-formfile)))
        (mh-mapc
         (function
          (lambda (header-field)
            (let ((field (car header-field))
                  (value (cdr header-field))
                  (case-fold-search t))
              (cond
               ((string-match field "^Resent-Fcc$")
                (setq comp-fcc value))
               ((string-match field "^Resent-From$")
                (or from
                    (setq from value)))
               ((string-match field "^Resent-To$")
                (setq comp-to value))
               ((string-match field "^Resent-Cc$")
                (setq comp-cc value))
               ((string-match field "^Resent-Bcc$")
                (setq comp-bcc value))
               ((string-match field "^Resent-.*$")
                (mh-insert-fields field value))))))
         (mh-components-to-list components-file))
        (delete-file components-file))
      (mh-insert-fields "Resent-To:" (mapconcat 'identity (list to comp-to) ", ")
                        "Resent-Cc:" (mapconcat 'identity (list cc comp-cc) ", ")
                        "Resent-Fcc:" (mapconcat 'identity (list fcc
                                                                 comp-fcc) ", ")
                        "Resent-Bcc:" (mapconcat 'identity (list bcc
                                                                 comp-bcc) ", ")
                        "Resent-From:" from)
Richard M. Stallman's avatar
Richard M. Stallman committed
731 732
      (save-buffer)
      (message "Redistributing...")
Bill Wohler's avatar
Bill Wohler committed
733 734
      (let ((env "mhdist=1"))
        ;; Setup environment...
735 736 737 738 739
        (setq env (concat env " mhaltmsg="
                          (if mh-redist-full-contents-flag
                              buffer-file-name
                            (mh-msg-filename message folder))))
        (unless mh-redist-full-contents-flag
Bill Wohler's avatar
Bill Wohler committed
740 741 742 743 744 745
          (setq env (concat env " mhannotate=1")))
        ;; Redistribute...
        (if mh-redist-background
            (mh-exec-cmd-env-daemon env mh-send-prog nil buffer-file-name)
          (mh-exec-cmd-error env mh-send-prog "-push" buffer-file-name))
        ;; Annotate...
746
        (mh-annotate-msg message folder mh-note-dist
Bill Wohler's avatar
Bill Wohler committed
747
                         "-component" "Resent:"
748 749
                         "-text" (format "\"To: %s Cc: %s From: %s\""
                                         to cc from)))
Richard M. Stallman's avatar
Richard M. Stallman committed
750 751 752
      (kill-buffer draft)
      (message "Redistributing...done"))))

Bill Wohler's avatar
Bill Wohler committed
753
;;;###mh-autoload
Bill Wohler's avatar
Bill Wohler committed
754
(defun mh-reply (message &optional reply-to includep)
755
  "Reply to a MESSAGE.
Bill Wohler's avatar
Bill Wohler committed
756

757 758 759
When you reply to a message, you are first prompted with \"Reply
to whom?\" (unless the optional argument REPLY-TO is provided).
You have several choices here.
760 761 762

     Response     Reply Goes To

763
     from         The person who sent the message. This is the
764
                  default, so <RET> is sufficient.
765 766 767 768

     to           Replies to the sender, plus all recipients in the
                  \"To:\" header field.

769 770 771 772
     all cc       Forms a reply to the addresses in the
                  \"Mail-Followup-To:\" header field if one
                  exists; otherwise forms a reply to the sender,
                  plus all recipients.
773

774 775 776 777 778
Depending on your answer, \"repl\" is given a different argument
to form your reply. Specifically, a choice of \"from\" or none at
all runs \"repl -nocc all\", and a choice of \"to\" runs \"repl
-cc to\". Finally, either \"cc\" or \"all\" runs \"repl -cc all
-nocc me\".
779

780 781
Two windows are then created. One window contains the message to
which you are replying in an MH-Show buffer. Your draft, in
782 783 784 785 786
MH-Letter mode (*note `mh-letter-mode'), is in the other window.
If the reply draft was not one that you expected, check the
things that affect the behavior of \"repl\" which include the
\"repl:\" profile component and the \"replcomps\" and
\"replgroupcomps\" files.
787

788 789 790
If you supply a prefix argument INCLUDEP, the message you are
replying to is inserted in your reply after having first been run
through \"mhl\" with the format file \"mhl.reply\".
791

792 793 794
Alternatively, you can customize the option `mh-yank-behavior'
and choose one of its \"Automatically\" variants to do the same
thing. If you do so, the prefix argument has no effect.
795

796 797
Another way to include the message automatically in your draft is
to use \"repl: -filter repl.filter\" in your MH profile.
798

799 800
If you wish to customize the header or other parts of the reply
draft, please see \"repl\" and \"mh-format\".
801

802 803
See also `mh-reply-show-message-flag',
`mh-reply-default-reply-to', and `mh-send'."
Bill Wohler's avatar
Bill Wohler committed
804 805 806 807 808
  (interactive (list
                (mh-get-msg-num t)
                (let ((minibuffer-help-form
                       "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
                  (or mh-reply-default-reply-to
809
                      (completing-read "Reply to whom (default from): "
Bill Wohler's avatar
Bill Wohler committed
810 811 812 813 814 815 816 817
                                       '(("from") ("to") ("cc") ("all"))
                                       nil
                                       t)))
                current-prefix-arg))
  (let* ((folder mh-current-folder)
         (show-buffer mh-show-buffer)
         (config (current-window-configuration))
         (group-reply (or (equal reply-to "cc") (equal reply-to "all")))
818
         (form-file (cond ((and (mh-variant-p 'nmh 'gnu-mh) group-reply
Bill Wohler's avatar
Bill Wohler committed
819 820 821 822 823 824 825 826 827 828 829 830 831
                                (stringp mh-repl-group-formfile))
                           mh-repl-group-formfile)
                          ((stringp mh-repl-formfile) mh-repl-formfile)
                          (t nil))))
    (message "Composing a reply...")
    (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
                 (if form-file
                     (list "-form" form-file))
                 mh-current-folder message
                 (cond ((or (equal reply-to "from") (equal reply-to ""))
                        '("-nocc" "all"))
                       ((equal reply-to "to")
                        '("-cc" "to"))
832
                       (group-reply (if (mh-variant-p 'nmh 'gnu-mh)
Bill Wohler's avatar
Bill Wohler committed
833 834
                                        '("-group" "-nocc" "me")
                                      '("-cc" "all" "-nocc" "me"))))
835 836
                 (cond ((or (eq mh-yank-behavior 'autosupercite)
                            (eq mh-yank-behavior 'autoattrib))
Bill Wohler's avatar
Bill Wohler committed
837 838 839
                        '("-noformat"))
                       (includep '("-filter" "mhl.reply"))
                       (t '())))
Bill Wohler's avatar
Bill Wohler committed
840 841 842 843 844
    (let ((draft (mh-read-draft "reply"
                                (expand-file-name "reply" mh-user-path)
                                t)))
      (delete-other-windows)
      (save-buffer)
845

Bill Wohler's avatar
Bill Wohler committed
846 847 848 849 850 851 852 853 854 855 856 857 858
      (let ((to (mh-get-header-field "To:"))
            (subject (mh-get-header-field "Subject:"))
            (cc (mh-get-header-field "Cc:")))
        (goto-char (point-min))
        (mh-goto-header-end 1)
        (or includep
            (not mh-reply-show-message-flag)
            (mh-in-show-buffer (show-buffer)
              (mh-display-msg message folder)))
        (mh-add-msgs-to-seq message 'answered t)
        (message "Composing a reply...done")
        (mh-compose-and-send-mail draft "" folder message to subject cc
                                  mh-note-repl "Replied:" config))
859 860
      (when (and (or (eq 'autosupercite mh-yank-behavior)
                     (eq 'autoattrib mh-yank-behavior))
Bill Wohler's avatar
Bill Wohler committed
861 862 863 864
                 (eq (mh-show-buffer-message-number) mh-sent-from-msg))
        (undo-boundary)
        (mh-yank-cur-msg))
      (mh-letter-mode-message))))
Richard M. Stallman's avatar
Richard M. Stallman committed
865

Bill Wohler's avatar
Bill Wohler committed
866
;;;###mh-autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
867
(defun mh-send (to cc subject)
868 869
  "Compose a message.

870 871
Your letter appears in an Emacs buffer whose mode is
MH-Letter (see `mh-letter-mode').
872

873 874 875 876
The arguments TO, CC, and SUBJECT can be used to prefill the
draft fields or suppress the prompts if `mh-compose-prompt-flag'
is on. They are also passed to the function set in the option
`mh-compose-letter-function'.
877 878 879

See also `mh-insert-x-mailer-flag' and `mh-letter-mode-hook'.

880 881 882
Outside of an MH-Folder buffer (`mh-folder-mode'), you must call
either \\[mh-smail] or \\[mh-smail-other-window] to compose a new
message."
Richard M. Stallman's avatar
Richard M. Stallman committed
883
  (interactive (list
Bill Wohler's avatar
Bill Wohler committed
884 885 886
                (mh-interactive-read-address "To: ")
                (mh-interactive-read-address "Cc: ")
                (mh-interactive-read-string "Subject: ")))
Richard M. Stallman's avatar
Richard M. Stallman committed
887 888 889 890
  (let ((config (current-window-configuration)))
    (delete-other-windows)
    (mh-send-sub to cc subject config)))

Bill Wohler's avatar
Bill Wohler committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908


;;; Support Routines

(defun mh-interactive-read-address (prompt)
  "Read an address.
If `mh-compose-prompt-flag' is non-nil, then read an address with
PROMPT.
Otherwise return the empty string."
  (if mh-compose-prompt-flag (mh-read-address prompt) ""))

(defun mh-interactive-read-string (prompt)
  "Read a string.
If `mh-compose-prompt-flag' is non-nil, then read a string with
PROMPT.
Otherwise return the empty string."
  (if mh-compose-prompt-flag (read-string prompt) ""))

Bill Wohler's avatar
Bill Wohler committed
909
;;;###mh-autoload
Bill Wohler's avatar
Bill Wohler committed
910 911
(defun mh-show-buffer-message-number (&optional buffer)
  "Message number of displayed message in corresponding show buffer.
912

Bill Wohler's avatar
Bill Wohler committed
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
Return nil if show buffer not displayed.
If in `mh-letter-mode', don't display the message number being replied
to, but rather the message number of the show buffer associated with
our originating folder buffer.
Optional argument BUFFER can be used to specify the buffer."
  (save-excursion
    (if buffer
        (set-buffer buffer))
    (cond ((eq major-mode 'mh-show-mode)
           (let ((number-start (mh-search-from-end ?/ buffer-file-name)))
             (string-to-number (substring buffer-file-name
                                          (1+ number-start)))))
          ((and (eq major-mode 'mh-folder-mode)
                mh-show-buffer
                (get-buffer mh-show-buffer))
           (mh-show-buffer-message-number mh-show-buffer))
          ((and (eq major-mode 'mh-letter-mode)
                mh-sent-from-folder
                (get-buffer mh-sent-from-folder))
           (mh-show-buffer-message-number mh-sent-from-folder))
          (t
           nil))))
Richard M. Stallman's avatar
Richard M. Stallman committed
935 936

(defun mh-send-sub (to cc subject config)
Bill Wohler's avatar
Bill Wohler committed
937 938 939
  "Do the real work of composing and sending a letter.
Expects the TO, CC, and SUBJECT fields as arguments.
CONFIG is the window configuration before sending mail."
Richard M. Stallman's avatar
Richard M. Stallman committed
940
  (let ((folder mh-current-folder)
Bill Wohler's avatar
Bill Wohler committed
941
        (msg-num (mh-get-msg-num nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
942 943
    (message "Composing a message...")
    (let ((draft (mh-read-draft
Bill Wohler's avatar
Bill Wohler committed
944
                  "message"
945
                  (mh-bare-components mh-comp-formfile)
946
                  t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
947 948 949
      (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
      (goto-char (point-max))
      (mh-compose-and-send-mail draft "" folder msg-num
Bill Wohler's avatar
Bill Wohler committed
950 951
                                to subject cc
                                nil nil config)
Bill Wohler's avatar
Bill Wohler committed
952 953
      (mh-letter-mode-message)
      (mh-letter-adjust-point))))
Richard M. Stallman's avatar
Richard M. Stallman committed
954

955 956 957 958
(defun mh-bare-components (formfile)
  "Generate a temporary, clean components file from FORMFILE.
Return the path to the temporary file."
  ;; Let comp(1) create the skeleton for us. This is particularly
959
  ;; important with nmh-1.5, because its default "components" needs
960 961 962 963 964 965
  ;; some processing before it can be used. Unfortunately, comp(1)
  ;; didn't have a -build option until later versions of nmh. So, to
  ;; avoid the possibility of clobbering an existing draft, create
  ;; a temporary directory and use it as the drafts folder. Then
  ;; copy the skeleton to a regular temp file, and return the
  ;; regular temp file.
966
  (let (new
967
        (temp-folder (make-temp-file
968 969 970 971
                      (concat mh-user-path "draftfolder.") t)))
    (mh-exec-cmd "comp" "-nowhatnowproc"
                 "-draftfolder" (format "+%s"
                                        (file-name-nondirectory temp-folder))
972 973
                 (if (stringp formfile)
                     (list "-form" formfile)))
974
    (setq new (make-temp-file "comp."))
975
    (rename-file (concat temp-folder "/" "1") new t)
976 977 978 979
    ;; The temp folder could contain various metadata files.  Rather
    ;; than trying to enumerate all the known files, just do a
    ;; recursive delete on the directory.
    (delete-directory temp-folder t)
980 981
    new))

Richard M. Stallman's avatar
Richard M. Stallman committed
982
(defun mh-read-draft (use initial-contents delete-contents-file)
Bill Wohler's avatar
Bill Wohler committed
983
  "Read draft file into a draft buffer and make that buffer the current one.
984 985 986

USE is a message used for prompting about the intended use of the
message.
Bill Wohler's avatar
Bill Wohler committed
987
INITIAL-CONTENTS is filename that is read into an empty buffer, or nil
988
if buffer should not be modified. Delete the initial-contents file if
Bill Wohler's avatar
Bill Wohler committed
989 990
DELETE-CONTENTS-FILE flag is set.
Returns the draft folder's name.
991 992 993
If the draft folder facility is enabled in ~/.mh_profile, a new buffer
is used each time and saved in the draft folder. The draft file can
then be reused."
Richard M. Stallman's avatar
Richard M. Stallman committed
994
  (cond (mh-draft-folder
Bill Wohler's avatar
Bill Wohler committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
         (let ((orig-default-dir default-directory)
               (draft-file-name (mh-new-draft-name)))
           (pop-to-buffer (generate-new-buffer
                           (format "draft-%s"
                                   (file-name-nondirectory draft-file-name))))
           (condition-case ()
               (insert-file-contents draft-file-name t)
             (file-error))
           (setq default-directory orig-default-dir)))
        (t
         (let ((draft-name (expand-file-name "draft" mh-user-path)))
           (pop-to-buffer "draft")      ; Create if necessary
           (if (buffer-modified-p)
               (if (y-or-n-p "Draft has been modified; kill anyway? ")
                   (set-buffer-modified-p nil)
                 (error "Draft preserved")))
           (setq buffer-file-name draft-name)
           (clear-visited-file-modtime)
           (unlock-buffer)
           (cond ((and (file-exists-p draft-name)
                       (not (equal draft-name initial-contents)))
                  (insert-file-contents draft-name)
                  (delete-file draft-name))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1018
  (cond ((and initial-contents
Bill Wohler's avatar
Bill Wohler committed
1019 1020 1021 1022 1023 1024 1025 1026 1027
              (or (zerop (buffer-size))
                  (if (y-or-n-p
                       (format "A draft exists.  Use for %s? " use))
                      (if mh-error-if-no-draft
                          (error "A prior draft exists"))
                    t)))
         (erase-buffer)
         (insert-file-contents initial-contents)
         (if delete-contents-file (delete-file initial-contents))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1028 1029
  (auto-save-mode 1)
  (if mh-draft-folder
Bill Wohler's avatar
Bill Wohler committed
1030
      (save-buffer))                    ; Do not reuse draft name
Richard M. Stallman's avatar
Richard M. Stallman committed
1031 1032 1033
  (buffer-name))

(defun mh-new-draft-name ()
Bill Wohler's avatar
Bill Wohler committed
1034
  "Return the pathname of folder for draft messages."
Richard M. Stallman's avatar
Richard M. Stallman committed
1035 1036 1037 1038 1039
  (save-excursion
    (mh-exec-cmd-quiet t "mhpath" mh-draft-folder "new")
    (buffer-substring (point-min) (1- (point-max)))))

(defun mh-insert-fields (&rest name-values)
Bill Wohler's avatar
Bill Wohler committed
1040 1041 1042
  "Insert the NAME-VALUES pairs in the current buffer.
If the field exists, append the value to it.
Do not insert any pairs whose value is the empty string."
Richard M. Stallman's avatar
Richard M. Stallman committed
1043 1044 1045
  (let ((case-fold-search t))
    (while name-values
      (let ((field-name (car name-values))
Bill Wohler's avatar
Bill Wohler committed
1046
            (value (car (cdr name-values))))
Bill Wohler's avatar
Bill Wohler committed
1047 1048
        (if (not (string-match "^.*:$" field-name))
            (setq field-name (concat field-name ":")))
1049 1050
        (cond ((or (null value)
                   (equal value ""))
Bill Wohler's avatar
Bill Wohler committed
1051 1052 1053 1054 1055 1056
               nil)
              ((mh-position-on-field field-name)
               (insert " " (or value "")))
              (t
               (insert field-name " " value "\n")))
        (setq name-values (cdr (cdr name-values)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1057

Bill Wohler's avatar
Bill Wohler committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
(defun mh-compose-and-send-mail (draft send-args
                                       sent-from-folder sent-from-msg
                                       to subject cc
                                       annotate-char annotate-field
                                       config)
  "Edit and compose a draft message in buffer DRAFT and send or save it.
SEND-ARGS is the argument passed to the send command.
SENT-FROM-FOLDER is buffer containing scan listing of current folder,
or nil if none exists.
SENT-FROM-MSG is the message number or sequence name or nil.
The TO, SUBJECT, and CC fields are passed to the
`mh-compose-letter-function'.
If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of
the message. In that case, the ANNOTATE-FIELD is used to build a
string for `mh-annotate-msg'.
CONFIG is the window configuration to restore after sending the
letter."
  (pop-to-buffer draft)
  (mh-letter-mode)
Karl Heuer's avatar
Karl Heuer committed
1077

Bill Wohler's avatar
Bill Wohler committed
1078 1079 1080 1081
  ;; Insert identity.
  (mh-insert-identity mh-identity-default t)
  (mh-identity-make-menu)
  (mh-identity-add-menu)
Richard M. Stallman's avatar
Richard M. Stallman committed
1082

1083 1084 1085
  ;; Cleanup possibly RFC2047 encoded subject header
  (mh-decode-message-subject)

Bill Wohler's avatar
Bill Wohler committed
1086 1087 1088
  ;; Insert extra fields.
  (mh-insert-x-mailer)
  (mh-insert-x-face)
Richard M. Stallman's avatar
Richard M. Stallman committed
1089

Bill Wohler's avatar
Bill Wohler committed
1090
  (mh-letter-hide-all-skipped-fields)
Bill Wohler's avatar
Bill Wohler committed
1091

Bill Wohler's avatar
Bill Wohler committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
  (setq mh-sent-from-folder sent-from-folder)
  (setq mh-sent-from-msg sent-from-msg)
  (setq mh-send-args send-args)
  (setq mh-annotate-char annotate-char)
  (setq mh-annotate-field annotate-field)
  (setq mh-previous-window-config config)
  (setq mode-line-buffer-identification (list "    {%b}"))
  (mh-logo-display)
  (mh-make-local-hook 'kill-buffer-hook)
  (add-hook 'kill-buffer-hook 'mh-tidy-draft-buffer nil t)
1102
  (run-hook-with-args 'mh-compose-letter-function to subject cc))
Bill Wohler's avatar
Bill Wohler committed
1103

Bill Wohler's avatar
Bill Wohler committed
1104
(defun mh-insert-x-mailer ()
Bill Wohler's avatar
Bill Wohler committed
1105 1106
  "Append an X-Mailer field to the header.
The versions of MH-E, Emacs, and MH are shown."
1107
  (or mh-variant-in-use (mh-variant-set mh-variant))
Bill Wohler's avatar
Bill Wohler committed
1108
  ;; Lazily initialize mh-x-mailer-string.
Bill Wohler's avatar
Bill Wohler committed
1109
  (when (and mh-insert-x-mailer-flag (null mh-x-mailer-string))
Bill Wohler's avatar
Bill Wohler committed
1110 1111 1112
    (setq mh-x-mailer-string
          (format "MH-E %s; %s; %sEmacs %s"
                  mh-version mh-variant-in-use
1113 1114
                  (if (featurep 'xemacs) "X" "GNU ")
                  (cond ((not (featurep 'xemacs))
1115 1116 1117
                         (string-match "[0-9]+\\.[0-9]+\\(\\.[0-9]+\\)?"
                                       emacs-version)
                         (match-string 0 emacs-version))
1118
                        ((string-match "[0-9.]*\\( +([ a-z]+[0-9]+)\\)?"
Bill Wohler's avatar
Bill Wohler committed
1119 1120 1121 1122
                                       emacs-version)
                         (match-string 0 emacs-version))
                        (t (format "%s.%s" emacs-major-version
                                   emacs-minor-version))))))
Bill Wohler's avatar
Bill Wohler committed
1123 1124
  ;; Insert X-Mailer, but only if it doesn't already exist.
  (save-excursion
Bill Wohler's avatar
Bill Wohler committed
1125 1126
    (when (and mh-insert-x-mailer-flag
               (null (mh-goto-header-field "X-Mailer")))
Bill Wohler's avatar
Bill Wohler committed
1127
      (mh-insert-fields "X-Mailer:" mh-x-mailer-string))))
Bill Wohler's avatar
Bill Wohler committed
1128

Bill Wohler's avatar
Bill Wohler committed
1129 1130 1131
(defun mh-insert-x-face ()
  "Append X-Face, Face or X-Image-URL field to header.
If the field already exists, this function does nothing."
1132 1133
  (when (and (stringp mh-x-face-file)
             (file-exists-p mh-x-face-file)
Bill Wohler's avatar
Bill Wohler committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
             (file-readable-p mh-x-face-file))
    (save-excursion
      (unless (or (mh-position-on-field "X-Face")
                  (mh-position-on-field "Face")
                  (mh-position-on-field "X-Image-URL"))
        (save-excursion
          (goto-char (+ (point) (cadr (insert-file-contents mh-x-face-file))))
          (if (not (looking-at "^"))
              (insert "\n")))
        (unless (looking-at "\\(X-Face\\|Face\\|X-Image-URL\\): ")
          (insert "X-Face: "))))))

(defun mh-tidy-draft-buffer ()
  "Run when a draft buffer is destroyed."
  (let ((buffer (get-buffer mh-recipients-buffer)))
    (if buffer
        (kill-buffer buffer))))

(defun mh-letter-mode-message ()
  "Display a help message for users of `mh-letter-mode'.
This should be the last function called when composing the draft."
  (message "%s" (substitute-command-keys
                 (concat "Type \\[mh-send-letter] to send message, "
                         "\\[mh-help] for help"))))

(defun mh-letter-adjust-point ()
  "Move cursor to first header field if are using the no prompt mode."
  (unless mh-compose-prompt-flag
    (goto-char (point-max))
    (mh-letter-next-header-field)))

Stephen Gildea's avatar
Stephen Gildea committed
1165 1166
(defun mh-annotate-msg (msg folder note &rest args)
  "Mark MSG in FOLDER with character NOTE and annotate message with ARGS.
1167
MSG can be a message number, a list of message numbers, or a sequence.
Stephen Gildea's avatar
Stephen Gildea committed
1168 1169 1170
The hook `mh-annotate-msg-hook' is run after annotating; see its
documentation for variables it can use."
  (apply 'mh-exec-cmd "anno" folder
Bill Wohler's avatar
Bill Wohler committed
1171 1172
         (if (listp msg) (append msg args) (cons msg args)))
  (save-excursion
Stephen Gildea's avatar
Stephen Gildea committed
1173 1174
    (cond ((get-buffer folder)          ; Buffer may be deleted
           (set-buffer folder)
Bill Wohler's avatar
Bill Wohler committed
1175 1176
           (mh-iterate-on-range nil msg
             (mh-notate nil note
Stephen Gildea's avatar
Stephen Gildea committed
1177 1178 1179 1180 1181
                        (+ mh-cmd-note mh-scan-field-destination-offset))))))
  (let ((mh-current-folder folder)
        ;; mh-annotate-list is a sequence name or a list of message numbers
        (mh-annotate-list (if (numberp msg) (list msg) msg)))
    (run-hooks 'mh-annotate-msg-hook)))
Bill Wohler's avatar
Bill Wohler committed
1182 1183 1184 1185 1186 1187 1188 1189

(defun mh-insert-header-separator ()
  "Insert `mh-mail-header-separator', if absent."
  (save-excursion
    (goto-char (point-min))
    (rfc822-goto-eoh)
    (if (looking-at "$")
        (insert mh-mail-header-separator))))
Bill Wohler's avatar
Bill Wohler committed
1190

Bill Wohler's avatar
Bill Wohler committed
1191 1192
;;;###mh-autoload
(defun mh-insert-auto-fields (&optional non-interactive)
1193
  "Insert custom fields if recipient is found in `mh-auto-fields-list'.
Bill Wohler's avatar
Bill Wohler committed
1194

1195 1196