2002-01-01 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-ems)
34 (require 'message)
35 (require 'gnus-art)
36
37 (defcustom gnus-post-method 'current
38   "*Preferred method for posting USENET news.
39
40 If this variable is `current' (which is the default), Gnus will use
41 the \"current\" select method when posting.  If it is `native', Gnus
42 will use the native select method when posting.
43
44 This method will not be used in mail groups and the like, only in
45 \"real\" newsgroups.
46
47 If not `native' nor `current', the value must be a valid method as discussed
48 in the documentation of `gnus-select-method'.  It can also be a list of
49 methods.  If that is the case, the user will be queried for what select
50 method to use when posting."
51   :group 'gnus-group-foreign
52   :link '(custom-manual "(gnus)Posting Server")
53   :type `(choice (const native)
54                  (const current)
55                  (sexp :tag "Methods" ,gnus-select-method)))
56
57 (defcustom gnus-outgoing-message-group nil
58   "*All outgoing messages will be put in this group.
59 If you want to store all your outgoing mail and articles in the group
60 \"nnml:archive\", you set this variable to that value.  This variable
61 can also be a list of group names.
62
63 If you want to have greater control over what group to put each
64 message in, you can set this variable to a function that checks the
65 current newsgroup name and then returns a suitable group name (or list
66 of names)."
67   :group 'gnus-message
68   :type '(choice (string :tag "Group")
69                  (function)))
70
71 (defcustom gnus-mailing-list-groups nil
72   "*Regexp matching groups that are really mailing lists.
73 This is useful when you're reading a mailing list that has been
74 gatewayed to a newsgroup, and you want to followup to an article in
75 the group."
76   :group 'gnus-message
77   :type 'regexp)
78
79 (defcustom gnus-add-to-list nil
80   "*If non-nil, add a `to-list' parameter automatically."
81   :group 'gnus-message
82   :type 'boolean)
83
84 (defcustom gnus-crosspost-complaint
85   "Hi,
86
87 You posted the article below with the following Newsgroups header:
88
89 Newsgroups: %s
90
91 The %s group, at least, was an inappropriate recipient
92 of this message.  Please trim your Newsgroups header to exclude this
93 group before posting in the future.
94
95 Thank you.
96
97 "
98   "Format string to be inserted when complaining about crossposts.
99 The first %s will be replaced by the Newsgroups header;
100 the second with the current group name."
101   :group 'gnus-message
102   :type 'string)
103
104 (defcustom gnus-message-setup-hook nil
105   "Hook run after setting up a message buffer."
106   :group 'gnus-message
107   :type 'hook)
108
109 (defcustom gnus-bug-create-help-buffer t
110   "*Should we create the *Gnus Help Bug* buffer?"
111   :group 'gnus-message
112   :type 'boolean)
113
114 (defcustom gnus-posting-styles nil
115   "*Alist of styles to use when posting.
116 See Info node `(gnus)Posting Styles'."
117   :group 'gnus-message
118   :type '(repeat (cons (choice (regexp)
119                                (function)
120                                (variable)
121                                (sexp))
122                        (repeat (list
123                                 (choice (const signature)
124                                         (const signature-file)
125                                         (const organization)
126                                         (const address)
127                                         (const name)
128                                         (const body)
129                                         (string :tag "Header"))
130                                 (choice (string)
131                                         (function)
132                                         (variable)
133                                         (sexp)))))))
134
135 (defcustom gnus-inews-mark-gcc-as-read nil
136   "If non-nil, automatically mark Gcc articles as read."
137   :group 'gnus-message
138   :type 'boolean)
139
140 (defcustom gnus-gcc-externalize-attachments nil
141   "Should local-file attachments be included as external parts?
142 If it is `all', attach files as external parts;
143 if a regexp and matches the Gcc group name, attach files as external parts;
144 If nil, attach files as normal parts."
145   :group 'gnus-message
146   :type '(choice (const nil :tag "None")
147                  (const all :tag "Any")
148                  (string :tag "Regexp")))
149
150 (defcustom gnus-group-posting-charset-alist
151   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
152     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
153     (message-this-is-mail nil nil)
154     (message-this-is-news nil t))
155   "Alist of regexps and permitted unencoded charsets for posting.
156 Each element of the alist has the form (TEST HEADER BODY-LIST), where
157 TEST is either a regular expression matching the newsgroup header or a
158 variable to query,
159 HEADER is the charset which may be left unencoded in the header (nil
160 means encode all charsets),
161 BODY-LIST is a list of charsets which may be encoded using 8bit
162 content-transfer encoding in the body, or one of the special values
163 nil (always encode using quoted-printable) or t (always use 8bit).
164
165 Note that any value other than nil for HEADER infringes some RFCs, so
166 use this option with care."
167   :type '(repeat (list :tag "Permitted unencoded charsets"
168                   (choice :tag "Where"
169                    (regexp :tag "Group")
170                    (const :tag "Mail message" :value message-this-is-mail)
171                    (const :tag "News article" :value message-this-is-news))
172                   (choice :tag "Header"
173                    (const :tag "None" nil)
174                    (symbol :tag "Charset"))
175                   (choice :tag "Body"
176                           (const :tag "Any" :value t)
177                           (const :tag "None" :value nil)
178                           (repeat :tag "Charsets"
179                                   (symbol :tag "Charset")))))
180   :group 'gnus-charset)
181
182 ;;; Internal variables.
183
184 (defvar gnus-inhibit-posting-styles nil
185   "Inhibit the use of posting styles.")
186
187 (defvar gnus-message-buffer "*Mail Gnus*")
188 (defvar gnus-article-copy nil)
189 (defvar gnus-check-before-posting nil)
190 (defvar gnus-last-posting-server nil)
191 (defvar gnus-message-group-art nil)
192
193 (defvar gnus-msg-force-broken-reply-to nil)
194
195 (defconst gnus-bug-message
196   "Sending a bug report to the Gnus Towers.
197 ========================================
198
199 The buffer below is a mail buffer.  When you press `C-c C-c', it will
200 be sent to the Gnus Bug Exterminators.
201
202 The thing near the bottom of the buffer is how the environment
203 settings will be included in the mail.  Please do not delete that.
204 They will tell the Bug People what your environment is, so that it
205 will be easier to locate the bugs.
206
207 If you have found a bug that makes Emacs go \"beep\", set
208 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
209 and include the backtrace in your bug report.
210
211 Please describe the bug in annoying, painstaking detail.
212
213 Thank you for your help in stamping out bugs.
214 ")
215
216 (eval-and-compile
217   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
218   (autoload 'news-setup "rnewspost")
219   (autoload 'news-reply-mode "rnewspost")
220   (autoload 'rmail-dont-reply-to "mail-utils")
221   (autoload 'rmail-output "rmailout"))
222
223 \f
224 ;;;
225 ;;; Gnus Posting Functions
226 ;;;
227
228 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
229   "p" gnus-summary-post-news
230   "i" gnus-summary-news-other-window
231   "f" gnus-summary-followup
232   "F" gnus-summary-followup-with-original
233   "c" gnus-summary-cancel-article
234   "s" gnus-summary-supersede-article
235   "r" gnus-summary-reply
236   "y" gnus-summary-yank-message
237   "R" gnus-summary-reply-with-original
238   "w" gnus-summary-wide-reply
239   "W" gnus-summary-wide-reply-with-original
240   "v" gnus-summary-very-wide-reply
241   "W" gnus-summary-very-wide-reply-with-original
242   "n" gnus-summary-followup-to-mail
243   "N" gnus-summary-followup-to-mail-with-original
244   "m" gnus-summary-mail-other-window
245   "u" gnus-uu-post-news
246   "\M-c" gnus-summary-mail-crosspost-complaint
247   "Br" gnus-summary-reply-broken-reply-to
248   "BR" gnus-summary-reply-broken-reply-to-with-original
249   "om" gnus-summary-mail-forward
250   "op" gnus-summary-post-forward
251   "Om" gnus-uu-digest-mail-forward
252   "Op" gnus-uu-digest-post-forward)
253
254 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
255   "b" gnus-summary-resend-bounced-mail
256   ;; "c" gnus-summary-send-draft
257   "r" gnus-summary-resend-message)
258
259 ;;; Internal functions.
260
261 (defvar gnus-article-reply nil)
262 (defmacro gnus-setup-message (config &rest forms)
263   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
264         (buffer (make-symbol "gnus-setup-message-buffer"))
265         (article (make-symbol "gnus-setup-message-article"))
266         (group (make-symbol "gnus-setup-message-group")))
267     `(let ((,winconf (current-window-configuration))
268            (,buffer (buffer-name (current-buffer)))
269            (,article gnus-article-reply)
270            (,group gnus-newsgroup-name)
271            (message-header-setup-hook
272             (copy-sequence message-header-setup-hook))
273            (mbl mml-buffer-list)
274            (message-mode-hook (copy-sequence message-mode-hook)))
275        (setq mml-buffer-list nil)
276        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
277        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
278        ;; #### FIXME: for a reason that I did not manage to identify yet,
279        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
280        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
281        ;; After evaluation of @forms below, it gets the value we actually want
282        ;; to override, and the posting styles are used. For that reason, I've
283        ;; added an optional argument to `gnus-configure-posting-styles' to
284        ;; make sure that the correct value for the group name is used. -- drv
285        (add-hook 'message-mode-hook
286                  (lambda ()
287                    (gnus-configure-posting-styles ,group)))
288        (unwind-protect
289            (progn
290              ,@forms)
291          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config)
292          (gnus-inews-insert-draft-meta-information ,group ,article)
293          (setq gnus-message-buffer (current-buffer))
294          (set (make-local-variable 'gnus-message-group-art)
295               (cons ,group ,article))
296          (set (make-local-variable 'gnus-newsgroup-name) ,group)
297          (gnus-run-hooks 'gnus-message-setup-hook)
298          (if (eq major-mode 'message-mode)
299              (let ((mbl1 mml-buffer-list))
300                (setq mml-buffer-list mbl)  ;; Global value
301                (set (make-local-variable 'mml-buffer-list) mbl1);; Local value
302                ;; LOCAL argument of add-hook differs between GNU Emacs
303                ;; and XEmacs. make-local-hook makes sure they are local.
304                (make-local-hook 'kill-buffer-hook)
305                (make-local-hook 'change-major-mode-hook)
306                (add-hook 'change-major-mode-hook 'mml-destroy-buffers nil t)
307                (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))
308            (mml-destroy-buffers)
309            (setq mml-buffer-list mbl)))
310        (gnus-add-buffer)
311        (gnus-configure-windows ,config t)
312        (run-hooks 'post-command-hook)
313        (set-buffer-modified-p nil))))
314
315 (defun gnus-inews-insert-draft-meta-information (group article)
316   (save-excursion
317     (when (and group
318                (not (string= group ""))
319                (not (message-fetch-field gnus-draft-meta-information-header)))
320       (goto-char (point-min))
321       (insert gnus-draft-meta-information-header ": (\"" group "\" "
322               (if article (number-to-string
323                            (if (listp article)
324                                (car article)
325                              article)) "\"\"")
326               ")\n"))))
327
328 ;;;###autoload
329 (defun gnus-msg-mail (&optional to subject other-headers continue
330                                 switch-action yank-action send-actions)
331   "Start editing a mail message to be sent.
332 Like `message-mail', but with Gnus paraphernalia, particularly the
333 Gcc: header for archiving purposes."
334   (interactive)
335   (let ((buf (current-buffer))
336         mail-buf)
337     (gnus-setup-message 'message
338       (message-mail to subject other-headers continue
339                     nil yank-action send-actions))
340     (when switch-action
341       (setq mail-buf (current-buffer))
342       (switch-to-buffer buf)
343       (apply switch-action mail-buf nil)))
344   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
345   t)
346
347 (defvar save-selected-window-window)
348
349 ;;;###autoload
350 (defun gnus-button-mailto (address)
351   "Mail to ADDRESS."
352   (set-buffer (gnus-copy-article-buffer))
353   (gnus-setup-message 'message
354     (message-reply address))
355   (and (boundp 'save-selected-window-window)
356        (not (window-live-p save-selected-window-window))
357        (setq save-selected-window-window (selected-window))))
358
359 ;;;###autoload
360 (defun gnus-button-reply (&optional to-address wide)
361   "Like `message-reply'."
362   (interactive)
363   (gnus-setup-message 'message
364     (message-reply to-address wide))
365   (and (boundp 'save-selected-window-window)
366        (not (window-live-p save-selected-window-window))
367        (setq save-selected-window-window (selected-window))))
368
369 ;;;###autoload
370 (define-mail-user-agent 'gnus-user-agent
371   'gnus-msg-mail 'message-send-and-exit
372   'message-kill-buffer 'message-send-hook)
373
374 (defun gnus-setup-posting-charset (group)
375   (let ((alist gnus-group-posting-charset-alist)
376         (group (or group ""))
377         elem)
378     (when group
379       (catch 'found
380         (while (setq elem (pop alist))
381           (when (or (and (stringp (car elem))
382                          (string-match (car elem) group))
383                     (and (gnus-functionp (car elem))
384                          (funcall (car elem) group))
385                     (and (symbolp (car elem))
386                          (symbol-value (car elem))))
387             (throw 'found (cons (cadr elem) (caddr elem)))))))))
388
389 (defun gnus-inews-add-send-actions (winconf buffer article &optional config)
390   (make-local-hook 'message-sent-hook)
391   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
392                                  'gnus-inews-do-gcc) nil t)
393   (when gnus-agent
394     (make-local-hook 'message-header-hook)
395     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
396   (setq message-post-method
397         `(lambda (arg)
398            (gnus-post-method arg ,gnus-newsgroup-name)))
399   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
400   (message-add-action
401    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
402   (message-add-action
403    `(when (gnus-buffer-exists-p ,buffer)
404       (save-excursion
405         (set-buffer ,buffer)
406         ,(when article
407            (if (eq config 'forward)
408                `(gnus-summary-mark-article-as-forwarded ',article)
409              `(gnus-summary-mark-article-as-replied ',article)))))
410    'send))
411
412 (put 'gnus-setup-message 'lisp-indent-function 1)
413 (put 'gnus-setup-message 'edebug-form-spec '(form body))
414
415 ;;; Post news commands of Gnus group mode and summary mode
416
417 (defun gnus-group-mail (&optional arg)
418   "Start composing a mail.
419 If ARG, use the group under the point to find a posting style.
420 If ARG is 1, prompt for a group name to find the posting style."
421   (interactive "P")
422   ;; We can't `let' gnus-newsgroup-name here, since that leads
423   ;; to local variables leaking.
424   (let ((group gnus-newsgroup-name)
425         (buffer (current-buffer)))
426     (unwind-protect
427         (progn
428           (setq gnus-newsgroup-name
429                 (if arg
430                     (if (= 1 (prefix-numeric-value arg))
431                         (completing-read "Use posting style of group: "
432                                          gnus-active-hashtb nil
433                                          (gnus-read-active-file-p))
434                       (gnus-group-group-name))
435                   ""))
436           ;; #### see comment in gnus-setup-message -- drv
437           (gnus-setup-message 'message (message-mail)))
438       (save-excursion
439         (set-buffer buffer)
440         (setq gnus-newsgroup-name group)))))
441
442 (defun gnus-group-news (&optional arg)
443   "Start composing a news.
444 If ARG, post to group under point.
445 If ARG is 1, prompt for group name to post to.
446
447 This function prepares a news even when using mail groups.  This is useful
448 for posting messages to mail groups without actually sending them over the
449 network.  The corresponding backend must have a 'request-post method."
450   (interactive "P")
451   ;; We can't `let' gnus-newsgroup-name here, since that leads
452   ;; to local variables leaking.
453   (let ((group gnus-newsgroup-name)
454         (buffer (current-buffer)))
455     (unwind-protect
456         (progn
457           (setq gnus-newsgroup-name
458                 (if arg
459                     (if (= 1 (prefix-numeric-value arg))
460                         (completing-read "Use group: "
461                                          gnus-active-hashtb nil
462                                          (gnus-read-active-file-p))
463                       (gnus-group-group-name))
464                   ""))
465           ;; #### see comment in gnus-setup-message -- drv
466           (gnus-setup-message 'message
467             (message-news (gnus-group-real-name gnus-newsgroup-name))))
468       (save-excursion
469         (set-buffer buffer)
470         (setq gnus-newsgroup-name group)))))
471
472 (defun gnus-group-post-news (&optional arg)
473   "Start composing a message (a news by default).
474 If ARG, post to group under point.  If ARG is 1, prompt for group name.
475 Depending on the selected group, the message might be either a mail or
476 a news."
477   (interactive "P")
478   ;; Bind this variable here to make message mode hooks work ok.
479   (let ((gnus-newsgroup-name
480          (if arg
481              (if (= 1 (prefix-numeric-value arg))
482                  (completing-read "Newsgroup: " gnus-active-hashtb nil
483                                   (gnus-read-active-file-p))
484                (gnus-group-group-name))
485            "")))
486     (gnus-post-news 'post gnus-newsgroup-name)))
487
488 (defun gnus-summary-mail-other-window (&optional arg)
489   "Start composing a mail in another window.
490 Use the posting of the current group by default.
491 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
492 posting style."
493   (interactive "P")
494   ;; We can't `let' gnus-newsgroup-name here, since that leads
495   ;; to local variables leaking.
496   (let ((group gnus-newsgroup-name)
497         (buffer (current-buffer)))
498     (unwind-protect
499         (progn
500           (setq gnus-newsgroup-name
501                 (if arg
502                     (if (= 1 (prefix-numeric-value arg))
503                         (completing-read "Use group: "
504                                          gnus-active-hashtb nil
505                                          (gnus-read-active-file-p))
506                       "")
507                   gnus-newsgroup-name))
508           ;; #### see comment in gnus-setup-message -- drv
509           (gnus-setup-message 'message (message-mail)))
510       (save-excursion
511         (set-buffer buffer)
512         (setq gnus-newsgroup-name group)))))
513
514 (defun gnus-summary-news-other-window (&optional arg)
515   "Start composing a news in another window.
516 Post to the current group by default.
517 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
518
519 This function prepares a news even when using mail groups.  This is useful
520 for posting messages to mail groups without actually sending them over the
521 network.  The corresponding backend must have a 'request-post method."
522   (interactive "P")
523   ;; We can't `let' gnus-newsgroup-name here, since that leads
524   ;; to local variables leaking.
525   (let ((group gnus-newsgroup-name)
526         (buffer (current-buffer)))
527     (unwind-protect
528         (progn
529           (setq gnus-newsgroup-name
530                 (if arg
531                     (if (= 1 (prefix-numeric-value arg))
532                         (completing-read "Use group: "
533                                          gnus-active-hashtb nil
534                                          (gnus-read-active-file-p))
535                       "")
536                   gnus-newsgroup-name))
537           ;; #### see comment in gnus-setup-message -- drv
538           (gnus-setup-message 'message
539             (message-news (gnus-group-real-name gnus-newsgroup-name))))
540       (save-excursion
541         (set-buffer buffer)
542         (setq gnus-newsgroup-name group)))))
543
544 (defun gnus-summary-post-news (&optional arg)
545   "Start composing a message.  Post to the current group by default.
546 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
547 Depending on the selected group, the message might be either a mail or
548 a news."
549   (interactive "P")
550   ;; Bind this variable here to make message mode hooks work ok.
551   (let ((gnus-newsgroup-name
552          (if arg
553              (if (= 1 (prefix-numeric-value arg))
554                  (completing-read "Newsgroup: " gnus-active-hashtb nil
555                                   (gnus-read-active-file-p))
556                "")
557            gnus-newsgroup-name)))
558     (gnus-post-news 'post gnus-newsgroup-name)))
559
560
561 (defun gnus-summary-followup (yank &optional force-news)
562   "Compose a followup to an article.
563 If prefix argument YANK is non-nil, original article is yanked automatically."
564   (interactive
565    (list (and current-prefix-arg
566               (gnus-summary-work-articles 1))))
567   (when yank
568     (gnus-summary-goto-subject (car yank)))
569   (save-window-excursion
570     (gnus-summary-select-article))
571   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
572         (gnus-newsgroup-name gnus-newsgroup-name))
573     ;; Send a followup.
574     (gnus-post-news nil gnus-newsgroup-name
575                     headers gnus-article-buffer
576                     yank nil force-news)))
577
578 (defun gnus-summary-followup-with-original (n &optional force-news)
579   "Compose a followup to an article and include the original article."
580   (interactive "P")
581   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
582
583 (defun gnus-summary-followup-to-mail (&optional arg)
584   "Followup to the current mail message via news."
585   (interactive
586    (list (and current-prefix-arg
587               (gnus-summary-work-articles 1))))
588   (gnus-summary-followup arg t))
589
590 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
591   "Followup to the current mail message via news."
592   (interactive "P")
593   (gnus-summary-followup (gnus-summary-work-articles arg) t))
594
595 (defun gnus-inews-yank-articles (articles)
596   (let (beg article)
597     (message-goto-body)
598     (while (setq article (pop articles))
599       (save-window-excursion
600         (set-buffer gnus-summary-buffer)
601         (gnus-summary-select-article nil nil nil article)
602         (gnus-summary-remove-process-mark article))
603       (gnus-copy-article-buffer)
604       (let ((message-reply-buffer gnus-article-copy)
605             (message-reply-headers
606              (with-current-buffer gnus-article