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