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