Merge with CEDET upstream (rev. 8499).
[bpt/emacs.git] / lisp / gnus / message.el
CommitLineData
23f87bed 1;;; message.el --- composing mail and news messages
e84b4b86 2
ab422c4d 3;; Copyright (C) 1996-2013 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
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
eec82323 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
eec82323
LMI
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
5e809f55 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eec82323
LMI
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eec82323
LMI
22
23;;; Commentary:
24
25;; This mode provides mail-sending facilities from within Emacs. It
26;; consists mainly of large chunks of code from the sendmail.el,
27;; gnus-msg.el and rnewspost.el files.
28
29;;; Code:
30
f0b7f5a8 31;; For Emacs <22.2 and XEmacs.
b43906b6
GM
32(eval-and-compile
33 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
16409b0b 34(eval-when-compile
9efa445f 35 (require 'cl))
b43906b6 36
eec82323 37(require 'mailheader)
18c06a99 38(require 'gmm-utils)
aa8f8277
GM
39(require 'mail-utils)
40;; Only for the trivial macros mail-header-from, mail-header-date
41;; mail-header-references, mail-header-subject, mail-header-id
42(eval-when-compile (require 'nnheader))
23f87bed
MB
43;; This is apparently necessary even though things are autoloaded.
44;; Because we dynamically bind mail-abbrev-mode-regexp, we'd better
45;; require mailabbrev here.
16409b0b 46(if (featurep 'xemacs)
23f87bed
MB
47 (require 'mail-abbrevs)
48 (require 'mailabbrev))
16409b0b
GM
49(require 'mail-parse)
50(require 'mml)
23f87bed 51(require 'rfc822)
a123622d 52(require 'format-spec)
01c52d31 53
2ea6167b
RS
54(autoload 'mailclient-send-it "mailclient") ;; Emacs 22 or contrib/
55
9efa445f
DN
56(defvar gnus-message-group-art)
57(defvar gnus-list-identifiers) ; gnus-sum is required where necessary
58(defvar rmail-enable-mime-composing)
eec82323
LMI
59
60(defgroup message '((user-mail-address custom-variable)
61 (user-full-name custom-variable))
62 "Mail and news message composing."
63 :link '(custom-manual "(message)Top")
64 :group 'mail
65 :group 'news)
66
67(put 'user-mail-address 'custom-type 'string)
68(put 'user-full-name 'custom-type 'string)
69
70(defgroup message-various nil
339ccc6a 71 "Various Message Variables."
eec82323
LMI
72 :link '(custom-manual "(message)Various Message Variables")
73 :group 'message)
74
75(defgroup message-buffers nil
339ccc6a 76 "Message Buffers."
eec82323
LMI
77 :link '(custom-manual "(message)Message Buffers")
78 :group 'message)
79
80(defgroup message-sending nil
339ccc6a 81 "Message Sending."
eec82323
LMI
82 :link '(custom-manual "(message)Sending Variables")
83 :group 'message)
84
85(defgroup message-interface nil
339ccc6a 86 "Message Interface."
eec82323
LMI
87 :link '(custom-manual "(message)Interface")
88 :group 'message)
89
90(defgroup message-forwarding nil
339ccc6a 91 "Message Forwarding."
eec82323
LMI
92 :link '(custom-manual "(message)Forwarding")
93 :group 'message-interface)
94
95(defgroup message-insertion nil
339ccc6a 96 "Message Insertion."
eec82323
LMI
97 :link '(custom-manual "(message)Insertion")
98 :group 'message)
99
100(defgroup message-headers nil
339ccc6a 101 "Message Headers."
eec82323
LMI
102 :link '(custom-manual "(message)Message Headers")
103 :group 'message)
104
105(defgroup message-news nil
339ccc6a 106 "Composing News Messages."
eec82323
LMI
107 :group 'message)
108
109(defgroup message-mail nil
339ccc6a 110 "Composing Mail Messages."
eec82323
LMI
111 :group 'message)
112
113(defgroup message-faces nil
114 "Faces used for message composing."
115 :group 'message
116 :group 'faces)
117
118(defcustom message-directory "~/Mail/"
119 "*Directory from which all other mail file variables are derived."
120 :group 'message-various
121 :type 'directory)
122
123(defcustom message-max-buffers 10
124 "*How many buffers to keep before starting to kill them off."
125 :group 'message-buffers
126 :type 'integer)
127
128(defcustom message-send-rename-function nil
129 "Function called to rename the buffer after sending it."
130 :group 'message-buffers
7d829636 131 :type '(choice function (const nil)))
eec82323
LMI
132
133(defcustom message-fcc-handler-function 'message-output
134 "*A function called to save outgoing articles.
135This function will be called with the name of the file to store the
136article in. The default function is `message-output' which saves in Unix
137mailbox format."
138 :type '(radio (function-item message-output)
139 (function :tag "Other"))
140 :group 'message-sending)
141
23f87bed
MB
142(defcustom message-fcc-externalize-attachments nil
143 "If non-nil, attachments are included as external parts in Fcc copies."
bf247b6e 144 :version "22.1"
23f87bed
MB
145 :type 'boolean
146 :group 'message-sending)
147
eec82323 148(defcustom message-courtesy-message
f129a4df 149 "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
eec82323
LMI
150 "*This is inserted at the start of a mailed copy of a posted message.
151If the string contains the format spec \"%s\", the Newsgroups
152the article has been posted to will be inserted there.
153If this variable is nil, no such courtesy message will be added."
154 :group 'message-sending
ad136a7c 155 :type '(radio string (const nil)))
eec82323 156
23f87bed
MB
157(defcustom message-ignored-bounced-headers
158 "^\\(Received\\|Return-Path\\|Delivered-To\\):"
eec82323
LMI
159 "*Regexp that matches headers to be removed in resent bounced mail."
160 :group 'message-interface
161 :type 'regexp)
162
106c1842 163(defcustom message-from-style mail-from-style
ed797193 164 "Specifies how \"From\" headers look.
eec82323 165
7d829636 166If nil, they contain just the return address like:
eec82323
LMI
167 king@grassland.com
168If `parens', they look like:
169 king@grassland.com (Elvis Parsley)
170If `angles', they look like:
171 Elvis Parsley <king@grassland.com>
172
173Otherwise, most addresses look like `angles', but they look like
174`parens' if `angles' would need quoting and `parens' would not."
937e60c8 175 :version "23.2"
eec82323
LMI
176 :type '(choice (const :tag "simple" nil)
177 (const parens)
178 (const angles)
179 (const default))
180 :group 'message-headers)
181
23f87bed
MB
182(defcustom message-insert-canlock t
183 "Whether to insert a Cancel-Lock header in news postings."
bf247b6e 184 :version "22.1"
23f87bed
MB
185 :group 'message-headers
186 :type 'boolean)
187
188(defcustom message-syntax-checks
189 (if message-insert-canlock '((sender . disabled)) nil)
16409b0b 190 ;; Guess this one shouldn't be easy to customize...
6748645f 191 "*Controls what syntax checks should not be performed on outgoing posts.
eec82323
LMI
192To disable checking of long signatures, for instance, add
193 `(signature . disabled)' to this list.
194
195Don't touch this variable unless you really know what you're doing.
196
437ce4be
MB
197Checks include `approved', `bogus-recipient', `continuation-headers',
198`control-chars', `empty', `existing-newsgroups', `from', `illegible-text',
f362b760
MB
199`invisible-text', `long-header-lines', `long-lines', `message-id',
200`multiple-headers', `new-text', `newsgroups', `quoting-style',
5cfd0f2e 201`repeated-newsgroups', `reply-to', `sender', `sendsys', `shoot',
f362b760
MB
202`shorten-followup-to', `signature', `size', `subject', `subject-cmsg'
203and `valid-newsgroups'."
16409b0b 204 :group 'message-news
7d829636 205 :type '(repeat sexp)) ; Fixme: improve this
eec82323 206
23f87bed
MB
207(defcustom message-required-headers '((optional . References)
208 From)
209 "*Headers to be generated or prompted for when sending a message.
210Also see `message-required-news-headers' and
211`message-required-mail-headers'."
bf247b6e 212 :version "22.1"
23f87bed
MB
213 :group 'message-news
214 :group 'message-headers
215 :link '(custom-manual "(message)Message Headers")
216 :type '(repeat sexp))
217
01c52d31 218(defcustom message-draft-headers '(References From Date)
23f87bed 219 "*Headers to be generated when saving a draft message."
bf247b6e 220 :version "22.1"
23f87bed
MB
221 :group 'message-news
222 :group 'message-headers
223 :link '(custom-manual "(message)Message Headers")
224 :type '(repeat sexp))
225
eec82323
LMI
226(defcustom message-required-news-headers
227 '(From Newsgroups Subject Date Message-ID
23f87bed 228 (optional . Organization)
16409b0b 229 (optional . User-Agent))
6748645f 230 "*Headers to be generated or prompted for when posting an article.
eec82323
LMI
231RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
232Message-ID. Organization, Lines, In-Reply-To, Expires, and
815b81c8 233User-Agent are optional. If you don't want message to insert some
eec82323
LMI
234header, remove it from this list."
235 :group 'message-news
236 :group 'message-headers
23f87bed 237 :link '(custom-manual "(message)Message Headers")
eec82323
LMI
238 :type '(repeat sexp))
239
240(defcustom message-required-mail-headers
23f87bed 241 '(From Subject Date (optional . In-Reply-To) Message-ID
16409b0b 242 (optional . User-Agent))
6748645f 243 "*Headers to be generated or prompted for when mailing a message.
23f87bed
MB
244It is recommended that From, Date, To, Subject and Message-ID be
245included. Organization and User-Agent are optional."
eec82323
LMI
246 :group 'message-mail
247 :group 'message-headers
23f87bed 248 :link '(custom-manual "(message)Message Headers")
eec82323
LMI
249 :type '(repeat sexp))
250
2cdd366f
KY
251(defcustom message-prune-recipient-rules nil
252 "Rules for how to prune the list of recipients when doing wide replies.
253This is a list of regexps and regexp matches."
b0e30310 254 :version "24.1"
2cdd366f
KY
255 :group 'message-mail
256 :group 'message-headers
257 :link '(custom-manual "(message)Wide Reply")
258 :type '(repeat regexp))
259
eec82323
LMI
260(defcustom message-deletable-headers '(Message-ID Date Lines)
261 "Headers to be deleted if they already exist and were generated by message previously."
262 :group 'message-headers
23f87bed 263 :link '(custom-manual "(message)Message Headers")
eec82323
LMI
264 :type 'sexp)
265
266(defcustom message-ignored-news-headers
61bbede6 267 "^NNTP-Posting-Host:\\|^Xref:\\|^[BGF]cc:\\|^Resent-Fcc:\\|^X-Draft-From:\\|^X-Gnus-Agent-Meta-Information:\\|^X-Message-SMTP-Method:"
eec82323
LMI
268 "*Regexp of headers to be removed unconditionally before posting."
269 :group 'message-news
270 :group 'message-headers
23f87bed 271 :link '(custom-manual "(message)Message Headers")
8903a9c8
MB
272 :type '(repeat :value-to-internal (lambda (widget value)
273 (custom-split-regexp-maybe value))
274 :match (lambda (widget value)
275 (or (stringp value)
276 (widget-editable-list-match widget value)))
277 regexp))
eec82323 278
23f87bed 279(defcustom message-ignored-mail-headers
674c5ccf 280 "^\\([GF]cc\\|Resent-Fcc\\|Xref\\|X-Draft-From\\|X-Gnus-Agent-Meta-Information\\):"
eec82323
LMI
281 "*Regexp of headers to be removed unconditionally before mailing."
282 :group 'message-mail
283 :group 'message-headers
23f87bed 284 :link '(custom-manual "(message)Mail Headers")
eec82323
LMI
285 :type 'regexp)
286
6b958814 287(defcustom message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|^X-Trace:\\|^X-ID:\\|^X-Complaints-To:\\|Return-Path:\\|^Supersedes:\\|^NNTP-Posting-Date:\\|^X-Trace:\\|^X-Complaints-To:\\|^Cancel-Lock:\\|^Cancel-Key:\\|^X-Hashcash:\\|^X-Payment:\\|^Approved:\\|^Injection-Date:\\|^Injection-Info:"
eec82323
LMI
288 "*Header lines matching this regexp will be deleted before posting.
289It's best to delete old Path and Date headers before posting to avoid
290any confusion."
291 :group 'message-interface
23f87bed 292 :link '(custom-manual "(message)Superseding")
8903a9c8
MB
293 :type '(repeat :value-to-internal (lambda (widget value)
294 (custom-split-regexp-maybe value))
295 :match (lambda (widget value)
296 (or (stringp value)
297 (widget-editable-list-match widget value)))
298 regexp))
eec82323 299
23f87bed
MB
300(defcustom message-subject-re-regexp
301 "^[ \t]*\\([Rr][Ee]\\(\\[[0-9]*\\]\\)*:[ \t]*\\)*[ \t]*"
6748645f
LMI
302 "*Regexp matching \"Re: \" in the subject line."
303 :group 'message-various
23f87bed 304 :link '(custom-manual "(message)Message Headers")
6748645f
LMI
305 :type 'regexp)
306
23f87bed
MB
307;;; Start of variables adopted from `message-utils.el'.
308
a8336650 309(defcustom message-subject-trailing-was-query t
23f87bed
MB
310 "*What to do with trailing \"(was: <old subject>)\" in subject lines.
311If nil, leave the subject unchanged. If it is the symbol `ask', query
312the user what do do. In this case, the subject is matched against
313`message-subject-trailing-was-ask-regexp'. If
314`message-subject-trailing-was-query' is t, always strip the trailing
315old subject. In this case, `message-subject-trailing-was-regexp' is
316used."
a8336650 317 :version "24.1"
23f87bed
MB
318 :type '(choice (const :tag "never" nil)
319 (const :tag "always strip" t)
01c52d31 320 (const ask))
23f87bed
MB
321 :link '(custom-manual "(message)Message Headers")
322 :group 'message-various)
323
324(defcustom message-subject-trailing-was-ask-regexp
be3c11b3 325 "[ \t]*\\([[(]+[Ww][Aa][Ss]:?[ \t]*.*[])]+\\)"
23f87bed
MB
326 "*Regexp matching \"(was: <old subject>)\" in the subject line.
327
328The function `message-strip-subject-trailing-was' uses this regexp if
329`message-subject-trailing-was-query' is set to the symbol `ask'. If
330the variable is t instead of `ask', use
331`message-subject-trailing-was-regexp' instead.
332
333It is okay to create some false positives here, as the user is asked."
be3c11b3 334 :version "22.1"
23f87bed
MB
335 :group 'message-various
336 :link '(custom-manual "(message)Message Headers")
337 :type 'regexp)
338
339(defcustom message-subject-trailing-was-regexp
be3c11b3 340 "[ \t]*\\((*[Ww][Aa][Ss]:[ \t]*.*)\\)"
23f87bed
MB
341 "*Regexp matching \"(was: <old subject>)\" in the subject line.
342
343If `message-subject-trailing-was-query' is set to t, the subject is
344matched against `message-subject-trailing-was-regexp' in
345`message-strip-subject-trailing-was'. You should use a regexp creating very
346few false positives here."
be3c11b3 347 :version "22.1"
23f87bed
MB
348 :group 'message-various
349 :link '(custom-manual "(message)Message Headers")
350 :type 'regexp)
351
23f87bed
MB
352;;; marking inserted text
353
23f87bed 354(defcustom message-mark-insert-begin
f129a4df 355 "--8<---------------cut here---------------start------------->8---\n"
23f87bed 356 "How to mark the beginning of some inserted text."
bf247b6e 357 :version "22.1"
23f87bed
MB
358 :type 'string
359 :link '(custom-manual "(message)Insertion Variables")
360 :group 'message-various)
361
23f87bed 362(defcustom message-mark-insert-end
f129a4df 363 "--8<---------------cut here---------------end--------------->8---\n"
23f87bed 364 "How to mark the end of some inserted text."
bf247b6e 365 :version "22.1"
23f87bed
MB
366 :type 'string
367 :link '(custom-manual "(message)Insertion Variables")
368 :group 'message-various)
369
f129a4df 370(defcustom message-archive-header "X-No-Archive: Yes\n"
23f87bed
MB
371 "Header to insert when you don't want your article to be archived.
372Archives \(such as groups.google.com\) respect this header."
bf247b6e 373 :version "22.1"
23f87bed
MB
374 :type 'string
375 :link '(custom-manual "(message)Header Commands")
376 :group 'message-various)
377
23f87bed
MB
378(defcustom message-archive-note
379 "X-No-Archive: Yes - save http://groups.google.com/"
380 "Note to insert why you wouldn't want this posting archived.
381If nil, don't insert any text in the body."
bf247b6e 382 :version "22.1"
ad136a7c 383 :type '(radio string (const nil))
23f87bed
MB
384 :link '(custom-manual "(message)Header Commands")
385 :group 'message-various)
386
387;;; Crossposts and Followups
388;; inspired by JoH-followup-to by Jochem Huhman <joh at gmx.de>
389;; new suggestions by R. Weikusat <rw at another.de>
390
391(defvar message-cross-post-old-target nil
392 "Old target for cross-posts or follow-ups.")
393(make-variable-buffer-local 'message-cross-post-old-target)
394
23f87bed
MB
395(defcustom message-cross-post-default t
396 "When non-nil `message-cross-post-followup-to' will perform a crosspost.
397If nil, `message-cross-post-followup-to' will only do a followup. Note that
398you can explicitly override this setting by calling
399`message-cross-post-followup-to' with a prefix."
bf247b6e 400 :version "22.1"
23f87bed
MB
401 :type 'boolean
402 :group 'message-various)
403
5f5475ac 404(defcustom message-cross-post-note "Crosspost & Followup-To: "
23f87bed 405 "Note to insert before signature to notify of cross-post and follow-up."
bf247b6e 406 :version "22.1"
23f87bed
MB
407 :type 'string
408 :group 'message-various)
409
5f5475ac 410(defcustom message-followup-to-note "Followup-To: "
23f87bed 411 "Note to insert before signature to notify of follow-up only."
bf247b6e 412 :version "22.1"
23f87bed
MB
413 :type 'string
414 :group 'message-various)
415
5f5475ac 416(defcustom message-cross-post-note-function 'message-cross-post-insert-note
23f87bed
MB
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
9b3ebcb6
MB
427(defcustom message-signature-separator "^-- $"
428 "Regexp matching the signature separator.
429This variable is used to strip off the signature from quoted text
430when `message-cite-function' is
431`message-cite-original-without-signature'. Most useful values
432are \"^-- $\" (strict) and \"^-- *$\" (loose; allow missing
433whitespace)."
434 :type '(choice (const :tag "strict" "^-- $")
435 (const :tag "loose" "^-- *$")
436 regexp)
d55fe5bb 437 :version "22.3" ;; Gnus 5.10.12 (changed default)
23f87bed 438 :link '(custom-manual "(message)Various Message Variables")
eec82323
LMI
439 :group 'message-various)
440
f129a4df 441(defcustom message-elide-ellipsis "\n[...]\n\n"
a123622d
G
442 "*The string which is inserted for elided text.
443This is a format-spec string, and you can use %l to say how many
444lines were removed, and %c to say how many characters were
445removed."
6748645f 446 :type 'string
23f87bed 447 :link '(custom-manual "(message)Various Commands")
6748645f 448 :group 'message-various)
eec82323 449
937e60c8 450(defcustom message-interactive mail-interactive
eec82323 451 "Non-nil means when sending a message wait for and display errors.
2b8f62e9 452A value of nil means let mailer mail back a message to report errors."
937e60c8 453 :version "23.2"
eec82323
LMI
454 :group 'message-sending
455 :group 'message-mail
23f87bed 456 :link '(custom-manual "(message)Sending Variables")
eec82323
LMI
457 :type 'boolean)
458
16dd13df 459(defcustom message-confirm-send nil
eef5ade7 460 "When non-nil, ask for confirmation when sending a message."
16dd13df
MB
461 :group 'message-sending
462 :group 'message-mail
4520e527 463 :version "23.1" ;; No Gnus
16dd13df
MB
464 :link '(custom-manual "(message)Sending Variables")
465 :type 'boolean)
466
1d297c49 467(defcustom message-generate-new-buffers 'unsent
815b81c8
MB
468 "*Say whether to create a new message buffer to compose a message.
469Valid values include:
470
471nil
472 Generate the buffer name in the Message way (e.g., *mail*, *news*,
473 *mail to whom*, *news on group*, etc.) and continue editing in the
474 existing buffer of that name. If there is no such buffer, it will
475 be newly created.
476
477`unique' or t
478 Create the new buffer with the name generated in the Message way.
479
480`unsent'
481 Similar to `unique' but the buffer name begins with \"*unsent \".
482
483`standard'
484 Similar to nil but the buffer name is simpler like *mail message*.
485
486function
487 If this is a function, call that function with three parameters:
488 The type, the To address and the group name (any of these may be nil).
489 The function should return the new buffer name."
1d297c49 490 :version "24.1"
eec82323 491 :group 'message-buffers
23f87bed 492 :link '(custom-manual "(message)Message Buffers")
815b81c8
MB
493 :type '(choice (const nil)
494 (sexp :tag "unique" :format "unique\n" :value unique
495 :match (lambda (widget value) (memq value '(unique t))))
496 (const unsent)
497 (const standard)
498 (function :format "\n %{%t%}: %v")))
eec82323
LMI
499
500(defcustom message-kill-buffer-on-exit nil
501 "*Non-nil means that the message buffer will be killed after sending a message."
502 :group 'message-buffers
23f87bed 503 :link '(custom-manual "(message)Message Buffers")
eec82323
LMI
504 :type 'boolean)
505
01c52d31
MB
506(defcustom message-kill-buffer-query t
507 "*Non-nil means that killing a modified message buffer has to be confirmed.
508This is used by `message-kill-buffer'."
330f707b 509 :version "23.1" ;; No Gnus
01c52d31
MB
510 :group 'message-buffers
511 :type 'boolean)
512
eec82323 513(defcustom message-user-organization
ed797193
G
514 (or (getenv "ORGANIZATION") t)
515 "String to be used as an Organization header.
eec82323
LMI
516If t, use `message-user-organization-file'."
517 :group 'message-headers
518 :type '(choice string
519 (const :tag "consult file" t)))
520
01c52d31
MB
521(defcustom message-user-organization-file
522 (let (orgfile)
523 (dolist (f (list "/etc/organization"
524 "/etc/news/organization"
525 "/usr/lib/news/organization"))
526 (when (file-readable-p f)
527 (setq orgfile f)))
528 orgfile)
eec82323
LMI
529 "*Local news organization file."
530 :type 'file
23f87bed 531 :link '(custom-manual "(message)News Headers")
eec82323
LMI
532 :group 'message-headers)
533
6748645f 534(defcustom message-make-forward-subject-function
23f87bed 535 #'message-forward-subject-name-subject
7d829636 536 "*List of functions called to generate subject headers for forwarded messages.
6748645f
LMI
537The subject generated by the previous function is passed into each
538successive function.
539
540The provided functions are:
541
23f87bed
MB
542* `message-forward-subject-author-subject' Source of article (author or
543 newsgroup), in brackets followed by the subject
544* `message-forward-subject-name-subject' Source of article (name of author
545 or newsgroup), in brackets followed by the subject
546* `message-forward-subject-fwd' Subject of article with 'Fwd:' prepended
6748645f 547 to it."
16409b0b 548 :group 'message-forwarding
23f87bed 549 :link '(custom-manual "(message)Forwarding")
16409b0b 550 :type '(radio (function-item message-forward-subject-author-subject)
7d829636 551 (function-item message-forward-subject-fwd)
23f87bed 552 (function-item message-forward-subject-name-subject)
7d829636 553 (repeat :tag "List of functions" function)))
16409b0b
GM
554
555(defcustom message-forward-as-mime t
23f87bed
MB
556 "*Non-nil means forward messages as an inline/rfc822 MIME section.
557Otherwise, directly inline the old message in the forwarded message."
2d447df6 558 :version "21.1"
16409b0b 559 :group 'message-forwarding
23f87bed 560 :link '(custom-manual "(message)Forwarding")
16409b0b
GM
561 :type 'boolean)
562
23f87bed
MB
563(defcustom message-forward-show-mml 'best
564 "*Non-nil means show forwarded messages as MML (decoded from MIME).
565Otherwise, forwarded messages are unchanged.
566Can also be the symbol `best' to indicate that MML should be
567used, except when it is a bad idea to use MML. One example where
568it is a bad idea is when forwarding a signed or encrypted
569message, because converting MIME to MML would invalidate the
570digital signature."
88818fbe 571 :version "21.1"
16409b0b 572 :group 'message-forwarding
23f87bed
MB
573 :type '(choice (const :tag "use MML" t)
574 (const :tag "don't use MML " nil)
575 (const :tag "use MML when appropriate" best)))
16409b0b
GM
576
577(defcustom message-forward-before-signature t
23f87bed 578 "*Non-nil means put forwarded message before signature, else after."
16409b0b
GM
579 :group 'message-forwarding
580 :type 'boolean)
6748645f
LMI
581
582(defcustom message-wash-forwarded-subjects nil
23f87bed
MB
583 "*Non-nil means try to remove as much cruft as possible from the subject.
584Done before generating the new subject of a forward."
6748645f 585 :group 'message-forwarding
23f87bed 586 :link '(custom-manual "(message)Forwarding")
6748645f
LMI
587 :type 'boolean)
588
dd58a686
SM
589(defcustom message-ignored-resent-headers
590 ;; `Delivered-To' needs to be removed because some mailers use it to
591 ;; detect loops, so if you resend a message to an address that ultimately
592 ;; comes back to you (e.g. a mailing-list to which you subscribe, in which
593 ;; case you may be removed from the list on the grounds that mail to you
594 ;; bounced with a "mailing loop" error).
a71e2379
G
595 "^Return-receipt\\|^X-Gnus\\|^Gnus-Warning:\\|^>?From \\|^Delivered-To:\
596\\|^X-Content-Length:\\|^X-UIDL:"
eec82323 597 "*All headers that match this regexp will be deleted when resending a message."
a71e2379 598 :version "24.4"
eec82323 599 :group 'message-interface
23f87bed 600 :link '(custom-manual "(message)Resending")
8903a9c8
MB
601 :type '(repeat :value-to-internal (lambda (widget value)
602 (custom-split-regexp-maybe value))
603 :match (lambda (widget value)
604 (or (stringp value)
605 (widget-editable-list-match widget value)))
606 regexp))
eec82323 607
16409b0b
GM
608(defcustom message-forward-ignored-headers "^Content-Transfer-Encoding:\\|^X-Gnus"
609 "*All headers that match this regexp will be deleted when forwarding a message."
2d447df6 610 :version "21.1"
16409b0b 611 :group 'message-forwarding
8903a9c8
MB
612 :type '(repeat :value-to-internal (lambda (widget value)
613 (custom-split-regexp-maybe value))
614 :match (lambda (widget value)
615 (or (stringp value)
616 (widget-editable-list-match widget value)))
16409b0b
GM
617 regexp))
618
eec82323
LMI
619(defcustom message-ignored-cited-headers "."
620 "*Delete these headers from the messages you yank."
621 :group 'message-insertion
23f87bed
MB
622 :link '(custom-manual "(message)Insertion Variables")
623 :type 'regexp)
624
e0a185ae 625(defcustom message-cite-prefix-regexp mail-citation-prefix-regexp
23f87bed 626 "*Regexp matching the longest possible citation prefix on a line."
2696d88f 627 :version "24.1"
23f87bed
MB
628 :group 'message-insertion
629 :link '(custom-manual "(message)Insertion Variables")
01c52d31
MB
630 :type 'regexp
631 :set (lambda (symbol value)
632 (prog1
633 (custom-set-default symbol value)
634 (if (boundp 'gnus-message-cite-prefix-regexp)
635 (setq gnus-message-cite-prefix-regexp
636 (concat "^\\(?:" value "\\)"))))))
eec82323 637
f129a4df 638(defcustom message-cancel-message "I am canceling my own article.\n"
eec82323
LMI
639 "Message to be inserted in the cancel message."
640 :group 'message-interface
23f87bed 641 :link '(custom-manual "(message)Canceling News")
eec82323
LMI
642 :type 'string)
643
2ea6167b
RS
644(defun message-send-mail-function ()
645 "Return suitable value for the variable `message-send-mail-function'."
b62a39c4
RS
646 (cond ((and (require 'sendmail)
647 (boundp 'sendmail-program)
648 sendmail-program
9e0a8576 649 (executable-find sendmail-program))
2ea6167b 650 'message-send-mail-with-sendmail)
9e0a8576 651 ((and (locate-library "smtpmail")
aa8f8277 652 (boundp 'smtpmail-default-smtp-server)
9e0a8576
RS
653 smtpmail-default-smtp-server)
654 'message-smtpmail-send-it)
655 ((locate-library "mailclient")
2ea6167b
RS
656 'message-send-mail-with-mailclient)
657 (t
aa8f8277 658 (error "Don't know how to send mail. Please customize `message-send-mail-function'"))))
2ea6167b 659
cc26d239 660(defun message-default-send-mail-function ()
ac1af635
GM
661 (cond ((eq send-mail-function 'smtpmail-send-it) 'message-smtpmail-send-it)
662 ((eq send-mail-function 'feedmail-send-it) 'feedmail-send-it)
3451795c 663 ((eq send-mail-function 'sendmail-query-once) 'sendmail-query-once)
ac1af635
GM
664 ((eq send-mail-function 'mailclient-send-it)
665 'message-send-mail-with-mailclient)
cc26d239
LI
666 (t (message-send-mail-function))))
667
668;; Useful to set in site-init.el
669(defcustom message-send-mail-function (message-default-send-mail-function)
eec82323
LMI
670 "Function to call to send the current buffer as mail.
671The headers should be delimited by a line whose contents match the
672variable `mail-header-separator'.
673
2ea6167b 674Valid values include `message-send-mail-with-sendmail'
7d829636 675`message-send-mail-with-mh', `message-send-mail-with-qmail',
2ea6167b
RS
676`message-smtpmail-send-it', `smtpmail-send-it',
677`feedmail-send-it' and `message-send-mail-with-mailclient'. The
8f7abae3
MB
678default is system dependent and determined by the function
679`message-send-mail-function'.
7d829636
DL
680
681See also `send-mail-function'."
eec82323
LMI
682 :type '(radio (function-item message-send-mail-with-sendmail)
683 (function-item message-send-mail-with-mh)
684 (function-item message-send-mail-with-qmail)
23f87bed 685 (function-item message-smtpmail-send-it)
6748645f 686 (function-item smtpmail-send-it)
7d829636 687 (function-item feedmail-send-it)
2ea6167b
RS
688 (function-item message-send-mail-with-mailclient
689 :tag "Use Mailclient package")
690 (function :tag "Other"))
eec82323 691 :group 'message-sending
ac1af635 692 :version "23.2"
2ea6167b 693 :initialize 'custom-initialize-default
23f87bed 694 :link '(custom-manual "(message)Mail Variables")
eec82323
LMI
695 :group 'message-mail)
696
697(defcustom message-send-news-function 'message-send-news
698 "Function to call to send the current buffer as news.
699The headers should be delimited by a line whose contents match the
700variable `mail-header-separator'."
701 :group 'message-sending
702 :group 'message-news
23f87bed 703 :link '(custom-manual "(message)News Variables")
eec82323
LMI
704 :type 'function)
705
706(defcustom message-reply-to-function nil
7d829636 707 "If non-nil, function that should return a list of headers.
eec82323
LMI
708This function should pick out addresses from the To, Cc, and From headers
709and respond with new To and Cc headers."
710 :group 'message-interface
23f87bed 711 :link '(custom-manual "(message)Reply")
7d829636 712 :type '(choice function (const nil)))
eec82323
LMI
713
714(defcustom message-wide-reply-to-function nil
7d829636 715 "If non-nil, function that should return a list of headers.
eec82323
LMI
716This function should pick out addresses from the To, Cc, and From headers
717and respond with new To and Cc headers."
718 :group 'message-interface
23f87bed 719 :link '(custom-manual "(message)Wide Reply")
7d829636 720 :type '(choice function (const nil)))
eec82323
LMI
721
722(defcustom message-followup-to-function nil
7d829636 723 "If non-nil, function that should return a list of headers.
eec82323
LMI
724This function should pick out addresses from the To, Cc, and From headers
725and respond with new To and Cc headers."
726 :group 'message-interface
23f87bed 727 :link '(custom-manual "(message)Followup")
7d829636 728 :type '(choice function (const nil)))
eec82323 729
01c52d31
MB
730(defcustom message-extra-wide-headers nil
731 "If non-nil, a list of additional address headers.
732These are used when composing a wide reply."
733 :group 'message-sending
734 :type '(repeat string))
735
eec82323
LMI
736(defcustom message-use-followup-to 'ask
737 "*Specifies what to do with Followup-To header.
738If nil, always ignore the header. If it is t, use its value, but
739query before using the \"poster\" value. If it is the symbol `ask',
740always query the user whether to use the value. If it is the symbol
741`use', always use the value."
742 :group 'message-interface
23f87bed 743 :link '(custom-manual "(message)Followup")
eec82323 744 :type '(choice (const :tag "ignore" nil)
23f87bed 745 (const :tag "use & query" t)
eec82323
LMI
746 (const use)
747 (const ask)))
748
23f87bed
MB
749(defcustom message-use-mail-followup-to 'use
750 "*Specifies what to do with Mail-Followup-To header.
751If nil, always ignore the header. If it is the symbol `ask', always
752query the user whether to use the value. If it is the symbol `use',
753always use the value."
bf247b6e 754 :version "22.1"
23f87bed
MB
755 :group 'message-interface
756 :link '(custom-manual "(message)Mailing Lists")
757 :type '(choice (const :tag "ignore" nil)
758 (const use)
759 (const ask)))
760
761(defcustom message-subscribed-address-functions nil
762 "*Specifies functions for determining list subscription.
763If nil, do not attempt to determine list subscription with functions.
764If non-nil, this variable contains a list of functions which return
765regular expressions to match lists. These functions can be used in
766conjunction with `message-subscribed-regexps' and
767`message-subscribed-addresses'."
bf247b6e 768 :version "22.1"
23f87bed
MB
769 :group 'message-interface
770 :link '(custom-manual "(message)Mailing Lists")
771 :type '(repeat sexp))
772
773(defcustom message-subscribed-address-file nil
774 "*A file containing addresses the user is subscribed to.
775If nil, do not look at any files to determine list subscriptions. If
776non-nil, each line of this file should be a mailing list address."
bf247b6e 777 :version "22.1"
23f87bed
MB
778 :group 'message-interface
779 :link '(custom-manual "(message)Mailing Lists")
ad136a7c 780 :type '(radio file (const nil)))
23f87bed
MB
781
782(defcustom message-subscribed-addresses nil
783 "*Specifies a list of addresses the user is subscribed to.
784If nil, do not use any predefined list subscriptions. This list of
785addresses can be used in conjunction with
786`message-subscribed-address-functions' and `message-subscribed-regexps'."
bf247b6e 787 :version "22.1"
23f87bed
MB
788 :group 'message-interface
789 :link '(custom-manual "(message)Mailing Lists")
790 :type '(repeat string))
791
792(defcustom message-subscribed-regexps nil
793 "*Specifies a list of addresses the user is subscribed to.
794If nil, do not use any predefined list subscriptions. This list of
795regular expressions can be used in conjunction with
796`message-subscribed-address-functions' and `message-subscribed-addresses'."
bf247b6e 797 :version "22.1"
23f87bed
MB
798 :group 'message-interface
799 :link '(custom-manual "(message)Mailing Lists")
800 :type '(repeat regexp))
801
802(defcustom message-allow-no-recipients 'ask
803 "Specifies what to do when there are no recipients other than Gcc/Fcc.
804If it is the symbol `always', the posting is allowed. If it is the
805symbol `never', the posting is not allowed. If it is the symbol
806`ask', you are prompted."
bf247b6e 807 :version "22.1"
23f87bed
MB
808 :group 'message-interface
809 :link '(custom-manual "(message)Message Headers")
810 :type '(choice (const always)
811 (const never)
812 (const ask)))
813
eec82323 814(defcustom message-sendmail-f-is-evil nil
7d829636 815 "*Non-nil means don't add \"-f username\" to the sendmail command line.
16409b0b 816Doing so would be even more evil than leaving it out."
eec82323 817 :group 'message-sending
23f87bed 818 :link '(custom-manual "(message)Mail Variables")
eec82323
LMI
819 :type 'boolean)
820
4332f3ec 821(defcustom message-sendmail-envelope-from
e0a185ae 822 ;; `mail-envelope-from' is unavailable unless sendmail.el is loaded.
937e60c8 823 (if (boundp 'mail-envelope-from) mail-envelope-from)
23f87bed
MB
824 "*Envelope-from when sending mail with sendmail.
825If this is nil, use `user-mail-address'. If it is the symbol
826`header', use the From: header of the message."
937e60c8 827 :version "23.2"
23f87bed
MB
828 :type '(choice (string :tag "From name")
829 (const :tag "Use From: header from message" header)
830 (const :tag "Use `user-mail-address'" nil))
831 :link '(custom-manual "(message)Mail Variables")
832 :group 'message-sending)
833
01c52d31
MB
834(defcustom message-sendmail-extra-arguments nil
835 "Additional arguments to `sendmail-program'."
836 ;; E.g. '("-a" "account") for msmtp
330f707b 837 :version "23.1" ;; No Gnus
01c52d31
MB
838 :type '(repeat string)
839 ;; :link '(custom-manual "(message)Mail Variables")
840 :group 'message-sending)
841
eec82323
LMI
842;; qmail-related stuff
843(defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
844 "Location of the qmail-inject program."
845 :group 'message-sending
23f87bed 846 :link '(custom-manual "(message)Mail Variables")
eec82323
LMI
847 :type 'file)
848
849(defcustom message-qmail-inject-args nil
850 "Arguments passed to qmail-inject programs.
2b8f62e9
JB
851This should be a list of strings, one string for each argument.
852It may also be a function.
eec82323
LMI
853
854For e.g., if you wish to set the envelope sender address so that bounces
855go to the right place or to deal with listserv's usage of that address, you
856might set this variable to '(\"-f\" \"you@some.where\")."
857 :group 'message-sending
23f87bed
MB
858 :link '(custom-manual "(message)Mail Variables")
859 :type '(choice (function)
860 (repeat string)))
eec82323 861
9efa445f
DN
862(defvar gnus-post-method)
863(defvar gnus-select-method)
eec82323
LMI
864(defcustom message-post-method
865 (cond ((and (boundp 'gnus-post-method)
6748645f 866 (listp gnus-post-method)
eec82323
LMI
867 gnus-post-method)
868 gnus-post-method)
869 ((boundp 'gnus-select-method)
870 gnus-select-method)
871 (t '(nnspool "")))
6748645f
LMI
872 "*Method used to post news.
873Note that when posting from inside Gnus, for instance, this
874variable isn't used."
eec82323
LMI
875 :group 'message-news
876 :group 'message-sending
877 ;; This should be the `gnus-select-method' widget, but that might
878 ;; create a dependence to `gnus.el'.
879 :type 'sexp)
880
0d1c2cc8 881(defcustom message-generate-headers-first nil
23f87bed 882 "Which headers should be generated before starting to compose a message.
f5d01350 883If t, generate all required headers. This can also be a list of headers to
23f87bed
MB
884generate. The variables `message-required-news-headers' and
885`message-required-mail-headers' specify which headers to generate.
886
887Note that the variable `message-deletable-headers' specifies headers which
888are to be deleted and then re-generated before sending, so this variable
889will not have a visible effect for those headers."
eec82323 890 :group 'message-headers
23f87bed
MB
891 :link '(custom-manual "(message)Message Headers")
892 :type '(choice (const :tag "None" nil)
01c52d31
MB
893 (const :tag "All" t)
894 (repeat (sexp :tag "Header"))))
895
896(defcustom message-fill-column 72
897 "Column beyond which automatic line-wrapping should happen.
898Local value for message buffers. If non-nil, also turn on
899auto-fill in message buffers."
900 :group 'message-various
901 ;; :link '(custom-manual "(message)Message Headers")
902 :type '(choice (const :tag "Don't turn on auto fill" nil)
903 (integer)))
eec82323 904
4e14547f
CY
905(defcustom message-setup-hook nil
906 "Normal hook, run each time a new outgoing message is initialized.
907The function `message-setup' runs this hook."
908 :group 'message-various
909 :link '(custom-manual "(message)Various Message Variables")
910 :type 'hook)
eec82323 911
16409b0b 912(defcustom message-cancel-hook nil
c80e3b4a 913 "Hook run when canceling articles."
16409b0b 914 :group 'message-various
23f87bed 915 :link '(custom-manual "(message)Various Message Variables")
16409b0b
GM
916 :type 'hook)
917
eec82323
LMI
918(defcustom message-signature-setup-hook nil
919 "Normal hook, run each time a new outgoing message is initialized.
920It is run after the headers have been inserted and before
921the signature is inserted."
922 :group 'message-various
23f87bed 923 :link '(custom-manual "(message)Various Message Variables")
eec82323
LMI
924 :type 'hook)
925
926(defcustom message-mode-hook nil
927 "Hook run in message mode buffers."
928 :group 'message-various
929 :type 'hook)
930
931(defcustom message-header-hook nil
932 "Hook run in a message mode buffer narrowed to the headers."
933 :group 'message-various
934 :type 'hook)
935
936(defcustom message-header-setup-hook nil
6748645f 937 "Hook called narrowed to the headers when setting up a message buffer."
eec82323 938 :group 'message-various
23f87bed 939 :link '(custom-manual "(message)Various Message Variables")
eec82323
LMI
940 :type 'hook)
941
23f87bed
MB
942(defcustom message-minibuffer-local-map
943 (let ((map (make-sparse-keymap 'message-minibuffer-local-map)))
944 (set-keymap-parent map minibuffer-local-map)
945 map)
e2642250 946 "Keymap for `message-read-from-minibuffer'."
d0859c9a
MB
947 :version "22.1"
948 :group 'message-various)
23f87bed 949
eec82323 950(defcustom message-citation-line-function 'message-insert-citation-line
23f87bed
MB
951 "*Function called to insert the \"Whomever writes:\" line.
952
01c52d31 953Predefined functions include `message-insert-citation-line' and
4d8a28ec 954`message-insert-formatted-citation-line' (see the variable
01c52d31
MB
955`message-citation-line-format').
956
23f87bed
MB
957Note that Gnus provides a feature where the reader can click on
958`writes:' to hide the cited text. If you change this line too much,
959people who read your message will have to change their Gnus
960configuration. See the variable `gnus-cite-attribution-suffix'."
01c52d31
MB
961 :type '(choice
962 (function-item :tag "plain" message-insert-citation-line)
4d8a28ec 963 (function-item :tag "formatted" message-insert-formatted-citation-line)
01c52d31 964 (function :tag "Other"))
23f87bed 965 :link '(custom-manual "(message)Insertion Variables")
eec82323
LMI
966 :group 'message-insertion)
967
01c52d31
MB
968(defcustom message-citation-line-format "On %a, %b %d %Y, %N wrote:\n"
969 "Format of the \"Whomever writes:\" line.
970
971The string is formatted using `format-spec'. The following
972constructs are replaced:
973
974 %f The full From, e.g. \"John Doe <john.doe@example.invalid>\".
975 %n The mail address, e.g. \"john.doe@example.invalid\".
976 %N The real name if present, e.g.: \"John Doe\", else fall
977 back to the mail address.
978 %F The first name if present, e.g.: \"John\".
979 %L The last name if present, e.g.: \"Doe\".
980
981All other format specifiers are passed to `format-time-string'
982which is called using the date from the article your replying to.
983Extracting the first (%F) and last name (%L) is done
984heuristically, so you should always check it yourself.
985
986Please also read the note in the documentation of
987`message-citation-line-function'."
988 :type '(choice (const :tag "Plain" "%f writes:")
989 (const :tag "Include date" "On %a, %b %d %Y, %n wrote:")
990 string)
991 :link '(custom-manual "(message)Insertion Variables")
330f707b 992 :version "23.1" ;; No Gnus
01c52d31
MB
993 :group 'message-insertion)
994
e0a185ae 995(defcustom message-yank-prefix mail-yank-prefix
23f87bed
MB
996 "*Prefix inserted on the lines of yanked messages.
997Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
01c52d31 998See also `message-yank-cited-prefix' and `message-yank-empty-prefix'."
937e60c8 999 :version "23.2"
23f87bed
MB
1000 :type 'string
1001 :link '(custom-manual "(message)Insertion Variables")
1002 :group 'message-insertion)
1003
1004(defcustom message-yank-cited-prefix ">"
01c52d31 1005 "*Prefix inserted on cited lines of yanked messages.
23f87bed 1006Fix `message-cite-prefix-regexp' if it is set to an abnormal value.
01c52d31
MB
1007See also `message-yank-prefix' and `message-yank-empty-prefix'."
1008 :version "22.1"
1009 :type 'string
1010 :link '(custom-manual "(message)Insertion Variables")
1011 :group 'message-insertion)
1012
1013(defcustom message-yank-empty-prefix ">"
1014 "*Prefix inserted on empty lines of yanked messages.
1015See also `message-yank-prefix' and `message-yank-cited-prefix'."
bf247b6e 1016 :version "22.1"
eec82323 1017 :type 'string
23f87bed 1018 :link '(custom-manual "(message)Insertion Variables")
eec82323
LMI
1019 :group 'message-insertion)
1020
e0a185ae 1021(defcustom message-indentation-spaces mail-indentation-spaces
eec82323
LMI
1022 "*Number of spaces to insert at the beginning of each cited line.
1023Used by `message-yank-original' via `message-yank-cite'."
937e60c8 1024 :version "23.2"
eec82323 1025 :group 'message-insertion
23f87bed 1026 :link '(custom-manual "(message)Insertion Variables")
eec82323
LMI
1027 :type 'integer)
1028
9b3ebcb6 1029(defcustom message-cite-function 'message-cite-original-without-signature
4a55f847
RS
1030 "*Function for citing an original message.
1031Predefined functions include `message-cite-original' and
1032`message-cite-original-without-signature'.
01c52d31 1033Note that these functions use `mail-citation-hook' if that is non-nil."
eec82323 1034 :type '(radio (function-item message-cite-original)
16409b0b 1035 (function-item message-cite-original-without-signature)
eec82323
LMI
1036 (function-item sc-cite-original)
1037 (function :tag "Other"))
23f87bed 1038 :link '(custom-manual "(message)Insertion Variables")
d55fe5bb 1039 :version "22.3" ;; Gnus 5.10.12 (changed default)
eec82323
LMI
1040 :group 'message-insertion)
1041
eec82323
LMI
1042(defcustom message-indent-citation-function 'message-indent-citation
1043 "*Function for modifying a citation just inserted in the mail buffer.
1044This can also be a list of functions. Each function can find the
1045citation between (point) and (mark t). And each function should leave
1046point and mark around the citation text as modified."
1047 :type 'function
23f87bed 1048 :link '(custom-manual "(message)Insertion Variables")
eec82323
LMI
1049 :group 'message-insertion)
1050
937e60c8 1051(defcustom message-signature mail-signature
eec82323
LMI
1052 "*String to be inserted at the end of the message buffer.
1053If t, the `message-signature-file' file will be inserted instead.
1054If a function, the result from the function will be used instead.
1055If a form, the result from the form will be used instead."
937e60c8 1056 :version "23.2"
eec82323 1057 :type 'sexp
23f87bed 1058 :link '(custom-manual "(message)Insertion Variables")
eec82323
LMI
1059 :group 'message-insertion)
1060
e0a185ae 1061(defcustom message-signature-file mail-signature-file
5449c317
DL
1062 "*Name of file containing the text inserted at end of message buffer.
1063Ignored if the named file doesn't exist.
01c52d31
MB
1064If nil, don't insert a signature.
1065If a path is specified, the value of `message-signature-directory' is ignored,
1066even if set."
937e60c8 1067 :version "23.2"
5449c317 1068 :type '(choice file (const :tags "None" nil))
23f87bed
MB
1069 :link '(custom-manual "(message)Insertion Variables")
1070 :group 'message-insertion)
1071
01c52d31
MB
1072(defcustom message-signature-directory nil
1073 "*Name of directory containing signature files.
1074Comes in handy if you have many such files, handled via posting styles for
1075instance.
1076If nil, `message-signature-file' is expected to specify the directory if
1077needed."
1078 :type '(choice string (const :tags "None" nil))
1079 :link '(custom-manual "(message)Insertion Variables")
1080 :group 'message-insertion)
1081
23f87bed
MB
1082(defcustom message-signature-insert-empty-line t
1083 "*If non-nil, insert an empty line before the signature separator."
bf247b6e 1084 :version "22.1"
23f87bed
MB
1085 :type 'boolean
1086 :link '(custom-manual "(message)Insertion Variables")
eec82323
LMI
1087 :group 'message-insertion)
1088
706b5974
G
1089(defcustom message-cite-reply-position 'traditional
1090 "*Where the reply should be positioned.
1091If `traditional', reply inline.
1092If `above', reply above quoted text.
1093If `below', reply below quoted text.
1094
1095Note: Many newsgroups frown upon nontraditional reply styles. You
1096probably want to set this variable only for specific groups,
1097e.g. using `gnus-posting-styles':
1098
b5244046 1099 (eval (set (make-local-variable 'message-cite-reply-position) 'above))"
2bed3f04 1100 :version "24.1"
706b5974
G
1101 :type '(choice (const :tag "Reply inline" 'traditional)
1102 (const :tag "Reply above" 'above)
1103 (const :tag "Reply below" 'below))
1104 :group 'message-insertion)
1105
1106(defcustom message-cite-style nil
1107 "*The overall style to be used when yanking cited text.
1108Value is either `nil' (no variable overrides) or a let-style list
1109of pairs (VARIABLE VALUE) that will be bound in
1110`message-yank-original' to do the quoting.
1111
1112Presets to impersonate popular mail agents are found in the
1113message-cite-style-* variables. This variable is intended for
1114use in `gnus-posting-styles', such as:
1115
1116 ((posting-from-work-p) (eval (set (make-local-variable 'message-cite-style) message-cite-style-outlook)))"
1117 :version "24.1"
1118 :group 'message-insertion
1119 :type '(choice (const :tag "Do not override variables" :value nil)
1120 (const :tag "MS Outlook" :value message-cite-style-outlook)
1121 (const :tag "Mozilla Thunderbird" :value message-cite-style-thunderbird)
1122 (const :tag "Gmail" :value message-cite-style-gmail)
1123 (variable :tag "User-specified")))
1124
1125(defconst message-cite-style-outlook
1126 '((message-cite-function 'message-cite-original)
1127 (message-citation-line-function 'message-insert-formatted-citation-line)
1128 (message-cite-reply-position 'above)
1129 (message-yank-prefix "")
1130 (message-yank-cited-prefix "")
1131 (message-yank-empty-prefix "")
1132 (message-citation-line-format "\n\n-----------------------\nOn %a, %b %d %Y, %N wrote:\n"))
1133 "Message citation style used by MS Outlook. Use with message-cite-style.")
1134
1135(defconst message-cite-style-thunderbird
1136 '((message-cite-function 'message-cite-original)
1137 (message-citation-line-function 'message-insert-formatted-citation-line)
1138 (message-cite-reply-position 'above)
1139 (message-yank-prefix "> ")
1140 (message-yank-cited-prefix ">")
1141 (message-yank-empty-prefix ">")
1142 (message-citation-line-format "On %D %R %p, %N wrote:"))
1143 "Message citation style used by Mozilla Thunderbird. Use with message-cite-style.")
1144
1145(defconst message-cite-style-gmail
1146 '((message-cite-function 'message-cite-original)
1147 (message-citation-line-function 'message-insert-formatted-citation-line)
1148 (message-cite-reply-position 'above)
1149 (message-yank-prefix " ")
1150 (message-yank-cited-prefix " ")
1151 (message-yank-empty-prefix " ")
1152 (message-citation-line-format "On %e %B %Y %R, %f wrote:\n"))
1153 "Message citation style used by Gmail. Use with message-cite-style.")
1154
eec82323
LMI
1155(defcustom message-distribution-function nil
1156 "*Function called to return a Distribution header."
1157 :group 'message-news
1158 :group 'message-headers
23f87bed 1159 :link '(custom-manual "(message)News Headers")
7d829636 1160 :type '(choice function (const nil)))
eec82323
LMI
1161
1162(defcustom message-expires 14
1163 "Number of days before your article expires."
1164 :group 'message-news
1165 :group 'message-headers
1166 :link '(custom-manual "(message)News Headers")
1167 :type 'integer)
1168
1169(defcustom message-user-path nil
1170 "If nil, use the NNTP server name in the Path header.
1171If stringp, use this; if non-nil, use no host name (user name only)."
1172 :group 'message-news
1173 :group 'message-headers
1174 :link '(custom-manual "(message)News Headers")
1175 :type '(choice (const :tag "nntp" nil)
1176 (string :tag "name")
1177 (sexp :tag "none" :format "%t" t)))
1178
b1ea3797
CY
1179;; This can be the name of a buffer, or a cons cell (FUNCTION . ARGS)
1180;; for yanking the original buffer.
eec82323 1181(defvar message-reply-buffer nil)
23f87bed
MB
1182(defvar message-reply-headers nil
1183 "The headers of the current replied article.
1184It is a vector of the following headers:
1185\[number subject from date id references chars lines xref extra].")
eec82323
LMI
1186(defvar message-newsreader nil)
1187(defvar message-mailer nil)
1188(defvar message-sent-message-via nil)
1189(defvar message-checksum nil)
1190(defvar message-send-actions nil
1191 "A list of actions to be performed upon successful sending of a message.")
25ca2e61 1192(defvar message-return-action nil
9173deec 1193 "Action to return to the caller after sending or postponing a message.")
eec82323
LMI
1194(defvar message-exit-actions nil
1195 "A list of actions to be performed upon exiting after sending a message.")
1196(defvar message-kill-actions nil
1197 "A list of actions to be performed before killing a message buffer.")
1198(defvar message-postpone-actions nil
1199 "A list of actions to be performed after postponing a message.")
1200
6748645f
LMI
1201(define-widget 'message-header-lines 'text
1202 "All header lines must be LFD terminated."
3536d0c1 1203 :format "%{%t%}:%n%v"
6748645f
LMI
1204 :valid-regexp "^\\'"
1205 :error "All header lines must be newline terminated")
1206
481134fd 1207(defcustom message-default-headers ""
b5c575e6
G
1208 "Header lines to be inserted in outgoing messages.
1209This can be set to a string containing or a function returning
1210header lines to be inserted before you edit the message, so you
1211can edit or delete these lines. If set to a function, it is
1212called and its result is inserted."
937e60c8 1213 :version "23.2"
eec82323 1214 :group 'message-headers
23f87bed 1215 :link '(custom-manual "(message)Message Headers")
b5c575e6
G
1216 :type '(choice
1217 (message-header-lines :tag "String")
1218 (function :tag "Function")))
eec82323 1219
dc4d6273
GM
1220(defcustom message-default-mail-headers
1221 ;; Ease the transition from mail-mode to message-mode. See bugs#4431, 5555.
1222 (concat (if (and (boundp 'mail-default-reply-to)
1223 (stringp mail-default-reply-to))
481134fd 1224 (format "Reply-to: %s\n" mail-default-reply-to))
dc4d6273
GM
1225 (if (and (boundp 'mail-self-blind)
1226 mail-self-blind)
481134fd 1227 (format "BCC: %s\n" user-mail-address))
dc4d6273
GM
1228 (if (and (boundp 'mail-archive-file-name)
1229 (stringp mail-archive-file-name))
481134fd
CY
1230 (format "FCC: %s\n" mail-archive-file-name))
1231 ;; Use the value of `mail-default-headers' if available.
5b5dafd2
KY
1232 ;; Note: as for XEmacs 21.4 and 21.5, it is unavailable
1233 ;; unless sendmail.el is loaded.
481134fd
CY
1234 (if (boundp 'mail-default-headers)
1235 mail-default-headers))
eec82323 1236 "*A string of header lines to be inserted in outgoing mails."
dc4d6273 1237 :version "23.2"
eec82323
LMI
1238 :group 'message-headers
1239 :group 'message-mail
23f87bed 1240 :link '(custom-manual "(message)Mail Headers")
6748645f 1241 :type 'message-header-lines)
eec82323
LMI
1242
1243(defcustom message-default-news-headers ""
16409b0b 1244 "*A string of header lines to be inserted in outgoing news articles."
eec82323
LMI
1245 :group 'message-headers
1246 :group 'message-news
23f87bed 1247 :link '(custom-manual "(message)News Headers")
6748645f 1248 :type 'message-header-lines)
eec82323
LMI
1249
1250;; Note: could use /usr/ucb/mail instead of sendmail;
1251;; options -t, and -v if not interactive.
1252(defcustom message-mailer-swallows-blank-line
1253 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
1254 system-configuration)
1255 (file-readable-p "/etc/sendmail.cf")
8dabbfd6
SM
1256 (with-temp-buffer
1257 (insert-file-contents "/etc/sendmail.cf")
1258 (goto-char (point-min))
1259 (let ((case-fold-search nil))
1260 (re-search-forward "^OR\\>" nil t))))
eec82323
LMI
1261 ;; According to RFC822, "The field-name must be composed of printable
1262 ;; ASCII characters (i. e., characters that have decimal values between
1263 ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
1264 ;; space, or colon.
1265 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
6748645f 1266 "*Set this non-nil if the system's mailer runs the header and body together.
eec82323
LMI
1267\(This problem exists on Sunos 4 when sendmail is run in remote mode.)
1268The value should be an expression to test whether the problem will
1269actually occur."
1270 :group 'message-sending
23f87bed 1271 :link '(custom-manual "(message)Mail Variables")
eec82323
LMI
1272 :type 'sexp)
1273
961a48db 1274;;;###autoload
16409b0b
GM
1275(define-mail-user-agent 'message-user-agent
1276 'message-mail 'message-send-and-exit
1277 'message-kill-buffer 'message-send-hook)
eec82323
LMI
1278
1279(defvar message-mh-deletable-headers '(Message-ID Date Lines Sender)
1280 "If non-nil, delete the deletable headers before feeding to mh.")
1281
a8151ef7
LMI
1282(defvar message-send-method-alist
1283 '((news message-news-p message-send-via-news)
1284 (mail message-mail-p message-send-via-mail))
1285 "Alist of ways to send outgoing messages.
1286Each element has the form
1287
1288 \(TYPE PREDICATE FUNCTION)
1289
1290where TYPE is a symbol that names the method; PREDICATE is a function
1291called without any parameters to determine whether the message is
1292a message of type TYPE; and FUNCTION is a function to be called if
1293PREDICATE returns non-nil. FUNCTION is called with one parameter --
1294the prefix.")
1295
c541eb9a 1296(defcustom message-mail-alias-type 'abbrev
a8151ef7 1297 "*What alias expansion type to use in Message buffers.
01c52d31
MB
1298The default is `abbrev', which uses mailabbrev. `ecomplete' uses
1299an electric completion mode. nil switches mail aliases off.
1300This can also be a list of values."
c541eb9a
DL
1301 :group 'message
1302 :link '(custom-manual "(message)Mail Aliases")
1303 :type '(choice (const :tag "Use Mailabbrev" abbrev)
01c52d31 1304 (const :tag "Use ecomplete" ecomplete)
c541eb9a 1305 (const :tag "No expansion" nil)))
a8151ef7 1306
01c52d31
MB
1307(defcustom message-self-insert-commands '(self-insert-command)
1308 "List of `self-insert-command's used to trigger ecomplete.
1309When one of those commands is invoked to enter a character in To or Cc
1310header, ecomplete will suggest the candidates of recipients (see also
1311`message-mail-alias-type'). If you use some tool to enter non-ASCII
1312text and it replaces `self-insert-command' with the other command, e.g.
1313`egg-self-insert-command', you may want to add it to this list."
1314 :group 'message-various
1315 :type '(repeat function))
1316
6748645f 1317(defcustom message-auto-save-directory
4dcb0d7a 1318 (if (file-writable-p message-directory)
64a465b2
LMI
1319 (file-name-as-directory (expand-file-name "drafts" message-directory))
1320 "~/")
6748645f
LMI
1321 "*Directory where Message auto-saves buffers if Gnus isn't running.
1322If nil, Message won't auto-save."
1323 :group 'message-buffers
23f87bed 1324 :link '(custom-manual "(message)Various Message Variables")
7d829636 1325 :type '(choice directory (const :tag "Don't auto-save" nil)))
6748645f 1326
7d829636 1327(defcustom message-default-charset
83595c0c
DL
1328 (and (not (mm-multibyte-p)) 'iso-8859-1)
1329 "Default charset used in non-MULE Emacsen.
1330If nil, you might be asked to input the charset."
2d447df6 1331 :version "21.1"
16409b0b 1332 :group 'message
23f87bed 1333 :link '(custom-manual "(message)Various Message Variables")
16409b0b
GM
1334 :type 'symbol)
1335
7d829636 1336(defcustom message-dont-reply-to-names
0fb40182 1337 (and (boundp 'mail-dont-reply-to-names) mail-dont-reply-to-names)
01c52d31 1338 "*Addresses to prune when doing wide replies.
82816ba1 1339This can be a regexp or a list of regexps. Also, a value of nil means
01c52d31 1340exclude your own user name only."
2a1e2476 1341 :version "24.3"
16409b0b 1342 :group 'message
23f87bed 1343 :link '(custom-manual "(message)Wide Reply")
16409b0b 1344 :type '(choice (const :tag "Yourself" nil)
01c52d31
MB
1345 regexp
1346 (repeat :tag "Regexp List" regexp)))
1347
1348(defsubst message-dont-reply-to-names ()
1349 (gmm-regexp-concat message-dont-reply-to-names))
16409b0b 1350
23f87bed
MB
1351(defvar message-shoot-gnksa-feet nil
1352 "*A list of GNKSA feet you are allowed to shoot.
1353Gnus gives you all the opportunity you could possibly want for
1354shooting yourself in the foot. Also, Gnus allows you to shoot the
1355feet of Good Net-Keeping Seal of Approval. The following are foot
1356candidates:
1357`empty-article' Allow you to post an empty article;
1358`quoted-text-only' Allow you to post quoted text only;
1359`multiple-copies' Allow you to post multiple copies;
1360`cancel-messages' Allow you to cancel or supersede messages from
4dcb0d7a
LMI
1361 your other email addresses;
1362`canlock-verify' Allow you to cancel messages without verifying canlock.")
23f87bed
MB
1363
1364(defsubst message-gnksa-enable-p (feature)
1365 (or (not (listp message-shoot-gnksa-feet))
1366 (memq feature message-shoot-gnksa-feet)))
1367
01c52d31
MB
1368(defcustom message-hidden-headers '("^References:" "^Face:" "^X-Face:"
1369 "^X-Draft-From:")
23f87bed
MB
1370 "Regexp of headers to be hidden when composing new messages.
1371This can also be a list of regexps to match headers. Or a list
1372starting with `not' and followed by regexps."
bf247b6e 1373 :version "22.1"
23f87bed
MB
1374 :group 'message
1375 :link '(custom-manual "(message)Message Headers")
01c52d31
MB
1376 :type '(choice
1377 :format "%{%t%}: %[Value Type%] %v"
1378 (regexp :menu-tag "regexp" :format "regexp\n%t: %v")
1379 (repeat :menu-tag "(regexp ...)" :format "(regexp ...)\n%v%i"
1380 (regexp :format "%t: %v"))
1381 (cons :menu-tag "(not regexp ...)" :format "(not regexp ...)\n%v"
1382 (const not)
1383 (repeat :format "%v%i"
1384 (regexp :format "%t: %v")))))
1385
1386(defcustom message-cite-articles-with-x-no-archive t
1387 "If non-nil, cite text from articles that has X-No-Archive set."
1388 :group 'message
1389 :type 'boolean)
23f87bed 1390
eec82323
LMI
1391;;; Internal variables.
1392;;; Well, not really internal.
1393
1394(defvar message-mode-syntax-table
1395 (let ((table (copy-syntax-table text-mode-syntax-table)))
1396 (modify-syntax-entry ?% ". " table)
16409b0b
GM
1397 (modify-syntax-entry ?> ". " table)
1398 (modify-syntax-entry ?< ". " table)
eec82323
LMI
1399 table)
1400 "Syntax table used while in Message mode.")
1401
0f49874b 1402(defface message-header-to
eec82323
LMI
1403 '((((class color)
1404 (background dark))
01c52d31 1405 (:foreground "DarkOliveGreen1" :bold t))
eec82323
LMI
1406 (((class color)
1407 (background light))
23f87bed 1408 (:foreground "MidnightBlue" :bold t))
eec82323 1409 (t
23f87bed 1410 (:bold t :italic t)))
eec82323
LMI
1411 "Face used for displaying From headers."
1412 :group 'message-faces)
0f49874b
MB
1413;; backward-compatibility alias
1414(put 'message-header-to-face 'face-alias 'message-header-to)
3d493bef 1415(put 'message-header-to-face 'obsolete-face "22.1")
eec82323 1416
0f49874b 1417(defface message-header-cc
eec82323
LMI
1418 '((((class color)
1419 (background dark))
01c52d31 1420 (:foreground "chartreuse1" :bold t))
eec82323
LMI
1421 (((class color)
1422 (background light))
1423 (:foreground "MidnightBlue"))
1424 (t
23f87bed 1425 (:bold t)))
eec82323
LMI
1426 "Face used for displaying Cc headers."
1427 :group 'message-faces)
0f49874b
MB
1428;; backward-compatibility alias
1429(put 'message-header-cc-face 'face-alias 'message-header-cc)
3d493bef 1430(put 'message-header-cc-face 'obsolete-face "22.1")
eec82323 1431
0f49874b 1432(defface message-header-subject
eec82323
LMI
1433 '((((class color)
1434 (background dark))
01c52d31 1435 (:foreground "OliveDrab1"))
eec82323
LMI
1436 (((class color)
1437 (background light))
23f87bed 1438 (:foreground "navy blue" :bold t))
eec82323 1439 (t
23f87bed 1440 (:bold t)))
eec82323
LMI
1441 "Face used for displaying subject headers."
1442 :group 'message-faces)
0f49874b
MB
1443;; backward-compatibility alias
1444(put 'message-header-subject-face 'face-alias 'message-header-subject)
3d493bef 1445(put 'message-header-subject-face 'obsolete-face "22.1")
eec82323 1446
0f49874b 1447(defface message-header-newsgroups
eec82323
LMI
1448 '((((class color)
1449 (background dark))
23f87bed 1450 (:foreground "yellow" :bold t :italic t))
eec82323
LMI
1451 (((class color)
1452 (background light))
23f87bed 1453 (:foreground "blue4" :bold t :italic t))
eec82323 1454 (t
23f87bed 1455 (:bold t :italic t)))
eec82323
LMI
1456 "Face used for displaying newsgroups headers."
1457 :group 'message-faces)
0f49874b
MB
1458;; backward-compatibility alias
1459(put 'message-header-newsgroups-face 'face-alias 'message-header-newsgroups)
3d493bef 1460(put 'message-header-newsgroups-face 'obsolete-face "22.1")
eec82323 1461
0f49874b 1462(defface message-header-other
eec82323
LMI
1463 '((((class color)
1464 (background dark))
01c52d31 1465 (:foreground "VioletRed1"))
eec82323
LMI
1466 (((class color)
1467 (background light))
1468 (:foreground "steel blue"))
1469 (t
23f87bed 1470 (:bold t :italic t)))
eec82323
LMI
1471 "Face used for displaying newsgroups headers."
1472 :group 'message-faces)
0f49874b
MB
1473;; backward-compatibility alias
1474(put 'message-header-other-face 'face-alias 'message-header-other)
3d493bef 1475(put 'message-header-other-face 'obsolete-face "22.1")
eec82323 1476
0f49874b 1477(defface message-header-name
eec82323
LMI
1478 '((((class color)
1479 (background dark))
01c52d31 1480 (:foreground "green"))
eec82323
LMI
1481 (((class color)
1482 (background light))
1483 (:foreground "cornflower blue"))
1484 (t
23f87bed 1485 (:bold t)))
eec82323
LMI
1486 "Face used for displaying header names."
1487 :group 'message-faces)
0f49874b
MB
1488;; backward-compatibility alias
1489(put 'message-header-name-face 'face-alias 'message-header-name)
3d493bef 1490(put 'message-header-name-face 'obsolete-face "22.1")
eec82323 1491
0f49874b 1492(defface message-header-xheader
eec82323
LMI
1493 '((((class color)
1494 (background dark))
01c52d31 1495 (:foreground "DeepSkyBlue1"))
eec82323
LMI
1496 (((class color)
1497 (background light))
1498 (:foreground "blue"))
1499 (t
23f87bed 1500 (:bold t)))
eec82323
LMI
1501 "Face used for displaying X-Header headers."
1502 :group 'message-faces)
0f49874b
MB
1503;; backward-compatibility alias
1504(put 'message-header-xheader-face 'face-alias 'message-header-xheader)
3d493bef 1505(put 'message-header-xheader-face 'obsolete-face "22.1")
eec82323 1506
0f49874b 1507(defface message-separator
eec82323
LMI
1508 '((((class color)
1509 (background dark))
01c52d31 1510 (:foreground "LightSkyBlue1"))
eec82323
LMI
1511 (((class color)
1512 (background light))
1513 (:foreground "brown"))
1514 (t
23f87bed 1515 (:bold t)))
eec82323
LMI
1516 "Face used for displaying the separator."
1517 :group 'message-faces)
0f49874b
MB
1518;; backward-compatibility alias
1519(put 'message-separator-face 'face-alias 'message-separator)
3d493bef 1520(put 'message-separator-face 'obsolete-face "22.1")
eec82323 1521
0f49874b 1522(defface message-cited-text
eec82323
LMI
1523 '((((class color)
1524 (background dark))
01c52d31 1525 (:foreground "LightPink1"))
eec82323
LMI
1526 (((class color)
1527 (background light))
1528 (:foreground "red"))
1529 (t
23f87bed 1530 (:bold t)))
eec82323
LMI
1531 "Face used for displaying cited text names."
1532 :group 'message-faces)
0f49874b
MB
1533;; backward-compatibility alias
1534(put 'message-cited-text-face 'face-alias 'message-cited-text)
3d493bef 1535(put 'message-cited-text-face 'obsolete-face "22.1")
eec82323 1536
0f49874b 1537(defface message-mml
16409b0b
GM
1538 '((((class color)
1539 (background dark))
01c52d31 1540 (:foreground "MediumSpringGreen"))
16409b0b
GM
1541 (((class color)
1542 (background light))
1543 (:foreground "ForestGreen"))
1544 (t
23f87bed 1545 (:bold t)))
16409b0b
GM
1546 "Face used for displaying MML."
1547 :group 'message-faces)
0f49874b
MB
1548;; backward-compatibility alias
1549(put 'message-mml-face 'face-alias 'message-mml)
3d493bef 1550(put 'message-mml-face 'obsolete-face "22.1")
16409b0b 1551
23f87bed
MB
1552(defun message-font-lock-make-header-matcher (regexp)
1553 (let ((form
1554 `(lambda (limit)
1555 (let ((start (point)))
1556 (save-restriction
1557 (widen)
1558 (goto-char (point-min))
1559 (if (re-search-forward
1560 (concat "^" (regexp-quote mail-header-separator) "$")
1561 nil t)
1562 (setq limit (min limit (match-beginning 0))))
1563 (goto-char start))
1564 (and (< start limit)
1565 (re-search-forward ,regexp limit t))))))
1566 (if (featurep 'bytecomp)
1567 (byte-compile form)
1568 form)))
1569
eec82323 1570(defvar message-font-lock-keywords
23f87bed
MB
1571 (let ((content "[ \t]*\\(.+\\(\n[ \t].*\\)*\\)\n?"))
1572 `((,(message-font-lock-make-header-matcher
1573 (concat "^\\([Tt]o:\\)" content))
0f49874b
MB
1574 (1 'message-header-name)
1575 (2 'message-header-to nil t))
23f87bed
MB
1576 (,(message-font-lock-make-header-matcher
1577 (concat "^\\(^[GBF]?[Cc][Cc]:\\|^[Rr]eply-[Tt]o:\\)" content))
0f49874b
MB
1578 (1 'message-header-name)
1579 (2 'message-header-cc nil t))
23f87bed
MB
1580 (,(message-font-lock-make-header-matcher
1581 (concat "^\\([Ss]ubject:\\)" content))
0f49874b
MB
1582 (1 'message-header-name)
1583 (2 'message-header-subject nil t))
23f87bed
MB
1584 (,(message-font-lock-make-header-matcher
1585 (concat "^\\([Nn]ewsgroups:\\|Followup-[Tt]o:\\)" content))
0f49874b
MB
1586 (1 'message-header-name)
1587 (2 'message-header-newsgroups nil t))
23f87bed 1588 (,(message-font-lock-make-header-matcher
01c52d31 1589 (concat "^\\(X-[A-Za-z0-9-]+:\\|In-Reply-To:\\)" content))
0f49874b 1590 (1 'message-header-name)
01c52d31 1591 (2 'message-header-xheader))
23f87bed 1592 (,(message-font-lock-make-header-matcher
01c52d31 1593 (concat "^\\([A-Z][^: \n\t]+:\\)" content))
0f49874b 1594 (1 'message-header-name)
01c52d31 1595 (2 'message-header-other nil t))
6748645f
LMI
1596 ,@(if (and mail-header-separator
1597 (not (equal mail-header-separator "")))
1598 `((,(concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
0f49874b 1599 1 'message-separator))
6748645f 1600 nil)
23f87bed
MB
1601 ((lambda (limit)
1602 (re-search-forward (concat "^\\("
1603 message-cite-prefix-regexp
1604 "\\).*")
1605 limit t))
0f49874b 1606 (0 'message-cited-text))
23f87bed 1607 ("<#/?\\(multipart\\|part\\|external\\|mml\\|secure\\)[^>]*>"
0f49874b 1608 (0 'message-mml))))
eec82323
LMI
1609 "Additional expressions to highlight in Message mode.")
1610
23f87bed 1611
6748645f
LMI
1612;; XEmacs does it like this. For Emacs, we have to set the
1613;; `font-lock-defaults' buffer-local variable.
1614(put 'message-mode 'font-lock-defaults '(message-font-lock-keywords t))
1615
eec82323 1616(defvar message-face-alist
01c52d31 1617 '((bold . message-bold-region)
eec82323
LMI
1618 (underline . underline-region)
1619 (default . (lambda (b e)
01c52d31 1620 (message-unbold-region b e)
eec82323
LMI
1621 (ununderline-region b e))))
1622 "Alist of mail and news faces for facemenu.
8f688cb0 1623The cdr of each entry is a function for applying the face to a region.")
eec82323 1624
4e14547f
CY
1625(defcustom message-send-hook nil
1626 "Hook run before sending messages.
1627This hook is run quite early when sending."
1628 :group 'message-various
1629 :options '(ispell-message)
1630 :link '(custom-manual "(message)Various Message Variables")
1631 :type 'hook)
eec82323
LMI
1632
1633(defcustom message-send-mail-hook nil
23f87bed
MB
1634 "Hook run before sending mail messages.
1635This hook is run very late -- just before the message is sent as
1636mail."
eec82323 1637 :group 'message-various
23f87bed 1638 :link '(custom-manual "(message)Various Message Variables")
eec82323
LMI
1639 :type 'hook)
1640
1641(defcustom message-send-news-hook nil
23f87bed
MB
1642 "Hook run before sending news messages.
1643This hook is run very late -- just before the message is sent as
1644news."
eec82323 1645 :group 'message-various
23f87bed 1646 :link '(custom-manual "(message)Various Message Variables")
eec82323
LMI
1647 :type 'hook)
1648
1649(defcustom message-sent-hook nil
1650 "Hook run after sending messages."
1651 :group 'message-various
1652 :type 'hook)
1653
6748645f
LMI
1654(defvar message-send-coding-system 'binary
1655 "Coding system to encode outgoing mail.")
1656
16409b0b
GM
1657(defvar message-draft-coding-system
1658 mm-auto-save-coding-system
23f87bed
MB
1659 "*Coding system to compose mail.
1660If you'd like to make it possible to share draft files between XEmacs
1661and Emacs, you may use `iso-2022-7bit' for this value at your own risk.
1662Note that the coding-system `iso-2022-7bit' isn't suitable to all data.")
16409b0b 1663
8bff7c00 1664(defcustom message-send-mail-partially-limit nil
16409b0b 1665 "The limitation of messages sent as message/partial.
7d829636
DL
1666The lower bound of message size in characters, beyond which the message
1667should be sent in several parts. If it is nil, the size is unlimited."
1a10d421 1668 :version "24.1"
16409b0b 1669 :group 'message-buffers
23f87bed 1670 :link '(custom-manual "(message)Mail Variables")
16409b0b
GM
1671 :type '(choice (const :tag "unlimited" nil)
1672 (integer 1000000)))
1673
83595c0c 1674(defcustom message-alternative-emails nil
46e8fe3d
MB
1675 "*Regexp matching alternative email addresses.
1676The first address in the To, Cc or From headers of the original
1677article matching this variable is used as the From field of
1678outgoing messages.
1679
1680This variable has precedence over posting styles and anything that runs
1681off `message-setup-hook'."
83595c0c 1682 :group 'message-headers
23f87bed 1683 :link '(custom-manual "(message)Message Headers")
83595c0c
DL
1684 :type '(choice (const :tag "Always use primary" nil)
1685 regexp))
1686
23f87bed
MB
1687(defcustom message-hierarchical-addresses nil
1688 "A list of hierarchical mail address definitions.
1689
1690Inside each entry, the first address is the \"top\" address, and
1691subsequent addresses are subaddresses; this is used to indicate that
1692mail sent to the first address will automatically be delivered to the
1693subaddresses. So if the first address appears in the recipient list
1694for a message, the subaddresses will be removed (if present) before
1695the mail is sent. All addresses in this structure should be
1696downcased."
bf247b6e 1697 :version "22.1"
23f87bed
MB
1698 :group 'message-headers
1699 :type '(repeat (repeat string)))
1700
88818fbe
SZ
1701(defcustom message-mail-user-agent nil
1702 "Like `mail-user-agent'.
7d829636 1703Except if it is nil, use Gnus native MUA; if it is t, use
88818fbe 1704`mail-user-agent'."
bf247b6e 1705 :version "22.1"
88818fbe
SZ
1706 :type '(radio (const :tag "Gnus native"
1707 :format "%t\n"
1708 nil)
1709 (const :tag "`mail-user-agent'"
1710 :format "%t\n"
1711 t)
1712 (function-item :tag "Default Emacs mail"
1713 :format "%t\n"
1714 sendmail-user-agent)
1715 (function-item :tag "Emacs interface to MH"
1716 :format "%t\n"
1717 mh-e-user-agent)
1718 (function :tag "Other"))
1719 :version "21.1"
1720 :group 'message)
1721
23f87bed
MB
1722(defcustom message-wide-reply-confirm-recipients nil
1723 "Whether to confirm a wide reply to multiple email recipients.
1724If this variable is nil, don't ask whether to reply to all recipients.
1725If this variable is non-nil, pose the question \"Reply to all
1726recipients?\" before a wide reply to multiple recipients. If the user
1727answers yes, reply to all recipients as usual. If the user answers
1728no, only reply back to the author."
bf247b6e 1729 :version "22.1"
23f87bed
MB
1730 :group 'message-headers
1731 :link '(custom-manual "(message)Wide Reply")
1732 :type 'boolean)
1733
1734(defcustom message-user-fqdn nil
bcc7dd61 1735 "*Domain part of Message-Ids."
bf247b6e 1736 :version "22.1"
23f87bed
MB
1737 :group 'message-headers
1738 :link '(custom-manual "(message)News Headers")
1739 :type '(radio (const :format "%v " nil)
ad136a7c 1740 (string :format "FQDN: %v")))
23f87bed
MB
1741
1742(defcustom message-use-idna (and (condition-case nil (require 'idna)
1743 (file-error))
1744 (mm-coding-system-p 'utf-8)
1745 (executable-find idna-program)
c38e0c97 1746 (string= (idna-to-ascii "räksmörgås")
a33704bb
MB
1747 "xn--rksmrgs-5wao1o")
1748 t)
1749 "Whether to encode non-ASCII in domain names into ASCII according to IDNA.
1750GNU Libidn, and in particular the elisp package \"idna.el\" and
1751the external program \"idn\", must be installed for this
1752functionality to work."
bf247b6e 1753 :version "22.1"
23f87bed
MB
1754 :group 'message-headers
1755 :link '(custom-manual "(message)IDNA")
1756 :type '(choice (const :tag "Ask" ask)
1757 (const :tag "Never" nil)
1758 (const :tag "Always" t)))
1759
5d97d032 1760(defcustom message-generate-hashcash (if (executable-find "hashcash") 'opportunistic)
01c52d31 1761 "*Whether to generate X-Hashcash: headers.
82816ba1 1762If t, always generate hashcash headers. If `opportunistic',
01c52d31
MB
1763only generate hashcash headers if it can be done without the user
1764waiting (i.e., only asynchronously).
1765
1766You must have the \"hashcash\" binary installed, see `hashcash-path'."
5d97d032 1767 :version "24.1"
01c52d31
MB
1768 :group 'message-headers
1769 :link '(custom-manual "(message)Mail Headers")
1770 :type '(choice (const :tag "Always" t)
1771 (const :tag "Never" nil)
1772 (const :tag "Opportunistic" opportunistic)))
1773
eec82323
LMI
1774;;; Internal variables.
1775
83595c0c 1776(defvar message-sending-message "Sending...")
eec82323
LMI
1777(defvar message-buffer-list nil)
1778(defvar message-this-is-news nil)
1779(defvar message-this-is-mail nil)
6748645f 1780(defvar message-draft-article nil)
16409b0b
GM
1781(defvar message-mime-part nil)
1782(defvar message-posting-charset nil)
23f87bed 1783(defvar message-inserted-headers nil)
6b8382e4 1784(defvar message-inhibit-ecomplete nil)
eec82323
LMI
1785
1786;; Byte-compiler warning
9efa445f
DN
1787(defvar gnus-active-hashtb)
1788(defvar gnus-read-active-file)
eec82323
LMI
1789
1790;;; Regexp matching the delimiter of messages in UNIX mail format
05d70628
RS
1791;;; (UNIX From lines), minus the initial ^. It should be a copy
1792;;; of rmail.el's rmail-unix-mail-delimiter.
eec82323
LMI
1793(defvar message-unix-mail-delimiter
1794 (let ((time-zone-regexp
1795 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
1796 "\\|[-+]?[0-9][0-9][0-9][0-9]"
1797 "\\|"
1798 "\\) *")))
1799 (concat
1800 "From "
1801
05d70628
RS
1802 ;; Many things can happen to an RFC 822 mailbox before it is put into
1803 ;; a `From' line. The leading phrase can be stripped, e.g.
1804 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
1805 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
1806 ;; can be removed, e.g.
1807 ;; From: joe@y.z (Joe K
1808 ;; User)
23f87bed 1809 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
05d70628
RS
1810 ;; From: Joe User
1811 ;; <joe@y.z>
1812 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
1813 ;; The mailbox can be removed or be replaced by white space, e.g.
1814 ;; From: "Joe User"{space}{tab}
1815 ;; <joe@y.z>
1816 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
1817 ;; where {space} and {tab} represent the Ascii space and tab characters.
1818 ;; We want to match the results of any of these manglings.
1819 ;; The following regexp rejects names whose first characters are
1820 ;; obviously bogus, but after that anything goes.
23f87bed 1821 "\\([^\0-\b\n-\r\^?].*\\)?"
eec82323
LMI
1822
1823 ;; The time the message was sent.
16409b0b
GM
1824 "\\([^\0-\r \^?]+\\) +" ; day of the week
1825 "\\([^\0-\r \^?]+\\) +" ; month
1826 "\\([0-3]?[0-9]\\) +" ; day of month
1827 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
eec82323
LMI
1828
1829 ;; Perhaps a time zone, specified by an abbreviation, or by a
1830 ;; numeric offset.
1831 time-zone-regexp
1832
1833 ;; The year.
05d70628 1834 " \\([0-9][0-9]+\\) *"
eec82323
LMI
1835
1836 ;; On some systems the time zone can appear after the year, too.
1837 time-zone-regexp
1838
1839 ;; Old uucp cruft.
1840 "\\(remote from .*\\)?"
1841
05d70628 1842 "\n"))
6748645f 1843 "Regexp matching the delimiter of messages in UNIX mail format.")
eec82323
LMI
1844
1845(defvar message-unsent-separator
1846 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1847 "^ *---+ +Returned message +---+ *$\\|"
1848 "^Start of returned message$\\|"
1849 "^ *---+ +Original message +---+ *$\\|"
1850 "^ *--+ +begin message +--+ *$\\|"
1851 "^ *---+ +Original message follows +---+ *$\\|"
16409b0b 1852 "^ *---+ +Undelivered message follows +---+ *$\\|"
52bec650 1853 "^------ This is a copy of the message, including all the headers. ------ *$\\|"
eec82323
LMI
1854 "^|? *---+ +Message text follows: +---+ *|?$")
1855 "A regexp that matches the separator before the text of a failed message.")
1856
01c52d31
MB
1857(defvar message-field-fillers
1858 '((To message-fill-field-address)
1859 (Cc message-fill-field-address)
1860 (From message-fill-field-address))
1861 "Alist of header names/filler functions.")
1862
eec82323 1863(defvar message-header-format-alist
01c52d31
MB
1864 `((From)
1865 (Newsgroups)
1866 (To)
1867 (Cc)
eec82323
LMI
1868 (Subject)
1869 (In-Reply-To)
1870 (Fcc)
1871 (Bcc)
1872 (Date)
1873 (Organization)
1874 (Distribution)
1875 (Lines)
1876 (Expires)
1877 (Message-ID)
6748645f 1878 (References . message-shorten-references)
16409b0b 1879 (User-Agent))
eec82323
LMI
1880 "Alist used for formatting headers.")
1881
23f87bed
MB
1882(defvar message-options nil
1883 "Some saved answers when sending message.")
8879add8
DU
1884;; FIXME: On XEmacs this causes problems since let-binding like:
1885;; (let ((message-options message-options)) ...)
1886;; as in `message-send' and `mml-preview' loses to buffer-local
1887;; variable initialization.
1888(unless (featurep 'xemacs)
1889 (make-variable-buffer-local 'message-options))
23f87bed
MB
1890
1891(defvar message-send-mail-real-function nil
1892 "Internal send mail function.")
1893
70f6144d 1894(defvar message-bogus-system-names "\\`localhost\\.\\|\\.local\\'"
23f87bed
MB
1895 "The regexp of bogus system names.")
1896
1897(defcustom message-valid-fqdn-regexp
1898 (concat "[a-z0-9][-.a-z0-9]+\\." ;; [hostname.subdomain.]domain.
1899 ;; valid TLDs:
0565caeb 1900 "\\([a-z][a-z]\\|" ;; two letter country TDLs
bacede64 1901 "aero\\|arpa\\|asia\\|bitnet\\|biz\\|bofh\\|"
0565caeb
MB
1902 "cat\\|com\\|coop\\|edu\\|gov\\|"
1903 "info\\|int\\|jobs\\|"
1904 "mil\\|mobi\\|museum\\|name\\|net\\|"
bacede64 1905 "org\\|pro\\|tel\\|travel\\|uucp\\)")
0565caeb
MB
1906 ;; http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
1907 ;; http://en.wikipedia.org/wiki/GTLD
bacede64 1908 ;; `approved, but not yet in operation': .xxx
0565caeb 1909 ;; "dead" nato bitnet uucp
23f87bed
MB
1910 "Regular expression that matches a valid FQDN."
1911 ;; see also: gnus-button-valid-fqdn-regexp
bf247b6e 1912 :version "22.1"
23f87bed
MB
1913 :group 'message-headers
1914 :type 'regexp)
1915
8abf1b22
GM
1916(autoload 'gnus-alive-p "gnus-util")
1917(autoload 'gnus-delay-article "gnus-delay")
1918(autoload 'gnus-extract-address-components "gnus-util")
1919(autoload 'gnus-find-method-for-group "gnus")
1920(autoload 'gnus-group-decoded-name "gnus-group")
1921(autoload 'gnus-group-name-charset "gnus-group")
1922(autoload 'gnus-group-name-decode "gnus-group")
1923(autoload 'gnus-groups-from-server "gnus")
1924(autoload 'gnus-make-local-hook "gnus-util")
1925(autoload 'gnus-open-server "gnus-int")
1926(autoload 'gnus-output-to-mail "gnus-util")
1927(autoload 'gnus-output-to-rmail "gnus-util")
1928(autoload 'gnus-request-post "gnus-int")
1929(autoload 'gnus-select-frame-set-input-focus "gnus-util")
1930(autoload 'gnus-server-string "gnus")
1931(autoload 'idna-to-ascii "idna")
1932(autoload 'message-setup-toolbar "messagexmas")
1933(autoload 'mh-new-draft-name "mh-comp")
1934(autoload 'mh-send-letter "mh-comp")
1935(autoload 'nndraft-request-associate-buffer "nndraft")
1936(autoload 'nndraft-request-expire-articles "nndraft")
1937(autoload 'nnvirtual-find-group-art "nnvirtual")
8abf1b22 1938(autoload 'rmail-msg-is-pruned "rmail")
8abf1b22 1939(autoload 'rmail-output "rmailout")
eec82323 1940
69c3b839
JD
1941;; Emacs < 24.1 do not have mail-dont-reply-to
1942(unless (fboundp 'mail-dont-reply-to)
1943 (defalias 'mail-dont-reply-to 'rmail-dont-reply-to))
1944
eec82323
LMI
1945\f
1946
1947;;;
1948;;; Utility functions.
1949;;;
1950
1951(defmacro message-y-or-n-p (question show &rest text)
7d829636 1952 "Ask QUESTION, displaying remaining args in a temporary buffer if SHOW."
eec82323
LMI
1953 `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
1954
eec82323 1955(defmacro message-delete-line (&optional n)
7d829636 1956 "Delete the current line (and the next N lines)."
eec82323
LMI
1957 `(delete-region (progn (beginning-of-line) (point))
1958 (progn (forward-line ,(or n 1)) (point))))
1959
23f87bed
MB
1960(defun message-mark-active-p ()
1961 "Non-nil means the mark and region are currently active in this buffer."
1962 mark-active)
1963
16409b0b 1964(defun message-unquote-tokens (elems)
7d829636 1965 "Remove double quotes (\") from strings in list ELEMS."
16409b0b 1966 (mapcar (lambda (item)
23f87bed
MB
1967 (while (string-match "^\\(.*\\)\"\\(.*\\)$" item)
1968 (setq item (concat (match-string 1 item)
1969 (match-string 2 item))))
1970 item)
1971 elems))
16409b0b 1972
eec82323
LMI
1973(defun message-tokenize-header (header &optional separator)
1974 "Split HEADER into a list of header elements.
16409b0b
GM
1975SEPARATOR is a string of characters to be used as separators. \",\"
1976is used by default."
eec82323
LMI
1977 (if (not header)
1978 nil
1979 (let ((regexp (format "[%s]+" (or separator ",")))
eec82323 1980 (first t)
ebbeed62 1981 beg quoted elems paren)
23f87bed
MB
1982 (with-temp-buffer
1983 (mm-enable-multibyte)
ebbeed62 1984 (setq beg (point-min))
eec82323
LMI
1985 (insert header)
1986 (goto-char (point-min))
1987 (while (not (eobp))
1988 (if first
1989 (setq first nil)
1990 (forward-char 1))
1991 (cond ((and (> (point) beg)
1992 (or (eobp)
1993 (and (looking-at regexp)
1994 (not quoted)
1995 (not paren))))
1996 (push (buffer-substring beg (point)) elems)
1997 (setq beg (match-end 0)))
16409b0b 1998 ((eq (char-after) ?\")
eec82323 1999 (setq quoted (not quoted)))
16409b0b 2000 ((and (eq (char-after) ?\()
eec82323
LMI
2001 (not quoted))
2002 (setq paren t))
16409b0b 2003 ((and (eq (char-after) ?\))
eec82323
LMI
2004 (not quoted))
2005 (setq paren nil))))
23f87bed 2006 (nreverse elems)))))
eec82323 2007
aa8f8277
GM
2008(autoload 'nnheader-insert-file-contents "nnheader")
2009
eec82323
LMI
2010(defun message-mail-file-mbox-p (file)
2011 "Say whether FILE looks like a Unix mbox file."
2012 (when (and (file-exists-p file)
2013 (file-readable-p file)
2014 (file-regular-p file))
16409b0b 2015 (with-temp-buffer
eec82323
LMI
2016 (nnheader-insert-file-contents file)
2017 (goto-char (point-min))
2018 (looking-at message-unix-mail-delimiter))))
2019
2020(defun message-fetch-field (header &optional not-all)
23f87bed
MB
2021 "The same as `mail-fetch-field', only remove all newlines.
2022The buffer is expected to be narrowed to just the header of the message;
2023see `message-narrow-to-headers-or-head'."
6748645f
LMI
2024 (let* ((inhibit-point-motion-hooks t)
2025 (value (mail-fetch-field header nil (not not-all))))
eec82323 2026 (when value
16409b0b
GM
2027 (while (string-match "\n[\t ]+" value)
2028 (setq value (replace-match " " t t value)))
16409b0b
GM
2029 value)))
2030
23f87bed
MB
2031(defun message-field-value (header &optional not-all)
2032 "The same as `message-fetch-field', only narrow to the headers first."
2033 (save-excursion
2034 (save-restriction
2035 (message-narrow-to-headers-or-head)
2036 (message-fetch-field header not-all))))
2037
16409b0b
GM
2038(defun message-narrow-to-field ()
2039 "Narrow the buffer to the header on the current line."
2040 (beginning-of-line)
01c52d31
MB
2041 (while (looking-at "[ \t]")
2042 (forward-line -1))
16409b0b
GM
2043 (narrow-to-region
2044 (point)
2045 (progn
2046 (forward-line 1)
2047 (if (re-search-forward "^[^ \n\t]" nil t)
01c52d31 2048 (point-at-bol)
16409b0b
GM
2049 (point-max))))
2050 (goto-char (point-min)))
eec82323
LMI
2051
2052(defun message-add-header (&rest headers)
2053 "Add the HEADERS to the message header, skipping those already present."
2054 (while headers
2055 (let (hclean)
2056 (unless (string-match "^\\([^:]+\\):[ \t]*[^ \t]" (car headers))
2057 (error "Invalid header `%s'" (car headers)))
2058 (setq hclean (match-string 1 (car headers)))
16409b0b
GM
2059 (save-restriction
2060 (message-narrow-to-headers)
2061 (unless (re-search-forward (concat "^" (regexp-quote hclean) ":") nil t)
23f87bed
MB
2062 (goto-char (point-max))
2063 (if (string-match "\n$" (car headers))
2064 (insert (car headers))
f129a4df 2065 (insert (car headers) ?\n)))))
eec82323
LMI
2066 (setq headers (cdr headers))))
2067
23f87bed
MB
2068(defmacro message-with-reply-buffer (&rest forms)
2069 "Evaluate FORMS in the reply buffer, if it exists."
b1ea3797 2070 `(when (and (bufferp message-reply-buffer)
23f87bed 2071 (buffer-name message-reply-buffer))
82816ba1 2072 (with-current-buffer message-reply-buffer
23f87bed
MB
2073 ,@forms)))
2074
2075(put 'message-with-reply-buffer 'lisp-indent-function 0)
2076(put 'message-with-reply-buffer 'edebug-form-spec '(body))
16409b0b 2077
eec82323 2078(defun message-fetch-reply-field (header)
7d829636 2079 "Fetch field HEADER from the message we're replying to."
23f87bed
MB
2080 (message-with-reply-buffer
2081 (save-restriction
2082 (mail-narrow-to-head)
eec82323
LMI
2083 (message-fetch-field header))))
2084
16409b0b 2085(defun message-strip-list-identifiers (subject)
7d829636 2086 "Remove list identifiers in `gnus-list-identifiers' from string SUBJECT."
16409b0b
GM
2087 (require 'gnus-sum) ; for gnus-list-identifiers
2088 (let ((regexp (if (stringp gnus-list-identifiers)
2089 gnus-list-identifiers
2090 (mapconcat 'identity gnus-list-identifiers " *\\|"))))
7d829636 2091 (if (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
23f87bed 2092 " *\\)\\)+\\(Re: +\\)?\\)") subject)
16409b0b
GM
2093 (concat (substring subject 0 (match-beginning 1))
2094 (or (match-string 3 subject)
2095 (match-string 5 subject))
2096 (substring subject
2097 (match-end 1)))
2098 subject)))
2099
eec82323 2100(defun message-strip-subject-re (subject)
7d829636 2101 "Remove \"Re:\" from subject lines in string SUBJECT."
6748645f 2102 (if (string-match message-subject-re-regexp subject)
eec82323
LMI
2103 (substring subject (match-end 0))
2104 subject))
2105
4573e0df
MB
2106(defcustom message-replacement-char "."
2107 "Replacement character used instead of unprintable or not decodable chars."
2108 :group 'message-various
2109 :version "22.1" ;; Gnus 5.10.9
2110 :type '(choice string
2111 (const ".")
2112 (const "?")))
2113
2114;; FIXME: We also should call `message-strip-subject-encoded-words'
2115;; when forwarding. Probably in `message-make-forward-subject' and
2116;; `message-forward-make-body'.
2117
2118(defun message-strip-subject-encoded-words (subject)
2119 "Fix non-decodable words in SUBJECT."
2120 ;; Cf. `gnus-simplify-subject-fully'.
2121 (let* ((case-fold-search t)
2122 (replacement-chars (format "[%s%s%s]"
2123 message-replacement-char
2124 message-replacement-char
2125 message-replacement-char))
2126 (enc-word-re "=\\?\\([^?]+\\)\\?\\([QB]\\)\\?\\([^?]+\\)\\(\\?=\\)")
2127 cs-string
2128 (have-marker
2129 (with-temp-buffer
2130 (insert subject)
2131 (goto-char (point-min))
2132 (when (re-search-forward enc-word-re nil t)
2133 (setq cs-string (match-string 1)))))
2134 cs-coding q-or-b word-beg word-end)
2135 (if (or (not have-marker) ;; No encoded word found...
2136 ;; ... or double encoding was correct:
2137 (and (stringp cs-string)
2138 (setq cs-string (downcase cs-string))
2139 (mm-coding-system-p (intern cs-string))
2140 (not (prog1
2141 (y-or-n-p
2142 (format "\
2143Decoded Subject \"%s\"
2144contains a valid encoded word. Decode again? "
2145 subject))
2146 (setq cs-coding (intern cs-string))))))
2147 subject
2148 (with-temp-buffer
2149 (insert subject)
2150 (goto-char (point-min))
2151 (while (re-search-forward enc-word-re nil t)
2152 (setq cs-string (downcase (match-string 1))
2153 q-or-b (match-string 2)
2154 word-beg (match-beginning 0)
2155 word-end (match-end 0))
2156 (setq cs-coding
2157 (if (mm-coding-system-p (intern cs-string))
2158 (setq cs-coding (intern cs-string))
2159 nil))
2160 ;; No double encoded subject? => bogus charset.
2161 (unless cs-coding
2162 (setq cs-coding
2163 (mm-read-coding-system
2164 (format "\
2165Decoded Subject \"%s\"
2166contains an encoded word. The charset `%s' is unknown or invalid.
2167Hit RET to replace non-decodable characters with \"%s\" or enter replacement
2168charset: "
2169 subject cs-string message-replacement-char)))
2170 (if cs-coding
2171 (replace-match (concat "=?" (symbol-name cs-coding)
2172 "?\\2?\\3\\4\\5"))
2173 (save-excursion
2174 (goto-char word-beg)
2175 (re-search-forward "=\\?\\([^?]+\\)\\?\\([QB]\\)\\?" word-end t)
2176 (replace-match "")
2177 ;; QP or base64
2178 (if (string-match "\\`Q\\'" q-or-b)
2179 ;; QP
2180 (progn
2181 (message "Replacing non-decodable characters with \"%s\"."
2182 message-replacement-char)
2183 (while (re-search-forward "\\(=[A-F0-9][A-F0-9]\\)+"
2184 word-end t)
2185 (replace-match message-replacement-char)))
2186 ;; base64
2187 (message "Replacing non-decodable characters with \"%s\"."
2188 replacement-chars)
2189 (re-search-forward "[^?]+" word-end t)
2190 (replace-match replacement-chars))
2191 (re-search-forward "\\?=")
2192 (replace-match "")))))
2193 (rfc2047-decode-region (point-min) (point-max))
2194 (buffer-string)))))
2195
23f87bed
MB
2196;;; Start of functions adopted from `message-utils.el'.
2197
2198(defun message-strip-subject-trailing-was (subject)
5f5475ac 2199 "Remove trailing \"(was: <old subject>)\" from SUBJECT lines.
23f87bed
MB
2200Leading \"Re: \" is not stripped by this function. Use the function
2201`message-strip-subject-re' for this."
2202 (let* ((query message-subject-trailing-was-query)
2203 (new) (found))
2204 (setq found
2205 (string-match
2206 (if (eq query 'ask)
2207 message-subject-trailing-was-ask-regexp
2208 message-subject-trailing-was-regexp)
2209 subject))
2210 (if found
2211 (setq new (substring subject 0 (match-beginning 0))))
2212 (if (or (not found) (eq query nil))
2213 subject
2214 (if (eq query 'ask)
2215 (if (message-y-or-n-p
2216 "Strip `(was: <old subject>)' in subject? " t
2217 (concat
2218 "Strip `(was: <old subject>)' in subject "
2219 "and use the new one instead?\n\n"
2220 "Current subject is: \""
2221 subject "\"\n\n"
2222 "New subject would be: \""
2223 new "\"\n\n"
2224 "See the variable `message-subject-trailing-was-query' "
2225 "to get rid of this query."
2226 ))
2227 new subject)
2228 new))))
2229
2230;;; Suggested by Jonas Steverud @ www.dtek.chalmers.se/~d4jonas/
2231
23f87bed
MB
2232(defun message-change-subject (new-subject)
2233 "Ask for NEW-SUBJECT header, append (was: <Old Subject>)."
23f87bed
MB
2234 (interactive
2235 (list
2236 (read-from-minibuffer "New subject: ")))
2237 (cond ((and (not (or (null new-subject) ; new subject not empty
2238 (zerop (string-width new-subject))
2239 (string-match "^[ \t]*$" new-subject))))
2240 (save-excursion
2241 (let ((old-subject
2242 (save-restriction
2243 (message-narrow-to-headers)
2244 (message-fetch-field "Subject"))))
2245 (cond ((not old-subject)
2246 (error "No current subject"))
2247 ((not (string-match
2248 (concat "^[ \t]*"
2249 (regexp-quote new-subject)
2250 " \t]*$")
2251 old-subject)) ; yes, it really is a new subject
2252 ;; delete eventual Re: prefix
2253 (setq old-subject
2254 (message-strip-subject-re old-subject))
2255 (message-goto-subject)
2256 (message-delete-line)
2257 (insert (concat "Subject: "
2258 new-subject
2259 " (was: "
f129a4df 2260 old-subject ")\n")))))))))
23f87bed 2261
01c52d31 2262(defun message-mark-inserted-region (beg end &optional verbatim)
23f87bed 2263 "Mark some region in the current article with enclosing tags.
01c52d31
MB
2264See `message-mark-insert-begin' and `message-mark-insert-end'.
2265If VERBATIM, use slrn style verbatim marks (\"#v+\" and \"#v-\")."
2266 (interactive "r\nP")
23f87bed
MB
2267 (save-excursion
2268 ;; add to the end of the region first, otherwise end would be invalid
2269 (goto-char end)
01c52d31 2270 (insert (if verbatim "#v-\n" message-mark-insert-end))
23f87bed 2271 (goto-char beg)
01c52d31 2272 (insert (if verbatim "#v+\n" message-mark-insert-begin))))
23f87bed 2273
01c52d31 2274(defun message-mark-insert-file (file &optional verbatim)
23f87bed 2275 "Insert FILE at point, marking it with enclosing tags.
01c52d31
MB
2276See `message-mark-insert-begin' and `message-mark-insert-end'.
2277If VERBATIM, use slrn style verbatim marks (\"#v+\" and \"#v-\")."
2278 (interactive "fFile to insert: \nP")
23f87bed
MB
2279 ;; reverse insertion to get correct result.
2280 (let ((p (point)))
01c52d31 2281 (insert (if verbatim "#v-\n" message-mark-insert-end))
23f87bed
MB
2282 (goto-char p)
2283 (insert-file-contents file)
2284 (goto-char p)
01c52d31 2285 (insert (if verbatim "#v+\n" message-mark-insert-begin))))
23f87bed 2286
23f87bed
MB
2287(defun message-add-archive-header ()
2288 "Insert \"X-No-Archive: Yes\" in the header and a note in the body.
2289The note can be customized using `message-archive-note'. When called with a
2290prefix argument, ask for a text to insert. If you don't want the note in the
2291body, set `message-archive-note' to nil."
2292 (interactive)
2293 (if current-prefix-arg
2294 (setq message-archive-note
2295 (read-from-minibuffer "Reason for No-Archive: "
2296 (cons message-archive-note 0))))
2297 (save-excursion
2298 (if (message-goto-signature)
2299 (re-search-backward message-signature-separator))
2300 (when message-archive-note
2301 (insert message-archive-note)
2302 (newline))
2303 (message-add-header message-archive-header)
2304 (message-sort-headers)))
2305
23f87bed
MB
2306(defun message-cross-post-followup-to-header (target-group)
2307 "Mangles FollowUp-To and Newsgroups header to point to TARGET-GROUP.
2308With prefix-argument just set Follow-Up, don't cross-post."
2309 (interactive
2310 (list ; Completion based on Gnus
2311 (completing-read "Followup To: "
2312 (if (boundp 'gnus-newsrc-alist)
2313 gnus-newsrc-alist)
2314 nil nil '("poster" . 0)
2315 (if (boundp 'gnus-group-history)
2316 'gnus-group-history))))
2317 (message-remove-header "Follow[Uu]p-[Tt]o" t)
2318 (message-goto-newsgroups)
2319 (beginning-of-line)
2320 ;; if we already did a crosspost before, kill old target
2321 (if (and message-cross-post-old-target
2322 (re-search-forward
2323 (regexp-quote (concat "," message-cross-post-old-target))
2324 nil t))
2325 (replace-match ""))
2326 ;; unless (followup is to poster or user explicitly asked not
2327 ;; to cross-post, or target-group is already in Newsgroups)
2328 ;; add target-group to Newsgroups line.
2329 (cond ((and (or
2330 ;; def: cross-post, req:no
2331 (and message-cross-post-default (not current-prefix-arg))
2332 ;; def: no-cross-post, req:yes
2333 (and (not message-cross-post-default) current-prefix-arg))
2334 (not (string-match "poster" target-group))
2335 (not (string-match (regexp-quote target-group)
2336 (message-fetch-field "Newsgroups"))))
2337 (end-of-line)
2338 (insert (concat "," target-group))))
2339 (end-of-line) ; ensure Followup: comes after Newsgroups:
2340 ;; unless new followup would be identical to Newsgroups line
2341 ;; make a new Followup-To line
2342 (if (not (string-match (concat "^[ \t]*"
2343 target-group
2344 "[ \t]*$")
2345 (message-fetch-field "Newsgroups")))
f129a4df 2346 (insert (concat "\nFollowup-To: " target-group)))
23f87bed
MB
2347 (setq message-cross-post-old-target target-group))
2348
23f87bed
MB
2349(defun message-cross-post-insert-note (target-group cross-post in-old
2350 old-groups)
2351 "Insert a in message body note about a set Followup or Crosspost.
2352If there have been previous notes, delete them. TARGET-GROUP specifies the
2353group to Followup-To. When CROSS-POST is t, insert note about
2354crossposting. IN-OLD specifies whether TARGET-GROUP is a member of
2355OLD-GROUPS. OLD-GROUPS lists the old-groups the posting would have
2356been made to before the user asked for a Crosspost."
2357 ;; start scanning body for previous uses
2358 (message-goto-signature)
2359 (let ((head (re-search-backward
2360 (concat "^" mail-header-separator)
2361 nil t))) ; just search in body
2362 (message-goto-signature)
2363 (while (re-search-backward
2364 (concat "^" (regexp-quote message-cross-post-note) ".*")
2365 head t)
2366 (message-delete-line))
2367 (message-goto-signature)
2368 (while (re-search-backward
2369 (concat "^" (regexp-quote message-followup-to-note) ".*")
2370 head t)
2371 (message-delete-line))
2372 ;; insert new note
2373 (if (message-goto-signature)
2374 (re-search-backward message-signature-separator))
2375 (if (or in-old
2376 (not cross-post)
2377 (string-match "^[ \t]*poster[ \t]*$" target-group))
f129a4df
CY
2378 (insert (concat message-followup-to-note target-group "\n"))
2379 (insert (concat message-cross-post-note target-group "\n")))))
23f87bed 2380
23f87bed
MB
2381(defun message-cross-post-followup-to (target-group)
2382 "Crossposts message and set Followup-To to TARGET-GROUP.
2383With prefix-argument just set Follow-Up, don't cross-post."
2384 (interactive
2385 (list ; Completion based on Gnus
2386 (completing-read "Followup To: "
2387 (if (boundp 'gnus-newsrc-alist)
2388 gnus-newsrc-alist)
2389 nil nil '("poster" . 0)
2390 (if (boundp 'gnus-group-history)
2391 'gnus-group-history))))
2392 (cond ((not (or (null target-group) ; new subject not empty
2393 (zerop (string-width target-group))
2394 (string-match "^[ \t]*$" target-group)))
2395 (save-excursion
2396 (let* ((old-groups (message-fetch-field "Newsgroups"))
2397 (in-old (string-match
2398 (regexp-quote target-group)
2399 (or old-groups ""))))
2400 ;; check whether target exactly matches old Newsgroups
2401 (cond ((not old-groups)
2402 (error "No current newsgroup"))
2403 ((or (not in-old)
2404 (not (string-match
2405 (concat "^[ \t]*"
2406 (regexp-quote target-group)
2407 "[ \t]*$")
2408 old-groups)))
2409 ;; yes, Newsgroups line must change
2410 (message-cross-post-followup-to-header target-group)
2411 ;; insert note whether we do cross-post or followup-to
2412 (funcall message-cross-post-note-function
2413 target-group
2414 (if (or (and message-cross-post-default
2415 (not current-prefix-arg))
2416 (and (not message-cross-post-default)
2417 current-prefix-arg)) t)
2418 in-old old-groups))))))))
2419
2420;;; Reduce To: to Cc: or Bcc: header
2421
23f87bed
MB
2422(defun message-reduce-to-to-cc ()
2423 "Replace contents of To: header with contents of Cc: or Bcc: header."
2424 (interactive)
2425 (let ((cc-content
2426 (save-restriction (message-narrow-to-headers)
2427 (message-fetch-field "cc")))
2428 (bcc nil))
2429 (if (and (not cc-content)
2430 (setq cc-content
2431 (save-restriction
2432 (message-narrow-to-headers)
2433 (message-fetch-field "bcc"))))
2434 (setq bcc t))
2435 (cond (cc-content
2436 (save-excursion
2437 (message-goto-to)
2438 (message-delete-line)
f129a4df 2439 (insert (concat "To: " cc-content "\n"))
23f87bed
MB
2440 (save-restriction
2441 (message-narrow-to-headers)
2442 (message-remove-header (if bcc
2443 "bcc"
2444 "cc"))))))))
2445
2446;;; End of functions adopted from `message-utils.el'.
2447
eec82323
LMI
2448(defun message-remove-header (header &optional is-regexp first reverse)
2449 "Remove HEADER in the narrowed buffer.
7d829636 2450If IS-REGEXP, HEADER is a regular expression.
eec82323
LMI
2451If FIRST, only remove the first instance of the header.
2452Return the number of headers removed."
2453 (goto-char (point-min))
6748645f 2454 (let ((regexp (if is-regexp header (concat "^" (regexp-quote header) ":")))
eec82323
LMI
2455 (number 0)
2456 (case-fold-search t)
2457 last)
2458 (while (and (not (eobp))
2459 (not last))
2460 (if (if reverse
2461 (not (looking-at regexp))
2462 (looking-at regexp))
2463 (progn
2464 (incf number)
2465 (when first
2466 (setq last t))
2467 (delete-region
2468 (point)
2469 ;; There might be a continuation header, so we have to search
2470 ;; until we find a new non-continuation line.
2471 (progn
2472 (forward-line 1)
2473 (if (re-search-forward "^[^ \t]" nil t)
2474 (goto-char (match-beginning 0))
2475 (point-max)))))
2476 (forward-line 1)
2477 (if (re-search-forward "^[^ \t]" nil t)
2478 (goto-char (match-beginning 0))
16409b0b 2479 (goto-char (point-max)))))
eec82323
LMI
2480 number))
2481
16409b0b
GM
2482(defun message-remove-first-header (header)
2483 "Remove the first instance of HEADER if there is more than one."
2484 (let ((count 0)
2485 (regexp (concat "^" (regexp-quote header) ":")))
2486 (save-excursion
2487 (goto-char (point-min))
2488 (while (re-search-forward regexp nil t)
2489 (incf count)))
2490 (while (> count 1)
2491 (message-remove-header header nil t)
2492 (decf count))))
2493
eec82323
LMI
2494(defun message-narrow-to-headers ()
2495 "Narrow the buffer to the head of the message."
2496 (widen)
2497 (narrow-to-region
2498 (goto-char (point-min))
2499 (if (re-search-forward
2500 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
2501 (match-beginning 0)
2502 (point-max)))
2503 (goto-char (point-min)))
2504
158d6e07 2505(defun message-narrow-to-head-1 ()
7d829636 2506 "Like `message-narrow-to-head'. Don't widen."
eec82323
LMI
2507 (narrow-to-region
2508 (goto-char (point-min))
2509 (if (search-forward "\n\n" nil 1)
2510 (1- (point))
2511 (point-max)))
2512 (goto-char (point-min)))
2513
4c36be58 2514;; FIXME: clarify difference: message-narrow-to-head,
e3e955fe 2515;; message-narrow-to-headers-or-head, message-narrow-to-headers
158d6e07
SZ
2516(defun message-narrow-to-head ()
2517 "Narrow the buffer to the head of the message.
2518Point is left at the beginning of the narrowed-to region."
2519 (widen)
2520 (message-narrow-to-head-1))
2521
16409b0b
GM
2522(defun message-narrow-to-headers-or-head ()
2523 "Narrow the buffer to the head of the message."
2524 (widen)
2525 (narrow-to-region
2526 (goto-char (point-min))
d05b3cfd
CY
2527 (if (re-search-forward (concat "\\(\n\\)\n\\|^\\("
2528 (regexp-quote mail-header-separator)
2529 "\n\\)")
2530 nil t)
2531 (or (match-end 1) (match-beginning 2))
2532 (point-max)))
16409b0b
GM
2533 (goto-char (point-min)))
2534
eec82323
LMI
2535(defun message-news-p ()
2536 "Say whether the current buffer contains a news message."
6748645f
LMI
2537 (and (not message-this-is-mail)
2538 (or message-this-is-news
2539 (save-excursion
2540 (save-restriction
2541 (message-narrow-to-headers)
2542 (and (message-fetch-field "newsgroups")
2543 (not (message-fetch-field "posted-to"))))))))
eec82323
LMI
2544
2545(defun message-mail-p ()
2546 "Say whether the current buffer contains a mail message."
6748645f
LMI
2547 (and (not message-this-is-news)
2548 (or message-this-is-mail
2549 (save-excursion
2550 (save-restriction
2551 (message-narrow-to-headers)
2552 (or (message-fetch-field "to")
2553 (message-fetch-field "cc")
2554 (message-fetch-field "bcc")))))))
eec82323 2555
23f87bed
MB
2556(defun message-subscribed-p ()
2557 "Say whether we need to insert a MFT header."
2558 (or message-subscribed-regexps
2559 message-subscribed-addresses
2560 message-subscribed-address-file
2561 message-subscribed-address-functions))
2562
eec82323
LMI
2563(defun message-next-header ()
2564 "Go to the beginning of the next header."
2565 (beginning-of-line)
2566 (or (eobp) (forward-char 1))
2567 (not (if (re-search-forward "^[^ \t]" nil t)
2568 (beginning-of-line)
2569 (goto-char (point-max)))))
2570
2571(defun message-sort-headers-1 ()
2572 "Sort the buffer as headers using `message-rank' text props."
2573 (goto-char (point-min))
16409b0b 2574 (require 'sort)
eec82323
LMI
2575 (sort-subr
2576 nil 'message-next-header
2577 (lambda ()
2578 (message-next-header)
2579 (unless (bobp)
2580 (forward-char -1)))
2581 (lambda ()
2582 (or (get-text-property (point) 'message-rank)
2583 10000))))
2584
2585(defun message-sort-headers ()
2586 "Sort the headers of the current message according to `message-header-format-alist'."
2587 (interactive)
2588 (save-excursion
2589 (save-restriction
2590 (let ((max (1+ (length message-header-format-alist)))
2591 rank)
2592 (message-narrow-to-headers)
2593 (while (re-search-forward "^[^ \n]+:" nil t)
2594 (put-text-property
2595 (match-beginning 0) (1+ (match-beginning 0))
2596 'message-rank
2597 (if (setq rank (length (memq (assq (intern (buffer-substring
2598 (match-beginning 0)
2599 (1- (match-end 0))))
2600 message-header-format-alist)
2601 message-header-format-alist)))
2602 (- max rank)
2603 (1+ max)))))
2604 (message-sort-headers-1))))
2605
01c52d31
MB
2606(defun message-kill-address ()
2607 "Kill the address under point."
2608 (interactive)
2609 (let ((start (point)))
2610 (message-skip-to-next-address)
0000d0d5 2611 (kill-region start (if (bolp) (1- (point)) (point)))))
01c52d31
MB
2612
2613
ea7d768a 2614(autoload 'Info-goto-node "info")
a0e08467 2615(defvar mml2015-use)
ea7d768a 2616
c4288669
MB
2617(defun message-info (&optional arg)
2618 "Display the Message manual.
2619
9b3ebcb6
MB
2620Prefixed with one \\[universal-argument], display the Emacs MIME
2621manual. With two \\[universal-argument]'s, display the EasyPG or
2622PGG manual, depending on the value of `mml2015-use'."
c4288669 2623 (interactive "p")
4a9b5bf5
RS
2624 ;; Don't use `info' because support for `(filename)nodename' is not
2625 ;; available in XEmacs < 21.5.12.
9b3ebcb6 2626 (Info-goto-node (format "(%s)Top"
a0e08467
GM
2627 (cond ((eq arg 16)
2628 (require 'mml2015)
2629 mml2015-use)
9b3ebcb6
MB
2630 ((eq arg 4) 'emacs-mime)
2631 ;; `booleanp' only available in Emacs 22+
2632 ((and (not (memq arg '(nil t)))
2633 (symbolp arg))
2634 arg)
2635 (t
2636 'message)))))
23f87bed 2637
eec82323
LMI
2638\f
2639
2640;;;
2641;;; Message mode
2642;;;
2643
2644;;; Set up keymap.
2645
2646(defvar message-mode-map nil)
2647
2648(unless message-mode-map
16409b0b
GM
2649 (setq message-mode-map (make-keymap))
2650 (set-keymap-parent message-mode-map text-mode-map)
eec82323
LMI
2651 (define-key message-mode-map "\C-c?" 'describe-mode)
2652
2653 (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
23f87bed 2654 (define-key message-mode-map "\C-c\C-f\C-o" 'message-goto-from)
eec82323
LMI
2655 (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
2656 (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
2657 (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
2658 (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
2659 (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
2660 (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
2661 (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
2662 (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
23f87bed 2663 (define-key message-mode-map "\C-c\C-f\C-m" 'message-goto-mail-followup-to)
eec82323
LMI
2664 (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
2665 (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
23f87bed
MB
2666 (define-key message-mode-map "\C-c\C-f\C-i"
2667 'message-insert-or-toggle-importance)
2668 (define-key message-mode-map "\C-c\C-f\C-a"
2669 'message-generate-unsubscribed-mail-followup-to)
2670
2671 ;; modify headers (and insert notes in body)
2672 (define-key message-mode-map "\C-c\C-fs" 'message-change-subject)
2673 ;;
2674 (define-key message-mode-map "\C-c\C-fx" 'message-cross-post-followup-to)
2675 ;; prefix+message-cross-post-followup-to = same w/o cross-post
2676 (define-key message-mode-map "\C-c\C-ft" 'message-reduce-to-to-cc)
2677 (define-key message-mode-map "\C-c\C-fa" 'message-add-archive-header)
2678 ;; mark inserted text
2679 (define-key message-mode-map "\C-c\M-m" 'message-mark-inserted-region)
2680 (define-key message-mode-map "\C-c\M-f" 'message-mark-insert-file)
2681
eec82323
LMI
2682 (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
2683 (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
2684
2685 (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
23f87bed 2686 (define-key message-mode-map "\C-c\C-fw" 'message-insert-wide-reply)
eec82323 2687 (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
23f87bed 2688 (define-key message-mode-map "\C-c\C-l" 'message-to-list-only)
01c52d31 2689 (define-key message-mode-map "\C-c\C-f\C-e" 'message-insert-expires)
23f87bed
MB
2690
2691 (define-key message-mode-map "\C-c\C-u" 'message-insert-or-toggle-importance)
2692 (define-key message-mode-map "\C-c\M-n"
2693 'message-insert-disposition-notification-to)
eec82323
LMI
2694
2695 (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
16409b0b 2696 (define-key message-mode-map "\C-c\M-\C-y" 'message-yank-buffer)
eec82323
LMI
2697 (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
2698 (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
16409b0b 2699 (define-key message-mode-map "\C-c\M-h" 'message-insert-headers)
eec82323
LMI
2700 (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
2701 (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
2702 (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
2703
2704 (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
2705 (define-key message-mode-map "\C-c\C-s" 'message-send)
2706 (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
2707 (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
23f87bed 2708 (define-key message-mode-map "\C-c\n" 'gnus-delay-article)
eec82323 2709
01c52d31 2710 (define-key message-mode-map "\C-c\M-k" 'message-kill-address)
eec82323 2711 (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
6748645f
LMI
2712 (define-key message-mode-map "\C-c\C-v" 'message-delete-not-region)
2713 (define-key message-mode-map "\C-c\C-z" 'message-kill-to-signature)
2714 (define-key message-mode-map "\M-\r" 'message-newline-and-reformat)
10893bb6 2715 (define-key message-mode-map [remap split-line] 'message-split-line)
eec82323 2716
16409b0b
GM
2717 (define-key message-mode-map "\C-c\C-a" 'mml-attach-file)
2718
23f87bed
MB
2719 (define-key message-mode-map "\C-a" 'message-beginning-of-line)
2720 (define-key message-mode-map "\t" 'message-tab)
01c52d31
MB
2721
2722 (define-key message-mode-map "\M-n" 'message-display-abbrev))
eec82323
LMI
2723
2724(easy-menu-define
23f87bed
MB
2725 message-mode-menu message-mode-map "Message Menu."
2726 `("Message"
2727 ["Yank Original" message-yank-original message-reply-buffer]
2728 ["Fill Yanked Message" message-fill-yanked-message t]
2729 ["Insert Signature" message-insert-signature t]
2730 ["Caesar (rot13) Message" message-caesar-buffer-body t]
2731 ["Caesar (rot13) Region" message-caesar-region (message-mark-active-p)]
2732 ["Elide Region" message-elide-region
2733 :active (message-mark-active-p)
2734 ,@(if (featurep 'xemacs) nil
2735 '(:help "Replace text in region with an ellipsis"))]
2736 ["Delete Outside Region" message-delete-not-region
2737 :active (message-mark-active-p)
2738 ,@(if (featurep 'xemacs) nil
2739 '(:help "Delete all quoted text outside region"))]
2740 ["Kill To Signature" message-kill-to-signature t]
2741 ["Newline and Reformat" message-newline-and-reformat t]
2742 ["Rename buffer" message-rename-buffer t]
2743 ["Spellcheck" ispell-message
2744 ,@(if (featurep 'xemacs) '(t)
2745 '(:help "Spellcheck this message"))]
2746 "----"
2747 ["Insert Region Marked" message-mark-inserted-region
2748 :active (message-mark-active-p)
2749 ,@(if (featurep 'xemacs) nil
2750 '(:help "Mark region with enclosing tags"))]
2751 ["Insert File Marked..." message-mark-insert-file
2752 ,@(if (featurep 'xemacs) '(t)
2753 '(:help "Insert file at point marked with enclosing tags"))]
2754 "----"
2755 ["Send Message" message-send-and-exit
2756 ,@(if (featurep 'xemacs) '(t)
2757 '(:help "Send this message"))]
2758 ["Postpone Message" message-dont-send
2759 ,@(if (featurep 'xemacs) '(t)
2760 '(:help "File this draft message and exit"))]
2761 ["Send at Specific Time..." gnus-delay-article
2762 ,@(if (featurep 'xemacs) '(t)
2763 '(:help "Ask, then arrange to send message at that time"))]
2764 ["Kill Message" message-kill-buffer
2765 ,@(if (featurep 'xemacs) '(t)
c4288669
MB
2766 '(:help "Delete this message without sending"))]
2767 "----"
2768 ["Message manual" message-info
2769 ,@(if (featurep 'xemacs) '(t)
2770 '(:help "Display the Message manual"))]))
eec82323
LMI
2771
2772(easy-menu-define
23f87bed
MB
2773 message-mode-field-menu message-mode-map ""
2774 `("Field"
2775 ["To" message-goto-to t]
2776 ["From" message-goto-from t]
2777 ["Subject" message-goto-subject t]
2778 ["Change subject..." message-change-subject t]
2779 ["Cc" message-goto-cc t]
2780 ["Bcc" message-goto-bcc t]
2781 ["Fcc" message-goto-fcc t]
2782 ["Reply-To" message-goto-reply-to t]
2783 ["Flag As Important" message-insert-importance-high
2784 ,@(if (featurep 'xemacs) '(t)
2785 '(:help "Mark this message as important"))]
2786 ["Flag As Unimportant" message-insert-importance-low
2787 ,@(if (featurep 'xemacs) '(t)
2788 '(:help "Mark this message as unimportant"))]
2789 ["Request Receipt"
2790 message-insert-disposition-notification-to
2791 ,@(if (featurep 'xemacs) '(t)
2792 '(:help "Request a receipt notification"))]
2793 "----"
2794 ;; (typical) news stuff
2795 ["Summary" message-goto-summary t]
2796 ["Keywords" message-goto-keywords t]
2797 ["Newsgroups" message-goto-newsgroups t]
2798 ["Fetch Newsgroups" message-insert-newsgroups t]
2799 ["Followup-To" message-goto-followup-to t]
2800 ;; ["Followup-To (with note in body)" message-cross-post-followup-to t]
2801 ["Crosspost / Followup-To..." message-cross-post-followup-to t]
2802 ["Distribution" message-goto-distribution t]
01c52d31
MB
2803 ["Expires" message-insert-expires t ]
2804 ["X-No-Archive" message-add-archive-header t ]
23f87bed
MB
2805 "----"
2806 ;; (typical) mailing-lists stuff
2807 ["Fetch To" message-insert-to
2808 ,@(if (featurep 'xemacs) '(t)
2809 '(:help "Insert a To header that points to the author."))]
2810 ["Fetch To and Cc" message-insert-wide-reply
2811 ,@(if (featurep 'xemacs) '(t)
2812 '(:help
2813 "Insert To and Cc headers as if you were doing a wide reply."))]
2814 "----"
2815 ["Send to list only" message-to-list-only t]
2816 ["Mail-Followup-To" message-goto-mail-followup-to t]
2817 ["Unsubscribed list post" message-generate-unsubscribed-mail-followup-to
2818 ,@(if (featurep 'xemacs) '(t)
2819 '(:help "Insert a reasonable `Mail-Followup-To:' header."))]
2820 ["Reduce To: to Cc:" message-reduce-to-to-cc t]
2821 "----"
2822 ["Sort Headers" message-sort-headers t]
2823 ["Encode non-ASCII domain names" message-idna-to-ascii-rhs t]
01c52d31
MB
2824 ;; We hide `message-hidden-headers' by narrowing the buffer.
2825 ["Show Hidden Headers" widen t]
23f87bed
MB
2826 ["Goto Body" message-goto-body t]
2827 ["Goto Signature" message-goto-signature t]))
2828
2829(defvar message-tool-bar-map nil)
eec82323 2830
9efa445f
DN
2831(defvar facemenu-add-face-function)
2832(defvar facemenu-remove-face-function)
eec82323 2833
23f87bed
MB
2834;;; Forbidden properties
2835;;
2836;; We use `after-change-functions' to keep special text properties
dc4d6273 2837;; that interfere with the normal function of message mode out of the
23f87bed
MB
2838;; buffer.
2839
2840(defcustom message-strip-special-text-properties t
2841 "Strip special properties from the message buffer.
2842
2843Emacs has a number of special text properties which can break message
2844composing in various ways. If this option is set, message will strip
2845these properties from the message composition buffer. However, some
2846packages requires these properties to be present in order to work.
2847If you use one of these packages, turn this option off, and hope the
2848message composition doesn't break too bad."
bf247b6e 2849 :version "22.1"
23f87bed
MB
2850 :group 'message-various
2851 :link '(custom-manual "(message)Various Message Variables")
2852 :type 'boolean)
2853
2b7feab0 2854(defvar message-forbidden-properties
23f87bed
MB
2855 ;; No reason this should be clutter up customize. We make it a
2856 ;; property list (rather than a list of property symbols), to be
2857 ;; directly useful for `remove-text-properties'.
2858 '(field nil read-only nil invisible nil intangible nil
2859 mouse-face nil modification-hooks nil insert-in-front-hooks nil
2860 insert-behind-hooks nil point-entered nil point-left nil)
2861 ;; Other special properties:
2862 ;; category, face, display: probably doesn't do any harm.
2863 ;; fontified: is used by font-lock.
2864 ;; syntax-table, local-map: I dunno.
2865 ;; We need to add XEmacs names to the list.
ad136a7c 2866 "Property list of with properties forbidden in message buffers.
23f87bed
MB
2867The values of the properties are ignored, only the property names are used.")
2868
2869(defun message-tamago-not-in-use-p (pos)
2870 "Return t when tamago version 4 is not in use at the cursor position.
2871Tamago version 4 is a popular input method for writing Japanese text.
2872It uses the properties `intangible', `invisible', `modification-hooks'
2873and `read-only' when translating ascii or kana text to kanji text.
2874These properties are essential to work, so we should never strip them."
2875 (not (and (boundp 'egg-modefull-mode)
2876 (symbol-value 'egg-modefull-mode)
2877 (or (memq (get-text-property pos 'intangible)
2878 '(its-part-1 its-part-2))
2879 (get-text-property pos 'egg-end)
2880 (get-text-property pos 'egg-lang)
2881 (get-text-property pos 'egg-start)))))
2882
01c52d31
MB
2883(defsubst message-mail-alias-type-p (type)
2884 (if (atom message-mail-alias-type)
2885 (eq message-mail-alias-type type)
2886 (memq type message-mail-alias-type)))
2887
23f87bed
MB
2888(defun message-strip-forbidden-properties (begin end &optional old-length)
2889 "Strip forbidden properties between BEGIN and END, ignoring the third arg.
2890This function is intended to be called from `after-change-functions'.
2891See also `message-forbidden-properties'."
01c52d31
MB
2892 (when (and (message-mail-alias-type-p 'ecomplete)
2893 (memq this-command message-self-insert-commands))
2894 (message-display-abbrev))
23f87bed
MB
2895 (when (and message-strip-special-text-properties
2896 (message-tamago-not-in-use-p begin))
ad136a7c
MB
2897 (let ((buffer-read-only nil)
2898 (inhibit-read-only t))
01c52d31 2899 (remove-text-properties begin end message-forbidden-properties))))
23f87bed 2900
ec7995fa
KY
2901(autoload 'ecomplete-setup "ecomplete") ;; for Emacs <23.
2902
eec82323 2903;;;###autoload
23f87bed 2904(define-derived-mode message-mode text-mode "Message"
eec82323 2905 "Major mode for editing mail and news to be sent.
7d829636
DL
2906Like Text Mode but with these additional commands:\\<message-mode-map>
2907C-c C-s `message-send' (send the message) C-c C-c `message-send-and-exit'
2908C-c C-d Postpone sending the message C-c C-k Kill the message
eec82323
LMI
2909C-c C-f move to a header field (and create it if there isn't):
2910 C-c C-f C-t move to To C-c C-f C-s move to Subject
2911 C-c C-f C-c move to Cc C-c C-f C-b move to Bcc
2912 C-c C-f C-w move to Fcc C-c C-f C-r move to Reply-To
2913 C-c C-f C-u move to Summary C-c C-f C-n move to Newsgroups
2914 C-c C-f C-k move to Keywords C-c C-f C-d move to Distribution
01c52d31 2915 C-c C-f C-o move to From (\"Originator\")
eec82323 2916 C-c C-f C-f move to Followup-To
23f87bed 2917 C-c C-f C-m move to Mail-Followup-To
01c52d31 2918 C-c C-f C-e move to Expires
23f87bed
MB
2919 C-c C-f C-i cycle through Importance values
2920 C-c C-f s change subject and append \"(was: <Old Subject>)\"
2921 C-c C-f x crossposting with FollowUp-To header and note in body
2922 C-c C-f t replace To: header with contents of Cc: or Bcc:
2923 C-c C-f a Insert X-No-Archive: header and a note in the body
7d829636 2924C-c C-t `message-insert-to' (add a To header to a news followup)
23f87bed 2925C-c C-l `message-to-list-only' (removes all but list address in to/cc)
7d829636
DL
2926C-c C-n `message-insert-newsgroups' (add a Newsgroup header to a news reply)
2927C-c C-b `message-goto-body' (move to beginning of message text).
2928C-c C-i `message-goto-signature' (move to the beginning of the signature).
2929C-c C-w `message-insert-signature' (insert `message-signature-file' file).
2930C-c C-y `message-yank-original' (insert current message, if any).
2931C-c C-q `message-fill-yanked-message' (fill what was yanked).
2932C-c C-e `message-elide-region' (elide the text between point and mark).
2933C-c C-v `message-delete-not-region' (remove the text outside the region).
2934C-c C-z `message-kill-to-signature' (kill the text up to the signature).
2935C-c C-r `message-caesar-buffer-body' (rot13 the message body).
2936C-c C-a `mml-attach-file' (attach a file as MIME).
23f87bed
MB
2937C-c C-u `message-insert-or-toggle-importance' (insert or cycle importance).
2938C-c M-n `message-insert-disposition-notification-to' (request receipt).
2939C-c M-m `message-mark-inserted-region' (mark region with enclosing tags).
2940C-c M-f `message-mark-insert-file' (insert file marked with enclosing tags).
7d829636 2941M-RET `message-newline-and-reformat' (break the line and reformat)."
23f87bed 2942 (setq local-abbrev-table text-mode-abbrev-table)
16409b0b 2943 (set (make-local-variable 'message-reply-buffer) nil)
23f87bed
MB
2944 (set (make-local-variable 'message-inserted-headers) nil)
2945 (set (make-local-variable 'message-send-actions) nil)
25ca2e61 2946 (set (make-local-variable 'message-return-action) nil)
23f87bed
MB
2947 (set (make-local-variable 'message-exit-actions) nil)
2948 (set (make-local-variable 'message-kill-actions) nil)
2949 (set (make-local-variable 'message-postpone-actions) nil)
2950 (set (make-local-variable 'message-draft-article) nil)
eec82323 2951 (setq buffer-offer-save t)
23f87bed
MB
2952 (set (make-local-variable 'facemenu-add-face-function)
2953 (lambda (face end)
2954 (let ((face-fun (cdr (assq face message-face-alist))))
2955 (if face-fun
2956 (funcall face-fun (point) end)
2957 (error "Face %s not configured for %s mode" face mode-name)))
2958 ""))
2959 (set (make-local-variable 'facemenu-remove-face-function) t)
2960 (set (make-local-variable 'message-reply-headers) nil)
eec82323
LMI
2961 (make-local-variable 'message-newsreader)
2962 (make-local-variable 'message-mailer)
2963 (make-local-variable 'message-post-method)
16409b0b
GM
2964 (set (make-local-variable 'message-sent-message-via) nil)
2965 (set (make-local-variable 'message-checksum) nil)
2966 (set (make-local-variable 'message-mime-part) 0)
f8c0f31e 2967 (message-setup-fill-variables)
01c52d31
MB
2968 (when message-fill-column
2969 (setq fill-column message-fill-column)
2970 (turn-on-auto-fill))
980984d8 2971 ;; Allow using comment commands to add/remove quoting.
23f87bed 2972 ;; (set (make-local-variable 'comment-start) message-yank-prefix)
528f876a
SM
2973 (when message-yank-prefix
2974 (set (make-local-variable 'comment-start) message-yank-prefix)
2975 (set (make-local-variable 'comment-start-skip)
2976 (concat "^" (regexp-quote message-yank-prefix) "[ \t]*")))
16409b0b
GM
2977 (if (featurep 'xemacs)
2978 (message-setup-toolbar)
2979 (set (make-local-variable 'font-lock-defaults)
c47a0dc8 2980 '(message-font-lock-keywords t))
23f87bed 2981 (if (boundp 'tool-bar-map)
18c06a99 2982 (set (make-local-variable 'tool-bar-map) (message-make-tool-bar))))
eec82323
LMI
2983 (easy-menu-add message-mode-menu message-mode-map)
2984 (easy-menu-add message-mode-field-menu message-mode-map)
23f87bed
MB
2985 (gnus-make-local-hook 'after-change-functions)
2986 ;; Mmmm... Forbidden properties...
2987 (add-hook 'after-change-functions 'message-strip-forbidden-properties
2988 nil 'local)
eec82323 2989 ;; Allow mail alias things.
01c52d31
MB
2990 (cond
2991 ((message-mail-alias-type-p 'abbrev)
a8151ef7
LMI
2992 (if (fboundp 'mail-abbrevs-setup)
2993 (mail-abbrevs-setup)
23f87bed
MB
2994 (if (fboundp 'mail-aliases-setup) ; warning avoidance
2995 (mail-aliases-setup))))
01c52d31
MB
2996 ((message-mail-alias-type-p 'ecomplete)
2997 (ecomplete-setup)))
674c5ccf 2998 (add-hook 'completion-at-point-functions 'message-completion-function nil t)
3efe5554
SZ
2999 (unless buffer-file-name
3000 (message-set-auto-save-file-name))
23f87bed
MB
3001 (unless (buffer-base-buffer)
3002 ;; Don't enable multibyte on an indirect buffer. Maybe enabling
3003 ;; multibyte is not necessary at all. -- zsh
3004 (mm-enable-multibyte))
3005 (set (make-local-variable 'indent-tabs-mode) nil) ;No tabs for indentation.
3006 (mml-mode))
eec82323 3007
f8c0f31e
DL
3008(defun message-setup-fill-variables ()
3009 "Setup message fill variables."
23f87bed
MB
3010 (set (make-local-variable 'fill-paragraph-function)
3011 'message-fill-paragraph)
f8c0f31e
DL
3012 (make-local-variable 'paragraph-separate)
3013 (make-local-variable 'paragraph-start)
3014 (make-local-variable 'adaptive-fill-regexp)
3015 (unless (boundp 'adaptive-fill-first-line-regexp)
3016 (setq adaptive-fill-first-line-regexp nil))
3017 (make-local-variable 'adaptive-fill-first-line-regexp)
f8c0f31e 3018 (let ((quote-prefix-regexp
23f87bed
MB
3019 ;; User should change message-cite-prefix-regexp if
3020 ;; message-yank-prefix is set to an abnormal value.
3021 (concat "\\(" message-cite-prefix-regexp "\\)[ \t]*")))
f8c0f31e 3022 (setq paragraph-start
23f87bed
MB
3023 (concat
3024 (regexp-quote mail-header-separator) "$\\|"
3025 "[ \t]*$\\|" ; blank lines
3026 "-- $\\|" ; signature delimiter
3027 "---+$\\|" ; delimiters for forwarded messages
3028 page-delimiter "$\\|" ; spoiler warnings
3029 ".*wrote:$\\|" ; attribution lines
3030 quote-prefix-regexp "$\\|" ; empty lines in quoted text
3031 ; mml tags
3032 "<#!*/?\\(multipart\\|part\\|external\\|mml\\|secure\\)"))
f8c0f31e
DL
3033 (setq paragraph-separate paragraph-start)
3034 (setq adaptive-fill-regexp
23f87bed 3035 (concat quote-prefix-regexp "\\|" adaptive-fill-regexp))
f8c0f31e 3036 (setq adaptive-fill-first-line-regexp
23f87bed
MB
3037 (concat quote-prefix-regexp "\\|"
3038 adaptive-fill-first-line-regexp)))
3039 (make-local-variable 'auto-fill-inhibit-regexp)
3040 ;;(setq auto-fill-inhibit-regexp "^[A-Z][^: \n\t]+:")
3041 (setq auto-fill-inhibit-regexp nil)
3042 (make-local-variable 'normal-auto-fill-function)
3043 (setq normal-auto-fill-function 'message-do-auto-fill)
3044 ;; KLUDGE: auto fill might already be turned on in `text-mode-hook'.
3045 ;; In that case, ensure that it uses the right function. The real
3046 ;; solution would be not to use `define-derived-mode', and run
3047 ;; `text-mode-hook' ourself at the end of the mode.
3048 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-19.
c70dbcd5
SM
3049 ;; This kludge is unneeded in Emacs>=21 since define-derived-mode is
3050 ;; now careful to run parent hooks after the body. --Stef
23f87bed
MB
3051 (when auto-fill-function
3052 (setq auto-fill-function normal-auto-fill-function)))
f8c0f31e 3053
eec82323
LMI
3054\f
3055
3056;;;
3057;;; Message mode commands
3058;;;
3059
3060;;; Movement commands
3061
3062(defun message-goto-to ()
3063 "Move point to the To header."
3064 (interactive)
89b163db 3065 (push-mark)
eec82323
LMI
3066 (message-position-on-field "To"))
3067
23f87bed
MB
3068(defun message-goto-from ()
3069 "Move point to the From header."
3070 (interactive)
89b163db 3071 (push-mark)
23f87bed
MB
3072 (message-position-on-field "From"))
3073
eec82323
LMI
3074(defun message-goto-subject ()
3075 "Move point to the Subject header."
3076 (interactive)
89b163db 3077 (push-mark)
eec82323
LMI
3078 (message-position-on-field "Subject"))
3079
3080(defun message-goto-cc ()
3081 "Move point to the Cc header."
3082 (interactive)
89b163db 3083 (push-mark)
eec82323
LMI
3084 (message-position-on-field "Cc" "To"))
3085
3086(defun message-goto-bcc ()
3087 "Move point to the Bcc header."
3088 (interactive)
89b163db 3089 (push-mark)
eec82323
LMI
3090 (message-position-on-field "Bcc" "Cc" "To"))
3091
3092(defun message-goto-fcc ()
3093 "Move point to the Fcc header."
3094 (interactive)
89b163db 3095 (push-mark)
eec82323
LMI
3096 (message-position-on-field "Fcc" "To" "Newsgroups"))
3097
3098(defun message-goto-reply-to ()
3099 "Move point to the Reply-To header."
3100 (interactive)
89b163db 3101 (push-mark)
eec82323
LMI
3102 (message-position-on-field "Reply-To" "Subject"))
3103
3104(defun message-goto-newsgroups ()
3105 "Move point to the Newsgroups header."
3106 (interactive)
89b163db 3107 (push-mark)
eec82323
LMI
3108 (message-position-on-field "Newsgroups"))
3109
3110(defun message-goto-distribution ()
3111 "Move point to the Distribution header."
3112 (interactive)
89b163db 3113 (push-mark)
eec82323
LMI
3114 (message-position-on-field "Distribution"))
3115
3116(defun message-goto-followup-to ()
3117 "Move point to the Followup-To header."
3118 (interactive)
89b163db 3119 (push-mark)
eec82323
LMI
3120 (message-position-on-field "Followup-To" "Newsgroups"))
3121
23f87bed
MB
3122(defun message-goto-mail-followup-to ()
3123 "Move point to the Mail-Followup-To header."
3124 (interactive)
89b163db 3125 (push-mark)
531e5812 3126 (message-position-on-field "Mail-Followup-To" "To"))
23f87bed 3127
eec82323
LMI
3128(defun message-goto-keywords ()
3129 "Move point to the Keywords header."
3130 (interactive)
89b163db 3131 (push-mark)
eec82323
LMI
3132 (message-position-on-field "Keywords" "Subject"))
3133
3134(defun message-goto-summary ()
3135 "Move point to the Summary header."
3136 (interactive)
89b163db 3137 (push-mark)
eec82323
LMI
3138 (message-position-on-field "Summary" "Subject"))
3139
144b7b5c 3140(defun message-goto-body ()
eec82323 3141 "Move point to the beginning of the message body."
144b7b5c 3142 (interactive)
da946239 3143 (when (and (gmm-called-interactively-p 'any)
23f87bed
MB
3144 (looking-at "[ \t]*\n"))
3145 (expand-abbrev))
89b163db 3146 (push-mark)
eec82323 3147 (goto-char (point-min))
16409b0b 3148 (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
23f87bed 3149 (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t)))
eec82323 3150
0565caeb
MB
3151(defun message-in-body-p ()
3152 "Return t if point is in the message body."
cdfca592
RS
3153 (>= (point)
3154 (save-excursion
3155 (goto-char (point-min))
3156 (or (search-forward (concat "\n" mail-header-separator "\n") nil t)
3157 (search-forward-regexp "[^:]+:\\([^\n]\\|\n[ \t]\\)+\n\n" nil t))
3158 (point))))
0565caeb 3159
6748645f
LMI
3160(defun message-goto-eoh ()
3161 "Move point to the end of the headers."
3162 (interactive)
3163 (message-goto-body)
16409b0b 3164 (forward-line -1))
6748645f 3165
eec82323 3166(defun message-goto-signature ()
6748645f
LMI
3167 "Move point to the beginning of the message signature.
3168If there is no signature in the article, go to the end and
3169return nil."
eec82323 3170 (interactive)
89b163db 3171 (push-mark)
eec82323
LMI
3172 (goto-char (point-min))
3173 (if (re-search-forward message-signature-separator nil t)
3174 (forward-line 1)
6748645f
LMI
3175 (goto-char (point-max))
3176 nil))
eec82323 3177
23f87bed
MB
3178(defun message-generate-unsubscribed-mail-followup-to (&optional include-cc)
3179 "Insert a reasonable MFT header in a post to an unsubscribed list.
3180When making original posts to a mailing list you are not subscribed to,
3181you have to type in a MFT header by hand. The contents, usually, are
3182the addresses of the list and your own address. This function inserts
3183such a header automatically. It fetches the contents of the To: header
3184in the current mail buffer, and appends the current `user-mail-address'.
3185
3186If the optional argument INCLUDE-CC is non-nil, the addresses in the
3187Cc: header are also put into the MFT."
3188
3189 (interactive "P")
3190 (let* (cc tos)
3191 (save-restriction
3192 (message-narrow-to-headers)
3193 (message-remove-header "Mail-Followup-To")
3194 (setq cc (and include-cc (message-fetch-field "Cc")))
3195 (setq tos (if cc
3196 (concat (message-fetch-field "To") "," cc)
3197 (message-fetch-field "To"))))
3198 (message-goto-mail-followup-to)
3199 (insert (concat tos ", " user-mail-address))))
3200
eec82323
LMI
3201\f
3202
a8151ef7
LMI
3203(defun message-insert-to (&optional force)
3204 "Insert a To header that points to the author of the article being replied to.
23f87bed
MB
3205If the original author requested not to be sent mail, don't insert unless the
3206prefix FORCE is given."
a8151ef7 3207 (interactive "P")
23f87bed 3208 (let* ((mct (message-fetch-reply-field "mail-copies-to"))
01c52d31 3209 (dont (and mct (or (equal (downcase mct) "never")
23f87bed 3210 (equal (downcase mct) "nobody"))))
01c52d31
MB
3211 (to (or (message-fetch-reply-field "mail-reply-to")
3212 (message-fetch-reply-field "reply-to")
3213 (message-fetch-reply-field "from"))))
23f87bed
MB
3214 (when (and dont to)
3215 (message
3216 (if force
3217 "Ignoring the user request not to have copies sent via mail"
3218 "Complying with the user request not to have copies sent via mail")))
3219 (when (and force (not to))
3220 (error "No mail address in the article"))
3221 (when (and to (or force (not dont)))
3222 (message-carefully-insert-headers (list (cons 'To to))))))
3223
3224(defun message-insert-wide-reply ()
3225 "Insert To and Cc headers as if you were doing a wide reply."
3226 (interactive)
3227 (let ((headers (message-with-reply-buffer
3228 (message-get-reply-headers t))))
3229 (message-carefully-insert-headers headers)))
3230
3231(defcustom message-header-synonyms
14e6dc54
MB
3232 '((To Cc Bcc)
3233 (Original-To))
23f87bed
MB
3234 "List of lists of header synonyms.
3235E.g., if this list contains a member list with elements `Cc' and `To',
3236then `message-carefully-insert-headers' will not insert a `To' header
3237when the message is already `Cc'ed to the recipient."
bf247b6e 3238 :version "22.1"
23f87bed
MB
3239 :group 'message-headers
3240 :link '(custom-manual "(message)Message Headers")
3241 :type '(repeat sexp))
3242
3243(defun message-carefully-insert-headers (headers)
3244 "Insert the HEADERS, an alist, into the message buffer.
3245Does not insert the headers when they are already present there
3246or in the synonym headers, defined by `message-header-synonyms'."
3247 ;; FIXME: Should compare only the address and not the full name. Comparison
3248 ;; should be done case-folded (and with `string=' rather than
3249 ;; `string-match').
531e5812 3250 ;; (mail-strip-quoted-names "Foo Bar <foo@bar>, bla@fasel (Bla Fasel)")
23f87bed
MB
3251 (dolist (header headers)
3252 (let* ((header-name (symbol-name (car header)))
01c52d31
MB
3253 (new-header (cdr header))
3254 (synonyms (loop for synonym in message-header-synonyms
23f87bed 3255 when (memq (car header) synonym) return synonym))
01c52d31
MB
3256 (old-header
3257 (loop for synonym in synonyms
23f87bed
MB
3258 for old-header = (mail-fetch-field (symbol-name synonym))
3259 when (and old-header (string-match new-header old-header))
3260 return synonym)))
3261 (if old-header
01c52d31 3262 (message "already have `%s' in `%s'" new-header old-header)
23f87bed 3263 (when (and (message-position-on-field header-name)
01c52d31
MB
3264 (setq old-header (mail-fetch-field header-name))
3265 (not (string-match "\\` *\\'" old-header)))
23f87bed 3266 (insert ", "))
01c52d31 3267 (insert new-header)))))
eec82323 3268
16409b0b
GM
3269(defun message-widen-reply ()
3270 "Widen the reply to include maximum recipients."
3271 (interactive)
3272 (let ((follow-to
b1ea3797 3273 (and (bufferp message-reply-buffer)
16409b0b 3274 (buffer-name message-reply-buffer)
82816ba1 3275 (with-current-buffer message-reply-buffer
16409b0b
GM
3276 (message-get-reply-headers t)))))
3277 (save-excursion
3278 (save-restriction
3279 (message-narrow-to-headers)
3280 (dolist (elem follow-to)
3281 (message-remove-header (symbol-name (car elem)))
3282 (goto-char (point-min))
3283 (insert (symbol-name (car elem)) ": "
f129a4df 3284 (cdr elem) "\n"))))))
16409b0b 3285
eec82323
LMI
3286(defun message-insert-newsgroups ()
3287 "Insert the Newsgroups header from the article being replied to."
3288 (interactive)
350a1888
G
3289 (let ((old-newsgroups (mail-fetch-field "newsgroups"))
3290 (new-newsgroups (message-fetch-reply-field "newsgroups"))
3291 (first t)
3292 insert-newsgroups)
3293 (message-position-on-field "Newsgroups")
3294 (cond
3295 ((not new-newsgroups)
3296 (error "No Newsgroups to insert"))
3297 ((not old-newsgroups)
3298 (insert new-newsgroups))
3299 (t
3300 (setq new-newsgroups (split-string new-newsgroups "[, ]+")
3301 old-newsgroups (split-string old-newsgroups "[, ]+"))
3302 (dolist (group new-newsgroups)
3303 (unless (member group old-newsgroups)
3304 (push group insert-newsgroups)))
3305 (if (null insert-newsgroups)
3306 (error "Newgroup%s already in the header"
3307 (if (> (length new-newsgroups) 1)
3308 "s" ""))
3309 (when old-newsgroups
3310 (setq first nil))
3311 (dolist (group insert-newsgroups)
3312 (unless first
3313 (insert ","))
3314 (setq first nil)
3315 (insert group)))))))
eec82323
LMI
3316
3317\f
3318
3319;;; Various commands
3320
6748645f 3321(defun message-delete-not-region (beg end)
7d829636 3322 "Delete everything in the body of the current message outside of the region."
6748645f 3323 (interactive "r")
23f87bed
MB
3324 (let (citeprefix)
3325 (save-excursion
3326 (goto-char beg)
3327 ;; snarf citation prefix, if appropriate
3328 (unless (eq (point) (progn (beginning-of-line) (point)))
3329 (when (looking-at message-cite-prefix-regexp)
3330 (setq citeprefix (match-string 0))))
3331 (goto-char end)
3332 (delete-region (point) (if (not (message-goto-signature))
3333 (point)
3334 (forward-line -2)
3335 (point)))
f129a4df 3336 (insert "\n")
23f87bed
MB
3337 (goto-char beg)
3338 (delete-region beg (progn (message-goto-body)
3339 (forward-line 2)
3340 (point)))
3341 (when citeprefix
3342 (insert citeprefix))))
6748645f
LMI
3343 (when (message-goto-signature)
3344 (forward-line -2)))
3345
01c52d31
MB
3346(defun message-kill-to-signature (&optional arg)
3347 "Kill all text up to the signature.
2b8f62e9 3348If a numeric argument or prefix arg is given, leave that number
01c52d31
MB
3349of lines before the signature intact."
3350 (interactive "P")
3351 (save-excursion
3352 (save-restriction
3353 (let ((point (point)))
3354 (narrow-to-region point (point-max))
3355 (message-goto-signature)
3356 (unless (eobp)
3357 (if (and arg (numberp arg))
3358 (forward-line (- -1 arg))
3359 (end-of-line -1)))
3360 (unless (= point (point))
3361 (kill-region point (point))
3362 (unless (bolp)
3363 (insert "\n")))))))
6748645f 3364
23f87bed
MB
3365(defun message-newline-and-reformat (&optional arg not-break)
3366 "Insert four newlines, and then reformat if inside quoted text.
3367Prefix arg means justify as well."
3368 (interactive (list (if current-prefix-arg 'full)))
01c52d31 3369 (let (quoted point beg end leading-space bolp fill-paragraph-function)
16409b0b 3370 (setq point (point))
23f87bed
MB
3371 (beginning-of-line)
3372 (setq beg (point))
3373 (setq bolp (= beg point))
3374 ;; Find first line of the paragraph.
3375 (if not-break
3376 (while (and (not (eobp))
3377 (not (looking-at message-cite-prefix-regexp))
3378 (looking-at paragraph-start))
3379 (forward-line 1)))
3380 ;; Find the prefix
3381 (when (looking-at message-cite-prefix-regexp)
3382 (setq quoted (match-string 0))
3383 (goto-char (match-end 0))
3384 (looking-at "[ \t]*")
3385 (setq leading-space (match-string 0)))
3386 (if (and quoted
3387 (not not-break)
3388 (not bolp)
3389 (< (- point beg) (length quoted)))
3390 ;; break inside the cite prefix.
3391 (setq quoted nil
3392 end nil))
3393 (if quoted
3394 (progn
3395 (forward-line 1)
3396 (while (and (not (eobp))
3397 (not (looking-at paragraph-separate))
3398 (looking-at message-cite-prefix-regexp)
3399 (equal quoted (match-string 0)))
3400 (goto-char (match-end 0))
3401 (looking-at "[ \t]*")
3402 (if (> (length leading-space) (length (match-string 0)))
3403 (setq leading-space (match-string 0)))
3404 (forward-line 1))
3405 (setq end (point))
3406 (goto-char beg)
3407 (while (and (if (bobp) nil (forward-line -1) t)
3408 (not (looking-at paragraph-start))
3409 (looking-at message-cite-prefix-regexp)
3410 (equal quoted (match-string 0)))
3411 (setq beg (point))
3412 (goto-char (match-end 0))
3413 (looking-at "[ \t]*")
3414 (if (> (length leading-space) (length (match-string 0)))
3415 (setq leading-space (match-string 0)))))
3416 (while (and (not (eobp))
3417 (not (looking-at paragraph-separate))
3418 (not (looking-at message-cite-prefix-regexp)))
3419 (forward-line 1))
3420 (setq end (point))
3421 (goto-char beg)
3422 (while (and (if (bobp) nil (forward-line -1) t)
3423 (not (looking-at paragraph-start))
3424 (not (looking-at message-cite-prefix-regexp)))
3425 (setq beg (point))))
6748645f 3426 (goto-char point)
23f87bed
MB
3427 (save-restriction
3428 (narrow-to-region beg end)
3429 (if not-break
3430 (setq point nil)
3431 (if bolp
3432 (newline)
3433 (newline)
3434 (newline))
3435 (setq point (point))
3436 ;; (newline 2) doesn't mark both newline's as hard, so call
3437 ;; newline twice. -jas
3438 (newline)
3439 (newline)
3440 (delete-region (point) (re-search-forward "[ \t]*"))
3441 (when (and quoted (not bolp))
3442 (insert quoted leading-space)))
3443 (undo-boundary)
3444 (if quoted
3445 (let* ((adaptive-fill-regexp
3446 (regexp-quote (concat quoted leading-space)))
3447 (adaptive-fill-first-line-regexp
3448 adaptive-fill-regexp ))
3449 (fill-paragraph arg))
3450 (fill-paragraph arg))
3451 (if point (goto-char point)))))
3452
3453(defun message-fill-paragraph (&optional arg)
01c52d31
MB
3454 "Message specific function to fill a paragraph.
3455This function is used as the value of `fill-paragraph-function' in
3456Message buffers and is not meant to be called directly."
23f87bed
MB
3457 (interactive (list (if current-prefix-arg 'full)))
3458 (if (if (boundp 'filladapt-mode) filladapt-mode)
3459 nil
01c52d31
MB
3460 (if (message-point-in-header-p)
3461 (message-fill-field)
3462 (message-newline-and-reformat arg t))
23f87bed 3463 t))
6748645f 3464
23f87bed
MB
3465(defun message-point-in-header-p ()
3466 "Return t if point is in the header."
3467 (save-excursion
3451795c
LMI
3468 (and
3469 (not
3470 (re-search-backward
3471 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
3472 (re-search-forward
3473 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))))
23f87bed
MB
3474
3475(defun message-do-auto-fill ()
3476 "Like `do-auto-fill', but don't fill in message header."
3477 (unless (message-point-in-header-p)
3478 (do-auto-fill)))
10893bb6 3479
eec82323 3480(defun message-insert-signature (&optional force)
7d829636 3481 "Insert a signature. See documentation for variable `message-signature'."
eec82323
LMI
3482 (interactive (list 0))
3483 (let* ((signature
3484 (cond
3485 ((and (null message-signature)
3486 (eq force 0))
3487 (save-excursion
3488 (goto-char (point-max))
16409b0b 3489 (not (re-search-backward message-signature-separator nil t))))
eec82323
LMI
3490 ((and (null message-signature)
3491 force)
3492 t)
23f87bed 3493 ((functionp message-signature)
eec82323
LMI
3494 (funcall message-signature))
3495 ((listp message-signature)
3496 (eval message-signature))
3497 (t message-signature)))
01c52d31
MB
3498 signature-file)
3499 (setq signature
eec82323
LMI
3500 (cond ((stringp signature)
3501 signature)
01c52d31
MB
3502 ((and (eq t signature) message-signature-file)
3503 (setq signature-file
3504 (if (and message-signature-directory
3505 ;; don't actually use the signature directory
3506 ;; if message-signature-file contains a path.
3507 (not (file-name-directory
3508 message-signature-file)))
aa8f8277
GM
3509 (expand-file-name message-signature-file
3510 message-signature-directory)
01c52d31
MB
3511 message-signature-file))
3512 (file-exists-p signature-file))))
eec82323
LMI
3513 (when signature
3514 (goto-char (point-max))
3515 ;; Insert the signature.
3516 (unless (bolp)
f129a4df 3517 (insert "\n"))
23f87bed 3518 (when message-signature-insert-empty-line
f129a4df
CY
3519 (insert "\n"))
3520 (insert "-- \n")
eec82323 3521 (if (eq signature t)
01c52d31 3522 (insert-file-contents signature-file)
eec82323
LMI
3523 (insert signature))
3524 (goto-char (point-max))
f129a4df 3525 (or (bolp) (insert "\n")))))
eec82323 3526
23f87bed
MB
3527(defun message-insert-importance-high ()
3528 "Insert header to mark message as important."
3529 (interactive)
3530 (save-excursion
3531 (save-restriction
3532 (message-narrow-to-headers)
3533 (message-remove-header "Importance"))
3534 (message-goto-eoh)
f129a4df 3535 (insert "Importance: high\n")))
23f87bed
MB
3536
3537(defun message-insert-importance-low ()
3538 "Insert header to mark message as unimportant."
3539 (interactive)
3540 (save-excursion
3541 (save-restriction
3542 (message-narrow-to-headers)
3543 (message-remove-header "Importance"))
3544 (message-goto-eoh)
f129a4df 3545 (insert "Importance: low\n")))
23f87bed
MB
3546
3547(defun message-insert-or-toggle-importance ()
3548 "Insert a \"Importance: high\" header, or cycle through the header values.
3549The three allowed values according to RFC 1327 are `high', `normal'
3550and `low'."
3551 (interactive)
3552 (save-excursion
82816ba1 3553 (let ((new "high")
23f87bed
MB
3554 cur)
3555 (save-restriction
3556 (message-narrow-to-headers)
3557 (when (setq cur (message-fetch-field "Importance"))
3558 (message-remove-header "Importance")
3559 (setq new (cond ((string= cur "high")
3560 "low")
3561 ((string= cur "low")
3562 "normal")
3563 (t
3564 "high")))))
3565 (message-goto-eoh)
f129a4df 3566 (insert (format "Importance: %s\n" new)))))
23f87bed
MB
3567
3568(defun message-insert-disposition-notification-to ()
3569 "Request a disposition notification (return receipt) to this message.
3570Note that this should not be used in newsgroups."
3571 (interactive)
3572 (save-excursion
3573 (save-restriction
3574 (message-narrow-to-headers)
3575 (message-remove-header "Disposition-Notification-To"))
3576 (message-goto-eoh)
f129a4df 3577 (insert (format "Disposition-Notification-To: %s\n"
23f87bed
MB
3578 (or (message-field-value "Reply-to")
3579 (message-field-value "From")
f129a4df 3580 (message-make-from))))))
23f87bed 3581
eec82323 3582(defun message-elide-region (b e)
7d829636 3583 "Elide the text in the region.
16409b0b 3584An ellipsis (from `message-elide-ellipsis') will be inserted where the
6748645f 3585text was killed."
eec82323 3586 (interactive "r")
a123622d
G
3587 (let ((lines (count-lines b e))
3588 (chars (- e b)))
3589 (kill-region b e)
3590 (insert (format-spec message-elide-ellipsis
3591 `((?l . ,lines)
3592 (?c . ,chars))))))
eec82323
LMI
3593
3594(defvar message-caesar-translation-table nil)
3595
3596(defun message-caesar-region (b e &optional n)
7d829636 3597 "Caesar rotate region B to E by N, default 13, for decrypting netnews."
eec82323
LMI
3598 (interactive
3599 (list
3600 (min (point) (or (mark t) (point)))
3601 (max (point) (or (mark t) (point)))
3602 (when current-prefix-arg
3603 (prefix-numeric-value current-prefix-arg))))
3604
3605 (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
23f87bed 3606 (unless (or (zerop n) ; no action needed for a rot of 0
eec82323
LMI
3607 (= b e)) ; no region to rotate
3608 ;; We build the table, if necessary.
3609 (when (or (not message-caesar-translation-table)
3610 (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
16409b0b
GM
3611 (setq message-caesar-translation-table
3612 (message-make-caesar-translation-table n)))
3613 (translate-region b e message-caesar-translation-table)))
eec82323
LMI
3614
3615(defun message-make-caesar-translation-table (n)
3616 "Create a rot table with offset N."
3617 (let ((i -1)
3618 (table (make-string 256 0)))
3619 (while (< (incf i) 256)
3620 (aset table i i))
3621 (concat
3622 (substring table 0 ?A)
3623 (substring table (+ ?A n) (+ ?A n (- 26 n)))
3624 (substring table ?A (+ ?A n))
3625 (substring table (+ ?A 26) ?a)
3626 (substring table (+ ?a n) (+ ?a n (- 26 n)))
3627 (substring table ?a (+ ?a n))
3628 (substring table (+ ?a 26) 255))))
3629
01c52d31 3630(defun message-caesar-buffer-body (&optional rotnum wide)
7d829636
DL
3631 "Caesar rotate all letters in the current buffer by 13 places.
3632Used to encode/decode possibly offensive messages (commonly in rec.humor).
eec82323 3633With prefix arg, specifies the number of places to rotate each letter forward.
01c52d31 3634Mail and USENET news headers are not rotated unless WIDE is non-nil."
eec82323
LMI
3635 (interactive (if current-prefix-arg
3636 (list (prefix-numeric-value current-prefix-arg))
3637 (list nil)))
3638 (save-excursion
3639 (save-restriction
01c52d31 3640 (when (and (not wide) (message-goto-body))
eec82323
LMI
3641 (narrow-to-region (point) (point-max)))
3642 (message-caesar-region (point-min) (point-max) rotnum))))
3643
3644(defun message-pipe-buffer-body (program)
3645 "Pipe the message body in the current buffer through PROGRAM."
3646 (save-excursion
3647 (save-restriction
3648 (when (message-goto-body)
23f87bed 3649 (narrow-to-region (point) (point-max)))
16409b0b
GM
3650 (shell-command-on-region
3651 (point-min) (point-max) program nil t))))
eec82323
LMI
3652
3653(defun message-rename-buffer (&optional enter-string)
3654 "Rename the *message* buffer to \"*message* RECIPIENT\".
3655If the function is run with a prefix, it will ask for a new buffer
3656name, rather than giving an automatic name."
3657 (interactive "Pbuffer name: ")
3658 (save-excursion
3659 (save-restriction
3660 (goto-char (point-min))
3661 (narrow-to-region (point)
3662 (search-forward mail-header-separator nil 'end))
3663 (let* ((mail-to (or
3664 (if (message-news-p) (message-fetch-field "Newsgroups")
3665 (message-fetch-field "To"))
3666 ""))
3667 (mail-trimmed-to
3668 (if (string-match "," mail-to)
3669 (concat (substring mail-to 0 (match-beginning 0)) ", ...")
3670 mail-to))
3671 (name-default (concat "*message* " mail-trimmed-to))
3672 (name (if enter-string
3673 (read-string "New buffer name: " name-default)
6748645f 3674 name-default)))
eec82323
LMI
3675 (rename-buffer name t)))))
3676
3677(defun message-fill-yanked-message (&optional justifyp)
3678 "Fill the paragraphs of a message yanked into this one.
3679Numeric argument means justify as well."
3680 (interactive "P")
3681 (save-excursion
3682 (goto-char (point-min))
3683 (search-forward (concat "\n" mail-header-separator "\n") nil t)
3684 (let ((fill-prefix message-yank-prefix))
16409b0b 3685 (fill-individual-paragraphs (point) (point-max) justifyp))))
eec82323 3686
01c52d31 3687(defun message-indent-citation (&optional start end yank-only)
eec82323
LMI
3688 "Modify text just inserted from a message to be cited.
3689The inserted text should be the region.
3690When this function returns, the region is again around the modified text.
3691
3692Normally, indent each nonblank line `message-indentation-spaces' spaces.
3693However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
01c52d31
MB
3694 (unless start (setq start (point)))
3695 (unless yank-only
eec82323
LMI
3696 ;; Remove unwanted headers.
3697 (when message-ignored-cited-headers
3698 (let (all-removed)
3699 (save-restriction
3700 (narrow-to-region
3701 (goto-char start)
3702 (if (search-forward "\n\n" nil t)
3703 (1- (point))
3704 (point)))
3705 (message-remove-header message-ignored-cited-headers t)
3706 (when (= (point-min) (point-max))
3707 (setq all-removed t))
3708 (goto-char (point-max)))
3709 (if all-removed
3710 (goto-char start)
3711 (forward-line 1))))
3712 ;; Delete blank lines at the start of the buffer.
3713 (while (and (point-min)
3714 (eolp)
3715 (not (eobp)))
3716 (message-delete-line))
3717 ;; Delete blank lines at the end of the buffer.
3718 (goto-char (point-max))
a5166359
LMI
3719 (unless (eq (preceding-char) ?\n)
3720 (insert "\n"))
3721 (while (and (zerop (forward-line -1))
3722 (looking-at "$"))
01c52d31
MB
3723 (message-delete-line)))
3724 ;; Do the indentation.
3725 (if (null message-yank-prefix)
3726 (indent-rigidly start (or end (mark t)) message-indentation-spaces)
3727 (save-excursion
3728 (goto-char start)
3729 (while (< (point) (or end (mark t)))
3730 (cond ((looking-at ">")
3731 (insert message-yank-cited-prefix))
3732 ((looking-at "^$")
3733 (insert message-yank-empty-prefix))
3734 (t
3735 (insert message-yank-prefix)))
3736 (forward-line 1))))
3737 (goto-char start))
3738
3739(defun message-remove-blank-cited-lines (&optional remove)
3740 "Remove cited lines containing only blanks.
3741If REMOVE is non-nil, remove newlines, too.
3742
3743To use this automatically, you may add this function to
3744`gnus-message-setup-hook'."
3745 (interactive "P")
3746 (let ((citexp
3747 (concat
3748 "^\\("
437ce4be
MB
3749 (when (boundp 'message-yank-cited-prefix)
3750 (concat message-yank-cited-prefix "\\|"))
01c52d31 3751 message-yank-prefix
437ce4be
MB
3752 "\\)+ *\n"
3753 )))
01c52d31
MB
3754 (gnus-message 8 "removing `%s'" citexp)
3755 (save-excursion
3756 (message-goto-body)
3757 (while (re-search-forward citexp nil t)
437ce4be 3758 (replace-match (if remove "" "\n"))))))
01c52d31 3759
a5954fa5 3760(defun message--yank-original-internal (arg)
01c52d31
MB
3761 (let ((modified (buffer-modified-p))
3762 body-text)
706b5974
G
3763 (when (and message-reply-buffer
3764 message-cite-function)
3765 (when (equal message-cite-reply-position 'above)
01c52d31
MB
3766 (save-excursion
3767 (setq body-text
3768 (buffer-substring (message-goto-body)
3769 (point-max)))
706b5974
G
3770 (delete-region (message-goto-body) (point-max))))
3771 (if (bufferp message-reply-buffer)
3772 (delete-windows-on message-reply-buffer t))
3773 (push-mark (save-excursion
3774 (cond
3775 ((bufferp message-reply-buffer)
3776 (insert-buffer-substring message-reply-buffer))
3777 ((and (consp message-reply-buffer)
3778 (functionp (car message-reply-buffer)))
3779 (apply (car message-reply-buffer)
3780 (cdr message-reply-buffer))))
3781 (unless (bolp)
3782 (insert ?\n))
3783 (point)))
3784 (unless arg
3785 (funcall message-cite-function)
3786 (unless (eq (char-before (mark t)) ?\n)
3787 (let ((pt (point)))
3788 (goto-char (mark t))
3789 (insert-before-markers ?\n)
3790 (goto-char pt))))
0adf5618
SM
3791 (case message-cite-reply-position
3792 (above
706b5974
G
3793 (message-goto-body)
3794 (insert body-text)
3795 (insert (if (bolp) "\n" "\n\n"))
3796 (message-goto-body))
0adf5618 3797 (below
706b5974
G
3798 (message-goto-signature)))
3799 ;; Add a `message-setup-very-last-hook' here?
3800 ;; Add `gnus-article-highlight-citation' here?
3801 (unless modified
a5954fa5
G
3802 (setq message-checksum (message-checksum))))))
3803
3804(defun message-yank-original (&optional arg)
3805 "Insert the message being replied to, if any.
3806Puts point before the text and mark after.
3807Normally indents each nonblank line ARG spaces (default 3). However,
3808if `message-yank-prefix' is non-nil, insert that prefix on each line.
3809
3810This function uses `message-cite-function' to do the actual citing.
3811
3812Just \\[universal-argument] as argument means don't indent, insert no
3813prefix, and don't delete any headers."
3814 (interactive "P")
3815 ;; eval the let forms contained in message-cite-style
3816 (eval
cc21c235
G
3817 `(let ,(if (symbolp message-cite-style)
3818 (symbol-value message-cite-style)
3819 message-cite-style)
a5954fa5 3820 (message--yank-original-internal ',arg))))
eec82323 3821
16409b0b
GM
3822(defun message-yank-buffer (buffer)
3823 "Insert BUFFER into the current buffer and quote it."
3824 (interactive "bYank buffer: ")
23f87bed 3825 (let ((message-reply-buffer (get-buffer buffer)))
16409b0b
GM
3826 (save-window-excursion
3827 (message-yank-original))))
3828
3829(defun message-buffers ()
3830 "Return a list of active message buffers."
3831 (let (buffers)
82816ba1 3832 (save-current-buffer
16409b0b
GM
3833 (dolist (buffer (buffer-list t))
3834 (set-buffer buffer)
89b163db 3835 (when (and (derived-mode-p 'message-mode)
16409b0b
GM
3836 (null message-sent-message-via))
3837 (push (buffer-name buffer) buffers))))
3838 (nreverse buffers)))
3839
01c52d31
MB
3840(defun message-cite-original-1 (strip-signature)
3841 "Cite an original message.
3842If STRIP-SIGNATURE is non-nil, strips off the signature from the
3843original message.
3844
3845This function uses `mail-citation-hook' if that is non-nil."
4a55f847 3846 (if (and (boundp 'mail-citation-hook)
16409b0b 3847 mail-citation-hook)
4a55f847 3848 (run-hooks 'mail-citation-hook)
23f87bed
MB
3849 (let* ((start (point))
3850 (end (mark t))
01c52d31 3851 (x-no-archive nil)
23f87bed
MB
3852 (functions
3853 (when message-indent-citation-function
3854 (if (listp message-indent-citation-function)
3855 message-indent-citation-function
3856 (list message-indent-citation-function))))
3857 ;; This function may be called by `gnus-summary-yank-message' and
3858 ;; may insert a different article from the original. So, we will
3859 ;; modify the value of `message-reply-headers' with that article.
3860 (message-reply-headers
3861 (save-restriction
3862 (narrow-to-region start end)
3863 (message-narrow-to-head-1)
01c52d31 3864 (setq x-no-archive (message-fetch-field "x-no-archive"))
23f87bed
MB
3865 (vector 0
3866 (or (message-fetch-field "subject") "none")
0327a464 3867 (or (message-fetch-field "from") "nobody")
23f87bed
MB
3868 (message-fetch-field "date")
3869 (message-fetch-field "message-id" t)
3870 (message-fetch-field "references")
3871 0 0 ""))))
16409b0b 3872 (mml-quote-region start end)
01c52d31
MB
3873 (when strip-signature
3874 ;; Allow undoing.
3875 (undo-boundary)
3876 (goto-char end)
3877 (when (re-search-backward message-signature-separator start t)
3878 ;; Also peel off any blank lines before the signature.
3879 (forward-line -1)
3880 (while (looking-at "^[ \t]*$")
3881 (forward-line -1))
3882 (forward-line 1)
3883 (delete-region (point) end)
3884 (unless (search-backward "\n\n" start t)
3885 ;; Insert a blank line if it is peeled off.
3886 (insert "\n"))))
4a55f847 3887 (goto-char start)
01c52d31 3888 (mapc 'funcall functions)
4a55f847 3889 (when message-citation-line-function
16409b0b 3890 (unless (bolp)
f129a4df 3891 (insert "\n"))
01c52d31
MB
3892 (funcall message-citation-line-function))
3893 (when (and x-no-archive
3894 (not message-cite-articles-with-x-no-archive)
3895 (string-match "yes" x-no-archive))
3896 (undo-boundary)
3897 (delete-region (point) (mark t))
3898 (insert "> [Quoted text removed due to X-No-Archive]\n")
3899 (push-mark)
3900 (forward-line -1)))))
3901
3902(defun message-cite-original ()
3903 "Cite function in the standard Message manner."
3904 (message-cite-original-1 nil))
3905
237dd97f
GM
3906(defvar gnus-extract-address-components)
3907
ea7d768a
GM
3908(autoload 'format-spec "format-spec")
3909
4d8a28ec
MB
3910(defun message-insert-formatted-citation-line (&optional from date)
3911 "Function that inserts a formatted citation line.
01c52d31
MB
3912
3913See `message-citation-line-format'."
3914 ;; The optional args are for testing/debugging. They will disappear later.
3915 ;; Example:
3916 ;; (with-temp-buffer
4d8a28ec 3917 ;; (message-insert-formatted-citation-line
01c52d31
MB
3918 ;; "John Doe <john.doe@example.invalid>"
3919 ;; (current-time))
3920 ;; (buffer-string))
3921 (when (or message-reply-headers (and from date))
3922 (unless from
3923 (setq from (mail-header-from message-reply-headers)))
3924 (let* ((data (condition-case ()
3925 (funcall (if (boundp gnus-extract-address-components)
3926 gnus-extract-address-components
3927 'mail-extract-address-components)
3928 from)
3929 (error nil)))
3930 (name (car data))
3931 (fname name)
3932 (lname name)
3933 (net (car (cdr data)))
3934 (name-or-net (or (car data)
3935 (car (cdr data)) from))
3936 (replydate
3937 (or
3938 date
3939 ;; We need Gnus functionality if the user wants date or time from
3940 ;; the original article:
3941 (when (string-match "%[^fnNFL]" message-citation-line-format)
3942 (autoload 'gnus-date-get-time "gnus-util")
3943 (gnus-date-get-time (mail-header-date message-reply-headers)))))
3944 (flist
3945 (let ((i ?A) lst)
3946 (when (stringp name)
3947 ;; Guess first name and last name:
3948 (cond ((string-match
3949 "\\`\\(\\w\\|[-.]\\)+ \\(\\w\\|[-.]\\)+\\'" name)
3950 (setq fname (nth 0 (split-string name "[ \t]+"))
3951 lname (nth 1 (split-string name "[ \t]+"))))
3952 ((string-match
3953 "\\`\\(\\w\\|[-.]\\)+, \\(\\w\\|[-.]\\)+\\'" name)
3954 (setq fname (nth 1 (split-string name "[ \t,]+"))
3955 lname (nth 0 (split-string name "[ \t,]+"))))
3956 ((string-match
3957 "\\`\\(\\w\\|[-.]\\)+\\'" name)
3958 (setq fname name
3959 lname ""))))
3960 ;; The following letters are not used in `format-time-string':
3961 (push ?E lst) (push "<E>" lst)
3962 (push ?F lst) (push fname lst)
3963 ;; We might want to use "" instead of "<X>" later.
3964 (push ?J lst) (push "<J>" lst)
3965 (push ?K lst) (push "<K>" lst)
3966 (push ?L lst) (push lname lst)
3967 (push ?N lst) (push name-or-net lst)
3968 (push ?O lst) (push "<O>" lst)
3969 (push ?P lst) (push "<P>" lst)
3970 (push ?Q lst) (push "<Q>" lst)
3971 (push ?f lst) (push from lst)
3972 (push ?i lst) (push "<i>" lst)
3973 (push ?n lst) (push net lst)
3974 (push ?o lst) (push "<o>" lst)
3975 (push ?q lst) (push "<q>" lst)
3976 (push ?t lst) (push "<t>" lst)
3977 (push ?v lst) (push "<v>" lst)
3978 ;; Delegate the rest to `format-time-string':
3979 (while (<= i ?z)
3980 (when (and (not (memq i lst))
3981 ;; Skip (Z,a)
3982 (or (<= i ?Z)
3983 (>= i ?a)))
3984 (push i lst)
3985 (push (condition-case nil
63348d24
GM
3986 (format-time-string (format "%%%c" i) replydate)
3987 (error (format ">%c<" i)))
01c52d31
MB
3988 lst))
3989 (setq i (1+ i)))
3990 (reverse lst)))
3991 (spec (apply 'format-spec-make flist)))
3992 (insert (format-spec message-citation-line-format spec)))
3993 (newline)))
3994
3995(defun message-cite-original-without-signature ()
3996 "Cite function in the standard Message manner.
3997This function strips off the signature from the original message."
3998 (message-cite-original-1 t))
eec82323
LMI
3999
4000(defun message-insert-citation-line ()
7d829636 4001 "Insert a simple citation line."
eec82323 4002 (when message-reply-headers
1271a058
CY
4003 (insert (mail-header-from message-reply-headers) " writes:")
4004 (newline)
4005 (newline)))
eec82323
LMI
4006
4007(defun message-position-on-field (header &rest afters)
4008 (let ((case-fold-search t))
4009 (save-restriction
4010 (narrow-to-region
4011 (goto-char (point-min))
4012 (progn
4013 (re-search-forward
4014 (concat "^" (regexp-quote mail-header-separator) "$"))
4015 (match-beginning 0)))
4016 (goto-char (point-min))
4017 (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
4018 (progn
4019 (re-search-forward "^[^ \t]" nil 'move)
4020 (beginning-of-line)
4021 (skip-chars-backward "\n")
4022 t)
4023 (while (and afters
4024 (not (re-search-forward
4025 (concat "^" (regexp-quote (car afters)) ":")
4026 nil t)))
4027 (pop afters))
4028 (when afters
4029 (re-search-forward "^[^ \t]" nil 'move)
4030 (beginning-of-line))
f129a4df 4031 (insert header ": \n")
eec82323
LMI
4032 (forward-char -1)
4033 nil))))
4034
eec82323
LMI
4035\f
4036
4037;;;
4038;;; Sending messages
4039;;;
4040
4041(defun message-send-and-exit (&optional arg)
9d217cac
LMI
4042 "Send message like `message-send', then, if no errors, exit from mail buffer.
4043The usage of ARG is defined by the instance that called Message.
4044It should typically alter the sending method in some way or other."
eec82323
LMI
4045 (interactive "P")
4046 (let ((buf (current-buffer))
4047 (actions message-exit-actions))
4048 (when (and (message-send arg)
4049 (buffer-name buf))
25ca2e61 4050 (message-bury buf)
eec82323 4051 (if message-kill-buffer-on-exit
25ca2e61 4052 (kill-buffer buf))
6748645f
LMI
4053 (message-do-actions actions)
4054 t)))
eec82323
LMI
4055
4056(defun message-dont-send ()
23f87bed
MB
4057 "Don't send the message you have been editing.
4058Instead, just auto-save the buffer and then bury it."
eec82323 4059 (interactive)
6748645f
LMI
4060 (set-buffer-modified-p t)
4061 (save-buffer)
eec82323
LMI
4062 (let ((actions message-postpone-actions))
4063 (message-bury (current-buffer))
4064 (message-do-actions actions)))
4065
4066(defun message-kill-buffer ()
4067 "Kill the current buffer."
4068 (interactive)
4069 (when (or (not (buffer-modified-p))
01c52d31 4070 (not message-kill-buffer-query)
eec82323 4071 (yes-or-no-p "Message modified; kill anyway? "))
23f87bed
MB
4072 (let ((actions message-kill-actions)
4073 (draft-article message-draft-article)
4074 (auto-save-file-name buffer-auto-save-file-name)
4075 (file-name buffer-file-name)
4076 (modified (buffer-modified-p)))
6748645f 4077 (setq buffer-file-name nil)
eec82323 4078 (kill-buffer (current-buffer))
23f87bed
MB
4079 (when (and (or (and auto-save-file-name
4080 (file-exists-p auto-save-file-name))
4081 (and file-name
4082 (file-exists-p file-name)))
97f78c9b
MB
4083 (progn
4084 ;; If the message buffer has lived in a dedicated window,
4085 ;; `kill-buffer' has killed the frame. Thus the
4086 ;; `yes-or-no-p' may show up in a lowered frame. Make sure
4087 ;; that the user can see the question by raising the
4088 ;; current frame:
4089 (raise-frame)
4090 (yes-or-no-p (format "Remove the backup file%s? "
4091 (if modified " too" "")))))
23f87bed
MB
4092 (ignore-errors
4093 (delete-file auto-save-file-name))
4094 (let ((message-draft-article draft-article))
4095 (message-disassociate-draft)))
eec82323
LMI
4096 (message-do-actions actions))))
4097
4098(defun message-bury (buffer)
7d829636 4099 "Bury this mail BUFFER."
a22b4ca1
SS
4100 (bury-buffer buffer)
4101 (when message-return-action
4102 (apply (car message-return-action) (cdr message-return-action))))
eec82323
LMI
4103
4104(defun message-send (&optional arg)
4105 "Send the message in the current buffer.
16409b0b
GM
4106If `message-interactive' is non-nil, wait for success indication or
4107error messages, and inform user.
4108Otherwise any failure is reported in a message back to the user from
4109the mailer.
4110The usage of ARG is defined by the instance that called Message.
4111It should typically alter the sending method in some way or other."
eec82323 4112 (interactive "P")
16409b0b
GM
4113 ;; Make it possible to undo the coming changes.
4114 (undo-boundary)
4115 (let ((inhibit-read-only t))
4116 (put-text-property (point-min) (point-max) 'read-only nil))
4117 (message-fix-before-sending)
4118 (run-hooks 'message-send-hook)
16dd13df
MB
4119 (when message-confirm-send
4120 (or (y-or-n-p "Send message? ")
4121 (keyboard-quit)))
83595c0c 4122 (message message-sending-message)
16409b0b
GM
4123 (let ((alist message-send-method-alist)
4124 (success t)
23f87bed
MB
4125 elem sent dont-barf-on-no-method
4126 (message-options message-options))
4127 (message-options-set-recipient)
16409b0b
GM
4128 (while (and success
4129 (setq elem (pop alist)))
83595c0c
DL
4130 (when (funcall (cadr elem))
4131 (when (and (or (not (memq (car elem)
4132 message-sent-message-via))
23f87bed
MB
4133 (message-fetch-field "supersedes")
4134 (if (or (message-gnksa-enable-p 'multiple-copies)
4135 (not (eq (car elem) 'news)))
4136 (y-or-n-p
4137 (format
4138 "Already sent message via %s; resend? "
4139 (car elem)))
4140 (error "Denied posting -- multiple copies")))
83595c0c
DL
4141 (setq success (funcall (caddr elem) arg)))
4142 (setq sent t))))
23f87bed
MB
4143 (unless (or sent
4144 (not success)
4145 (let ((fcc (message-fetch-field "Fcc"))
4146 (gcc (message-fetch-field "Gcc")))
4147 (when (or fcc gcc)
4148 (or (eq message-allow-no-recipients 'always)
4149 (and (not (eq message-allow-no-recipients 'never))
4150 (setq dont-barf-on-no-method
4151 (gnus-y-or-n-p
4152 (format "No receiver, perform %s anyway? "
4153 (cond ((and fcc gcc) "Fcc and Gcc")
4154 (fcc "Fcc")
4155 (t "Gcc"))))))))))
16409b0b 4156 (error "No methods specified to send by"))
23f87bed
MB
4157 (when (or dont-barf-on-no-method
4158 (and success sent))
16409b0b
GM
4159 (message-do-fcc)
4160 (save-excursion
4161 (run-hooks 'message-sent-hook))
4162 (message "Sending...done")
01c52d31 4163 ;; Do ecomplete address snarfing.
6b8382e4
KY
4164 (when (and (message-mail-alias-type-p 'ecomplete)
4165 (not message-inhibit-ecomplete))
01c52d31 4166 (message-put-addresses-in-ecomplete))
16409b0b
GM
4167 ;; Mark the buffer as unmodified and delete auto-save.
4168 (set-buffer-modified-p nil)
4169 (delete-auto-save-file-if-necessary t)
4170 (message-disassociate-draft)
4171 ;; Delete other mail buffers and stuff.
4172 (message-do-send-housekeeping)
4173 (message-do-actions message-send-actions)
4174 ;; Return success.
4175 t)))
a8151ef7
LMI
4176
4177(defun message-send-via-mail (arg)
6748645f 4178 "Send the current message via mail."
a8151ef7
LMI
4179 (message-send-mail arg))
4180
4181(defun message-send-via-news (arg)
4182 "Send the current message via news."
4183 (funcall message-send-news-function arg))
eec82323 4184
16409b0b
GM
4185(defmacro message-check (type &rest forms)
4186 "Eval FORMS if TYPE is to be checked."
4187 `(or (message-check-element ,type)
4188 (save-excursion
4189 ,@forms)))
4190
4191(put 'message-check 'lisp-indent-function 1)
4192(put 'message-check 'edebug-form-spec '(form body))
4193
01c52d31
MB
4194(defun message-text-with-property (prop &optional start end reverse)
4195 "Return a list of start and end positions where the text has PROP.
4196START and END bound the search, they default to `point-min' and
4197`point-max' respectively. If REVERSE is non-nil, find text which does
4198not have PROP."
4199 (unless start
4200 (setq start (point-min)))
4201 (unless end
4202 (setq end (point-max)))
4203 (let (next regions)
4204 (if reverse
4205 (while (and start
4206 (setq start (text-property-any start end prop nil)))
4207 (setq next (next-single-property-change start prop nil end))
4208 (push (cons start (or next end)) regions)
4209 (setq start next))
4210 (while (and start
4211 (or (get-text-property start prop)
4212 (and (setq start (next-single-property-change
4213 start prop nil end))
4214 (get-text-property start prop))))
4215 (setq next (text-property-any start end prop nil))
4216 (push (cons start (or next end)) regions)
4217 (setq start next)))
4218 (nreverse regions)))
23f87bed 4219
656b4fa1 4220(defcustom message-bogus-addresses
656b4fa1
MB
4221 '("noreply" "nospam" "invalid" "@@" "[^[:ascii:]].*@" "[ \t]")
4222 "List of regexps of potentially bogus mail addresses.
4223See `message-check-recipients' how to setup checking.
4224
4225This list should make it possible to catch typos or warn about
4226spam-trap addresses. It doesn't aim to verify strict RFC
4227conformance."
330f707b 4228 :version "23.1" ;; No Gnus
437ce4be 4229 :group 'message-headers
656b4fa1
MB
4230 :type '(choice
4231 (const :tag "None" nil)
4232 (list
4233 (set :inline t
4234 (const "noreply")
4235 (const "nospam")
4236 (const "invalid")
4237 (const :tag "duplicate @" "@@")
4238 (const :tag "non-ascii local part" "[^[:ascii:]].*@")
4239 ;; Already caught by `message-valid-fqdn-regexp'
4240 ;; (const :tag "`_' in domain part" "@.*_")
4241 (const :tag "whitespace" "[ \t]"))
4242 (repeat :inline t
4243 :tag "Other"
4244 (regexp)))))
437ce4be 4245
eec82323
LMI
4246(defun message-fix-before-sending ()
4247 "Do various things to make the message nice before sending it."
4248 ;; Make sure there's a newline at the end of the message.
4249 (goto-char (point-max))
4250 (unless (bolp)
f129a4df 4251 (insert "\n"))
23f87bed 4252 ;; Make the hidden headers visible.
01c52d31
MB
4253 (widen)
4254 ;; Sort headers before sending the message.
4255 (message-sort-headers)
23f87bed
MB
4256 ;; Make invisible text visible.
4257 ;; It doesn't seem as if this is useful, since the invisible property
4258 ;; is clobbered by an after-change hook anyhow.
16409b0b 4259 (message-check 'invisible-text
01c52d31
MB
4260 (let ((regions (message-text-with-property 'invisible))
4261 from to)
4262 (when regions
4263 (while regions
4264 (setq from (caar regions)
4265 to (cdar regions)
4266 regions (cdr regions))
4267 (put-text-property from to 'invisible nil)
4268 (message-overlay-put (message-make-overlay from to)
23f87bed
MB
4269 'face 'highlight))
4270 (unless (yes-or-no-p
4271 "Invisible text found and made visible; continue sending? ")
4272 (error "Invisible text found and made visible")))))
4273 (message-check 'illegible-text
4d9db491 4274 (let (char found choice nul-chars)
23f87bed 4275 (message-goto-body)
4d9db491
G
4276 (setq nul-chars (save-excursion
4277 (search-forward "\000" nil t)))
01c52d31
MB
4278 (while (progn
4279 (skip-chars-forward mm-7bit-chars)
4280 (when (get-text-property (point) 'no-illegible-text)
4281 ;; There is a signed or encrypted raw message part
4282 ;; that is considered to be safe.
4283 (goto-char (or (next-single-property-change
4284 (point) 'no-illegible-text)
4285 (point-max))))
4286 (setq char (char-after)))
4287 (when (or (< (mm-char-int char) 128)
4288 (and (mm-multibyte-p)
4289 (memq (char-charset char)
4290 '(eight-bit-control eight-bit-graphic
e3e955fe
MB
4291 ;; Emacs 23, Bug#1770:
4292 eight-bit
01c52d31
MB
4293 control-1))
4294 (not (get-text-property
4295 (point) 'untranslated-utf-8))))
23f87bed
MB
4296 (message-overlay-put (message-make-overlay (point) (1+ (point)))
4297 'face 'highlight)
4298 (setq found t))
01c52d31 4299 (forward-char))
23f87bed
MB
4300 (when found
4301 (setq choice
4302 (gnus-multiple-choice
4d9db491
G
4303 (if nul-chars
4304 "NUL characters found, which may cause problems. Continue sending?"
4305 "Non-printable characters found. Continue sending?")
4573e0df
MB
4306 `((?d "Remove non-printable characters and send")
4307 (?r ,(format
4308 "Replace non-printable characters with \"%s\" and send"
4309 message-replacement-char))
6c85a14f 4310 (?s "Send as is without removing anything")
23f87bed
MB
4311 (?e "Continue editing"))))
4312 (if (eq choice ?e)
4313 (error "Non-printable characters"))
4314 (message-goto-body)
4315 (skip-chars-forward mm-7bit-chars)
4316 (while (not (eobp))
4317 (when (let ((char (char-after)))
4318 (or (< (mm-char-int char) 128)
4319 (and (mm-multibyte-p)
5f4264e5 4320 ;; FIXME: Wrong for Emacs 23 (unicode) and for
e3e955fe
MB
4321 ;; things like undecodable utf-8 (in Emacs 21?).
4322 ;; Should at least use find-coding-systems-region.
4323 ;; -- fx
23f87bed
MB
4324 (memq (char-charset char)
4325 '(eight-bit-control eight-bit-graphic
e3e955fe
MB
4326 ;; Emacs 23, Bug#1770:
4327 eight-bit
23f87bed
MB
4328 control-1))
4329 (not (get-text-property
4330 (point) 'untranslated-utf-8)))))
4331 (if (eq choice ?i)
4332 (message-kill-all-overlays)
4333 (delete-char 1)
4334 (when (eq choice ?r)
4573e0df 4335 (insert message-replacement-char))))
23f87bed 4336 (forward-char)
437ce4be
MB
4337 (skip-chars-forward mm-7bit-chars)))))
4338 (message-check 'bogus-recipient
03c673c9 4339 ;; Warn before sending a mail to an invalid address.
437ce4be
MB
4340 (message-check-recipients)))
4341
4342(defun message-bogus-recipient-p (recipients)
4343 "Check if a mail address in RECIPIENTS looks bogus.
4344
4345RECIPIENTS is a mail header. Return a list of potentially bogus
4346addresses. If none is found, return nil.
4347
656b4fa1
MB
4348An address might be bogus if the domain part is not fully
4349qualified, see `message-valid-fqdn-regexp', or if there's a
4350matching entry in `message-bogus-addresses'."
437ce4be
MB
4351 ;; FIXME: How about "foo@subdomain", when the MTA adds ".domain.tld"?
4352 (let (found)
4353 (mapc (lambda (address)
3b84b005 4354 (setq address (or (cadr address) ""))
437ce4be 4355 (when
3b84b005
G
4356 (or (string= "" address)
4357 (not
437ce4be
MB
4358 (or
4359 (not (string-match "@" address))
4360 (string-match
4361 (concat ".@.*\\("
4362 message-valid-fqdn-regexp "\\)\\'") address)))
656b4fa1
MB
4363 (and message-bogus-addresses
4364 (let ((re
4365 (if (listp message-bogus-addresses)
4366 (mapconcat 'identity
4367 message-bogus-addresses
4368 "\\|")
4369 message-bogus-addresses)))
4370 (string-match re address))))
3b84b005 4371 (push address found)))
437ce4be
MB
4372 ;;
4373 (mail-extract-address-components recipients t))
4374 found))
4375
4376(defun message-check-recipients ()
4377 "Warn before composing or sending a mail to an invalid address.
4378
4379This function could be useful in `message-setup-hook'."
4380 (interactive)
4381 (save-restriction
4382 (message-narrow-to-headers)
4383 (dolist (hdr '("To" "Cc" "Bcc"))
4384 (let ((addr (message-fetch-field hdr)))
4385 (when (stringp addr)
4386 (dolist (bog (message-bogus-recipient-p addr))
4387 (and bog
4388 (not (y-or-n-p
4389 (format
9e928ac9
G
4390 "Address `%s'%s might be bogus. Continue? "
4391 bog
4392 ;; If the encoded version of the email address
4393 ;; is different from the unencoded version,
4394 ;; then we likely have invisible characters or
4395 ;; the like. Display the encoded version,
4396 ;; too.
4397 (let ((encoded (rfc2047-encode-string bog)))
4398 (if (string= encoded bog)
4399 ""
4400 (format " (%s)" encoded))))))
d93ec753 4401 (error "Bogus address"))))))))
eec82323 4402
03c673c9
MB
4403(custom-add-option 'message-setup-hook 'message-check-recipients)
4404
eec82323
LMI
4405(defun message-add-action (action &rest types)
4406 "Add ACTION to be performed when doing an exit of type TYPES."
23f87bed
MB
4407 (while types
4408 (add-to-list (intern (format "message-%s-actions" (pop types)))
4409 action)))
4410
4411(defun message-delete-action (action &rest types)
4412 "Delete ACTION from lists of actions performed when doing an exit of type TYPES."
eec82323
LMI
4413 (let (var)
4414 (while types
4415 (set (setq var (intern (format "message-%s-actions" (pop types))))
23f87bed 4416 (delq action (symbol-value var))))))
eec82323
LMI
4417
4418(defun message-do-actions (actions)
4419 "Perform all actions in ACTIONS."
4420 ;; Now perform actions on successful sending.
01c52d31 4421 (dolist (action actions)
eec82323
LMI
4422 (ignore-errors
4423 (cond
4424 ;; A simple function.
01c52d31
MB
4425 ((functionp action)
4426 (funcall action))
99d99081 4427 ;; Something to be evalled.
eec82323 4428 (t
01c52d31 4429 (eval action))))))
eec82323 4430
16409b0b 4431(defun message-send-mail-partially ()
23f87bed 4432 "Send mail as message/partial."
83595c0c
DL
4433 ;; replace the header delimiter with a blank line
4434 (goto-char (point-min))
4435 (re-search-forward
4436 (concat "^" (regexp-quote mail-header-separator) "\n"))
f129a4df 4437 (replace-match "\n")
83595c0c 4438 (run-hooks 'message-send-mail-hook)
16409b0b
GM
4439 (let ((p (goto-char (point-min)))
4440 (tembuf (message-generate-new-buffer-clone-locals " message temp"))
4441 (curbuf (current-buffer))
4442 (id (message-make-message-id)) (n 1)
99c2a4e3 4443 plist total header)
16409b0b
GM
4444 (while (not (eobp))
4445 (if (< (point-max) (+ p message-send-mail-partially-limit))
4446 (goto-char (point-max))
4447 (goto-char (+ p message-send-mail-partially-limit))
4448 (beginning-of-line)
4449 (if (<= (point) p) (forward-line 1))) ;; In case of bad message.
4450 (push p plist)
4451 (setq p (point)))
4452 (setq total (length plist))
4453 (push (point-max) plist)
4454 (setq plist (nreverse plist))
4455 (unwind-protect
4456 (save-excursion
4457 (setq p (pop plist))
4458 (while plist
4459 (set-buffer curbuf)
4460 (copy-to-buffer tembuf p (car plist))
4461 (set-buffer tembuf)
4462 (goto-char (point-min))
4463 (if header
4464 (progn
4465 (goto-char (point-min))
4466 (narrow-to-region (point) (point))
4467 (insert header))
4468 (message-goto-eoh)
4469 (setq header (buffer-substring (point-min) (point)))
4470 (goto-char (point-min))
4471 (narrow-to-region (point) (point))
4472 (insert header)
4473 (message-remove-header "Mime-Version")
4474 (message-remove-header "Content-Type")
4475 (message-remove-header "Content-Transfer-Encoding")
4476 (message-remove-header "Message-ID")
4477 (message-remove-header "Lines")
4478 (goto-char (point-max))
f129a4df 4479 (insert "Mime-Version: 1.0\n")
23f87bed 4480 (setq header (buffer-string)))
16409b0b 4481 (goto-char (point-max))
f129a4df
CY
4482 (insert (format "Content-Type: message/partial; id=\"%s\"; number=%d; total=%d\n\n"
4483 id n total))
23f87bed 4484 (forward-char -1)
16409b0b
GM
4485 (let ((mail-header-separator ""))
4486 (when (memq 'Message-ID message-required-mail-headers)
f129a4df 4487 (insert "Message-ID: " (message-make-message-id) "\n"))
16409b0b 4488 (when (memq 'Lines message-required-mail-headers)
f129a4df 4489 (insert "Lines: " (message-make-lines) "\n"))
16409b0b
GM
4490 (message-goto-subject)
4491 (end-of-line)
4492 (insert (format " (%d/%d)" n total))
16409b0b 4493 (widen)
89b163db
G
4494 (if message-send-mail-real-function
4495 (funcall message-send-mail-real-function)
4496 (message-multi-smtp-send-mail)))
16409b0b
GM
4497 (setq n (+ n 1))
4498 (setq p (pop plist))
4499 (erase-buffer)))
4500 (kill-buffer tembuf))))
4501
aa8f8277
GM
4502(declare-function hashcash-wait-async "hashcash" (&optional buffer))
4503
eec82323
LMI
4504(defun message-send-mail (&optional arg)
4505 (require 'mail-utils)
16409b0b
GM
4506 (let* ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
4507 (case-fold-search nil)
4508 (news (message-news-p))
4509 (mailbuf (current-buffer))
4510 (message-this-is-mail t)
aa8f8277
GM
4511 ;; gnus-setup-posting-charset is autoloaded in mml.el (FIXME
4512 ;; maybe it should not be), which this file requires. Hence
4513 ;; the fboundp test is always true. Loading it from gnus-msg
4514 ;; loads many Gnus files (Bug#5642). If
4515 ;; gnus-group-posting-charset-alist hasn't been customized,
4516 ;; this is just going to return nil anyway. FIXME it would
4517 ;; be good to improve this further, because even if g-g-p-c-a
4518 ;; has been customized, that is likely to just be for news.
4519 ;; Eg either move the definition from gnus-msg, or separate out
4520 ;; the mail and news parts.
16409b0b 4521 (message-posting-charset
aa8f8277
GM
4522 (if (and (fboundp 'gnus-setup-posting-charset)
4523 (boundp 'gnus-group-posting-charset-alist))
16409b0b 4524 (gnus-setup-posting-charset nil)
23f87bed 4525 message-posting-charset))
f65bd833
KY
4526 (headers message-required-mail-headers)
4527 options)
01c52d31
MB
4528 (when (and message-generate-hashcash
4529 (not (eq message-generate-hashcash 'opportunistic)))
4530 (message "Generating hashcash...")
aa8f8277 4531 (require 'hashcash)
01c52d31
MB
4532 ;; Wait for calculations already started to finish...
4533 (hashcash-wait-async)
4534 ;; ...and do calculations not already done. mail-add-payment
4535 ;; will leave existing X-Hashcash headers alone.
4536 (mail-add-payment)
4537 (message "Generating hashcash...done"))
eec82323
LMI
4538 (save-restriction
4539 (message-narrow-to-headers)
23f87bed
MB
4540 ;; Generate the Mail-Followup-To header if the header is not there...
4541 (if (and (message-subscribed-p)
4542 (not (mail-fetch-field "mail-followup-to")))
4543 (setq headers
4544 (cons
4545 (cons "Mail-Followup-To" (message-make-mail-followup-to))
4546 message-required-mail-headers))
4547 ;; otherwise, delete the MFT header if the field is empty
4548 (when (equal "" (mail-fetch-field "mail-followup-to"))
4549 (message-remove-header "^Mail-Followup-To:")))
eec82323
LMI
4550 ;; Insert some headers.
4551 (let ((message-deletable-headers
4552 (if news nil message-deletable-headers)))
23f87bed 4553 (message-generate-headers headers))
f362b760
MB
4554 ;; Check continuation headers.
4555 (message-check 'continuation-headers
4556 (goto-char (point-min))
4557 (while (re-search-forward "^[^ \t\n][^ \t\n:]*[ \t\n]" nil t)
4558 (goto-char (match-beginning 0))
4559 (if (y-or-n-p "Fix continuation lines? ")
4560 (insert " ")
4561 (forward-line 1)
4562 (unless (y-or-n-p "Send anyway? ")
4563 (error "Failed to send the message")))))
eec82323
LMI
4564 ;; Let the user do all of the above.
4565 (run-hooks 'message-header-hook))
f65bd833 4566 (setq options message-options)
eec82323 4567 (unwind-protect
82816ba1 4568 (with-current-buffer tembuf
eec82323 4569 (erase-buffer)
f65bd833 4570 (setq message-options options)
23f87bed 4571 ;; Avoid copying text props (except hard newlines).
ce1ec550 4572 (insert (with-current-buffer mailbuf
23f87bed
MB
4573 (mml-buffer-substring-no-properties-except-hard-newlines
4574 (point-min) (point-max))))
eec82323 4575 ;; Remove some headers.
16409b0b 4576 (message-encode-message-body)
eec82323
LMI
4577 (save-restriction
4578 (message-narrow-to-headers)
16409b0b
GM
4579 ;; We (re)generate the Lines header.
4580 (when (memq 'Lines message-required-mail-headers)
4581 (message-generate-headers '(Lines)))
eec82323 4582 ;; Remove some headers.
16409b0b
GM
4583 (message-remove-header message-ignored-mail-headers t)
4584 (let ((mail-parse-charset message-default-charset))
4585 (mail-encode-encoded-word-buffer)))
eec82323
LMI
4586 (goto-char (point-max))
4587 ;; require one newline at the end.
4588 (or (= (preceding-char) ?\n)
f129a4df 4589 (insert ?\n))
23f87bed
MB
4590 (message-cleanup-headers)
4591 ;; FIXME: we're inserting the courtesy copy after encoding.
4592 ;; This is wrong if the courtesy copy string contains
4593 ;; non-ASCII characters. -- jh
7d829636 4594 (when
16409b0b
GM
4595 (save-restriction
4596 (message-narrow-to-headers)
4597 (and news
4ddab346
G
4598 (not (message-fetch-field "List-Post"))
4599 (not (message-fetch-field "List-ID"))
eec82323 4600 (or (message-fetch-field "cc")
23f87bed 4601 (message-fetch-field "bcc")
16409b0b 4602 (message-fetch-field "to"))
23f87bed
MB
4603 (let ((content-type (message-fetch-field
4604 "content-type")))
4605 (and
4606 (or
4607 (not content-type)
4608 (string= "text/plain"
4609 (car
4610 (mail-header-parse-content-type
4611 content-type))))
4612 (not
4613 (string= "base64"
4614 (message-fetch-field
4615 "content-transfer-encoding")))))))
181cb5fb
G
4616 (message-insert-courtesy-copy
4617 (with-current-buffer mailbuf
4618 message-courtesy-message)))
8a986865
SM
4619 ;; Let's make sure we encoded all the body.
4620 (assert (save-excursion
4621 (goto-char (point-min))
4622 (not (re-search-forward "[^\000-\377]" nil t))))
4623 (mm-disable-multibyte)
16409b0b 4624 (if (or (not message-send-mail-partially-limit)
3c3979f1 4625 (< (buffer-size) message-send-mail-partially-limit)
23f87bed
MB
4626 (not (message-y-or-n-p
4627 "The message size is too large, split? "
4628 t
4629 "\
4630The message size, "
3c3979f1 4631 (/ (buffer-size) 1000) "KB, is too large.
23f87bed
MB
4632
4633Some mail gateways (MTA's) bounce large messages. To avoid the
4634problem, answer `y', and the message will be split into several
4635smaller pieces, the size of each is about "
4636 (/ message-send-mail-partially-limit 1000)
4637 "KB except the last
4638one.
4639
4640However, some mail readers (MUA's) can't read split messages, i.e.,
4641mails in message/partially format. Answer `n', and the message will be
4642sent in one piece.
4643
4644The size limit is controlled by `message-send-mail-partially-limit'.
4645If you always want Gnus to send messages in one piece, set
4646`message-send-mail-partially-limit' to nil.
4647")))
8a986865 4648 (progn
23f87bed 4649 (message "Sending via mail...")
89b163db
G
4650 (if message-send-mail-real-function
4651 (funcall message-send-mail-real-function)
4652 (message-multi-smtp-send-mail)))
f65bd833
KY
4653 (message-send-mail-partially))
4654 (setq options message-options))
eec82323
LMI
4655 (kill-buffer tembuf))
4656 (set-buffer mailbuf)
f65bd833 4657 (setq message-options options)
eec82323
LMI
4658 (push 'mail message-sent-message-via)))
4659
f0dd243b 4660(defvar sendmail-program)
89b163db
G
4661(defvar smtpmail-smtp-user)
4662
4663(defun message-multi-smtp-send-mail ()
4664 "Send the current buffer to `message-send-mail-function'.
4665Or, if there's a header that specifies a different method, use
4666that instead."
4667 (let ((method (message-field-value "X-Message-SMTP-Method")))
4668 (if (not method)
4669 (funcall message-send-mail-function)
4670 (message-remove-header "X-Message-SMTP-Method")
4671 (setq method (split-string method))
4672 (cond
4673 ((equal (car method) "sendmail")
4674 (message-send-mail-with-sendmail))
4675 ((equal (car method) "smtp")
4676 (require 'smtpmail)
4677 (let ((smtpmail-smtp-server (nth 1 method))
4678 (smtpmail-smtp-service (nth 2 method))
4679 (smtpmail-smtp-user (or (nth 3 method) smtpmail-smtp-user)))
4680 (message-smtpmail-send-it)))
4681 (t
4682 (error "Unknown method %s" method))))))
f0dd243b 4683
eec82323
LMI
4684(defun message-send-mail-with-sendmail ()
4685 "Send off the prepared buffer with sendmail."
aa8f8277 4686 (require 'sendmail)
eec82323 4687 (let ((errbuf (if message-interactive
16409b0b
GM
4688 (message-generate-new-buffer-clone-locals
4689 " sendmail errors")
eec82323
LMI
4690 0))
4691 resend-to-addresses delimline)
23f87bed
MB
4692 (unwind-protect
4693 (progn
4694 (let ((case-fold-search t))
4695 (save-restriction
4696 (message-narrow-to-headers)
4697 (setq resend-to-addresses (message-fetch-field "resent-to")))
4698 ;; Change header-delimiter to be what sendmail expects.
4699 (goto-char (point-min))
4700 (re-search-forward
4701 (concat "^" (regexp-quote mail-header-separator) "\n"))
f129a4df 4702 (replace-match "\n")
23f87bed
MB
4703 (backward-char 1)
4704 (setq delimline (point-marker))
4705 (run-hooks 'message-send-mail-hook)
4706 ;; Insert an extra newline if we need it to work around
4707 ;; Sun's bug that swallows newlines.
4708 (goto-char (1+ delimline))
4709 (when (eval message-mailer-swallows-blank-line)
4710 (newline))
4711 (when message-interactive
01c52d31 4712 (with-current-buffer errbuf
23f87bed
MB
4713 (erase-buffer))))
4714 (let* ((default-directory "/")
4715 (coding-system-for-write message-send-coding-system)
4716 (cpr (apply
4717 'call-process-region
4718 (append
9d463c8c 4719 (list (point-min) (point-max) sendmail-program
23f87bed 4720 nil errbuf nil "-oi")
01c52d31 4721 message-sendmail-extra-arguments
23f87bed
MB
4722 ;; Always specify who from,
4723 ;; since some systems have broken sendmails.
4724 ;; But some systems are more broken with -f, so
4725 ;; we'll let users override this.
4332f3ec 4726 (and (null message-sendmail-f-is-evil)
4332f3ec 4727 (list "-f" (message-sendmail-envelope-from)))
23f87bed
MB
4728 ;; These mean "report errors by mail"
4729 ;; and "deliver in background".
4730 (if (null message-interactive) '("-oem" "-odb"))
4731 ;; Get the addresses from the message
4732 ;; unless this is a resend.
4733 ;; We must not do that for a resend
4734 ;; because we would find the original addresses.
4735 ;; For a resend, include the specific addresses.
4736 (if resend-to-addresses
4737 (list resend-to-addresses)
4738 '("-t"))))))
4739 (unless (or (null cpr) (and (numberp cpr) (zerop cpr)))
ad546360 4740 (if errbuf (pop-to-buffer errbuf))
23f87bed
MB
4741 (error "Sending...failed with exit value %d" cpr)))
4742 (when message-interactive
82816ba1 4743 (with-current-buffer errbuf
23f87bed 4744 (goto-char (point-min))
01c52d31 4745 (while (re-search-forward "\n+ *" nil t)
23f87bed
MB
4746 (replace-match "; "))
4747 (if (not (zerop (buffer-size)))
4748 (error "Sending...failed to %s"
4749 (buffer-string))))))
eec82323
LMI
4750 (when (bufferp errbuf)
4751 (kill-buffer errbuf)))))
4752
4753(defun message-send-mail-with-qmail ()
4754 "Pass the prepared message buffer to qmail-inject.
4755Refer to the documentation for the variable `message-send-mail-function'
4756to find out how to use this."
4757 ;; replace the header delimiter with a blank line
4758 (goto-char (point-min))
4759 (re-search-forward
4760 (concat "^" (regexp-quote mail-header-separator) "\n"))
f129a4df 4761 (replace-match "\n")
eec82323
LMI
4762 (run-hooks 'message-send-mail-hook)
4763 ;; send the message
4764 (case
6748645f
LMI
4765 (let ((coding-system-for-write message-send-coding-system))
4766 (apply
3c3979f1
SM
4767 'call-process-region (point-min) (point-max)
4768 message-qmail-inject-program nil nil nil
fffa137c 4769 ;; qmail-inject's default behavior is to look for addresses on the
6748645f
LMI
4770 ;; command line; if there're none, it scans the headers.
4771 ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
4772 ;;
4773 ;; in general, ALL of qmail-inject's defaults are perfect for simply
4774 ;; reading a formatted (i. e., at least a To: or Resent-To header)
4775 ;; message from stdin.
4776 ;;
4777 ;; qmail also has the advantage of not having been raped by
4778 ;; various vendors, so we don't have to allow for that, either --
4779 ;; compare this with message-send-mail-with-sendmail and weep
4780 ;; for sendmail's lost innocence.
4781 ;;
4782 ;; all this is way cool coz it lets us keep the arguments entirely
4783 ;; free for -inject-arguments -- a big win for the user and for us
4784 ;; since we don't have to play that double-guessing game and the user
4785 ;; gets full control (no gestapo'ish -f's, for instance). --sj
01c52d31
MB
4786 (if (functionp message-qmail-inject-args)
4787 (funcall message-qmail-inject-args)
4788 message-qmail-inject-args)))
eec82323
LMI
4789 ;; qmail-inject doesn't say anything on it's stdout/stderr,
4790 ;; we have to look at the retval instead
4791 (0 nil)
23f87bed 4792 (100 (error "qmail-inject reported permanent failure"))
a8151ef7 4793 (111 (error "qmail-inject reported transient failure"))
eec82323 4794 ;; should never happen
a8151ef7 4795 (t (error "qmail-inject reported unknown failure"))))
eec82323 4796
99c2a4e3
SM
4797(defvar mh-previous-window-config)
4798
eec82323
LMI
4799(defun message-send-mail-with-mh ()
4800 "Send the prepared message buffer with mh."
4801 (let ((mh-previous-window-config nil)
6748645f 4802 (name (mh-new-draft-name)))
eec82323
LMI
4803 (setq buffer-file-name name)
4804 ;; MH wants to generate these headers itself.
4805 (when message-mh-deletable-headers
4806 (let ((headers message-mh-deletable-headers))
4807 (while headers
4808 (goto-char (point-min))
4809 (and (re-search-forward
4810 (concat "^" (symbol-name (car headers)) ": *") nil t)
4811 (message-delete-line))
4812 (pop headers))))
4813 (run-hooks 'message-send-mail-hook)
4814 ;; Pass it on to mh.
4815 (mh-send-letter)))
4816
23f87bed
MB
4817(defun message-smtpmail-send-it ()
4818 "Send the prepared message buffer with `smtpmail-send-it'.
daf4b83b
GM
4819The only difference from `smtpmail-send-it' is that this command
4820evaluates `message-send-mail-hook' just before sending a message.
4821It is useful if your ISP requires the POP-before-SMTP
4822authentication. See the Gnus manual for details."
23f87bed
MB
4823 (run-hooks 'message-send-mail-hook)
4824 (smtpmail-send-it))
4825
2ea6167b
RS
4826(defun message-send-mail-with-mailclient ()
4827 "Send the prepared message buffer with `mailclient-send-it'.
daf4b83b
GM
4828The only difference from `mailclient-send-it' is that this
4829command evaluates `message-send-mail-hook' just before sending a message."
2ea6167b
RS
4830 (run-hooks 'message-send-mail-hook)
4831 (mailclient-send-it))
4832
23f87bed
MB
4833(defun message-canlock-generate ()
4834 "Return a string that is non-trivial to guess.
4835Do not use this for anything important, it is cryptographically weak."
4836 (require 'sha1)
4837 (let (sha1-maximum-internal-length)
4838 (sha1 (concat (message-unique-id)
0e23ef9d 4839 (format "%x%x%x" (random) (random) (random))
23f87bed
MB
4840 (prin1-to-string (recent-keys))
4841 (prin1-to-string (garbage-collect))))))
4842
aa8f8277
GM
4843(defvar canlock-password)
4844(defvar canlock-password-for-verify)
4845
23f87bed
MB
4846(defun message-canlock-password ()
4847 "The password used by message for cancel locks.
4848This is the value of `canlock-password', if that option is non-nil.
4849Otherwise, generate and save a value for `canlock-password' first."
aa8f8277 4850 (require 'canlock)
23f87bed
MB
4851 (unless canlock-password
4852 (customize-save-variable 'canlock-password (message-canlock-generate))
4853 (setq canlock-password-for-verify canlock-password))
4854 canlock-password)
4855
4856(defun message-insert-canlock ()
4857 (when message-insert-canlock
4858 (message-canlock-password)
4859 (canlock-insert-header)))
4860
aa8f8277
GM
4861(autoload 'nnheader-get-report "nnheader")
4862
4863(declare-function gnus-setup-posting-charset "gnus-msg" (group))
4864
eec82323 4865(defun message-send-news (&optional arg)
aa8f8277 4866 (require 'gnus-msg)
16409b0b
GM
4867 (let* ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
4868 (case-fold-search nil)
23f87bed 4869 (method (if (functionp message-post-method)
16409b0b
GM
4870 (funcall message-post-method arg)
4871 message-post-method))
23f87bed
MB
4872 (newsgroups-field (save-restriction
4873 (message-narrow-to-headers-or-head)
4874 (message-fetch-field "Newsgroups")))
4875 (followup-field (save-restriction
4876 (message-narrow-to-headers-or-head)
4877 (message-fetch-field "Followup-To")))
4878 ;; BUG: We really need to get the charset for each name in the
4879 ;; Newsgroups and Followup-To lines to allow crossposting
c80e3b4a 4880 ;; between group names with incompatible character sets.
23f87bed
MB
4881 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
4882 (group-field-charset
4883 (gnus-group-name-charset method newsgroups-field))
4884 (followup-field-charset
4885 (gnus-group-name-charset method (or followup-field "")))
16409b0b 4886 (rfc2047-header-encoding-alist
23f87bed
MB
4887 (append (when group-field-charset
4888 (list (cons "Newsgroups" group-field-charset)))
4889 (when followup-field-charset
4890 (list (cons "Followup-To" followup-field-charset)))
4891 rfc2047-header-encoding-alist))
16409b0b
GM
4892 (messbuf (current-buffer))
4893 (message-syntax-checks
23f87bed
MB
4894 (if (and arg
4895 (listp message-syntax-checks))
16409b0b
GM
4896 (cons '(existing-newsgroups . disabled)
4897 message-syntax-checks)
4898 message-syntax-checks))
4899 (message-this-is-news t)
23f87bed
MB
4900 (message-posting-charset
4901 (gnus-setup-posting-charset newsgroups-field))
16409b0b
GM
4902 result)
4903 (if (not (message-check-news-body-syntax))
4904 nil
4905 (save-restriction
4906 (message-narrow-to-headers)
4907 ;; Insert some headers.
4908 (message-generate-headers message-required-news-headers)
23f87bed 4909 (message-insert-canlock)
16409b0b
GM
4910 ;; Let the user do all of the above.
4911 (run-hooks 'message-header-hook))
23f87bed
MB
4912 ;; Note: This check will be disabled by the ".*" default value for
4913 ;; gnus-group-name-charset-group-alist. -- Pa 2001-10-07.
4914 (when (and group-field-charset
4915 (listp message-syntax-checks))
4916 (setq message-syntax-checks
16409b0b
GM
4917 (cons '(valid-newsgroups . disabled)
4918 message-syntax-checks)))
4919 (message-cleanup-headers)
23f87bed
MB
4920 (if (not (let ((message-post-method method))
4921 (message-check-news-syntax)))
16409b0b
GM
4922 nil
4923 (unwind-protect
82816ba1 4924 (with-current-buffer tembuf
16409b0b
GM
4925 (buffer-disable-undo)
4926 (erase-buffer)
23f87bed
MB
4927 ;; Avoid copying text props (except hard newlines).
4928 (insert
4929 (with-current-buffer messbuf
4930 (mml-buffer-substring-no-properties-except-hard-newlines
4931 (point-min) (point-max))))
16409b0b 4932 (message-encode-message-body)
eec82323 4933 ;; Remove some headers.
16409b0b
GM
4934 (save-restriction
4935 (message-narrow-to-headers)
4936 ;; We (re)generate the Lines header.
4937 (when (memq 'Lines message-required-mail-headers)
4938 (message-generate-headers '(Lines)))
4939 ;; Remove some headers.
4940 (message-remove-header message-ignored-news-headers t)
4941 (let ((mail-parse-charset message-default-charset))
4942 (mail-encode-encoded-word-buffer)))
4943 (goto-char (point-max))
4944 ;; require one newline at the end.
4945 (or (= (preceding-char) ?\n)
f129a4df 4946 (insert ?\n))
16409b0b
GM
4947 (let ((case-fold-search t))
4948 ;; Remove the delimiter.
4949 (goto-char (point-min))
4950 (re-search-forward
4951 (concat "^" (regexp-quote mail-header-separator) "\n"))
f129a4df 4952 (replace-match "\n")
16409b0b
GM
4953 (backward-char 1))
4954 (run-hooks 'message-send-news-hook)
4955 (gnus-open-server method)
23f87bed 4956 (message "Sending news via %s..." (gnus-server-string method))
16409b0b
GM
4957 (setq result (let ((mail-header-separator ""))
4958 (gnus-request-post method))))
4959 (kill-buffer tembuf))
4960 (set-buffer messbuf)
4961 (if result
4962 (push 'news message-sent-message-via)
4963 (message "Couldn't send message via news: %s"
4964 (nnheader-get-report (car method)))
4965 nil)))))
eec82323
LMI
4966
4967;;;
4968;;; Header generation & syntax checking.
4969;;;
4970
eec82323 4971(defun message-check-element (type)
7d829636 4972 "Return non-nil if this TYPE is not to be checked."
eec82323
LMI
4973 (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
4974 t
4975 (let ((able (assq type message-syntax-checks)))
4976 (and (consp able)
4977 (eq (cdr able) 'disabled)))))
4978
4979(defun message-check-news-syntax ()
4980 "Check the syntax of the message."
4981 (save-excursion
4982 (save-restriction
4983 (widen)
16409b0b
GM
4984 ;; We narrow to the headers and check them first.
4985 (save-excursion
4986 (save-restriction
4987 (message-narrow-to-headers)
4988 (message-check-news-header-syntax))))))
eec82323
LMI
4989
4990(defun message-check-news-header-syntax ()
4991 (and
16409b0b
GM
4992 ;; Check Newsgroups header.
4993 (message-check 'newsgroups
4994 (let ((group (message-fetch-field "newsgroups")))
4995 (or
4996 (and group
4997 (not (string-match "\\`[ \t]*\\'" group)))
4998 (ignore
4999 (message
5000 "The newsgroups field is empty or missing. Posting is denied.")))))
eec82323
LMI
5001 ;; Check the Subject header.
5002 (message-check 'subject
5003 (let* ((case-fold-search t)
5004 (subject (message-fetch-field "subject")))
5005 (or
5006 (and subject
5007 (not (string-match "\\`[ \t]*\\'" subject)))
5008 (ignore
5009 (message
5010 "The subject field is empty or missing. Posting is denied.")))))
5011 ;; Check for commands in Subject.
5012 (message-check 'subject-cmsg
5013 (if (string-match "^cmsg " (message-fetch-field "subject"))
5014 (y-or-n-p
5015 "The control code \"cmsg\" is in the subject. Really post? ")
5016 t))
23f87bed
MB
5017 ;; Check long header lines.
5018 (message-check 'long-header-lines
99c2a4e3 5019 (let ((header nil)
23f87bed
MB
5020 (length 0)
5021 found)
5022 (while (and (not found)
5023 (re-search-forward "^\\([^ \t:]+\\): " nil t))
5024 (if (> (- (point) (match-beginning 0)) 998)
5025 (setq found t
5026 length (- (point) (match-beginning 0)))
5027 (setq header (match-string-no-properties 1)))
23f87bed
MB
5028 (forward-line 1))
5029 (if found
5030 (y-or-n-p (format "Your %s header is too long (%d). Really post? "
5031 header length))
5032 t)))
eec82323
LMI
5033 ;; Check for multiple identical headers.
5034 (message-check 'multiple-headers
5035 (let (found)
5036 (while (and (not found)
5037 (re-search-forward "^[^ \t:]+: " nil t))
5038 (save-excursion
5039 (or (re-search-forward
5040 (concat "^"
5041 (regexp-quote
5042 (setq found
5043 (buffer-substring
5044 (match-beginning 0) (- (match-end 0) 2))))
5045 ":")
5046 nil t)
5047 (setq found nil))))
5048 (if found
5049 (y-or-n-p (format "Multiple %s headers. Really post? " found))
5050 t)))
5051 ;; Check for Version and Sendsys.
5052 (message-check 'sendsys
5053 (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
5054 (y-or-n-p
5055 (format "The article contains a %s command. Really post? "
5056 (buffer-substring (match-beginning 0)
5057 (1- (match-end 0)))))
5058 t))
5059 ;; See whether we can shorten Followup-To.
5060 (message-check 'shorten-followup-to
5061 (let ((newsgroups (message-fetch-field "newsgroups"))
5062 (followup-to (message-fetch-field "followup-to"))
5063 to)
5064 (when (and newsgroups
5065 (string-match "," newsgroups)
5066 (not followup-to)
5067 (not
5068 (zerop
5069 (length
5070 (setq to (completing-read
81df110a 5071 "Followups to (default no Followup-To header): "
23f87bed 5072 (mapcar #'list
eec82323
LMI
5073 (cons "poster"
5074 (message-tokenize-header
5075 newsgroups)))))))))
5076 (goto-char (point-min))
f129a4df 5077 (insert "Followup-To: " to "\n"))
eec82323
LMI
5078 t))
5079 ;; Check "Shoot me".
5080 (message-check 'shoot
5081 (if (re-search-forward
23f87bed 5082 "Message-ID.*.i-did-not-set--mail-host-address--so-tickle-me" nil t)
eec82323
LMI
5083 (y-or-n-p "You appear to have a misconfigured system. Really post? ")
5084 t))
5085 ;; Check for Approved.
5086 (message-check 'approved
5087 (if (re-search-forward "^Approved:" nil t)
5088 (y-or-n-p "The article contains an Approved header. Really post? ")
5089 t))
5090 ;; Check the Message-ID header.
5091 (message-check 'message-id
5092 (let* ((case-fold-search t)
5093 (message-id (message-fetch-field "message-id" t)))
5094 (or (not message-id)
6748645f 5095 ;; Is there an @ in the ID?
eec82323 5096 (and (string-match "@" message-id)
6748645f
LMI
5097 ;; Is there a dot in the ID?
5098 (string-match "@[^.]*\\." message-id)
5099 ;; Does the ID end with a dot?
5100 (not (string-match "\\.>" message-id)))
eec82323
LMI
5101 (y-or-n-p
5102 (format "The Message-ID looks strange: \"%s\". Really post? "
5103 message-id)))))
5104 ;; Check the Newsgroups & Followup-To headers.
5105 (message-check 'existing-newsgroups
5106 (let* ((case-fold-search t)
5107 (newsgroups (message-fetch-field "newsgroups"))
5108 (followup-to (message-fetch-field "followup-to"))
5109 (groups (message-tokenize-header
5110 (if followup-to
5111 (concat newsgroups "," followup-to)
5112 newsgroups)))
23f87bed
MB
5113 (post-method (if (functionp message-post-method)
5114 (funcall message-post-method)
5115 message-post-method))
5116 ;; KLUDGE to handle nnvirtual groups. Doing this right
5117 ;; would probably involve a new nnoo function.
5118 ;; -- Per Abrahamsen <abraham@dina.kvl.dk>, 2001-10-17.
5119 (method (if (and (consp post-method)
5120 (eq (car post-method) 'nnvirtual)
5121 gnus-message-group-art)
5122 (let ((group (car (nnvirtual-find-group-art
5123 (car gnus-message-group-art)
5124 (cdr gnus-message-group-art)))))
5125 (gnus-find-method-for-group group))
5126 post-method))
5127 (known-groups
5128 (mapcar (lambda (n)
5129 (gnus-group-name-decode
5130 (gnus-group-real-name n)
5131 (gnus-group-name-charset method n)))
5132 (gnus-groups-from-server method)))
eec82323 5133 errors)
23f87bed
MB
5134 (while groups
5135 (when (and (not (equal (car groups) "poster"))
5136 (not (member (car groups) known-groups))
5137 (not (member (car groups) errors)))
5138 (push (car groups) errors))
5139 (pop groups))
5140 (cond
5141 ;; Gnus is not running.
5142 ((or (not (and (boundp 'gnus-active-hashtb)
5143 gnus-active-hashtb))
5144 (not (boundp 'gnus-read-active-file)))
5145 t)
5146 ;; We don't have all the group names.
5147 ((and (or (not gnus-read-active-file)
5148 (eq gnus-read-active-file 'some))
5149 errors)
5150 (y-or-n-p
5151 (format
5152 "Really use %s possibly unknown group%s: %s? "
5153 (if (= (length errors) 1) "this" "these")
5154 (if (= (length errors) 1) "" "s")
5155 (mapconcat 'identity errors ", "))))
5156 ;; There were no errors.
5157 ((not errors)
5158 t)
5159 ;; There are unknown groups.
5160 (t
5161 (y-or-n-p
5162 (format
5163 "Really post to %s unknown group%s: %s? "
5164 (if (= (length errors) 1) "this" "these")
5165 (if (= (length errors) 1) "" "s")
5166 (mapconcat 'identity errors ", ")))))))
5167 ;; Check continuation headers.
5168 (message-check 'continuation-headers
5169 (goto-char (point-min))
5170 (let ((do-posting t))
f362b760
MB
5171 (while (re-search-forward "^[^ \t\n][^ \t\n:]*[ \t\n]" nil t)
5172 (goto-char (match-beginning 0))
23f87bed 5173 (if (y-or-n-p "Fix continuation lines? ")
f362b760
MB
5174 (insert " ")
5175 (forward-line 1)
23f87bed
MB
5176 (unless (y-or-n-p "Send anyway? ")
5177 (setq do-posting nil))))
5178 do-posting))
eec82323
LMI
5179 ;; Check the Newsgroups & Followup-To headers for syntax errors.
5180 (message-check 'valid-newsgroups
5181 (let ((case-fold-search t)
5182 (headers '("Newsgroups" "Followup-To"))
5183 header error)
5184 (while (and headers (not error))
5185 (when (setq header (mail-fetch-field (car headers)))
5186 (if (or
5187 (not
5188 (string-match
5189 "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-+_&.a-zA-Z0-9]+\\)*\\'"
5190 header))
5191 (memq
5192 nil (mapcar
5193 (lambda (g)
5194 (not (string-match "\\.\\'\\|\\.\\." g)))
5195 (message-tokenize-header header ","))))
5196 (setq error t)))
5197 (unless error
5198 (pop headers)))
5199 (if (not error)
5200 t
5201 (y-or-n-p
5202 (format "The %s header looks odd: \"%s\". Really post? "
5203 (car headers) header)))))
a8151ef7
LMI
5204 (message-check 'repeated-newsgroups
5205 (let ((case-fold-search t)
5206 (headers '("Newsgroups" "Followup-To"))
5207 header error groups group)
5208 (while (and headers
5209 (not error))
5210 (when (setq header (mail-fetch-field (pop headers)))
5211 (setq groups (message-tokenize-header header ","))
5212 (while (setq group (pop groups))
5213 (when (member group groups)
5214 (setq error group
5215 groups nil)))))
5216 (if (not error)
5217 t
5218 (y-or-n-p
5219 (format "Group %s is repeated in headers. Really post? " error)))))
eec82323
LMI
5220 ;; Check the From header.
5221 (message-check 'from
5222 (let* ((case-fold-search t)
5223 (from (message-fetch-field "from"))
16409b0b 5224 ad)
eec82323
LMI
5225 (cond
5226 ((not from)
5227 (message "There is no From line. Posting is denied.")
5228 nil)
16409b0b
GM
5229 ((or (not (string-match
5230 "@[^\\.]*\\."
5231 (setq ad (nth 1 (mail-extract-address-components
5232 from))))) ;larsi@ifi
23f87bed 5233 (string-match "\\.\\." ad) ;larsi@ifi..uio
eec82323
LMI
5234 (string-match "@\\." ad) ;larsi@.ifi.uio
5235 (string-match "\\.$" ad) ;larsi@ifi.uio.
5236 (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
5237 (string-match "(.*).*(.*)" from)) ;(lars) (lars)
5238 (message
5239 "Denied posting -- the From looks strange: \"%s\"." from)
5240 nil)
23f87bed 5241 ((let ((addresses (rfc822-addresses from)))
8a908224
KY
5242 ;; `rfc822-addresses' returns a string if parsing fails.
5243 (while (and (consp addresses)
23f87bed
MB
5244 (not (eq (string-to-char (car addresses)) ?\()))
5245 (setq addresses (cdr addresses)))
5246 addresses)
5247 (message
5248 "Denied posting -- bad From address: \"%s\"." from)
5249 nil)
5250 (t t))))
5251 ;; Check the Reply-To header.
5252 (message-check 'reply-to
5253 (let* ((case-fold-search t)
5254 (reply-to (message-fetch-field "reply-to"))
5255 ad)
5256 (cond
5257 ((not reply-to)
5258 t)
5259 ((string-match "," reply-to)
5260 (y-or-n-p
5261 (format "Multiple Reply-To addresses: \"%s\". Really post? "
5262 reply-to)))
5263 ((or (not (string-match
5264 "@[^\\.]*\\."
5265 (setq ad (nth 1 (mail-extract-address-components
5266 reply-to))))) ;larsi@ifi
5267 (string-match "\\.\\." ad) ;larsi@ifi..uio
5268 (string-match "@\\." ad) ;larsi@.ifi.uio
5269 (string-match "\\.$" ad) ;larsi@ifi.uio.
5270 (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
5271 (string-match "(.*).*(.*)" reply-to)) ;(lars) (lars)
5272 (y-or-n-p
5273 (format
5274 "The Reply-To looks strange: \"%s\". Really post? "
5275 reply-to)))
eec82323
LMI
5276 (t t))))))
5277
5278(defun message-check-news-body-syntax ()
5279 (and
5280 ;; Check for long lines.
5281 (message-check 'long-lines
5282 (goto-char (point-min))
5283 (re-search-forward
5284 (concat "^" (regexp-quote mail-header-separator) "$"))
23f87bed 5285 (forward-line 1)
eec82323 5286 (while (and
23f87bed
MB
5287 (or (looking-at
5288 "<#\\(/\\)?\\(multipart\\|part\\|external\\|mml\\)")
5289 (let ((p (point)))
5290 (end-of-line)
5291 (< (- (point) p) 80)))
eec82323
LMI
5292 (zerop (forward-line 1))))
5293 (or (bolp)
5294 (eobp)
5295 (y-or-n-p
5296 "You have lines longer than 79 characters. Really post? ")))
5297 ;; Check whether the article is empty.
5298 (message-check 'empty
5299 (goto-char (point-min))
5300 (re-search-forward
5301 (concat "^" (regexp-quote mail-header-separator) "$"))
5302 (forward-line 1)
5303 (let ((b (point)))
5304 (goto-char (point-max))
5305 (re-search-backward message-signature-separator nil t)
5306 (beginning-of-line)
5307 (or (re-search-backward "[^ \n\t]" b t)
23f87bed
MB
5308 (if (message-gnksa-enable-p 'empty-article)
5309 (y-or-n-p "Empty article. Really post? ")
5310 (message "Denied posting -- Empty article.")
5311 nil))))
eec82323
LMI
5312 ;; Check for control characters.
5313 (message-check 'control-chars
7c3bb5a5 5314 (if (re-search-forward
c70dbcd5 5315 (mm-string-to-multibyte "[\000-\007\013\015-\032\034-\037\200-\237]")
7c3bb5a5 5316 nil t)
eec82323
LMI
5317 (y-or-n-p
5318 "The article contains control characters. Really post? ")
5319 t))
5320 ;; Check excessive size.
5321 (message-check 'size
5322 (if (> (buffer-size) 60000)
5323 (y-or-n-p
5324 (format "The article is %d octets long. Really post? "
5325 (buffer-size)))
5326 t))
5327 ;; Check whether any new text has been added.
5328 (message-check 'new-text
5329 (or
5330 (not message-checksum)
6748645f 5331 (not (eq (message-checksum) message-checksum))
23f87bed
MB
5332 (if (message-gnksa-enable-p 'quoted-text-only)
5333 (y-or-n-p
5334 "It looks like no new text has been added. Really post? ")
5335 (message "Denied posting -- no new text has been added.")
5336 nil)))
eec82323
LMI
5337 ;; Check the length of the signature.
5338 (message-check 'signature
e3e955fe
MB
5339 (let (sig-start sig-end)
5340 (goto-char (point-max))
5341 (if (not (re-search-backward message-signature-separator nil t))
5342 t
5343 (setq sig-start (1+ (point-at-eol)))
5344 (setq sig-end
5345 (if (re-search-forward
5346 "<#/?\\(multipart\\|part\\|external\\|mml\\)" nil t)
5347 (- (point-at-bol) 1)
5348 (point-max)))
5349 (if (>= (count-lines sig-start sig-end) 5)
5350 (if (message-gnksa-enable-p 'signature)
5351 (y-or-n-p
5352 (format "Signature is excessively long (%d lines). Really post? "
5353 (count-lines sig-start sig-end)))
5354 (message "Denied posting -- Excessive signature.")
5355 nil)
5356 t))))
16409b0b
GM
5357 ;; Ensure that text follows last quoted portion.
5358 (message-check 'quoting-style
5359 (goto-char (point-max))
5360 (let ((no-problem t))
23f87bed
MB
5361 (when (search-backward-regexp "^>[^\n]*\n" nil t)
5362 (setq no-problem (search-forward-regexp "^[ \t]*[^>\n]" nil t)))
16409b0b
GM
5363 (if no-problem
5364 t
23f87bed
MB
5365 (if (message-gnksa-enable-p 'quoted-text-only)
5366 (y-or-n-p "Your text should follow quoted text. Really post? ")
5367 ;; Ensure that
5368 (goto-char (point-min))
5369 (re-search-forward
5370 (concat "^" (regexp-quote mail-header-separator) "$"))
5371 (if (search-forward-regexp "^[ \t]*[^>\n]" nil t)
5372 (y-or-n-p "Your text should follow quoted text. Really post? ")
5373 (message "Denied posting -- only quoted text.")
5374 nil)))))))
eec82323
LMI
5375
5376(defun message-checksum ()
5377 "Return a \"checksum\" for the current buffer."
5378 (let ((sum 0))
5379 (save-excursion
5380 (goto-char (point-min))
5381 (re-search-forward
5382 (concat "^" (regexp-quote mail-header-separator) "$"))
5383 (while (not (eobp))
5384 (when (not (looking-at "[ \t\n]"))
23f87bed
MB
5385 (setq sum (logxor (ash sum 1) (if (natnump sum) 0 1)
5386 (char-after))))
eec82323
LMI
5387 (forward-char 1)))
5388 sum))
5389
5390(defun message-do-fcc ()
5391 "Process Fcc headers in the current buffer."
5392 (let ((case-fold-search t)
5393 (buf (current-buffer))
23f87bed
MB
5394 list file
5395 (mml-externalize-attachments message-fcc-externalize-attachments))
eec82323 5396 (save-excursion
eec82323
LMI
5397 (save-restriction
5398 (message-narrow-to-headers)
23f87bed
MB
5399 (setq file (message-fetch-field "fcc" t)))
5400 (when file
5401 (set-buffer (get-buffer-create " *message temp*"))
5402 (erase-buffer)
5403 (insert-buffer-substring buf)
5404 (message-encode-message-body)
5405 (save-restriction
5406 (message-narrow-to-headers)
5407 (while (setq file (message-fetch-field "fcc" t))
5408 (push file list)
5409 (message-remove-header "fcc" nil t))
5410 (let ((mail-parse-charset message-default-charset)
5411 (rfc2047-header-encoding-alist
5412 (cons '("Newsgroups" . default)
5413 rfc2047-header-encoding-alist)))
5414 (mail-encode-encoded-word-buffer)))
5415 (goto-char (point-min))
5416 (when (re-search-forward
5417 (concat "^" (regexp-quote mail-header-separator) "$")
5418 nil t)
5419 (replace-match "" t t ))
5420 ;; Process FCC operations.
5421 (while list
5422 (setq file (pop list))
5423 (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
5424 ;; Pipe the article to the program in question.
5425 (call-process-region (point-min) (point-max) shell-file-name
5426 nil nil nil shell-command-switch
5427 (match-string 1 file))
5428 ;; Save the article.
5429 (setq file (expand-file-name file))
5430 (unless (file-exists-p (file-name-directory file))
5431 (make-directory (file-name-directory file) t))
5432 (if (and message-fcc-handler-function
5433 (not (eq message-fcc-handler-function 'rmail-output)))
5434 (funcall message-fcc-handler-function file)
95ccabb5
GM
5435 ;; FIXME this option, rmail-output (also used if
5436 ;; message-fcc-handler-function is nil) is not
5437 ;; documented anywhere AFAICS. It should work in Emacs
5438 ;; 23; I suspect it does not work in Emacs 22.
5439 ;; FIXME I don't see the need for the two different cases here.
5440 ;; mail-use-rfc822 makes no difference (in Emacs 23),and
5441 ;; the third argument just controls \"Wrote file\" message.
23f87bed
MB
5442 (if (and (file-readable-p file) (mail-file-babyl-p file))
5443 (rmail-output file 1 nil t)
5444 (let ((mail-use-rfc822 t))
5445 (rmail-output file 1 t t))))))
5446 (kill-buffer (current-buffer))))))
eec82323
LMI
5447
5448(defun message-output (filename)
7d829636 5449 "Append this article to Unix/babyl mail file FILENAME."
872b1b16
GM
5450 (if (or (and (file-readable-p filename)
5451 (mail-file-babyl-p filename))
5452 ;; gnus-output-to-mail does the wrong thing with live, mbox
5453 ;; Rmail buffers in Emacs 23.
5454 ;; http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=597255
5455 (let ((buff (find-buffer-visiting filename)))
5456 (and buff (with-current-buffer buff
5457 (eq major-mode 'rmail-mode)))))
eec82323
LMI
5458 (gnus-output-to-rmail filename t)
5459 (gnus-output-to-mail filename t)))
5460
5461(defun message-cleanup-headers ()
5462 "Do various automatic cleanups of the headers."
5463 ;; Remove empty lines in the header.
5464 (save-restriction
5465 (message-narrow-to-headers)
6748645f 5466 ;; Remove blank lines.
eec82323 5467 (while (re-search-forward "^[ \t]*\n" nil t)
6748645f 5468 (replace-match "" t t))
eec82323 5469
6748645f
LMI
5470 ;; Correct Newsgroups and Followup-To headers: Change sequence of
5471 ;; spaces to comma and eliminate spaces around commas. Eliminate
5472 ;; embedded line breaks.
5473 (goto-char (point-min))
5474 (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
5475 (save-restriction
5476 (narrow-to-region
5477 (point)
5478 (if (re-search-forward "^[^ \t]" nil t)
5479 (match-beginning 0)
5480 (forward-line 1)
5481 (point)))
5482 (goto-char (point-min))
5483 (while (re-search-forward "\n[ \t]+" nil t)
23f87bed 5484 (replace-match " " t t)) ;No line breaks (too confusing)
6748645f
LMI
5485 (goto-char (point-min))
5486 (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
5487 (replace-match "," t t))
5488 (goto-char (point-min))
5489 ;; Remove trailing commas.
5490 (when (re-search-forward ",+$" nil t)
5491 (replace-match "" t t))))))
eec82323 5492
16409b0b
GM
5493(defun message-make-date (&optional now)
5494 "Make a valid data header.
5495If NOW, use that time instead."
01c52d31
MB
5496 (let ((system-time-locale "C"))
5497 (format-time-string "%a, %d %b %Y %T %z" now)))
5498
5499(defun message-insert-expires (days)
5500 "Insert the Expires header. Expiry in DAYS days."
5501 (interactive "NExpire article in how many days? ")
5502 (save-excursion
5503 (message-position-on-field "Expires" "X-Draft-From")
5504 (insert (message-make-expires-date days))))
5505
5506(defun message-make-expires-date (days)
5507 "Make date string for the Expires header. Expiry in DAYS days.
5508
5509In posting styles use `(\"Expires\" (make-expires-date 30))'."
5510 (let* ((cur (decode-time (current-time)))
5511 (nday (+ days (nth 3 cur))))
5512 (setf (nth 3 cur) nday)
5513 (message-make-date (apply 'encode-time cur))))
eec82323
LMI
5514
5515(defun message-make-message-id ()
5516 "Make a unique Message-ID."
5517 (concat "<" (message-unique-id)
a8151ef7
LMI
5518 (let ((psubject (save-excursion (message-fetch-field "subject")))
5519 (psupersedes
5520 (save-excursion (message-fetch-field "supersedes"))))
5521 (if (or
5522 (and message-reply-headers
5523 (mail-header-references message-reply-headers)
5524 (mail-header-subject message-reply-headers)
5525 psubject
a8151ef7
LMI
5526 (not (string=
5527 (message-strip-subject-re
5528 (mail-header-subject message-reply-headers))
5529 (message-strip-subject-re psubject))))
5530 (and psupersedes
5531 (string-match "_-_@" psupersedes)))
eec82323
LMI
5532 "_-_" ""))
5533 "@" (message-make-fqdn) ">"))
5534
5535(defvar message-unique-id-char nil)
5536
5537;; If you ever change this function, make sure the new version
5538;; cannot generate IDs that the old version could.
5539;; You might for example insert a "." somewhere (not next to another dot
5540;; or string boundary), or modify the "fsf" string.
5541(defun message-unique-id ()
5542 ;; Don't use microseconds from (current-time), they may be unsupported.
5543 ;; Instead we use this randomly inited counter.
5544 (setq message-unique-id-char
ae97e645
G
5545 (% (1+ (or message-unique-id-char
5546 (logand (random most-positive-fixnum) (1- (lsh 1 20)))))
eec82323
LMI
5547 ;; (current-time) returns 16-bit ints,
5548 ;; and 2^16*25 just fits into 4 digits i base 36.
5549 (* 25 25)))
5550 (let ((tm (current-time)))
5551 (concat
f5ec697d 5552 (if (or (eq system-type 'ms-dos)
531bedc3
MB
5553 ;; message-number-base36 doesn't handle bigints.
5554 (floatp (user-uid)))
eec82323
LMI
5555 (let ((user (downcase (user-login-name))))
5556 (while (string-match "[^a-z0-9_]" user)
5557 (aset user (match-beginning 0) ?_))
5558 user)
5559 (message-number-base36 (user-uid) -1))
23f87bed 5560 (message-number-base36 (+ (car tm)
eec82323
LMI
5561 (lsh (% message-unique-id-char 25) 16)) 4)
5562 (message-number-base36 (+ (nth 1 tm)
5563 (lsh (/ message-unique-id-char 25) 16)) 4)
23f87bed
MB
5564 ;; Append a given name, because while the generated ID is unique
5565 ;; to this newsreader, other newsreaders might otherwise generate
5566 ;; the same ID via another algorithm.
eec82323
LMI
5567 ".fsf")))
5568
5569(defun message-number-base36 (num len)
5570 (if (if (< len 0)
5571 (<= num 0)
5572 (= len 0))
5573 ""
5574 (concat (message-number-base36 (/ num 36) (1- len))
5575 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
5576 (% num 36))))))
5577
5578(defun message-make-organization ()
5579 "Make an Organization header."
5580 (let* ((organization
6748645f 5581 (when message-user-organization
23f87bed 5582 (if (functionp message-user-organization)
16409b0b
GM
5583 (funcall message-user-organization)
5584 message-user-organization))))
23f87bed
MB
5585 (with-temp-buffer
5586 (mm-enable-multibyte)
eec82323
LMI
5587 (cond ((stringp organization)
5588 (insert organization))
5589 ((and (eq t organization)
5590 message-user-organization-file
5591 (file-exists-p message-user-organization-file))
5592 (insert-file-contents message-user-organization-file)))
5593 (goto-char (point-min))
5594 (while (re-search-forward "[\t\n]+" nil t)
5595 (replace-match "" t t))
5596 (unless (zerop (buffer-size))
5597 (buffer-string)))))
5598
5599(defun message-make-lines ()
5600 "Count the number of lines and return numeric string."
5601 (save-excursion
5602 (save-restriction
5603 (widen)
23f87bed 5604 (message-goto-body)
eec82323
LMI
5605 (int-to-string (count-lines (point) (point-max))))))
5606
23f87bed
MB
5607(defun message-make-references ()
5608 "Return the References header for this message."
5609 (when message-reply-headers
aa8f8277 5610 (let ((message-id (mail-header-id message-reply-headers))
82816ba1 5611 (references (mail-header-references message-reply-headers)))
23f87bed
MB
5612 (if (or references message-id)
5613 (concat (or references "") (and references " ")
5614 (or message-id ""))
5615 nil))))
5616
eec82323
LMI
5617(defun message-make-in-reply-to ()
5618 "Return the In-Reply-To header for this message."
5619 (when message-reply-headers
23f87bed
MB
5620 (let ((from (mail-header-from message-reply-headers))
5621 (date (mail-header-date message-reply-headers))
aa8f8277 5622 (msg-id (mail-header-id message-reply-headers)))
23f87bed
MB
5623 (when from
5624 (let ((name (mail-extract-address-components from)))
45cb30ee
MB
5625 (concat
5626 msg-id (if msg-id " (")
5627 (if (car name)
5628 (if (string-match "[^\000-\177]" (car name))
5629 ;; Quote a string containing non-ASCII characters.
5630 ;; It will make the RFC2047 encoder cause an error
5631 ;; if there are special characters.
fa98585c
SM
5632 (mm-with-multibyte-buffer
5633 (insert (car name))
5634 (goto-char (point-min))
5635 (while (search-forward "\"" nil t)
5636 (when (prog2
5637 (backward-char)
5638 (zerop (% (skip-chars-backward "\\\\") 2))
5639 (goto-char (match-beginning 0)))
5640 (insert "\\"))
5641 (forward-char))
5642 ;; Those quotes will be removed by the RFC2047 encoder.
5643 (concat "\"" (buffer-string) "\""))
45cb30ee
MB
5644 (car name))
5645 (nth 1 name))
5646 "'s message of \""
5647 (if (or (not date) (string= date ""))
5648 "(unknown date)" date)
5649 "\"" (if msg-id ")")))))))
eec82323
LMI
5650
5651(defun message-make-distribution ()
5652 "Make a Distribution header."
5653 (let ((orig-distribution (message-fetch-reply-field "distribution")))
23f87bed 5654 (cond ((functionp message-distribution-function)
eec82323
LMI
5655 (funcall message-distribution-function))
5656 (t orig-distribution))))
5657
5658(defun message-make-expires ()
5659 "Return an Expires header based on `message-expires'."
5660 (let ((current (current-time))
5661 (future (* 1.0 message-expires 60 60 24)))
5662 ;; Add the future to current.
5663 (setcar current (+ (car current) (round (/ future (expt 2 16)))))
5664 (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
16409b0b 5665 (message-make-date current)))
eec82323
LMI
5666
5667(defun message-make-path ()
5668 "Return uucp path."
5669 (let ((login-name (user-login-name)))
5670 (cond ((null message-user-path)
5671 (concat (system-name) "!" login-name))
5672 ((stringp message-user-path)
5673 ;; Support GENERICPATH. Suggested by vixie@decwrl.dec.com.
5674 (concat message-user-path "!" login-name))
5675 (t login-name))))
5676
60ece9b0 5677(defun message-make-from (&optional name address)
eec82323
LMI
5678 "Make a From header."
5679 (let* ((style message-from-style)
01c52d31
MB
5680 (login (or address (message-make-address)))
5681 (fullname (or name
5682 (and (boundp 'user-full-name)
5683 user-full-name)
5684 (user-full-name))))
eec82323
LMI
5685 (when (string= fullname "&")
5686 (setq fullname (user-login-name)))
23f87bed
MB
5687 (with-temp-buffer
5688 (mm-enable-multibyte)
eec82323
LMI
5689 (cond
5690 ((or (null style)
5691 (equal fullname ""))
5692 (insert login))
5693 ((or (eq style 'angles)
5694 (and (not (eq style 'parens))
5695 ;; Use angles if no quoting is needed, or if parens would
5696 ;; need quoting too.
5697 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
5698 (let ((tmp (concat fullname nil)))
5699 (while (string-match "([^()]*)" tmp)
5700 (aset tmp (match-beginning 0) ?-)
5701 (aset tmp (1- (match-end 0)) ?-))
5702 (string-match "[\\()]" tmp)))))
5703 (insert fullname)
5704 (goto-char (point-min))
01c52d31
MB
5705 ;; Look for a character that cannot appear unquoted
5706 ;; according to RFC 822.
5707 (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
5708 ;; Quote fullname, escaping specials.
5709 (goto-char (point-min))
5710 (insert "\"")
5711 (while (re-search-forward "[\"\\]" nil 1)
5712 (replace-match "\\\\\\&" t))
5713 (insert "\""))
eec82323
LMI
5714 (insert " <" login ">"))
5715 (t ; 'parens or default
5716 (insert login " (")
5717 (let ((fullname-start (point)))
5718 (insert fullname)
5719 (goto-char fullname-start)
5720 ;; RFC 822 says \ and nonmatching parentheses
5721 ;; must be escaped in comments.
5722 ;; Escape every instance of ()\ ...
5723 (while (re-search-forward "[()\\]" nil 1)
5724 (replace-match "\\\\\\&" t))
5725 ;; ... then undo escaping of matching parentheses,
5726 ;; including matching nested parentheses.
5727 (goto-char fullname-start)
5728 (while (re-search-forward
5729 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
5730 nil 1)
5731 (replace-match "\\1(\\3)" t)
5732 (goto-char fullname-start)))
5733 (insert ")")))
5734 (buffer-string))))
5735
5736(defun message-make-sender ()
5737 "Return the \"real\" user address.
5738This function tries to ignore all user modifications, and
5739give as trustworthy answer as possible."
5740 (concat (user-login-name) "@" (system-name)))
5741
5742(defun message-make-address ()
5743 "Make the address of the user."
5744 (or (message-user-mail-address)
5745 (concat (user-login-name) "@" (message-make-domain))))
5746
5747(defun message-user-mail-address ()
5748 "Return the pertinent part of `user-mail-address'."
23f87bed
MB
5749 (when (and user-mail-address
5750 (string-match "@.*\\." user-mail-address))
eec82323
LMI
5751 (if (string-match " " user-mail-address)
5752 (nth 1 (mail-extract-address-components user-mail-address))
5753 user-mail-address)))
5754
23f87bed
MB
5755(defun message-sendmail-envelope-from ()
5756 "Return the envelope from."
5757 (cond ((eq message-sendmail-envelope-from 'header)
5758 (nth 1 (mail-extract-address-components
5759 (message-fetch-field "from"))))
5760 ((stringp message-sendmail-envelope-from)
5761 message-sendmail-envelope-from)
5762 (t
5763 (message-make-address))))
5764
eec82323
LMI
5765(defun message-make-fqdn ()
5766 "Return user's fully qualified domain name."
23f87bed
MB
5767 (let* ((system-name (system-name))
5768 (user-mail (message-user-mail-address))
5769 (user-domain
5770 (if (and user-mail
5771 (string-match "@\\(.*\\)\\'" user-mail))
5772 (match-string 1 user-mail)))
5773 (case-fold-search t))
eec82323 5774 (cond
23f87bed
MB
5775 ((and message-user-fqdn
5776 (stringp message-user-fqdn)
5777 (string-match message-valid-fqdn-regexp message-user-fqdn)
5778 (not (string-match message-bogus-system-names message-user-fqdn)))
11e95b02 5779 ;; `message-user-fqdn' seems to be valid
23f87bed 5780 message-user-fqdn)
23f87bed
MB
5781 ((and (string-match message-valid-fqdn-regexp system-name)
5782 (not (string-match message-bogus-system-names system-name)))
eec82323
LMI
5783 ;; `system-name' returned the right result.
5784 system-name)
5785 ;; Try `mail-host-address'.
5786 ((and (boundp 'mail-host-address)
5787 (stringp mail-host-address)
23f87bed
MB
5788 (string-match message-valid-fqdn-regexp mail-host-address)
5789 (not (string-match message-bogus-system-names mail-host-address)))
eec82323
LMI
5790 mail-host-address)
5791 ;; We try `user-mail-address' as a backup.
23f87bed
MB
5792 ((and user-domain
5793 (stringp user-domain)
5794 (string-match message-valid-fqdn-regexp user-domain)
5795 (not (string-match message-bogus-system-names user-domain)))
5796 user-domain)
eec82323
LMI
5797 ;; Default to this bogus thing.
5798 (t
23f87bed
MB
5799 (concat system-name
5800 ".i-did-not-set--mail-host-address--so-tickle-me")))))
eec82323 5801
eec82323
LMI
5802(defun message-make-domain ()
5803 "Return the domain name."
5804 (or mail-host-address
5805 (message-make-fqdn)))
5806
23f87bed
MB
5807(defun message-to-list-only ()
5808 "Send a message to the list only.
5809Remove all addresses but the list address from To and Cc headers."
5810 (interactive)
5811 (let ((listaddr (message-make-mail-followup-to t)))
5812 (when listaddr
5813 (save-excursion
5814 (message-remove-header "to")
5815 (message-remove-header "cc")
5816 (message-position-on-field "To" "X-Draft-From")
5817 (insert listaddr)))))
5818
5819(defun message-make-mail-followup-to (&optional only-show-subscribed)
5820 "Return the Mail-Followup-To header.
5821If passed the optional argument ONLY-SHOW-SUBSCRIBED only return the
5822subscribed address (and not the additional To and Cc header contents)."
5823 (let* ((case-fold-search t)
5824 (to (message-fetch-field "To"))
5825 (cc (message-fetch-field "cc"))
5826 (msg-recipients (concat to (and to cc ", ") cc))
5827 (recipients
5828 (mapcar 'mail-strip-quoted-names
5829 (message-tokenize-header msg-recipients)))
5830 (file-regexps
5831 (if message-subscribed-address-file
5832 (let (begin end item re)
5833 (save-excursion
5834 (with-temp-buffer
5835 (insert-file-contents message-subscribed-address-file)
5836 (while (not (eobp))
5837 (setq begin (point))
5838 (forward-line 1)
5839 (setq end (point))
5840 (if (bolp) (setq end (1- end)))
5841 (setq item (regexp-quote (buffer-substring begin end)))
5842 (if re (setq re (concat re "\\|" item))
5843 (setq re (concat "\\`\\(" item))))
5844 (and re (list (concat re "\\)\\'"))))))))
5845 (mft-regexps (apply 'append message-subscribed-regexps
5846 (mapcar 'regexp-quote
5847 message-subscribed-addresses)
5848 file-regexps
5849 (mapcar 'funcall
5850 message-subscribed-address-functions))))
5851 (save-match-data
82816ba1 5852 (let ((list
23f87bed
MB
5853 (loop for recipient in recipients
5854 when (loop for regexp in mft-regexps
5855 when (string-match regexp recipient) return t)
5856 return recipient)))
5857 (when list
5858 (if only-show-subscribed
5859 list
5860 msg-recipients))))))
5861
5862(defun message-idna-to-ascii-rhs-1 (header)
5863 "Interactively potentially IDNA encode domain names in HEADER."
5864 (let ((field (message-fetch-field header))
99c2a4e3 5865 ace)
23f87bed 5866 (when field
53cfefc8 5867 (dolist (rhs
8753ddee 5868 (mm-delete-duplicates
53cfefc8 5869 (mapcar (lambda (rhs) (or (cadr (split-string rhs "@")) ""))
1bd6cd6c 5870 (mapcar 'downcase
53cfefc8 5871 (mapcar
71e691a5
G
5872 (lambda (elem)
5873 (or (cadr elem)
5874 ""))
4520e527 5875 (mail-extract-address-components field t))))))
1bd6cd6c
KY
5876 ;; Note that `rhs' will be "" if the address does not have
5877 ;; the domain part, i.e., if it is a local user's address.
5878 (setq ace (if (string-match "\\`[[:ascii:]]*\\'" rhs)
b890d447
MB
5879 rhs
5880 (downcase (idna-to-ascii rhs))))
23f87bed
MB
5881 (when (and (not (equal rhs ace))
5882 (or (not (eq message-use-idna 'ask))
53cfefc8
MB
5883 (y-or-n-p (format "Replace %s with %s in %s:? "
5884 rhs ace header))))
23f87bed
MB
5885 (goto-char (point-min))
5886 (while (re-search-forward (concat "^" header ":") nil t)
5887 (message-narrow-to-field)
5888 (while (search-forward (concat "@" rhs) nil t)
5889 (replace-match (concat "@" ace) t t))
5890 (goto-char (point-max))
5891 (widen)))))))
5892
5893(defun message-idna-to-ascii-rhs ()
5894 "Possibly IDNA encode non-ASCII domain names in From:, To: and Cc: headers.
5895See `message-idna-encode'."
5896 (interactive)
5897 (when message-use-idna
5898 (save-excursion
5899 (save-restriction
1bd6cd6c
KY
5900 ;; `message-narrow-to-head' that recognizes only the first empty
5901 ;; line as the message header separator used to be used here.
5902 ;; However, since there is the "--text follows this line--" line
5903 ;; normally, it failed in narrowing to the headers and potentially
5904 ;; caused the IDNA encoding on lines that look like headers in
5905 ;; the message body.
f66de92f 5906 (message-narrow-to-headers-or-head)
23f87bed
MB
5907 (message-idna-to-ascii-rhs-1 "From")
5908 (message-idna-to-ascii-rhs-1 "To")
53cfefc8
MB
5909 (message-idna-to-ascii-rhs-1 "Reply-To")
5910 (message-idna-to-ascii-rhs-1 "Mail-Reply-To")
5911 (message-idna-to-ascii-rhs-1 "Mail-Followup-To")
23f87bed
MB
5912 (message-idna-to-ascii-rhs-1 "Cc")))))
5913
99c2a4e3
SM
5914(defvar Date)
5915(defvar Message-ID)
5916(defvar Organization)
5917(defvar From)
5918(defvar Path)
5919(defvar Subject)
5920(defvar Newsgroups)
5921(defvar In-Reply-To)
5922(defvar References)
5923(defvar To)
5924(defvar Distribution)
5925(defvar Lines)
5926(defvar User-Agent)
5927(defvar Expires)
5928
eec82323
LMI
5929(defun message-generate-headers (headers)
5930 "Prepare article HEADERS.
5931Headers already prepared in the buffer are not modified."
23f87bed 5932 (setq headers (append headers message-required-headers))
eec82323
LMI
5933 (save-restriction
5934 (message-narrow-to-headers)
5935 (let* ((Date (message-make-date))
5936 (Message-ID (message-make-message-id))
5937 (Organization (message-make-organization))
5938 (From (message-make-from))
5939 (Path (message-make-path))
5940 (Subject nil)
5941 (Newsgroups nil)
5942 (In-Reply-To (message-make-in-reply-to))
23f87bed 5943 (References (message-make-references))
eec82323
LMI
5944 (To nil)
5945 (Distribution (message-make-distribution))
5946 (Lines (message-make-lines))
16409b0b 5947 (User-Agent message-newsreader)
eec82323
LMI
5948 (Expires (message-make-expires))
5949 (case-fold-search t)
23f87bed
MB
5950 (optionalp nil)
5951 header value elem header-string)
eec82323
LMI
5952 ;; First we remove any old generated headers.
5953 (let ((headers message-deletable-headers))
a8151ef7
LMI
5954 (unless (buffer-modified-p)
5955 (setq headers (delq 'Message-ID (copy-sequence headers))))
eec82323
LMI
5956 (while headers
5957 (goto-char (point-min))
5958 (and (re-search-forward
5959 (concat "^" (symbol-name (car headers)) ": *") nil t)
5960 (get-text-property (1+ (match-beginning 0)) 'message-deletable)
5961 (message-delete-line))
5962 (pop headers)))
5963 ;; Go through all the required headers and see if they are in the
5964 ;; articles already. If they are not, or are empty, they are
5965 ;; inserted automatically - except for Subject, Newsgroups and
5966 ;; Distribution.
5967 (while headers
5968 (goto-char (point-min))
5969 (setq elem (pop headers))
5970 (if (consp elem)
5971 (if (eq (car elem) 'optional)
23f87bed
MB
5972 (setq header (cdr elem)
5973 optionalp t)
eec82323
LMI
5974 (setq header (car elem)))
5975 (setq header elem))
23f87bed
MB
5976 (setq header-string (if (stringp header)
5977 header
5978 (symbol-name header)))
eec82323 5979 (when (or (not (re-search-forward
6748645f 5980 (concat "^"
23f87bed 5981 (regexp-quote (downcase header-string))
6748645f 5982 ":")
eec82323
LMI
5983 nil t))
5984 (progn
5985 ;; The header was found. We insert a space after the
5986 ;; colon, if there is none.
16409b0b 5987 (if (/= (char-after) ? ) (insert " ") (forward-char 1))
23f87bed 5988 ;; Find out whether the header is empty.
16409b0b 5989 (looking-at "[ \t]*\n[^ \t]")))
eec82323
LMI
5990 ;; So we find out what value we should insert.
5991 (setq value
5992 (cond
23f87bed
MB
5993 ((and (consp elem)
5994 (eq (car elem) 'optional)
5995 (not (member header-string message-inserted-headers)))
eec82323
LMI
5996 ;; This is an optional header. If the cdr of this
5997 ;; is something that is nil, then we do not insert
5998 ;; this header.
5999 (setq header (cdr elem))
23f87bed
MB
6000 (or (and (functionp (cdr elem))
6001 (funcall (cdr elem)))
6002 (and (boundp (cdr elem))
6003 (symbol-value (cdr elem)))))
eec82323
LMI
6004 ((consp elem)
6005 ;; The element is a cons. Either the cdr is a
6006 ;; string to be inserted verbatim, or it is a
6007 ;; function, and we insert the value returned from
6008 ;; this function.
23f87bed
MB
6009 (or (and (stringp (cdr elem))
6010 (cdr elem))
6011 (and (functionp (cdr elem))
6012 (funcall (cdr elem)))))
6013 ((and (boundp header)
6014 (symbol-value header))
eec82323
LMI
6015 ;; The element is a symbol. We insert the value
6016 ;; of this symbol, if any.
6017 (symbol-value header))
e8beac8a
MB
6018 ((not (message-check-element
6019 (intern (downcase (symbol-name header)))))
eec82323
LMI
6020 ;; We couldn't generate a value for this header,
6021 ;; so we just ask the user.
6022 (read-from-minibuffer
6023 (format "Empty header for %s; enter value: " header)))))
6024 ;; Finally insert the header.
6025 (when (and value
6026 (not (equal value "")))
6027 (save-excursion
6028 (if (bolp)
6029 (progn
6030 ;; This header didn't exist, so we insert it.
6031 (goto-char (point-max))
23f87bed
MB
6032 (let ((formatter
6033 (cdr (assq header message-header-format-alist))))
6034 (if formatter
6035 (funcall formatter header value)
6036 (insert header-string ": " value))
731533fd 6037 (push header-string message-inserted-headers)
01c52d31 6038 (goto-char (message-fill-field))
23f87bed 6039 ;; We check whether the value was ended by a
01c52d31 6040 ;; newline. If not, we insert one.
23f87bed 6041 (unless (bolp)
f129a4df 6042 (insert "\n"))
23f87bed 6043 (forward-line -1)))
eec82323
LMI
6044 ;; The value of this header was empty, so we clear
6045 ;; totally and insert the new value.
01c52d31 6046 (delete-region (point) (point-at-eol))
23f87bed 6047 ;; If the header is optional, and the header was
01c52d31 6048 ;; empty, we can't insert it anyway.
23f87bed
MB
6049 (unless optionalp
6050 (push header-string message-inserted-headers)
01c52d31
MB
6051 (insert value)
6052 (message-fill-field)))
eec82323
LMI
6053 ;; Add the deletable property to the headers that require it.
6054 (and (memq header message-deletable-headers)
6055 (progn (beginning-of-line) (looking-at "[^:]+: "))
6056 (add-text-properties
6057 (point) (match-end 0)
6058 '(message-deletable t face italic) (current-buffer)))))))
6059 ;; Insert new Sender if the From is strange.
6060 (let ((from (message-fetch-field "from"))
6061 (sender (message-fetch-field "sender"))
6062 (secure-sender (message-make-sender)))
6063 (when (and from
6064 (not (message-check-element 'sender))
6065 (not (string=
6066 (downcase
6067 (cadr (mail-extract-address-components from)))
6068 (downcase secure-sender)))
6069 (or (null sender)
6070 (not
6071 (string=
6072 (downcase
6073 (cadr (mail-extract-address-components sender)))
6074 (downcase secure-sender)))))
6075 (goto-char (point-min))
6076 ;; Rename any old Sender headers to Original-Sender.
6077 (when (re-search-forward "^\\(Original-\\)*Sender:" nil t)
6078 (beginning-of-line)
6079 (insert "Original-")
6080 (beginning-of-line))
6081 (when (or (message-news-p)
6748645f 6082 (string-match "@.+\\.." secure-sender))
f129a4df 6083 (insert "Sender: " secure-sender "\n"))))
23f87bed
MB
6084 ;; Check for IDNA
6085 (message-idna-to-ascii-rhs))))
eec82323 6086
181cb5fb 6087(defun message-insert-courtesy-copy (message)
eec82323
LMI
6088 "Insert a courtesy message in mail copies of combined messages."
6089 (let (newsgroups)
6090 (save-excursion
6091 (save-restriction
6092 (message-narrow-to-headers)
6093 (when (setq newsgroups (message-fetch-field "newsgroups"))
6094 (goto-char (point-max))
f129a4df 6095 (insert "Posted-To: " newsgroups "\n")))
eec82323 6096 (forward-line 1)
181cb5fb 6097 (when message
eec82323 6098 (cond
181cb5fb
G
6099 ((string-match "%s" message)
6100 (insert (format message newsgroups)))
eec82323 6101 (t
181cb5fb 6102 (insert message)))))))
eec82323
LMI
6103
6104;;;
6105;;; Setting up a message buffer
6106;;;
6107
01c52d31
MB
6108(defun message-skip-to-next-address ()
6109 (let ((end (save-excursion
6110 (message-next-header)
6111 (point)))
6112 quoted char)
6113 (when (looking-at ",")
6114 (forward-char 1))
6115 (while (and (not (= (point) end))
6116 (or (not (eq char ?,))
6117 quoted))
0000d0d5 6118 (skip-chars-forward "^,\"" end)
01c52d31
MB
6119 (when (eq (setq char (following-char)) ?\")
6120 (setq quoted (not quoted)))
6121 (unless (= (point) end)
6122 (forward-char 1)))
6123 (skip-chars-forward " \t\n")))
6124
23f87bed
MB
6125(defun message-split-line ()
6126 "Split current line, moving portion beyond point vertically down.
6127If the current line has `message-yank-prefix', insert it on the new line."
6128 (interactive "*")
6129 (condition-case nil
bf247b6e 6130 (split-line message-yank-prefix) ;; Emacs 22.1+ supports arg.
23f87bed
MB
6131 (error
6132 (split-line))))
bf247b6e 6133
01c52d31
MB
6134(defun message-insert-header (header value)
6135 (insert (capitalize (symbol-name header))
6136 ": "
6137 (if (consp value) (car value) value)))
6138
6139(defun message-field-name ()
6140 (save-excursion
6141 (goto-char (point-min))
6142 (when (looking-at "\\([^:]+\\):")
6143 (intern (capitalize (match-string 1))))))
6144
6145(defun message-fill-field ()
6146 (save-excursion
6147 (save-restriction
6148 (message-narrow-to-field)
6149 (let ((field-name (message-field-name)))
6150 (funcall (or (cadr (assq field-name message-field-fillers))
6151 'message-fill-field-general)))
6152 (point-max))))
6153
6154(defun message-fill-field-address ()
0000d0d5
KY
6155 (let (end last)
6156 (while (not end)
6157 (message-skip-to-next-address)
6158 (cond ((bolp)
6159 (end-of-line 0)
6160 (setq end 1))
6161 ((eobp)
6162 (setq end 0)))
6163 (when (and (> (current-column) 78)
6164 last)
6165 (save-excursion
6166 (goto-char last)
6167 (delete-char (- (skip-chars-backward " \t")))
6168 (insert "\n\t")))
6169 (setq last (point)))
6170 (forward-line end)))
01c52d31
MB
6171
6172(defun message-fill-field-general ()
eec82323 6173 (let ((begin (point))
16409b0b 6174 (fill-column 78)
eec82323 6175 (fill-prefix "\t"))
01c52d31
MB
6176 (while (and (search-forward "\n" nil t)
6177 (not (eobp)))
6178 (replace-match " " t t))
6179 (fill-region-as-paragraph begin (point-max))
6180 ;; Tapdance around looong Message-IDs.
6181 (forward-line -1)
6182 (when (looking-at "[ \t]*$")
6183 (message-delete-line))
6184 (goto-char begin)
6185 (search-forward ":" nil t)
6186 (when (looking-at "\n[ \t]+")
6187 (replace-match " " t t))
6188 (goto-char (point-max))))
eec82323 6189
16409b0b 6190(defun message-shorten-1 (list cut surplus)
7d829636 6191 "Cut SURPLUS elements out of LIST, beginning with CUTth one."
16409b0b
GM
6192 (setcdr (nthcdr (- cut 2) list)
6193 (nthcdr (+ (- cut 2) surplus 1) list)))
6194
6748645f 6195(defun message-shorten-references (header references)
23f87bed 6196 "Trim REFERENCES to be 21 Message-ID long or less, and fold them.
01c52d31
MB
6197When sending via news, also check that the REFERENCES are less
6198than 988 characters long, and if they are not, trim them until
6199they are."
22bcf204 6200 ;; 21 is the number suggested by USAGE.
23f87bed 6201 (let ((maxcount 21)
16409b0b 6202 (count 0)
23f87bed 6203 (cut 2)
6748645f 6204 refs)
16409b0b 6205 (with-temp-buffer
6748645f
LMI
6206 (insert references)
6207 (goto-char (point-min))
9b3ebcb6
MB
6208 ;; Cons a list of valid references. GNKSA says we must not include MIDs
6209 ;; with whitespace or missing brackets (7.a "Does not propagate broken
6210 ;; Message-IDs in original References").
6211 (while (re-search-forward "<[^ <]+@[^ <]+>" nil t)
6748645f 6212 (push (match-string 0) refs))
16409b0b
GM
6213 (setq refs (nreverse refs)
6214 count (length refs)))
6215
6216 ;; If the list has more than MAXCOUNT elements, trim it by
6217 ;; removing the CUTth element and the required number of
6218 ;; elements that follow.
6219 (when (> count maxcount)
6220 (let ((surplus (- count maxcount)))
6221 (message-shorten-1 refs cut surplus)
6222 (decf count surplus)))
6223
01c52d31
MB
6224 ;; When sending via news, make sure the total folded length will
6225 ;; be less than 998 characters. This is to cater to broken INN
6226 ;; 2.3 which counts the total number of characters in a header
6227 ;; rather than the physical line length of each line, as it should.
16409b0b 6228 ;;
01c52d31
MB
6229 ;; This hack should be removed when it's believed than INN 2.3 is
6230 ;; no longer widely used.
6231 ;;
6232 ;; At this point the headers have not been generated, thus we use
6233 ;; message-this-is-news directly.
6234 (when message-this-is-news
6235 (while (< 998
6236 (with-temp-buffer
6237 (message-insert-header
6238 header (mapconcat #'identity refs " "))
6239 (buffer-size)))
6240 (message-shorten-1 refs cut 1)))
16409b0b
GM
6241 ;; Finally, collect the references back into a string and insert
6242 ;; it into the buffer.
01c52d31 6243 (message-insert-header header (mapconcat #'identity refs " "))))
6748645f 6244
eec82323
LMI
6245(defun message-position-point ()
6246 "Move point to where the user probably wants to find it."
6247 (message-narrow-to-headers)
6248 (cond
6249 ((re-search-forward "^[^:]+:[ \t]*$" nil t)
6250 (search-backward ":" )
6251 (widen)
6252 (forward-char 1)
16409b0b 6253 (if (eq (char-after) ? )
eec82323
LMI
6254 (forward-char 1)
6255 (insert " ")))
6256 (t
6257 (goto-char (point-max))
6258 (widen)
6259 (forward-line 1)
6260 (unless (looking-at "$")
6261 (forward-line 2)))
6262 (sit-for 0)))
6263
23f87bed
MB
6264(defcustom message-beginning-of-line t
6265 "Whether \\<message-mode-map>\\[message-beginning-of-line]\
6266 goes to beginning of header values."
bf247b6e 6267 :version "22.1"
23f87bed
MB
6268 :group 'message-buffers
6269 :link '(custom-manual "(message)Movement")
6270 :type 'boolean)
6271
6272(defun message-beginning-of-line (&optional n)
6273 "Move point to beginning of header value or to beginning of line.
6274The prefix argument N is passed directly to `beginning-of-line'.
6275
6276This command is identical to `beginning-of-line' if point is
6277outside the message header or if the option `message-beginning-of-line'
6278is nil.
6279
6280If point is in the message header and on a (non-continued) header
f5d01350
SM
6281line, move point to the beginning of the header value or the beginning of line,
6282whichever is closer. If point is already at beginning of line, move point to
6283beginning of header value. Therefore, repeated calls will toggle point
6284between beginning of field and beginning of line."
23f87bed
MB
6285 (interactive "p")
6286 (let ((zrs 'zmacs-region-stays))
a445370f 6287 (when (and (featurep 'xemacs) (interactive-p) (boundp zrs))
23f87bed
MB
6288 (set zrs t)))
6289 (if (and message-beginning-of-line
6290 (message-point-in-header-p))
6291 (let* ((here (point))
6292 (bol (progn (beginning-of-line n) (point)))
01c52d31 6293 (eol (point-at-eol))
23f87bed 6294 (eoh (re-search-forward ": *" eol t)))
f5d01350
SM
6295 (goto-char
6296 (if (and eoh (or (< eoh here) (= bol here)))
6297 eoh bol)))
23f87bed
MB
6298 (beginning-of-line n)))
6299
eec82323
LMI
6300(defun message-buffer-name (type &optional to group)
6301 "Return a new (unique) buffer name based on TYPE and TO."
6302 (cond
16409b0b 6303 ;; Generate a new buffer name The Message Way.
815b81c8 6304 ((memq message-generate-new-buffers '(unique t))
16409b0b
GM
6305 (generate-new-buffer-name
6306 (concat "*" type
6307 (if to
6308 (concat " to "
6309 (or (car (mail-extract-address-components to))
6310 to) "")
6311 "")
6312 (if (and group (not (string= group ""))) (concat " on " group) "")
6313 "*")))
eec82323
LMI
6314 ;; Check whether `message-generate-new-buffers' is a function,
6315 ;; and if so, call it.
23f87bed 6316 ((functionp message-generate-new-buffers)
eec82323 6317 (funcall message-generate-new-buffers type to group))
16409b0b 6318 ((eq message-generate-new-buffers 'unsent)
eec82323 6319 (generate-new-buffer-name
16409b0b 6320 (concat "*unsent " type
eec82323
LMI
6321 (if to
6322 (concat " to "
6323 (or (car (mail-extract-address-components to))
6324 to) "")
6325 "")
6326 (if (and group (not (string= group ""))) (concat " on " group) "")
6327 "*")))
815b81c8 6328 ;; Search for the existing message buffer with the specified name.
eec82323 6329 (t
815b81c8
MB
6330 (let* ((new (if (eq message-generate-new-buffers 'standard)
6331 (generate-new-buffer-name (concat "*" type " message*"))
6332 (let ((message-generate-new-buffers 'unique))
6333 (message-buffer-name type to group))))
6334 (regexp (concat "\\`"
6335 (regexp-quote
6336 (if (string-match "<[0-9]+>\\'" new)
6337 (substring new 0 (match-beginning 0))
6338 new))
6339 "\\(?:<\\([0-9]+\\)>\\)?\\'"))
6340 (case-fold-search nil))
6341 (or (cdar
6342 (last
6343 (sort
6344 (delq nil
6345 (mapcar
6346 (lambda (b)
6347 (when (and (string-match regexp (setq b (buffer-name b)))
6348 (eq (with-current-buffer b major-mode)
6349 'message-mode))
6350 (cons (string-to-number (or (match-string 1 b) "1"))
6351 b)))
6352 (buffer-list)))
6353 'car-less-than-car)))
6354 new)))))
eec82323 6355
4a508479 6356(defun message-pop-to-buffer (name &optional switch-function)
eec82323
LMI
6357 "Pop to buffer NAME, and warn if it already exists and is modified."
6358 (let ((buffer (get-buffer name)))
6359 (if (and buffer
6360 (buffer-name buffer))
815b81c8
MB
6361 (let ((window (get-buffer-window buffer 0)))
6362 (if window
6363 ;; Raise the frame already displaying the message buffer.
6364 (progn
6365 (gnus-select-frame-set-input-focus (window-frame window))
6366 (select-window window))
52749742 6367 (funcall (or switch-function #'pop-to-buffer) buffer)
4a508479 6368 (set-buffer buffer))
eec82323 6369 (when (and (buffer-modified-p)
815b81c8
MB
6370 (not (prog1
6371 (y-or-n-p
6372 "Message already being composed; erase? ")
6373 (message nil))))
eec82323 6374 (error "Message being composed")))
1ea1c2fa
KY
6375 (funcall (or switch-function
6376 (if (fboundp #'pop-to-buffer-same-window)
6377 #'pop-to-buffer-same-window
6378 #'pop-to-buffer))
6379 name)
4a508479 6380 (set-buffer name))
6748645f
LMI
6381 (erase-buffer)
6382 (message-mode)))
eec82323
LMI
6383
6384(defun message-do-send-housekeeping ()
6385 "Kill old message buffers."
6386 ;; We might have sent this buffer already. Delete it from the
6387 ;; list of buffers.
6388 (setq message-buffer-list (delq (current-buffer) message-buffer-list))
6389 (while (and message-max-buffers
23f87bed 6390 message-buffer-list
eec82323
LMI
6391 (>= (length message-buffer-list) message-max-buffers))
6392 ;; Kill the oldest buffer -- unless it has been changed.
6393 (let ((buffer (pop message-buffer-list)))
6394 (when (and (buffer-name buffer)
6395 (not (buffer-modified-p buffer)))
6396 (kill-buffer buffer))))
6397 ;; Rename the buffer.
6398 (if message-send-rename-function
6399 (funcall message-send-rename-function)
a5166359 6400 (message-default-send-rename-function))
eec82323
LMI
6401 ;; Push the current buffer onto the list.
6402 (when message-max-buffers
6403 (setq message-buffer-list
6404 (nconc message-buffer-list (list (current-buffer))))))
6405
a5166359
LMI
6406(defun message-default-send-rename-function ()
6407 ;; Note: mail-abbrevs of XEmacs renames buffer name behind Gnus.
6408 (when (string-match
6409 "\\`\\*\\(sent \\|unsent \\)?\\(.+\\)\\*[^\\*]*\\|\\`mail to "
6410 (buffer-name))
6411 (let ((name (match-string 2 (buffer-name)))
6412 to group)
6413 (if (not (or (null name)
6414 (string-equal name "mail")
6415 (string-equal name "posting")))
6416 (setq name (concat "*sent " name "*"))
6417 (message-narrow-to-headers)
6418 (setq to (message-fetch-field "to"))
6419 (setq group (message-fetch-field "newsgroups"))
6420 (widen)
6421 (setq name
6422 (cond
6423 (to (concat "*sent mail to "
6424 (or (car (mail-extract-address-components to))
6425 to) "*"))
6426 ((and group (not (string= group "")))
6427 (concat "*sent posting on " group "*"))
6428 (t "*sent mail*"))))
6429 (unless (string-equal name (buffer-name))
6430 (rename-buffer name t)))))
6431
88818fbe
SZ
6432(defun message-mail-user-agent ()
6433 (let ((mua (cond
6434 ((not message-mail-user-agent) nil)
6435 ((eq message-mail-user-agent t) mail-user-agent)
6436 (t message-mail-user-agent))))
6437 (if (memq mua '(message-user-agent gnus-user-agent))
6438 nil
6439 mua)))
6440
b1ea3797
CY
6441;; YANK-ACTION, if non-nil, can be a buffer or a yank action of the
6442;; form (FUNCTION . ARGS).
6443(defun message-setup (headers &optional yank-action actions
25ca2e61 6444 continue switch-function return-action)
88818fbe 6445 (let ((mua (message-mail-user-agent))
b1ea3797 6446 subject to field)
88818fbe 6447 (if (not (and message-this-is-mail mua))
25ca2e61 6448 (message-setup-1 headers yank-action actions return-action)
88818fbe
SZ
6449 (setq headers (copy-sequence headers))
6450 (setq field (assq 'Subject headers))
6451 (when field
6452 (setq subject (cdr field))
6453 (setq headers (delq field headers)))
6454 (setq field (assq 'To headers))
6455 (when field
6456 (setq to (cdr field))
6457 (setq headers (delq field headers)))
6458 (let ((mail-user-agent mua))
7d829636 6459 (compose-mail to subject
88818fbe
SZ
6460 (mapcar (lambda (item)
6461 (cons
6462 (format "%s" (car item))
6463 (cdr item)))
6464 headers)
b1ea3797
CY
6465 continue switch-function
6466 (if (bufferp yank-action)
6467 (list 'insert-buffer yank-action)
6468 yank-action)
6469 actions)))))
a1506d29 6470
23f87bed
MB
6471(defun message-headers-to-generate (headers included-headers excluded-headers)
6472 "Return a list that includes all headers from HEADERS.
c86d4601 6473If INCLUDED-HEADERS is a list, just include those headers. If it is
23f87bed
MB
6474t, include all headers. In any case, headers from EXCLUDED-HEADERS
6475are not included."
6476 (let ((result nil)
6477 header-name)
6478 (dolist (header headers)
6479 (setq header-name (cond
6480 ((and (consp header)
6481 (eq (car header) 'optional))
6482 ;; On the form (optional . Header)
6483 (cdr header))
6484 ((consp header)
6485 ;; On the form (Header . function)
6486 (car header))
6487 (t
6488 ;; Just a Header.
6489 header)))
6490 (when (and (not (memq header-name excluded-headers))
6491 (or (eq included-headers t)
6492 (memq header-name included-headers)))
6493 (push header result)))
6494 (nreverse result)))
6495
25ca2e61 6496(defun message-setup-1 (headers &optional yank-action actions return-action)
0c773047
SZ
6497 (dolist (action actions)
6498 (condition-case nil
6499 (add-to-list 'message-send-actions
6500 `(apply ',(car action) ',(cdr action)))))
25ca2e61 6501 (setq message-return-action return-action)
f53a6590
CY
6502 (setq message-reply-buffer
6503 (if (and (consp yank-action)
6504 (eq (car yank-action) 'insert-buffer))
6505 (nth 1 yank-action)
6506 yank-action))
eec82323
LMI
6507 (goto-char (point-min))
6508 ;; Insert all the headers.
6509 (mail-header-format
6510 (let ((h headers)
6511 (alist message-header-format-alist))
6512 (while h
6513 (unless (assq (caar h) message-header-format-alist)
6514 (push (list (caar h)) alist))
6515 (pop h))
6516 alist)
6517 headers)
6518 (delete-region (point) (progn (forward-line -1) (point)))
6519 (when message-default-headers
d806c872
G
6520 (insert
6521 (if (functionp message-default-headers)
6522 (funcall message-default-headers)
6523 message-default-headers))
f129a4df 6524 (or (bolp) (insert ?\n)))
ec72bf63 6525 (insert (concat mail-header-separator "\n"))
eec82323 6526 (forward-line -1)
0d1c2cc8
G
6527 ;; If a crash happens while replying, the auto-save file would *not* have a
6528 ;; `References:' header if `message-generate-headers-first' was nil.
6529 ;; Therefore, always generate it first.
6530 (let ((message-generate-headers-first
184469b7
JD
6531 (if (eq message-generate-headers-first t)
6532 t
6533 (append message-generate-headers-first '(References)))))
0d1c2cc8
G
6534 (when (message-news-p)
6535 (when message-default-news-headers
6536 (insert message-default-news-headers)
6537 (or (bolp) (insert ?\n)))
eec82323 6538 (message-generate-headers
23f87bed 6539 (message-headers-to-generate
0d1c2cc8
G
6540 (append message-required-news-headers
6541 message-required-headers)
6542 message-generate-headers-first
6543 '(Lines Subject))))
6544 (when (message-mail-p)
6545 (when message-default-mail-headers
6546 (insert message-default-mail-headers)
6547 (or (bolp) (insert ?\n)))
eec82323 6548 (message-generate-headers
23f87bed 6549 (message-headers-to-generate
0d1c2cc8
G
6550 (append message-required-mail-headers
6551 message-required-headers)
6552 message-generate-headers-first
6553 '(Lines Subject)))))
eec82323
LMI
6554 (run-hooks 'message-signature-setup-hook)
6555 (message-insert-signature)
eec82323
LMI
6556 (save-restriction
6557 (message-narrow-to-headers)
6558 (run-hooks 'message-header-setup-hook))
a8151ef7 6559 (setq buffer-undo-list nil)
01c52d31
MB
6560 (when message-generate-hashcash
6561 ;; Generate hashcash headers for recipients already known
6562 (mail-add-payment-async))
9b3ebcb6
MB
6563 ;; Gnus posting styles are applied via buffer-local `message-setup-hook'
6564 ;; values.
eec82323 6565 (run-hooks 'message-setup-hook)
46e8fe3d
MB
6566 ;; Do this last to give it precedence over posting styles, etc.
6567 (when (message-mail-p)
6568 (save-restriction
6569 (message-narrow-to-headers)
6570 (if message-alternative-emails
6571 (message-use-alternative-email-as-from))))
eec82323 6572 (message-position-point)
9b3ebcb6
MB
6573 ;; Allow correct handling of `message-checksum' in `message-yank-original':
6574 (set-buffer-modified-p nil)
9af32a1a
EZ
6575 (undo-boundary)
6576 ;; rmail-start-mail expects message-mail to return t (Bug#9392)
6577 t)
eec82323
LMI
6578
6579(defun message-set-auto-save-file-name ()
6580 "Associate the message buffer with a file in the drafts directory."
83e905f3 6581 (when message-auto-save-directory
8c6f6f4b
DL
6582 (unless (file-directory-p
6583 (directory-file-name message-auto-save-directory))
62363b21 6584 (make-directory message-auto-save-directory t))
6748645f
LMI
6585 (if (gnus-alive-p)
6586 (setq message-draft-article
6587 (nndraft-request-associate-buffer "drafts"))
e52cac88
MB
6588
6589 ;; If Gnus were alive, draft messages would be saved in the drafts folder.
6590 ;; But Gnus is not alive, so arrange to save the draft message in a
6591 ;; regular file in message-auto-save-directory. Append a unique
6592 ;; time-based suffix to the filename to allow multiple drafts to be saved
6593 ;; simultaneously without overwriting each other (which mimics the
6594 ;; functionality of the Gnus drafts folder).
23f87bed 6595 (setq buffer-file-name (expand-file-name
e52cac88 6596 (concat
23f87bed 6597 (if (memq system-type
f5ec697d 6598 '(ms-dos windows-nt cygwin))
23f87bed
MB
6599 "message"
6600 "*message*")
e52cac88 6601 (format-time-string "-%Y%m%d-%H%M%S"))
23f87bed 6602 message-auto-save-directory))
6748645f 6603 (setq buffer-auto-save-file-name (make-auto-save-file-name)))
16409b0b
GM
6604 (clear-visited-file-modtime)
6605 (setq buffer-file-coding-system message-draft-coding-system)))
6748645f
LMI
6606
6607(defun message-disassociate-draft ()
6608 "Disassociate the message buffer from the drafts directory."
6609 (when message-draft-article
6610 (nndraft-request-expire-articles
b1d6cda3 6611 (list message-draft-article) "drafts" nil t)))
eec82323 6612
16409b0b
GM
6613(defun message-insert-headers ()
6614 "Generate the headers for the article."
6615 (interactive)
6616 (save-excursion
6617 (save-restriction
6618 (message-narrow-to-headers)
6619 (when (message-news-p)
6620 (message-generate-headers
6621 (delq 'Lines
6622 (delq 'Subject
6623 (copy-sequence message-required-news-headers)))))
6624 (when (message-mail-p)
6625 (message-generate-headers
6626 (delq 'Lines
6627 (delq 'Subject
6628 (copy-sequence message-required-mail-headers))))))))
6629
eec82323
LMI
6630\f
6631
6632;;;
6633;;; Commands for interfacing with message
6634;;;
6635
6636;;;###autoload
25ca2e61
CY
6637(defun message-mail (&optional to subject other-headers continue
6638 switch-function yank-action send-actions
6639 return-action &rest ignored)
6748645f 6640 "Start editing a mail message to be sent.
815b81c8
MB
6641OTHER-HEADERS is an alist of header/value pairs. CONTINUE says whether
6642to continue editing a message already being composed. SWITCH-FUNCTION
6643is a function used to switch to and display the mail buffer."
eec82323 6644 (interactive)
b1ea3797 6645 (let ((message-this-is-mail t))
88818fbe 6646 (unless (message-mail-user-agent)
4a508479 6647 (message-pop-to-buffer
815b81c8
MB
6648 ;; Search for the existing message buffer if `continue' is non-nil.
6649 (let ((message-generate-new-buffers
6650 (when (or (not continue)
6651 (eq message-generate-new-buffers 'standard)
6652 (functionp message-generate-new-buffers))
6653 message-generate-new-buffers)))
4a508479
CY
6654 (message-buffer-name "mail" to))
6655 switch-function))
eec82323
LMI
6656 (message-setup
6657 (nconc
6658 `((To . ,(or to "")) (Subject . ,(or subject "")))
ae01d7ca
GM
6659 ;; C-h f compose-mail says that headers should be specified as
6660 ;; (string . value); however all the rest of message expects
6661 ;; headers to be symbols, not strings (eg message-header-format-alist).
6662 ;; http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
6663 ;; We need to convert any string input, eg from rmail-start-mail.
6664 (dolist (h other-headers other-headers)
6665 (if (stringp (car h)) (setcar h (intern (capitalize (car h)))))))
25ca2e61 6666 yank-action send-actions continue switch-function
0d1c2cc8 6667 return-action)))
eec82323
LMI
6668
6669;;;###autoload
6670(defun message-news (&optional newsgroups subject)
6671 "Start editing a news article to be sent."
6672 (interactive)
6673 (let ((message-this-is-news t))
23f87bed 6674 (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups))
eec82323
LMI
6675 (message-setup `((Newsgroups . ,(or newsgroups ""))
6676 (Subject . ,(or subject ""))))))
6677
9b3ebcb6
MB
6678(defun message-alter-recipients-discard-bogus-full-name (addrcell)
6679 "Discard mail address in full names.
6680When the full name in reply headers contains the mail
6681address (e.g. \"foo@bar <foo@bar>\"), discard full name.
6682ADDRCELL is a cons cell where the car is the mail address and the
6683cdr is the complete address (full name and mail address)."
6684 (if (string-match (concat (regexp-quote (car addrcell)) ".*"
6685 (regexp-quote (car addrcell)))
6686 (cdr addrcell))
6687 (cons (car addrcell) (car addrcell))
6688 addrcell))
6689
6690(defcustom message-alter-recipients-function nil
6691 "Function called to allow alteration of reply header structures.
6692It is called in `message-get-reply-headers' for each recipient.
6693The function is called with one parameter, a cons cell ..."
6694 :type '(choice (const :tag "None" nil)
6695 (const :tag "Discard bogus full name"
6696 message-alter-recipients-discard-bogus-full-name)
6697 function)
6698 :version "23.1" ;; No Gnus
6699 :group 'message-headers)
6700
23f87bed 6701(defun message-get-reply-headers (wide &optional to-address address-headers)
01c52d31 6702 (let (follow-to mct never-mct to cc author mft recipients extra)
2cdd366f 6703 ;; Find all relevant headers we need.
23f87bed
MB
6704 (save-restriction
6705 (message-narrow-to-headers-or-head)
6706 ;; Gmane renames "To". Look at "Original-To", too, if it is present in
6707 ;; message-header-synonyms.
6708 (setq to (or (message-fetch-field "to")
6709 (and (loop for synonym in message-header-synonyms
6710 when (memq 'Original-To synonym)
6711 return t)
6712 (message-fetch-field "original-to")))
6713 cc (message-fetch-field "cc")
01c52d31
MB
6714 extra (when message-extra-wide-headers
6715 (mapconcat 'identity
6716 (mapcar 'message-fetch-field
6717 message-extra-wide-headers)
6718 ", "))
23f87bed
MB
6719 mct (message-fetch-field "mail-copies-to")
6720 author (or (message-fetch-field "mail-reply-to")
70678cd5 6721 (message-fetch-field "reply-to"))
23f87bed 6722 mft (and message-use-mail-followup-to
70678cd5
KY
6723 (message-fetch-field "mail-followup-to")))
6724 ;; Make sure this message goes to the author if this is a wide
3394be35 6725 ;; reply, since Reply-To address may be a list address a mailing
70678cd5
KY
6726 ;; list server added.
6727 (when (and wide author)
6728 (setq cc (concat author ", " cc)))
6729 (when (or wide (not author))
6730 (setq author (or (message-fetch-field "from") ""))))
16409b0b
GM
6731
6732 ;; Handle special values of Mail-Copies-To.
6733 (when mct
6734 (cond ((or (equal (downcase mct) "never")
6735 (equal (downcase mct) "nobody"))
6736 (setq never-mct t)
6737 (setq mct nil))
6738 ((or (equal (downcase mct) "always")
6739 (equal (downcase mct) "poster"))
23f87bed 6740 (setq mct author))))
16409b0b 6741
23f87bed
MB
6742 (save-match-data
6743 ;; Build (textual) list of new recipient addresses.
6744 (cond
2755ee78
LMI
6745 (to-address
6746 (setq recipients (concat ", " to-address))
6747 ;; If the author explicitly asked for a copy, we don't deny it to them.
6748 (if mct (setq recipients (concat recipients ", " mct))))
23f87bed
MB
6749 ((not wide)
6750 (setq recipients (concat ", " author)))
6751 (address-headers
6752 (dolist (header address-headers)
6753 (let ((value (message-fetch-field header)))
6754 (when value
6755 (setq recipients (concat recipients ", " value))))))
6756 ((and mft
6757 (string-match "[^ \t,]" mft)
6758 (or (not (eq message-use-mail-followup-to 'ask))
6759 (message-y-or-n-p "Obey Mail-Followup-To? " t "\
6760You should normally obey the Mail-Followup-To: header. In this
6761article, it has the value of
6762
6763" mft "
6764
6765which directs your response to " (if (string-match "," mft)
6766 "the specified addresses"
6767 "that address only") ".
6768
6769Most commonly, Mail-Followup-To is used by a mailing list poster to
6770express that responses should be sent to just the list, and not the
6771poster as well.
6772
6773If a message is posted to several mailing lists, Mail-Followup-To may
6774also be used to direct the following discussion to one list only,
6775because discussions that are spread over several lists tend to be
6776fragmented and very difficult to follow.
6777
6778Also, some source/announcement lists are not intended for discussion;
2fd561a9
MB
6779responses here are directed to other addresses.
6780
6781You may customize the variable `message-use-mail-followup-to', if you
6782want to get rid of this query permanently.")))
23f87bed 6783 (setq recipients (concat ", " mft)))
23f87bed
MB
6784 (t
6785 (setq recipients (if never-mct "" (concat ", " author)))
01c52d31
MB
6786 (if to (setq recipients (concat recipients ", " to)))
6787 (if cc (setq recipients (concat recipients ", " cc)))
6788 (if extra (setq recipients (concat recipients ", " extra)))
23f87bed
MB
6789 (if mct (setq recipients (concat recipients ", " mct)))))
6790 (if (>= (length recipients) 2)
6791 ;; Strip the leading ", ".
6792 (setq recipients (substring recipients 2)))
6793 ;; Squeeze whitespace.
6794 (while (string-match "[ \t][ \t]+" recipients)
6795 (setq recipients (replace-match " " t t recipients)))
0fb40182
JD
6796 ;; Remove addresses that match `mail-dont-reply-to-names'.
6797 (let ((mail-dont-reply-to-names (message-dont-reply-to-names)))
6798 (setq recipients (mail-dont-reply-to recipients)))
23f87bed
MB
6799 ;; Perhaps "Mail-Copies-To: never" removed the only address?
6800 (if (string-equal recipients "")
6801 (setq recipients author))
6802 ;; Convert string to a list of (("foo@bar" . "Name <Foo@BAR>") ...).
6803 (setq recipients
6804 (mapcar
6805 (lambda (addr)
9b3ebcb6
MB
6806 (if message-alter-recipients-function
6807 (funcall message-alter-recipients-function
6808 (cons (downcase (mail-strip-quoted-names addr))
6809 addr))
6810 (cons (downcase (mail-strip-quoted-names addr)) addr)))
23f87bed 6811 (message-tokenize-header recipients)))
ecd283b9 6812 ;; Remove all duplicates.
23f87bed
MB
6813 (let ((s recipients))
6814 (while s
ecd283b9
LMI
6815 (let ((address (car (pop s))))
6816 (while (assoc address s)
6817 (setq recipients (delq (assoc address s) recipients)
6818 s (delq (assoc address s) s))))))
23f87bed
MB
6819
6820 ;; Remove hierarchical lists that are contained within each other,
6821 ;; if message-hierarchical-addresses is defined.
6822 (when message-hierarchical-addresses
6823 (let ((plain-addrs (mapcar 'car recipients))
6824 subaddrs recip)
6825 (while plain-addrs
6826 (setq subaddrs (assoc (car plain-addrs)
6827 message-hierarchical-addresses)
6828 plain-addrs (cdr plain-addrs))
6829 (when subaddrs
6830 (setq subaddrs (cdr subaddrs))
6831 (while subaddrs
6832 (setq recip (assoc (car subaddrs) recipients)
6833 subaddrs (cdr subaddrs))
6834 (if recip
6835 (setq recipients (delq recip recipients))))))))
6836
2cdd366f 6837 (setq recipients (message-prune-recipients recipients))
c9fc72fa 6838
23f87bed
MB
6839 ;; Build the header alist. Allow the user to be asked whether
6840 ;; or not to reply to all recipients in a wide reply.
6841 (setq follow-to (list (cons 'To (cdr (pop recipients)))))
6842 (when (and recipients
6843 (or (not message-wide-reply-confirm-recipients)
6844 (y-or-n-p "Reply to all recipients? ")))
6845 (setq recipients (mapconcat
6846 (lambda (addr) (cdr addr)) recipients ", "))
6847 (if (string-match "^ +" recipients)
6848 (setq recipients (substring recipients (match-end 0))))
6849 (push (cons 'Cc recipients) follow-to)))
16409b0b
GM
6850 follow-to))
6851
2cdd366f
KY
6852(defun message-prune-recipients (recipients)
6853 (dolist (rule message-prune-recipient-rules)
6854 (let ((match (car rule))
c9fc72fa 6855 dup-match
2cdd366f
KY
6856 address)
6857 (dolist (recipient recipients)
6858 (setq address (car recipient))
6859 (when (string-match match address)
6860 (setq dup-match (replace-match (cadr rule) nil nil address))
6861 (dolist (recipient recipients)
6862 ;; Don't delete the address that triggered this.
6863 (when (and (not (eq address (car recipient)))
6864 (string-match dup-match (car recipient)))
6865 (setq recipients (delq recipient recipients))))))))
6866 recipients)
6867
4573e0df
MB
6868(defcustom message-simplify-subject-functions
6869 '(message-strip-list-identifiers
6870 message-strip-subject-re
6871 message-strip-subject-trailing-was
6872 message-strip-subject-encoded-words)
6873 "List of functions taking a string argument that simplify subjects.
6874The functions are applied when replying to a message.
6875
6876Useful functions to put in this list include:
6877`message-strip-list-identifiers', `message-strip-subject-re',
6878`message-strip-subject-trailing-was', and
6879`message-strip-subject-encoded-words'."
6880 :version "22.1" ;; Gnus 5.10.9
6881 :group 'message-various
6882 :type '(repeat function))
6883
6884(defun message-simplify-subject (subject &optional functions)
6885 "Return simplified SUBJECT."
6886 (unless functions
6887 ;; Simplify fully:
6888 (setq functions message-simplify-subject-functions))
6889 (when (and (memq 'message-strip-list-identifiers functions)
6890 gnus-list-identifiers)
6891 (setq subject (message-strip-list-identifiers subject)))
6892 (when (memq 'message-strip-subject-re functions)
6893 (setq subject (concat "Re: " (message-strip-subject-re subject))))
6894 (when (and (memq 'message-strip-subject-trailing-was functions)
6895 message-subject-trailing-was-query)
6896 (setq subject (message-strip-subject-trailing-was subject)))
6897 (when (memq 'message-strip-subject-encoded-words functions)
6898 (setq subject (message-strip-subject-encoded-words subject)))
6899 subject)
6900
eec82323 6901;;;###autoload
5415d076 6902(defun message-reply (&optional to-address wide switch-function)
eec82323
LMI
6903 "Start editing a reply to the article in the current buffer."
6904 (interactive)
16409b0b 6905 (require 'gnus-sum) ; for gnus-list-identifiers
eec82323 6906 (let ((cur (current-buffer))
99c2a4e3 6907 from subject date
eec82323
LMI
6908 references message-id follow-to
6909 (inhibit-point-motion-hooks t)
16409b0b
GM
6910 (message-this-is-mail t)
6911 gnus-warning)
eec82323 6912 (save-restriction
158d6e07 6913 (message-narrow-to-head-1)
eec82323
LMI
6914 ;; Allow customizations to have their say.
6915 (if (not wide)
6916 ;; This is a regular reply.
23f87bed 6917 (when (functionp message-reply-to-function)
eec82323 6918 (save-excursion
23f87bed
MB
6919 (setq follow-to (funcall message-reply-to-function))))
6920 ;; This is a followup.
6921 (when (functionp message-wide-reply-to-function)
6922 (save-excursion
6923 (setq follow-to
6924 (funcall message-wide-reply-to-function)))))
16409b0b 6925 (setq message-id (message-fetch-field "message-id" t)
eec82323 6926 references (message-fetch-field "references")
16409b0b 6927 date (message-fetch-field "date")
0327a464 6928 from (or (message-fetch-field "from") "nobody")
16409b0b 6929 subject (or (message-fetch-field "subject") "none"))
4573e0df
MB
6930
6931 ;; Strip list identifiers, "Re: ", and "was:"
6932 (setq subject (message-simplify-subject subject))
eec82323 6933
23f87bed
MB
6934 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
6935 (string-match "<[^>]+>" gnus-warning))
6936 (setq message-id (match-string 0 gnus-warning)))
eec82323 6937
23f87bed
MB
6938 (unless follow-to
6939 (setq follow-to (message-get-reply-headers wide to-address))))
eec82323 6940
c152c1d6
LMI
6941 (let ((headers
6942 `((Subject . ,subject)
6943 ,@follow-to)))
6944 (unless (message-mail-user-agent)
6945 (message-pop-to-buffer
6946 (message-buffer-name
6947 (if wide "wide reply" "reply") from
6948 (if wide to-address nil))
6949 switch-function))
6950 (setq message-reply-headers
6951 (vector 0 (cdr (assq 'Subject headers))
6952 from date message-id references 0 0 ""))
6953 (message-setup headers cur))))
eec82323
LMI
6954
6955;;;###autoload
6748645f 6956(defun message-wide-reply (&optional to-address)
eec82323
LMI
6957 "Make a \"wide\" reply to the message in the current buffer."
6958 (interactive)
6748645f 6959 (message-reply to-address t))
eec82323
LMI
6960
6961;;;###autoload
6962(defun message-followup (&optional to-newsgroups)
6963 "Follow up to the message in the current buffer.
6964If TO-NEWSGROUPS, use that as the new Newsgroups line."
6965 (interactive)
16409b0b 6966 (require 'gnus-sum) ; for gnus-list-identifiers
eec82323 6967 (let ((cur (current-buffer))
23f87bed 6968 from subject date reply-to mrt mct
eec82323
LMI
6969 references message-id follow-to
6970 (inhibit-point-motion-hooks t)
6971 (message-this-is-news t)
6972 followup-to distribution newsgroups gnus-warning posted-to)
6973 (save-restriction
6974 (narrow-to-region
6975 (goto-char (point-min))
6976 (if (search-forward "\n\n" nil t)
6977 (1- (point))
6978 (point-max)))
23f87bed 6979 (when (functionp message-followup-to-function)
eec82323
LMI
6980 (setq follow-to
6981 (funcall message-followup-to-function)))
6982 (setq from (message-fetch-field "from")
6983 date (message-fetch-field "date")
6984 subject (or (message-fetch-field "subject") "none")
6985 references (message-fetch-field "references")
6986 message-id (message-fetch-field "message-id" t)
6987 followup-to (message-fetch-field "followup-to")
6988 newsgroups (message-fetch-field "newsgroups")
6989 posted-to (message-fetch-field "posted-to")
6990 reply-to (message-fetch-field "reply-to")
23f87bed 6991 mrt (message-fetch-field "mail-reply-to")
eec82323
LMI
6992 distribution (message-fetch-field "distribution")
6993 mct (message-fetch-field "mail-copies-to"))
6994 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
6995 (string-match "<[^>]+>" gnus-warning))
6996 (setq message-id (match-string 0 gnus-warning)))
6997 ;; Remove bogus distribution.
6998 (when (and (stringp distribution)
6999 (let ((case-fold-search t))
7000 (string-match "world" distribution)))
7001 (setq distribution nil))
4573e0df
MB
7002 ;; Strip list identifiers, "Re: ", and "was:"
7003 (setq subject (message-simplify-subject subject))
eec82323
LMI
7004 (widen))
7005
7006 (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
7007
23f87bed
MB
7008 (setq message-reply-headers
7009 (vector 0 subject from date message-id references 0 0 ""))
7010
eec82323
LMI
7011 (message-setup
7012 `((Subject . ,subject)
7013 ,@(cond
7014 (to-newsgroups
7015 (list (cons 'Newsgroups to-newsgroups)))
7016 (follow-to follow-to)
7017 ((and followup-to message-use-followup-to)
7018 (list
7019 (cond
7020 ((equal (downcase followup-to) "poster")
7021 (if (or (eq message-use-followup-to 'use)
7022 (message-y-or-n-p "Obey Followup-To: poster? " t "\
7023You should normally obey the Followup-To: header.
7024
7025`Followup-To: poster' sends your response via e-mail instead of news.
7026
7027A typical situation where `Followup-To: poster' is used is when the poster
2fd561a9
MB
7028does not read the newsgroup, so he wouldn't see any replies sent to it.
7029
7030You may customize the variable `message-use-followup-to', if you
7031want to get rid of this query permanently."))
eec82323
LMI
7032 (progn
7033 (setq message-this-is-news nil)
23f87bed 7034 (cons 'To (or mrt reply-to from "")))
eec82323
LMI
7035 (cons 'Newsgroups newsgroups)))
7036 (t
7037 (if (or (equal followup-to newsgroups)
7038 (not (eq message-use-followup-to 'ask))
7039 (message-y-or-n-p
7040 (concat "Obey Followup-To: " followup-to "? ") t "\
7041You should normally obey the Followup-To: header.
7042
7043 `Followup-To: " followup-to "'
7044directs your response to " (if (string-match "," followup-to)
7045 "the specified newsgroups"
7046 "that newsgroup only") ".
7047
7048If a message is posted to several newsgroups, Followup-To is often
7049used to direct the following discussion to one newsgroup only,
7050because discussions that are spread over several newsgroup tend to
7051be fragmented and very difficult to follow.
7052
23f87bed 7053Also, some source/announcement newsgroups are not intended for discussion;
2fd561a9
MB
7054responses here are directed to other newsgroups.
7055
7056You may customize the variable `message-use-followup-to', if you
7057want to get rid of this query permanently."))
eec82323
LMI
7058 (cons 'Newsgroups followup-to)
7059 (cons 'Newsgroups newsgroups))))))
7060 (posted-to
7061 `((Newsgroups . ,posted-to)))
7062 (t
7063 `((Newsgroups . ,newsgroups))))
7064 ,@(and distribution (list (cons 'Distribution distribution)))
eec82323 7065 ,@(when (and mct
16409b0b
GM
7066 (not (or (equal (downcase mct) "never")
7067 (equal (downcase mct) "nobody"))))
7068 (list (cons 'Cc (if (or (equal (downcase mct) "always")
7069 (equal (downcase mct) "poster"))
23f87bed 7070 (or mrt reply-to from "")
eec82323
LMI
7071 mct)))))
7072
23f87bed 7073 cur)))
eec82323 7074
23f87bed
MB
7075(defun message-is-yours-p ()
7076 "Non-nil means current article is yours.
768e2c65 7077If you have added 'cancel-messages to `message-shoot-gnksa-feet', all articles
23f87bed 7078are yours except those that have Cancel-Lock header not belonging to you.
768e2c65 7079Instead of shooting GNKSA feet, you should modify `message-alternative-emails'
23f87bed
MB
7080regexp to match all of yours addresses."
7081 ;; Canlock-logic as suggested by Per Abrahamsen
7082 ;; <abraham@dina.kvl.dk>
7083 ;;
7084 ;; IF article has cancel-lock THEN
7085 ;; IF we can verify it THEN
7086 ;; issue cancel
7087 ;; ELSE
7088 ;; error: cancellock: article is not yours
7089 ;; ELSE
7090 ;; Use old rules, comparing sender...
7091 (save-excursion
7092 (save-restriction
7093 (message-narrow-to-head-1)
4dcb0d7a
LMI
7094 (if (and (message-fetch-field "Cancel-Lock")
7095 (message-gnksa-enable-p 'canlock-verify))
23f87bed
MB
7096 (if (null (canlock-verify))
7097 t
7098 (error "Failed to verify Cancel-lock: This article is not yours"))
7099 (let (sender from)
7100 (or
7101 (message-gnksa-enable-p 'cancel-messages)
7102 (and (setq sender (message-fetch-field "sender"))
7103 (string-equal (downcase sender)
7104 (downcase (message-make-sender))))
7105 ;; Email address in From field equals to our address
7106 (and (setq from (message-fetch-field "from"))
7107 (string-equal
01c52d31
MB
7108 (downcase (car (mail-header-parse-address from)))
7109 (downcase (car (mail-header-parse-address
7110 (message-make-from))))))
23f87bed
MB
7111 ;; Email address in From field matches
7112 ;; 'message-alternative-emails' regexp
7113 (and from
7114 message-alternative-emails
7115 (string-match
7116 message-alternative-emails
01c52d31 7117 (car (mail-header-parse-address from))))))))))
eec82323
LMI
7118
7119;;;###autoload
16409b0b
GM
7120(defun message-cancel-news (&optional arg)
7121 "Cancel an article you posted.
7122If ARG, allow editing of the cancellation message."
7123 (interactive "P")
eec82323
LMI
7124 (unless (message-news-p)
7125 (error "This is not a news article; canceling is impossible"))
23f87bed
MB
7126 (let (from newsgroups message-id distribution buf)
7127 (save-excursion
7128 ;; Get header info from original article.
7129 (save-restriction
7130 (message-narrow-to-head-1)
7131 (setq from (message-fetch-field "from")
7132 newsgroups (message-fetch-field "newsgroups")
7133 message-id (message-fetch-field "message-id" t)
7134 distribution (message-fetch-field "distribution")))
7135 ;; Make sure that this article was written by the user.
7136 (unless (message-is-yours-p)
7137 (error "This article is not yours"))
7138 (when (yes-or-no-p "Do you really want to cancel this article? ")
eec82323 7139 ;; Make control message.
16409b0b
GM
7140 (if arg
7141 (message-news)
7142 (setq buf (set-buffer (get-buffer-create " *message cancel*"))))
eec82323 7143 (erase-buffer)
f129a4df
CY
7144 (insert "Newsgroups: " newsgroups "\n"
7145 "From: " from "\n"
7146 "Subject: cmsg cancel " message-id "\n"
7147 "Control: cancel " message-id "\n"
eec82323 7148 (if distribution
f129a4df 7149 (concat "Distribution: " distribution "\n")
eec82323 7150 "")
f129a4df 7151 mail-header-separator "\n"
eec82323 7152 message-cancel-message)
16409b0b
GM
7153 (run-hooks 'message-cancel-hook)
7154 (unless arg
7155 (message "Canceling your article...")
7156 (if (let ((message-syntax-checks
7157 'dont-check-for-anything-just-trust-me))
7158 (funcall message-send-news-function))
7159 (message "Canceling your article...done"))
7160 (kill-buffer buf))))))
eec82323
LMI
7161
7162;;;###autoload
7163(defun message-supersede ()
7164 "Start composing a message to supersede the current message.
7165This is done simply by taking the old article and adding a Supersedes
7166header line with the old Message-ID."
7167 (interactive)
23f87bed 7168 (let ((cur (current-buffer)))
eec82323 7169 ;; Check whether the user owns the article that is to be superseded.
23f87bed 7170 (unless (message-is-yours-p)
eec82323
LMI
7171 (error "This article is not yours"))
7172 ;; Get a normal message buffer.
7173 (message-pop-to-buffer (message-buffer-name "supersede"))
7174 (insert-buffer-substring cur)
16409b0b 7175 (mime-to-mml)
158d6e07 7176 (message-narrow-to-head-1)
eec82323
LMI
7177 ;; Remove unwanted headers.
7178 (when message-ignored-supersedes-headers
7179 (message-remove-header message-ignored-supersedes-headers t))
7180 (goto-char (point-min))
7181 (if (not (re-search-forward "^Message-ID: " nil t))
7182 (error "No Message-ID in this article")
7183 (replace-match "Supersedes: " t t))
7184 (goto-char (point-max))
7185 (insert mail-header-separator)
7186 (widen)
7187 (forward-line 1)))
7188
7189;;;###autoload
7190(defun message-recover ()
7191 "Reread contents of current buffer from its last auto-save file."
7192 (interactive)
7193 (let ((file-name (make-auto-save-file-name)))
7194 (cond ((save-window-excursion
7c2fb837
DN
7195 (with-output-to-temp-buffer "*Directory*"
7196 (with-current-buffer standard-output
7197 (fundamental-mode)) ; for Emacs 20.4+
7198 (buffer-disable-undo standard-output)
7199 (let ((default-directory "/"))
7200 (call-process
7201 "ls" nil standard-output nil "-l" file-name)))
eec82323
LMI
7202 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
7203 (let ((buffer-read-only nil))
7204 (erase-buffer)
7205 (insert-file-contents file-name nil)))
7206 (t (error "message-recover cancelled")))))
7207
6748645f
LMI
7208;;; Washing Subject:
7209
7210(defun message-wash-subject (subject)
7d829636 7211 "Remove junk like \"Re:\", \"(fwd)\", etc. added to subject string SUBJECT.
40ba43b4 7212Previous forwarders, repliers, etc. may add it."
16409b0b 7213 (with-temp-buffer
47b63dfa 7214 (insert subject)
6748645f
LMI
7215 (goto-char (point-min))
7216 ;; strip Re/Fwd stuff off the beginning
7217 (while (re-search-forward
7218 "\\([Rr][Ee]:\\|[Ff][Ww][Dd]\\(\\[[0-9]*\\]\\)?:\\|[Ff][Ww]:\\)" nil t)
7219 (replace-match ""))
7220
7221 ;; and gnus-style forwards [foo@bar.com] subject
7222 (goto-char (point-min))
7223 (while (re-search-forward "\\[[^ \t]*\\(@\\|\\.\\)[^ \t]*\\]" nil t)
7224 (replace-match ""))
7225
7226 ;; and off the end
7227 (goto-char (point-max))
7228 (while (re-search-backward "([Ff][Ww][Dd])" nil t)
7229 (replace-match ""))
7230
7231 ;; and finally, any whitespace that was left-over
7232 (goto-char (point-min))
7233 (while (re-search-forward "^[ \t]+" nil t)
7234 (replace-match ""))
7235 (goto-char (point-max))
7236 (while (re-search-backward "[ \t]+$" nil t)
7237 (replace-match ""))
7238
7239 (buffer-string)))
7240
eec82323
LMI
7241;;; Forwarding messages.
7242
42b7180c
SZ
7243(defvar message-forward-decoded-p nil
7244 "Non-nil means the original message is decoded.")
7245
23f87bed
MB
7246(defun message-forward-subject-name-subject (subject)
7247 "Generate a SUBJECT for a forwarded message.
7248The form is: [Source] Subject, where if the original message was mail,
7249Source is the name of the sender, and if the original message was
7250news, Source is the list of newsgroups is was posted to."
7251 (let* ((group (message-fetch-field "newsgroups"))
7252 (from (message-fetch-field "from"))
7253 (prefix
7254 (if group
7255 (gnus-group-decoded-name group)
01c52d31
MB
7256 (or (and from (or
7257 (car (gnus-extract-address-components from))
7258 (cadr (gnus-extract-address-components from))))
23f87bed
MB
7259 "(nowhere)"))))
7260 (concat "["
7261 (if message-forward-decoded-p
7262 prefix
7263 (mail-decode-encoded-word-string prefix))
7264 "] " subject)))
7265
6748645f 7266(defun message-forward-subject-author-subject (subject)
7d829636 7267 "Generate a SUBJECT for a forwarded message.
6748645f
LMI
7268The form is: [Source] Subject, where if the original message was mail,
7269Source is the sender, and if the original message was news, Source is
7270the list of newsgroups is was posted to."
23f87bed
MB
7271 (let* ((group (message-fetch-field "newsgroups"))
7272 (prefix
7273 (if group
7274 (gnus-group-decoded-name group)
7275 (or (message-fetch-field "from")
7276 "(nowhere)"))))
7277 (concat "["
7278 (if message-forward-decoded-p
7279 prefix
7280 (mail-decode-encoded-word-string prefix))
7281 "] " subject)))
6748645f
LMI
7282
7283(defun message-forward-subject-fwd (subject)
7d829636 7284 "Generate a SUBJECT for a forwarded message.
6748645f
LMI
7285The form is: Fwd: Subject, where Subject is the original subject of
7286the message."
23f87bed
MB
7287 (if (string-match "^Fwd: " subject)
7288 subject
7289 (concat "Fwd: " subject)))
6748645f 7290
42b7180c 7291(defun message-make-forward-subject ()
eec82323
LMI
7292 "Return a Subject header suitable for the message in the current buffer."
7293 (save-excursion
7294 (save-restriction
158d6e07 7295 (message-narrow-to-head-1)
6748645f 7296 (let ((funcs message-make-forward-subject-function)
158d6e07
SZ
7297 (subject (message-fetch-field "Subject")))
7298 (setq subject
7299 (if subject
42b7180c 7300 (if message-forward-decoded-p
1653df0f
SZ
7301 subject
7302 (mail-decode-encoded-word-string subject))
158d6e07 7303 ""))
01c52d31
MB
7304 (when message-wash-forwarded-subjects
7305 (setq subject (message-wash-subject subject)))
6748645f
LMI
7306 ;; Make sure funcs is a list.
7307 (and funcs
7308 (not (listp funcs))
7309 (setq funcs (list funcs)))
7310 ;; Apply funcs in order, passing subject generated by previous
7311 ;; func to the next one.
01c52d31
MB
7312 (dolist (func funcs)
7313 (when (functionp func)
7314 (setq subject (funcall func subject))))
6748645f 7315 subject))))
eec82323 7316
9efa445f 7317(defvar gnus-article-decoded-p)
42b7180c 7318
0c773047 7319
eec82323 7320;;;###autoload
16409b0b 7321(defun message-forward (&optional news digest)
eec82323 7322 "Forward the current message via mail.
16409b0b
GM
7323Optional NEWS will use news to forward instead of mail.
7324Optional DIGEST will use digest to forward."
eec82323 7325 (interactive "P")
42b7180c 7326 (let* ((cur (current-buffer))
0c773047 7327 (message-forward-decoded-p
42b7180c 7328 (if (local-variable-p 'gnus-article-decoded-p (current-buffer))
0c773047 7329 gnus-article-decoded-p ;; In an article buffer.
42b7180c 7330 message-forward-decoded-p))
0c773047 7331 (subject (message-make-forward-subject)))
16409b0b
GM
7332 (if news
7333 (message-news nil subject)
7334 (message-mail nil subject))
0c773047
SZ
7335 (message-forward-make-body cur digest)))
7336
23f87bed
MB
7337(defun message-forward-make-body-plain (forward-buffer)
7338 (insert
f129a4df 7339 "\n-------------------- Start of forwarded message --------------------\n")
d8a7b272 7340 (let ((b (point))
8b320e66
KY
7341 (contents (with-current-buffer forward-buffer (buffer-string)))
7342 e)
7343 (unless (featurep 'xemacs)
706deb23 7344 (unless (mm-multibyte-string-p contents)
8b320e66
KY
7345 (error "Attempt to insert unibyte string from the buffer \"%s\"\
7346 to the multibyte buffer \"%s\""
7347 (if (bufferp forward-buffer)
7348 (buffer-name forward-buffer)
7349 forward-buffer)
7350 (buffer-name))))
7351 (insert (mm-with-multibyte-buffer
7352 (insert contents)
7353 (mime-to-mml)
7354 (goto-char (point-min))
7355 (when (looking-at "From ")
7356 (replace-match "X-From-Line: "))
7357 (buffer-string)))
7358 (unless (bolp) (insert "\n"))
23f87bed
MB
7359 (setq e (point))
7360 (insert
8b320e66 7361 "-------------------- End of forwarded message --------------------\n")
01c52d31
MB
7362 (message-remove-ignored-headers b e)))
7363
7364(defun message-remove-ignored-headers (b e)
7365 (when message-forward-ignored-headers
7366 (save-restriction
7367 (narrow-to-region b e)
7368 (goto-char b)
7369 (narrow-to-region (point)
7370 (or (search-forward "\n\n" nil t) (point)))
7371 (let ((ignored (if (stringp message-forward-ignored-headers)
7372 (list message-forward-ignored-headers)
7373 message-forward-ignored-headers)))
7374 (dolist (elem ignored)
7375 (message-remove-header elem t))))))
23f87bed 7376
4c0a6d4a 7377(defun message-forward-make-body-mime (forward-buffer &optional beg end)
01c52d31
MB
7378 (let ((b (point)))
7379 (insert "\n\n<#part type=message/rfc822 disposition=inline raw=t>\n")
23f87bed
MB
7380 (save-restriction
7381 (narrow-to-region (point) (point))
4c0a6d4a
RS
7382 (insert-buffer-substring forward-buffer beg end)
7383 (mml-quote-region (point-min) (point-max))
23f87bed
MB
7384 (goto-char (point-min))
7385 (when (looking-at "From ")
7386 (replace-match "X-From-Line: "))
7387 (goto-char (point-max)))
01c52d31
MB
7388 (insert "<#/part>\n")
7389 ;; Consider there is no illegible text.
7390 (add-text-properties
7391 b (point)
7392 `(no-illegible-text t rear-nonsticky t start-open t))))
23f87bed
MB
7393
7394(defun message-forward-make-body-mml (forward-buffer)
f129a4df 7395 (insert "\n\n<#mml type=message/rfc822 disposition=inline>\n")
23f87bed
MB
7396 (let ((b (point)) e)
7397 (if (not message-forward-decoded-p)
8b320e66
KY
7398 (let ((contents (with-current-buffer forward-buffer (buffer-string))))
7399 (unless (featurep 'xemacs)
706deb23 7400 (unless (mm-multibyte-string-p contents)
8b320e66
KY
7401 (error "Attempt to insert unibyte string from the buffer \"%s\"\
7402 to the multibyte buffer \"%s\""
7403 (if (bufferp forward-buffer)
7404 (buffer-name forward-buffer)
7405 forward-buffer)
7406 (buffer-name))))
7407 (insert (mm-with-multibyte-buffer
7408 (insert contents)
7409 (mime-to-mml)
7410 (goto-char (point-min))
7411 (when (looking-at "From ")
7412 (replace-match "X-From-Line: "))
7413 (buffer-string))))
23f87bed
MB
7414 (save-restriction
7415 (narrow-to-region (point) (point))
7416 (mml-insert-buffer forward-buffer)
7417 (goto-char (point-min))
7418 (when (looking-at "From ")
7419 (replace-match "X-From-Line: "))
7420 (goto-char (point-max))))
7421 (setq e (point))
f129a4df 7422 (insert "<#/mml>\n")
ad136a7c 7423 (when (and (not message-forward-decoded-p)
23f87bed 7424 message-forward-ignored-headers)
01c52d31 7425 (message-remove-ignored-headers b e))))
23f87bed
MB
7426
7427(defun message-forward-make-body-digest-plain (forward-buffer)
7428 (insert
f129a4df 7429 "\n-------------------- Start of forwarded message --------------------\n")
011f86c7
LI
7430 (mml-insert-buffer forward-buffer)
7431 (insert
7432 "\n-------------------- End of forwarded message --------------------\n"))
23f87bed
MB
7433
7434(defun message-forward-make-body-digest-mime (forward-buffer)
f129a4df 7435 (insert "\n<#multipart type=digest>\n")
23f87bed
MB
7436 (let ((b (point)) e)
7437 (insert-buffer-substring forward-buffer)
7438 (setq e (point))
f129a4df 7439 (insert "<#/multipart>\n")
23f87bed
MB
7440 (save-restriction
7441 (narrow-to-region b e)
7442 (goto-char b)
7443 (narrow-to-region (point)
7444 (or (search-forward "\n\n" nil t) (point)))
7445 (delete-region (point-min) (point-max)))))
7446
7447(defun message-forward-make-body-digest (forward-buffer)
7448 (if message-forward-as-mime
7449 (message-forward-make-body-digest-mime forward-buffer)
7450 (message-forward-make-body-digest-plain forward-buffer)))
7451
8abf1b22
GM
7452(autoload 'mm-uu-dissect-text-parts "mm-uu")
7453(autoload 'mm-uu-dissect "mm-uu")
01c52d31
MB
7454
7455(defun message-signed-or-encrypted-p (&optional dont-emulate-mime handles)
7456 "Say whether the current buffer contains signed or encrypted message.
7457If DONT-EMULATE-MIME is nil, this function does the MIME emulation on
7458messages that don't conform to PGP/MIME described in RFC2015. HANDLES
7459is for the internal use."
7460 (unless handles
7461 (let ((mm-decrypt-option 'never)
7462 (mm-verify-option 'never))
7463 (if (setq handles (mm-dissect-buffer nil t))
7464 (unless dont-emulate-mime
7465 (mm-uu-dissect-text-parts handles))
7466 (unless dont-emulate-mime
7467 (setq handles (mm-uu-dissect))))))
7468 ;; Check text/plain message in which there is a signed or encrypted
7469 ;; body that has been encoded by B or Q.
7470 (unless (or handles dont-emulate-mime)
7471 (let ((cur (current-buffer))
7472 (mm-decrypt-option 'never)
7473 (mm-verify-option 'never))
7474 (with-temp-buffer
7475 (insert-buffer-substring cur)
7476 (when (setq handles (mm-dissect-buffer t t))
f5b71bc0 7477 (if (and (bufferp (car handles))
01c52d31
MB
7478 (equal (mm-handle-media-type handles) "text/plain"))
7479 (progn
f5b71bc0
KY
7480 (erase-buffer)
7481 (insert-buffer-substring (car handles))
01c52d31
MB
7482 (mm-decode-content-transfer-encoding
7483 (mm-handle-encoding handles))
f5b71bc0 7484 (mm-destroy-parts handles)
01c52d31 7485 (setq handles (mm-uu-dissect)))
f5b71bc0 7486 (mm-destroy-parts handles)
01c52d31
MB
7487 (setq handles nil))))))
7488 (when handles
7489 (prog1
7490 (catch 'found
7491 (dolist (handle (if (stringp (car handles))
7492 (if (member (car handles)
7493 '("multipart/signed"
7494 "multipart/encrypted"))
7495 (throw 'found t)
7496 (cdr handles))
7497 (list handles)))
7498 (if (stringp (car handle))
7499 (when (message-signed-or-encrypted-p dont-emulate-mime handle)
7500 (throw 'found t))
7501 (when (and (bufferp (car handle))
7502 (equal (mm-handle-media-type handle)
7503 "message/rfc822"))
7504 (with-current-buffer (mm-handle-buffer handle)
7505 (when (message-signed-or-encrypted-p dont-emulate-mime)
7506 (throw 'found t)))))))
7507 (mm-destroy-parts handles))))
7508
0c773047
SZ
7509;;;###autoload
7510(defun message-forward-make-body (forward-buffer &optional digest)
7511 ;; Put point where we want it before inserting the forwarded
7512 ;; message.
7513 (if message-forward-before-signature
7514 (message-goto-body)
7515 (goto-char (point-max)))
23f87bed
MB
7516 (if digest
7517 (message-forward-make-body-digest forward-buffer)
0c773047 7518 (if message-forward-as-mime
23f87bed
MB
7519 (if (and message-forward-show-mml
7520 (not (and (eq message-forward-show-mml 'best)
01c52d31
MB
7521 ;; Use the raw form in the body if it contains
7522 ;; signed or encrypted message so as not to be
7523 ;; destroyed by re-encoding.
23f87bed 7524 (with-current-buffer forward-buffer
01c52d31
MB
7525 (condition-case nil
7526 (message-signed-or-encrypted-p)
7527 (error t))))))
23f87bed
MB
7528 (message-forward-make-body-mml forward-buffer)
7529 (message-forward-make-body-mime forward-buffer))
7530 (message-forward-make-body-plain forward-buffer)))
0c773047
SZ
7531 (message-position-point))
7532
c07dfdd9
GM
7533(declare-function rmail-toggle-header "rmail" (&optional arg))
7534
0c773047
SZ
7535;;;###autoload
7536(defun message-forward-rmail-make-body (forward-buffer)
7537 (save-window-excursion
7538 (set-buffer forward-buffer)
5ea0e32b 7539 (if (rmail-msg-is-pruned)
c07dfdd9
GM
7540 (if (fboundp 'rmail-msg-restore-non-pruned-header)
7541 (rmail-msg-restore-non-pruned-header) ; Emacs 22
7542 (rmail-toggle-header 0)))) ; Emacs 23
0c773047
SZ
7543 (message-forward-make-body forward-buffer))
7544
23f87bed 7545;; Fixme: Should have defcustom.
0c773047
SZ
7546;;;###autoload
7547(defun message-insinuate-rmail ()
23f87bed 7548 "Let RMAIL use message to forward."
0c773047
SZ
7549 (interactive)
7550 (setq rmail-enable-mime-composing t)
a1506d29 7551 (setq rmail-insert-mime-forwarded-message-function
0c773047 7552 'message-forward-rmail-make-body))
eec82323 7553
99c2a4e3
SM
7554(defvar message-inhibit-body-encoding nil)
7555
eec82323
LMI
7556;;;###autoload
7557(defun message-resend (address)
7558 "Resend the current article to ADDRESS."
16409b0b
GM
7559 (interactive
7560 (list (message-read-from-minibuffer "Resend message to: ")))
eec82323
LMI
7561 (message "Resending message to %s..." address)
7562 (save-excursion
7563 (let ((cur (current-buffer))
89b163db 7564 gcc beg)
eec82323 7565 ;; We first set up a normal mail buffer.
88818fbe
SZ
7566 (unless (message-mail-user-agent)
7567 (set-buffer (get-buffer-create " *message resend*"))
2b7feab0
LI
7568 (let ((inhibit-read-only t))
7569 (erase-buffer)))
23f87bed 7570 (let ((message-this-is-mail t)
01c52d31 7571 message-generate-hashcash
23f87bed 7572 message-setup-hook)
88818fbe 7573 (message-setup `((To . ,address))))
eec82323 7574 ;; Insert our usual headers.
23f87bed 7575 (message-generate-headers '(From Date To Message-ID))
eec82323 7576 (message-narrow-to-headers)
89b163db
G
7577 (when (setq gcc (mail-fetch-field "gcc" nil t))
7578 (message-remove-header "gcc"))
23f87bed
MB
7579 ;; Remove X-Draft-From header etc.
7580 (message-remove-header message-ignored-mail-headers t)
eec82323 7581 ;; Rename them all to "Resent-*".
23f87bed 7582 (goto-char (point-min))
eec82323
LMI
7583 (while (re-search-forward "^[A-Za-z]" nil t)
7584 (forward-char -1)
7585 (insert "Resent-"))
7586 (widen)
7587 (forward-line)
2b7feab0
LI
7588 (let ((inhibit-read-only t))
7589 (delete-region (point) (point-max)))
eec82323
LMI
7590 (setq beg (point))
7591 ;; Insert the message to be resent.
7592 (insert-buffer-substring cur)
7593 (goto-char (point-min))
7594 (search-forward "\n\n")
7595 (forward-char -1)
7596 (save-restriction
7597 (narrow-to-region beg (point))
7598 (message-remove-header message-ignored-resent-headers t)
7599 (goto-char (point-max)))
7600 (insert mail-header-separator)
7601 ;; Rename all old ("Also-")Resent headers.
6748645f 7602 (while (re-search-backward "^\\(Also-\\)*Resent-" beg t)
eec82323
LMI
7603 (beginning-of-line)
7604 (insert "Also-"))
7605 ;; Quote any "From " lines at the beginning.
7606 (goto-char beg)
7607 (when (looking-at "From ")
7608 (replace-match "X-From-Line: "))
7609 ;; Send it.
a7f6e5b9
LMI
7610 (let ((message-inhibit-body-encoding
7611 ;; Don't do any further encoding if it looks like the
7612 ;; message has already been encoded.
7613 (let ((case-fold-search t))
7614 (re-search-forward "^mime-version:" nil t)))
6b8382e4 7615 (message-inhibit-ecomplete t)
10ace8ea 7616 message-required-mail-headers
01c52d31 7617 message-generate-hashcash
10ace8ea 7618 rfc2047-encode-encoded-words)
16409b0b 7619 (message-send-mail))
89b163db
G
7620 (when gcc
7621 (message-goto-eoh)
7622 (insert "Gcc: " gcc "\n"))
7623 (run-hooks 'message-sent-hook)
eec82323
LMI
7624 (kill-buffer (current-buffer)))
7625 (message "Resending message to %s...done" address)))
7626
7627;;;###autoload
7628(defun message-bounce ()
7629 "Re-mail the current message.
16409b0b 7630This only makes sense if the current message is a bounce message that
eec82323
LMI
7631contains some mail you have written which has been bounced back to
7632you."
7633 (interactive)
16409b0b 7634 (let ((handles (mm-dissect-buffer t))
eec82323
LMI
7635 boundary)
7636 (message-pop-to-buffer (message-buffer-name "bounce"))
16409b0b
GM
7637 (if (stringp (car handles))
7638 ;; This is a MIME bounce.
7639 (mm-insert-part (car (last handles)))
7640 ;; This is a non-MIME bounce, so we try to remove things
7641 ;; manually.
7642 (mm-insert-part handles)
7643 (undo-boundary)
7644 (goto-char (point-min))
23f87bed
MB
7645 (re-search-forward "\n\n+" nil t)
7646 (setq boundary (point))
16409b0b 7647 ;; We remove everything before the bounced mail.
23f87bed
MB
7648 (if (or (re-search-forward message-unsent-separator nil t)
7649 (progn
7650 (search-forward "\n\n" nil 'move)
7651 (re-search-backward "^Return-Path:.*\n" boundary t)))
7652 (progn
7653 (forward-line 1)
7654 (delete-region (point-min)
7655 (if (re-search-forward "^[^ \n\t]+:" nil t)
7656 (match-beginning 0)
7657 (point))))
7658 (goto-char boundary)
7659 (when (re-search-backward "^.?From .*\n" nil t)
7660 (delete-region (match-beginning 0) (match-end 0)))))
2f62a044 7661 (mime-to-mml)
eec82323 7662 (save-restriction
158d6e07 7663 (message-narrow-to-head-1)
eec82323
LMI
7664 (message-remove-header message-ignored-bounced-headers t)
7665 (goto-char (point-max))
7666 (insert mail-header-separator))
7667 (message-position-point)))
7668
7669;;;
7670;;; Interactive entry points for new message buffers.
7671;;;
7672
7673;;;###autoload
7674(defun message-mail-other-window (&optional to subject)
7675 "Like `message-mail' command, but display mail buffer in another window."
7676 (interactive)
88818fbe 7677 (unless (message-mail-user-agent)
37ac18a3
CY
7678 (message-pop-to-buffer (message-buffer-name "mail" to)
7679 'switch-to-buffer-other-window))
6748645f 7680 (let ((message-this-is-mail t))
88818fbe 7681 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))
815b81c8 7682 nil nil nil 'switch-to-buffer-other-window)))
eec82323
LMI
7683
7684;;;###autoload
7685(defun message-mail-other-frame (&optional to subject)
7686 "Like `message-mail' command, but display mail buffer in another frame."
7687 (interactive)
88818fbe 7688 (unless (message-mail-user-agent)
37ac18a3
CY
7689 (message-pop-to-buffer (message-buffer-name "mail" to)
7690 'switch-to-buffer-other-frame))
6748645f 7691 (let ((message-this-is-mail t))
88818fbe 7692 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))
815b81c8 7693 nil nil nil 'switch-to-buffer-other-frame)))
eec82323
LMI
7694
7695;;;###autoload
7696(defun message-news-other-window (&optional newsgroups subject)
7697 "Start editing a news article to be sent."
7698 (interactive)
37ac18a3
CY
7699 (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups)
7700 'switch-to-buffer-other-window)
6748645f
LMI
7701 (let ((message-this-is-news t))
7702 (message-setup `((Newsgroups . ,(or newsgroups ""))
7703 (Subject . ,(or subject ""))))))
eec82323
LMI
7704
7705;;;###autoload
7706(defun message-news-other-frame (&optional newsgroups subject)
7707 "Start editing a news article to be sent."
7708 (interactive)
37ac18a3
CY
7709 (message-pop-to-buffer (message-buffer-name "posting" nil newsgroups)
7710 'switch-to-buffer-other-frame)
6748645f
LMI
7711 (let ((message-this-is-news t))
7712 (message-setup `((Newsgroups . ,(or newsgroups ""))
7713 (Subject . ,(or subject ""))))))
eec82323
LMI
7714
7715;;; underline.el
7716
7717;; This code should be moved to underline.el (from which it is stolen).
7718
7719;;;###autoload
01c52d31 7720(defun message-bold-region (start end)
eec82323
LMI
7721 "Bold all nonblank characters in the region.
7722Works by overstriking characters.
7723Called from program, takes two arguments START and END
7724which specify the range to operate on."
7725 (interactive "r")
7726 (save-excursion
7727 (let ((end1 (make-marker)))
7728 (move-marker end1 (max start end))
7729 (goto-char (min start end))
7730 (while (< (point) end1)
7731 (or (looking-at "[_\^@- ]")
16409b0b 7732 (insert (char-after) "\b"))
eec82323
LMI
7733 (forward-char 1)))))
7734
7735;;;###autoload
01c52d31 7736(defun message-unbold-region (start end)
eec82323
LMI
7737 "Remove all boldness (overstruck characters) in the region.
7738Called from program, takes two arguments START and END
7739which specify the range to operate on."
7740 (interactive "r")
7741 (save-excursion
7742 (let ((end1 (make-marker)))
7743 (move-marker end1 (max start end))
7744 (goto-char (min start end))
01c52d31 7745 (while (search-forward "\b" end1 t)
16409b0b 7746 (if (eq (char-after) (char-after (- (point) 2)))
eec82323
LMI
7747 (delete-char -2))))))
7748
23f87bed
MB
7749(defun message-exchange-point-and-mark ()
7750 "Exchange point and mark, but don't activate region if it was inactive."
61f49e0b
CY
7751 (goto-char (prog1 (mark t)
7752 (set-marker (mark-marker) (point)))))
23f87bed
MB
7753
7754(defalias 'message-make-overlay 'make-overlay)
7755(defalias 'message-delete-overlay 'delete-overlay)
7756(defalias 'message-overlay-put 'overlay-put)
7757(defun message-kill-all-overlays ()
7758 (if (featurep 'xemacs)
7759 (map-extents (lambda (extent ignore) (delete-extent extent)))
7760 (mapcar #'delete-overlay (overlays-in (point-min) (point-max)))))
eec82323
LMI
7761
7762;; Support for toolbar
9efa445f 7763(defvar tool-bar-mode)
23f87bed 7764
18c06a99
RS
7765;; Note: The :set function in the `message-tool-bar*' variables will only
7766;; affect _new_ message buffers. We might add a function that walks thru all
7767;; message-mode buffers and force the update.
7768(defun message-tool-bar-update (&optional symbol value)
7769 "Update message mode toolbar.
7770Setter function for custom variables."
7771 (setq-default message-tool-bar-map nil)
7772 (when symbol
7773 ;; When used as ":set" function:
7774 (set-default symbol value)))
7775
7776(defcustom message-tool-bar (if (eq gmm-tool-bar-style 'gnome)
7777 'message-tool-bar-gnome
7778 'message-tool-bar-retro)
7779 "Specifies the message mode tool bar.
7780
fac916bf 7781It can be either a list or a symbol referring to a list. See
18c06a99
RS
7782`gmm-tool-bar-from-list' for the format of the list. The
7783default key map is `message-mode-map'.
7784
7785Pre-defined symbols include `message-tool-bar-gnome' and
7786`message-tool-bar-retro'."
7787 :type '(repeat gmm-tool-bar-list-item)
7788 :type '(choice (const :tag "GNOME style" message-tool-bar-gnome)
7789 (const :tag "Retro look" message-tool-bar-retro)
7790 (repeat :tag "User defined list" gmm-tool-bar-item)
7791 (symbol))
330f707b 7792 :version "23.1" ;; No Gnus
18c06a99
RS
7793 :initialize 'custom-initialize-default
7794 :set 'message-tool-bar-update
7795 :group 'message)
7796
7797(defcustom message-tool-bar-gnome
7798 '((ispell-message "spell" nil
25ca2e61 7799 :vert-only t
18c06a99
RS
7800 :visible (or (not (boundp 'flyspell-mode))
7801 (not flyspell-mode)))
7802 (flyspell-buffer "spell" t
25ca2e61 7803 :vert-only t
18c06a99
RS
7804 :visible (and (boundp 'flyspell-mode)
7805 flyspell-mode)
7806 :help "Flyspell whole buffer")
25ca2e61 7807 (message-send-and-exit "mail/send" t :label "Send")
18c06a99 7808 (message-dont-send "mail/save-draft")
25ca2e61 7809 (mml-attach-file "attach" mml-mode-map :vert-only t)
18c06a99 7810 (mml-preview "mail/preview" mml-mode-map)
01c52d31 7811 (mml-secure-message-sign-encrypt "lock" mml-mode-map :visible nil)
18c06a99
RS
7812 (message-insert-importance-high "important" nil :visible nil)
7813 (message-insert-importance-low "unimportant" nil :visible nil)
25ca2e61 7814 (message-insert-disposition-notification-to "receipt" nil :visible nil))
18c06a99
RS
7815 "List of items for the message tool bar (GNOME style).
7816
7817See `gmm-tool-bar-from-list' for details on the format of the list."
7818 :type '(repeat gmm-tool-bar-item)
330f707b 7819 :version "23.1" ;; No Gnus
18c06a99
RS
7820 :initialize 'custom-initialize-default
7821 :set 'message-tool-bar-update
7822 :group 'message)
7823
7824(defcustom message-tool-bar-retro
7825 '(;; Old Emacs 21 icon for consistency.
77fb0e18 7826 (message-send-and-exit "gnus/mail-send")
18c06a99
RS
7827 (message-kill-buffer "close")
7828 (message-dont-send "cancel")
7829 (mml-attach-file "attach" mml-mode-map)
7830 (ispell-message "spell")
7831 (mml-preview "preview" mml-mode-map)
7832 (message-insert-importance-high "gnus/important")
7833 (message-insert-importance-low "gnus/unimportant")
7834 (message-insert-disposition-notification-to "gnus/receipt"))
7835 "List of items for the message tool bar (retro style).
7836
7837See `gmm-tool-bar-from-list' for details on the format of the list."
7838 :type '(repeat gmm-tool-bar-item)
330f707b 7839 :version "23.1" ;; No Gnus
18c06a99
RS
7840 :initialize 'custom-initialize-default
7841 :set 'message-tool-bar-update
7842 :group 'message)
7843
7844(defcustom message-tool-bar-zap-list
7845 '(new-file open-file dired kill-buffer write-file
7846 print-buffer customize help)
7847 "List of icon items from the global tool bar.
7848These items are not displayed on the message mode tool bar.
7849
7850See `gmm-tool-bar-from-list' for the format of the list."
7851 :type 'gmm-tool-bar-zap-list
330f707b 7852 :version "23.1" ;; No Gnus
18c06a99
RS
7853 :initialize 'custom-initialize-default
7854 :set 'message-tool-bar-update
7855 :group 'message)
7856
7857(defvar image-load-path)
7858
7859(defun message-make-tool-bar (&optional force)
7860 "Make a message mode tool bar from `message-tool-bar-list'.
7861When FORCE, rebuild the tool bar."
7862 (when (and (not (featurep 'xemacs))
7863 (boundp 'tool-bar-mode)
23f87bed 7864 tool-bar-mode
18c06a99
RS
7865 (or (not message-tool-bar-map) force))
7866 (setq message-tool-bar-map
7867 (let* ((load-path
7868 (gmm-image-load-path-for-library "message"
7869 "mail/save-draft.xpm"
7870 nil t))
7871 (image-load-path (cons (car load-path)
7872 (when (boundp 'image-load-path)
7873 image-load-path))))
7874 (gmm-tool-bar-from-list message-tool-bar
7875 message-tool-bar-zap-list
7876 'message-mode-map))))
7877 message-tool-bar-map)
eec82323
LMI
7878
7879;;; Group name completion.
7880
23f87bed 7881(defcustom message-newgroups-header-regexp
eec82323 7882 "^\\(Newsgroups\\|Followup-To\\|Posted-To\\|Gcc\\):"
23f87bed
MB
7883 "Regexp that match headers that lists groups."
7884 :group 'message
7885 :type 'regexp)
7886
7887(defcustom message-completion-alist
7888 (list (cons message-newgroups-header-regexp 'message-expand-group)
7889 '("^\\(Resent-\\)?\\(To\\|B?Cc\\):" . message-expand-name)
7890 '("^\\(Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\):"
7891 . message-expand-name)
7892 '("^\\(Disposition-Notification-To\\|Return-Receipt-To\\):"
7893 . message-expand-name))
7894 "Alist of (RE . FUN). Use FUN for completion on header lines matching RE."
bf247b6e 7895 :version "22.1"
23f87bed
MB
7896 :group 'message
7897 :type '(alist :key-type regexp :value-type function))
7898
01c52d31 7899(defcustom message-expand-name-databases
674c5ccf 7900 '(bbdb eudc)
01c52d31
MB
7901 "List of databases to try for name completion (`message-expand-name').
7902Each element is a symbol and can be `bbdb' or `eudc'."
7903 :group 'message
7904 :type '(set (const bbdb) (const eudc)))
7905
23f87bed
MB
7906(defcustom message-tab-body-function nil
7907 "*Function to execute when `message-tab' (TAB) is executed in the body.
7908If nil, the function bound in `text-mode-map' or `global-map' is executed."
bf247b6e 7909 :version "22.1"
23f87bed
MB
7910 :group 'message
7911 :link '(custom-manual "(message)Various Commands")
4a2358e9
MB
7912 :type '(choice (const nil)
7913 function))
eec82323 7914
b43906b6
GM
7915(declare-function mail-abbrev-in-expansion-header-p "mailabbrev" ())
7916
eec82323 7917(defun message-tab ()
23f87bed
MB
7918 "Complete names according to `message-completion-alist'.
7919Execute function specified by `message-tab-body-function' when not in
7920those headers."
eec82323 7921 (interactive)
674c5ccf
SM
7922 (cond
7923 ((if (and (boundp 'completion-fail-discreetly)
7924 (fboundp 'completion-at-point))
7925 (let ((completion-fail-discreetly t)) (completion-at-point))
7926 (funcall (or (message-completion-function) #'ignore)))
7927 ;; Completion was performed; nothing else to do.
7928 nil)
7929 (message-tab-body-function (funcall message-tab-body-function))
7930 (t (funcall (or (lookup-key text-mode-map "\t")
7931 (lookup-key global-map "\t")
7932 'indent-relative)))))
7933
99c2a4e3
SM
7934(defvar mail-abbrev-mode-regexp)
7935
674c5ccf 7936(defun message-completion-function ()
23f87bed
MB
7937 (let ((alist message-completion-alist))
7938 (while (and alist
7939 (let ((mail-abbrev-mode-regexp (caar alist)))
7940 (not (mail-abbrev-in-expansion-header-p))))
7941 (setq alist (cdr alist)))
6e724ca2
SM
7942 (when (cdar alist)
7943 (lexical-let ((fun (cdar alist)))
7944 ;; Even if completion fails, return a non-nil value, so as to avoid
7945 ;; falling back to message-tab-body-function.
7946 (lambda () (funcall fun) 'completion-attempted)))))
eec82323 7947
3b059693
MB
7948(eval-and-compile
7949 (condition-case nil
7950 (with-temp-buffer
7951 (let ((standard-output (current-buffer)))
7952 (eval '(display-completion-list nil "")))
7953 (defalias 'message-display-completion-list 'display-completion-list))
7954 (error ;; Don't use `wrong-number-of-arguments' here because of XEmacs.
7955 (defun message-display-completion-list (completions &optional ignore)
7956 "Display the list of completions, COMPLETIONS, using `standard-output'."
7957 (display-completion-list completions)))))
7958
eec82323 7959(defun message-expand-group ()
6748645f 7960 "Expand the group name under point."
eec82323
LMI
7961 (let* ((b (save-excursion
7962 (save-restriction
7963 (narrow-to-region
7964 (save-excursion
7965 (beginning-of-line)
7966 (skip-chars-forward "^:")
7967 (1+ (point)))
7968 (point))
7969 (skip-chars-backward "^, \t\n") (point))))
7970 (completion-ignore-case t)
96bdcdc4
SM
7971 (e (progn (skip-chars-forward "^,\t\n ") (point)))
7972 (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb)))
7973 (message-completion-in-region e b hashtb)))
7974
7975(defalias 'message-completion-in-region
7976 (if (fboundp 'completion-in-region)
7977 'completion-in-region
7978 (lambda (e b hashtb)
7979 (let* ((string (buffer-substring b e))
7980 (completions (all-completions string hashtb))
7981 comp)
7982 (delete-region b (point))
7983 (cond
7984 ((= (length completions) 1)
7985 (if (string= (car completions) string)
7986 (progn
7987 (insert string)
7988 (message "Only matching group"))
7989 (insert (car completions))))
7990 ((and (setq comp (try-completion string hashtb))
7991 (not (string= comp string)))
7992 (insert comp))
7993 (t
7994 (insert string)
7995 (if (not comp)
7996 (message "No matching groups")
7997 (save-selected-window
7998 (pop-to-buffer "*Completions*")
7999 (buffer-disable-undo)
8000 (let ((buffer-read-only nil))
8001 (erase-buffer)
8002 (let ((standard-output (current-buffer)))
8003 (message-display-completion-list (sort completions 'string<)
8004 string))
8005 (setq buffer-read-only nil)
8006 (goto-char (point-min))
8007 (delete-region (point)
8008 (progn (forward-line 3) (point))))))))))))
eec82323 8009
23f87bed 8010(defun message-expand-name ()
01c52d31
MB
8011 (cond ((and (memq 'eudc message-expand-name-databases)
8012 (boundp 'eudc-protocol)
8013 eudc-protocol)
8014 (eudc-expand-inline))
8015 ((and (memq 'bbdb message-expand-name-databases)
8016 (fboundp 'bbdb-complete-name))
99c2a4e3
SM
8017 (let ((starttick (buffer-modified-tick)))
8018 (or (bbdb-complete-name)
8019 ;; Apparently, bbdb-complete-name can return nil even when
8020 ;; completion took place. So let's double check the buffer was
8021 ;; not modified.
8022 (/= starttick (buffer-modified-tick)))))
01c52d31
MB
8023 (t
8024 (expand-abbrev))))
23f87bed 8025
eec82323
LMI
8026;;; Help stuff.
8027
8028(defun message-talkative-question (ask question show &rest text)
8df72730
RS
8029 "Call FUNCTION with argument QUESTION; optionally display TEXT... args.
8030If SHOW is non-nil, the arguments TEXT... are displayed in a temp buffer.
eec82323
LMI
8031The following arguments may contain lists of values."
8032 (if (and show
8033 (setq text (message-flatten-list text)))
8034 (save-window-excursion
82816ba1
SM
8035 (with-output-to-temp-buffer " *MESSAGE information message*"
8036 (with-current-buffer " *MESSAGE information message*"
16409b0b 8037 (fundamental-mode) ; for Emacs 20.4+
01c52d31 8038 (mapc 'princ text)
eec82323
LMI
8039 (goto-char (point-min))))
8040 (funcall ask question))
8041 (funcall ask question)))
8042
8043(defun message-flatten-list (list)
8044 "Return a new, flat list that contains all elements of LIST.
8045
8046\(message-flatten-list '(1 (2 3 (4 5 (6))) 7))
8047=> (1 2 3 4 5 6 7)"
8048 (cond ((consp list)
8049 (apply 'append (mapcar 'message-flatten-list list)))
8050 (list
8051 (list list))))
8052
8053(defun message-generate-new-buffer-clone-locals (name &optional varstr)
23f87bed 8054 "Create and return a buffer with name based on NAME using `generate-new-buffer'.
eec82323
LMI
8055Then clone the local variables and values from the old buffer to the
8056new one, cloning only the locals having a substring matching the
23f87bed 8057regexp VARSTR."
a8151ef7 8058 (let ((oldbuf (current-buffer)))
82816ba1 8059 (with-current-buffer (generate-new-buffer name)
16409b0b 8060 (message-clone-locals oldbuf varstr)
eec82323
LMI
8061 (current-buffer))))
8062
16409b0b 8063(defun message-clone-locals (buffer &optional varstr)
a8151ef7 8064 "Clone the local variables from BUFFER to the current buffer."
82816ba1 8065 (let ((locals (with-current-buffer buffer (buffer-local-variables)))
f4dd4ae8 8066 (regexp "^gnus\\|^nn\\|^message\\|^sendmail\\|^smtp\\|^user-mail-address"))
a8151ef7
LMI
8067 (mapcar
8068 (lambda (local)
6748645f
LMI
8069 (when (and (consp local)
8070 (car local)
16409b0b
GM
8071 (string-match regexp (symbol-name (car local)))
8072 (or (null varstr)
8073 (string-match varstr (symbol-name (car local)))))
a8151ef7
LMI
8074 (ignore-errors
8075 (set (make-local-variable (car local))
8076 (cdr local)))))
8077 locals)))
8078
16409b0b
GM
8079;;;
8080;;; MIME functions
8081;;;
8082
16409b0b 8083(defun message-encode-message-body ()
7d829636 8084 (unless message-inhibit-body-encoding
16409b0b
GM
8085 (let ((mail-parse-charset (or mail-parse-charset
8086 message-default-charset))
8087 (case-fold-search t)
8088 lines content-type-p)
8089 (message-goto-body)
8090 (save-restriction
8091 (narrow-to-region (point) (point-max))
8092 (let ((new (mml-generate-mime)))
8093 (when new
8094 (delete-region (point-min) (point-max))
8095 (insert new)
8096 (goto-char (point-min))
8097 (if (eq (aref new 0) ?\n)
8098 (delete-char 1)
8099 (search-forward "\n\n")
8100 (setq lines (buffer-substring (point-min) (1- (point))))
8101 (delete-region (point-min) (point))))))
8102 (save-restriction
8103 (message-narrow-to-headers-or-head)
8104 (message-remove-header "Mime-Version")
8105 (goto-char (point-max))
f129a4df 8106 (insert "MIME-Version: 1.0\n")
16409b0b
GM
8107 (when lines
8108 (insert lines))
8109 (setq content-type-p
23f87bed
MB
8110 (or mml-boundary
8111 (re-search-backward "^Content-Type:" nil t))))
16409b0b
GM
8112 (save-restriction
8113 (message-narrow-to-headers-or-head)
8114 (message-remove-first-header "Content-Type")
8115 (message-remove-first-header "Content-Transfer-Encoding"))
23f87bed
MB
8116 ;; We always make sure that the message has a Content-Type
8117 ;; header. This is because some broken MTAs and MUAs get
8118 ;; awfully confused when confronted with a message with a
8119 ;; MIME-Version header and without a Content-Type header. For
8120 ;; instance, Solaris' /usr/bin/mail.
16409b0b
GM
8121 (unless content-type-p
8122 (goto-char (point-min))
0c773047
SZ
8123 ;; For unknown reason, MIME-Version doesn't exist.
8124 (when (re-search-forward "^MIME-Version:" nil t)
8125 (forward-line 1)
f129a4df 8126 (insert "Content-Type: text/plain; charset=us-ascii\n"))))))
16409b0b 8127
23f87bed 8128(defun message-read-from-minibuffer (prompt &optional initial-contents)
16409b0b
GM
8129 "Read from the minibuffer while providing abbrev expansion."
8130 (if (fboundp 'mail-abbrevs-setup)
d5fdf93f 8131 (let ((minibuffer-setup-hook 'mail-abbrevs-setup)
066f0e09 8132 (minibuffer-local-map message-minibuffer-local-map))
9ad948e2 8133 (read-from-minibuffer prompt initial-contents))
23f87bed
MB
8134 (let ((minibuffer-setup-hook 'mail-abbrev-minibuffer-setup-hook)
8135 (minibuffer-local-map message-minibuffer-local-map))
8136 (read-string prompt initial-contents))))
eec82323 8137
83595c0c 8138(defun message-use-alternative-email-as-from ()
46e8fe3d
MB
8139 "Set From field of the outgoing message to the first matching
8140address in `message-alternative-emails', looking at To, Cc and
8141From headers in the original article."
83595c0c 8142 (require 'mail-utils)
01c52d31 8143 (let* ((fields '("To" "Cc" "From"))
83595c0c
DL
8144 (emails
8145 (split-string
8146 (mail-strip-quoted-names
8147 (mapconcat 'message-fetch-reply-field fields ","))
8148 "[ \f\t\n\r\v,]+"))
8149 email)
8150 (while emails
8151 (if (string-match message-alternative-emails (car emails))
8152 (setq email (car emails)
8153 emails nil))
8154 (pop emails))
8155 (unless (or (not email) (equal email user-mail-address))
46e8fe3d 8156 (message-remove-header "From")
83595c0c 8157 (goto-char (point-max))
01c52d31
MB
8158 (insert "From: " (let ((user-mail-address email)) (message-make-from))
8159 "\n"))))
83595c0c 8160
23f87bed
MB
8161(defun message-options-get (symbol)
8162 (cdr (assq symbol message-options)))
8163
8164(defun message-options-set (symbol value)
8165 (let ((the-cons (assq symbol message-options)))
8166 (if the-cons
8167 (if value
8168 (setcdr the-cons value)
8169 (setq message-options (delq the-cons message-options)))
8170 (and value
8171 (push (cons symbol value) message-options))))
8172 value)
8173
8174(defun message-options-set-recipient ()
8175 (save-restriction
8176 (message-narrow-to-headers-or-head)
8177 (message-options-set 'message-sender
8178 (mail-strip-quoted-names
8179 (message-fetch-field "from")))
8180 (message-options-set 'message-recipients
8181 (mail-strip-quoted-names
8182 (let ((to (message-fetch-field "to"))
8183 (cc (message-fetch-field "cc"))
8184 (bcc (message-fetch-field "bcc")))
8185 (concat
8186 (or to "")
8187 (if (and to cc) ", ")
8188 (or cc "")
8189 (if (and (or to cc) bcc) ", ")
8190 (or bcc "")))))))
8191
8192(defun message-hide-headers ()
8193 "Hide headers based on the `message-hidden-headers' variable."
8194 (let ((regexps (if (stringp message-hidden-headers)
8195 (list message-hidden-headers)
8196 message-hidden-headers))
8197 (inhibit-point-motion-hooks t)
01c52d31 8198 (after-change-functions nil)
c70dbcd5 8199 (end-of-headers (point-min)))
23f87bed
MB
8200 (when regexps
8201 (save-excursion
8202 (save-restriction
8203 (message-narrow-to-headers)
8204 (goto-char (point-min))
8205 (while (not (eobp))
8206 (if (not (message-hide-header-p regexps))
8207 (message-next-header)
01c52d31
MB
8208 (let ((begin (point))
8209 header header-len)
23f87bed 8210 (message-next-header)
01c52d31
MB
8211 (setq header (buffer-substring begin (point))
8212 header-len (- (point) begin))
8213 (delete-region begin (point))
c70dbcd5 8214 (goto-char end-of-headers)
01c52d31
MB
8215 (insert header)
8216 (setq end-of-headers
8217 (+ end-of-headers header-len))))))))
c70dbcd5 8218 (narrow-to-region end-of-headers (point-max))))
23f87bed
MB
8219
8220(defun message-hide-header-p (regexps)
8221 (let ((result nil)
8222 (reverse nil))
8223 (when (eq (car regexps) 'not)
8224 (setq reverse t)
8225 (pop regexps))
8226 (dolist (regexp regexps)
8227 (setq result (or result (looking-at regexp))))
8228 (if reverse
8229 (not result)
8230 result)))
8231
aa8f8277
GM
8232(declare-function ecomplete-add-item "ecomplete" (type key text))
8233(declare-function ecomplete-save "ecomplete" ())
8234
01c52d31 8235(defun message-put-addresses-in-ecomplete ()
aa8f8277 8236 (require 'ecomplete)
01c52d31
MB
8237 (dolist (header '("to" "cc" "from" "reply-to"))
8238 (let ((value (message-field-value header)))
8239 (dolist (string (mail-header-parse-addresses value 'raw))
8240 (setq string
8241 (gnus-replace-in-string
8242 (gnus-replace-in-string string "^ +\\| +$" "") "\n" ""))
8243 (ecomplete-add-item 'mail (car (mail-header-parse-address string))
8244 string))))
8245 (ecomplete-save))
8246
aa8f8277
GM
8247(autoload 'ecomplete-display-matches "ecomplete")
8248
01c52d31
MB
8249(defun message-display-abbrev (&optional choose)
8250 "Display the next possible abbrev for the text before point."
8251 (interactive (list t))
8252 (when (and (memq (char-after (point-at-bol)) '(?C ?T ?\t ? ))
8253 (message-point-in-header-p)
8254 (save-excursion
8255 (beginning-of-line)
8256 (while (and (memq (char-after) '(?\t ? ))
8257 (zerop (forward-line -1))))
8258 (looking-at "To:\\|Cc:")))
8259 (let* ((end (point))
8260 (start (save-excursion
8261 (and (re-search-backward "[\n\t ]" nil t)
8262 (1+ (point)))))
8263 (word (when start (buffer-substring start end)))
8264 (match (when (and word
8265 (not (zerop (length word))))
8266 (ecomplete-display-matches 'mail word choose))))
8267 (when (and choose match)
8268 (delete-region start end)
8269 (insert match)))))
8270
8f7abae3
MB
8271;; To send pre-formatted letters like the example below, you can use
8272;; `message-send-form-letter':
8273;; --8<---------------cut here---------------start------------->8---
8274;; To: alice@invalid.invalid
8275;; Subject: Verification of your contact information
8276;; From: Contact verification <admin@foo.invalid>
8277;; --text follows this line--
8278;; Hi Alice,
8279;; please verify that your contact information is still valid:
8280;; Alice A, A avenue 11, 1111 A town, Austria
8281;; ----------next form letter message follows this line----------
8282;; To: bob@invalid.invalid
8283;; Subject: Verification of your contact information
8284;; From: Contact verification <admin@foo.invalid>
8285;; --text follows this line--
8286;; Hi Bob,
8287;; please verify that your contact information is still valid:
8288;; Bob, B street 22, 22222 Be town, Belgium
8289;; ----------next form letter message follows this line----------
8290;; To: charlie@invalid.invalid
8291;; Subject: Verification of your contact information
8292;; From: Contact verification <admin@foo.invalid>
8293;; --text follows this line--
8294;; Hi Charlie,
8295;; please verify that your contact information is still valid:
8296;; Charlie Chaplin, C plaza 33, 33333 C town, Chile
8297;; --8<---------------cut here---------------end--------------->8---
8298
8299;; FIXME: What is the most common term (circular letter, form letter, serial
8300;; letter, standard letter) for such kind of letter? See also
8301;; <http://en.wikipedia.org/wiki/Form_letter>
8302
8303;; FIXME: Maybe extent message-mode's font-lock support to recognize
8304;; `message-form-letter-separator', i.e. highlight each message like a single
8305;; message.
8306
8307(defcustom message-form-letter-separator
8308 "\n----------next form letter message follows this line----------\n"
8309 "Separator for `message-send-form-letter'."
8310 ;; :group 'message-form-letter
8311 :group 'message-various
8312 :version "23.1" ;; No Gnus
8313 :type 'string)
8314
8315(defcustom message-send-form-letter-delay 1
8316 "Delay in seconds when sending a message with `message-send-form-letter'.
8317Only used when `message-send-form-letter' is called with non-nil
8318argument `force'."
8319 ;; :group 'message-form-letter
8320 :group 'message-various
8321 :version "23.1" ;; No Gnus
8322 :type 'integer)
8323
8324(defun message-send-form-letter (&optional force)
8325 "Sent all form letter messages from current buffer.
8326Unless FORCE, prompt before sending.
8327
8328The messages are separated by `message-form-letter-separator'.
8329Header and body are separated by `mail-header-separator'."
8330 (interactive "P")
8331 (let ((sent 0) (skipped 0)
8332 start end text
8333 buff
8334 to done)
8335 (goto-char (point-min))
8336 (while (not done)
8337 (setq start (point)
8338 end (if (search-forward message-form-letter-separator nil t)
8339 (- (point) (length message-form-letter-separator) -1)
8340 (setq done t)
8341 (point-max)))
8342 (setq text
8343 (buffer-substring-no-properties start end))
8344 (setq buff (generate-new-buffer "*mail - form letter*"))
8345 (with-current-buffer buff
8346 (insert text)
8347 (message-mode)
8348 (setq to (message-fetch-field "To"))
8349 (switch-to-buffer buff)
8350 (when force
8351 (sit-for message-send-form-letter-delay))
8352 (if (or force
8353 (y-or-n-p (format "Send message to `%s'? " to)))
8354 (progn
8355 (setq sent (1+ sent))
8356 (message-send-and-exit))
8357 (message (format "Message to `%s' skipped." to))
8358 (setq skipped (1+ skipped)))
8359 (when (buffer-live-p buff)
8360 (kill-buffer buff))))
8361 (message "%s message(s) sent, %s skipped." sent skipped)))
8362
9b3ebcb6
MB
8363(defun message-replace-header (header new-value &optional after force)
8364 "Remove HEADER and insert the NEW-VALUE.
8365If AFTER, insert after this header. If FORCE, insert new field
8366even if NEW-VALUE is empty."
8367 ;; Similar to `nnheader-replace-header' but for message buffers.
8368 (save-excursion
8369 (save-restriction
8370 (message-narrow-to-headers)
8371 (message-remove-header header))
8372 (when (or force (> (length new-value) 0))
8373 (if after
8374 (message-position-on-field header after)
8375 (message-position-on-field header))
8376 (insert new-value))))
8377
8378(defcustom message-recipients-without-full-name
8379 (list "ding@gnus.org"
8380 "bugs@gnus.org"
8381 "emacs-devel@gnu.org"
8382 "emacs-pretest-bug@gnu.org"
8383 "bug-gnu-emacs@gnu.org")
8384 "Mail addresses that have no full name.
8385Used in `message-simplify-recipients'."
8386 ;; Maybe the addresses could be extracted from
8387 ;; `gnus-parameter-to-list-alist'?
8388 :type '(choice (const :tag "None" nil)
8389 (repeat string))
8390 :version "23.1" ;; No Gnus
8391 :group 'message-headers)
8392
8393(defun message-simplify-recipients ()
8394 (interactive)
8395 (dolist (hdr '("Cc" "To"))
8396 (message-replace-header
8397 hdr
8398 (mapconcat
8399 (lambda (addrcomp)
8400 (if (and message-recipients-without-full-name
8401 (string-match
8402 (regexp-opt message-recipients-without-full-name)
8403 (cadr addrcomp)))
8404 (cadr addrcomp)
8405 (if (car addrcomp)
8406 (message-make-from (car addrcomp) (cadr addrcomp))
8407 (cadr addrcomp))))
8408 (when (message-fetch-field hdr)
8409 (mail-extract-address-components
8410 (message-fetch-field hdr) t))
8411 ", "))))
8412
23f87bed
MB
8413(when (featurep 'xemacs)
8414 (require 'messagexmas)
8415 (message-xmas-redefine))
8416
eec82323
LMI
8417(provide 'message)
8418
16409b0b
GM
8419(run-hooks 'message-load-hook)
8420
8421;; Local Variables:
c38e0c97 8422;; coding: utf-8
16409b0b
GM
8423;; End:
8424
eec82323 8425;;; message.el ends here