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