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