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