[BTTF] * gnus-msg.el (gnus-inews-add-send-actions): Allow a list of articles
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;      Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;;      Katsumi Yamaoka  <yamaoka@jpl.org>
10 ;;      Kiyokazu SUTO    <suto@merry.xmath.ous.ac.jp>
11 ;; Keywords: mail, news, MIME
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'static))
36
37 (require 'gnus)
38 (require 'gnus-ems)
39 (require 'message)
40 (require 'gnus-art)
41
42 (defcustom gnus-post-method 'current
43   "*Preferred method for posting USENET news.
44
45 If this variable is `current' (which is the default), Gnus will use
46 the \"current\" select method when posting.  If it is `native', Gnus
47 will use the native select method when posting.
48
49 This method will not be used in mail groups and the like, only in
50 \"real\" newsgroups.
51
52 If not `native' nor `current', the value must be a valid method as discussed
53 in the documentation of `gnus-select-method'.  It can also be a list of
54 methods.  If that is the case, the user will be queried for what select
55 method to use when posting."
56   :group 'gnus-group-foreign
57   :link '(custom-manual "(gnus)Posting Server")
58   :type `(choice (const native)
59                  (const current)
60                  (sexp :tag "Methods" ,gnus-select-method)))
61
62 (defcustom gnus-outgoing-message-group nil
63   "*All outgoing messages will be put in this group.
64 If you want to store all your outgoing mail and articles in the group
65 \"nnml:archive\", you set this variable to that value.  This variable
66 can also be a list of group names.
67
68 If you want to have greater control over what group to put each
69 message in, you can set this variable to a function that checks the
70 current newsgroup name and then returns a suitable group name (or list
71 of names)."
72   :group 'gnus-message
73   :type '(choice (string :tag "Group")
74                  (function)))
75
76 (defcustom gnus-mailing-list-groups nil
77   "*Regexp matching groups that are really mailing lists.
78 This is useful when you're reading a mailing list that has been
79 gatewayed to a newsgroup, and you want to followup to an article in
80 the group."
81   :group 'gnus-message
82   :type 'regexp)
83
84 (defcustom gnus-add-to-list nil
85   "*If non-nil, add a `to-list' parameter automatically."
86   :group 'gnus-message
87   :type 'boolean)
88
89 (defcustom gnus-crosspost-complaint
90   "Hi,
91
92 You posted the article below with the following Newsgroups header:
93
94 Newsgroups: %s
95
96 The %s group, at least, was an inappropriate recipient
97 of this message.  Please trim your Newsgroups header to exclude this
98 group before posting in the future.
99
100 Thank you.
101
102 "
103   "Format string to be inserted when complaining about crossposts.
104 The first %s will be replaced by the Newsgroups header;
105 the second with the current group name."
106   :group 'gnus-message
107   :type 'string)
108
109 (defcustom gnus-message-setup-hook nil
110   "Hook run after setting up a message buffer."
111   :group 'gnus-message
112   :type 'hook)
113
114 (defcustom gnus-bug-create-help-buffer t
115   "*Should we create the *Gnus Help Bug* buffer?"
116   :group 'gnus-message
117   :type 'boolean)
118
119 (defcustom gnus-posting-styles nil
120   "*Alist of styles to use when posting.
121 See Info node `(gnus)Posting Styles'."
122   :group 'gnus-message
123   :link '(custom-manual "(gnus)Posting Styles")
124   :type '(repeat (cons (choice (regexp)
125                                (variable)
126                                (list (const header)
127                                      (string :tag "Header")
128                                      (regexp :tag "Regexp"))
129                                (function)
130                                (sexp))
131                        (repeat (list
132                                 (choice (const signature)
133                                         (const signature-file)
134                                         (const organization)
135                                         (const address)
136                                         (const x-face-file)
137                                         (const name)
138                                         (const body)
139                                         (const import)
140                                         (symbol)
141                                         (string :tag "Header"))
142                                 (choice (string)
143                                         (function)
144                                         (variable)
145                                         (sexp)))))))
146
147 (defcustom gnus-named-posting-styles nil
148   "Alist mapping names to the user-defined posting styles."
149   :group 'gnus-message
150   :type '(repeat (cons string
151                        (repeat (list
152                                 (choice (const signature)
153                                         (const signature-file)
154                                         (const organization)
155                                         (const address)
156                                         (const x-face-file)
157                                         (const name)
158                                         (const body)
159                                         (const import)
160                                         (symbol)
161                                         (string :tag "Header"))
162                                 (choice (string)
163                                         (function)
164                                         (variable)
165                                         (sexp)))))))
166
167 (defcustom gnus-gcc-mark-as-read nil
168   "If non-nil, automatically mark Gcc articles as read."
169   :version "21.1"
170   :group 'gnus-message
171   :type 'boolean)
172
173 (defvar gnus-inews-mark-gcc-as-read nil
174   "Obsolete variable. Use `gnus-gcc-mark-as-read' instead.")
175
176 (make-obsolete-variable 'gnus-inews-mark-gcc-as-read
177                         'gnus-gcc-mark-as-read)
178
179 (defcustom gnus-gcc-externalize-attachments nil
180   "Should local-file attachments be included as external parts in Gcc copies?
181 If it is `all', attach files as external parts;
182 if a regexp and matches the Gcc group name, attach files as external parts;
183 if nil, attach files as normal parts."
184   :version "21.1"
185   :group 'gnus-message
186   :type '(choice (const nil :tag "None")
187                  (const all :tag "Any")
188                  (string :tag "Regexp")))
189
190 (gnus-define-group-parameter
191  posting-charset-alist
192  :type list
193  :function-document
194  "Return the permitted unencoded charsets for posting of GROUP."
195  :variable gnus-group-posting-charset-alist
196  :variable-default
197  '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
198    ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
199    (message-this-is-mail nil nil)
200    (message-this-is-news nil t))
201  :variable-document
202  "Alist of regexps and permitted unencoded charsets for posting.
203 Each element of the alist has the form (TEST HEADER BODY-LIST), where
204 TEST is either a regular expression matching the newsgroup header or a
205 variable to query,
206 HEADER is the charset which may be left unencoded in the header (nil
207 means encode all charsets),
208 BODY-LIST is a list of charsets which may be encoded using 8bit
209 content-transfer encoding in the body, or one of the special values
210 nil (always encode using quoted-printable) or t (always use 8bit).
211
212 Note that any value other than nil for HEADER infringes some RFCs, so
213 use this option with care."
214  :variable-group gnus-charset
215  :variable-type
216  '(repeat (list :tag "Permitted unencoded charsets"
217                 (choice :tag "Where"
218                         (regexp :tag "Group")
219                         (const :tag "Mail message" :value message-this-is-mail)
220                         (const :tag "News article" :value message-this-is-news))
221                 (choice :tag "Header"
222                         (const :tag "None" nil)
223                         (symbol :tag "Charset"))
224                 (choice :tag "Body"
225                         (const :tag "Any" :value t)
226                         (const :tag "None" :value nil)
227                         (repeat :tag "Charsets"
228                                 (symbol :tag "Charset")))))
229  :parameter-type '(choice :tag "Permitted unencoded charsets"
230                           :value nil
231                           (repeat (symbol)))
232  :parameter-document       "\
233 List of charsets that are permitted to be unencoded.")
234
235 (defcustom gnus-debug-files
236   '("gnus.el" "gnus-sum.el" "gnus-group.el"
237     "gnus-art.el" "gnus-start.el" "gnus-async.el"
238     "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
239     "gnus-agent.el" "gnus-cache.el" "gnus-srvr.el"
240     "mm-util.el" "mm-decode.el" "nnmail.el" "nntp.el" "message.el")
241   "Files whose variables will be reported in `gnus-bug'."
242   :version "21.1"
243   :group 'gnus-message
244   :type '(repeat (string :tag "File")))
245
246 (defcustom gnus-debug-exclude-variables
247   '(mm-mime-mule-charset-alist
248     nnmail-split-fancy message-minibuffer-local-map)
249   "Variables that should not be reported in `gnus-bug'."
250   :version "21.1"
251   :group 'gnus-message
252   :type '(repeat (symbol :tag "Variable")))
253
254 (defcustom gnus-discouraged-post-methods
255   '(nndraft nnml nnimap nnmaildir nnmh nnfolder nndir)
256   "A list of back ends that are not used in \"real\" newsgroups.
257 This variable is used only when `gnus-post-method' is `current'."
258   :version "21.3"
259   :group 'gnus-group-foreign
260   :type '(repeat (symbol :tag "Back end")))
261
262 (defcustom gnus-message-replysign
263   nil
264   "Automatically sign replys to signed messages.
265 See also the `mml-default-sign-method' variable."
266   :group 'gnus-message
267   :type 'boolean)
268
269 (defcustom gnus-message-replyencrypt
270   nil
271   "Automatically encrypt replys to encrypted messages.
272 See also the `mml-default-encrypt-method' variable."
273   :group 'gnus-message
274   :type 'boolean)
275
276 (defcustom gnus-message-replysignencrypted
277   t
278   "Setting this causes automatically encryped messages to also be signed."
279   :group 'gnus-message
280   :type 'boolean)
281
282 (defcustom gnus-confirm-mail-reply-to-news nil
283   "If non-nil, Gnus requests confirmation when replying to news.
284 This is done because new users often reply by mistake when reading
285 news.
286 This can also be a function receiving the group name as the only
287 parameter which should return non-nil iff a confirmation is needed, or
288 a regexp, in which case a confirmation is asked for iff the group name
289 matches the regexp."
290   :group 'gnus-message
291   :type '(choice (const :tag "No" nil)
292                  (const :tag "Yes" t)
293                  (regexp :tag "Iff group matches regexp")
294                  (function :tag "Iff function evaluates to non-nil")))
295
296 (defcustom gnus-confirm-treat-mail-like-news
297   nil
298   "If non-nil, Gnus will treat mail like news with regard to confirmation
299 when replying by mail.  See the `gnus-confirm-mail-reply-to-news' variable
300 for fine-tuning this.
301 If nil, Gnus will never ask for confirmation if replying to mail."
302   :group 'gnus-message
303   :type 'boolean)
304
305 (defcustom gnus-summary-resend-default-address t
306   "If non-nil, Gnus tries to suggest a default address to resend to.
307 If nil, the address field will always be empty after invoking
308 `gnus-summary-resend-message'."
309   :group 'gnus-message
310   :type 'boolean)
311
312 ;;; Internal variables.
313
314 (defvar gnus-inhibit-posting-styles nil
315   "Inhibit the use of posting styles.")
316
317 (defvar gnus-article-yanked-articles nil)
318 (defvar gnus-message-buffer "*Mail Gnus*")
319 (defvar gnus-article-copy nil)
320 (defvar gnus-check-before-posting nil)
321 (defvar gnus-last-posting-server nil)
322 (defvar gnus-message-group-art nil)
323
324 (defvar gnus-msg-force-broken-reply-to nil)
325
326 (defconst gnus-bug-message
327   (format "Sending a bug report to the Gnus Towers.
328 ========================================
329
330 This gnus is the %s%s.
331 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
332 Developers. (the addresses below are mailing list addresses)
333
334 ========================================
335
336 The buffer below is a mail buffer.  When you press `C-c C-c', it will
337 be sent to the Gnus Bug Exterminators.
338
339 The thing near the bottom of the buffer is how the environment
340 settings will be included in the mail.  Please do not delete that.
341 They will tell the Bug People what your environment is, so that it
342 will be easier to locate the bugs.
343
344 If you have found a bug that makes Emacs go \"beep\", set
345 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
346 and include the backtrace in your bug report.
347
348 Please describe the bug in annoying, painstaking detail.
349
350 Thank you for your help in stamping out bugs.
351 "
352           gnus-product-name
353           (if (string= gnus-product-name "Semi-gnus")
354               ""
355             ", a modified version of Semi-gnus")))
356
357 (eval-and-compile
358   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
359   (autoload 'news-setup "rnewspost")
360   (autoload 'news-reply-mode "rnewspost")
361   (autoload 'rmail-dont-reply-to "mail-utils")
362   (autoload 'rmail-output "rmailout"))
363
364 \f
365 ;;;
366 ;;; Gnus Posting Functions
367 ;;;
368
369 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
370   "p" gnus-summary-post-news
371   "i" gnus-summary-news-other-window
372   "f" gnus-summary-followup
373   "F" gnus-summary-followup-with-original
374   "c" gnus-summary-cancel-article
375   "s" gnus-summary-supersede-article
376   "r" gnus-summary-reply
377   "y" gnus-summary-yank-message
378   "R" gnus-summary-reply-with-original
379   "w" gnus-summary-wide-reply
380   "W" gnus-summary-wide-reply-with-original
381   "v" gnus-summary-very-wide-reply
382   "V" gnus-summary-very-wide-reply-with-original
383   "n" gnus-summary-followup-to-mail
384   "N" gnus-summary-followup-to-mail-with-original
385   "m" gnus-summary-mail-other-window
386   "u" gnus-uu-post-news
387   "\M-c" gnus-summary-mail-crosspost-complaint
388   "Br" gnus-summary-reply-broken-reply-to
389   "BR" gnus-summary-reply-broken-reply-to-with-original
390   "om" gnus-summary-mail-forward
391   "op" gnus-summary-post-forward
392   "Om" gnus-summary-digest-mail-forward
393   "Op" gnus-summary-digest-post-forward
394   "P" gnus-summary-execute-command-with-posting-style)
395
396 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
397   "b" gnus-summary-resend-bounced-mail
398   ;; "c" gnus-summary-send-draft
399   "r" gnus-summary-resend-message
400   "e" gnus-summary-resend-message-edit)
401
402 ;;; Internal functions.
403
404 (defun gnus-inews-make-draft ()
405   `(lambda ()
406      (gnus-inews-make-draft-meta-information
407       ,gnus-newsgroup-name ',gnus-article-reply)))
408
409 (defvar gnus-article-reply nil)
410 (defmacro gnus-setup-message (config &rest forms)
411   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
412         (buffer (make-symbol "gnus-setup-message-buffer"))
413         (article (make-symbol "gnus-setup-message-article"))
414         (yanked (make-symbol "gnus-setup-yanked-articles"))
415         (group (make-symbol "gnus-setup-message-group")))
416     `(let ((,winconf (current-window-configuration))
417            (,buffer (buffer-name (current-buffer)))
418            (,article gnus-article-reply)
419            (,yanked gnus-article-yanked-articles)
420            (,group gnus-newsgroup-name)
421            (message-header-setup-hook
422             (copy-sequence message-header-setup-hook))
423            (message-mode-hook (copy-sequence message-mode-hook))
424            (message-startup-parameter-alist
425             '((reply-buffer . gnus-copy-article-buffer)
426               (original-buffer . gnus-original-article-buffer)
427               (user-agent . Gnus))))
428        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
429        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
430        ;; #### FIXME: for a reason that I did not manage to identify yet,
431        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
432        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
433        ;; After evaluation of @forms below, it gets the value we actually want
434        ;; to override, and the posting styles are used. For that reason, I've
435        ;; added an optional argument to `gnus-configure-posting-styles' to
436        ;; make sure that the correct value for the group name is used. -- drv
437        (add-hook 'message-mode-hook
438                  (lambda ()
439                    (gnus-configure-posting-styles ,group)))
440        (gnus-pull ',(intern gnus-draft-meta-information-header)
441                   message-required-headers)
442        (when (and ,group
443                   (not (string= ,group "")))
444          (push (cons
445                 (intern gnus-draft-meta-information-header)
446                 (gnus-inews-make-draft))
447                message-required-headers))
448        (unwind-protect
449            (progn
450              ,@forms)
451          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config
452                                       ,yanked)
453          (setq gnus-message-buffer (current-buffer))
454          (set (make-local-variable 'gnus-message-group-art)
455               (cons ,group ,article))
456          (set (make-local-variable 'gnus-newsgroup-name) ,group)
457          (gnus-maybe-setup-default-charset)
458          (gnus-run-hooks 'gnus-message-setup-hook))
459        (gnus-add-buffer)
460        (gnus-configure-windows ,config t)
461        (run-hooks 'post-command-hook)
462        (set-buffer-modified-p nil))))
463
464 (defun gnus-inews-make-draft-meta-information (group article)
465   (concat "(\"" group "\" "
466           (if article (number-to-string
467                        (if (listp article)
468                            (car article)
469                          article)) "\"\"")
470           ")"))
471
472 ;;;###autoload
473 (defun gnus-msg-mail (&optional to subject other-headers continue
474                                 switch-action yank-action send-actions)
475   "Start editing a mail message to be sent.
476 Like `message-mail', but with Gnus paraphernalia, particularly the
477 Gcc: header for archiving purposes."
478   (interactive)
479   (let ((buf (current-buffer))
480         mail-buf)
481     (gnus-setup-message 'message
482       (message-mail to subject other-headers continue
483                     nil yank-action send-actions))
484     (when switch-action
485       (setq mail-buf (current-buffer))
486       (switch-to-buffer buf)
487       (apply switch-action mail-buf nil)))
488   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
489   t)
490
491 (defvar save-selected-window-window)
492
493 ;;;###autoload
494 (defun gnus-button-mailto (address)
495   "Mail to ADDRESS."
496   (set-buffer (gnus-copy-article-buffer))
497   (gnus-setup-message 'message
498     (message-reply address))
499   (and (boundp 'save-selected-window-window)
500        (not (window-live-p save-selected-window-window))
501        (setq save-selected-window-window (selected-window))))
502
503 ;;;###autoload
504 (defun gnus-button-reply (&optional to-address wide)
505   "Like `message-reply'."
506   (interactive)
507   (gnus-setup-message 'message
508     (message-reply to-address wide))
509   (and (boundp 'save-selected-window-window)
510        (not (window-live-p save-selected-window-window))
511        (setq save-selected-window-window (selected-window))))
512
513 ;;;###autoload
514 (define-mail-user-agent 'gnus-user-agent
515   'gnus-msg-mail 'message-send-and-exit
516   'message-kill-buffer 'message-send-hook)
517
518 (defun gnus-setup-posting-charset (group)
519   (let ((alist gnus-group-posting-charset-alist)
520         (group (or group ""))
521         elem)
522     (when group
523       (catch 'found
524         (while (setq elem (pop alist))
525           (when (or (and (stringp (car elem))
526                          (string-match (car elem) group))
527                     (and (gnus-functionp (car elem))
528                          (funcall (car elem) group))
529                     (and (symbolp (car elem))
530                          (symbol-value (car elem))))
531             (throw 'found (cons (cadr elem) (caddr elem)))))))))
532
533 (defun gnus-inews-add-send-actions (winconf buffer article
534                                             &optional config yanked)
535   (make-local-hook 'message-sent-hook)
536   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
537                                  'gnus-inews-do-gcc) nil t)
538   (when gnus-agent
539     (make-local-hook 'message-header-hook)
540     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
541   (setq message-post-method
542         `(lambda (arg)
543            (gnus-post-method arg ,gnus-newsgroup-name)))
544   (setq message-user-agent (gnus-extended-version))
545   (when (not message-use-multi-frames)
546     (message-add-action
547      `(set-window-configuration ,winconf) 'exit 'postpone 'kill))
548   (let ((to-be-marked (cond
549                        (yanked yanked)
550                        (article (if (listp article) article (list article)))
551                        (t nil))))
552     (message-add-action
553      `(when (gnus-buffer-exists-p ,buffer)
554         (save-excursion
555           (set-buffer ,buffer)
556           ,(when to-be-marked
557              (if (eq config 'forward)
558                  `(gnus-summary-mark-article-as-forwarded ',to-be-marked)
559                `(gnus-summary-mark-article-as-replied ',to-be-marked)))))
560      'send)))
561
562 (put 'gnus-setup-message 'lisp-indent-function 1)
563 (put 'gnus-setup-message 'edebug-form-spec '(form body))
564
565 ;;; Post news commands of Gnus group mode and summary mode
566
567 (defun gnus-group-mail (&optional arg)
568   "Start composing a mail.
569 If ARG, use the group under the point to find a posting style.
570 If ARG is 1, prompt for a group name to find the posting style."
571   (interactive "P")
572   ;; We can't `let' gnus-newsgroup-name here, since that leads
573   ;; to local variables leaking.
574   (let ((group gnus-newsgroup-name)
575         ;; make sure last viewed article doesn't affect posting styles:
576         (gnus-article-copy)
577         (buffer (current-buffer)))
578     (unwind-protect
579         (progn
580           (setq gnus-newsgroup-name
581                 (if arg
582                     (if (= 1 (prefix-numeric-value arg))
583                         (completing-read "Use posting style of group: "
584                                          gnus-active-hashtb nil
585                                          (gnus-read-active-file-p))
586                       (gnus-group-group-name))
587                   ""))
588           ;; #### see comment in gnus-setup-message -- drv
589           (gnus-setup-message 'message (message-mail)))
590       (save-excursion
591         (set-buffer buffer)
592         (setq gnus-newsgroup-name group)))))
593
594 (defun gnus-group-news (&optional arg)
595   "Start composing a news.
596 If ARG, post to group under point.
597 If ARG is 1, prompt for group name to post to.
598
599 This function prepares a news even when using mail groups.  This is useful
600 for posting messages to mail groups without actually sending them over the
601 network.  The corresponding backend must have a 'request-post method."
602   (interactive "P")
603   ;; We can't `let' gnus-newsgroup-name here, since that leads
604   ;; to local variables leaking.
605   (let ((group gnus-newsgroup-name)
606         ;; make sure last viewed article doesn't affect posting styles:
607         (gnus-article-copy)
608         (buffer (current-buffer)))
609     (unwind-protect
610         (progn
611           (setq gnus-newsgroup-name
612                 (if arg
613                     (if (= 1 (prefix-numeric-value arg))
614                         (completing-read "Use group: "
615                                          gnus-active-hashtb nil
616                                          (gnus-read-active-file-p))
617                       (gnus-group-group-name))
618                   ""))
619           ;; #### see comment in gnus-setup-message -- drv
620           (gnus-setup-message 'message
621             (message-news (gnus-group-real-name gnus-newsgroup-name))))
622       (save-excursion
623         (set-buffer buffer)
624         (setq gnus-newsgroup-name group)))))
625
626 (defun gnus-group-post-news (&optional arg)
627   "Start composing a message (a news by default).
628 If ARG, post to group under point.  If ARG is 1, prompt for group name.
629 Depending on the selected group, the message might be either a mail or
630 a news."
631   (interactive "P")
632   ;; Bind this variable here to make message mode hooks work ok.
633   (let ((gnus-newsgroup-name
634          (if arg
635              (if (= 1 (prefix-numeric-value arg))
636                  (completing-read "Newsgroup: " gnus-active-hashtb nil
637                                   (gnus-read-active-file-p))
638                (gnus-group-group-name))
639            ""))
640         ;; make sure last viewed article doesn't affect posting styles:
641         (gnus-article-copy))
642     (gnus-post-news 'post gnus-newsgroup-name nil nil nil nil
643                     (string= gnus-newsgroup-name ""))))
644
645 (defun gnus-summary-mail-other-window (&optional arg)
646   "Start composing a mail in another window.
647 Use the posting of the current group by default.
648 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
649 posting style."
650   (interactive "P")
651   ;; We can't `let' gnus-newsgroup-name here, since that leads
652   ;; to local variables leaking.
653   (let ((group gnus-newsgroup-name)
654         ;; make sure last viewed article doesn't affect posting styles:
655         (gnus-article-copy)
656         (buffer (current-buffer)))
657     (unwind-protect
658         (progn
659           (setq gnus-newsgroup-name
660                 (if arg
661                     (if (= 1 (prefix-numeric-value arg))
662                         (completing-read "Use group: "
663                                          gnus-active-hashtb nil
664                                          (gnus-read-active-file-p))
665                       "")
666                   gnus-newsgroup-name))
667           ;; #### see comment in gnus-setup-message -- drv
668           (gnus-setup-message 'message (message-mail)))
669       (save-excursion
670         (set-buffer buffer)
671         (setq gnus-newsgroup-name group)))))
672
673 (defun gnus-summary-news-other-window (&optional arg)
674   "Start composing a news in another window.
675 Post to the current group by default.
676 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
677
678 This function prepares a news even when using mail groups.  This is useful
679 for posting messages to mail groups without actually sending them over the
680 network.  The corresponding backend must have a 'request-post method."
681   (interactive "P")
682   ;; We can't `let' gnus-newsgroup-name here, since that leads
683   ;; to local variables leaking.
684   (let ((group gnus-newsgroup-name)
685         ;; make sure last viewed article doesn't affect posting styles:
686         (gnus-article-copy)
687         (buffer (current-buffer)))
688     (unwind-protect
689         (progn
690           (setq gnus-newsgroup-name
691                 (if arg
692                     (if (= 1 (prefix-numeric-value arg))
693                         (completing-read "Use group: "
694                                          gnus-active-hashtb nil
695                                          (gnus-read-active-file-p))
696                       "")
697                   gnus-newsgroup-name))
698           ;; #### see comment in gnus-setup-message -- drv
699           (gnus-setup-message 'message
700             (message-news (gnus-group-real-name gnus-newsgroup-name))))
701       (save-excursion
702         (set-buffer buffer)
703         (setq gnus-newsgroup-name group)))))
704
705 (defun gnus-summary-post-news (&optional arg)
706   "Start composing a message.  Post to the current group by default.
707 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
708 Depending on the selected group, the message might be either a mail or
709 a news."
710   (interactive "P")
711   ;; Bind this variable here to make message mode hooks work ok.
712   (let ((gnus-newsgroup-name
713          (if arg
714              (if (= 1 (prefix-numeric-value arg))
715                  (completing-read "Newsgroup: " gnus-active-hashtb nil
716                                   (gnus-read-active-file-p))
717                "")
718            gnus-newsgroup-name))
719         ;; make sure last viewed article doesn't affect posting styles:
720         (gnus-article-copy))
721     (gnus-post-news 'post gnus-newsgroup-name)))
722
723
724 (defun gnus-summary-followup (yank &optional force-news)
725   "Compose a followup to an article.
726 If prefix argument YANK is non-nil, the original article is yanked
727 automatically.
728 YANK is a list of elements, where the car of each element is the
729 article number, and the two following numbers is the region to be
730 yanked."
731   (interactive
732    (list (and current-prefix-arg
733               (gnus-summary-work-articles 1))))
734   (when yank
735     (gnus-summary-goto-subject
736      (if (listp (car yank))
737          (caar yank)
738        (car yank))))
739   (save-window-excursion
740     (gnus-summary-select-article))
741   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
742         (gnus-newsgroup-name gnus-newsgroup-name))
743     ;; Send a followup.
744     (gnus-post-news nil gnus-newsgroup-name
745                     headers gnus-article-buffer
746                     yank nil force-news)
747     (gnus-summary-handle-replysign)))
748
749 (defun gnus-summary-followup-with-original (n &optional force-news)
750   "Compose a followup to an article and include the original article."
751   (interactive "P")
752   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
753
754 (defun gnus-summary-followup-to-mail (&optional arg)
755   "Followup to the current mail message via news."
756   (interactive
757    (list (and current-prefix-arg
758               (gnus-summary-work-articles 1))))
759   (gnus-summary-followup arg t))
760
761 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
762   "Followup to the current mail message via news."
763   (interactive "P")
764   (gnus-summary-followup (gnus-summary-work-articles arg) t))
765
766 (defun gnus-inews-yank-articles (articles)
767   (let (beg article yank-string
768             (more-than-one (cdr articles))
769             (cur (current-buffer))
770             refs window)
771     (message-goto-body)
772     (while (setq article (pop articles))
773       (when (listp article)
774         (setq yank-string (nth 1 article)
775               article (nth 0 article)))
776       (save-window-excursion
777         (set-buffer gnus-summary-buffer)
778         (gnus-summary-select-article nil nil nil article)
779         (gnus-summary-remove-process-mark article))
780
781       ;; Gathering references.
782       (when more-than-one
783         (setq refs (message-list-references
784                     refs
785                     (mail-header-references gnus-current-headers)
786                     (mail-header-message-id gnus-current-headers)))
787         (when message-use-multi-frames
788           (when (setq window (get-buffer-window cur t))
789             (select-frame (window-frame window)))))
790
791       (gnus-copy-article-buffer nil yank-string)
792       (let ((message-reply-buffer gnus-article-copy)
793             (message-reply-headers
794              ;; The headers are decoded.
795              (with-current-buffer gnus-article-copy
796                (save-restriction
797                  (nnheader-narrow-to-headers)
798                  (nnheader-parse-naked-head)))))
799         (message-yank-original)
800         (setq beg (or beg (mark t))))
801       (when articles
802         (insert "\n")))
803     (push-mark)
804
805     ;; Replace with the gathered references.
806     (when refs
807       (push-mark beg)
808       (save-restriction
809         (message-narrow-to-headers)
810         (let ((case-fold-search t))
811           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
812               (replace-match "")
813             (goto-char (point-max))))
814         (mail-header-format
815          (list (or (assq 'References message-header-format-alist)
816                    '(References . message-shorten-references)))
817          (list (cons 'References
818                      (mapconcat 'identity (nreverse refs) " "))))
819         (backward-delete-char 1))
820       (setq beg (mark t))
821       (pop-mark))
822
823     (goto-char beg)))
824
825 (defun gnus-summary-cancel-article (&optional n symp)
826   "Cancel an article you posted.
827 Uses the process-prefix convention.  If given the symbolic
828 prefix `a', cancel using the standard posting method; if not
829 post using the current select method."
830   (interactive (gnus-interactive "P\ny"))
831   (let ((articles (gnus-summary-work-articles n))
832         (message-post-method
833          `(lambda (arg)
834             (gnus-post-method (eq ',symp 'a) ,gnus-newsgroup-name)))
835         article)
836     (while (setq article (pop articles))
837       (when (gnus-summary-select-article t nil nil article)
838         (when (gnus-eval-in-buffer-window gnus-article-buffer
839                 (save-excursion
840                   (set-buffer gnus-original-article-buffer)
841                   (message-cancel-news)))
842           (gnus-summary-mark-as-read article gnus-canceled-mark)
843           (gnus-cache-remove-article 1))
844         (gnus-article-hide-headers-if-wanted))
845       (gnus-summary-remove-process-mark article))))
846
847 (defun gnus-summary-supersede-article ()
848   "Compose an article that will supersede a previous article.
849 This is done simply by taking the old article and adding a Supersedes
850 header line with the old Message-ID."
851   (interactive)
852   (let ((article (gnus-summary-article-number)))
853     (gnus-setup-message 'reply-yank
854       (gnus-summary-select-article t)
855       (set-buffer gnus-original-article-buffer)
856       (message-supersede)
857       (push
858        `((lambda ()
859            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
860              (save-excursion
861                (set-buffer ,gnus-summary-buffer)
862                (gnus-cache-possibly-remove-article ,article nil nil nil t)
863                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
864        message-send-actions))))
865
866 \f
867
868 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
869   ;; make a copy of the article buffer with all text properties removed
870   ;; this copy is in the buffer gnus-article-copy.
871   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
872   ;; this buffer should be passed to all mail/news reply/post routines.
873   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
874   (save-excursion
875     (set-buffer gnus-article-copy)
876     (set-buffer-multibyte t))
877   (let ((article-buffer (or article-buffer gnus-article-buffer))
878         end beg)
879     (if (not (and (get-buffer article-buffer)
880                   (gnus-buffer-exists-p article-buffer)))
881         (error "Can't find any article buffer")
882       (save-excursion
883         (set-buffer article-buffer)
884         (let ((gnus-newsgroup-charset (or gnus-article-charset
885                                           gnus-newsgroup-charset))
886               (gnus-newsgroup-ignored-charsets
887                (or gnus-article-ignored-charsets
888                    gnus-newsgroup-ignored-charsets)))
889           (save-restriction
890             ;; Copy over the (displayed) article buffer, delete
891             ;; hidden text and remove text properties.
892             (widen)
893             (copy-to-buffer gnus-article-copy (point-min) (point-max))
894             (set-buffer gnus-article-copy)
895             ;; There's invisible and intangible text in T-gnus.  Especially,
896             ;; if there is a boundary line (X-Boundary: ------------------),
897             ;; in the end of a header, it will cause a serious problem.
898             (add-text-properties (point-min) (point-max)
899                                  '(invisible nil intangible nil))
900             (when yank-string
901               (message-goto-body)
902               (delete-region (point) (point-max))
903               (insert yank-string))
904             ;; Encode bitmap smileys to ordinary text.
905             ;; Possibly, the original text might be restored.
906             (static-unless (featurep 'xemacs)
907               (when (featurep 'smiley-mule)
908                 (smiley-encode-buffer)))
909             (gnus-article-delete-text-of-type 'annotation)
910             (gnus-remove-text-with-property 'gnus-prev)
911             (gnus-remove-text-with-property 'gnus-next)
912             (gnus-remove-text-with-property 'gnus-decoration)
913             (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
914             (insert
915              (prog1
916                  (static-if (featurep 'xemacs)
917                      ;; Revome smiley extents for (possibly) XEmacs 21.1.
918                      (format "%s"
919                              (buffer-substring-no-properties (point-min)
920                                                              (point-max)))
921                    (buffer-substring-no-properties (point-min) (point-max)))
922                (erase-buffer)))
923             ;; Find the original headers.
924             (set-buffer gnus-original-article-buffer)
925             (goto-char (point-min))
926             (while (looking-at message-unix-mail-delimiter)
927               (forward-line 1))
928             (let ((mail-header-separator ""))
929               (setq beg (point)
930                     end (or (message-goto-body) beg)))
931             ;; Delete the headers from the displayed articles.
932             (set-buffer gnus-article-copy)
933             (let ((mail-header-separator ""))
934               (delete-region (goto-char (point-min))
935                              (or (message-goto-body) (point-max))))
936             ;; Insert the original article headers.
937             (insert-buffer-substring gnus-original-article-buffer beg end)
938             ;; Decode charsets.
939             (let ((gnus-article-decode-hook
940                    (delq 'article-decode-charset
941                          (copy-sequence gnus-article-decode-hook))))
942               ;; Needed for T-gnus.
943               (add-hook 'gnus-article-decode-hook
944                         'article-decode-encoded-words)
945               (run-hooks 'gnus-article-decode-hook)))))
946       gnus-article-copy)))
947
948 (defun gnus-post-news (post &optional group header article-buffer yank subject
949                             force-news)
950   (when article-buffer
951     (gnus-copy-article-buffer))
952   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
953         (gnus-article-yanked-articles yank)
954         (add-to-list gnus-add-to-list))
955     (gnus-setup-message (cond (yank 'reply-yank)
956                               (article-buffer 'reply)
957                               (t 'message))
958       (let* ((group (or group gnus-newsgroup-name))
959              (charset (gnus-group-name-charset nil group))
960              (pgroup group)
961              to-address to-group mailing-list to-list
962              newsgroup-p)
963         (when group
964           (setq to-address (gnus-parameter-to-address group)
965                 to-group (gnus-group-find-parameter group 'to-group)
966                 to-list (gnus-parameter-to-list group)
967                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
968                 mailing-list (when gnus-mailing-list-groups
969                                (string-match gnus-mailing-list-groups group))
970                 group (gnus-group-name-decode (gnus-group-real-name group)
971                                               charset)))
972         (if (or (and to-group
973                      (gnus-news-group-p to-group))
974                 newsgroup-p
975                 force-news
976                 (and (gnus-news-group-p
977                       (or pgroup gnus-newsgroup-name)
978                       (or header gnus-current-article))
979                      (not mailing-list)
980                      (not to-list)
981                      (not to-address)))
982             ;; This is news.
983             (if post
984                 (message-news (or to-group group))
985               (set-buffer gnus-article-copy)
986               (gnus-msg-treat-broken-reply-to)
987               (message-followup (if (or newsgroup-p force-news)
988                                     (if (save-restriction
989                                           (article-narrow-to-head)
990                                           (message-fetch-field "newsgroups"))
991                                         nil
992                                       "")
993                                   to-group)))
994           ;; The is mail.
995           (if post
996               (progn
997                 (message-mail (or to-address to-list))
998                 ;; Arrange for mail groups that have no `to-address' to
999                 ;; get that when the user sends off the mail.
1000                 (when (and (not to-list)
1001                            (not to-address)
1002                            add-to-list)
1003                   (push (list 'gnus-inews-add-to-address pgroup)
1004                         message-send-actions)))
1005             (set-buffer gnus-article-copy)
1006             (gnus-msg-treat-broken-reply-to)
1007             (message-wide-reply to-address)))
1008         (when yank
1009           (gnus-inews-yank-articles yank))))))
1010
1011 (defun gnus-msg-treat-broken-reply-to (&optional force)
1012   "Remove the Reply-to header if broken-reply-to."
1013   (when (or force
1014             (gnus-group-find-parameter
1015              gnus-newsgroup-name 'broken-reply-to))
1016     (save-restriction
1017       (message-narrow-to-head)
1018       (message-remove-header "reply-to"))))
1019
1020 (defun gnus-post-method (arg group &optional silent)
1021   "Return the posting method based on GROUP and ARG.
1022 If SILENT, don't prompt the user."
1023   (let ((gnus-post-method (or (gnus-parameter-post-method group)
1024                               gnus-post-method))
1025         (group-method (gnus-find-method-for-group group)))
1026     (cond
1027      ;; If the group-method is nil (which shouldn't happen) we use
1028      ;; the default method.
1029      ((null group-method)
1030       (or (and (listp gnus-post-method) ;If not current/native/nil
1031                (not (listp (car gnus-post-method))) ; and not a list of methods
1032                gnus-post-method)        ;then use it.
1033           gnus-select-method
1034           message-post-method))
1035      ;; We want the inverse of the default
1036      ((and arg (not (eq arg 0)))
1037       (if (eq gnus-post-method 'current)
1038           gnus-select-method
1039         group-method))
1040      ;; We query the user for a post method.
1041      ((or arg
1042           (and (listp gnus-post-method)
1043                (listp (car gnus-post-method))))
1044       (let* ((methods
1045               ;; Collect all methods we know about.
1046               (append
1047                (when (listp gnus-post-method)
1048                  (if (listp (car gnus-post-method))
1049                      gnus-post-method
1050                    (list gnus-post-method)))
1051                gnus-secondary-select-methods
1052                (mapcar 'cdr gnus-server-alist)
1053                (mapcar 'car gnus-opened-servers)
1054                (list gnus-select-method)
1055                (list group-method)))
1056              method-alist post-methods method)
1057         ;; Weed out all mail methods.
1058         (while methods
1059           (setq method (gnus-server-get-method "" (pop methods)))
1060           (when (and (or (gnus-method-option-p method 'post)
1061                          (gnus-method-option-p method 'post-mail))
1062                      (not (member method post-methods)))
1063             (push method post-methods)))
1064         ;; Create a name-method alist.
1065         (setq method-alist
1066               (mapcar
1067                (lambda (m)
1068                  (if (equal (cadr m) "")
1069                      (list (symbol-name (car m)) m)
1070                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
1071                post-methods))
1072         ;; Query the user.
1073         (cadr
1074          (assoc
1075           (setq gnus-last-posting-server
1076                 (if (and silent
1077                          gnus-last-posting-server)
1078                     ;; Just use the last value.
1079                     gnus-last-posting-server
1080                   (completing-read
1081                    "Posting method: " method-alist nil t
1082                    (cons (or gnus-last-posting-server "") 0))))
1083           method-alist))))
1084      ;; Override normal method.
1085      ((and (eq gnus-post-method 'current)
1086            (not (memq (car group-method) gnus-discouraged-post-methods))
1087            (gnus-get-function group-method 'request-post t))
1088       (assert (not arg))
1089       group-method)
1090      ;; Use gnus-post-method.
1091      ((listp gnus-post-method)          ;A method...
1092       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
1093       gnus-post-method)
1094      ;; Use the normal select method (nil or native).
1095      (t gnus-select-method))))
1096
1097 \f
1098 (defun gnus-message-make-user-agent (&optional include-mime-info max-column
1099                                                  newline-product)
1100   "Return a user-agent info.  If INCLUDE-MIME-INFO is non-nil and the
1101 variable `mime-edit-user-agent-value' is bound, the value will be
1102 included in the return value.  If MAX-COLUMN is specified, the return
1103 value will be folded up as it were filled.  NEWLINE-PRODUCT specifies
1104 whether a newline should be inserted in front of each product-token.
1105 If the value is t or `hard', it works strictly.  Otherwise, if it is
1106 non-nil (e.g. `soft'), it works semi-strictly.
1107
1108 Here is an example of how to use this function:
1109
1110 \(add-hook 'gnus-message-setup-hook
1111           (lambda nil
1112             (setq message-user-agent nil)
1113             (save-excursion
1114               (save-restriction
1115                 (message-narrow-to-headers)
1116                 (goto-char (point-max))
1117                 (insert \"User-Agent: \"
1118                         (gnus-message-make-user-agent t 76 'soft)
1119                         \"\\n\")))))
1120 "
1121   (let ((user-agent (if (and include-mime-info
1122                              (boundp 'mime-edit-user-agent-value))
1123                         (concat (gnus-extended-version)
1124                                 " "
1125                                 mime-edit-user-agent-value)
1126                       (gnus-extended-version))))
1127     (when max-column
1128       (unless (natnump max-column)
1129         (setq max-column 76))
1130       (with-temp-buffer
1131         (set-buffer-multibyte t)
1132         (insert (mapconcat 'identity (split-string user-agent) " "))
1133         (goto-char (point-min))
1134         (let ((bol t)
1135               start agent agents width element swidth)
1136           (while (re-search-forward "\\([^ ]+\\) ?" nil t)
1137             (setq start (match-beginning 0))
1138             (if (eq (char-after start) ?\()
1139                 (progn
1140                   (goto-char start)
1141                   (forward-list)
1142                   (push (buffer-substring start (point)) agent))
1143               (when agent
1144                 (push (nreverse agent) agents))
1145               (setq agent (list (match-string 1)))))
1146           (when agent
1147             (push (nreverse agent) agents))
1148           (setq agents (nreverse agents))
1149           (if (> (+ 12 (string-width (caar agents))) max-column)
1150               (setq user-agent "\n"
1151                     width 0)
1152             (setq user-agent ""
1153                   width 11))
1154           (while agents
1155             (setq agent (car agents)
1156                   agents (cdr agents))
1157             (when (and (not bol)
1158                        (or (memq newline-product '(t hard))
1159                            (and newline-product
1160                                 (> (+ width 1
1161                                       (string-width (mapconcat 'identity
1162                                                                agent " ")))
1163                                    max-column))))
1164               (setq user-agent (concat user-agent "\n")
1165                     width 0
1166                     bol t))
1167             (while agent
1168               (setq element (car agent)
1169                     swidth (string-width element)
1170                     agent (cdr agent))
1171               (if bol
1172                   (setq user-agent (if (member user-agent '("" "\n"))
1173                                        (concat user-agent element)
1174                                      (concat user-agent " " element))
1175                         width (+ width 1 swidth)
1176                         bol nil)
1177                 (if (> (+ width 1 swidth) max-column)
1178                     (setq user-agent (concat user-agent "\n " element)
1179                           width (1+ swidth))
1180                   (setq user-agent (concat user-agent " " element)
1181                         width (+ width 1 swidth)))))))))
1182     user-agent))
1183
1184 \f
1185 ;;;
1186 ;;; Gnus Mail Functions
1187 ;;;
1188
1189 ;;; Mail reply commands of Gnus summary mode
1190
1191 (defun gnus-summary-reply (&optional yank wide very-wide)
1192   "Start composing a mail reply to the current message.
1193 If prefix argument YANK is non-nil, the original article is yanked
1194 automatically.
1195 If WIDE, make a wide reply.
1196 If VERY-WIDE, make a very wide reply."
1197   (interactive
1198    (list (and current-prefix-arg
1199               (gnus-summary-work-articles 1))))
1200   ;; Allow user to require confirmation before replying by mail to the
1201   ;; author of a news article (or mail message).
1202   (when (or 
1203             (not (or (gnus-news-group-p gnus-newsgroup-name)
1204                      gnus-confirm-treat-mail-like-news))
1205             (not (cond ((stringp gnus-confirm-mail-reply-to-news)
1206                         (string-match gnus-confirm-mail-reply-to-news
1207                                       gnus-newsgroup-name))
1208                        ((functionp gnus-confirm-mail-reply-to-news)
1209                         (funcall gnus-confirm-mail-reply-to-news gnus-newsgroup-name))
1210                        (t gnus-confirm-mail-reply-to-news)))
1211             (y-or-n-p "Really reply by mail to article author? "))
1212     (let* ((article
1213             (if (listp (car yank))
1214                 (caar yank)
1215               (car yank)))
1216            (gnus-article-reply (or article (gnus-summary-article-number)))
1217            (gnus-article-yanked-articles yank)
1218            (headers ""))
1219       ;; Stripping headers should be specified with mail-yank-ignored-headers.
1220       (when yank
1221         (gnus-summary-goto-subject article))
1222       (gnus-setup-message (if yank 'reply-yank 'reply)
1223         (if (not very-wide)
1224             (gnus-summary-select-article)
1225           (dolist (article very-wide)
1226             (gnus-summary-select-article nil nil nil article)
1227             (save-excursion
1228               (set-buffer (gnus-copy-article-buffer))
1229               (gnus-msg-treat-broken-reply-to)
1230               (save-restriction
1231                 (message-narrow-to-head)
1232                 (setq headers (concat headers (buffer-string)))))))
1233         (set-buffer (gnus-copy-article-buffer))
1234         (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
1235         (save-restriction
1236           (message-narrow-to-head)
1237           (when very-wide
1238             (erase-buffer)
1239             (insert headers))
1240           (goto-char (point-max)))
1241         (message-reply nil wide)
1242         (when yank
1243           (gnus-inews-yank-articles yank))
1244         (gnus-summary-handle-replysign)))))
1245
1246 (defun gnus-summary-handle-replysign ()
1247   "Check the various replysign variables and take action accordingly."
1248   (when nil;;(or gnus-message-replysign gnus-message-replyencrypt)
1249     (let (signed encrypted)
1250       (save-excursion
1251         (set-buffer gnus-article-buffer)
1252         (setq signed (memq 'signed gnus-article-wash-types))
1253         (setq encrypted (memq 'encrypted gnus-article-wash-types)))
1254       (cond ((and gnus-message-replyencrypt encrypted)
1255              (mml-secure-message mml-default-encrypt-method
1256                                  (if gnus-message-replysignencrypted
1257                                      'signencrypt
1258                                    'encrypt)))
1259             ((and gnus-message-replysign signed)
1260              (mml-secure-message mml-default-sign-method 'sign))))))
1261
1262 (defun gnus-summary-reply-with-original (n &optional wide)
1263   "Start composing a reply mail to the current message.
1264 The original article will be yanked."
1265   (interactive "P")
1266   (gnus-summary-reply (gnus-summary-work-articles n) wide))
1267
1268 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
1269   "Like `gnus-summary-reply' except removing reply-to field.
1270 If prefix argument YANK is non-nil, the original article is yanked
1271 automatically.
1272 If WIDE, make a wide reply.
1273 If VERY-WIDE, make a very wide reply."
1274   (interactive
1275    (list (and current-prefix-arg
1276               (gnus-summary-work-articles 1))))
1277   (let ((gnus-msg-force-broken-reply-to t))
1278     (gnus-summary-reply yank wide very-wide)))
1279
1280 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
1281   "Like `gnus-summary-reply-with-original' except removing reply-to field.
1282 The original article will be yanked."
1283   (interactive "P")
1284   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
1285
1286 (defun gnus-summary-wide-reply (&optional yank)
1287   "Start composing a wide reply mail to the current message.
1288 If prefix argument YANK is non-nil, the original article is yanked
1289 automatically."
1290   (interactive
1291    (list (and current-prefix-arg
1292               (gnus-summary-work-articles 1))))
1293   (gnus-summary-reply yank t))
1294
1295 (defun gnus-summary-wide-reply-with-original (n)
1296   "Start composing a wide reply mail to the current message.
1297 The original article will be yanked."
1298   (interactive "P")
1299   (gnus-summary-reply-with-original n t))
1300
1301 (defun gnus-summary-very-wide-reply (&optional yank)
1302   "Start composing a very wide reply mail to the current message.
1303 If prefix argument YANK is non-nil, the original article is yanked
1304 automatically."
1305   (interactive
1306    (list (and current-prefix-arg
1307               (gnus-summary-work-articles 1))))
1308   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
1309
1310 (defun gnus-summary-very-wide-reply-with-original (n)
1311   "Start composing a very wide reply mail to the current message.
1312 The original article will be yanked."
1313   (interactive "P")
1314   (gnus-summary-reply
1315    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1316
1317 (defun gnus-summary-mail-forward (&optional full-headers post)
1318   "Forward the current message(s) to another user.
1319 If process marks exist, forward all marked messages;
1320 If FULL-HEADERS (the prefix), include full headers when forwarding.
1321
1322 Note that this function definition for T-gnus is totally different
1323 from the original Gnus."
1324   (interactive "P")
1325   (if (cdr (gnus-summary-work-articles nil))
1326       ;; Process marks are given.
1327       (gnus-summary-digest-mail-forward nil post)
1328     ;; No process marks.
1329     (let* ((gnus-article-reply (gnus-summary-article-number))
1330            (gnus-article-yanked-articles (list (list gnus-article-reply)))
1331            charset
1332            (message-included-forward-headers
1333             (if full-headers "" message-included-forward-headers)))
1334       (gnus-setup-message 'forward
1335         (gnus-summary-select-article)
1336         (setq charset default-mime-charset)
1337         (set-buffer gnus-original-article-buffer)
1338         (make-local-variable 'default-mime-charset)
1339         (setq default-mime-charset charset)
1340         (message-forward post)))))
1341
1342 (defun gnus-summary-digest-mail-forward (&optional n post)
1343   "Digests and forwards all articles in this series.
1344 If N is a positive number, forward the N next articles.
1345 If N is a negative number, forward the N previous articles.
1346 If N is nil and any articles have been marked with the process mark,
1347 forward those articles instead.
1348 Optional POST will use news to forward instead of mail."
1349   (interactive "P")
1350   (let ((articles (gnus-summary-work-articles n))
1351         (topics "Topics:\n")
1352         subject article frame)
1353     (when (car articles)
1354       (gnus-setup-message 'forward
1355         (gnus-summary-select-article)
1356         (if (cdr articles)
1357             (setq articles (sort articles '<)
1358                   subject "Digested Articles")
1359           (with-current-buffer gnus-original-article-buffer
1360             (setq subject (message-make-forward-subject))))
1361         (if post
1362             (message-news nil subject)
1363           (message-mail nil subject))
1364         (when (and message-use-multi-frames (cdr articles))
1365           (setq frame (window-frame (get-buffer-window (current-buffer)))))
1366         (message-goto-body)
1367         (while (setq article (pop articles))
1368           (save-window-excursion
1369             (set-buffer gnus-summary-buffer)
1370             (gnus-summary-select-article nil nil nil article)
1371             (setq topics (concat topics "    "
1372                                  (mail-header-subject gnus-current-headers)
1373                                  "\n"))
1374             (gnus-summary-remove-process-mark article))
1375           (when frame
1376             (select-frame frame))
1377           (insert (mime-make-tag "message" "rfc822") "\n")
1378           (narrow-to-region (point) (point))
1379           (insert-buffer-substring gnus-original-article-buffer)
1380           (save-restriction
1381             (article-narrow-to-head)
1382             (message-remove-header message-included-forward-headers t nil t))
1383           (goto-char (point-max))
1384           (widen))
1385         (push-mark)
1386         (message-goto-body)
1387         (insert topics)
1388         (message-goto-body)
1389         (mime-edit-enclose-digest-region (point)(mark t))))))
1390
1391 (defun gnus-summary-digest-post-forward (&optional n)
1392   "Digest and forwards all articles in this series to a newsgroup.
1393 If N is a positive number, forward the N next articles.
1394 If N is a negative number, forward the N previous articles.
1395 If N is nil and any articles have been marked with the process mark,
1396 forward those articles instead."
1397   (interactive "P")
1398   (gnus-summary-digest-mail-forward n t))
1399
1400 (defun gnus-summary-resend-message (address n)
1401   "Resend the current article to ADDRESS."
1402   (interactive
1403    (list (message-read-from-minibuffer
1404           "Resend message(s) to: "
1405           (when (and gnus-summary-resend-default-address
1406                      (gnus-buffer-live-p gnus-original-article-buffer))
1407             ;; If some other article is currently selected, the
1408             ;; initial-contents is wrong. Whatever, it is just the
1409             ;; initial-contents.
1410             (with-current-buffer gnus-original-article-buffer
1411               (nnmail-fetch-field "to"))))
1412          current-prefix-arg))
1413   (let ((articles (gnus-summary-work-articles n))
1414         article)
1415     (while (setq article (pop articles))
1416       (gnus-summary-select-article nil nil nil article)
1417       (save-excursion
1418         (set-buffer gnus-original-article-buffer)
1419         (message-resend address))
1420       (gnus-summary-mark-article-as-forwarded article))))
1421
1422 ;; From: Matthieu Moy <Matthieu.Moy@imag.fr>
1423 (defun gnus-summary-resend-message-edit ()
1424   "Resend an article that has already been sent.
1425 A new buffer will be created to allow the user to modify body and
1426 contents of the message, and then, everything will happen as when
1427 composing a new message."
1428   (interactive)
1429   (let ((article (gnus-summary-article-number)))
1430     (gnus-setup-message 'reply-yank
1431       (gnus-summary-select-article t)
1432       (set-buffer gnus-original-article-buffer)
1433       (let ((cur (current-buffer))
1434             (to (message-fetch-field "to")))
1435         ;; Get a normal message buffer.
1436         (message-pop-to-buffer (message-buffer-name "Resend" to))
1437         (insert-buffer-substring cur)
1438
1439         ;; T-gnus change: Use MIME-Edit to recompose a message.
1440         ;;(mime-to-mml)
1441         (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
1442           (fset 'mime-edit-decode-single-part-in-buffer
1443                 (lambda (&rest args)
1444                   (if (let ((content-type (car args)))
1445                         (and (eq 'message (mime-content-type-primary-type
1446                                            content-type))
1447                              (eq 'rfc822 (mime-content-type-subtype
1448                                           content-type))))
1449                       (setcar (cdr args) 'not-decode-text))
1450                   (apply ofn args)))
1451           (unwind-protect
1452               (mime-edit-again nil t)
1453             (fset 'mime-edit-decode-single-part-in-buffer ofn)))
1454         (message-narrow-to-head-1)
1455         (insert "From: " (message-make-from) "\n")
1456         (while (re-search-forward "^From:" nil t)
1457           (beginning-of-line)
1458           (insert "Original-"))
1459         (message-remove-header "^>From[\t ]" t)
1460
1461         ;; Gnus will generate a new one when sending.
1462         (message-remove-header "Message-ID")
1463         (message-remove-header message-ignored-resent-headers t)
1464         ;; Remove unwanted headers.
1465         (goto-char (point-max))
1466         (insert mail-header-separator)
1467         (goto-char (point-min))
1468         (re-search-forward "^To:\\|^Newsgroups:" nil 'move)
1469         (forward-char 1)
1470         (widen)))))
1471
1472 (defun gnus-summary-post-forward (&optional full-headers)
1473   "Forward the current article to a newsgroup.
1474 If FULL-HEADERS (the prefix), include full headers when forwarding."
1475   (interactive "P")
1476   (gnus-summary-mail-forward full-headers t))
1477
1478 (defvar gnus-nastygram-message
1479   "The following article was inappropriately posted to %s.\n\n"
1480   "Format string to insert in nastygrams.
1481 The current group name will be inserted at \"%s\".")
1482
1483 (defun gnus-summary-mail-nastygram (n)
1484   "Send a nastygram to the author of the current article."
1485   (interactive "P")
1486   (when (or gnus-expert-user
1487             (gnus-y-or-n-p
1488              "Really send a nastygram to the author of the current article? "))
1489     (let ((group gnus-newsgroup-name))
1490       (gnus-summary-reply-with-original n)
1491       (set-buffer gnus-message-buffer)
1492       (message-goto-body)
1493       (insert (format gnus-nastygram-message group))
1494       (message-send-and-exit))))
1495
1496 (defun gnus-summary-mail-crosspost-complaint (n)
1497   "Send a complaint about crossposting to the current article(s)."
1498   (interactive "P")
1499   (let ((articles (gnus-summary-work-articles n))
1500         article)
1501     (while (setq article (pop articles))
1502       (set-buffer gnus-summary-buffer)
1503       (gnus-summary-goto-subject article)
1504       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1505             newsgroups followup-to)
1506         (gnus-summary-select-article)
1507         (set-buffer gnus-original-article-buffer)
1508         (if (and (<= (length (message-tokenize-header
1509                               (setq newsgroups
1510                                     (mail-fetch-field "newsgroups"))
1511                               ", "))
1512                      1)
1513                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1514                      (not (member group (message-tokenize-header
1515                                          followup-to ", ")))))
1516             (if followup-to
1517                 (gnus-message 1 "Followup-to restricted")
1518               (gnus-message 1 "Not a crossposted article"))
1519           (set-buffer gnus-summary-buffer)
1520           (gnus-summary-reply-with-original 1)
1521           (set-buffer gnus-message-buffer)
1522           (message-goto-body)
1523           (insert (format gnus-crosspost-complaint newsgroups group))
1524           (message-goto-subject)
1525           (re-search-forward " *$")
1526           (replace-match " (crosspost notification)" t t)
1527           (gnus-deactivate-mark)
1528           (when (gnus-y-or-n-p "Send this complaint? ")
1529             (message-send-and-exit)))))))
1530
1531 (defun gnus-mail-parse-comma-list ()
1532   (let (accumulated
1533         beg)
1534     (skip-chars-forward " ")
1535     (while (not (eobp))
1536       (setq beg (point))
1537       (skip-chars-forward "^,")
1538       (while (zerop
1539               (save-excursion
1540                 (save-restriction
1541                   (let ((i 0))
1542                     (narrow-to-region beg (point))
1543                     (goto-char beg)
1544                     (logand (progn
1545                               (while (search-forward "\"" nil t)
1546                                 (incf i))
1547                               (if (zerop i) 2 i))
1548                             2)))))
1549         (skip-chars-forward ",")
1550         (skip-chars-forward "^,"))
1551       (skip-chars-backward " ")
1552       (push (buffer-substring beg (point))
1553             accumulated)
1554       (skip-chars-forward "^,")
1555       (skip-chars-forward ", "))
1556     accumulated))
1557
1558 (defun gnus-inews-add-to-address (group)
1559   (let ((to-address (mail-fetch-field "to")))
1560     (when (and to-address
1561                (gnus-alive-p))
1562       ;; This mail group doesn't have a `to-list', so we add one
1563       ;; here.  Magic!
1564       (when (gnus-y-or-n-p
1565              (format "Do you want to add this as `to-list': %s " to-address))
1566         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1567
1568 (defun gnus-put-message ()
1569   "Put the current message in some group and return to Gnus."
1570   (interactive)
1571   (let ((reply gnus-article-reply)
1572         (winconf gnus-prev-winconf)
1573         (group gnus-newsgroup-name))
1574     (unless (and group
1575                  (not (gnus-group-read-only-p group)))
1576       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1577
1578     (when (gnus-gethash group gnus-newsrc-hashtb)
1579       (error "No such group: %s" group))
1580     (save-excursion
1581       (save-restriction
1582         (widen)
1583         (message-narrow-to-headers)
1584         (let ((gnus-deletable-headers nil))
1585           (message-generate-headers
1586            (if (message-news-p)
1587                message-required-news-headers
1588              message-required-mail-headers)))
1589         (goto-char (point-max))
1590         (if (string-match " " group)
1591             (insert "Gcc: \"" group "\"\n")
1592           (insert "Gcc: " group "\n"))
1593         (widen)))
1594     (gnus-inews-do-gcc)
1595     (when (and (get-buffer gnus-group-buffer)
1596                (gnus-buffer-exists-p (car-safe reply))
1597                (cdr reply))
1598       (set-buffer (car reply))
1599       (gnus-summary-mark-article-as-replied (cdr reply)))
1600     (when winconf
1601       (set-window-configuration winconf))))
1602
1603 (defun gnus-article-mail (yank)
1604   "Send a reply to the address near point.
1605 If YANK is non-nil, include the original article."
1606   (interactive "P")
1607   (let ((address
1608          (buffer-substring
1609           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1610           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1611     (when address
1612       (gnus-msg-mail address)
1613       (when yank
1614         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1615
1616 (defvar nntp-server-type)
1617 (defun gnus-bug ()
1618   "Send a bug report to the Gnus maintainers."
1619   (interactive)
1620   (unless (gnus-alive-p)
1621     (error "Gnus has been shut down"))
1622   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1623     (unless (message-mail-user-agent)
1624       (message-pop-to-buffer "*Gnus Bug*")
1625       (delete-other-windows)
1626       (when gnus-bug-create-help-buffer
1627         (switch-to-buffer "*Gnus Help Bug*")
1628         (erase-buffer)
1629         (insert gnus-bug-message)
1630         (goto-char (point-min))
1631         (sit-for 0)
1632         (set-buffer "*Gnus Bug*")))
1633     (let ((message-this-is-mail t))
1634       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1635     (when gnus-bug-create-help-buffer
1636       (push `(gnus-bug-kill-buffer) message-send-actions))
1637     (goto-char (point-min))
1638     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1639     (forward-line 1)
1640     (insert gnus-product-name " " gnus-version-number
1641             " (r" gnus-revision-number ") "
1642             "based on " gnus-original-product-name " v"
1643             gnus-original-version-number "\n"
1644             (emacs-version) "\n")
1645     (when (and (boundp 'nntp-server-type)
1646                (stringp nntp-server-type))
1647       (insert nntp-server-type))
1648     (insert "\n\n\n\n\n")
1649     (let (mime-content-types)
1650       (mime-edit-insert-tag
1651        "application" "emacs-lisp"
1652        "\nContent-Disposition: inline\nContent-Description: User settings"))
1653     (insert (with-temp-buffer
1654               (gnus-debug)
1655               (buffer-string)))
1656     (let (mime-content-types)
1657       (mime-edit-insert-tag "text" "plain"))
1658     (goto-char (point-min))
1659     (search-forward "Subject: " nil t)
1660     (message "")))
1661
1662 (defun gnus-bug-kill-buffer ()
1663   (when (get-buffer "*Gnus Help Bug*")
1664     (kill-buffer "*Gnus Help Bug*")))
1665
1666 (defun gnus-summary-yank-message (buffer n)
1667   "Yank the current article into a composed message."
1668   (interactive
1669    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1670          current-prefix-arg))
1671   (when (gnus-buffer-live-p buffer)
1672     (let ((summary-frame (selected-frame))
1673           (message-frame (when (static-if (featurep 'xemacs)
1674                                    (device-on-window-system-p)
1675                                  window-system)
1676                            (let ((window (get-buffer-window buffer t)))
1677                              (when window
1678                                (window-frame window)))))
1679           (separator (concat "^" (regexp-quote mail-header-separator)
1680                              "\n")))
1681       (gnus-summary-iterate n
1682         (gnus-summary-select-article)
1683         (gnus-copy-article-buffer)
1684         (when (frame-live-p message-frame)
1685           (raise-frame message-frame)
1686           (select-frame message-frame))
1687         (with-current-buffer buffer
1688           (when (save-excursion
1689                   (beginning-of-line)
1690                   (let (case-fold-search)
1691                     (and (not (re-search-backward separator nil t))
1692                          (re-search-forward separator nil t))))
1693             (goto-char (match-end 0)))
1694           (message-yank-buffer gnus-article-copy))
1695         (select-frame summary-frame))
1696       (when (frame-live-p message-frame)
1697         (select-frame message-frame)))))
1698
1699 (defun gnus-debug ()
1700   "Attempts to go through the Gnus source file and report what variables have been changed.
1701 The source file has to be in the Emacs load path."
1702   (interactive)
1703   (let ((files gnus-debug-files)
1704         (point (point))
1705         file expr olist sym)
1706     (gnus-message 4 "Please wait while we snoop your variables...")
1707     ;; Go through all the files looking for non-default values for variables.
1708     (save-excursion
1709       (sit-for 0)
1710       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1711       (while files
1712         (erase-buffer)
1713         (when (and (setq file (locate-library (pop files)))
1714                    (file-exists-p file))
1715           (insert-file-contents file)
1716           (goto-char (point-min))
1717           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1718               (gnus-message 4 "Malformed sources in file %s" file)
1719             (narrow-to-region (point-min) (point))
1720             (goto-char (point-min))
1721             (while (setq expr (ignore-errors (read (current-buffer))))
1722               (ignore-errors
1723                 (and (memq (car expr) '(defvar defcustom defvoo))
1724                      (stringp (nth 3 expr))
1725                      (not (memq (nth 1 expr) gnus-debug-exclude-variables))
1726                      (or (not (boundp (nth 1 expr)))
1727                          (not (equal (eval (nth 2 expr))
1728                                      (symbol-value (nth 1 expr)))))
1729                      (push (nth 1 expr) olist)))))))
1730       (kill-buffer (current-buffer)))
1731     (when (setq olist (nreverse olist))
1732       (insert ";----------------- Environment follows ------------------\n\n"))
1733     (while olist
1734       (if (boundp (car olist))
1735           (ignore-errors
1736             (pp `(setq ,(car olist)
1737                        ,(if (or (consp (setq sym (symbol-value (car olist))))
1738                                 (and (symbolp sym)
1739                                      (not (or (eq sym nil)
1740                                               (eq sym t)))))
1741                             (list 'quote (symbol-value (car olist)))
1742                           (symbol-value (car olist))))
1743                 (current-buffer)))
1744         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1745       (setq olist (cdr olist)))
1746     ;; Remove any control chars - they seem to cause trouble for some
1747     ;; mailers.  (Byte-compiled output from the stuff above.)
1748     (goto-char point)
1749     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1750       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1751                      t t))
1752     ;; Break MIME tags purposely.
1753     (goto-char point)
1754     (while (re-search-forward mime-edit-tag-regexp nil t)
1755       (goto-char (1+ (match-beginning 0)))
1756       (insert "X"))))
1757
1758 ;;; Treatment of rejected articles.
1759 ;;; Bounced mail.
1760
1761 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1762   "Re-mail the current message.
1763 This only makes sense if the current message is a bounce message than
1764 contains some mail you have written which has been bounced back to
1765 you.
1766 If FETCH, try to fetch the article that this is a reply to, if indeed
1767 this is a reply."
1768   (interactive "P")
1769   (gnus-summary-select-article t)
1770   (set-buffer gnus-original-article-buffer)
1771   (gnus-setup-message 'compose-bounce
1772     (let* ((references (mail-fetch-field "references"))
1773            (parent (and references (gnus-parent-id references))))
1774       (message-bounce)
1775       ;; If there are references, we fetch the article we answered to.
1776       (and fetch parent
1777            (gnus-summary-refer-article parent)
1778            (gnus-summary-show-all-headers)))))
1779
1780 ;;; Gcc handling.
1781
1782 (defun gnus-inews-group-method (group)
1783   (cond
1784    ;; If the group doesn't exist, we assume
1785    ;; it's an archive group...
1786    ((and (null (gnus-get-info group))
1787          (eq (car (gnus-server-to-method gnus-message-archive-method))
1788              (car (gnus-server-to-method (gnus-group-method group)))))
1789     gnus-message-archive-method)
1790    ;; Use the method.
1791    ((gnus-info-method (gnus-get-info group))
1792     (gnus-info-method (gnus-get-info group)))
1793    ;; Find the method.
1794    (t (gnus-server-to-method (gnus-group-method group)))))
1795
1796 ;; Do Gcc handling, which copied the message over to some group.
1797 (defun gnus-inews-do-gcc (&optional gcc)
1798   (interactive)
1799   (save-excursion
1800     (save-restriction
1801       (message-narrow-to-headers)
1802       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1803             (coding-system-for-write 'raw-text)
1804             (output-coding-system 'raw-text)
1805             groups group method group-art
1806             mml-externalize-attachments)
1807         (when gcc
1808           (message-remove-header "gcc")
1809           (widen)
1810           (setq groups (message-unquote-tokens
1811                         (message-tokenize-header gcc " ,")))
1812           ;; Copy the article over to some group(s).
1813           (while (setq group (pop groups))
1814             (unless (gnus-check-server
1815                      (setq method (gnus-inews-group-method group)))
1816               (error "Can't open server %s" (if (stringp method) method
1817                                               (car method))))
1818             (unless (gnus-request-group group nil method)
1819               (gnus-request-create-group group method))
1820             (setq mml-externalize-attachments
1821                   (if (stringp gnus-gcc-externalize-attachments)
1822                       (string-match gnus-gcc-externalize-attachments group)
1823                     gnus-gcc-externalize-attachments))
1824             (save-excursion
1825               (nnheader-set-temp-buffer " *acc*")
1826               (insert-buffer-substring message-encoding-buffer)
1827               (gnus-run-hooks 'gnus-before-do-gcc-hook)
1828               (goto-char (point-min))
1829               (when (re-search-forward
1830                      (concat "^" (regexp-quote mail-header-separator) "$")
1831                      nil t)
1832                 (replace-match "" t t ))
1833               (unless (setq group-art
1834                             (gnus-request-accept-article group method t t))
1835                 (gnus-message 1 "Couldn't store article in group %s: %s"
1836                               group (gnus-status-message method))
1837                 (sit-for 2))
1838               (when (and group-art
1839                          (gnus-alive-p)
1840                          (or gnus-gcc-mark-as-read
1841                              gnus-inews-mark-gcc-as-read))
1842                 (gnus-group-mark-article-read group (cdr group-art)))
1843               (kill-buffer (current-buffer)))))))))
1844
1845 (defun gnus-inews-insert-gcc ()
1846   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1847   (save-excursion
1848     (save-restriction
1849       (message-narrow-to-headers)
1850       (let* ((group gnus-outgoing-message-group)
1851              (gcc (cond
1852                    ((gnus-functionp group)
1853                     (funcall group))
1854                    ((or (stringp group) (list group))
1855                     group))))
1856         (when gcc
1857           (insert "Gcc: "
1858                   (if (stringp gcc)
1859                       (if (string-match " " gcc)
1860                           (concat "\"" gcc "\"")
1861                         gcc)
1862                     (mapconcat (lambda (group)
1863                                  (if (string-match " " group)
1864                                      (concat "\"" group "\"")
1865                                    group))
1866                                gcc " "))
1867                   "\n"))))))
1868
1869 (defun gnus-inews-insert-archive-gcc (&optional group)
1870   "Insert the Gcc to say where the article is to be archived."
1871   (let* ((var gnus-message-archive-group)
1872          (group (or group gnus-newsgroup-name ""))
1873          (gcc-self-val
1874           (and gnus-newsgroup-name
1875                (not (equal gnus-newsgroup-name ""))
1876                (gnus-group-find-parameter
1877                 gnus-newsgroup-name 'gcc-self)))
1878          result
1879          (groups
1880           (cond
1881            ((null gnus-message-archive-method)
1882             ;; Ignore.
1883             nil)
1884            ((stringp var)
1885             ;; Just a single group.
1886             (list var))
1887            ((null var)
1888             ;; We don't want this.
1889             nil)
1890            ((and (listp var) (stringp (car var)))
1891             ;; A list of groups.
1892             var)
1893            ((gnus-functionp var)
1894             ;; A function.
1895             (funcall var group))
1896            (t
1897             ;; An alist of regexps/functions/forms.
1898             (while (and var
1899                         (not
1900                          (setq result
1901                                (cond
1902                                 ((stringp (caar var))
1903                                  ;; Regexp.
1904                                  (when (string-match (caar var) group)
1905                                    (cdar var)))
1906                                 ((gnus-functionp (car var))
1907                                  ;; Function.
1908                                  (funcall (car var) group))
1909                                 (t
1910                                  (eval (car var)))))))
1911               (setq var (cdr var)))
1912             result)))
1913          name)
1914     (when (or groups gcc-self-val)
1915       (when (stringp groups)
1916         (setq groups (list groups)))
1917       (save-excursion
1918         (save-restriction
1919           (message-narrow-to-headers)
1920           (goto-char (point-max))
1921           (insert "Gcc: ")
1922           (if gcc-self-val
1923               ;; Use the `gcc-self' param value instead.
1924               (progn
1925                 (insert
1926                  (if (stringp gcc-self-val)
1927                      (if (string-match " " gcc-self-val)
1928                          (concat "\"" gcc-self-val "\"")
1929                        gcc-self-val)
1930                    (if (string-match " " group)
1931                        (concat "\"" group "\"")
1932                      group)))
1933                 (if (not (eq gcc-self-val 'none))
1934                     (insert "\n")
1935                   (progn
1936                     (beginning-of-line)
1937                     (kill-line))))
1938             ;; Use the list of groups.
1939             (while (setq name (pop groups))
1940               (let ((str (if (string-match ":" name)
1941                              name
1942                            (gnus-group-prefixed-name
1943                             name gnus-message-archive-method))))
1944                 (insert (if (string-match " " str)
1945                             (concat "\"" str "\"")
1946                           str)))
1947               (when groups
1948                 (insert " ")))
1949             (insert "\n")))))))
1950
1951 ;;; Posting styles.
1952
1953 (defun gnus-configure-posting-styles (&optional group-name)
1954   "Configure posting styles according to `gnus-posting-styles'."
1955   (unless gnus-inhibit-posting-styles
1956     (let ((group (or group-name gnus-newsgroup-name ""))
1957           (styles gnus-posting-styles)
1958           style match attribute results
1959           name address)
1960       ;; If the group has a posting-style parameter, add it at the end with a
1961       ;; regexp matching everything, to be sure it takes precedence over all
1962       ;; the others.
1963       (when gnus-newsgroup-name
1964         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1965           (when tmp-style
1966             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1967       ;; Go through all styles and look for matches.
1968       (dolist (style styles)
1969         (setq match (pop style))
1970         (goto-char (point-min))
1971         (when (cond
1972                ((stringp match)
1973                 ;; Regexp string match on the group name.
1974                 (string-match match group))
1975                ((eq match 'header)
1976                 ;; Obsolete format of header match.
1977                 (and (gnus-buffer-live-p gnus-article-copy)
1978                      (with-current-buffer gnus-article-copy
1979                        (let ((header (message-fetch-field (pop style))))
1980                          (and header
1981                               (string-match (pop style) header))))))
1982                ((or (symbolp match)
1983                     (gnus-functionp match))
1984                 (cond
1985                  ((gnus-functionp match)
1986                   ;; Function to be called.
1987                   (funcall match))
1988                  ((boundp match)
1989                   ;; Variable to be checked.
1990                   (symbol-value match))))
1991                ((listp match)
1992                 (cond
1993                  ((eq (car match) 'header)
1994                   ;; New format of header match.
1995                   (and (gnus-buffer-live-p gnus-article-copy)
1996                        (with-current-buffer gnus-article-copy
1997                          (let ((header (message-fetch-field (nth 1 match))))
1998                            (and header
1999                                 (string-match (nth 2 match) header))))))
2000                  (t
2001                   ;; This is a form to be evaled.
2002                   (eval match)))))
2003           ;; We have a match, so we set the variables.
2004           (setq style (gnus-configure-posting-style style nil))
2005           (dolist (attribute style)
2006             (setq results (delq (assoc (car attribute) results) results))
2007             (push attribute results))))
2008       ;; Now we have all the styles, so we insert them.
2009       (setq name (assq 'name results)
2010             address (assq 'address results))
2011       (setq results (delq name (delq address results)))
2012       ;; make-local-hook is not obsolete in Emacs 20 or XEmacs.
2013       (make-local-hook 'message-setup-hook)
2014       (setq results (sort results (lambda (x y)
2015                                     (string-lessp (car x) (car y)))))
2016       (dolist (result results)
2017         (add-hook 'message-setup-hook
2018                   (cond
2019                    ((eq 'eval (car result))
2020                     'ignore)
2021                    ((eq 'body (car result))
2022                     `(lambda ()
2023                        (save-excursion
2024                          (message-goto-body)
2025                          (insert ,(cdr result)))))
2026                    ((eq 'signature (car result))
2027                     (set (make-local-variable 'message-signature) nil)
2028                     (set (make-local-variable 'message-signature-file) nil)
2029                     (if (not (cdr result))
2030                         'ignore
2031                       `(lambda ()
2032                          (save-excursion
2033                            (let ((message-signature ,(cdr result)))
2034                              (when message-signature
2035                                (message-insert-signature)))))))
2036                    (t
2037                     (let ((header
2038                            (if (symbolp (car result))
2039                                (capitalize (symbol-name (car result)))
2040                              (car result))))
2041                       `(lambda ()
2042                          (save-excursion
2043                            (message-remove-header ,header)
2044                            (let ((value ,(cdr result)))
2045                              (when value
2046                                (message-goto-eoh)
2047                                (insert ,header ": " value)
2048                                (unless (bolp)
2049                                  (insert "\n")))))))))
2050                   nil 'local))
2051       (when (or name address)
2052         (add-hook 'message-setup-hook
2053                   `(lambda ()
2054                      (set (make-local-variable 'user-mail-address)
2055                           ,(or (cdr address) user-mail-address))
2056                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
2057                            (user-mail-address
2058                             ,(or (cdr address) user-mail-address)))
2059                        (save-excursion
2060                          (message-remove-header "From")
2061                          (message-goto-eoh)
2062                          (insert "From: " (message-make-from) "\n"))))
2063                   nil 'local)))))
2064
2065 ;; splitted from gnus-configure-posting-styles to allow recursive traversal.
2066 (defun gnus-configure-posting-style (style stack)
2067   "Parse one posting style STYLE and returns the value as an alist."
2068   (let (results element variable filep value v)
2069     (dolist (attribute style)
2070       (setq element (pop attribute)
2071             variable nil
2072             filep nil)
2073       (setq value
2074             (cond
2075              ((eq (car attribute) ':file)
2076               (setq filep t)
2077               (cadr attribute))
2078              ((eq (car attribute) :value)
2079               (cadr attribute))
2080              (t
2081               (car attribute))))
2082       ;; We get the value.
2083       (setq v
2084             (cond
2085              ((stringp value)
2086               value)
2087              ((or (symbolp value)
2088                   (gnus-functionp value))
2089               (cond ((gnus-functionp value)
2090                      (funcall value))
2091                     ((boundp value)
2092                      (symbol-value value))))
2093              ((listp value)
2094               (eval value))))
2095       ;; Translate obsolescent value.
2096       (cond
2097        ((eq element 'signature-file)
2098         (setq element 'signature
2099               filep t))
2100        ((eq element 'x-face-file)
2101         (setq element 'x-face
2102               filep t)))
2103       ;; Get the contents of file elems.
2104       (when (and filep v)
2105         (setq v (with-temp-buffer
2106                   (insert-file-contents v)
2107                   (goto-char (point-max))
2108                   (while (bolp)
2109                     (delete-char -1))
2110                   (buffer-string))))
2111       (if (eq element 'import)
2112           (progn
2113             (if (member v stack)
2114                 (error "Circular import of \"%s\"" v))
2115             (setq results
2116                   (nconc (nreverse (gnus-configure-posting-style
2117                                     (cdr (assoc v gnus-named-posting-styles))
2118                                     (cons v stack)))
2119                          results)))
2120         (push (cons element v) results)))
2121     (nreverse results)))
2122
2123 (defun gnus-summary-execute-command-with-posting-style (style command)
2124   "Temporarily select a posting-style named STYLE and execute COMMAND."
2125   (interactive
2126    (let ((style (completing-read "Posting style: "
2127                                  gnus-named-posting-styles nil t)))
2128      (list style
2129            (key-binding
2130             (read-key-sequence
2131              (format "Command to execute with %s: " style))))))
2132   (let ((gnus-posting-styles (list (list ".*" (list 'import style)))))
2133     (call-interactively command)))
2134
2135
2136 ;;; @ for MIME Edit mode
2137 ;;;
2138
2139 (defun gnus-maybe-setup-default-charset ()
2140   (let ((charset
2141          (and (boundp 'gnus-summary-buffer)
2142               (buffer-live-p gnus-summary-buffer)
2143               (save-excursion
2144                 (set-buffer gnus-summary-buffer)
2145                 default-mime-charset))))
2146     (if charset
2147         (progn
2148           (make-local-variable 'default-mime-charset)
2149           (setq default-mime-charset charset)
2150           ))))
2151
2152
2153 ;;; @ for MIME view mode
2154 ;;;
2155
2156 (defun gnus-following-method (buf)
2157   (gnus-setup-message 'reply-yank
2158     (set-buffer buf)
2159     (if (message-news-p)
2160         (message-followup)
2161       (message-reply nil 'wide))
2162     (let ((message-reply-buffer buf))
2163       (message-yank-original))
2164     (message-goto-body))
2165   (kill-buffer buf))
2166
2167
2168 ;;; Allow redefinition of functions.
2169
2170 (gnus-ems-redefine)
2171
2172 (provide 'gnus-msg)
2173
2174 ;;; gnus-msg.el ends here