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