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