Initial revision
[bpt/emacs.git] / lisp / message.el
CommitLineData
526baa41
LMI
1;;; message.el --- composing mail and news messages
2;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5;; Keywords: mail, news
6
7;; This file is part of GNU Emacs.
8
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)
12;; any later version.
13
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.
18
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.
23
24;;; Commentary:
25
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.
29
30;;; Code:
31
32(eval-when-compile
33 (require 'cl))
34(require 'mailheader)
35(require 'rmail)
36(require 'nnheader)
37(require 'timezone)
38(require 'easymenu)
39(if (string-match "XEmacs\\|Lucid" emacs-version)
40 (require 'mail-abbrevs)
41 (require 'mailabbrev))
42
43;;;###autoload
44(defvar message-directory "~/Mail/"
45 "*Directory from which all other mail file variables are derived.")
46
47(defvar message-max-buffers 10
48 "*How many buffers to keep before starting to kill them off.")
49
50(defvar message-send-rename-function nil
51 "Function called to rename the buffer after sending it.")
52
53;;;###autoload
54(defvar message-fcc-handler-function 'rmail-output
55 "*A function called to save outgoing articles.
56This function will be called with the name of the file to store the
57article in. The default function is `rmail-output' which saves in Unix
58mailbox format.")
59
60;;;###autoload
61(defvar message-courtesy-message
62 "The following message is a courtesy copy of an article\nthat has been posted as well.\n\n"
63 "*This is inserted at the start of a mailed copy of a posted message.
64If this variable is nil, no such courtesy message will be added.")
65
66;;;###autoload
67(defvar message-ignored-bounced-headers "^\\(Received\\|Return-Path\\):"
68 "*Regexp that matches headers to be removed in resent bounced mail.")
69
70;;;###autoload
71(defvar message-from-style 'default
72 "*Specifies how \"From\" headers look.
73
74If `nil', they contain just the return address like:
75 king@grassland.com
76If `parens', they look like:
77 king@grassland.com (Elvis Parsley)
78If `angles', they look like:
79 Elvis Parsley <king@grassland.com>
80
81Otherwise, most addresses look like `angles', but they look like
82`parens' if `angles' would need quoting and `parens' would not.")
83
84;;;###autoload
85(defvar message-syntax-checks nil
86 "Controls what syntax checks should not be performed on outgoing posts.
87To disable checking of long signatures, for instance, add
88 `(signature . disabled)' to this list.
89
90Don't touch this variable unless you really know what you're doing.
91
92Checks include subject-cmsg multiple-headers sendsys message-id from
93long-lines control-chars size new-text redirected-followup signature
94approved sender empty empty-headers message-id from subject.")
95
96;;;###autoload
97(defvar message-required-news-headers
98 '(From Newsgroups Subject Date Message-ID
99 (optional . Organization) Lines
100 (optional . X-Newsreader))
101 "*Headers to be generated or prompted for when posting an article.
102RFC977 and RFC1036 require From, Date, Newsgroups, Subject,
103Message-ID. Organization, Lines, In-Reply-To, Expires, and
104X-Newsreader are optional. If don't you want message to insert some
105header, remove it from this list.")
106
107;;;###autoload
108(defvar message-required-mail-headers
109 '(From Subject Date (optional . In-Reply-To) Message-ID Lines
110 (optional . X-Mailer))
111 "*Headers to be generated or prompted for when mailing a message.
112RFC822 required that From, Date, To, Subject and Message-ID be
113included. Organization, Lines and X-Mailer are optional.")
114
115;;;###autoload
116(defvar message-deletable-headers '(Message-ID Date)
117 "*Headers to be deleted if they already exist and were generated by message previously.")
118
119;;;###autoload
120(defvar message-ignored-news-headers
121 "^NNTP-Posting-Host:\\|^Xref:\\|^Bcc:\\|^Gcc:\\|^Fcc:"
122 "*Regexp of headers to be removed unconditionally before posting.")
123
124;;;###autoload
125(defvar message-ignored-mail-headers "^Gcc:\\|^Fcc:"
126 "*Regexp of headers to be removed unconditionally before mailing.")
127
128;;;###autoload
129(defvar message-ignored-supersedes-headers "^Path:\\|^Date\\|^NNTP-Posting-Host:\\|^Xref:\\|^Lines:\\|^Received:\\|^X-From-Line:\\|Return-Path:\\|^Supersedes:"
130 "*Header lines matching this regexp will be deleted before posting.
131It's best to delete old Path and Date headers before posting to avoid
132any confusion.")
133
134;;;###autoload
135(defvar message-signature-separator "^-- *$"
136 "Regexp matching the signature separator.")
137
138;;;###autoload
139(defvar message-interactive nil
140 "Non-nil means when sending a message wait for and display errors.
141nil means let mailer mail back a message to report errors.")
142
143;;;###autoload
144(defvar message-generate-new-buffers t
145 "*Non-nil means that a new message buffer will be created whenever `mail-setup' is called.
146If this is a function, call that function with three parameters: The type,
147the to address and the group name. (Any of these may be nil.) The function
148should return the new buffer name.")
149
150;;;###autoload
151(defvar message-kill-buffer-on-exit nil
152 "*Non-nil means that the message buffer will be killed after sending a message.")
153
154(defvar gnus-local-organization)
155(defvar message-user-organization
156 (or (and (boundp 'gnus-local-organization)
157 gnus-local-organization)
158 (getenv "ORGANIZATION")
159 t)
160 "*String to be used as an Organization header.
161If t, use `message-user-organization-file'.")
162
163;;;###autoload
164(defvar message-user-organization-file "/usr/lib/news/organization"
165 "*Local news organization file.")
166
167;;;###autoload
168(defvar message-autosave-directory
169 (concat (file-name-as-directory message-directory) "drafts/")
170 "*Directory where message autosaves buffers.
171If nil, message won't autosave.")
172
173(defvar message-forward-start-separator
174 "------- Start of forwarded message -------\n"
175 "*Delimiter inserted before forwarded messages.")
176
177(defvar message-forward-end-separator
178 "------- End of forwarded message -------\n"
179 "*Delimiter inserted after forwarded messages.")
180
181;;;###autoload
182(defvar message-signature-before-forwarded-message t
183 "*If non-nil, put the signature before any included forwarded message.")
184
185;;;###autoload
186(defvar message-included-forward-headers
187 "^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:"
188 "*Regexp matching headers to be included in forwarded messages.")
189
190;;;###autoload
191(defvar message-ignored-resent-headers "^Return-receipt"
192 "*All headers that match this regexp will be deleted when resending a message.")
193
194;;;###autoload
195(defvar message-ignored-cited-headers "."
196 "Delete these headers from the messages you yank.")
197
198;; Useful to set in site-init.el
199;;;###autoload
200(defvar message-send-mail-function 'message-send-mail-with-sendmail
201 "Function to call to send the current buffer as mail.
202The headers should be delimited by a line whose contents match the
203variable `mail-header-separator'.
204
205Legal values include `message-send-mail-with-mh' and
206`message-send-mail-with-sendmail', which is the default.")
207
208;;;###autoload
209(defvar message-send-news-function 'message-send-news
210 "Function to call to send the current buffer as news.
211The headers should be delimited by a line whose contents match the
212variable `mail-header-separator'.")
213
214;;;###autoload
215(defvar message-reply-to-function nil
216 "Function that should return a list of headers.
217This function should pick out addresses from the To, Cc, and From headers
218and respond with new To and Cc headers.")
219
220;;;###autoload
221(defvar message-wide-reply-to-function nil
222 "Function that should return a list of headers.
223This function should pick out addresses from the To, Cc, and From headers
224and respond with new To and Cc headers.")
225
226;;;###autoload
227(defvar message-followup-to-function nil
228 "Function that should return a list of headers.
229This function should pick out addresses from the To, Cc, and From headers
230and respond with new To and Cc headers.")
231
232;;;###autoload
233(defvar message-use-followup-to 'ask
234 "*Specifies what to do with Followup-To header.
235If nil, ignore the header. If it is t, use its value, but query before
236using the \"poster\" value. If it is the symbol `ask', query the user
237whether to ignore the \"poster\" value. If it is the symbol `use',
238always use the value.")
239
240(defvar gnus-post-method)
241(defvar gnus-select-method)
242;;;###autoload
243(defvar message-post-method
244 (cond ((and (boundp 'gnus-post-method)
245 gnus-post-method)
246 gnus-post-method)
247 ((boundp 'gnus-select-method)
248 gnus-select-method)
249 (t '(nnspool "")))
250 "Method used to post news.")
251
252;;;###autoload
253(defvar message-generate-headers-first nil
254 "*If non-nil, generate all possible headers before composing.")
255
256(defvar message-setup-hook nil
257 "Normal hook, run each time a new outgoing message is initialized.
258The function `message-setup' runs this hook.")
259
260(defvar message-signature-setup-hook nil
261 "Normal hook, run each time a new outgoing message is initialized.
262It is run after the headers have been inserted and before
263the signature is inserted.")
264
265(defvar message-mode-hook nil
266 "Hook run in message mode buffers.")
267
268(defvar message-header-hook nil
269 "Hook run in a message mode buffer narrowed to the headers.")
270
271(defvar message-header-setup-hook nil
272 "Hook called narrowed to the headers when setting up a message buffer.")
273
274;;;###autoload
275(defvar message-citation-line-function 'message-insert-citation-line
276 "*Function called to insert the \"Whomever writes:\" line.")
277
278;;;###autoload
279(defvar message-yank-prefix "> "
280 "*Prefix inserted on the lines of yanked messages.
281nil means use indentation.")
282
283(defvar message-indentation-spaces 3
284 "*Number of spaces to insert at the beginning of each cited line.
285Used by `message-yank-original' via `message-yank-cite'.")
286
287;;;###autoload
288(defvar message-cite-function 'message-cite-original
289 "*Function for citing an original message.")
290
291;;;###autoload
292(defvar message-indent-citation-function 'message-indent-citation
293 "*Function for modifying a citation just inserted in the mail buffer.
294This can also be a list of functions. Each function can find the
295citation between (point) and (mark t). And each function should leave
296point and mark around the citation text as modified.")
297
298(defvar message-abbrevs-loaded nil)
299
300;;;###autoload
301(defvar message-signature t
302 "*String to be inserted at the end of the message buffer.
303If t, the `message-signature-file' file will be inserted instead.
304If a function, the result from the function will be used instead.
305If a form, the result from the form will be used instead.")
306
307;;;###autoload
308(defvar message-signature-file "~/.signature"
309 "*File containing the text inserted at end of message. buffer.")
310
311(defvar message-distribution-function nil
312 "*Function called to return a Distribution header.")
313
314(defvar message-expires 14
315 "*Number of days before your article expires.")
316
317(defvar message-user-path nil
318 "If nil, use the NNTP server name in the Path header.
319If stringp, use this; if non-nil, use no host name (user name only).")
320
321(defvar message-reply-buffer nil)
322(defvar message-reply-headers nil)
323(defvar message-newsreader nil)
324(defvar message-mailer nil)
325(defvar message-sent-message-via nil)
326(defvar message-checksum nil)
327(defvar message-send-actions nil
328 "A list of actions to be performed upon successful sending of a message.")
329(defvar message-exit-actions nil
330 "A list of actions to be performed upon exiting after sending a message.")
331(defvar message-kill-actions nil
332 "A list of actions to be performed before killing a message buffer.")
333(defvar message-postpone-actions nil
334 "A list of actions to be performed after postponing a message.")
335
336;;;###autoload
337(defvar message-default-headers nil
338 "*A string containing header lines to be inserted in outgoing messages.
339It is inserted before you edit the message, so you can edit or delete
340these lines.")
341
342;;;###autoload
343(defvar message-default-mail-headers nil
344 "*A string of header lines to be inserted in outgoing mails.")
345
346;;;###autoload
347(defvar message-default-news-headers nil
348 "*A string of header lines to be inserted in outgoing news articles.")
349
350;; Note: could use /usr/ucb/mail instead of sendmail;
351;; options -t, and -v if not interactive.
352(defvar message-mailer-swallows-blank-line
353 (if (and (string-match "sparc-sun-sunos\\(\\'\\|[^5]\\)"
354 system-configuration)
355 (file-readable-p "/etc/sendmail.cf")
356 (let ((buffer (get-buffer-create " *temp*")))
357 (unwind-protect
358 (save-excursion
359 (set-buffer buffer)
360 (insert-file-contents "/etc/sendmail.cf")
361 (goto-char (point-min))
362 (let ((case-fold-search nil))
363 (re-search-forward "^OR\\>" nil t)))
364 (kill-buffer buffer))))
365 ;; According to RFC822, "The field-name must be composed of printable
366 ;; ASCII characters (i.e. characters that have decimal values between
367 ;; 33 and 126, except colon)", i.e. any chars except ctl chars,
368 ;; space, or colon.
369 '(looking-at "[ \t]\\|[][!\"#$%&'()*+,-./0-9;<=>?@A-Z\\\\^_`a-z{|}~]+:"))
370 "Set this non-nil if the system's mailer runs the header and body together.
371\(This problem exists on Sunos 4 when sendmail is run in remote mode.)
372The value should be an expression to test whether the problem will
373actually occur.")
374
375(defvar message-mode-syntax-table
376 (let ((table (copy-syntax-table text-mode-syntax-table)))
377 (modify-syntax-entry ?% ". " table)
378 table)
379 "Syntax table used while in Message mode.")
380
381(defvar message-font-lock-keywords
382 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
383 (list '("^To:" . font-lock-function-name-face)
384 '("^[GBF]?[Cc][Cc]:\\|^Reply-To:" . font-lock-keyword-face)
385 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
386 (1 font-lock-comment-face) (2 font-lock-type-face nil t))
387 (list (concat "^\\(" (regexp-quote mail-header-separator) "\\)$")
388 1 'font-lock-comment-face)
389 (cons (concat "^[ \t]*"
390 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
391 "[>|}].*")
392 'font-lock-reference-face)
393 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*"
394 . font-lock-string-face)))
395 "Additional expressions to highlight in Message mode.")
396
397(defvar message-face-alist
398 '((bold . bold-region)
399 (underline . underline-region)
400 (default . (lambda (b e)
401 (unbold-region b e)
402 (ununderline-region b e))))
403 "Alist of mail and news faces for facemenu.
404The cdr of ech entry is a function for applying the face to a region.")
405
406(defvar message-send-hook nil
407 "Hook run before sending messages.")
408
409(defvar message-sent-hook nil
410 "Hook run after sending messages.")
411
412;;; Internal variables.
413
414(defvar message-buffer-list nil)
415
416;;; Regexp matching the delimiter of messages in UNIX mail format
417;;; (UNIX From lines), minus the initial ^.
418(defvar message-unix-mail-delimiter
419 (let ((time-zone-regexp
420 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
421 "\\|[-+]?[0-9][0-9][0-9][0-9]"
422 "\\|"
423 "\\) *")))
424 (concat
425 "From "
426
427 ;; Username, perhaps with a quoted section that can contain spaces.
428 "\\("
429 "[^ \n]*"
430 "\\(\\|\".*\"[^ \n]*\\)"
431 "\\|<[^<>\n]+>"
432 "\\) ?"
433
434 ;; The time the message was sent.
435 "\\([^ \n]*\\) *" ; day of the week
436 "\\([^ ]*\\) *" ; month
437 "\\([0-9]*\\) *" ; day of month
438 "\\([0-9:]*\\) *" ; time of day
439
440 ;; Perhaps a time zone, specified by an abbreviation, or by a
441 ;; numeric offset.
442 time-zone-regexp
443
444 ;; The year.
445 " [0-9][0-9]\\([0-9]*\\) *"
446
447 ;; On some systems the time zone can appear after the year, too.
448 time-zone-regexp
449
450 ;; Old uucp cruft.
451 "\\(remote from .*\\)?"
452
453 "\n")))
454
455(defvar message-unsent-separator
456 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
457 "^ *---+ +Returned message +---+ *$\\|"
458 "^Start of returned message$\\|"
459 "^ *---+ +Original message +---+ *$\\|"
460 "^ *--+ +begin message +--+ *$\\|"
461 "^ *---+ +Original message follows +---+ *$\\|"
462 "^|? *---+ +Message text follows: +---+ *|?$")
463 "A regexp that matches the separator before the text of a failed message.")
464
465(defvar message-header-format-alist
466 `((Newsgroups)
467 (To . message-fill-address)
468 (Cc . message-fill-address)
469 (Subject)
470 (In-Reply-To)
471 (Fcc)
472 (Bcc)
473 (Date)
474 (Organization)
475 (Distribution)
476 (Lines)
477 (Expires)
478 (Message-ID)
479 (References . message-fill-header)
480 (X-Mailer)
481 (X-Newsreader))
482 "Alist used for formatting headers.")
483
484(eval-and-compile
485 (autoload 'message-setup-toolbar "messagexmas")
486 (autoload 'mh-send-letter "mh-comp"))
487
488\f
489
490;;;
491;;; Utility functions.
492;;;
493
494(defun message-point-at-bol ()
495 "Return point at the beginning of the line."
496 (let ((p (point)))
497 (beginning-of-line)
498 (prog1
499 (point)
500 (goto-char p))))
501
502(defun message-point-at-eol ()
503 "Return point at the end of the line."
504 (let ((p (point)))
505 (end-of-line)
506 (prog1
507 (point)
508 (goto-char p))))
509
510;; Delete the current line (and the next N lines.);
511(defmacro message-delete-line (&optional n)
512 `(delete-region (progn (beginning-of-line) (point))
513 (progn (forward-line ,(or n 1)) (point))))
514
515(defun message-tokenize-header (header &optional separator)
516 "Split HEADER into a list of header elements.
517\",\" is used as the separator."
518 (let ((regexp (format "[%s]+" (or separator ",")))
519 (beg 1)
520 quoted elems)
521 (save-excursion
522 (message-set-work-buffer)
523 (insert header)
524 (goto-char (point-min))
525 (while (not (eobp))
526 (forward-char 1)
527 (cond ((and (> (point) beg)
528 (or (eobp)
529 (and (looking-at regexp)
530 (not quoted))))
531 (push (buffer-substring beg (point)) elems)
532 (setq beg (match-end 0)))
533 ((= (following-char) ?\")
534 (setq quoted (not quoted)))))
535 (nreverse elems))))
536
537(defun message-fetch-field (header)
538 "The same as `mail-fetch-field', only remove all newlines."
539 (let ((value (mail-fetch-field header)))
540 (when value
541 (nnheader-replace-chars-in-string value ?\n ? ))))
542
543(defun message-fetch-reply-field (header)
544 "Fetch FIELD from the message we're replying to."
545 (when (and message-reply-buffer
546 (buffer-name message-reply-buffer))
547 (save-excursion
548 (set-buffer message-reply-buffer)
549 (message-fetch-field header))))
550
551(defun message-set-work-buffer ()
552 (if (get-buffer " *message work*")
553 (progn
554 (set-buffer " *message work*")
555 (erase-buffer))
556 (set-buffer (get-buffer-create " *message work*"))
557 (kill-all-local-variables)
558 (buffer-disable-undo (current-buffer))))
559
560(defun message-functionp (form)
561 "Return non-nil if FORM is funcallable."
562 (or (and (symbolp form) (fboundp form))
563 (and (listp form) (eq (car form) 'lambda))))
564
565(defun message-strip-subject-re (subject)
566 "Remove \"Re:\" from subject lines."
567 (if (string-match "^[Rr][Ee]: *" subject)
568 (substring subject (match-end 0))
569 subject))
570
571(defun message-remove-header (header &optional is-regexp first reverse)
572 "Remove HEADER in the narrowed buffer.
573If REGEXP, HEADER is a regular expression.
574If FIRST, only remove the first instance of the header.
575Return the number of headers removed."
576 (goto-char (point-min))
577 (let ((regexp (if is-regexp header (concat "^" header ":")))
578 (number 0)
579 (case-fold-search t)
580 last)
581 (while (and (not (eobp))
582 (not last))
583 (if (if reverse
584 (not (looking-at regexp))
585 (looking-at regexp))
586 (progn
587 (incf number)
588 (when first
589 (setq last t))
590 (delete-region
591 (point)
592 ;; There might be a continuation header, so we have to search
593 ;; until we find a new non-continuation line.
594 (progn
595 (forward-line 1)
596 (if (re-search-forward "^[^ \t]" nil t)
597 (goto-char (match-beginning 0))
598 (point-max)))))
599 (forward-line 1)
600 (if (re-search-forward "^[^ \t]" nil t)
601 (goto-char (match-beginning 0))
602 (point-max))))
603 number))
604
605(defun message-narrow-to-headers ()
606 "Narrow the buffer to the head of the message."
607 (widen)
608 (narrow-to-region
609 (goto-char (point-min))
610 (if (re-search-forward
611 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
612 (match-beginning 0)
613 (point-max)))
614 (goto-char (point-min)))
615
616(defun message-narrow-to-head ()
617 "Narrow the buffer to the head of the message."
618 (widen)
619 (narrow-to-region
620 (goto-char (point-min))
621 (if (search-forward "\n\n" nil 1)
622 (1- (point))
623 (point-max)))
624 (goto-char (point-min)))
625
626(defun message-news-p ()
627 "Say whether the current buffer contains a news message."
628 (save-excursion
629 (save-restriction
630 (message-narrow-to-headers)
631 (message-fetch-field "newsgroups"))))
632
633(defun message-mail-p ()
634 "Say whether the current buffer contains a mail message."
635 (save-excursion
636 (save-restriction
637 (message-narrow-to-headers)
638 (or (message-fetch-field "to")
639 (message-fetch-field "cc")
640 (message-fetch-field "bcc")))))
641
642(defun message-next-header ()
643 "Go to the beginning of the next header."
644 (beginning-of-line)
645 (or (eobp) (forward-char 1))
646 (not (if (re-search-forward "^[^ \t]" nil t)
647 (beginning-of-line)
648 (goto-char (point-max)))))
649
650(defun message-sort-headers-1 ()
651 "Sort the buffer as headers using `message-rank' text props."
652 (goto-char (point-min))
653 (sort-subr
654 nil 'message-next-header
655 (lambda ()
656 (message-next-header)
657 (unless (bobp)
658 (forward-char -1)))
659 (lambda ()
660 (or (get-text-property (point) 'message-rank)
661 0))))
662
663(defun message-sort-headers ()
664 "Sort the headers of the current message according to `message-header-format-alist'."
665 (interactive)
666 (save-excursion
667 (save-restriction
668 (let ((max (1+ (length message-header-format-alist)))
669 rank)
670 (message-narrow-to-headers)
671 (while (re-search-forward "^[^ \n]+:" nil t)
672 (put-text-property
673 (match-beginning 0) (1+ (match-beginning 0))
674 'message-rank
675 (if (setq rank (length (memq (assq (intern (buffer-substring
676 (match-beginning 0)
677 (1- (match-end 0))))
678 message-header-format-alist)
679 message-header-format-alist)))
680 (- max rank)
681 (1+ max)))))
682 (message-sort-headers-1))))
683
684\f
685
686;;;
687;;; Message mode
688;;;
689
690;;; Set up keymap.
691
692(defvar message-mode-map nil)
693
694(unless message-mode-map
695 (setq message-mode-map (copy-keymap text-mode-map))
696 (define-key message-mode-map "\C-c?" 'describe-mode)
697
698 (define-key message-mode-map "\C-c\C-f\C-t" 'message-goto-to)
699 (define-key message-mode-map "\C-c\C-f\C-b" 'message-goto-bcc)
700 (define-key message-mode-map "\C-c\C-f\C-w" 'message-goto-fcc)
701 (define-key message-mode-map "\C-c\C-f\C-c" 'message-goto-cc)
702 (define-key message-mode-map "\C-c\C-f\C-s" 'message-goto-subject)
703 (define-key message-mode-map "\C-c\C-f\C-r" 'message-goto-reply-to)
704 (define-key message-mode-map "\C-c\C-f\C-n" 'message-goto-newsgroups)
705 (define-key message-mode-map "\C-c\C-f\C-d" 'message-goto-distribution)
706 (define-key message-mode-map "\C-c\C-f\C-f" 'message-goto-followup-to)
707 (define-key message-mode-map "\C-c\C-f\C-k" 'message-goto-keywords)
708 (define-key message-mode-map "\C-c\C-f\C-u" 'message-goto-summary)
709 (define-key message-mode-map "\C-c\C-b" 'message-goto-body)
710 (define-key message-mode-map "\C-c\C-i" 'message-goto-signature)
711
712 (define-key message-mode-map "\C-c\C-t" 'message-insert-to)
713 (define-key message-mode-map "\C-c\C-n" 'message-insert-newsgroups)
714
715 (define-key message-mode-map "\C-c\C-y" 'message-yank-original)
716 (define-key message-mode-map "\C-c\C-q" 'message-fill-yanked-message)
717 (define-key message-mode-map "\C-c\C-w" 'message-insert-signature)
718 (define-key message-mode-map "\C-c\C-r" 'message-caesar-buffer-body)
719 (define-key message-mode-map "\C-c\C-o" 'message-sort-headers)
720 (define-key message-mode-map "\C-c\M-r" 'message-rename-buffer)
721
722 (define-key message-mode-map "\C-c\C-c" 'message-send-and-exit)
723 (define-key message-mode-map "\C-c\C-s" 'message-send)
724 (define-key message-mode-map "\C-c\C-k" 'message-kill-buffer)
725 (define-key message-mode-map "\C-c\C-d" 'message-dont-send)
726
727 (define-key message-mode-map "\t" 'message-tab))
728
729(easy-menu-define message-mode-menu message-mode-map
730 "Message Menu."
731 '("Message"
732 "Go to Field:"
733 "----"
734 ["To" message-goto-to t]
735 ["Subject" message-goto-subject t]
736 ["Cc" message-goto-cc t]
737 ["Reply-to" message-goto-reply-to t]
738 ["Summary" message-goto-summary t]
739 ["Keywords" message-goto-keywords t]
740 ["Newsgroups" message-goto-newsgroups t]
741 ["Followup-To" message-goto-followup-to t]
742 ["Distribution" message-goto-distribution t]
743 ["Body" message-goto-body t]
744 ["Signature" message-goto-signature t]
745 "----"
746 "Miscellaneous Commands:"
747 "----"
748 ["Sort Headers" message-sort-headers t]
749 ["Yank Original" message-yank-original t]
750 ["Fill Yanked Message" message-fill-yanked-message t]
751 ["Insert Signature" message-insert-signature t]
752 ["Caesar (rot13) Message" message-caesar-buffer-body t]
753 ["Rename buffer" message-rename-buffer t]
754 ["Spellcheck" ispell-message t]
755 "----"
756 ["Send Message" message-send-and-exit t]
757 ["Abort Message" message-dont-send t]))
758
759(defvar facemenu-add-face-function)
760(defvar facemenu-remove-face-function)
761
762;;;###autoload
763(defun message-mode ()
764 "Major mode for editing mail and news to be sent.
765Like Text Mode but with these additional commands:
766C-c C-s message-send (send the message) C-c C-c message-send-and-exit
767C-c C-f move to a header field (and create it if there isn't):
768 C-c C-f C-t move to To C-c C-f C-s move to Subject
769 C-c C-f C-c move to Cc C-c C-f C-b move to Bcc
770 C-c C-f C-f move to Fcc C-c C-f C-r move to Reply-To
771 C-c C-f C-u move to Summary C-c C-f C-n move to Newsgroups
772 C-c C-f C-k move to Keywords C-c C-f C-d move to Distribution
773 C-c C-f C-o move to Followup-To
774C-c C-t message-insert-to (add a To header to a news followup)
775C-c C-n message-insert-newsgroups (add a Newsgroup header to a news reply)
776C-c C-b message-goto-body (move to beginning of message text).
777C-c C-i message-goto-signature (move to the beginning of the signature).
778C-c C-w message-insert-signature (insert `message-signature-file' file).
779C-c C-y message-yank-original (insert current message, if any).
780C-c C-q message-fill-yanked-message (fill what was yanked).
781C-c C-r message-ceasar-buffer-body (rot13 the message body)."
782 (interactive)
783 (kill-all-local-variables)
784 (make-local-variable 'message-reply-buffer)
785 (setq message-reply-buffer nil)
786 (make-local-variable 'message-send-actions)
787 (make-local-variable 'message-exit-actions)
788 (make-local-variable 'message-kill-actions)
789 (make-local-variable 'message-postpone-actions)
790 (set-syntax-table message-mode-syntax-table)
791 (use-local-map message-mode-map)
792 (setq local-abbrev-table text-mode-abbrev-table)
793 (setq major-mode 'message-mode)
794 (setq mode-name "Message")
795 (setq buffer-offer-save t)
796 (make-local-variable 'font-lock-defaults)
797 (setq font-lock-defaults '(message-font-lock-keywords t))
798 (make-local-variable 'facemenu-add-face-function)
799 (make-local-variable 'facemenu-remove-face-function)
800 (setq facemenu-add-face-function
801 (lambda (face end)
802 (let ((face-fun (cdr (assq face message-face-alist))))
803 (if face-fun
804 (funcall face-fun (point) end)
805 (error "Face %s not configured for %s mode" face mode-name)))
806 "")
807 facemenu-remove-face-function t)
808 (make-local-variable 'paragraph-separate)
809 (make-local-variable 'paragraph-start)
810 (setq paragraph-start (concat (regexp-quote mail-header-separator)
811 "$\\|[ \t]*[-_][-_][-_]+$\\|"
812 "-- $\\|"
813 paragraph-start))
814 (setq paragraph-separate (concat (regexp-quote mail-header-separator)
815 "$\\|[ \t]*[-_][-_][-_]+$\\|"
816 "-- $\\|"
817 paragraph-separate))
818 (make-local-variable 'message-reply-headers)
819 (setq message-reply-headers nil)
820 (make-local-variable 'message-newsreader)
821 (make-local-variable 'message-mailer)
822 (make-local-variable 'message-post-method)
823 (make-local-variable 'message-sent-message-via)
824 (setq message-sent-message-via nil)
825 (make-local-variable 'message-checksum)
826 (setq message-checksum nil)
827 (when (fboundp 'mail-hist-define-keys)
828 (mail-hist-define-keys))
829 (when (string-match "XEmacs\\|Lucid" emacs-version)
830 (message-setup-toolbar))
831 (easy-menu-add message-mode-menu message-mode-map)
832 ;; Allow mail alias things.
833 (if (fboundp 'mail-abbrevs-setup)
834 (mail-abbrevs-setup)
835 (funcall (intern "mail-aliases-setup")))
836 (run-hooks 'text-mode-hook 'message-mode-hook))
837
838\f
839
840;;;
841;;; Message mode commands
842;;;
843
844;;; Movement commands
845
846(defun message-goto-to ()
847 "Move point to the To header."
848 (interactive)
849 (message-position-on-field "To"))
850
851(defun message-goto-subject ()
852 "Move point to the Subject header."
853 (interactive)
854 (message-position-on-field "Subject"))
855
856(defun message-goto-cc ()
857 "Move point to the Cc header."
858 (interactive)
859 (message-position-on-field "Cc" "To"))
860
861(defun message-goto-bcc ()
862 "Move point to the Bcc header."
863 (interactive)
864 (message-position-on-field "Bcc" "Cc" "To"))
865
866(defun message-goto-fcc ()
867 "Move point to the Fcc header."
868 (interactive)
869 (message-position-on-field "Fcc" "To" "Newsgroups"))
870
871(defun message-goto-reply-to ()
872 "Move point to the Reply-To header."
873 (interactive)
874 (message-position-on-field "Reply-To" "Subject"))
875
876(defun message-goto-newsgroups ()
877 "Move point to the Newsgroups header."
878 (interactive)
879 (message-position-on-field "Newsgroups"))
880
881(defun message-goto-distribution ()
882 "Move point to the Distribution header."
883 (interactive)
884 (message-position-on-field "Distribution"))
885
886(defun message-goto-followup-to ()
887 "Move point to the Followup-To header."
888 (interactive)
889 (message-position-on-field "Followup-To" "Newsgroups"))
890
891(defun message-goto-keywords ()
892 "Move point to the Keywords header."
893 (interactive)
894 (message-position-on-field "Keywords" "Subject"))
895
896(defun message-goto-summary ()
897 "Move point to the Summary header."
898 (interactive)
899 (message-position-on-field "Summary" "Subject"))
900
901(defun message-goto-body ()
902 "Move point to the beginning of the message body."
903 (interactive)
904 (if (looking-at "[ \t]*\n") (expand-abbrev))
905 (goto-char (point-min))
906 (search-forward (concat "\n" mail-header-separator "\n") nil t))
907
908(defun message-goto-signature ()
909 "Move point to the beginning of the message signature."
910 (interactive)
911 (goto-char (point-min))
912 (or (re-search-forward message-signature-separator nil t)
913 (goto-char (point-max))))
914
915\f
916
917(defun message-insert-to ()
918 "Insert a To header that points to the author of the article being replied to."
919 (interactive)
920 (when (and (message-position-on-field "To")
921 (mail-fetch-field "to")
922 (not (string-match "\\` *\\'" (mail-fetch-field "to"))))
923 (insert ", "))
924 (insert (or (message-fetch-reply-field "reply-to")
925 (message-fetch-reply-field "from") "")))
926
927(defun message-insert-newsgroups ()
928 "Insert the Newsgroups header from the article being replied to."
929 (interactive)
930 (when (and (message-position-on-field "Newsgroups")
931 (mail-fetch-field "newsgroups")
932 (not (string-match "\\` *\\'" (mail-fetch-field "newsgroups"))))
933 (insert ","))
934 (insert (or (message-fetch-reply-field "newsgroups") "")))
935
936\f
937
938;;; Various commands
939
940(defun message-insert-signature (&optional force)
941 "Insert a signature. See documentation for the `message-signature' variable."
942 (interactive (list 0))
943 (let* ((signature
944 (cond ((and (null message-signature)
945 (eq force 0))
946 (save-excursion
947 (goto-char (point-max))
948 (not (re-search-backward
949 message-signature-separator nil t))))
950 ((and (null message-signature)
951 force)
952 t)
953 ((message-functionp message-signature)
954 (funcall message-signature))
955 ((listp message-signature)
956 (eval message-signature))
957 (t message-signature)))
958 (signature
959 (cond ((stringp signature)
960 signature)
961 ((and (eq t signature)
962 message-signature-file
963 (file-exists-p message-signature-file))
964 signature))))
965 (when signature
966; ;; Remove blank lines at the end of the message.
967 (goto-char (point-max))
968; (skip-chars-backward " \t\n")
969; (delete-region (point) (point-max))
970 ;; Insert the signature.
971 (unless (bolp)
972 (insert "\n"))
973 (insert "\n-- \n")
974 (if (eq signature t)
975 (insert-file-contents message-signature-file)
976 (insert signature))
977 (goto-char (point-max))
978 (or (bolp) (insert "\n")))))
979
980(defvar message-caesar-translation-table nil)
981
982(defun message-caesar-region (b e &optional n)
983 "Caesar rotation of region by N, default 13, for decrypting netnews."
984 (interactive
985 (list
986 (min (point) (or (mark t) (point)))
987 (max (point) (or (mark t) (point)))
988 (when current-prefix-arg
989 (prefix-numeric-value current-prefix-arg))))
990
991 (setq n (if (numberp n) (mod n 26) 13)) ;canonize N
992 (unless (or (zerop n) ; no action needed for a rot of 0
993 (= b e)) ; no region to rotate
994 ;; We build the table, if necessary.
995 (when (or (not message-caesar-translation-table)
996 (/= (aref message-caesar-translation-table ?a) (+ ?a n)))
997 (let ((i -1)
998 (table (make-string 256 0)))
999 (while (< (incf i) 256)
1000 (aset table i i))
1001 (setq table
1002 (concat
1003 (substring table 0 ?A)
1004 (substring table (+ ?A n) (+ ?A n (- 26 n)))
1005 (substring table ?A (+ ?A n))
1006 (substring table (+ ?A 26) ?a)
1007 (substring table (+ ?a n) (+ ?a n (- 26 n)))
1008 (substring table ?a (+ ?a n))
1009 (substring table (+ ?a 26) 255)))
1010 (setq message-caesar-translation-table table)))
1011 ;; Then we translate the region. Do it this way to retain
1012 ;; text properties.
1013 (while (< b e)
1014 (subst-char-in-region
1015 b (1+ b) (char-after b)
1016 (aref message-caesar-translation-table (char-after b)))
1017 (incf b))))
1018
1019(defun message-caesar-buffer-body (&optional rotnum)
1020 "Caesar rotates all letters in the current buffer by 13 places.
1021Used to encode/decode possibly offensive messages (commonly in net.jokes).
1022With prefix arg, specifies the number of places to rotate each letter forward.
1023Mail and USENET news headers are not rotated."
1024 (interactive (if current-prefix-arg
1025 (list (prefix-numeric-value current-prefix-arg))
1026 (list nil)))
1027 (save-excursion
1028 (save-restriction
1029 (when (message-goto-body)
1030 (narrow-to-region (point) (point-max)))
1031 (message-caesar-region (point-min) (point-max) rotnum))))
1032
1033(defun message-rename-buffer (&optional enter-string)
1034 "Rename the *message* buffer to \"*message* RECIPIENT\".
1035If the function is run with a prefix, it will ask for a new buffer
1036name, rather than giving an automatic name."
1037 (interactive "Pbuffer name: ")
1038 (save-excursion
1039 (save-restriction
1040 (goto-char (point-min))
1041 (narrow-to-region (point)
1042 (search-forward mail-header-separator nil 'end))
1043 (let* ((mail-to (if (message-news-p) (message-fetch-field "Newsgroups")
1044 (message-fetch-field "To")))
1045 (mail-trimmed-to
1046 (if (string-match "," mail-to)
1047 (concat (substring mail-to 0 (match-beginning 0)) ", ...")
1048 mail-to))
1049 (name-default (concat "*message* " mail-trimmed-to))
1050 (name (if enter-string
1051 (read-string "New buffer name: " name-default)
1052 name-default)))
1053 (rename-buffer name t)))))
1054
1055(defun message-fill-yanked-message (&optional justifyp)
1056 "Fill the paragraphs of a message yanked into this one.
1057Numeric argument means justify as well."
1058 (interactive "P")
1059 (save-excursion
1060 (goto-char (point-min))
1061 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1062 (let ((fill-prefix message-yank-prefix))
1063 (fill-individual-paragraphs (point) (point-max) justifyp t))))
1064
1065(defun message-indent-citation ()
1066 "Modify text just inserted from a message to be cited.
1067The inserted text should be the region.
1068When this function returns, the region is again around the modified text.
1069
1070Normally, indent each nonblank line `message-indentation-spaces' spaces.
1071However, if `message-yank-prefix' is non-nil, insert that prefix on each line."
1072 (let ((start (point)))
1073 ;; Remove unwanted headers.
1074 (when message-ignored-cited-headers
1075 (save-restriction
1076 (narrow-to-region
1077 (goto-char start)
1078 (if (search-forward "\n\n" nil t)
1079 (1- (point))
1080 (point)))
1081 (message-remove-header message-ignored-cited-headers t)))
1082 ;; Do the indentation.
1083 (if (null message-yank-prefix)
1084 (indent-rigidly start (mark t) message-indentation-spaces)
1085 (save-excursion
1086 (goto-char start)
1087 (while (< (point) (mark t))
1088 (insert message-yank-prefix)
1089 (forward-line 1)))
1090 (goto-char start))))
1091
1092(defun message-yank-original (&optional arg)
1093 "Insert the message being replied to, if any.
1094Puts point before the text and mark after.
1095Normally indents each nonblank line ARG spaces (default 3). However,
1096if `message-yank-prefix' is non-nil, insert that prefix on each line.
1097
1098Just \\[universal-argument] as argument means don't indent, insert no
1099prefix, and don't delete any headers."
1100 (interactive "P")
1101 (let ((modified (buffer-modified-p)))
1102 (when (and message-reply-buffer
1103 message-cite-function)
1104 (delete-windows-on message-reply-buffer t)
1105 (insert-buffer message-reply-buffer)
1106 (funcall message-cite-function)
1107 (message-exchange-point-and-mark)
1108 (unless (bolp)
1109 (insert ?\n))
1110 (unless modified
1111 (setq message-checksum (cons (message-checksum) (buffer-size)))))))
1112
1113(defun message-cite-original ()
1114 (let ((start (point))
1115 (functions
1116 (when message-indent-citation-function
1117 (if (listp message-indent-citation-function)
1118 message-indent-citation-function
1119 (list message-indent-citation-function)))))
1120 (goto-char start)
1121 (while functions
1122 (funcall (pop functions)))
1123 (when message-citation-line-function
1124 (unless (bolp)
1125 (insert "\n"))
1126 (funcall message-citation-line-function))))
1127
1128(defun message-insert-citation-line ()
1129 "Function that inserts a simple citation line."
1130 (when message-reply-headers
1131 (insert (mail-header-from message-reply-headers) " writes:\n\n")))
1132
1133(defun message-position-on-field (header &rest afters)
1134 (let ((case-fold-search t))
1135 (save-restriction
1136 (narrow-to-region
1137 (goto-char (point-min))
1138 (progn
1139 (re-search-forward
1140 (concat "^" (regexp-quote mail-header-separator) "$"))
1141 (match-beginning 0)))
1142 (goto-char (point-min))
1143 (if (re-search-forward (concat "^" (regexp-quote header) ":") nil t)
1144 (progn
1145 (re-search-forward "^[^ \t]" nil 'move)
1146 (beginning-of-line)
1147 (skip-chars-backward "\n")
1148 t)
1149 (while (and afters
1150 (not (re-search-forward
1151 (concat "^" (regexp-quote (car afters)) ":")
1152 nil t)))
1153 (pop afters))
1154 (when afters
1155 (re-search-forward "^[^ \t]" nil 'move)
1156 (beginning-of-line))
1157 (insert header ": \n")
1158 (forward-char -1)
1159 nil))))
1160
1161(defun message-remove-signature ()
1162 "Remove the signature from the text between point and mark.
1163The text will also be indented the normal way."
1164 (save-excursion
1165 (let ((start (point))
1166 mark)
1167 (if (not (re-search-forward message-signature-separator (mark t) t))
1168 ;; No signature here, so we just indent the cited text.
1169 (message-indent-citation)
1170 ;; Find the last non-empty line.
1171 (forward-line -1)
1172 (while (looking-at "[ \t]*$")
1173 (forward-line -1))
1174 (forward-line 1)
1175 (setq mark (set-marker (make-marker) (point)))
1176 (goto-char start)
1177 (message-indent-citation)
1178 ;; Enable undoing the deletion.
1179 (undo-boundary)
1180 (delete-region mark (mark t))
1181 (set-marker mark nil)))))
1182
1183\f
1184
1185;;;
1186;;; Sending messages
1187;;;
1188
1189(defun message-send-and-exit (&optional arg)
1190 "Send message like `message-send', then, if no errors, exit from mail buffer."
1191 (interactive "P")
1192 (let ((buf (current-buffer))
1193 (actions message-exit-actions))
1194 (when (and (message-send arg)
1195 (buffer-name buf))
1196 (if message-kill-buffer-on-exit
1197 (kill-buffer buf)
1198 (bury-buffer buf)
1199 (when (eq buf (current-buffer))
1200 (message-bury buf)))
1201 (message-do-actions actions))))
1202
1203(defun message-dont-send ()
1204 "Don't send the message you have been editing."
1205 (interactive)
1206 (message-bury (current-buffer))
1207 (message-do-actions message-postpone-actions))
1208
1209(defun message-kill-buffer ()
1210 "Kill the current buffer."
1211 (interactive)
1212 (let ((actions message-kill-actions))
1213 (kill-buffer (current-buffer))
1214 (message-do-actions actions)))
1215
1216(defun message-bury (buffer)
1217 "Bury this mail buffer."
1218 (let ((newbuf (other-buffer buffer)))
1219 (bury-buffer buffer)
1220 (if (and (fboundp 'frame-parameters)
1221 (cdr (assq 'dedicated (frame-parameters)))
1222 (not (null (delq (selected-frame) (visible-frame-list)))))
1223 (delete-frame (selected-frame))
1224 (switch-to-buffer newbuf))))
1225
1226(defun message-send (&optional arg)
1227 "Send the message in the current buffer.
1228If `message-interactive' is non-nil, wait for success indication
1229or error messages, and inform user.
1230Otherwise any failure is reported in a message back to
1231the user from the mailer."
1232 (interactive "P")
1233 (when (if buffer-file-name
1234 (y-or-n-p (format "Send buffer contents as %s message? "
1235 (if (message-mail-p)
1236 (if (message-news-p) "mail and news" "mail")
1237 "news")))
1238 (or (buffer-modified-p)
1239 (y-or-n-p "No changes in the buffer; really send? ")))
1240 ;; Make it possible to undo the coming changes.
1241 (undo-boundary)
1242 (let ((inhibit-read-only t))
1243 (put-text-property (point-min) (point-max) 'read-only nil))
1244 (message-fix-before-sending)
1245 (run-hooks 'message-send-hook)
1246 (message "Sending...")
1247 (when (and (or (not (message-news-p))
1248 (and (or (not (memq 'news message-sent-message-via))
1249 (y-or-n-p
1250 "Already sent message via news; resend? "))
1251 (funcall message-send-news-function arg)))
1252 (or (not (message-mail-p))
1253 (and (or (not (memq 'mail message-sent-message-via))
1254 (y-or-n-p
1255 "Already sent message via mail; resend? "))
1256 (message-send-mail arg))))
1257 (message-do-fcc)
1258 (when (fboundp 'mail-hist-put-headers-into-history)
1259 (mail-hist-put-headers-into-history))
1260 (run-hooks 'message-sent-hook)
1261 (message "Sending...done")
1262 ;; If buffer has no file, mark it as unmodified and delete autosave.
1263 (unless buffer-file-name
1264 (set-buffer-modified-p nil)
1265 (delete-auto-save-file-if-necessary t))
1266 ;; Delete other mail buffers and stuff.
1267 (message-do-send-housekeeping)
1268 (message-do-actions message-send-actions)
1269 ;; Return success.
1270 t)))
1271
1272(defun message-fix-before-sending ()
1273 "Do various things to make the message nice before sending it."
1274 ;; Make sure there's a newline at the end of the message.
1275 (goto-char (point-max))
1276 (unless (bolp)
1277 (insert "\n")))
1278
1279(defun message-add-action (action &rest types)
1280 "Add ACTION to be performed when doing an exit of type TYPES."
1281 (let (var)
1282 (while types
1283 (set (setq var (intern (format "message-%s-actions" (pop types))))
1284 (nconc (symbol-value var) (list action))))))
1285
1286(defun message-do-actions (actions)
1287 "Perform all actions in ACTIONS."
1288 ;; Now perform actions on successful sending.
1289 (while actions
1290 (condition-case nil
1291 (cond
1292 ;; A simple function.
1293 ((message-functionp (car actions))
1294 (funcall (car actions)))
1295 ;; Something to be evaled.
1296 (t
1297 (eval (car actions))))
1298 (error))
1299 (pop actions)))
1300
1301(defun message-send-mail (&optional arg)
1302 (require 'mail-utils)
1303 (let ((tembuf (generate-new-buffer " message temp"))
1304 (case-fold-search nil)
1305 (news (message-news-p))
1306 (mailbuf (current-buffer)))
1307 (save-restriction
1308 (message-narrow-to-headers)
1309 ;; Insert some headers.
1310 (let ((message-deletable-headers
1311 (if news nil message-deletable-headers)))
1312 (message-generate-headers message-required-mail-headers))
1313 ;; Let the user do all of the above.
1314 (run-hooks 'message-header-hook))
1315 (unwind-protect
1316 (save-excursion
1317 (set-buffer tembuf)
1318 (erase-buffer)
1319 (insert-buffer-substring mailbuf)
1320 ;; Remove some headers.
1321 (save-restriction
1322 (message-narrow-to-headers)
1323 ;; Remove some headers.
1324 (message-remove-header message-ignored-mail-headers t))
1325 (goto-char (point-max))
1326 ;; require one newline at the end.
1327 (or (= (preceding-char) ?\n)
1328 (insert ?\n))
1329 (when (and news
1330 (or (message-fetch-field "cc")
1331 (message-fetch-field "to")))
1332 (message-insert-courtesy-copy))
1333 (funcall message-send-mail-function))
1334 (kill-buffer tembuf))
1335 (set-buffer mailbuf)
1336 (push 'mail message-sent-message-via)))
1337
1338(defun message-send-mail-with-sendmail ()
1339 "Send off the prepared buffer with sendmail."
1340 (let ((errbuf (if message-interactive
1341 (generate-new-buffer " sendmail errors")
1342 0))
1343 resend-to-addresses delimline)
1344 (let ((case-fold-search t))
1345 (save-restriction
1346 (message-narrow-to-headers)
1347 (setq resend-to-addresses (message-fetch-field "resent-to")))
1348 ;; Change header-delimiter to be what sendmail expects.
1349 (goto-char (point-min))
1350 (re-search-forward
1351 (concat "^" (regexp-quote mail-header-separator) "\n"))
1352 (replace-match "\n")
1353 (backward-char 1)
1354 (setq delimline (point-marker))
1355 ;; Insert an extra newline if we need it to work around
1356 ;; Sun's bug that swallows newlines.
1357 (goto-char (1+ delimline))
1358 (when (eval message-mailer-swallows-blank-line)
1359 (newline))
1360 (when message-interactive
1361 (save-excursion
1362 (set-buffer errbuf)
1363 (erase-buffer))))
1364 (let ((default-directory "/"))
1365 (apply 'call-process-region
1366 (append (list (point-min) (point-max)
1367 (if (boundp 'sendmail-program)
1368 sendmail-program
1369 "/usr/lib/sendmail")
1370 nil errbuf nil "-oi")
1371 ;; Always specify who from,
1372 ;; since some systems have broken sendmails.
1373 (list "-f" (user-login-name))
1374 ;; These mean "report errors by mail"
1375 ;; and "deliver in background".
1376 (if (null message-interactive) '("-oem" "-odb"))
1377 ;; Get the addresses from the message
1378 ;; unless this is a resend.
1379 ;; We must not do that for a resend
1380 ;; because we would find the original addresses.
1381 ;; For a resend, include the specific addresses.
1382 (if resend-to-addresses
1383 (list resend-to-addresses)
1384 '("-t")))))
1385 (when message-interactive
1386 (save-excursion
1387 (set-buffer errbuf)
1388 (goto-char (point-min))
1389 (while (re-search-forward "\n\n* *" nil t)
1390 (replace-match "; "))
1391 (if (not (zerop (buffer-size)))
1392 (error "Sending...failed to %s"
1393 (buffer-substring (point-min) (point-max)))))
1394 (when (bufferp errbuf)
1395 (kill-buffer errbuf)))))
1396
1397(defun message-send-mail-with-mh ()
1398 "Send the prepared message buffer with mh."
1399 (let ((mh-previous-window-config nil)
1400 (name (make-temp-name
1401 (concat (file-name-as-directory message-autosave-directory)
1402 "msg."))))
1403 (setq buffer-file-name name)
1404 (mh-send-letter)
1405 (condition-case ()
1406 (delete-file name)
1407 (error nil))))
1408
1409(defun message-send-news (&optional arg)
1410 (let ((tembuf (generate-new-buffer " *message temp*"))
1411 (case-fold-search nil)
1412 (method (if (message-functionp message-post-method)
1413 (funcall message-post-method arg)
1414 message-post-method))
1415 (messbuf (current-buffer))
1416 (message-syntax-checks
1417 (if arg
1418 (cons '(existing-newsgroups . disabled)
1419 message-syntax-checks)
1420 message-syntax-checks))
1421 result)
1422 (save-restriction
1423 (message-narrow-to-headers)
1424 ;; Insert some headers.
1425 (message-generate-headers message-required-news-headers)
1426 ;; Let the user do all of the above.
1427 (run-hooks 'message-header-hook))
1428 (message-cleanup-headers)
1429 (when (message-check-news-syntax)
1430 (unwind-protect
1431 (save-excursion
1432 (set-buffer tembuf)
1433 (buffer-disable-undo (current-buffer))
1434 (erase-buffer)
1435 (insert-buffer-substring messbuf)
1436 ;; Remove some headers.
1437 (save-restriction
1438 (message-narrow-to-headers)
1439 ;; Remove some headers.
1440 (message-remove-header message-ignored-news-headers t))
1441 (goto-char (point-max))
1442 ;; require one newline at the end.
1443 (or (= (preceding-char) ?\n)
1444 (insert ?\n))
1445 (let ((case-fold-search t))
1446 ;; Remove the delimeter.
1447 (goto-char (point-min))
1448 (re-search-forward
1449 (concat "^" (regexp-quote mail-header-separator) "\n"))
1450 (replace-match "\n")
1451 (backward-char 1))
1452 (require (car method))
1453 (funcall (intern (format "%s-open-server" (car method)))
1454 (cadr method) (cddr method))
1455 (setq result
1456 (funcall (intern (format "%s-request-post" (car method))))))
1457 (kill-buffer tembuf))
1458 (set-buffer messbuf)
1459 (if result
1460 (push 'news message-sent-message-via)
1461 (message "Couldn't send message via news: %s"
1462 (nnheader-get-report (car method)))
1463 nil))))
1464
1465;;;
1466;;; Header generation & syntax checking.
1467;;;
1468
1469(defun message-check-news-syntax ()
1470 "Check the syntax of the message."
1471 (and
1472 ;; We narrow to the headers and check them first.
1473 (save-excursion
1474 (save-restriction
1475 (message-narrow-to-headers)
1476 (and
1477 ;; Check for commands in Subject.
1478 (or
1479 (message-check-element 'subject-cmsg)
1480 (save-excursion
1481 (if (string-match "^cmsg " (message-fetch-field "subject"))
1482 (y-or-n-p
1483 "The control code \"cmsg \" is in the subject. Really post? ")
1484 t)))
1485 ;; Check for multiple identical headers.
1486 (or (message-check-element 'multiple-headers)
1487 (save-excursion
1488 (let (found)
1489 (while (and (not found)
1490 (re-search-forward "^[^ \t:]+: " nil t))
1491 (save-excursion
1492 (or (re-search-forward
1493 (concat "^" (setq found
1494 (buffer-substring
1495 (match-beginning 0)
1496 (- (match-end 0) 2))))
1497 nil t)
1498 (setq found nil))))
1499 (if found
1500 (y-or-n-p
1501 (format "Multiple %s headers. Really post? " found))
1502 t))))
1503 ;; Check for Version and Sendsys.
1504 (or (message-check-element 'sendsys)
1505 (save-excursion
1506 (if (re-search-forward "^Sendsys:\\|^Version:" nil t)
1507 (y-or-n-p
1508 (format "The article contains a %s command. Really post? "
1509 (buffer-substring (match-beginning 0)
1510 (1- (match-end 0)))))
1511 t)))
1512 ;; See whether we can shorten Followup-To.
1513 (or (message-check-element 'shorten-followup-to)
1514 (let ((newsgroups (message-fetch-field "newsgroups"))
1515 (followup-to (message-fetch-field "followup-to"))
1516 to)
1517 (when (and newsgroups (string-match "," newsgroups)
1518 (not followup-to)
1519 (not
1520 (zerop
1521 (length
1522 (setq to (completing-read
1523 "Followups to: (default all groups) "
1524 (mapcar (lambda (g) (list g))
1525 (cons "poster"
1526 (message-tokenize-header
1527 newsgroups)))))))))
1528 (goto-char (point-min))
1529 (insert "Followup-To: " to "\n"))
1530 t))
1531 ;; Check "Shoot me".
1532 (or (message-check-element 'shoot)
1533 (save-excursion
1534 (if (search-forward
1535 ".i-have-a-misconfigured-system-so-shoot-me" nil t)
1536 (y-or-n-p
1537 "You appear to have a misconfigured system. Really post? ")
1538 t)))
1539 ;; Check for Approved.
1540 (or (message-check-element 'approved)
1541 (save-excursion
1542 (if (re-search-forward "^Approved:" nil t)
1543 (y-or-n-p
1544 "The article contains an Approved header. Really post? ")
1545 t)))
1546 ;; Check the Message-Id header.
1547 (or (message-check-element 'message-id)
1548 (save-excursion
1549 (let* ((case-fold-search t)
1550 (message-id (message-fetch-field "message-id")))
1551 (or (not message-id)
1552 (and (string-match "@" message-id)
1553 (string-match "@[^\\.]*\\." message-id))
1554 (y-or-n-p
1555 (format
1556 "The Message-ID looks strange: \"%s\". Really post? "
1557 message-id))))))
1558 ;; Check the Subject header.
1559 (or
1560 (message-check-element 'subject)
1561 (save-excursion
1562 (let* ((case-fold-search t)
1563 (subject (message-fetch-field "subject")))
1564 (or
1565 (and subject
1566 (not (string-match "\\`[ \t]*\\'" subject)))
1567 (progn
1568 (message
1569 "The subject field is empty or missing. Posting is denied.")
1570 nil)))))
1571 ;; Check the Newsgroups & Followup-To headers.
1572 (or
1573 (message-check-element 'existing-newsgroups)
1574 (let* ((case-fold-search t)
1575 (newsgroups (message-fetch-field "newsgroups"))
1576 (followup-to (message-fetch-field "followup-to"))
1577 (groups (message-tokenize-header
1578 (if followup-to
1579 (concat newsgroups "," followup-to)
1580 newsgroups)))
1581 (hashtb (and (boundp 'gnus-active-hashtb)
1582 gnus-active-hashtb))
1583 errors)
1584 (if (not hashtb)
1585 t
1586 (while groups
1587 (when (and (not (boundp (intern (car groups) hashtb)))
1588 (not (equal (car groups) "poster")))
1589 (push (car groups) errors))
1590 (pop groups))
1591 (if (not errors)
1592 t
1593 (y-or-n-p
1594 (format
1595 "Really post to %s unknown group%s: %s "
1596 (if (= (length errors) 1) "this" "these")
1597 (if (= (length errors) 1) "" "s")
1598 (mapconcat 'identity errors ", ")))))))
1599 ;; Check the Newsgroups & Followup-To headers for syntax errors.
1600 (or
1601 (message-check-element 'valid-newsgroups)
1602 (let ((case-fold-search t)
1603 (headers '("Newsgroups" "Followup-To"))
1604 header error)
1605 (while (and headers (not error))
1606 (when (setq header (mail-fetch-field (car headers)))
1607 (if (or
1608 (not
1609 (string-match
1610 "\\`\\([-+_&.a-zA-Z0-9]+\\)?\\(,[-.a-zA-Z0-9]+\\)*\\'"
1611 header))
1612 (memq
1613 nil (mapcar
1614 (lambda (g)
1615 (not (string-match "\\.\\'\\|\\.\\." g)))
1616 (message-tokenize-header header ","))))
1617 (setq error t)))
1618 (unless error
1619 (pop headers)))
1620 (if (not error)
1621 t
1622 (y-or-n-p
1623 (format "The %s header looks odd: \"%s\". Really post? "
1624 (car headers) header)))))
1625 ;; Check the From header.
1626 (or
1627 (save-excursion
1628 (let* ((case-fold-search t)
1629 (from (message-fetch-field "from")))
1630 (cond
1631 ((not from)
1632 (message "There is no From line. Posting is denied.")
1633 nil)
1634 ((not (string-match "@[^\\.]*\\." from))
1635 (message
1636 "Denied posting -- the From looks strange: \"%s\"." from)
1637 nil)
1638 ((string-match "@[^@]*@" from)
1639 (message
1640 "Denied posting -- two \"@\"'s in the From header: %s." from)
1641 nil)
1642 ((string-match "(.*).*(.*)" from)
1643 (message
1644 "Denied posting -- the From header looks strange: \"%s\"."
1645 from)
1646 nil)
1647 (t t))))))))
1648 ;; Check for long lines.
1649 (or (message-check-element 'long-lines)
1650 (save-excursion
1651 (goto-char (point-min))
1652 (re-search-forward
1653 (concat "^" (regexp-quote mail-header-separator) "$"))
1654 (while (and
1655 (progn
1656 (end-of-line)
1657 (< (current-column) 80))
1658 (zerop (forward-line 1))))
1659 (or (bolp)
1660 (eobp)
1661 (y-or-n-p
1662 "You have lines longer than 79 characters. Really post? "))))
1663 ;; Check whether the article is empty.
1664 (or (message-check-element 'empty)
1665 (save-excursion
1666 (goto-char (point-min))
1667 (re-search-forward
1668 (concat "^" (regexp-quote mail-header-separator) "$"))
1669 (forward-line 1)
1670 (let ((b (point)))
1671 (or (re-search-forward message-signature-separator nil t)
1672 (goto-char (point-max)))
1673 (beginning-of-line)
1674 (or (re-search-backward "[^ \n\t]" b t)
1675 (y-or-n-p "Empty article. Really post? ")))))
1676 ;; Check for control characters.
1677 (or (message-check-element 'control-chars)
1678 (save-excursion
1679 (if (re-search-forward "[\000-\007\013\015-\037\200-\237]" nil t)
1680 (y-or-n-p
1681 "The article contains control characters. Really post? ")
1682 t)))
1683 ;; Check excessive size.
1684 (or (message-check-element 'size)
1685 (if (> (buffer-size) 60000)
1686 (y-or-n-p
1687 (format "The article is %d octets long. Really post? "
1688 (buffer-size)))
1689 t))
1690 ;; Check whether any new text has been added.
1691 (or (message-check-element 'new-text)
1692 (not message-checksum)
1693 (not (and (eq (message-checksum) (car message-checksum))
1694 (eq (buffer-size) (cdr message-checksum))))
1695 (y-or-n-p
1696 "It looks like no new text has been added. Really post? "))
1697 ;; Check the length of the signature.
1698 (or
1699 (message-check-element 'signature)
1700 (progn
1701 (goto-char (point-max))
1702 (if (or (not (re-search-backward "^-- $" nil t))
1703 (search-forward message-forward-end-separator nil t))
1704 t
1705 (if (> (count-lines (point) (point-max)) 5)
1706 (y-or-n-p
1707 (format
1708 "Your .sig is %d lines; it should be max 4. Really post? "
1709 (count-lines (point) (point-max))))
1710 t))))))
1711
1712(defun message-check-element (type)
1713 "Returns non-nil if this type is not to be checked."
1714 (if (eq message-syntax-checks 'dont-check-for-anything-just-trust-me)
1715 t
1716 (let ((able (assq type message-syntax-checks)))
1717 (and (consp able)
1718 (eq (cdr able) 'disabled)))))
1719
1720(defun message-checksum ()
1721 "Return a \"checksum\" for the current buffer."
1722 (let ((sum 0))
1723 (save-excursion
1724 (goto-char (point-min))
1725 (re-search-forward
1726 (concat "^" (regexp-quote mail-header-separator) "$"))
1727 (while (not (eobp))
1728 (when (not (looking-at "[ \t\n]"))
1729 (setq sum (logxor (ash sum 1) (following-char))))
1730 (forward-char 1)))
1731 sum))
1732
1733(defun message-do-fcc ()
1734 "Process Fcc headers in the current buffer."
1735 (let ((case-fold-search t)
1736 (buf (current-buffer))
1737 list file)
1738 (save-excursion
1739 (set-buffer (get-buffer-create " *message temp*"))
1740 (buffer-disable-undo (current-buffer))
1741 (erase-buffer)
1742 (insert-buffer-substring buf)
1743 (save-restriction
1744 (message-narrow-to-headers)
1745 (while (setq file (message-fetch-field "fcc"))
1746 (push file list)
1747 (message-remove-header "fcc" nil t)))
1748 (goto-char (point-min))
1749 (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1750 (replace-match "" t t)
1751 ;; Process FCC operations.
1752 (while list
1753 (setq file (pop list))
1754 (if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
1755 ;; Pipe the article to the program in question.
1756 (call-process-region (point-min) (point-max) shell-file-name
1757 nil nil nil shell-command-switch
1758 (match-string 1 file))
1759 ;; Save the article.
1760 (setq file (expand-file-name file))
1761 (unless (file-exists-p (file-name-directory file))
1762 (make-directory (file-name-directory file) t))
1763 (if (and message-fcc-handler-function
1764 (not (eq message-fcc-handler-function 'rmail-output)))
1765 (funcall message-fcc-handler-function file)
1766 (if (and (file-readable-p file) (mail-file-babyl-p file))
1767 (rmail-output file 1)
1768 (let ((mail-use-rfc822 t))
1769 (rmail-output file 1 t t))))))
1770 (kill-buffer (current-buffer)))))
1771
1772(defun message-cleanup-headers ()
1773 "Do various automatic cleanups of the headers."
1774 ;; Remove empty lines in the header.
1775 (save-restriction
1776 (message-narrow-to-headers)
1777 (while (re-search-forward "^[ \t]*\n" nil t)
1778 (replace-match "" t t)))
1779
1780 ;; Correct Newsgroups and Followup-To headers: change sequence of
1781 ;; spaces to comma and eliminate spaces around commas. Eliminate
1782 ;; embedded line breaks.
1783 (goto-char (point-min))
1784 (while (re-search-forward "^\\(Newsgroups\\|Followup-To\\): +" nil t)
1785 (save-restriction
1786 (narrow-to-region
1787 (point)
1788 (if (re-search-forward "^[^ \t]" nil t)
1789 (match-beginning 0)
1790 (forward-line 1)
1791 (point)))
1792 (goto-char (point-min))
1793 (while (re-search-forward "\n[ \t]+" nil t)
1794 (replace-match " " t t)) ;No line breaks (too confusing)
1795 (goto-char (point-min))
1796 (while (re-search-forward "[ \t\n]*,[ \t\n]*\\|[ \t]+" nil t)
1797 (replace-match "," t t))
1798 (goto-char (point-min))
1799 ;; Remove trailing commas.
1800 (when (re-search-forward ",+$" nil t)
1801 (replace-match "" t t)))))
1802
1803(defun message-make-date ()
1804 "Make a valid data header."
1805 (let ((now (current-time)))
1806 (timezone-make-date-arpa-standard
1807 (current-time-string now) (current-time-zone now))))
1808
1809(defun message-make-message-id ()
1810 "Make a unique Message-ID."
1811 (concat "<" (message-unique-id)
1812 (let ((psubject (save-excursion (message-fetch-field "subject"))))
1813 (if (and message-reply-headers
1814 (mail-header-references message-reply-headers)
1815 (mail-header-subject message-reply-headers)
1816 psubject
1817 (mail-header-subject message-reply-headers)
1818 (not (string=
1819 (message-strip-subject-re
1820 (mail-header-subject message-reply-headers))
1821 (message-strip-subject-re psubject))))
1822 "_-_" ""))
1823 "@" (message-make-fqdn) ">"))
1824
1825(defvar message-unique-id-char nil)
1826
1827;; If you ever change this function, make sure the new version
1828;; cannot generate IDs that the old version could.
1829;; You might for example insert a "." somewhere (not next to another dot
1830;; or string boundary), or modify the "fsf" string.
1831(defun message-unique-id ()
1832 ;; Don't use microseconds from (current-time), they may be unsupported.
1833 ;; Instead we use this randomly inited counter.
1834 (setq message-unique-id-char
1835 (% (1+ (or message-unique-id-char (logand (random t) (1- (lsh 1 20)))))
1836 ;; (current-time) returns 16-bit ints,
1837 ;; and 2^16*25 just fits into 4 digits i base 36.
1838 (* 25 25)))
1839 (let ((tm (current-time)))
1840 (concat
1841 (if (memq system-type '(ms-dos emx vax-vms))
1842 (let ((user (downcase (user-login-name))))
1843 (while (string-match "[^a-z0-9_]" user)
1844 (aset user (match-beginning 0) ?_))
1845 user)
1846 (message-number-base36 (user-uid) -1))
1847 (message-number-base36 (+ (car tm)
1848 (lsh (% message-unique-id-char 25) 16)) 4)
1849 (message-number-base36 (+ (nth 1 tm)
1850 (lsh (/ message-unique-id-char 25) 16)) 4)
1851 ;; Append the newsreader name, because while the generated
1852 ;; ID is unique to this newsreader, other newsreaders might
1853 ;; otherwise generate the same ID via another algorithm.
1854 ".fsf")))
1855
1856(defun message-number-base36 (num len)
1857 (if (if (< len 0) (<= num 0) (= len 0))
1858 ""
1859 (concat (message-number-base36 (/ num 36) (1- len))
1860 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
1861 (% num 36))))))
1862
1863(defun message-make-organization ()
1864 "Make an Organization header."
1865 (let* ((organization
1866 (or (getenv "ORGANIZATION")
1867 (when message-user-organization
1868 (if (message-functionp message-user-organization)
1869 (funcall message-user-organization)
1870 message-user-organization)))))
1871 (save-excursion
1872 (message-set-work-buffer)
1873 (cond ((stringp organization)
1874 (insert organization))
1875 ((and (eq t organization)
1876 message-user-organization-file
1877 (file-exists-p message-user-organization-file))
1878 (insert-file-contents message-user-organization-file)))
1879 (goto-char (point-min))
1880 (while (re-search-forward "[\t\n]+" nil t)
1881 (replace-match "" t t))
1882 (unless (zerop (buffer-size))
1883 (buffer-string)))))
1884
1885(defun message-make-lines ()
1886 "Count the number of lines and return numeric string."
1887 (save-excursion
1888 (save-restriction
1889 (widen)
1890 (goto-char (point-min))
1891 (re-search-forward
1892 (concat "^" (regexp-quote mail-header-separator) "$"))
1893 (forward-line 1)
1894 (int-to-string (count-lines (point) (point-max))))))
1895
1896(defun message-make-in-reply-to ()
1897 "Return the In-Reply-To header for this message."
1898 (when message-reply-headers
1899 (let ((from (mail-header-from message-reply-headers))
1900 (date (mail-header-date message-reply-headers)))
1901 (when from
1902 (let ((stop-pos
1903 (string-match " *at \\| *@ \\| *(\\| *<" from)))
1904 (concat (if stop-pos (substring from 0 stop-pos) from)
1905 "'s message of "
1906 (if (or (not date) (string= date ""))
1907 "(unknown date)" date)))))))
1908
1909(defun message-make-distribution ()
1910 "Make a Distribution header."
1911 (let ((orig-distribution (message-fetch-reply-field "distribution")))
1912 (cond ((message-functionp message-distribution-function)
1913 (funcall message-distribution-function))
1914 (t orig-distribution))))
1915
1916(defun message-make-expires ()
1917 "Return an Expires header based on `message-expires'."
1918 (let ((current (current-time))
1919 (future (* 1.0 message-expires 60 60 24)))
1920 ;; Add the future to current.
1921 (setcar current (+ (car current) (round (/ future (expt 2 16)))))
1922 (setcar (cdr current) (+ (nth 1 current) (% (round future) (expt 2 16))))
1923 ;; Return the date in the future in UT.
1924 (timezone-make-date-arpa-standard
1925 (current-time-string current) (current-time-zone current) '(0 "UT"))))
1926
1927(defun message-make-path ()
1928 "Return uucp path."
1929 (let ((login-name (user-login-name)))
1930 (cond ((null message-user-path)
1931 (concat (system-name) "!" login-name))
1932 ((stringp message-user-path)
1933 ;; Support GENERICPATH. Suggested by vixie@decwrl.dec.com.
1934 (concat message-user-path "!" login-name))
1935 (t login-name))))
1936
1937(defun message-make-from ()
1938 "Make a From header."
1939 (let* ((login (message-make-address))
1940 (fullname
1941 (or (and (boundp 'user-full-name)
1942 user-full-name)
1943 (user-full-name))))
1944 (when (string= fullname "&")
1945 (setq fullname (user-login-name)))
1946 (save-excursion
1947 (message-set-work-buffer)
1948 (cond
1949 ((or (null message-from-style)
1950 (equal fullname ""))
1951 (insert login))
1952 ((or (eq message-from-style 'angles)
1953 (and (not (eq message-from-style 'parens))
1954 ;; Use angles if no quoting is needed, or if parens would
1955 ;; need quoting too.
1956 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1957 (let ((tmp (concat fullname nil)))
1958 (while (string-match "([^()]*)" tmp)
1959 (aset tmp (match-beginning 0) ?-)
1960 (aset tmp (1- (match-end 0)) ?-))
1961 (string-match "[\\()]" tmp)))))
1962 (insert fullname)
1963 (goto-char (point-min))
1964 ;; Look for a character that cannot appear unquoted
1965 ;; according to RFC 822.
1966 (when (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" nil 1)
1967 ;; Quote fullname, escaping specials.
1968 (goto-char (point-min))
1969 (insert "\"")
1970 (while (re-search-forward "[\"\\]" nil 1)
1971 (replace-match "\\\\\\&" t))
1972 (insert "\""))
1973 (insert " <" login ">"))
1974 (t ; 'parens or default
1975 (insert login " (")
1976 (let ((fullname-start (point)))
1977 (insert fullname)
1978 (goto-char fullname-start)
1979 ;; RFC 822 says \ and nonmatching parentheses
1980 ;; must be escaped in comments.
1981 ;; Escape every instance of ()\ ...
1982 (while (re-search-forward "[()\\]" nil 1)
1983 (replace-match "\\\\\\&" t))
1984 ;; ... then undo escaping of matching parentheses,
1985 ;; including matching nested parentheses.
1986 (goto-char fullname-start)
1987 (while (re-search-forward
1988 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1989 nil 1)
1990 (replace-match "\\1(\\3)" t)
1991 (goto-char fullname-start)))
1992 (insert ")")))
1993 (buffer-string))))
1994
1995(defun message-make-sender ()
1996 "Return the \"real\" user address.
1997This function tries to ignore all user modifications, and
1998give as trustworthy answer as possible."
1999 (concat (user-login-name) "@" (system-name)))
2000
2001(defun message-make-address ()
2002 "Make the address of the user."
2003 (or (message-user-mail-address)
2004 (concat (user-login-name) "@" (message-make-domain))))
2005
2006(defun message-user-mail-address ()
2007 "Return the pertinent part of `user-mail-address'."
2008 (when user-mail-address
2009 (nth 1 (mail-extract-address-components user-mail-address))))
2010
2011(defun message-make-fqdn ()
2012 "Return user's fully qualified domain name."
2013 (let ((system-name (system-name))
2014 (user-mail (message-user-mail-address)))
2015 (cond
2016 ((string-match "[^.]\\.[^.]" system-name)
2017 ;; `system-name' returned the right result.
2018 system-name)
2019 ;; Try `mail-host-address'.
2020 ((and (boundp 'mail-host-address)
2021 (stringp mail-host-address)
2022 (string-match "\\." mail-host-address))
2023 mail-host-address)
2024 ;; We try `user-mail-address' as a backup.
2025 ((and (string-match "\\." user-mail)
2026 (string-match "@\\(.*\\)\\'" user-mail))
2027 (match-string 1 user-mail))
2028 ;; Default to this bogus thing.
2029 (t
2030 (concat system-name ".i-have-a-misconfigured-system-so-shoot-me")))))
2031
2032(defun message-make-host-name ()
2033 "Return the name of the host."
2034 (let ((fqdn (message-make-fqdn)))
2035 (string-match "^[^.]+\\." fqdn)
2036 (substring fqdn 0 (1- (match-end 0)))))
2037
2038(defun message-make-domain ()
2039 "Return the domain name."
2040 (or mail-host-address
2041 (message-make-fqdn)))
2042
2043(defun message-generate-headers (headers)
2044 "Prepare article HEADERS.
2045Headers already prepared in the buffer are not modified."
2046 (save-restriction
2047 (message-narrow-to-headers)
2048 (let* ((Date (message-make-date))
2049 (Message-ID (message-make-message-id))
2050 (Organization (message-make-organization))
2051 (From (message-make-from))
2052 (Path (message-make-path))
2053 (Subject nil)
2054 (Newsgroups nil)
2055 (In-Reply-To (message-make-in-reply-to))
2056 (To nil)
2057 (Distribution (message-make-distribution))
2058 (Lines (message-make-lines))
2059 (X-Newsreader message-newsreader)
2060 (X-Mailer (and (not (message-fetch-field "X-Newsreader"))
2061 message-mailer))
2062 (Expires (message-make-expires))
2063 (case-fold-search t)
2064 header value elem)
2065 ;; First we remove any old generated headers.
2066 (let ((headers message-deletable-headers))
2067 (while headers
2068 (goto-char (point-min))
2069 (and (re-search-forward
2070 (concat "^" (symbol-name (car headers)) ": *") nil t)
2071 (get-text-property (1+ (match-beginning 0)) 'message-deletable)
2072 (message-delete-line))
2073 (pop headers)))
2074 ;; Go through all the required headers and see if they are in the
2075 ;; articles already. If they are not, or are empty, they are
2076 ;; inserted automatically - except for Subject, Newsgroups and
2077 ;; Distribution.
2078 (while headers
2079 (goto-char (point-min))
2080 (setq elem (pop headers))
2081 (if (consp elem)
2082 (if (eq (car elem) 'optional)
2083 (setq header (cdr elem))
2084 (setq header (car elem)))
2085 (setq header elem))
2086 (when (or (not (re-search-forward
2087 (concat "^" (downcase (symbol-name header)) ":")
2088 nil t))
2089 (progn
2090 ;; The header was found. We insert a space after the
2091 ;; colon, if there is none.
2092 (if (/= (following-char) ? ) (insert " ") (forward-char 1))
2093 ;; Find out whether the header is empty...
2094 (looking-at "[ \t]*$")))
2095 ;; So we find out what value we should insert.
2096 (setq value
2097 (cond
2098 ((and (consp elem) (eq (car elem) 'optional))
2099 ;; This is an optional header. If the cdr of this
2100 ;; is something that is nil, then we do not insert
2101 ;; this header.
2102 (setq header (cdr elem))
2103 (or (and (fboundp (cdr elem)) (funcall (cdr elem)))
2104 (and (boundp (cdr elem)) (symbol-value (cdr elem)))))
2105 ((consp elem)
2106 ;; The element is a cons. Either the cdr is a
2107 ;; string to be inserted verbatim, or it is a
2108 ;; function, and we insert the value returned from
2109 ;; this function.
2110 (or (and (stringp (cdr elem)) (cdr elem))
2111 (and (fboundp (cdr elem)) (funcall (cdr elem)))))
2112 ((and (boundp header) (symbol-value header))
2113 ;; The element is a symbol. We insert the value
2114 ;; of this symbol, if any.
2115 (symbol-value header))
2116 (t
2117 ;; We couldn't generate a value for this header,
2118 ;; so we just ask the user.
2119 (read-from-minibuffer
2120 (format "Empty header for %s; enter value: " header)))))
2121 ;; Finally insert the header.
2122 (when (and value
2123 (not (equal value "")))
2124 (save-excursion
2125 (if (bolp)
2126 (progn
2127 ;; This header didn't exist, so we insert it.
2128 (goto-char (point-max))
2129 (insert (symbol-name header) ": " value "\n")
2130 (forward-line -1))
2131 ;; The value of this header was empty, so we clear
2132 ;; totally and insert the new value.
2133 (delete-region (point) (message-point-at-eol))
2134 (insert value))
2135 ;; Add the deletable property to the headers that require it.
2136 (and (memq header message-deletable-headers)
2137 (progn (beginning-of-line) (looking-at "[^:]+: "))
2138 (add-text-properties
2139 (point) (match-end 0)
2140 '(message-deletable t face italic) (current-buffer)))))))
2141 ;; Insert new Sender if the From is strange.
2142 (let ((from (message-fetch-field "from"))
2143 (sender (message-fetch-field "sender"))
2144 (secure-sender (message-make-sender)))
2145 (when (and from
2146 (not (message-check-element 'sender))
2147 (not (string=
2148 (downcase
2149 (cadr (mail-extract-address-components from)))
2150 (downcase secure-sender)))
2151 (or (null sender)
2152 (not
2153 (string=
2154 (downcase
2155 (cadr (mail-extract-address-components sender)))
2156 (downcase secure-sender)))))
2157 (goto-char (point-min))
2158 ;; Rename any old Sender headers to Original-Sender.
2159 (when (re-search-forward "^Sender:" nil t)
2160 (beginning-of-line)
2161 (insert "Original-")
2162 (beginning-of-line))
2163 (insert "Sender: " secure-sender "\n"))))))
2164
2165(defun message-insert-courtesy-copy ()
2166 "Insert a courtesy message in mail copies of combined messages."
2167 (save-excursion
2168 (save-restriction
2169 (message-narrow-to-headers)
2170 (let ((newsgroups (message-fetch-field "newsgroups")))
2171 (when newsgroups
2172 (goto-char (point-max))
2173 (insert "Posted-To: " newsgroups "\n"))))
2174 (forward-line 1)
2175 (insert message-courtesy-message)))
2176
2177;;;
2178;;; Setting up a message buffer
2179;;;
2180
2181(defun message-fill-address (header value)
2182 (save-restriction
2183 (narrow-to-region (point) (point))
2184 (insert (capitalize (symbol-name header))
2185 ": "
2186 (if (consp value) (car value) value)
2187 "\n")
2188 (narrow-to-region (point-min) (1- (point-max)))
2189 (let (quoted last)
2190 (goto-char (point-min))
2191 (while (not (eobp))
2192 (skip-chars-forward "^,\"" (point-max))
2193 (if (or (= (following-char) ?,)
2194 (eobp))
2195 (when (not quoted)
2196 (if (and (> (current-column) 78)
2197 last)
2198 (progn
2199 (save-excursion
2200 (goto-char last)
2201 (insert "\n\t"))
2202 (setq last (1+ (point))))
2203 (setq last (1+ (point)))))
2204 (setq quoted (not quoted)))
2205 (unless (eobp)
2206 (forward-char 1))))
2207 (goto-char (point-max))
2208 (widen)
2209 (forward-line 1)))
2210
2211(defun message-fill-header (header value)
2212 (let ((begin (point))
2213 (fill-column 78)
2214 (fill-prefix "\t"))
2215 (insert (capitalize (symbol-name header))
2216 ": "
2217 (if (consp value) (car value) value)
2218 "\n")
2219 (save-restriction
2220 (narrow-to-region begin (point))
2221 (fill-region-as-paragraph begin (point))
2222 ;; Tapdance around looong Message-IDs.
2223 (forward-line -1)
2224 (when (looking-at "[ \t]*$")
2225 (message-delete-line))
2226 (goto-char begin)
2227 (re-search-forward ":" nil t)
2228 (when (looking-at "\n[ \t]+")
2229 (replace-match " " t t))
2230 (goto-char (point-max)))))
2231
2232(defun message-position-point ()
2233 "Move point to where the user probably wants to find it."
2234 (message-narrow-to-headers)
2235 (cond
2236 ((re-search-forward "^[^:]+:[ \t]*$" nil t)
2237 (search-backward ":" )
2238 (widen)
2239 (forward-char 1)
2240 (if (= (following-char) ? )
2241 (forward-char 1)
2242 (insert " ")))
2243 (t
2244 (goto-char (point-max))
2245 (widen)
2246 (forward-line 1)
2247 (unless (looking-at "$")
2248 (forward-line 2)))
2249 (sit-for 0)))
2250
2251(defun message-buffer-name (type &optional to group)
2252 "Return a new (unique) buffer name based on TYPE and TO."
2253 (cond
2254 ;; Check whether `message-generate-new-buffers' is a function,
2255 ;; and if so, call it.
2256 ((message-functionp message-generate-new-buffers)
2257 (funcall message-generate-new-buffers type to group))
2258 ;; Generate a new buffer name The Message Way.
2259 (message-generate-new-buffers
2260 (generate-new-buffer-name
2261 (concat "*" type
2262 (if to
2263 (concat " to "
2264 (or (car (mail-extract-address-components to))
2265 to) "")
2266 "")
2267 (if (and group (not (string= group ""))) (concat " on " group) "")
2268 "*")))
2269 ;; Use standard name.
2270 (t
2271 (format "*%s message*" type))))
2272
2273(defun message-pop-to-buffer (name)
2274 "Pop to buffer NAME, and warn if it already exists and is modified."
2275 (let ((buffer (get-buffer name)))
2276 (if (and buffer
2277 (buffer-name buffer))
2278 (progn
2279 (set-buffer (pop-to-buffer buffer))
2280 (when (and (buffer-modified-p)
2281 (not (y-or-n-p
2282 "Message already being composed; erase? ")))
2283 (error "Message being composed")))
2284 (set-buffer (pop-to-buffer name))))
2285 (erase-buffer)
2286 (message-mode))
2287
2288(defun message-do-send-housekeeping ()
2289 "Kill old message buffers."
2290 ;; We might have sent this buffer already. Delete it from the
2291 ;; list of buffers.
2292 (setq message-buffer-list (delq (current-buffer) message-buffer-list))
2293 (when (and message-max-buffers
2294 (>= (length message-buffer-list) message-max-buffers))
2295 ;; Kill the oldest buffer -- unless it has been changed.
2296 (let ((buffer (pop message-buffer-list)))
2297 (when (and (buffer-name buffer)
2298 (not (buffer-modified-p buffer)))
2299 (kill-buffer buffer))))
2300 ;; Rename the buffer.
2301 (if message-send-rename-function
2302 (funcall message-send-rename-function)
2303 (when (string-match "\\`\\*" (buffer-name))
2304 (rename-buffer
2305 (concat "*sent " (substring (buffer-name) (match-end 0))) t)))
2306 ;; Push the current buffer onto the list.
2307 (when message-max-buffers
2308 (setq message-buffer-list
2309 (nconc message-buffer-list (list (current-buffer))))))
2310
2311(defvar mc-modes-alist)
2312(defun message-setup (headers &optional replybuffer actions)
2313 (when (and (boundp 'mc-modes-alist)
2314 (not (assq 'message-mode mc-modes-alist)))
2315 (push '(message-mode (encrypt . mc-encrypt-message)
2316 (sign . mc-sign-message))
2317 mc-modes-alist))
2318 (when actions
2319 (setq message-send-actions actions))
2320 (setq message-reply-buffer replybuffer)
2321 (goto-char (point-min))
2322 ;; Insert all the headers.
2323 (mail-header-format
2324 (let ((h headers)
2325 (alist message-header-format-alist))
2326 (while h
2327 (unless (assq (caar h) message-header-format-alist)
2328 (push (list (caar h)) alist))
2329 (pop h))
2330 alist)
2331 headers)
2332 (delete-region (point) (progn (forward-line -1) (point)))
2333 (when message-default-headers
2334 (insert message-default-headers))
2335 (put-text-property
2336 (point)
2337 (progn
2338 (insert mail-header-separator "\n")
2339 (1- (point)))
2340 'read-only nil)
2341 (forward-line -1)
2342 (when (message-news-p)
2343 (when message-default-news-headers
2344 (insert message-default-news-headers))
2345 (when message-generate-headers-first
2346 (message-generate-headers
2347 (delq 'Lines
2348 (delq 'Subject
2349 (copy-sequence message-required-news-headers))))))
2350 (when (message-mail-p)
2351 (when message-default-mail-headers
2352 (insert message-default-mail-headers))
2353 (when message-generate-headers-first
2354 (message-generate-headers
2355 (delq 'Lines
2356 (delq 'Subject
2357 (copy-sequence message-required-mail-headers))))))
2358 (run-hooks 'message-signature-setup-hook)
2359 (message-insert-signature)
2360 (message-set-auto-save-file-name)
2361 (save-restriction
2362 (message-narrow-to-headers)
2363 (run-hooks 'message-header-setup-hook))
2364 (set-buffer-modified-p nil)
2365 (run-hooks 'message-setup-hook)
2366 (message-position-point)
2367 (undo-boundary))
2368
2369(defun message-set-auto-save-file-name ()
2370 "Associate the message buffer with a file in the drafts directory."
2371 (when message-autosave-directory
2372 (unless (file-exists-p message-autosave-directory)
2373 (make-directory message-autosave-directory t))
2374 (let ((name (make-temp-name
2375 (concat (file-name-as-directory message-autosave-directory)
2376 "msg."))))
2377 (setq buffer-auto-save-file-name
2378 (save-excursion
2379 (prog1
2380 (progn
2381 (set-buffer (get-buffer-create " *draft tmp*"))
2382 (setq buffer-file-name name)
2383 (make-auto-save-file-name))
2384 (kill-buffer (current-buffer)))))
2385 (clear-visited-file-modtime))))
2386
2387\f
2388
2389;;;
2390;;; Commands for interfacing with message
2391;;;
2392
2393;;;###autoload
2394(defun message-mail (&optional to subject)
2395 "Start editing a mail message to be sent."
2396 (interactive)
2397 (message-pop-to-buffer (message-buffer-name "mail" to))
2398 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2399
2400;;;###autoload
2401(defun message-news (&optional newsgroups subject)
2402 "Start editing a news article to be sent."
2403 (interactive)
2404 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups))
2405 (message-setup `((Newsgroups . ,(or newsgroups ""))
2406 (Subject . ,(or subject "")))))
2407
2408;;;###autoload
2409(defun message-reply (&optional to-address wide ignore-reply-to)
2410 "Start editing a reply to the article in the current buffer."
2411 (interactive)
2412 (let ((cur (current-buffer))
2413 from subject date reply-to to cc
2414 references message-id follow-to
2415 mct never-mct gnus-warning)
2416 (save-restriction
2417 (narrow-to-region
2418 (goto-char (point-min))
2419 (if (search-forward "\n\n" nil t)
2420 (1- (point))
2421 (point-max)))
2422 ;; Allow customizations to have their say.
2423 (if (not wide)
2424 ;; This is a regular reply.
2425 (if (message-functionp message-reply-to-function)
2426 (setq follow-to (funcall message-reply-to-function)))
2427 ;; This is a followup.
2428 (if (message-functionp message-wide-reply-to-function)
2429 (save-excursion
2430 (setq follow-to
2431 (funcall message-wide-reply-to-function)))))
2432 ;; Find all relevant headers we need.
2433 (setq from (message-fetch-field "from")
2434 date (message-fetch-field "date")
2435 subject (or (message-fetch-field "subject") "none")
2436 to (message-fetch-field "to")
2437 cc (message-fetch-field "cc")
2438 mct (message-fetch-field "mail-copies-to")
2439 reply-to (unless ignore-reply-to (message-fetch-field "reply-to"))
2440 references (message-fetch-field "references")
2441 message-id (message-fetch-field "message-id"))
2442 ;; Remove any (buggy) Re:'s that are present and make a
2443 ;; proper one.
2444 (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2445 (setq subject (substring subject (match-end 0))))
2446 (setq subject (concat "Re: " subject))
2447
2448 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
2449 (string-match "<[^>]+>" gnus-warning))
2450 (setq message-id (match-string 0 gnus-warning)))
2451
2452 ;; Handle special values of Mail-Copies-To.
2453 (when mct
2454 (cond ((equal (downcase mct) "never")
2455 (setq never-mct t)
2456 (setq mct nil))
2457 ((equal (downcase mct) "always")
2458 (setq mct (or reply-to from)))))
2459
2460 (unless follow-to
2461 (if (or (not wide)
2462 to-address)
2463 (setq follow-to (list (cons 'To (or to-address reply-to from))))
2464 (let (ccalist)
2465 (save-excursion
2466 (message-set-work-buffer)
2467 (unless never-mct
2468 (insert (or reply-to from "")))
2469 (insert
2470 (if (bolp) "" ", ") (or to "")
2471 (if mct (concat (if (bolp) "" ", ") mct) "")
2472 (if cc (concat (if (bolp) "" ", ") cc) ""))
2473 ;; Remove addresses that match `rmail-dont-reply-to-names'.
2474 (insert (prog1 (rmail-dont-reply-to (buffer-string))
2475 (erase-buffer)))
2476 (goto-char (point-min))
2477 (setq ccalist
2478 (mapcar
2479 (lambda (addr)
2480 (cons (mail-strip-quoted-names addr) addr))
2481 (nreverse (mail-parse-comma-list))))
2482 (let ((s ccalist))
2483 (while s
2484 (setq ccalist (delq (assoc (car (pop s)) s) ccalist)))))
2485 (setq follow-to (list (cons 'To (cdr (pop ccalist)))))
2486 (when ccalist
2487 (push (cons 'Cc
2488 (mapconcat (lambda (addr) (cdr addr)) ccalist ", "))
2489 follow-to)))))
2490 (widen))
2491
2492 (message-pop-to-buffer (message-buffer-name "reply" from))
2493
2494 (setq message-reply-headers
2495 (vector 0 subject from date message-id references 0 0 ""))
2496
2497 (message-setup
2498 `((Subject . ,subject)
2499 ,@follow-to
2500 ,@(if (or references message-id)
2501 `((References . ,(concat (or references "") (and references " ")
2502 (or message-id ""))))
2503 nil))
2504 cur)))
2505
2506;;;###autoload
2507(defun message-wide-reply (&optional to-address)
2508 (interactive)
2509 (message-reply to-address t))
2510
2511;;;###autoload
2512(defun message-followup ()
2513 (interactive)
2514 (let ((cur (current-buffer))
2515 from subject date reply-to mct
2516 references message-id follow-to
2517 followup-to distribution newsgroups gnus-warning)
2518 (save-restriction
2519 (narrow-to-region
2520 (goto-char (point-min))
2521 (if (search-forward "\n\n" nil t)
2522 (1- (point))
2523 (point-max)))
2524 (when (message-functionp message-followup-to-function)
2525 (setq follow-to
2526 (funcall message-followup-to-function)))
2527 (setq from (message-fetch-field "from")
2528 date (message-fetch-field "date")
2529 subject (or (message-fetch-field "subject") "none")
2530 references (message-fetch-field "references")
2531 message-id (message-fetch-field "message-id")
2532 followup-to (message-fetch-field "followup-to")
2533 newsgroups (message-fetch-field "newsgroups")
2534 reply-to (message-fetch-field "reply-to")
2535 distribution (message-fetch-field "distribution")
2536 mct (message-fetch-field "mail-copies-to"))
2537 (when (and (setq gnus-warning (message-fetch-field "gnus-warning"))
2538 (string-match "<[^>]+>" gnus-warning))
2539 (setq message-id (match-string 0 gnus-warning)))
2540 ;; Remove bogus distribution.
2541 (and (stringp distribution)
2542 (string-match "world" distribution)
2543 (setq distribution nil))
2544 ;; Remove any (buggy) Re:'s that are present and make a
2545 ;; proper one.
2546 (when (string-match "^[ \t]*[Rr][Ee]:[ \t]*" subject)
2547 (setq subject (substring subject (match-end 0))))
2548 (setq subject (concat "Re: " subject))
2549 (widen))
2550
2551 (message-pop-to-buffer (message-buffer-name "followup" from newsgroups))
2552
2553 (message-setup
2554 `((Subject . ,subject)
2555 ,@(cond
2556 (follow-to follow-to)
2557 ((and followup-to message-use-followup-to)
2558 (list
2559 (cond
2560 ((equal (downcase followup-to) "poster")
2561 (if (or (eq message-use-followup-to 'use)
2562 (message-y-or-n-p "Obey Followup-To: poster? " t "\
2563You should normally obey the Followup-To: header.
2564
2565`Followup-To: poster' sends your response via e-mail instead of news.
2566
2567A typical situation where `Followup-To: poster' is used is when the poster
2568does not read the newsgroup, so he wouldn't see any replies sent to it."))
2569 (cons 'To (or reply-to from ""))
2570 (cons 'Newsgroups newsgroups)))
2571 (t
2572 (if (or (equal followup-to newsgroups)
2573 (not (eq message-use-followup-to 'ask))
2574 (message-y-or-n-p
2575 (concat "Obey Followup-To: " followup-to "? ") t "\
2576You should normally obey the Followup-To: header.
2577
2578 `Followup-To: " followup-to "'
2579directs your response to " (if (string-match "," followup-to)
2580 "the specified newsgroups"
2581 "that newsgroup only") ".
2582
2583If a message is posted to several newsgroups, Followup-To is often
2584used to direct the following discussion to one newsgroup only,
2585because discussions that are spread over several newsgroup tend to
2586be fragmented and very difficult to follow.
2587
2588Also, some source/announcment newsgroups are not indented for discussion;
2589responses here are directed to other newsgroups."))
2590 (cons 'Newsgroups followup-to)
2591 (cons 'Newsgroups newsgroups))))))
2592 (t
2593 `((Newsgroups . ,newsgroups))))
2594 ,@(and distribution (list (cons 'Distribution distribution)))
2595 (References . ,(concat (or references "") (and references " ")
2596 (or message-id "")))
2597 ,@(when (and mct
2598 (not (equal (downcase mct) "never")))
2599 (list (cons 'Cc (if (equal (downcase mct) "always")
2600 (or reply-to from "")
2601 mct)))))
2602
2603 cur)
2604
2605 (setq message-reply-headers
2606 (vector 0 subject from date message-id references 0 0 ""))))
2607
2608
2609;;;###autoload
2610(defun message-cancel-news ()
2611 "Cancel an article you posted."
2612 (interactive)
2613 (unless (message-news-p)
2614 (error "This is not a news article; canceling is impossible"))
2615 (when (yes-or-no-p "Do you really want to cancel this article? ")
2616 (let (from newsgroups message-id distribution buf)
2617 (save-excursion
2618 ;; Get header info. from original article.
2619 (save-restriction
2620 (message-narrow-to-head)
2621 (setq from (message-fetch-field "from")
2622 newsgroups (message-fetch-field "newsgroups")
2623 message-id (message-fetch-field "message-id")
2624 distribution (message-fetch-field "distribution")))
2625 ;; Make sure that this article was written by the user.
2626 (unless (string-equal
2627 (downcase (cadr (mail-extract-address-components from)))
2628 (downcase (message-make-address)))
2629 (error "This article is not yours"))
2630 ;; Make control message.
2631 (setq buf (set-buffer (get-buffer-create " *message cancel*")))
2632 (buffer-disable-undo (current-buffer))
2633 (erase-buffer)
2634 (insert "Newsgroups: " newsgroups "\n"
2635 "From: " (message-make-from) "\n"
2636 "Subject: cmsg cancel " message-id "\n"
2637 "Control: cancel " message-id "\n"
2638 (if distribution
2639 (concat "Distribution: " distribution "\n")
2640 "")
2641 mail-header-separator "\n"
2642 "This is a cancel message from " from ".\n")
2643 (message "Canceling your article...")
2644 (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me))
2645 (funcall message-send-news-function))
2646 (message "Canceling your article...done")
2647 (kill-buffer buf)))))
2648
2649;;;###autoload
2650(defun message-supersede ()
2651 "Start composing a message to supersede the current message.
2652This is done simply by taking the old article and adding a Supersedes
2653header line with the old Message-ID."
2654 (interactive)
2655 (let ((cur (current-buffer)))
2656 ;; Check whether the user owns the article that is to be superseded.
2657 (unless (string-equal
2658 (downcase (cadr (mail-extract-address-components
2659 (message-fetch-field "from"))))
2660 (downcase (message-make-address)))
2661 (error "This article is not yours"))
2662 ;; Get a normal message buffer.
2663 (message-pop-to-buffer (message-buffer-name "supersede"))
2664 (insert-buffer-substring cur)
2665 (message-narrow-to-head)
2666 ;; Remove unwanted headers.
2667 (when message-ignored-supersedes-headers
2668 (message-remove-header message-ignored-supersedes-headers t))
2669 (goto-char (point-min))
2670 (if (not (re-search-forward "^Message-ID: " nil t))
2671 (error "No Message-ID in this article")
2672 (replace-match "Supersedes: " t t))
2673 (goto-char (point-max))
2674 (insert mail-header-separator)
2675 (widen)
2676 (forward-line 1)))
2677
2678;;;###autoload
2679(defun message-recover ()
2680 "Reread contents of current buffer from its last auto-save file."
2681 (interactive)
2682 (let ((file-name (make-auto-save-file-name)))
2683 (cond ((save-window-excursion
2684 (if (not (eq system-type 'vax-vms))
2685 (with-output-to-temp-buffer "*Directory*"
2686 (buffer-disable-undo standard-output)
2687 (let ((default-directory "/"))
2688 (call-process
2689 "ls" nil standard-output nil "-l" file-name))))
2690 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
2691 (let ((buffer-read-only nil))
2692 (erase-buffer)
2693 (insert-file-contents file-name nil)))
2694 (t (error "message-recover cancelled")))))
2695
2696;;; Forwarding messages.
2697
2698(defun message-make-forward-subject ()
2699 "Return a Subject header suitable for the message in the current buffer."
2700 (concat "[" (or (message-fetch-field (if (message-news-p) "newsgroups" "from"))
2701 "(nowhere)")
2702 "] " (or (message-fetch-field "Subject") "")))
2703
2704;;;###autoload
2705(defun message-forward (&optional news)
2706 "Forward the current message via mail.
2707Optional NEWS will use news to forward instead of mail."
2708 (interactive "P")
2709 (let ((cur (current-buffer))
2710 (subject (message-make-forward-subject)))
2711 (if news (message-news nil subject) (message-mail nil subject))
2712 ;; Put point where we want it before inserting the forwarded
2713 ;; message.
2714 (if message-signature-before-forwarded-message
2715 (goto-char (point-max))
2716 (message-goto-body))
2717 ;; Make sure we're at the start of the line.
2718 (unless (eolp)
2719 (insert "\n"))
2720 ;; Narrow to the area we are to insert.
2721 (narrow-to-region (point) (point))
2722 ;; Insert the separators and the forwarded buffer.
2723 (insert message-forward-start-separator)
2724 (insert-buffer-substring cur)
2725 (goto-char (point-max))
2726 (insert message-forward-end-separator)
2727 (set-text-properties (point-min) (point-max) nil)
2728 ;; Remove all unwanted headers.
2729 (goto-char (point-min))
2730 (forward-line 1)
2731 (narrow-to-region (point) (if (search-forward "\n\n" nil t)
2732 (1- (point))
2733 (point)))
2734 (goto-char (point-min))
2735 (message-remove-header message-included-forward-headers t nil t)
2736 (widen)
2737 (message-position-point)))
2738
2739;;;###autoload
2740(defun message-resend (address)
2741 "Resend the current article to ADDRESS."
2742 (interactive "sResend message to: ")
2743 (save-excursion
2744 (let ((cur (current-buffer))
2745 beg)
2746 ;; We first set up a normal mail buffer.
2747 (set-buffer (get-buffer-create " *message resend*"))
2748 (buffer-disable-undo (current-buffer))
2749 (erase-buffer)
2750 (message-setup `((To . ,address)))
2751 ;; Insert our usual headers.
2752 (message-generate-headers '(From Date To))
2753 (message-narrow-to-headers)
2754 ;; Rename them all to "Resent-*".
2755 (while (re-search-forward "^[A-Za-z]" nil t)
2756 (forward-char -1)
2757 (insert "Resent-"))
2758 (widen)
2759 (forward-line)
2760 (delete-region (point) (point-max))
2761 (setq beg (point))
2762 ;; Insert the message to be resent.
2763 (insert-buffer-substring cur)
2764 (goto-char (point-min))
2765 (search-forward "\n\n")
2766 (forward-char -1)
2767 (save-restriction
2768 (narrow-to-region beg (point))
2769 (message-remove-header message-ignored-resent-headers t)
2770 (goto-char (point-max)))
2771 (insert mail-header-separator)
2772 ;; Rename all old ("Also-")Resent headers.
2773 (while (re-search-backward "^\\(Also-\\)?Resent-" beg t)
2774 (beginning-of-line)
2775 (insert "Also-"))
2776 ;; Send it.
2777 (message-send-mail)
2778 (kill-buffer (current-buffer)))))
2779
2780;;;###autoload
2781(defun message-bounce ()
2782 "Re-mail the current message.
2783This only makes sense if the current message is a bounce message than
2784contains some mail you have written which has been bounced back to
2785you."
2786 (interactive)
2787 (let ((cur (current-buffer))
2788 boundary)
2789 (message-pop-to-buffer (message-buffer-name "bounce"))
2790 (insert-buffer-substring cur)
2791 (undo-boundary)
2792 (message-narrow-to-head)
2793 (if (and (message-fetch-field "Mime-Version")
2794 (setq boundary (message-fetch-field "Content-Type")))
2795 (if (string-match "boundary=\"\\([^\"]+\\)\"" boundary)
2796 (setq boundary (concat (match-string 1 boundary) " *\n"
2797 "Content-Type: message/rfc822"))
2798 (setq boundary nil)))
2799 (widen)
2800 (goto-char (point-min))
2801 (search-forward "\n\n" nil t)
2802 (or (and boundary
2803 (re-search-forward boundary nil t)
2804 (forward-line 2))
2805 (and (re-search-forward message-unsent-separator nil t)
2806 (forward-line 1))
2807 (and (search-forward "\n\n" nil t)
2808 (re-search-forward "^Return-Path:.*\n" nil t)))
2809 ;; We remove everything before the bounced mail.
2810 (delete-region
2811 (point-min)
2812 (if (re-search-forward "^[^ \n\t]+:" nil t)
2813 (match-beginning 0)
2814 (point)))
2815 (save-restriction
2816 (message-narrow-to-head)
2817 (message-remove-header message-ignored-bounced-headers t)
2818 (goto-char (point-max))
2819 (insert mail-header-separator))
2820 (message-position-point)))
2821
2822;;;
2823;;; Interactive entry points for new message buffers.
2824;;;
2825
2826;;;###autoload
2827(defun message-mail-other-window (&optional to subject)
2828 "Like `message-mail' command, but display mail buffer in another window."
2829 (interactive)
2830 (let ((pop-up-windows t)
2831 (special-display-buffer-names nil)
2832 (special-display-regexps nil)
2833 (same-window-buffer-names nil)
2834 (same-window-regexps nil))
2835 (message-pop-to-buffer (message-buffer-name "mail" to)))
2836 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2837
2838;;;###autoload
2839(defun message-mail-other-frame (&optional to subject)
2840 "Like `message-mail' command, but display mail buffer in another frame."
2841 (interactive)
2842 (let ((pop-up-frames t)
2843 (special-display-buffer-names nil)
2844 (special-display-regexps nil)
2845 (same-window-buffer-names nil)
2846 (same-window-regexps nil))
2847 (message-pop-to-buffer (message-buffer-name "mail" to)))
2848 (message-setup `((To . ,(or to "")) (Subject . ,(or subject "")))))
2849
2850;;;###autoload
2851(defun message-news-other-window (&optional newsgroups subject)
2852 "Start editing a news article to be sent."
2853 (interactive)
2854 (let ((pop-up-windows t)
2855 (special-display-buffer-names nil)
2856 (special-display-regexps nil)
2857 (same-window-buffer-names nil)
2858 (same-window-regexps nil))
2859 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
2860 (message-setup `((Newsgroups . ,(or newsgroups ""))
2861 (Subject . ,(or subject "")))))
2862
2863;;;###autoload
2864(defun message-news-other-frame (&optional newsgroups subject)
2865 "Start editing a news article to be sent."
2866 (interactive)
2867 (let ((pop-up-frames t)
2868 (special-display-buffer-names nil)
2869 (special-display-regexps nil)
2870 (same-window-buffer-names nil)
2871 (same-window-regexps nil))
2872 (message-pop-to-buffer (message-buffer-name "news" nil newsgroups)))
2873 (message-setup `((Newsgroups . ,(or newsgroups ""))
2874 (Subject . ,(or subject "")))))
2875
2876;;; underline.el
2877
2878;; This code should be moved to underline.el (from which it is stolen).
2879
2880;;;###autoload
2881(defun bold-region (start end)
2882 "Bold all nonblank characters in the region.
2883Works by overstriking characters.
2884Called from program, takes two arguments START and END
2885which specify the range to operate on."
2886 (interactive "r")
2887 (save-excursion
2888 (let ((end1 (make-marker)))
2889 (move-marker end1 (max start end))
2890 (goto-char (min start end))
2891 (while (< (point) end1)
2892 (or (looking-at "[_\^@- ]")
2893 (insert (following-char) "\b"))
2894 (forward-char 1)))))
2895
2896;;;###autoload
2897(defun unbold-region (start end)
2898 "Remove all boldness (overstruck characters) in the region.
2899Called from program, takes two arguments START and END
2900which specify the range to operate on."
2901 (interactive "r")
2902 (save-excursion
2903 (let ((end1 (make-marker)))
2904 (move-marker end1 (max start end))
2905 (goto-char (min start end))
2906 (while (re-search-forward "\b" end1 t)
2907 (if (eq (following-char) (char-after (- (point) 2)))
2908 (delete-char -2))))))
2909
2910(fset 'message-exchange-point-and-mark 'exchange-point-and-mark)
2911
2912;; Support for toolbar
2913(when (string-match "XEmacs\\|Lucid" emacs-version)
2914 (require 'messagexmas))
2915
2916;;; Group name completion.
2917
2918(defvar message-newgroups-header-regexp
2919 "^\\(Newsgroups\\|Followup-To\\|Posted-To\\):"
2920 "Regexp that match headers that lists groups.")
2921
2922(defun message-tab ()
2923 "Expand group names in Newsgroups and Followup-To headers.
2924Do a `tab-to-tab-stop' if not in those headers."
2925 (interactive)
2926 (if (let ((mail-abbrev-mode-regexp message-newgroups-header-regexp))
2927 (mail-abbrev-in-expansion-header-p))
2928 (message-expand-group)
2929 (tab-to-tab-stop)))
2930
2931(defvar gnus-active-hashtb)
2932(defun message-expand-group ()
2933 (let* ((b (save-excursion (skip-chars-backward "^, :\t\n") (point)))
2934 (completion-ignore-case t)
2935 (string (buffer-substring b (point)))
2936 (hashtb (and (boundp 'gnus-active-hashtb) gnus-active-hashtb))
2937 (completions (all-completions string hashtb))
2938 (cur (current-buffer))
2939 comp)
2940 (delete-region b (point))
2941 (cond
2942 ((= (length completions) 1)
2943 (if (string= (car completions) string)
2944 (progn
2945 (insert string)
2946 (message "Only matching group"))
2947 (insert (car completions))))
2948 ((and (setq comp (try-completion string hashtb))
2949 (not (string= comp string)))
2950 (insert comp))
2951 (t
2952 (insert string)
2953 (if (not comp)
2954 (message "No matching groups")
2955 (pop-to-buffer "*Completions*")
2956 (buffer-disable-undo (current-buffer))
2957 (let ((buffer-read-only nil))
2958 (erase-buffer)
2959 (let ((standard-output (current-buffer)))
2960 (display-completion-list (sort completions 'string<)))
2961 (goto-char (point-min))
2962 (pop-to-buffer cur)))))))
2963
2964;;; Help stuff.
2965
2966(defmacro message-y-or-n-p (question show &rest text)
2967 "Ask QUESTION, displaying the rest of the arguments in a temporary buffer."
2968 `(message-talkative-question 'y-or-n-p ,question ,show ,@text))
2969
2970(defun message-talkative-question (ask question show &rest text)
2971 "Call FUNCTION with argument QUESTION, displaying the rest of the arguments in a temporary buffer if SHOW.
2972The following arguments may contain lists of values."
2973 (if (and show
2974 (setq text (message-flatten-list text)))
2975 (save-window-excursion
2976 (save-excursion
2977 (with-output-to-temp-buffer " *MESSAGE information message*"
2978 (set-buffer " *MESSAGE information message*")
2979 (mapcar 'princ text)
2980 (goto-char (point-min))))
2981 (funcall ask question))
2982 (funcall ask question)))
2983
2984(defun message-flatten-list (&rest list)
2985 (message-flatten-list-1 list))
2986
2987(defun message-flatten-list-1 (list)
2988 (cond ((consp list)
2989 (apply 'append (mapcar 'message-flatten-list-1 list)))
2990 (list
2991 (list list))))
2992
2993(run-hooks 'message-load-hook)
2994
2995(provide 'message)
2996
2997;;; message.el ends here