6ce2c66e849f5428c8c5228d173d20678706ca08
[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   "R" gnus-summary-reply-with-original
165   "w" gnus-summary-wide-reply
166   "W" gnus-summary-wide-reply-with-original
167   "n" gnus-summary-followup-to-mail
168   "N" gnus-summary-followup-to-mail-with-original
169   "m" gnus-summary-mail-other-window
170   "u" gnus-uu-post-news
171   "\M-c" gnus-summary-mail-crosspost-complaint
172   "om" gnus-summary-mail-forward
173   "op" gnus-summary-post-forward
174   "Om" gnus-uu-digest-mail-forward
175   "Op" gnus-uu-digest-post-forward)
176
177 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
178   "b" gnus-summary-resend-bounced-mail
179   ;; "c" gnus-summary-send-draft
180   "r" gnus-summary-resend-message)
181
182 ;;; Internal functions.
183
184 (defvar gnus-article-reply nil)
185 (defmacro gnus-setup-message (config &rest forms)
186   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
187         (buffer (make-symbol "gnus-setup-message-buffer"))
188         (article (make-symbol "gnus-setup-message-article"))
189         (group (make-symbol "gnus-setup-message-group")))
190     `(let ((,winconf (current-window-configuration))
191            (,buffer (buffer-name (current-buffer)))
192            (,article (and gnus-article-reply (gnus-summary-article-number)))
193            (,group gnus-newsgroup-name)
194            (message-header-setup-hook
195             (copy-sequence message-header-setup-hook))
196            (message-mode-hook (copy-sequence message-mode-hook)))
197        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
198        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
199        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
200        (unwind-protect
201            (progn
202              ,@forms)
203          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
204          (setq gnus-message-buffer (current-buffer))
205          (set (make-local-variable 'gnus-message-group-art)
206               (cons ,group ,article))
207          (set (make-local-variable 'gnus-newsgroup-name) ,group)
208          (set (make-local-variable 'message-posting-charset)
209               (gnus-setup-posting-charset ,group))
210          (gnus-run-hooks 'gnus-message-setup-hook))
211        (gnus-add-buffer)
212        (gnus-configure-windows ,config t)
213        (set-buffer-modified-p nil))))
214
215 (defun gnus-setup-posting-charset (group)
216   (let ((alist gnus-group-posting-charset-alist)
217         (group (or group ""))
218         elem)
219     (when group
220       (catch 'found
221         (while (setq elem (pop alist))
222           (when (or (and (stringp (car elem))
223                          (string-match (car elem) group))
224                     (and (gnus-functionp (car elem))
225                          (funcall (car elem) group))
226                     (and (symbolp (car elem))
227                          (symbol-value (car elem))))
228             (throw 'found (cadr elem))))))))
229
230 (defun gnus-inews-add-send-actions (winconf buffer article)
231   (make-local-hook 'message-sent-hook)
232   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
233   (setq message-post-method
234         `(lambda (arg)
235            (gnus-post-method arg ,gnus-newsgroup-name)))
236   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
237   (message-add-action
238    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
239   (message-add-action
240    `(when (gnus-buffer-exists-p ,buffer)
241       (save-excursion
242         (set-buffer ,buffer)
243         ,(when article
244            `(gnus-summary-mark-article-as-replied ,article))))
245    'send))
246
247 (put 'gnus-setup-message 'lisp-indent-function 1)
248 (put 'gnus-setup-message 'edebug-form-spec '(form body))
249
250 ;;; Post news commands of Gnus group mode and summary mode
251
252 (defun gnus-group-mail (&optional arg)
253   "Start composing a mail.
254 If ARG, use the group under the point to find a posting style.
255 If ARG is 1, prompt for a group name to find the posting style."
256   (interactive "P")
257   ;; We can't `let' gnus-newsgroup-name here, since that leads
258   ;; to local variables leaking.
259   (let ((group gnus-newsgroup-name)
260         (buffer (current-buffer)))
261     (unwind-protect
262         (progn
263           (setq gnus-newsgroup-name
264                 (if arg
265                     (if (= 1 (prefix-numeric-value arg))
266                         (completing-read "Use posting style of group: "
267                                          gnus-active-hashtb nil
268                                          (gnus-read-active-file-p))
269                       (gnus-group-group-name))
270                   ""))
271           (gnus-setup-message 'message (message-mail)))
272       (save-excursion
273         (set-buffer buffer)
274         (setq gnus-newsgroup-name group)))))
275
276 (defun gnus-group-post-news (&optional arg)
277   "Start composing a news message.
278 If ARG, post to the group under point.
279 If ARG is 1, prompt for a group name."
280   (interactive "P")
281   ;; Bind this variable here to make message mode hooks work ok.
282   (let ((gnus-newsgroup-name
283          (if arg
284              (if (= 1 (prefix-numeric-value arg))
285                  (completing-read "Newsgroup: " gnus-active-hashtb nil
286                                   (gnus-read-active-file-p))
287                (gnus-group-group-name))
288            "")))
289     (gnus-post-news 'post gnus-newsgroup-name)))
290
291 (defun gnus-summary-post-news ()
292   "Start composing a news message."
293   (interactive)
294   (gnus-post-news 'post gnus-newsgroup-name))
295
296 (defun gnus-summary-followup (yank &optional force-news)
297   "Compose a followup to an article.
298 If prefix argument YANK is non-nil, original article is yanked automatically."
299   (interactive
300    (list (and current-prefix-arg
301               (gnus-summary-work-articles 1))))
302   (when yank
303     (gnus-summary-goto-subject (car yank)))
304   (save-window-excursion
305     (gnus-summary-select-article))
306   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
307         (gnus-newsgroup-name gnus-newsgroup-name))
308     ;; Send a followup.
309     (gnus-post-news nil gnus-newsgroup-name
310                     headers gnus-article-buffer
311                     yank nil force-news)))
312
313 (defun gnus-summary-followup-with-original (n &optional force-news)
314   "Compose a followup to an article and include the original article."
315   (interactive "P")
316   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
317
318 (defun gnus-summary-followup-to-mail (&optional arg)
319   "Followup to the current mail message via news."
320   (interactive
321    (list (and current-prefix-arg
322               (gnus-summary-work-articles 1))))
323   (gnus-summary-followup arg t))
324
325 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
326   "Followup to the current mail message via news."
327   (interactive "P")
328   (gnus-summary-followup (gnus-summary-work-articles arg) t))
329
330 (defun gnus-inews-yank-articles (articles)
331   (let (beg article)
332     (message-goto-body)
333     (while (setq article (pop articles))
334       (save-window-excursion
335         (set-buffer gnus-summary-buffer)
336         (gnus-summary-select-article nil nil nil article)
337         (gnus-summary-remove-process-mark article))
338       (gnus-copy-article-buffer)
339       (let ((message-reply-buffer gnus-article-copy)
340             (message-reply-headers gnus-current-headers))
341         (message-yank-original)
342         (setq beg (or beg (mark t))))
343       (when articles
344         (insert "\n")))
345     (push-mark)
346     (goto-char beg)))
347
348 (defun gnus-summary-cancel-article (&optional n symp)
349   "Cancel an article you posted.
350 Uses the process-prefix convention.  If given the symbolic
351 prefix `a', cancel using the standard posting method; if not
352 post using the current select method."
353   (interactive (gnus-interactive "P\ny"))
354   (let ((articles (gnus-summary-work-articles n))
355         (message-post-method
356          `(lambda (arg)
357             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
358         article)
359     (while (setq article (pop articles))
360       (when (gnus-summary-select-article t nil nil article)
361         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
362                 (message-cancel-news))
363           (gnus-summary-mark-as-read article gnus-canceled-mark)
364           (gnus-cache-remove-article 1))
365         (gnus-article-hide-headers-if-wanted))
366       (gnus-summary-remove-process-mark article))))
367
368 (defun gnus-summary-supersede-article ()
369   "Compose an article that will supersede a previous article.
370 This is done simply by taking the old article and adding a Supersedes
371 header line with the old Message-ID."
372   (interactive)
373   (let ((article (gnus-summary-article-number)))
374     (gnus-setup-message 'reply-yank
375       (gnus-summary-select-article t)
376       (set-buffer gnus-original-article-buffer)
377       (message-supersede)
378       (push
379        `((lambda ()
380            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
381              (save-excursion
382                (set-buffer ,gnus-summary-buffer)
383                (gnus-cache-possibly-remove-article ,article nil nil nil t)
384                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
385        message-send-actions))))
386
387 \f
388
389 (defun gnus-copy-article-buffer (&optional article-buffer)
390   ;; make a copy of the article buffer with all text properties removed
391   ;; this copy is in the buffer gnus-article-copy.
392   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
393   ;; this buffer should be passed to all mail/news reply/post routines.
394   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
395   (save-excursion
396     (set-buffer gnus-article-copy)
397     (mm-enable-multibyte))
398   (let ((article-buffer (or article-buffer gnus-article-buffer))
399         end beg)
400     (if (not (and (get-buffer article-buffer)
401                   (gnus-buffer-exists-p article-buffer)))
402         (error "Can't find any article buffer")
403       (save-excursion
404         (set-buffer article-buffer)
405         (save-restriction
406           ;; Copy over the (displayed) article buffer, delete
407           ;; hidden text and remove text properties.
408           (widen)
409           (copy-to-buffer gnus-article-copy (point-min) (point-max))
410           (set-buffer gnus-article-copy)
411           (gnus-article-delete-text-of-type 'annotation)
412           (gnus-remove-text-with-property 'gnus-prev)
413           (gnus-remove-text-with-property 'gnus-next)
414           (insert
415            (prog1
416                (format "%s" (buffer-string))
417              (erase-buffer)))
418           ;; Find the original headers.
419           (set-buffer gnus-original-article-buffer)
420           (goto-char (point-min))
421           (while (looking-at message-unix-mail-delimiter)
422             (forward-line 1))
423           (setq beg (point))
424           (setq end (or (search-forward "\n\n" nil t) (point)))
425           ;; Delete the headers from the displayed articles.
426           (set-buffer gnus-article-copy)
427           (delete-region (goto-char (point-min))
428                          (or (search-forward "\n\n" nil t) (point-max)))
429           ;; Insert the original article headers.
430           (insert-buffer-substring gnus-original-article-buffer beg end)
431           (article-decode-encoded-words)))
432       gnus-article-copy)))
433
434 (defun gnus-post-news (post &optional group header article-buffer yank subject
435                             force-news)
436   (when article-buffer
437     (gnus-copy-article-buffer))
438   (let ((gnus-article-reply article-buffer)
439         (add-to-list gnus-add-to-list))
440     (gnus-setup-message (cond (yank 'reply-yank)
441                               (article-buffer 'reply)
442                               (t 'message))
443       (let* ((group (or group gnus-newsgroup-name))
444              (pgroup group)
445              to-address to-group mailing-list to-list
446              newsgroup-p)
447         (when group
448           (setq to-address (gnus-group-find-parameter group 'to-address)
449                 to-group (gnus-group-find-parameter group 'to-group)
450                 to-list (gnus-group-find-parameter group 'to-list)
451                 newsgroup-p&n