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