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