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