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