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