* gnus-art.el (gnus-article-reply-with-original): New command.
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-ems)
34 (require 'message)
35 (require 'gnus-art)
36
37 (defcustom gnus-post-method 'current
38   "*Preferred method for posting USENET news.
39
40 If this variable is `current' (which is the default), Gnus will use
41 the \"current\" select method when posting.  If it is `native', Gnus
42 will use the native select method when posting.
43
44 This method will not be used in mail groups and the like, only in
45 \"real\" newsgroups.
46
47 If not `native' nor `current', the value must be a valid method as discussed
48 in the documentation of `gnus-select-method'.  It can also be a list of
49 methods.  If that is the case, the user will be queried for what select
50 method to use when posting."
51   :group 'gnus-group-foreign
52   :link '(custom-manual "(gnus)Posting Server")
53   :type `(choice (const native)
54                  (const current)
55                  (sexp :tag "Methods" ,gnus-select-method)))
56
57 (defcustom gnus-outgoing-message-group nil
58   "*All outgoing messages will be put in this group.
59 If you want to store all your outgoing mail and articles in the group
60 \"nnml:archive\", you set this variable to that value.  This variable
61 can also be a list of group names.
62
63 If you want to have greater control over what group to put each
64 message in, you can set this variable to a function that checks the
65 current newsgroup name and then returns a suitable group name (or list
66 of names)."
67   :group 'gnus-message
68   :type '(choice (string :tag "Group")
69                  (function)))
70
71 (defcustom gnus-mailing-list-groups nil
72   "*Regexp matching groups that are really mailing lists.
73 This is useful when you're reading a mailing list that has been
74 gatewayed to a newsgroup, and you want to followup to an article in
75 the group."
76   :group 'gnus-message
77   :type 'regexp)
78
79 (defcustom gnus-add-to-list nil
80   "*If non-nil, add a `to-list' parameter automatically."
81   :group 'gnus-message
82   :type 'boolean)
83
84 (defcustom gnus-crosspost-complaint
85   "Hi,
86
87 You posted the article below with the following Newsgroups header:
88
89 Newsgroups: %s
90
91 The %s group, at least, was an inappropriate recipient
92 of this message.  Please trim your Newsgroups header to exclude this
93 group before posting in the future.
94
95 Thank you.
96
97 "
98   "Format string to be inserted when complaining about crossposts.
99 The first %s will be replaced by the Newsgroups header;
100 the second with the current group name."
101   :group 'gnus-message
102   :type 'string)
103
104 (defcustom gnus-message-setup-hook nil
105   "Hook run after setting up a message buffer."
106   :group 'gnus-message
107   :type 'hook)
108
109 (defcustom gnus-bug-create-help-buffer t
110   "*Should we create the *Gnus Help Bug* buffer?"
111   :group 'gnus-message
112   :type 'boolean)
113
114 (defcustom gnus-posting-styles nil
115   "*Alist of styles to use when posting.
116 See Info node `(gnus)Posting Styles'."
117   :group 'gnus-message
118   :type '(repeat (cons (choice (regexp)
119                                (function)
120                                (variable)
121                                (sexp))
122                        (repeat (list
123                                 (choice (const signature)
124                                         (const signature-file)
125                                         (const organization)
126                                         (const address)
127                                         (const name)
128                                         (const body)
129                                         (string :tag "Header"))
130                                 (choice (string)
131                                         (function)
132                                         (variable)
133                                         (sexp)))))))
134
135 (defcustom gnus-inews-mark-gcc-as-read nil
136   "If non-nil, automatically mark Gcc articles as read."
137   :group 'gnus-message
138   :type 'boolean)
139
140 (defcustom gnus-gcc-externalize-attachments nil
141   "Should local-file attachments be included as external parts in Gcc copies?
142 If it is `all', attach files as external parts;
143 if a regexp and matches the Gcc group name, attach files as external parts;
144 If nil, attach files as normal parts."
145   :group 'gnus-message
146   :type '(choice (const nil :tag "None")
147                  (const all :tag "Any")
148                  (string :tag "Regexp")))
149
150 (defcustom gnus-group-posting-charset-alist
151   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
152     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
153     (message-this-is-mail nil nil)
154     (message-this-is-news nil t))
155   "Alist of regexps and permitted unencoded charsets for posting.
156 Each element of the alist has the form (TEST HEADER BODY-LIST), where
157 TEST is either a regular expression matching the newsgroup header or a
158 variable to query,
159 HEADER is the charset which may be left unencoded in the header (nil
160 means encode all charsets),
161 BODY-LIST is a list of charsets which may be encoded using 8bit
162 content-transfer encoding in the body, or one of the special values
163 nil (always encode using quoted-printable) or t (always use 8bit).
164
165 Note that any value other than nil for HEADER infringes some RFCs, so
166 use this option with care."
167   :type '(repeat (list :tag "Permitted unencoded charsets"
168                   (choice :tag "Where"
169                    (regexp :tag "Group")
170                    (const :tag "Mail message" :value message-this-is-mail)
171                    (const :tag "News article" :value message-this-is-news))
172                   (choice :tag "Header"
173                    (const :tag "None" nil)
174                    (symbol :tag "Charset"))
175                   (choice :tag "Body"
176                           (const :tag "Any" :value t)
177                           (const :tag "None" :value nil)
178                           (repeat :tag "Charsets"
179                                   (symbol :tag "Charset")))))
180   :group 'gnus-charset)
181
182 ;;; Internal variables.
183
184 (defvar gnus-inhibit-posting-styles nil
185   "Inhibit the use of posting styles.")
186
187 (defvar gnus-message-buffer "*Mail Gnus*")
188 (defvar gnus-article-copy nil)
189 (defvar gnus-check-before-posting nil)
190 (defvar gnus-last-posting-server nil)
191 (defvar gnus-message-group-art nil)
192
193 (defvar gnus-msg-force-broken-reply-to nil)
194
195 (defconst gnus-bug-message
196   "Sending a bug report to the Gnus Towers.
197 ========================================
198
199 The buffer below is a mail buffer.  When you press `C-c C-c', it will
200 be sent to the Gnus Bug Exterminators.
201
202 The thing near the bottom of the buffer is how the environment
203 settings will be included in the mail.  Please do not delete that.
204 They will tell the Bug People what your environment is, so that it
205 will be easier to locate the bugs.
206
207 If you have found a bug that makes Emacs go \"beep\", set
208 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
209 and include the backtrace in your bug report.
210
211 Please describe the bug in annoying, painstaking detail.
212
213 Thank you for your help in stamping out bugs.
214 ")
215
216 (eval-and-compile
217   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
218   (autoload 'news-setup "rnewspost")
219   (autoload 'news-reply-mode "rnewspost")
220   (autoload 'rmail-dont-reply-to "mail-utils")
221   (autoload 'rmail-output "rmailout"))
222
223 \f
224 ;;;
225 ;;; Gnus Posting Functions
226 ;;;
227
228 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
229   "p" gnus-summary-post-news
230   "i" gnus-summary-news-other-window
231   "f" gnus-summary-followup
232   "F" gnus-summary-followup-with-original
233   "c" gnus-summary-cancel-article
234   "s" gnus-summary-supersede-article
235   "r" gnus-summary-reply
236   "y" gnus-summary-yank-message
237   "R" gnus-summary-reply-with-original
238   "w" gnus-summary-wide-reply
239   "W" gnus-summary-wide-reply-with-original
240   "v" gnus-summary-very-wide-reply
241   "W" gnus-summary-very-wide-reply-with-original
242   "n" gnus-summary-followup-to-mail
243   "N" gnus-summary-followup-to-mail-with-original
244   "m" gnus-summary-mail-other-window
245   "u" gnus-uu-post-news
246   "\M-c" gnus-summary-mail-crosspost-complaint
247   "Br" gnus-summary-reply-broken-reply-to
248   "BR" gnus-summary-reply-broken-reply-to-with-original
249   "om" gnus-summary-mail-forward
250   "op" gnus-summary-post-forward
251   "Om" gnus-uu-digest-mail-forward
252   "Op" gnus-uu-digest-post-forward)
253
254 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
255   "b" gnus-summary-resend-bounced-mail
256   ;; "c" gnus-summary-send-draft
257   "r" gnus-summary-resend-message)
258
259 ;;; Internal functions.
260
261 (defvar gnus-article-reply nil)
262 (defmacro gnus-setup-message (config &rest forms)
263   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
264         (buffer (make-symbol "gnus-setup-message-buffer"))
265         (article (make-symbol "gnus-setup-message-article"))
266         (group (make-symbol "gnus-setup-message-group")))
267     `(let ((,winconf (current-window-configuration))
268            (,buffer (buffer-name (current-buffer)))
269            (,article gnus-article-reply)
270            (,group gnus-newsgroup-name)
271            (message-header-setup-hook
272             (copy-sequence message-header-setup-hook))
273            (mbl mml-buffer-list)
274            (message-mode-hook (copy-sequence message-mode-hook)))
275        (setq mml-buffer-list nil)
276        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
277        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
278        ;; #### FIXME: for a reason that I did not manage to identify yet,
279        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
280        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
281        ;; After evaluation of @forms below, it gets the value we actually want
282        ;; to override, and the posting styles are used. For that reason, I've
283        ;; added an optional argument to `gnus-configure-posting-styles' to
284        ;; make sure that the correct value for the group name is used. -- drv
285        (add-hook 'message-mode-hook
286                  (lambda ()
287                    (gnus-configure-posting-styles ,group)))
288        (unwind-protect
289            (progn
290              ,@forms)
291          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config)
292          (gnus-inews-insert-draft-meta-information ,group ,article)
293          (setq gnus-message-buffer (current-buffer))
294          (set (make-local-variable 'gnus-message-group-art)
295               (cons ,group ,article))
296          (set (make-local-variable 'gnus-newsgroup-name) ,group)
297          (gnus-run-hooks 'gnus-message-setup-hook)
298          (if (eq major-mode 'message-mode)
299              (let ((mbl1 mml-buffer-list))
300                (setq mml-buffer-list mbl)  ;; Global value
301                (set (make-local-variable 'mml-buffer-list) mbl1);; Local value
302                ;; LOCAL argument of add-hook differs between GNU Emacs
303                ;; and XEmacs. make-local-hook makes sure they are local.
304                (make-local-hook 'kill-buffer-hook)
305                (make-local-hook 'change-major-mode-hook)
306                (add-hook 'change-major-mode-hook 'mml-destroy-buffers nil t)
307                (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))
308            (mml-destroy-buffers)
309            (setq mml-buffer-list mbl)))
310        (gnus-add-buffer)
311        (gnus-configure-windows ,config t)
312        (run-hooks 'post-command-hook)
313        (set-buffer-modified-p nil))))
314
315 (defun gnus-inews-insert-draft-meta-information (group article)
316   (save-excursion
317     (when (and group
318                (not (string= group ""))
319                (not (message-fetch-field gnus-draft-meta-information-header)))
320       (goto-char (point-min))
321       (insert gnus-draft-meta-information-header ": (\"" group "\" "
322               (if article (number-to-string
323                            (if (listp article)
324                                (car article)
325                              article)) "\"\"")
326               ")\n"))))
327
328 ;;;###autoload
329 (defun gnus-msg-mail (&optional to subject other-headers continue
330                                 switch-action yank-action send-actions)
331   "Start editing a mail message to be sent.
332 Like `message-mail', but with Gnus paraphernalia, particularly the
333 Gcc: header for archiving purposes."
334   (interactive)
335   (let ((buf (current-buffer))
336         mail-buf)
337     (gnus-setup-message 'message
338       (message-mail to subject other-headers continue
339                     nil yank-action send-actions))
340     (when switch-action
341       (setq mail-buf (current-buffer))
342       (switch-to-buffer buf)
343       (apply switch-action mail-buf nil)))
344   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
345   t)
346
347 (defvar save-selected-window-window)
348
349 ;;;###autoload
350 (defun gnus-button-mailto (address)
351   "Mail to ADDRESS."
352   (set-buffer (gnus-copy-article-buffer))
353   (gnus-setup-message 'message
354     (message-reply address))
355   (and (boundp 'save-selected-window-window)
356        (not (window-live-p save-selected-window-window))
357        (setq save-selected-window-window (selected-window))))
358
359 ;;;###autoload
360 (defun gnus-button-reply (&optional to-address wide)
361   "Like `message-reply'."
362   (interactive)
363   (gnus-setup-message 'message
364     (message-reply to-address wide))
365   (and (boundp 'save-selected-window-window)
366        (not (window-live-p save-selected-window-window))
367        (setq save-selected-window-window (selected-window))))
368
369 ;;;###autoload
370 (define-mail-user-agent 'gnus-user-agent
371   'gnus-msg-mail 'message-send-and-exit
372   'message-kill-buffer 'message-send-hook)
373
374 (defun gnus-setup-posting-charset (group)
375   (let ((alist gnus-group-posting-charset-alist)
376         (group (or group ""))
377         elem)
378     (when group
379       (catch 'found
380         (while (setq elem (pop alist))
381           (when (or (and (stringp (car elem))
382                          (string-match (car elem) group))
383                     (and (gnus-functionp (car elem))
384                          (funcall (car elem) group))
385                     (and (symbolp (car elem))
386                          (symbol-value (car elem))))
387             (throw 'found (cons (cadr elem) (caddr elem)))))))))
388
389 (defun gnus-inews-add-send-actions (winconf buffer article &optional config)
390   (make-local-hook 'message-sent-hook)
391   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
392                                  'gnus-inews-do-gcc) nil t)
393   (when gnus-agent
394     (make-local-hook 'message-header-hook)
395     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
396   (setq message-post-method
397         `(lambda (arg)
398            (gnus-post-method arg ,gnus-newsgroup-name)))
399   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
400   (message-add-action
401    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
402   (message-add-action
403    `(when (gnus-buffer-exists-p ,buffer)
404       (save-excursion
405         (set-buffer ,buffer)
406         ,(when article
407            (if (eq config 'forward)
408                `(gnus-summary-mark-article-as-forwarded ',article)
409              `(gnus-summary-mark-article-as-replied ',article)))))
410    'send))
411
412 (put 'gnus-setup-message 'lisp-indent-function 1)
413 (put 'gnus-setup-message 'edebug-form-spec '(form body))
414
415 ;;; Post news commands of Gnus group mode and summary mode
416
417 (defun gnus-group-mail (&optional arg)
418   "Start composing a mail.
419 If ARG, use the group under the point to find a posting style.
420 If ARG is 1, prompt for a group name to find the posting style."
421   (interactive "P")
422   ;; We can't `let' gnus-newsgroup-name here, since that leads
423   ;; to local variables leaking.
424   (let ((group gnus-newsgroup-name)
425         (buffer (current-buffer)))
426     (unwind-protect
427         (progn
428           (setq gnus-newsgroup-name
429                 (if arg
430                     (if (= 1 (prefix-numeric-value arg))
431                         (completing-read "Use posting style of group: "
432                                          gnus-active-hashtb nil
433                                          (gnus-read-active-file-p))
434                       (gnus-group-group-name))
435                   ""))
436           ;; #### see comment in gnus-setup-message -- drv
437           (gnus-setup-message 'message (message-mail)))
438       (save-excursion
439         (set-buffer buffer)
440         (setq gnus-newsgroup-name group)))))
441
442 (defun gnus-group-news (&optional arg)
443   "Start composing a news.
444 If ARG, post to group under point.
445 If ARG is 1, prompt for group name to post to.
446
447 This function prepares a news even when using mail groups.  This is useful
448 for posting messages to mail groups without actually sending them over the
449 network.  The corresponding backend must have a 'request-post method."
450   (interactive "P")
451   ;; We can't `let' gnus-newsgroup-name here, since that leads
452   ;; to local variables leaking.
453   (let ((group gnus-newsgroup-name)
454         (buffer (current-buffer)))
455     (unwind-protect
456         (progn
457           (setq gnus-newsgroup-name
458                 (if arg
459                     (if (= 1 (prefix-numeric-value arg))
460                         (completing-read "Use group: "
461                                          gnus-active-hashtb nil
462                                          (gnus-read-active-file-p))
463                       (gnus-group-group-name))
464                   ""))
465           ;; #### see comment in gnus-setup-message -- drv
466           (gnus-setup-message 'message
467             (message-news (gnus-group-real-name gnus-newsgroup-name))))
468       (save-excursion
469         (set-buffer buffer)
470         (setq gnus-newsgroup-name group)))))
471
472 (defun gnus-group-post-news (&optional arg)
473   "Start composing a message (a news by default).
474 If ARG, post to group under point.  If ARG is 1, prompt for group name.
475 Depending on the selected group, the message might be either a mail or
476 a news."
477   (interactive "P")
478   ;; Bind this variable here to make message mode hooks work ok.
479   (let ((gnus-newsgroup-name
480          (if arg
481              (if (= 1 (prefix-numeric-value arg))
482                  (completing-read "Newsgroup: " gnus-active-hashtb nil
483                                   (gnus-read-active-file-p))
484                (gnus-group-group-name))
485            "")))
486     (gnus-post-news 'post gnus-newsgroup-name)))
487
488 (defun gnus-summary-mail-other-window (&optional arg)
489   "Start composing a mail in another window.
490 Use the posting of the current group by default.
491 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
492 posting style."
493   (interactive "P")
494   ;; We can't `let' gnus-newsgroup-name here, since that leads
495   ;; to local variables leaking.
496   (let ((group gnus-newsgroup-name)
497         (buffer (current-buffer)))
498     (unwind-protect
499         (progn
500           (setq gnus-newsgroup-name
501                 (if arg
502                     (if (= 1 (prefix-numeric-value arg))
503                         (completing-read "Use group: "
504                                          gnus-active-hashtb nil
505                                          (gnus-read-active-file-p))
506                       "")
507                   gnus-newsgroup-name))
508           ;; #### see comment in gnus-setup-message -- drv
509           (gnus-setup-message 'message (message-mail)))
510       (save-excursion
511         (set-buffer buffer)
512         (setq gnus-newsgroup-name group)))))
513
514 (defun gnus-summary-news-other-window (&optional arg)
515   "Start composing a news in another window.
516 Post to the current group by default.
517 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
518
519 This function prepares a news even when using mail groups.  This is useful
520 for posting messages to mail groups without actually sending them over the
521 network.  The corresponding backend must have a 'request-post method."
522   (interactive "P")
523   ;; We can't `let' gnus-newsgroup-name here, since that leads
524   ;; to local variables leaking.
525   (let ((group gnus-newsgroup-name)
526         (buffer (current-buffer)))
527     (unwind-protect
528         (progn
529           (setq gnus-newsgroup-name
530                 (if arg
531                     (if (= 1 (prefix-numeric-value arg))
532                         (completing-read "Use group: "
533                                          gnus-active-hashtb nil
534                                          (gnus-read-active-file-p))
535                       "")
536                   gnus-newsgroup-name))
537           ;; #### see comment in gnus-setup-message -- drv
538           (gnus-setup-message 'message
539             (message-news (gnus-group-real-name gnus-newsgroup-name))))
540       (save-excursion
541         (set-buffer buffer)
542         (setq gnus-newsgroup-name group)))))
543
544 (defun gnus-summary-post-news (&optional arg)
545   "Start composing a message.  Post to the current group by default.
546 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
547 Depending on the selected group, the message might be either a mail or
548 a news."
549   (interactive "P")
550   ;; Bind this variable here to make message mode hooks work ok.
551   (let ((gnus-newsgroup-name
552          (if arg
553              (if (= 1 (prefix-numeric-value arg))
554                  (completing-read "Newsgroup: " gnus-active-hashtb nil
555                                   (gnus-read-active-file-p))
556                "")
557            gnus-newsgroup-name)))
558     (gnus-post-news 'post gnus-newsgroup-name)))
559
560
561 (defun gnus-summary-followup (yank &optional force-news)
562   "Compose a followup to an article.
563 If prefix argument YANK is non-nil, the original article is yanked
564 automatically.
565 YANK is a list of elements, where the car of each element is the
566 article number, and the two following numbers is the region to be
567 yanked."
568   (interactive
569    (list (and current-prefix-arg
570               (gnus-summary-work-articles 1))))
571   (when yank
572     (gnus-summary-goto-subject
573      (if (listp (car yank))
574          (caar yank)
575        (car yank))))
576   (save-window-excursion
577     (gnus-summary-select-article))
578   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
579         (gnus-newsgroup-name gnus-newsgroup-name))
580     ;; Send a followup.
581     (gnus-post-news nil gnus-newsgroup-name
582                     headers gnus-article-buffer
583                     yank nil force-news)))
584
585 (defun gnus-summary-followup-with-original (n &optional force-news)
586   "Compose a followup to an article and include the original article."
587   (interactive "P")
588   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
589
590 (defun gnus-summary-followup-to-mail (&optional arg)
591   "Followup to the current mail message via news."
592   (interactive
593    (list (and current-prefix-arg
594               (gnus-summary-work-articles 1))))
595   (gnus-summary-followup arg t))
596
597 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
598   "Followup to the current mail message via news."
599   (interactive "P")
600   (gnus-summary-followup (gnus-summary-work-articles arg) t))
601
602 (defun gnus-inews-yank-articles (articles)
603   (let (beg article yank-string)
604     (message-goto-body)
605     (while (setq article (pop articles))
606       (when (listp article)
607         (setq yank-string (nth 1 article)
608               article (nth 0 article)))
609       (save-window-excursion
610         (set-buffer gnus-summary-buffer)
611         (gnus-summary-select-article nil nil nil article)
612         (gnus-summary-remove-process-mark article))
613       (gnus-copy-article-buffer nil yank-string)
614       (let ((message-reply-buffer gnus-article-copy)
615             (message-reply-headers
616              ;; The headers are decoded.
617              (with-current-buffer gnus-article-copy
618                (nnheader-parse-head t))))
619         (message-yank-original)
620         (setq beg (or beg (mark t))))
621       (when articles
622         (insert "\n")))
623     (push-mark)
624     (goto-char beg)))
625
626 (defun gnus-summary-cancel-article (&optional n symp)
627   "Cancel an article you posted.
628 Uses the process-prefix convention.  If given the symbolic
629 prefix `a', cancel using the standard posting method; if not
630 post using the current select method."
631   (interactive (gnus-interactive "P\ny"))
632   (let ((articles (gnus-summary-work-articles n))
633         (message-post-method
634          `(lambda (arg)
635             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
636         article)
637     (while (setq article (pop articles))
638       (when (gnus-summary-select-article t nil nil article)
639         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
640                 (message-cancel-news))
641           (gnus-summary-mark-as-read article gnus-canceled-mark)
642           (gnus-cache-remove-article 1))
643         (gnus-article-hide-headers-if-wanted))
644       (gnus-summary-remove-process-mark article))))
645
646 (defun gnus-summary-supersede-article ()
647   "Compose an article that will supersede a previous article.
648 This is done simply by taking the old article and adding a Supersedes
649 header line with the old Message-ID."
650   (interactive)
651   (let ((article (gnus-summary-article-number)))
652     (gnus-setup-message 'reply-yank
653       (gnus-summary-select-article t)
654       (set-buffer gnus-original-article-buffer)
655       (message-supersede)
656       (push
657        `((lambda ()
658            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
659              (save-excursion
660                (set-buffer ,gnus-summary-buffer)
661                (gnus-cache-possibly-remove-article ,article nil nil nil t)
662                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
663        message-send-actions))))
664
665 \f
666
667 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
668   ;; make a copy of the article buffer with all text properties removed
669   ;; this copy is in the buffer gnus-article-copy.
670   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
671   ;; this buffer should be passed to all mail/news reply/post routines.
672   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
673   (save-excursion
674     (set-buffer gnus-article-copy)
675     (mm-enable-multibyte))
676   (let ((article-buffer (or article-buffer gnus-article-buffer))
677         end beg)
678     (if (not (and (get-buffer article-buffer)
679                   (gnus-buffer-exists-p article-buffer)))
680         (error "Can't find any article buffer")
681       (save-excursion
682         (set-buffer article-buffer)
683         (let ((gnus-newsgroup-charset (or gnus-article-charset
684                                           gnus-newsgroup-charset))
685               (gnus-newsgroup-ignored-charsets
686                (or gnus-article-ignored-charsets
687                    gnus-newsgroup-ignored-charsets)))
688           (save-restriction
689             ;; Copy over the (displayed) article buffer, delete
690             ;; hidden text and remove text properties.
691             (widen)
692             (copy-to-buffer gnus-article-copy (point-min) (point-max))
693             (set-buffer gnus-article-copy)
694             (when yank-string
695               (message-goto-body)
696               (delete-region (point) (point-max))
697               (insert yank-string))
698             (gnus-article-delete-text-of-type 'annotation)
699             (gnus-remove-text-with-property 'gnus-prev)
700             (gnus-remove-text-with-property 'gnus-next)
701             (insert
702              (prog1
703                  (buffer-substring-no-properties (point-min) (point-max))
704                (erase-buffer)))
705             ;; Find the original headers.
706             (set-buffer gnus-original-article-buffer)
707             (goto-char (point-min))
708             (while (looking-at message-unix-mail-delimiter)
709               (forward-line 1))
710             (setq beg (point)
711                   end (or (message-goto-body) beg))
712             ;; Delete the headers from the displayed articles.
713             (set-buffer gnus-article-copy)
714             (delete-region (goto-char (point-min))
715                            (or (message-goto-body) (point-max)))
716             ;; Insert the original article headers.
717             (insert-buffer-substring gnus-original-article-buffer beg end)
718             ;; Decode charsets.
719             (let ((gnus-article-decode-hook
720                    (delq 'article-decode-charset
721                          (copy-sequence gnus-article-decode-hook))))
722               (run-hooks 'gnus-article-decode-hook)))))
723       gnus-article-copy)))
724
725 (defun gnus-post-news (post &optional group header article-buffer yank subject
726                             force-news)
727   (when article-buffer
728     (gnus-copy-article-buffer))
729   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
730         (add-to-list gnus-add-to-list))
731     (gnus-setup-message (cond (yank 'reply-yank)
732                               (article-buffer 'reply)
733                               (t 'message))
734       (let* ((group (or group gnus-newsgroup-name))
735              (charset (gnus-group-name-charset nil group))
736              (pgroup group)
737              to-address to-group mailing-list to-list
738              newsgroup-p)
739         (when group
740           (setq to-address (gnus-parameter-to-address group)
741                 to-group (gnus-group-find-parameter group 'to-group)
742                 to-list (gnus-parameter-to-list group)
743                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
744                 mailing-list (when gnus-mailing-list-groups
745                                (string-match gnus-mailing-list-groups group))
746                 group (gnus-group-name-decode (gnus-group-real-name group)
747                                               charset)))
748         (if (or (and to-group
749                      (gnus-news-group-p to-group))
750                 newsgroup-p
751                 force-news
752                 (and (gnus-news-group-p
753                       (or pgroup gnus-newsgroup-name)
754                       (or header gnus-current-article))
755                      (not mailing-list)
756                      (not to-list)
757                      (not to-address)))
758             ;; This is news.
759             (if post
760                 (message-news (or to-group group))
761               (set-buffer gnus-article-copy)
762               (gnus-msg-treat-broken-reply-to)
763               (message-followup (if (or newsgroup-p force-news)
764                                     (if (save-restriction
765                                           (article-narrow-to-head)
766                                           (message-fetch-field "newsgroups"))
767                                         nil
768                                       "")
769                                   to-group)))
770           ;; The is mail.
771           (if post
772               (progn
773                 (message-mail (or to-address to-list))
774                 ;; Arrange for mail groups that have no `to-address' to
775                 ;; get that when the user sends off the mail.
776                 (when (and (not to-list)
777                            (not to-address)
778                            add-to-list)
779                   (push (list 'gnus-inews-add-to-address pgroup)
780                         message-send-actions)))
781             (set-buffer gnus-article-copy)
782             (gnus-msg-treat-broken-reply-to)
783             (message-wide-reply to-address)))
784         (when yank
785           (gnus-inews-yank-articles yank))))))
786
787 (defun gnus-msg-treat-broken-reply-to (&optional force)
788   "Remove the Reply-to header iff broken-reply-to."
789   (when (or force
790             (gnus-group-find-parameter
791              gnus-newsgroup-name 'broken-reply-to))
792     (save-restriction
793       (message-narrow-to-head)
794       (message-remove-header "reply-to"))))
795
796 (defun gnus-post-method (arg group &optional silent)
797   "Return the posting method based on GROUP and ARG.
798 If SILENT, don't prompt the user."
799   (let ((gnus-post-method (or (gnus-parameter-post-method group)
800                               gnus-post-method))
801         (group-method (gnus-find-method-for-group group)))
802     (cond
803      ;; If the group-method is nil (which shouldn't happen) we use
804      ;; the default method.
805      ((null group-method)
806       (or (and (listp gnus-post-method) ;If not current/native/nil
807                (not (listp (car gnus-post-method))) ; and not a list of methods
808                gnus-post-method)        ;then use it.
809           gnus-select-method
810           message-post-method))
811      ;; We want the inverse of the default
812      ((and arg (not (eq arg 0)))
813       (if (eq gnus-post-method 'current)
814           gnus-select-method
815         group-method))
816      ;; We query the user for a post method.
817      ((or arg
818           (and (listp gnus-post-method)
819                (listp (car gnus-post-method))))
820       (let* ((methods
821               ;; Collect all methods we know about.
822               (append
823                (when (listp gnus-post-method)
824                  (if (listp (car gnus-post-method))
825                      gnus-post-method
826                    (list gnus-post-method)))
827                gnus-secondary-select-methods
828                (mapcar 'cdr gnus-server-alist)
829                (mapcar 'car gnus-opened-servers)
830                (list gnus-select-method)
831                (list group-method)))
832              method-alist post-methods method)
833         ;; Weed out all mail methods.
834         (while methods
835           (setq method (gnus-server-get-method "" (pop methods)))
836           (when (and (or (gnus-method-option-p method 'post)
837                          (gnus-method-option-p method 'post-mail))
838                      (not (member method post-methods)))
839             (push method post-methods)))
840         ;; Create a name-method alist.
841         (setq method-alist
842               (mapcar
843                (lambda (m)
844                  (if (equal (cadr m) "")
845                      (list (symbol-name (car m)) m)
846                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
847                post-methods))
848         ;; Query the user.
849         (cadr
850          (assoc
851           (setq gnus-last-posting-server
852                 (if (and silent
853                          gnus-last-posting-server)
854                     ;; Just use the last value.
855                     gnus-last-posting-server
856                   (completing-read
857                    "Posting method: " method-alist nil t
858                    (cons (or gnus-last-posting-server "") 0))))
859           method-alist))))
860      ;; Override normal method.
861      ((and (eq gnus-post-method 'current)
862            (not (eq (car group-method) 'nndraft))
863            (gnus-get-function group-method 'request-post t))
864       (assert (not arg))
865       group-method)
866      ;; Use gnus-post-method.
867      ((listp gnus-post-method)          ;A method...
868       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
869       gnus-post-method)
870      ;; Use the normal select method (nil or native).
871      (t gnus-select-method))))
872
873 \f
874
875 ;; Dummies to avoid byte-compile warning.
876 (eval-when-compile
877   (defvar nnspool-rejected-article-hook)
878   (defvar xemacs-codename))
879
880 (defun gnus-extended-version ()
881   "Stringified Gnus version and Emacs version."
882   (interactive)
883   (concat
884    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
885    " (" gnus-version ")"
886    " "
887    (cond
888     ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
889      (concat "Emacs/" (match-string 1 emacs-version)
890              " (" system-configuration ")"))
891     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
892                    emacs-version)
893      (concat (match-string 1 emacs-version)
894              (format "/%d.%d" emacs-major-version emacs-minor-version)
895              (if (match-beginning 3)
896                  (match-string 3 emacs-version)
897                "")
898              (if (boundp 'xemacs-codename)
899                  (concat " (" xemacs-codename ", " system-configuration ")")
900                "")))
901     (t emacs-version))))
902
903 \f
904 ;;;
905 ;;; Gnus Mail Functions
906 ;;;
907
908 ;;; Mail reply commands of Gnus summary mode
909
910 (defun gnus-summary-reply (&optional yank wide very-wide)
911   "Start composing a mail reply to the current message.
912 If prefix argument YANK is non-nil, the original article is yanked
913 automatically.
914 If WIDE, make a wide reply.
915 If VERY-WIDE, make a very wide reply."
916   (interactive
917    (list (and current-prefix-arg
918               (gnus-summary-work-articles 1))))
919   ;; Stripping headers should be specified with mail-yank-ignored-headers.
920   (when yank
921     (gnus-summary-goto-subject
922      (if (listp (car yank))
923          (caar yank)
924        (car yank))))
925   (let ((gnus-article-reply (or yank (gnus-summary-article-number)))
926         (headers ""))
927     (gnus-setup-message (if yank 'reply-yank 'reply)
928       (if (not very-wide)
929           (gnus-summary-select-article)
930         (dolist (article very-wide)
931           (gnus-summary-select-article nil nil nil article)
932           (save-excursion
933             (set-buffer (gnus-copy-article-buffer))
934             (gnus-msg-treat-broken-reply-to)
935             (save-restriction
936               (message-narrow-to-head)
937               (setq headers (concat headers (buffer-string)))))))
938       (set-buffer (gnus-copy-article-buffer))
939       (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
940       (save-restriction
941         (message-narrow-to-head)
942         (when very-wide
943           (erase-buffer)
944           (insert headers))
945         (goto-char (point-max)))
946       (mml-quote-region (point) (point-max))
947       (message-reply nil wide)
948       (when yank
949         (gnus-inews-yank-articles yank)))))
950
951 (defun gnus-summary-reply-with-original (n &optional wide)
952   "Start composing a reply mail to the current message.
953 The original article will be yanked."
954   (interactive "P")
955   (gnus-summary-reply (gnus-summary-work-articles n) wide))
956
957 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
958   "Like `gnus-summary-reply' except removing reply-to field.
959 If prefix argument YANK is non-nil, the original article is yanked
960 automatically.
961 If WIDE, make a wide reply.
962 If VERY-WIDE, make a very wide reply."
963   (interactive
964    (list (and current-prefix-arg
965               (gnus-summary-work-articles 1))))
966   (let ((gnus-msg-force-broken-reply-to t))
967     (gnus-summary-reply yank wide very-wide)))
968
969 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
970   "Like `gnus-summary-reply-with-original' except removing reply-to field.
971 The original article will be yanked."
972   (interactive "P")
973   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
974
975 (defun gnus-summary-wide-reply (&optional yank)
976   "Start composing a wide reply mail to the current message.
977 If prefix argument YANK is non-nil, the original article is yanked
978 automatically."
979   (interactive
980    (list (and current-prefix-arg
981               (gnus-summary-work-articles 1))))
982   (gnus-summary-reply yank t))
983
984 (defun gnus-summary-wide-reply-with-original (n)
985   "Start composing a wide reply mail to the current message.
986 The original article will be yanked."
987   (interactive "P")
988   (gnus-summary-reply-with-original n t))
989
990 (defun gnus-summary-very-wide-reply (&optional yank)
991   "Start composing a very wide reply mail to the current message.
992 If prefix argument YANK is non-nil, the original article is yanked
993 automatically."
994   (interactive
995    (list (and current-prefix-arg
996               (gnus-summary-work-articles 1))))
997   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
998
999 (defun gnus-summary-very-wide-reply-with-original (n)
1000   "Start composing a very wide reply mail to the current message.
1001 The original article will be yanked."
1002   (interactive "P")
1003   (gnus-summary-reply
1004    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1005
1006 (defun gnus-summary-mail-forward (&optional arg post)
1007   "Forward the current message(s) to another user.
1008 If process marks exist, forward all marked messages;
1009 if ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
1010 if ARG is 1, decode the message and forward directly inline;
1011 if ARG is 2, forward message as an rfc822 MIME section;
1012 if ARG is 3, decode message and forward as an rfc822 MIME section;
1013 if ARG is 4, forward message directly inline;
1014 otherwise, use flipped `message-forward-as-mime'.
1015 If POST, post instead of mail.
1016 For the `inline' alternatives, also see the variable
1017 `message-forward-ignored-headers'."
1018   (interactive "P")
1019   (if (null (cdr (gnus-summary-work-articles nil)))
1020       (let ((message-forward-as-mime message-forward-as-mime)
1021             (message-forward-show-mml message-forward-show-mml))
1022         (cond
1023          ((null arg))
1024          ((eq arg 1)
1025           (setq message-forward-as-mime nil
1026                 message-forward-show-mml t))
1027          ((eq arg 2)
1028           (setq message-forward-as-mime t
1029                 message-forward-show-mml nil))
1030          ((eq arg 3)
1031           (setq message-forward-as-mime t
1032                 message-forward-show-mml t))
1033          ((eq arg 4)
1034           (setq message-forward-as-mime nil
1035                 message-forward-show-mml nil))
1036          (t
1037           (setq message-forward-as-mime (not message-forward-as-mime))))
1038         (let ((gnus-article-reply (gnus-summary-article-number)))
1039           (gnus-setup-message 'forward
1040             (gnus-summary-select-article)
1041             (let ((mail-parse-charset gnus-newsgroup-charset)
1042                   (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
1043               (set-buffer gnus-original-article-buffer)
1044               (message-forward post)))))
1045     (gnus-uu-digest-mail-forward arg post)))
1046
1047 (defun gnus-summary-resend-message (address n)
1048   "Resend the current article to ADDRESS."
1049   (interactive
1050    (list (message-read-from-minibuffer "Resend message(s) to: ")
1051          current-prefix-arg))
1052   (let ((articles (gnus-summary-work-articles n))
1053         article)
1054     (while (setq article (pop articles))
1055       (gnus-summary-select-article nil nil nil article)
1056       (save-excursion
1057         (set-buffer gnus-original-article-buffer)
1058         (message-resend address))
1059       (gnus-summary-mark-article-as-forwarded article))))
1060
1061 (defun gnus-summary-post-forward (&optional arg)
1062   "Forward the current article to a newsgroup.
1063 See `gnus-summary-mail-forward' for ARG."
1064   (interactive "P")
1065   (gnus-summary-mail-forward arg t))
1066
1067 (defvar gnus-nastygram-message
1068   "The following article was inappropriately posted to %s.\n\n"
1069   "Format string to insert in nastygrams.
1070 The current group name will be inserted at \"%s\".")
1071
1072 (defun gnus-summary-mail-nastygram (n)
1073   "Send a nastygram to the author of the current article."
1074   (interactive "P")
1075   (when (or gnus-expert-user
1076             (gnus-y-or-n-p
1077              "Really send a nastygram to the author of the current article? "))
1078     (let ((group gnus-newsgroup-name))
1079       (gnus-summary-reply-with-original n)
1080       (set-buffer gnus-message-buffer)
1081       (message-goto-body)
1082       (insert (format gnus-nastygram-message group))
1083       (message-send-and-exit))))
1084
1085 (defun gnus-summary-mail-crosspost-complaint (n)
1086   "Send a complaint about crossposting to the current article(s)."
1087   (interactive "P")
1088   (let ((articles (gnus-summary-work-articles n))
1089         article)
1090     (while (setq article (pop articles))
1091       (set-buffer gnus-summary-buffer)
1092       (gnus-summary-goto-subject article)
1093       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1094             newsgroups followup-to)
1095         (gnus-summary-select-article)
1096         (set-buffer gnus-original-article-buffer)
1097         (if (and (<= (length (message-tokenize-header
1098                               (setq newsgroups
1099                                     (mail-fetch-field "newsgroups"))
1100                               ", "))
1101                      1)
1102                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1103                      (not (member group (message-tokenize-header
1104                                          followup-to ", ")))))
1105             (if followup-to
1106                 (gnus-message 1 "Followup-to restricted")
1107               (gnus-message 1 "Not a crossposted article"))
1108           (set-buffer gnus-summary-buffer)
1109           (gnus-summary-reply-with-original 1)
1110           (set-buffer gnus-message-buffer)
1111           (message-goto-body)
1112           (insert (format gnus-crosspost-complaint newsgroups group))
1113           (message-goto-subject)
1114           (re-search-forward " *$")
1115           (replace-match " (crosspost notification)" t t)
1116           (gnus-deactivate-mark)
1117           (when (gnus-y-or-n-p "Send this complaint? ")
1118             (message-send-and-exit)))))))
1119
1120 (defun gnus-mail-parse-comma-list ()
1121   (let (accumulated
1122         beg)
1123     (skip-chars-forward " ")
1124     (while (not (eobp))
1125       (setq beg (point))
1126       (skip-chars-forward "^,")
1127       (while (zerop
1128               (save-excursion
1129                 (save-restriction
1130                   (let ((i 0))
1131                     (narrow-to-region beg (point))
1132                     (goto-char beg)
1133                     (logand (progn
1134                               (while (search-forward "\"" nil t)
1135                                 (incf i))
1136                               (if (zerop i) 2 i))
1137                             2)))))
1138         (skip-chars-forward ",")
1139         (skip-chars-forward "^,"))
1140       (skip-chars-backward " ")
1141       (push (buffer-substring beg (point))
1142             accumulated)
1143       (skip-chars-forward "^,")
1144       (skip-chars-forward ", "))
1145     accumulated))
1146
1147 (defun gnus-inews-add-to-address (group)
1148   (let ((to-address (mail-fetch-field "to")))
1149     (when (and to-address
1150                (gnus-alive-p))
1151       ;; This mail group doesn't have a `to-list', so we add one
1152       ;; here.  Magic!
1153       (when (gnus-y-or-n-p
1154              (format "Do you want to add this as `to-list': %s " to-address))
1155         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1156
1157 (defun gnus-put-message ()
1158   "Put the current message in some group and return to Gnus."
1159   (interactive)
1160   (let ((reply gnus-article-reply)
1161         (winconf gnus-prev-winconf)
1162         (group gnus-newsgroup-name))
1163     (unless (and group
1164                  (not (gnus-group-read-only-p group)))
1165       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1166
1167     (when (gnus-gethash group gnus-newsrc-hashtb)
1168       (error "No such group: %s" group))
1169     (save-excursion
1170       (save-restriction
1171         (widen)
1172         (message-narrow-to-headers)
1173         (let ((gnus-deletable-headers nil))
1174           (message-generate-headers
1175            (if (message-news-p)
1176                message-required-news-headers
1177              message-required-mail-headers)))
1178         (goto-char (point-max))
1179         (insert "Gcc: " group "\n")
1180         (widen)))
1181     (gnus-inews-do-gcc)
1182     (when (and (get-buffer gnus-group-buffer)
1183                (gnus-buffer-exists-p (car-safe reply))
1184                (cdr reply))
1185       (set-buffer (car reply))
1186       (gnus-summary-mark-article-as-replied (cdr reply)))
1187     (when winconf
1188       (set-window-configuration winconf))))
1189
1190 (defun gnus-article-mail (yank)
1191   "Send a reply to the address near point.
1192 If YANK is non-nil, include the original article."
1193   (interactive "P")
1194   (let ((address
1195          (buffer-substring
1196           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1197           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1198     (when address
1199       (message-reply address)
1200       (when yank
1201         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1202
1203 (defvar nntp-server-type)
1204 (defun gnus-bug ()
1205   "Send a bug report to the Gnus maintainers."
1206   (interactive)
1207   (unless (gnus-alive-p)
1208     (error "Gnus has been shut down"))
1209   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1210     (unless (message-mail-user-agent)
1211       (delete-other-windows)
1212       (when gnus-bug-create-help-buffer
1213         (switch-to-buffer "*Gnus Help Bug*")
1214         (erase-buffer)
1215         (insert gnus-bug-message)
1216         (goto-char (point-min)))
1217       (message-pop-to-buffer "*Gnus Bug*"))
1218     (let ((message-this-is-mail t))
1219       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1220     (when gnus-bug-create-help-buffer
1221       (push `(gnus-bug-kill-buffer) message-send-actions))
1222     (goto-char (point-min))
1223     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1224     (forward-line 1)
1225     (insert (gnus-version) "\n"
1226             (emacs-version) "\n")
1227     (when (and (boundp 'nntp-server-type)
1228                (stringp nntp-server-type))
1229       (insert nntp-server-type))
1230     (insert "\n\n\n\n\n")
1231     (let (text)
1232       (save-excursion
1233         (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
1234         (erase-buffer)
1235         (gnus-debug)
1236         (setq text (buffer-string)))
1237       (insert "<#part type=application/x-emacs-lisp disposition=inline description=\"User settings\">\n" text "\n<#/part>"))
1238     (goto-char (point-min))
1239     (search-forward "Subject: " nil t)
1240     (message "")))
1241
1242 (defun gnus-bug-kill-buffer ()
1243   (when (get-buffer "*Gnus Help Bug*")
1244     (kill-buffer "*Gnus Help Bug*")))
1245
1246 (defun gnus-summary-yank-message (buffer n)
1247   "Yank the current article into a composed message."
1248   (interactive
1249    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1250          current-prefix-arg))
1251   (gnus-summary-iterate n
1252     (let ((gnus-display-mime-function nil)
1253           (gnus-inhibit-treatment t))
1254       (gnus-summary-select-article))
1255     (save-excursion
1256       (set-buffer buffer)
1257       (message-yank-buffer gnus-article-buffer))))
1258
1259 (defun gnus-debug ()
1260   "Attempts to go through the Gnus source file and report what variables have been changed.
1261 The source file has to be in the Emacs load path."
1262   (interactive)
1263   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1264                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1265                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1266                  "nnmail.el" "message.el"))
1267         (point (point))
1268         file expr olist sym)
1269     (gnus-message 4 "Please wait while we snoop your variables...")
1270     (sit-for 0)
1271     ;; Go through all the files looking for non-default values for variables.
1272     (save-excursion
1273       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1274       (while files
1275         (erase-buffer)
1276         (when (and (setq file (locate-library (pop files)))
1277                    (file-exists-p file))
1278           (insert-file-contents file)
1279           (goto-char (point-min))
1280           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1281               (gnus-message 4 "Malformed sources in file %s" file)
1282             (narrow-to-region (point-min) (point))
1283             (goto-char (point-min))
1284             (while (setq expr (ignore-errors (read (current-buffer))))
1285               (ignore-errors
1286                 (and (or (eq (car expr) 'defvar)
1287                          (eq (car expr) 'defcustom))
1288                      (stringp (nth 3 expr))
1289                      (or (not (boundp (nth 1 expr)))
1290                          (not (equal (eval (nth 2 expr))
1291                                      (symbol-value (nth 1 expr)))))
1292                      (push (nth 1 expr) olist)))))))
1293       (kill-buffer (current-buffer)))
1294     (when (setq olist (nreverse olist))
1295       (insert "------------------ Environment follows ------------------\n\n"))
1296     (while olist
1297       (if (boundp (car olist))
1298           (condition-case ()
1299               (pp `(setq ,(car olist)
1300                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1301                                   (and (symbolp sym)
1302                                        (not (or (eq sym nil)
1303                                                 (eq sym t)))))
1304                               (list 'quote (symbol-value (car olist)))
1305                             (symbol-value (car olist))))
1306                   (current-buffer))
1307             (error
1308              (format "(setq %s 'whatever)\n" (car olist))))
1309         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1310       (setq olist (cdr olist)))
1311     (insert "\n\n")
1312     ;; Remove any control chars - they seem to cause trouble for some
1313     ;; mailers.  (Byte-compiled output from the stuff above.)
1314     (goto-char point)
1315     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1316       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1317                      t t))))
1318
1319 ;;; Treatment of rejected articles.
1320 ;;; Bounced mail.
1321
1322 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1323   "Re-mail the current message.
1324 This only makes sense if the current message is a bounce message than
1325 contains some mail you have written which has been bounced back to
1326 you.
1327 If FETCH, try to fetch the article that this is a reply to, if indeed
1328 this is a reply."
1329   (interactive "P")
1330   (gnus-summary-select-article t)
1331   (set-buffer gnus-original-article-buffer)
1332   (gnus-setup-message 'compose-bounce
1333     (let* ((references (mail-fetch-field "references"))
1334            (parent (and references (gnus-parent-id references))))
1335       (message-bounce)
1336       ;; If there are references, we fetch the article we answered to.
1337       (and fetch parent
1338            (gnus-summary-refer-article parent)
1339            (gnus-summary-show-all-headers)))))
1340
1341 ;;; Gcc handling.
1342
1343 (defun gnus-inews-group-method (group)
1344   (cond
1345    ;; If the group doesn't exist, we assume
1346    ;; it's an archive group...
1347    ((and (null (gnus-get-info group))
1348          (eq (car (gnus-server-to-method gnus-message-archive-method))
1349              (car (gnus-server-to-method (gnus-group-method group)))))
1350     gnus-message-archive-method)
1351    ;; Use the method.
1352    ((gnus-info-method (gnus-get-info group))
1353     (gnus-info-method (gnus-get-info group)))
1354    ;; Find the method.
1355    (t (gnus-server-to-method (gnus-group-method group)))))
1356
1357 ;; Do Gcc handling, which copied the message over to some group.
1358 (defun gnus-inews-do-gcc (&optional gcc)
1359   (interactive)
1360   (save-excursion
1361     (save-restriction
1362       (message-narrow-to-headers)
1363       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1364             (cur (current-buffer))
1365             groups group method group-art
1366             mml-externalize-attachments)
1367         (when gcc
1368           (message-remove-header "gcc")
1369           (widen)
1370           (setq groups (message-unquote-tokens
1371                         (message-tokenize-header gcc " ,")))
1372           ;; Copy the article over to some group(s).
1373           (while (setq group (pop groups))
1374             (unless (gnus-check-server
1375                      (setq method (gnus-inews-group-method group)))
1376               (error "Can't open server %s" (if (stringp method) method
1377                                               (car method))))
1378             (unless (gnus-request-group group nil method)
1379               (gnus-request-create-group group method))
1380             (setq mml-externalize-attachments
1381                   (if (stringp gnus-gcc-externalize-attachments)
1382                       (string-match gnus-gcc-externalize-attachments group)
1383                     gnus-gcc-externalize-attachments))
1384             (save-excursion
1385               (nnheader-set-temp-buffer " *acc*")
1386               (insert-buffer-substring cur)
1387               (message-encode-message-body)
1388               (save-restriction
1389                 (message-narrow-to-headers)
1390                 (let* ((mail-parse-charset message-default-charset)
1391                        (newsgroups-field (save-restriction
1392                                            (message-narrow-to-headers-or-head)
1393                                            (message-fetch-field "Newsgroups")))
1394                        (followup-field (save-restriction
1395                                          (message-narrow-to-headers-or-head)
1396                                          (message-fetch-field "Followup-To")))
1397                        ;; BUG: We really need to get the charset for
1398                        ;; each name in the Newsgroups and Followup-To
1399                        ;; lines to allow crossposting between group
1400                        ;; namess with incompatible character sets.
1401                        ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
1402                        (group-field-charset
1403                         (gnus-group-name-charset
1404                          method (or newsgroups-field "")))
1405                        (followup-field-charset
1406                         (gnus-group-name-charset
1407                          method (or followup-field "")))
1408                        (rfc2047-header-encoding-alist
1409                         (append
1410                          (when group-field-charset
1411                            (list (cons "Newsgroups" group-field-charset)))
1412                          (when followup-field-charset
1413                            (list (cons "Followup-To" followup-field-charset)))
1414                          rfc2047-header-encoding-alist)))
1415                   (mail-encode-encoded-word-buffer)))
1416               (goto-char (point-min))
1417               (when (re-search-forward
1418                      (concat "^" (regexp-quote mail-header-separator) "$")
1419                      nil t)
1420                 (replace-match "" t t ))
1421               (unless (setq group-art
1422                             (gnus-request-accept-article group method t t))
1423                 (gnus-message 1 "Couldn't store article in group %s: %s"
1424                               group (gnus-status-message method))
1425                 (sit-for 2))
1426               (when (and group-art gnus-inews-mark-gcc-as-read)
1427                 (gnus-group-mark-article-read group (cdr group-art)))
1428               (kill-buffer (current-buffer)))))))))
1429
1430 (defun gnus-inews-insert-gcc ()
1431   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1432   (save-excursion
1433     (save-restriction
1434       (message-narrow-to-headers)
1435       (let* ((group gnus-outgoing-message-group)
1436              (gcc (cond
1437                    ((gnus-functionp group)
1438                     (funcall group))
1439                    ((or (stringp group) (list group))
1440                     group))))
1441         (when gcc
1442           (insert "Gcc: "
1443                   (if (stringp gcc) gcc
1444                     (mapconcat 'identity gcc " "))
1445                   "\n"))))))
1446
1447 (defun gnus-inews-insert-archive-gcc (&optional group)
1448   "Insert the Gcc to say where the article is to be archived."
1449   (let* ((var gnus-message-archive-group)
1450          (group (or group gnus-newsgroup-name ""))
1451          (gcc-self-val
1452           (and gnus-newsgroup-name
1453                (not (equal gnus-newsgroup-name ""))
1454                (gnus-group-find-parameter
1455                 gnus-newsgroup-name 'gcc-self)))
1456          result
1457          (groups
1458           (cond
1459            ((null gnus-message-archive-method)
1460             ;; Ignore.
1461             nil)
1462            ((stringp var)
1463             ;; Just a single group.
1464             (list var))
1465            ((null var)
1466             ;; We don't want this.
1467             nil)
1468            ((and (listp var) (stringp (car var)))
1469             ;; A list of groups.
1470             var)
1471            ((gnus-functionp var)
1472             ;; A function.
1473             (funcall var group))
1474            (t
1475             ;; An alist of regexps/functions/forms.
1476             (while (and var
1477                         (not
1478                          (setq result
1479                                (cond
1480                                 ((stringp (caar var))
1481                                  ;; Regexp.
1482                                  (when (string-match (caar var) group)
1483                                    (cdar var)))
1484                                 ((gnus-functionp (car var))
1485                                  ;; Function.
1486                                  (funcall (car var) group))
1487                                 (t
1488                                  (eval (car var)))))))
1489               (setq var (cdr var)))
1490             result)))
1491          name)
1492     (when (or groups gcc-self-val)
1493       (when (stringp groups)
1494         (setq groups (list groups)))
1495       (save-excursion
1496         (save-restriction
1497           (message-narrow-to-headers)
1498           (goto-char (point-max))
1499           (insert "Gcc: ")
1500           (if gcc-self-val
1501               ;; Use the `gcc-self' param value instead.
1502               (progn
1503                 (insert
1504                  (if (stringp gcc-self-val)
1505                      gcc-self-val
1506                    group))
1507                 (if (not (eq gcc-self-val 'none))
1508                     (insert "\n")
1509                   (progn
1510                     (beginning-of-line)
1511                     (kill-line))))
1512             ;; Use the list of groups.
1513             (while (setq name (pop groups))
1514               (insert (if (string-match ":" name)
1515                           name
1516                         (gnus-group-prefixed-name
1517                          name gnus-message-archive-method)))
1518               (when groups
1519                 (insert " ")))
1520             (insert "\n")))))))
1521
1522 ;;; Posting styles.
1523
1524 (defun gnus-configure-posting-styles (&optional group-name)
1525   "Configure posting styles according to `gnus-posting-styles'."
1526   (unless gnus-inhibit-posting-styles
1527     (let ((group (or group-name gnus-newsgroup-name ""))
1528           (styles gnus-posting-styles)
1529           style match variable attribute value v results
1530           filep name address element)
1531       ;; If the group has a posting-style parameter, add it at the end with a
1532       ;; regexp matching everything, to be sure it takes precedence over all
1533       ;; the others.
1534       (when gnus-newsgroup-name
1535         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1536           (when tmp-style
1537             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1538       ;; Go through all styles and look for matches.
1539       (dolist (style styles)
1540         (setq match (pop style))
1541         (goto-char (point-min))
1542         (when (cond
1543                ((stringp match)
1544                 ;; Regexp string match on the group name.
1545                 (string-match match group))
1546                ((eq match 'header)
1547                 (and (gnus-buffer-live-p gnus-article-copy)
1548                      (with-current-buffer gnus-article-copy
1549                        (let ((header (message-fetch-field (pop style))))
1550                          (and header
1551                               (string-match (pop style) header))))))
1552                ((or (symbolp match)
1553                     (gnus-functionp match))
1554                 (cond
1555                  ((gnus-functionp match)
1556                   ;; Function to be called.
1557                   (funcall match))
1558                  ((boundp match)
1559                   ;; Variable to be checked.
1560                   (symbol-value match))))
1561                ((listp match)
1562                 ;; This is a form to be evaled.
1563                 (eval match)))
1564           ;; We have a match, so we set the variables.
1565           (dolist (attribute style)
1566             (setq element (pop attribute)
1567                   variable nil
1568                   filep nil)
1569             (setq value
1570                   (cond
1571                    ((eq (car attribute) :file)
1572                     (setq filep t)
1573                     (cadr attribute))
1574                    ((eq (car attribute) :value)
1575                     (cadr attribute))
1576                    (t
1577                     (car attribute))))
1578             ;; We get the value.
1579             (setq v
1580                   (cond
1581                    ((stringp value)
1582                     value)
1583                    ((or (symbolp value)
1584                         (gnus-functionp value))
1585                     (cond ((gnus-functionp value)
1586                            (funcall value))
1587                           ((boundp value)
1588                            (symbol-value value))))
1589                    ((listp value)
1590                     (eval value))))
1591             ;; Translate obsolescent value.
1592             (when (eq element 'signature-file)
1593               (setq element 'signature
1594                     filep t))
1595             ;; Get the contents of file elems.
1596             (when (and filep v)
1597               (setq v (with-temp-buffer
1598                         (insert-file-contents v)
1599                         (buffer-string))))
1600             (setq results (delq (assoc element results) results))
1601             (push (cons element v) results))))
1602       ;; Now we have all the styles, so we insert them.
1603       (setq name (assq 'name results)
1604             address (assq 'address results))
1605       (setq results (delq name (delq address results)))
1606       ;; make-local-hook is not obsolete in Emacs 20 or XEmacs.
1607       (make-local-hook 'message-setup-hook)
1608       (dolist (result results)
1609         (add-hook 'message-setup-hook
1610                   (cond
1611                    ((eq 'eval (car result))
1612                     'ignore)
1613                    ((eq 'body (car result))
1614                     `(lambda ()
1615                        (save-excursion
1616                          (message-goto-body)
1617                          (insert ,(cdr result)))))
1618                    ((eq 'signature (car result))
1619                     (set (make-local-variable 'message-signature) nil)
1620                     (set (make-local-variable 'message-signature-file) nil)
1621                     (if (not (cdr result))
1622                         'ignore
1623                       `(lambda ()
1624                          (save-excursion
1625                            (let ((message-signature ,(cdr result)))
1626                              (when message-signature
1627                                (message-insert-signature)))))))
1628                    (t
1629                     (let ((header
1630                            (if (symbolp (car result))
1631                                (capitalize (symbol-name (car result)))
1632                              (car result))))
1633                       `(lambda ()
1634                          (save-excursion
1635                            (message-remove-header ,header)
1636                            (let ((value ,(cdr result)))
1637                              (when value
1638                                (message-goto-eoh)
1639                                (insert ,header ": " value "\n"))))))))
1640                   nil 'local))
1641       (when (or name address)
1642         (add-hook 'message-setup-hook
1643                   `(lambda ()
1644                      (set (make-local-variable 'user-mail-address)
1645                           ,(or (cdr address) user-mail-address))
1646                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1647                            (user-mail-address
1648                             ,(or (cdr address) user-mail-address)))
1649                        (save-excursion
1650                          (message-remove-header "From")
1651                          (message-goto-eoh)
1652                          (insert "From: " (message-make-from) "\n"))))
1653                   nil 'local)))))
1654
1655 ;;; Allow redefinition of functions.
1656
1657 (gnus-ems-redefine)
1658
1659 (provide 'gnus-msg)
1660
1661 ;;; gnus-msg.el ends here