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