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