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