(message-idna-to-ascii-rhs-1): Don't choke on
[gnus] / lisp / message.el
1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: mail, news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This mode provides mail-sending facilities from within Emacs.  It
28 ;; consists mainly of large chunks of code from the sendmail.el,
29 ;; gnus-msg.el and rnewspost.el files.
30
31 ;;; Code:
32
33 (eval-when-compile
34   (require 'cl)
35   (defvar gnus-message-group-art)
36   (defvar gnus-list-identifiers)) ; gnus-sum is required where necessary
37 (require 'canlock)
38 (require 'mailheader)
39 (require 'nnheader)
40 ;; This is apparently necessary even though things are autoloaded.
41 ;; Because we dynamically bind mail-abbrev-mode-regexp, we'd better
42 ;; require mailabbrev here.
43 (if (featurep 'xemacs)
44     (require 'mail-abbrevs)
45   (require 'mailabbrev))
46 (require 'mail-parse)
47 (require 'mml)
48 (require 'rfc822)
49
50 (defgroup message '((user-mail-address custom-variable)
51                     (user-full-name custom-variable))
52   "Mail and news message composing."
53   :link '(custom-manual "(message)Top")
54   :group 'mail
55   :group 'news)
56
57 (put 'user-mail-address 'custom-type 'string)
58 (put 'user-full-name 'custom-type 'string)
59
60 (defgroup message-various nil
61   "Various Message Variables"
62   :link '(custom-manual "(message)Various Message Variables")
63   :group 'message)
64
65 (defgroup message-buffers nil
66   "Message Buffers"
67   :link '(custom-manual "(message)Message Buffers")
68   :group 'message)
69
70 (defgroup message-sending nil
71   "Message Sending"
72   :link '(custom-manual "(message)Sending Variables")
73   :group 'message)
74
75 (defgroup message-interface nil
76   "Message Interface"
77   :link '(custom-manual "(message)Interface")
78   :group 'message)
79
80 (defgroup message-forwarding nil
81   "Message Forwarding"
82   :link '(custom-manual "(message)Forwarding")
83   :group 'message-interface)
84
85 (defgroup message-insertion nil
86   "Message Insertion"
87   :link '(custom-manual "(message)Insertion")
88   :group 'message)
89
90 (defgroup message-headers nil
91   "Message Headers"
92   :link '(custom-manual "(message)Message Headers")
93   :group 'message)
94
95 (defgroup message-news nil
96   "Composing News Messages"
97   :group 'message)
98
99 (defgroup message-mail nil
100   "Composing Mail Messages"
101   :group 'message)
102
103 (defgroup message-faces nil
104   "Faces used for message composing."
105   :group 'message
106   :group 'faces)
107
108 (defcustom message-directory "~/Mail/"
109   "*Directory from which all other mail file variables are derived."
110   :group 'message-various
111   :type 'directory)
112
113 (defcustom message-max-buffers 10
114   "*How many buffers to keep before starting to kill them off."
115   :group 'message-buffers
116   :type 'integer)
117
118 (defcustom message-send-rename-function nil
119   "Function called to rename the buffer after sending it."
120   :group 'message-buffers
121   :type '(choice function (const nil)))
122
123 (defcustom message-fcc-handler-function 'message-output
124   "*A function called to save outgoing articles.
125 This function will be called with the name of the file to store the
126 article in.  The default function is `message-output' which saves in Unix
127 mailbox format."
128   :type '(radio (function-item message-output)
129                 (function :tag "Other"))
130   :group 'message-sending)
131
132 (defcustom message-fcc-externalize-attachments nil
133   "If non-nil, attachments are included as external parts in Fcc copies."
134   :type 'boolean
135   :group 'message-sending)
136
137 (defcustom message-courtesy-message
138   "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
139   "*This is inserted at the start of a mailed copy of a posted message.
140 If the string contains the format spec \"%s\", the Newsgroups
141 the article has been posted to will be inserted there.
142 If this variable is nil, no such courtesy message will be added."
143   :group 'message-sending
144   :type '(radio (string :format "%t: %v\n" :size 0) (const nil)))
145
146 (defcustom message-ignored-bounced-headers
147   "^\\(Received\\|Return-Path\\|Delivered-To\\):"
148   "*Regexp that matches headers to be removed in resent bounced mail."
149   :group 'message-interface
150   :type 'regexp)
151
152 ;;;###autoload
153 (defcustom message-from-style 'default
154   "*Specifies how \"From\" headers look.
155
156 If nil, they contain just the return address like:
157         king@grassland.com
158 If `parens', they look like:
159         king@grassland.com (Elvis Parsley)
160 If `angles', they look like:
161         Elvis Parsley <king@grassland.com>
162
163 Otherwise, most addresses look like `angles', but they look like
164 `parens' if `angles' would need quoting and `parens' would not."
165   :type '(choice (const :tag "simple" nil)
166                  (const parens)
167                  (const angles)
168                  (const default))
169   :group 'message-headers)
170
171 (defcustom message-insert-canlock t
172   "Whether to insert a Cancel-Lock header in news postings."
173   :version "21.3"
174   :group 'message-headers
175   :type 'boolean)
176
177 (defcustom message-syntax-checks
178   (if message-insert-canlock '((sender . disabled)) nil)
179   ;; Guess this one shouldn't be easy to customize...
180   "*Controls what syntax checks should not be performed on outgoing posts.
181 To disable checking of long signatures, for instance, add
182  `(signature . disabled)' to this list.
183
184 Don't touch this variable unless you really know what you're doing.
185
186 Checks include `subject-cmsg', `multiple-headers', `sendsys',
187 `message-id', `from', `long-lines', `control-chars', `size',
188 `new-text', `quoting-style', `redirected-followup', `signature',
189 `approved', `sender', `empty', `empty-headers', `message-id', `from',
190 `subject', `shorten-followup-to', `existing-newsgroups',
191 `buffer-file-name', `unchanged', `newsgroups', `reply-to',
192 `continuation-headers', `long-header-lines', `invisible-text' and
193 `illegible-text'."
194   :group 'message-news
195   :type '(repeat sexp))                 ; Fixme: improve this
196
197 (defcustom message-required-headers '((optional . References)
198                                       From)
199   "*Headers to be generated or prompted for when sending a message.
200 Also see `message-required-news-headers' and
201 `message-required-mail-headers'."
202   :group 'message-news
203   :group 'message-headers
204   :link '(custom-manual "(message)Message Headers")
205   :type '(repeat sexp))
206
207 (defcustom message-draft-headers '(References From)
208   "*Headers to be generated when saving a draft message."
209   :group 'message-news
210   :group 'message-headers
211   :link '(custom-manual "(message)Message Headers")
212   :type '(repeat sexp))
213
214 (defcustom message-required-news-headers
215   '(From Newsgroups Subject Date Message-ID
216          (optional . Organization)
217          (optional . User-Agent))
218   "*Headers to be generated or prompted for when posting an article.
219 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
220 Message-ID.  Organization, Lines, In-Reply-To, Expires, and
221 User-Agent are optional.  If don't you want message to insert some
222 header, remove it from this list."
223   :group 'message-news
224   :group 'message-headers
225   :link '(custom-manual "(message)Message Headers")
226   :type '(repeat sexp))
227
228 (defcustom message-required-mail-headers
229   '(From Subject Date (optional . In-Reply-To) Message-ID
230          (optional . User-Agent))
231   "*Headers to be generated or prompted for when mailing a message.
232 It is recommended that From, Date, To, Subject and Message-ID be
233 included.  Organization and User-Agent are optional."
234   :group 'message-mail
235   :group 'message-headers
236   :link '(custom-manual "(message)Message Headers")
237   :type '(repeat sexp))
238
239 (defcustom message-deletable-headers '(Message-ID Date Lines)
240   "Headers to be deleted if they already exist and were generated by message previously."
241   :group 'message-headers
242   :link '(custom-manual "(message)Message Headers")
243   :type 'sexp)
244
245 (defcustom message-ignored-news-headers
246   "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
247   "*Regexp of headers to be removed unconditionally before posting."
248   :group 'message-news
249   :group 'message-headers
250   :link '(custom-manual "(message)Message Headers")
251   :type 'regexp)
252
253 (defcustom message-ignored-mail-headers
254   "^[GF]cc:\\|^Resent-Fcc:\\|^Xref:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:"
255   "*Regexp of headers to be removed unconditionally before mailing."
256   :group 'message-mail
257   :group 'message-headers
258   :link '(custom-manual "(message)Mail Headers")
259   :type 'regexp)
260
261 (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:\\|^X-Payment:\\|^Approved:"
262   "*Header lines matching this regexp will be deleted before posting.
263 It's best to delete old Path and Date headers before posting to avoid
264 any confusion."
265   :group 'message-interface
266   :link '(custom-manual "(message)Superseding")
267   :type 'regexp)
268
269 (defcustom message-subject-re-regexp
270   "^[ \t]*\\([Rr][Ee]\\(\\[[0-9]*\\]\\)*:[ \t]*\\)*[ \t]*"
271   "*Regexp matching \"Re: \" in the subject line."
272   :group 'message-various
273   :link '(custom-manual "(message)Message Headers")
274   :type 'regexp)
275
276 ;;; Start of variables adopted from `message-utils.el'.
277
278 (defcustom message-subject-trailing-was-query 'ask
279   "*What to do with trailing \"(was: <old subject>)\" in subject lines.
280 If nil, leave the subject unchanged.  If it is the symbol `ask', query
281 the user what do do.  In this case, the subject is matched against
282 `message-subject-trailing-was-ask-regexp'.  If
283 `message-subject-trailing-was-query' is t, always strip the trailing
284 old subject.  In this case, `message-subject-trailing-was-regexp' is
285 used."
286   :type '(choice (const :tag "never" nil)
287                  (const :tag "always strip" t)
288                  (const ask))
289   :link '(custom-manual "(message)Message Headers")
290   :group 'message-various)
291
292 (defcustom message-subject-trailing-was-ask-regexp
293   "[ \t]*\\([[(]+[Ww][Aa][Ss][ \t]*.*[\])]+\\)"
294   "*Regexp matching \"(was: <old subject>)\" in the subject line.
295
296 The function `message-strip-subject-trailing-was' uses this regexp if
297 `message-subject-trailing-was-query' is set to the symbol `ask'.  If
298 the variable is t instead of `ask', use
299 `message-subject-trailing-was-regexp' instead.
300
301 It is okay to create some false positives here, as the user is asked."
302   :group 'message-various
303   :link '(custom-manual "(message)Message Headers")
304   :type 'regexp)
305
306 (defcustom message-subject-trailing-was-regexp
307   "[ \t]*\\((*[Ww][Aa][Ss]:[ \t]*.*)\\)"
308   "*Regexp matching \"(was: <old subject>)\" in the subject line.
309
310 If `message-subject-trailing-was-query' is set to t, the subject is
311 matched against `message-subject-trailing-was-regexp' in
312 `message-strip-subject-trailing-was'.  You should use a regexp creating very
313 few false positives here."
314   :group 'message-various
315   :link '(custom-manual "(message)Message Headers")
316   :type 'regexp)
317
318 ;; Fixme: Why are all these things autoloaded?
319
320 ;;; marking inserted text
321
322 ;;;###autoload
323 (defcustom message-mark-insert-begin
324   "--8<---------------cut here---------------start------------->8---\n"
325   "How to mark the beginning of some inserted text."
326   :type 'string
327   :link '(custom-manual "(message)Insertion Variables")
328   :group 'message-various)
329
330 ;;;###autoload
331 (defcustom message-mark-insert-end
332   "--8<---------------cut here---------------end--------------->8---\n"
333   "How to mark the end of some inserted text."
334   :type 'string
335   :link '(custom-manual "(message)Insertion Variables")
336   :group 'message-various)
337
338 ;;;###autoload
339 (defcustom message-archive-header
340   "X-No-Archive: Yes\n"
341   "Header to insert when you don't want your article to be archived.
342 Archives \(such as groups.google.com\) respect this header."
343   :type 'string
344   :link '(custom-manual "(message)Header Commands")
345   :group 'message-various)
346
347 ;;;###autoload
348 (defcustom message-archive-note
349   "X-No-Archive: Yes - save http://groups.google.com/"
350   "Note to insert why you wouldn't want this posting archived.
351 If nil, don't insert any text in the body."
352   :type '(radio (string :format "%t: %v\n" :size 0)
353                 (const nil))
354   :link '(custom-manual "(message)Header Commands")
355   :group 'message-various)
356
357 ;;; Crossposts and Followups
358 ;; inspired by JoH-followup-to by Jochem Huhman <joh  at gmx.de>
359 ;; new suggestions by R. Weikusat <rw at another.de>
360
361 (defvar message-cross-post-old-target nil
362   "Old target for cross-posts or follow-ups.")
363 (make-variable-buffer-local 'message-cross-post-old-target)
364
365 ;;;###autoload
366 (defcustom message-cross-post-default t
367   "When non-nil `message-cross-post-followup-to' will perform a crosspost.
368 If nil, `message-cross-post-followup-to' will only do a followup.  Note that
369 you can explicitly override this setting by calling
370 `message-cross-post-followup-to' with a prefix."
371   :type 'boolean
372   :group 'message-various)
373
374 ;;;###autoload
375 (defcustom message-cross-post-note
376   "Crosspost & Followup-To: "
377   "Note to insert before signature to notify of cross-post and follow-up."
378   :type 'string
379   :group 'message-various)
380
381 ;;;###autoload
382 (defcustom message-followup-to-note
383   "Followup-To: "
384   "Note to insert before signature to notify of follow-up only."
385   :type 'string
386   :group 'message-various)
387
388 ;;;###autoload
389 (defcustom message-cross-post-note-function
390   'message-cross-post-insert-note
391   "Function to use to insert note about Crosspost or Followup-To.
392 The function will be called with four arguments.  The function should not only
393 insert a note, but also ensure old notes are deleted.  See the documentation
394 for `message-cross-post-insert-note'."
395   :type 'function
396   :group 'message-various)
397
398 ;;; End of variables adopted from `message-utils.el'.
399
400 ;;;###autoload
401 (defcustom message-signature-separator "^-- *$"
402   "Regexp matching the signature separator."
403   :type 'regexp
404   :link '(custom-manual "(message)Various Message Variables")
405   :group 'message-various)
406
407 (defcustom message-elide-ellipsis "\n[...]\n\n"
408   "*The string which is inserted for elided text."
409   :type 'string
410   :link '(custom-manual "(message)Various Commands")
411   :group 'message-various)
412
413 (defcustom message-interactive t
414   "Non-nil means when sending a message wait for and display errors.
415 nil means let mailer mail back a message to report errors."
416   :group 'message-sending
417   :group 'message-mail
418   :link '(custom-manual "(message)Sending Variables")
419   :type 'boolean)
420
421 (defcustom message-generate-new-buffers 'unique
422   "*Non-nil means create a new message buffer whenever `message-setup' is called.
423 If this is a function, call that function with three parameters:  The type,
424 the to address and the group name.  (Any of these may be nil.)  The function
425 should return the new buffer name."
426   :group 'message-buffers
427   :link '(custom-manual "(message)Message Buffers")
428   :type '(choice (const :tag "off" nil)
429                  (const :tag "unique" unique)
430                  (const :tag "unsent" unsent)
431                  (function fun)))
432
433 (defcustom message-kill-buffer-on-exit nil
434   "*Non-nil means that the message buffer will be killed after sending a message."
435   :group 'message-buffers
436   :link '(custom-manual "(message)Message Buffers")
437   :type 'boolean)
438
439 (eval-when-compile
440   (defvar gnus-local-organization))
441 (defcustom message-user-organization
442   (or (and (boundp 'gnus-local-organization)
443            (stringp gnus-local-organization)
444            gnus-local-organization)
445       (getenv "ORGANIZATION")
446       t)
447   "*String to be used as an Organization header.
448 If t, use `message-user-organization-file'."
449   :group 'message-headers
450   :type '(choice string
451                  (const :tag "consult file" t)))
452
453 ;;;###autoload
454 (defcustom message-user-organization-file "/usr/lib/news/organization"
455   "*Local news organization file."
456   :type 'file
457   :link '(custom-manual "(message)News Headers")
458   :group 'message-headers)
459
460 (defcustom message-make-forward-subject-function
461   #'message-forward-subject-name-subject
462   "*List of functions called to generate subject headers for forwarded messages.
463 The subject generated by the previous function is passed into each
464 successive function.
465
466 The provided functions are:
467
468 * `message-forward-subject-author-subject' Source of article (author or
469       newsgroup), in brackets followed by the subject
470 * `message-forward-subject-name-subject' Source of article (name of author
471       or newsgroup), in brackets followed by the subject
472 * `message-forward-subject-fwd' Subject of article with 'Fwd:' prepended
473       to it."
474   :group 'message-forwarding
475   :link '(custom-manual "(message)Forwarding")
476   :type '(radio (function-item message-forward-subject-author-subject)
477                 (function-item message-forward-subject-fwd)
478                 (function-item message-forward-subject-name-subject)
479                 (repeat :tag "List of functions" function)))
480
481 (defcustom message-forward-as-mime t
482   "*Non-nil means forward messages as an inline/rfc822 MIME section.
483 Otherwise, directly inline the old message in the forwarded message."
484   :version "21.1"
485   :group 'message-forwarding
486   :link '(custom-manual "(message)Forwarding")
487   :type 'boolean)
488
489 (defcustom message-forward-show-mml 'best
490   "*Non-nil means show forwarded messages as MML (decoded from MIME).
491 Otherwise, forwarded messages are unchanged.
492 Can also be the symbol `best' to indicate that MML should be
493 used, except when it is a bad idea to use MML.  One example where
494 it is a bad idea is when forwarding a signed or encrypted
495 message, because converting MIME to MML would invalidate the
496 digital signature."
497   :version "21.1"
498   :group 'message-forwarding
499   :type '(choice (const :tag "use MML" t)
500                  (const :tag "don't use MML " nil)
501                  (const :tag "use MML when appropriate" best)))
502
503 (defcustom message-forward-before-signature t
504   "*Non-nil means put forwarded message before signature, else after."
505   :group 'message-forwarding
506   :type 'boolean)
507
508 (defcustom message-wash-forwarded-subjects nil
509   "*Non-nil means try to remove as much cruft as possible from the subject.
510 Done before generating the new subject of a forward."
511   :group 'message-forwarding
512   :link '(custom-manual "(message)Forwarding")
513   :type 'boolean)
514
515 (defcustom message-ignored-resent-headers "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:\\|^>?From "
516   "*All headers that match this regexp will be deleted when resending a message."
517   :group 'message-interface
518   :link '(custom-manual "(message)Resending")
519   :type 'regexp)
520
521 (defcustom message-forward-ignored-headers "^Content-Transfer-Encoding:\\|^X-Gnus"
522   "*All headers that match this regexp will be deleted when forwarding a message."
523   :version "21.1"
524   :group 'message-forwarding
525   :type '(choice (const :tag "None" nil)
526                  regexp))
527
528 (defcustom message-ignored-cited-headers "."
529   "*Delete these headers from the messages you yank."
530   :group 'message-insertion
531   :link '(custom-manual "(message)Insertion Variables")
532   :type 'regexp)
533
534 (defcustom message-cite-prefix-regexp
535   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
536       "\\([ \t]*[-_.[:word:]]+>+\\|[ \t]*[]>|}+]\\)+"
537     ;; ?-, ?_ or ?. MUST NOT be in syntax entry w.
538     (let (non-word-constituents)
539       (with-syntax-table text-mode-syntax-table
540         (setq non-word-constituents
541               (concat
542                (if (string-match "\\w" "-")  "" "-")
543                (if (string-match "\\w" "_")  "" "_")
544                (if (string-match "\\w" ".")  "" "."))))
545       (if (equal non-word-constituents "")
546           "\\([ \t]*\\(\\w\\)+>+\\|[ \t]*[]>|}+]\\)+"
547         (concat "\\([ \t]*\\(\\w\\|["
548                 non-word-constituents
549                 "]\\)+>+\\|[ \t]*[]>|}+]\\)+"))))
550   "*Regexp matching the longest possible citation prefix on a line."
551   :group 'message-insertion
552   :link '(custom-manual "(message)Insertion Variables")
553   :type 'regexp)
554
555 (defcustom message-cancel-message "I am canceling my own article.\n"
556   "Message to be inserted in the cancel message."
557   :group 'message-interface
558   :link '(custom-manual "(message)Canceling News")
559   :type 'string)
560
561 ;; Useful to set in site-init.el
562 ;;;###autoload
563 (defcustom message-send-mail-function 'message-send-mail-with-sendmail
564   "Function to call to send the current buffer as mail.
565 The headers should be delimited by a line whose contents match the
566 variable `mail-header-separator'.
567
568 Valid values include `message-send-mail-with-sendmail' (the default),
569 `message-send-mail-with-mh', `message-send-mail-with-qmail',
570 `message-smtpmail-send-it', `smtpmail-send-it' and `feedmail-send-it'.
571
572 See also `send-mail-function'."
573   :type '(radio (function-item message-send-mail-with-sendmail)
574                 (function-item message-send-mail-with-mh)
575                 (function-item message-send-mail-with-qmail)
576                 (function-item message-smtpmail-send-it)
577                 (function-item smtpmail-send-it)
578                 (function-item feedmail-send-it)
579                 (function :tag "Other"))
580   :group 'message-sending
581   :link '(custom-manual "(message)Mail Variables")
582   :group 'message-mail)
583
584 (defcustom message-send-news-function 'message-send-news
585   "Function to call to send the current buffer as news.
586 The headers should be delimited by a line whose contents match the
587 variable `mail-header-separator'."
588   :group 'message-sending
589   :group 'message-news
590   :link '(custom-manual "(message)News Variables")
591   :type 'function)
592
593 (defcustom message-reply-to-function nil
594   "If non-nil, function that should return a list of headers.
595 This function should pick out addresses from the To, Cc, and From headers
596 and respond with new To and Cc headers."
597   :group 'message-interface
598   :link '(custom-manual "(message)Reply")
599   :type '(choice function (const nil)))
600
601 (defcustom message-wide-reply-to-function nil
602   "If non-nil, function that should return a list of headers.
603 This function should pick out addresses from the To, Cc, and From headers
604 and respond with new To and Cc headers."
605   :group 'message-interface
606   :link '(custom-manual "(message)Wide Reply")
607   :type '(choice function (const nil)))
608
609 (defcustom message-followup-to-function nil
610   "If non-nil, function that should return a list of headers.
611 This function should pick out addresses from the To, Cc, and From headers
612 and respond with new To and Cc headers."
613   :group 'message-interface
614   :link '(custom-manual "(message)Followup")
615   :type '(choice function (const nil)))
616
617 (defcustom message-use-followup-to 'ask
618   "*Specifies what to do with Followup-To header.
619 If nil, always ignore the header.  If it is t, use its value, but
620 query before using the \"poster\" value.  If it is the symbol `ask',
621 always query the user whether to use the value.  If it is the symbol
622 `use', always use the value."
623   :group 'message-interface
624   :link '(custom-manual "(message)Followup")
625   :type '(choice (const :tag "ignore" nil)
626                  (const :tag "use & query" t)
627                  (const use)
628                  (const ask)))
629
630 (defcustom message-use-mail-followup-to 'use
631   "*Specifies what to do with Mail-Followup-To header.
632 If nil, always ignore the header.  If it is the symbol `ask', always
633 query the user whether to use the value.  If it is the symbol `use',
634 always use the value."
635   :group 'message-interface
636   :link '(custom-manual "(message)Mailing Lists")
637   :type '(choice (const :tag "ignore" nil)
638                  (const use)
639                  (const ask)))
640
641 (defcustom message-subscribed-address-functions nil
642   "*Specifies functions for determining list subscription.
643 If nil, do not attempt to determine list subscription with functions.
644 If non-nil, this variable contains a list of functions which return
645 regular expressions to match lists.  These functions can be used in
646 conjunction with `message-subscribed-regexps' and
647 `message-subscribed-addresses'."
648   :group 'message-interface
649   :link '(custom-manual "(message)Mailing Lists")
650   :type '(repeat sexp))
651
652 (defcustom message-subscribed-address-file nil
653   "*A file containing addresses the user is subscribed to.
654 If nil, do not look at any files to determine list subscriptions.  If
655 non-nil, each line of this file should be a mailing list address."
656   :group 'message-interface
657   :link '(custom-manual "(message)Mailing Lists")
658   :type '(radio (file :format "%t: %v\n" :size 0)
659                 (const nil)))
660
661 (defcustom message-subscribed-addresses nil
662   "*Specifies a list of addresses the user is subscribed to.
663 If nil, do not use any predefined list subscriptions.  This list of
664 addresses can be used in conjunction with
665 `message-subscribed-address-functions' and `message-subscribed-regexps'."
666   :group 'message-interface
667   :link '(custom-manual "(message)Mailing Lists")
668   :type '(repeat string))
669
670 (defcustom message-subscribed-regexps nil
671   "*Specifies a list of addresses the user is subscribed to.
672 If nil, do not use any predefined list subscriptions.  This list of
673 regular expressions can be used in conjunction with
674 `message-subscribed-address-functions' and `message-subscribed-addresses'."
675   :group 'message-interface
676   :link '(custom-manual "(message)Mailing Lists")
677   :type '(repeat regexp))
678
679 (defcustom message-allow-no-recipients 'ask
680   "Specifies what to do when there are no recipients other than Gcc/Fcc.
681 If it is the symbol `always', the posting is allowed.  If it is the
682 symbol `never', the posting is not allowed.  If it is the symbol
683 `ask', you are prompted."
684   :group 'message-interface
685   :link '(custom-manual "(message)Message Headers")
686   :type '(choice (const always)
687                  (const never)
688                  (const ask)))
689
690 (defcustom message-sendmail-f-is-evil nil
691   "*Non-nil means don't add \"-f username\" to the sendmail command line.
692 Doing so would be even more evil than leaving it out."
693   :group 'message-sending
694   :link '(custom-manual "(message)Mail Variables")
695   :type 'boolean)
696
697 (defcustom message-sendmail-envelope-from nil
698   "*Envelope-from when sending mail with sendmail.
699 If this is nil, use `user-mail-address'.  If it is the symbol
700 `header', use the From: header of the message."
701   :type '(choice (string :tag "From name")
702                  (const :tag "Use From: header from message" header)
703                  (const :tag "Use `user-mail-address'" nil))
704   :link '(custom-manual "(message)Mail Variables")
705   :group 'message-sending)
706
707 ;; qmail-related stuff
708 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
709   "Location of the qmail-inject program."
710   :group 'message-sending
711   :link '(custom-manual "(message)Mail Variables")
712   :type 'file)
713
714 (defcustom message-qmail-inject-args nil
715   "Arguments passed to qmail-inject programs.
716 This should be a list of strings, one string for each argument.  It
717 may also be a function.
718
719 For e.g., if you wish to set the envelope sender address so that bounces
720 go to the right place or to deal with listserv's usage of that address, you
721 might set this variable to '(\"-f\" \"you@some.where\")."
722   :group 'message-sending
723   :link '(custom-manual "(message)Mail Variables")
724   :type '(choice (function)
725                  (repeat string)))
726
727 (eval-when-compile
728   (defvar gnus-post-method)
729   (defvar gnus-select-method))
730 (defcustom message-post-method
731   (cond ((and (boundp 'gnus-post-method)
732               (listp gnus-post-method)
733               gnus-post-method)
734          gnus-post-method)
735         ((boundp 'gnus-select-method)
736          gnus-select-method)
737         (t '(nnspool "")))
738   "*Method used to post news.
739 Note that when posting from inside Gnus, for instance, this
740 variable isn't used."
741   :group 'message-news
742   :group 'message-sending
743   ;; This should be the `gnus-select-method' widget, but that might
744   ;; create a dependence to `gnus.el'.
745   :type 'sexp)
746
747 ;; FIXME: This should be a temporary workaround until someone implements a
748 ;; proper solution.  If a crash happens while replying, the auto-save file
749 ;; will *not* have a `References:' header if `message-generate-headers-first'
750 ;; is nil.  See: http://article.gmane.org/gmane.emacs.gnus.general/51138
751 (defcustom message-generate-headers-first '(references)
752   "Which headers should be generated before starting to compose a message.
753 If t, generate all required headers.  This can also be a list of headers to
754 generate.  The variables `message-required-news-headers' and
755 `message-required-mail-headers' specify which headers to generate.
756
757 Note that the variable `message-deletable-headers' specifies headers which
758 are to be deleted and then re-generated before sending, so this variable
759 will not have a visible effect for those headers."
760   :group 'message-headers
761   :link '(custom-manual "(message)Message Headers")
762   :type '(choice (const :tag "None" nil)
763                  (const :tag "References" '(references))
764                  (const :tag "All" t)
765                  (repeat (sexp :tag "Header"))))
766
767 (defcustom message-setup-hook nil
768   "Normal hook, run each time a new outgoing message is initialized.
769 The function `message-setup' runs this hook."
770   :group 'message-various
771   :link '(custom-manual "(message)Various Message Variables")
772   :type 'hook)
773
774 (defcustom message-cancel-hook nil
775   "Hook run when cancelling articles."
776   :group 'message-various
777   :link '(custom-manual "(message)Various Message Variables")
778   :type 'hook)
779
780 (defcustom message-signature-setup-hook nil
781   "Normal hook, run each time a new outgoing message is initialized.
782 It is run after the headers have been inserted and before
783 the signature is inserted."
784   :group 'message-various
785   :link '(custom-manual "(message)Various Message Variables")
786   :type 'hook)
787
788 (defcustom message-mode-hook nil
789   "Hook run in message mode buffers."
790   :group 'message-various
791   :type 'hook)
792
793 (defcustom message-header-hook nil
794   "Hook run in a message mode buffer narrowed to the headers."
795   :group 'message-various
796   :type 'hook)
797
798 (defcustom message-header-setup-hook nil
799   "Hook called narrowed to the headers when setting up a message buffer."
800   :group 'message-various
801   :link '(custom-manual "(message)Various Message Variables")
802   :type 'hook)
803
804 (defcustom message-minibuffer-local-map
805   (let ((map (make-sparse-keymap 'message-minibuffer-local-map)))
806     (set-keymap-parent map minibuffer-local-map)
807     map)
808   "Keymap for `message-read-from-minibuffer'.")
809
810 ;;;###autoload
811 (defcustom message-citation-line-function 'message-insert-citation-line
812   "*Function called to insert the \"Whomever writes:\" line.
813
814 Note that Gnus provides a feature where the reader can click on
815 `writes:' to hide the cited text.  If you change this line too much,
816 people who read your message will have to change their Gnus
817 configuration.  See the variable `gnus-cite-attribution-suffix'."
818   :type 'function
819   :link '(custom-manual "(message)Insertion Variables")
820   :group 'message-insertion)
821
822 ;;;###autoload
823 (defcustom message-yank-prefix "> "
824   "*Prefix inserted on the lines of yanked messages.
825 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
826 See also `message-yank-cited-prefix'."
827   :type 'string
828   :link '(custom-manual "(message)Insertion Variables")
829   :group 'message-insertion)
830
831 (defcustom message-yank-cited-prefix ">"
832   "*Prefix inserted on cited or empty lines of yanked messages.
833 Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
834 See also `message-yank-prefix'."
835   :type 'string
836   :link '(custom-manual "(message)Insertion Variables")
837   :group 'message-insertion)
838
839 (defcustom message-indentation-spaces 3
840   "*Number of spaces to insert at the beginning of each cited line.
841 Used by `message-yank-original' via `message-yank-cite'."
842   :group 'message-insertion
843   :link '(custom-manual "(message)Insertion Variables")
844   :type 'integer)
845
846 ;;;###autoload
847 (defcustom message-cite-function 'message-cite-original
848   "*Function for citing an original message.
849 Predefined functions include `message-cite-original' and
850 `message-cite-original-without-signature'.
851 Note that `message-cite-original' uses `mail-citation-hook' if that is non-nil."
852   :type '(radio (function-item message-cite-original)
853                 (function-item message-cite-original-without-signature)
854                 (function-item sc-cite-original)
855                 (function :tag "Other"))
856   :link '(custom-manual "(message)Insertion Variables")
857   :group 'message-insertion)
858
859 ;;;###autoload
860 (defcustom message-indent-citation-function 'message-indent-citation
861   "*Function for modifying a citation just inserted in the mail buffer.
862 This can also be a list of functions.  Each function can find the
863 citation between (point) and (mark t).  And each function should leave
864 point and mark around the citation text as modified."
865   :type 'function
866   :link '(custom-manual "(message)Insertion Variables")
867   :group 'message-insertion)
868
869 ;;;###autoload
870 (defcustom message-signature t
871   "*String to be inserted at the end of the message buffer.
872 If t, the `message-signature-file' file will be inserted instead.
873 If a function, the result from the function will be used instead.
874 If a form, the result from the form will be used instead."
875   :type 'sexp
876   :link '(custom-manual "(message)Insertion Variables")
877   :group 'message-insertion)
878
879 ;;;###autoload
880 (defcustom message-signature-file "~/.signature"
881   "*Name of file containing the text inserted at end of message buffer.
882 Ignored if the named file doesn't exist.
883 If nil, don't insert a signature."
884   :type '(choice file (const :tags "None" nil))
885   :link '(custom-manual "(message)Insertion Variables")
886   :group 'message-insertion)
887
888 ;;;###autoload
889 (defcustom message-signature-insert-empty-line t
890   "*If non-nil, insert an empty line before the signature separator."
891   :type 'boolean
892   :link '(custom-manual "(message)Insertion Variables")
893   :group 'message-insertion)
894
895 (defcustom message-distribution-function nil
896   "*Function called to return a Distribution header."
897   :group 'message-news
898   :group 'message-headers
899   :link '(custom-manual "(message)News Headers")
900   :type '(choice function (const nil)))
901
902 (defcustom message-expires 14
903   "Number of days before your article expires."
904   :group 'message-news
905   :group 'message-headers
906   :link '(custom-manual "(message)News Headers")
907   :type 'integer)
908
909 (defcustom message-user-path nil
910   "If nil, use the NNTP server name in the Path header.
911 If stringp, use this; if non-nil, use no host name (user name only)."
912   :group 'message-news
913   :group 'message-headers
914   :link '(custom-manual "(message)News Headers")
915   :type '(choice (const :tag "nntp" nil)
916                  (string :tag "name")
917                  (sexp :tag "none" :format "%t" t)))
918
919 (defvar message-reply-buffer nil)
920 (defvar message-reply-headers nil
921   "The headers of the current replied article.
922 It is a vector of the following headers:
923 \[number subject from date id references chars lines xref extra].")
924 (defvar message-newsreader nil)
925 (defvar message-mailer nil)
926 (defvar message-sent-message-via nil)
927 (defvar message-checksum nil)
928 (defvar message-send-actions nil
929   "A list of actions to be performed upon successful sending of a message.")
930 (defvar message-exit-actions nil
931   "A list of actions to be performed upon exiting after sending a message.")
932 (defvar message-kill-actions nil
933   "A list of actions to be performed before killing a message buffer.")
934 (defvar message-postpone-actions nil
935   "A list of actions to be performed after postponing a message.")
936
937 (define-widget 'message-header-lines 'text
938   "All header lines must be LFD terminated."
939   :format "%{%t%}:%n%v"
940   :valid-regexp "^\\'"
941   :error "All header lines must be newline terminated")
942
943 (defcustom message-default-headers ""
944   "*A string containing header lines to be inserted in outgoing messages.
945 It is inserted before you edit the message, so you can edit or delete
946 these lines."
947   :group 'message-headers
948   :link '(custom-manual "(message)Message Headers")
949   :type 'message-header-lines)
950
951 (defcustom message-default-mail-headers ""
952   "*A string of header lines to be inserted in outgoing mails."
953   :group 'message-headers
954   :group 'message-mail
955   :link '(custom-manual "(message)Mail Headers")
956   :type 'message-header-lines)
957
958 (defcustom message-default-news-headers ""
959   "*A string of header lines to be inserted in outgoing news articles."
960   :group 'message-headers
961   :group 'message-news
962   :link '(custom-manual "(message)News Headers")
963   :type 'message-header-lines)
964
965 ;; Note: could use /usr/ucb/mail instead of sendmail;
966 ;; options -t, and -v if not interactive.
967 (defcustom message-mailer-swallows-blank-line
968   (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
969                          system-configuration)
970            (file-readable-p "/etc/sendmail.cf")
971            (let ((buffer (get-buffer-create " *temp*")))
972              (unwind-protect
973                  (save-excursion
974                    (set-buffer buffer)
975                    (insert-file-contents "/etc/sendmail.cf")
976                    (goto-char (point-min))
977                    (let ((case-fold-search nil))
978                      (re-search-forward "^OR\\>" nil t)))
979                (kill-buffer buffer))))
980       ;; According to RFC822, "The field-name must be composed of printable
981       ;; ASCII characters (i. e., characters that have decimal values between
982       ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
983       ;; space, or colon.
984       '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
985   "*Set this non-nil if the system's mailer runs the header and body together.
986 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
987 The value should be an expression to test whether the problem will
988 actually occur."
989   :group 'message-sending
990   :link '(custom-manual "(message)Mail Variables")
991   :type 'sexp)
992
993 ;;;###autoload
994 (define-mail-user-agent 'message-user-agent
995   'message-mail 'message-send-and-exit
996   'message-kill-buffer 'message-send-hook)
997
998 (defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
999   "If non-nil, delete the deletable headers before feeding to mh.")
1000
1001 (defvar message-send-method-alist
1002   '((news message-news-p message-send-via-news)
1003     (mail message-mail-p message-send-via-mail))
1004   "Alist of ways to send outgoing messages.
1005 Each element has the form
1006
1007   \(TYPE PREDICATE FUNCTION)
1008
1009 where TYPE is a symbol that names the method; PREDICATE is a function
1010 called without any parameters to determine whether the message is
1011 a message of type TYPE; and FUNCTION is a function to be called if
1012 PREDICATE returns non-nil.  FUNCTION is called with one parameter --
1013 the prefix.")
1014
1015 (defcustom message-mail-alias-type 'abbrev
1016   "*What alias expansion type to use in Message buffers.
1017 The default is `abbrev', which uses mailabbrev.  nil switches
1018 mail aliases off."
1019   :group 'message
1020   :link '(custom-manual "(message)Mail Aliases")
1021   :type '(choice (const :tag "Use Mailabbrev" abbrev)
1022                  (const :tag "No expansion" nil)))
1023
1024 (defcustom message-auto-save-directory
1025   (file-name-as-directory (nnheader-concat message-directory "drafts"))
1026   "*Directory where Message auto-saves buffers if Gnus isn't running.
1027 If nil, Message won't auto-save."
1028   :group 'message-buffers
1029   :link '(custom-manual "(message)Various Message Variables")
1030   :type '(choice directory (const :tag "Don't auto-save" nil)))
1031
1032 (defcustom message-default-charset
1033   (and (not (mm-multibyte-p)) 'iso-8859-1)
1034   "Default charset used in non-MULE Emacsen.
1035 If nil, you might be asked to input the charset."
1036   :version "21.1"
1037   :group 'message
1038   :link '(custom-manual "(message)Various Message Variables")
1039   :type 'symbol)
1040
1041 (defcustom message-dont-reply-to-names
1042   (and (boundp 'rmail-dont-reply-to-names) rmail-dont-reply-to-names)
1043   "*A regexp specifying addresses to prune when doing wide replies.
1044 A value of nil means exclude your own user name only."
1045   :version "21.1"
1046   :group 'message
1047   :link '(custom-manual "(message)Wide Reply")
1048   :type '(choice (const :tag "Yourself" nil)
1049                  regexp))
1050
1051 (defvar message-shoot-gnksa-feet nil
1052   "*A list of GNKSA feet you are allowed to shoot.
1053 Gnus gives you all the opportunity you could possibly want for
1054 shooting yourself in the foot.  Also, Gnus allows you to shoot the
1055 feet of Good Net-Keeping Seal of Approval.  The following are foot
1056 candidates:
1057 `empty-article'     Allow you to post an empty article;
1058 `quoted-text-only'  Allow you to post quoted text only;
1059 `multiple-copies'   Allow you to post multiple copies;
1060 `cancel-messages'   Allow you to cancel or supersede messages from
1061                     your other email addresses.")
1062
1063 (defsubst message-gnksa-enable-p (feature)
1064   (or (not (listp message-shoot-gnksa-feet))
1065       (memq feature message-shoot-gnksa-feet)))
1066
1067 (defcustom message-hidden-headers nil
1068   "Regexp of headers to be hidden when composing new messages.
1069 This can also be a list of regexps to match headers.  Or a list
1070 starting with `not' and followed by regexps."
1071   :group 'message
1072   :link '(custom-manual "(message)Message Headers")
1073   :type '(repeat regexp))
1074
1075 (defcustom message-cite-articles-with-x-no-archive t
1076   "If non-nil, cite text from articles that has X-No-Archive set."
1077   :group 'message
1078   :type 'boolean)
1079
1080 ;;; Internal variables.
1081 ;;; Well, not really internal.
1082
1083 (defvar message-mode-syntax-table
1084   (let ((table (copy-syntax-table text-mode-syntax-table)))
1085     (modify-syntax-entry ?% ". " table)
1086     (modify-syntax-entry ?> ". " table)
1087     (modify-syntax-entry ?< ". " table)
1088     table)
1089   "Syntax table used while in Message mode.")
1090
1091 (defface message-header-to-face
1092   '((((class color)
1093       (background dark))
1094      (:foreground "green2" :bold t))
1095     (((class color)
1096       (background light))
1097      (:foreground "MidnightBlue" :bold t))
1098     (t
1099      (:bold t :italic t)))
1100   "Face used for displaying From headers."
1101   :group 'message-faces)
1102
1103 (defface message-header-cc-face
1104   '((((class color)
1105       (background dark))
1106      (:foreground "green4" :bold t))
1107     (((class color)
1108       (background light))
1109      (:foreground "MidnightBlue"))
1110     (t
1111      (:bold t)))
1112   "Face used for displaying Cc headers."
1113   :group 'message-faces)
1114
1115 (defface message-header-subject-face
1116   '((((class color)
1117       (background dark))
1118      (:foreground "green3"))
1119     (((class color)
1120       (background light))
1121      (:foreground "navy blue" :bold t))
1122     (t
1123      (:bold t)))
1124   "Face used for displaying subject headers."
1125   :group 'message-faces)
1126
1127 (defface message-header-newsgroups-face
1128   '((((class color)
1129       (background dark))
1130      (:foreground "yellow" :bold t :italic t))
1131     (((class color)
1132       (background light))
1133      (:foreground "blue4" :bold t :italic t))
1134     (t
1135      (:bold t :italic t)))
1136   "Face used for displaying newsgroups headers."
1137   :group 'message-faces)
1138
1139 (defface message-header-other-face
1140   '((((class color)
1141       (background dark))
1142      (:foreground "#b00000"))
1143     (((class color)
1144       (background light))
1145      (:foreground "steel blue"))
1146     (t
1147      (:bold t :italic t)))
1148   "Face used for displaying newsgroups headers."
1149   :group 'message-faces)
1150
1151 (defface message-header-name-face
1152   '((((class color)
1153       (background dark))
1154      (:foreground "DarkGreen"))
1155     (((class color)
1156       (background light))
1157      (:foreground "cornflower blue"))
1158     (t
1159      (:bold t)))
1160   "Face used for displaying header names."
1161   :group 'message-faces)
1162
1163 (defface message-header-xheader-face
1164   '((((class color)
1165       (background dark))
1166      (:foreground "blue"))
1167     (((class color)
1168       (background light))
1169      (:foreground "blue"))
1170     (t
1171      (:bold t)))
1172   "Face used for displaying X-Header headers."
1173   :group 'message-faces)
1174
1175 (defface message-separator-face
1176   '((((class color)
1177       (background dark))
1178      (:foreground "blue3"))
1179     (((class color)
1180       (background light))
1181      (:foreground "brown"))
1182     (t
1183      (:bold t)))
1184   "Face used for displaying the separator."
1185   :group 'message-faces)
1186
1187 (defface message-cited-text-face
1188   '((((class color)
1189       (background dark))
1190      (:foreground "red"))
1191     (((class color)
1192       (background light))
1193      (:foreground "red"))
1194     (t
1195      (:bold t)))
1196   "Face used for displaying cited text names."
1197   :group 'message-faces)
1198
1199 (defface message-mml-face
1200   '((((class color)
1201       (background dark))
1202      (:foreground "ForestGreen"))
1203     (((class color)
1204       (background light))
1205      (:foreground "ForestGreen"))
1206     (t
1207      (:bold t)))
1208   "Face used for displaying MML."
1209   :group 'message-faces)
1210
1211 (defun message-font-lock-make-header-matcher (regexp)
1212   (let ((form
1213          `(lambda (limit)
1214             (let ((start (point)))
1215               (save-restriction
1216                 (widen)
1217                 (goto-char (point-min))
1218                 (if (re-search-forward
1219                      (concat "^" (regexp-quote mail-header-separator) "$")
1220                      nil t)
1221                     (setq limit (min limit (match-beginning 0))))
1222                 (goto-char start))
1223               (and (< start limit)
1224                    (re-search-forward ,regexp limit t))))))
1225     (if (featurep 'bytecomp)
1226         (byte-compile form)
1227       form)))
1228
1229 (defvar message-font-lock-keywords
1230   (let ((content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)\n?"))
1231     `((,(message-font-lock-make-header-matcher
1232          (concat "^\\([Tt]o:\\)" content))
1233        (1 'message-header-name-face)
1234        (2 'message-header-to-face nil t))
1235       (,(message-font-lock-make-header-matcher
1236          (concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content))
1237        (1 'message-header-name-face)
1238        (2 'message-header-cc-face nil t))
1239       (,(message-font-lock-make-header-matcher
1240          (concat "^\\([Ss]ubject:\\)" content))
1241        (1 'message-header-name-face)
1242        (2 'message-header-subject-face nil t))
1243       (,(message-font-lock-make-header-matcher
1244          (concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content))
1245        (1 'message-header-name-face)
1246        (2 'message-header-newsgroups-face nil t))
1247       (,(message-font-lock-make-header-matcher
1248          (concat "^\\([A-Z][^: \n\t]+:\\)" content))
1249        (1 'message-header-name-face)
1250        (2 'message-header-other-face nil t))
1251       (,(message-font-lock-make-header-matcher
1252          (concat "^\\(X-[A-Za-z0-9-]+:\\|In-Reply-To:\\)" content))
1253        (1 'message-header-name-face)
1254        (2 'message-header-name-face))
1255       ,@(if (and mail-header-separator
1256                  (not (equal mail-header-separator "")))
1257             `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
1258                1 'message-separator-face))
1259           nil)
1260       ((lambda (limit)
1261          (re-search-forward (concat "^\\("
1262                                     message-cite-prefix-regexp
1263                                     "\\).*")
1264                             limit t))
1265        (0 'message-cited-text-face))
1266       ("<#/?\\(multipart\\|part\\|external\\|mml\\|secure\\)[^>]*>"
1267        (0 'message-mml-face))))
1268   "Additional expressions to highlight in Message mode.")
1269
1270
1271 ;; XEmacs does it like this.  For Emacs, we have to set the
1272 ;; `font-lock-defaults' buffer-local variable.
1273 (put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
1274
1275 (defvar message-face-alist
1276   '((bold . bold-region)
1277     (underline . underline-region)
1278     (default . (lambda (b e)
1279                  (unbold-region b e)
1280                  (ununderline-region b e))))
1281   "Alist of mail and news faces for facemenu.
1282 The cdr of each entry is a function for applying the face to a region.")
1283
1284 (defcustom message-send-hook nil
1285   "Hook run before sending messages.
1286 This hook is run quite early when sending."
1287   :group 'message-various
1288   :options '(ispell-message)
1289   :link '(custom-manual "(message)Various Message Variables")
1290   :type 'hook)
1291
1292 (defcustom message-send-mail-hook nil
1293   "Hook run before sending mail messages.
1294 This hook is run very late -- just before the message is sent as
1295 mail."
1296   :group 'message-various
1297   :link '(custom-manual "(message)Various Message Variables")
1298   :type 'hook)
1299
1300 (defcustom message-send-news-hook nil
1301   "Hook run before sending news messages.
1302 This hook is run very late -- just before the message is sent as
1303 news."
1304   :group 'message-various
1305   :link '(custom-manual "(message)Various Message Variables")
1306   :type 'hook)
1307
1308 (defcustom message-sent-hook nil
1309   "Hook run after sending messages."
1310   :group 'message-various
1311   :type 'hook)
1312
1313 (defvar message-send-coding-system 'binary
1314   "Coding system to encode outgoing mail.")
1315
1316 (defvar message-draft-coding-system
1317   mm-auto-save-coding-system
1318   "*Coding system to compose mail.
1319 If you'd like to make it possible to share draft files between XEmacs
1320 and Emacs, you may use `iso-2022-7bit' for this value at your own risk.
1321 Note that the coding-system `iso-2022-7bit' isn't suitable to all data.")
1322
1323 (defcustom message-send-mail-partially-limit 1000000
1324   "The limitation of messages sent as message/partial.
1325 The lower bound of message size in characters, beyond which the message
1326 should be sent in several parts.  If it is nil, the size is unlimited."
1327   :version "21.1"
1328   :group 'message-buffers
1329   :link '(custom-manual "(message)Mail Variables")
1330   :type '(choice (const :tag "unlimited" nil)
1331                  (integer 1000000)))
1332
1333 (defcustom message-alternative-emails nil
1334   "A regexp to match the alternative email addresses.
1335 The first matched address (not primary one) is used in the From field."
1336   :group 'message-headers
1337   :link '(custom-manual "(message)Message Headers")
1338   :type '(choice (const :tag "Always use primary" nil)
1339                  regexp))
1340
1341 (defcustom message-hierarchical-addresses nil
1342   "A list of hierarchical mail address definitions.
1343
1344 Inside each entry, the first address is the \"top\" address, and
1345 subsequent addresses are subaddresses; this is used to indicate that
1346 mail sent to the first address will automatically be delivered to the
1347 subaddresses.  So if the first address appears in the recipient list
1348 for a message, the subaddresses will be removed (if present) before
1349 the mail is sent.  All addresses in this structure should be
1350 downcased."
1351   :group 'message-headers
1352   :type '(repeat (repeat string)))
1353
1354 (defcustom message-mail-user-agent nil
1355   "Like `mail-user-agent'.
1356 Except if it is nil, use Gnus native MUA; if it is t, use
1357 `mail-user-agent'."
1358   :type '(radio (const :tag "Gnus native"
1359                        :format "%t\n"
1360                        nil)
1361                 (const :tag "`mail-user-agent'"
1362                        :format "%t\n"
1363                        t)
1364                 (function-item :tag "Default Emacs mail"
1365                                :format "%t\n"
1366                                sendmail-user-agent)
1367                 (function-item :tag "Emacs interface to MH"
1368                                :format "%t\n"
1369                                mh-e-user-agent)
1370                 (function :tag "Other"))
1371   :version "21.1"
1372   :group 'message)
1373
1374 (defcustom message-wide-reply-confirm-recipients nil
1375   "Whether to confirm a wide reply to multiple email recipients.
1376 If this variable is nil, don't ask whether to reply to all recipients.
1377 If this variable is non-nil, pose the question \"Reply to all
1378 recipients?\" before a wide reply to multiple recipients.  If the user
1379 answers yes, reply to all recipients as usual.  If the user answers
1380 no, only reply back to the author."
1381   :version "21.3"
1382   :group 'message-headers
1383   :link '(custom-manual "(message)Wide Reply")
1384   :type 'boolean)
1385
1386 (defcustom message-user-fqdn nil
1387   "*Domain part of Messsage-Ids."
1388   :group 'message-headers
1389   :link '(custom-manual "(message)News Headers")
1390   :type '(radio (const :format "%v  " nil)
1391                 (string :format "FQDN: %v\n" :size 0)))
1392
1393 (defcustom message-use-idna (and (condition-case nil (require 'idna)
1394                                    (file-error))
1395                                  (mm-coding-system-p 'utf-8)
1396                                  (executable-find idna-program)
1397                                  'ask)
1398   "Whether to encode non-ASCII in domain names into ASCII according to IDNA."
1399   :group 'message-headers
1400   :link '(custom-manual "(message)IDNA")
1401   :type '(choice (const :tag "Ask" ask)
1402                  (const :tag "Never" nil)
1403                  (const :tag "Always" t)))
1404
1405 ;;; Internal variables.
1406
1407 (defvar message-sending-message "Sending...")
1408 (defvar message-buffer-list nil)
1409 (defvar message-this-is-news nil)
1410 (defvar message-this-is-mail nil)
1411 (defvar message-draft-article nil)
1412 (defvar message-mime-part nil)
1413 (defvar message-posting-charset nil)
1414 (defvar message-inserted-headers nil)
1415
1416 ;; Byte-compiler warning
1417 (eval-when-compile
1418   (defvar gnus-active-hashtb)
1419   (defvar gnus-read-active-file))
1420
1421 ;;; Regexp matching the delimiter of messages in UNIX mail format
1422 ;;; (UNIX From lines), minus the initial ^.  It should be a copy
1423 ;;; of rmail.el's rmail-unix-mail-delimiter.
1424 (defvar message-unix-mail-delimiter
1425   (let ((time-zone-regexp
1426          (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
1427                  "\\|[-+]?[0-9][0-9][0-9][0-9]"
1428                  "\\|"
1429                  "\\) *")))
1430     (concat
1431      "From "
1432
1433      ;; Many things can happen to an RFC 822 mailbox before it is put into
1434      ;; a `From' line.  The leading phrase can be stripped, e.g.
1435      ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'.  The <> can be stripped, e.g.
1436      ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'.  Everything starting with a CRLF
1437      ;; can be removed, e.g.
1438      ;;         From: joe@y.z (Joe      K
1439      ;;                 User)
1440      ;; can yield `From joe@y.z (Joe    K Fri Mar 22 08:11:15 1996', and
1441      ;;         From: Joe User
1442      ;;                 <joe@y.z>
1443      ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
1444      ;; The mailbox can be removed or be replaced by white space, e.g.
1445      ;;         From: "Joe User"{space}{tab}
1446      ;;                 <joe@y.z>
1447      ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
1448      ;; where {space} and {tab} represent the Ascii space and tab characters.
1449      ;; We want to match the results of any of these manglings.
1450      ;; The following regexp rejects names whose first characters are
1451      ;; obviously bogus, but after that anything goes.
1452      "\\([^\0-\b\n-\r\^?].*\\)?"
1453
1454      ;; The time the message was sent.
1455      "\\([^\0-\r \^?]+\\) +"            ; day of the week
1456      "\\([^\0-\r \^?]+\\) +"            ; month
1457      "\\([0-3]?[0-9]\\) +"              ; day of month
1458      "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
1459
1460      ;; Perhaps a time zone, specified by an abbreviation, or by a
1461      ;; numeric offset.
1462      time-zone-regexp
1463
1464      ;; The year.
1465      " \\([0-9][0-9]+\\) *"
1466
1467      ;; On some systems the time zone can appear after the year, too.
1468      time-zone-regexp
1469
1470      ;; Old uucp cruft.
1471      "\\(remote from .*\\)?"
1472
1473      "\n"))
1474   "Regexp matching the delimiter of messages in UNIX mail format.")
1475
1476 (defvar message-unsent-separator
1477   (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1478           "^ *---+ +Returned message +---+ *$\\|"
1479           "^Start of returned message$\\|"
1480           "^ *---+ +Original message +---+ *$\\|"
1481           "^ *--+ +begin message +--+ *$\\|"
1482           "^ *---+ +Original message follows +---+ *$\\|"
1483           "^ *---+ +Undelivered message follows +---+ *$\\|"
1484           "^|? *---+ +Message text follows: +---+ *|?$")
1485   "A regexp that matches the separator before the text of a failed message.")
1486
1487 (defvar message-field-fillers
1488   '((To message-fill-field-address)
1489     (Cc message-fill-field-address)
1490     (From message-fill-field-address))
1491   "Alist of header names/filler functions.")
1492
1493 (defvar message-header-format-alist
1494   `((Newsgroups)
1495     (To)
1496     (Cc)
1497     (Subject)
1498     (In-Reply-To)
1499     (Fcc)
1500     (Bcc)
1501     (Date)
1502     (Organization)
1503     (Distribution)
1504     (Lines)
1505     (Expires)
1506     (Message-ID)
1507     (References . message-shorten-references)
1508     (User-Agent))
1509   "Alist used for formatting headers.")
1510
1511 (defvar message-options nil
1512   "Some saved answers when sending message.")
1513
1514 (defvar message-send-mail-real-function nil
1515   "Internal send mail function.")
1516
1517 (defvar message-bogus-system-names "^localhost\\."
1518   "The regexp of bogus system names.")
1519
1520 (defcustom message-valid-fqdn-regexp
1521   (concat "[a-z0-9][-.a-z0-9]+\\." ;; [hostname.subdomain.]domain.
1522           ;; valid TLDs:
1523           "\\([a-z][a-z]" ;; two letter country TDLs
1524           "\\|biz\\|com\\|edu\\|gov\\|int\\|mil\\|net\\|org"
1525           "\\|aero\\|coop\\|info\\|name\\|museum"
1526           "\\|arpa\\|pro\\|uucp\\|bitnet\\|bofh" ;; old style?
1527           "\\)")
1528   "Regular expression that matches a valid FQDN."
1529   ;; see also: gnus-button-valid-fqdn-regexp
1530   :group 'message-headers
1531   :type 'regexp)
1532
1533 (eval-and-compile
1534   (autoload 'gnus-alive-p "gnus-util")
1535   (autoload 'gnus-delay-article "gnus-delay")
1536   (autoload 'gnus-extract-address-components "gnus-util")
1537   (autoload 'gnus-find-method-for-group "gnus")
1538   (autoload 'gnus-group-decoded-name "gnus-group")
1539   (autoload 'gnus-group-name-charset "gnus-group")
1540   (autoload 'gnus-group-name-decode "gnus-group")
1541   (autoload 'gnus-groups-from-server "gnus")
1542   (autoload 'gnus-make-local-hook "gnus-util")
1543   (autoload 'gnus-open-server "gnus-int")
1544   (autoload 'gnus-output-to-mail "gnus-util")
1545   (autoload 'gnus-output-to-rmail "gnus-util")
1546   (autoload 'gnus-request-post "gnus-int")
1547   (autoload 'gnus-server-string "gnus")
1548   (autoload 'idna-to-ascii "idna")
1549   (autoload 'message-setup-toolbar "messagexmas")
1550   (autoload 'mh-new-draft-name "mh-comp")
1551   (autoload 'mh-send-letter "mh-comp")
1552   (autoload 'nndraft-request-associate-buffer "nndraft")
1553   (autoload 'nndraft-request-expire-articles "nndraft")
1554   (autoload 'nnvirtual-find-group-art "nnvirtual")
1555   (autoload 'rmail-dont-reply-to "mail-utils")
1556   (autoload 'rmail-msg-is-pruned "rmail")
1557   (autoload 'rmail-msg-restore-non-pruned-header "rmail")
1558   (autoload 'rmail-output "rmailout"))
1559
1560 (eval-when-compile
1561   (autoload 'sha1 "sha1-el"))
1562
1563 \f
1564
1565 ;;;
1566 ;;; Utility functions.
1567 ;;;
1568
1569 (defmacro message-y-or-n-p (question show &rest text)
1570   "Ask QUESTION, displaying remaining args in a temporary buffer if SHOW."
1571   `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1572
1573 (defmacro message-delete-line (&optional n)
1574   "Delete the current line (and the next N lines)."
1575   `(delete-region (progn (beginning-of-line) (point))
1576                   (progn (forward-line ,(or n 1)) (point))))
1577
1578 (defun message-mark-active-p ()
1579   "Non-nil means the mark and region are currently active in this buffer."
1580   mark-active)
1581
1582 (defun message-unquote-tokens (elems)
1583   "Remove double quotes (\") from strings in list ELEMS."
1584   (mapcar (lambda (item)
1585             (while (string-match "^\\(.*\\)\"\\(.*\\)$" item)
1586               (setq item (concat (match-string 1 item)
1587                                  (match-string 2 item))))
1588             item)
1589           elems))
1590
1591 (defun message-tokenize-header (header &optional separator)
1592   "Split HEADER into a list of header elements.
1593 SEPARATOR is a string of characters to be used as separators.  \",\"
1594 is used by default."
1595   (if (not header)
1596       nil
1597     (let ((regexp (format "[%s]+" (or separator ",")))
1598           (beg 1)
1599           (first t)
1600           quoted elems paren)
1601       (with-temp-buffer
1602         (mm-enable-multibyte)
1603         (insert header)
1604         (goto-char (point-min))
1605         (while (not (eobp))
1606           (if first
1607               (setq first nil)
1608             (forward-char 1))
1609           (cond ((and (> (point) beg)
1610                       (or (eobp)
1611                           (and (looking-at regexp)
1612                                (not quoted)
1613                                (not paren))))
1614                  (push (buffer-substring beg (point)) elems)
1615                  (setq beg (match-end 0)))
1616                 ((eq (char-after) ?\")
1617                  (setq quoted (not quoted)))
1618                 ((and (eq (char-after) ?\()
1619                       (not quoted))
1620                  (setq paren t))
1621                 ((and (eq (char-after) ?\))
1622                       (not quoted))
1623                  (setq paren nil))))
1624         (nreverse elems)))))
1625
1626 (defun message-mail-file-mbox-p (file)
1627   "Say whether FILE looks like a Unix mbox file."
1628   (when (and (file-exists-p file)
1629              (file-readable-p file)
1630              (file-regular-p file))
1631     (with-temp-buffer
1632       (nnheader-insert-file-contents file)
1633       (goto-char (point-min))
1634       (looking-at message-unix-mail-delimiter))))
1635
1636 (defun message-fetch-field (header &optional not-all)
1637   "The same as `mail-fetch-field', only remove all newlines.
1638 The buffer is expected to be narrowed to just the header of the message;
1639 see `message-narrow-to-headers-or-head'."
1640   (let* ((inhibit-point-motion-hooks t)
1641          (value (mail-fetch-field header nil (not not-all))))
1642     (when value
1643       (while (string-match "\n[\t ]+" value)
1644         (setq value (replace-match " " t t value)))
1645       value)))
1646
1647 (defun message-field-value (header &optional not-all)
1648   "The same as `message-fetch-field', only narrow to the headers first."
1649   (save-excursion
1650     (save-restriction
1651       (message-narrow-to-headers-or-head)
1652       (message-fetch-field header not-all))))
1653
1654 (defun message-narrow-to-field ()
1655   "Narrow the buffer to the header on the current line."
1656   (beginning-of-line)
1657   (while (looking-at "[ \t]")
1658     (forward-line -1))
1659   (narrow-to-region
1660    (point)
1661    (progn
1662      (forward-line 1)
1663      (if (re-search-forward "^[^ \n\t]" nil t)
1664          (point-at-bol)
1665        (point-max))))
1666   (goto-char (point-min)))
1667
1668 (defun message-add-header (&rest headers)
1669   "Add the HEADERS to the message header, skipping those already present."
1670   (while headers
1671     (let (hclean)
1672       (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
1673         (error "Invalid header `%s'" (car headers)))
1674       (setq hclean (match-string 1 (car headers)))
1675       (save-restriction
1676         (message-narrow-to-headers)
1677         (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
1678           (goto-char (point-max))
1679           (if (string-match "\n$" (car headers))
1680               (insert (car headers))
1681             (insert (car headers) ?\n)))))
1682     (setq headers (cdr headers))))
1683
1684 (defmacro message-with-reply-buffer (&rest forms)
1685   "Evaluate FORMS in the reply buffer, if it exists."
1686   `(when (and message-reply-buffer
1687               (buffer-name message-reply-buffer))
1688      (save-excursion
1689        (set-buffer message-reply-buffer)
1690        ,@forms)))
1691
1692 (put 'message-with-reply-buffer 'lisp-indent-function 0)
1693 (put 'message-with-reply-buffer 'edebug-form-spec '(body))
1694
1695 (defun message-fetch-reply-field (header)
1696   "Fetch field HEADER from the message we're replying to."
1697   (message-with-reply-buffer
1698     (save-restriction
1699       (mail-narrow-to-head)
1700       (message-fetch-field header))))
1701
1702 (defun message-strip-list-identifiers (subject)
1703   "Remove list identifiers in `gnus-list-identifiers' from string SUBJECT."
1704   (require 'gnus-sum)                   ; for gnus-list-identifiers
1705   (let ((regexp (if (stringp gnus-list-identifiers)
1706                     gnus-list-identifiers
1707                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1708     (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
1709                               " *\\)\\)+\\(Re: +\\)?\\)") subject)
1710         (concat (substring subject 0 (match-beginning 1))
1711                 (or (match-string 3 subject)
1712                     (match-string 5 subject))
1713                 (substring subject
1714                            (match-end 1)))
1715       subject)))
1716
1717 (defun message-strip-subject-re (subject)
1718   "Remove \"Re:\" from subject lines in string SUBJECT."
1719   (if (string-match message-subject-re-regexp subject)
1720       (substring subject (match-end 0))
1721     subject))
1722
1723 ;;; Start of functions adopted from `message-utils.el'.
1724
1725 (defun message-strip-subject-trailing-was (subject)
1726   "Remove trailing \"(Was: <old subject>)\" from SUBJECT lines.
1727 Leading \"Re: \" is not stripped by this function.  Use the function
1728 `message-strip-subject-re' for this."
1729   (let* ((query message-subject-trailing-was-query)
1730          (new) (found))
1731     (setq found
1732           (string-match
1733            (if (eq query 'ask)
1734                message-subject-trailing-was-ask-regexp
1735              message-subject-trailing-was-regexp)
1736            subject))
1737     (if found
1738         (setq new (substring subject 0 (match-beginning 0))))
1739     (if (or (not found) (eq query nil))
1740         subject
1741       (if (eq query 'ask)
1742           (if (message-y-or-n-p
1743                "Strip `(was: <old subject>)' in subject? " t
1744                (concat
1745                 "Strip `(was: <old subject>)' in subject "
1746                 "and use the new one instead?\n\n"
1747                 "Current subject is:   \""
1748                 subject "\"\n\n"
1749                 "New subject would be: \""
1750                 new "\"\n\n"
1751                 "See the variable `message-subject-trailing-was-query' "
1752                 "to get rid of this query."
1753                 ))
1754               new subject)
1755         new))))
1756
1757 ;;; Suggested by Jonas Steverud  @  www.dtek.chalmers.se/~d4jonas/
1758
1759 ;;;###autoload
1760 (defun message-change-subject (new-subject)
1761   "Ask for NEW-SUBJECT header, append (was: <Old Subject>)."
1762   ;; <URL:http://www.landfield.com/usefor/drafts/draft-ietf-usefor-useage--1.02.unpaged>
1763   (interactive
1764    (list
1765     (read-from-minibuffer "New subject: ")))
1766   (cond ((and (not (or (null new-subject) ; new subject not empty
1767                        (zerop (string-width new-subject))
1768                        (string-match "^[ \t]*$" new-subject))))
1769          (save-excursion
1770            (let ((old-subject
1771                   (save-restriction
1772                     (message-narrow-to-headers)
1773                     (message-fetch-field "Subject"))))
1774              (cond ((not old-subject)
1775                     (error "No current subject"))
1776                    ((not (string-match
1777                           (concat "^[ \t]*"
1778                                   (regexp-quote new-subject)
1779                                   " \t]*$")
1780                           old-subject))  ; yes, it really is a new subject
1781                     ;; delete eventual Re: prefix
1782                     (setq old-subject
1783                           (message-strip-subject-re old-subject))
1784                     (message-goto-subject)
1785                     (message-delete-line)
1786                     (insert (concat "Subject: "
1787                                     new-subject
1788                                     " (was: "
1789                                     old-subject ")\n")))))))))
1790
1791 ;;;###autoload
1792 (defun message-mark-inserted-region (beg end)
1793   "Mark some region in the current article with enclosing tags.
1794 See `message-mark-insert-begin' and `message-mark-insert-end'."
1795   (interactive "r")
1796   (save-excursion
1797     ;; add to the end of the region first, otherwise end would be invalid
1798     (goto-char end)
1799     (insert message-mark-insert-end)
1800     (goto-char beg)
1801     (insert message-mark-insert-begin)))
1802
1803 ;;;###autoload
1804 (defun message-mark-insert-file (file)
1805   "Insert FILE at point, marking it with enclosing tags.
1806 See `message-mark-insert-begin' and `message-mark-insert-end'."
1807   (interactive "fFile to insert: ")
1808     ;; reverse insertion to get correct result.
1809   (let ((p (point)))
1810     (insert message-mark-insert-end)
1811     (goto-char p)
1812     (insert-file-contents file)
1813     (goto-char p)
1814     (insert message-mark-insert-begin)))
1815
1816 ;;;###autoload
1817 (defun message-add-archive-header ()
1818   "Insert \"X-No-Archive: Yes\" in the header and a note in the body.
1819 The note can be customized using `message-archive-note'.  When called with a
1820 prefix argument, ask for a text to insert.  If you don't want the note in the
1821 body, set  `message-archive-note' to nil."
1822   (interactive)
1823   (if current-prefix-arg
1824       (setq message-archive-note
1825             (read-from-minibuffer "Reason for No-Archive: "
1826                                   (cons message-archive-note 0))))
1827     (save-excursion
1828       (if (message-goto-signature)
1829           (re-search-backward message-signature-separator))
1830       (when message-archive-note
1831         (insert message-archive-note)
1832         (newline))
1833       (message-add-header message-archive-header)
1834       (message-sort-headers)))
1835
1836 ;;;###autoload
1837 (defun message-cross-post-followup-to-header (target-group)
1838   "Mangles FollowUp-To and Newsgroups header to point to TARGET-GROUP.
1839 With prefix-argument just set Follow-Up, don't cross-post."
1840   (interactive
1841    (list ; Completion based on Gnus
1842     (completing-read "Followup To: "
1843                      (if (boundp 'gnus-newsrc-alist)
1844                          gnus-newsrc-alist)
1845                      nil nil '("poster" . 0)
1846                      (if (boundp 'gnus-group-history)
1847                          'gnus-group-history))))
1848   (message-remove-header "Follow[Uu]p-[Tt]o" t)
1849   (message-goto-newsgroups)
1850   (beginning-of-line)
1851   ;; if we already did a crosspost before, kill old target
1852   (if (and message-cross-post-old-target
1853            (re-search-forward
1854             (regexp-quote (concat "," message-cross-post-old-target))
1855             nil t))
1856       (replace-match ""))
1857   ;; unless (followup is to poster or user explicitly asked not
1858   ;; to cross-post, or target-group is already in Newsgroups)
1859   ;; add target-group to Newsgroups line.
1860   (cond ((and (or
1861                ;; def: cross-post, req:no
1862                (and message-cross-post-default (not current-prefix-arg))
1863                ;; def: no-cross-post, req:yes
1864                (and (not message-cross-post-default) current-prefix-arg))
1865               (not (string-match "poster" target-group))
1866               (not (string-match (regexp-quote target-group)
1867                                  (message-fetch-field "Newsgroups"))))
1868          (end-of-line)
1869          (insert (concat "," target-group))))
1870   (end-of-line) ; ensure Followup: comes after Newsgroups:
1871   ;; unless new followup would be identical to Newsgroups line
1872   ;; make a new Followup-To line
1873   (if (not (string-match (concat "^[ \t]*"
1874                                  target-group
1875                                  "[ \t]*$")
1876                          (message-fetch-field "Newsgroups")))
1877       (insert (concat "\nFollowup-To: " target-group)))
1878   (setq message-cross-post-old-target target-group))
1879
1880 ;;;###autoload
1881 (defun message-cross-post-insert-note (target-group cross-post in-old
1882                                                     old-groups)
1883   "Insert a in message body note about a set Followup or Crosspost.
1884 If there have been previous notes, delete them.  TARGET-GROUP specifies the
1885 group to Followup-To.  When CROSS-POST is t, insert note about
1886 crossposting.  IN-OLD specifies whether TARGET-GROUP is a member of
1887 OLD-GROUPS.  OLD-GROUPS lists the old-groups the posting would have
1888 been made to before the user asked for a Crosspost."
1889   ;; start scanning body for previous uses
1890   (message-goto-signature)
1891   (let ((head (re-search-backward
1892                (concat "^" mail-header-separator)
1893                nil t))) ; just search in body
1894     (message-goto-signature)
1895     (while (re-search-backward
1896             (concat "^" (regexp-quote message-cross-post-note) ".*")
1897             head t)
1898       (message-delete-line))
1899     (message-goto-signature)
1900     (while (re-search-backward
1901             (concat "^" (regexp-quote message-followup-to-note) ".*")
1902             head t)
1903       (message-delete-line))
1904     ;; insert new note
1905     (if (message-goto-signature)
1906         (re-search-backward message-signature-separator))
1907     (if (or in-old
1908             (not cross-post)
1909             (string-match "^[ \t]*poster[ \t]*$" target-group))
1910         (insert (concat message-followup-to-note target-group "\n"))
1911       (insert (concat message-cross-post-note target-group "\n")))))
1912
1913 ;;;###autoload
1914 (defun message-cross-post-followup-to (target-group)
1915   "Crossposts message and set Followup-To to TARGET-GROUP.
1916 With prefix-argument just set Follow-Up, don't cross-post."
1917   (interactive
1918    (list ; Completion based on Gnus
1919     (completing-read "Followup To: "
1920                      (if (boundp 'gnus-newsrc-alist)
1921                          gnus-newsrc-alist)
1922                      nil nil '("poster" . 0)
1923                      (if (boundp 'gnus-group-history)
1924                          'gnus-group-history))))
1925   (cond ((not (or (null target-group) ; new subject not empty
1926                   (zerop (string-width target-group))
1927                   (string-match "^[ \t]*$" target-group)))
1928          (save-excursion
1929            (let* ((old-groups (message-fetch-field "Newsgroups"))
1930                   (in-old (string-match
1931                            (regexp-quote target-group)
1932                            (or old-groups ""))))
1933              ;; check whether target exactly matches old Newsgroups
1934              (cond ((not old-groups)
1935                     (error "No current newsgroup"))
1936                    ((or (not in-old)
1937                         (not (string-match
1938                               (concat "^[ \t]*"
1939                                       (regexp-quote target-group)
1940                                       "[ \t]*$")
1941                               old-groups)))
1942                     ;; yes, Newsgroups line must change
1943                     (message-cross-post-followup-to-header target-group)
1944                     ;; insert note whether we do cross-post or followup-to
1945                     (funcall message-cross-post-note-function
1946                              target-group
1947                              (if (or (and message-cross-post-default
1948                                           (not current-prefix-arg))
1949                                      (and (not message-cross-post-default)
1950                                           current-prefix-arg)) t)
1951                              in-old old-groups))))))))
1952
1953 ;;; Reduce To: to Cc: or Bcc: header
1954
1955 ;;;###autoload
1956 (defun message-reduce-to-to-cc ()
1957  "Replace contents of To: header with contents of Cc: or Bcc: header."
1958  (interactive)
1959  (let ((cc-content
1960         (save-restriction (message-narrow-to-headers)
1961                           (message-fetch-field "cc")))
1962        (bcc nil))
1963    (if (and (not cc-content)
1964             (setq cc-content
1965                   (save-restriction
1966                     (message-narrow-to-headers)
1967                     (message-fetch-field "bcc"))))
1968        (setq bcc t))
1969    (cond (cc-content
1970           (save-excursion
1971             (message-goto-to)
1972             (message-delete-line)
1973             (insert (concat "To: " cc-content "\n"))
1974             (save-restriction
1975               (message-narrow-to-headers)
1976               (message-remove-header (if bcc
1977                                          "bcc"
1978                                        "cc"))))))))
1979
1980 ;;; End of functions adopted from `message-utils.el'.
1981
1982 (defun message-remove-header (header &optional is-regexp first reverse)
1983   "Remove HEADER in the narrowed buffer.
1984 If IS-REGEXP, HEADER is a regular expression.
1985 If FIRST, only remove the first instance of the header.
1986 Return the number of headers removed."
1987   (goto-char (point-min))
1988   (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
1989         (number 0)
1990         (case-fold-search t)
1991         last)
1992     (while (and (not (eobp))
1993                 (not last))
1994       (if (if reverse
1995               (not (looking-at regexp))
1996             (looking-at regexp))
1997           (progn
1998             (incf number)
1999             (when first
2000               (setq last t))
2001             (delete-region
2002              (point)
2003              ;; There might be a continuation header, so we have to search
2004              ;; until we find a new non-continuation line.
2005              (progn
2006                (forward-line 1)
2007                (if (re-search-forward "^[^ \t]" nil t)
2008                    (goto-char (match-beginning 0))
2009                  (point-max)))))
2010         (forward-line 1)
2011         (if (re-search-forward "^[^ \t]" nil t)
2012             (goto-char (match-beginning 0))
2013           (goto-char (point-max)))))
2014     number))
2015
2016 (defun message-remove-first-header (header)
2017   "Remove the first instance of HEADER if there is more than one."
2018   (let ((count 0)
2019         (regexp (concat "^" (regexp-quote header) ":")))
2020     (save-excursion
2021       (goto-char (point-min))
2022       (while (re-search-forward regexp nil t)
2023         (incf count)))
2024     (while (> count 1)
2025       (message-remove-header header nil t)
2026       (decf count))))
2027
2028 (defun message-narrow-to-headers ()
2029   "Narrow the buffer to the head of the message."
2030   (widen)
2031   (narrow-to-region
2032    (goto-char (point-min))
2033    (if (re-search-forward
2034         (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
2035        (match-beginning 0)
2036      (point-max)))
2037   (goto-char (point-min)))
2038
2039 (defun message-narrow-to-head-1 ()
2040   "Like `message-narrow-to-head'.  Don't widen."
2041   (narrow-to-region
2042    (goto-char (point-min))
2043    (if (search-forward "\n\n" nil 1)
2044        (1- (point))
2045      (point-max)))
2046   (goto-char (point-min)))
2047
2048 (defun message-narrow-to-head ()
2049   "Narrow the buffer to the head of the message.
2050 Point is left at the beginning of the narrowed-to region."
2051   (widen)
2052   (message-narrow-to-head-1))
2053
2054 (defun message-narrow-to-headers-or-head ()
2055   "Narrow the buffer to the head of the message."
2056   (widen)
2057   (narrow-to-region
2058    (goto-char (point-min))
2059    (cond
2060     ((re-search-forward
2061       (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
2062      (match-beginning 0))
2063     ((search-forward "\n\n" nil t)
2064      (1- (point)))
2065     (t
2066      (point-max))))
2067   (goto-char (point-min)))
2068
2069 (defun message-news-p ()
2070   "Say whether the current buffer contains a news message."
2071   (and (not message-this-is-mail)
2072        (or message-this-is-news
2073            (save-excursion
2074              (save-restriction
2075                (message-narrow-to-headers)
2076                (and (message-fetch-field "newsgroups")
2077                     (not (message-fetch-field "posted-to"))))))))
2078
2079 (defun message-mail-p ()
2080   "Say whether the current buffer contains a mail message."
2081   (and (not message-this-is-news)
2082        (or message-this-is-mail
2083            (save-excursion
2084              (save-restriction
2085                (message-narrow-to-headers)
2086                (or (message-fetch-field "to")
2087                    (message-fetch-field "cc")
2088                    (message-fetch-field "bcc")))))))
2089
2090 (defun message-subscribed-p ()
2091   "Say whether we need to insert a MFT header."
2092   (or message-subscribed-regexps
2093       message-subscribed-addresses
2094       message-subscribed-address-file
2095       message-subscribed-address-functions))
2096
2097 (defun message-next-header ()
2098   "Go to the beginning of the next header."
2099   (beginning-of-line)
2100   (or (eobp) (forward-char 1))
2101   (not (if (re-search-forward "^[^ \t]" nil t)
2102            (beginning-of-line)
2103          (goto-char (point-max)))))
2104
2105 (defun message-sort-headers-1 ()
2106   "Sort the buffer as headers using `message-rank' text props."
2107   (goto-char (point-min))
2108   (require 'sort)
2109   (sort-subr
2110    nil 'message-next-header
2111    (lambda ()
2112      (message-next-header)
2113      (unless (bobp)
2114        (forward-char -1)))
2115    (lambda ()
2116      (or (get-text-property (point) 'message-rank)
2117          10000))))
2118
2119 (defun message-sort-headers ()
2120   "Sort the headers of the current message according to `message-header-format-alist'."
2121   (interactive)
2122   (save-excursion
2123     (save-restriction
2124       (let ((max (1+ (length message-header-format-alist)))
2125             rank)
2126         (message-narrow-to-headers)
2127         (while (re-search-forward "^[^ \n]+:" nil t)
2128           (put-text-property
2129            (match-beginning 0) (1+ (match-beginning 0))
2130            'message-rank
2131            (if (setq rank (length (memq (assq (intern (buffer-substring
2132                                                        (match-beginning 0)
2133                                                        (1- (match-end 0))))
2134                                               message-header-format-alist)
2135                                         message-header-format-alist)))
2136                (- max rank)
2137              (1+ max)))))
2138       (message-sort-headers-1))))
2139
2140 (defun message-kill-address ()
2141   "Kill the address under point."
2142   (interactive)
2143   (let ((start (point)))
2144     (message-skip-to-next-address)
2145     (kill-region start (point))))
2146
2147 \f
2148
2149 ;;;
2150 ;;; Message mode
2151 ;;;
2152
2153 ;;; Set up keymap.
2154
2155 (defvar message-mode-map nil)
2156
2157 (unless message-mode-map
2158   (setq message-mode-map (make-keymap))
2159   (set-keymap-parent message-mode-map text-mode-map)
2160   (define-key message-mode-map "\C-c?" 'describe-mode)
2161
2162   (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
2163   (define-key message-mode-map "\C-c\C-f\C-o" 'message-goto-from)
2164   (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
2165   (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
2166   (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
2167   (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
2168   (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
2169   (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
2170   (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
2171   (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
2172   (define-key message-mode-map "\C-c\C-f\C-m" 'message-goto-mail-followup-to)
2173   (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
2174   (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
2175   (define-key message-mode-map "\C-c\C-f\C-i"
2176     'message-insert-or-toggle-importance)
2177   (define-key message-mode-map "\C-c\C-f\C-a"
2178     'message-generate-unsubscribed-mail-followup-to)
2179
2180   ;; modify headers (and insert notes in body)
2181   (define-key message-mode-map "\C-c\C-fs"    'message-change-subject)
2182   ;;
2183   (define-key message-mode-map "\C-c\C-fx"    'message-cross-post-followup-to)
2184   ;; prefix+message-cross-post-followup-to = same w/o cross-post
2185   (define-key message-mode-map "\C-c\C-ft"    'message-reduce-to-to-cc)
2186   (define-key message-mode-map "\C-c\C-fa"    'message-add-archive-header)
2187   ;; mark inserted text
2188   (define-key message-mode-map "\C-c\M-m" 'message-mark-inserted-region)
2189   (define-key message-mode-map "\C-c\M-f" 'message-mark-insert-file)
2190
2191   (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
2192   (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
2193
2194   (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
2195   (define-key message-mode-map "\C-c\C-fw" 'message-insert-wide-reply)
2196   (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
2197   (define-key message-mode-map "\C-c\C-l" 'message-to-list-only)
2198
2199   (define-key message-mode-map "\C-c\C-u" 'message-insert-or-toggle-importance)
2200   (define-key message-mode-map "\C-c\M-n"
2201     'message-insert-disposition-notification-to)
2202
2203   (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
2204   (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
2205   (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
2206   (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
2207   (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
2208   (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
2209   (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
2210   (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
2211
2212   (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
2213   (define-key message-mode-map "\C-c\C-s" 'message-send)
2214   (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
2215   (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
2216   (define-key message-mode-map "\C-c\n" 'gnus-delay-article)
2217
2218   (define-key message-mode-map "\C-c\M-k" 'message-kill-address)
2219   (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
2220   (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
2221   (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
2222   (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
2223   (define-key message-mode-map [remap split-line]  'message-split-line)
2224
2225   (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
2226
2227   (define-key message-mode-map "\C-a" 'message-beginning-of-line)
2228   (define-key message-mode-map "\t" 'message-tab)
2229   (define-key message-mode-map "\M-;" 'comment-region))
2230
2231 (easy-menu-define
2232   message-mode-menu message-mode-map "Message Menu."
2233   `("Message"
2234     ["Yank Original" message-yank-original message-reply-buffer]
2235     ["Fill Yanked Message" message-fill-yanked-message t]
2236     ["Insert Signature" message-insert-signature t]
2237     ["Caesar (rot13) Message" message-caesar-buffer-body t]
2238     ["Caesar (rot13) Region" message-caesar-region (message-mark-active-p)]
2239     ["Elide Region" message-elide-region
2240      :active (message-mark-active-p)
2241      ,@(if (featurep 'xemacs) nil
2242          '(:help "Replace text in region with an ellipsis"))]
2243     ["Delete Outside Region" message-delete-not-region
2244      :active (message-mark-active-p)
2245      ,@(if (featurep 'xemacs) nil
2246          '(:help "Delete all quoted text outside region"))]
2247     ["Kill To Signature" message-kill-to-signature t]
2248     ["Newline and Reformat" message-newline-and-reformat t]
2249     ["Rename buffer" message-rename-buffer t]
2250     ["Spellcheck" ispell-message
2251      ,@(if (featurep 'xemacs) '(t)
2252          '(:help "Spellcheck this message"))]
2253     "----"
2254     ["Insert Region Marked" message-mark-inserted-region
2255      :active (message-mark-active-p)
2256      ,@(if (featurep 'xemacs) nil
2257          '(:help "Mark region with enclosing tags"))]
2258     ["Insert File Marked..." message-mark-insert-file
2259      ,@(if (featurep 'xemacs) '(t)
2260          '(:help "Insert file at point marked with enclosing tags"))]
2261     "----"
2262     ["Send Message" message-send-and-exit
2263      ,@(if (featurep 'xemacs) '(t)
2264          '(:help "Send this message"))]
2265     ["Postpone Message" message-dont-send
2266      ,@(if (featurep 'xemacs) '(t)
2267          '(:help "File this draft message and exit"))]
2268     ["Send at Specific Time..." gnus-delay-article
2269      ,@(if (featurep 'xemacs) '(t)
2270          '(:help "Ask, then arrange to send message at that time"))]
2271     ["Kill Message" message-kill-buffer
2272      ,@(if (featurep 'xemacs) '(t)
2273          '(:help "Delete this message without sending"))]))
2274
2275 (easy-menu-define
2276   message-mode-field-menu message-mode-map ""
2277   `("Field"
2278     ["To" message-goto-to t]
2279     ["From" message-goto-from t]
2280     ["Subject" message-goto-subject t]
2281     ["Change subject..." message-change-subject t]
2282     ["Cc" message-goto-cc t]
2283     ["Bcc" message-goto-bcc t]
2284     ["Fcc" message-goto-fcc t]
2285     ["Reply-To" message-goto-reply-to t]
2286     ["Flag As Important" message-insert-importance-high
2287      ,@(if (featurep 'xemacs) '(t)
2288          '(:help "Mark this message as important"))]
2289     ["Flag As Unimportant" message-insert-importance-low
2290      ,@(if (featurep 'xemacs) '(t)
2291          '(:help "Mark this message as unimportant"))]
2292     ["Request Receipt"
2293      message-insert-disposition-notification-to
2294      ,@(if (featurep 'xemacs) '(t)
2295          '(:help "Request a receipt notification"))]
2296     "----"
2297     ;; (typical) news stuff
2298     ["Summary" message-goto-summary t]
2299     ["Keywords" message-goto-keywords t]
2300     ["Newsgroups" message-goto-newsgroups t]
2301     ["Fetch Newsgroups" message-insert-newsgroups t]
2302     ["Followup-To" message-goto-followup-to t]
2303     ;; ["Followup-To (with note in body)" message-cross-post-followup-to t]
2304     ["Crosspost / Followup-To..." message-cross-post-followup-to t]
2305     ["Distribution" message-goto-distribution t]
2306     ["X-No-Archive:" message-add-archive-header t ]
2307     "----"
2308     ;; (typical) mailing-lists stuff
2309     ["Fetch To" message-insert-to
2310      ,@(if (featurep 'xemacs) '(t)
2311          '(:help "Insert a To header that points to the author."))]
2312     ["Fetch To and Cc" message-insert-wide-reply
2313      ,@(if (featurep 'xemacs) '(t)
2314          '(:help
2315            "Insert To and Cc headers as if you were doing a wide reply."))]
2316     "----"
2317     ["Send to list only" message-to-list-only t]
2318     ["Mail-Followup-To" message-goto-mail-followup-to t]
2319     ["Unsubscribed list post" message-generate-unsubscribed-mail-followup-to
2320      ,@(if (featurep 'xemacs) '(t)
2321          '(:help "Insert a reasonable `Mail-Followup-To:' header."))]
2322     ["Reduce To: to Cc:" message-reduce-to-to-cc t]
2323     "----"
2324     ["Sort Headers" message-sort-headers t]
2325     ["Encode non-ASCII domain names" message-idna-to-ascii-rhs t]
2326     ["Goto Body" message-goto-body t]
2327     ["Goto Signature" message-goto-signature t]))
2328
2329 (defvar message-tool-bar-map nil)
2330
2331 (eval-when-compile
2332   (defvar facemenu-add-face-function)
2333   (defvar facemenu-remove-face-function))
2334
2335 ;;; Forbidden properties
2336 ;;
2337 ;; We use `after-change-functions' to keep special text properties
2338 ;; that interfer with the normal function of message mode out of the
2339 ;; buffer.
2340
2341 (defcustom message-strip-special-text-properties t
2342   "Strip special properties from the message buffer.
2343
2344 Emacs has a number of special text properties which can break message
2345 composing in various ways.  If this option is set, message will strip
2346 these properties from the message composition buffer.  However, some
2347 packages requires these properties to be present in order to work.
2348 If you use one of these packages, turn this option off, and hope the
2349 message composition doesn't break too bad."
2350   :group 'message-various
2351   :link '(custom-manual "(message)Various Message Variables")
2352   :type 'boolean)
2353
2354 (defconst message-forbidden-properties
2355   ;; No reason this should be clutter up customize.  We make it a
2356   ;; property list (rather than a list of property symbols), to be
2357   ;; directly useful for `remove-text-properties'.
2358   '(field nil read-only nil invisible nil intangible nil
2359           mouse-face nil modification-hooks nil insert-in-front-hooks nil
2360           insert-behind-hooks nil point-entered nil point-left nil)
2361   ;; Other special properties:
2362   ;; category, face, display: probably doesn't do any harm.
2363   ;; fontified: is used by font-lock.
2364   ;; syntax-table, local-map: I dunno.
2365   ;; We need to add XEmacs names to the list.
2366   "Property list of with properties.forbidden in message buffers.
2367 The values of the properties are ignored, only the property names are used.")
2368
2369 (defun message-tamago-not-in-use-p (pos)
2370   "Return t when tamago version 4 is not in use at the cursor position.
2371 Tamago version 4 is a popular input method for writing Japanese text.
2372 It uses the properties `intangible', `invisible', `modification-hooks'
2373 and `read-only' when translating ascii or kana text to kanji text.
2374 These properties are essential to work, so we should never strip them."
2375   (not (and (boundp 'egg-modefull-mode)
2376             (symbol-value 'egg-modefull-mode)
2377             (or (memq (get-text-property pos 'intangible)
2378                       '(its-part-1 its-part-2))
2379                 (get-text-property pos 'egg-end)
2380                 (get-text-property pos 'egg-lang)
2381                 (get-text-property pos 'egg-start)))))
2382
2383 (defun message-strip-forbidden-properties (begin end &optional old-length)
2384   "Strip forbidden properties between BEGIN and END, ignoring the third arg.
2385 This function is intended to be called from `after-change-functions'.
2386 See also `message-forbidden-properties'."
2387   (when (and message-strip-special-text-properties
2388              (message-tamago-not-in-use-p begin))
2389     (dolist (from-to (message-text-with-property 'message-hidden
2390                                                  begin end t))
2391       (remove-text-properties (car from-to) (cdr from-to)
2392                               message-forbidden-properties))))
2393
2394 ;;;###autoload
2395 (define-derived-mode message-mode text-mode "Message"
2396   "Major mode for editing mail and news to be sent.
2397 Like Text Mode but with these additional commands:\\<message-mode-map>
2398 C-c C-s  `message-send' (send the message)  C-c C-c  `message-send-and-exit'
2399 C-c C-d  Postpone sending the message       C-c C-k  Kill the message
2400 C-c C-f  move to a header field (and create it if there isn't):
2401          C-c C-f C-t  move to To        C-c C-f C-s  move to Subject
2402          C-c C-f C-c  move to Cc        C-c C-f C-b  move to Bcc
2403          C-c C-f C-w  move to Fcc       C-c C-f C-r  move to Reply-To
2404          C-c C-f C-u  move to Summary   C-c C-f C-n  move to Newsgroups
2405          C-c C-f C-k  move to Keywords  C-c C-f C-d  move to Distribution
2406          C-c C-f C-o  move to From (\"Originator\")
2407          C-c C-f C-f  move to Followup-To
2408          C-c C-f C-m  move to Mail-Followup-To
2409          C-c C-f C-i  cycle through Importance values
2410          C-c C-f s    change subject and append \"(was: <Old Subject>)\"
2411          C-c C-f x    crossposting with FollowUp-To header and note in body
2412          C-c C-f t    replace To: header with contents of Cc: or Bcc:
2413          C-c C-f a    Insert X-No-Archive: header and a note in the body
2414 C-c C-t  `message-insert-to' (add a To header to a news followup)
2415 C-c C-l  `message-to-list-only' (removes all but list address in to/cc)
2416 C-c C-n  `message-insert-newsgroups' (add a Newsgroup header to a news reply)
2417 C-c C-b  `message-goto-body' (move to beginning of message text).
2418 C-c C-i  `message-goto-signature' (move to the beginning of the signature).
2419 C-c C-w  `message-insert-signature' (insert `message-signature-file' file).
2420 C-c C-y  `message-yank-original' (insert current message, if any).
2421 C-c C-q  `message-fill-yanked-message' (fill what was yanked).
2422 C-c C-e  `message-elide-region' (elide the text between point and mark).
2423 C-c C-v  `message-delete-not-region' (remove the text outside the region).
2424 C-c C-z  `message-kill-to-signature' (kill the text up to the signature).
2425 C-c C-r  `message-caesar-buffer-body' (rot13 the message body).
2426 C-c C-a  `mml-attach-file' (attach a file as MIME).
2427 C-c C-u  `message-insert-or-toggle-importance'  (insert or cycle importance).
2428 C-c M-n  `message-insert-disposition-notification-to'  (request receipt).
2429 C-c M-m  `message-mark-inserted-region' (mark region with enclosing tags).
2430 C-c M-f  `message-mark-insert-file' (insert file marked with enclosing tags).
2431 M-RET    `message-newline-and-reformat' (break the line and reformat)."
2432   (setq local-abbrev-table text-mode-abbrev-table)
2433   (set (make-local-variable 'message-reply-buffer) nil)
2434   (set (make-local-variable 'message-inserted-headers) nil)
2435   (set (make-local-variable 'message-send-actions) nil)
2436   (set (make-local-variable 'message-exit-actions) nil)
2437   (set (make-local-variable 'message-kill-actions) nil)
2438   (set (make-local-variable 'message-postpone-actions) nil)
2439   (set (make-local-variable 'message-draft-article) nil)
2440   (setq buffer-offer-save t)
2441   (set (make-local-variable 'facemenu-add-face-function)
2442        (lambda (face end)
2443          (let ((face-fun (cdr (assq face message-face-alist))))
2444            (if face-fun
2445                (funcall face-fun (point) end)
2446              (error "Face %s not configured for %s mode" face mode-name)))
2447          ""))
2448   (set (make-local-variable 'facemenu-remove-face-function) t)
2449   (set (make-local-variable 'message-reply-headers) nil)
2450   (make-local-variable 'message-newsreader)
2451   (make-local-variable 'message-mailer)
2452   (make-local-variable 'message-post-method)
2453   (set (make-local-variable 'message-sent-message-via) nil)
2454   (set (make-local-variable 'message-checksum) nil)
2455   (set (make-local-variable 'message-mime-part) 0)
2456   (message-setup-fill-variables)
2457   ;; Allow using comment commands to add/remove quoting.
2458   (set (make-local-variable 'comment-start) message-yank-prefix)
2459   (if (featurep 'xemacs)
2460       (message-setup-toolbar)
2461     (set (make-local-variable 'font-lock-defaults)
2462          '(message-font-lock-keywords t))
2463     (if (boundp 'tool-bar-map)
2464         (set (make-local-variable 'tool-bar-map) (message-tool-bar-map))))
2465   (easy-menu-add message-mode-menu message-mode-map)
2466   (easy-menu-add message-mode-field-menu message-mode-map)
2467   (gnus-make-local-hook 'after-change-functions)
2468   ;; Mmmm... Forbidden properties...
2469   (add-hook 'after-change-functions 'message-strip-forbidden-properties
2470             nil 'local)
2471   ;; Allow mail alias things.
2472   (when (eq message-mail-alias-type 'abbrev)
2473     (if (fboundp 'mail-abbrevs-setup)
2474         (mail-abbrevs-setup)
2475       (if (fboundp 'mail-aliases-setup) ; warning avoidance
2476           (mail-aliases-setup))))
2477   (unless buffer-file-name
2478     (message-set-auto-save-file-name))
2479   (unless (buffer-base-buffer)
2480     ;; Don't enable multibyte on an indirect buffer.  Maybe enabling
2481     ;; multibyte is not necessary at all. -- zsh
2482     (mm-enable-multibyte))
2483   (set (make-local-variable 'indent-tabs-mode) nil) ;No tabs for indentation.
2484   (mml-mode))
2485
2486 (defun message-setup-fill-variables ()
2487   "Setup message fill variables."
2488   (set (make-local-variable 'fill-paragraph-function)
2489        'message-fill-paragraph)
2490   (make-local-variable 'paragraph-separate)
2491   (make-local-variable 'paragraph-start)
2492   (make-local-variable 'adaptive-fill-regexp)
2493   (unless (boundp 'adaptive-fill-first-line-regexp)
2494     (setq adaptive-fill-first-line-regexp nil))
2495   (make-local-variable 'adaptive-fill-first-line-regexp)
2496   (let ((quote-prefix-regexp
2497          ;; User should change message-cite-prefix-regexp if
2498          ;; message-yank-prefix is set to an abnormal value.
2499          (concat "\\(" message-cite-prefix-regexp "\\)[ \t]*")))
2500     (setq paragraph-start
2501           (concat
2502            (regexp-quote mail-header-separator) "$\\|"
2503            "[ \t]*$\\|"                 ; blank lines
2504            "-- $\\|"                    ; signature delimiter
2505            "---+$\\|"              ; delimiters for forwarded messages
2506            page-delimiter "$\\|"        ; spoiler warnings
2507            ".*wrote:$\\|"               ; attribution lines
2508            quote-prefix-regexp "$\\|"   ; empty lines in quoted text
2509                                         ; mml tags
2510            "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)"))
2511     (setq paragraph-separate paragraph-start)
2512     (setq adaptive-fill-regexp
2513           (concat quote-prefix-regexp "\\|" adaptive-fill-regexp))
2514     (setq adaptive-fill-first-line-regexp
2515           (concat quote-prefix-regexp "\\|"
2516                   adaptive-fill-first-line-regexp)))
2517   (make-local-variable 'auto-fill-inhibit-regexp)
2518   ;;(setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
2519   (setq auto-fill-inhibit-regexp nil)
2520   (make-local-variable 'normal-auto-fill-function)
2521   (setq normal-auto-fill-function 'message-do-auto-fill)
2522   ;; KLUDGE: auto fill might already be turned on in `text-mode-hook'.
2523   ;; In that case, ensure that it uses the right function.  The real
2524   ;; solution would be not to use `define-derived-mode', and run
2525   ;; `text-mode-hook' ourself at the end of the mode.
2526   ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-19.
2527   (when auto-fill-function
2528     (setq auto-fill-function normal-auto-fill-function)))
2529
2530 \f
2531
2532 ;;;
2533 ;;; Message mode commands
2534 ;;;
2535
2536 ;;; Movement commands
2537
2538 (defun message-goto-to ()
2539   "Move point to the To header."
2540   (interactive)
2541   (message-position-on-field "To"))
2542
2543 (defun message-goto-from ()
2544   "Move point to the From header."
2545   (interactive)
2546   (message-position-on-field "From"))
2547
2548 (defun message-goto-subject ()
2549   "Move point to the Subject header."
2550   (interactive)
2551   (message-position-on-field "Subject"))
2552
2553 (defun message-goto-cc ()
2554   "Move point to the Cc header."
2555   (interactive)
2556   (message-position-on-field "Cc" "To"))
2557
2558 (defun message-goto-bcc ()
2559   "Move point to the Bcc  header."
2560   (interactive)
2561   (message-position-on-field "Bcc" "Cc" "To"))
2562
2563 (defun message-goto-fcc ()
2564   "Move point to the Fcc header."
2565   (interactive)
2566   (message-position-on-field "Fcc" "To" "Newsgroups"))
2567
2568 (defun message-goto-reply-to ()
2569   "Move point to the Reply-To header."
2570   (interactive)
2571   (message-position-on-field "Reply-To" "Subject"))
2572
2573 (defun message-goto-newsgroups ()
2574   "Move point to the Newsgroups header."
2575   (interactive)
2576   (message-position-on-field "Newsgroups"))
2577
2578 (defun message-goto-distribution ()
2579   "Move point to the Distribution header."
2580   (interactive)
2581   (message-position-on-field "Distribution"))
2582
2583 (defun message-goto-followup-to ()
2584   "Move point to the Followup-To header."
2585   (interactive)
2586   (message-position-on-field "Followup-To" "Newsgroups"))
2587
2588 (defun message-goto-mail-followup-to ()
2589   "Move point to the Mail-Followup-To header."
2590   (interactive)
2591   (message-position-on-field "Mail-Followup-To" "From"))
2592
2593 (defun message-goto-keywords ()
2594   "Move point to the Keywords header."
2595   (interactive)
2596   (message-position-on-field "Keywords" "Subject"))
2597
2598 (defun message-goto-summary ()
2599   "Move point to the Summary header."
2600   (interactive)
2601   (message-position-on-field "Summary" "Subject"))
2602
2603 (defun message-goto-body (&optional interactivep)
2604   "Move point to the beginning of the message body."
2605   (interactive (list t))
2606   (when (and interactivep
2607              (looking-at "[ \t]*\n"))
2608     (expand-abbrev))
2609   (goto-char (point-min))
2610   (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
2611       (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t)))
2612
2613 (defun message-goto-eoh ()
2614   "Move point to the end of the headers."
2615   (interactive)
2616   (message-goto-body)
2617   (forward-line -1))
2618
2619 (defun message-goto-signature ()
2620   "Move point to the beginning of the message signature.
2621 If there is no signature in the article, go to the end and
2622 return nil."
2623   (interactive)
2624   (goto-char (point-min))
2625   (if (re-search-forward message-signature-separator nil t)
2626       (forward-line 1)
2627     (goto-char (point-max))
2628     nil))
2629
2630 (defun message-generate-unsubscribed-mail-followup-to (&optional include-cc)
2631   "Insert a reasonable MFT header in a post to an unsubscribed list.
2632 When making original posts to a mailing list you are not subscribed to,
2633 you have to type in a MFT header by hand.  The contents, usually, are
2634 the addresses of the list and your own address.  This function inserts
2635 such a header automatically.  It fetches the contents of the To: header
2636 in the current mail buffer, and appends the current `user-mail-address'.
2637
2638 If the optional argument INCLUDE-CC is non-nil, the addresses in the
2639 Cc: header are also put into the MFT."
2640
2641   (interactive "P")
2642   (let* (cc tos)
2643     (save-restriction
2644       (message-narrow-to-headers)
2645       (message-remove-header "Mail-Followup-To")
2646       (setq cc (and include-cc (message-fetch-field "Cc")))
2647       (setq tos (if cc
2648                     (concat (message-fetch-field "To") "," cc)
2649                   (message-fetch-field "To"))))
2650     (message-goto-mail-followup-to)
2651     (insert (concat tos ", " user-mail-address))))
2652
2653 \f
2654
2655 (defun message-insert-to (&optional force)
2656   "Insert a To header that points to the author of the article being replied to.
2657 If the original author requested not to be sent mail, don't insert unless the
2658 prefix FORCE is given."
2659   (interactive "P")
2660   (let* ((mct (message-fetch-reply-field "mail-copies-to"))
2661          (dont (and mct (or (equal (downcase mct) "never")
2662                             (equal (downcase mct) "nobody"))))
2663          (to (or (message-fetch-reply-field "mail-reply-to")
2664                  (message-fetch-reply-field "reply-to")
2665                  (message-fetch-reply-field "from"))))
2666     (when (and dont to)
2667       (message
2668        (if force
2669            "Ignoring the user request not to have copies sent via mail"
2670          "Complying with the user request not to have copies sent via mail")))
2671     (when (and force (not to))
2672       (error "No mail address in the article"))
2673     (when (and to (or force (not dont)))
2674       (message-carefully-insert-headers (list (cons 'To to))))))
2675
2676 (defun message-insert-wide-reply ()
2677   "Insert To and Cc headers as if you were doing a wide reply."
2678   (interactive)
2679   (let ((headers (message-with-reply-buffer
2680                    (message-get-reply-headers t))))
2681     (message-carefully-insert-headers headers)))
2682
2683 (defcustom message-header-synonyms
2684   '((To Cc Bcc))
2685   "List of lists of header synonyms.
2686 E.g., if this list contains a member list with elements `Cc' and `To',
2687 then `message-carefully-insert-headers' will not insert a `To' header
2688 when the message is already `Cc'ed to the recipient."
2689   :group 'message-headers
2690   :link '(custom-manual "(message)Message Headers")
2691   :type '(repeat sexp))
2692
2693 (defun message-carefully-insert-headers (headers)
2694   "Insert the HEADERS, an alist, into the message buffer.
2695 Does not insert the headers when they are already present there
2696 or in the synonym headers, defined by `message-header-synonyms'."
2697   ;; FIXME: Should compare only the address and not the full name.  Comparison
2698   ;; should be done case-folded (and with `string=' rather than
2699   ;; `string-match').
2700   (dolist (header headers)
2701     (let* ((header-name (symbol-name (car header)))
2702            (new-header (cdr header))
2703            (synonyms (loop for synonym in message-header-synonyms
2704                            when (memq (car header) synonym) return synonym))
2705            (old-header
2706             (loop for synonym in synonyms
2707                   for old-header = (mail-fetch-field (symbol-name synonym))
2708                   when (and old-header (string-match new-header old-header))
2709                   return synonym)))
2710       (if old-header
2711           (message "already have `%s' in `%s'" new-header old-header)
2712         (when (and (message-position-on-field header-name)
2713                    (setq old-header (mail-fetch-field header-name))
2714                    (not (string-match "\\` *\\'" old-header)))
2715           (insert ", "))
2716         (insert new-header)))))
2717
2718 (defun message-widen-reply ()
2719   "Widen the reply to include maximum recipients."
2720   (interactive)
2721   (let ((follow-to
2722          (and message-reply-buffer
2723               (buffer-name message-reply-buffer)
2724               (save-excursion
2725                 (set-buffer message-reply-buffer)
2726                 (message-get-reply-headers t)))))
2727     (save-excursion
2728       (save-restriction
2729         (message-narrow-to-headers)
2730         (dolist (elem follow-to)
2731           (message-remove-header (symbol-name (car elem)))
2732           (goto-char (point-min))
2733           (insert (symbol-name (car elem)) ": "
2734                   (cdr elem) "\n"))))))
2735
2736 (defun message-insert-newsgroups ()
2737   "Insert the Newsgroups header from the article being replied to."
2738   (interactive)
2739   (when (and (message-position-on-field "Newsgroups")
2740              (mail-fetch-field "newsgroups")
2741              (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
2742     (insert ","))
2743   (insert (or (message-fetch-reply-field "newsgroups") "")))
2744
2745 \f
2746
2747 ;;; Various commands
2748
2749 (defun message-delete-not-region (beg end)
2750   "Delete everything in the body of the current message outside of the region."
2751   (interactive "r")
2752   (let (citeprefix)
2753     (save-excursion
2754       (goto-char beg)
2755       ;; snarf citation prefix, if appropriate
2756       (unless (eq (point) (progn (beginning-of-line) (point)))
2757         (when (looking-at message-cite-prefix-regexp)
2758           (setq citeprefix (match-string 0))))
2759       (goto-char end)
2760       (delete-region (point) (if (not (message-goto-signature))
2761                                  (point)
2762                                (forward-line -2)
2763                                (point)))
2764       (insert "\n")
2765       (goto-char beg)
2766       (delete-region beg (progn (message-goto-body)
2767                                 (forward-line 2)
2768                                 (point)))
2769       (when citeprefix
2770         (insert citeprefix))))
2771   (when (message-goto-signature)
2772     (forward-line -2)))
2773
2774 (defun message-kill-to-signature (&optional arg)
2775   "Kill all text up to the signature.
2776 If a numberic argument or prefix arg is given, leave that number
2777 of lines before the signature intact."
2778   (interactive "p")
2779   (save-excursion
2780     (save-restriction
2781       (let ((point (point)))
2782         (narrow-to-region point (point-max))
2783         (message-goto-signature)
2784         (unless (eobp)
2785           (if (and arg (numberp arg))
2786               (forward-line (- -1 arg))
2787             (end-of-line -1)))
2788         (unless (= point (point))
2789           (kill-region point (point))
2790           (insert "\n"))))))
2791
2792 (defun message-newline-and-reformat (&optional arg not-break)
2793   "Insert four newlines, and then reformat if inside quoted text.
2794 Prefix arg means justify as well."
2795   (interactive (list (if current-prefix-arg 'full)))
2796   (let (quoted point beg end leading-space bolp)
2797     (setq point (point))
2798     (beginning-of-line)
2799     (setq beg (point))
2800     (setq bolp (= beg point))
2801     ;; Find first line of the paragraph.
2802     (if not-break
2803         (while (and (not (eobp))
2804                     (not (looking-at message-cite-prefix-regexp))
2805                     (looking-at paragraph-start))
2806           (forward-line 1)))
2807     ;; Find the prefix
2808     (when (looking-at message-cite-prefix-regexp)
2809       (setq quoted (match-string 0))
2810       (goto-char (match-end 0))
2811       (looking-at "[ \t]*")
2812       (setq leading-space (match-string 0)))
2813     (if (and quoted
2814              (not not-break)
2815              (not bolp)
2816              (< (- point beg) (length quoted)))
2817         ;; break inside the cite prefix.
2818         (setq quoted nil
2819               end nil))
2820     (if quoted
2821         (progn
2822           (forward-line 1)
2823           (while (and (not (eobp))
2824                       (not (looking-at paragraph-separate))
2825                       (looking-at message-cite-prefix-regexp)
2826                       (equal quoted (match-string 0)))
2827             (goto-char (match-end 0))
2828             (looking-at "[ \t]*")
2829             (if (> (length leading-space) (length (match-string 0)))
2830                 (setq leading-space (match-string 0)))
2831             (forward-line 1))
2832           (setq end (point))
2833           (goto-char beg)
2834           (while (and (if (bobp) nil (forward-line -1) t)
2835                       (not (looking-at paragraph-start))
2836                       (looking-at message-cite-prefix-regexp)
2837                       (equal quoted (match-string 0)))
2838             (setq beg (point))
2839             (goto-char (match-end 0))
2840             (looking-at "[ \t]*")
2841             (if (> (length leading-space) (length (match-string 0)))
2842                 (setq leading-space (match-string 0)))))
2843       (while (and (not (eobp))
2844                   (not (looking-at paragraph-separate))
2845                   (not (looking-at message-cite-prefix-regexp)))
2846         (forward-line 1))
2847       (setq end (point))
2848       (goto-char beg)
2849       (while (and (if (bobp) nil (forward-line -1) t)
2850                   (not (looking-at paragraph-start))
2851                   (not (looking-at message-cite-prefix-regexp)))
2852         (setq beg (point))))
2853     (goto-char point)
2854     (save-restriction
2855       (narrow-to-region beg end)
2856       (if not-break
2857           (setq point nil)
2858         (if bolp
2859             (newline)
2860           (newline)
2861           (newline))
2862         (setq point (point))
2863         ;; (newline 2) doesn't mark both newline's as hard, so call
2864         ;; newline twice. -jas
2865         (newline)
2866         (newline)
2867         (delete-region (point) (re-search-forward "[ \t]*"))
2868         (when (and quoted (not bolp))
2869           (insert quoted leading-space)))
2870       (undo-boundary)
2871       (if quoted
2872           (let* ((adaptive-fill-regexp
2873                   (regexp-quote (concat quoted leading-space)))
2874                  (adaptive-fill-first-line-regexp
2875                   adaptive-fill-regexp ))
2876             (fill-paragraph arg))
2877         (fill-paragraph arg))
2878       (if point (goto-char point)))))
2879
2880 (defun message-fill-paragraph (&optional arg)
2881   "Like `fill-paragraph'."
2882   (interactive (list (if current-prefix-arg 'full)))
2883   (if (if (boundp 'filladapt-mode) filladapt-mode)
2884       nil
2885     (if (message-point-in-header-p)
2886         (message-fill-field)
2887       (message-newline-and-reformat arg t))
2888     t))
2889
2890 ;; Is it better to use `mail-header-end'?
2891 (defun message-point-in-header-p ()
2892   "Return t if point is in the header."
2893   (save-excursion
2894     (let ((p (point)))
2895       (goto-char (point-min))
2896       (not (re-search-forward
2897             (concat "^" (regexp-quote mail-header-separator) "\n")
2898             p t)))))
2899
2900 (defun message-do-auto-fill ()
2901   "Like `do-auto-fill', but don't fill in message header."
2902   (unless (message-point-in-header-p)
2903     (do-auto-fill)))
2904
2905 (defun message-insert-signature (&optional force)
2906   "Insert a signature.  See documentation for variable `message-signature'."
2907   (interactive (list 0))
2908   (let* ((signature
2909           (cond
2910            ((and (null message-signature)
2911                  (eq force 0))
2912             (save-excursion
2913               (goto-char (point-max))
2914               (not (re-search-backward message-signature-separator nil t))))
2915            ((and (null message-signature)
2916                  force)
2917             t)
2918            ((functionp message-signature)
2919             (funcall message-signature))
2920            ((listp message-signature)
2921             (eval message-signature))
2922            (t message-signature)))
2923          (signature
2924           (cond ((stringp signature)
2925                  signature)
2926                 ((and (eq t signature)
2927                       message-signature-file
2928                       (file-exists-p message-signature-file))
2929                  signature))))
2930     (when signature
2931       (goto-char (point-max))
2932       ;; Insert the signature.
2933       (unless (bolp)
2934         (insert "\n"))
2935       (when message-signature-insert-empty-line
2936         (insert "\n"))
2937       (insert "-- \n")
2938       (if (eq signature t)
2939           (insert-file-contents message-signature-file)
2940         (insert signature))
2941       (goto-char (point-max))
2942       (or (bolp) (insert "\n")))))
2943
2944 (defun message-insert-importance-high ()
2945   "Insert header to mark message as important."
2946   (interactive)
2947   (save-excursion
2948     (save-restriction
2949       (message-narrow-to-headers)
2950       (message-remove-header "Importance"))
2951     (message-goto-eoh)
2952     (insert "Importance: high\n")))
2953
2954 (defun message-insert-importance-low ()
2955   "Insert header to mark message as unimportant."
2956   (interactive)
2957   (save-excursion
2958     (save-restriction
2959       (message-narrow-to-headers)
2960       (message-remove-header "Importance"))
2961     (message-goto-eoh)
2962     (insert "Importance: low\n")))
2963
2964 (defun message-insert-or-toggle-importance ()
2965   "Insert a \"Importance: high\" header, or cycle through the header values.
2966 The three allowed values according to RFC 1327 are `high', `normal'
2967 and `low'."
2968   (interactive)
2969   (save-excursion
2970     (let ((valid '("high" "normal" "low"))
2971           (new "high")
2972           cur)
2973       (save-restriction
2974         (message-narrow-to-headers)
2975         (when (setq cur (message-fetch-field "Importance"))
2976           (message-remove-header "Importance")
2977           (setq new (cond ((string= cur "high")
2978                            "low")
2979                           ((string= cur "low")
2980                            "normal")
2981                           (t
2982                            "high")))))
2983       (message-goto-eoh)
2984       (insert (format "Importance: %s\n" new)))))
2985
2986 (defun message-insert-disposition-notification-to ()
2987   "Request a disposition notification (return receipt) to this message.
2988 Note that this should not be used in newsgroups."
2989   (interactive)
2990   (save-excursion
2991     (save-restriction
2992       (message-narrow-to-headers)
2993       (message-remove-header "Disposition-Notification-To"))
2994     (message-goto-eoh)
2995     (insert (format "Disposition-Notification-To: %s\n"
2996                     (or (message-field-value "Reply-to")
2997                         (message-field-value "From")
2998                         (message-make-from))))))
2999
3000 (defun message-elide-region (b e)
3001   "Elide the text in the region.
3002 An ellipsis (from `message-elide-ellipsis') will be inserted where the
3003 text was killed."
3004   (interactive "r")
3005   (kill-region b e)
3006   (insert message-elide-ellipsis))
3007
3008 (defvar message-caesar-translation-table nil)
3009
3010 (defun message-caesar-region (b e &optional n)
3011   "Caesar rotate region B to E by N, default 13, for decrypting netnews."
3012   (interactive
3013    (list
3014     (min (point) (or (mark t) (point)))
3015     (max (point) (or (mark t) (point)))
3016     (when current-prefix-arg
3017       (prefix-numeric-value current-prefix-arg))))
3018
3019   (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
3020   (unless (or (zerop n)                 ; no action needed for a rot of 0
3021               (= b e))                  ; no region to rotate
3022     ;; We build the table, if necessary.
3023     (when (or (not message-caesar-translation-table)
3024               (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
3025       (setq message-caesar-translation-table
3026             (message-make-caesar-translation-table n)))
3027     (translate-region b e message-caesar-translation-table)))
3028
3029 (defun message-make-caesar-translation-table (n)
3030   "Create a rot table with offset N."
3031   (let ((i -1)
3032         (table (make-string 256 0)))
3033     (while (< (incf i) 256)
3034       (aset table i i))
3035     (concat
3036      (substring table 0 ?A)
3037      (substring table (+ ?A n) (+ ?A n (- 26 n)))
3038      (substring table ?A (+ ?A n))
3039      (substring table (+ ?A 26) ?a)
3040      (substring table (+ ?a n) (+ ?a n (- 26 n)))
3041      (substring table ?a (+ ?a n))
3042      (substring table (+ ?a 26) 255))))
3043
3044 (defun message-caesar-buffer-body (&optional rotnum)
3045   "Caesar rotate all letters in the current buffer by 13 places.
3046 Used to encode/decode possibly offensive messages (commonly in rec.humor).
3047 With prefix arg, specifies the number of places to rotate each letter forward.
3048 Mail and USENET news headers are not rotated."
3049   (interactive (if current-prefix-arg
3050                    (list (prefix-numeric-value current-prefix-arg))
3051                  (list nil)))
3052   (save-excursion
3053     (save-restriction
3054       (when (message-goto-body)
3055         (narrow-to-region (point) (point-max)))
3056       (message-caesar-region (point-min) (point-max) rotnum))))
3057
3058 (defun message-pipe-buffer-body (program)
3059   "Pipe the message body in the current buffer through PROGRAM."
3060   (save-excursion
3061     (save-restriction
3062       (when (message-goto-body)
3063         (narrow-to-region (point) (point-max)))
3064       (shell-command-on-region
3065        (point-min) (point-max) program nil t))))
3066
3067 (defun message-rename-buffer (&optional enter-string)
3068   "Rename the *message* buffer to \"*message* RECIPIENT\".
3069 If the function is run with a prefix, it will ask for a new buffer
3070 name, rather than giving an automatic name."
3071   (interactive "Pbuffer name: ")
3072   (save-excursion
3073     (save-restriction
3074       (goto-char (point-min))
3075       (narrow-to-region (point)
3076                         (search-forward mail-header-separator nil 'end))
3077       (let* ((mail-to (or
3078                        (if (message-news-p) (message-fetch-field "Newsgroups")
3079                          (message-fetch-field "To"))
3080                        ""))
3081              (mail-trimmed-to
3082               (if (string-match "," mail-to)
3083                   (concat (substring mail-to 0 (match-beginning 0)) ", ...")
3084                 mail-to))
3085              (name-default (concat "*message* " mail-trimmed-to))
3086              (name (if enter-string
3087                        (read-string "New buffer name: " name-default)
3088                      name-default)))
3089         (rename-buffer name t)))))
3090
3091 (defun message-fill-yanked-message (&optional justifyp)
3092   "Fill the paragraphs of a message yanked into this one.
3093 Numeric argument means justify as well."
3094   (interactive "P")
3095   (save-excursion
3096     (goto-char (point-min))
3097     (search-forward (concat "\n" mail-header-separator "\n") nil t)
3098     (let ((fill-prefix message-yank-prefix))
3099       (fill-individual-paragraphs (point) (point-max) justifyp))))
3100
3101 (defun message-indent-citation ()
3102   "Modify text just inserted from a message to be cited.
3103 The inserted text should be the region.
3104 When this function returns, the region is again around the modified text.
3105
3106 Normally, indent each nonblank line `message-indentation-spaces' spaces.
3107 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
3108   (let ((start (point)))
3109     ;; Remove unwanted headers.
3110     (when message-ignored-cited-headers
3111       (let (all-removed)
3112         (save-restriction
3113           (narrow-to-region
3114            (goto-char start)
3115            (if (search-forward "\n\n" nil t)
3116                (1- (point))
3117              (point)))
3118           (message-remove-header message-ignored-cited-headers t)
3119           (when (= (point-min) (point-max))
3120             (setq all-removed t))
3121           (goto-char (point-max)))
3122         (if all-removed
3123             (goto-char start)
3124           (forward-line 1))))
3125     ;; Delete blank lines at the start of the buffer.
3126     (while (and (point-min)
3127                 (eolp)
3128                 (not (eobp)))
3129       (message-delete-line))
3130     ;; Delete blank lines at the end of the buffer.
3131     (goto-char (point-max))
3132     (unless (eolp)
3133       (insert "\n"))
3134     (while (and (zerop (forward-line -1))
3135                 (looking-at "$"))
3136       (message-delete-line))
3137     ;; Do the indentation.
3138     (if (null message-yank-prefix)
3139         (indent-rigidly start (mark t) message-indentation-spaces)
3140       (save-excursion
3141         (goto-char start)
3142         (while (< (point) (mark t))
3143           (if (or (looking-at ">") (looking-at "^$"))
3144               (insert message-yank-cited-prefix)
3145             (insert message-yank-prefix))
3146           (forward-line 1))))
3147     (goto-char start)))
3148
3149 (defun message-yank-original (&optional arg)
3150   "Insert the message being replied to, if any.
3151 Puts point before the text and mark after.
3152 Normally indents each nonblank line ARG spaces (default 3).  However,
3153 if `message-yank-prefix' is non-nil, insert that prefix on each line.
3154
3155 This function uses `message-cite-function' to do the actual citing.
3156
3157 Just \\[universal-argument] as argument means don't indent, insert no
3158 prefix, and don't delete any headers."
3159   (interactive "P")
3160   (let ((modified (buffer-modified-p)))
3161     (when (and message-reply-buffer
3162                message-cite-function)
3163       (delete-windows-on message-reply-buffer t)
3164       (insert-buffer message-reply-buffer)
3165       (unless arg
3166         (funcall message-cite-function))
3167       (message-exchange-point-and-mark)
3168       (unless (bolp)
3169         (insert ?\n))
3170       (unless modified
3171         (setq message-checksum (message-checksum))))))
3172
3173 (defun message-yank-buffer (buffer)
3174   "Insert BUFFER into the current buffer and quote it."
3175   (interactive "bYank buffer: ")
3176   (let ((message-reply-buffer (get-buffer buffer)))
3177     (save-window-excursion
3178       (message-yank-original))))
3179
3180 (defun message-buffers ()
3181   "Return a list of active message buffers."
3182   (let (buffers)
3183     (save-excursion
3184       (dolist (buffer (buffer-list t))
3185         (set-buffer buffer)
3186         (when (and (eq major-mode 'message-mode)
3187                    (null message-sent-message-via))
3188           (push (buffer-name buffer) buffers))))
3189     (nreverse buffers)))
3190
3191 (defun message-cite-original-without-signature ()
3192   "Cite function in the standard Message manner."
3193   (let* ((start (point))
3194          (end (mark t))
3195          (functions
3196           (when message-indent-citation-function
3197             (if (listp message-indent-citation-function)
3198                 message-indent-citation-function
3199               (list message-indent-citation-function))))
3200          ;; This function may be called by `gnus-summary-yank-message' and
3201          ;; may insert a different article from the original.  So, we will
3202          ;; modify the value of `message-reply-headers' with that article.
3203          (message-reply-headers
3204           (save-restriction
3205             (narrow-to-region start end)
3206             (message-narrow-to-head-1)
3207             (vector 0
3208                     (or (message-fetch-field "subject") "none")
3209                     (message-fetch-field "from")
3210                     (message-fetch-field "date")
3211                     (message-fetch-field "message-id" t)
3212                     (message-fetch-field "references")
3213                     0 0 ""))))
3214     (mml-quote-region start end)
3215     ;; Allow undoing.
3216     (undo-boundary)
3217     (goto-char end)
3218     (when (re-search-backward message-signature-separator start t)
3219       ;; Also peel off any blank lines before the signature.
3220       (forward-line -1)
3221       (while (looking-at "^[ \t]*$")
3222         (forward-line -1))
3223       (forward-line 1)
3224       (delete-region (point) end)
3225       (unless (search-backward "\n\n" start t)
3226         ;; Insert a blank line if it is peeled off.
3227         (insert "\n")))
3228     (goto-char start)
3229     (while functions
3230       (funcall (pop functions)))
3231     (when message-citation-line-function
3232       (unless (bolp)
3233         (insert "\n"))
3234       (funcall message-citation-line-function))))
3235
3236 (eval-when-compile (defvar mail-citation-hook)) ;Compiler directive
3237 (defun message-cite-original ()
3238   "Cite function in the standard Message manner."
3239   (if (and (boundp 'mail-citation-hook)
3240            mail-citation-hook)
3241       (run-hooks 'mail-citation-hook)
3242     (let* ((start (point))
3243            (end (mark t))
3244            (x-no-archive nil)
3245            (functions
3246             (when message-indent-citation-function
3247               (if (listp message-indent-citation-function)
3248                   message-indent-citation-function
3249                 (list message-indent-citation-function))))
3250            ;; This function may be called by `gnus-summary-yank-message' and
3251            ;; may insert a different article from the original.  So, we will
3252            ;; modify the value of `message-reply-headers' with that article.
3253            (message-reply-headers
3254             (save-restriction
3255               (narrow-to-region start end)
3256               (message-narrow-to-head-1)
3257               (setq x-no-archive (message-fetch-field "x-no-archive"))
3258               (vector 0
3259                       (or (message-fetch-field "subject") "none")
3260                       (message-fetch-field "from")
3261                       (message-fetch-field "date")
3262                       (message-fetch-field "message-id" t)
3263                       (message-fetch-field "references")
3264                       0 0 ""))))
3265       (mml-quote-region start end)
3266       (goto-char start)
3267       (while functions
3268         (funcall (pop functions)))
3269       (when message-citation-line-function
3270         (unless (bolp)
3271           (insert "\n"))
3272         (funcall message-citation-line-function))
3273       (when (and x-no-archive
3274                  message-cite-articles-with-x-no-archive
3275                  (string-match "yes" x-no-archive))
3276         (undo-boundary)
3277         (delete-region (point) (mark t))
3278         (insert "> [Quoted text removed due to X-No-Archive]\n")
3279         (forward-line -1)))))
3280
3281 (defun message-insert-citation-line ()
3282   "Insert a simple citation line."
3283   (when message-reply-headers
3284     (insert (mail-header-from message-reply-headers) " writes:\n\n")))
3285
3286 (defun message-position-on-field (header &rest afters)
3287   (let ((case-fold-search t))
3288     (save-restriction
3289       (narrow-to-region
3290        (goto-char (point-min))
3291        (progn
3292          (re-search-forward
3293           (concat "^" (regexp-quote mail-header-separator) "$"))
3294          (match-beginning 0)))
3295       (goto-char (point-min))
3296       (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
3297           (progn
3298             (re-search-forward "^[^ \t]" nil 'move)
3299             (beginning-of-line)
3300             (skip-chars-backward "\n")
3301             t)
3302         (while (and afters
3303                     (not (re-search-forward
3304                           (concat "^" (regexp-quote (car afters)) ":")
3305                           nil t)))
3306           (pop afters))
3307         (when afters
3308           (re-search-forward "^[^ \t]" nil 'move)
3309           (beginning-of-line))
3310         (insert header ": \n")
3311         (forward-char -1)
3312         nil))))
3313
3314 (defun message-remove-signature ()
3315   "Remove the signature from the text between point and mark.
3316 The text will also be indented the normal way."
3317   (save-excursion
3318     (let ((start (point))
3319           mark)
3320       (if (not (re-search-forward message-signature-separator (mark t) t))
3321           ;; No signature here, so we just indent the cited text.
3322           (message-indent-citation)
3323         ;; Find the last non-empty line.
3324         (forward-line -1)
3325         (while (looking-at "[ \t]*$")
3326           (forward-line -1))
3327         (forward-line 1)
3328         (setq mark (set-marker (make-marker) (point)))
3329         (goto-char start)
3330         (message-indent-citation)
3331         ;; Enable undoing the deletion.
3332         (undo-boundary)
3333         (delete-region mark (mark t))
3334         (set-marker mark nil)))))
3335
3336 \f
3337
3338 ;;;
3339 ;;; Sending messages
3340 ;;;
3341
3342 (defun message-send-and-exit (&optional arg)
3343   "Send message like `message-send', then, if no errors, exit from mail buffer."
3344   (interactive "P")
3345   (let ((buf (current-buffer))
3346         (actions message-exit-actions))
3347     (when (and (message-send arg)
3348                (buffer-name buf))
3349       (if message-kill-buffer-on-exit
3350           (kill-buffer buf)
3351         (bury-buffer buf)
3352         (when (eq buf (current-buffer))
3353           (message-bury buf)))
3354       (message-do-actions actions)
3355       t)))
3356
3357 (defun message-dont-send ()
3358   "Don't send the message you have been editing.
3359 Instead, just auto-save the buffer and then bury it."
3360   (interactive)
3361   (set-buffer-modified-p t)
3362   (save-buffer)
3363   (let ((actions message-postpone-actions))
3364     (message-bury (current-buffer))
3365     (message-do-actions actions)))
3366
3367 (defun message-kill-buffer ()
3368   "Kill the current buffer."
3369   (interactive)
3370   (when (or (not (buffer-modified-p))
3371             (yes-or-no-p "Message modified; kill anyway? "))
3372     (let ((actions message-kill-actions)
3373           (draft-article message-draft-article)
3374           (auto-save-file-name buffer-auto-save-file-name)
3375           (file-name buffer-file-name)
3376           (modified (buffer-modified-p)))
3377       (setq buffer-file-name nil)
3378       (kill-buffer (current-buffer))
3379       (when (and (or (and auto-save-file-name
3380                           (file-exists-p auto-save-file-name))
3381                      (and file-name
3382                           (file-exists-p file-name)))
3383                (yes-or-no-p (format "Remove the backup file%s? "
3384                                     (if modified " too" ""))))
3385         (ignore-errors
3386           (delete-file auto-save-file-name))
3387         (let ((message-draft-article draft-article))
3388           (message-disassociate-draft)))
3389       (message-do-actions actions))))
3390
3391 (defun message-bury (buffer)
3392   "Bury this mail BUFFER."
3393   (let ((newbuf (other-buffer buffer)))
3394     (bury-buffer buffer)
3395     (if (and (fboundp 'frame-parameters)
3396              (cdr (assq 'dedicated (frame-parameters)))
3397              (not (null (delq (selected-frame) (visible-frame-list)))))
3398         (delete-frame (selected-frame))
3399       (switch-to-buffer newbuf))))
3400
3401 (defun message-send (&optional arg)
3402   "Send the message in the current buffer.
3403 If `message-interactive' is non-nil, wait for success indication or
3404 error messages, and inform user.
3405 Otherwise any failure is reported in a message back to the user from
3406 the mailer.
3407 The usage of ARG is defined by the instance that called Message.
3408 It should typically alter the sending method in some way or other."
3409   (interactive "P")
3410   ;; Make it possible to undo the coming changes.
3411   (undo-boundary)
3412   (let ((inhibit-read-only t))
3413     (put-text-property (point-min) (point-max) 'read-only nil))
3414   (message-fix-before-sending)
3415   (run-hooks 'message-send-hook)
3416   (message message-sending-message)
3417   (let ((alist message-send-method-alist)
3418         (success t)
3419         elem sent dont-barf-on-no-method
3420         (message-options message-options))
3421     (message-options-set-recipient)
3422     (while (and success
3423                 (setq elem (pop alist)))
3424       (when (funcall (cadr elem))
3425         (when (and (or (not (memq (car elem)
3426                                   message-sent-message-via))
3427                        (message-fetch-field "supersedes")
3428                        (if (or (message-gnksa-enable-p 'multiple-copies)
3429                                (not (eq (car elem) 'news)))
3430                            (y-or-n-p
3431                             (format
3432                              "Already sent message via %s; resend? "
3433                              (car elem)))
3434                          (error "Denied posting -- multiple copies")))
3435                    (setq success (funcall (caddr elem) arg)))
3436           (setq sent t))))
3437     (unless (or sent
3438                 (not success)
3439                 (let ((fcc (message-fetch-field "Fcc"))
3440                       (gcc (message-fetch-field "Gcc")))
3441                   (when (or fcc gcc)
3442                     (or (eq message-allow-no-recipients 'always)
3443                         (and (not (eq message-allow-no-recipients 'never))
3444                              (setq dont-barf-on-no-method
3445                                    (gnus-y-or-n-p
3446                                     (format "No receiver, perform %s anyway? "
3447                                             (cond ((and fcc gcc) "Fcc and Gcc")
3448                                                   (fcc "Fcc")
3449                                                   (t "Gcc"))))))))))
3450       (error "No methods specified to send by"))
3451     (when (or dont-barf-on-no-method
3452               (and success sent))
3453       (message-do-fcc)
3454       (save-excursion
3455         (run-hooks 'message-sent-hook))
3456       (message "Sending...done")
3457       ;; Mark the buffer as unmodified and delete auto-save.
3458       (set-buffer-modified-p nil)
3459       (delete-auto-save-file-if-necessary t)
3460       (message-disassociate-draft)
3461       ;; Delete other mail buffers and stuff.
3462       (message-do-send-housekeeping)
3463       (message-do-actions message-send-actions)
3464       ;; Return success.
3465       t)))
3466
3467 (defun message-send-via-mail (arg)
3468   "Send the current message via mail."
3469   (message-send-mail arg))
3470
3471 (defun message-send-via-news (arg)
3472   "Send the current message via news."
3473   (funcall message-send-news-function arg))
3474
3475 (defmacro message-check (type &rest forms)
3476   "Eval FORMS if TYPE is to be checked."
3477   `(or (message-check-element ,type)
3478        (save-excursion
3479          ,@forms)))
3480
3481 (put 'message-check 'lisp-indent-function 1)
3482 (put 'message-check 'edebug-form-spec '(form body))
3483
3484 (defun message-text-with-property (prop &optional start end reverse)
3485   "Return a list of start and end positions where the text has PROP.
3486 START and END bound the search, they default to `point-min' and
3487 `point-max' respectively.  If REVERSE is non-nil, find text which does
3488 not have PROP."
3489   (unless start
3490     (setq start (point-min)))
3491   (unless end
3492     (setq end (point-max)))
3493   (let (next regions)
3494     (if reverse
3495         (while (and start
3496                     (setq start (text-property-any start end prop nil)))
3497           (setq next (next-single-property-change start prop nil end))
3498           (push (cons start (or next end)) regions)
3499           (setq start next))
3500       (while (and start
3501                   (or (get-text-property start prop)
3502                       (and (setq start (next-single-property-change
3503                                         start prop nil end))
3504                            (get-text-property start prop))))
3505         (setq next (text-property-any start end prop nil))
3506         (push (cons start (or next end)) regions)
3507         (setq start next)))
3508     (nreverse regions)))
3509
3510 (defun message-fix-before-sending ()
3511   "Do various things to make the message nice before sending it."
3512   ;; Make sure there's a newline at the end of the message.
3513   (goto-char (point-max))
3514   (unless (bolp)
3515     (insert "\n"))
3516   ;; Make the hidden headers visible.
3517   (dolist (from-to (message-text-with-property 'message-hidden))
3518     (add-text-properties (car from-to) (cdr from-to)
3519                          '(invisible nil intangible nil)))
3520   ;; Make invisible text visible.
3521   ;; It doesn't seem as if this is useful, since the invisible property
3522   ;; is clobbered by an after-change hook anyhow.
3523   (message-check 'invisible-text
3524     (let ((regions (message-text-with-property 'invisible))
3525           from to)
3526       (when regions
3527         (while regions
3528           (setq from (caar regions)
3529                 to (cdar regions)
3530                 regions (cdr regions))
3531           (put-text-property from to 'invisible nil)
3532           (message-overlay-put (message-make-overlay from to)
3533                                'face 'highlight))
3534         (unless (yes-or-no-p
3535                  "Invisible text found and made visible; continue sending? ")
3536           (error "Invisible text found and made visible")))))
3537   (message-check 'illegible-text
3538     (let (found choice)
3539       (message-goto-body)
3540       (skip-chars-forward mm-7bit-chars)
3541       (while (not (eobp))
3542         (when (let ((char (char-after)))
3543                 (or (< (mm-char-int char) 128)
3544                     (and (mm-multibyte-p)
3545                          (memq (char-charset char)
3546                                '(eight-bit-control eight-bit-graphic
3547                                                    control-1))
3548                          (not (get-text-property
3549                                (point) 'untranslated-utf-8)))))
3550           (message-overlay-put (message-make-overlay (point) (1+ (point)))
3551                                'face 'highlight)
3552           (setq found t))
3553         (forward-char)
3554         (skip-chars-forward mm-7bit-chars))
3555       (when found
3556         (setq choice
3557               (gnus-multiple-choice
3558                "Non-printable characters found.  Continue sending?"
3559                '((?d "Remove non-printable characters and send")
3560                  (?r "Replace non-printable characters with dots and send")
3561                  (?i "Ignore non-printable characters and send")
3562                  (?e "Continue editing"))))
3563         (if (eq choice ?e)
3564           (error "Non-printable characters"))
3565         (message-goto-body)
3566         (skip-chars-forward mm-7bit-chars)
3567         (while (not (eobp))
3568           (when (let ((char (char-after)))
3569                   (or (< (mm-char-int char) 128)
3570                       (and (mm-multibyte-p)
3571                            ;; Fixme: Wrong for Emacs 22 and for things
3572                            ;; like undecable utf-8.  Should at least
3573                            ;; use find-coding-systems-region.
3574                            (memq (char-charset char)
3575                                  '(eight-bit-control eight-bit-graphic
3576                                                      control-1))
3577                            (not (get-text-property
3578                                  (point) 'untranslated-utf-8)))))
3579             (if (eq choice ?i)
3580                 (message-kill-all-overlays)
3581               (delete-char 1)
3582               (when (eq choice ?r)
3583                 (insert "."))))
3584           (forward-char)
3585           (skip-chars-forward mm-7bit-chars))))))
3586
3587 (defun message-add-action (action &rest types)
3588   "Add ACTION to be performed when doing an exit of type TYPES."
3589   (while types
3590     (add-to-list (intern (format "message-%s-actions" (pop types)))
3591                  action)))
3592
3593 (defun message-delete-action (action &rest types)
3594   "Delete ACTION from lists of actions performed when doing an exit of type TYPES."
3595   (let (var)
3596     (while types
3597       (set (setq var (intern (format "message-%s-actions" (pop types))))
3598            (delq action (symbol-value var))))))
3599
3600 (defun message-do-actions (actions)
3601   "Perform all actions in ACTIONS."
3602   ;; Now perform actions on successful sending.
3603   (while actions
3604     (ignore-errors
3605       (cond
3606        ;; A simple function.
3607        ((functionp (car actions))
3608         (funcall (car actions)))
3609        ;; Something to be evaled.
3610        (t
3611         (eval (car actions)))))
3612     (pop actions)))
3613
3614 (defun message-send-mail-partially ()
3615   "Send mail as message/partial."
3616   ;; replace the header delimiter with a blank line
3617   (goto-char (point-min))
3618   (re-search-forward
3619    (concat "^" (regexp-quote mail-header-separator) "\n"))
3620   (replace-match "\n")
3621   (run-hooks 'message-send-mail-hook)
3622   (let ((p (goto-char (point-min)))
3623         (tembuf (message-generate-new-buffer-clone-locals " message temp"))
3624         (curbuf (current-buffer))
3625         (id (message-make-message-id)) (n 1)
3626         plist total  header required-mail-headers)
3627     (while (not (eobp))
3628       (if (< (point-max) (+ p message-send-mail-partially-limit))
3629           (goto-char (point-max))
3630         (goto-char (+ p message-send-mail-partially-limit))
3631         (beginning-of-line)
3632         (if (<= (point) p) (forward-line 1))) ;; In case of bad message.
3633       (push p plist)
3634       (setq p (point)))
3635     (setq total (length plist))
3636     (push (point-max) plist)
3637     (setq plist (nreverse plist))
3638     (unwind-protect
3639         (save-excursion
3640           (setq p (pop plist))
3641           (while plist
3642             (set-buffer curbuf)
3643             (copy-to-buffer tembuf p (car plist))
3644             (set-buffer tembuf)
3645             (goto-char (point-min))
3646             (if header
3647                 (progn
3648                   (goto-char (point-min))
3649                   (narrow-to-region (point) (point))
3650                   (insert header))
3651               (message-goto-eoh)
3652               (setq header (buffer-substring (point-min) (point)))
3653               (goto-char (point-min))
3654               (narrow-to-region (point) (point))
3655               (insert header)
3656               (message-remove-header "Mime-Version")
3657               (message-remove-header "Content-Type")
3658               (message-remove-header "Content-Transfer-Encoding")
3659               (message-remove-header "Message-ID")
3660               (message-remove-header "Lines")
3661               (goto-char (point-max))
3662               (insert "Mime-Version: 1.0\n")
3663               (setq header (buffer-string)))
3664             (goto-char (point-max))
3665             (insert (format "Content-Type: message/partial; id=\"%s\"; number=%d; total=%d\n\n"
3666                             id n total))
3667             (forward-char -1)
3668             (let ((mail-header-separator ""))
3669               (when (memq 'Message-ID message-required-mail-headers)
3670                 (insert "Message-ID: " (message-make-message-id) "\n"))
3671               (when (memq 'Lines message-required-mail-headers)
3672                 (insert "Lines: " (message-make-lines) "\n"))
3673               (message-goto-subject)
3674               (end-of-line)
3675               (insert (format " (%d/%d)" n total))
3676               (widen)
3677               (mm-with-unibyte-current-buffer
3678                 (funcall (or message-send-mail-real-function
3679                              message-send-mail-function))))
3680             (setq n (+ n 1))
3681             (setq p (pop plist))
3682             (erase-buffer)))
3683       (kill-buffer tembuf))))
3684
3685 (defun message-send-mail (&optional arg)
3686   (require 'mail-utils)
3687   (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
3688          (case-fold-search nil)
3689          (news (message-news-p))
3690          (mailbuf (current-buffer))
3691          (message-this-is-mail t)
3692          (message-posting-charset
3693           (if (fboundp 'gnus-setup-posting-charset)
3694               (gnus-setup-posting-charset nil)
3695             message-posting-charset))
3696          (headers message-required-mail-headers))
3697     (save-restriction
3698       (message-narrow-to-headers)
3699       ;; Generate the Mail-Followup-To header if the header is not there...
3700       (if (and (message-subscribed-p)
3701                (not (mail-fetch-field "mail-followup-to")))
3702           (setq headers
3703                 (cons
3704                  (cons "Mail-Followup-To" (message-make-mail-followup-to))
3705                  message-required-mail-headers))
3706         ;; otherwise, delete the MFT header if the field is empty
3707         (when (equal "" (mail-fetch-field "mail-followup-to"))
3708           (message-remove-header "^Mail-Followup-To:")))
3709       ;; Insert some headers.
3710       (let ((message-deletable-headers
3711              (if news nil message-deletable-headers)))
3712         (message-generate-headers headers))
3713       ;; Let the user do all of the above.
3714       (run-hooks 'message-header-hook))
3715     (unwind-protect
3716         (save-excursion
3717           (set-buffer tembuf)
3718           (erase-buffer)
3719           ;; Avoid copying text props (except hard newlines).
3720           (insert (with-current-buffer mailbuf
3721                     (mml-buffer-substring-no-properties-except-hard-newlines
3722                      (point-min) (point-max))))
3723           ;; Remove some headers.
3724           (message-encode-message-body)
3725           (save-restriction
3726             (message-narrow-to-headers)
3727             ;; We (re)generate the Lines header.
3728             (when (memq 'Lines message-required-mail-headers)
3729               (message-generate-headers '(Lines)))
3730             ;; Remove some headers.
3731             (message-remove-header message-ignored-mail-headers t)
3732             (let ((mail-parse-charset message-default-charset))
3733               (mail-encode-encoded-word-buffer)))
3734           (goto-char (point-max))
3735           ;; require one newline at the end.
3736           (or (= (preceding-char) ?\n)
3737               (insert ?\n))
3738           (message-cleanup-headers)
3739           ;; FIXME: we're inserting the courtesy copy after encoding.
3740           ;; This is wrong if the courtesy copy string contains
3741           ;; non-ASCII characters. -- jh
3742           (when
3743               (save-restriction
3744                 (message-narrow-to-headers)
3745                 (and news
3746                      (or (message-fetch-field "cc")
3747                          (message-fetch-field "bcc")
3748                          (message-fetch-field "to"))
3749                      (let ((content-type (message-fetch-field
3750                                           "content-type")))
3751                        (and
3752                         (or
3753                          (not content-type)
3754                          (string= "text/plain"
3755                                   (car
3756                                    (mail-header-parse-content-type
3757                                     content-type))))
3758                         (not
3759                          (string= "base64"
3760                                   (message-fetch-field
3761                                    "content-transfer-encoding")))))))
3762             (message-insert-courtesy-copy))
3763           (if (or (not message-send-mail-partially-limit)
3764                   (< (point-max) message-send-mail-partially-limit)
3765                   (not (message-y-or-n-p
3766                         "The message size is too large, split? "
3767                         t
3768                         "\
3769 The message size, "
3770                         (/ (point-max) 1000) "KB, is too large.
3771
3772 Some mail gateways (MTA's) bounce large messages.  To avoid the
3773 problem, answer `y', and the message will be split into several
3774 smaller pieces, the size of each is about "
3775                         (/ message-send-mail-partially-limit 1000)
3776                         "KB except the last
3777 one.
3778
3779 However, some mail readers (MUA's) can't read split messages, i.e.,
3780 mails in message/partially format. Answer `n', and the message will be
3781 sent in one piece.
3782
3783 The size limit is controlled by `message-send-mail-partially-limit'.
3784 If you always want Gnus to send messages in one piece, set
3785 `message-send-mail-partially-limit' to nil.
3786 ")))
3787               (mm-with-unibyte-current-buffer
3788                 (message "Sending via mail...")
3789                 (funcall (or message-send-mail-real-function
3790                              message-send-mail-function)))
3791             (message-send-mail-partially)))
3792       (kill-buffer tembuf))
3793     (set-buffer mailbuf)
3794     (push 'mail message-sent-message-via)))
3795
3796 (defun message-send-mail-with-sendmail ()
3797   "Send off the prepared buffer with sendmail."
3798   (let ((errbuf (if message-interactive
3799                     (message-generate-new-buffer-clone-locals
3800                      " sendmail errors")
3801                   0))
3802         resend-to-addresses delimline)
3803     (unwind-protect
3804         (progn
3805           (let ((case-fold-search t))
3806             (save-restriction
3807               (message-narrow-to-headers)
3808               (setq resend-to-addresses (message-fetch-field "resent-to")))
3809             ;; Change header-delimiter to be what sendmail expects.
3810             (goto-char (point-min))
3811             (re-search-forward
3812              (concat "^" (regexp-quote mail-header-separator) "\n"))
3813             (replace-match "\n")
3814             (backward-char 1)
3815             (setq delimline (point-marker))
3816             (run-hooks 'message-send-mail-hook)
3817             ;; Insert an extra newline if we need it to work around
3818             ;; Sun's bug that swallows newlines.
3819             (goto-char (1+ delimline))
3820             (when (eval message-mailer-swallows-blank-line)
3821               (newline))
3822             (when message-interactive
3823               (with-current-buffer errbuf
3824                 (erase-buffer))))
3825           (let* ((default-directory "/")
3826                  (coding-system-for-write message-send-coding-system)
3827                  (cpr (apply
3828                        'call-process-region
3829                        (append
3830                         (list (point-min) (point-max)
3831                               (if (boundp 'sendmail-program)
3832                                   sendmail-program
3833                                 "/usr/lib/sendmail")
3834                               nil errbuf nil "-oi")
3835                         ;; Always specify who from,
3836                         ;; since some systems have broken sendmails.
3837                         ;; But some systems are more broken with -f, so
3838                         ;; we'll let users override this.
3839                         (if (null message-sendmail-f-is-evil)
3840                             (list "-f" (message-sendmail-envelope-from)))
3841                         ;; These mean "report errors by mail"
3842                         ;; and "deliver in background".
3843                         (if (null message-interactive) '("-oem" "-odb"))
3844                         ;; Get the addresses from the message
3845                         ;; unless this is a resend.
3846                         ;; We must not do that for a resend
3847                         ;; because we would find the original addresses.
3848                         ;; For a resend, include the specific addresses.
3849                         (if resend-to-addresses
3850                             (list resend-to-addresses)
3851                           '("-t"))))))
3852             (unless (or (null cpr) (and (numberp cpr) (zerop cpr)))
3853               (error "Sending...failed with exit value %d" cpr)))
3854           (when message-interactive
3855             (save-excursion
3856               (set-buffer errbuf)
3857               (goto-char (point-min))
3858               (while (re-search-forward "\n\n* *" nil t)
3859                 (replace-match "; "))
3860               (if (not (zerop (buffer-size)))
3861                   (error "Sending...failed to %s"
3862                          (buffer-string))))))
3863       (when (bufferp errbuf)
3864         (kill-buffer errbuf)))))
3865
3866 (defun message-send-mail-with-qmail ()
3867   "Pass the prepared message buffer to qmail-inject.
3868 Refer to the documentation for the variable `message-send-mail-function'
3869 to find out how to use this."
3870   ;; replace the header delimiter with a blank line
3871   (goto-char (point-min))
3872   (re-search-forward
3873    (concat "^" (regexp-quote mail-header-separator) "\n"))
3874   (replace-match "\n")
3875   (run-hooks 'message-send-mail-hook)
3876   ;; send the message
3877   (case
3878       (let ((coding-system-for-write message-send-coding-system))
3879         (apply
3880          'call-process-region 1 (point-max) message-qmail-inject-program
3881          nil nil nil
3882          ;; qmail-inject's default behaviour is to look for addresses on the
3883          ;; command line; if there're none, it scans the headers.
3884          ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
3885          ;;
3886          ;; in general, ALL of qmail-inject's defaults are perfect for simply
3887          ;; reading a formatted (i. e., at least a To: or Resent-To header)
3888          ;; message from stdin.
3889          ;;
3890          ;; qmail also has the advantage of not having been raped by
3891          ;; various vendors, so we don't have to allow for that, either --
3892          ;; compare this with message-send-mail-with-sendmail and weep
3893          ;; for sendmail's lost innocence.
3894          ;;
3895          ;; all this is way cool coz it lets us keep the arguments entirely
3896          ;; free for -inject-arguments -- a big win for the user and for us
3897          ;; since we don't have to play that double-guessing game and the user
3898          ;; gets full control (no gestapo'ish -f's, for instance).  --sj
3899          (if (functionp message-qmail-inject-args)
3900              (funcall message-qmail-inject-args)
3901            message-qmail-inject-args)))
3902     ;; qmail-inject doesn't say anything on it's stdout/stderr,
3903     ;; we have to look at the retval instead
3904     (0 nil)
3905     (100 (error "qmail-inject reported permanent failure"))
3906     (111 (error "qmail-inject reported transient failure"))
3907     ;; should never happen
3908     (t   (error "qmail-inject reported unknown failure"))))
3909
3910 (defun message-send-mail-with-mh ()
3911   "Send the prepared message buffer with mh."
3912   (let ((mh-previous-window-config nil)
3913         (name (mh-new-draft-name)))
3914     (setq buffer-file-name name)
3915     ;; MH wants to generate these headers itself.
3916     (when message-mh-deletable-headers
3917       (let ((headers message-mh-deletable-headers))
3918         (while headers
3919           (goto-char (point-min))
3920           (and (re-search-forward
3921                 (concat "^" (symbol-name (car headers)) ": *") nil t)
3922                (message-delete-line))
3923           (pop headers))))
3924     (run-hooks 'message-send-mail-hook)
3925     ;; Pass it on to mh.
3926     (mh-send-letter)))
3927
3928 (defun message-smtpmail-send-it ()
3929   "Send the prepared message buffer with `smtpmail-send-it'.
3930 This only differs from `smtpmail-send-it' that this command evaluates
3931 `message-send-mail-hook' just before sending a message.  It is useful
3932 if your ISP requires the POP-before-SMTP authentication.  See the Gnus
3933 manual for details."
3934   (run-hooks 'message-send-mail-hook)
3935   (smtpmail-send-it))
3936
3937 (defun message-canlock-generate ()
3938   "Return a string that is non-trivial to guess.
3939 Do not use this for anything important, it is cryptographically weak."
3940   (require 'sha1-el)
3941   (let (sha1-maximum-internal-length)
3942     (sha1 (concat (message-unique-id)
3943                   (format "%x%x%x" (random) (random t) (random))
3944                   (prin1-to-string (recent-keys))
3945                   (prin1-to-string (garbage-collect))))))
3946
3947 (defun message-canlock-password ()
3948   "The password used by message for cancel locks.
3949 This is the value of `canlock-password', if that option is non-nil.
3950 Otherwise, generate and save a value for `canlock-password' first."
3951   (unless canlock-password
3952     (customize-save-variable 'canlock-password (message-canlock-generate))
3953     (setq canlock-password-for-verify canlock-password))
3954   canlock-password)
3955
3956 (defun message-insert-canlock ()
3957   (when message-insert-canlock
3958     (message-canlock-password)
3959     (canlock-insert-header)))
3960
3961 (defun message-send-news (&optional arg)
3962   (let* ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
3963          (case-fold-search nil)
3964          (method (if (functionp message-post-method)
3965                      (funcall message-post-method arg)
3966                    message-post-method))
3967          (newsgroups-field (save-restriction
3968                             (message-narrow-to-headers-or-head)
3969                             (message-fetch-field "Newsgroups")))
3970          (followup-field (save-restriction
3971                            (message-narrow-to-headers-or-head)
3972                            (message-fetch-field "Followup-To")))
3973          ;; BUG: We really need to get the charset for each name in the
3974          ;; Newsgroups and Followup-To lines to allow crossposting
3975          ;; between group namess with incompatible character sets.
3976          ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
3977          (group-field-charset
3978           (gnus-group-name-charset method newsgroups-field))
3979          (followup-field-charset
3980           (gnus-group-name-charset method (or followup-field "")))
3981          (rfc2047-header-encoding-alist
3982           (append (when group-field-charset
3983                     (list (cons "Newsgroups" group-field-charset)))
3984                   (when followup-field-charset
3985                     (list (cons "Followup-To" followup-field-charset)))
3986                   rfc2047-header-encoding-alist))
3987          (messbuf (current-buffer))
3988          (message-syntax-checks
3989           (if (and arg
3990                    (listp message-syntax-checks))
3991               (cons '(existing-newsgroups . disabled)
3992                     message-syntax-checks)
3993             message-syntax-checks))
3994          (message-this-is-news t)
3995          (message-posting-charset
3996           (gnus-setup-posting-charset newsgroups-field))
3997          result)
3998     (if (not (message-check-news-body-syntax))
3999         nil
4000       (save-restriction
4001         (message-narrow-to-headers)
4002         ;; Insert some headers.
4003         (message-generate-headers message-required-news-headers)
4004         (message-insert-canlock)
4005         ;; Let the user do all of the above.
4006         (run-hooks 'message-header-hook))
4007       ;; Note: This check will be disabled by the ".*" default value for
4008       ;; gnus-group-name-charset-group-alist. -- Pa 2001-10-07.
4009       (when (and group-field-charset
4010                  (listp message-syntax-checks))
4011         (setq message-syntax-checks
4012               (cons '(valid-newsgroups . disabled)
4013                     message-syntax-checks)))
4014       (message-cleanup-headers)
4015       (if (not (let ((message-post-method method))
4016                  (message-check-news-syntax)))
4017           nil
4018         (unwind-protect
4019             (save-excursion
4020               (set-buffer tembuf)
4021               (buffer-disable-undo)
4022               (erase-buffer)
4023               ;; Avoid copying text props (except hard newlines).
4024               (insert
4025                (with-current-buffer messbuf
4026                  (mml-buffer-substring-no-properties-except-hard-newlines
4027                   (point-min) (point-max))))
4028               (message-encode-message-body)
4029               ;; Remove some headers.
4030               (save-restriction
4031                 (message-narrow-to-headers)
4032                 ;; We (re)generate the Lines header.
4033                 (when (memq 'Lines message-required-mail-headers)
4034                   (message-generate-headers '(Lines)))
4035                 ;; Remove some headers.
4036                 (message-remove-header message-ignored-news-headers t)
4037                 (let ((mail-parse-charset message-default-charset))
4038                   (mail-encode-encoded-word-buffer)))
4039               (goto-char (point-max))
4040               ;; require one newline at the end.
4041               (or (= (preceding-char) ?\n)
4042                   (insert ?\n))
4043               (let ((case-fold-search t))
4044                 ;; Remove the delimiter.
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               (run-hooks 'message-send-news-hook)
4051               (gnus-open-server method)
4052               (message "Sending news via %s..." (gnus-server-string method))
4053               (setq result (let ((mail-header-separator ""))
4054                              (gnus-request-post method))))
4055           (kill-buffer tembuf))
4056         (set-buffer messbuf)
4057         (if result
4058             (push 'news message-sent-message-via)
4059           (message "Couldn't send message via news: %s"
4060                    (nnheader-get-report (car method)))
4061           nil)))))
4062
4063 ;;;
4064 ;;; Header generation & syntax checking.
4065 ;;;
4066
4067 (defun message-check-element (type)
4068   "Return non-nil if this TYPE is not to be checked."
4069   (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
4070       t
4071     (let ((able (assq type message-syntax-checks)))
4072       (and (consp able)
4073            (eq (cdr able) 'disabled)))))
4074
4075 (defun message-check-news-syntax ()
4076   "Check the syntax of the message."
4077   (save-excursion
4078     (save-restriction
4079       (widen)
4080       ;; We narrow to the headers and check them first.
4081       (save-excursion
4082         (save-restriction
4083           (message-narrow-to-headers)
4084           (message-check-news-header-syntax))))))
4085
4086 (defun message-check-news-header-syntax ()
4087   (and
4088    ;; Check Newsgroups header.
4089    (message-check 'newsgroups
4090      (let ((group (message-fetch-field "newsgroups")))
4091        (or
4092         (and group
4093              (not (string-match "\\`[ \t]*\\'" group)))
4094         (ignore
4095          (message
4096           "The newsgroups field is empty or missing.  Posting is denied.")))))
4097    ;; Check the Subject header.
4098    (message-check 'subject
4099      (let* ((case-fold-search t)
4100             (subject (message-fetch-field "subject")))
4101        (or
4102         (and subject
4103              (not (string-match "\\`[ \t]*\\'" subject)))
4104         (ignore
4105          (message
4106           "The subject field is empty or missing.  Posting is denied.")))))
4107    ;; Check for commands in Subject.
4108    (message-check 'subject-cmsg
4109      (if (string-match "^cmsg " (message-fetch-field "subject"))
4110          (y-or-n-p
4111           "The control code \"cmsg\" is in the subject.  Really post? ")
4112        t))
4113    ;; Check long header lines.
4114    (message-check 'long-header-lines
4115      (let ((start (point))
4116            (header nil)
4117            (length 0)
4118            found)
4119        (while (and (not found)
4120                    (re-search-forward "^\\([^ \t:]+\\): " nil t))
4121          (if (> (- (point) (match-beginning 0)) 998)
4122              (setq found t
4123                    length (- (point) (match-beginning 0)))
4124            (setq header (match-string-no-properties 1)))
4125          (setq start (match-beginning 0))
4126          (forward-line 1))
4127        (if found
4128            (y-or-n-p (format "Your %s header is too long (%d).  Really post? "
4129                              header length))
4130          t)))
4131    ;; Check for multiple identical headers.
4132    (message-check 'multiple-headers
4133      (let (found)
4134        (while (and (not found)
4135                    (re-search-forward "^[^ \t:]+: " nil t))
4136          (save-excursion
4137            (or (re-search-forward
4138                 (concat "^"
4139                         (regexp-quote
4140                          (setq found
4141                                (buffer-substring
4142                                 (match-beginning 0) (- (match-end 0) 2))))
4143                         ":")
4144                 nil t)
4145                (setq found nil))))
4146        (if found
4147            (y-or-n-p (format "Multiple %s headers.  Really post? " found))
4148          t)))
4149    ;; Check for Version and Sendsys.
4150    (message-check 'sendsys
4151      (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
4152          (y-or-n-p
4153           (format "The article contains a %s command.  Really post? "
4154                   (buffer-substring (match-beginning 0)
4155                                     (1- (match-end 0)))))
4156        t))
4157    ;; See whether we can shorten Followup-To.
4158    (message-check 'shorten-followup-to
4159      (let ((newsgroups (message-fetch-field "newsgroups"))
4160            (followup-to (message-fetch-field "followup-to"))
4161            to)
4162        (when (and newsgroups
4163                   (string-match "," newsgroups)
4164                   (not followup-to)
4165                   (not
4166                    (zerop
4167                     (length
4168                      (setq to (completing-read
4169                                "Followups to (default: no Followup-To header) "
4170                                (mapcar #'list
4171                                        (cons "poster"
4172                                              (message-tokenize-header
4173                                               newsgroups)))))))))
4174          (goto-char (point-min))
4175          (insert "Followup-To: " to "\n"))
4176        t))
4177    ;; Check "Shoot me".
4178    (message-check 'shoot
4179      (if (re-search-forward
4180           "Message-ID.*.i-did-not-set--mail-host-address--so-tickle-me" nil t)
4181          (y-or-n-p "You appear to have a misconfigured system.  Really post? ")
4182        t))
4183    ;; Check for Approved.
4184    (message-check 'approved
4185      (if (re-search-forward "^Approved:" nil t)
4186          (y-or-n-p "The article contains an Approved header.  Really post? ")
4187        t))
4188    ;; Check the Message-ID header.
4189    (message-check 'message-id
4190      (let* ((case-fold-search t)
4191             (message-id (message-fetch-field "message-id" t)))
4192        (or (not message-id)
4193            ;; Is there an @ in the ID?
4194            (and (string-match "@" message-id)
4195                 ;; Is there a dot in the ID?
4196                 (string-match "@[^.]*\\." message-id)
4197                 ;; Does the ID end with a dot?
4198                 (not (string-match "\\.>" message-id)))
4199            (y-or-n-p
4200             (format "The Message-ID looks strange: \"%s\".  Really post? "
4201                     message-id)))))
4202    ;; Check the Newsgroups & Followup-To headers.
4203    (message-check 'existing-newsgroups
4204      (let* ((case-fold-search t)
4205             (newsgroups (message-fetch-field "newsgroups"))
4206             (followup-to (message-fetch-field "followup-to"))
4207             (groups (message-tokenize-header
4208                      (if followup-to
4209                          (concat newsgroups "," followup-to)
4210                        newsgroups)))
4211             (post-method (if (functionp message-post-method)
4212                              (funcall message-post-method)
4213                            message-post-method))
4214             ;; KLUDGE to handle nnvirtual groups.  Doing this right
4215             ;; would probably involve a new nnoo function.
4216             ;; -- Per Abrahamsen <abraham@dina.kvl.dk>, 2001-10-17.
4217             (method (if (and (consp post-method)
4218                              (eq (car post-method) 'nnvirtual)
4219                              gnus-message-group-art)
4220                         (let ((group (car (nnvirtual-find-group-art
4221                                            (car gnus-message-group-art)
4222                                            (cdr gnus-message-group-art)))))
4223                           (gnus-find-method-for-group group))
4224                       post-method))
4225             (known-groups
4226              (mapcar (lambda (n)
4227                        (gnus-group-name-decode
4228                         (gnus-group-real-name n)
4229                         (gnus-group-name-charset method n)))
4230                      (gnus-groups-from-server method)))
4231             errors)
4232        (while groups
4233          (when (and (not (equal (car groups) "poster"))
4234                     (not (member (car groups) known-groups))
4235                     (not (member (car groups) errors)))
4236            (push (car groups) errors))
4237          (pop groups))
4238        (cond
4239         ;; Gnus is not running.
4240         ((or (not (and (boundp 'gnus-active-hashtb)
4241                        gnus-active-hashtb))
4242              (not (boundp 'gnus-read-active-file)))
4243          t)
4244         ;; We don't have all the group names.
4245         ((and (or (not gnus-read-active-file)
4246                   (eq gnus-read-active-file 'some))
4247               errors)
4248          (y-or-n-p
4249           (format
4250            "Really use %s possibly unknown group%s: %s? "
4251            (if (= (length errors) 1) "this" "these")
4252            (if (= (length errors) 1) "" "s")
4253            (mapconcat 'identity errors ", "))))
4254         ;; There were no errors.
4255         ((not errors)
4256          t)
4257         ;; There are unknown groups.
4258         (t
4259          (y-or-n-p
4260           (format
4261            "Really post to %s unknown group%s: %s? "
4262            (if (= (length errors) 1) "this" "these")
4263            (if (= (length errors) 1) "" "s")
4264            (mapconcat 'identity errors ", ")))))))
4265    ;; Check continuation headers.
4266    (message-check 'continuation-headers
4267      (goto-char (point-min))
4268      (let ((do-posting t))
4269        (while (re-search-forward "^[^ \t\n][^:\n]*$" nil t)
4270          (if (y-or-n-p "Fix continuation lines? ")
4271              (progn
4272                (goto-char (match-beginning 0))
4273                (insert " "))
4274            (unless (y-or-n-p "Send anyway? ")
4275              (setq do-posting nil))))
4276        do-posting))
4277    ;; Check the Newsgroups & Followup-To headers for syntax errors.
4278    (message-check 'valid-newsgroups
4279      (let ((case-fold-search t)
4280            (headers '("Newsgroups" "Followup-To"))
4281            header error)
4282        (while (and headers (not error))
4283          (when (setq header (mail-fetch-field (car headers)))
4284            (if (or
4285                 (not
4286                  (string-match
4287                   "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-+_&.a-zA-Z0-9]+\\)*\\'"
4288                   header))
4289                 (memq
4290                  nil (mapcar
4291                       (lambda (g)
4292                         (not (string-match "\\.\\'\\|\\.\\." g)))
4293                       (message-tokenize-header header ","))))
4294                (setq error t)))
4295          (unless error
4296            (pop headers)))
4297        (if (not error)
4298            t
4299          (y-or-n-p
4300           (format "The %s header looks odd: \"%s\".  Really post? "
4301                   (car headers) header)))))
4302    (message-check 'repeated-newsgroups
4303      (let ((case-fold-search t)
4304            (headers '("Newsgroups" "Followup-To"))
4305            header error groups group)
4306        (while (and headers
4307                    (not error))
4308          (when (setq header (mail-fetch-field (pop headers)))
4309            (setq groups (message-tokenize-header header ","))
4310            (while (setq group (pop groups))
4311              (when (member group groups)
4312                (setq error group
4313                      groups nil)))))
4314        (if (not error)
4315            t
4316          (y-or-n-p
4317           (format "Group %s is repeated in headers.  Really post? " error)))))
4318    ;; Check the From header.
4319    (message-check 'from
4320      (let* ((case-fold-search t)
4321             (from (message-fetch-field "from"))
4322             ad)
4323        (cond
4324         ((not from)
4325          (message "There is no From line.  Posting is denied.")
4326          nil)
4327         ((or (not (string-match
4328                    "@[^\\.]*\\."
4329                    (setq ad (nth 1 (mail-extract-address-components
4330                                     from))))) ;larsi@ifi
4331              (string-match "\\.\\." ad) ;larsi@ifi..uio
4332              (string-match "@\\." ad)   ;larsi@.ifi.uio
4333              (string-match "\\.$" ad)   ;larsi@ifi.uio.
4334              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
4335              (string-match "(.*).*(.*)" from)) ;(lars) (lars)
4336          (message
4337           "Denied posting -- the From looks strange: \"%s\"." from)
4338          nil)
4339         ((let ((addresses (rfc822-addresses from)))
4340            (while (and addresses
4341                        (not (eq (string-to-char (car addresses)) ?\()))
4342              (setq addresses (cdr addresses)))
4343            addresses)
4344          (message
4345           "Denied posting -- bad From address: \"%s\"." from)
4346          nil)
4347         (t t))))
4348    ;; Check the Reply-To header.
4349    (message-check 'reply-to
4350      (let* ((case-fold-search t)
4351             (reply-to (message-fetch-field "reply-to"))
4352             ad)
4353        (cond
4354         ((not reply-to)
4355          t)
4356         ((string-match "," reply-to)
4357          (y-or-n-p
4358           (format "Multiple Reply-To addresses: \"%s\". Really post? "
4359                   reply-to)))
4360         ((or (not (string-match
4361                    "@[^\\.]*\\."
4362                    (setq ad (nth 1 (mail-extract-address-components
4363                                     reply-to))))) ;larsi@ifi
4364              (string-match "\\.\\." ad) ;larsi@ifi..uio
4365              (string-match "@\\." ad)   ;larsi@.ifi.uio
4366              (string-match "\\.$" ad)   ;larsi@ifi.uio.
4367              (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
4368              (string-match "(.*).*(.*)" reply-to)) ;(lars) (lars)
4369          (y-or-n-p
4370           (format
4371            "The Reply-To looks strange: \"%s\". Really post? "
4372            reply-to)))
4373         (t t))))))
4374
4375 (defun message-check-news-body-syntax ()
4376   (and
4377    ;; Check for long lines.
4378    (message-check 'long-lines
4379      (goto-char (point-min))
4380      (re-search-forward
4381       (concat "^" (regexp-quote mail-header-separator) "$"))
4382      (forward-line 1)
4383      (while (and
4384              (or (looking-at
4385                   "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)")
4386                  (let ((p (point)))
4387                    (end-of-line)
4388                    (< (- (point) p) 80)))
4389              (zerop (forward-line 1))))
4390      (or (bolp)
4391          (eobp)
4392          (y-or-n-p
4393           "You have lines longer than 79 characters.  Really post? ")))
4394    ;; Check whether the article is empty.
4395    (message-check 'empty
4396      (goto-char (point-min))
4397      (re-search-forward
4398       (concat "^" (regexp-quote mail-header-separator) "$"))
4399      (forward-line 1)
4400      (let ((b (point)))
4401        (goto-char (point-max))
4402        (re-search-backward message-signature-separator nil t)
4403        (beginning-of-line)
4404        (or (re-search-backward "[^ \n\t]" b t)
4405            (if (message-gnksa-enable-p 'empty-article)
4406                (y-or-n-p "Empty article.  Really post? ")
4407              (message "Denied posting -- Empty article.")
4408              nil))))
4409    ;; Check for control characters.
4410    (message-check 'control-chars
4411      (if (re-search-forward "[\000-\007\013\015-\032\034-\037\200-\237]" nil t)
4412          (y-or-n-p
4413           "The article contains control characters.  Really post? ")
4414        t))
4415    ;; Check excessive size.
4416    (message-check 'size
4417      (if (> (buffer-size) 60000)
4418          (y-or-n-p
4419           (format "The article is %d octets long.  Really post? "
4420                   (buffer-size)))
4421        t))
4422    ;; Check whether any new text has been added.
4423    (message-check 'new-text
4424      (or
4425       (not message-checksum)
4426       (not (eq (message-checksum) message-checksum))
4427       (if (message-gnksa-enable-p 'quoted-text-only)
4428           (y-or-n-p
4429            "It looks like no new text has been added.  Really post? ")
4430         (message "Denied posting -- no new text has been added.")
4431         nil)))
4432    ;; Check the length of the signature.
4433    (message-check 'signature
4434      (goto-char (point-max))
4435      (if (> (count-lines (point) (point-max)) 5)
4436          (y-or-n-p
4437           (format
4438            "Your .sig is %d lines; it should be max 4.  Really post? "
4439            (1- (count-lines (point) (point-max)))))
4440        t))
4441    ;; Ensure that text follows last quoted portion.
4442    (message-check 'quoting-style
4443      (goto-char (point-max))
4444      (let ((no-problem t))
4445        (when (search-backward-regexp "^>[^\n]*\n" nil t)
4446          (setq no-problem (search-forward-regexp "^[ \t]*[^>\n]" nil t)))
4447        (if no-problem
4448            t
4449          (if (message-gnksa-enable-p 'quoted-text-only)
4450              (y-or-n-p "Your text should follow quoted text.  Really post? ")
4451            ;; Ensure that
4452            (goto-char (point-min))
4453            (re-search-forward
4454             (concat "^" (regexp-quote mail-header-separator) "$"))
4455            (if (search-forward-regexp "^[ \t]*[^>\n]" nil t)
4456                (y-or-n-p "Your text should follow quoted text.  Really post? ")
4457              (message "Denied posting -- only quoted text.")
4458              nil)))))))
4459
4460 (defun message-checksum ()
4461   "Return a \"checksum\" for the current buffer."
4462   (let ((sum 0))
4463     (save-excursion
4464       (goto-char (point-min))
4465       (re-search-forward
4466        (concat "^" (regexp-quote mail-header-separator) "$"))
4467       (while (not (eobp))
4468         (when (not (looking-at "[ \t\n]"))
4469           (setq sum (logxor (ash sum 1) (if (natnump sum) 0 1)
4470                             (char-after))))
4471         (forward-char 1)))
4472     sum))
4473
4474 (defun message-do-fcc ()
4475   "Process Fcc headers in the current buffer."
4476   (let ((case-fold-search t)
4477         (buf (current-buffer))
4478         list file
4479         (mml-externalize-attachments message-fcc-externalize-attachments))
4480     (save-excursion
4481       (save-restriction
4482         (message-narrow-to-headers)
4483         (setq file (message-fetch-field "fcc" t)))
4484       (when file
4485         (set-buffer (get-buffer-create " *message temp*"))
4486         (erase-buffer)
4487         (insert-buffer-substring buf)
4488         (message-encode-message-body)
4489         (save-restriction
4490           (message-narrow-to-headers)
4491           (while (setq file (message-fetch-field "fcc" t))
4492             (push file list)
4493             (message-remove-header "fcc" nil t))
4494           (let ((mail-parse-charset message-default-charset)
4495                 (rfc2047-header-encoding-alist
4496                  (cons '("Newsgroups" . default)
4497                        rfc2047-header-encoding-alist)))
4498             (mail-encode-encoded-word-buffer)))
4499         (goto-char (point-min))
4500         (when (re-search-forward
4501                (concat "^" (regexp-quote mail-header-separator) "$")
4502                nil t)
4503           (replace-match "" t t ))
4504         ;; Process FCC operations.
4505         (while list
4506           (setq file (pop list))
4507           (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
4508               ;; Pipe the article to the program in question.
4509               (call-process-region (point-min) (point-max) shell-file-name
4510                                    nil nil nil shell-command-switch
4511                                    (match-string 1 file))
4512             ;; Save the article.
4513             (setq file (expand-file-name file))
4514             (unless (file-exists-p (file-name-directory file))
4515               (make-directory (file-name-directory file) t))
4516             (if (and message-fcc-handler-function
4517                      (not (eq message-fcc-handler-function 'rmail-output)))
4518                 (funcall message-fcc-handler-function file)
4519               (if (and (file-readable-p file) (mail-file-babyl-p file))
4520                   (rmail-output file 1 nil t)
4521                 (let ((mail-use-rfc822 t))
4522                   (rmail-output file 1 t t))))))
4523         (kill-buffer (current-buffer))))))
4524
4525 (defun message-output (filename)
4526   "Append this article to Unix/babyl mail file FILENAME."
4527   (if (and (file-readable-p filename)
4528            (mail-file-babyl-p filename))
4529       (gnus-output-to-rmail filename t)
4530     (gnus-output-to-mail filename t)))
4531
4532 (defun message-cleanup-headers ()
4533   "Do various automatic cleanups of the headers."
4534   ;; Remove empty lines in the header.
4535   (save-restriction
4536     (message-narrow-to-headers)
4537     ;; Remove blank lines.
4538     (while (re-search-forward "^[ \t]*\n" nil t)
4539       (replace-match "" t t))
4540
4541     ;; Correct Newsgroups and Followup-To headers:  Change sequence of
4542     ;; spaces to comma and eliminate spaces around commas.  Eliminate
4543     ;; embedded line breaks.
4544     (goto-char (point-min))
4545     (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
4546       (save-restriction
4547         (narrow-to-region
4548          (point)
4549          (if (re-search-forward "^[^ \t]" nil t)
4550              (match-beginning 0)
4551            (forward-line 1)
4552            (point)))
4553         (goto-char (point-min))
4554         (while (re-search-forward "\n[ \t]+" nil t)
4555           (replace-match " " t t))     ;No line breaks (too confusing)
4556         (goto-char (point-min))
4557         (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
4558           (replace-match "," t t))
4559         (goto-char (point-min))
4560         ;; Remove trailing commas.
4561         (when (re-search-forward ",+$" nil t)
4562           (replace-match "" t t))))))
4563
4564 (defun message-make-date (&optional now)
4565   "Make a valid data header.
4566 If NOW, use that time instead."
4567   (let ((system-time-locale "C"))
4568     (format-time-string "%a, %d %b %Y %T %z" now)))
4569
4570 (defun message-make-message-id ()
4571   "Make a unique Message-ID."
4572   (concat "<" (message-unique-id)
4573           (let ((psubject (save-excursion (message-fetch-field "subject")))
4574                 (psupersedes
4575                  (save-excursion (message-fetch-field "supersedes"))))
4576             (if (or
4577                  (and message-reply-headers
4578                       (mail-header-references message-reply-headers)
4579                       (mail-header-subject message-reply-headers)
4580                       psubject
4581                       (not (string=
4582                             (message-strip-subject-re
4583                              (mail-header-subject message-reply-headers))
4584                             (message-strip-subject-re psubject))))
4585                  (and psupersedes
4586                       (string-match "_-_@" psupersedes)))
4587                 "_-_" ""))
4588           "@" (message-make-fqdn) ">"))
4589
4590 (defvar message-unique-id-char nil)
4591
4592 ;; If you ever change this function, make sure the new version
4593 ;; cannot generate IDs that the old version could.
4594 ;; You might for example insert a "." somewhere (not next to another dot
4595 ;; or string boundary), or modify the "fsf" string.
4596 (defun message-unique-id ()
4597   ;; Don't use microseconds from (current-time), they may be unsupported.
4598   ;; Instead we use this randomly inited counter.
4599   (setq message-unique-id-char
4600         (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
4601            ;; (current-time) returns 16-bit ints,
4602            ;; and 2^16*25 just fits into 4 digits i base 36.
4603            (* 25 25)))
4604   (let ((tm (current-time)))
4605     (concat
4606      (if (memq system-type '(ms-dos emx vax-vms))
4607          (let ((user (downcase (user-login-name))))
4608            (while (string-match "[^a-z0-9_]" user)
4609              (aset user (match-beginning 0) ?_))
4610            user)
4611        (message-number-base36 (user-uid) -1))
4612      (message-number-base36 (+ (car tm)
4613                                (lsh (% message-unique-id-char 25) 16)) 4)
4614      (message-number-base36 (+ (nth 1 tm)
4615                                (lsh (/ message-unique-id-char 25) 16)) 4)
4616      ;; Append a given name, because while the generated ID is unique
4617      ;; to this newsreader, other newsreaders might otherwise generate
4618      ;; the same ID via another algorithm.
4619      ".fsf")))
4620
4621 (defun message-number-base36 (num len)
4622   (if (if (< len 0)
4623           (<= num 0)
4624         (= len 0))
4625       ""
4626     (concat (message-number-base36 (/ num 36) (1- len))
4627             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
4628                                   (% num 36))))))
4629
4630 (defun message-make-organization ()
4631   "Make an Organization header."
4632   (let* ((organization
4633           (when message-user-organization
4634             (if (functionp message-user-organization)
4635                 (funcall message-user-organization)
4636               message-user-organization))))
4637     (with-temp-buffer
4638       (mm-enable-multibyte)
4639       (cond ((stringp organization)
4640              (insert organization))
4641             ((and (eq t organization)
4642                   message-user-organization-file
4643                   (file-exists-p message-user-organization-file))
4644              (insert-file-contents message-user-organization-file)))
4645       (goto-char (point-min))
4646       (while (re-search-forward "[\t\n]+" nil t)
4647         (replace-match "" t t))
4648       (unless (zerop (buffer-size))
4649         (buffer-string)))))
4650
4651 (defun message-make-lines ()
4652   "Count the number of lines and return numeric string."
4653   (save-excursion
4654     (save-restriction
4655       (widen)
4656       (message-goto-body)
4657       (int-to-string (count-lines (point) (point-max))))))
4658
4659 (defun message-make-references ()
4660   "Return the References header for this message."
4661   (when message-reply-headers
4662     (let ((message-id (mail-header-message-id message-reply-headers))
4663           (references (mail-header-references message-reply-headers))
4664           new-references)
4665       (if (or references message-id)
4666           (concat (or references "") (and references " ")
4667                   (or message-id ""))
4668         nil))))
4669
4670 (defun message-make-in-reply-to ()
4671   "Return the In-Reply-To header for this message."
4672   (when message-reply-headers
4673     (let ((from (mail-header-from message-reply-headers))
4674           (date (mail-header-date message-reply-headers))
4675           (msg-id (mail-header-message-id message-reply-headers)))
4676       (when from
4677         (let ((name (mail-extract-address-components from)))
4678           (concat msg-id (if msg-id " (")
4679                   (or (car name)
4680                       (nth 1 name))
4681                   "'s message of \""
4682                   (if (or (not date) (string= date ""))
4683                       "(unknown date)" date)
4684                   "\"" (if msg-id ")")))))))
4685
4686 (defun message-make-distribution ()
4687   "Make a Distribution header."
4688   (let ((orig-distribution (message-fetch-reply-field "distribution")))
4689     (cond ((functionp message-distribution-function)
4690            (funcall message-distribution-function))
4691           (t orig-distribution))))
4692
4693 (defun message-make-expires ()
4694   "Return an Expires header based on `message-expires'."
4695   (let ((current (current-time))
4696         (future (* 1.0 message-expires 60 60 24)))
4697     ;; Add the future to current.
4698     (setcar current (+ (car current) (round (/ future (expt 2 16)))))
4699     (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
4700     (message-make-date current)))
4701
4702 (defun message-make-path ()
4703   "Return uucp path."
4704   (let ((login-name (user-login-name)))
4705     (cond ((null message-user-path)
4706            (concat (system-name) "!" login-name))
4707           ((stringp message-user-path)
4708            ;; Support GENERICPATH.  Suggested by vixie@decwrl.dec.com.
4709            (concat message-user-path "!" login-name))
4710           (t login-name))))
4711
4712 (defun message-make-from ()
4713   "Make a From header."
4714   (let* ((style message-from-style)
4715          (login (message-make-address))
4716          (fullname
4717           (or (and (boundp 'user-full-name)
4718                    user-full-name)
4719               (user-full-name))))
4720     (when (string= fullname "&")
4721       (setq fullname (user-login-name)))
4722     (with-temp-buffer
4723       (mm-enable-multibyte)
4724       (cond
4725        ((or (null style)
4726             (equal fullname ""))
4727         (insert login))
4728        ((or (eq style 'angles)
4729             (and (not (eq style 'parens))