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