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