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