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