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