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