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