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