1 ;;; message.el --- composing mail and news messages
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: mail, news
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; This mode provides mail-sending facilities from within Emacs. It
27 ;; consists mainly of large chunks of code from the sendmail.el,
28 ;; gnus-msg.el and rnewspost.el files.
40 (if (string-match "XEmacs\\|Lucid" emacs-version)
41 (require 'mail-abbrevs)
42 (require 'mailabbrev))
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")
50 (put 'user-mail-address 'custom-type 'string)
51 (put 'user-full-name 'custom-type 'string)
53 (defgroup message-various nil
54 "Various Message Variables"
55 :link '(custom-manual "(message)Various Message Variables")
58 (defgroup message-buffers nil
60 :link '(custom-manual "(message)Message Buffers")
63 (defgroup message-sending nil
65 :link '(custom-manual "(message)Sending Variables")
68 (defgroup message-interface nil
70 :link '(custom-manual "(message)Interface")
73 (defgroup message-forwarding nil
75 :link '(custom-manual "(message)Forwarding")
76 :group 'message-interface)
78 (defgroup message-insertion nil
80 :link '(custom-manual "(message)Insertion")
83 (defgroup message-headers nil
85 :link '(custom-manual "(message)Message Headers")
88 (defgroup message-news nil
89 "Composing News Messages"
92 (defgroup message-mail nil
93 "Composing Mail Messages"
96 (defcustom message-directory "~/Mail/"
97 "*Directory from which all other mail file variables are derived."
98 :group 'message-various
101 (defcustom message-max-buffers 10
102 "*How many buffers to keep before starting to kill them off."
103 :group 'message-buffers
106 (defcustom message-send-rename-function nil
107 "Function called to rename the buffer after sending it."
108 :group 'message-buffers
112 (defcustom message-fcc-handler-function 'rmail-output
113 "*A function called to save outgoing articles.
114 This function will be called with the name of the file to store the
115 article in. The default function is `rmail-output' which saves in Unix
117 :type '(radio (function-item rmail-output)
118 (function :tag "Other"))
119 :group 'message-sending)
121 (defcustom message-courtesy-message
122 "The following message is a courtesy copy of an article\nthat has been posted to %s as well.\n\n"
123 "*This is inserted at the start of a mailed copy of a posted message.
124 If the string contains the format spec \"%s\", the Newsgroups
125 the article has been posted to will be inserted there.
126 If this variable is nil, no such courtesy message will be added."
127 :group 'message-sending
130 (defcustom message-ignored-bounced-headers "^\\(Received\\|Return-Path\\):"
131 "*Regexp that matches headers to be removed in resent bounced mail."
132 :group 'message-interface
136 (defcustom message-from-style 'default
137 "*Specifies how \"From\" headers look.
139 If `nil', they contain just the return address like:
141 If `parens', they look like:
142 king@grassland.com (Elvis Parsley)
143 If `angles', they look like:
144 Elvis Parsley <king@grassland.com>
146 Otherwise, most addresses look like `angles', but they look like
147 `parens' if `angles' would need quoting and `parens' would not."
148 :type '(choice (const :tag "simple" nil)
152 :group 'message-headers)
154 (defcustom message-syntax-checks nil
155 ;; Guess this one shouldn't be easy to customize...
156 "Controls what syntax checks should not be performed on outgoing posts.
157 To disable checking of long signatures, for instance, add
158 `(signature . disabled)' to this list.
160 Don't touch this variable unless you really know what you're doing.
162 Checks include subject-cmsg multiple-headers sendsys message-id from
163 long-lines control-chars size new-text redirected-followup signature
164 approved sender empty empty-headers message-id from subject
165 shorten-followup-to existing-newsgroups."
166 :group 'message-news)
168 (defcustom message-required-news-headers
169 '(From Newsgroups Subject Date Message-ID
170 (optional . Organization) Lines
171 (optional . X-Newsreader))
172 "Headers to be generated or prompted for when posting an article.
173 RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
174 Message-ID. Organization, Lines, In-Reply-To, Expires, and
175 X-Newsreader are optional. If don't you want message to insert some
176 header, remove it from this list."
178 :group 'message-headers
179 :type '(repeat sexp))
181 (defcustom message-required-mail-headers
182 '(From Subject Date (optional . In-Reply-To) Message-ID Lines
183 (optional . X-Mailer))
184 "Headers to be generated or prompted for when mailing a message.
185 RFC822 required that From, Date, To, Subject and Message-ID be
186 included. Organization, Lines and X-Mailer are optional."
188 :group 'message-headers
189 :type '(repeat sexp))
191 (defcustom message-deletable-headers '(Message-ID Date Lines)
192 "Headers to be deleted if they already exist and were generated by message previously."
193 :group 'message-headers
196 (defcustom message-ignored-news-headers
197 "^NNTP-Posting-Host:\\|^Xref:\\|^Bcc:\\|^Gcc:\\|^Fcc:\\|^Resent-Fcc:"
198 "*Regexp of headers to be removed unconditionally before posting."
200 :group 'message-headers
203 (defcustom message-ignored-mail-headers "^Gcc:\\|^Fcc:\\|^Resent-Fcc:"
204 "*Regexp of headers to be removed unconditionally before mailing."
206 :group 'message-headers
209 (defcustom message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|Return-Path:\\|^Supersedes:"
210 "*Header lines matching this regexp will be deleted before posting.
211 It's best to delete old Path and Date headers before posting to avoid
213 :group 'message-interface
217 (defcustom message-signature-separator "^-- *$"
218 "Regexp matching the signature separator."
220 :group 'message-various)
222 (defcustom message-elide-elipsis "\n[...]\n\n"
223 "*The string which is inserted for elided text.")
225 (defcustom message-interactive nil
226 "Non-nil means when sending a message wait for and display errors.
227 nil means let mailer mail back a message to report errors."
228 :group 'message-sending
232 (defcustom message-generate-new-buffers t
233 "*Non-nil means that a new message buffer will be created whenever `mail-setup' is called.
234 If this is a function, call that function with three parameters: The type,
235 the to address and the group name. (Any of these may be nil.) The function
236 should return the new buffer name."
237 :group 'message-buffers
238 :type '(choice (const :tag "off" nil)
242 (defcustom message-kill-buffer-on-exit nil
243 "*Non-nil means that the message buffer will be killed after sending a message."
244 :group 'message-buffers
247 (defvar gnus-local-organization)
248 (defcustom message-user-organization
249 (or (and (boundp 'gnus-local-organization)
250 gnus-local-organization)
251 (getenv "ORGANIZATION")
253 "*String to be used as an Organization header.
254 If t, use `message-user-organization-file'."
255 :group 'message-headers
256 :type '(choice string
257 (const :tag "consult file" t)))
260 (defcustom message-user-organization-file "/usr/lib/news/organization"
261 "*Local news organization file."
263 :group 'message-headers)
265 (defcustom message-autosave-directory "~/"
266 ; (concat (file-name-as-directory message-directory) "drafts/")
267 "*Directory where message autosaves buffers.
268 If nil, message won't autosave."
269 :group 'message-buffers
272 (defcustom message-forward-start-separator
273 "------- Start of forwarded message -------\n"
274 "*Delimiter inserted before forwarded messages."
275 :group 'message-forwarding
278 (defcustom message-forward-end-separator
279 "------- End of forwarded message -------\n"
280 "*Delimiter inserted after forwarded messages."
281 :group 'message-forwarding
284 (defcustom message-signature-before-forwarded-message t
285 "*If non-nil, put the signature before any included forwarded message."
286 :group 'message-forwarding
289 (defcustom message-included-forward-headers
290 "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-\\|^Message-ID:\\|^References:"
291 "*Regexp matching headers to be included in forwarded messages."
292 :group 'message-forwarding
295 (defcustom message-ignored-resent-headers "^Return-receipt"
296 "*All headers that match this regexp will be deleted when resending a message."
297 :group 'message-interface
300 (defcustom message-ignored-cited-headers "."
301 "*Delete these headers from the messages you yank."
302 :group 'message-insertion
305 (defcustom message-cancel-message "I am canceling my own article."
306 "Message to be inserted in the cancel message."
307 :group 'message-interface
310 ;; Useful to set in site-init.el
312 (defcustom message-send-mail-function 'message-send-mail-with-sendmail
313 "Function to call to send the current buffer as mail.
314 The headers should be delimited by a line whose contents match the
315 variable `mail-header-separator'.
317 Legal values include `message-send-mail-with-sendmail' (the default),
318 `message-send-mail-with-mh' and `message-send-mail-with-qmail'."
319 :type '(radio (function-item message-send-mail-with-sendmail)
320 (function-item message-send-mail-with-mh)
321 (function-item message-send-mail-with-qmail)
322 (function :tag "Other"))
323 :group 'message-sending
324 :group 'message-mail)
326 (defcustom message-send-news-function 'message-send-news
327 "Function to call to send the current buffer as news.
328 The headers should be delimited by a line whose contents match the
329 variable `mail-header-separator'."
330 :group 'message-sending
334 (defcustom message-reply-to-function nil
335 "Function that should return a list of headers.
336 This function should pick out addresses from the To, Cc, and From headers
337 and respond with new To and Cc headers."
338 :group 'message-interface
341 (defcustom message-wide-reply-to-function nil
342 "Function that should return a list of headers.
343 This function should pick out addresses from the To, Cc, and From headers
344 and respond with new To and Cc headers."
345 :group 'message-interface
348 (defcustom message-followup-to-function nil
349 "Function that should return a list of headers.
350 This function should pick out addresses from the To, Cc, and From headers
351 and respond with new To and Cc headers."
352 :group 'message-interface
355 (defcustom message-use-followup-to 'ask
356 "*Specifies what to do with Followup-To header.
357 If nil, ignore the header. If it is t, use its value, but query before
358 using the \"poster\" value. If it is the symbol `ask', query the user
359 whether to ignore the \"poster\" value. If it is the symbol `use',
360 always use the value."
361 :group 'message-interface
362 :type '(choice (const :tag "ignore" nil)
366 ;; stuff relating to broken sendmail in MMDF
367 (defcustom message-sendmail-f-is-evil nil
368 "*Non-nil means that \"-f username\" should not be added to the sendmail
369 command line, because it is even more evil than leaving it out."
370 :group 'message-sending
373 ;; qmail-related stuff
374 (defcustom message-qmail-inject-program "/var/qmail/bin/qmail-inject"
375 "Location of the qmail-inject program."
376 :group 'message-sending
379 (defcustom message-qmail-inject-args nil
380 "Arguments passed to qmail-inject programs.
381 This should be a list of strings, one string for each argument.
383 For e.g., if you wish to set the envelope sender address so that bounces
384 go to the right place or to deal with listserv's usage of that address, you
385 might set this variable to '(\"-f\" \"you@some.where\")."
386 :group 'message-sending
387 :type '(repeat string))
389 (defvar gnus-post-method)
390 (defvar gnus-select-method)
391 (defcustom message-post-method
392 (cond ((and (boundp 'gnus-post-method)
395 ((boundp 'gnus-select-method)
398 "Method used to post news."
400 :group 'mesage-sending
401 ;; This should be the `gnus-select-method' widget, but that might
402 ;; create a dependence to `gnus.el'.
405 (defcustom message-generate-headers-first nil
406 "*If non-nil, generate all possible headers before composing."
407 :group 'message-headers
410 (defcustom message-setup-hook nil
411 "Normal hook, run each time a new outgoing message is initialized.
412 The function `message-setup' runs this hook."
413 :group 'message-various
416 (defcustom message-signature-setup-hook nil
417 "Normal hook, run each time a new outgoing message is initialized.
418 It is run after the headers have been inserted and before
419 the signature is inserted."
420 :group 'message-various
423 (defcustom message-mode-hook nil
424 "Hook run in message mode buffers."
425 :group 'message-various
428 (defcustom message-header-hook nil
429 "Hook run in a message mode buffer narrowed to the headers."
430 :group 'message-various
433 (defcustom message-header-setup-hook nil
434 "Hook called narrowed to the headers when setting up a message
436 :group 'message-various
440 (defcustom message-citation-line-function 'message-insert-citation-line
441 "*Function called to insert the \"Whomever writes:\" line."
443 :group 'message-insertion)
446 (defcustom message-yank-prefix "> "
447 "*Prefix inserted on the lines of yanked messages.
448 nil means use indentation."
450 :group 'message-insertion)
452 (defcustom message-indentation-spaces 3
453 "*Number of spaces to insert at the beginning of each cited line.
454 Used by `message-yank-original' via `message-yank-cite'."
455 :group 'message-insertion
459 (defcustom message-cite-function
460 (if (and (boundp 'mail-citation-hook)
463 'message-cite-original)
464 "*Function for citing an original message."
465 :type '(radio (function-item message-cite-original)
466 (function-item sc-cite-original)
467 (function :tag "Other"))
468 :group 'message-insertion)
471 (defcustom message-indent-citation-function 'message-indent-citation
472 "*Function for modifying a citation just inserted in the mail buffer.
473 This can also be a list of functions. Each function can find the
474 citation between (point) and (mark t). And each function should leave
475 point and mark around the citation text as modified."
477 :group 'message-insertion)
479 (defvar message-abbrevs-loaded nil)
482 (defcustom message-signature t
483 "*String to be inserted at the end of the message buffer.
484 If t, the `message-signature-file' file will be inserted instead.
485 If a function, the result from the function will be used instead.
486 If a form, the result from the form will be used instead."
488 :group 'message-insertion)
491 (defcustom message-signature-file "~/.signature"
492 "*File containing the text inserted at end of message buffer."
494 :group 'message-insertion)
496 (defcustom message-distribution-function nil
497 "*Function called to return a Distribution header."
499 :group 'message-headers
502 (defcustom message-expires 14
503 "Number of days before your article expires."
505 :group 'message-headers
506 :link '(custom-manual "(message)News Headers")
509 (defcustom message-user-path nil
510 "If nil, use the NNTP server name in the Path header.
511 If stringp, use this; if non-nil, use no host name (user name only)."
513 :group 'message-headers
514 :link '(custom-manual "(message)News Headers")
515 :type '(choice (const :tag "nntp" nil)
517 (sexp :tag "none" :format "%t" t)))
519 (defvar message-reply-buffer nil)
520 (defvar message-reply-headers nil)
521 (defvar message-newsreader nil)
522 (defvar message-mailer nil)
523 (defvar message-sent-message-via nil)
524 (defvar message-checksum nil)
525 (defvar message-send-actions nil
526 "A list of actions to be performed upon successful sending of a message.")
527 (defvar message-exit-actions nil
528 "A list of actions to be performed upon exiting after sending a message.")
529 (defvar message-kill-actions nil
530 "A list of actions to be performed before killing a message buffer.")
531 (defvar message-postpone-actions nil
532 "A list of actions to be performed after postponing a message.")
534 (defcustom message-default-headers ""
535 "*A string containing header lines to be inserted in outgoing messages.
536 It is inserted before you edit the message, so you can edit or delete
538 :group 'message-headers
541 (defcustom message-default-mail-headers ""
542 "*A string of header lines to be inserted in outgoing mails."
543 :group 'message-headers
547 (defcustom message-default-news-headers ""
548 "*A string of header lines to be inserted in outgoing news
550 :group 'message-headers
554 ;; Note: could use /usr/ucb/mail instead of sendmail;
555 ;; options -t, and -v if not interactive.
556 (defcustom message-mailer-swallows-blank-line
557 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
558 system-configuration)
559 (file-readable-p "/etc/sendmail.cf")
560 (let ((buffer (get-buffer-create " *temp*")))
564 (insert-file-contents "/etc/sendmail.cf")
565 (goto-char (point-min))
566 (let ((case-fold-search nil))
567 (re-search-forward "^OR\\>" nil t)))
568 (kill-buffer buffer))))
569 ;; According to RFC822, "The field-name must be composed of printable
570 ;; ASCII characters (i. e., characters that have decimal values between
571 ;; 33 and 126, except colon)", i. e., any chars except ctl chars,
573 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
574 "Set this non-nil if the system's mailer runs the header and body together.
575 \(This problem exists on Sunos 4 when sendmail is run in remote mode.)
576 The value should be an expression to test whether the problem will
578 :group 'message-sending
582 (define-mail-user-agent 'message-user-agent
583 'message-mail 'message-send-and-exit
584 'message-kill-buffer 'message-send-hook))
586 ;;; Internal variables.
587 ;;; Well, not really internal.
589 (defvar message-mode-syntax-table
590 (let ((table (copy-syntax-table text-mode-syntax-table)))
591 (modify-syntax-entry ?% ". " table)
593 "Syntax table used while in Message mode.")
595 (defvar message-mode-abbrev-table text-mode-abbrev-table
596 "Abbrev table used in Message mode buffers.
597 Defaults to `text-mode-abbrev-table'.")
599 (defvar message-font-lock-keywords
600 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
601 (list '("^To:" . font-lock-function-name-face)
602 '("^[GBF]?[Cc][Cc]:\\|^Reply-To:" . font-lock-keyword-face)
603 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
604 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
605 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
606 1 'font-lock-comment-face)
607 (cons (concat "^[ \t]*"
608 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
610 'font-lock-reference-face)
611 '("^\\(X-[A-Za-z0-9-]+\\|In-Reply-To\\):.*"
612 . font-lock-string-face)))
613 "Additional expressions to highlight in Message mode.")
615 (defvar message-face-alist
616 '((bold . bold-region)
617 (underline . underline-region)
618 (default . (lambda (b e)
620 (ununderline-region b e))))
621 "Alist of mail and news faces for facemenu.
622 The cdr of ech entry is a function for applying the face to a region.")
624 (defcustom message-send-hook nil
625 "Hook run before sending messages."
626 :group 'message-various
627 :options '(ispell-message)
630 (defcustom message-send-mail-hook nil
631 "Hook run before sending mail messages."
632 :group 'message-various
635 (defcustom message-send-news-hook nil
636 "Hook run before sending news messages."
637 :group 'message-various
640 (defcustom message-sent-hook nil
641 "Hook run after sending messages."
642 :group 'message-various
645 ;;; Internal variables.
647 (defvar message-buffer-list nil)
648 (defvar message-this-is-news nil)
649 (defvar message-this-is-mail nil)
651 ;; Byte-compiler warning
652 (defvar gnus-active-hashtb)
653 (defvar gnus-read-active-file)
655 ;;; Regexp matching the delimiter of messages in UNIX mail format
656 ;;; (UNIX From lines), minus the initial ^.
657 (defvar message-unix-mail-delimiter
658 (let ((time-zone-regexp
659 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
660 "\\|[-+]?[0-9][0-9][0-9][0-9]"
666 ;; Username, perhaps with a quoted section that can contain spaces.
669 "\\(\\|\".*\"[^ \n]*\\)"
673 ;; The time the message was sent.
674 "\\([^ \n]*\\) *" ; day of the week
675 "\\([^ ]*\\) *" ; month
676 "\\([0-9]*\\) *" ; day of month
677 "\\([0-9:]*\\) *" ; time of day
679 ;; Perhaps a time zone, specified by an abbreviation, or by a
684 " [0-9][0-9]\\([0-9]*\\) *"
686 ;; On some systems the time zone can appear after the year, too.
690 "\\(remote from .*\\)?"
694 (defvar message-unsent-separator
695 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
696 "^ *---+ +Returned message +---+ *$\\|"
697 "^Start of returned message$\\|"
698 "^ *---+ +Original message +---+ *$\\|"
699 "^ *--+ +begin message +--+ *$\\|"
700 "^ *---+ +Original message follows +---+ *$\\|"
701 "^|? *---+ +Message text follows: +---+ *|?$")
702 "A regexp that matches the separator before the text of a failed message.")
704 (defvar message-header-format-alist
706 (To . message-fill-address)
707 (Cc . message-fill-address)
721 "Alist used for formatting headers.")
724 (autoload 'message-setup-toolbar "messagexmas")
725 (autoload 'mh-send-letter "mh-comp"))
730 ;;; Utility functions.
733 (defun message-point-at-bol ()
734 "Return point at the beginning of the line."
741 (defun message-point-at-eol ()
742 "Return point at the end of the line."
749 (defmacro message-y-or-n-p (question show &rest text)
750 "Ask QUESTION, displaying the rest of the arguments in a temp. buffer if SHOW"
751 `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
753 ;; Delete the current line (and the next N lines.);
754 (defmacro message-delete-line (&optional n)
755 `(delete-region (progn (beginning-of-line) (point))
756 (progn (forward-line ,(or n 1)) (point))))
758 (defun message-tokenize-header (header &optional separator)
759 "Split HEADER into a list of header elements.
760 \",\" is used as the separator."
763 (let ((regexp (format "[%s]+" (or separator ",")))
768 (message-set-work-buffer)
770 (goto-char (point-min))
775 (cond ((and (> (point) beg)
777 (and (looking-at regexp)
780 (push (buffer-substring beg (point)) elems)
781 (setq beg (match-end 0)))
782 ((= (following-char) ?\")
783 (setq quoted (not quoted)))
784 ((and (= (following-char) ?\()
787 ((and (= (following-char) ?\))
792 (defun message-fetch-field (header)
793 "The same as `mail-fetch-field', only remove all newlines."
794 (let ((value (mail-fetch-field header nil t)))
796 (nnheader-replace-chars-in-string value ?\n ? ))))
798 (defun message-fetch-reply-field (header)
799 "Fetch FIELD from the message we're replying to."
800 (when (and message-reply-buffer
801 (buffer-name message-reply-buffer))
803 (set-buffer message-reply-buffer)
804 (message-fetch-field header))))
806 (defun message-set-work-buffer ()
807 (if (get-buffer " *message work*")
809 (set-buffer " *message work*")
811 (set-buffer (get-buffer-create " *message work*"))
812 (kill-all-local-variables)
813 (buffer-disable-undo (current-buffer))))
815 (defun message-functionp (form)
816 "Return non-nil if FORM is funcallable."
817 (or (and (symbolp form) (fboundp form))
818 (and (listp form) (eq (car form) 'lambda))))
820 (defun message-strip-subject-re (subject)
821 "Remove \"Re:\" from subject lines."
822 (if (string-match "^[Rr][Ee]: *" subject)
823 (substring subject (match-end 0))
826 (defun message-remove-header (header &optional is-regexp first reverse)
827 "Remove HEADER in the narrowed buffer.
828 If REGEXP, HEADER is a regular expression.
829 If FIRST, only remove the first instance of the header.
830 Return the number of headers removed."
831 (goto-char (point-min))
832 (let ((regexp (if is-regexp header (concat "^" header ":")))
836 (while (and (not (eobp))
839 (not (looking-at regexp))
847 ;; There might be a continuation header, so we have to search
848 ;; until we find a new non-continuation line.
851 (if (re-search-forward "^[^ \t]" nil t)
852 (goto-char (match-beginning 0))
855 (if (re-search-forward "^[^ \t]" nil t)
856 (goto-char (match-beginning 0))
860 (defun message-narrow-to-headers ()
861 "Narrow the buffer to the head of the message."
864 (goto-char (point-min))
865 (if (re-search-forward
866 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
869 (goto-char (point-min)))
871 (defun message-narrow-to-head ()
872 "Narrow the buffer to the head of the message."
875 (goto-char (point-min))
876 (if (search-forward "\n\n" nil 1)
879 (goto-char (point-min)))
881 (defun message-news-p ()
882 "Say whether the current buffer contains a news message."
883 (or message-this-is-news
886 (message-narrow-to-headers)
887 (message-fetch-field "newsgroups")))))
889 (defun message-mail-p ()
890 "Say whether the current buffer contains a mail message."
891 (or message-this-is-mail
894 (message-narrow-to-headers)
895 (or (message-fetch-field "to")
896 (message-fetch-field "cc")
897 (message-fetch-field "bcc"))))))
899 (defun message-next-header ()
900 "Go to the beginning of the next header."
902 (or (eobp) (forward-char 1))
903 (not (if (re-search-forward "^[^ \t]" nil t)
905 (goto-char (point-max)))))
907 (defun message-sort-headers-1 ()
908 "Sort the buffer as headers using `message-rank' text props."
909 (goto-char (point-min))
911 nil 'message-next-header
913 (message-next-header)
917 (or (get-text-property (point) 'message-rank)
920 (defun message-sort-headers ()
921 "Sort the headers of the current message according to `message-header-format-alist'."
925 (let ((max (1+ (length message-header-format-alist)))
927 (message-narrow-to-headers)
928 (while (re-search-forward "^[^ \n]+:" nil t)
930 (match-beginning 0) (1+ (match-beginning 0))
932 (if (setq rank (length (memq (assq (intern (buffer-substring
935 message-header-format-alist)
936 message-header-format-alist)))
939 (message-sort-headers-1))))
949 (defvar message-mode-map nil)
951 (unless message-mode-map
952 (setq message-mode-map (copy-keymap text-mode-map))
953 (define-key message-mode-map "\C-c?" 'describe-mode)
955 (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
956 (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
957 (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
958 (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
959 (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
960 (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
961 (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
962 (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
963 (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
964 (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
965 (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
966 (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
967 (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
969 (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
970 (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
972 (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
973 (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
974 (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
975 (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
976 (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
977 (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
979 (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
980 (define-key message-mode-map "\C-c\C-s" 'message-send)
981 (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
982 (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
984 (define-key message-mode-map "\C-c\C-e" 'message-elide-region)
986 (define-key message-mode-map "\t" 'message-tab))
989 message-mode-menu message-mode-map "Message Menu."
991 ["Sort Headers" message-sort-headers t]
992 ["Yank Original" message-yank-original t]
993 ["Fill Yanked Message" message-fill-yanked-message t]
994 ["Insert Signature" message-insert-signature t]
995 ["Caesar (rot13) Message" message-caesar-buffer-body t]
996 ["Caesar (rot13) Region" message-caesar-region (mark t)]
997 ["Elide Region" message-elide-region (mark t)]
998 ["Rename buffer" message-rename-buffer t]
999 ["Spellcheck" ispell-message t]
1001 ["Send Message" message-send-and-exit t]
1002 ["Abort Message" message-dont-send t]))
1005 message-mode-field-menu message-mode-map ""
1007 ["Fetch To" message-insert-to t]
1008 ["Fetch Newsgroups" message-insert-newsgroups t]
1010 ["To" message-goto-to t]
1011 ["Subject" message-goto-subject t]
1012 ["Cc" message-goto-cc t]
1013 ["Reply-To" message-goto-reply-to t]
1014 ["Summary" message-goto-summary t]
1015 ["Keywords" message-goto-keywords t]
1016 ["Newsgroups" message-goto-newsgroups t]
1017 ["Followup-To" message-goto-followup-to t]
1018 ["Distribution" message-goto-distribution t]
1019 ["Body" message-goto-body t]
1020 ["Signature" message-goto-signature t]))
1022 (defvar facemenu-add-face-function)
1023 (defvar facemenu-remove-face-function)
1026 (defun message-mode ()
1027 "Major mode for editing mail and news to be sent.
1028 Like Text Mode but with these additional commands:
1029 C-c C-s message-send (send the message) C-c C-c message-send-and-exit
1030 C-c C-f move to a header field (and create it if there isn't):
1031 C-c C-f C-t move to To C-c C-f C-s move to Subject
1032 C-c C-f C-c move to Cc C-c C-f C-b move to Bcc
1033 C-c C-f C-w move to Fcc C-c C-f C-r move to Reply-To
1034 C-c C-f C-u move to Summary C-c C-f C-n move to Newsgroups
1035 C-c C-f C-k move to Keywords C-c C-f C-d move to Distribution
1036 C-c C-f C-f move to Followup-To
1037 C-c C-t message-insert-to (add a To header to a news followup)
1038 C-c C-n message-insert-newsgroups (add a Newsgroup header to a news reply)
1039 C-c C-b message-goto-body (move to beginning of message text).
1040 C-c C-i message-goto-signature (move to the beginning of the signature).
1041 C-c C-w message-insert-signature (insert `message-signature-file' file).
1042 C-c C-y message-yank-original (insert current message, if any).
1043 C-c C-q message-fill-yanked-message (fill what was yanked).
1044 C-c C-e message-elide-region (elide the text between point and mark).
1045 C-c C-r message-caesar-buffer-body (rot13 the message body)."
1047 (kill-all-local-variables)
1048 (make-local-variable 'message-reply-buffer)
1049 (setq message-reply-buffer nil)
1050 (make-local-variable 'message-send-actions)
1051 (make-local-variable 'message-exit-actions)
1052 (make-local-variable 'message-kill-actions)
1053 (make-local-variable 'message-postpone-actions)
1054 (set-syntax-table message-mode-syntax-table)
1055 (use-local-map message-mode-map)
1056 (setq local-abbrev-table message-mode-abbrev-table)
1057 (setq major-mode 'message-mode)
1058 (setq mode-name "Message")
1059 (setq buffer-offer-save t)
1060 (make-local-variable 'font-lock-defaults)
1061 (setq font-lock-defaults '(message-font-lock-keywords t))
1062 (make-local-variable 'facemenu-add-face-function)
1063 (make-local-variable 'facemenu-remove-face-function)
1064 (setq facemenu-add-face-function
1066 (let ((face-fun (cdr (assq face message-face-alist))))
1068 (funcall face-fun (point) end)
1069 (error "Face %s not configured for %s mode" face mode-name)))
1071 facemenu-remove-face-function t)
1072 (make-local-variable 'paragraph-separate)
1073 (make-local-variable 'paragraph-start)
1074 (setq paragraph-start (concat (regexp-quote mail-header-separator)
1075 "$\\|[ \t]*[-_][-_][-_]+$\\|"
1078 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
1079 "$\\|[ \t]*[-_][-_][-_]+$\\|"
1081 paragraph-separate))
1082 (make-local-variable 'message-reply-headers)
1083 (setq message-reply-headers nil)
1084 (make-local-variable 'message-newsreader)
1085 (make-local-variable 'message-mailer)
1086 (make-local-variable 'message-post-method)
1087 (make-local-variable 'message-sent-message-via)
1088 (setq message-sent-message-via nil)
1089 (make-local-variable 'message-checksum)
1090 (setq message-checksum nil)
1091 ;;(when (fboundp 'mail-hist-define-keys)
1092 ;; (mail-hist-define-keys))
1093 (when (string-match "XEmacs\\|Lucid" emacs-version)
1094 (message-setup-toolbar))
1095 (easy-menu-add message-mode-menu message-mode-map)
1096 ;; Allow mail alias things.
1097 (if (fboundp 'mail-abbrevs-setup)
1098 (mail-abbrevs-setup)
1099 (funcall (intern "mail-aliases-setup")))
1100 (run-hooks 'text-mode-hook 'message-mode-hook))
1105 ;;; Message mode commands
1108 ;;; Movement commands
1110 (defun message-goto-to ()
1111 "Move point to the To header."
1113 (message-position-on-field "To"))
1115 (defun message-goto-subject ()
1116 "Move point to the Subject header."
1118 (message-position-on-field "Subject"))
1120 (defun message-goto-cc ()
1121 "Move point to the Cc header."
1123 (message-position-on-field "Cc" "To"))
1125 (defun message-goto-bcc ()
1126 "Move point to the Bcc header."
1128 (message-position-on-field "Bcc" "Cc" "To"))
1130 (defun message-goto-fcc ()
1131 "Move point to the Fcc header."
1133 (message-position-on-field "Fcc" "To" "Newsgroups"))
1135 (defun message-goto-reply-to ()
1136 "Move point to the Reply-To header."
1138 (message-position-on-field "Reply-To" "Subject"))
1140 (defun message-goto-newsgroups ()
1141 "Move point to the Newsgroups header."
1143 (message-position-on-field "Newsgroups"))
1145 (defun message-goto-distribution ()
1146 "Move point to the Distribution header."
1148 (message-position-on-field "Distribution"))
1150 (defun message-goto-followup-to ()
1151 "Move point to the Followup-To header."
1153 (message-position-on-field "Followup-To" "Newsgroups"))
1155 (defun message-goto-keywords ()
1156 "Move point to the Keywords header."
1158 (message-position-on-field "Keywords" "Subject"))
1160 (defun message-goto-summary ()
1161 "Move point to the Summary header."
1163 (message-position-on-field "Summary" "Subject"))
1165 (defun message-goto-body ()
1166 "Move point to the beginning of the message body."
1168 (if (looking-at "[ \t]*\n") (expand-abbrev))
1169 (goto-char (point-min))
1170 (search-forward (concat "\n" mail-header-separator "\n") nil t))
1172 (defun message-goto-signature ()
1173 "Move point to the beginning of the message signature."
1175 (goto-char (point-min))
1176 (if (re-search-forward message-signature-separator nil t)
1178 (goto-char (point-max))))
1182 (defun message-insert-to ()
1183 "Insert a To header that points to the author of the article being replied to."
1185 (let ((co (message-fetch-field "courtesy-copies-to")))
1187 (equal (downcase co) "never"))
1188 (error "The user has requested not to have copies sent via mail")))
1189 (when (and (message-position-on-field "To")
1190 (mail-fetch-field "to")
1191 (not (string-match "\\` *\\'" (mail-fetch-field "to"))))
1193 (insert (or (message-fetch-reply-field "reply-to")
1194 (message-fetch-reply-field "from") "")))
1196 (defun message-insert-newsgroups ()
1197 "Insert the Newsgroups header from the article being replied to."
1199 (when (and (message-position-on-field "Newsgroups")
1200 (mail-fetch-field "newsgroups")
1201 (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
1203 (insert (or (message-fetch-reply-field "newsgroups") "")))
1207 ;;; Various commands
1209 (defun message-insert-signature (&optional force)
1210 "Insert a signature. See documentation for the `message-signature' variable."
1211 (interactive (list 0))
1214 ((and (null message-signature)
1217 (goto-char (point-max))
1218 (not (re-search-backward
1219 message-signature-separator nil t))))
1220 ((and (null message-signature)
1223 ((message-functionp message-signature)
1224 (funcall message-signature))
1225 ((listp message-signature)
1226 (eval message-signature))
1227 (t message-signature)))
1229 (cond ((stringp signature)
1231 ((and (eq t signature)
1232 message-signature-file
1233 (file-exists-p message-signature-file))
1236 (goto-char (point-max))
1237 ;; Insert the signature.
1241 (if (eq signature t)
1242 (insert-file-contents message-signature-file)
1244 (goto-char (point-max))
1245 (or (bolp) (insert "\n")))))
1247 (defun message-elide-region (b e)
1248 "Elide the text between point and mark. An ellipsis (from
1249 message-elide-elipsis) will be inserted where the text was killed."
1254 (insert message-elide-elipsis))
1256 (defvar message-caesar-translation-table nil)
1258 (defun message-caesar-region (b e &optional n)
1259 "Caesar rotation of region by N, default 13, for decrypting netnews."
1262 (min (point) (or (mark t) (point)))
1263 (max (point) (or (mark t) (point)))
1264 (when current-prefix-arg
1265 (prefix-numeric-value current-prefix-arg))))
1267 (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
1268 (unless (or (zerop n) ; no action needed for a rot of 0
1269 (= b e)) ; no region to rotate
1270 ;; We build the table, if necessary.
1271 (when (or (not message-caesar-translation-table)
1272 (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
1274 (table (make-string 256 0)))
1275 (while (< (incf i) 256)
1279 (substring table 0 ?A)
1280 (substring table (+ ?A n) (+ ?A n (- 26 n)))
1281 (substring table ?A (+ ?A n))
1282 (substring table (+ ?A 26) ?a)
1283 (substring table (+ ?a n) (+ ?a n (- 26 n)))
1284 (substring table ?a (+ ?a n))
1285 (substring table (+ ?a 26) 255)))
1286 (setq message-caesar-translation-table table)))
1287 ;; Then we translate the region. Do it this way to retain
1290 (subst-char-in-region
1291 b (1+ b) (char-after b)
1292 (aref message-caesar-translation-table (char-after b)))
1295 (defun message-caesar-buffer-body (&optional rotnum)
1296 "Caesar rotates all letters in the current buffer by 13 places.
1297 Used to encode/decode possibly offensive messages (commonly in net.jokes).
1298 With prefix arg, specifies the number of places to rotate each letter forward.
1299 Mail and USENET news headers are not rotated."
1300 (interactive (if current-prefix-arg
1301 (list (prefix-numeric-value current-prefix-arg))
1305 (when (message-goto-body)
1306 (narrow-to-region (point) (point-max)))
1307 (message-caesar-region (point-min) (point-max) rotnum))))
1309 (defun message-rename-buffer (&optional enter-string)
1310 "Rename the *message* buffer to \"*message* RECIPIENT\".
1311 If the function is run with a prefix, it will ask for a new buffer
1312 name, rather than giving an automatic name."
1313 (interactive "Pbuffer name: ")
1316 (goto-char (point-min))
1317 (narrow-to-region (point)
1318 (search-forward mail-header-separator nil 'end))
1320 (if (message-news-p) (message-fetch-field "Newsgroups")
1321 (message-fetch-field "To"))
1324 (if (string-match "," mail-to)
1325 (concat (substring mail-to 0 (match-beginning 0)) ", ...")
1327 (name-default (concat "*message* " mail-trimmed-to))
1328 (name (if enter-string
1329 (read-string "New buffer name: " name-default)
1332 (file-name-as-directory message-autosave-directory)))
1333 (rename-buffer name t)))))
1335 (defun message-fill-yanked-message (&optional justifyp)
1336 "Fill the paragraphs of a message yanked into this one.
1337 Numeric argument means justify as well."
1340 (goto-char (point-min))
1341 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1342 (let ((fill-prefix message-yank-prefix))
1343 (fill-individual-paragraphs (point) (point-max) justifyp t))))
1345 (defun message-indent-citation ()
1346 "Modify text just inserted from a message to be cited.
1347 The inserted text should be the region.
1348 When this function returns, the region is again around the modified text.
1350 Normally, indent each nonblank line `message-indentation-spaces' spaces.
1351 However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
1352 (let ((start (point)))
1353 ;; Remove unwanted headers.
1354 (when message-ignored-cited-headers
1358 (if (search-forward "\n\n" nil t)
1361 (message-remove-header message-ignored-cited-headers t)
1362 (goto-char (point-max))))
1363 ;; Delete blank lines at the start of the buffer.
1364 (while (and (point-min)
1367 (message-delete-line))
1368 ;; Delete blank lines at the end of the buffer.
1369 (goto-char (point-max))
1372 (while (and (zerop (forward-line -1))
1374 (message-delete-line))
1375 ;; Do the indentation.
1376 (if (null message-yank-prefix)
1377 (indent-rigidly start (mark t) message-indentation-spaces)
1380 (while (< (point) (mark t))
1381 (insert message-yank-prefix)
1383 (goto-char start))))
1385 (defun message-yank-original (&optional arg)
1386 "Insert the message being replied to, if any.
1387 Puts point before the text and mark after.
1388 Normally indents each nonblank line ARG spaces (default 3). However,
1389 if `message-yank-prefix' is non-nil, insert that prefix on each line.
1391 This function uses `message-cite-function' to do the actual citing.
1393 Just \\[universal-argument] as argument means don't indent, insert no
1394 prefix, and don't delete any headers."
1396 (let ((modified (buffer-modified-p)))
1397 (when (and message-reply-buffer
1398 message-cite-function)
1399 (delete-windows-on message-reply-buffer t)
1400 (insert-buffer message-reply-buffer)
1401 (funcall message-cite-function)
1402 (message-exchange-point-and-mark)
1406 (setq message-checksum (cons (message-checksum) (buffer-size)))))))
1408 (defun message-cite-original ()
1409 "Cite function in the standard Message manner."
1410 (let ((start (point))
1412 (when message-indent-citation-function
1413 (if (listp message-indent-citation-function)
1414 message-indent-citation-function
1415 (list message-indent-citation-function)))))
1418 (funcall (pop functions)))
1419 (when message-citation-line-function
1422 (funcall message-citation-line-function))))
1424 (defun message-insert-citation-line ()
1425 "Function that inserts a simple citation line."
1426 (when message-reply-headers
1427 (insert (mail-header-from message-reply-headers) " writes:\n\n")))
1429 (defun message-position-on-field (header &rest afters)
1430 (let ((case-fold-search t))
1433 (goto-char (point-min))
1436 (concat "^" (regexp-quote mail-header-separator) "$"))
1437 (match-beginning 0)))
1438 (goto-char (point-min))
1439 (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
1441 (re-search-forward "^[^ \t]" nil 'move)
1443 (skip-chars-backward "\n")
1446 (not (re-search-forward
1447 (concat "^" (regexp-quote (car afters)) ":")
1451 (re-search-forward "^[^ \t]" nil 'move)
1452 (beginning-of-line))
1453 (insert header ": \n")
1457 (defun message-remove-signature ()
1458 "Remove the signature from the text between point and mark.
1459 The text will also be indented the normal way."
1461 (let ((start (point))
1463 (if (not (re-search-forward message-signature-separator (mark t) t))
1464 ;; No signature here, so we just indent the cited text.
1465 (message-indent-citation)
1466 ;; Find the last non-empty line.
1468 (while (looking-at "[ \t]*$")
1471 (setq mark (set-marker (make-marker) (point)))
1473 (message-indent-citation)
1474 ;; Enable undoing the deletion.
1476 (delete-region mark (mark t))
1477 (set-marker mark nil)))))
1482 ;;; Sending messages
1485 (defun message-send-and-exit (&optional arg)
1486 "Send message like `message-send', then, if no errors, exit from mail buffer."
1488 (let ((buf (current-buffer))
1489 (actions message-exit-actions))
1490 (when (and (message-send arg)
1492 (if message-kill-buffer-on-exit
1495 (when (eq buf (current-buffer))
1496 (message-bury buf)))
1497 (message-do-actions actions))))
1499 (defun message-dont-send ()
1500 "Don't send the message you have been editing."
1502 (let ((actions message-postpone-actions))
1503 (message-bury (current-buffer))
1504 (message-do-actions actions)))
1506 (defun message-kill-buffer ()
1507 "Kill the current buffer."
1509 (let ((actions message-kill-actions))
1510 (kill-buffer (current-buffer))
1511 (message-do-actions actions)))
1513 (defun message-bury (buffer)
1514 "Bury this mail buffer."
1515 (let ((newbuf (other-buffer buffer)))
1516 (bury-buffer buffer)
1517 (if (and (fboundp 'frame-parameters)
1518 (cdr (assq 'dedicated (frame-parameters)))
1519 (not (null (delq (selected-frame) (visible-frame-list)))))
1520 (delete-frame (selected-frame))
1521 (switch-to-buffer newbuf))))
1523 (defun message-send (&optional arg)
1524 "Send the message in the current buffer.
1525 If `message-interactive' is non-nil, wait for success indication
1526 or error messages, and inform user.
1527 Otherwise any failure is reported in a message back to
1528 the user from the mailer."
1530 (when (if buffer-file-name
1531 (y-or-n-p (format "Send buffer contents as %s message? "
1532 (if (message-mail-p)
1533 (if (message-news-p) "mail and news" "mail")
1535 (or (buffer-modified-p)
1536 (y-or-n-p "No changes in the buffer; really send? ")))
1537 ;; Make it possible to undo the coming changes.
1539 (let ((inhibit-read-only t))
1540 (put-text-property (point-min) (point-max) 'read-only nil))
1541 (message-fix-before-sending)
1542 (run-hooks 'message-send-hook)
1543 (message "Sending...")
1544 (when (and (or (not (message-news-p))
1545 (and (or (not (memq 'news message-sent-message-via))
1547 "Already sent message via news; resend? "))
1548 (funcall message-send-news-function arg)))
1549 (or (not (message-mail-p))
1550 (and (or (not (memq 'mail message-sent-message-via))
1552 "Already sent message via mail; resend? "))
1553 (message-send-mail arg))))
1555 ;;(when (fboundp 'mail-hist-put-headers-into-history)
1556 ;; (mail-hist-put-headers-into-history))
1557 (run-hooks 'message-sent-hook)
1558 (message "Sending...done")
1559 ;; If buffer has no file, mark it as unmodified and delete autosave.
1560 (unless buffer-file-name
1561 (set-buffer-modified-p nil)
1562 (delete-auto-save-file-if-necessary t))
1563 ;; Delete other mail buffers and stuff.
1564 (message-do-send-housekeeping)
1565 (message-do-actions message-send-actions)
1569 (defun message-fix-before-sending ()
1570 "Do various things to make the message nice before sending it."
1571 ;; Make sure there's a newline at the end of the message.
1572 (goto-char (point-max))
1576 (defun message-add-action (action &rest types)
1577 "Add ACTION to be performed when doing an exit of type TYPES."
1580 (set (setq var (intern (format "message-%s-actions" (pop types))))
1581 (nconc (symbol-value var) (list action))))))
1583 (defun message-do-actions (actions)
1584 "Perform all actions in ACTIONS."
1585 ;; Now perform actions on successful sending.
1589 ;; A simple function.
1590 ((message-functionp (car actions))
1591 (funcall (car actions)))
1592 ;; Something to be evaled.
1594 (eval (car actions)))))
1597 (defun message-send-mail (&optional arg)
1598 (require 'mail-utils)
1599 (let ((tembuf (message-generate-new-buffer-clone-locals " message temp"))
1600 (case-fold-search nil)
1601 (news (message-news-p))
1602 (mailbuf (current-buffer)))
1604 (message-narrow-to-headers)
1605 ;; Insert some headers.
1606 (let ((message-deletable-headers
1607 (if news nil message-deletable-headers)))
1608 (message-generate-headers message-required-mail-headers))
1609 ;; Let the user do all of the above.
1610 (run-hooks 'message-header-hook))
1615 ;; Avoid copying text props.
1617 "%s" (save-excursion
1618 (set-buffer mailbuf)
1620 ;; Remove some headers.
1622 (message-narrow-to-headers)
1623 ;; Remove some headers.
1624 (message-remove-header message-ignored-mail-headers t))
1625 (goto-char (point-max))
1626 ;; require one newline at the end.
1627 (or (= (preceding-char) ?\n)
1630 (or (message-fetch-field "cc")
1631 (message-fetch-field "to")))
1632 (message-insert-courtesy-copy))
1633 (funcall message-send-mail-function))
1634 (kill-buffer tembuf))
1635 (set-buffer mailbuf)
1636 (push 'mail message-sent-message-via)))
1638 (defun message-send-mail-with-sendmail ()
1639 "Send off the prepared buffer with sendmail."
1640 (let ((errbuf (if message-interactive
1641 (generate-new-buffer " sendmail errors")
1643 resend-to-addresses delimline)
1644 (let ((case-fold-search t))
1646 (message-narrow-to-headers)
1647 (setq resend-to-addresses (message-fetch-field "resent-to")))
1648 ;; Change header-delimiter to be what sendmail expects.
1649 (goto-char (point-min))
1651 (concat "^" (regexp-quote mail-header-separator) "\n"))
1652 (replace-match "\n")
1654 (setq delimline (point-marker))
1655 (run-hooks 'message-send-mail-hook)
1656 ;; Insert an extra newline if we need it to work around
1657 ;; Sun's bug that swallows newlines.
1658 (goto-char (1+ delimline))
1659 (when (eval message-mailer-swallows-blank-line)
1661 (when message-interactive
1665 (let ((default-directory "/"))
1666 (apply 'call-process-region
1667 (append (list (point-min) (point-max)
1668 (if (boundp 'sendmail-program)
1670 "/usr/lib/sendmail")
1671 nil errbuf nil "-oi")
1672 ;; Always specify who from,
1673 ;; since some systems have broken sendmails.
1674 ;; But some systems are more broken with -f, so
1675 ;; we'll let users override this.
1676 (if (null message-sendmail-f-is-evil)
1677 (list "-f" (user-login-name)))
1678 ;; These mean "report errors by mail"
1679 ;; and "deliver in background".
1680 (if (null message-interactive) '("-oem" "-odb"))
1681 ;; Get the addresses from the message
1682 ;; unless this is a resend.
1683 ;; We must not do that for a resend
1684 ;; because we would find the original addresses.
1685 ;; For a resend, include the specific addresses.
1686 (if resend-to-addresses
1687 (list resend-to-addresses)
1689 (when message-interactive
1692 (goto-char (point-min))
1693 (while (re-search-forward "\n\n* *" nil t)
1694 (replace-match "; "))
1695 (if (not (zerop (buffer-size)))
1696 (error "Sending...failed to %s"
1697 (buffer-substring (point-min) (point-max)))))
1698 (when (bufferp errbuf)
1699 (kill-buffer errbuf)))))
1701 (defun message-send-mail-with-qmail ()
1702 "Pass the prepared message buffer to qmail-inject.
1703 Refer to the documentation for the variable `message-send-mail-function'
1704 to find out how to use this."
1705 ;; replace the header delimiter with a blank line
1706 (goto-char (point-min))
1708 (concat "^" (regexp-quote mail-header-separator) "\n"))
1709 (replace-match "\n")
1710 (run-hooks 'message-send-mail-hook)
1714 'call-process-region 1 (point-max) message-qmail-inject-program
1716 ;; qmail-inject's default behaviour is to look for addresses on the
1717 ;; command line; if there're none, it scans the headers.
1718 ;; yes, it does The Right Thing w.r.t. Resent-To and it's kin.
1720 ;; in general, ALL of qmail-inject's defaults are perfect for simply
1721 ;; reading a formatted (i. e., at least a To: or Resent-To header)
1722 ;; message from stdin.
1724 ;; qmail also has the advantage of not having been raped by
1725 ;; various vendors, so we don't have to allow for that, either --
1726 ;; compare this with message-send-mail-with-sendmail and weep
1727 ;; for sendmail's lost innocence.
1729 ;; all this is way cool coz it lets us keep the arguments entirely
1730 ;; free for -inject-arguments -- a big win for the user and for us
1731 ;; since we don't have to play that double-guessing game and the user
1732 ;; gets full control (no gestapo'ish -f's, for instance). --sj
1733 message-qmail-inject-args)
1734 ;; qmail-inject doesn't say anything on it's stdout/stderr,
1735 ;; we have to look at the retval instead
1737 (1 (error "qmail-inject reported permanent failure."))
1738 (111 (error "qmail-inject reported transient failure."))
1739 ;; should never happen
1740 (t (error "qmail-inject reported unknown failure."))))
1742 (defun message-send-mail-with-mh ()
1743 "Send the prepared message buffer with mh."
1744 (let ((mh-previous-window-config nil)
1745 (name (make-temp-name
1746 (concat (file-name-as-directory
1747 (expand-file-name message-autosave-directory))
1749 (setq buffer-file-name name)
1750 ;; MH wants to generate these headers itself.
1751 (let ((headers message-deletable-headers))
1753 (goto-char (point-min))
1754 (and (re-search-forward
1755 (concat "^" (symbol-name (car headers)) ": *") nil t)
1756 (message-delete-line))
1758 (run-hooks 'message-send-mail-hook)
1759 ;; Pass it on to mh.
1762 (defun message-send-news (&optional arg)
1763 (let ((tembuf (message-generate-new-buffer-clone-locals " *message temp*"))
1764 (case-fold-search nil)
1765 (method (if (message-functionp message-post-method)
1766 (funcall message-post-method arg)
1767 message-post-method))
1768 (messbuf (current-buffer))
1769 (message-syntax-checks
1771 (cons '(existing-newsgroups . disabled)
1772 message-syntax-checks)
1773 message-syntax-checks))
1776 (message-narrow-to-headers)
1777 ;; Insert some headers.
1778 (message-generate-headers message-required-news-headers)
1779 ;; Let the user do all of the above.
1780 (run-hooks 'message-header-hook))
1781 (message-cleanup-headers)
1782 (if (not (message-check-news-syntax))
1784 ;;(message "Posting not performed")
1789 (buffer-disable-undo (current-buffer))
1791 ;; Avoid copying text props.
1793 "%s" (save-excursion
1794 (set-buffer messbuf)
1796 ;; Remove some headers.
1798 (message-narrow-to-headers)
1799 ;; Remove some headers.
1800 (message-remove-header message-ignored-news-headers t))
1801 (goto-char (point-max))
1802 ;; require one newline at the end.
1803 (or (= (preceding-char) ?\n)
1805 (let ((case-fold-search t))
1806 ;; Remove the delimiter.
1807 (goto-char (point-min))
1809 (concat "^" (regexp-quote mail-header-separator) "\n"))
1810 (replace-match "\n")
1812 (run-hooks 'message-send-news-hook)
1813 (require (car method))
1814 (funcall (intern (format "%s-open-server" (car method)))
1815 (cadr method) (cddr method))
1817 (funcall (intern (format "%s-request-post" (car method))))))
1818 (kill-buffer tembuf))
1819 (set-buffer messbuf)
1821 (push 'news message-sent-message-via)
1822 (message "Couldn't send message via news: %s"
1823 (nnheader-get-report (car method)))
1827 ;;; Header generation & syntax checking.
1830 (defmacro message-check (type &rest forms)
1831 "Eval FORMS if TYPE is to be checked."
1832 `(or (message-check-element ,type)
1836 (put 'message-check 'lisp-indent-function 1)
1837 (put 'message-check 'edebug-form-spec '(form body))
1839 (defun message-check-element (type)
1840 "Returns non-nil if this type is not to be checked."
1841 (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
1843 (let ((able (assq type message-syntax-checks)))
1845 (eq (cdr able) 'disabled)))))
1847 (defun message-check-news-syntax ()
1848 "Check the syntax of the message."
1853 ;; We narrow to the headers and check them first.
1856 (message-narrow-to-headers)
1857 (message-check-news-header-syntax)))
1859 (message-check-news-body-syntax)))))
1861 (defun message-check-news-header-syntax ()
1863 ;; Check for commands in Subject.
1864 (message-check 'subject-cmsg
1865 (if (string-match "^cmsg " (message-fetch-field "subject"))
1867 "The control code \"cmsg\" is in the subject. Really post? ")
1869 ;; Check for multiple identical headers.
1870 (message-check 'multiple-headers
1872 (while (and (not found)
1873 (re-search-forward "^[^ \t:]+: " nil t))
1875 (or (re-search-forward
1880 (match-beginning 0) (- (match-end 0) 2))))
1885 (y-or-n-p (format "Multiple %s headers. Really post? " found))
1887 ;; Check for Version and Sendsys.
1888 (message-check 'sendsys
1889 (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
1891 (format "The article contains a %s command. Really post? "
1892 (buffer-substring (match-beginning 0)
1893 (1- (match-end 0)))))
1895 ;; See whether we can shorten Followup-To.
1896 (message-check 'shorten-followup-to
1897 (let ((newsgroups (message-fetch-field "newsgroups"))
1898 (followup-to (message-fetch-field "followup-to"))
1900 (when (and newsgroups
1901 (string-match "," newsgroups)
1906 (setq to (completing-read
1907 "Followups to: (default all groups) "
1908 (mapcar (lambda (g) (list g))
1910 (message-tokenize-header
1912 (goto-char (point-min))
1913 (insert "Followup-To: " to "\n"))
1915 ;; Check "Shoot me".
1916 (message-check 'shoot
1917 (if (re-search-forward
1918 "Message-ID.*.i-did-not-set--mail-host-address--so-shoot-me" nil t)
1919 (y-or-n-p "You appear to have a misconfigured system. Really post? ")
1921 ;; Check for Approved.
1922 (message-check 'approved
1923 (if (re-search-forward "^Approved:" nil t)
1924 (y-or-n-p "The article contains an Approved header. Really post? ")
1926 ;; Check the Message-ID header.
1927 (message-check 'message-id
1928 (let* ((case-fold-search t)
1929 (message-id (message-fetch-field "message-id")))
1930 (or (not message-id)
1931 (and (string-match "@" message-id)
1932 (string-match "@[^\\.]*\\." message-id))
1934 (format "The Message-ID looks strange: \"%s\". Really post? "
1936 ;; Check the Subject header.
1937 (message-check 'subject
1938 (let* ((case-fold-search t)
1939 (subject (message-fetch-field "subject")))
1942 (not (string-match "\\`[ \t]*\\'" subject)))
1945 "The subject field is empty or missing. Posting is denied.")))))
1946 ;; Check the Newsgroups & Followup-To headers.
1947 (message-check 'existing-newsgroups
1948 (let* ((case-fold-search t)
1949 (newsgroups (message-fetch-field "newsgroups"))
1950 (followup-to (message-fetch-field "followup-to"))
1951 (groups (message-tokenize-header
1953 (concat newsgroups "," followup-to)
1955 (hashtb (and (boundp 'gnus-active-hashtb)
1956 gnus-active-hashtb))
1958 (if (or (not hashtb)
1959 (not (boundp 'gnus-read-active-file))
1960 (not gnus-read-active-file)
1961 (eq gnus-read-active-file 'some))
1964 (when (and (not (boundp (intern (car groups) hashtb)))
1965 (not (equal (car groups) "poster")))
1966 (push (car groups) errors))
1972 "Really post to %s unknown group%s: %s "
1973 (if (= (length errors) 1) "this" "these")
1974 (if (= (length errors) 1) "" "s")
1975 (mapconcat 'identity errors ", ")))))))
1976 ;; Check the Newsgroups & Followup-To headers for syntax errors.
1977 (message-check 'valid-newsgroups
1978 (let ((case-fold-search t)
1979 (headers '("Newsgroups" "Followup-To"))
1981 (while (and headers (not error))
1982 (when (setq header (mail-fetch-field (car headers)))
1986 "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-+_&.a-zA-Z0-9]+\\)*\\'"
1991 (not (string-match "\\.\\'\\|\\.\\." g)))
1992 (message-tokenize-header header ","))))
1999 (format "The %s header looks odd: \"%s\". Really post? "
2000 (car headers) header)))))
2001 ;; Check the From header.
2002 (message-check 'from
2003 (let* ((case-fold-search t)
2004 (from (message-fetch-field "from"))
2005 (ad (nth 1 (mail-extract-address-components from))))
2008 (message "There is no From line. Posting is denied.")
2010 ((or (not (string-match "@[^\\.]*\\." ad)) ;larsi@ifi
2011 (string-match "\\.\\." ad) ;larsi@ifi..uio
2012 (string-match "@\\." ad) ;larsi@.ifi.uio
2013 (string-match "\\.$" ad) ;larsi@ifi.uio.
2014 (not (string-match "^[^@]+@[^@]+$" ad)) ;larsi.ifi.uio
2015 (string-match "(.*).*(.*)" from)) ;(lars) (lars)
2017 "Denied posting -- the From looks strange: \"%s\"." from)
2021 (defun message-check-news-body-syntax ()
2023 ;; Check for long lines.
2024 (message-check 'long-lines
2025 (goto-char (point-min))
2027 (concat "^" (regexp-quote mail-header-separator) "$"))
2031 (< (current-column) 80))
2032 (zerop (forward-line 1))))
2036 "You have lines longer than 79 characters. Really post? ")))
2037 ;; Check whether the article is empty.
2038 (message-check 'empty
2039 (goto-char (point-min))
2041 (concat "^" (regexp-quote mail-header-separator) "$"))
2044 (goto-char (point-max))
2045 (re-search-backward message-signature-separator nil t)
2047 (or (re-search-backward "[^ \n\t]" b t)
2048 (y-or-n-p "Empty article. Really post? "))))
2049 ;; Check for control characters.
2050 (message-check 'control-chars
2051 (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
2053 "The article contains control characters. Really post? ")
2055 ;; Check excessive size.
2056 (message-check 'size
2057 (if (> (buffer-size) 60000)
2059 (format "The article is %d octets long. Really post? "
2062 ;; Check whether any new text has been added.
2063 (message-check 'new-text
2065 (not message-checksum)
2066 (not (and (eq (message-checksum) (car message-checksum))
2067 (eq (buffer-size) (cdr message-checksum))))
2069 "It looks like no new text has been added. Really post? ")))
2070 ;; Check the length of the signature.
2071 (message-check 'signature
2072 (goto-char (point-max))
2073 (if (or (not (re-search-backward message-signature-separator nil t))
2074 (search-forward message-forward-end-separator nil t))
2076 (if (> (count-lines (point) (point-max)) 5)
2079 "Your .sig is %d lines; it should be max 4. Really post? "
2080 (1- (count-lines (point) (point-max)))))
2083 (defun message-checksum ()
2084 "Return a \"checksum\" for the current buffer."
2087 (goto-char (point-min))
2089 (concat "^" (regexp-quote mail-header-separator) "$"))
2091 (when (not (looking-at "[ \t\n]"))
2092 (setq sum (logxor (ash sum 1) (following-char))))
2096 (defun message-do-fcc ()
2097 "Process Fcc headers in the current buffer."
2098 (let ((case-fold-search t)
2099 (buf (current-buffer))
2102 (set-buffer (get-buffer-create " *message temp*"))
2103 (buffer-disable-undo (current-buffer))
2105 (insert-buffer-substring buf)
2107 (message-narrow-to-headers)
2108 (while (setq file (message-fetch-field "fcc"))
2110 (message-remove-header "fcc" nil t)))
2111 (goto-char (point-min))
2112 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
2113 (replace-match "" t t)
2114 ;; Process FCC operations.
2116 (setq file (pop list))
2117 (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
2118 ;; Pipe the article to the program in question.
2119 (call-process-region (point-min) (point-max) shell-file-name
2120 nil nil nil shell-command-switch
2121 (match-string 1 file))
2122 ;; Save the article.
2123 (setq file (expand-file-name file))
2124 (unless (file-exists-p (file-name-directory file))
2125 (make-directory (file-name-directory file) t))
2126 (if (and message-fcc-handler-function
2127 (not (eq message-fcc-handler-function 'rmail-output)))
2128 (funcall message-fcc-handler-function file)
2129 (if (and (file-readable-p file) (mail-file-babyl-p file))
2130 (rmail-output file 1 nil t)
2131 (let ((mail-use-rfc822 t))
2132 (rmail-output file 1 t t))))))
2133 (kill-buffer (current-buffer)))))
2135 (defun message-cleanup-headers ()
2136 "Do various automatic cleanups of the headers."
2137 ;; Remove empty lines in the header.
2139 (message-narrow-to-headers)
2140 (while (re-search-forward "^[ \t]*\n" nil t)
2141 (replace-match "" t t)))
2143 ;; Correct Newsgroups and Followup-To headers: change sequence of
2144 ;; spaces to comma and eliminate spaces around commas. Eliminate
2145 ;; embedded line breaks.
2146 (goto-char (point-min))
2147 (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
2151 (if (re-search-forward "^[^ \t]" nil t)
2155 (goto-char (point-min))
2156 (while (re-search-forward "\n[ \t]+" nil t)
2157 (replace-match " " t t)) ;No line breaks (too confusing)
2158 (goto-char (point-min))
2159 (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
2160 (replace-match "," t t))
2161 (goto-char (point-min))
2162 ;; Remove trailing commas.
2163 (when (re-search-forward ",+$" nil t)
2164 (replace-match "" t t)))))
2166 (defun message-make-date ()
2167 "Make a valid data header."
2168 (let ((now (current-time)))
2169 (timezone-make-date-arpa-standard
2170 (current-time-string now) (current-time-zone now))))
2172 (defun message-make-message-id ()
2173 "Make a unique Message-ID."
2174 (concat "<" (message-unique-id)
2175 (let ((psubject (save-excursion (message-fetch-field "subject"))))
2176 (if (and message-reply-headers
2177 (mail-header-references message-reply-headers)
2178 (mail-header-subject message-reply-headers)
2180 (mail-header-subject message-reply-headers)
2182 (message-strip-subject-re
2183 (mail-header-subject message-reply-headers))
2184 (message-strip-subject-re psubject))))
2186 "@" (message-make-fqdn) ">"))
2188 (defvar message-unique-id-char nil)
2190 ;; If you ever change this function, make sure the new version
2191 ;; cannot generate IDs that the old version could.
2192 ;; You might for example insert a "." somewhere (not next to another dot
2193 ;; or string boundary), or modify the "fsf" string.
2194 (defun message-unique-id ()
2195 ;; Don't use microseconds from (current-time), they may be unsupported.
2196 ;; Instead we use this randomly inited counter.
2197 (setq message-unique-id-char
2198 (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
2199 ;; (current-time) returns 16-bit ints,
2200 ;; and 2^16*25 just fits into 4 digits i base 36.
2202 (let ((tm (current-time)))
2204 (if (memq system-type '(ms-dos emx vax-vms))
2205 (let ((user (downcase (user-login-name))))
2206 (while (string-match "[^a-z0-9_]" user)
2207 (aset user (match-beginning 0) ?_))
2209 (message-number-base36 (user-uid) -1))
2210 (message-number-base36 (+ (car tm)
2211 (lsh (% message-unique-id-char 25) 16)) 4)
2212 (message-number-base36 (+ (nth 1 tm)
2213 (lsh (/ message-unique-id-char 25) 16)) 4)
2214 ;; Append the newsreader name, because while the generated
2215 ;; ID is unique to this newsreader, other newsreaders might
2216 ;; otherwise generate the same ID via another algorithm.
2219 (defun message-number-base36 (num len)
2220 (if (if (< len 0) (<= num 0) (= len 0))
2222 (concat (message-number-base36 (/ num 36) (1- len))
2223 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
2226 (defun message-make-organization ()
2227 "Make an Organization header."
2228 (let* ((organization
2229 (or (getenv "ORGANIZATION")
2230 (when message-user-organization
2231 (if (message-functionp message-user-organization)
2232 (funcall message-user-organization)
2233 message-user-organization)))))
2235 (message-set-work-buffer)
2236 (cond ((stringp organization)
2237 (insert organization))
2238 ((and (eq t organization)
2239 message-user-organization-file
2240 (file-exists-p message-user-organization-file))
2241 (insert-file-contents message-user-organization-file)))
2242 (goto-char (point-min))
2243 (while (re-search-forward "[\t\n]+" nil t)
2244 (replace-match "" t t))
2245 (unless (zerop (buffer-size))
2248 (defun message-make-lines ()
2249 "Count the number of lines and return numeric string."
2253 (goto-char (point-min))
2255 (concat "^" (regexp-quote mail-header-separator) "$"))
2257 (int-to-string (count-lines (point) (point-max))))))
2259 (defun message-make-in-reply-to ()
2260 "Return the In-Reply-To header for this message."
2261 (when message-reply-headers
2262 (let ((from (mail-header-from message-reply-headers))
2263 (date (mail-header-date message-reply-headers)))
2266 (string-match " *at \\| *@ \\| *(\\| *<" from)))
2267 (concat (if stop-pos (substring from 0 stop-pos) from)
2269 (if (or (not date) (string= date ""))
2270 "(unknown date)" date)))))))
2272 (defun message-make-distribution ()
2273 "Make a Distribution header."
2274 (let ((orig-distribution (message-fetch-reply-field "distribution")))
2275 (cond ((message-functionp message-distribution-function)
2276 (funcall message-distribution-function))
2277 (t orig-distribution))))
2279 (defun message-make-expires ()
2280 "Return an Expires header based on `message-expires'."
2281 (let ((current (current-time))
2282 (future (* 1.0 message-expires 60 60 24)))
2283 ;; Add the future to current.
2284 (setcar current (+ (car current) (round (/ future (expt 2 16)))))
2285 (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
2286 ;; Return the date in the future in UT.
2287 (timezone-make-date-arpa-standard
2288 (current-time-string current) (current-time-zone current) '(0 "UT"))))
2290 (defun message-make-path ()
2292 (let ((login-name (user-login-name)))
2293 (cond ((null message-user-path)
2294 (concat (system-name) "!" login-name))
2295 ((stringp message-user-path)
2296 ;; Support GENERICPATH. Suggested by vixie@decwrl.dec.com.
2297 (concat message-user-path "!" login-name))
2300 (defun message-make-from ()
2301 "Make a From header."
2302 (let* ((login (message-make-address))
2304 (or (and (boundp 'user-full-name)
2307 (when (string= fullname "&")
2308 (setq fullname (user-login-name)))
2310 (message-set-work-buffer)
2312 ((or (null message-from-style)
2313 (equal fullname ""))
2315 ((or (eq message-from-style 'angles)
2316 (and (not (eq message-from-style 'parens))
2317 ;; Use angles if no quoting is needed, or if parens would
2318 ;; need quoting too.
2319 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
2320 (let ((tmp (concat fullname nil)))
2321 (while (string-match "([^()]*)" tmp)
2322 (aset tmp (match-beginning 0) ?-)
2323 (aset tmp (1- (match-end 0)) ?-))
2324 (string-match "[\\()]" tmp)))))
2326 (goto-char (point-min))
2327 ;; Look for a character that cannot appear unquoted
2328 ;; according to RFC 822.
2329 (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
2330 ;; Quote fullname, escaping specials.
2331 (goto-char (point-min))
2333 (while (re-search-forward "[\"\\]" nil 1)
2334 (replace-match "\\\\\\&" t))
2336 (insert " <" login ">"))
2337 (t ; 'parens or default
2339 (let ((fullname-start (point)))
2341 (goto-char fullname-start)
2342 ;; RFC 822 says \ and nonmatching parentheses
2343 ;; must be escaped in comments.
2344 ;; Escape every instance of ()\ ...
2345 (while (re-search-forward "[()\\]" nil 1)
2346 (replace-match "\\\\\\&" t))
2347 ;; ... then undo escaping of matching parentheses,
2348 ;; including matching nested parentheses.
2349 (goto-char fullname-start)
2350 (while (re-search-forward
2351 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
2353 (replace-match "\\1(\\3)" t)
2354 (goto-char fullname-start)))
2358 (defun message-make-sender ()
2359 "Return the \"real\" user address.
2360 This function tries to ignore all user modifications, and
2361 give as trustworthy answer as possible."
2362 (concat (user-login-name) "@" (system-name)))
2364 (defun message-make-address ()
2365 "Make the address of the user."
2366 (or (message-user-mail-address)
2367 (concat (user-login-name) "@" (message-make-domain))))
2369 (defun message-user-mail-address ()
2370 "Return the pertinent part of `user-mail-address'."
2371 (when user-mail-address
2372 (if (string-match " " user-mail-address)
2373 (nth 1 (mail-extract-address-components user-mail-address))
2374 user-mail-address)))
2376 (defun message-make-fqdn ()
2377 "Return user's fully qualified domain name."
2378 (let ((system-name (system-name))
2379 (user-mail (message-user-mail-address)))
2381 ((string-match "[^.]\\.[^.]" system-name)
2382 ;; `system-name' returned the right result.
2384 ;; Try `mail-host-address'.
2385 ((and (boundp 'mail-host-address)
2386 (stringp mail-host-address)
2387 (string-match "\\." mail-host-address))
2389 ;; We try `user-mail-address' as a backup.
2390 ((and (string-match "\\." user-mail)
2391 (string-match "@\\(.*\\)\\'" user-mail))
2392 (match-string 1 user-mail))
2393 ;; Default to this bogus thing.
2395 (concat system-name ".i-did-not-set--mail-host-address--so-shoot-me")))))
2397 (defun message-make-host-name ()
2398 "Return the name of the host."
2399 (let ((fqdn (message-make-fqdn)))
2400 (string-match "^[^.]+\\." fqdn)
2401 (substring fqdn 0 (1- (match-end 0)))))
2403 (defun message-make-domain ()
2404 "Return the domain name."
2405 (or mail-host-address
2406 (message-make-fqdn)))
2408 (defun message-generate-headers (headers)
2409 "Prepare article HEADERS.
2410 Headers already prepared in the buffer are not modified."
2412 (message-narrow-to-headers)
2413 (let* ((Date (message-make-date))
2414 (Message-ID (message-make-message-id))
2415 (Organization (message-make-organization))
2416 (From (message-make-from))
2417 (Path (message-make-path))
2420 (In-Reply-To (message-make-in-reply-to))
2422 (Distribution (message-make-distribution))
2423 (Lines (message-make-lines))
2424 (X-Newsreader message-newsreader)
2425 (X-Mailer (and (not (message-fetch-field "X-Newsreader"))
2427 (Expires (message-make-expires))
2428 (case-fold-search t)
2430 ;; First we remove any old generated headers.
2431 (let ((headers message-deletable-headers))
2433 (goto-char (point-min))
2434 (and (re-search-forward
2435 (concat "^" (symbol-name (car headers)) ": *") nil t)
2436 (get-text-property (1+ (match-beginning 0)) 'message-deletable)
2437 (message-delete-line))
2439 ;; Go through all the required headers and see if they are in the
2440 ;; articles already. If they are not, or are empty, they are
2441 ;; inserted automatically - except for Subject, Newsgroups and
2444 (goto-char (point-min))
2445 (setq elem (pop headers))
2447 (if (eq (car elem) 'optional)
2448 (setq header (cdr elem))
2449 (setq header (car elem)))
2451 (when (or (not (re-search-forward
2452 (concat "^" (downcase (symbol-name header)) ":")
2455 ;; The header was found. We insert a space after the
2456 ;; colon, if there is none.
2457 (if (/= (following-char) ? ) (insert " ") (forward-char 1))
2458 ;; Find out whether the header is empty...
2459 (looking-at "[ \t]*$")))
2460 ;; So we find out what value we should insert.
2463 ((and (consp elem) (eq (car elem) 'optional))
2464 ;; This is an optional header. If the cdr of this
2465 ;; is something that is nil, then we do not insert
2467 (setq header (cdr elem))
2468 (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
2469 (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
2471 ;; The element is a cons. Either the cdr is a
2472 ;; string to be inserted verbatim, or it is a
2473 ;; function, and we insert the value returned from
2475 (or (and (stringp (cdr elem)) (cdr elem))
2476 (and (fboundp (cdr elem)) (funcall (cdr elem)))))
2477 ((and (boundp header) (symbol-value header))
2478 ;; The element is a symbol. We insert the value
2479 ;; of this symbol, if any.
2480 (symbol-value header))
2482 ;; We couldn't generate a value for this header,
2483 ;; so we just ask the user.
2484 (read-from-minibuffer
2485 (format "Empty header for %s; enter value: " header)))))
2486 ;; Finally insert the header.
2488 (not (equal value "")))
2492 ;; This header didn't exist, so we insert it.
2493 (goto-char (point-max))
2494 (insert (symbol-name header) ": " value "\n")
2496 ;; The value of this header was empty, so we clear
2497 ;; totally and insert the new value.
2498 (delete-region (point) (message-point-at-eol))
2500 ;; Add the deletable property to the headers that require it.
2501 (and (memq header message-deletable-headers)
2502 (progn (beginning-of-line) (looking-at "[^:]+: "))
2503 (add-text-properties
2504 (point) (match-end 0)
2505 '(message-deletable t face italic) (current-buffer)))))))
2506 ;; Insert new Sender if the From is strange.
2507 (let ((from (message-fetch-field "from"))
2508 (sender (message-fetch-field "sender"))
2509 (secure-sender (message-make-sender)))
2511 (not (message-check-element 'sender))
2514 (cadr (mail-extract-address-components from)))
2515 (downcase secure-sender)))
2520 (cadr (mail-extract-address-components sender)))
2521 (downcase secure-sender)))))
2522 (goto-char (point-min))
2523 ;; Rename any old Sender headers to Original-Sender.
2524 (when (re-search-forward "^\\(Original-\\)*Sender:" nil t)
2526 (insert "Original-")
2527 (beginning-of-line))
2528 (insert "Sender: " secure-sender "\n"))))))
2530 (defun message-insert-courtesy-copy ()
2531 "Insert a courtesy message in mail copies of combined messages."
2535 (message-narrow-to-headers)
2536 (when (setq newsgroups (message-fetch-field "newsgroups"))
2537 (goto-char (point-max))
2538 (insert "Posted-To: " newsgroups "\n")))
2540 (when message-courtesy-message
2542 ((string-match "%s" message-courtesy-message)
2543 (insert (format message-courtesy-message newsgroups)))
2545 (insert message-courtesy-message)))))))
2548 ;;; Setting up a message buffer
2551 (defun message-fill-address (header value)
2553 (narrow-to-region (point) (point))
2554 (insert (capitalize (symbol-name header))
2556 (if (consp value) (car value) value)
2558 (narrow-to-region (point-min) (1- (point-max)))
2560 (goto-char (point-min))
2562 (skip-chars-forward "^,\"" (point-max))
2563 (if (or (= (following-char) ?,)
2566 (if (and (> (current-column) 78)
2572 (setq last (1+ (point))))
2573 (setq last (1+ (point)))))
2574 (setq quoted (not quoted)))
2577 (goto-char (point-max))
2581 (defun message-fill-header (header value)
2582 (let ((begin (point))
2585 (insert (capitalize (symbol-name header))
2587 (if (consp value) (car value) value)
2590 (narrow-to-region begin (point))
2591 (fill-region-as-paragraph begin (point))
2592 ;; Tapdance around looong Message-IDs.
2594 (when (looking-at "[ \t]*$")
2595 (message-delete-line))
2597 (re-search-forward ":" nil t)
2598 (when (looking-at "\n[ \t]+")
2599 (replace-match " " t t))
2600 (goto-char (point-max)))))
2602 (defun message-position-point ()
2603 "Move point to where the user probably wants to find it."
2604 (message-narrow-to-headers)
2606 ((re-search-forward "^[^:]+:[ \t]*$" nil t)
2607 (search-backward ":" )
2610 (if (= (following-char) ? )
2614 (goto-char (point-max))
2617 (unless (looking-at "$")
2621 (defun message-buffer-name (type &optional to group)
2622 "Return a new (unique) buffer name based on TYPE and TO."
2624 ;; Check whether `message-generate-new-buffers' is a function,
2625 ;; and if so, call it.
2626 ((message-functionp message-generate-new-buffers)
2627 (funcall message-generate-new-buffers type to group))
2628 ;; Generate a new buffer name The Message Way.
2629 (message-generate-new-buffers
2630 (generate-new-buffer-name
2634 (or (car (mail-extract-address-components to))
2637 (if (and group (not (string= group ""))) (concat " on " group) "")
2639 ;; Use standard name.
2641 (format "*%s message*" type))))
2643 (defun message-pop-to-buffer (name)
2644 "Pop to buffer NAME, and warn if it already exists and is modified."
2645 (let ((buffer (get-buffer name)))
2647 (buffer-name buffer))
2649 (set-buffer (pop-to-buffer buffer))
2650 (when (and (buffer-modified-p)
2652 "Message already being composed; erase? ")))
2653 (error "Message being composed")))
2654 (set-buffer (pop-to-buffer name))))
2658 (defun message-do-send-housekeeping ()
2659 "Kill old message buffers."
2660 ;; We might have sent this buffer already. Delete it from the
2662 (setq message-buffer-list (delq (current-buffer) message-buffer-list))
2663 (while (and message-max-buffers
2665 (>= (length message-buffer-list) message-max-buffers))
2666 ;; Kill the oldest buffer -- unless it has been changed.
2667 (let ((buffer (pop message-buffer-list)))
2668 (when (and (buffer-name buffer)
2669 (not (buffer-modified-p buffer)))
2670 (kill-buffer buffer))))
2671 ;; Rename the buffer.
2672 (if message-send-rename-function
2673 (funcall message-send-rename-function)
2674 (when (string-match "\\`\\*" (buffer-name))
2676 (concat "*sent " (substring (buffer-name) (match-end 0))) t)))
2677 ;; Push the current buffer onto the list.
2678 (when message-max-buffers
2679 (setq message-buffer-list
2680 (nconc message-buffer-list (list (current-buffer))))))
2682 (defvar mc-modes-alist)
2683 (defun message-setup (headers &optional replybuffer actions)
2684 (when (and (boundp 'mc-modes-alist)
2685 (not (assq 'message-mode mc-modes-alist)))
2686 (push '(message-mode (encrypt . mc-encrypt-message)
2687 (sign . mc-sign-message))
2690 (setq message-send-actions actions))
2691 (setq message-reply-buffer replybuffer)
2692 (goto-char (point-min))
2693 ;; Insert all the headers.
2696 (alist message-header-format-alist))
2698 (unless (assq (caar h) message-header-format-alist)
2699 (push (list (caar h)) alist))
2703 (delete-region (point) (progn (forward-line -1) (point)))
2704 (when message-default-headers
2705 (insert message-default-headers))
2709 (insert mail-header-separator "\n")
2713 (when (message-news-p)
2714 (when message-default-news-headers
2715 (insert message-default-news-headers))
2716 (when message-generate-headers-first
2717 (message-generate-headers
2720 (copy-sequence message-required-news-headers))))))
2721 (when (message-mail-p)
2722 (when message-default-mail-headers
2723 (insert message-default-mail-headers))
2724 (when message-generate-headers-first
2725 (message-generate-headers
2728 (copy-sequence message-required-mail-headers))))))
2729 (run-hooks 'message-signature-setup-hook)
2730 (message-insert-signature)
2731 (message-set-auto-save-file-name)
2733 (message-narrow-to-headers)
2734 (run-hooks 'message-header-setup-hook))
2735 (set-buffer-modified-p nil)
2736 (run-hooks 'message-setup-hook)
2737 (message-position-point)
2740 (defun message-set-auto-save-file-name ()
2741 "Associate the message buffer with a file in the drafts directory."
2742 (when message-autosave-directory
2743 (unless (file-exists-p message-autosave-directory)
2744 (make-directory message-autosave-directory t))
2745 (let ((name (make-temp-name
2746 (concat (file-name-as-directory message-autosave-directory)
2748 (setq buffer-auto-save-file-name
2752 (set-buffer (get-buffer-create " *draft tmp*"))
2753 (setq buffer-file-name name)
2754 (make-auto-save-file-name))
2755 (kill-buffer (current-buffer)))))
2756 (clear-visited-file-modtime))))
2761 ;;; Commands for interfacing with message
2765 (defun message-mail (&optional to subject
2766 other-headers continue switch-function
2767 yank-action send-actions)
2768 "Start editing a mail message to be sent."
2770 (let ((message-this-is-mail t))
2771 (message-pop-to-buffer (message-buffer-name "mail" to))
2774 `((To . ,(or to "")) (Subject . ,(or subject "")))
2775 (when other-headers (list other-headers))))))
2778 (defun message-news (&optional newsgroups subject)
2779 "Start editing a news article to be sent."
2781 (let ((message-this-is-news t))
2782 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups))
2783 (message-setup `((Newsgroups . ,(or newsgroups ""))
2784 (Subject . ,(or subject ""))))))
2787 (defun message-reply (&optional to-address wide ignore-reply-to)
2788 "Start editing a reply to the article in the current buffer."
2790 (let ((cur (current-buffer))
2791 from subject date reply-to to cc
2792 references message-id follow-to
2793 (inhibit-point-motion-hooks t)
2794 mct never-mct gnus-warning)
2796 (message-narrow-to-head)
2797 ;; Allow customizations to have their say.
2799 ;; This is a regular reply.
2800 (if (message-functionp message-reply-to-function)
2801 (setq follow-to (funcall message-reply-to-function)))
2802 ;; This is a followup.
2803 (if (message-functionp message-wide-reply-to-function)
2806 (funcall message-wide-reply-to-function)))))
2807 ;; Find all relevant headers we need.
2808 (setq from (message-fetch-field "from")
2809 date (message-fetch-field "date")
2810 subject (or (message-fetch-field "subject") "none")
2811 to (message-fetch-field "to")
2812 cc (message-fetch-field "cc")
2813 mct (message-fetch-field "mail-copies-to")
2814 reply-to (unless ignore-reply-to (message-fetch-field "reply-to"))
2815 references (message-fetch-field "references")
2816 message-id (message-fetch-field "message-id"))
2817 ;; Remove any (buggy) Re:'s that are present and make a
2819 (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2820 (setq subject (substring subject (match-end 0))))
2821 (setq subject (concat "Re: " subject))
2823 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
2824 (string-match "<[^>]+>" gnus-warning))
2825 (setq message-id (match-string 0 gnus-warning)))
2827 ;; Handle special values of Mail-Copies-To.
2829 (cond ((equal (downcase mct) "never")
2832 ((equal (downcase mct) "always")
2833 (setq mct (or reply-to from)))))
2838 (setq follow-to (list (cons 'To (or to-address reply-to from))))
2841 (message-set-work-buffer)
2843 (insert (or reply-to from "")))
2844 (insert (if (bolp) "" ", ") (or to ""))
2845 (insert (if mct (concat (if (bolp) "" ", ") mct) ""))
2846 (insert (if cc (concat (if (bolp) "" ", ") cc) ""))
2847 ;; Remove addresses that match `rmail-dont-reply-to-names'.
2848 (insert (prog1 (rmail-dont-reply-to (buffer-string))
2850 (goto-char (point-min))
2854 (cons (mail-strip-quoted-names addr) addr))
2855 (message-tokenize-header (buffer-string))))
2858 (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
2859 (setq follow-to (list (cons 'To (cdr (pop ccalist)))))
2861 (let ((ccs (cons 'Cc (mapconcat
2862 (lambda (addr) (cdr addr)) ccalist ", "))))
2863 (when (string-match "^ +" (cdr ccs))
2864 (setcdr ccs (substring (cdr ccs) (match-end 0))))
2865 (push ccs follow-to))))))
2868 (message-pop-to-buffer (message-buffer-name
2869 (if wide "wide reply" "reply") from
2870 (if wide to-address nil)))
2872 (setq message-reply-headers
2873 (vector 0 subject from date message-id references 0 0 ""))
2876 `((Subject . ,subject)
2878 ,@(if (or references message-id)
2879 `((References . ,(concat (or references "") (and references " ")
2880 (or message-id ""))))
2885 (defun message-wide-reply (&optional to-address)
2886 "Make a \"wide\" reply to the message in the current buffer."
2888 (message-reply to-address t))
2891 (defun message-followup (&optional to-newsgroups)
2892 "Follow up to the message in the current buffer.
2893 If TO-NEWSGROUPS, use that as the new Newsgroups line."
2895 (let ((cur (current-buffer))
2896 from subject date reply-to mct
2897 references message-id follow-to
2898 (inhibit-point-motion-hooks t)
2899 (message-this-is-news t)
2900 followup-to distribution newsgroups gnus-warning)
2903 (goto-char (point-min))
2904 (if (search-forward "\n\n" nil t)
2907 (when (message-functionp message-followup-to-function)
2909 (funcall message-followup-to-function)))
2910 (setq from (message-fetch-field "from")
2911 date (message-fetch-field "date")
2912 subject (or (message-fetch-field "subject") "none")
2913 references (message-fetch-field "references")
2914 message-id (message-fetch-field "message-id")
2915 followup-to (message-fetch-field "followup-to")
2916 newsgroups (message-fetch-field "newsgroups")
2917 reply-to (message-fetch-field "reply-to")
2918 distribution (message-fetch-field "distribution")
2919 mct (message-fetch-field "mail-copies-to"))
2920 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
2921 (string-match "<[^>]+>" gnus-warning))
2922 (setq message-id (match-string 0 gnus-warning)))
2923 ;; Remove bogus distribution.
2924 (when (and (stringp distribution)
2925 (let ((case-fold-search t))
2926 (string-match "world" distribution)))
2927 (setq distribution nil))
2928 ;; Remove any (buggy) Re:'s that are present and make a
2930 (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2931 (setq subject (substring subject (match-end 0))))
2932 (setq subject (concat "Re: " subject))
2935 (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
2938 `((Subject . ,subject)
2941 (list (cons 'Newsgroups to-newsgroups)))
2942 (follow-to follow-to)
2943 ((and followup-to message-use-followup-to)
2946 ((equal (downcase followup-to) "poster")
2947 (if (or (eq message-use-followup-to 'use)
2948 (message-y-or-n-p "Obey Followup-To: poster? " t "\
2949 You should normally obey the Followup-To: header.
2951 `Followup-To: poster' sends your response via e-mail instead of news.
2953 A typical situation where `Followup-To: poster' is used is when the poster
2954 does not read the newsgroup, so he wouldn't see any replies sent to it."))
2955 (cons 'To (or reply-to from ""))
2956 (cons 'Newsgroups newsgroups)))
2958 (if (or (equal followup-to newsgroups)
2959 (not (eq message-use-followup-to 'ask))
2961 (concat "Obey Followup-To: " followup-to "? ") t "\
2962 You should normally obey the Followup-To: header.
2964 `Followup-To: " followup-to "'
2965 directs your response to " (if (string-match "," followup-to)
2966 "the specified newsgroups"
2967 "that newsgroup only") ".
2969 If a message is posted to several newsgroups, Followup-To is often
2970 used to direct the following discussion to one newsgroup only,
2971 because discussions that are spread over several newsgroup tend to
2972 be fragmented and very difficult to follow.
2974 Also, some source/announcement newsgroups are not indented for discussion;
2975 responses here are directed to other newsgroups."))
2976 (cons 'Newsgroups followup-to)
2977 (cons 'Newsgroups newsgroups))))))
2979 `((Newsgroups . ,newsgroups))))
2980 ,@(and distribution (list (cons 'Distribution distribution)))
2981 ,@(if (or references message-id)
2982 `((References . ,(concat (or references "") (and references " ")
2983 (or message-id "")))))
2985 (not (equal (downcase mct) "never")))
2986 (list (cons 'Cc (if (equal (downcase mct) "always")
2987 (or reply-to from "")
2992 (setq message-reply-headers
2993 (vector 0 subject from date message-id references 0 0 ""))))
2997 (defun message-cancel-news ()
2998 "Cancel an article you posted."
3000 (unless (message-news-p)
3001 (error "This is not a news article; canceling is impossible"))
3002 (when (yes-or-no-p "Do you really want to cancel this article? ")
3003 (let (from newsgroups message-id distribution buf)
3005 ;; Get header info. from original article.
3007 (message-narrow-to-head)
3008 (setq from (message-fetch-field "from")
3009 newsgroups (message-fetch-field "newsgroups")
3010 message-id (message-fetch-field "message-id")
3011 distribution (message-fetch-field "distribution")))
3012 ;; Make sure that this article was written by the user.
3013 (unless (string-equal
3014 (downcase (cadr (mail-extract-address-components from)))
3015 (downcase (message-make-address)))
3016 (error "This article is not yours"))
3017 ;; Make control message.
3018 (setq buf (set-buffer (get-buffer-create " *message cancel*")))
3019 (buffer-disable-undo (current-buffer))
3021 (insert "Newsgroups: " newsgroups "\n"
3022 "From: " (message-make-from) "\n"
3023 "Subject: cmsg cancel " message-id "\n"
3024 "Control: cancel " message-id "\n"
3026 (concat "Distribution: " distribution "\n")
3028 mail-header-separator "\n"
3029 message-cancel-message)
3030 (message "Canceling your article...")
3031 (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me))
3032 (funcall message-send-news-function))
3033 (message "Canceling your article...done")
3034 (kill-buffer buf)))))
3037 (defun message-supersede ()
3038 "Start composing a message to supersede the current message.
3039 This is done simply by taking the old article and adding a Supersedes
3040 header line with the old Message-ID."
3042 (let ((cur (current-buffer)))
3043 ;; Check whether the user owns the article that is to be superseded.
3044 (unless (string-equal
3045 (downcase (cadr (mail-extract-address-components
3046 (message-fetch-field "from"))))
3047 (downcase (message-make-address)))
3048 (error "This article is not yours"))
3049 ;; Get a normal message buffer.
3050 (message-pop-to-buffer (message-buffer-name "supersede"))
3051 (insert-buffer-substring cur)
3052 (message-narrow-to-head)
3053 ;; Remove unwanted headers.
3054 (when message-ignored-supersedes-headers
3055 (message-remove-header message-ignored-supersedes-headers t))
3056 (goto-char (point-min))
3057 (if (not (re-search-forward "^Message-ID: " nil t))
3058 (error "No Message-ID in this article")
3059 (replace-match "Supersedes: " t t))
3060 (goto-char (point-max))
3061 (insert mail-header-separator)
3066 (defun message-recover ()
3067 "Reread contents of current buffer from its last auto-save file."
3069 (let ((file-name (make-auto-save-file-name)))
3070 (cond ((save-window-excursion
3071 (if (not (eq system-type 'vax-vms))
3072 (with-output-to-temp-buffer "*Directory*"
3073 (buffer-disable-undo standard-output)
3074 (let ((default-directory "/"))
3076 "ls" nil standard-output nil "-l" file-name))))
3077 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
3078 (let ((buffer-read-only nil))
3080 (insert-file-contents file-name nil)))
3081 (t (error "message-recover cancelled")))))
3083 ;;; Forwarding messages.
3085 (defun message-make-forward-subject ()
3086 "Return a Subject header suitable for the message in the current buffer."
3090 (message-narrow-to-head)
3091 (concat "[" (or (message-fetch-field
3092 (if (message-news-p) "newsgroups" "from"))
3094 "] " (or (message-fetch-field "Subject") "")))))
3097 (defun message-forward (&optional news)
3098 "Forward the current message via mail.
3099 Optional NEWS will use news to forward instead of mail."
3101 (let ((cur (current-buffer))
3102 (subject (message-make-forward-subject))
3104 (if news (message-news nil subject) (message-mail nil subject))
3105 ;; Put point where we want it before inserting the forwarded
3107 (if message-signature-before-forwarded-message
3108 (goto-char (point-max))
3109 (message-goto-body))
3110 ;; Make sure we're at the start of the line.
3113 ;; Narrow to the area we are to insert.
3114 (narrow-to-region (point) (point))
3115 ;; Insert the separators and the forwarded buffer.
3116 (insert message-forward-start-separator)
3117 (setq art-beg (point))
3118 (insert-buffer-substring cur)
3119 (goto-char (point-max))
3120 (insert message-forward-end-separator)
3121 (set-text-properties (point-min) (point-max) nil)
3122 ;; Remove all unwanted headers.
3124 (narrow-to-region (point) (if (search-forward "\n\n" nil t)
3127 (goto-char (point-min))
3128 (message-remove-header message-included-forward-headers t nil t)
3130 (message-position-point)))
3133 (defun message-resend (address)
3134 "Resend the current article to ADDRESS."
3135 (interactive "sResend message to: ")
3136 (message "Resending message to %s..." address)
3138 (let ((cur (current-buffer))
3140 ;; We first set up a normal mail buffer.
3141 (set-buffer (get-buffer-create " *message resend*"))
3142 (buffer-disable-undo (current-buffer))
3144 (message-setup `((To . ,address)))
3145 ;; Insert our usual headers.
3146 (message-generate-headers '(From Date To))
3147 (message-narrow-to-headers)
3148 ;; Rename them all to "Resent-*".
3149 (while (re-search-forward "^[A-Za-z]" nil t)
3154 (delete-region (point) (point-max))
3156 ;; Insert the message to be resent.
3157 (insert-buffer-substring cur)
3158 (goto-char (point-min))
3159 (search-forward "\n\n")
3162 (narrow-to-region beg (point))
3163 (message-remove-header message-ignored-resent-headers t)
3164 (goto-char (point-max)))
3165 (insert mail-header-separator)
3166 ;; Rename all old ("Also-")Resent headers.
3167 (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
3172 (kill-buffer (current-buffer)))
3173 (message "Resending message to %s...done" address)))
3176 (defun message-bounce ()
3177 "Re-mail the current message.
3178 This only makes sense if the current message is a bounce message than
3179 contains some mail you have written which has been bounced back to
3182 (let ((cur (current-buffer))
3184 (message-pop-to-buffer (message-buffer-name "bounce"))
3185 (insert-buffer-substring cur)
3187 (message-narrow-to-head)
3188 (if (and (message-fetch-field "Mime-Version")
3189 (setq boundary (message-fetch-field "Content-Type")))
3190 (if (string-match "boundary=\"\\([^\"]+\\)\"" boundary)
3191 (setq boundary (concat (match-string 1 boundary) " *\n"
3192 "Content-Type: message/rfc822"))
3193 (setq boundary nil)))
3195 (goto-char (point-min))
3196 (search-forward "\n\n" nil t)
3198 (re-search-forward boundary nil t)
3200 (and (re-search-forward message-unsent-separator nil t)
3202 (and (search-forward "\n\n" nil t)
3203 (re-search-forward "^Return-Path:.*\n" nil t)))
3204 ;; We remove everything before the bounced mail.
3207 (if (re-search-forward "^[^ \n\t]+:" nil t)
3211 (message-narrow-to-head)
3212 (message-remove-header message-ignored-bounced-headers t)
3213 (goto-char (point-max))
3214 (insert mail-header-separator))
3215 (message-position-point)))
3218 ;;; Interactive entry points for new message buffers.
3222 (defun message-mail-other-window (&optional to subject)
3223 "Like `message-mail' command, but display mail buffer in another window."
3225 (let ((pop-up-windows t)
3226 (special-display-buffer-names nil)
3227 (special-display-regexps nil)
3228 (same-window-buffer-names nil)
3229 (same-window-regexps nil))
3230 (message-pop-to-buffer (message-buffer-name "mail" to)))
3231 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
3234 (defun message-mail-other-frame (&optional to subject)
3235 "Like `message-mail' command, but display mail buffer in another frame."
3237 (let ((pop-up-frames t)
3238 (special-display-buffer-names nil)
3239 (special-display-regexps nil)
3240 (same-window-buffer-names nil)
3241 (same-window-regexps nil))
3242 (message-pop-to-buffer (message-buffer-name "mail" to)))
3243 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
3246 (defun message-news-other-window (&optional newsgroups subject)
3247 "Start editing a news article to be sent."
3249 (let ((pop-up-windows t)
3250 (special-display-buffer-names nil)
3251 (special-display-regexps nil)
3252 (same-window-buffer-names nil)
3253 (same-window-regexps nil))
3254 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
3255 (message-setup `((Newsgroups . ,(or newsgroups ""))
3256 (Subject . ,(or subject "")))))
3259 (defun message-news-other-frame (&optional newsgroups subject)
3260 "Start editing a news article to be sent."
3262 (let ((pop-up-frames t)
3263 (special-display-buffer-names nil)
3264 (special-display-regexps nil)
3265 (same-window-buffer-names nil)
3266 (same-window-regexps nil))
3267 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
3268 (message-setup `((Newsgroups . ,(or newsgroups ""))
3269 (Subject . ,(or subject "")))))
3273 ;; This code should be moved to underline.el (from which it is stolen).
3276 (defun bold-region (start end)
3277 "Bold all nonblank characters in the region.
3278 Works by overstriking characters.
3279 Called from program, takes two arguments START and END
3280 which specify the range to operate on."
3283 (let ((end1 (make-marker)))
3284 (move-marker end1 (max start end))
3285 (goto-char (min start end))
3286 (while (< (point) end1)
3287 (or (looking-at "[_\^@- ]")
3288 (insert (following-char) "\b"))
3289 (forward-char 1)))))
3292 (defun unbold-region (start end)
3293 "Remove all boldness (overstruck characters) in the region.
3294 Called from program, takes two arguments START and END
3295 which specify the range to operate on."
3298 (let ((end1 (make-marker)))
3299 (move-marker end1 (max start end))
3300 (goto-char (min start end))
3301 (while (re-search-forward "\b" end1 t)
3302 (if (eq (following-char) (char-after (- (point) 2)))
3303 (delete-char -2))))))
3305 (defalias 'message-exchange-point-and-mark 'exchange-point-and-mark)
3307 ;; Support for toolbar
3308 (when (string-match "XEmacs\\|Lucid" emacs-version)
3309 (require 'messagexmas))
3311 ;;; Group name completion.
3313 (defvar message-newgroups-header-regexp
3314 "^\\(Newsgroups\\|Followup-To\\|Posted-To\\):"
3315 "Regexp that match headers that lists groups.")
3317 (defun message-tab ()
3318 "Expand group names in Newsgroups and Followup-To headers.
3319 Do a `tab-to-tab-stop' if not in those headers."
3321 (if (let ((mail-abbrev-mode-regexp message-newgroups-header-regexp))
3322 (mail-abbrev-in-expansion-header-p))
3323 (message-expand-group)
3326 (defvar gnus-active-hashtb)
3327 (defun message-expand-group ()
3328 (let* ((b (save-excursion (skip-chars-backward "^, :\t\n") (point)))
3329 (completion-ignore-case t)
3330 (string (buffer-substring b (point)))
3331 (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb))
3332 (completions (all-completions string hashtb))
3333 (cur (current-buffer))
3335 (delete-region b (point))
3337 ((= (length completions) 1)
3338 (if (string= (car completions) string)
3341 (message "Only matching group"))
3342 (insert (car completions))))
3343 ((and (setq comp (try-completion string hashtb))
3344 (not (string= comp string)))
3349 (message "No matching groups")
3350 (pop-to-buffer "*Completions*")
3351 (buffer-disable-undo (current-buffer))
3352 (let ((buffer-read-only nil))
3354 (let ((standard-output (current-buffer)))
3355 (display-completion-list (sort completions 'string<)))
3356 (goto-char (point-min))
3357 (pop-to-buffer cur)))))))
3361 (defun message-talkative-question (ask question show &rest text)
3362 "Call FUNCTION with argument QUESTION, displaying the rest of the arguments in a temporary buffer if SHOW.
3363 The following arguments may contain lists of values."
3365 (setq text (message-flatten-list text)))
3366 (save-window-excursion
3368 (with-output-to-temp-buffer " *MESSAGE information message*"
3369 (set-buffer " *MESSAGE information message*")
3370 (mapcar 'princ text)
3371 (goto-char (point-min))))
3372 (funcall ask question))
3373 (funcall ask question)))
3375 (defun message-flatten-list (list)
3376 "Return a new, flat list that contains all elements of LIST.
3378 \(message-flatten-list '(1 (2 3 (4 5 (6))) 7))
3381 (apply 'append (mapcar 'message-flatten-list list)))
3385 (defun message-generate-new-buffer-clone-locals (name &optional varstr)
3386 "Create and return a buffer with a name based on NAME using generate-new-buffer.
3387 Then clone the local variables and values from the old buffer to the
3388 new one, cloning only the locals having a substring matching the
3390 (let ((oldlocals (buffer-local-variables)))
3392 (set-buffer (generate-new-buffer name))
3393 (mapcar (lambda (dude)
3394 (when (and (car dude)
3396 (string-match varstr (symbol-name (car dude)))))
3398 (set (make-local-variable (car dude))
3403 (run-hooks 'message-load-hook)
3407 ;;; message.el ends here