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