0af7f10c40a30e4adde3e36d4312d8b595251430
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-ems)
33 (require 'message)
34 (require 'gnus-art)
35
36 (defcustom gnus-post-method 'current
37   "*Preferred method for posting USENET news.
38
39 If this variable is `current', Gnus will use the \"current\" select
40 method when posting.  If it is nil (which is the default), Gnus will
41 use the native select method when posting.
42
43 This method will not be used in mail groups and the like, only in
44 \"real\" newsgroups.
45
46 If not nil nor `native', the value must be a valid method as discussed
47 in the documentation of `gnus-select-method'. It can also be a list of
48 methods. If that is the case, the user will be queried for what select
49 method to use when posting."
50   :group 'gnus-group-foreign
51   :type `(choice (const nil)
52                  (const current)
53                  (const native)
54                  (sexp :tag "Methods" ,gnus-select-method)))
55
56 (defvar gnus-outgoing-message-group nil
57   "*All outgoing messages will be put in this group.
58 If you want to store all your outgoing mail and articles in the group
59 \"nnml:archive\", you set this variable to that value.  This variable
60 can also be a list of group names.
61
62 If you want to have greater control over what group to put each
63 message in, you can set this variable to a function that checks the
64 current newsgroup name and then returns a suitable group name (or list
65 of names).")
66
67 (defvar gnus-mailing-list-groups nil
68   "*Regexp matching groups that are really mailing lists.
69 This is useful when you're reading a mailing list that has been
70 gatewayed to a newsgroup, and you want to followup to an article in
71 the group.")
72
73 (defvar gnus-add-to-list nil
74   "*If non-nil, add a `to-list' parameter automatically.")
75
76 (defvar gnus-crosspost-complaint
77   "Hi,
78
79 You posted the article below with the following Newsgroups header:
80
81 Newsgroups: %s
82
83 The %s group, at least, was an inappropriate recipient
84 of this message.  Please trim your Newsgroups header to exclude this
85 group before posting in the future.
86
87 Thank you.
88
89 "
90   "Format string to be inserted when complaining about crossposts.
91 The first %s will be replaced by the Newsgroups header;
92 the second with the current group name.")
93
94 (defvar gnus-message-setup-hook nil
95   "Hook run after setting up a message buffer.")
96
97 (defvar gnus-bug-create-help-buffer t
98   "*Should we create the *Gnus Help Bug* buffer?")
99
100 (defvar gnus-posting-styles nil
101   "*Alist of styles to use when posting.")
102
103 (defcustom gnus-group-posting-charset-alist
104   '(("^no\\." iso-8859-1)
105     (message-this-is-mail nil)
106     ("^de\\." nil)
107     (".*" iso-8859-1)
108     (message-this-is-news iso-8859-1))
109   "Alist of regexps (to match group names) and default charsets to be unencoded when posting."
110   :type '(repeat (list (regexp :tag "Group")
111                        (symbol :tag "Charset")))
112   :group 'gnus-charset)
113
114 ;;; Internal variables.
115
116 (defvar gnus-inhibit-posting-styles nil
117   "Inhibit the use of posting styles.")
118
119 (defvar gnus-message-buffer "*Mail Gnus*")
120 (defvar gnus-article-copy nil)
121 (defvar gnus-last-posting-server nil)
122 (defvar gnus-message-group-art nil)
123
124 (defconst gnus-bug-message
125   "Sending a bug report to the Gnus Towers.
126 ========================================
127
128 The buffer below is a mail buffer.  When you press `C-c C-c', it will
129 be sent to the Gnus Bug Exterminators.
130
131 The thing near the bottom of the buffer is how the environment
132 settings will be included in the mail.  Please do not delete that.
133 They will tell the Bug People what your environment is, so that it
134 will be easier to locate the bugs.
135
136 If you have found a bug that makes Emacs go \"beep\", set
137 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
138 and include the backtrace in your bug report.
139
140 Please describe the bug in annoying, painstaking detail.
141
142 Thank you for your help in stamping out bugs.
143 ")
144
145 (eval-and-compile
146   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
147   (autoload 'news-setup "rnewspost")
148   (autoload 'news-reply-mode "rnewspost")
149   (autoload 'rmail-dont-reply-to "mail-utils")
150   (autoload 'rmail-output "rmailout"))
151
152 \f
153 ;;;
154 ;;; Gnus Posting Functions
155 ;;;
156
157 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
158   "p" gnus-summary-post-news
159   "f" gnus-summary-followup
160   "F" gnus-summary-followup-with-original
161   "c" gnus-summary-cancel-article
162   "s" gnus-summary-supersede-article
163   "r" gnus-summary-reply
164   "y" gnus-summary-yank-message
165   "R" gnus-summary-reply-with-original
166   "w" gnus-summary-wide-reply
167   "W" gnus-summary-wide-reply-with-original
168   "n" gnus-summary-followup-to-mail
169   "N" gnus-summary-followup-to-mail-with-original
170   "m" gnus-summary-mail-other-window
171   "u" gnus-uu-post-news
172   "\M-c" gnus-summary-mail-crosspost-complaint
173   "om" gnus-summary-mail-forward
174   "op" gnus-summary-post-forward
175   "Om" gnus-uu-digest-mail-forward
176   "Op" gnus-uu-digest-post-forward)
177
178 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
179   "b" gnus-summary-resend-bounced-mail
180   ;; "c" gnus-summary-send-draft
181   "r" gnus-summary-resend-message)
182
183 ;;; Internal functions.
184
185 (defvar gnus-article-reply nil)
186 (defmacro gnus-setup-message (config &rest forms)
187   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
188         (buffer (make-symbol "gnus-setup-message-buffer"))
189         (article (make-symbol "gnus-setup-message-article"))
190         (group (make-symbol "gnus-setup-message-group")))
191     `(let ((,winconf (current-window-configuration))
192            (,buffer (buffer-name (current-buffer)))
193            (,article (and gnus-article-reply (gnus-summary-article-number)))
194            (,group gnus-newsgroup-name)
195            (message-header-setup-hook
196             (copy-sequence message-header-setup-hook))
197            (message-mode-hook (copy-sequence message-mode-hook)))
198        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
199        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
200        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
201        (unwind-protect
202            (progn
203              ,@forms)
204          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
205          (setq gnus-message-buffer (current-buffer))
206          (set (make-local-variable 'gnus-message-group-art)
207               (cons ,group ,article))
208          (set (make-local-variable 'gnus-newsgroup-name) ,group)
209          (set (make-local-variable 'message-posting-charset)
210               (gnus-setup-posting-charset ,group))
211          (gnus-run-hooks 'gnus-message-setup-hook))
212        (gnus-add-buffer)
213        (gnus-configure-windows ,config t)
214        (set-buffer-modified-p nil))))
215
216 (defun gnus-setup-posting-charset (group)
217   (let ((alist gnus-group-posting-charset-alist)
218         (group (or group ""))
219         elem)
220     (when group
221       (catch 'found
222         (while (setq elem (pop alist))
223           (when (or (and (stringp (car elem))
224                          (string-match (car elem) group))
225                     (and (gnus-functionp (car elem))
226                          (funcall (car elem) group))
227                     (and (symbolp (car elem))
228                          (symbol-value (car elem))))
229             (throw 'found (cadr elem))))))))
230
231 (defun gnus-inews-add-send-actions (winconf buffer article)
232   (make-local-hook 'message-sent-hook)
233   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
234   (setq message-post-method
235         `(lambda (arg)
236            (gnus-post-method arg ,gnus-newsgroup-name)))
237   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
238   (message-add-action
239    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
240   (message-add-action
241    `(when (gnus-buffer-exists-p ,buffer)
242       (save-excursion
243         (set-buffer ,buffer)
244         ,(when article
245            `(gnus-summary-mark-article-as-replied ,article))))
246    'send))
247
248 (put 'gnus-setup-message 'lisp-indent-function 1)
249 (put 'gnus-setup-message 'edebug-form-spec '(form body))
250
251 ;;; Post news commands of Gnus group mode and summary mode
252
253 (defun gnus-group-mail (&optional arg)
254   "Start composing a mail.
255 If ARG, use the group under the point to find a posting style.
256 If ARG is 1, prompt for a group name to find the posting style."
257   (interactive "P")
258   ;; We can't `let' gnus-newsgroup-name here, since that leads
259   ;; to local variables leaking.
260   (let ((group gnus-newsgroup-name)
261         (buffer (current-buffer)))
262     (unwind-protect
263         (progn
264           (setq gnus-newsgroup-name
265                 (if arg
266                     (if (= 1 (prefix-numeric-value arg))
267                         (completing-read "Use posting style of group: "
268                                          gnus-active-hashtb nil
269                                          (gnus-read-active-file-p))
270                       (gnus-group-group-name))
271                   ""))
272           (gnus-setup-message 'message (message-mail)))
273       (save-excursion
274         (set-buffer buffer)
275         (setq gnus-newsgroup-name group)))))
276
277 (defun gnus-group-post-news (&optional arg)
278   "Start composing a news message.
279 If ARG, post to the group under point.
280 If ARG is 1, prompt for a group name."
281   (interactive "P")
282   ;; Bind this variable here to make message mode hooks work ok.
283   (let ((gnus-newsgroup-name
284          (if arg
285              (if (= 1 (prefix-numeric-value arg))
286                  (completing-read "Newsgroup: " gnus-active-hashtb nil
287                                   (gnus-read-active-file-p))
288                (gnus-group-group-name))
289            "")))
290     (gnus-post-news 'post gnus-newsgroup-name)))
291
292 (defun gnus-summary-post-news ()
293   "Start composing a news message."
294   (interactive)
295   (gnus-post-news 'post gnus-newsgroup-name))
296
297 (defun gnus-summary-followup (yank &optional force-news)
298   "Compose a followup to an article.
299 If prefix argument YANK is non-nil, original article is yanked automatically."
300   (interactive
301    (list (and current-prefix-arg
302               (gnus-summary-work-articles 1))))
303   (when yank
304     (gnus-summary-goto-subject (car yank)))
305   (save-window-excursion
306     (gnus-summary-select-article))
307   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
308         (gnus-newsgroup-name gnus-newsgroup-name))
309     ;; Send a followup.
310     (gnus-post-news nil gnus-newsgroup-name
311                     headers gnus-article-buffer
312                     yank nil force-news)))
313
314 (defun gnus-summary-followup-with-original (n &optional force-news)
315   "Compose a followup to an article and include the original article."
316   (interactive "P")
317   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
318
319 (defun gnus-summary-followup-to-mail (&optional arg)
320   "Followup to the current mail message via news."
321   (interactive
322    (list (and current-prefix-arg
323               (gnus-summary-work-articles 1))))
324   (gnus-summary-followup arg t))
325
326 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
327   "Followup to the current mail message via news."
328   (interactive "P")
329   (gnus-summary-followup (gnus-summary-work-articles arg) t))
330
331 (defun gnus-inews-yank-articles (articles)
332   (let (beg article)
333     (message-goto-body)
334     (while (setq article (pop articles))
335       (save-window-excursion
336         (set-buffer gnus-summary-buffer)
337         (gnus-summary-select-article nil nil nil article)
338         (gnus-summary-remove-process-mark article))
339       (gnus-copy-article-buffer)
340       (let ((message-reply-buffer gnus-article-copy)
341             (message-reply-headers gnus-current-headers))
342         (message-yank-original)
343         (setq beg (or beg (mark t))))
344       (when articles
345         (insert "\n")))
346     (push-mark)
347     (goto-char beg)))
348
349 (defun gnus-summary-cancel-article (&optional n symp)
350   "Cancel an article you posted.
351 Uses the process-prefix convention.  If given the symbolic
352 prefix `a', cancel using the standard posting method; if not
353 post using the current select method."
354   (interactive (gnus-interactive "P\ny"))
355   (let ((articles (gnus-summary-work-articles n))
356         (message-post-method
357          `(lambda (arg)
358             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
359         article)
360     (while (setq article (pop articles))
361       (when (gnus-summary-select-article t nil nil article)
362         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
363                 (message-cancel-news))
364           (gnus-summary-mark-as-read article gnus-canceled-mark)
365           (gnus-cache-remove-article 1))
366         (gnus-article-hide-headers-if-wanted))
367       (gnus-summary-remove-process-mark article))))
368
369 (defun gnus-summary-supersede-article ()
370   "Compose an article that will supersede a previous article.
371 This is done simply by taking the old article and adding a Supersedes
372 header line with the old Message-ID."
373   (interactive)
374   (let ((article (gnus-summary-article-number)))
375     (gnus-setup-message 'reply-yank
376       (gnus-summary-select-article t)
377       (set-buffer gnus-original-article-buffer)
378       (message-supersede)
379       (push
380        `((lambda ()
381            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
382              (save-excursion
383                (set-buffer ,gnus-summary-buffer)
384                (gnus-cache-possibly-remove-article ,article nil nil nil t)
385                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
386        message-send-actions))))
387
388 \f
389
390 (defun gnus-copy-article-buffer (&optional article-buffer)
391   ;; make a copy of the article buffer with all text properties removed
392   ;; this copy is in the buffer gnus-article-copy.
393   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
394   ;; this buffer should be passed to all mail/news reply/post routines.
395   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
396   (save-excursion
397     (set-buffer gnus-article-copy)
398     (mm-enable-multibyte))
399   (let ((article-buffer (or article-buffer gnus-article-buffer))
400         end beg)
401     (if (not (and (get-buffer article-buffer)
402                   (gnus-buffer-exists-p article-buffer)))
403         (error "Can't find any article buffer")
404       (save-excursion
405         (set-buffer article-buffer)
406         (save-restriction
407           ;; Copy over the (displayed) article buffer, delete
408           ;; hidden text and remove text properties.
409           (widen)
410           (copy-to-buffer gnus-article-copy (point-min) (point-max))
411           (set-buffer gnus-article-copy)
412           (gnus-article-delete-text-of-type 'annotation)
413           (gnus-remove-text-with-property 'gnus-prev)
414           (gnus-remove-text-with-property 'gnus-next)
415           (insert
416            (prog1
417                (format "%s" (buffer-string))
418              (erase-buffer)))
419           ;; Find the original headers.
420           (set-buffer gnus-original-article-buffer)
421           (goto-char (point-min))
422           (while (looking-at message-unix-mail-delimiter)
423             (forward-line 1))
424           (setq beg (point))
425           (setq end&nbs