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