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