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