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