Quassia Gnus v0.15.
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995,96,97 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 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-ems)
33 (require 'message)
34 (require 'gnus-art)
35
36 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
37 (defvar gnus-post-method nil
38   "*Preferred method for posting USENET news.
39 If this variable is nil, Gnus will use the current method to decide
40 which method to use when posting.  If it is non-nil, it will override
41 the current method.  This method will not be used in mail groups and
42 the like, only in \"real\" newsgroups.
43
44 The value must be a valid method as discussed in the documentation of
45 `gnus-select-method'.  It can also be a list of methods.  If that is
46 the case, the user will be queried for what select method to use when
47 posting.")
48
49 (defvar gnus-outgoing-message-group nil
50   "*All outgoing messages will be put in this group.
51 If you want to store all your outgoing mail and articles in the group
52 \"nnml:archive\", you set this variable to that value.  This variable
53 can also be a list of group names.
54
55 If you want to have greater control over what group to put each
56 message in, you can set this variable to a function that checks the
57 current newsgroup name and then returns a suitable group name (or list
58 of names).")
59
60 (defvar gnus-mailing-list-groups nil
61   "*Regexp matching groups that are really mailing lists.
62 This is useful when you're reading a mailing list that has been
63 gatewayed to a newsgroup, and you want to followup to an article in
64 the group.")
65
66 (defvar gnus-add-to-list nil
67   "*If non-nil, add a `to-list' parameter automatically.")
68
69 (defvar gnus-sent-message-ids-file
70   (nnheader-concat gnus-directory "Sent-Message-IDs")
71   "File where Gnus saves a cache of sent message ids.")
72
73 (defvar gnus-sent-message-ids-length 1000
74   "The number of sent Message-IDs to save.")
75
76 (defvar gnus-crosspost-complaint
77   "Hi,
78
79 You posted the article below with the following Newsgroups header:
80
81 Newsgroups: %s
82
83 The %s group, at least, was an inappropriate recipient
84 of this message.  Please trim your Newsgroups header to exclude this
85 group before posting in the future.
86
87 Thank you.
88
89 "
90   "Format string to be inserted when complaining about crossposts.
91 The first %s will be replaced by the Newsgroups header;
92 the second with the current group name.")
93
94 (defvar gnus-message-setup-hook nil
95   "Hook run after setting up a message buffer.")
96
97 ;;; Internal variables.
98
99 (defvar gnus-message-buffer "*Mail Gnus*")
100 (defvar gnus-article-copy nil)
101 (defvar gnus-last-posting-server nil)
102 (defvar gnus-message-group-art nil)
103
104 (defconst gnus-bug-message
105   "Sending a bug report to the Gnus Towers.
106 ========================================
107
108 The buffer below is a mail buffer.  When you press `C-c C-c', it will
109 be sent to the Gnus Bug Exterminators.
110
111 At the bottom of the buffer you'll see lots of variable settings.
112 Please do not delete those.  They will tell the Bug People what your
113 environment is, so that it will be easier to locate the bugs.
114
115 If you have found a bug that makes Emacs go \"beep\", set
116 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
117 and include the backtrace in your bug report.
118
119 Please describe the bug in annoying, painstaking detail.
120
121 Thank you for your help in stamping out bugs.
122 ")
123
124 (eval-and-compile
125   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
126   (autoload 'news-setup "rnewspost")
127   (autoload 'news-reply-mode "rnewspost")
128   (autoload 'rmail-dont-reply-to "mail-utils")
129   (autoload 'rmail-output "rmailout"))
130
131 \f
132 ;;;
133 ;;; Gnus Posting Functions
134 ;;;
135
136 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
137   "p" gnus-summary-post-news
138   "f" gnus-summary-followup
139   "F" gnus-summary-followup-with-original
140   "c" gnus-summary-cancel-article
141   "s" gnus-summary-supersede-article
142   "r" gnus-summary-reply
143   "R" gnus-summary-reply-with-original
144   "w" gnus-summary-wide-reply
145   "W" gnus-summary-wide-reply-with-original
146   "n" gnus-summary-followup-to-mail
147   "N" gnus-summary-followup-to-mail-with-original
148   "m" gnus-summary-mail-other-window
149   "u" gnus-uu-post-news
150   "\M-c" gnus-summary-mail-crosspost-complaint
151   "om" gnus-summary-mail-forward
152   "op" gnus-summary-post-forward
153   "Om" gnus-uu-digest-mail-forward
154   "Op" gnus-uu-digest-post-forward)
155
156 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
157   "b" gnus-summary-resend-bounced-mail
158   ;; "c" gnus-summary-send-draft
159   "r" gnus-summary-resend-message)
160
161 ;;; Internal functions.
162
163 (defvar gnus-article-reply nil)
164 (defmacro gnus-setup-message (config &rest forms)
165   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
166         (buffer (make-symbol "gnus-setup-message-buffer"))
167         (article (make-symbol "gnus-setup-message-article")))
168     `(let ((,winconf (current-window-configuration))
169            (,buffer (buffer-name (current-buffer)))
170            (,article (and gnus-article-reply (gnus-summary-article-number)))
171            (message-header-setup-hook
172             (copy-sequence message-header-setup-hook)))
173        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
174        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
175        (unwind-protect
176            (progn
177              ,@forms)
178          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
179          (setq gnus-message-buffer (current-buffer))
180          (set (make-local-variable 'gnus-message-group-art)
181               (cons ,gnus-newsgroup-name ,article))
182          (make-local-variable 'gnus-newsgroup-name)
183          (run-hooks 'gnus-message-setup-hook))
184        (gnus-configure-windows ,config t)
185        (set-buffer-modified-p nil))))
186
187 (defun gnus-inews-add-send-actions (winconf buffer article)
188   (make-local-hook 'message-sent-hook)
189   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
190   (setq message-post-method
191         `(lambda (arg)
192            (gnus-post-method arg ,gnus-newsgroup-name)))
193   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
194   (message-add-action
195    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
196   (message-add-action
197    `(when (buffer-name (get-buffer ,buffer))
198       (save-excursion
199         (set-buffer (get-buffer ,buffer))
200         ,(when article
201            `(gnus-summary-mark-article-as-replied ,article))))
202    'send))
203
204 (put 'gnus-setup-message 'lisp-indent-function 1)
205 (put 'gnus-setup-message 'edebug-form-spec '(form body))
206
207 ;;; Post news commands of Gnus group mode and summary mode
208
209 (defun gnus-group-mail ()
210   "Start composing a mail."
211   (interactive)
212   (gnus-setup-message 'message
213     (message-mail)))
214
215 (defun gnus-group-post-news (&optional arg)
216   "Start composing a news message.
217 If ARG, post to the group under point.
218 If ARG is 1, prompt for a group name."
219   (interactive "P")
220   ;; Bind this variable here to make message mode hooks
221   ;; work ok.
222   (let ((gnus-newsgroup-name
223          (if arg
224              (if (= 1 (prefix-numeric-value arg))
225                  (completing-read "Newsgroup: " gnus-active-hashtb nil
226                                   (gnus-read-active-file-p))
227                (gnus-group-group-name))
228            "")))
229     (gnus-post-news 'post gnus-newsgroup-name)))
230
231 (defun gnus-summary-post-news ()
232   "Start composing a news message."
233   (interactive)
234   (gnus-post-news 'post gnus-newsgroup-name))
235
236 (defun gnus-summary-followup (yank &optional force-news)
237   "Compose a followup to an article.
238 If prefix argument YANK is non-nil, original article is yanked automatically."
239   (interactive
240    (list (and current-prefix-arg
241               (gnus-summary-work-articles 1))))
242   (when yank
243     (gnus-summary-goto-subject (car yank)))
244   (save-window-excursion
245     (gnus-summary-select-article))
246   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
247         (gnus-newsgroup-name gnus-newsgroup-name))
248     ;; Send a followup.
249     (gnus-post-news nil gnus-newsgroup-name
250                     headers gnus-article-buffer
251                     yank nil force-news)))
252
253 (defun gnus-summary-followup-with-original (n &optional force-news)
254   "Compose a followup to an article and include the original article."
255   (interactive "P")
256   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
257
258 (defun gnus-summary-followup-to-mail (&optional arg)
259   "Followup to the current mail message via news."
260   (interactive
261    (list (and current-prefix-arg
262               (gnus-summary-work-articles 1))))
263   (gnus-summary-followup arg t))
264
265 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
266   "Followup to the current mail message via news."
267   (interactive "P")
268   (gnus-summary-followup (gnus-summary-work-articles arg) t))
269
270 (defun gnus-inews-yank-articles (articles)
271   (let (beg article)
272     (message-goto-body)
273     (while (setq article (pop articles))
274       (save-window-excursion
275         (set-buffer gnus-summary-buffer)
276         (gnus-summary-select-article nil nil nil article)
277         (gnus-summary-remove-process-mark article))
278       (gnus-copy-article-buffer)
279       (let ((message-reply-buffer gnus-article-copy)
280             (message-reply-headers gnus-current-headers))
281         (message-yank-original)
282         (setq beg (or beg (mark t))))
283       (when articles
284         (insert "\n")))
285     (push-mark)
286     (goto-char beg)))
287
288 (defun gnus-summary-cancel-article (&optional n symp)
289   "Cancel an article you posted.
290 Uses the process-prefix convention.  If given the symbolic
291 prefix `a', cancel using the standard posting method; if not
292 post using the current select method."
293   (interactive (gnus-interactive "P\ny"))
294   (let ((articles (gnus-summary-work-articles n))
295         (message-post-method
296          `(lambda (arg)
297             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
298         article)
299     (while (setq article (pop articles))
300       (when (gnus-summary-select-article t nil nil article)
301         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
302                 (message-cancel-news))
303           (gnus-summary-mark-as-read article gnus-canceled-mark)
304           (gnus-cache-remove-article 1))
305         (gnus-article-hide-headers-if-wanted))
306       (gnus-summary-remove-process-mark article))))
307
308 (defun gnus-summary-supersede-article ()
309   "Compose an article that will supersede a previous article.
310 This is done simply by taking the old article and adding a Supersedes
311 header line with the old Message-ID."
312   (interactive)
313   (let ((article (gnus-summary-article-number)))
314     (gnus-setup-message 'reply-yank
315       (gnus-summary-select-article t)
316       (set-buffer gnus-original-article-buffer)
317       (message-supersede)
318       (push
319        `((lambda ()
320            (when (buffer-name (get-buffer ,gnus-summary-buffer))
321              (save-excursion
322                (set-buffer (get-buffer ,gnus-summary-buffer))
323                (gnus-cache-possibly-remove-article ,article nil nil nil t)
324                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
325        message-send-actions))))
326
327 \f
328
329 (defun gnus-copy-article-buffer (&optional article-buffer)
330   ;; make a copy of the article buffer with all text properties removed
331   ;; this copy is in the buffer gnus-article-copy.
332   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
333   ;; this buffer should be passed to all mail/news reply/post routines.
334   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
335   (buffer-disable-undo gnus-article-copy)
336   (or (memq gnus-article-copy gnus-buffer-list)
337       (push gnus-article-copy gnus-buffer-list))
338   (let ((article-buffer (or article-buffer gnus-article-buffer))
339         end beg contents)
340     (if (not (and (get-buffer article-buffer)
341                   (buffer-name (get-buffer article-buffer))))
342         (error "Can't find any article buffer")
343       (save-excursion
344         (set-buffer article-buffer)
345         (save-restriction
346           ;; Copy over the (displayed) article buffer, delete
347           ;; hidden text and remove text properties.
348           (widen)
349           (copy-to-buffer gnus-article-copy (point-min) (point-max))
350           (set-buffer gnus-article-copy)
351           (gnus-article-delete-text-of-type 'annotation)
352           (gnus-remove-text-with-property 'gnus-prev)
353           (gnus-remove-text-with-property 'gnus-next)
354           (insert
355            (prog1
356                (format "%s" (buffer-string))
357              (erase-buffer)))
358           ;; Find the original headers.
359           (set-buffer gnus-original-article-buffer)
360           (goto-char (point-min))
361           (while (looking-at message-unix-mail-delimiter)
362             (forward-line 1))
363           (setq beg (point))
364           (setq end (or (search-forward "\n\n" nil t) (point)))
365           ;; Delete the headers from the displayed articles.
366           (set-buffer gnus-article-copy)
367           (delete-region (goto-char (point-min))
368                          (or (search-forward "\n\n" nil t) (point)))
369           ;; Insert the original article headers.
370           (insert-buffer-substring gnus-original-article-buffer beg end)
371           (gnus-article-decode-rfc1522)))
372       gnus-article-copy)))
373
374 (defun gnus-post-news (post &optional group header article-buffer yank subject
375                             force-news)
376   (when article-buffer
377     (gnus-copy-article-buffer))
378   (let ((gnus-article-reply article-buffer)
379         (add-to-list gnus-add-to-list))
380     (gnus-setup-message (cond (yank 'reply-yank)
381                               (article-buffer 'reply)
382                               (t 'message))
383       (let* ((group (or group gnus-newsgroup-name))
384              (pgroup group)
385              to-address to-group mailing-list to-list
386              newsgroup-p)
387         (when group
388           (setq to-address (gnus-group-find-parameter group 'to-address)
389                 to-group (gnus-group-find-parameter group 'to-group)
390                 to-list (gnus-group-find-parameter group 'to-list)
391                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
392                 mailing-list (when gnus-mailing-list-groups
393                                (string-match gnus-mailing-list-groups group))
394                 group (gnus-group-real-name group)))
395         (if (or (and to-group
396                      (gnus-news-group-p to-group))
397                 newsgroup-p
398                 force-news
399                 (and (gnus-news-group-p
400                       (or pgroup gnus-newsgroup-name)
401                       (if header (mail-header-number header)
402                         gnus-current-article))
403                      (not mailing-list)
404                      (not to-list)
405                      (not to-address)))
406             ;; This is news.
407             (if post
408                 (message-news (or to-group group))
409               (set-buffer gnus-article-copy)
410               (message-followup (if (or newsgroup-p force-news) nil to-group)))
411           ;; The is mail.
412           (if post
413               (progn
414                 (message-mail (or to-address to-list))
415                 ;; Arrange for mail groups that have no `to-address' to
416                 ;; get that when the user sends off the mail.
417                 (when (and (not to-list)
418                            (not to-address)
419                            add-to-list)
420                   (push (list 'gnus-inews-add-to-address pgroup)
421                         message-send-actions)))
422             (set-buffer gnus-article-copy)
423             (message-wide-reply to-address
424                                 (gnus-group-find-parameter
425                                  gnus-newsgroup-name 'broken-reply-to))))
426         (when yank
427           (gnus-inews-yank-articles yank))))))
428
429 (defun gnus-post-method (arg group &optional silent)
430   "Return the posting method based on GROUP and ARG.
431 If SILENT, don't prompt the user."
432   (let ((group-method (gnus-find-method-for-group group)))
433     (cond
434      ;; If the group-method is nil (which shouldn't happen) we use
435      ;; the default method.
436      ((null group-method)
437       (or gnus-post-method gnus-select-method message-post-method))
438      ;; We want this group's method.
439      ((and arg (not (eq arg 0)))
440       group-method)
441      ;; We query the user for a post method.
442      ((or arg
443           (and gnus-post-method
444                (listp (car gnus-post-method))))
445       (let* ((methods
446               ;; Collect all methods we know about.
447               (append
448                (when gnus-post-method
449                  (if (listp (car gnus-post-method))
450                      gnus-post-method
451                    (list gnus-post-method)))
452                gnus-secondary-select-methods
453                (list gnus-select-method)
454                (list group-method)))
455              method-alist post-methods method)
456         ;; Weed out all mail methods.
457         (while methods
458           (setq method (gnus-server-get-method "" (pop methods)))
459           (when (or (gnus-method-option-p method 'post)
460                     (gnus-method-option-p method 'post-mail))
461             (push method post-methods)))
462         ;; Create a name-method alist.
463         (setq method-alist
464               (mapcar
465                (lambda (m)
466                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
467                post-methods))
468         ;; Query the user.
469         (cadr
470          (assoc
471           (setq gnus-last-posting-server
472                 (if (and silent
473                          gnus-last-posting-server)
474                     ;; Just use the last value.
475                     gnus-last-posting-server
476                   (completing-read
477                    "Posting method: " method-alist nil t
478                    (cons (or gnus-last-posting-server "") 0))))
479           method-alist))))
480      ;; Override normal method.
481      (gnus-post-method
482       gnus-post-method)
483      ;; Use the normal select method.
484      (t gnus-select-method))))
485
486 ;;;
487 ;;; Check whether the message has been sent already.
488 ;;;
489
490 (defvar gnus-inews-sent-ids nil)
491
492 (defun gnus-inews-reject-message ()
493   "Check whether this message has already been sent."
494   (when gnus-sent-message-ids-file
495     (let ((message-id (save-restriction (message-narrow-to-headers)
496                                         (mail-fetch-field "message-id")))
497           end)
498       (when message-id
499         (unless gnus-inews-sent-ids
500           (ignore-errors
501             (load t t t)))
502         (if (member message-id gnus-inews-sent-ids)
503             ;; Reject this message.
504             (not (gnus-yes-or-no-p
505                   (format "Message %s already sent.  Send anyway? "
506                           message-id)))
507           (push message-id gnus-inews-sent-ids)
508           ;; Chop off the last Message-IDs.
509           (when (setq end (nthcdr gnus-sent-message-ids-length
510                                   gnus-inews-sent-ids))
511             (setcdr end nil))
512           (nnheader-temp-write gnus-sent-message-ids-file
513             (gnus-prin1 `(setq gnus-inews-sent-ids ',gnus-inews-sent-ids)))
514           nil)))))
515
516 \f
517
518 ;; Dummy to avoid byte-compile warning.
519 (defvar nnspool-rejected-article-hook)
520 (defvar xemacs-codename)
521
522 ;;; Since the X-Newsreader/X-Mailer are ``vanity'' headers, they might
523 ;;; as well include the Emacs version as well.
524 ;;; The following function works with later GNU Emacs, and XEmacs.
525 (defun gnus-extended-version ()
526   "Stringified Gnus version and Emacs version"
527   (interactive)
528   (concat
529    gnus-version
530    "/"
531    (cond
532     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
533      (concat "Emacs " (substring emacs-version
534                                  (match-beginning 1)
535                                  (match-end 1))))
536     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
537                    emacs-version)
538      (concat (substring emacs-version
539                         (match-beginning 1)
540                         (match-end 1))
541              (format " %d.%d" emacs-major-version emacs-minor-version)
542              (if (match-beginning 3)
543                  (substring emacs-version
544                             (match-beginning 3)
545                             (match-end 3))
546                "")
547              (if (boundp 'xemacs-codename)
548                  (concat " - \"" xemacs-codename "\""))))
549     (t emacs-version))))
550
551 ;; Written by "Mr. Per Persson" <pp@gnu.ai.mit.edu>.
552 (defun gnus-inews-insert-mime-headers ()
553   (goto-char (point-min))
554   (let ((mail-header-separator
555          (progn
556            (goto-char (point-min))
557            (if (and (search-forward (concat "\n" mail-header-separator "\n")
558                                     nil t)
559                     (not (search-backward "\n\n" nil t)))
560                mail-header-separator
561              ""))))
562     (or (mail-position-on-field "Mime-Version")
563         (insert "1.0")
564         (cond ((save-restriction
565                  (widen)
566                  (goto-char (point-min))
567                  (re-search-forward "[\200-\377]" nil t))
568                (or (mail-position-on-field "Content-Type")
569                    (insert "text/plain; charset=ISO-8859-1"))
570                (or (mail-position-on-field "Content-Transfer-Encoding")
571                    (insert "8bit")))
572               (t (or (mail-position-on-field "Content-Type")
573                      (insert "text/plain; charset=US-ASCII"))
574                  (or (mail-position-on-field "Content-Transfer-Encoding")
575                      (insert "7bit")))))))
576
577 \f
578 ;;;
579 ;;; Gnus Mail Functions
580 ;;;
581
582 ;;; Mail reply commands of Gnus summary mode
583
584 (defun gnus-summary-reply (&optional yank wide)
585   "Start composing a reply mail to the current message.
586 If prefix argument YANK is non-nil, the original article is yanked
587 automatically."
588   (interactive
589    (list (and current-prefix-arg
590               (gnus-summary-work-articles 1))))
591   ;; Stripping headers should be specified with mail-yank-ignored-headers.
592   (when yank
593     (gnus-summary-goto-subject (car yank)))
594   (let ((gnus-article-reply t))
595     (gnus-setup-message (if yank 'reply-yank 'reply)
596       (gnus-summary-select-article)
597       (set-buffer (gnus-copy-article-buffer))
598       (message-reply nil wide (gnus-group-find-parameter
599                                gnus-newsgroup-name 'broken-reply-to))
600       (when yank
601         (gnus-inews-yank-articles yank)))))
602
603 (defun gnus-summary-reply-with-original (n &optional wide)
604   "Start composing a reply mail to the current message.
605 The original article will be yanked."
606   (interactive "P")
607   (gnus-summary-reply (gnus-summary-work-articles n) wide))
608
609 (defun gnus-summary-wide-reply (&optional yank)
610   "Start composing a wide reply mail to the current message.
611 If prefix argument YANK is non-nil, the original article is yanked
612 automatically."
613   (interactive
614    (list (and current-prefix-arg
615               (gnus-summary-work-articles 1))))
616   (gnus-summary-reply yank t))
617
618 (defun gnus-summary-wide-reply-with-original (n)
619   "Start composing a wide reply mail to the current message.
620 The original article will be yanked."
621   (interactive "P")
622   (gnus-summary-reply-with-original n t))
623
624 (defun gnus-summary-mail-forward (&optional full-headers post)
625   "Forward the current message to another user.
626 If FULL-HEADERS (the prefix), include full headers when forwarding."
627   (interactive "P")
628   (gnus-setup-message 'forward
629     (gnus-summary-select-article)
630     (set-buffer gnus-original-article-buffer)
631     (let ((message-included-forward-headers
632            (if full-headers "" message-included-forward-headers)))
633       (message-forward post))))
634
635 (defun gnus-summary-resend-message (address n)
636   "Resend the current article to ADDRESS."
637   (interactive "sResend message(s) to: \nP")
638   (let ((articles (gnus-summary-work-articles n))
639         article)
640     (while (setq article (pop articles))
641       (gnus-summary-select-article nil nil nil article)
642       (save-excursion
643         (set-buffer gnus-original-article-buffer)
644         (message-resend address)))))
645
646 (defun gnus-summary-post-forward (&optional full-headers)
647   "Forward the current article to a newsgroup.
648 If FULL-HEADERS (the prefix), include full headers when forwarding."
649   (interactive "P")
650   (gnus-summary-mail-forward full-headers t))
651
652 (defvar gnus-nastygram-message
653   "The following article was inappropriately posted to %s.\n\n"
654   "Format string to insert in nastygrams.
655 The current group name will be inserted at \"%s\".")
656
657 (defun gnus-summary-mail-nastygram (n)
658   "Send a nastygram to the author of the current article."
659   (interactive "P")
660   (when (or gnus-expert-user
661             (gnus-y-or-n-p
662              "Really send a nastygram to the author of the current article? "))
663     (let ((group gnus-newsgroup-name))
664       (gnus-summary-reply-with-original n)
665       (set-buffer gnus-message-buffer)
666       (message-goto-body)
667       (insert (format gnus-nastygram-message group))
668       (message-send-and-exit))))
669
670 (defun gnus-summary-mail-crosspost-complaint (n)
671   "Send a complaint about crossposting to the current article(s)."
672   (interactive "P")
673   (let ((articles (gnus-summary-work-articles n))
674         article)
675     (while (setq article (pop articles))
676       (set-buffer gnus-summary-buffer)
677       (gnus-summary-goto-subject article)
678       (let ((group (gnus-group-real-name gnus-newsgroup-name))
679             newsgroups followup-to)
680         (gnus-summary-select-article)
681         (set-buffer gnus-original-article-buffer)
682         (if (and (<= (length (message-tokenize-header
683                               (setq newsgroups (mail-fetch-field "newsgroups"))
684                               ", "))
685                      1)
686                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
687                      (not (member group (message-tokenize-header
688                                          followup-to ", ")))))
689             (if followup-to
690                 (gnus-message 1 "Followup-to restricted")
691               (gnus-message 1 "Not a crossposted article"))
692           (set-buffer gnus-summary-buffer)
693           (gnus-summary-reply-with-original 1)
694           (set-buffer gnus-message-buffer)
695           (message-goto-body)
696           (insert (format gnus-crosspost-complaint newsgroups group))
697           (message-goto-subject)
698           (re-search-forward " *$")
699           (replace-match " (crosspost notification)" t t)
700           (gnus-deactivate-mark)
701           (when (gnus-y-or-n-p "Send this complaint? ")
702             (message-send-and-exit)))))))
703
704 (defun gnus-summary-mail-other-window ()
705   "Compose mail in other window."
706   (interactive)
707   (gnus-setup-message 'message
708     (message-mail)))
709
710 (defun gnus-mail-parse-comma-list ()
711   (let (accumulated
712         beg)
713     (skip-chars-forward " ")
714     (while (not (eobp))
715       (setq beg (point))
716       (skip-chars-forward "^,")
717       (while (zerop
718               (save-excursion
719                 (save-restriction
720                   (let ((i 0))
721                     (narrow-to-region beg (point))
722                     (goto-char beg)
723                     (logand (progn
724                               (while (search-forward "\"" nil t)
725                                 (incf i))
726                               (if (zerop i) 2 i))
727                             2)))))
728         (skip-chars-forward ",")
729         (skip-chars-forward "^,"))
730       (skip-chars-backward " ")
731       (push (buffer-substring beg (point))
732             accumulated)
733       (skip-chars-forward "^,")
734       (skip-chars-forward ", "))
735     accumulated))
736
737 (defun gnus-inews-add-to-address (group)
738   (let ((to-address (mail-fetch-field "to")))
739     (when (and to-address
740                (gnus-alive-p))
741       ;; This mail group doesn't have a `to-list', so we add one
742       ;; here.  Magic!
743       (when (gnus-y-or-n-p
744              (format "Do you want to add this as `to-list': %s " to-address))
745         (gnus-group-add-parameter group (cons 'to-list to-address))))))
746
747 (defun gnus-put-message ()
748   "Put the current message in some group and return to Gnus."
749   (interactive)
750   (let ((reply gnus-article-reply)
751         (winconf gnus-prev-winconf)
752         (group gnus-newsgroup-name))
753
754     (or (and group (not (gnus-group-read-only-p group)))
755         (setq group (read-string "Put in group: " nil
756                                  (gnus-writable-groups))))
757     (when (gnus-gethash group gnus-newsrc-hashtb)
758       (error "No such group: %s" group))
759
760     (save-excursion
761       (save-restriction
762         (widen)
763         (message-narrow-to-headers)
764         (let (gnus-deletable-headers)
765           (if (message-news-p)
766               (message-generate-headers message-required-news-headers)
767             (message-generate-headers message-required-mail-headers)))
768         (goto-char (point-max))
769         (insert "Gcc: " group "\n")
770         (widen)))
771
772     (gnus-inews-do-gcc)
773
774     (when (get-buffer gnus-group-buffer)
775       (when (gnus-buffer-exists-p (car-safe reply))
776         (set-buffer (car reply))
777         (and (cdr reply)
778              (gnus-summary-mark-article-as-replied
779               (cdr reply))))
780       (when winconf
781         (set-window-configuration winconf)))))
782
783 (defun gnus-article-mail (yank)
784   "Send a reply to the address near point.
785 If YANK is non-nil, include the original article."
786   (interactive "P")
787   (let ((address
788          (buffer-substring
789           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
790           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
791     (when address
792       (message-reply address)
793       (when yank
794         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
795
796 (defvar nntp-server-type)
797 (defun gnus-bug ()
798   "Send a bug report to the Gnus maintainers."
799   (interactive)
800   (unless (gnus-alive-p)
801     (error "Gnus has been shut down"))
802   (gnus-setup-message 'bug
803     (delete-other-windows)
804     (switch-to-buffer (get-buffer-create "*Gnus Help Bug*"))
805     (erase-buffer)
806     (insert gnus-bug-message)
807     (goto-char (point-min))
808     (message-pop-to-buffer "*Gnus Bug*")
809     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
810     (push `(gnus-bug-kill-buffer) message-send-actions)
811     (goto-char (point-min))
812     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
813     (forward-line 1)
814     (insert (gnus-version) "\n")
815     (insert (emacs-version) "\n")
816     (when (and (boundp 'nntp-server-type)
817                (stringp nntp-server-type))
818       (insert nntp-server-type))
819     (insert "\n\n\n\n\n")
820     (gnus-debug)
821     (goto-char (point-min))
822     (search-forward "Subject: " nil t)
823     (message "")))
824
825 (defun gnus-bug-kill-buffer ()
826   (when (get-buffer "*Gnus Help Bug*")
827     (kill-buffer "*Gnus Help Bug*")))
828
829 (defun gnus-debug ()
830   "Attempts to go through the Gnus source file and report what variables have been changed.
831 The source file has to be in the Emacs load path."
832   (interactive)
833   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
834                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
835                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
836                  "nnmail.el" "message.el"))
837         (point (point))
838         file expr olist sym)
839     (gnus-message 4 "Please wait while we snoop your variables...")
840     (sit-for 0)
841     ;; Go through all the files looking for non-default values for variables.
842     (save-excursion
843       (set-buffer (get-buffer-create " *gnus bug info*"))
844       (buffer-disable-undo (current-buffer))
845       (while files
846         (erase-buffer)
847         (when (and (setq file (locate-library (pop files)))
848                    (file-exists-p file))
849           (insert-file-contents file)
850           (goto-char (point-min))
851           (if (not (re-search-forward "^;;* *Internal variables" nil t))
852               (gnus-message 4 "Malformed sources in file %s" file)
853             (narrow-to-region (point-min) (point))
854             (goto-char (point-min))
855             (while (setq expr (ignore-errors (read (current-buffer))))
856               (ignore-errors
857                 (and (or (eq (car expr) 'defvar)
858                          (eq (car expr) 'defcustom))
859                      (stringp (nth 3 expr))
860                      (or (not (boundp (nth 1 expr)))
861                          (not (equal (eval (nth 2 expr))
862                                      (symbol-value (nth 1 expr)))))
863                      (push (nth 1 expr) olist)))))))
864       (kill-buffer (current-buffer)))
865     (when (setq olist (nreverse olist))
866       (insert "------------------ Environment follows ------------------\n\n"))
867     (while olist
868       (if (boundp (car olist))
869           (condition-case ()
870               (pp `(setq ,(car olist)
871                          ,(if (or (consp (setq sym (symbol-value (car olist))))
872                                   (and (symbolp sym)
873                                        (not (or (eq sym nil)
874                                                 (eq sym t)))))
875                               (list 'quote (symbol-value (car olist)))
876                             (symbol-value (car olist))))
877                   (current-buffer))
878             (error
879              (format "(setq %s 'whatever)\n" (car olist))))
880         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
881       (setq olist (cdr olist)))
882     (insert "\n\n")
883     ;; Remove any control chars - they seem to cause trouble for some
884     ;; mailers.  (Byte-compiled output from the stuff above.)
885     (goto-char point)
886     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
887       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
888                      t t))))
889
890 ;;; Treatment of rejected articles.
891 ;;; Bounced mail.
892
893 (defun gnus-summary-resend-bounced-mail (&optional fetch)
894   "Re-mail the current message.
895 This only makes sense if the current message is a bounce message than
896 contains some mail you have written which has been bounced back to
897 you.
898 If FETCH, try to fetch the article that this is a reply to, if indeed
899 this is a reply."
900   (interactive "P")
901   (gnus-summary-select-article t)
902   (set-buffer gnus-original-article-buffer)
903   (gnus-setup-message 'compose-bounce
904     (let* ((references (mail-fetch-field "references"))
905            (parent (and references (gnus-parent-id references))))
906       (message-bounce)
907       ;; If there are references, we fetch the article we answered to.
908       (and fetch parent
909            (gnus-summary-refer-article parent)
910            (gnus-summary-show-all-headers)))))
911
912 ;;; Gcc handling.
913
914 ;; Do Gcc handling, which copied the message over to some group.
915 (defun gnus-inews-do-gcc (&optional gcc)
916   (interactive)
917   (when (gnus-alive-p)
918     (save-excursion
919       (save-restriction
920         (message-narrow-to-headers)
921         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
922               (cur (current-buffer))
923               groups group method)
924           (when gcc
925             (message-remove-header "gcc")
926             (widen)
927             (setq groups (message-tokenize-header gcc " ,"))
928             ;; Copy the article over to some group(s).
929             (while (setq group (pop groups))
930               (gnus-check-server
931                (setq method
932                      (cond ((and (null (gnus-get-info group))
933                                  (eq (car gnus-message-archive-method)
934                                      (car
935                                       (gnus-server-to-method
936                                        (gnus-group-method group)))))
937                             ;; If the group doesn't exist, we assume
938                             ;; it's an archive group...
939                             gnus-message-archive-method)
940                            ;; Use the method.
941                            ((gnus-info-method (gnus-get-info group))
942                             (gnus-info-method (gnus-get-info group)))
943                            ;; Find the method.
944                            (t (gnus-group-method group)))))
945               (gnus-check-server method)
946               (unless (gnus-request-group group t method)
947                 (gnus-request-create-group group method))
948               (save-excursion
949                 (nnheader-set-temp-buffer " *acc*")
950                 (insert-buffer-substring cur)
951                 (goto-char (point-min))
952                 (when (re-search-forward
953                        (concat "^" (regexp-quote mail-header-separator) "$")
954                        nil t)
955                   (replace-match "" t t ))
956                 (unless (gnus-request-accept-article group method t)
957                   (gnus-message 1 "Couldn't store article in group %s: %s"
958                                 group (gnus-status-message method))
959                   (sit-for 2))
960                 (kill-buffer (current-buffer))))))))))
961
962 (defun gnus-inews-insert-gcc ()
963   "Insert Gcc headers based on `gnus-outgoing-message-group'."
964   (save-excursion
965     (save-restriction
966       (message-narrow-to-headers)
967       (let* ((group gnus-outgoing-message-group)
968              (gcc (cond
969                    ((gnus-functionp group)
970                     (funcall group))
971                    ((or (stringp group) (list group))
972                     group))))
973         (when gcc
974           (insert "Gcc: "
975                   (if (stringp gcc) gcc
976                     (mapconcat 'identity gcc " "))
977                   "\n"))))))
978
979 (defun gnus-inews-insert-archive-gcc (&optional group)
980   "Insert the Gcc to say where the article is to be archived."
981   (let* ((var gnus-message-archive-group)
982          (group (or group gnus-newsgroup-name ""))
983          (gcc-self-val
984           (and gnus-newsgroup-name
985                (gnus-group-find-parameter
986                 gnus-newsgroup-name 'gcc-self)))
987          result 
988          (groups
989           (cond
990            ((null gnus-message-archive-method)
991             ;; Ignore.
992             nil)
993            ((stringp var)
994             ;; Just a single group.
995             (list var))
996            ((null var)
997             ;; We don't want this.
998             nil)
999            ((and (listp var) (stringp (car var)))
1000             ;; A list of groups.
1001             var)
1002            ((gnus-functionp var)
1003             ;; A function.
1004             (funcall var group))
1005            (t
1006             ;; An alist of regexps/functions/forms.
1007             (while (and var
1008                         (not
1009                          (setq result
1010                                (cond
1011                                 ((stringp (caar var))
1012                                  ;; Regexp.
1013                                  (when (string-match (caar var) group)
1014                                    (cdar var)))
1015                                 ((gnus-functionp (car var))
1016                                  ;; Function.
1017                                  (funcall (car var) group))
1018                                 (t
1019                                  (eval (car var)))))))
1020               (setq var (cdr var)))
1021             result)))
1022          name)
1023     (when (or groups gcc-self-val)
1024       (when (stringp groups)
1025         (setq groups (list groups)))
1026       (save-excursion
1027         (save-restriction
1028           (message-narrow-to-headers)
1029           (goto-char (point-max))
1030           (insert "Gcc: ")
1031           (if gcc-self-val
1032               ;; Use the `gcc-self' param value instead.
1033               (progn
1034                 (insert
1035                  (if (stringp gcc-self-val)
1036                      gcc-self-val
1037                    group))
1038                 (if (not (eq gcc-self-val 'none))
1039                     (insert "\n")
1040                   (progn
1041                     (beginning-of-line)
1042                     (kill-line))))
1043             ;; Use the list of groups.
1044             (while (setq name (pop groups))
1045               (insert (if (string-match ":" name)
1046                           name
1047                         (gnus-group-prefixed-name
1048                          name gnus-message-archive-method)))
1049               (when groups
1050                 (insert " ")))
1051             (insert "\n")))))))
1052
1053 (gnus-add-shutdown 'gnus-inews-close 'gnus)
1054
1055 (defun gnus-inews-close ()
1056   (setq gnus-inews-sent-ids nil))
1057
1058 ;;; Allow redefinition of functions.
1059
1060 (gnus-ems-redefine)
1061
1062 (provide 'gnus-msg)
1063
1064 ;;; gnus-msg.el ends here