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