3aaa8c25745fef4abfb23b32ef4f9acdaaf439b1
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: mail, news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; This mode provides mail-sending facilities from within Emacs.  It
29 ;; consists mainly of large chunks of code from the sendmail.el,
30 ;; gnus-msg.el and rnewspost.el files.
31
32 ;;; Code:
33
34 (eval-when-compile
35   (require 'cl))
36   
37 (require 'hashcash)
38 (require 'canlock)
39 (require 'mailheader)
40 (require 'gmm-utils)
41 (require 'nnheader)
42 ;; This is apparently necessary even though things are autoloaded.
43 ;; Because we dynamically bind mail-abbrev-mode-regexp, we'd better
44 ;; require mailabbrev here.
45 (if (featurep 'xemacs)
46     (require 'mail-abbrevs)
47   (require 'mailabbrev))
48 (require 'mail-parse)
49 (require 'mml)
50 (require 'rfc822)
51 (require 'ecomplete)
52
53 (autoload 'mailclient-send-it "mailclient") ;; Emacs 22 or contrib/
54
55 (defvar gnus-message-group-art)
56 (defvar gnus-list-identifiers) ; gnus-sum is required where necessary
57 (defvar rmail-enable-mime-composing)
58
59 (defgroup message '((user-mail-address custom-variable)
60                     (user-full-name custom-variable))
61   "Mail and news message composing."
62   :link '(custom-manual "(message)Top")
63   :group 'mail
64   :group 'news)
65
66 (put 'user-mail-address 'custom-type 'string)
67 (put 'user-full-name 'custom-type 'string)
68
69 (defgroup message-various nil
70   "Various Message Variables."
71   :link '(custom-manual "(message)Various Message Variables")
72   :group 'message)
73
74 (defgroup message-buffers nil
75   "Message Buffers."
76   :link '(custom-manual "(message)Message Buffers")
77   :group 'message)
78
79 (defgroup message-sending nil
80   "Message Sending."
81   :link '(custom-manual "(message)Sending Variables")
82   :group 'message)
83
84 (defgroup message-interface nil
85   "Message Interface."
86   :link '(custom-manual "(message)Interface")
87   :group 'message)
88
89 (defgroup message-forwarding nil
90   "Message Forwarding."
91   :link '(custom-manual "(message)Forwarding")
92   :group 'message-interface)
93
94 (defgroup message-insertion nil
95   "Message Insertion."
96   :link '(custom-manual "(message)Insertion")
97   :group 'message)
98
99 (defgroup message-headers nil
100   "Message Headers."
101   :link '(custom-manual "(message)Message Headers")
102   :group 'message)
103
104 (defgroup message-news nil
105   "Composing News Messages."
106   :group 'message)
107
108 (defgroup message-mail nil
109   "Composing Mail Messages."
110   :group 'message)
111
112 (defgroup message-faces nil
113   "Faces used for message composing."
114   :group 'message
115   :group 'faces)
116
117 (defcustom message-directory "~/Mail/"
118   "*Directory from which all other mail file variables are derived."
119   :group 'message-various
120   :type 'directory)
121
122 (defcustom message-max-buffers 10
123   "*How many buffers to keep before starting to kill them off."
124   :group 'message-buffers
125   :type 'integer)
126
127 (defcustom message-send-rename-function nil
128   "Function called to rename the buffer after sending it."
129   :group 'message-buffers
130   :type '(choice function (const nil)))
131
132 (defcustom message-fcc-handler-function 'message-output
133   "*A function called to save outgoing articles.
134 This function will be called with the name of the file to store the
135 article in.  The default function is `message-output' which saves in Unix
136 mailbox format."
137   :type '(radio (function-item message-output)
138                 (function :tag "Other"))
139   :group 'message-sending)
140
141 (defcustom message-fcc-externalize-attachments nil
142   "If non-nil, attachments are included as external parts in Fcc copies."
143   :version "22.1"
144   :type 'boolean
145   :group 'message-sending)
146
147 (defcustom message-courtesy-message
148   "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
149   "*This is inserted at the start of a mailed copy of a posted message.
150 If the string contains the format spec \"%s\", the Newsgroups
151 the article has been posted to will be inserted there.
152 If this variable is nil, no such courtesy message will be added."
153   :group 'message-sending
154   :type '(radio string (const nil)))
155
156 (defcustom message-ignored-bounced-headers
157   "^\\(Received\\|Return-Path\\|Delivered-To\\):"
158   "*Regexp that matches headers to be removed in resent bounced mail."
159   :group 'message-interface
160   :type 'regexp)
161
162 (defcustom message-from-style 'default
163   "*Specifies how \"From\" headers look.
164
165 If nil, they contain just the return address like:
166         king@grassland.com
167 If `parens', they look like:
168         king@grassland.com (Elvis Parsley)
169 If `angles', they look like:
170         Elvis Parsley <king@grassland.com>
171
172 Otherwise, most addresses look like `angles', but they look like
173 `parens' if `angles' would need quoting and `parens' would not."
174   :type '(choice (const :tag "simple" nil)
175                  (const parens)
176                  (const angles)
177                  (const default))
178   :group 'message-headers)
179
180 (defcustom message-insert-canlock t
181   "Whether to insert a Cancel-Lock header in news postings."
182   :version "22.1"
183   :group 'message-headers
184   :type 'boolean)
185
186 (defcustom message-syntax-checks
187   (if message-insert-canlock '((sender . disabled)) nil)
188   ;; Guess this one shouldn't be easy to customize...
189   "*Controls what syntax checks should not be performed on outgoing posts.
190 To disable checking of long signatures, for instance, add
191  `(signature . disabled)' to this list.
192
193 Don't touch this variable unless you really know what you're doing.
194
195 Checks include `approved', `bogus-recipient', `continuation-headers',
196 `control-chars', `empty', `existing-newsgroups', `from', `illegible-text',
197 `invisible-text', `long-header-lines', `long-lines', `message-id',
198 `multiple-headers', `new-text', `newsgroups', `quoting-style',
199 `repeated-newsgroups', `reply-to', `sender', `sendsys', `shoot',
200 `shorten-followup-to', `signature', `size', `subject', `subject-cmsg'
201 and `valid-newsgroups'."
202   :group 'message-news
203   :type '(repeat sexp))                 ; Fixme: improve this
204
205 (defcustom message-required-headers '((optional . References)
206                                       From)
207   "*Headers to be generated or prompted for when sending a message.
208 Also see `message-required-news-headers' and
209 `message-required-mail-headers'."
210   :version "22.1"
211   :group 'message-news
212   :group 'message-headers
213   :link '(custom-manual "(message)Message Headers")
214   :type '(repeat sexp))
215
216 (defcustom message-draft-headers '(References From Date)
217   "*Headers to be generated when saving a draft message."
218   :version "22.1"
219   :group 'message-news
220   :group 'message-headers
221   :link '(custom-manual "(message)Message Headers")
222   :type '(repeat sexp))
223
224 (defcustom message-required-news-headers
225   '(From Newsgroups Subject Date Message-ID
226          (optional . Organization)
227          (optional . User-Agent))
228   "*Headers to be generated or prompted for when posting an article.
229 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
230 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
231 User-Agent are optional.  If you don't want message to insert some
232 header, remove it from this list."
233   :group 'message-news
234   :group 'message-headers
235   :link '(custom-manual "(message)Message Headers")
236   :type '(repeat sexp))
237
238 (defcustom message-required-mail-headers
239   '(From Subject Date (optional . In-Reply-To) Message-ID
240          (optional . User-Agent))
241   "*Headers to be generated or prompted for when mailing a message.
242 It is recommended that From, Date, To, Subject and Message-ID be
243 included.  Organization and User-Agent are optional."
244   :group 'message-mail
245   :group 'message-headers
246   :link '(custom-manual "(message)Message Headers")
247   :type '(repeat sexp))
248
249 (defcustom message-deletable-headers '(Message-ID Date Lines)
250   "Headers to be deleted if they already exist and were generated by message previously."
251   :group 'message-headers
252   :link '(custom-manual "(message)Message Headers")
253   :type 'sexp)
254
255 (defcustom message-ignored-news-headers
256   "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
257   "*Regexp of headers to be removed unconditionally before posting."
258   :group 'message-news
259   :group 'message-headers
260   :link '(custom-manual "(message)Message Headers")
261   :type '(repeat :value-to-internal (lambda (widget value)
262                                       (custom-split-regexp-maybe value))
263                  :match (lambda (widget value)
264                           (or (stringp value)
265                               (widget-editable-list-match widget value)))
266                  regexp))
267
268 (defcustom message-ignored-mail-headers
269   "^[GF]cc:\\|^Resent-Fcc:\\|^Xref:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
270   "*Regexp of headers to be removed unconditionally before mailing."
271   :group 'message-mail
272   :group 'message-headers
273   :link '(custom-manual "(message)Mail Headers")
274   :type 'regexp)
275
276 (defcustom message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|^X-Trace:\\|^X-ID:\\|^X-Complaints-To:\\|Return-Path:\\|^Supersedes:\\|^NNTP-Posting-Date:\\|^X-Trace:\\|^X-Complaints-To:\\|^Cancel-Lock:\\|^Cancel-Key:\\|^X-Hashcash:\\|^X-Payment:\\|^Approved:"
277   "*Header lines matching this regexp will be deleted before posting.
278 It's best to delete old Path and Date headers before posting to avoid
279 any confusion."
280   :group 'message-interface
281   :link '(custom-manual "(message)Superseding")
282   :type '(repeat :value-to-internal (lambda (widget value)
283                                       (custom-split-regexp-maybe value))
284                  :match (lambda (widget value)
285                           (or (stringp value)
286                               (widget-editable-list-match widget value)))
287                  regexp))
288
289 (defcustom message-subject-re-regexp
290   "^[ \t]*\\([Rr][Ee]\\(\\[[0-9]*\\]\\)*:[ \t]*\\)*[ \t]*"
291   "*Regexp matching \"Re: \" in the subject line."
292   :group 'message-various
293   :link '(custom-manual "(message)Message Headers")
294   :type 'regexp)
295
296 ;;; Start of variables adopted from `message-utils.el'.
297
298 (defcustom message-subject-trailing-was-query 'ask
299   "*What to do with trailing \"(was: <old subject>)\" in subject lines.
300 If nil, leave the subject unchanged.  If it is the symbol `ask', query
301 the user what do do.  In this case, the subject is matched against
302 `message-subject-trailing-was-ask-regexp'.  If
303 `message-subject-trailing-was-query' is t, always strip the trailing
304 old subject.  In this case, `message-subject-trailing-was-regexp' is
305 used."
306   :version "22.1"
307   :type '(choice (const :tag "never" nil)
308                  (const :tag "always strip" t)
309                  (const ask))
310   :link '(custom-manual "(message)Message Headers")
311   :group 'message-various)
312
313 (defcustom message-subject-trailing-was-ask-regexp
314   "[ \t]*\\([[(]+[Ww][Aa][Ss][ \t]*.*[\])]+\\)"
315   "*Regexp matching \"(was: <old subject>)\" in the subject line.
316
317 The function `message-strip-subject-trailing-was' uses this regexp if
318 `message-subject-trailing-was-query' is set to the symbol `ask'.  If
319 the variable is t instead of `ask', use
320 `message-subject-trailing-was-regexp' instead.
321
322 It is okay to create some false positives here, as the user is asked."
323   :version "22.1"
324   :group 'message-various
325   :link '(custom-manual "(message)Message Headers")
326   :type 'regexp)
327
328 (defcustom message-subject-trailing-was-regexp
329   "[ \t]*\\((*[Ww][Aa][Ss]:[ \t]*.*)\\)"
330   "*Regexp matching \"(was: <old subject>)\" in the subject line.
331
332 If `message-subject-trailing-was-query' is set to t, the subject is
333 matched against `message-subject-trailing-was-regexp' in
334 `message-strip-subject-trailing-was'.  You should use a regexp creating very
335 few false positives here."
336   :version "22.1"
337   :group 'message-various
338   :link '(custom-manual "(message)Message Headers")
339   :type 'regexp)
340
341 ;;; marking inserted text
342
343 (defcustom message-mark-insert-begin
344   "--8<---------------cut here---------------start------------->8---\n"
345   "How to mark the beginning of some inserted text."
346   :version "22.1"
347   :type 'string
348   :link '(custom-manual "(message)Insertion Variables")
349   :group 'message-various)
350
351 (defcustom message-mark-insert-end
352   "--8<---------------cut here---------------end--------------->8---\n"
353   "How to mark the end of some inserted text."
354   :version "22.1"
355   :type 'string
356   :link '(custom-manual "(message)Insertion Variables")
357   :group 'message-various)
358
359 (defcustom message-archive-header "X-No-Archive: Yes\n"
360   "Header to insert when you don't want your article to be archived.
361 Archives \(such as groups.google.com\) respect this header."
362   :version "22.1"
363   :type 'string
364   :link '(custom-manual "(message)Header Commands")
365   :group 'message-various)
366
367 (defcustom message-archive-note
368   "X-No-Archive: Yes - save http://groups.google.com/"
369   "Note to insert why you wouldn't want this posting archived.
370 If nil, don't insert any text in the body."
371   :version "22.1"
372   :type '(radio string (const nil))
373   :link '(custom-manual "(message)Header Commands")
374   :group 'message-various)
375
376 ;;; Crossposts and Followups
377 ;; inspired by JoH-followup-to by Jochem Huhman <joh  at gmx.de>
378 ;; new suggestions by R. Weikusat <rw at another.de>
379
380 (defvar message-cross-post-old-target nil
381   "Old target for cross-posts or follow-ups.")
382 (make-variable-buffer-local 'message-cross-post-old-target)
383
384 (defcustom message-cross-post-default t
385   "When non-nil `message-cross-post-followup-to' will perform a crosspost.
386 If nil, `message-cross-post-followup-to' will only do a followup.  Note that
387 you can explicitly override this setting by calling
388 `message-cross-post-followup-to' with a prefix."
389   :version "22.1"
390   :type 'boolean
391   :group 'message-various)
392
393 (defcustom message-cross-post-note "Crosspost & Followup-To: "
394   "Note to insert before signature to notify of cross-post and follow-up."
395   :version "22.1"
396   :type 'string
397   :group 'message-various)
398
399 (defcustom message-followup-to-note "Followup-To: "
400   "Note to insert before signature to notify of follow-up only."
401   :version "22.1"
402   :type 'string
403   :group 'message-various)
404
405 (defcustom message-cross-post-note-function 'message-cross-post-insert-note
406   "Function to use to insert note about Crosspost or Followup-To.
407 The function will be called with four arguments.  The function should not only
408 insert a note, but also ensure old notes are deleted.  See the documentation
409 for `message-cross-post-insert-note'."
410   :version "22.1"
411   :type 'function
412   :group 'message-various)
413
414 ;;; End of variables adopted from `message-utils.el'.
415
416 (defcustom message-signature-separator "^-- *$"
417   "Regexp matching the signature separator."
418   :type 'regexp
419   :link '(custom-manual "(message)Various Message Variables")
420   :group 'message-various)
421
422 (defcustom message-elide-ellipsis "\n[...]\n\n"
423   "*The string which is inserted for elided text."
424   :type 'string
425   :link '(custom-manual "(message)Various Commands")
426   :group 'message-various)
427
428 (defcustom message-interactive t
429   "Non-nil means when sending a message wait for and display errors.
430 nil means let mailer mail back a message to report errors."
431   :group 'message-sending
432   :group 'message-mail
433   :link '(custom-manual "(message)Sending Variables")
434   :type 'boolean)
435
436 (defcustom message-generate-new-buffers 'unique
437   "*Say whether to create a new message buffer to compose a message.
438 Valid values include:
439
440 nil
441   Generate the buffer name in the Message way (e.g., *mail*, *news*,
442   *mail to whom*, *news on group*, etc.) and continue editing in the
443   existing buffer of that name.  If there is no such buffer, it will
444   be newly created.
445
446 `unique' or t
447   Create the new buffer with the name generated in the Message way.
448
449 `unsent'
450   Similar to `unique' but the buffer name begins with \"*unsent \".
451
452 `standard'
453   Similar to nil but the buffer name is simpler like *mail message*.
454
455 function
456   If this is a function, call that function with three parameters:
457   The type, the To address and the group name (any of these may be nil).
458   The function should return the new buffer name."
459   :group 'message-buffers
460   :link '(custom-manual "(message)Message Buffers")
461   :type '(choice (const nil)
462                  (sexp :tag "unique" :format "unique\n" :value unique
463                        :match (lambda (widget value) (memq value '(unique t))))
464                  (const unsent)
465                  (const standard)
466                  (function :format "\n    %{%t%}: %v")))
467
468 (defcustom message-kill-buffer-on-exit nil
469   "*Non-nil means that the message buffer will be killed after sending a message."
470   :group 'message-buffers
471   :link '(custom-manual "(message)Message Buffers")
472   :type 'boolean)
473
474 (defcustom message-kill-buffer-query t
475   "*Non-nil means that killing a modified message buffer has to be confirmed.
476 This is used by `message-kill-buffer'."
477   :version "23.0" ;; No Gnus
478   :group 'message-buffers
479   :type 'boolean)
480
481 (defvar gnus-local-organization)
482 (defcustom message-user-organization
483   (or (and (boundp 'gnus-local-organization)
484            (stringp gnus-local-organization)
485            gnus-local-organization)
486       (getenv "ORGANIZATION")
487       t)
488   "*String to be used as an Organization header.
489 If t, use `message-user-organization-file'."
490   :group 'message-headers
491   :type '(choice string
492                  (const :tag "consult file" t)))
493
494 (defcustom message-user-organization-file
495   (let (orgfile)
496     (dolist (f (list "/etc/organization"
497                      "/etc/news/organization"
498                      "/usr/lib/news/organization"))
499       (when (file-readable-p f)
500         (setq orgfile f)))
501     orgfile)
502   "*Local news organization file."
503   :type 'file
504   :link '(custom-manual "(message)News Headers")
505   :group 'message-headers)
506
507 (defcustom message-make-forward-subject-function
508   #'message-forward-subject-name-subject
509   "*List of functions called to generate subject headers for forwarded messages.
510 The subject generated by the previous function is passed into each
511 successive function.
512
513 The provided functions are:
514
515 * `message-forward-subject-author-subject' Source of article (author or
516       newsgroup), in brackets followed by the subject
517 * `message-forward-subject-name-subject' Source of article (name of author
518       or newsgroup), in brackets followed by the subject
519 * `message-forward-subject-fwd' Subject of article with 'Fwd:' prepended
520       to it."
521   :group 'message-forwarding
522   :link '(custom-manual "(message)Forwarding")
523   :type '(radio (function-item message-forward-subject-author-subject)
524                 (function-item message-forward-subject-fwd)
525                 (function-item message-forward-subject-name-subject)
526                 (repeat :tag "List of functions" function)))
527
528 (defcustom message-forward-as-mime t
529   "*Non-nil means forward messages as an inline/rfc822 MIME section.
530 Otherwise, directly inline the old message in the forwarded message."
531   :version "21.1"
532   :group 'message-forwarding
533   :link '(custom-manual "(message)Forwarding")
534   :type 'boolean)
535
536 (defcustom message-forward-show-mml 'best
537   "*Non-nil means show forwarded messages as MML (decoded from MIME).
538 Otherwise, forwarded messages are unchanged.
539 Can also be the symbol `best' to indicate that MML should be
540 used, except when it is a bad idea to use MML.  One example where
541 it is a bad idea is when forwarding a signed or encrypted
542 message, because converting MIME to MML would invalidate the
543 digital signature."
544   :version "21.1"
545   :group 'message-forwarding
546   :type '(choice (const :tag "use MML" t)
547                  (const :tag "don't use MML " nil)
548                  (const :tag "use MML when appropriate" best)))
549
550 (defcustom message-forward-before-signature t
551   "*Non-nil means put forwarded message before signature, else after."
552   :group 'message-forwarding
553   :type 'boolean)
554
555 (defcustom message-wash-forwarded-subjects nil
556   "*Non-nil means try to remove as much cruft as possible from the subject.
557 Done before generating the new subject of a forward."
558   :group 'message-forwarding
559   :link '(custom-manual "(message)Forwarding")
560   :type 'boolean)
561
562 (defcustom message-ignored-resent-headers "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:\\|^>?From "
563   "*All headers that match this regexp will be deleted when resending a message."
564   :group 'message-interface
565   :link '(custom-manual "(message)Resending")
566   :type '(repeat :value-to-internal (lambda (widget value)
567                                       (custom-split-regexp-maybe value))
568                  :match (lambda (widget value)
569                           (or (stringp value)
570                               (widget-editable-list-match widget value)))
571                  regexp))
572
573 (defcustom message-forward-ignored-headers "^Content-Transfer-Encoding:\\|^X-Gnus"
574   "*All headers that match this regexp will be deleted when forwarding a message."
575   :version "21.1"
576   :group 'message-forwarding
577   :type '(repeat :value-to-internal (lambda (widget value)
578                                       (custom-split-regexp-maybe value))
579                  :match (lambda (widget value)
580                           (or (stringp value)
581                               (widget-editable-list-match widget value)))
582                  regexp))
583
584 (defcustom message-ignored-cited-headers "."
585   "*Delete these headers from the messages you yank."
586   :group 'message-insertion
587   :link '(custom-manual "(message)Insertion Variables")
588   :type 'regexp)
589
590 (defcustom message-cite-prefix-regexp
591   (if (string-match "[[:digit:]]" "1")
592       ;; Support POSIX?  XEmacs 21.5.27 doesn't.
593       "\\([ \t]*[_.[:word:]]+>+\\|[ \t]*[]>|}]\\)+"
594     ;; ?-, ?_ or ?. MUST NOT be in syntax entry w.
595     (let (non-word-constituents)
596       (with-syntax-table text-mode-syntax-table
597         (setq non-word-constituents
598               (concat
599                (if (string-match "\\w" "_")  "" "_")
600                (if (string-match "\\w" ".")  "" "."))))
601       (if (equal non-word-constituents "")
602           "\\([ \t]*\\(\\w\\)+>+\\|[ \t]*[]>|}]\\)+"
603         (concat "\\([ \t]*\\(\\w\\|["
604                 non-word-constituents
605                 "]\\)+>+\\|[ \t]*[]>|}]\\)+"))))
606   "*Regexp matching the longest possible citation prefix on a line."
607   :version "22.1"
608   :group 'message-insertion
609   :link '(custom-manual "(message)Insertion Variables")
610   :type 'regexp
611   :set (lambda (symbol value)
612          (prog1
613              (custom-set-default symbol value)
614            (if (boundp 'gnus-message-cite-prefix-regexp)
615                (setq gnus-message-cite-prefix-regexp
616                      (concat "^\\(?:" value "\\)"))))))
617
618 (defcustom message-cancel-message "I am canceling my own article.\n"
619   "Message to be inserted in the cancel message."
620   :group 'message-interface
621   :link '(custom-manual "(message)Canceling News")
622   :type 'string)
623
624 (defvar smtpmail-default-smtp-server)
625
626 (defun message-send-mail-function ()
627   "Return suitable value for the variable `message-send-mail-function'."
628   (cond ((and (require 'sendmail)
629               (boundp 'sendmail-program)
630               sendmail-program
631               (executable-find sendmail-program))
632          'message-send-mail-with-sendmail)
633         ((and (locate-library "smtpmail")
634               (require 'smtpmail)
635               smtpmail-default-smtp-server)
636          'message-smtpmail-send-it)
637         ((locate-library "mailclient")
638          'message-send-mail-with-mailclient)
639         (t
640          (lambda ()
641            (error "Don't know how to send mail.  Please customize `message-send-mail-function'")))))
642
643 ;; Useful to set in site-init.el
644 (defcustom message-send-mail-function (message-send-mail-function)
645   "Function to call to send the current buffer as mail.
646 The headers should be delimited by a line whose contents match the
647 variable `mail-header-separator'.
648
649 Valid values include `message-send-mail-with-sendmail'
650 `message-send-mail-with-mh', `message-send-mail-with-qmail',
651 `message-smtpmail-send-it', `smtpmail-send-it',
652 `feedmail-send-it' and `message-send-mail-with-mailclient'.  The
653 default is system dependent.
654
655 See also `send-mail-function'."
656   :type '(radio (function-item message-send-mail-with-sendmail)
657                 (function-item message-send-mail-with-mh)
658                 (function-item message-send-mail-with-qmail)
659                 (function-item message-smtpmail-send-it)
660                 (function-item smtpmail-send-it)
661                 (function-item feedmail-send-it)
662                 (function :tag "Other")
663                 (function-item message-send-mail-with-mailclient
664                                :tag "Use Mailclient package")
665                 (function :tag "Other"))
666   :group 'message-sending
667   :initialize 'custom-initialize-default
668   :link '(custom-manual "(message)Mail Variables")
669   :group 'message-mail)
670
671 (defcustom message-send-news-function 'message-send-news
672   "Function to call to send the current buffer as news.
673 The headers should be delimited by a line whose contents match the
674 variable `mail-header-separator'."
675   :group 'message-sending
676   :group 'message-news
677   :link '(custom-manual "(message)News Variables")
678   :type 'function)
679
680 (defcustom message-reply-to-function nil
681   "If non-nil, function that should return a list of headers.
682 This function should pick out addresses from the To, Cc, and From headers
683 and respond with new To and Cc headers."
684   :group 'message-interface
685   :link '(custom-manual "(message)Reply")
686   :type '(choice function (const nil)))
687
688 (defcustom message-wide-reply-to-function nil
689   "If non-nil, function that should return a list of headers.
690 This function should pick out addresses from the To, Cc, and From headers
691 and respond with new To and Cc headers."
692   :group 'message-interface
693   :link '(custom-manual "(message)Wide Reply")
694   :type '(choice function (const nil)))
695
696 (defcustom message-followup-to-function nil
697   "If non-nil, function that should return a list of headers.
698 This function should pick out addresses from the To, Cc, and From headers
699 and respond with new To and Cc headers."
700   :group 'message-interface
701   :link '(custom-manual "(message)Followup")
702   :type '(choice function (const nil)))
703
704 (defcustom message-extra-wide-headers nil
705   "If non-nil, a list of additional address headers.
706 These are used when composing a wide reply."
707   :group 'message-sending
708   :type '(repeat string))
709
710 (defcustom message-use-followup-to 'ask
711   "*Specifies what to do with Followup-To header.
712 If nil, always ignore the header.  If it is t, use its value, but
713 query before using the \"poster\" value.  If it is the symbol `ask',
714 always query the user whether to use the value.  If it is the symbol
715 `use', always use the value."
716   :group 'message-interface
717   :link '(custom-manual "(message)Followup")
718   :type '(choice (const :tag "ignore" nil)
719                  (const :tag "use & query" t)
720                  (const use)
721                  (const ask)))
722
723 (defcustom message-use-mail-followup-to 'use
724   "*Specifies what to do with Mail-Followup-To header.
725 If nil, always ignore the header.  If it is the symbol `ask', always
726 query the user whether to use the value.  If it is the symbol `use',
727 always use the value."
728   :version "22.1"
729   :group 'message-interface
730   :link '(custom-manual "(message)Mailing Lists")
731   :type '(choice (const :tag "ignore" nil)
732                  (const use)
733                  (const ask)))
734
735 (defcustom message-subscribed-address-functions nil
736   "*Specifies functions for determining list subscription.
737 If nil, do not attempt to determine list subscription with functions.
738 If non-nil, this variable contains a list of functions which return
739 regular expressions to match lists.  These functions can be used in
740 conjunction with `message-subscribed-regexps' and
741 `message-subscribed-addresses'."
742   :version "22.1"
743   :group 'message-interface
744   :link '(custom-manual "(message)Mailing Lists")
745   :type '(repeat sexp))
746
747 (defcustom message-subscribed-address-file nil
748   "*A file containing addresses the user is subscribed to.
749 If nil, do not look at any files to determine list subscriptions.  If
750 non-nil, each line of this file should be a mailing list address."
751   :version "22.1"
752   :group 'message-interface
753   :link '(custom-manual "(message)Mailing Lists")
754   :type '(radio file (const nil)))
755
756 (defcustom message-subscribed-addresses nil
757   "*Specifies a list of addresses the user is subscribed to.
758 If nil, do not use any predefined list subscriptions.  This list of
759 addresses can be used in conjunction with
760 `message-subscribed-address-functions' and `message-subscribed-regexps'."
761   :version "22.1"
762   :group 'message-interface
763   :link '(custom-manual "(message)Mailing Lists")
764   :type '(repeat string))
765
766 (defcustom message-subscribed-regexps nil
767   "*Specifies a list of addresses the user is subscribed to.
768 If nil, do not use any predefined list subscriptions.  This list of
769 regular expressions can be used in conjunction with
770 `message-subscribed-address-functions' and `message-subscribed-addresses'."
771   :version "22.1"
772   :group 'message-interface
773   :link '(custom-manual "(message)Mailing Lists")
774   :type '(repeat regexp))
775
776 (defcustom message-allow-no-recipients 'ask
777   "Specifies what to do when there are no recipients other than Gcc/Fcc.
778 If it is the symbol `always', the posting is allowed.  If it is the
779 symbol `never', the posting is not allowed.  If it is the symbol
780 `ask', you are prompted."
781   :version "22.1"
782   :group 'message-interface
783   :link '(custom-manual "(message)Message Headers")
784   :type '(choice (const always)
785                  (const never)
786                  (const ask)))
787
788 (defcustom message-sendmail-f-is-evil nil
789   "*Non-nil means don't add \"-f username\" to the sendmail command line.
790 Doing so would be even more evil than leaving it out."
791   :group 'message-sending
792   :link '(custom-manual "(message)Mail Variables")
793   :type 'boolean)
794
795 (defcustom message-sendmail-envelope-from nil
796   "*Envelope-from when sending mail with sendmail.
797 If this is nil, use `user-mail-address'.  If it is the symbol
798 `header', use the From: header of the message."
799   :version "22.1"
800   :type '(choice (string :tag "From name")
801                  (const :tag "Use From: header from message" header)
802                  (const :tag "Use `user-mail-address'" nil))
803   :link '(custom-manual "(message)Mail Variables")
804   :group 'message-sending)
805
806 (defcustom message-sendmail-extra-arguments nil
807   "Additional arguments to `sendmail-program'."
808   ;; E.g. '("-a" "account") for msmtp
809   :version "23.0" ;; No Gnus
810   :type '(repeat string)
811   ;; :link '(custom-manual "(message)Mail Variables")
812   :group 'message-sending)
813
814 ;; qmail-related stuff
815 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
816   "Location of the qmail-inject program."
817   :group 'message-sending
818   :link '(custom-manual "(message)Mail Variables")
819   :type 'file)
820
821 (defcustom message-qmail-inject-args nil
822   "Arguments passed to qmail-inject programs.
823 This should be a list of strings, one string for each argument.  It
824 may also be a function.
825
826 For e.g., if you wish to set the envelope sender address so that bounces
827 go to the right place or to deal with listserv's usage of that address, you
828 might set this variable to '(\"-f\" \"you@some.where\")."
829   :group 'message-sending
830   :link '(custom-manual "(message)Mail Variables")
831   :type '(choice (function)
832                  (repeat string)))
833
834 (defvar gnus-post-method)
835 (defvar gnus-select-method)
836 (defcustom message-post-method
837   (cond ((and (boundp 'gnus-post-method)
838               (listp gnus-post-method)
839               gnus-post-method)
840          gnus-post-method)
841         ((boundp 'gnus-select-method)
842          gnus-select-method)
843         (t '(nnspool "")))
844   "*Method used to post news.
845 Note that when posting from inside Gnus, for instance, this
846 variable isn't used."
847   :group 'message-news
848   :group 'message-sending
849   ;; This should be the `gnus-select-method' widget, but that might
850   ;; create a dependence to `gnus.el'.
851   :type 'sexp)
852
853 ;; FIXME: This should be a temporary workaround until someone implements a
854 ;; proper solution.  If a crash happens while replying, the auto-save file
855 ;; will *not* have a `References:' header if `message-generate-headers-first'
856 ;; is nil.  See: http://article.gmane.org/gmane.emacs.gnus.general/51138
857 (defcustom message-generate-headers-first '(references)
858   "Which headers should be generated before starting to compose a message.
859 If t, generate all required headers.  This can also be a list of headers to
860 generate.  The variables `message-required-news-headers' and
861 `message-required-mail-headers' specify which headers to generate.
862
863 Note that the variable `message-deletable-headers' specifies headers which
864 are to be deleted and then re-generated before sending, so this variable
865 will not have a visible effect for those headers."
866   :group 'message-headers
867   :link '(custom-manual "(message)Message Headers")
868   :type '(choice (const :tag "None" nil)
869                  (const :tag "References" '(references))
870                  (const :tag "All" t)
871                  (repeat (sexp :tag "Header"))))
872
873 (defcustom message-fill-column 72
874   "Column beyond which automatic line-wrapping should happen.
875 Local value for message buffers.  If non-nil, also turn on
876 auto-fill in message buffers."
877   :group 'message-various
878   ;; :link '(custom-manual "(message)Message Headers")
879   :type '(choice (const :tag "Don't turn on auto fill" nil)
880                  (integer)))
881
882 (defcustom message-setup-hook nil
883   "Normal hook, run each time a new outgoing message is initialized.
884 The function `message-setup' runs this hook."
885   :group 'message-various
886   :link '(custom-manual "(message)Various Message Variables")
887   :type 'hook)
888
889 (defcustom message-cancel-hook nil
890   "Hook run when cancelling articles."
891   :group 'message-various
892   :link '(custom-manual "(message)Various Message Variables")
893   :type 'hook)
894
895 (defcustom message-signature-setup-hook nil
896   "Normal hook, run each time a new outgoing message is initialized.
897 It is run after the headers have been inserted and before
898 the signature is inserted."
899   :group 'message-various
900   :link '(custom-manual "(message)Various Message Variables")
901   :type 'hook)
902
903 (defcustom message-mode-hook nil
904   "Hook run in message mode buffers."
905   :group 'message-various
906   :type 'hook)
907
908 (defcustom message-header-hook nil
909   "Hook run in a message mode buffer narrowed to the headers."
910   :group 'message-various
911   :type 'hook)
912
913 (defcustom message-header-setup-hook nil
914   "Hook called narrowed to the headers when setting up a message buffer."
915   :group 'message-various
916   :link '(custom-manual "(message)Various Message Variables")
917   :type 'hook)
918
919 (defcustom message-minibuffer-local-map
920   (let ((map (make-sparse-keymap 'message-minibuffer-local-map)))
921     (set-keymap-parent map minibuffer-local-map)
922     map)
923   "Keymap for `message-read-from-minibuffer'."
924   :version "22.1"
925   :group 'message-various)
926
927 (defcustom message-citation-line-function 'message-insert-citation-line
928   "*Function called to insert the \"Whomever writes:\" line.
929
930 Predefined functions include `message-insert-citation-line' and
931 `message-insert-formatted-citation-line' (see the variable
932 `message-citation-line-format').
933
934 Note that Gnus provides a feature where the reader can click on
935 `writes:' to hide the cited text.  If you change this line too much,
936 people who read your message will have to change their Gnus
937 configuration.  See the variable `gnus-cite-attribution-suffix'."
938   :type '(choice
939           (function-item :tag "plain" message-insert-citation-line)
940           (function-item :tag "formatted" message-insert-formatted-citation-line)
941           (function :tag "Other"))
942   :link '(custom-manual "(message)Insertion Variables")
943   :group 'message-insertion)
944
945 (defcustom message-citation-line-format "On %a, %b %d %Y, %N wrote:\n"
946   "Format of the \"Whomever writes:\" line.
947
948 The string is formatted using `format-spec'.  The following
949 constructs are replaced:
950
951   %f   The full From, e.g. \"John Doe <john.doe@example.invalid>\".
952   %n   The mail address, e.g. \"john.doe@example.invalid\".
953   %N   The real name if present, e.g.: \"John Doe\", else fall
954        back to the mail address.
955   %F   The first name if present, e.g.: \"John\".
956   %L   The last name if present, e.g.: \"Doe\".
957
958 All other format specifiers are passed to `format-time-string'
959 which is called using the date from the article your replying to.
960 Extracting the first (%F) and last name (%L) is done
961 heuristically, so you should always check it yourself.
962
963 Please also read the note in the documentation of
964 `message-citation-line-function'."
965   :type '(choice (const :tag "Plain" "%f writes:")
966                  (const :tag "Include date" "On %a, %b %d %Y, %n wrote:")
967                  string)
968   :link '(custom-manual "(message)Insertion Variables")
969   :version "23.0" ;; No Gnus
970   :group 'message-insertion)
971
972 (defcustom message-yank-prefix "> "
973   "*Prefix inserted on the lines of yanked messages.
974 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
975 See also `message-yank-cited-prefix' and `message-yank-empty-prefix'."
976   :type 'string
977   :link '(custom-manual "(message)Insertion Variables")
978   :group 'message-insertion)
979
980 (defcustom message-yank-cited-prefix ">"
981   "*Prefix inserted on cited lines of yanked messages.
982 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
983 See also `message-yank-prefix' and `message-yank-empty-prefix'."
984   :version "22.1"
985   :type 'string
986   :link '(custom-manual "(message)Insertion Variables")
987   :group 'message-insertion)
988
989 (defcustom message-yank-empty-prefix ">"
990   "*Prefix inserted on empty lines of yanked messages.
991 See also `message-yank-prefix' and `message-yank-cited-prefix'."
992   :version "22.1"
993   :type 'string
994   :link '(custom-manual "(message)Insertion Variables")
995   :group 'message-insertion)
996
997 (defcustom message-indentation-spaces 3
998   "*Number of spaces to insert at the beginning of each cited line.
999 Used by `message-yank-original' via `message-yank-cite'."
1000   :group 'message-insertion
1001   :link '(custom-manual "(message)Insertion Variables")
1002   :type 'integer)
1003
1004 (defcustom message-cite-function 'message-cite-original
1005   "*Function for citing an original message.
1006 Predefined functions include `message-cite-original' and
1007 `message-cite-original-without-signature'.
1008 Note that these functions use `mail-citation-hook' if that is non-nil."
1009   :type '(radio (function-item message-cite-original)
1010                 (function-item message-cite-original-without-signature)
1011                 (function-item sc-cite-original)
1012                 (function :tag "Other"))
1013   :link '(custom-manual "(message)Insertion Variables")
1014   :group 'message-insertion)
1015
1016 (defcustom message-indent-citation-function 'message-indent-citation
1017   "*Function for modifying a citation just inserted in the mail buffer.
1018 This can also be a list of functions.  Each function can find the
1019 citation between (point) and (mark t).  And each function should leave
1020 point and mark around the citation text as modified."
1021   :type 'function
1022   :link '(custom-manual "(message)Insertion Variables")
1023   :group 'message-insertion)
1024
1025 (defcustom message-signature t
1026   "*String to be inserted at the end of the message buffer.
1027 If t, the `message-signature-file' file will be inserted instead.
1028 If a function, the result from the function will be used instead.
1029 If a form, the result from the form will be used instead."
1030   :type 'sexp
1031   :link '(custom-manual "(message)Insertion Variables")
1032   :group 'message-insertion)
1033
1034 (defcustom message-signature-file "~/.signature"
1035   "*Name of file containing the text inserted at end of message buffer.
1036 Ignored if the named file doesn't exist.
1037 If nil, don't insert a signature.
1038 If a path is specified, the value of `message-signature-directory' is ignored,
1039 even if set."
1040   :type '(choice file (const :tags "None" nil))
1041   :link '(custom-manual "(message)Insertion Variables")
1042   :group 'message-insertion)
1043
1044 (defcustom message-signature-directory nil
1045   "*Name of directory containing signature files.
1046 Comes in handy if you have many such files, handled via posting styles for
1047 instance.
1048 If nil, `message-signature-file' is expected to specify the directory if
1049 needed."
1050   :type '(choice string (const :tags "None" nil))
1051   :link '(custom-manual "(message)Insertion Variables")
1052   :group 'message-insertion)
1053
1054 (defcustom message-signature-insert-empty-line t
1055   "*If non-nil, insert an empty line before the signature separator."
1056   :version "22.1"
1057   :type 'boolean
1058   :link '(custom-manual "(message)Insertion Variables")
1059   :group 'message-insertion)
1060
1061 (defcustom message-distribution-function nil
1062   "*Function called to return a Distribution header."
1063   :group 'message-news
1064   :group 'message-headers
1065   :link '(custom-manual "(message)News Headers")
1066   :type '(choice function (const nil)))
1067
1068 (defcustom message-expires 14
1069   "Number of days before your article expires."
1070   :group 'message-news
1071   :group 'message-headers
1072   :link '(custom-manual "(message)News Headers")
1073   :type 'integer)
1074
1075 (defcustom message-user-path nil
1076   "If nil, use the NNTP server name in the Path header.
1077 If stringp, use this; if non-nil, use no host name (user name only)."
1078   :group 'message-news
1079   :group 'message-headers
1080   :link '(custom-manual "(message)News Headers")
1081   :type '(choice (const :tag "nntp" nil)
1082                  (string :tag "name")
1083                  (sexp :tag "none" :format "%t" t)))
1084
1085 (defvar message-reply-buffer nil)
1086 (defvar message-reply-headers nil
1087   "The headers of the current replied article.
1088 It is a vector of the following headers:
1089 \[number subject from date id references chars lines xref extra].")
1090 (defvar message-newsreader nil)
1091 (defvar message-mailer nil)
1092 (defvar message-sent-message-via nil)
1093 (defvar message-checksum nil)
1094 (defvar message-send-actions nil
1095   "A list of actions to be performed upon successful sending of a message.")
1096 (defvar message-exit-actions nil
1097   "A list of actions to be performed upon exiting after sending a message.")
1098 (defvar message-kill-actions nil
1099   "A list of actions to be performed before killing a message buffer.")
1100 (defvar message-postpone-actions nil
1101   "A list of actions to be performed after postponing a message.")
1102
1103 (define-widget 'message-header-lines 'text
1104   "All header lines must be LFD terminated."
1105   :format "%{%t%}:%n%v"
1106   :valid-regexp "^\\'"
1107   :error "All header lines must be newline terminated")
1108
1109 (defcustom message-default-headers ""
1110   "*A string containing header lines to be inserted in outgoing messages.
1111 It is inserted before you edit the message, so you can edit or delete
1112 these lines."
1113   :group 'message-headers
1114   :link '(custom-manual "(message)Message Headers")
1115   :type 'message-header-lines)
1116
1117 (defcustom message-default-mail-headers ""
1118   "*A string of header lines to be inserted in outgoing mails."
1119   :group 'message-headers
1120   :group 'message-mail
1121   :link '(custom-manual "(message)Mail Headers")
1122   :type 'message-header-lines)
1123
1124 (defcustom message-default-news-headers ""
1125   "*A string of header lines to be inserted in outgoing news articles."
1126   :group 'message-headers
1127   :group 'message-news
1128   :link '(custom-manual "(message)News Headers")
1129   :type 'message-header-lines)
1130
1131 ;; Note: could use /usr/ucb/mail instead of sendmail;
1132 ;; options -t, and -v if not interactive.
1133 (defcustom message-mailer-swallows-blank-line
1134   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
1135                          system-configuration)
1136            (file-readable-p "/etc/sendmail.cf")
1137            (let ((buffer (get-buffer-create " *temp*")))
1138              (unwind-protect
1139                  (with-current-buffer buffer
1140                    (insert-file-contents "/etc/sendmail.cf")
1141                    (goto-char (point-min))
1142                    (let ((case-fold-search nil))
1143                      (re-search-forward "^OR\\>" nil t)))
1144                (kill-buffer buffer))))
1145       ;; According to RFC822, "The field-name must be composed of printable
1146       ;; ASCII characters (i. e., characters that have decimal values between
1147       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
1148       ;; space, or colon.
1149       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
1150   "*Set this non-nil if the system's mailer runs the header and body together.
1151 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
1152 The value should be an expression to test whether the problem will
1153 actually occur."
1154   :group 'message-sending
1155   :link '(custom-manual "(message)Mail Variables")
1156   :type 'sexp)
1157
1158 ;;;###autoload
1159 (define-mail-user-agent 'message-user-agent
1160   'message-mail 'message-send-and-exit
1161   'message-kill-buffer 'message-send-hook)
1162
1163 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
1164   "If non-nil, delete the deletable headers before feeding to mh.")
1165
1166 (defvar message-send-method-alist
1167   '((news message-news-p message-send-via-news)
1168     (mail message-mail-p message-send-via-mail))
1169   "Alist of ways to send outgoing messages.
1170 Each element has the form
1171
1172   \(TYPE PREDICATE FUNCTION)
1173
1174 where TYPE is a symbol that names the method; PREDICATE is a function
1175 called without any parameters to determine whether the message is
1176 a message of type TYPE; and FUNCTION is a function to be called if
1177 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
1178 the prefix.")
1179
1180 (defcustom message-mail-alias-type 'abbrev
1181   "*What alias expansion type to use in Message buffers.
1182 The default is `abbrev', which uses mailabbrev.  `ecomplete' uses
1183 an electric completion mode.  nil switches mail aliases off.
1184 This can also be a list of values."
1185   :group 'message
1186   :link '(custom-manual "(message)Mail Aliases")
1187   :type '(choice (const :tag "Use Mailabbrev" abbrev)
1188                  (const :tag "Use ecomplete" ecomplete)
1189                  (const :tag "No expansion" nil)))
1190
1191 (defcustom message-self-insert-commands '(self-insert-command)
1192   "List of `self-insert-command's used to trigger ecomplete.
1193 When one of those commands is invoked to enter a character in To or Cc
1194 header, ecomplete will suggest the candidates of recipients (see also
1195 `message-mail-alias-type').  If you use some tool to enter non-ASCII
1196 text and it replaces `self-insert-command' with the other command, e.g.
1197 `egg-self-insert-command', you may want to add it to this list."
1198   :group 'message-various
1199   :type '(repeat function))
1200
1201 (defcustom message-auto-save-directory
1202   (file-name-as-directory (nnheader-concat message-directory "drafts"))
1203   "*Directory where Message auto-saves buffers if Gnus isn't running.
1204 If nil, Message won't auto-save."
1205   :group 'message-buffers
1206   :link '(custom-manual "(message)Various Message Variables")
1207   :type '(choice directory (const :tag "Don't auto-save" nil)))
1208
1209 (defcustom message-default-charset
1210   (and (not (mm-multibyte-p)) 'iso-8859-1)
1211   "Default charset used in non-MULE Emacsen.
1212 If nil, you might be asked to input the charset."
1213   :version "21.1"
1214   :group 'message
1215   :link '(custom-manual "(message)Various Message Variables")
1216   :type 'symbol)
1217
1218 (defcustom message-dont-reply-to-names
1219   (and (boundp 'rmail-dont-reply-to-names) rmail-dont-reply-to-names)
1220   "*Addresses to prune when doing wide replies.
1221 This can be a regexp or a list of regexps.  Also, a value of nil means
1222 exclude your own user name only."
1223   :version "21.1"
1224   :group 'message
1225   :link '(custom-manual "(message)Wide Reply")
1226   :type '(choice (const :tag "Yourself" nil)
1227                  regexp
1228                  (repeat :tag "Regexp List" regexp)))
1229
1230 (defsubst message-dont-reply-to-names ()
1231   (gmm-regexp-concat message-dont-reply-to-names))
1232
1233 (defvar message-shoot-gnksa-feet nil
1234   "*A list of GNKSA feet you are allowed to shoot.
1235 Gnus gives you all the opportunity you could possibly want for
1236 shooting yourself in the foot.  Also, Gnus allows you to shoot the
1237 feet of Good Net-Keeping Seal of Approval.  The following are foot
1238 candidates:
1239 `empty-article'     Allow you to post an empty article;
1240 `quoted-text-only'  Allow you to post quoted text only;
1241 `multiple-copies'   Allow you to post multiple copies;
1242 `cancel-messages'   Allow you to cancel or supersede messages from
1243                     your other email addresses.")
1244
1245 (defsubst message-gnksa-enable-p (feature)
1246   (or (not (listp message-shoot-gnksa-feet))
1247       (memq feature message-shoot-gnksa-feet)))
1248
1249 (defcustom message-hidden-headers '("^References:" "^Face:" "^X-Face:"
1250                                     "^X-Draft-From:")
1251   "Regexp of headers to be hidden when composing new messages.
1252 This can also be a list of regexps to match headers.  Or a list
1253 starting with `not' and followed by regexps."
1254   :version "22.1"
1255   :group 'message
1256   :link '(custom-manual "(message)Message Headers")
1257   :type '(choice
1258           :format "%{%t%}: %[Value Type%] %v"
1259           (regexp :menu-tag "regexp" :format "regexp\n%t: %v")
1260           (repeat :menu-tag "(regexp ...)" :format "(regexp ...)\n%v%i"
1261                   (regexp :format "%t: %v"))
1262           (cons :menu-tag "(not regexp ...)" :format "(not regexp ...)\n%v"
1263                 (const not)
1264                 (repeat :format "%v%i"
1265                         (regexp :format "%t: %v")))))
1266
1267 (defcustom message-cite-articles-with-x-no-archive t
1268   "If non-nil, cite text from articles that has X-No-Archive set."
1269   :group 'message
1270   :type 'boolean)
1271
1272 ;;; Internal variables.
1273 ;;; Well, not really internal.
1274
1275 (defvar message-mode-syntax-table
1276   (let ((table (copy-syntax-table text-mode-syntax-table)))
1277     (modify-syntax-entry ?% ". " table)
1278     (modify-syntax-entry ?> ". " table)
1279     (modify-syntax-entry ?< ". " table)
1280     table)
1281   "Syntax table used while in Message mode.")
1282
1283 (defface message-header-to
1284   '((((class color)
1285       (background dark))
1286      (:foreground "DarkOliveGreen1" :bold t))
1287     (((class color)
1288       (background light))
1289      (:foreground "MidnightBlue" :bold t))
1290     (t
1291      (:bold t :italic t)))
1292   "Face used for displaying From headers."
1293   :group 'message-faces)
1294 ;; backward-compatibility alias
1295 (put 'message-header-to-face 'face-alias 'message-header-to)
1296
1297 (defface message-header-cc
1298   '((((class color)
1299       (background dark))
1300      (:foreground "chartreuse1" :bold t))
1301     (((class color)
1302       (background light))
1303      (:foreground "MidnightBlue"))
1304     (t
1305      (:bold t)))
1306   "Face used for displaying Cc headers."
1307   :group 'message-faces)
1308 ;; backward-compatibility alias
1309 (put 'message-header-cc-face 'face-alias 'message-header-cc)
1310
1311 (defface message-header-subject
1312   '((((class color)
1313       (background dark))
1314      (:foreground "OliveDrab1"))
1315     (((class color)
1316       (background light))
1317      (:foreground "navy blue" :bold t))
1318     (t
1319      (:bold t)))
1320   "Face used for displaying subject headers."
1321   :group 'message-faces)
1322 ;; backward-compatibility alias
1323 (put 'message-header-subject-face 'face-alias 'message-header-subject)
1324
1325 (defface message-header-newsgroups
1326   '((((class color)
1327       (background dark))
1328      (:foreground "yellow" :bold t :italic t))
1329     (((class color)
1330       (background light))
1331      (:foreground "blue4" :bold t :italic t))
1332     (t
1333      (:bold t :italic t)))
1334   "Face used for displaying newsgroups headers."
1335   :group 'message-faces)
1336 ;; backward-compatibility alias
1337 (put 'message-header-newsgroups-face 'face-alias 'message-header-newsgroups)
1338
1339 (defface message-header-other
1340   '((((class color)
1341       (background dark))
1342      (:foreground "VioletRed1"))
1343     (((class color)
1344       (background light))
1345      (:foreground "steel blue"))
1346     (t
1347      (:bold t :italic t)))
1348   "Face used for displaying newsgroups headers."
1349   :group 'message-faces)
1350 ;; backward-compatibility alias
1351 (put 'message-header-other-face 'face-alias 'message-header-other)
1352
1353 (defface message-header-name
1354   '((((class color)
1355       (background dark))
1356      (:foreground "green"))
1357     (((class color)
1358       (background light))
1359      (:foreground "cornflower blue"))
1360     (t
1361      (:bold t)))
1362   "Face used for displaying header names."
1363   :group 'message-faces)
1364 ;; backward-compatibility alias
1365 (put 'message-header-name-face 'face-alias 'message-header-name)
1366
1367 (defface message-header-xheader
1368   '((((class color)
1369       (background dark))
1370      (:foreground "DeepSkyBlue1"))
1371     (((class color)
1372       (background light))
1373      (:foreground "blue"))
1374     (t
1375      (:bold t)))
1376   "Face used for displaying X-Header headers."
1377   :group 'message-faces)
1378 ;; backward-compatibility alias
1379 (put 'message-header-xheader-face 'face-alias 'message-header-xheader)
1380
1381 (defface message-separator
1382   '((((class color)
1383       (background dark))
1384      (:foreground "LightSkyBlue1"))
1385     (((class color)
1386       (background light))
1387      (:foreground "brown"))
1388     (t
1389      (:bold t)))
1390   "Face used for displaying the separator."
1391   :group 'message-faces)
1392 ;; backward-compatibility alias
1393 (put 'message-separator-face 'face-alias 'message-separator)
1394
1395 (defface message-cited-text
1396   '((((class color)
1397       (background dark))
1398      (:foreground "LightPink1"))
1399     (((class color)
1400       (background light))
1401      (:foreground "red"))
1402     (t
1403      (:bold t)))
1404   "Face used for displaying cited text names."
1405   :group 'message-faces)
1406 ;; backward-compatibility alias
1407 (put 'message-cited-text-face 'face-alias 'message-cited-text)
1408
1409 (defface message-mml
1410   '((((class color)
1411       (background dark))
1412      (:foreground "MediumSpringGreen"))
1413     (((class color)
1414       (background light))
1415      (:foreground "ForestGreen"))
1416     (t
1417      (:bold t)))
1418   "Face used for displaying MML."
1419   :group 'message-faces)
1420 ;; backward-compatibility alias
1421 (put 'message-mml-face 'face-alias 'message-mml)
1422
1423 (defun message-font-lock-make-header-matcher (regexp)
1424   (let ((form
1425          `(lambda (limit)
1426             (let ((start (point)))
1427               (save-restriction
1428                 (widen)
1429                 (goto-char (point-min))
1430                 (if (re-search-forward
1431                      (concat "^" (regexp-quote mail-header-separator) "$")
1432                      nil t)
1433                     (setq limit (min limit (match-beginning 0))))
1434                 (goto-char start))
1435               (and (< start limit)
1436                    (re-search-forward ,regexp limit t))))))
1437     (if (featurep 'bytecomp)
1438         (byte-compile form)
1439       form)))
1440
1441 (defvar message-font-lock-keywords
1442   (let ((content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)\n?"))
1443     `((,(message-font-lock-make-header-matcher
1444          (concat "^\\([Tt]o:\\)" content))
1445        (1 'message-header-name)
1446        (2 'message-header-to nil t))
1447       (,(message-font-lock-make-header-matcher
1448          (concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content))
1449        (1 'message-header-name)
1450        (2 'message-header-cc nil t))
1451       (,(message-font-lock-make-header-matcher
1452          (concat "^\\([Ss]ubject:\\)" content))
1453        (1 'message-header-name)
1454        (2 'message-header-subject nil t))
1455       (,(message-font-lock-make-header-matcher
1456          (concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content))
1457        (1 'message-header-name)
1458        (2 'message-header-newsgroups nil t))
1459       (,(message-font-lock-make-header-matcher
1460          (concat "^\\(X-[A-Za-z0-9-]+:\\|In-Reply-To:\\)" content))
1461        (1 'message-header-name)
1462        (2 'message-header-xheader))
1463       (,(message-font-lock-make-header-matcher
1464          (concat "^\\([A-Z][^: \n\t]+:\\)" content))
1465        (1 'message-header-name)
1466        (2 'message-header-other nil t))
1467       ,@(if (and mail-header-separator
1468                  (not (equal mail-header-separator "")))
1469             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
1470                1 'message-separator))
1471           nil)
1472       ((lambda (limit)
1473          (re-search-forward (concat "^\\("
1474                                     message-cite-prefix-regexp
1475                                     "\\).*")
1476                             limit t))
1477        (0 'message-cited-text))
1478       ("<#/?\\(multipart\\|part\\|external\\|mml\\|secure\\)[^>]*>"
1479        (0 'message-mml))))
1480   "Additional expressions to highlight in Message mode.")
1481
1482
1483 ;; XEmacs does it like this.  For Emacs, we have to set the
1484 ;; `font-lock-defaults' buffer-local variable.
1485 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
1486
1487 (defvar message-face-alist
1488   '((bold . message-bold-region)
1489     (underline . underline-region)
1490     (default . (lambda (b e)
1491                  (message-unbold-region b e)
1492                  (ununderline-region b e))))
1493   "Alist of mail and news faces for facemenu.
1494 The cdr of each entry is a function for applying the face to a region.")
1495
1496 (defcustom message-send-hook nil
1497   "Hook run before sending messages.
1498 This hook is run quite early when sending."
1499   :group 'message-various
1500   :options '(ispell-message)
1501   :link '(custom-manual "(message)Various Message Variables")
1502   :type 'hook)
1503
1504 (defcustom message-send-mail-hook nil
1505   "Hook run before sending mail messages.
1506 This hook is run very late -- just before the message is sent as
1507 mail."
1508   :group 'message-various
1509   :link '(custom-manual "(message)Various Message Variables")
1510   :type 'hook)
1511
1512 (defcustom message-send-news-hook nil
1513   "Hook run before sending news messages.
1514 This hook is run very late -- just before the message is sent as
1515 news."
1516   :group 'message-various
1517   :link '(custom-manual "(message)Various Message Variables")
1518   :type 'hook)
1519
1520 (defcustom message-sent-hook nil
1521   "Hook run after sending messages."
1522   :group 'message-various
1523   :type 'hook)
1524
1525 (defvar message-send-coding-system 'binary
1526   "Coding system to encode outgoing mail.")
1527
1528 (defvar message-draft-coding-system
1529   mm-auto-save-coding-system
1530   "*Coding system to compose mail.
1531 If you'd like to make it possible to share draft files between XEmacs
1532 and Emacs, you may use `iso-2022-7bit' for this value at your own risk.
1533 Note that the coding-system `iso-2022-7bit' isn't suitable to all data.")
1534
1535 (defcustom message-send-mail-partially-limit 1000000
1536   "The limitation of messages sent as message/partial.
1537 The lower bound of message size in characters, beyond which the message
1538 should be sent in several parts.  If it is nil, the size is unlimited."
1539   :version "21.1"
1540   :group 'message-buffers
1541   :link '(custom-manual "(message)Mail Variables")
1542   :type '(choice (const :tag "unlimited" nil)
1543                  (integer 1000000)))
1544
1545 (defcustom message-alternative-emails nil
1546   "*Regexp matching alternative email addresses.
1547 The first address in the To, Cc or From headers of the original
1548 article matching this variable is used as the From field of
1549 outgoing messages.
1550
1551 This variable has precedence over posting styles and anything that runs
1552 off `message-setup-hook'."
1553   :group 'message-headers
1554   :link '(custom-manual "(message)Message Headers")
1555   :type '(choice (const :tag "Always use primary" nil)
1556                  regexp))
1557
1558 (defcustom message-hierarchical-addresses nil
1559   "A list of hierarchical mail address definitions.
1560
1561 Inside each entry, the first address is the \"top\" address, and
1562 subsequent addresses are subaddresses; this is used to indicate that
1563 mail sent to the first address will automatically be delivered to the
1564 subaddresses.  So if the first address appears in the recipient list
1565 for a message, the subaddresses will be removed (if present) before
1566 the mail is sent.  All addresses in this structure should be
1567 downcased."
1568   :version "22.1"
1569   :group 'message-headers
1570   :type '(repeat (repeat string)))
1571
1572 (defcustom message-mail-user-agent nil
1573   "Like `mail-user-agent'.
1574 Except if it is nil, use Gnus native MUA; if it is t, use
1575 `mail-user-agent'."
1576   :version "22.1"
1577   :type '(radio (const :tag "Gnus native"
1578                        :format "%t\n"
1579                        nil)
1580                 (const :tag "`mail-user-agent'"
1581                        :format "%t\n"
1582                        t)
1583                 (function-item :tag "Default Emacs mail"
1584                                :format "%t\n"
1585                                sendmail-user-agent)
1586                 (function-item :tag "Emacs interface to MH"
1587                                :format "%t\n"
1588                                mh-e-user-agent)
1589                 (function :tag "Other"))
1590   :version "21.1"
1591   :group 'message)
1592
1593 (defcustom message-wide-reply-confirm-recipients nil
1594   "Whether to confirm a wide reply to multiple email recipients.
1595 If this variable is nil, don't ask whether to reply to all recipients.
1596 If this variable is non-nil, pose the question \"Reply to all
1597 recipients?\" before a wide reply to multiple recipients.  If the user
1598 answers yes, reply to all recipients as usual.  If the user answers
1599 no, only reply back to the author."
1600   :version "22.1"
1601   :group 'message-headers
1602   :link '(custom-manual "(message)Wide Reply")
1603   :type 'boolean)
1604
1605 (defcustom message-user-fqdn nil
1606   "*Domain part of Message-Ids."
1607   :version "22.1"
1608   :group 'message-headers
1609   :link '(custom-manual "(message)News Headers")
1610   :type '(radio (const :format "%v  " nil)
1611                 (string :format "FQDN: %v")))
1612
1613 (defcustom message-use-idna (and (condition-case nil (require 'idna)
1614                                    (file-error))
1615                                  (mm-coding-system-p 'utf-8)
1616                                  (executable-find idna-program)
1617                                  (string= (idna-to-ascii "räksmörgÃ¥s")
1618                                           "xn--rksmrgs-5wao1o")
1619                                  t)
1620   "Whether to encode non-ASCII in domain names into ASCII according to IDNA.
1621 GNU Libidn, and in particular the elisp package \"idna.el\" and
1622 the external program \"idn\", must be installed for this
1623 functionality to work."
1624   :version "22.1"
1625   :group 'message-headers
1626   :link '(custom-manual "(message)IDNA")
1627   :type '(choice (const :tag "Ask" ask)
1628                  (const :tag "Never" nil)
1629                  (const :tag "Always" t)))
1630
1631 (defcustom message-generate-hashcash (if (executable-find "hashcash") t)
1632   "*Whether to generate X-Hashcash: headers.
1633 If t, always generate hashcash headers.  If `opportunistic',
1634 only generate hashcash headers if it can be done without the user
1635 waiting (i.e., only asynchronously).
1636
1637 You must have the \"hashcash\" binary installed, see `hashcash-path'."
1638   :group 'message-headers
1639   :link '(custom-manual "(message)Mail Headers")
1640   :type '(choice (const :tag "Always" t)
1641                  (const :tag "Never" nil)
1642                  (const :tag "Opportunistic" opportunistic)))
1643
1644 ;;; Internal variables.
1645
1646 (defvar message-sending-message "Sending...")
1647 (defvar message-buffer-list nil)
1648 (defvar message-this-is-news nil)
1649 (defvar message-this-is-mail nil)
1650 (defvar message-draft-article nil)
1651 (defvar message-mime-part nil)
1652 (defvar message-posting-charset nil)
1653 (defvar message-inserted-headers nil)
1654
1655 ;; Byte-compiler warning
1656 (defvar gnus-active-hashtb)
1657 (defvar gnus-read-active-file)
1658
1659 ;;; Regexp matching the delimiter of messages in UNIX mail format
1660 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
1661 ;;; of rmail.el's rmail-unix-mail-delimiter.
1662 (defvar message-unix-mail-delimiter
1663   (let ((time-zone-regexp
1664          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
1665                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
1666                  "\\|"
1667                  "\\) *")))
1668     (concat
1669      "From "
1670
1671      ;; Many things can happen to an RFC 822 mailbox before it is put into
1672      ;; a `From' line.  The leading phrase can be stripped, e.g.
1673      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
1674      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
1675      ;; can be removed, e.g.
1676      ;;         From: joe@y.z (Joe      K
1677      ;;                 User)
1678      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
1679      ;;         From: Joe User
1680      ;;                 <joe@y.z>
1681      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
1682      ;; The mailbox can be removed or be replaced by white space, e.g.
1683      ;;         From: "Joe User"{space}{tab}
1684      ;;                 <joe@y.z>
1685      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
1686      ;; where {space} and {tab} represent the Ascii space and tab characters.
1687      ;; We want to match the results of any of these manglings.
1688      ;; The following regexp rejects names whose first characters are
1689      ;; obviously bogus, but after that anything goes.
1690      "\\([^\0-\b\n-\r\^?].*\\)?"
1691
1692      ;; The time the message was sent.
1693      "\\([^\0-\r \^?]+\\) +"            ; day of the week
1694      "\\([^\0-\r \^?]+\\) +"            ; month
1695      "\\([0-3]?[0-9]\\) +"              ; day of month
1696      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
1697
1698      ;; Perhaps a time zone, specified by an abbreviation, or by a
1699      ;; numeric offset.
1700      time-zone-regexp
1701
1702      ;; The year.
1703      " \\([0-9][0-9]+\\) *"
1704
1705      ;; On some systems the time zone can appear after the year, too.
1706      time-zone-regexp
1707
1708      ;; Old uucp cruft.
1709      "\\(remote from .*\\)?"
1710
1711      "\n"))
1712   "Regexp matching the delimiter of messages in UNIX mail format.")
1713
1714 (defvar message-unsent-separator
1715   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1716           "^ *---+ +Returned message +---+ *$\\|"
1717           "^Start of returned message$\\|"
1718           "^ *---+ +Original message +---+ *$\\|"
1719           "^ *--+ +begin message +--+ *$\\|"
1720           "^ *---+ +Original message follows +---+ *$\\|"
1721           "^ *---+ +Undelivered message follows +---+ *$\\|"
1722           "^|? *---+ +Message text follows: +---+ *|?$")
1723   "A regexp that matches the separator before the text of a failed message.")
1724
1725 (defvar message-field-fillers
1726   '((To message-fill-field-address)
1727     (Cc message-fill-field-address)
1728     (From message-fill-field-address))
1729   "Alist of header names/filler functions.")
1730
1731 (defvar message-header-format-alist
1732   `((From)
1733     (Newsgroups)
1734     (To)
1735     (Cc)
1736     (Subject)
1737     (In-Reply-To)
1738     (Fcc)
1739     (Bcc)
1740     (Date)
1741     (Organization)
1742     (Distribution)
1743     (Lines)
1744     (Expires)
1745     (Message-ID)
1746     (References . message-shorten-references)
1747     (User-Agent))
1748   "Alist used for formatting headers.")
1749
1750 (defvar message-options nil
1751   "Some saved answers when sending message.")
1752
1753 (defvar message-send-mail-real-function nil
1754   "Internal send mail function.")
1755
1756 (defvar message-bogus-system-names "^localhost\\.\\|\\.local$"
1757   "The regexp of bogus system names.")
1758
1759 (defcustom message-valid-fqdn-regexp
1760   (concat "[a-z0-9][-.a-z0-9]+\\." ;; [hostname.subdomain.]domain.
1761           ;; valid TLDs:
1762           "\\([a-z][a-z]\\|" ;; two letter country TDLs
1763           "aero\\|arpa\\|bitnet\\|biz\\|bofh\\|"
1764           "cat\\|com\\|coop\\|edu\\|gov\\|"
1765           "info\\|int\\|jobs\\|"
1766           "mil\\|mobi\\|museum\\|name\\|net\\|"
1767           "org\\|pro\\|travel\\|uucp\\)")
1768   ;; http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
1769   ;; http://en.wikipedia.org/wiki/GTLD
1770   ;; `in the process of being approved': .asia .post .tel .sex
1771   ;; "dead" nato bitnet uucp
1772   "Regular expression that matches a valid FQDN."
1773   ;; see also: gnus-button-valid-fqdn-regexp
1774   :version "22.1"
1775   :group 'message-headers
1776   :type 'regexp)
1777
1778 (eval-and-compile
1779   (autoload 'gnus-alive-p "gnus-util")
1780   (autoload 'gnus-delay-article "gnus-delay")
1781   (autoload 'gnus-extract-address-components "gnus-util")
1782   (autoload 'gnus-find-method-for-group "gnus")
1783   (autoload 'gnus-group-decoded-name "gnus-group")
1784   (autoload 'gnus-group-name-charset "gnus-group")
1785   (autoload 'gnus-group-name-decode "gnus-group")
1786   (autoload 'gnus-groups-from-server "gnus")
1787   (autoload 'gnus-make-local-hook "gnus-util")
1788   (autoload 'gnus-open-server "gnus-int")
1789   (autoload 'gnus-output-to-mail "gnus-util")
1790   (autoload 'gnus-output-to-rmail "gnus-util")
1791   (autoload 'gnus-request-post "gnus-int")
1792   (autoload 'gnus-select-frame-set-input-focus "gnus-util")
1793   (autoload 'gnus-server-string "gnus")
1794   (autoload 'idna-to-ascii "idna")
1795   (autoload 'message-setup-toolbar "messagexmas")
1796   (autoload 'mh-new-draft-name "mh-comp")
1797   (autoload 'mh-send-letter "mh-comp")
1798   (autoload 'nndraft-request-associate-buffer "nndraft")
1799   (autoload 'nndraft-request-expire-articles "nndraft")
1800   (autoload 'nnvirtual-find-group-art "nnvirtual")
1801   (autoload 'rmail-dont-reply-to "mail-utils")
1802   (autoload 'rmail-msg-is-pruned "rmail")
1803   (autoload 'rmail-msg-restore-non-pruned-header "rmail")
1804   (autoload 'rmail-output "rmailout"))
1805
1806 \f
1807
1808 ;;;
1809 ;;; Utility functions.
1810 ;;;
1811
1812 (defmacro message-y-or-n-p (question show &rest text)
1813   "Ask QUESTION, displaying remaining args in a temporary buffer if SHOW."
1814   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1815
1816 (defmacro message-delete-line (&optional n)
1817   "Delete the current line (and the next N lines)."
1818   `(delete-region (progn (beginning-of-line) (point))
1819                   (progn (forward-line ,(or n 1)) (point))))
1820
1821 (defun message-mark-active-p ()
1822   "Non-nil means the mark and region are currently active in this buffer."
1823   mark-active)
1824
1825 (defun message-unquote-tokens (elems)
1826   "Remove double quotes (\") from strings in list ELEMS."
1827   (mapcar (lambda (item)
1828             (while (string-match "^\\(.*\\)\"\\(.*\\)$" item)
1829               (setq item (concat (match-string 1 item)
1830                                  (match-string 2 item))))
1831             item)
1832           elems))
1833
1834 (defun message-tokenize-header (header &optional separator)
1835   "Split HEADER into a list of header elements.
1836 SEPARATOR is a string of characters to be used as separators.  \",\"
1837 is used by default."
1838   (if (not header)
1839       nil
1840     (let ((regexp (format "[%s]+" (or separator ",")))
1841           (first t)
1842           beg quoted elems paren)
1843       (with-temp-buffer
1844         (mm-enable-multibyte)
1845         (setq beg (point-min))
1846         (insert header)
1847         (goto-char (point-min))
1848         (while (not (eobp))
1849           (if first
1850               (setq first nil)
1851             (forward-char 1))
1852           (cond ((and (> (point) beg)
1853                       (or (eobp)
1854                           (and (looking-at regexp)
1855                                (not quoted)
1856                                (not paren))))
1857                  (push (buffer-substring beg (point)) elems)
1858                  (setq beg (match-end 0)))
1859                 ((eq (char-after) ?\")
1860                  (setq quoted (not quoted)))
1861                 ((and (eq (char-after) ?\()
1862                       (not quoted))
1863                  (setq paren t))
1864                 ((and (eq (char-after) ?\))
1865                       (not quoted))
1866                  (setq paren nil))))
1867         (nreverse elems)))))
1868
1869 (defun message-mail-file-mbox-p (file)
1870   "Say whether FILE looks like a Unix mbox file."
1871   (when (and (file-exists-p file)
1872              (file-readable-p file)
1873              (file-regular-p file))
1874     (with-temp-buffer
1875       (nnheader-insert-file-contents file)
1876       (goto-char (point-min))
1877       (looking-at message-unix-mail-delimiter))))
1878
1879 (defun message-fetch-field (header &optional not-all)
1880   "The same as `mail-fetch-field', only remove all newlines.
1881 The buffer is expected to be narrowed to just the header of the message;
1882 see `message-narrow-to-headers-or-head'."
1883   (let* ((inhibit-point-motion-hooks t)
1884          (value (mail-fetch-field header nil (not not-all))))
1885     (when value
1886       (while (string-match "\n[\t ]+" value)
1887         (setq value (replace-match " " t t value)))
1888       value)))
1889
1890 (defun message-field-value (header &optional not-all)
1891   "The same as `message-fetch-field', only narrow to the headers first."
1892   (save-excursion
1893     (save-restriction
1894       (message-narrow-to-headers-or-head)
1895       (message-fetch-field header not-all))))
1896
1897 (defun message-narrow-to-field ()
1898   "Narrow the buffer to the header on the current line."
1899   (beginning-of-line)
1900   (while (looking-at "[ \t]")
1901     (forward-line -1))
1902   (narrow-to-region
1903    (point)
1904    (progn
1905      (forward-line 1)
1906      (if (re-search-forward "^[^ \n\t]" nil t)
1907          (point-at-bol)
1908        (point-max))))
1909   (goto-char (point-min)))
1910
1911 (defun message-add-header (&rest headers)
1912   "Add the HEADERS to the message header, skipping those already present."
1913   (while headers
1914     (let (hclean)
1915       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1916         (error "Invalid header `%s'" (car headers)))
1917       (setq hclean (match-string 1 (car headers)))
1918       (save-restriction
1919         (message-narrow-to-headers)
1920         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1921           (goto-char (point-max))
1922           (if (string-match "\n$" (car headers))
1923               (insert (car headers))
1924             (insert (car headers) ?\n)))))
1925     (setq headers (cdr headers))))
1926
1927 (defmacro message-with-reply-buffer (&rest forms)
1928   "Evaluate FORMS in the reply buffer, if it exists."
1929   `(when (and message-reply-buffer
1930               (buffer-name message-reply-buffer))
1931      (with-current-buffer message-reply-buffer
1932        ,@forms)))
1933
1934 (put 'message-with-reply-buffer 'lisp-indent-function 0)
1935 (put 'message-with-reply-buffer 'edebug-form-spec '(body))
1936
1937 (defun message-fetch-reply-field (header)
1938   "Fetch field HEADER from the message we're replying to."
1939   (message-with-reply-buffer
1940     (save-restriction
1941       (mail-narrow-to-head)
1942       (message-fetch-field header))))
1943
1944 (defun message-strip-list-identifiers (subject)
1945   "Remove list identifiers in `gnus-list-identifiers' from string SUBJECT."
1946   (require 'gnus-sum)                   ; for gnus-list-identifiers
1947   (let ((regexp (if (stringp gnus-list-identifiers)
1948                     gnus-list-identifiers
1949                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1950     (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
1951                               " *\\)\\)+\\(Re: +\\)?\\)") subject)
1952         (concat (substring subject 0 (match-beginning 1))
1953                 (or (match-string 3 subject)
1954                     (match-string 5 subject))
1955                 (substring subject
1956                            (match-end 1)))
1957       subject)))
1958
1959 (defun message-strip-subject-re (subject)
1960   "Remove \"Re:\" from subject lines in string SUBJECT."
1961   (if (string-match message-subject-re-regexp subject)
1962       (substring subject (match-end 0))
1963     subject))
1964
1965 (defcustom message-replacement-char "."
1966   "Replacement character used instead of unprintable or not decodable chars."
1967   :group 'message-various
1968   :version "22.1" ;; Gnus 5.10.9
1969   :type '(choice string
1970                  (const ".")
1971                  (const "?")))
1972
1973 ;; FIXME: We also should call `message-strip-subject-encoded-words'
1974 ;; when forwarding.  Probably in `message-make-forward-subject' and
1975 ;; `message-forward-make-body'.
1976
1977 (defun message-strip-subject-encoded-words (subject)
1978   "Fix non-decodable words in SUBJECT."
1979   ;; Cf. `gnus-simplify-subject-fully'.
1980   (let* ((case-fold-search t)
1981          (replacement-chars (format "[%s%s%s]"
1982                                     message-replacement-char
1983                                     message-replacement-char
1984                                     message-replacement-char))
1985          (enc-word-re "=\\?\\([^?]+\\)\\?\\([QB]\\)\\?\\([^?]+\\)\\(\\?=\\)")
1986          cs-string
1987          (have-marker
1988           (with-temp-buffer
1989             (insert subject)
1990             (goto-char (point-min))
1991             (when (re-search-forward enc-word-re nil t)
1992               (setq cs-string (match-string 1)))))
1993          cs-coding q-or-b word-beg word-end)
1994     (if (or (not have-marker) ;; No encoded word found...
1995             ;; ... or double encoding was correct:
1996             (and (stringp cs-string)
1997                  (setq cs-string (downcase cs-string))
1998                  (mm-coding-system-p (intern cs-string))
1999                  (not (prog1
2000                           (y-or-n-p
2001                            (format "\
2002 Decoded Subject \"%s\"
2003 contains a valid encoded word.  Decode again? "
2004                                    subject))
2005                         (setq cs-coding (intern cs-string))))))
2006         subject
2007       (with-temp-buffer
2008         (insert subject)
2009         (goto-char (point-min))
2010         (while (re-search-forward enc-word-re nil t)
2011           (setq cs-string (downcase (match-string 1))
2012                 q-or-b    (match-string 2)
2013                 word-beg (match-beginning 0)
2014                 word-end (match-end 0))
2015           (setq cs-coding
2016                 (if (mm-coding-system-p (intern cs-string))
2017                     (setq cs-coding (intern cs-string))
2018                   nil))
2019           ;; No double encoded subject? => bogus charset.
2020           (unless cs-coding
2021             (setq cs-coding
2022                   (mm-read-coding-system
2023                    (format "\
2024 Decoded Subject \"%s\"
2025 contains an encoded word.  The charset `%s' is unknown or invalid.
2026 Hit RET to replace non-decodable characters with \"%s\" or enter replacement
2027 charset: "
2028                            subject cs-string message-replacement-char)))
2029             (if cs-coding
2030                 (replace-match (concat "=?" (symbol-name cs-coding)
2031                                        "?\\2?\\3\\4\\5"))
2032               (save-excursion
2033                 (goto-char word-beg)
2034                 (re-search-forward "=\\?\\([^?]+\\)\\?\\([QB]\\)\\?" word-end t)
2035                 (replace-match "")
2036                 ;; QP or base64
2037                 (if (string-match "\\`Q\\'" q-or-b)
2038                     ;; QP
2039                     (progn
2040                       (message "Replacing non-decodable characters with \"%s\"."
2041                                message-replacement-char)
2042                       (while (re-search-forward "\\(=[A-F0-9][A-F0-9]\\)+"
2043                                                 word-end t)
2044                         (replace-match message-replacement-char)))
2045                   ;; base64
2046                   (message "Replacing non-decodable characters with \"%s\"."
2047                            replacement-chars)
2048                   (re-search-forward "[^?]+" word-end t)
2049                   (replace-match replacement-chars))
2050                 (re-search-forward "\\?=")
2051                 (replace-match "")))))
2052         (rfc2047-decode-region (point-min) (point-max))
2053         (buffer-string)))))
2054
2055 ;;; Start of functions adopted from `message-utils.el'.
2056
2057 (defun message-strip-subject-trailing-was (subject)
2058   "Remove trailing \"(was: <old subject>)\" from SUBJECT lines.
2059 Leading \"Re: \" is not stripped by this function.  Use the function
2060 `message-strip-subject-re' for this."
2061   (let* ((query message-subject-trailing-was-query)
2062          (new) (found))
2063     (setq found
2064           (string-match
2065            (if (eq query 'ask)
2066                message-subject-trailing-was-ask-regexp
2067              message-subject-trailing-was-regexp)
2068            subject))
2069     (if found
2070         (setq new (substring subject 0 (match-beginning 0))))
2071     (if (or (not found) (eq query nil))
2072         subject
2073       (if (eq query 'ask)
2074           (if (message-y-or-n-p
2075                "Strip `(was: <old subject>)' in subject? " t
2076                (concat
2077                 "Strip `(was: <old subject>)' in subject "
2078                 "and use the new one instead?\n\n"
2079                 "Current subject is:   \""
2080                 subject "\"\n\n"
2081                 "New subject would be: \""
2082                 new "\"\n\n"
2083                 "See the variable `message-subject-trailing-was-query' "
2084                 "to get rid of this query."
2085                 ))
2086               new subject)
2087         new))))
2088
2089 ;;; Suggested by Jonas Steverud  @  www.dtek.chalmers.se/~d4jonas/
2090
2091 (defun message-change-subject (new-subject)
2092   "Ask for NEW-SUBJECT header, append (was: <Old Subject>)."
2093   ;; <URL:http://www.landfield.com/usefor/drafts/draft-ietf-usefor-useage--1.02.unpaged>
2094   (interactive
2095    (list
2096     (read-from-minibuffer "New subject: ")))
2097   (cond ((and (not (or (null new-subject) ; new subject not empty
2098                        (zerop (string-width new-subject))
2099                        (string-match "^[ \t]*$" new-subject))))
2100          (save-excursion
2101            (let ((old-subject
2102                   (save-restriction
2103                     (message-narrow-to-headers)
2104                     (message-fetch-field "Subject"))))
2105              (cond ((not old-subject)
2106                     (error "No current subject"))
2107                    ((not (string-match
2108                           (concat "^[ \t]*"
2109                                   (regexp-quote new-subject)
2110                                   " \t]*$")
2111                           old-subject))  ; yes, it really is a new subject
2112                     ;; delete eventual Re: prefix
2113                     (setq old-subject
2114                           (message-strip-subject-re old-subject))
2115                     (message-goto-subject)
2116                     (message-delete-line)
2117                     (insert (concat "Subject: "
2118                                     new-subject
2119                                     " (was: "
2120                                     old-subject ")\n")))))))))
2121
2122 (defun message-mark-inserted-region (beg end &optional verbatim)
2123   "Mark some region in the current article with enclosing tags.
2124 See `message-mark-insert-begin' and `message-mark-insert-end'.
2125 If VERBATIM, use slrn style verbatim marks (\"#v+\" and \"#v-\")."
2126   (interactive "r\nP")
2127   (save-excursion
2128     ;; add to the end of the region first, otherwise end would be invalid
2129     (goto-char end)
2130     (insert (if verbatim "#v-\n" message-mark-insert-end))
2131     (goto-char beg)
2132     (insert (if verbatim "#v+\n" message-mark-insert-begin))))
2133
2134 (defun message-mark-insert-file (file &optional verbatim)
2135   "Insert FILE at point, marking it with enclosing tags.
2136 See `message-mark-insert-begin' and `message-mark-insert-end'.
2137 If VERBATIM, use slrn style verbatim marks (\"#v+\" and \"#v-\")."
2138   (interactive "fFile to insert: \nP")
2139     ;; reverse insertion to get correct result.
2140   (let ((p (point)))
2141     (insert (if verbatim "#v-\n" message-mark-insert-end))
2142     (goto-char p)
2143     (insert-file-contents file)
2144     (goto-char p)
2145     (insert (if verbatim "#v+\n" message-mark-insert-begin))))
2146
2147 (defun message-add-archive-header ()
2148   "Insert \"X-No-Archive: Yes\" in the header and a note in the body.
2149 The note can be customized using `message-archive-note'.  When called with a
2150 prefix argument, ask for a text to insert.  If you don't want the note in the
2151 body, set  `message-archive-note' to nil."
2152   (interactive)
2153   (if current-prefix-arg
2154       (setq message-archive-note
2155             (read-from-minibuffer "Reason for No-Archive: "
2156                                   (cons message-archive-note 0))))
2157     (save-excursion
2158       (if (message-goto-signature)
2159           (re-search-backward message-signature-separator))
2160       (when message-archive-note
2161         (insert message-archive-note)
2162         (newline))
2163       (message-add-header message-archive-header)
2164       (message-sort-headers)))
2165
2166 (defun message-cross-post-followup-to-header (target-group)
2167   "Mangles FollowUp-To and Newsgroups header to point to TARGET-GROUP.
2168 With prefix-argument just set Follow-Up, don't cross-post."
2169   (interactive
2170    (list ; Completion based on Gnus
2171     (completing-read "Followup To: "
2172                      (if (boundp 'gnus-newsrc-alist)
2173                          gnus-newsrc-alist)
2174                      nil nil '("poster" . 0)
2175                      (if (boundp 'gnus-group-history)
2176                          'gnus-group-history))))
2177   (message-remove-header "Follow[Uu]p-[Tt]o" t)
2178   (message-goto-newsgroups)
2179   (beginning-of-line)
2180   ;; if we already did a crosspost before, kill old target
2181   (if (and message-cross-post-old-target
2182            (re-search-forward
2183             (regexp-quote (concat "," message-cross-post-old-target))
2184             nil t))
2185       (replace-match ""))
2186   ;; unless (followup is to poster or user explicitly asked not
2187   ;; to cross-post, or target-group is already in Newsgroups)
2188   ;; add target-group to Newsgroups line.
2189   (cond ((and (or
2190                ;; def: cross-post, req:no
2191                (and message-cross-post-default (not current-prefix-arg))
2192                ;; def: no-cross-post, req:yes
2193                (and (not message-cross-post-default) current-prefix-arg))
2194               (not (string-match "poster" target-group))
2195               (not (string-match (regexp-quote target-group)
2196                                  (message-fetch-field "Newsgroups"))))
2197          (end-of-line)
2198          (insert (concat "," target-group))))
2199   (end-of-line) ; ensure Followup: comes after Newsgroups:
2200   ;; unless new followup would be identical to Newsgroups line
2201   ;; make a new Followup-To line
2202   (if (not (string-match (concat "^[ \t]*"
2203                                  target-group
2204                                  "[ \t]*$")
2205                          (message-fetch-field "Newsgroups")))
2206       (insert (concat "\nFollowup-To: " target-group)))
2207   (setq message-cross-post-old-target target-group))
2208
2209 (defun message-cross-post-insert-note (target-group cross-post in-old
2210                                                     old-groups)
2211   "Insert a in message body note about a set Followup or Crosspost.
2212 If there have been previous notes, delete them.  TARGET-GROUP specifies the
2213 group to Followup-To.  When CROSS-POST is t, insert note about
2214 crossposting.  IN-OLD specifies whether TARGET-GROUP is a member of
2215 OLD-GROUPS.  OLD-GROUPS lists the old-groups the posting would have
2216 been made to before the user asked for a Crosspost."
2217   ;; start scanning body for previous uses
2218   (message-goto-signature)
2219   (let ((head (re-search-backward
2220                (concat "^" mail-header-separator)
2221                nil t))) ; just search in body
2222     (message-goto-signature)
2223     (while (re-search-backward
2224             (concat "^" (regexp-quote message-cross-post-note) ".*")
2225             head t)
2226       (message-delete-line))
2227     (message-goto-signature)
2228     (while (re-search-backward
2229             (concat "^" (regexp-quote message-followup-to-note) ".*")
2230             head t)
2231       (message-delete-line))
2232     ;; insert new note
2233     (if (message-goto-signature)
2234         (re-search-backward message-signature-separator))
2235     (if (or in-old
2236             (not cross-post)
2237             (string-match "^[ \t]*poster[ \t]*$" target-group))
2238         (insert (concat message-followup-to-note target-group "\n"))
2239       (insert (concat message-cross-post-note target-group "\n")))))
2240
2241 (defun message-cross-post-followup-to (target-group)
2242   "Crossposts message and set Followup-To to TARGET-GROUP.
2243 With prefix-argument just set Follow-Up, don't cross-post."
2244   (interactive
2245    (list ; Completion based on Gnus
2246     (completing-read "Followup To: "
2247                      (if (boundp 'gnus-newsrc-alist)
2248                          gnus-newsrc-alist)
2249                      nil nil '("poster" . 0)
2250                      (if (boundp 'gnus-group-history)
2251                          'gnus-group-history))))
2252   (cond ((not (or (null target-group) ; new subject not empty
2253                   (zerop (string-width target-group))
2254                   (string-match "^[ \t]*$" target-group)))
2255          (save-excursion
2256            (let* ((old-groups (message-fetch-field "Newsgroups"))
2257                   (in-old (string-match
2258                            (regexp-quote target-group)
2259                            (or old-groups ""))))
2260              ;; check whether target exactly matches old Newsgroups
2261              (cond ((not old-groups)
2262                     (error "No current newsgroup"))
2263                    ((or (not in-old)
2264                         (not (string-match
2265                               (concat "^[ \t]*"
2266                                       (regexp-quote target-group)
2267                                       "[ \t]*$")
2268                               old-groups)))
2269                     ;; yes, Newsgroups line must change
2270                     (message-cross-post-followup-to-header target-group)
2271                     ;; insert note whether we do cross-post or followup-to
2272                     (funcall message-cross-post-note-function
2273                              target-group
2274                              (if (or (and message-cross-post-default
2275                                           (not current-prefix-arg))
2276                                      (and (not message-cross-post-default)
2277                                           current-prefix-arg)) t)
2278                              in-old old-groups))))))))
2279
2280 ;;; Reduce To: to Cc: or Bcc: header
2281
2282 (defun message-reduce-to-to-cc ()
2283  "Replace contents of To: header with contents of Cc: or Bcc: header."
2284  (interactive)
2285  (let ((cc-content
2286         (save-restriction (message-narrow-to-headers)
2287                           (message-fetch-field "cc")))
2288        (bcc nil))
2289    (if (and (not cc-content)
2290             (setq cc-content
2291                   (save-restriction
2292                     (message-narrow-to-headers)
2293                     (message-fetch-field "bcc"))))
2294        (setq bcc t))
2295    (cond (cc-content
2296           (save-excursion
2297             (message-goto-to)
2298             (message-delete-line)
2299             (insert (concat "To: " cc-content "\n"))
2300             (save-restriction
2301               (message-narrow-to-headers)
2302               (message-remove-header (if bcc
2303                                          "bcc"
2304                                        "cc"))))))))
2305
2306 ;;; End of functions adopted from `message-utils.el'.
2307
2308 (defun message-remove-header (header &optional is-regexp first reverse)
2309   "Remove HEADER in the narrowed buffer.
2310 If IS-REGEXP, HEADER is a regular expression.
2311 If FIRST, only remove the first instance of the header.
2312 Return the number of headers removed."
2313   (goto-char (point-min))
2314   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
2315         (number 0)
2316         (case-fold-search t)
2317         last)
2318     (while (and (not (eobp))
2319                 (not last))
2320       (if (if reverse
2321               (not (looking-at regexp))
2322             (looking-at regexp))
2323           (progn
2324             (incf number)
2325             (when first
2326               (setq last t))
2327             (delete-region
2328              (point)
2329              ;; There might be a continuation header, so we have to search
2330              ;; until we find a new non-continuation line.
2331              (progn
2332                (forward-line 1)
2333                (if (re-search-forward "^[^ \t]" nil t)
2334                    (goto-char (match-beginning 0))
2335                  (point-max)))))
2336         (forward-line 1)
2337         (if (re-search-forward "^[^ \t]" nil t)
2338             (goto-char (match-beginning 0))
2339           (goto-char (point-max)))))
2340     number))
2341
2342 (defun message-remove-first-header (header)
2343   "Remove the first instance of HEADER if there is more than one."
2344   (let ((count 0)
2345         (regexp (concat "^" (regexp-quote header) ":")))
2346     (save-excursion
2347       (goto-char (point-min))
2348       (while (re-search-forward regexp nil t)
2349         (incf count)))
2350     (while (> count 1)
2351       (message-remove-header header nil t)
2352       (decf count))))
2353
2354 (defun message-narrow-to-headers ()
2355   "Narrow the buffer to the head of the message."
2356   (widen)
2357   (narrow-to-region
2358    (goto-char (point-min))
2359    (if (re-search-forward
2360         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
2361        (match-beginning 0)
2362      (point-max)))
2363   (goto-char (point-min)))
2364
2365 (defun message-narrow-to-head-1 ()
2366   "Like `message-narrow-to-head'.  Don't widen."
2367   (narrow-to-region
2368    (goto-char (point-min))
2369    (if (search-forward "\n\n" nil 1)
2370        (1- (point))
2371      (point-max)))
2372   (goto-char (point-min)))
2373
2374 (defun message-narrow-to-head ()
2375   "Narrow the buffer to the head of the message.
2376 Point is left at the beginning of the narrowed-to region."
2377   (widen)
2378   (message-narrow-to-head-1))
2379
2380 (defun message-narrow-to-headers-or-head ()
2381   "Narrow the buffer to the head of the message."
2382   (widen)
2383   (narrow-to-region
2384    (goto-char (point-min))
2385    (if (re-search-forward (concat "\\(\n\\)\n\\|^\\("
2386                                   (regexp-quote mail-header-separator)
2387                                   "\n\\)")
2388                           nil t)
2389        (or (match-end 1) (match-beginning 2))
2390      (point-max)))
2391   (goto-char (point-min)))
2392
2393 (defun message-news-p ()
2394   "Say whether the current buffer contains a news message."
2395   (and (not message-this-is-mail)
2396        (or message-this-is-news
2397            (save-excursion
2398              (save-restriction
2399                (message-narrow-to-headers)
2400                (and (message-fetch-field "newsgroups")
2401                     (not (message-fetch-field "posted-to"))))))))
2402
2403 (defun message-mail-p ()
2404   "Say whether the current buffer contains a mail message."
2405   (and (not message-this-is-news)
2406        (or message-this-is-mail
2407            (save-excursion
2408              (save-restriction
2409                (message-narrow-to-headers)
2410                (or (message-fetch-field "to")
2411                    (message-fetch-field "cc")
2412                    (message-fetch-field "bcc")))))))
2413
2414 (defun message-subscribed-p ()
2415   "Say whether we need to insert a MFT header."
2416   (or message-subscribed-regexps
2417       message-subscribed-addresses
2418       message-subscribed-address-file
2419       message-subscribed-address-functions))
2420
2421 (defun message-next-header ()
2422   "Go to the beginning of the next header."
2423   (beginning-of-line)
2424   (or (eobp) (forward-char 1))
2425   (not (if (re-search-forward "^[^ \t]" nil t)
2426            (beginning-of-line)
2427          (goto-char (point-max)))))
2428
2429 (defun message-sort-headers-1 ()
2430   "Sort the buffer as headers using `message-rank' text props."
2431   (goto-char (point-min))
2432   (require 'sort)
2433   (sort-subr
2434    nil 'message-next-header
2435    (lambda ()
2436      (message-next-header)
2437      (unless (bobp)
2438        (forward-char -1)))
2439    (lambda ()
2440      (or (get-text-property (point) 'message-rank)
2441          10000))))
2442
2443 (defun message-sort-headers ()
2444   "Sort the headers of the current message according to `message-header-format-alist'."
2445   (interactive)
2446   (save-excursion
2447     (save-restriction
2448       (let ((max (1+ (length message-header-format-alist)))
2449             rank)
2450         (message-narrow-to-headers)
2451         (while (re-search-forward "^[^ \n]+:" nil t)
2452           (put-text-property
2453            (match-beginning 0) (1+ (match-beginning 0))
2454            'message-rank
2455            (if (setq rank (length (memq (assq (intern (buffer-substring
2456                                                        (match-beginning 0)
2457                                                        (1- (match-end 0))))
2458                                               message-header-format-alist)
2459                                         message-header-format-alist)))
2460                (- max rank)
2461              (1+ max)))))
2462       (message-sort-headers-1))))
2463
2464 (defun message-kill-address ()
2465   "Kill the address under point."
2466   (interactive)
2467   (let ((start (point)))
2468     (message-skip-to-next-address)
2469     (kill-region start (point))))
2470
2471
2472 (defun message-info (&optional arg)
2473   "Display the Message manual.
2474
2475 Prefixed with one \\[universal-argument], display the Emacs MIME manual.
2476 Prefixed with two \\[universal-argument]'s, display the PGG manual."
2477   (interactive "p")
2478   (cond ((eq arg 16) (Info-goto-node "(pgg)Top"))
2479         ((eq arg  4) (Info-goto-node "(emacs-mime)Top"))
2480         (t           (Info-goto-node "(message)Top"))))
2481
2482 \f
2483
2484 ;;;
2485 ;;; Message mode
2486 ;;;
2487
2488 ;;; Set up keymap.
2489
2490 (defvar message-mode-map nil)
2491
2492 (unless message-mode-map
2493   (setq message-mode-map (make-keymap))
2494   (set-keymap-parent message-mode-map text-mode-map)
2495   (define-key message-mode-map "\C-c?" 'describe-mode)
2496
2497   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
2498   (define-key message-mode-map "\C-c\C-f\C-o" 'message-goto-from)
2499   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
2500   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
2501   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
2502   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
2503   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
2504   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
2505   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
2506   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
2507   (define-key message-mode-map "\C-c\C-f\C-m" 'message-goto-mail-followup-to)
2508   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
2509   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
2510   (define-key message-mode-map "\C-c\C-f\C-i"
2511     'message-insert-or-toggle-importance)
2512   (define-key message-mode-map "\C-c\C-f\C-a"
2513     'message-generate-unsubscribed-mail-followup-to)
2514
2515   ;; modify headers (and insert notes in body)
2516   (define-key message-mode-map "\C-c\C-fs"    'message-change-subject)
2517   ;;
2518   (define-key message-mode-map "\C-c\C-fx"    'message-cross-post-followup-to)
2519   ;; prefix+message-cross-post-followup-to = same w/o cross-post
2520   (define-key message-mode-map "\C-c\C-ft"    'message-reduce-to-to-cc)
2521   (define-key message-mode-map "\C-c\C-fa"    'message-add-archive-header)
2522   ;; mark inserted text
2523   (define-key message-mode-map "\C-c\M-m" 'message-mark-inserted-region)
2524   (define-key message-mode-map "\C-c\M-f" 'message-mark-insert-file)
2525
2526   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
2527   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
2528
2529   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
2530   (define-key message-mode-map "\C-c\C-fw" 'message-insert-wide-reply)
2531   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
2532   (define-key message-mode-map "\C-c\C-l" 'message-to-list-only)
2533   (define-key message-mode-map "\C-c\C-f\C-e" 'message-insert-expires)
2534
2535   (define-key message-mode-map "\C-c\C-u" 'message-insert-or-toggle-importance)
2536   (define-key message-mode-map "\C-c\M-n"
2537     'message-insert-disposition-notification-to)
2538
2539   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
2540   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
2541   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
2542   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
2543   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
2544   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
2545   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
2546   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
2547
2548   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
2549   (define-key message-mode-map "\C-c\C-s" 'message-send)
2550   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
2551   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
2552   (define-key message-mode-map "\C-c\n" 'gnus-delay-article)
2553
2554   (define-key message-mode-map "\C-c\M-k" 'message-kill-address)
2555   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
2556   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
2557   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
2558   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
2559   (define-key message-mode-map [remap split-line]  'message-split-line)
2560
2561   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
2562
2563   (define-key message-mode-map "\C-a" 'message-beginning-of-line)
2564   (define-key message-mode-map "\t" 'message-tab)
2565   (define-key message-mode-map "\M-;" 'comment-region)
2566
2567   (define-key message-mode-map "\M-n" 'message-display-abbrev))
2568
2569 (easy-menu-define
2570   message-mode-menu message-mode-map "Message Menu."
2571   `("Message"
2572     ["Yank Original" message-yank-original message-reply-buffer]
2573     ["Fill Yanked Message" message-fill-yanked-message t]
2574     ["Insert Signature" message-insert-signature t]
2575     ["Caesar (rot13) Message" message-caesar-buffer-body t]
2576     ["Caesar (rot13) Region" message-caesar-region (message-mark-active-p)]
2577     ["Elide Region" message-elide-region
2578      :active (message-mark-active-p)
2579      ,@(if (featurep 'xemacs) nil
2580          '(:help "Replace text in region with an ellipsis"))]
2581     ["Delete Outside Region" message-delete-not-region
2582      :active (message-mark-active-p)
2583      ,@(if (featurep 'xemacs) nil
2584          '(:help "Delete all quoted text outside region"))]
2585     ["Kill To Signature" message-kill-to-signature t]
2586     ["Newline and Reformat" message-newline-and-reformat t]
2587     ["Rename buffer" message-rename-buffer t]
2588     ["Spellcheck" ispell-message
2589      ,@(if (featurep 'xemacs) '(t)
2590          '(:help "Spellcheck this message"))]
2591     "----"
2592     ["Insert Region Marked" message-mark-inserted-region
2593      :active (message-mark-active-p)
2594      ,@(if (featurep 'xemacs) nil
2595          '(:help "Mark region with enclosing tags"))]
2596     ["Insert File Marked..." message-mark-insert-file
2597      ,@(if (featurep 'xemacs) '(t)
2598          '(:help "Insert file at point marked with enclosing tags"))]
2599     "----"
2600     ["Send Message" message-send-and-exit
2601      ,@(if (featurep 'xemacs) '(t)
2602          '(:help "Send this message"))]
2603     ["Postpone Message" message-dont-send
2604      ,@(if (featurep 'xemacs) '(t)
2605          '(:help "File this draft message and exit"))]
2606     ["Send at Specific Time..." gnus-delay-article
2607      ,@(if (featurep 'xemacs) '(t)
2608          '(:help "Ask, then arrange to send message at that time"))]
2609     ["Kill Message" message-kill-buffer
2610      ,@(if (featurep 'xemacs) '(t)
2611          '(:help "Delete this message without sending"))]
2612     "----"
2613     ["Message manual" message-info
2614      ,@(if (featurep 'xemacs) '(t)
2615          '(:help "Display the Message manual"))]))
2616
2617 (easy-menu-define
2618   message-mode-field-menu message-mode-map ""
2619   `("Field"
2620     ["To" message-goto-to t]
2621     ["From" message-goto-from t]
2622     ["Subject" message-goto-subject t]
2623     ["Change subject..." message-change-subject t]
2624     ["Cc" message-goto-cc t]
2625     ["Bcc" message-goto-bcc t]
2626     ["Fcc" message-goto-fcc t]
2627     ["Reply-To" message-goto-reply-to t]
2628     ["Flag As Important" message-insert-importance-high
2629      ,@(if (featurep 'xemacs) '(t)
2630          '(:help "Mark this message as important"))]
2631     ["Flag As Unimportant" message-insert-importance-low
2632      ,@(if (featurep 'xemacs) '(t)
2633          '(:help "Mark this message as unimportant"))]
2634     ["Request Receipt"
2635      message-insert-disposition-notification-to
2636      ,@(if (featurep 'xemacs) '(t)
2637          '(:help "Request a receipt notification"))]
2638     "----"
2639     ;; (typical) news stuff
2640     ["Summary" message-goto-summary t]
2641     ["Keywords" message-goto-keywords t]
2642     ["Newsgroups" message-goto-newsgroups t]
2643     ["Fetch Newsgroups" message-insert-newsgroups t]
2644     ["Followup-To" message-goto-followup-to t]
2645     ;; ["Followup-To (with note in body)" message-cross-post-followup-to t]
2646     ["Crosspost / Followup-To..." message-cross-post-followup-to t]
2647     ["Distribution" message-goto-distribution t]
2648     ["Expires" message-insert-expires t ]
2649     ["X-No-Archive" message-add-archive-header t ]
2650     "----"
2651     ;; (typical) mailing-lists stuff
2652     ["Fetch To" message-insert-to
2653      ,@(if (featurep 'xemacs) '(t)
2654          '(:help "Insert a To header that points to the author."))]
2655     ["Fetch To and Cc" message-insert-wide-reply
2656      ,@(if (featurep 'xemacs) '(t)
2657          '(:help
2658            "Insert To and Cc headers as if you were doing a wide reply."))]
2659     "----"
2660     ["Send to list only" message-to-list-only t]
2661     ["Mail-Followup-To" message-goto-mail-followup-to t]
2662     ["Unsubscribed list post" message-generate-unsubscribed-mail-followup-to
2663      ,@(if (featurep 'xemacs) '(t)
2664          '(:help "Insert a reasonable `Mail-Followup-To:' header."))]
2665     ["Reduce To: to Cc:" message-reduce-to-to-cc t]
2666     "----"
2667     ["Sort Headers" message-sort-headers t]
2668     ["Encode non-ASCII domain names" message-idna-to-ascii-rhs t]
2669     ;; We hide `message-hidden-headers' by narrowing the buffer.
2670     ["Show Hidden Headers" widen t]
2671     ["Goto Body" message-goto-body t]
2672     ["Goto Signature" message-goto-signature t]))
2673
2674 (defvar message-tool-bar-map nil)
2675
2676 (defvar facemenu-add-face-function)
2677 (defvar facemenu-remove-face-function)
2678
2679 ;;; Forbidden properties
2680 ;;
2681 ;; We use `after-change-functions' to keep special text properties
2682 ;; that interfer with the normal function of message mode out of the
2683 ;; buffer.
2684
2685 (defcustom message-strip-special-text-properties t
2686   "Strip special properties from the message buffer.
2687
2688 Emacs has a number of special text properties which can break message
2689 composing in various ways.  If this option is set, message will strip
2690 these properties from the message composition buffer.  However, some
2691 packages requires these properties to be present in order to work.
2692 If you use one of these packages, turn this option off, and hope the
2693 message composition doesn't break too bad."
2694   :version "22.1"
2695   :group 'message-various
2696   :link '(custom-manual "(message)Various Message Variables")
2697   :type 'boolean)
2698
2699 (defconst message-forbidden-properties
2700   ;; No reason this should be clutter up customize.  We make it a
2701   ;; property list (rather than a list of property symbols), to be
2702   ;; directly useful for `remove-text-properties'.
2703   '(field nil read-only nil invisible nil intangible nil
2704           mouse-face nil modification-hooks nil insert-in-front-hooks nil
2705           insert-behind-hooks nil point-entered nil point-left nil)
2706   ;; Other special properties:
2707   ;; category, face, display: probably doesn't do any harm.
2708   ;; fontified: is used by font-lock.
2709   ;; syntax-table, local-map: I dunno.
2710   ;; We need to add XEmacs names to the list.
2711   "Property list of with properties forbidden in message buffers.
2712 The values of the properties are ignored, only the property names are used.")
2713
2714 (defun message-tamago-not-in-use-p (pos)
2715   "Return t when tamago version 4 is not in use at the cursor position.
2716 Tamago version 4 is a popular input method for writing Japanese text.
2717 It uses the properties `intangible', `invisible', `modification-hooks'
2718 and `read-only' when translating ascii or kana text to kanji text.
2719 These properties are essential to work, so we should never strip them."
2720   (not (and (boundp 'egg-modefull-mode)
2721             (symbol-value 'egg-modefull-mode)
2722             (or (memq (get-text-property pos 'intangible)
2723                       '(its-part-1 its-part-2))
2724                 (get-text-property pos 'egg-end)
2725                 (get-text-property pos 'egg-lang)
2726                 (get-text-property pos 'egg-start)))))
2727
2728 (defsubst message-mail-alias-type-p (type)
2729   (if (atom message-mail-alias-type)
2730       (eq message-mail-alias-type type)
2731     (memq type message-mail-alias-type)))
2732
2733 (defun message-strip-forbidden-properties (begin end &optional old-length)
2734   "Strip forbidden properties between BEGIN and END, ignoring the third arg.
2735 This function is intended to be called from `after-change-functions'.
2736 See also `message-forbidden-properties'."
2737   (when (and (message-mail-alias-type-p 'ecomplete)
2738              (memq this-command message-self-insert-commands))
2739     (message-display-abbrev))
2740   (when (and message-strip-special-text-properties
2741              (message-tamago-not-in-use-p begin))
2742     (let ((buffer-read-only nil)
2743           (inhibit-read-only t))
2744       (remove-text-properties begin end message-forbidden-properties))))
2745
2746 ;;;###autoload
2747 (define-derived-mode message-mode text-mode "Message"
2748   "Major mode for editing mail and news to be sent.
2749 Like Text Mode but with these additional commands:\\<message-mode-map>
2750 C-c C-s  `message-send' (send the message)  C-c C-c  `message-send-and-exit'
2751 C-c C-d  Postpone sending the message       C-c C-k  Kill the message
2752 C-c C-f  move to a header field (and create it if there isn't):
2753          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
2754          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
2755          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
2756          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
2757          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
2758          C-c C-f C-o  move to From (\"Originator\")
2759          C-c C-f C-f  move to Followup-To
2760          C-c C-f C-m  move to Mail-Followup-To
2761          C-c C-f C-e  move to Expires
2762          C-c C-f C-i  cycle through Importance values
2763          C-c C-f s    change subject and append \"(was: <Old Subject>)\"
2764          C-c C-f x    crossposting with FollowUp-To header and note in body
2765          C-c C-f t    replace To: header with contents of Cc: or Bcc:
2766          C-c C-f a    Insert X-No-Archive: header and a note in the body
2767 C-c C-t  `message-insert-to' (add a To header to a news followup)
2768 C-c C-l  `message-to-list-only' (removes all but list address in to/cc)
2769 C-c C-n  `message-insert-newsgroups' (add a Newsgroup header to a news reply)
2770 C-c C-b  `message-goto-body' (move to beginning of message text).
2771 C-c C-i  `message-goto-signature' (move to the beginning of the signature).
2772 C-c C-w  `message-insert-signature' (insert `message-signature-file' file).
2773 C-c C-y  `message-yank-original' (insert current message, if any).
2774 C-c C-q  `message-fill-yanked-message' (fill what was yanked).
2775 C-c C-e  `message-elide-region' (elide the text between point and mark).
2776 C-c C-v  `message-delete-not-region' (remove the text outside the region).
2777 C-c C-z  `message-kill-to-signature' (kill the text up to the signature).
2778 C-c C-r  `message-caesar-buffer-body' (rot13 the message body).
2779 C-c C-a  `mml-attach-file' (attach a file as MIME).
2780 C-c C-u  `message-insert-or-toggle-importance'  (insert or cycle importance).
2781 C-c M-n  `message-insert-disposition-notification-to'  (request receipt).
2782 C-c M-m  `message-mark-inserted-region' (mark region with enclosing tags).
2783 C-c M-f  `message-mark-insert-file' (insert file marked with enclosing tags).
2784 M-RET    `message-newline-and-reformat' (break the line and reformat)."
2785   (setq local-abbrev-table text-mode-abbrev-table)
2786   (set (make-local-variable 'message-reply-buffer) nil)
2787   (set (make-local-variable 'message-inserted-headers) nil)
2788   (set (make-local-variable 'message-send-actions) nil)
2789   (set (make-local-variable 'message-exit-actions) nil)
2790   (set (make-local-variable 'message-kill-actions) nil)
2791   (set (make-local-variable 'message-postpone-actions) nil)
2792   (set (make-local-variable 'message-draft-article) nil)
2793   (setq buffer-offer-save t)
2794   (set (make-local-variable 'facemenu-add-face-function)
2795        (lambda (face end)
2796          (let ((face-fun (cdr (assq face message-face-alist))))
2797            (if face-fun
2798                (funcall face-fun (point) end)
2799              (error "Face %s not configured for %s mode" face mode-name)))
2800          ""))
2801   (set (make-local-variable 'facemenu-remove-face-function) t)
2802   (set (make-local-variable 'message-reply-headers) nil)
2803   (make-local-variable 'message-newsreader)
2804   (make-local-variable 'message-mailer)
2805   (make-local-variable 'message-post-method)
2806   (set (make-local-variable 'message-sent-message-via) nil)
2807   (set (make-local-variable 'message-checksum) nil)
2808   (set (make-local-variable 'message-mime-part) 0)
2809   (message-setup-fill-variables)
2810   (when message-fill-column
2811     (setq fill-column message-fill-column)
2812     (turn-on-auto-fill))
2813   ;; Allow using comment commands to add/remove quoting.
2814   ;; (set (make-local-variable 'comment-start) message-yank-prefix)
2815   (when message-yank-prefix
2816     (set (make-local-variable 'comment-start) message-yank-prefix)
2817     (set (make-local-variable 'comment-start-skip)
2818          (concat "^" (regexp-quote message-yank-prefix) "[ \t]*")))
2819   (if (featurep 'xemacs)
2820       (message-setup-toolbar)
2821     (set (make-local-variable 'font-lock-defaults)
2822          '(message-font-lock-keywords t))
2823     (if (boundp 'tool-bar-map)
2824         (set (make-local-variable 'tool-bar-map) (message-make-tool-bar))))
2825   (easy-menu-add message-mode-menu message-mode-map)
2826   (easy-menu-add message-mode-field-menu message-mode-map)
2827   (gnus-make-local-hook 'after-change-functions)
2828   ;; Mmmm... Forbidden properties...
2829   (add-hook 'after-change-functions 'message-strip-forbidden-properties
2830             nil 'local)
2831   ;; Allow mail alias things.
2832   (cond
2833    ((message-mail-alias-type-p 'abbrev)
2834     (if (fboundp 'mail-abbrevs-setup)
2835         (mail-abbrevs-setup)
2836       (if (fboundp 'mail-aliases-setup) ; warning avoidance
2837           (mail-aliases-setup))))
2838    ((message-mail-alias-type-p 'ecomplete)
2839     (ecomplete-setup)))
2840   (unless buffer-file-name
2841     (message-set-auto-save-file-name))
2842   (unless (buffer-base-buffer)
2843     ;; Don't enable multibyte on an indirect buffer.  Maybe enabling
2844     ;; multibyte is not necessary at all. -- zsh
2845     (mm-enable-multibyte))
2846   (set (make-local-variable 'indent-tabs-mode) nil) ;No tabs for indentation.
2847   (mml-mode))
2848
2849 (defun message-setup-fill-variables ()
2850   "Setup message fill variables."
2851   (set (make-local-variable 'fill-paragraph-function)
2852        'message-fill-paragraph)
2853   (make-local-variable 'paragraph-separate)
2854   (make-local-variable 'paragraph-start)
2855   (make-local-variable 'adaptive-fill-regexp)
2856   (unless (boundp 'adaptive-fill-first-line-regexp)
2857     (setq adaptive-fill-first-line-regexp nil))
2858   (make-local-variable 'adaptive-fill-first-line-regexp)
2859   (let ((quote-prefix-regexp
2860          ;; User should change message-cite-prefix-regexp if
2861          ;; message-yank-prefix is set to an abnormal value.
2862          (concat "\\(" message-cite-prefix-regexp "\\)[ \t]*")))
2863     (setq paragraph-start
2864           (concat
2865            (regexp-quote mail-header-separator) "$\\|"
2866            "[ \t]*$\\|"                 ; blank lines
2867            "-- $\\|"                    ; signature delimiter
2868            "---+$\\|"              ; delimiters for forwarded messages
2869            page-delimiter "$\\|"        ; spoiler warnings
2870            ".*wrote:$\\|"               ; attribution lines
2871            quote-prefix-regexp "$\\|"   ; empty lines in quoted text
2872                                         ; mml tags
2873            "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)"))
2874     (setq paragraph-separate paragraph-start)
2875     (setq adaptive-fill-regexp
2876           (concat quote-prefix-regexp "\\|" adaptive-fill-regexp))
2877     (setq adaptive-fill-first-line-regexp
2878           (concat quote-prefix-regexp "\\|"
2879                   adaptive-fill-first-line-regexp)))
2880   (make-local-variable 'auto-fill-inhibit-regexp)
2881   ;;(setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
2882   (setq auto-fill-inhibit-regexp nil)
2883   (make-local-variable 'normal-auto-fill-function)
2884   (setq normal-auto-fill-function 'message-do-auto-fill)
2885   ;; KLUDGE: auto fill might already be turned on in `text-mode-hook'.
2886   ;; In that case, ensure that it uses the right function.  The real
2887   ;; solution would be not to use `define-derived-mode', and run
2888   ;; `text-mode-hook' ourself at the end of the mode.
2889   ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-19.
2890   ;; This kludge is unneeded in Emacs>=21 since define-derived-mode is
2891   ;; now careful to run parent hooks after the body.  --Stef
2892   (when auto-fill-function
2893     (setq auto-fill-function normal-auto-fill-function)))
2894
2895 \f
2896
2897 ;;;
2898 ;;; Message mode commands
2899 ;;;
2900
2901 ;;; Movement commands
2902
2903 (defun message-goto-to ()
2904   "Move point to the To header."
2905   (interactive)
2906   (message-position-on-field "To"))
2907
2908 (defun message-goto-from ()
2909   "Move point to the From header."
2910   (interactive)
2911   (message-position-on-field "From"))
2912
2913 (defun message-goto-subject ()
2914   "Move point to the Subject header."
2915   (interactive)
2916   (message-position-on-field "Subject"))
2917
2918 (defun message-goto-cc ()
2919   "Move point to the Cc header."
2920   (interactive)
2921   (message-position-on-field "Cc" "To"))
2922
2923 (defun message-goto-bcc ()
2924   "Move point to the Bcc  header."
2925   (interactive)
2926   (message-position-on-field "Bcc" "Cc" "To"))
2927
2928 (defun message-goto-fcc ()
2929   "Move point to the Fcc header."
2930   (interactive)
2931   (message-position-on-field "Fcc" "To" "Newsgroups"))
2932
2933 (defun message-goto-reply-to ()
2934   "Move point to the Reply-To header."
2935   (interactive)
2936   (message-position-on-field "Reply-To" "Subject"))
2937
2938 (defun message-goto-newsgroups ()
2939   "Move point to the Newsgroups header."
2940   (interactive)
2941   (message-position-on-field "Newsgroups"))
2942
2943 (defun message-goto-distribution ()
2944   "Move point to the Distribution header."
2945   (interactive)
2946   (message-position-on-field "Distribution"))
2947
2948 (defun message-goto-followup-to ()
2949   "Move point to the Followup-To header."
2950   (interactive)
2951   (message-position-on-field "Followup-To" "Newsgroups"))
2952
2953 (defun message-goto-mail-followup-to ()
2954   "Move point to the Mail-Followup-To header."
2955   (interactive)
2956   (message-position-on-field "Mail-Followup-To" "To"))
2957
2958 (defun message-goto-keywords ()
2959   "Move point to the Keywords header."
2960   (interactive)
2961   (message-position-on-field "Keywords" "Subject"))
2962
2963 (defun message-goto-summary ()
2964   "Move point to the Summary header."
2965   (interactive)
2966   (message-position-on-field "Summary" "Subject"))
2967
2968 (defun message-goto-body (&optional interactivep)
2969   "Move point to the beginning of the message body."
2970   (interactive (list t))
2971   (when (and interactivep
2972              (looking-at "[ \t]*\n"))
2973     (expand-abbrev))
2974   (goto-char (point-min))
2975   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
2976       (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t)))
2977
2978 (defun message-in-body-p ()
2979   "Return t if point is in the message body."
2980   (let ((body (save-excursion (message-goto-body) (point))))
2981     (>= (point) body)))
2982
2983 (defun message-goto-eoh ()
2984   "Move point to the end of the headers."
2985   (interactive)
2986   (message-goto-body)
2987   (forward-line -1))
2988
2989 (defun message-goto-signature ()
2990   "Move point to the beginning of the message signature.
2991 If there is no signature in the article, go to the end and
2992 return nil."
2993   (interactive)
2994   (goto-char (point-min))
2995   (if (re-search-forward message-signature-separator nil t)
2996       (forward-line 1)
2997     (goto-char (point-max))
2998     nil))
2999
3000 (defun message-generate-unsubscribed-mail-followup-to (&optional include-cc)
3001   "Insert a reasonable MFT header in a post to an unsubscribed list.
3002 When making original posts to a mailing list you are not subscribed to,
3003 you have to type in a MFT header by hand.  The contents, usually, are
3004 the addresses of the list and your own address.  This function inserts
3005 such a header automatically.  It fetches the contents of the To: header
3006 in the current mail buffer, and appends the current `user-mail-address'.
3007
3008 If the optional argument INCLUDE-CC is non-nil, the addresses in the
3009 Cc: header are also put into the MFT."
3010
3011   (interactive "P")
3012   (let* (cc tos)
3013     (save-restriction
3014       (message-narrow-to-headers)
3015       (message-remove-header "Mail-Followup-To")
3016       (setq cc (and include-cc (message-fetch-field "Cc")))
3017       (setq tos (if cc
3018                     (concat (message-fetch-field "To") "," cc)
3019                   (message-fetch-field "To"))))
3020     (message-goto-mail-followup-to)
3021     (insert (concat tos ", " user-mail-address))))
3022
3023 \f
3024
3025 (defun message-insert-to (&optional force)
3026   "Insert a To header that points to the author of the article being replied to.
3027 If the original author requested not to be sent mail, don't insert unless the
3028 prefix FORCE is given."
3029   (interactive "P")
3030   (let* ((mct (message-fetch-reply-field "mail-copies-to"))
3031          (dont (and mct (or (equal (downcase mct) "never")
3032                             (equal (downcase mct) "nobody"))))
3033          (to (or (message-fetch-reply-field "mail-reply-to")
3034                  (message-fetch-reply-field "reply-to")
3035                  (message-fetch-reply-field "from"))))
3036     (when (and dont to)
3037       (message
3038        (if force
3039            "Ignoring the user request not to have copies sent via mail"
3040          "Complying with the user request not to have copies sent via mail")))
3041     (when (and force (not to))
3042       (error "No mail address in the article"))
3043     (when (and to (or force (not dont)))
3044       (message-carefully-insert-headers (list (cons 'To to))))))
3045
3046 (defun message-insert-wide-reply ()
3047   "Insert To and Cc headers as if you were doing a wide reply."
3048   (interactive)
3049   (let ((headers (message-with-reply-buffer
3050                    (message-get-reply-headers t))))
3051     (message-carefully-insert-headers headers)))
3052
3053 (defcustom message-header-synonyms
3054   '((To Cc Bcc)
3055     (Original-To))
3056   "List of lists of header synonyms.
3057 E.g., if this list contains a member list with elements `Cc' and `To',
3058 then `message-carefully-insert-headers' will not insert a `To' header
3059 when the message is already `Cc'ed to the recipient."
3060   :version "22.1"
3061   :group 'message-headers
3062   :link '(custom-manual "(message)Message Headers")
3063   :type '(repeat sexp))
3064
3065 (defun message-carefully-insert-headers (headers)
3066   "Insert the HEADERS, an alist, into the message buffer.
3067 Does not insert the headers when they are already present there
3068 or in the synonym headers, defined by `message-header-synonyms'."
3069   ;; FIXME: Should compare only the address and not the full name.  Comparison
3070   ;; should be done case-folded (and with `string=' rather than
3071   ;; `string-match').
3072   ;; (mail-strip-quoted-names "Foo Bar <foo@bar>, bla@fasel (Bla Fasel)")
3073   (dolist (header headers)
3074     (let* ((header-name (symbol-name (car header)))
3075            (new-header (cdr header))
3076            (synonyms (loop for synonym in message-header-synonyms
3077                            when (memq (car header) synonym) return synonym))
3078            (old-header
3079             (loop for synonym in synonyms
3080                   for old-header = (mail-fetch-field (symbol-name synonym))
3081                   when (and old-header (string-match new-header old-header))
3082                   return synonym)))
3083       (if old-header
3084           (message "already have `%s' in `%s'" new-header old-header)
3085         (when (and (message-position-on-field header-name)
3086                    (setq old-header (mail-fetch-field header-name))
3087                    (not (string-match "\\` *\\'" old-header)))
3088           (insert ", "))
3089         (insert new-header)))))
3090
3091 (defun message-widen-reply ()
3092   "Widen the reply to include maximum recipients."
3093   (interactive)
3094   (let ((follow-to
3095          (and message-reply-buffer
3096               (buffer-name message-reply-buffer)
3097               (with-current-buffer message-reply-buffer
3098                 (message-get-reply-headers t)))))
3099     (save-excursion
3100       (save-restriction
3101         (message-narrow-to-headers)
3102         (dolist (elem follow-to)
3103           (message-remove-header (symbol-name (car elem)))
3104           (goto-char (point-min))
3105           (insert (symbol-name (car elem)) ": "
3106                   (cdr elem) "\n"))))))
3107
3108 (defun message-insert-newsgroups ()
3109   "Insert the Newsgroups header from the article being replied to."
3110   (interactive)
3111   (when (and (message-position-on-field "Newsgroups")
3112              (mail-fetch-field "newsgroups")
3113              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
3114     (insert ","))
3115   (insert (or (message-fetch-reply-field "newsgroups") "")))
3116
3117 \f
3118
3119 ;;; Various commands
3120
3121 (defun message-delete-not-region (beg end)
3122   "Delete everything in the body of the current message outside of the region."
3123   (interactive "r")
3124   (let (citeprefix)
3125     (save-excursion
3126       (goto-char beg)
3127       ;; snarf citation prefix, if appropriate
3128       (unless (eq (point) (progn (beginning-of-line) (point)))
3129         (when (looking-at message-cite-prefix-regexp)
3130           (setq citeprefix (match-string 0))))
3131       (goto-char end)
3132       (delete-region (point) (if (not (message-goto-signature))
3133                                  (point)
3134                                (forward-line -2)
3135                                (point)))
3136       (insert "\n")
3137       (goto-char beg)
3138       (delete-region beg (progn (message-goto-body)
3139                                 (forward-line 2)
3140                                 (point)))
3141       (when citeprefix
3142         (insert citeprefix))))
3143   (when (message-goto-signature)
3144     (forward-line -2)))
3145
3146 (defun message-kill-to-signature (&optional arg)
3147   "Kill all text up to the signature.
3148 If a numberic argument or prefix arg is given, leave that number
3149 of lines before the signature intact."
3150   (interactive "P")
3151   (save-excursion
3152     (save-restriction
3153       (let ((point (point)))
3154         (narrow-to-region point (point-max))
3155         (message-goto-signature)
3156         (unless (eobp)
3157           (if (and arg (numberp arg))
3158               (forward-line (- -1 arg))
3159             (end-of-line -1)))
3160         (unless (= point (point))
3161           (kill-region point (point))
3162           (unless (bolp)
3163             (insert "\n")))))))
3164
3165 (defun message-newline-and-reformat (&optional arg not-break)
3166   "Insert four newlines, and then reformat if inside quoted text.
3167 Prefix arg means justify as well."
3168   (interactive (list (if current-prefix-arg 'full)))
3169   (let (quoted point beg end leading-space bolp fill-paragraph-function)
3170     (setq point (point))
3171     (beginning-of-line)
3172     (setq beg (point))
3173     (setq bolp (= beg point))
3174     ;; Find first line of the paragraph.
3175     (if not-break
3176         (while (and (not (eobp))
3177                     (not (looking-at message-cite-prefix-regexp))
3178                     (looking-at paragraph-start))
3179           (forward-line 1)))
3180     ;; Find the prefix
3181     (when (looking-at message-cite-prefix-regexp)
3182       (setq quoted (match-string 0))
3183       (goto-char (match-end 0))
3184       (looking-at "[ \t]*")
3185       (setq leading-space (match-string 0)))
3186     (if (and quoted
3187              (not not-break)
3188              (not bolp)
3189              (< (- point beg) (length quoted)))
3190         ;; break inside the cite prefix.
3191         (setq quoted nil
3192               end nil))
3193     (if quoted
3194         (progn
3195           (forward-line 1)
3196           (while (and (not (eobp))
3197                       (not (looking-at paragraph-separate))
3198                       (looking-at message-cite-prefix-regexp)
3199                       (equal quoted (match-string 0)))
3200             (goto-char (match-end 0))
3201             (looking-at "[ \t]*")
3202             (if (> (length leading-space) (length (match-string 0)))
3203                 (setq leading-space (match-string 0)))
3204             (forward-line 1))
3205           (setq end (point))
3206           (goto-char beg)
3207           (while (and (if (bobp) nil (forward-line -1) t)
3208                       (not (looking-at paragraph-start))
3209                       (looking-at message-cite-prefix-regexp)
3210                       (equal quoted (match-string 0)))
3211             (setq beg (point))
3212             (goto-char (match-end 0))
3213             (looking-at "[ \t]*")
3214             (if (> (length leading-space) (length (match-string 0)))
3215                 (setq leading-space (match-string 0)))))
3216       (while (and (not (eobp))
3217                   (not (looking-at paragraph-separate))
3218                   (not (looking-at message-cite-prefix-regexp)))
3219         (forward-line 1))
3220       (setq end (point))
3221       (goto-char beg)
3222       (while (and (if (bobp) nil (forward-line -1) t)
3223                   (not (looking-at paragraph-start))
3224                   (not (looking-at message-cite-prefix-regexp)))
3225         (setq beg (point))))
3226     (goto-char point)
3227     (save-restriction
3228       (narrow-to-region beg end)
3229       (if not-break
3230           (setq point nil)
3231         (if bolp
3232             (newline)
3233           (newline)
3234           (newline))
3235         (setq point (point))
3236         ;; (newline 2) doesn't mark both newline's as hard, so call
3237         ;; newline twice. -jas
3238         (newline)
3239         (newline)
3240         (delete-region (point) (re-search-forward "[ \t]*"))
3241         (when (and quoted (not bolp))
3242           (insert quoted leading-space)))
3243       (undo-boundary)
3244       (if quoted
3245           (let* ((adaptive-fill-regexp
3246                   (regexp-quote (concat quoted leading-space)))
3247                  (adaptive-fill-first-line-regexp
3248                   adaptive-fill-regexp ))
3249             (fill-paragraph arg))
3250         (fill-paragraph arg))
3251       (if point (goto-char point)))))
3252
3253 (defun message-fill-paragraph (&optional arg)
3254   "Message specific function to fill a paragraph.
3255 This function is used as the value of `fill-paragraph-function' in
3256 Message buffers and is not meant to be called directly."
3257   (interactive (list (if current-prefix-arg 'full)))
3258   (if (if (boundp 'filladapt-mode) filladapt-mode)
3259       nil
3260     (if (message-point-in-header-p)
3261         (message-fill-field)
3262       (message-newline-and-reformat arg t))
3263     t))
3264
3265 (defun message-point-in-header-p ()
3266   "Return t if point is in the header."
3267   (save-excursion
3268     (not (re-search-backward
3269           (concat "^" (regexp-quote mail-header-separator) "\n") nil t))))
3270
3271 (defun message-do-auto-fill ()
3272   "Like `do-auto-fill', but don't fill in message header."
3273   (unless (message-point-in-header-p)
3274     (do-auto-fill)))
3275
3276 (defun message-insert-signature (&optional force)
3277   "Insert a signature.  See documentation for variable `message-signature'."
3278   (interactive (list 0))
3279   (let* ((signature
3280           (cond
3281            ((and (null message-signature)
3282                  (eq force 0))
3283             (save-excursion
3284               (goto-char (point-max))
3285               (not (re-search-backward message-signature-separator nil t))))
3286            ((and (null message-signature)
3287                  force)
3288             t)
3289            ((functionp message-signature)
3290             (funcall message-signature))
3291            ((listp message-signature)
3292             (eval message-signature))
3293            (t message-signature)))
3294          signature-file)
3295     (setq signature
3296           (cond ((stringp signature)
3297                  signature)
3298                 ((and (eq t signature) message-signature-file)
3299                  (setq signature-file
3300                        (if (and message-signature-directory
3301                                 ;; don't actually use the signature directory
3302                                 ;; if message-signature-file contains a path.
3303                                 (not (file-name-directory
3304                                       message-signature-file)))
3305                            (nnheader-concat message-signature-directory
3306                                             message-signature-file)
3307                          message-signature-file))
3308                  (file-exists-p signature-file))))
3309     (when signature
3310       (goto-char (point-max))
3311       ;; Insert the signature.
3312       (unless (bolp)
3313         (insert "\n"))
3314       (when message-signature-insert-empty-line
3315         (insert "\n"))
3316       (insert "-- \n")
3317       (if (eq signature t)
3318           (insert-file-contents signature-file)
3319         (insert signature))
3320       (goto-char (point-max))
3321       (or (bolp) (insert "\n")))))
3322
3323 (defun message-insert-importance-high ()
3324   "Insert header to mark message as important."
3325   (interactive)
3326   (save-excursion
3327     (save-restriction
3328       (message-narrow-to-headers)
3329       (message-remove-header "Importance"))
3330     (message-goto-eoh)
3331     (insert "Importance: high\n")))
3332
3333 (defun message-insert-importance-low ()
3334   "Insert header to mark message as unimportant."
3335   (interactive)
3336   (save-excursion
3337     (save-restriction
3338       (message-narrow-to-headers)
3339       (message-remove-header "Importance"))
3340     (message-goto-eoh)
3341     (insert "Importance: low\n")))
3342
3343 (defun message-insert-or-toggle-importance ()
3344   "Insert a \"Importance: high\" header, or cycle through the header values.
3345 The three allowed values according to RFC 1327 are `high', `normal'
3346 and `low'."
3347   (interactive)
3348   (save-excursion
3349     (let ((new "high")
3350           cur)
3351       (save-restriction
3352         (message-narrow-to-headers)
3353         (when (setq cur (message-fetch-field "Importance"))
3354           (message-remove-header "Importance")
3355           (setq new (cond ((string= cur "high")
3356                            "low")
3357                           ((string= cur "low")
3358                            "normal")
3359                           (t
3360                            "high")))))
3361       (message-goto-eoh)
3362       (insert (format "Importance: %s\n" new)))))
3363
3364 (defun message-insert-disposition-notification-to ()
3365   "Request a disposition notification (return receipt) to this message.
3366 Note that this should not be used in newsgroups."
3367   (interactive)
3368   (save-excursion
3369     (save-restriction
3370       (message-narrow-to-headers)
3371       (message-remove-header "Disposition-Notification-To"))
3372     (message-goto-eoh)
3373     (insert (format "Disposition-Notification-To: %s\n"
3374                     (or (message-field-value "Reply-to")
3375                         (message-field-value "From")
3376                         (message-make-from))))))
3377
3378 (defun message-elide-region (b e)
3379   "Elide the text in the region.
3380 An ellipsis (from `message-elide-ellipsis') will be inserted where the
3381 text was killed."
3382   (interactive "r")
3383   (kill-region b e)
3384   (insert message-elide-ellipsis))
3385
3386 (defvar message-caesar-translation-table nil)
3387
3388 (defun message-caesar-region (b e &optional n)
3389   "Caesar rotate region B to E by N, default 13, for decrypting netnews."
3390   (interactive
3391    (list
3392     (min (point) (or (mark t) (point)))
3393     (max (point) (or (mark t) (point)))
3394     (when current-prefix-arg
3395       (prefix-numeric-value current-prefix-arg))))
3396
3397   (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
3398   (unless (or (zerop n)                 ; no action needed for a rot of 0
3399               (= b e))                  ; no region to rotate
3400     ;; We build the table, if necessary.
3401     (when (or (not message-caesar-translation-table)
3402               (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
3403       (setq message-caesar-translation-table
3404             (message-make-caesar-translation-table n)))
3405     (translate-region b e message-caesar-translation-table)))
3406
3407 (defun message-make-caesar-translation-table (n)
3408   "Create a rot table with offset N."
3409   (let ((i -1)
3410         (table (make-string 256 0)))
3411     (while (< (incf i) 256)
3412       (aset table i i))
3413     (concat
3414      (substring table 0 ?A)
3415      (substring table (+ ?A n) (+ ?A n (- 26 n)))
3416      (substring table ?A (+ ?A n))
3417      (substring table (+ ?A 26) ?a)
3418      (substring table (+ ?a n) (+ ?a n (- 26 n)))
3419      (substring table ?a (+ ?a n))
3420      (substring table (+ ?a 26) 255))))
3421
3422 (defun message-caesar-buffer-body (&optional rotnum wide)
3423   "Caesar rotate all letters in the current buffer by 13 places.
3424 Used to encode/decode possibly offensive messages (commonly in rec.humor).
3425 With prefix arg, specifies the number of places to rotate each letter forward.
3426 Mail and USENET news headers are not rotated unless WIDE is non-nil."
3427   (interactive (if current-prefix-arg
3428                    (list (prefix-numeric-value current-prefix-arg))
3429                  (list nil)))
3430   (save-excursion
3431     (save-restriction
3432       (when (and (not wide) (message-goto-body))
3433         (narrow-to-region (point) (point-max)))
3434       (message-caesar-region (point-min) (point-max) rotnum))))
3435
3436 (defun message-pipe-buffer-body (program)
3437   "Pipe the message body in the current buffer through PROGRAM."
3438   (save-excursion
3439     (save-restriction
3440       (when (message-goto-body)
3441         (narrow-to-region (point) (point-max)))
3442       (shell-command-on-region
3443        (point-min) (point-max) program nil t))))
3444
3445 (defun message-rename-buffer (&optional enter-string)
3446   "Rename the *message* buffer to \"*message* RECIPIENT\".
3447 If the function is run with a prefix, it will ask for a new buffer
3448 name, rather than giving an automatic name."
3449   (interactive "Pbuffer name: ")
3450   (save-excursion
3451     (save-restriction
3452       (goto-char (point-min))
3453       (narrow-to-region (point)
3454                         (search-forward mail-header-separator nil 'end))
3455       (let* ((mail-to (or
3456                        (if (message-news-p) (message-fetch-field "Newsgroups")
3457                          (message-fetch-field "To"))
3458                        ""))
3459              (mail-trimmed-to
3460               (if (string-match "," mail-to)
3461                   (concat (substring mail-to 0 (match-beginning 0)) ", ...")
3462                 mail-to))
3463              (name-default (concat "*message* " mail-trimmed-to))
3464              (name (if enter-string
3465                        (read-string "New buffer name: " name-default)
3466                      name-default)))
3467         (rename-buffer name t)))))
3468
3469 (defun message-fill-yanked-message (&optional justifyp)
3470   "Fill the paragraphs of a message yanked into this one.
3471 Numeric argument means justify as well."
3472   (interactive "P")
3473   (save-excursion
3474     (goto-char (point-min))
3475     (search-forward (concat "\n" mail-header-separator "\n") nil t)
3476     (let ((fill-prefix message-yank-prefix))
3477       (fill-individual-paragraphs (point) (point-max) justifyp))))
3478
3479 (defun message-indent-citation (&optional start end yank-only)
3480   "Modify text just inserted from a message to be cited.
3481 The inserted text should be the region.
3482 When this function returns, the region is again around the modified text.
3483
3484 Normally, indent each nonblank line `message-indentation-spaces' spaces.
3485 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
3486   (unless start (setq start (point)))
3487   (unless yank-only
3488     ;; Remove unwanted headers.
3489     (when message-ignored-cited-headers
3490       (let (all-removed)
3491         (save-restriction
3492           (narrow-to-region
3493            (goto-char start)
3494            (if (search-forward "\n\n" nil t)
3495                (1- (point))
3496              (point)))
3497           (message-remove-header message-ignored-cited-headers t)
3498           (when (= (point-min) (point-max))
3499             (setq all-removed t))
3500           (goto-char (point-max)))
3501         (if all-removed
3502             (goto-char start)
3503           (forward-line 1))))
3504     ;; Delete blank lines at the start of the buffer.
3505     (while (and (point-min)
3506                 (eolp)
3507                 (not (eobp)))
3508       (message-delete-line))
3509     ;; Delete blank lines at the end of the buffer.
3510     (goto-char (point-max))
3511     (unless (eolp)
3512       (insert "\n"))
3513     (while (and (zerop (forward-line -1))
3514                 (looking-at "$"))
3515       (message-delete-line)))
3516   ;; Do the indentation.
3517   (if (null message-yank-prefix)
3518       (indent-rigidly start (or end (mark t)) message-indentation-spaces)
3519     (save-excursion
3520       (goto-char start)
3521       (while (< (point) (or end (mark t)))
3522         (cond ((looking-at ">")
3523                (insert message-yank-cited-prefix))
3524               ((looking-at "^$")
3525                (insert message-yank-empty-prefix))
3526               (t
3527                (insert message-yank-prefix)))
3528         (forward-line 1))))
3529   (goto-char start))
3530
3531 (defun message-remove-blank-cited-lines (&optional remove)
3532   "Remove cited lines containing only blanks.
3533 If REMOVE is non-nil, remove newlines, too.
3534
3535 To use this automatically, you may add this function to
3536 `gnus-message-setup-hook'."
3537   (interactive "P")
3538   (let ((citexp
3539          (concat
3540           "^\\("
3541           (when (boundp 'message-yank-cited-prefix)
3542             (concat message-yank-cited-prefix "\\|"))
3543           message-yank-prefix
3544           "\\)+ *\n"
3545           )))
3546     (gnus-message 8 "removing `%s'" citexp)
3547     (save-excursion
3548       (message-goto-body)
3549       (while (re-search-forward citexp nil t)
3550         (replace-match (if remove "" "\n"))))))
3551
3552 (defvar message-cite-reply-above nil
3553   "If non-nil, start own text above the quote.
3554
3555 Note: Top posting is bad netiquette.  Don't use it unless you
3556 really must.  You probably want to set variable only for specific
3557 groups, e.g. using `gnus-posting-styles':
3558
3559   (eval (set (make-local-variable 'message-cite-reply-above) t))
3560
3561 This variable has no effect in news postings.")
3562
3563 (defun message-yank-original (&optional arg)
3564   "Insert the message being replied to, if any.
3565 Puts point before the text and mark after.
3566 Normally indents each nonblank line ARG spaces (default 3).  However,
3567 if `message-yank-prefix' is non-nil, insert that prefix on each line.
3568
3569 This function uses `message-cite-function' to do the actual citing.
3570
3571 Just \\[universal-argument] as argument means don't indent, insert no
3572 prefix, and don't delete any headers."
3573   (interactive "P")
3574   (let ((modified (buffer-modified-p))
3575         body-text)
3576     (when (and message-reply-buffer
3577                message-cite-function)
3578       (when message-cite-reply-above
3579         (if (and (not (message-news-p))
3580                  (or (eq message-cite-reply-above 'is-evil)
3581                      (y-or-n-p "\
3582 Top posting is bad netiquette.  Please don't top post unless you really must.
3583 Really top post? ")))
3584             (save-excursion
3585               (setq body-text
3586                     (buffer-substring (message-goto-body)
3587                                       (point-max)))
3588               (delete-region (message-goto-body) (point-max)))
3589           (set (make-local-variable 'message-cite-reply-above) nil)))
3590       (delete-windows-on message-reply-buffer t)
3591       (push-mark (save-excursion
3592                    (insert-buffer-substring message-reply-buffer)
3593                    (unless (bolp)
3594                      (insert ?\n))
3595                    (point)))
3596       (unless arg
3597         (funcall message-cite-function)
3598         (unless (eq (char-before (mark t)) ?\n)
3599           (let ((pt (point)))
3600             (goto-char (mark t))
3601             (insert-before-markers ?\n)
3602             (goto-char pt))))
3603       (when message-cite-reply-above
3604         (message-goto-body)
3605         (insert body-text)
3606         (insert (if (bolp) "\n" "\n\n"))
3607         (message-goto-body))
3608       ;; Add a `message-setup-very-last-hook' here?
3609       ;; Add `gnus-article-highlight-citation' here?
3610       (unless modified
3611         (setq message-checksum (message-checksum))))))
3612
3613 (defun message-yank-buffer (buffer)
3614   "Insert BUFFER into the current buffer and quote it."
3615   (interactive "bYank buffer: ")
3616   (let ((message-reply-buffer (get-buffer buffer)))
3617     (save-window-excursion
3618       (message-yank-original))))
3619
3620 (defun message-buffers ()
3621   "Return a list of active message buffers."
3622   (let (buffers)
3623     (save-current-buffer
3624       (dolist (buffer (buffer-list t))
3625         (set-buffer buffer)
3626         (when (and (eq major-mode 'message-mode)
3627                    (null message-sent-message-via))
3628           (push (buffer-name buffer) buffers))))
3629     (nreverse buffers)))
3630
3631 (defun message-cite-original-1 (strip-signature)
3632   "Cite an original message.
3633 If STRIP-SIGNATURE is non-nil, strips off the signature from the
3634 original message.
3635
3636 This function uses `mail-citation-hook' if that is non-nil."
3637   (if (and (boundp 'mail-citation-hook)
3638            mail-citation-hook)
3639       (run-hooks 'mail-citation-hook)
3640     (let* ((start (point))
3641            (end (mark t))
3642            (x-no-archive nil)
3643            (functions
3644             (when message-indent-citation-function
3645               (if (listp message-indent-citation-function)
3646                   message-indent-citation-function
3647                 (list message-indent-citation-function))))
3648            ;; This function may be called by `gnus-summary-yank-message' and
3649            ;; may insert a different article from the original.  So, we will
3650            ;; modify the value of `message-reply-headers' with that article.
3651            (message-reply-headers
3652             (save-restriction
3653               (narrow-to-region start end)
3654               (message-narrow-to-head-1)
3655               (setq x-no-archive (message-fetch-field "x-no-archive"))
3656               (vector 0
3657                       (or (message-fetch-field "subject") "none")
3658                       (or (message-fetch-field "from") "nobody")
3659                       (message-fetch-field "date")
3660                       (message-fetch-field "message-id" t)
3661                       (message-fetch-field "references")
3662                       0 0 ""))))
3663       (mml-quote-region start end)
3664       (when strip-signature
3665         ;; Allow undoing.
3666         (undo-boundary)
3667         (goto-char end)
3668         (when (re-search-backward message-signature-separator start t)
3669           ;; Also peel off any blank lines before the signature.
3670           (forward-line -1)
3671           (while (looking-at "^[ \t]*$")
3672             (forward-line -1))
3673           (forward-line 1)
3674           (delete-region (point) end)
3675           (unless (search-backward "\n\n" start t)
3676             ;; Insert a blank line if it is peeled off.
3677             (insert "\n"))))
3678       (goto-char start)
3679       (mapc 'funcall functions)
3680       (when message-citation-line-function
3681         (unless (bolp)
3682           (insert "\n"))
3683         (funcall message-citation-line-function))
3684       (when (and x-no-archive
3685                  (not message-cite-articles-with-x-no-archive)
3686                  (string-match "yes" x-no-archive))
3687         (undo-boundary)
3688         (delete-region (point) (mark t))
3689         (insert "> [Quoted text removed due to X-No-Archive]\n")
3690         (push-mark)
3691         (forward-line -1)))))
3692
3693 (defun message-cite-original ()
3694   "Cite function in the standard Message manner."
3695   (message-cite-original-1 nil))
3696
3697 (defvar gnus-extract-address-components)
3698
3699 (defun message-insert-formatted-citation-line (&optional from date)
3700   "Function that inserts a formatted citation line.
3701
3702 See `message-citation-line-format'."
3703   ;; The optional args are for testing/debugging.  They will disappear later.
3704   ;; Example:
3705   ;; (with-temp-buffer
3706   ;;   (message-insert-formatted-citation-line
3707   ;;    "John Doe <john.doe@example.invalid>"
3708   ;;    (current-time))
3709   ;;   (buffer-string))
3710   (when (or message-reply-headers (and from date))
3711     (unless from
3712       (setq from (mail-header-from message-reply-headers)))
3713     (let* ((data (condition-case ()
3714                      (funcall (if (boundp gnus-extract-address-components)
3715                                   gnus-extract-address-components
3716                                 'mail-extract-address-components)
3717                               from)
3718                    (error nil)))
3719            (name (car data))
3720            (fname name)
3721            (lname name)
3722            (net (car (cdr data)))
3723            (name-or-net (or (car data)
3724                             (car (cdr data)) from))
3725            (replydate
3726             (or
3727              date
3728              ;; We need Gnus functionality if the user wants date or time from
3729              ;; the original article:
3730              (when (string-match "%[^fnNFL]" message-citation-line-format)
3731                (autoload 'gnus-date-get-time "gnus-util")
3732                (gnus-date-get-time (mail-header-date message-reply-headers)))))
3733            (flist
3734             (let ((i ?A) lst)
3735               (when (stringp name)
3736                 ;; Guess first name and last name:
3737                 (cond ((string-match
3738                         "\\`\\(\\w\\|[-.]\\)+ \\(\\w\\|[-.]\\)+\\'" name)
3739                        (setq fname (nth 0 (split-string name "[ \t]+"))
3740                              lname (nth 1 (split-string name "[ \t]+"))))
3741                       ((string-match
3742                         "\\`\\(\\w\\|[-.]\\)+, \\(\\w\\|[-.]\\)+\\'" name)
3743                        (setq fname (nth 1 (split-string name "[ \t,]+"))
3744                              lname (nth 0 (split-string name "[ \t,]+"))))
3745                       ((string-match
3746                         "\\`\\(\\w\\|[-.]\\)+\\'" name)
3747                        (setq fname name
3748                              lname ""))))
3749               ;; The following letters are not used in `format-time-string':
3750               (push ?E lst) (push "<E>" lst)
3751               (push ?F lst) (push fname lst)
3752               ;; We might want to use "" instead of "<X>" later.
3753               (push ?J lst) (push "<J>" lst)
3754               (push ?K lst) (push "<K>" lst)
3755               (push ?L lst) (push lname lst)
3756               (push ?N lst) (push name-or-net lst)
3757               (push ?O lst) (push "<O>" lst)
3758               (push ?P lst) (push "<P>" lst)
3759               (push ?Q lst) (push "<Q>" lst)
3760               (push ?f lst) (push from lst)
3761               (push ?i lst) (push "<i>" lst)
3762               (push ?n lst) (push net lst)
3763               (push ?o lst) (push "<o>" lst)
3764               (push ?q lst) (push "<q>" lst)
3765               (push ?t lst) (push "<t>" lst)
3766               (push ?v lst) (push "<v>" lst)
3767               ;; Delegate the rest to `format-time-string':
3768               (while (<= i ?z)
3769                 (when (and (not (memq i lst))
3770                            ;; Skip (Z,a)
3771                            (or (<= i ?Z)
3772                                (>= i ?a)))
3773                   (push i lst)
3774                   (push (condition-case nil
3775                             (progn (format-time-string (format "%%%c" i)
3776                                                        replydate))
3777                           (format ">%c<" i))
3778                         lst))
3779                 (setq i (1+ i)))
3780               (reverse lst)))
3781            (spec (apply 'format-spec-make flist)))
3782       (insert (format-spec message-citation-line-format spec)))
3783     (newline)))
3784
3785 (defun message-cite-original-without-signature ()
3786   "Cite function in the standard Message manner.
3787 This function strips off the signature from the original message."
3788   (message-cite-original-1 t))
3789
3790 (defun message-insert-citation-line ()
3791   "Insert a simple citation line."
3792   (when message-reply-headers
3793     (insert (mail-header-from message-reply-headers) " writes:")
3794     (newline)
3795     (newline)))
3796
3797 (defun message-position-on-field (header &rest afters)
3798   (let ((case-fold-search t))
3799     (save-restriction
3800       (narrow-to-region
3801        (goto-char (point-min))
3802        (progn
3803          (re-search-forward
3804           (concat "^" (regexp-quote mail-header-separator) "$"))
3805          (match-beginning 0)))
3806       (goto-char (point-min))
3807       (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
3808           (progn
3809             (re-search-forward "^[^ \t]" nil 'move)
3810             (beginning-of-line)
3811             (skip-chars-backward "\n")
3812             t)
3813         (while (and afters
3814                     (not (re-search-forward
3815                           (concat "^" (regexp-quote (car afters)) ":")
3816                           nil t)))
3817           (pop afters))
3818         (when afters
3819           (re-search-forward "^[^ \t]" nil 'move)
3820           (beginning-of-line))
3821         (insert header ": \n")
3822         (forward-char -1)
3823         nil))))
3824
3825 (defun message-remove-signature ()
3826   "Remove the signature from the text between point and mark.
3827 The text will also be indented the normal way."
3828   (save-excursion
3829     (let ((start (point))
3830           mark)
3831       (if (not (re-search-forward message-signature-separator (mark t) t))
3832           ;; No signature here, so we just indent the cited text.
3833           (message-indent-citation)
3834         ;; Find the last non-empty line.
3835         (forward-line -1)
3836         (while (looking-at "[ \t]*$")
3837           (forward-line -1))
3838         (forward-line 1)
3839         (setq mark (set-marker (make-marker) (point)))
3840         (goto-char start)
3841         (message-indent-citation)
3842         ;; Enable undoing the deletion.
3843         (undo-boundary)
3844         (delete-region mark (mark t))
3845         (set-marker mark nil)))))
3846
3847 \f
3848
3849 ;;;
3850 ;;; Sending messages
3851 ;;;
3852
3853 (defun message-send-and-exit (&optional arg)
3854   "Send message like `message-send', then, if no errors, exit from mail buffer."
3855   (interactive "P")
3856   (let ((buf (current-buffer))
3857         (actions message-exit-actions))
3858     (when (and (message-send arg)
3859                (buffer-name buf))
3860       (if message-kill-buffer-on-exit
3861           (kill-buffer buf)
3862         (bury-buffer buf)
3863         (when (eq buf (current-buffer))
3864           (message-bury buf)))
3865       (message-do-actions actions)
3866       t)))
3867
3868 (defun message-dont-send ()
3869   "Don't send the message you have been editing.
3870 Instead, just auto-save the buffer and then bury it."
3871   (interactive)
3872   (set-buffer-modified-p t)
3873   (save-buffer)
3874   (let ((actions message-postpone-actions))
3875     (message-bury (current-buffer))
3876     (message-do-actions actions)))
3877
3878 (defun message-kill-buffer ()
3879   "Kill the current buffer."
3880   (interactive)
3881   (when (or (not (buffer-modified-p))
3882             (not message-kill-buffer-query)
3883             (yes-or-no-p "Message modified; kill anyway? "))
3884     (let ((actions message-kill-actions)
3885           (draft-article message-draft-article)
3886           (auto-save-file-name buffer-auto-save-file-name)
3887           (file-name buffer-file-name)
3888           (modified (buffer-modified-p)))
3889       (setq buffer-file-name nil)
3890       (kill-buffer (current-buffer))
3891       (when (and (or (and auto-save-file-name
3892                           (file-exists-p auto-save-file-name))
3893                      (and file-name
3894                           (file-exists-p file-name)))
3895                  (progn
3896                    ;; If the message buffer has lived in a dedicated window,
3897                    ;; `kill-buffer' has killed the frame.  Thus the
3898                    ;; `yes-or-no-p' may show up in a lowered frame.  Make sure
3899                    ;; that the user can see the question by raising the
3900                    ;; current frame:
3901                    (raise-frame)
3902                    (yes-or-no-p (format "Remove the backup file%s? "
3903                                         (if modified " too" "")))))
3904         (ignore-errors
3905           (delete-file auto-save-file-name))
3906         (let ((message-draft-article draft-article))
3907           (message-disassociate-draft)))
3908       (message-do-actions actions))))
3909
3910 (defun message-bury (buffer)
3911   "Bury this mail BUFFER."
3912   (let ((newbuf (other-buffer buffer)))
3913     (bury-buffer buffer)
3914     (if (and (window-dedicated-p (selected-window))
3915              (not (null (delq (selected-frame) (visible-frame-list)))))
3916         (delete-frame (selected-frame))
3917       (switch-to-buffer newbuf))))
3918
3919 (defun message-send (&optional arg)
3920   "Send the message in the current buffer.
3921 If `message-interactive' is non-nil, wait for success indication or
3922 error messages, and inform user.
3923 Otherwise any failure is reported in a message back to the user from
3924 the mailer.
3925 The usage of ARG is defined by the instance that called Message.
3926 It should typically alter the sending method in some way or other."
3927   (interactive "P")
3928   ;; Make it possible to undo the coming changes.
3929   (undo-boundary)
3930   (let ((inhibit-read-only t))
3931     (put-text-property (point-min) (point-max) 'read-only nil))
3932   (message-fix-before-sending)
3933   (run-hooks 'message-send-hook)
3934   (message message-sending-message)
3935   (let ((alist message-send-method-alist)
3936         (success t)
3937         elem sent dont-barf-on-no-method
3938         (message-options message-options))
3939     (message-options-set-recipient)
3940     (while (and success
3941                 (setq elem (pop alist)))
3942       (when (funcall (cadr elem))
3943         (when (and (or (not (memq (car elem)
3944                                   message-sent-message-via))
3945                        (message-fetch-field "supersedes")
3946                        (if (or (message-gnksa-enable-p 'multiple-copies)
3947                                (not (eq (car elem) 'news)))
3948                            (y-or-n-p
3949                             (format
3950                              "Already sent message via %s; resend? "
3951                              (car elem)))
3952                          (error "Denied posting -- multiple copies")))
3953                    (setq success (funcall (caddr elem) arg)))
3954           (setq sent t))))
3955     (unless (or sent
3956                 (not success)
3957                 (let ((fcc (message-fetch-field "Fcc"))
3958                       (gcc (message-fetch-field "Gcc")))
3959                   (when (or fcc gcc)
3960                     (or (eq message-allow-no-recipients 'always)
3961                         (and (not (eq message-allow-no-recipients 'never))
3962                              (setq dont-barf-on-no-method
3963                                    (gnus-y-or-n-p
3964                                     (format "No receiver, perform %s anyway? "
3965                                             (cond ((and fcc gcc) "Fcc and Gcc")
3966                                                   (fcc "Fcc")
3967                                                   (t "Gcc"))))))))))
3968       (error "No methods specified to send by"))
3969     (when (or dont-barf-on-no-method
3970               (and success sent))
3971       (message-do-fcc)
3972       (save-excursion
3973         (run-hooks 'message-sent-hook))
3974       (message "Sending...done")
3975       ;; Do ecomplete address snarfing.
3976       (when (message-mail-alias-type-p 'ecomplete)
3977         (message-put-addresses-in-ecomplete))
3978       ;; Mark the buffer as unmodified and delete auto-save.
3979       (set-buffer-modified-p nil)
3980       (delete-auto-save-file-if-necessary t)
3981       (message-disassociate-draft)
3982       ;; Delete other mail buffers and stuff.
3983       (message-do-send-housekeeping)
3984       (message-do-actions message-send-actions)
3985       ;; Return success.
3986       t)))
3987
3988 (defun message-send-via-mail (arg)
3989   "Send the current message via mail."
3990   (message-send-mail arg))
3991
3992 (defun message-send-via-news (arg)
3993   "Send the current message via news."
3994   (funcall message-send-news-function arg))
3995
3996 (defmacro message-check (type &rest forms)
3997   "Eval FORMS if TYPE is to be checked."
3998   `(or (message-check-element ,type)
3999        (save-excursion
4000          ,@forms)))
4001
4002 (put 'message-check 'lisp-indent-function 1)
4003 (put 'message-check 'edebug-form-spec '(form body))
4004
4005 (defun message-text-with-property (prop &optional start end reverse)
4006   "Return a list of start and end positions where the text has PROP.
4007 START and END bound the search, they default to `point-min' and
4008 `point-max' respectively.  If REVERSE is non-nil, find text which does
4009 not have PROP."
4010   (unless start
4011     (setq start (point-min)))
4012   (unless end
4013     (setq end (point-max)))
4014   (let (next regions)
4015     (if reverse
4016         (while (and start
4017                     (setq start (text-property-any start end prop nil)))
4018           (setq next (next-single-property-change start prop nil end))
4019           (push (cons start (or next end)) regions)
4020           (setq start next))
4021       (while (and start
4022                   (or (get-text-property start prop)
4023                       (and (setq start (next-single-property-change
4024                                         start prop nil end))
4025                            (get-text-property start prop))))
4026         (setq next (text-property-any start end prop nil))
4027         (push (cons start (or next end)) regions)
4028         (setq start next)))
4029     (nreverse regions)))
4030
4031 (defcustom message-bogus-address-regexp nil ;; "noreply\\|nospam\\|invalid"
4032   "Regexp of potentially bogus mail addresses."
4033   :version "23.0" ;; No Gnus
4034   :group 'message-headers
4035   :type 'regexp)
4036
4037 (defun message-fix-before-sending ()
4038   "Do various things to make the message nice before sending it."
4039   ;; Make sure there's a newline at the end of the message.
4040   (goto-char (point-max))
4041   (unless (bolp)
4042     (insert "\n"))
4043   ;; Make the hidden headers visible.
4044   (widen)
4045   ;; Sort headers before sending the message.
4046   (message-sort-headers)
4047   ;; Make invisible text visible.
4048   ;; It doesn't seem as if this is useful, since the invisible property
4049   ;; is clobbered by an after-change hook anyhow.
4050   (message-check 'invisible-text
4051     (let ((regions (message-text-with-property 'invisible))
4052           from to)
4053       (when regions
4054         (while regions
4055           (setq from (caar regions)
4056                 to (cdar regions)
4057                 regions (cdr regions))
4058           (put-text-property from to 'invisible nil)
4059           (message-overlay-put (message-make-overlay from to)
4060                                'face 'highlight))
4061         (unless (yes-or-no-p
4062                  "Invisible text found and made visible; continue sending? ")
4063           (error "Invisible text found and made visible")))))
4064   (message-check 'illegible-text
4065     (let (char found choice)
4066       (message-goto-body)
4067       (while (progn
4068                (skip-chars-forward mm-7bit-chars)
4069                (when (get-text-property (point) 'no-illegible-text)
4070                  ;; There is a signed or encrypted raw message part
4071                  ;; that is considered to be safe.
4072                  (goto-char (or (next-single-property-change
4073                                  (point) 'no-illegible-text)
4074                                 (point-max))))
4075                (setq char (char-after)))
4076         (when (or (< (mm-char-int char) 128)
4077                   (and (mm-multibyte-p)
4078                        (memq (char-charset char)
4079                              '(eight-bit-control eight-bit-graphic
4080                                                  control-1))
4081                        (not (get-text-property
4082                              (point) 'untranslated-utf-8))))
4083           (message-overlay-put (message-make-overlay (point) (1+ (point)))
4084                                'face 'highlight)
4085           (setq found t))
4086         (forward-char))
4087       (when found
4088         (setq choice
4089               (gnus-multiple-choice
4090                "Non-printable characters found.  Continue sending?"
4091                `((?d "Remove non-printable characters and send")
4092                  (?r ,(format
4093                        "Replace non-printable characters with \"%s\" and send"
4094                        message-replacement-char))
4095                  (?i "Ignore non-printable characters and send")
4096                  (?e "Continue editing"))))
4097         (if (eq choice ?e)
4098           (error "Non-printable characters"))
4099         (message-goto-body)
4100         (skip-chars-forward mm-7bit-chars)
4101         (while (not (eobp))
4102           (when (let ((char (char-after)))
4103                   (or (< (mm-char-int char) 128)
4104                       (and (mm-multibyte-p)
4105                            ;; FIXME: Wrong for Emacs 23 (unicode) and for
4106                            ;; things like undecable utf-8.  Should at least
4107                            ;; use find-coding-systems-region.
4108                            (memq (char-charset char)
4109                                  '(eight-bit-control eight-bit-graphic
4110                                                      control-1))
4111                            (not (get-text-property
4112                                  (point) 'untranslated-utf-8)))))
4113             (if (eq choice ?i)
4114                 (message-kill-all-overlays)
4115               (delete-char 1)
4116               (when (eq choice ?r)
4117                 (insert message-replacement-char))))
4118           (forward-char)
4119           (skip-chars-forward mm-7bit-chars)))))
4120   (message-check 'bogus-recipient
4121     ;; Warn before composing or sending a mail to an invalid address.
4122     (message-check-recipients)))
4123
4124 (defun message-bogus-recipient-p (recipients)
4125   "Check if a mail address in RECIPIENTS looks bogus.
4126
4127 RECIPIENTS is a mail header.  Return a list of potentially bogus
4128 addresses.  If none is found, return nil.
4129
4130 An addresses might be bogus if the domain part is not fully
4131 qualified, see `message-valid-fqdn-regexp', or if it matches
4132 `message-bogus-address-regexp'."
4133   ;; FIXME: How about "foo@subdomain", when the MTA adds ".domain.tld"?
4134   (let (found)
4135     (mapc (lambda (address)
4136             (setq address (cadr address))
4137             (when
4138                 (or (not
4139                      (or
4140                       (not (string-match "@" address))
4141                       (string-match
4142                        (concat ".@.*\\("
4143                                message-valid-fqdn-regexp "\\)\\'") address)))
4144                     (and (stringp message-bogus-address-regexp)
4145                          (string-match message-bogus-address-regexp address)))
4146               (push address found)))
4147           ;;
4148           (mail-extract-address-components recipients t))
4149     found))
4150
4151 (defun message-check-recipients ()
4152   "Warn before composing or sending a mail to an invalid address.
4153
4154 This function could be useful in `message-setup-hook'."
4155   (interactive)
4156   (save-restriction
4157     (message-narrow-to-headers)
4158     (dolist (hdr '("To" "Cc" "Bcc"))
4159       (let ((addr (message-fetch-field hdr)))
4160         (when (stringp addr)
4161           (dolist (bog (message-bogus-recipient-p addr))
4162             (and bog
4163                  (not (y-or-n-p
4164                        (format
4165                         "Address `%s' might be bogus.  Continue? " bog)))
4166                  (error "Bogus address."))))))))
4167
4168 (defun message-add-action (action &rest types)
4169   "Add ACTION to be performed when doing an exit of type TYPES."
4170   (while types
4171     (add-to-list (intern (format "message-%s-actions" (pop types)))
4172                  action)))
4173
4174 (defun message-delete-action (action &rest types)
4175   "Delete ACTION from lists of actions performed when doing an exit of type TYPES."
4176   (let (var)
4177     (while types
4178       (set (setq var (intern (format "message-%s-actions" (pop types))))
4179            (delq action (symbol-value var))))))
4180
4181 (defun message-do-actions (actions)
4182   "Perform all actions in ACTIONS."
4183   ;; Now perform actions on successful sending.
4184   (dolist (action actions)
4185     (ignore-errors
4186       (cond
4187        ;; A simple function.
4188        ((functionp action)
4189         (funcall action))
4190        ;; Something to be evaled.
4191        (t
4192         (eval action))))))
4193
4194 (defun message-send-mail-partially ()
4195   "Send mail as message/partial."
4196   ;; replace the header delimiter with a blank line
4197   (goto-char (point-min))
4198   (re-search-forward
4199    (concat "^" (regexp-quote mail-header-separator) "\n"))
4200   (replace-match "\n")
4201   (run-hooks 'message-send-mail-hook)
4202   (let ((p (goto-char (point-min)))
4203         (tembuf (message-generate-new-buffer-clone-locals " message temp"))
4204         (curbuf (current-buffer))
4205         (id (message-make-message-id)) (n 1)
4206         plist total  header required-mail-headers)
4207     (while (not (eobp))
4208       (if (< (point-max) (+ p message-send-mail-partially-limit))
4209           (goto-char (point-max))
4210         (goto-char (+ p message-send-mail-partially-limit))
4211         (beginning-of-line)
4212         (if (<= (point) p) (forward-line 1))) ;; In case of bad message.
4213       (push p plist)
4214       (setq p (point)))
4215     (setq total (length plist))
4216     (push (point-max) plist)
4217     (setq plist (nreverse plist))
4218     (unwind-protect
4219         (save-excursion
4220           (setq p (pop plist))
4221           (while plist
4222             (set-buffer curbuf)
4223             (copy-to-buffer tembuf p (car plist))
4224             (set-buffer tembuf)
4225             (goto-char (point-min))
4226             (if header
4227                 (progn
4228                   (goto-char (point-min))
4229                   (narrow-to-region (point) (point))
4230                   (insert header))
4231               (message-goto-eoh)
4232               (setq header (buffer-substring (point-min) (point)))
4233               (goto-char (point-min))
4234               (narrow-to-region (point) (point))
4235               (insert header)
4236               (message-remove-header "Mime-Version")
4237               (message-remove-header "Content-Type")
4238               (message-remove-header "Content-Transfer-Encoding")
4239               (message-remove-header "Message-ID")
4240               (message-remove-header "Lines")
4241               (goto-char (point-max))
4242               (insert "Mime-Version: 1.0\n")
4243               (setq header (buffer-string)))
4244             (goto-char (point-max))
4245             (insert (format "Content-Type: message/partial; id=\"%s\"; number=%d; total=%d\n\n"
4246                             id n total))
4247             (forward-char -1)
4248             (let ((mail-header-separator ""))
4249               (when (memq 'Message-ID message-required-mail-headers)
4250                 (insert "Message-ID: " (message-make-message-id) "\n"))
4251               (when (memq 'Lines message-required-mail-headers)
4252                 (insert "Lines: " (message-make-lines) "\n"))
4253               (message-goto-subject)
4254               (end-of-line)
4255               (insert (format " (%d/%d)" n total))
4256               (widen)
4257               (mm-with-unibyte-current-buffer
4258                 (funcall (or message-send-mail-real-function
4259                              message-send-mail-function))))
4260             (setq n (+ n 1))
4261             (setq p (pop plist))
4262             (erase-buffer)))
4263       (kill-buffer tembuf))))
4264
4265 (defun message-send-mail (&optional arg)
4266   (require 'mail-utils)
4267   (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
4268          (case-fold-search nil)
4269          (news (message-news-p))
4270          (mailbuf (current-buffer))
4271          (message-this-is-mail t)
4272          (message-posting-charset
4273           (if (fboundp 'gnus-setup-posting-charset)
4274               (gnus-setup-posting-charset nil)
4275             message-posting-charset))
4276          (headers message-required-mail-headers))
4277     (when (and message-generate-hashcash
4278                (not (eq message-generate-hashcash 'opportunistic)))
4279       (message "Generating hashcash...")
4280       ;; Wait for calculations already started to finish...
4281       (hashcash-wait-async)
4282       ;; ...and do calculations not already done.  mail-add-payment
4283       ;; will leave existing X-Hashcash headers alone.
4284       (mail-add-payment)
4285       (message "Generating hashcash...done"))
4286     (save-restriction
4287       (message-narrow-to-headers)
4288       ;; Generate the Mail-Followup-To header if the header is not there...
4289       (if (and (message-subscribed-p)
4290                (not (mail-fetch-field "mail-followup-to")))
4291           (setq headers
4292                 (cons
4293                  (cons "Mail-Followup-To" (message-make-mail-followup-to))
4294                  message-required-mail-headers))
4295         ;; otherwise, delete the MFT header if the field is empty
4296         (when (equal "" (mail-fetch-field "mail-followup-to"))
4297           (message-remove-header "^Mail-Followup-To:")))
4298       ;; Insert some headers.
4299       (let ((message-deletable-headers
4300              (if news nil message-deletable-headers)))
4301         (message-generate-headers headers))
4302       ;; Check continuation headers.
4303       (message-check 'continuation-headers
4304         (goto-char (point-min))
4305         (while (re-search-forward "^[^ \t\n][^ \t\n:]*[ \t\n]" nil t)
4306           (goto-char (match-beginning 0))
4307           (if (y-or-n-p "Fix continuation lines? ")
4308               (insert " ")
4309             (forward-line 1)
4310             (unless (y-or-n-p "Send anyway? ")
4311               (error "Failed to send the message")))))
4312       ;; Let the user do all of the above.
4313       (run-hooks 'message-header-hook))
4314     (unwind-protect
4315         (with-current-buffer tembuf
4316           (erase-buffer)
4317           ;; Avoid copying text props (except hard newlines).
4318           (insert (with-current-buffer mailbuf
4319                     (mml-buffer-substring-no-properties-except-hard-newlines
4320                      (point-min) (point-max))))
4321           ;; Remove some headers.
4322           (message-encode-message-body)
4323           (save-restriction
4324             (message-narrow-to-headers)
4325             ;; We (re)generate the Lines header.
4326             (when (memq 'Lines message-required-mail-headers)
4327               (message-generate-headers '(Lines)))
4328             ;; Remove some headers.
4329             (message-remove-header message-ignored-mail-headers t)
4330             (let ((mail-parse-charset message-default-charset))
4331               (mail-encode-encoded-word-buffer)))
4332           (goto-char (point-max))
4333           ;; require one newline at the end.
4334           (or (= (preceding-char) ?\n)
4335               (insert ?\n))
4336           (message-cleanup-headers)
4337           ;; FIXME: we're inserting the courtesy copy after encoding.
4338           ;; This is wrong if the courtesy copy string contains
4339           ;; non-ASCII characters. -- jh
4340           (when
4341               (save-restriction
4342                 (message-narrow-to-headers)
4343                 (and news
4344                      (or (message-fetch-field "cc")
4345                          (message-fetch-field "bcc")
4346                          (message-fetch-field "to"))
4347                      (let ((content-type (message-fetch-field
4348                                           "content-type")))
4349                        (and
4350                         (or
4351                          (not content-type)
4352                          (string= "text/plain"
4353                                   (car
4354                                    (mail-header-parse-content-type
4355                                     content-type))))
4356                         (not
4357                          (string= "base64"
4358                                   (message-fetch-field
4359                                    "content-transfer-encoding")))))))
4360             (message-insert-courtesy-copy))
4361           (if (or (not message-send-mail-partially-limit)
4362                   (< (buffer-size) message-send-mail-partially-limit)
4363                   (not (message-y-or-n-p
4364                         "The message size is too large, split? "
4365                         t
4366                         "\
4367 The message size, "
4368                         (/ (buffer-size) 1000) "KB, is too large.
4369
4370 Some mail gateways (MTA's) bounce large messages.  To avoid the
4371 problem, answer `y', and the message will be split into several
4372 smaller pieces, the size of each is about "
4373                         (/ message-send-mail-partially-limit 1000)
4374                         "KB except the last
4375 one.
4376
4377 However, some mail readers (MUA's) can't read split messages, i.e.,
4378 mails in message/partially format. Answer `n', and the message will be
4379 sent in one piece.
4380
4381 The size limit is controlled by `message-send-mail-partially-limit'.
4382 If you always want Gnus to send messages in one piece, set
4383 `message-send-mail-partially-limit' to nil.
4384 ")))
4385               (mm-with-unibyte-current-buffer
4386                 (message "Sending via mail...")
4387                 (funcall (or message-send-mail-real-function
4388                              message-send-mail-function)))
4389             (message-send-mail-partially)))
4390       (kill-buffer tembuf))
4391     (set-buffer mailbuf)
4392     (push 'mail message-sent-message-via)))
4393
4394 (defun message-send-mail-with-sendmail ()
4395   "Send off the prepared buffer with sendmail."
4396   (let ((errbuf (if message-interactive
4397                     (message-generate-new-buffer-clone-locals
4398                      " sendmail errors")
4399                   0))
4400         resend-to-addresses delimline)
4401     (unwind-protect
4402         (progn
4403           (let ((case-fold-search t))
4404             (save-restriction
4405               (message-narrow-to-headers)
4406               (setq resend-to-addresses (message-fetch-field "resent-to")))
4407             ;; Change header-delimiter to be what sendmail expects.
4408             (goto-char (point-min))
4409             (re-search-forward
4410              (concat "^" (regexp-quote mail-header-separator) "\n"))
4411             (replace-match "\n")
4412             (backward-char 1)
4413             (setq delimline (point-marker))
4414             (run-hooks 'message-send-mail-hook)
4415             ;; Insert an extra newline if we need it to work around
4416             ;; Sun's bug that swallows newlines.
4417             (goto-char (1+ delimline))
4418             (when (eval message-mailer-swallows-blank-line)
4419               (newline))
4420             (when message-interactive
4421               (with-current-buffer errbuf
4422                 (erase-buffer))))
4423           (let* ((default-directory "/")
4424                  (coding-system-for-write message-send-coding-system)
4425                  (cpr (apply
4426                        'call-process-region
4427                        (append
4428                         (list (point-min) (point-max)
4429                               (cond ((boundp 'sendmail-program)
4430                                      sendmail-program)
4431                                     ((file-exists-p "/usr/sbin/sendmail")
4432                                      "/usr/sbin/sendmail")
4433                                     ((file-exists-p "/usr/lib/sendmail")
4434                                      "/usr/lib/sendmail")
4435                                     ((file-exists-p "/usr/ucblib/sendmail")
4436                                      "/usr/ucblib/sendmail")
4437                                     (t "fakemail"))
4438                               nil errbuf nil "-oi")
4439                         message-sendmail-extra-arguments
4440                         ;; Always specify who from,
4441                         ;; since some systems have broken sendmails.
4442                         ;; But some systems are more broken with -f, so
4443                         ;; we'll let users override this.
4444                         (if (null message-sendmail-f-is-evil)
4445                             (list "-f" (message-sendmail-envelope-from)))
4446                         ;; These mean "report errors by mail"
4447                         ;; and "deliver in background".
4448                         (if (null message-interactive) '("-oem" "-odb"))
4449                         ;; Get the addresses from the message
4450                         ;; unless this is a resend.
4451                         ;; We must not do that for a resend
4452                         ;; because we would find the original addresses.
4453                         ;; For a resend, include the specific addresses.
4454                         (if resend-to-addresses
4455                             (list resend-to-addresses)
4456                           '("-t"))))))
4457             (unless (or (null cpr) (and (numberp cpr) (zerop cpr)))
4458               (error "Sending...failed with exit value %d" cpr)))
4459           (when message-interactive
4460             (with-current-buffer errbuf
4461               (goto-char (point-min))
4462               (while (re-search-forward "\n+ *" nil t)
4463                 (replace-match "; "))
4464               (if (not (zerop (buffer-size)))
4465                   (error "Sending...failed to %s"
4466                          (buffer-string))))))
4467       (when (bufferp errbuf)
4468         (kill-buffer errbuf)))))
4469
4470 (defun message-send-mail-with-qmail ()
4471   "Pass the prepared message buffer to qmail-inject.
4472 Refer to the documentation for the variable `message-send-mail-function'
4473 to find out how to use this."
4474   ;; replace the header delimiter with a blank line
4475   (goto-char (point-min))
4476   (re-search-forward
4477    (concat "^" (regexp-quote mail-header-separator) "\n"))
4478   (replace-match "\n")
4479   (run-hooks 'message-send-mail-hook)
4480   ;; send the message
4481   (case
4482       (let ((coding-system-for-write message-send-coding-system))
4483         (apply
4484          'call-process-region (point-min) (point-max)
4485          message-qmail-inject-program nil nil nil
4486          ;; qmail-inject's default behaviour is to look for addresses on the
4487          ;; command line; if there're none, it scans the headers.
4488          ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
4489          ;;
4490          ;; in general, ALL of qmail-inject's defaults are perfect for simply
4491          ;; reading a formatted (i. e., at least a To: or Resent-To header)
4492          ;; message from stdin.
4493          ;;
4494          ;; qmail also has the advantage of not having been raped by
4495          ;; various vendors, so we don't have to allow for that, either --
4496          ;; compare this with message-send-mail-with-sendmail and weep
4497          ;; for sendmail's lost innocence.
4498          ;;
4499          ;; all this is way cool coz it lets us keep the arguments entirely
4500          ;; free for -inject-arguments -- a big win for the user and for us
4501          ;; since we don't have to play that double-guessing game and the user
4502          ;; gets full control (no gestapo'ish -f's, for instance).  --sj
4503          (if (functionp message-qmail-inject-args)
4504              (funcall message-qmail-inject-args)
4505            message-qmail-inject-args)))
4506     ;; qmail-inject doesn't say anything on it's stdout/stderr,
4507     ;; we have to look at the retval instead
4508     (0 nil)
4509     (100 (error "qmail-inject reported permanent failure"))
4510     (111 (error "qmail-inject reported transient failure"))
4511     ;; should never happen
4512     (t   (error "qmail-inject reported unknown failure"))))
4513
4514 (defun message-send-mail-with-mh ()
4515   "Send the prepared message buffer with mh."
4516   (let ((mh-previous-window-config nil)
4517         (name (mh-new-draft-name)))
4518     (setq buffer-file-name name)
4519     ;; MH wants to generate these headers itself.
4520     (when message-mh-deletable-headers
4521       (let ((headers message-mh-deletable-headers))
4522         (while headers
4523           (goto-char (point-min))
4524           (and (re-search-forward
4525                 (concat "^" (symbol-name (car headers)) ": *") nil t)
4526                (message-delete-line))
4527           (pop headers))))
4528     (run-hooks 'message-send-mail-hook)
4529     ;; Pass it on to mh.
4530     (mh-send-letter)))
4531
4532 (defun message-smtpmail-send-it ()
4533   "Send the prepared message buffer with `smtpmail-send-it'.
4534 This only differs from `smtpmail-send-it' that this command evaluates
4535 `message-send-mail-hook' just before sending a message.  It is useful
4536 if your ISP requires the POP-before-SMTP authentication.  See the Gnus
4537 manual for details."
4538   (run-hooks 'message-send-mail-hook)
4539   (smtpmail-send-it))
4540
4541 (defun message-send-mail-with-mailclient ()
4542   "Send the prepared message buffer with `mailclient-send-it'.
4543 This only differs from `smtpmail-send-it' that this command evaluates
4544 `message-send-mail-hook' just before sending a message."
4545   (run-hooks 'message-send-mail-hook)
4546   (mailclient-send-it))
4547
4548 (defun message-canlock-generate ()
4549   "Return a string that is non-trivial to guess.
4550 Do not use this for anything important, it is cryptographically weak."
4551   (require 'sha1)
4552   (let (sha1-maximum-internal-length)
4553     (sha1 (concat (message-unique-id)
4554                   (format "%x%x%x" (random) (random t) (random))
4555                   (prin1-to-string (recent-keys))
4556                   (prin1-to-string (garbage-collect))))))
4557
4558 (defun message-canlock-password ()
4559   "The password used by message for cancel locks.
4560 This is the value of `canlock-password', if that option is non-nil.
4561 Otherwise, generate and save a value for `canlock-password' first."
4562   (unless canlock-password
4563     (customize-save-variable 'canlock-password (message-canlock-generate))
4564     (setq canlock-password-for-verify canlock-password))
4565   canlock-password)
4566
4567 (defun message-insert-canlock ()
4568   (when message-insert-canlock
4569     (message-canlock-password)
4570     (canlock-insert-header)))
4571
4572 (defun message-send-news (&optional arg)
4573   (let* ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
4574          (case-fold-search nil)
4575          (method (if (functionp message-post-method)
4576                      (funcall message-post-method arg)
4577                    message-post-method))
4578          (newsgroups-field (save-restriction
4579                             (message-narrow-to-headers-or-head)
4580                             (message-fetch-field "Newsgroups")))
4581          (followup-field (save-restriction
4582                            (message-narrow-to-headers-or-head)
4583                            (message-fetch-field "Followup-To")))
4584          ;; BUG: We really need to get the charset for each name in the
4585          ;; Newsgroups and Followup-To lines to allow crossposting
4586          ;; between group namess with incompatible character sets.
4587          ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
4588          (group-field-charset
4589           (gnus-group-name-charset method newsgroups-field))
4590          (followup-field-charset
4591           (gnus-group-name-charset method (or followup-field "")))
4592          (rfc2047-header-encoding-alist
4593           (append (when group-field-charset
4594                     (list (cons "Newsgroups" group-field-charset)))
4595                   (when followup-field-charset
4596                     (list (cons "Followup-To" followup-field-charset)))
4597                   rfc2047-header-encoding-alist))
4598          (messbuf (current-buffer))
4599          (message-syntax-checks
4600           (if (and arg
4601                    (listp message-syntax-checks))
4602               (cons '(existing-newsgroups . disabled)
4603                     message-syntax-checks)
4604             message-syntax-checks))
4605          (message-this-is-news t)
4606          (message-posting-charset
4607           (gnus-setup-posting-charset newsgroups-field))
4608          result)
4609     (if (not (message-check-news-body-syntax))
4610         nil
4611       (save-restriction
4612         (message-narrow-to-headers)
4613         ;; Insert some headers.
4614         (message-generate-headers message-required-news-headers)
4615         (message-insert-canlock)
4616         ;; Let the user do all of the above.
4617         (run-hooks 'message-header-hook))
4618       ;; Note: This check will be disabled by the ".*" default value for
4619       ;; gnus-group-name-charset-group-alist. -- Pa 2001-10-07.
4620       (when (and group-field-charset
4621                  (listp message-syntax-checks))
4622         (setq message-syntax-checks
4623               (cons '(valid-newsgroups . disabled)
4624                     message-syntax-checks)))
4625       (message-cleanup-headers)
4626       (if (not (let ((message-post-method method))
4627                  (message-check-news-syntax)))
4628           nil
4629         (unwind-protect
4630             (with-current-buffer tembuf
4631               (buffer-disable-undo)
4632               (erase-buffer)
4633               ;; Avoid copying text props (except hard newlines).
4634               (insert
4635                (with-current-buffer messbuf
4636                  (mml-buffer-substring-no-properties-except-hard-newlines
4637                   (point-min) (point-max))))
4638               (message-encode-message-body)
4639               ;; Remove some headers.
4640               (save-restriction
4641                 (message-narrow-to-headers)
4642                 ;; We (re)generate the Lines header.
4643                 (when (memq 'Lines message-required-mail-headers)
4644                   (message-generate-headers '(Lines)))
4645                 ;; Remove some headers.
4646                 (message-remove-header message-ignored-news-headers t)
4647                 (let ((mail-parse-charset message-default-charset))
4648                   (mail-encode-encoded-word-buffer)))
4649               (goto-char (point-max))
4650               ;; require one newline at the end.
4651               (or (= (preceding-char) ?\n)
4652                   (insert ?\n))
4653               (let ((case-fold-search t))
4654                 ;; Remove the delimiter.
4655                 (goto-char (point-min))
4656                 (re-search-forward
4657                  (concat "^" (regexp-quote mail-header-separator) "\n"))
4658                 (replace-match "\n")
4659                 (backward-char 1))
4660               (run-hooks 'message-send-news-hook)
4661               (gnus-open-server method)
4662               (message "Sending news via %s..." (gnus-server-string method))
4663               (setq result (let ((mail-header-separator ""))
4664                              (gnus-request-post method))))
4665           (kill-buffer tembuf))
4666         (set-buffer messbuf)
4667         (if result
4668             (push 'news message-sent-message-via)
4669           (message "Couldn't send message via news: %s"
4670                    (nnheader-get-report (car method)))
4671           nil)))))
4672
4673 ;;;
4674 ;;; Header generation & syntax checking.
4675 ;;;
4676
4677 (defun message-check-element (type)
4678   "Return non-nil if this TYPE is not to be checked."
4679   (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
4680       t
4681     (let ((able (assq type message-syntax-checks)))
4682       (and (consp able)
4683            (eq (cdr able) 'disabled)))))
4684
4685 (defun message-check-news-syntax ()
4686   "Check the syntax of the message."
4687   (save-excursion
4688     (save-restriction
4689       (widen)
4690       ;; We narrow to the headers and check them first.
4691       (save-excursion
4692         (save-restriction
4693           (message-narrow-to-headers)
4694           (message-check-news-header-syntax))))))
4695
4696 (defun message-check-news-header-syntax ()
4697   (and
4698    ;; Check Newsgroups header.
4699    (message-check 'newsgroups
4700      (let ((group (message-fetch-field "newsgroups")))
4701        (or
4702         (and group
4703              (not (string-match "\\`[ \t]*\\'" group)))
4704         (ignore
4705          (message
4706           "The newsgroups field is empty or missing.  Posting is denied.")))))
4707    ;; Check the Subject header.
4708    (message-check 'subject
4709      (let* ((case-fold-search t)
4710             (subject (message-fetch-field "subject")))
4711        (or
4712         (and subject
4713              (not (string-match "\\`[ \t]*\\'" subject)))
4714         (ignore
4715          (message
4716           "The subject field is empty or missing.  Posting is denied.")))))
4717    ;; Check for commands in Subject.
4718    (message-check 'subject-cmsg
4719      (if (string-match "^cmsg " (message-fetch-field "subject"))
4720          (y-or-n-p
4721           "The control code \"cmsg\" is in the subject.  Really post? ")
4722        t))
4723    ;; Check long header lines.
4724    (message-check 'long-header-lines
4725      (let ((start (point))
4726            (header nil)
4727            (length 0)
4728            found)
4729        (while (and (not found)
4730                    (re-search-forward "^\\([^ \t:]+\\): " nil t))
4731          (if (> (- (point) (match-beginning 0)) 998)
4732              (setq found t
4733                    length (- (point) (match-beginning 0)))
4734            (setq header (match-string-no-properties 1)))
4735          (setq start (match-beginning 0))
4736          (forward-line 1))
4737        (if found
4738            (y-or-n-p (format "Your %s header is too long (%d).  Really post? "
4739                              header length))
4740          t)))
4741    ;; Check for multiple identical headers.
4742    (message-check 'multiple-headers
4743      (let (found)
4744        (while (and (not found)
4745                    (re-search-forward "^[^ \t:]+: " nil t))
4746          (save-excursion
4747            (or (re-search-forward
4748                 (concat "^"
4749                         (regexp-quote
4750                          (setq found
4751                                (buffer-substring
4752                                 (match-beginning 0) (- (match-end 0) 2))))
4753                         ":")
4754                 nil t)
4755                (setq found nil))))
4756        (if found
4757            (y-or-n-p (format "Multiple %s headers.  Really post? " found))
4758          t)))
4759    ;; Check for Version and Sendsys.
4760    (message-check 'sendsys
4761      (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
4762          (y-or-n-p
4763           (format "The article contains a %s command.  Really post? "
4764                   (buffer-substring (match-beginning 0)
4765                                     (1- (match-end 0)))))
4766        t))
4767    ;; See whether we can shorten Followup-To.
4768    (message-check 'shorten-followup-to
4769      (let ((newsgroups (message-fetch-field "newsgroups"))
4770            (followup-to (message-fetch-field "followup-to"))
4771            to)
4772        (when (and newsgroups
4773                   (string-match "," newsgroups)
4774                   (not followup-to)
4775                   (not
4776                    (zerop
4777                     (length
4778                      (setq to (completing-read
4779                                "Followups to (default no Followup-To header): "
4780                                (mapcar #'list
4781                                        (cons "poster"
4782                                              (message-tokenize-header
4783                                               newsgroups)))))))))
4784          (goto-char (point-min))
4785          (insert "Followup-To: " to "\n"))
4786        t))
4787    ;; Check "Shoot me".
4788    (message-check 'shoot
4789      (if (re-search-forward
4790           "Message-ID.*.i-did-not-set--mail-host-address--so-tickle-me" nil t)
4791          (y-or-n-p "You appear to have a misconfigured system.  Really post? ")
4792        t))
4793    ;; Check for Approved.
4794    (message-check 'approved
4795      (if (re-search-forward "^Approved:" nil t)
4796          (y-or-n-p "The article contains an Approved header.  Really post? ")
4797        t))
4798    ;; Check the Message-ID header.
4799    (message-check 'message-id
4800      (let* ((case-fold-search t)
4801             (message-id (message-fetch-field "message-id" t)))
4802        (or (not message-id)
4803            ;; Is there an @ in the ID?
4804            (and (string-match "@" message-id)
4805                 ;; Is there a dot in the ID?
4806                 (string-match "@[^.]*\\." message-id)
4807                 ;; Does the ID end with a dot?
4808                 (not (string-match "\\.>" message-id)))
4809            (y-or-n-p
4810             (format "The Message-ID looks strange: \"%s\".  Really post? "
4811                     message-id)))))
4812    ;; Check the Newsgroups & Followup-To headers.
4813    (message-check 'existing-newsgroups
4814      (let* ((case-fold-search t)
4815             (newsgroups (message-fetch-field "newsgroups"))
4816             (followup-to (message-fetch-field "followup-to"))
4817             (groups (message-tokenize-header
4818                      (if followup-to
4819                          (concat newsgroups "," followup-to)
4820                        newsgroups)))
4821             (post-method (if (functionp message-post-method)
4822                              (funcall message-post-method)
4823                            message-post-method))
4824             ;; KLUDGE to handle nnvirtual groups.  Doing this right
4825             ;; would probably involve a new nnoo function.
4826             ;; -- Per Abrahamsen <abraham@dina.kvl.dk>, 2001-10-17.
4827             (method (if (and (consp post-method)
4828                              (eq (car post-method) 'nnvirtual)
4829                              gnus-message-group-art)
4830                         (let ((group (car (nnvirtual-find-group-art
4831                                            (car gnus-message-group-art)
4832                                            (cdr gnus-message-group-art)))))
4833                           (gnus-find-method-for-group group))
4834                       post-method))
4835             (known-groups
4836              (mapcar (lambda (n)
4837                        (gnus-group-name-decode
4838                         (gnus-group-real-name n)
4839                         (gnus-group-name-charset method n)))
4840                      (gnus-groups-from-server method)))
4841             errors)
4842        (while groups
4843          (when (and (not (equal (car groups) "poster"))
4844                     (not (member (car groups) known-groups))
4845                     (not (member (car groups) errors)))
4846            (push (car groups) errors))
4847          (pop groups))
4848        (cond
4849         ;; Gnus is not running.
4850         ((or (not (and (boundp 'gnus-active-hashtb)
4851                        gnus-active-hashtb))
4852              (not (boundp 'gnus-read-active-file)))
4853          t)
4854         ;; We don't have all the group names.
4855         ((and (or (not gnus-read-active-file)
4856                   (eq gnus-read-active-file 'some))
4857               errors)
4858          (y-or-n-p
4859           (format
4860            "Really use %s possibly unknown group%s: %s? "
4861            (if (= (length errors) 1) "this" "these")
4862            (if (= (length errors) 1) "" "s")
4863            (mapconcat 'identity errors ", "))))
4864         ;; There were no errors.
4865         ((not errors)
4866          t)
4867         ;; There are unknown groups.
4868         (t
4869          (y-or-n-p
4870           (format
4871            "Really post to %s unknown group%s: %s? "
4872            (if (= (length errors) 1) "this" "these")
4873            (if (= (length errors) 1) "" "s")
4874            (mapconcat 'identity errors ", ")))))))
4875    ;; Check continuation headers.
4876    (message-check 'continuation-headers
4877      (goto-char (point-min))
4878      (let ((do-posting t))
4879        (while (re-search-forward "^[^ \t\n][^ \t\n:]*[ \t\n]" nil t)
4880          (goto-char (match-beginning 0))
4881          (if (y-or-n-p "Fix continuation lines? ")
4882              (insert " ")
4883            (forward-line 1)
4884            (unless (y-or-n-p "Send anyway? ")
4885              (setq do-posting nil))))
4886        do-posting))
4887    ;; Check the Newsgroups & Followup-To headers for syntax errors.
4888    (message-check 'valid-newsgroups
4889      (let ((case-fold-search t)
4890            (headers '("Newsgroups" "Followup-To"))
4891            header error)
4892        (while (and headers (not error))
4893          (when (setq header (mail-fetch-field (car headers)))
4894            (if (or
4895                 (not
4896                  (string-match
4897                   "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-+_&.a-zA-Z0-9]+\\)*\\'"
4898                   header))
4899                 (memq
4900                  nil (mapcar
4901                       (lambda (g)
4902                         (not (string-match "\\.\\'\\|\\.\\." g)))
4903                       (message-tokenize-header header ","))))
4904                (setq error t)))
4905          (unless error
4906            (pop headers)))
4907        (if (not error)
4908            t
4909          (y-or-n-p
4910           (format "The %s header looks odd: \"%s\".  Really post? "
4911                   (car headers) header)))))
4912    (message-check 'repeated-newsgroups
4913      (let ((case-fold-search t)
4914            (headers '("Newsgroups" "Followup-To"))
4915            header error groups group)
4916        (while (and headers
4917                    (not error))
4918          (when (setq header (mail-fetch-field (pop headers)))
4919            (setq groups (message-tokenize-header header ","))
4920            (while (setq group (pop groups))
4921              (when (member group groups)
4922                (setq error group
4923                      groups nil)))))
4924        (if (not error)
4925            t
4926          (y-or-n-p
4927           (format "Group %s is repeated in headers.  Really post? " error)))))
4928    ;; Check the From header.
4929    (message-check 'from
4930      (let* ((case-fold-search t)
4931             (from (message-fetch-field "from"))
4932             ad)
4933        (cond
4934         ((not from)
4935          (message "There is no From line.  Posting is denied.")
4936          nil)
4937         ((or (not (string-match
4938                    "@[^\\.]*\\."
4939                    (setq ad (nth 1 (mail-extract-address-components
4940                                     from))))) ;larsi@ifi
4941              (string-match "\\.\\." ad) ;larsi@ifi..uio
4942              (string-match "@\\." ad)   ;larsi@.ifi.uio
4943              (string-match "\\.$" ad)   ;larsi@ifi.uio.
4944              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
4945              (string-match "(.*).*(.*)" from)) ;(lars) (lars)
4946          (message
4947           "Denied posting -- the From looks strange: \"%s\"." from)
4948          nil)
4949         ((let ((addresses (rfc822-addresses from)))
4950            (while (and addresses
4951                        (not (eq (string-to-char (car addresses)) ?\()))
4952              (setq addresses (cdr addresses)))
4953            addresses)
4954          (message
4955           "Denied posting -- bad From address: \"%s\"." from)
4956          nil)
4957         (t t))))
4958    ;; Check the Reply-To header.
4959    (message-check 'reply-to
4960      (let* ((case-fold-search t)
4961             (reply-to (message-fetch-field "reply-to"))
4962             ad)
4963        (cond
4964         ((not reply-to)
4965          t)
4966         ((string-match "," reply-to)
4967          (y-or-n-p
4968           (format "Multiple Reply-To addresses: \"%s\". Really post? "
4969                   reply-to)))
4970         ((or (not (string-match
4971                    "@[^\\.]*\\."
4972                    (setq ad (nth 1 (mail-extract-address-components
4973                                     reply-to))))) ;larsi@ifi
4974              (string-match "\\.\\." ad) ;larsi@ifi..uio
4975              (string-match "@\\." ad)   ;larsi@.ifi.uio
4976              (string-match "\\.$" ad)   ;larsi@ifi.uio.
4977              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
4978              (string-match "(.*).*(.*)" reply-to)) ;(lars) (lars)
4979          (y-or-n-p
4980           (format
4981            "The Reply-To looks strange: \"%s\". Really post? "
4982            reply-to)))
4983         (t t))))))
4984
4985 (defun message-check-news-body-syntax ()
4986   (and
4987    ;; Check for long lines.
4988    (message-check 'long-lines
4989      (goto-char (point-min))
4990      (re-search-forward
4991       (concat "^" (regexp-quote mail-header-separator) "$"))
4992      (forward-line 1)
4993      (while (and
4994              (or (looking-at
4995                   "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)")
4996                  (let ((p (point)))
4997                    (end-of-line)
4998                    (< (- (point) p) 80)))
4999              (zerop (forward-line 1))))
5000      (or (bolp)
5001          (eobp)
5002          (y-or-n-p
5003           "You have lines longer than 79 characters.  Really post? ")))
5004    ;; Check whether the article is empty.
5005    (message-check 'empty
5006      (goto-char (point-min))
5007      (re-search-forward
5008       (concat "^" (regexp-quote mail-header-separator) "$"))
5009      (forward-line 1)
5010      (let ((b (point)))
5011        (goto-char (point-max))
5012        (re-search-backward message-signature-separator nil t)
5013        (beginning-of-line)
5014        (or (re-search-backward "[^ \n\t]" b t)
5015            (if (message-gnksa-enable-p 'empty-article)
5016                (y-or-n-p "Empty article.  Really post? ")
5017              (message "Denied posting -- Empty article.")
5018              nil))))
5019    ;; Check for control characters.
5020    (message-check 'control-chars
5021      (if (re-search-forward
5022           (mm-string-to-multibyte "[\000-\007\013\015-\032\034-\037\200-\237]")
5023           nil t)
5024          (y-or-n-p
5025           "The article contains control characters.  Really post? ")
5026        t))
5027    ;; Check excessive size.
5028    (message-check 'size
5029      (if (> (buffer-size) 60000)
5030          (y-or-n-p
5031           (format "The article is %d octets long.  Really post? "
5032                   (buffer-size)))
5033        t))
5034    ;; Check whether any new text has been added.
5035    (message-check 'new-text
5036      (or
5037       (not message-checksum)
5038       (not (eq (message-checksum) message-checksum))
5039       (if (message-gnksa-enable-p 'quoted-text-only)
5040           (y-or-n-p
5041            "It looks like no new text has been added.  Really post? ")
5042         (message "Denied posting -- no new text has been added.")
5043         nil)))
5044    ;; Check the length of the signature.
5045    (message-check 'signature
5046      (goto-char (point-max))
5047      (if (> (count-lines (point) (point-max)) 5)
5048          (y-or-n-p
5049           (format
5050            "Your .sig is %d lines; it should be max 4.  Really post? "
5051            (1- (count-lines (point) (point-max)))))
5052        t))
5053    ;; Ensure that text follows last quoted portion.
5054    (message-check 'quoting-style
5055      (goto-char (point-max))
5056      (let ((no-problem t))
5057        (when (search-backward-regexp "^>[^\n]*\n" nil t)
5058          (setq no-problem (search-forward-regexp "^[ \t]*[^>\n]" nil t)))
5059        (if no-problem
5060            t
5061          (if (message-gnksa-enable-p 'quoted-text-only)
5062              (y-or-n-p "Your text should follow quoted text.  Really post? ")
5063            ;; Ensure that
5064            (goto-char (point-min))
5065            (re-search-forward
5066             (concat "^" (regexp-quote mail-header-separator) "$"))
5067            (if (search-forward-regexp "^[ \t]*[^>\n]" nil t)
5068                (y-or-n-p "Your text should follow quoted text.  Really post? ")
5069              (message "Denied posting -- only quoted text.")
5070              nil)))))))
5071
5072 (defun message-checksum ()
5073   "Return a \"checksum\" for the current buffer."
5074   (let ((sum 0))
5075     (save-excursion
5076       (goto-char (point-min))
5077       (re-search-forward
5078        (concat "^" (regexp-quote mail-header-separator) "$"))
5079       (while (not (eobp))
5080         (when (not (looking-at "[ \t\n]"))
5081           (setq sum (logxor (ash sum 1) (if (natnump sum) 0 1)
5082                             (char-after))))
5083         (forward-char 1)))
5084     sum))
5085
5086 (defun message-do-fcc ()
5087   "Process Fcc headers in the current buffer."
5088   (let ((case-fold-search t)
5089         (buf (current-buffer))
5090         list file
5091         (mml-externalize-attachments message-fcc-externalize-attachments))
5092     (save-excursion
5093       (save-restriction
5094         (message-narrow-to-headers)
5095         (setq file (message-fetch-field "fcc" t)))
5096       (when file
5097         (set-buffer (get-buffer-create " *message temp*"))
5098         (erase-buffer)
5099         (insert-buffer-substring buf)
5100         (message-encode-message-body)
5101         (save-restriction
5102           (message-narrow-to-headers)
5103           (while (setq file (message-fetch-field "fcc" t))
5104             (push file list)
5105             (message-remove-header "fcc" nil t))
5106           (let ((mail-parse-charset message-default-charset)
5107                 (rfc2047-header-encoding-alist
5108                  (cons '("Newsgroups" . default)
5109                        rfc2047-header-encoding-alist)))
5110             (mail-encode-encoded-word-buffer)))
5111         (goto-char (point-min))
5112         (when (re-search-forward
5113                (concat "^" (regexp-quote mail-header-separator) "$")
5114                nil t)
5115           (replace-match "" t t ))
5116         ;; Process FCC operations.
5117         (while list
5118           (setq file (pop list))
5119           (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
5120               ;; Pipe the article to the program in question.
5121               (call-process-region (point-min) (point-max) shell-file-name
5122                                    nil nil nil shell-command-switch
5123                                    (match-string 1 file))
5124             ;; Save the article.
5125             (setq file (expand-file-name file))
5126             (unless (file-exists-p (file-name-directory file))
5127               (make-directory (file-name-directory file) t))
5128             (if (and message-fcc-handler-function
5129                      (not (eq message-fcc-handler-function 'rmail-output)))
5130                 (funcall message-fcc-handler-function file)
5131               (if (and (file-readable-p file) (mail-file-babyl-p file))
5132                   (rmail-output file 1 nil t)
5133                 (let ((mail-use-rfc822 t))
5134                   (rmail-output file 1 t t))))))
5135         (kill-buffer (current-buffer))))))
5136
5137 (defun message-output (filename)
5138   "Append this article to Unix/babyl mail file FILENAME."
5139   (if (and (file-readable-p filename)
5140            (mail-file-babyl-p filename))
5141       (gnus-output-to-rmail filename t)
5142     (gnus-output-to-mail filename t)))
5143
5144 (defun message-cleanup-headers ()
5145   "Do various automatic cleanups of the headers."
5146   ;; Remove empty lines in the header.
5147   (save-restriction
5148     (message-narrow-to-headers)
5149     ;; Remove blank lines.
5150     (while (re-search-forward "^[ \t]*\n" nil t)
5151       (replace-match "" t t))
5152
5153     ;; Correct Newsgroups and Followup-To headers:  Change sequence of
5154     ;; spaces to comma and eliminate spaces around commas.  Eliminate
5155     ;; embedded line breaks.
5156     (goto-char (point-min))
5157     (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
5158       (save-restriction
5159         (narrow-to-region
5160          (point)
5161          (if (re-search-forward "^[^ \t]" nil t)
5162              (match-beginning 0)
5163            (forward-line 1)
5164            (point)))
5165         (goto-char (point-min))
5166         (while (re-search-forward "\n[ \t]+" nil t)
5167           (replace-match " " t t))     ;No line breaks (too confusing)
5168         (goto-char (point-min))
5169         (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
5170           (replace-match "," t t))
5171         (goto-char (point-min))
5172         ;; Remove trailing commas.
5173         (when (re-search-forward ",+$" nil t)
5174           (replace-match "" t t))))))
5175
5176 (defun message-make-date (&optional now)
5177   "Make a valid data header.
5178 If NOW, use that time instead."
5179   (let ((system-time-locale "C"))
5180     (format-time-string "%a, %d %b %Y %T %z" now)))
5181
5182 (defun message-insert-expires (days)
5183   "Insert the Expires header.  Expiry in DAYS days."
5184   (interactive "NExpire article in how many days? ")
5185   (save-excursion
5186     (message-position-on-field "Expires" "X-Draft-From")
5187     (insert (message-make-expires-date days))))
5188
5189 (defun message-make-expires-date (days)
5190   "Make date string for the Expires header.  Expiry in DAYS days.
5191
5192 In posting styles use `(\"Expires\" (make-expires-date 30))'."
5193   (let* ((cur (decode-time (current-time)))
5194          (nday (+ days (nth 3 cur))))
5195     (setf (nth 3 cur) nday)
5196     (message-make-date (apply 'encode-time cur))))
5197
5198 (defun message-make-message-id ()
5199   "Make a unique Message-ID."
5200   (concat "<" (message-unique-id)
5201           (let ((psubject (save-excursion (message-fetch-field "subject")))
5202                 (psupersedes
5203                  (save-excursion (message-fetch-field "supersedes"))))
5204             (if (or
5205                  (and message-reply-headers
5206                       (mail-header-references message-reply-headers)
5207                       (mail-header-subject message-reply-headers)
5208                       psubject
5209                       (not (string=
5210                             (message-strip-subject-re
5211                              (mail-header-subject message-reply-headers))
5212                             (message-strip-subject-re psubject))))
5213                  (and psupersedes
5214                       (string-match "_-_@" psupersedes)))
5215                 "_-_" ""))
5216           "@" (message-make-fqdn) ">"))
5217
5218 (defvar message-unique-id-char nil)
5219
5220 ;; If you ever change this function, make sure the new version
5221 ;; cannot generate IDs that the old version could.
5222 ;; You might for example insert a "." somewhere (not next to another dot
5223 ;; or string boundary), or modify the "fsf" string.
5224 (defun message-unique-id ()
5225   ;; Don't use microseconds from (current-time), they may be unsupported.
5226   ;; Instead we use this randomly inited counter.
5227   (setq message-unique-id-char
5228         (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
5229            ;; (current-time) returns 16-bit ints,
5230            ;; and 2^16*25 just fits into 4 digits i base 36.
5231            (* 25 25)))
5232   (let ((tm (current-time)))
5233     (concat
5234      (if (or (memq system-type '(ms-dos emx vax-vms))
5235              ;; message-number-base36 doesn't handle bigints.
5236              (floatp (user-uid)))
5237          (let ((user (downcase (user-login-name))))
5238            (while (string-match "[^a-z0-9_]" user)
5239              (aset user (match-beginning 0) ?_))
5240            user)
5241        (message-number-base36 (user-uid) -1))
5242      (message-number-base36 (+ (car tm)
5243                                (lsh (% message-unique-id-char 25) 16)) 4)
5244      (message-number-base36 (+ (nth 1 tm)
5245                                (lsh (/ message-unique-id-char 25) 16)) 4)
5246      ;; Append a given name, because while the generated ID is unique
5247      ;; to this newsreader, other newsreaders might otherwise generate
5248      ;; the same ID via another algorithm.
5249      ".fsf")))
5250
5251 (defun message-number-base36 (num len)
5252   (if (if (< len 0)
5253           (<= num 0)
5254         (= len 0))
5255       ""
5256     (concat (message-number-base36 (/ num 36) (1- len))
5257             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
5258                                   (% num 36))))))
5259
5260 (defun message-make-organization ()
5261   "Make an Organization header."
5262   (let* ((organization
5263           (when message-user-organization
5264             (if (functionp message-user-organization)
5265                 (funcall message-user-organization)
5266               message-user-organization))))
5267     (with-temp-buffer
5268       (mm-enable-multibyte)
5269       (cond ((stringp organization)
5270              (insert organization))
5271             ((and (eq t organization)
5272                   message-user-organization-file
5273                   (file-exists-p message-user-organization-file))
5274              (insert-file-contents message-user-organization-file)))
5275       (goto-char (point-min))
5276       (while (re-search-forward "[\t\n]+" nil t)
5277         (replace-match "" t t))
5278       (unless (zerop (buffer-size))
5279         (buffer-string)))))
5280
5281 (defun message-make-lines ()
5282   "Count the number of lines and return numeric string."
5283   (save-excursion
5284     (save-restriction
5285       (widen)
5286       (message-goto-body)
5287       (int-to-string (count-lines (point) (point-max))))))
5288
5289 (defun message-make-references ()
5290   "Return the References header for this message."
5291   (when message-reply-headers
5292     (let ((message-id (mail-header-message-id message-reply-headers))
5293           (references (mail-header-references message-reply-headers)))
5294       (if (or references message-id)
5295           (concat (or references "") (and references " ")
5296                   (or message-id ""))
5297         nil))))
5298
5299 (defun message-make-in-reply-to ()
5300   "Return the In-Reply-To header for this message."
5301   (when message-reply-headers
5302     (let ((from (mail-header-from message-reply-headers))
5303           (date (mail-header-date message-reply-headers))
5304           (msg-id (mail-header-message-id message-reply-headers)))
5305       (when from
5306         (let ((name (mail-extract-address-components from)))
5307           (concat
5308            msg-id (if msg-id " (")
5309            (if (car name)
5310                (if (string-match "[^\000-\177]" (car name))
5311                    ;; Quote a string containing non-ASCII characters.
5312                    ;; It will make the RFC2047 encoder cause an error
5313                    ;; if there are special characters.
5314                    (let ((default-enable-multibyte-characters t))
5315                      (with-temp-buffer
5316                        (insert (car name))
5317                        (goto-char (point-min))
5318                        (while (search-forward "\"" nil t)
5319                          (when (prog2
5320                                    (backward-char)
5321                                    (zerop (% (skip-chars-backward "\\\\") 2))
5322                                  (goto-char (match-beginning 0)))
5323                            (insert "\\"))
5324                          (forward-char))
5325                        ;; Those quotes will be removed by the RFC2047 encoder.
5326                        (concat "\"" (buffer-string) "\"")))
5327                  (car name))
5328              (nth 1 name))
5329            "'s message of \""
5330            (if (or (not date) (string= date ""))
5331                "(unknown date)" date)
5332            "\"" (if msg-id ")")))))))
5333
5334 (defun message-make-distribution ()
5335   "Make a Distribution header."
5336   (let ((orig-distribution (message-fetch-reply-field "distribution")))
5337     (cond ((functionp message-distribution-function)
5338            (funcall message-distribution-function))
5339           (t orig-distribution))))
5340
5341 (defun message-make-expires ()
5342   "Return an Expires header based on `message-expires'."
5343   (let ((current (current-time))
5344         (future (* 1.0 message-expires 60 60 24)))
5345     ;; Add the future to current.
5346     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
5347     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
5348     (message-make-date current)))
5349
5350 (defun message-make-path ()
5351   "Return uucp path."
5352   (let ((login-name (user-login-name)))
5353     (cond ((null message-user-path)
5354            (concat (system-name) "!" login-name))
5355           ((stringp message-user-path)
5356            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
5357            (concat message-user-path "!" login-name))
5358           (t login-name))))
5359
5360 (defun message-make-from (&optional name address)
5361   "Make a From header."
5362   (let* ((style message-from-style)
5363          (login (or address (message-make-address)))
5364          (fullname (or name
5365                        (and (boundp 'user-full-name)
5366                             user-full-name)
5367                        (user-full-name))))
5368     (when (string= fullname "&")
5369       (setq fullname (user-login-name)))
5370     (with-temp-buffer
5371       (mm-enable-multibyte)
5372       (cond
5373        ((or (null style)
5374             (equal fullname ""))
5375         (insert login))
5376        ((or (eq style 'angles)
5377             (and (not (eq style 'parens))
5378                  ;; Use angles if no quoting is needed, or if parens would
5379                  ;; need quoting too.
5380                  (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
5381                      (let ((tmp (concat fullname nil)))
5382                        (while (string-match "([^()]*)" tmp)
5383                          (aset tmp (match-beginning 0) ?-)
5384                          (aset tmp (1- (match-end 0)) ?-))
5385                        (string-match "[\\()]" tmp)))))
5386         (insert fullname)
5387         (goto-char (point-min))
5388         ;; Look for a character that cannot appear unquoted
5389         ;; according to RFC 822.
5390         (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
5391           ;; Quote fullname, escaping specials.
5392           (goto-char (point-min))
5393           (insert "\"")
5394           (while (re-search-forward "[\"\\]" nil 1)
5395             (replace-match "\\\\\\&" t))
5396           (insert "\""))
5397         (insert " <" login ">"))
5398        (t                               ; 'parens or default
5399         (insert login " (")
5400         (let ((fullname-start (point)))
5401           (insert fullname)
5402           (goto-char fullname-start)
5403           ;; RFC 822 says \ and nonmatching parentheses
5404           ;; must be escaped in comments.
5405           ;; Escape every instance of ()\ ...
5406           (while (re-search-forward "[()\\]" nil 1)
5407             (replace-match "\\\\\\&" t))
5408           ;; ... then undo escaping of matching parentheses,
5409           ;; including matching nested parentheses.
5410           (goto-char fullname-start)
5411           (while (re-search-forward
5412                   "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
5413                   nil 1)
5414             (replace-match "\\1(\\3)" t)
5415             (goto-char fullname-start)))
5416         (insert ")")))
5417       (buffer-string))))
5418
5419 (defun message-make-sender ()
5420   "Return the \"real\" user address.
5421 This function tries to ignore all user modifications, and
5422 give as trustworthy answer as possible."
5423   (concat (user-login-name) "@" (system-name)))
5424
5425 (defun message-make-address ()
5426   "Make the address of the user."
5427   (or (message-user-mail-address)
5428       (concat (user-login-name) "@" (message-make-domain))))
5429
5430 (defun message-user-mail-address ()
5431   "Return the pertinent part of `user-mail-address'."
5432   (when (and user-mail-address
5433              (string-match "@.*\\." user-mail-address))
5434     (if (string-match " " user-mail-address)
5435         (nth 1 (mail-extract-address-components user-mail-address))
5436       user-mail-address)))
5437
5438 (defun message-sendmail-envelope-from ()
5439   "Return the envelope from."
5440   (cond ((eq message-sendmail-envelope-from 'header)
5441          (nth 1 (mail-extract-address-components
5442                  (message-fetch-field "from"))))
5443         ((stringp message-sendmail-envelope-from)
5444          message-sendmail-envelope-from)
5445         (t
5446          (message-make-address))))
5447
5448 (defun message-make-fqdn ()
5449   "Return user's fully qualified domain name."
5450   (let* ((system-name (system-name))
5451          (user-mail (message-user-mail-address))
5452          (user-domain
5453           (if (and user-mail
5454                    (string-match "@\\(.*\\)\\'" user-mail))
5455               (match-string 1 user-mail)))
5456          (case-fold-search t))
5457     (cond
5458      ((and message-user-fqdn
5459            (stringp message-user-fqdn)
5460            (string-match message-valid-fqdn-regexp message-user-fqdn)
5461            (not (string-match message-bogus-system-names message-user-fqdn)))
5462       ;; `message-user-fqdn' seems to be valid
5463       message-user-fqdn)
5464      ((and (string-match message-valid-fqdn-regexp system-name)
5465            (not (string-match message-bogus-system-names system-name)))
5466       ;; `system-name' returned the right result.
5467       system-name)
5468      ;; Try `mail-host-address'.
5469      ((and (boundp 'mail-host-address)
5470            (stringp mail-host-address)
5471            (string-match message-valid-fqdn-regexp mail-host-address)
5472            (not (string-match message-bogus-system-names mail-host-address)))
5473       mail-host-address)
5474      ;; We try `user-mail-address' as a backup.
5475      ((and user-domain
5476            (stringp user-domain)
5477            (string-match message-valid-fqdn-regexp user-domain)
5478            (not (string-match message-bogus-system-names user-domain)))
5479       user-domain)
5480      ;; Default to this bogus thing.
5481      (t
5482       (concat system-name
5483               ".i-did-not-set--mail-host-address--so-tickle-me")))))
5484
5485 (defun message-make-host-name ()
5486   "Return the name of the host."
5487   (let ((fqdn (message-make-fqdn)))
5488     (string-match "^[^.]+\\." fqdn)
5489     (substring fqdn 0 (1- (match-end 0)))))
5490
5491 (defun message-make-domain ()
5492   "Return the domain name."
5493   (or mail-host-address
5494       (message-make-fqdn)))
5495
5496 (defun message-to-list-only ()
5497   "Send a message to the list only.
5498 Remove all addresses but the list address from To and Cc headers."
5499   (interactive)
5500   (let ((listaddr (message-make-mail-followup-to t)))
5501     (when listaddr
5502       (save-excursion
5503         (message-remove-header "to")
5504         (message-remove-header "cc")
5505         (message-position-on-field "To" "X-Draft-From")
5506         (insert listaddr)))))
5507
5508 (defun message-make-mail-followup-to (&optional only-show-subscribed)
5509   "Return the Mail-Followup-To header.
5510 If passed the optional argument ONLY-SHOW-SUBSCRIBED only return the
5511 subscribed address (and not the additional To and Cc header contents)."
5512   (let* ((case-fold-search t)
5513          (to (message-fetch-field "To"))
5514          (cc (message-fetch-field "cc"))
5515          (msg-recipients (concat to (and to cc ", ") cc))
5516          (recipients
5517           (mapcar 'mail-strip-quoted-names
5518                   (message-tokenize-header msg-recipients)))
5519          (file-regexps
5520           (if message-subscribed-address-file
5521               (let (begin end item re)
5522                 (save-excursion
5523                   (with-temp-buffer
5524                     (insert-file-contents message-subscribed-address-file)
5525                     (while (not (eobp))
5526                       (setq begin (point))
5527                       (forward-line 1)
5528                       (setq end (point))
5529                       (if (bolp) (setq end (1- end)))
5530                       (setq item (regexp-quote (buffer-substring begin end)))
5531                       (if re (setq re (concat re "\\|" item))
5532                         (setq re (concat "\\`\\(" item))))
5533                     (and re (list (concat re "\\)\\'"))))))))
5534          (mft-regexps (apply 'append message-subscribed-regexps
5535                              (mapcar 'regexp-quote
5536                                      message-subscribed-addresses)
5537                              file-regexps
5538                              (mapcar 'funcall
5539                                      message-subscribed-address-functions))))
5540     (save-match-data
5541       (let ((list
5542              (loop for recipient in recipients
5543                when (loop for regexp in mft-regexps
5544                       when (string-match regexp recipient) return t)
5545                return recipient)))
5546         (when list
5547           (if only-show-subscribed
5548               list
5549             msg-recipients))))))
5550
5551 (defun message-idna-to-ascii-rhs-1 (header)
5552   "Interactively potentially IDNA encode domain names in HEADER."
5553   (let ((field (message-fetch-field header))
5554         rhs ace  address)
5555     (when field
5556       (dolist (rhs
5557                (mm-delete-duplicates
5558                 (mapcar (lambda (rhs) (or (cadr (split-string rhs "@")) ""))
5559                         (mapcar 'downcase
5560                                 (mapcar
5561                                  'car (mail-header-parse-addresses field))))))
5562         (setq ace (if (string-match "\\`[[:ascii:]]+\\'" rhs)
5563                       rhs
5564                     (downcase (idna-to-ascii rhs))))
5565         (when (and (not (equal rhs ace))
5566                    (or (not (eq message-use-idna 'ask))
5567                        (y-or-n-p (format "Replace %s with %s in %s:? "
5568                                          rhs ace header))))
5569           (goto-char (point-min))
5570           (while (re-search-forward (concat "^" header ":") nil t)
5571             (message-narrow-to-field)
5572             (while (search-forward (concat "@" rhs) nil t)
5573               (replace-match (concat "@" ace) t t))
5574             (goto-char (point-max))
5575             (widen)))))))
5576
5577 (defun message-idna-to-ascii-rhs ()
5578   "Possibly IDNA encode non-ASCII domain names in From:, To: and Cc: headers.
5579 See `message-idna-encode'."
5580   (interactive)
5581   (when message-use-idna
5582     (save-excursion
5583       (save-restriction
5584         (message-narrow-to-head)
5585         (message-idna-to-ascii-rhs-1 "From")
5586         (message-idna-to-ascii-rhs-1 "To")
5587         (message-idna-to-ascii-rhs-1 "Reply-To")
5588         (message-idna-to-ascii-rhs-1 "Mail-Reply-To")
5589         (message-idna-to-ascii-rhs-1 "Mail-Followup-To")
5590         (message-idna-to-ascii-rhs-1 "Cc")))))
5591
5592 (defun message-generate-headers (headers)
5593   "Prepare article HEADERS.
5594 Headers already prepared in the buffer are not modified."
5595   (setq headers (append headers message-required-headers))
5596   (save-restriction
5597     (message-narrow-to-headers)
5598     (let* ((Date (message-make-date))
5599            (Message-ID (message-make-message-id))
5600            (Organization (message-make-organization))
5601            (From (message-make-from))
5602            (Path (message-make-path))
5603            (Subject nil)
5604            (Newsgroups nil)
5605            (In-Reply-To (message-make-in-reply-to))
5606            (References (message-make-references))
5607            (To nil)
5608            (Distribution (message-make-distribution))
5609            (Lines (message-make-lines))
5610            (User-Agent message-newsreader)
5611            (Expires (message-make-expires))
5612            (case-fold-search t)
5613            (optionalp nil)
5614            header value elem header-string)
5615       ;; First we remove any old generated headers.
5616       (let ((headers message-deletable-headers))
5617         (unless (buffer-modified-p)
5618           (setq headers (delq 'Message-ID (copy-sequence headers))))
5619         (while headers
5620           (goto-char (point-min))
5621           (and (re-search-forward
5622                 (concat "^" (symbol-name (car headers)) ": *") nil t)
5623                (get-text-property (1+ (match-beginning 0)) 'message-deletable)
5624                (message-delete-line))
5625           (pop headers)))
5626       ;; Go through all the required headers and see if they are in the
5627       ;; articles already.  If they are not, or are empty, they are
5628       ;; inserted automatically - except for Subject, Newsgroups and
5629       ;; Distribution.
5630       (while headers
5631         (goto-char (point-min))
5632         (setq elem (pop headers))
5633         (if (consp elem)
5634             (if (eq (car elem) 'optional)
5635                 (setq header (cdr elem)
5636                       optionalp t)
5637               (setq header (car elem)))
5638           (setq header elem))
5639         (setq header-string  (if (stringp header)
5640                                  header
5641                                (symbol-name header)))
5642         (when (or (not (re-search-forward
5643                         (concat "^"
5644                                 (regexp-quote (downcase header-string))
5645                                 ":")
5646                         nil t))
5647                   (progn
5648                     ;; The header was found.  We insert a space after the
5649                     ;; colon, if there is none.
5650                     (if (/= (char-after) ? ) (insert " ") (forward-char 1))
5651                     ;; Find out whether the header is empty.
5652                     (looking-at "[ \t]*\n[^ \t]")))
5653           ;; So we find out what value we should insert.
5654           (setq value
5655                 (cond
5656                  ((and (consp elem)
5657                        (eq (car elem) 'optional)
5658                        (not (member header-string message-inserted-headers)))
5659                   ;; This is an optional header.  If the cdr of this
5660                   ;; is something that is nil, then we do not insert
5661                   ;; this header.
5662                   (setq header (cdr elem))
5663                   (or (and (functionp (cdr elem))
5664                            (funcall (cdr elem)))
5665                       (and (boundp (cdr elem))
5666                            (symbol-value (cdr elem)))))
5667                  ((consp elem)
5668                   ;; The element is a cons.  Either the cdr is a
5669                   ;; string to be inserted verbatim, or it is a
5670                   ;; function, and we insert the value returned from
5671                   ;; this function.
5672                   (or (and (stringp (cdr elem))
5673                            (cdr elem))
5674                       (and (functionp (cdr elem))
5675                            (funcall (cdr elem)))))
5676                  ((and (boundp header)
5677                        (symbol-value header))
5678                   ;; The element is a symbol.  We insert the value
5679                   ;; of this symbol, if any.
5680                   (symbol-value header))
5681                  ((not (message-check-element
5682                         (intern (downcase (symbol-name header)))))
5683                   ;; We couldn't generate a value for this header,
5684                   ;; so we just ask the user.
5685                   (read-from-minibuffer
5686                    (format "Empty header for %s; enter value: " header)))))
5687           ;; Finally insert the header.
5688           (when (and value
5689                      (not (equal value "")))
5690             (save-excursion
5691               (if (bolp)
5692                   (progn
5693                     ;; This header didn't exist, so we insert it.
5694                     (goto-char (point-max))
5695                     (let ((formatter
5696                            (cdr (assq header message-header-format-alist))))
5697                       (if formatter
5698                           (funcall formatter header value)
5699                         (insert header-string ": " value))
5700                       (goto-char (message-fill-field))
5701                       ;; We check whether the value was ended by a
5702                       ;; newline.  If not, we insert one.
5703                       (unless (bolp)
5704                         (insert "\n"))
5705                       (forward-line -1)))
5706                 ;; The value of this header was empty, so we clear
5707                 ;; totally and insert the new value.
5708                 (delete-region (point) (point-at-eol))
5709                 ;; If the header is optional, and the header was
5710                 ;; empty, we can't insert it anyway.
5711                 (unless optionalp
5712                   (push header-string message-inserted-headers)
5713                   (insert value)
5714                   (message-fill-field)))
5715               ;; Add the deletable property to the headers that require it.
5716               (and (memq header message-deletable-headers)
5717                    (progn (beginning-of-line) (looking-at "[^:]+: "))
5718                    (add-text-properties
5719                     (point) (match-end 0)
5720                     '(message-deletable t face italic) (current-buffer)))))))
5721       ;; Insert new Sender if the From is strange.
5722       (let ((from (message-fetch-field "from"))
5723             (sender (message-fetch-field "sender"))
5724             (secure-sender (message-make-sender)))
5725         (when (and from
5726                    (not (message-check-element 'sender))
5727                    (not (string=
5728                          (downcase
5729                           (cadr (mail-extract-address-components from)))
5730                          (downcase secure-sender)))
5731                    (or (null sender)
5732                        (not
5733                         (string=
5734                          (downcase
5735                           (cadr (mail-extract-address-components sender)))
5736                          (downcase secure-sender)))))
5737           (goto-char (point-min))
5738           ;; Rename any old Sender headers to Original-Sender.
5739           (when (re-search-forward "^\\(Original-\\)*Sender:" nil t)
5740             (beginning-of-line)
5741             (insert "Original-")
5742             (beginning-of-line))
5743           (when (or (message-news-p)
5744                     (string-match "@.+\\.." secure-sender))
5745             (insert "Sender: " secure-sender "\n"))))
5746       ;; Check for IDNA
5747       (message-idna-to-ascii-rhs))))
5748
5749 (defun message-insert-courtesy-copy ()
5750   "Insert a courtesy message in mail copies of combined messages."
5751   (let (newsgroups)
5752     (save-excursion
5753       (save-restriction
5754         (message-narrow-to-headers)
5755         (when (setq newsgroups (message-fetch-field "newsgroups"))
5756           (goto-char (point-max))
5757           (insert "Posted-To: " newsgroups "\n")))
5758       (forward-line 1)
5759       (when message-courtesy-message
5760         (cond
5761          ((string-match "%s" message-courtesy-message)
5762           (insert (format message-courtesy-message newsgroups)))
5763          (t
5764           (insert message-courtesy-message)))))))
5765
5766 ;;;
5767 ;;; Setting up a message buffer
5768 ;;;
5769
5770 (defun message-skip-to-next-address ()
5771   (let ((end (save-excursion
5772                (message-next-header)
5773                (point)))
5774         quoted char)
5775     (when (looking-at ",")
5776       (forward-char 1))
5777     (while (and (not (= (point) end))
5778                 (or (not (eq char ?,))
5779                     quoted))
5780       (skip-chars-forward "^,\"" (point-max))
5781       (when (eq (setq char (following-char)) ?\")
5782         (setq quoted (not quoted)))
5783       (unless (= (point) end)
5784         (forward-char 1)))
5785     (skip-chars-forward " \t\n")))
5786
5787 (defun message-fill-address (header value)
5788   (insert (capitalize (symbol-name header))
5789           ": "
5790           (if (consp value) (car value) value)
5791           "\n")
5792   (message-fill-field-address))
5793
5794 (defun message-split-line ()
5795   "Split current line, moving portion beyond point vertically down.
5796 If the current line has `message-yank-prefix', insert it on the new line."
5797   (interactive "*")
5798   (condition-case nil
5799       (split-line message-yank-prefix) ;; Emacs 22.1+ supports arg.
5800     (error
5801      (split-line))))
5802
5803 (defun message-insert-header (header value)
5804   (insert (capitalize (symbol-name header))
5805           ": "
5806           (if (consp value) (car value) value)))
5807
5808 (defun message-field-name ()
5809   (save-excursion
5810     (goto-char (point-min))
5811     (when (looking-at "\\([^:]+\\):")
5812       (intern (capitalize (match-string 1))))))
5813
5814 (defun message-fill-field ()
5815   (save-excursion
5816     (save-restriction
5817       (message-narrow-to-field)
5818       (let ((field-name (message-field-name)))
5819         (funcall (or (cadr (assq field-name message-field-fillers))
5820                      'message-fill-field-general)))
5821       (point-max))))
5822
5823 (defun message-fill-field-address ()
5824   (while (not (eobp))
5825     (message-skip-to-next-address)
5826     (let (last)
5827       (if (and (> (current-column) 78)
5828                last)
5829           (progn
5830             (save-excursion
5831               (goto-char last)
5832               (insert "\n\t"))
5833             (setq last (1+ (point))))
5834         (setq last (1+ (point)))))))
5835
5836 (defun message-fill-field-general ()
5837   (let ((begin (point))
5838         (fill-column 78)
5839         (fill-prefix "\t"))
5840     (while (and (search-forward "\n" nil t)
5841                 (not (eobp)))
5842       (replace-match " " t t))
5843     (fill-region-as-paragraph begin (point-max))
5844     ;; Tapdance around looong Message-IDs.
5845     (forward-line -1)
5846     (when (looking-at "[ \t]*$")
5847       (message-delete-line))
5848     (goto-char begin)
5849     (search-forward ":" nil t)
5850     (when (looking-at "\n[ \t]+")
5851       (replace-match " " t t))
5852     (goto-char (point-max))))
5853
5854 (defun message-shorten-1 (list cut surplus)
5855   "Cut SURPLUS elements out of LIST, beginning with CUTth one."
5856   (setcdr (nthcdr (- cut 2) list)
5857           (nthcdr (+ (- cut 2) surplus 1) list)))
5858
5859 (defun message-shorten-references (header references)
5860   "Trim REFERENCES to be 21 Message-ID long or less, and fold them.
5861 When sending via news, also check that the REFERENCES are less
5862 than 988 characters long, and if they are not, trim them until
5863 they are."
5864   (let ((maxcount 21)
5865         (count 0)
5866         (cut 2)
5867         refs)
5868     (with-temp-buffer
5869       (insert references)
5870       (goto-char (point-min))
5871       ;; Cons a list of valid references.
5872       (while (re-search-forward "<[^>]+>" nil t)
5873         (push (match-string 0) refs))
5874       (setq refs (nreverse refs)
5875             count (length refs)))
5876
5877     ;; If the list has more than MAXCOUNT elements, trim it by
5878     ;; removing the CUTth element and the required number of
5879     ;; elements that follow.
5880     (when (> count maxcount)
5881       (let ((surplus (- count maxcount)))
5882         (message-shorten-1 refs cut surplus)
5883         (decf count surplus)))
5884
5885     ;; When sending via news, make sure the total folded length will
5886     ;; be less than 998 characters.  This is to cater to broken INN
5887     ;; 2.3 which counts the total number of characters in a header
5888     ;; rather than the physical line length of each line, as it should.
5889     ;;
5890     ;; This hack should be removed when it's believed than INN 2.3 is
5891     ;; no longer widely used.
5892     ;;
5893     ;; At this point the headers have not been generated, thus we use
5894     ;; message-this-is-news directly.
5895     (when message-this-is-news
5896       (while (< 998
5897                 (with-temp-buffer
5898                   (message-insert-header
5899                    header (mapconcat #'identity refs " "))
5900                   (buffer-size)))
5901         (message-shorten-1 refs cut 1)))
5902     ;; Finally, collect the references back into a string and insert
5903     ;; it into the buffer.
5904     (message-insert-header header (mapconcat #'identity refs " "))))
5905
5906 (defun message-position-point ()
5907   "Move point to where the user probably wants to find it."
5908   (message-narrow-to-headers)
5909   (cond
5910    ((re-search-forward "^[^:]+:[ \t]*$" nil t)
5911     (search-backward ":" )
5912     (widen)
5913     (forward-char 1)
5914     (if (eq (char-after) ? )
5915         (forward-char 1)
5916       (insert " ")))
5917    (t
5918     (goto-char (point-max))
5919     (widen)
5920     (forward-line 1)
5921     (unless (looking-at "$")
5922       (forward-line 2)))
5923    (sit-for 0)))
5924
5925 (defcustom message-beginning-of-line t
5926   "Whether \\<message-mode-map>\\[message-beginning-of-line]\
5927  goes to beginning of header values."
5928   :version "22.1"
5929   :group 'message-buffers
5930   :link '(custom-manual "(message)Movement")
5931   :type 'boolean)
5932
5933 (defun message-beginning-of-line (&optional n)
5934   "Move point to beginning of header value or to beginning of line.
5935 The prefix argument N is passed directly to `beginning-of-line'.
5936
5937 This command is identical to `beginning-of-line' if point is
5938 outside the message header or if the option `message-beginning-of-line'
5939 is nil.
5940
5941 If point is in the message header and on a (non-continued) header
5942 line, move point to the beginning of the header value or the beginning of line,
5943 whichever is closer.  If point is already at beginning of line, move point to
5944 beginning of header value.  Therefore, repeated calls will toggle point
5945 between beginning of field and beginning of line."
5946   (interactive "p")
5947   (let ((zrs 'zmacs-region-stays))
5948     (when (and (interactive-p) (boundp zrs))
5949       (set zrs t)))
5950   (if (and message-beginning-of-line
5951            (message-point-in-header-p))
5952       (let* ((here (point))
5953              (bol (progn (beginning-of-line n) (point)))
5954              (eol (point-at-eol))
5955              (eoh (re-search-forward ": *" eol t)))
5956         (goto-char
5957          (if (and eoh (or (< eoh here) (= bol here)))
5958              eoh bol)))
5959     (beginning-of-line n)))
5960
5961 (defun message-buffer-name (type &optional to group)
5962   "Return a new (unique) buffer name based on TYPE and TO."
5963   (cond
5964    ;; Generate a new buffer name The Message Way.
5965    ((memq message-generate-new-buffers '(unique t))
5966     (generate-new-buffer-name
5967      (concat "*" type
5968              (if to
5969                  (concat " to "
5970                          (or (car (mail-extract-address-components to))
5971                              to) "")
5972                "")
5973              (if (and group (not (string= group ""))) (concat " on " group) "")
5974              "*")))
5975    ;; Check whether `message-generate-new-buffers' is a function,
5976    ;; and if so, call it.
5977    ((functionp message-generate-new-buffers)
5978     (funcall message-generate-new-buffers type to group))
5979    ((eq message-generate-new-buffers 'unsent)
5980     (generate-new-buffer-name
5981      (concat "*unsent " type
5982              (if to
5983                  (concat " to "
5984                          (or (car (mail-extract-address-components to))
5985                              to) "")
5986                "")
5987              (if (and group (not (string= group ""))) (concat " on " group) "")
5988              "*")))
5989    ;; Search for the existing message buffer with the specified name.
5990    (t
5991     (let* ((new (if (eq message-generate-new-buffers 'standard)
5992                     (generate-new-buffer-name (concat "*" type " message*"))
5993                   (let ((message-generate-new-buffers 'unique))
5994                     (message-buffer-name type to group))))
5995            (regexp (concat "\\`"
5996                            (regexp-quote
5997                             (if (string-match "<[0-9]+>\\'" new)
5998                                 (substring new 0 (match-beginning 0))
5999                               new))
6000                            "\\(?:<\\([0-9]+\\)>\\)?\\'"))
6001            (case-fold-search nil))
6002       (or (cdar
6003            (last
6004             (sort
6005              (delq nil
6006                    (mapcar
6007                     (lambda (b)
6008                       (when (and (string-match regexp (setq b (buffer-name b)))
6009                                  (eq (with-current-buffer b major-mode)
6010                                      'message-mode))
6011                         (cons (string-to-number (or (match-string 1 b) "1"))
6012                               b)))
6013                     (buffer-list)))
6014              'car-less-than-car)))
6015           new)))))
6016
6017 (defun message-pop-to-buffer (name &optional switch-function)
6018   "Pop to buffer NAME, and warn if it already exists and is modified."
6019   (let ((buffer (get-buffer name)))
6020     (if (and buffer
6021              (buffer-name buffer))
6022         (let ((window (get-buffer-window buffer 0)))
6023           (if window
6024               ;; Raise the frame already displaying the message buffer.
6025               (progn
6026                 (gnus-select-frame-set-input-focus (window-frame window))
6027                 (select-window window))
6028             (funcall (or switch-function 'pop-to-buffer) buffer)
6029             (set-buffer buffer))
6030           (when (and (buffer-modified-p)
6031                      (not (prog1
6032                               (y-or-n-p
6033                                "Message already being composed; erase? ")
6034                             (message nil))))
6035             (error "Message being composed")))
6036       (funcall (or switch-function 'pop-to-buffer) name)
6037       (set-buffer name))
6038     (erase-buffer)
6039     (message-mode)))
6040
6041 (defun message-do-send-housekeeping ()
6042   "Kill old message buffers."
6043   ;; We might have sent this buffer already.  Delete it from the
6044   ;; list of buffers.
6045   (setq message-buffer-list (delq (current-buffer) message-buffer-list))
6046   (while (and message-max-buffers
6047               message-buffer-list
6048               (>= (length message-buffer-list) message-max-buffers))
6049     ;; Kill the oldest buffer -- unless it has been changed.
6050     (let ((buffer (pop message-buffer-list)))
6051       (when (and (buffer-name buffer)
6052                  (not (buffer-modified-p buffer)))
6053         (kill-buffer buffer))))
6054   ;; Rename the buffer.
6055   (if message-send-rename-function
6056       (funcall message-send-rename-function)
6057     ;; Note: mail-abbrevs of XEmacs renames buffer name behind Gnus.
6058     (when (string-match
6059            "\\`\\*\\(sent \\|unsent \\)?\\(.+\\)\\*[^\\*]*\\|\\`mail to "
6060            (buffer-name))
6061       (let ((name (match-string 2 (buffer-name)))
6062             to group)
6063         (if (not (or (null name)
6064                      (string-equal name "mail")
6065                      (string-equal name "posting")))
6066             (setq name (concat "*sent " name "*"))
6067           (message-narrow-to-headers)
6068           (setq to (message-fetch-field "to"))
6069           (setq group (message-fetch-field "newsgroups"))
6070           (widen)
6071           (setq name
6072                 (cond
6073                  (to (concat "*sent mail to "
6074                              (or (car (mail-extract-address-components to))
6075                                  to) "*"))
6076                  ((and group (not (string= group "")))
6077                   (concat "*sent posting on " group "*"))
6078                  (t "*sent mail*"))))
6079         (unless (string-equal name (buffer-name))
6080           (rename-buffer name t)))))
6081   ;; Push the current buffer onto the list.
6082   (when message-max-buffers
6083     (setq message-buffer-list
6084           (nconc message-buffer-list (list (current-buffer))))))
6085
6086 (defun message-mail-user-agent ()
6087   (let ((mua (cond
6088               ((not message-mail-user-agent) nil)
6089               ((eq message-mail-user-agent t) mail-user-agent)
6090               (t message-mail-user-agent))))
6091     (if (memq mua '(message-user-agent gnus-user-agent))
6092         nil
6093       mua)))
6094
6095 (defun message-setup (headers &optional replybuffer actions
6096                               continue switch-function)
6097   (let ((mua (message-mail-user-agent))
6098         subject to field yank-action)
6099     (if (not (and message-this-is-mail mua))
6100         (message-setup-1 headers replybuffer actions)
6101       (if replybuffer
6102           (setq yank-action (list 'insert-buffer replybuffer)))
6103       (setq headers (copy-sequence headers))
6104       (setq field (assq 'Subject headers))
6105       (when field
6106         (setq subject (cdr field))
6107         (setq headers (delq field headers)))
6108       (setq field (assq 'To headers))
6109       (when field
6110         (setq to (cdr field))
6111         (setq headers (delq field headers)))
6112       (let ((mail-user-agent mua))
6113         (compose-mail to subject
6114                       (mapcar (lambda (item)
6115                                 (cons
6116                                  (format "%s" (car item))
6117                                  (cdr item)))
6118                               headers)
6119                       continue switch-function yank-action actions)))))
6120
6121 (defun message-headers-to-generate (headers included-headers excluded-headers)
6122   "Return a list that includes all headers from HEADERS.
6123 If INCLUDED-HEADERS is a list, just include those headers.  If it is
6124 t, include all headers.  In any case, headers from EXCLUDED-HEADERS
6125 are not included."
6126   (let ((result nil)
6127         header-name)
6128     (dolist (header headers)
6129       (setq header-name (cond
6130                          ((and (consp header)
6131                                (eq (car header) 'optional))
6132                           ;; On the form (optional . Header)
6133                           (cdr header))
6134                          ((consp header)
6135                           ;; On the form (Header . function)
6136                           (car header))
6137                          (t
6138                           ;; Just a Header.
6139                           header)))
6140       (when (and (not (memq header-name excluded-headers))
6141                  (or (eq included-headers t)
6142                      (memq header-name included-headers)))
6143         (push header result)))
6144     (nreverse result)))
6145
6146 (defun message-setup-1 (headers &optional replybuffer actions)
6147   (dolist (action actions)
6148     (condition-case nil
6149         (add-to-list 'message-send-actions
6150                      `(apply ',(car action) ',(cdr action)))))
6151   (setq message-reply-buffer replybuffer)
6152   (goto-char (point-min))
6153   ;; Insert all the headers.
6154   (mail-header-format
6155    (let ((h headers)
6156          (alist message-header-format-alist))
6157      (while h
6158        (unless (assq (caar h) message-header-format-alist)
6159          (push (list (caar h)) alist))
6160        (pop h))
6161      alist)
6162    headers)
6163   (delete-region (point) (progn (forward-line -1) (point)))
6164   (when message-default-headers
6165     (insert message-default-headers)
6166     (or (bolp) (insert ?\n)))
6167   (insert mail-header-separator "\n")
6168   (forward-line -1)
6169   (when (message-news-p)
6170     (when message-default-news-headers
6171       (insert message-default-news-headers)
6172       (or (bolp) (insert ?\n)))
6173     (when message-generate-headers-first
6174       (message-generate-headers
6175        (message-headers-to-generate
6176         (append message-required-news-headers
6177                 message-required-headers)
6178         message-generate-headers-first
6179         '(Lines Subject)))))
6180   (when (message-mail-p)
6181     (when message-default-mail-headers
6182       (insert message-default-mail-headers)
6183       (or (bolp) (insert ?\n)))
6184     (when message-generate-headers-first
6185       (message-generate-headers
6186        (message-headers-to-generate
6187         (append message-required-mail-headers
6188                 message-required-headers)
6189         message-generate-headers-first
6190         '(Lines Subject)))))
6191   (run-hooks 'message-signature-setup-hook)
6192   (message-insert-signature)
6193   (save-restriction
6194     (message-narrow-to-headers)
6195     (run-hooks 'message-header-setup-hook))
6196   (set-buffer-modified-p nil)
6197   (setq buffer-undo-list nil)
6198   (when message-generate-hashcash
6199     ;; Generate hashcash headers for recipients already known
6200     (mail-add-payment-async))
6201   (run-hooks 'message-setup-hook)
6202   ;; Do this last to give it precedence over posting styles, etc.
6203   (when (message-mail-p)
6204     (save-restriction
6205       (message-narrow-to-headers)
6206       (if message-alternative-emails
6207           (message-use-alternative-email-as-from))))
6208   (message-position-point)
6209   (undo-boundary))
6210
6211 (defun message-set-auto-save-file-name ()
6212   "Associate the message buffer with a file in the drafts directory."
6213   (when message-auto-save-directory
6214     (unless (file-directory-p
6215              (directory-file-name message-auto-save-directory))
6216       (make-directory message-auto-save-directory t))
6217     (if (gnus-alive-p)
6218         (setq message-draft-article
6219               (nndraft-request-associate-buffer "drafts"))
6220       (setq buffer-file-name (expand-file-name
6221                               (if (memq system-type
6222                                         '(ms-dos ms-windows windows-nt
6223                                                  cygwin cygwin32 win32 w32
6224                                                  mswindows))
6225                                   "message"
6226                                 "*message*")
6227                               message-auto-save-directory))
6228       (setq buffer-auto-save-file-name (make-auto-save-file-name)))
6229     (clear-visited-file-modtime)
6230     (setq buffer-file-coding-system message-draft-coding-system)))
6231
6232 (defun message-disassociate-draft ()
6233   "Disassociate the message buffer from the drafts directory."
6234   (when message-draft-article
6235     (nndraft-request-expire-articles
6236      (list message-draft-article) "drafts" nil t)))
6237
6238 (defun message-insert-headers ()
6239   "Generate the headers for the article."
6240   (interactive)
6241   (save-excursion
6242     (save-restriction
6243       (message-narrow-to-headers)
6244       (when (message-news-p)
6245         (message-generate-headers
6246          (delq 'Lines
6247                (delq 'Subject
6248                      (copy-sequence message-required-news-headers)))))
6249       (when (message-mail-p)
6250         (message-generate-headers
6251          (delq 'Lines
6252                (delq 'Subject
6253                      (copy-sequence message-required-mail-headers))))))))
6254
6255 \f
6256
6257 ;;;
6258 ;;; Commands for interfacing with message
6259 ;;;
6260
6261 ;;;###autoload
6262 (defun message-mail (&optional to subject
6263                                other-headers continue switch-function
6264                                yank-action send-actions)
6265   "Start editing a mail message to be sent.
6266 OTHER-HEADERS is an alist of header/value pairs.  CONTINUE says whether
6267 to continue editing a message already being composed.  SWITCH-FUNCTION
6268 is a function used to switch to and display the mail buffer."
6269   (interactive)
6270   (let ((message-this-is-mail t) replybuffer)
6271     (unless (message-mail-user-agent)
6272       (message-pop-to-buffer
6273        ;; Search for the existing message buffer if `continue' is non-nil.
6274        (let ((message-generate-new-buffers
6275               (when (or (not continue)
6276                         (eq message-generate-new-buffers 'standard)
6277                         (functionp message-generate-new-buffers))
6278                 message-generate-new-buffers)))
6279          (message-buffer-name "mail" to))
6280        switch-function))
6281     ;; FIXME: message-mail should do something if YANK-ACTION is not
6282     ;; insert-buffer.
6283     (and (consp yank-action) (eq (car yank-action) 'insert-buffer)
6284          (setq replybuffer (nth 1 yank-action)))
6285     (message-setup
6286      (nconc
6287       `((To . ,(or to "")) (Subject . ,(or subject "")))
6288       (when other-headers other-headers))
6289      replybuffer send-actions continue switch-function)
6290     ;; FIXME: Should return nil if failure.
6291     t))
6292
6293 ;;;###autoload
6294 (defun message-news (&optional newsgroups subject)
6295   "Start editing a news article to be sent."
6296   (interactive)
6297   (let ((message-this-is-news t))
6298     (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups))
6299     (message-setup `((Newsgroups . ,(or newsgroups ""))
6300                      (Subject . ,(or subject ""))))))
6301
6302 (defun message-get-reply-headers (wide &optional to-address address-headers)
6303   (let (follow-to mct never-mct to cc author mft recipients extra)
6304   ;; Find all relevant headers we need.
6305     (save-restriction
6306       (message-narrow-to-headers-or-head)
6307       ;; Gmane renames "To".  Look at "Original-To", too, if it is present in
6308       ;; message-header-synonyms.
6309       (setq to (or (message-fetch-field "to")
6310                    (and (loop for synonym in message-header-synonyms
6311                               when (memq 'Original-To synonym)
6312                               return t)
6313                         (message-fetch-field "original-to")))
6314             cc (message-fetch-field "cc")
6315             extra (when message-extra-wide-headers
6316                     (mapconcat 'identity
6317                                (mapcar 'message-fetch-field
6318                                        message-extra-wide-headers)
6319                                ", "))
6320             mct (message-fetch-field "mail-copies-to")
6321             author (or (message-fetch-field "mail-reply-to")
6322                        (message-fetch-field "reply-to")
6323                        (message-fetch-field "from")
6324                        "")
6325             mft (and message-use-mail-followup-to
6326                      (message-fetch-field "mail-followup-to"))))
6327
6328     ;; Handle special values of Mail-Copies-To.
6329     (when mct
6330       (cond ((or (equal (downcase mct) "never")
6331                  (equal (downcase mct) "nobody"))
6332              (setq never-mct t)
6333              (setq mct nil))
6334             ((or (equal (downcase mct) "always")
6335                  (equal (downcase mct) "poster"))
6336              (setq mct author))))
6337
6338     (save-match-data
6339       ;; Build (textual) list of new recipient addresses.
6340       (cond
6341        ((not wide)
6342         (setq recipients (concat ", " author)))
6343        (address-headers
6344         (dolist (header address-headers)
6345           (let ((value (message-fetch-field header)))
6346             (when value
6347               (setq recipients (concat recipients ", " value))))))
6348        ((and mft
6349              (string-match "[^ \t,]" mft)
6350              (or (not (eq message-use-mail-followup-to 'ask))
6351                  (message-y-or-n-p "Obey Mail-Followup-To? " t "\
6352 You should normally obey the Mail-Followup-To: header.  In this
6353 article, it has the value of
6354
6355 " mft "
6356
6357 which directs your response to " (if (string-match "," mft)
6358                                      "the specified addresses"
6359                                    "that address only") ".
6360
6361 Most commonly, Mail-Followup-To is used by a mailing list poster to
6362 express that responses should be sent to just the list, and not the
6363 poster as well.
6364
6365 If a message is posted to several mailing lists, Mail-Followup-To may
6366 also be used to direct the following discussion to one list only,
6367 because discussions that are spread over several lists tend to be
6368 fragmented and very difficult to follow.
6369
6370 Also, some source/announcement lists are not intended for discussion;
6371 responses here are directed to other addresses.
6372
6373 You may customize the variable `message-use-mail-followup-to', if you
6374 want to get rid of this query permanently.")))
6375         (setq recipients (concat ", " mft)))
6376        (to-address
6377         (setq recipients (concat ", " to-address))
6378         ;; If the author explicitly asked for a copy, we don't deny it to them.
6379         (if mct (setq recipients (concat recipients ", " mct))))
6380        (t
6381         (setq recipients (if never-mct "" (concat ", " author)))
6382         (if to (setq recipients (concat recipients ", " to)))
6383         (if cc (setq recipients (concat recipients ", " cc)))
6384         (if extra (setq recipients (concat recipients ", " extra)))
6385         (if mct (setq recipients (concat recipients ", " mct)))))
6386       (if (>= (length recipients) 2)
6387           ;; Strip the leading ", ".
6388           (setq recipients (substring recipients 2)))
6389       ;; Squeeze whitespace.
6390       (while (string-match "[ \t][ \t]+" recipients)
6391         (setq recipients (replace-match " " t t recipients)))
6392       ;; Remove addresses that match `rmail-dont-reply-to-names'.
6393       (let ((rmail-dont-reply-to-names (message-dont-reply-to-names)))
6394         (setq recipients (rmail-dont-reply-to recipients)))
6395       ;; Perhaps "Mail-Copies-To: never" removed the only address?
6396       (if (string-equal recipients "")
6397           (setq recipients author))
6398       ;; Convert string to a list of (("foo@bar" . "Name <Foo@BAR>") ...).
6399       (setq recipients
6400             (mapcar
6401              (lambda (addr)
6402                (cons (downcase (mail-strip-quoted-names addr)) addr))
6403              (message-tokenize-header recipients)))
6404       ;; Remove first duplicates.  (Why not all duplicates?  Is this a bug?)
6405       (let ((s recipients))
6406         (while s
6407           (setq recipients (delq (assoc (car (pop s)) s) recipients))))
6408
6409       ;; Remove hierarchical lists that are contained within each other,
6410       ;; if message-hierarchical-addresses is defined.
6411       (when message-hierarchical-addresses
6412         (let ((plain-addrs (mapcar 'car recipients))
6413               subaddrs recip)
6414           (while plain-addrs
6415             (setq subaddrs (assoc (car plain-addrs)
6416                                   message-hierarchical-addresses)
6417                   plain-addrs (cdr plain-addrs))
6418             (when subaddrs
6419               (setq subaddrs (cdr subaddrs))
6420               (while subaddrs
6421                 (setq recip (assoc (car subaddrs) recipients)
6422                       subaddrs (cdr subaddrs))
6423                 (if recip
6424                     (setq recipients (delq recip recipients))))))))
6425
6426       ;; Build the header alist.  Allow the user to be asked whether
6427       ;; or not to reply to all recipients in a wide reply.
6428       (setq follow-to (list (cons 'To (cdr (pop recipients)))))
6429       (when (and recipients
6430                  (or (not message-wide-reply-confirm-recipients)
6431                      (y-or-n-p "Reply to all recipients? ")))
6432         (setq recipients (mapconcat
6433                           (lambda (addr) (cdr addr)) recipients ", "))
6434         (if (string-match "^ +" recipients)
6435             (setq recipients (substring recipients (match-end 0))))
6436         (push (cons 'Cc recipients) follow-to)))
6437     follow-to))
6438
6439 (defcustom message-simplify-subject-functions
6440   '(message-strip-list-identifiers
6441     message-strip-subject-re
6442     message-strip-subject-trailing-was
6443     message-strip-subject-encoded-words)
6444   "List of functions taking a string argument that simplify subjects.
6445 The functions are applied when replying to a message.
6446
6447 Useful functions to put in this list include:
6448 `message-strip-list-identifiers', `message-strip-subject-re',
6449 `message-strip-subject-trailing-was', and
6450 `message-strip-subject-encoded-words'."
6451   :version "22.1" ;; Gnus 5.10.9
6452   :group 'message-various
6453   :type '(repeat function))
6454
6455 (defun message-simplify-subject (subject &optional functions)
6456   "Return simplified SUBJECT."
6457   (unless functions
6458     ;; Simplify fully:
6459     (setq functions message-simplify-subject-functions))
6460   (when (and (memq 'message-strip-list-identifiers functions)
6461              gnus-list-identifiers)
6462     (setq subject (message-strip-list-identifiers subject)))
6463   (when (memq 'message-strip-subject-re functions)
6464     (setq subject (concat "Re: " (message-strip-subject-re subject))))
6465   (when (and (memq 'message-strip-subject-trailing-was functions)
6466              message-subject-trailing-was-query)
6467     (setq subject (message-strip-subject-trailing-was subject)))
6468   (when (memq 'message-strip-subject-encoded-words functions)
6469     (setq subject (message-strip-subject-encoded-words subject)))
6470   subject)
6471
6472 ;;;###autoload
6473 (defun message-reply (&optional to-address wide)
6474   "Start editing a reply to the article in the current buffer."
6475   (interactive)
6476   (require 'gnus-sum)                   ; for gnus-list-identifiers
6477   (let ((cur (current-buffer))
6478         from subject date reply-to to cc
6479         references message-id follow-to
6480         (inhibit-point-motion-hooks t)
6481         (message-this-is-mail t)
6482         gnus-warning)
6483     (save-restriction
6484       (message-narrow-to-head-1)
6485       ;; Allow customizations to have their say.
6486       (if (not wide)
6487           ;; This is a regular reply.
6488           (when (functionp message-reply-to-function)
6489             (save-excursion
6490               (setq follow-to (funcall message-reply-to-function))))
6491         ;; This is a followup.
6492         (when (functionp message-wide-reply-to-function)
6493           (save-excursion
6494             (setq follow-to
6495                   (funcall message-wide-reply-to-function)))))
6496       (setq message-id (message-fetch-field "message-id" t)
6497             references (message-fetch-field "references")
6498             date (message-fetch-field "date")
6499             from (or (message-fetch-field "from") "nobody")
6500             subject (or (message-fetch-field "subject") "none"))
6501
6502       ;; Strip list identifiers, "Re: ", and "was:"
6503       (setq subject (message-simplify-subject subject))
6504
6505       (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
6506                  (string-match "<[^>]+>" gnus-warning))
6507         (setq message-id (match-string 0 gnus-warning)))
6508
6509       (unless follow-to
6510         (setq follow-to (message-get-reply-headers wide to-address))))
6511
6512     (unless (message-mail-user-agent)
6513       (message-pop-to-buffer
6514        (message-buffer-name
6515         (if wide "wide reply" "reply") from
6516         (if wide to-address nil))))
6517
6518     (setq message-reply-headers
6519           (vector 0 subject from date message-id references 0 0 ""))
6520
6521     (message-setup
6522      `((Subject . ,subject)
6523        ,@follow-to)
6524      cur)))
6525
6526 ;;;###autoload
6527 (defun message-wide-reply (&optional to-address)
6528   "Make a \"wide\" reply to the message in the current buffer."
6529   (interactive)
6530   (message-reply to-address t))
6531
6532 ;;;###autoload
6533 (defun message-followup (&optional to-newsgroups)
6534   "Follow up to the message in the current buffer.
6535 If TO-NEWSGROUPS, use that as the new Newsgroups line."
6536   (interactive)
6537   (require 'gnus-sum)                   ; for gnus-list-identifiers
6538   (let ((cur (current-buffer))
6539         from subject date reply-to mrt mct
6540         references message-id follow-to
6541         (inhibit-point-motion-hooks t)
6542         (message-this-is-news t)
6543         followup-to distribution newsgroups gnus-warning posted-to)
6544     (save-restriction
6545       (narrow-to-region
6546        (goto-char (point-min))
6547        (if (search-forward "\n\n" nil t)
6548            (1- (point))
6549          (point-max)))
6550       (when (functionp message-followup-to-function)
6551         (setq follow-to
6552               (funcall message-followup-to-function)))
6553       (setq from (message-fetch-field "from")
6554             date (message-fetch-field "date")
6555             subject (or (message-fetch-field "subject") "none")
6556             references (message-fetch-field "references")
6557             message-id (message-fetch-field "message-id" t)
6558             followup-to (message-fetch-field "followup-to")
6559             newsgroups (message-fetch-field "newsgroups")
6560             posted-to (message-fetch-field "posted-to")
6561             reply-to (message-fetch-field "reply-to")
6562             mrt (message-fetch-field "mail-reply-to")
6563             distribution (message-fetch-field "distribution")
6564             mct (message-fetch-field "mail-copies-to"))
6565       (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
6566                  (string-match "<[^>]+>" gnus-warning))
6567         (setq message-id (match-string 0 gnus-warning)))
6568       ;; Remove bogus distribution.
6569       (when (and (stringp distribution)
6570                  (let ((case-fold-search t))
6571                    (string-match "world" distribution)))
6572         (setq distribution nil))
6573       ;; Strip list identifiers, "Re: ", and "was:"
6574       (setq subject (message-simplify-subject subject))
6575       (widen))
6576
6577     (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
6578
6579     (setq message-reply-headers
6580           (vector 0 subject from date message-id references 0 0 ""))
6581
6582     (message-setup
6583      `((Subject . ,subject)
6584        ,@(cond
6585           (to-newsgroups
6586            (list (cons 'Newsgroups to-newsgroups)))
6587           (follow-to follow-to)
6588           ((and followup-to message-use-followup-to)
6589            (list
6590             (cond
6591              ((equal (downcase followup-to) "poster")
6592               (if (or (eq message-use-followup-to 'use)
6593                       (message-y-or-n-p "Obey Followup-To: poster? " t "\
6594 You should normally obey the Followup-To: header.
6595
6596 `Followup-To: poster' sends your response via e-mail instead of news.
6597
6598 A typical situation where `Followup-To: poster' is used is when the poster
6599 does not read the newsgroup, so he wouldn't see any replies sent to it.
6600
6601 You may customize the variable `message-use-followup-to', if you
6602 want to get rid of this query permanently."))
6603                   (progn
6604                     (setq message-this-is-news nil)
6605                     (cons 'To (or mrt reply-to from "")))
6606                 (cons 'Newsgroups newsgroups)))
6607              (t
6608               (if (or (equal followup-to newsgroups)
6609                       (not (eq message-use-followup-to 'ask))
6610                       (message-y-or-n-p
6611                        (concat "Obey Followup-To: " followup-to "? ") t "\
6612 You should normally obey the Followup-To: header.
6613
6614         `Followup-To: " followup-to "'
6615 directs your response to " (if (string-match "," followup-to)
6616                                "the specified newsgroups"
6617                              "that newsgroup only") ".
6618
6619 If a message is posted to several newsgroups, Followup-To is often
6620 used to direct the following discussion to one newsgroup only,
6621 because discussions that are spread over several newsgroup tend to
6622 be fragmented and very difficult to follow.
6623
6624 Also, some source/announcement newsgroups are not intended for discussion;
6625 responses here are directed to other newsgroups.
6626
6627 You may customize the variable `message-use-followup-to', if you
6628 want to get rid of this query permanently."))
6629                   (cons 'Newsgroups followup-to)
6630                 (cons 'Newsgroups newsgroups))))))
6631           (posted-to
6632            `((Newsgroups . ,posted-to)))
6633           (t
6634            `((Newsgroups . ,newsgroups))))
6635        ,@(and distribution (list (cons 'Distribution distribution)))
6636        ,@(when (and mct
6637                     (not (or (equal (downcase mct) "never")
6638                              (equal (downcase mct) "nobody"))))
6639            (list (cons 'Cc (if (or (equal (downcase mct) "always")
6640                                    (equal (downcase mct) "poster"))
6641                                (or mrt reply-to from "")
6642                              mct)))))
6643
6644      cur)))
6645
6646 (defun message-is-yours-p ()
6647   "Non-nil means current article is yours.
6648 If you have added 'cancel-messages to `message-shoot-gnksa-feet', all articles
6649 are yours except those that have Cancel-Lock header not belonging to you.
6650 Instead of shooting GNKSA feet, you should modify `message-alternative-emails'
6651 regexp to match all of yours addresses."
6652   ;; Canlock-logic as suggested by Per Abrahamsen
6653   ;; <abraham@dina.kvl.dk>
6654   ;;
6655   ;; IF article has cancel-lock THEN
6656   ;;   IF we can verify it THEN
6657   ;;     issue cancel
6658   ;;   ELSE
6659   ;;     error: cancellock: article is not yours
6660   ;; ELSE
6661   ;;   Use old rules, comparing sender...
6662   (save-excursion
6663     (save-restriction
6664       (message-narrow-to-head-1)
6665       (if (message-fetch-field "Cancel-Lock")
6666           (if (null (canlock-verify))
6667               t
6668             (error "Failed to verify Cancel-lock: This article is not yours"))
6669         (let (sender from)
6670           (or
6671            (message-gnksa-enable-p 'cancel-messages)
6672            (and (setq sender (message-fetch-field "sender"))
6673                 (string-equal (downcase sender)
6674                               (downcase (message-make-sender))))
6675            ;; Email address in From field equals to our address
6676            (and (setq from (message-fetch-field "from"))
6677                 (string-equal
6678                  (downcase (car (mail-header-parse-address from)))
6679                  (downcase (car (mail-header-parse-address
6680                                  (message-make-from))))))
6681            ;; Email address in From field matches
6682            ;; 'message-alternative-emails' regexp
6683            (and from
6684                 message-alternative-emails
6685                 (string-match
6686                  message-alternative-emails
6687                  (car (mail-header-parse-address from))))))))))
6688
6689 ;;;###autoload
6690 (defun message-cancel-news (&optional arg)
6691   "Cancel an article you posted.
6692 If ARG, allow editing of the cancellation message."
6693   (interactive "P")
6694   (unless (message-news-p)
6695     (error "This is not a news article; canceling is impossible"))
6696   (let (from newsgroups message-id distribution buf)
6697     (save-excursion
6698       ;; Get header info from original article.
6699       (save-restriction
6700         (message-narrow-to-head-1)
6701         (setq from (message-fetch-field "from")
6702               newsgroups (message-fetch-field "newsgroups")
6703               message-id (message-fetch-field "message-id" t)
6704               distribution (message-fetch-field "distribution")))
6705       ;; Make sure that this article was written by the user.
6706       (unless (message-is-yours-p)
6707         (error "This article is not yours"))
6708       (when (yes-or-no-p "Do you really want to cancel this article? ")
6709         ;; Make control message.
6710         (if arg
6711             (message-news)
6712           (setq buf (set-buffer (get-buffer-create " *message cancel*"))))
6713         (erase-buffer)
6714         (insert "Newsgroups: " newsgroups "\n"
6715                 "From: " from "\n"
6716                 "Subject: cmsg cancel " message-id "\n"
6717                 "Control: cancel " message-id "\n"
6718                 (if distribution
6719                     (concat "Distribution: " distribution "\n")
6720                   "")
6721                 mail-header-separator "\n"
6722                 message-cancel-message)
6723         (run-hooks 'message-cancel-hook)
6724         (unless arg
6725           (message "Canceling your article...")
6726           (if (let ((message-syntax-checks
6727                      'dont-check-for-anything-just-trust-me))
6728                 (funcall message-send-news-function))
6729               (message "Canceling your article...done"))
6730           (kill-buffer buf))))))
6731
6732 ;;;###autoload
6733 (defun message-supersede ()
6734   "Start composing a message to supersede the current message.
6735 This is done simply by taking the old article and adding a Supersedes
6736 header line with the old Message-ID."
6737   (interactive)
6738   (let ((cur (current-buffer)))
6739     ;; Check whether the user owns the article that is to be superseded.
6740     (unless (message-is-yours-p)
6741       (error "This article is not yours"))
6742     ;; Get a normal message buffer.
6743     (message-pop-to-buffer (message-buffer-name "supersede"))
6744     (insert-buffer-substring cur)
6745     (mime-to-mml)
6746     (message-narrow-to-head-1)
6747     ;; Remove unwanted headers.
6748     (when message-ignored-supersedes-headers
6749       (message-remove-header message-ignored-supersedes-headers t))
6750     (goto-char (point-min))
6751     (if (not (re-search-forward "^Message-ID: " nil t))
6752         (error "No Message-ID in this article")
6753       (replace-match "Supersedes: " t t))
6754     (goto-char (point-max))
6755     (insert mail-header-separator)
6756     (widen)
6757     (forward-line 1)))
6758
6759 ;;;###autoload
6760 (defun message-recover ()
6761   "Reread contents of current buffer from its last auto-save file."
6762   (interactive)
6763   (let ((file-name (make-auto-save-file-name)))
6764     (cond ((save-window-excursion
6765              (if (not (eq system-type 'vax-vms))
6766                  (with-output-to-temp-buffer "*Directory*"
6767                    (with-current-buffer standard-output
6768                      (fundamental-mode)) ; for Emacs 20.4+
6769                    (buffer-disable-undo standard-output)
6770                    (let ((default-directory "/"))
6771                      (call-process
6772                       "ls" nil standard-output nil "-l" file-name))))
6773              (yes-or-no-p (format "Recover auto save file %s? " file-name)))
6774            (let ((buffer-read-only nil))
6775              (erase-buffer)
6776              (insert-file-contents file-name nil)))
6777           (t (error "message-recover cancelled")))))
6778
6779 ;;; Washing Subject:
6780
6781 (defun message-wash-subject (subject)
6782   "Remove junk like \"Re:\", \"(fwd)\", etc. added to subject string SUBJECT.
6783 Previous forwarders, replyers, etc. may add it."
6784   (with-temp-buffer
6785     (insert subject)
6786     (goto-char (point-min))
6787     ;; strip Re/Fwd stuff off the beginning
6788     (while (re-search-forward
6789             "\\([Rr][Ee]:\\|[Ff][Ww][Dd]\\(\\[[0-9]*\\]\\)?:\\|[Ff][Ww]:\\)" nil t)
6790       (replace-match ""))
6791
6792     ;; and gnus-style forwards [foo@bar.com] subject
6793     (goto-char (point-min))
6794     (while (re-search-forward "\\[[^ \t]*\\(@\\|\\.\\)[^ \t]*\\]" nil t)
6795       (replace-match ""))
6796
6797     ;; and off the end
6798     (goto-char (point-max))
6799     (while (re-search-backward "([Ff][Ww][Dd])" nil t)
6800       (replace-match ""))
6801
6802     ;; and finally, any whitespace that was left-over
6803     (goto-char (point-min))
6804     (while (re-search-forward "^[ \t]+" nil t)
6805       (replace-match ""))
6806     (goto-char (point-max))
6807     (while (re-search-backward "[ \t]+$" nil t)
6808       (replace-match ""))
6809
6810     (buffer-string)))
6811
6812 ;;; Forwarding messages.
6813
6814 (defvar message-forward-decoded-p nil
6815   "Non-nil means the original message is decoded.")
6816
6817 (defun message-forward-subject-name-subject (subject)
6818   "Generate a SUBJECT for a forwarded message.
6819 The form is: [Source] Subject, where if the original message was mail,
6820 Source is the name of the sender, and if the original message was
6821 news, Source is the list of newsgroups is was posted to."
6822   (let* ((group (message-fetch-field "newsgroups"))
6823          (from (message-fetch-field "from"))
6824          (prefix
6825           (if group
6826               (gnus-group-decoded-name group)
6827             (or (and from (or
6828                            (car (gnus-extract-address-components from))
6829                            (cadr (gnus-extract-address-components from))))
6830                 "(nowhere)"))))
6831     (concat "["
6832             (if message-forward-decoded-p
6833                 prefix
6834               (mail-decode-encoded-word-string prefix))
6835             "] " subject)))
6836
6837 (defun message-forward-subject-author-subject (subject)
6838   "Generate a SUBJECT for a forwarded message.
6839 The form is: [Source] Subject, where if the original message was mail,
6840 Source is the sender, and if the original message was news, Source is
6841 the list of newsgroups is was posted to."
6842   (let* ((group (message-fetch-field "newsgroups"))
6843          (prefix
6844           (if group
6845               (gnus-group-decoded-name group)
6846             (or (message-fetch-field "from")
6847                 "(nowhere)"))))
6848     (concat "["
6849             (if message-forward-decoded-p
6850                 prefix
6851               (mail-decode-encoded-word-string prefix))
6852             "] " subject)))
6853
6854 (defun message-forward-subject-fwd (subject)
6855   "Generate a SUBJECT for a forwarded message.
6856 The form is: Fwd: Subject, where Subject is the original subject of
6857 the message."
6858   (if (string-match "^Fwd: " subject)
6859       subject
6860     (concat "Fwd: " subject)))
6861
6862 (defun message-make-forward-subject ()
6863   "Return a Subject header suitable for the message in the current buffer."
6864   (save-excursion
6865     (save-restriction
6866       (message-narrow-to-head-1)
6867       (let ((funcs message-make-forward-subject-function)
6868             (subject (message-fetch-field "Subject")))
6869         (setq subject
6870               (if subject
6871                   (if message-forward-decoded-p
6872                       subject
6873                     (mail-decode-encoded-word-string subject))
6874                 ""))
6875         (when message-wash-forwarded-subjects
6876           (setq subject (message-wash-subject subject)))
6877         ;; Make sure funcs is a list.
6878         (and funcs
6879              (not (listp funcs))
6880              (setq funcs (list funcs)))
6881         ;; Apply funcs in order, passing subject generated by previous
6882         ;; func to the next one.
6883         (dolist (func funcs)
6884           (when (functionp func)
6885             (setq subject (funcall func subject))))
6886         subject))))
6887
6888 (defvar gnus-article-decoded-p)
6889
6890
6891 ;;;###autoload
6892 (defun message-forward (&optional news digest)
6893   "Forward the current message via mail.
6894 Optional NEWS will use news to forward instead of mail.
6895 Optional DIGEST will use digest to forward."
6896   (interactive "P")
6897   (let* ((cur (current-buffer))
6898          (message-forward-decoded-p
6899           (if (local-variable-p 'gnus-article-decoded-p (current-buffer))
6900               gnus-article-decoded-p ;; In an article buffer.
6901             message-forward-decoded-p))
6902          (subject (message-make-forward-subject)))
6903     (if news
6904         (message-news nil subject)
6905       (message-mail nil subject))
6906     (message-forward-make-body cur digest)))
6907
6908 (defun message-forward-make-body-plain (forward-buffer)
6909   (insert
6910    "\n-------------------- Start of forwarded message --------------------\n")
6911   (let ((b (point)) e)
6912     (insert
6913      (with-temp-buffer
6914        (mm-disable-multibyte)
6915        (insert
6916         (with-current-buffer forward-buffer
6917           (mm-with-unibyte-current-buffer (buffer-string))))
6918        (mm-enable-multibyte)
6919        (mime-to-mml)
6920        (goto-char (point-min))
6921        (when (looking-at "From ")
6922          (replace-match "X-From-Line: "))
6923        (buffer-string)))
6924     (setq e (point))
6925     (insert
6926      "\n-------------------- End of forwarded message --------------------\n")
6927     (message-remove-ignored-headers b e)))
6928
6929 (defun message-remove-ignored-headers (b e)
6930   (when message-forward-ignored-headers
6931     (save-restriction
6932       (narrow-to-region b e)
6933       (goto-char b)
6934       (narrow-to-region (point)
6935                         (or (search-forward "\n\n" nil t) (point)))
6936       (let ((ignored (if (stringp message-forward-ignored-headers)
6937                          (list message-forward-ignored-headers)
6938                        message-forward-ignored-headers)))
6939         (dolist (elem ignored)
6940           (message-remove-header elem t))))))
6941
6942 (defun message-forward-make-body-mime (forward-buffer)
6943   (let ((b (point)))
6944     (insert "\n\n<#part type=message/rfc822 disposition=inline raw=t>\n")
6945     (save-restriction
6946       (narrow-to-region (point) (point))
6947       (mml-insert-buffer forward-buffer)
6948       (goto-char (point-min))
6949       (when (looking-at "From ")
6950         (replace-match "X-From-Line: "))
6951       (goto-char (point-max)))
6952     (insert "<#/part>\n")
6953     ;; Consider there is no illegible text.
6954     (add-text-properties
6955      b (point)
6956      `(no-illegible-text t rear-nonsticky t start-open t))))
6957
6958 (defun message-forward-make-body-mml (forward-buffer)
6959   (insert "\n\n<#mml type=message/rfc822 disposition=inline>\n")
6960   (let ((b (point)) e)
6961     (if (not message-forward-decoded-p)
6962         (insert
6963          (with-temp-buffer
6964            (mm-disable-multibyte)
6965            (insert
6966             (with-current-buffer forward-buffer
6967               (mm-with-unibyte-current-buffer (buffer-string))))
6968            (mm-enable-multibyte)
6969            (mime-to-mml)
6970            (goto-char (point-min))
6971            (when (looking-at "From ")
6972              (replace-match "X-From-Line: "))
6973            (buffer-string)))
6974       (save-restriction
6975         (narrow-to-region (point) (point))
6976         (mml-insert-buffer forward-buffer)
6977         (goto-char (point-min))
6978         (when (looking-at "From ")
6979           (replace-match "X-From-Line: "))
6980         (goto-char (point-max))))
6981     (setq e (point))
6982     (insert "<#/mml>\n")
6983     (when (and (not message-forward-decoded-p)
6984                message-forward-ignored-headers)
6985       (message-remove-ignored-headers b e))))
6986
6987 (defun message-forward-make-body-digest-plain (forward-buffer)
6988   (insert
6989    "\n-------------------- Start of forwarded message --------------------\n")
6990   (let ((b (point)) e)
6991     (mml-insert-buffer forward-buffer)
6992     (setq e (point))
6993     (insert
6994      "\n-------------------- End of forwarded message --------------------\n")))
6995
6996 (defun message-forward-make-body-digest-mime (forward-buffer)
6997   (insert "\n<#multipart type=digest>\n")
6998   (let ((b (point)) e)
6999     (insert-buffer-substring forward-buffer)
7000     (setq e (point))
7001     (insert "<#/multipart>\n")
7002     (save-restriction
7003       (narrow-to-region b e)
7004       (goto-char b)
7005       (narrow-to-region (point)
7006                         (or (search-forward "\n\n" nil t) (point)))
7007       (delete-region (point-min) (point-max)))))
7008
7009 (defun message-forward-make-body-digest (forward-buffer)
7010   (if message-forward-as-mime
7011       (message-forward-make-body-digest-mime forward-buffer)
7012     (message-forward-make-body-digest-plain forward-buffer)))
7013
7014 (eval-and-compile
7015   (autoload 'mm-uu-dissect-text-parts "mm-uu")
7016   (autoload 'mm-uu-dissect "mm-uu"))
7017
7018 (defun message-signed-or-encrypted-p (&optional dont-emulate-mime handles)
7019   "Say whether the current buffer contains signed or encrypted message.
7020 If DONT-EMULATE-MIME is nil, this function does the MIME emulation on
7021 messages that don't conform to PGP/MIME described in RFC2015.  HANDLES
7022 is for the internal use."
7023   (unless handles
7024     (let ((mm-decrypt-option 'never)
7025           (mm-verify-option 'never))
7026       (if (setq handles (mm-dissect-buffer nil t))
7027           (unless dont-emulate-mime
7028             (mm-uu-dissect-text-parts handles))
7029         (unless dont-emulate-mime
7030           (setq handles (mm-uu-dissect))))))
7031   ;; Check text/plain message in which there is a signed or encrypted
7032   ;; body that has been encoded by B or Q.
7033   (unless (or handles dont-emulate-mime)
7034     (let ((cur (current-buffer))
7035           (mm-decrypt-option 'never)
7036           (mm-verify-option 'never))
7037       (with-temp-buffer
7038         (insert-buffer-substring cur)
7039         (when (setq handles (mm-dissect-buffer t t))
7040           (if (and (prog1
7041                        (bufferp (car handles))
7042                      (mm-destroy-parts handles))
7043                    (equal (mm-handle-media-type handles) "text/plain"))
7044               (progn
7045                 (mm-decode-content-transfer-encoding
7046                  (mm-handle-encoding handles))
7047                 (setq handles (mm-uu-dissect)))
7048             (setq handles nil))))))
7049   (when handles
7050     (prog1
7051         (catch 'found
7052           (dolist (handle (if (stringp (car handles))
7053                               (if (member (car handles)
7054                                           '("multipart/signed"
7055                                             "multipart/encrypted"))
7056                                   (throw 'found t)
7057                                 (cdr handles))
7058                             (list handles)))
7059             (if (stringp (car handle))
7060                 (when (message-signed-or-encrypted-p dont-emulate-mime handle)
7061                   (throw 'found t))
7062               (when (and (bufferp (car handle))
7063                          (equal (mm-handle-media-type handle)
7064                                 "message/rfc822"))
7065                 (with-current-buffer (mm-handle-buffer handle)
7066                   (when (message-signed-or-encrypted-p dont-emulate-mime)
7067                     (throw 'found t)))))))
7068       (mm-destroy-parts handles))))
7069
7070 ;;;###autoload
7071 (defun message-forward-make-body (forward-buffer &optional digest)
7072   ;; Put point where we want it before inserting the forwarded
7073   ;; message.
7074   (if message-forward-before-signature
7075       (message-goto-body)
7076     (goto-char (point-max)))
7077   (if digest
7078       (message-forward-make-body-digest forward-buffer)
7079     (if message-forward-as-mime
7080         (if (and message-forward-show-mml
7081                  (not (and (eq message-forward-show-mml 'best)
7082                            ;; Use the raw form in the body if it contains
7083                            ;; signed or encrypted message so as not to be
7084                            ;; destroyed by re-encoding.
7085                            (with-current-buffer forward-buffer
7086                              (condition-case nil
7087                                  (message-signed-or-encrypted-p)
7088                                (error t))))))
7089             (message-forward-make-body-mml forward-buffer)
7090           (message-forward-make-body-mime forward-buffer))
7091       (message-forward-make-body-plain forward-buffer)))
7092   (message-position-point))
7093
7094 ;;;###autoload
7095 (defun message-forward-rmail-make-body (forward-buffer)
7096   (save-window-excursion
7097     (set-buffer forward-buffer)
7098     (if (rmail-msg-is-pruned)
7099         (rmail-msg-restore-non-pruned-header)))
7100   (message-forward-make-body forward-buffer))
7101
7102 ;; Fixme: Should have defcustom.
7103 ;;;###autoload
7104 (defun message-insinuate-rmail ()
7105   "Let RMAIL use message to forward."
7106   (interactive)
7107   (setq rmail-enable-mime-composing t)
7108   (setq rmail-insert-mime-forwarded-message-function
7109         'message-forward-rmail-make-body))
7110
7111 ;;;###autoload
7112 (defun message-resend (address)
7113   "Resend the current article to ADDRESS."
7114   (interactive
7115    (list (message-read-from-minibuffer "Resend message to: ")))
7116   (message "Resending message to %s..." address)
7117   (save-excursion
7118     (let ((cur (current-buffer))
7119           beg)
7120       ;; We first set up a normal mail buffer.
7121       (unless (message-mail-user-agent)
7122         (set-buffer (get-buffer-create " *message resend*"))
7123         (erase-buffer))
7124       (let ((message-this-is-mail t)
7125             message-generate-hashcash
7126             message-setup-hook)
7127         (message-setup `((To . ,address))))
7128       ;; Insert our usual headers.
7129       (message-generate-headers '(From Date To Message-ID))
7130       (message-narrow-to-headers)
7131       ;; Remove X-Draft-From header etc.
7132       (message-remove-header message-ignored-mail-headers t)
7133       ;; Rename them all to "Resent-*".
7134       (goto-char (point-min))
7135       (while (re-search-forward "^[A-Za-z]" nil t)
7136         (forward-char -1)
7137         (insert "Resent-"))
7138       (widen)
7139       (forward-line)
7140       (delete-region (point) (point-max))
7141       (setq beg (point))
7142       ;; Insert the message to be resent.
7143       (insert-buffer-substring cur)
7144       (goto-char (point-min))
7145       (search-forward "\n\n")
7146       (forward-char -1)
7147       (save-restriction
7148         (narrow-to-region beg (point))
7149         (message-remove-header message-ignored-resent-headers t)
7150         (goto-char (point-max)))
7151       (insert mail-header-separator)
7152       ;; Rename all old ("Also-")Resent headers.
7153       (while (re-search-backward "^\\(Also-\\)*Resent-" beg t)
7154         (beginning-of-line)
7155         (insert "Also-"))
7156       ;; Quote any "From " lines at the beginning.
7157       (goto-char beg)
7158       (when (looking-at "From ")
7159         (replace-match "X-From-Line: "))
7160       ;; Send it.
7161       (let ((message-inhibit-body-encoding t)
7162             message-required-mail-headers
7163             message-generate-hashcash
7164             rfc2047-encode-encoded-words)
7165         (message-send-mail))
7166       (kill-buffer (current-buffer)))
7167     (message "Resending message to %s...done" address)))
7168
7169 ;;;###autoload
7170 (defun message-bounce ()
7171   "Re-mail the current message.
7172 This only makes sense if the current message is a bounce message that
7173 contains some mail you have written which has been bounced back to
7174 you."
7175   (interactive)
7176   (let ((handles (mm-dissect-buffer t))
7177         boundary)
7178     (message-pop-to-buffer (message-buffer-name "bounce"))
7179     (if (stringp (car handles))
7180         ;; This is a MIME bounce.
7181         (mm-insert-part (car (last handles)))
7182       ;; This is a non-MIME bounce, so we try to remove things
7183       ;; manually.
7184       (mm-insert-part handles)
7185       (undo-boundary)
7186       (goto-char (point-min))
7187       (re-search-forward "\n\n+" nil t)
7188       (setq boundary (point))
7189       ;; We remove everything before the bounced mail.
7190       (if (or (re-search-forward message-unsent-separator nil t)
7191               (progn
7192                 (search-forward "\n\n" nil 'move)
7193                 (re-search-backward "^Return-Path:.*\n" boundary t)))
7194           (progn
7195             (forward-line 1)
7196             (delete-region (point-min)
7197                            (if (re-search-forward "^[^ \n\t]+:" nil t)
7198                                (match-beginning 0)
7199                              (point))))
7200         (goto-char boundary)
7201         (when (re-search-backward "^.?From .*\n" nil t)
7202           (delete-region (match-beginning 0) (match-end 0)))))
7203     (mime-to-mml)
7204     (save-restriction
7205       (message-narrow-to-head-1)
7206       (message-remove-header message-ignored-bounced-headers t)
7207       (goto-char (point-max))
7208       (insert mail-header-separator))
7209     (message-position-point)))
7210
7211 ;;;
7212 ;;; Interactive entry points for new message buffers.
7213 ;;;
7214
7215 ;;;###autoload
7216 (defun message-mail-other-window (&optional to subject)
7217   "Like `message-mail' command, but display mail buffer in another window."
7218   (interactive)
7219   (unless (message-mail-user-agent)
7220     (let ((pop-up-windows t)
7221           (special-display-buffer-names nil)
7222           (special-display-regexps nil)
7223           (same-window-buffer-names nil)
7224           (same-window-regexps nil))
7225       (message-pop-to-buffer (message-buffer-name "mail" to))))
7226   (let ((message-this-is-mail t))
7227     (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))
7228                    nil nil nil 'switch-to-buffer-other-window)))
7229
7230 ;;;###autoload
7231 (defun message-mail-other-frame (&optional to subject)
7232   "Like `message-mail' command, but display mail buffer in another frame."
7233   (interactive)
7234   (unless (message-mail-user-agent)
7235     (let ((pop-up-frames t)
7236           (special-display-buffer-names nil)
7237           (special-display-regexps nil)
7238           (same-window-buffer-names nil)
7239           (same-window-regexps nil))
7240       (message-pop-to-buffer (message-buffer-name "mail" to))))
7241   (let ((message-this-is-mail t))
7242     (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))
7243                    nil nil nil 'switch-to-buffer-other-frame)))
7244
7245 ;;;###autoload
7246 (defun message-news-other-window (&optional newsgroups subject)
7247   "Start editing a news article to be sent."
7248   (interactive)
7249   (let ((pop-up-windows t)
7250         (special-display-buffer-names nil)
7251         (special-display-regexps nil)
7252         (same-window-buffer-names nil)
7253         (same-window-regexps nil))
7254     (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups)))
7255   (let ((message-this-is-news t))
7256     (message-setup `((Newsgroups . ,(or newsgroups ""))
7257                      (Subject . ,(or subject ""))))))
7258
7259 ;;;###autoload
7260 (defun message-news-other-frame (&optional newsgroups subject)
7261   "Start editing a news article to be sent."
7262   (interactive)
7263   (let ((pop-up-frames t)
7264         (special-display-buffer-names nil)
7265         (special-display-regexps nil)
7266         (same-window-buffer-names nil)
7267         (same-window-regexps nil))
7268     (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups)))
7269   (let ((message-this-is-news t))
7270     (message-setup `((Newsgroups . ,(or newsgroups ""))
7271                      (Subject . ,(or subject ""))))))
7272
7273 ;;; underline.el
7274
7275 ;; This code should be moved to underline.el (from which it is stolen).
7276
7277 ;;;###autoload
7278 (defun message-bold-region (start end)
7279   "Bold all nonblank characters in the region.
7280 Works by overstriking characters.
7281 Called from program, takes two arguments START and END
7282 which specify the range to operate on."
7283   (interactive "r")
7284   (save-excursion
7285     (let ((end1 (make-marker)))
7286       (move-marker end1 (max start end))
7287       (goto-char (min start end))
7288       (while (< (point) end1)
7289         (or (looking-at "[_\^@- ]")
7290             (insert (char-after) "\b"))
7291         (forward-char 1)))))
7292
7293 ;;;###autoload
7294 (defun message-unbold-region (start end)
7295   "Remove all boldness (overstruck characters) in the region.
7296 Called from program, takes two arguments START and END
7297 which specify the range to operate on."
7298   (interactive "r")
7299   (save-excursion
7300     (let ((end1 (make-marker)))
7301       (move-marker end1 (max start end))
7302       (goto-char (min start end))
7303       (while (search-forward "\b" end1 t)
7304         (if (eq (char-after) (char-after (- (point) 2)))
7305             (delete-char -2))))))
7306
7307 (defun message-exchange-point-and-mark ()
7308   "Exchange point and mark, but don't activate region if it was inactive."
7309   (unless (prog1
7310               (message-mark-active-p)
7311             (exchange-point-and-mark))
7312     (setq mark-active nil)))
7313
7314 (defalias 'message-make-overlay 'make-overlay)
7315 (defalias 'message-delete-overlay 'delete-overlay)
7316 (defalias 'message-overlay-put 'overlay-put)
7317 (defun message-kill-all-overlays ()
7318   (if (featurep 'xemacs)
7319       (map-extents (lambda (extent ignore) (delete-extent extent)))
7320     (mapcar #'delete-overlay (overlays-in (point-min) (point-max)))))
7321
7322 ;; Support for toolbar
7323 (defvar tool-bar-mode)
7324
7325 ;; Note: The :set function in the `message-tool-bar*' variables will only
7326 ;; affect _new_ message buffers.  We might add a function that walks thru all
7327 ;; message-mode buffers and force the update.
7328 (defun message-tool-bar-update (&optional symbol value)
7329   "Update message mode toolbar.
7330 Setter function for custom variables."
7331   (setq-default message-tool-bar-map nil)
7332   (when symbol
7333     ;; When used as ":set" function:
7334     (set-default symbol value)))
7335
7336 (defcustom message-tool-bar (if (eq gmm-tool-bar-style 'gnome)
7337                                 'message-tool-bar-gnome
7338                               'message-tool-bar-retro)
7339   "Specifies the message mode tool bar.
7340
7341 It can be either a list or a symbol refering to a list.  See
7342 `gmm-tool-bar-from-list' for the format of the list.  The
7343 default key map is `message-mode-map'.
7344
7345 Pre-defined symbols include `message-tool-bar-gnome' and
7346 `message-tool-bar-retro'."
7347   :type '(repeat gmm-tool-bar-list-item)
7348   :type '(choice (const :tag "GNOME style" message-tool-bar-gnome)
7349                  (const :tag "Retro look"  message-tool-bar-retro)
7350                  (repeat :tag "User defined list" gmm-tool-bar-item)
7351                  (symbol))
7352   :version "23.0" ;; No Gnus
7353   :initialize 'custom-initialize-default
7354   :set 'message-tool-bar-update
7355   :group 'message)
7356
7357 (defcustom message-tool-bar-gnome
7358   '((ispell-message "spell" nil
7359                     :visible (or (not (boundp 'flyspell-mode))
7360                                  (not flyspell-mode)))
7361     (flyspell-buffer "spell" t
7362                      :visible (and (boundp 'flyspell-mode)
7363                                    flyspell-mode)
7364                      :help "Flyspell whole buffer")
7365     (gmm-ignore "separator")
7366     (message-send-and-exit "mail/send")
7367     (message-dont-send "mail/save-draft")
7368     (message-kill-buffer "close") ;; stock_cancel
7369     (mml-attach-file "attach" mml-mode-map)
7370     (mml-preview "mail/preview" mml-mode-map)
7371     (mml-secure-message-sign-encrypt "lock" mml-mode-map :visible nil)
7372     (message-insert-importance-high "important" nil :visible nil)
7373     (message-insert-importance-low "unimportant" nil :visible nil)
7374     (message-insert-disposition-notification-to "receipt" nil :visible nil)
7375     (gmm-customize-mode "preferences" t :help "Edit mode preferences")
7376     (message-info "help" t :help "Message manual"))
7377   "List of items for the message tool bar (GNOME style).
7378
7379 See `gmm-tool-bar-from-list' for details on the format of the list."
7380   :type '(repeat gmm-tool-bar-item)
7381   :version "23.0" ;; No Gnus
7382   :initialize 'custom-initialize-default
7383   :set 'message-tool-bar-update
7384   :group 'message)
7385
7386 (defcustom message-tool-bar-retro
7387   '(;; Old Emacs 21 icon for consistency.
7388     (message-send-and-exit "gnus/mail-send")
7389     (message-kill-buffer "close")
7390     (message-dont-send "cancel")
7391     (mml-attach-file "attach" mml-mode-map)
7392     (ispell-message "spell")
7393     (mml-preview "preview" mml-mode-map)
7394     (message-insert-importance-high "gnus/important")
7395     (message-insert-importance-low "gnus/unimportant")
7396     (message-insert-disposition-notification-to "gnus/receipt"))
7397   "List of items for the message tool bar (retro style).
7398
7399 See `gmm-tool-bar-from-list' for details on the format of the list."
7400   :type '(repeat gmm-tool-bar-item)
7401   :version "23.0" ;; No Gnus
7402   :initialize 'custom-initialize-default
7403   :set 'message-tool-bar-update
7404   :group 'message)
7405
7406 (defcustom message-tool-bar-zap-list
7407   '(new-file open-file dired kill-buffer write-file
7408              print-buffer customize help)
7409   "List of icon items from the global tool bar.
7410 These items are not displayed on the message mode tool bar.
7411
7412 See `gmm-tool-bar-from-list' for the format of the list."
7413   :type 'gmm-tool-bar-zap-list
7414   :version "23.0" ;; No Gnus
7415   :initialize 'custom-initialize-default
7416   :set 'message-tool-bar-update
7417   :group 'message)
7418
7419 (defvar image-load-path)
7420
7421 (defun message-make-tool-bar (&optional force)
7422   "Make a message mode tool bar from `message-tool-bar-list'.
7423 When FORCE, rebuild the tool bar."
7424   (when (and (not (featurep 'xemacs))
7425              (boundp 'tool-bar-mode)
7426              tool-bar-mode
7427              (or (not message-tool-bar-map) force))
7428     (setq message-tool-bar-map
7429           (let* ((load-path
7430                   (gmm-image-load-path-for-library "message"
7431                                                    "mail/save-draft.xpm"
7432                                                    nil t))
7433                  (image-load-path (cons (car load-path)
7434                                         (when (boundp 'image-load-path)
7435                                           image-load-path))))
7436             (gmm-tool-bar-from-list message-tool-bar
7437                                     message-tool-bar-zap-list
7438                                     'message-mode-map))))
7439   message-tool-bar-map)
7440
7441 ;;; Group name completion.
7442
7443 (defcustom message-newgroups-header-regexp
7444   "^\\(Newsgroups\\|Followup-To\\|Posted-To\\|Gcc\\):"
7445   "Regexp that match headers that lists groups."
7446   :group 'message
7447   :type 'regexp)
7448
7449 (defcustom message-completion-alist
7450   (list (cons message-newgroups-header-regexp 'message-expand-group)
7451         '("^\\(Resent-\\)?\\(To\\|B?Cc\\):" . message-expand-name)
7452         '("^\\(Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\):"
7453           . message-expand-name)
7454         '("^\\(Disposition-Notification-To\\|Return-Receipt-To\\):"
7455           . message-expand-name))
7456   "Alist of (RE . FUN).  Use FUN for completion on header lines matching RE."
7457   :version "22.1"
7458   :group 'message
7459   :type '(alist :key-type regexp :value-type function))
7460
7461 (defcustom message-expand-name-databases
7462   (list 'bbdb 'eudc)
7463   "List of databases to try for name completion (`message-expand-name').
7464 Each element is a symbol and can be `bbdb' or `eudc'."
7465   :group 'message
7466   :type '(set (const bbdb) (const eudc)))
7467
7468 (defcustom message-tab-body-function nil
7469   "*Function to execute when `message-tab' (TAB) is executed in the body.
7470 If nil, the function bound in `text-mode-map' or `global-map' is executed."
7471   :version "22.1"
7472   :group 'message
7473   :link '(custom-manual "(message)Various Commands")
7474   :type '(choice (const nil)
7475                  function))
7476
7477 (defun message-tab ()
7478   "Complete names according to `message-completion-alist'.
7479 Execute function specified by `message-tab-body-function' when not in
7480 those headers."
7481   (interactive)
7482   (let ((alist message-completion-alist))
7483     (while (and alist
7484                 (let ((mail-abbrev-mode-regexp (caar alist)))
7485                   (not (mail-abbrev-in-expansion-header-p))))
7486       (setq alist (cdr alist)))
7487     (funcall (or (cdar alist) message-tab-body-function
7488                  (lookup-key text-mode-map "\t")
7489                  (lookup-key global-map "\t")
7490                  'indent-relative))))
7491
7492 (eval-and-compile
7493   (condition-case nil
7494       (with-temp-buffer
7495         (let ((standard-output (current-buffer)))
7496           (eval '(display-completion-list nil "")))
7497         (defalias 'message-display-completion-list 'display-completion-list))
7498     (error ;; Don't use `wrong-number-of-arguments' here because of XEmacs.
7499      (defun message-display-completion-list (completions &optional ignore)
7500        "Display the list of completions, COMPLETIONS, using `standard-output'."
7501        (display-completion-list completions)))))
7502
7503 (defun message-expand-group ()
7504   "Expand the group name under point."
7505   (let* ((b (save-excursion
7506               (save-restriction
7507                 (narrow-to-region
7508                  (save-excursion
7509                    (beginning-of-line)
7510                    (skip-chars-forward "^:")
7511                    (1+ (point)))
7512                  (point))
7513                 (skip-chars-backward "^, \t\n") (point))))
7514          (completion-ignore-case t)
7515          (string (buffer-substring b (progn (skip-chars-forward "^,\t\n ")
7516                                             (point))))
7517          (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb))
7518          (completions (all-completions string hashtb))
7519          comp)
7520     (delete-region b (point))
7521     (cond
7522      ((= (length completions) 1)
7523       (if (string= (car completions) string)
7524           (progn
7525             (insert string)
7526             (message "Only matching group"))
7527         (insert (car completions))))
7528      ((and (setq comp (try-completion string hashtb))
7529            (not (string= comp string)))
7530       (insert comp))
7531      (t
7532       (insert string)
7533       (if (not comp)
7534           (message "No matching groups")
7535         (save-selected-window
7536           (pop-to-buffer "*Completions*")
7537           (buffer-disable-undo)
7538           (let ((buffer-read-only nil))
7539             (erase-buffer)
7540             (let ((standard-output (current-buffer)))
7541               (message-display-completion-list (sort completions 'string<)
7542                                                string))
7543             (setq buffer-read-only nil)
7544             (goto-char (point-min))
7545             (delete-region (point) (progn (forward-line 3) (point))))))))))
7546
7547 (defun message-expand-name ()
7548   (cond ((and (memq 'eudc message-expand-name-databases)
7549                     (boundp 'eudc-protocol)
7550                     eudc-protocol)
7551          (eudc-expand-inline))
7552         ((and (memq 'bbdb message-expand-name-databases)
7553               (fboundp 'bbdb-complete-name))
7554          (bbdb-complete-name))
7555         (t
7556          (expand-abbrev))))
7557
7558 ;;; Help stuff.
7559
7560 (defun message-talkative-question (ask question show &rest text)
7561   "Call FUNCTION with argument QUESTION; optionally display TEXT... args.
7562 If SHOW is non-nil, the arguments TEXT... are displayed in a temp buffer.
7563 The following arguments may contain lists of values."
7564   (if (and show
7565            (setq text (message-flatten-list text)))
7566       (save-window-excursion
7567         (with-output-to-temp-buffer " *MESSAGE information message*"
7568           (with-current-buffer " *MESSAGE information message*"
7569             (fundamental-mode)          ; for Emacs 20.4+
7570             (mapc 'princ text)
7571             (goto-char (point-min))))
7572         (funcall ask question))
7573     (funcall ask question)))
7574
7575 (defun message-flatten-list (list)
7576   "Return a new, flat list that contains all elements of LIST.
7577
7578 \(message-flatten-list '(1 (2 3 (4 5 (6))) 7))
7579 => (1 2 3 4 5 6 7)"
7580   (cond ((consp list)
7581          (apply 'append (mapcar 'message-flatten-list list)))
7582         (list
7583          (list list))))
7584
7585 (defun message-generate-new-buffer-clone-locals (name &optional varstr)
7586   "Create and return a buffer with name based on NAME using `generate-new-buffer'.
7587 Then clone the local variables and values from the old buffer to the
7588 new one, cloning only the locals having a substring matching the
7589 regexp VARSTR."
7590   (let ((oldbuf (current-buffer)))
7591     (with-current-buffer (generate-new-buffer name)
7592       (message-clone-locals oldbuf varstr)
7593       (current-buffer))))
7594
7595 (defun message-clone-locals (buffer &optional varstr)
7596   "Clone the local variables from BUFFER to the current buffer."
7597   (let ((locals (with-current-buffer buffer (buffer-local-variables)))
7598         (regexp "^gnus\\|^nn\\|^message\\|^sendmail\\|^smtp\\|^user-mail-address"))
7599     (mapcar
7600      (lambda (local)
7601        (when (and (consp local)
7602                   (car local)
7603                   (string-match regexp (symbol-name (car local)))
7604                   (or (null varstr)
7605                       (string-match varstr (symbol-name (car local)))))
7606          (ignore-errors
7607            (set (make-local-variable (car local))
7608                 (cdr local)))))
7609      locals)))
7610
7611 ;;;
7612 ;;; MIME functions
7613 ;;;
7614
7615 (defvar message-inhibit-body-encoding nil)
7616
7617 (defun message-encode-message-body ()
7618   (unless message-inhibit-body-encoding
7619     (let ((mail-parse-charset (or mail-parse-charset
7620                                   message-default-charset))
7621           (case-fold-search t)
7622           lines content-type-p)
7623       (message-goto-body)
7624       (save-restriction
7625         (narrow-to-region (point) (point-max))
7626         (let ((new (mml-generate-mime)))
7627           (when new
7628             (delete-region (point-min) (point-max))
7629             (insert new)
7630             (goto-char (point-min))
7631             (if (eq (aref new 0) ?\n)
7632                 (delete-char 1)
7633               (search-forward "\n\n")
7634               (setq lines (buffer-substring (point-min) (1- (point))))
7635               (delete-region (point-min) (point))))))
7636       (save-restriction
7637         (message-narrow-to-headers-or-head)
7638         (message-remove-header "Mime-Version")
7639         (goto-char (point-max))
7640         (insert "MIME-Version: 1.0\n")
7641         (when lines
7642           (insert lines))
7643         (setq content-type-p
7644               (or mml-boundary
7645                   (re-search-backward "^Content-Type:" nil t))))
7646       (save-restriction
7647         (message-narrow-to-headers-or-head)
7648         (message-remove-first-header "Content-Type")
7649         (message-remove-first-header "Content-Transfer-Encoding"))
7650       ;; We always make sure that the message has a Content-Type
7651       ;; header.  This is because some broken MTAs and MUAs get
7652       ;; awfully confused when confronted with a message with a
7653       ;; MIME-Version header and without a Content-Type header.  For
7654       ;; instance, Solaris' /usr/bin/mail.
7655       (unless content-type-p
7656         (goto-char (point-min))
7657         ;; For unknown reason, MIME-Version doesn't exist.
7658         (when (re-search-forward "^MIME-Version:" nil t)
7659           (forward-line 1)
7660           (insert "Content-Type: text/plain; charset=us-ascii\n"))))))
7661
7662 (defun message-read-from-minibuffer (prompt &optional initial-contents)
7663   "Read from the minibuffer while providing abbrev expansion."
7664   (if (fboundp 'mail-abbrevs-setup)
7665       (let ((mail-abbrev-mode-regexp "")
7666             (minibuffer-setup-hook 'mail-abbrevs-setup)
7667             (minibuffer-local-map message-minibuffer-local-map))
7668         (read-from-minibuffer prompt initial-contents))
7669     (let ((minibuffer-setup-hook 'mail-abbrev-minibuffer-setup-hook)
7670           (minibuffer-local-map message-minibuffer-local-map))
7671       (read-string prompt initial-contents))))
7672
7673 (defun message-use-alternative-email-as-from ()
7674   "Set From field of the outgoing message to the first matching
7675 address in `message-alternative-emails', looking at To, Cc and
7676 From headers in the original article."
7677   (require 'mail-utils)
7678   (let* ((fields '("To" "Cc" "From"))
7679          (emails
7680           (split-string
7681            (mail-strip-quoted-names
7682             (mapconcat 'message-fetch-reply-field fields ","))
7683            "[ \f\t\n\r\v,]+"))
7684          email)
7685     (while emails
7686       (if (string-match message-alternative-emails (car emails))
7687           (setq email (car emails)
7688                 emails nil))
7689       (pop emails))
7690     (unless (or (not email) (equal email user-mail-address))
7691       (message-remove-header "From")
7692       (goto-char (point-max))
7693       (insert "From: " (let ((user-mail-address email)) (message-make-from))
7694               "\n"))))
7695
7696 (defun message-options-get (symbol)
7697   (cdr (assq symbol message-options)))
7698
7699 (defun message-options-set (symbol value)
7700   (let ((the-cons (assq symbol message-options)))
7701     (if the-cons
7702         (if value
7703             (setcdr the-cons value)
7704           (setq message-options (delq the-cons message-options)))
7705       (and value
7706            (push (cons symbol value) message-options))))
7707   value)
7708
7709 (defun message-options-set-recipient ()
7710   (save-restriction
7711     (message-narrow-to-headers-or-head)
7712     (message-options-set 'message-sender
7713                          (mail-strip-quoted-names
7714                           (message-fetch-field "from")))
7715     (message-options-set 'message-recipients
7716                          (mail-strip-quoted-names
7717                           (let ((to (message-fetch-field "to"))
7718                                 (cc (message-fetch-field "cc"))
7719                                 (bcc (message-fetch-field "bcc")))
7720                             (concat
7721                              (or to "")
7722                              (if (and to cc) ", ")
7723                              (or cc "")
7724                              (if (and (or to cc) bcc) ", ")
7725                              (or bcc "")))))))
7726
7727 (defun message-hide-headers ()
7728   "Hide headers based on the `message-hidden-headers' variable."
7729   (let ((regexps (if (stringp message-hidden-headers)
7730                      (list message-hidden-headers)
7731                    message-hidden-headers))
7732         (inhibit-point-motion-hooks t)
7733         (after-change-functions nil)
7734         (end-of-headers (point-min)))
7735     (when regexps
7736       (save-excursion
7737         (save-restriction
7738           (message-narrow-to-headers)
7739           (goto-char (point-min))
7740           (while (not (eobp))
7741             (if (not (message-hide-header-p regexps))
7742                 (message-next-header)
7743               (let ((begin (point))
7744                     header header-len)
7745                 (message-next-header)
7746                 (setq header (buffer-substring begin (point))
7747                       header-len (- (point) begin))
7748                 (delete-region begin (point))
7749                 (goto-char end-of-headers)
7750                 (insert header)
7751                 (setq end-of-headers
7752                       (+ end-of-headers header-len))))))))
7753     (narrow-to-region end-of-headers (point-max))))
7754
7755 (defun message-hide-header-p (regexps)
7756   (let ((result nil)
7757         (reverse nil))
7758     (when (eq (car regexps) 'not)
7759       (setq reverse t)
7760       (pop regexps))
7761     (dolist (regexp regexps)
7762       (setq result (or result (looking-at regexp))))
7763     (if reverse
7764         (not result)
7765       result)))
7766
7767 (defun message-put-addresses-in-ecomplete ()
7768   (dolist (header '("to" "cc" "from" "reply-to"))
7769     (let ((value (message-field-value header)))
7770       (dolist (string (mail-header-parse-addresses value 'raw))
7771         (setq string
7772               (gnus-replace-in-string
7773                (gnus-replace-in-string string "^ +\\| +$" "") "\n" ""))
7774         (ecomplete-add-item 'mail (car (mail-header-parse-address string))
7775                             string))))
7776   (ecomplete-save))
7777
7778 (defun message-display-abbrev (&optional choose)
7779   "Display the next possible abbrev for the text before point."
7780   (interactive (list t))
7781   (when (and (memq (char-after (point-at-bol)) '(?C ?T ?\t ? ))
7782              (message-point-in-header-p)
7783              (save-excursion
7784                (beginning-of-line)
7785                (while (and (memq (char-after) '(?\t ? ))
7786                            (zerop (forward-line -1))))
7787                (looking-at "To:\\|Cc:")))
7788     (let* ((end (point))
7789            (start (save-excursion
7790                     (and (re-search-backward "[\n\t ]" nil t)
7791                          (1+ (point)))))
7792            (word (when start (buffer-substring start end)))
7793            (match (when (and word
7794                              (not (zerop (length word))))
7795                     (ecomplete-display-matches 'mail word choose))))
7796       (when (and choose match)
7797         (delete-region start end)
7798         (insert match)))))
7799
7800 (when (featurep 'xemacs)
7801   (require 'messagexmas)
7802   (message-xmas-redefine))
7803
7804 (provide 'message)
7805
7806 (run-hooks 'message-load-hook)
7807
7808 ;; Local Variables:
7809 ;; coding: iso-8859-1
7810 ;; End:
7811
7812 ;; arch-tag: 94b32cac-4504-4b6c-8181-030ebf380ee0
7813 ;;; message.el ends here