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