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