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