*** empty log message ***
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (require 'gnus)
30 (require 'gnus-ems)
31 (require 'message)
32 (require 'gnus-art)
33
34 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
35 (defvar gnus-post-method nil
36   "*Preferred method for posting USENET news.
37 If this variable is nil, Gnus will use the current method to decide
38 which method to use when posting.  If it is non-nil, it will override
39 the current method.  This method will not be used in mail groups and
40 the like, only in \"real\" newsgroups.
41
42 The value must be a valid method as discussed in the documentation of
43 `gnus-select-method'.  It can also be a list of methods.  If that is
44 the case, the user will be queried for what select method to use when
45 posting.")
46
47 (defvar gnus-outgoing-message-group nil
48   "*All outgoing messages will be put in this group.
49 If you want to store all your outgoing mail and articles in the group
50 \"nnml:archive\", you set this variable to that value.  This variable
51 can also be a list of group names.
52
53 If you want to have greater control over what group to put each
54 message in, you can set this variable to a function that checks the
55 current newsgroup name and then returns a suitable group name (or list
56 of names).")
57
58 (defvar gnus-mailing-list-groups nil
59   "*Regexp matching groups that are really mailing lists.
60 This is useful when you're reading a mailing list that has been
61 gatewayed to a newsgroup, and you want to followup to an article in
62 the group.")
63
64 (defvar gnus-sent-message-ids-file
65   (nnheader-concat gnus-directory "Sent-Message-IDs")
66   "File where Gnus saves a cache of sent message ids.")
67
68 (defvar gnus-sent-message-ids-length 1000
69   "The number of sent Message-IDs to save.")
70
71 (defvar gnus-crosspost-complaint
72   "Hi,
73
74 You posted the article below with the following Newsgroups header:
75
76 Newsgroups: %s
77
78 The %s group, at least, was an inappropriate recipient
79 of this message.  Please trim your Newsgroups header to exclude this
80 group before posting in the future.
81
82 Thank you.
83
84 "
85   "Format string to be inserted when complaining about crossposts.
86 The first %s will be replaced by the Newsgroups header;
87 the second with the current group name.")
88
89 (defvar gnus-message-setup-hook nil
90   "Hook run after setting up a message buffer.")
91
92 ;;; Internal variables.
93
94 (defvar gnus-message-buffer "*Mail Gnus*")
95 (defvar gnus-article-copy nil)
96 (defvar gnus-last-posting-server nil)
97
98 (defconst gnus-bug-message
99   "Sending a bug report to the Gnus Towers.
100 ========================================
101
102 The buffer below is a mail buffer.  When you press `C-c C-c', it will
103 be sent to the Gnus Bug Exterminators.
104
105 At the bottom of the buffer you'll see lots of variable settings.
106 Please do not delete those.  They will tell the Bug People what your
107 environment is, so that it will be easier to locate the bugs.
108
109 If you have found a bug that makes Emacs go \"beep\", set
110 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
111 and include the backtrace in your bug report.
112
113 Please describe the bug in annoying, painstaking detail.
114
115 Thank you for your help in stamping out bugs.
116 ")
117
118 (eval-and-compile
119   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
120   (autoload 'news-setup "rnewspost")
121   (autoload 'news-reply-mode "rnewspost")
122   (autoload 'rmail-dont-reply-to "mail-utils")
123   (autoload 'rmail-output "rmailout"))
124
125 \f
126 ;;;
127 ;;; Gnus Posting Functions
128 ;;;
129
130 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
131   "p" gnus-summary-post-news
132   "f" gnus-summary-followup
133   "F" gnus-summary-followup-with-original
134   "c" gnus-summary-cancel-article
135   "s" gnus-summary-supersede-article
136   "r" gnus-summary-reply
137   "R" gnus-summary-reply-with-original
138   "w" gnus-summary-wide-reply
139   "W" gnus-summary-wide-reply-with-original
140   "n" gnus-summary-followup-to-mail
141   "N" gnus-summary-followup-to-mail-with-original
142   "m" gnus-summary-mail-other-window
143   "u" gnus-uu-post-news
144   "\M-c" gnus-summary-mail-crosspost-complaint
145   "om" gnus-summary-mail-forward
146   "op" gnus-summary-post-forward
147   "Om" gnus-uu-digest-mail-forward
148   "Op" gnus-uu-digest-post-forward)
149
150 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
151   "b" gnus-summary-resend-bounced-mail
152   ;; "c" gnus-summary-send-draft
153   "r" gnus-summary-resend-message)
154
155 ;;; Internal functions.
156
157 (defvar gnus-article-reply nil)
158 (defmacro gnus-setup-message (config &rest forms)
159   (let ((winconf (make-symbol "winconf"))
160         (buffer (make-symbol "buffer"))
161         (article (make-symbol "article")))
162     `(let ((,winconf (current-window-configuration))
163            (,buffer (buffer-name (current-buffer)))
164            (,article (and gnus-article-reply (gnus-summary-article-number)))
165            (message-header-setup-hook
166             (copy-sequence message-header-setup-hook)))
167        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
168        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
169        (unwind-protect
170            ,@forms
171          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
172          (setq gnus-message-buffer (current-buffer))
173          (make-local-variable 'gnus-newsgroup-name)
174          (run-hooks 'gnus-message-setup-hook))
175        (gnus-configure-windows ,config t)
176        (set-buffer-modified-p nil))))
177
178 (defun gnus-inews-add-send-actions (winconf buffer article)
179   (make-local-hook 'message-sent-hook)
180   (gnus-add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
181   (setq message-post-method
182         `(lambda (arg)
183            (gnus-post-method arg ,gnus-newsgroup-name)))
184   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
185   (message-add-action
186    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
187   (message-add-action
188    `(when (buffer-name (get-buffer ,buffer))
189       (save-excursion
190         (set-buffer (get-buffer ,buffer))
191         ,(when article
192            `(gnus-summary-mark-article-as-replied ,article))))
193    'send))
194
195 (put 'gnus-setup-message 'lisp-indent-function 1)
196 (put 'gnus-setup-message 'edebug-form-spec '(form body))
197
198 ;;; Post news commands of Gnus group mode and summary mode
199
200 (defun gnus-group-mail ()
201   "Start composing a mail."
202   (interactive)
203   (gnus-setup-message 'message
204     (message-mail)))
205
206 (defun gnus-group-post-news (&optional arg)
207   "Start composing a news message.
208 If ARG, post to the group under point.
209 If ARG is 1, prompt for a group name."
210   (interactive "P")
211   ;; Bind this variable here to make message mode hooks
212   ;; work ok.
213   (let ((gnus-newsgroup-name
214          (if arg
215              (if (= 1 (prefix-numeric-value arg))
216                  (completing-read "Newsgroup: " gnus-active-hashtb nil
217                                   (gnus-read-active-file-p))
218                (gnus-group-group-name))
219            "")))
220     (gnus-post-news 'post gnus-newsgroup-name)))
221
222 (defun gnus-summary-post-news ()
223   "Start composing a news message."
224   (interactive)
225   (gnus-set-global-variables)
226   (gnus-post-news 'post gnus-newsgroup-name))
227
228 (defun gnus-summary-followup (yank &optional force-news)
229   "Compose a followup to an article.
230 If prefix argument YANK is non-nil, original article is yanked automatically."
231   (interactive
232    (list (and current-prefix-arg
233               (gnus-summary-work-articles 1))))
234   (gnus-set-global-variables)
235   (when yank
236     (gnus-summary-goto-subject (car yank)))
237   (save-window-excursion
238     (gnus-summary-select-article))
239   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
240         (gnus-newsgroup-name gnus-newsgroup-name))
241     ;; Send a followup.
242     (gnus-post-news nil gnus-newsgroup-name
243                     headers gnus-article-buffer
244                     yank nil force-news)))
245
246 (defun gnus-summary-followup-with-original (n &optional force-news)
247   "Compose a followup to an article and include the original article."
248   (interactive "P")
249   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
250
251 (defun gnus-summary-followup-to-mail (&optional arg)
252   "Followup to the current mail message via news."
253   (interactive
254    (list (and current-prefix-arg
255               (gnus-summary-work-articles 1))))
256   (gnus-summary-followup arg t))
257
258 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
259   "Followup to the current mail message via news."
260   (interactive "P")
261   (gnus-summary-followup (gnus-summary-work-articles arg) t))
262
263 (defun gnus-inews-yank-articles (articles)
264   (let (beg article)
265     (message-goto-body)
266     (while (setq article (pop articles))
267       (save-window-excursion
268         (set-buffer gnus-summary-buffer)
269         (gnus-summary-select-article nil nil nil article)
270         (gnus-summary-remove-process-mark article))
271       (gnus-copy-article-buffer)
272       (let ((message-reply-buffer gnus-article-copy)
273             (message-reply-headers gnus-current-headers))
274         (message-yank-original)
275         (setq beg (or beg (mark t))))
276       (when articles
277         (insert "\n")))
278     (push-mark)
279     (goto-char beg)))
280
281 (defun gnus-summary-cancel-article (n)
282   "Cancel an article you posted."
283   (interactive "P")
284   (gnus-set-global-variables)
285   (let ((articles (gnus-summary-work-articles n))
286         (message-post-method
287          `(lambda (arg)
288             (gnus-post-method nil ,gnus-newsgroup-name)))
289         article)
290     (while (setq article (pop articles))
291       (when (gnus-summary-select-article t nil nil article)
292         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
293                 (message-cancel-news))
294           (gnus-summary-mark-as-read article gnus-canceled-mark)
295           (gnus-cache-remove-article 1))
296         (gnus-article-hide-headers-if-wanted))
297       (gnus-summary-remove-process-mark article))))
298
299 (defun gnus-summary-supersede-article ()
300   "Compose an article that will supersede a previous article.
301 This is done simply by taking the old article and adding a Supersedes
302 header line with the old Message-ID."
303   (interactive)
304   (gnus-set-global-variables)
305   (let ((article (gnus-summary-article-number)))
306     (gnus-setup-message 'reply-yank
307       (gnus-summary-select-article t)
308       (set-buffer gnus-original-article-buffer)
309       (message-supersede)
310       (push
311        `((lambda ()
312            (when (buffer-name (get-buffer ,gnus-summary-buffer))
313              (save-excursion
314                (set-buffer (get-buffer ,gnus-summary-buffer))
315                (gnus-cache-possibly-remove-article ,article nil nil nil t)
316                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
317        message-send-actions))))
318
319 \f
320
321 (defun gnus-copy-article-buffer (&optional article-buffer)
322   ;; make a copy of the article buffer with all text properties removed
323   ;; this copy is in the buffer gnus-article-copy.
324   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
325   ;; this buffer should be passed to all mail/news reply/post routines.
326   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
327   (buffer-disable-undo gnus-article-copy)
328   (or (memq gnus-article-copy gnus-buffer-list)
329       (push gnus-article-copy gnus-buffer-list))
330   (let ((article-buffer (or article-buffer gnus-article-buffer))
331         end beg contents)
332     (if (not (and (get-buffer article-buffer)
333                   (buffer-name (get-buffer article-buffer))))
334         (error "Can't find any article buffer")
335       (save-excursion
336         (set-buffer article-buffer)
337         (save-restriction
338           ;; Copy over the (displayed) article buffer, delete
339           ;; hidden text and remove text properties.
340           (widen)
341           (copy-to-buffer gnus-article-copy (point-min) (point-max))
342           (set-buffer gnus-article-copy)
343           (gnus-article-delete-text-of-type 'annotation)
344           (gnus-remove-text-with-property 'gnus-prev)
345           (gnus-remove-text-with-property 'gnus-next)
346           (insert
347            (prog1
348                (format "%s" (buffer-string))
349              (erase-buffer)))
350           ;; Find the original headers.
351           (set-buffer gnus-original-article-buffer)
352           (goto-char (point-min))
353           (while (looking-at message-unix-mail-delimiter)
354             (forward-line 1))
355           (setq beg (point))
356           (setq end (or (search-forward "\n\n" nil t) (point)))
357           ;; Delete the headers from the displayed articles.
358           (set-buffer gnus-article-copy)
359           (delete-region (goto-char (point-min))
360                          (or (search-forward "\n\n" nil t) (point)))
361           ;; Insert the original article headers.
362           (insert-buffer-substring gnus-original-article-buffer beg end)
363           (gnus-article-decode-rfc1522)))
364       gnus-article-copy)))
365
366 (defun gnus-post-news (post &optional group header article-buffer yank subject
367                             force-news)
368   (when article-buffer
369     (gnus-copy-article-buffer))
370   (let ((gnus-article-reply article-buffer))
371     (gnus-setup-message (cond (yank 'reply-yank)
372                               (article-buffer 'reply)
373                               (t 'message))
374       (let* ((group (or group gnus-newsgroup-name))
375              (pgroup group)
376              to-address to-group mailing-list to-list
377              newsgroup-p)
378         (when group
379           (setq to-address (gnus-group-find-parameter group 'to-address)
380                 to-group (gnus-group-find-parameter group 'to-group)
381                 to-list (gnus-group-find-parameter group 'to-list)
382                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
383                 mailing-list (when gnus-mailing-list-groups
384                                (string-match gnus-mailing-list-groups group))
385                 group (gnus-group-real-name group)))
386         (if (or (and to-group
387                      (gnus-news-group-p to-group))
388                 newsgroup-p
389                 force-news
390                 (and (gnus-news-group-p
391                       (or pgroup gnus-newsgroup-name)
392                       (if header (mail-header-number header)
393                         gnus-current-article))
394                      (not mailing-list)
395                      (not to-list)
396                      (not to-address)))
397             ;; This is news.
398             (if post
399                 (message-news (or to-group group))
400               (set-buffer gnus-article-copy)
401               (message-followup (if (or newsgroup-p force-news) nil to-group)))
402           ;; The is mail.
403           (if post
404               (progn
405                 (message-mail (or to-address to-list))
406                 ;; Arrange for mail groups that have no `to-address' to
407                 ;; get that when the user sends off the mail.
408                 (when (and (not to-list)
409                            (not to-address))
410                   (push (list 'gnus-inews-add-to-address pgroup)
411                         message-send-actions)))
412             (set-buffer gnus-article-copy)
413             (message-wide-reply to-address)))
414         (when yank
415           (gnus-inews-yank-articles yank))))))
416
417 (defun gnus-post-method (arg group &optional silent)
418   "Return the posting method based on GROUP and ARG.
419 If SILENT, don't prompt the user."
420   (let ((group-method (gnus-find-method-for-group group)))
421     (cond
422      ;; If the group-method is nil (which shouldn't happen) we use
423      ;; the default method.
424      ((null group-method)
425       (or gnus-post-method gnus-select-method message-post-method))
426      ;; We want this group's method.
427      ((and arg (not (eq arg 0)))
428       group-method)
429      ;; We query the user for a post method.
430      ((or arg
431           (and gnus-post-method
432                (listp (car gnus-post-method))))
433       (let* ((methods
434               ;; Collect all methods we know about.
435               (append
436                (when gnus-post-method
437                  (if (listp (car gnus-post-method))
438                      gnus-post-method
439                    (list gnus-post-method)))
440                gnus-secondary-select-methods
441                (list gnus-select-method)
442                (list group-method)))
443              method-alist post-methods method)
444         ;; Weed out all mail methods.
445         (while methods
446           (setq method (gnus-server-get-method "" (pop methods)))
447           (when (or (gnus-method-option-p method 'post)
448                     (gnus-method-option-p method 'post-mail))
449             (push method post-methods)))
450         ;; Create a name-method alist.
451         (setq method-alist
452               (mapcar
453                (lambda (m)
454                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
455                post-methods))
456         ;; Query the user.
457         (cadr
458          (assoc
459           (setq gnus-last-posting-server
460                 (if (and silent
461                          gnus-last-posting-server)
462                     ;; Just use the last value.
463                     gnus-last-posting-server
464                   (completing-read
465                    "Posting method: " method-alist nil t
466                    (cons (or gnus-last-posting-server "") 0))))
467           method-alist))))
468      ;; Override normal method.
469      (gnus-post-method
470       gnus-post-method)
471      ;; Use the normal select method.
472      (t gnus-select-method))))
473
474 ;;;
475 ;;; Check whether the message has been sent already.
476 ;;;
477
478 (defvar gnus-inews-sent-ids nil)
479
480 (defun gnus-inews-reject-message ()
481   "Check whether this message has already been sent."
482   (when gnus-sent-message-ids-file
483     (let ((message-id (save-restriction (message-narrow-to-headers)
484                                         (mail-fetch-field "message-id")))
485           end)
486       (when message-id
487         (unless gnus-inews-sent-ids
488           (ignore-errors
489             (load t t t)))
490         (if (member message-id gnus-inews-sent-ids)
491             ;; Reject this message.
492             (not (gnus-yes-or-no-p
493                   (format "Message %s already sent.  Send anyway? "
494                           message-id)))
495           (push message-id gnus-inews-sent-ids)
496           ;; Chop off the last Message-IDs.
497           (when (setq end (nthcdr gnus-sent-message-ids-length
498                                   gnus-inews-sent-ids))
499             (setcdr end nil))
500           (nnheader-temp-write gnus-sent-message-ids-file
501             (gnus-prin1 `(setq gnus-inews-sent-ids ',gnus-inews-sent-ids)))
502           nil)))))
503
504 \f
505
506 ;; Dummy to avoid byte-compile warning.
507 (defvar nnspool-rejected-article-hook)
508
509 ;;; Since the X-Newsreader/X-Mailer are ``vanity'' headers, they might
510 ;;; as well include the Emacs version as well.
511 ;;; The following function works with later GNU Emacs, and XEmacs.
512 (defun gnus-extended-version ()
513   "Stringified Gnus version and Emacs version"
514   (interactive)
515   (concat
516    gnus-version
517    "/"
518    (cond
519     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
520      (concat "Emacs " (substring emacs-version
521                                  (match-beginning 1)
522                                  (match-end 1))))
523     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
524                    emacs-version)
525      (concat (substring emacs-version
526                         (match-beginning 1)
527                         (match-end 1))
528              (format " %d.%d" emacs-major-version emacs-minor-version)
529              (if (match-beginning 3)
530                  (substring emacs-version
531                             (match-beginning 3)
532                             (match-end 3))
533                "")))
534     (t emacs-version))))
535
536 ;; Written by "Mr. Per Persson" <pp@gnu.ai.mit.edu>.
537 (defun gnus-inews-insert-mime-headers ()
538   (goto-char (point-min))
539   (let ((mail-header-separator
540          (progn
541            (goto-char (point-min))
542            (if (and (search-forward (concat "\n" mail-header-separator "\n")
543                                     nil t)
544                     (not (search-backward "\n\n" nil t)))
545                mail-header-separator
546              ""))))
547     (or (mail-position-on-field "Mime-Version")
548         (insert "1.0")
549         (cond ((save-restriction
550                  (widen)
551                  (goto-char (point-min))
552                  (re-search-forward "[\200-\377]" nil t))
553                (or (mail-position-on-field "Content-Type")
554                    (insert "text/plain; charset=ISO-8859-1"))
555                (or (mail-position-on-field "Content-Transfer-Encoding")
556                    (insert "8bit")))
557               (t (or (mail-position-on-field "Content-Type")
558                      (insert "text/plain; charset=US-ASCII"))
559                  (or (mail-position-on-field "Content-Transfer-Encoding")
560                      (insert "7bit")))))))
561
562 \f
563 ;;;
564 ;;; Gnus Mail Functions
565 ;;;
566
567 ;;; Mail reply commands of Gnus summary mode
568
569 (defun gnus-summary-reply (&optional yank wide)
570   "Start composing a reply mail to the current message.
571 If prefix argument YANK is non-nil, the original article is yanked
572 automatically."
573   (interactive
574    (list (and current-prefix-arg
575               (gnus-summary-work-articles 1))))
576   ;; Stripping headers should be specified with mail-yank-ignored-headers.
577   (gnus-set-global-variables)
578   (when yank
579     (gnus-summary-goto-subject (car yank)))
580   (let ((gnus-article-reply t))
581     (gnus-setup-message (if yank 'reply-yank 'reply)
582       (gnus-summary-select-article)
583       (set-buffer (gnus-copy-article-buffer))
584       (message-reply nil wide (gnus-group-find-parameter
585                                gnus-newsgroup-name 'broken-reply-to))
586       (when yank
587         (gnus-inews-yank-articles yank)))))
588
589 (defun gnus-summary-reply-with-original (n &optional wide)
590   "Start composing a reply mail to the current message.
591 The original article will be yanked."
592   (interactive "P")
593   (gnus-summary-reply (gnus-summary-work-articles n) wide))
594
595 (defun gnus-summary-wide-reply (&optional yank)
596   "Start composing a wide reply mail to the current message.
597 If prefix argument YANK is non-nil, the original article is yanked
598 automatically."
599   (interactive
600    (list (and current-prefix-arg
601               (gnus-summary-work-articles 1))))
602   (gnus-summary-reply yank t))
603
604 (defun gnus-summary-wide-reply-with-original (n)
605   "Start composing a wide reply mail to the current message.
606 The original article will be yanked."
607   (interactive "P")
608   (gnus-summary-reply-with-original n t))
609
610 (defun gnus-summary-mail-forward (&optional full-headers post)
611   "Forward the current message to another user.
612 If FULL-HEADERS (the prefix), include full headers when forwarding."
613   (interactive "P")
614   (gnus-set-global-variables)
615   (gnus-setup-message 'forward
616     (gnus-summary-select-article)
617     (set-buffer gnus-original-article-buffer)
618     (let ((message-included-forward-headers
619            (if full-headers "" message-included-forward-headers)))
620       (message-forward post))))
621
622 (defun gnus-summary-resend-message (address n)
623   "Resend the current article to ADDRESS."
624   (interactive "sResend message(s) to: \nP")
625   (let ((articles (gnus-summary-work-articles n))
626         article)
627     (while (setq article (pop articles))
628       (gnus-summary-select-article nil nil nil article)
629       (save-excursion
630         (set-buffer gnus-original-article-buffer)
631         (message-resend address)))))
632
633 (defun gnus-summary-post-forward (&optional full-headers)
634   "Forward the current article to a newsgroup.
635 If FULL-HEADERS (the prefix), include full headers when forwarding."
636   (interactive "P")
637   (gnus-summary-mail-forward full-headers t))
638
639 (defvar gnus-nastygram-message
640   "The following article was inappropriately posted to %s.\n\n"
641   "Format string to insert in nastygrams.
642 The current group name will be inserted at \"%s\".")
643
644 (defun gnus-summary-mail-nastygram (n)
645   "Send a nastygram to the author of the current article."
646   (interactive "P")
647   (when (or gnus-expert-user
648             (gnus-y-or-n-p
649              "Really send a nastygram to the author of the current article? "))
650     (let ((group gnus-newsgroup-name))
651       (gnus-summary-reply-with-original n)
652       (set-buffer gnus-message-buffer)
653       (message-goto-body)
654       (insert (format gnus-nastygram-message group))
655       (message-send-and-exit))))
656
657 (defun gnus-summary-mail-crosspost-complaint (n)
658   "Send a complaint about crossposting to the current article(s)."
659   (interactive "P")
660   (let ((articles (gnus-summary-work-articles n))
661         article)
662     (while (setq article (pop articles))
663       (set-buffer gnus-summary-buffer)
664       (gnus-summary-goto-subject article)
665       (let ((group (gnus-group-real-name gnus-newsgroup-name))
666             newsgroups followup-to)
667         (gnus-summary-select-article)
668         (set-buffer gnus-original-article-buffer)
669         (if (and (<= (length (message-tokenize-header
670                               (setq newsgroups (mail-fetch-field "newsgroups"))
671                               ", "))
672                      1)
673                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
674                      (not (member group (message-tokenize-header
675                                          followup-to ", ")))))
676             (if followup-to
677                 (gnus-message 1 "Followup-to restricted")
678               (gnus-message 1 "Not a crossposted article"))
679           (set-buffer gnus-summary-buffer)
680           (gnus-summary-reply-with-original 1)
681           (set-buffer gnus-message-buffer)
682           (message-goto-body)
683           (insert (format gnus-crosspost-complaint newsgroups group))
684           (message-goto-subject)
685           (re-search-forward " *$")
686           (replace-match " (crosspost notification)" t t)
687           (when (gnus-y-or-n-p "Send this complaint? ")
688             (message-send-and-exit)))))))
689
690 (defun gnus-summary-mail-other-window ()
691   "Compose mail in other window."
692   (interactive)
693   (gnus-setup-message 'message
694     (message-mail)))
695
696 (defun gnus-mail-parse-comma-list ()
697   (let (accumulated
698         beg)
699     (skip-chars-forward " ")
700     (while (not (eobp))
701       (setq beg (point))
702       (skip-chars-forward "^,")
703       (while (zerop
704               (save-excursion
705                 (save-restriction
706                   (let ((i 0))
707                     (narrow-to-region beg (point))
708                     (goto-char beg)
709                     (logand (progn
710                               (while (search-forward "\"" nil t)
711                                 (incf i))
712                               (if (zerop i) 2 i))
713                             2)))))
714         (skip-chars-forward ",")
715         (skip-chars-forward "^,"))
716       (skip-chars-backward " ")
717       (push (buffer-substring beg (point))
718             accumulated)
719       (skip-chars-forward "^,")
720       (skip-chars-forward ", "))
721     accumulated))
722
723 (defun gnus-inews-add-to-address (group)
724   (let ((to-address (mail-fetch-field "to")))
725     (when (and to-address
726                (gnus-alive-p))
727       ;; This mail group doesn't have a `to-list', so we add one
728       ;; here.  Magic!
729       (when (gnus-y-or-n-p
730              (format "Do you want to add this as `to-list': %s " to-address))
731         (gnus-group-add-parameter group (cons 'to-list to-address))))))
732
733 (defun gnus-put-message ()
734   "Put the current message in some group and return to Gnus."
735   (interactive)
736   (let ((reply gnus-article-reply)
737         (winconf gnus-prev-winconf)
738         (group gnus-newsgroup-name))
739
740     (or (and group (not (gnus-group-read-only-p group)))
741         (setq group (read-string "Put in group: " nil
742                                  (gnus-writable-groups))))
743     (when (gnus-gethash group gnus-newsrc-hashtb)
744       (error "No such group: %s" group))
745
746     (save-excursion
747       (save-restriction
748         (widen)
749         (message-narrow-to-headers)
750         (let (gnus-deletable-headers)
751           (if (message-news-p)
752               (message-generate-headers message-required-news-headers)
753             (message-generate-headers message-required-mail-headers)))
754         (goto-char (point-max))
755         (insert "Gcc: " group "\n")
756         (widen)))
757
758     (gnus-inews-do-gcc)
759
760     (when (get-buffer gnus-group-buffer)
761       (when (gnus-buffer-exists-p (car-safe reply))
762         (set-buffer (car reply))
763         (and (cdr reply)
764              (gnus-summary-mark-article-as-replied
765               (cdr reply))))
766       (when winconf
767         (set-window-configuration winconf)))))
768
769 (defun gnus-article-mail (yank)
770   "Send a reply to the address near point.
771 If YANK is non-nil, include the original article."
772   (interactive "P")
773   (let ((address
774          (buffer-substring
775           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
776           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
777     (when address
778       (message-reply address)
779       (when yank
780         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
781
782 (defvar nntp-server-type)
783 (defun gnus-bug ()
784   "Send a bug report to the Gnus maintainers."
785   (interactive)
786   (unless (gnus-alive-p)
787     (error "Gnus has been shut down"))
788   (gnus-setup-message 'bug
789     (delete-other-windows)
790     (switch-to-buffer "*Gnus Help Bug*")
791     (erase-buffer)
792     (insert gnus-bug-message)
793     (goto-char (point-min))
794     (message-pop-to-buffer "*Gnus Bug*")
795     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
796     (push `(gnus-bug-kill-buffer) message-send-actions)
797     (goto-char (point-min))
798     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
799     (forward-line 1)
800     (insert (gnus-version) "\n")
801     (insert (emacs-version) "\n")
802     (when (and (boundp 'nntp-server-type)
803                (stringp nntp-server-type))
804       (insert nntp-server-type))
805     (insert "\n\n\n\n\n")
806     (gnus-debug)
807     (goto-char (point-min))
808     (search-forward "Subject: " nil t)
809     (message "")))
810
811 (defun gnus-bug-kill-buffer ()
812   (when (get-buffer "*Gnus Help Bug*")
813     (kill-buffer "*Gnus Help Bug*")))
814
815 (defun gnus-debug ()
816   "Attempts to go through the Gnus source file and report what variables have been changed.
817 The source file has to be in the Emacs load path."
818   (interactive)
819   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
820                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
821                  "gnus-msg.el" "gnus-score.el" "gnus-win.el"
822                  "nnmail.el" "message.el"))
823         file expr olist sym)
824     (gnus-message 4 "Please wait while we snoop your variables...")
825     (sit-for 0)
826     ;; Go through all the files looking for non-default values for variables.
827     (save-excursion
828       (set-buffer (get-buffer-create " *gnus bug info*"))
829       (buffer-disable-undo (current-buffer))
830       (while files
831         (erase-buffer)
832         (when (and (setq file (locate-library (pop files)))
833                    (file-exists-p file))
834           (insert-file-contents file)
835           (goto-char (point-min))
836           (if (not (re-search-forward "^;;* *Internal variables" nil t))
837               (gnus-message 4 "Malformed sources in file %s" file)
838             (narrow-to-region (point-min) (point))
839             (goto-char (point-min))
840             (while (setq expr (ignore-errors (read (current-buffer))))
841               (ignore-errors
842                 (and (or (eq (car expr) 'defvar)
843                          (eq (car expr) 'defcustom))
844                      (stringp (nth 3 expr))
845                      (or (not (boundp (nth 1 expr)))
846                          (not (equal (eval (nth 2 expr))
847                                      (symbol-value (nth 1 expr)))))
848                      (push (nth 1 expr) olist)))))))
849       (kill-buffer (current-buffer)))
850     (when (setq olist (nreverse olist))
851       (insert "------------------ Environment follows ------------------\n\n"))
852     (while olist
853       (if (boundp (car olist))
854           (condition-case ()
855               (pp `(setq ,(car olist)
856                          ,(if (or (consp (setq sym (symbol-value (car olist))))
857                                   (and (symbolp sym)
858                                        (not (or (eq sym nil)
859                                                 (eq sym t)))))
860                               (list 'quote (symbol-value (car olist)))
861                             (symbol-value (car olist))))
862                   (current-buffer))
863             (error
864              (format "(setq %s 'whatever)\n" (car olist))))
865         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
866       (setq olist (cdr olist)))
867     (insert "\n\n")
868     ;; Remove any null chars - they seem to cause trouble for some
869     ;; mailers.  (Byte-compiled output from the stuff above.)
870     (goto-char (point-min))
871     (while (re-search-forward "[\000\200]" nil t)
872       (replace-match "" t t))))
873
874 ;;; Treatment of rejected articles.
875 ;;; Bounced mail.
876
877 (defun gnus-summary-resend-bounced-mail (&optional fetch)
878   "Re-mail the current message.
879 This only makes sense if the current message is a bounce message than
880 contains some mail you have written which has been bounced back to
881 you.
882 If FETCH, try to fetch the article that this is a reply to, if indeed
883 this is a reply."
884   (interactive "P")
885   (gnus-summary-select-article t)
886   (set-buffer gnus-original-article-buffer)
887   (gnus-setup-message 'compose-bounce
888     (let* ((references (mail-fetch-field "references"))
889            (parent (and references (gnus-parent-id references))))
890       (message-bounce)
891       ;; If there are references, we fetch the article we answered to.
892       (and fetch parent
893            (gnus-summary-refer-article parent)
894            (gnus-summary-show-all-headers)))))
895
896 ;;; Gcc handling.
897
898 ;; Do Gcc handling, which copied the message over to some group.
899 (defun gnus-inews-do-gcc (&optional gcc)
900   (interactive)
901   (when (gnus-alive-p)
902     (save-excursion
903       (save-restriction
904         (message-narrow-to-headers)
905         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
906               (cur (current-buffer))
907               groups group method)
908           (when gcc
909             (message-remove-header "gcc")
910             (widen)
911             (setq groups (message-tokenize-header gcc " ,"))
912             ;; Copy the article over to some group(s).
913             (while (setq group (pop groups))
914               (gnus-check-server
915                (setq method
916                      (cond ((and (null (gnus-get-info group))
917                                  (eq (car gnus-message-archive-method)
918                                      (car
919                                       (gnus-server-to-method
920                                        (gnus-group-method group)))))
921                             ;; If the group doesn't exist, we assume
922                             ;; it's an archive group...
923                             gnus-message-archive-method)
924                            ;; Use the method.
925                            ((gnus-info-method (gnus-get-info group))
926                             (gnus-info-method (gnus-get-info group)))
927                            ;; Find the method.
928                            (t (gnus-group-method group)))))
929               (gnus-check-server method)
930               (unless (gnus-request-group group t method)
931                 (gnus-request-create-group group method))
932               (save-excursion
933                 (nnheader-set-temp-buffer " *acc*")
934                 (insert-buffer-substring cur)
935                 (goto-char (point-min))
936                 (when (re-search-forward
937                        (concat "^" (regexp-quote mail-header-separator) "$")
938                        nil t)
939                   (replace-match "" t t ))
940                 (unless (gnus-request-accept-article group method t)
941                   (gnus-message 1 "Couldn't store article in group %s: %s"
942                                 group (gnus-status-message method))
943                   (sit-for 2))
944                 (kill-buffer (current-buffer))))))))))
945
946 (defun gnus-inews-insert-gcc ()
947   "Insert Gcc headers based on `gnus-outgoing-message-group'."
948   (save-excursion
949     (save-restriction
950       (message-narrow-to-headers)
951       (let* ((group gnus-outgoing-message-group)
952              (gcc (cond
953                    ((gnus-functionp group)
954                     (funcall group))
955                    ((or (stringp group) (list group))
956                     group))))
957         (when gcc
958           (insert "Gcc: "
959                   (if (stringp gcc) gcc
960                     (mapconcat 'identity gcc " "))
961                   "\n"))))))
962
963 (defun gnus-inews-insert-archive-gcc (&optional group)
964   "Insert the Gcc to say where the article is to be archived."
965   (let* ((var gnus-message-archive-group)
966          (group (or group gnus-newsgroup-name ""))
967          result
968          gcc-self-val
969          (groups
970           (cond
971            ((null gnus-message-archive-method)
972             ;; Ignore.
973             nil)
974            ((stringp var)
975             ;; Just a single group.
976             (list var))
977            ((null var)
978             ;; We don't want this.
979             nil)
980            ((and (listp var) (stringp (car var)))
981             ;; A list of groups.
982             var)
983            ((gnus-functionp var)
984             ;; A function.
985             (funcall var group))
986            (t
987             ;; An alist of regexps/functions/forms.
988             (while (and var
989                         (not
990                          (setq result
991                                (cond
992                                 ((stringp (caar var))
993                                  ;; Regexp.
994                                  (when (string-match (caar var) group)
995                                    (cdar var)))
996                                 ((gnus-functionp (car var))
997                                  ;; Function.
998                                  (funcall (car var) group))
999                                 (t
1000                                  (eval (car var)))))))
1001               (setq var (cdr var)))
1002             result)))
1003          name)
1004     (when groups
1005       (when (stringp groups)
1006         (setq groups (list groups)))
1007       (save-excursion
1008         (save-restriction
1009           (message-narrow-to-headers)
1010           (goto-char (point-max))
1011           (insert "Gcc: ")
1012           (if (and gnus-newsgroup-name
1013                    (setq gcc-self-val
1014                          (gnus-group-find-parameter
1015                           gnus-newsgroup-name 'gcc-self)))
1016               (progn
1017                 (insert
1018                  (if (stringp gcc-self-val)
1019                      gcc-self-val
1020                    group))
1021                 (if (not (eq gcc-self-val 'none))
1022                     (insert "\n")
1023                   (progn
1024                     (beginning-of-line)
1025                     (kill-line))))
1026             (while (setq name (pop groups))
1027               (insert (if (string-match ":" name)
1028                           name
1029                         (gnus-group-prefixed-name
1030                          name gnus-message-archive-method)))
1031               (when groups
1032                 (insert " ")))
1033             (insert "\n")))))))
1034
1035 (defun gnus-summary-send-draft ()
1036   "Enter a mail/post buffer to edit and send the draft."
1037   (interactive)
1038   (gnus-set-global-variables)
1039   (let (buf)
1040     (if (not (setq buf (gnus-request-restore-buffer
1041                         (gnus-summary-article-number) gnus-newsgroup-name)))
1042         (error "Couldn't restore the article")
1043       (switch-to-buffer buf)
1044       (when (eq major-mode 'news-reply-mode)
1045         (local-set-key "\C-c\C-c" 'gnus-inews-news))
1046       ;; Insert the separator.
1047       (goto-char (point-min))
1048       (search-forward "\n\n")
1049       (forward-char -1)
1050       (insert mail-header-separator)
1051       ;; Configure windows.
1052       (let ((gnus-draft-buffer (current-buffer)))
1053         (gnus-configure-windows 'draft t)
1054         (goto-char (point))))))
1055
1056 (gnus-add-shutdown 'gnus-inews-close 'gnus)
1057
1058 (defun gnus-inews-close ()
1059   (setq gnus-inews-sent-ids nil))
1060
1061 ;;; Allow redefinition of functions.
1062
1063 (gnus-ems-redefine)
1064
1065 (provide 'gnus-msg)
1066
1067 ;;; gnus-msg.el ends here