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