* nnsoup.el (nnsoup-read-active-file): Use dolist, mapc or last
[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.4"
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.4"
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.4"
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.4"
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.4"
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   :version "21.4"
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   :version "21.4"
277   :group 'gnus-message
278   :type 'boolean)
279
280 (defcustom gnus-summary-resend-default-address t
281   "If non-nil, Gnus tries to suggest a default address to resend to.
282 If nil, the address field will always be empty after invoking
283 `gnus-summary-resend-message'."
284   :group 'gnus-message
285   :type 'boolean)
286
287 ;;; Internal variables.
288
289 (defvar gnus-inhibit-posting-styles nil
290   "Inhibit the use of posting styles.")
291
292 (defvar gnus-article-yanked-articles nil)
293 (defvar gnus-message-buffer "*Mail Gnus*")
294 (defvar gnus-article-copy nil)
295 (defvar gnus-check-before-posting nil)
296 (defvar gnus-last-posting-server nil)
297 (defvar gnus-message-group-art nil)
298
299 (defvar gnus-msg-force-broken-reply-to nil)
300
301 (defconst gnus-bug-message
302   "Sending a bug report to the Gnus Towers.
303 ========================================
304
305 The buffer below is a mail buffer.  When you press `C-c C-c', it will
306 be sent to the Gnus Bug Exterminators.
307
308 The thing near the bottom of the buffer is how the environment
309 settings will be included in the mail.  Please do not delete that.
310 They will tell the Bug People what your environment is, so that it
311 will be easier to locate the bugs.
312
313 If you have found a bug that makes Emacs go \"beep\", set
314 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
315 and include the backtrace in your bug report.
316
317 Please describe the bug in annoying, painstaking detail.
318
319 Thank you for your help in stamping out bugs.
320 ")
321
322 (eval-and-compile
323   (autoload 'gnus-uu-post-news "gnus-uu" nil t))
324
325 \f
326 ;;;
327 ;;; Gnus Posting Functions
328 ;;;
329
330 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
331   "p" gnus-summary-post-news
332   "i" gnus-summary-news-other-window
333   "f" gnus-summary-followup
334   "F" gnus-summary-followup-with-original
335   "c" gnus-summary-cancel-article
336   "s" gnus-summary-supersede-article
337   "r" gnus-summary-reply
338   "y" gnus-summary-yank-message
339   "R" gnus-summary-reply-with-original
340   "w" gnus-summary-wide-reply
341   "W" gnus-summary-wide-reply-with-original
342   "v" gnus-summary-very-wide-reply
343   "V" gnus-summary-very-wide-reply-with-original
344   "n" gnus-summary-followup-to-mail
345   "N" gnus-summary-followup-to-mail-with-original
346   "m" gnus-summary-mail-other-window
347   "u" gnus-uu-post-news
348   "\M-c" gnus-summary-mail-crosspost-complaint
349   "Br" gnus-summary-reply-broken-reply-to
350   "BR" gnus-summary-reply-broken-reply-to-with-original
351   "om" gnus-summary-mail-forward
352   "op" gnus-summary-post-forward
353   "Om" gnus-uu-digest-mail-forward
354   "Op" gnus-uu-digest-post-forward)
355
356 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
357   "b" gnus-summary-resend-bounced-mail
358   ;; "c" gnus-summary-send-draft
359   "r" gnus-summary-resend-message
360   "e" gnus-summary-resend-message-edit)
361
362 ;;; Internal functions.
363
364 (defun gnus-inews-make-draft (articles)
365   `(lambda ()
366      (gnus-inews-make-draft-meta-information
367       ,gnus-newsgroup-name ',articles)))
368
369 (defvar gnus-article-reply nil)
370 (defmacro gnus-setup-message (config &rest forms)
371   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
372         (buffer (make-symbol "gnus-setup-message-buffer"))
373         (article (make-symbol "gnus-setup-message-article"))
374         (yanked (make-symbol "gnus-setup-yanked-articles"))
375         (group (make-symbol "gnus-setup-message-group")))
376     `(let ((,winconf (current-window-configuration))
377            (,buffer (buffer-name (current-buffer)))
378            (,article gnus-article-reply)
379            (,yanked gnus-article-yanked-articles)
380            (,group gnus-newsgroup-name)
381            (message-header-setup-hook
382             (copy-sequence message-header-setup-hook))
383            (mbl mml-buffer-list)
384            (message-mode-hook (copy-sequence message-mode-hook)))
385        (setq mml-buffer-list nil)
386        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
387        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
388        ;; #### FIXME: for a reason that I did not manage to identify yet,
389        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
390        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
391        ;; After evaluation of @forms below, it gets the value we actually want
392        ;; to override, and the posting styles are used. For that reason, I've
393        ;; added an optional argument to `gnus-configure-posting-styles' to
394        ;; make sure that the correct value for the group name is used. -- drv
395        (add-hook 'message-mode-hook
396                  (if (memq ,config '(reply-yank reply))
397                      (lambda ()
398                        (gnus-configure-posting-styles ,group))
399                    (lambda ()
400                      ;; There may be an old " *gnus article copy*" buffer.
401                      (let (gnus-article-copy)
402                        (gnus-configure-posting-styles ,group)))))
403        (gnus-pull ',(intern gnus-draft-meta-information-header)
404                   message-required-headers)
405        (when (and ,group
406                   (not (string= ,group "")))
407          (push (cons
408                 (intern gnus-draft-meta-information-header)
409                 (gnus-inews-make-draft (or ,yanked ,article)))
410                message-required-headers))
411        (unwind-protect
412            (progn
413              ,@forms)
414          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config
415                                       ,yanked)
416          (setq gnus-message-buffer (current-buffer))
417          (set (make-local-variable 'gnus-message-group-art)
418               (cons ,group ,article))
419          (set (make-local-variable 'gnus-newsgroup-name) ,group)
420          (gnus-run-hooks 'gnus-message-setup-hook)
421          (if (eq major-mode 'message-mode)
422              (let ((mbl1 mml-buffer-list))
423                (setq mml-buffer-list mbl)  ;; Global value
424                (set (make-local-variable 'mml-buffer-list) mbl1);; Local value
425                (gnus-make-local-hook 'kill-buffer-hook)
426                (gnus-make-local-hook 'change-major-mode-hook)
427                (add-hook 'change-major-mode-hook 'mml-destroy-buffers nil t)
428                (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))
429            (mml-destroy-buffers)
430            (setq mml-buffer-list mbl)))
431        (message-hide-headers)
432        (gnus-add-buffer)
433        (gnus-configure-windows ,config t)
434        (run-hooks 'post-command-hook)
435        (set-buffer-modified-p nil))))
436
437 (defun gnus-inews-make-draft-meta-information (group articles)
438   (when (numberp articles)
439     (setq articles (list articles)))
440   (concat "(\"" group "\""
441           (if articles
442               (concat " "
443                       (mapconcat
444                        (lambda (elem)
445                          (number-to-string
446                           (if (consp elem)
447                               (car elem)
448                             elem)))
449                        articles " "))
450             "")
451           ")"))
452
453 ;;;###autoload
454 (defun gnus-msg-mail (&optional to subject other-headers continue
455                                 switch-action yank-action send-actions)
456   "Start editing a mail message to be sent.
457 Like `message-mail', but with Gnus paraphernalia, particularly the
458 Gcc: header for archiving purposes."
459   (interactive)
460   (let ((buf (current-buffer))
461         mail-buf)
462     (gnus-setup-message 'message
463       (message-mail to subject other-headers continue
464                     nil yank-action send-actions))
465     (when switch-action
466       (setq mail-buf (current-buffer))
467       (switch-to-buffer buf)
468       (apply switch-action mail-buf nil)))
469   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
470   t)
471
472 (defvar save-selected-window-window)
473
474 ;;;###autoload
475 (defun gnus-button-mailto (address)
476   "Mail to ADDRESS."
477   (set-buffer (gnus-copy-article-buffer))
478   (gnus-setup-message 'message
479     (message-reply address))
480   (and (boundp 'save-selected-window-window)
481        (not (window-live-p save-selected-window-window))
482        (setq save-selected-window-window (selected-window))))
483
484 ;;;###autoload
485 (defun gnus-button-reply (&optional to-address wide)
486   "Like `message-reply'."
487   (interactive)
488   (gnus-setup-message 'message
489     (message-reply to-address wide))
490   (and (boundp 'save-selected-window-window)
491        (not (window-live-p save-selected-window-window))
492        (setq save-selected-window-window (selected-window))))
493
494 ;;;###autoload
495 (define-mail-user-agent 'gnus-user-agent
496   'gnus-msg-mail 'message-send-and-exit
497   'message-kill-buffer 'message-send-hook)
498
499 (defun gnus-setup-posting-charset (group)
500   (let ((alist gnus-group-posting-charset-alist)
501         (group (or group ""))
502         elem)
503     (when group
504       (catch 'found
505         (while (setq elem (pop alist))
506           (when (or (and (stringp (car elem))
507                          (string-match (car elem) group))
508                     (and (functionp (car elem))
509                          (funcall (car elem) group))
510                     (and (symbolp (car elem))
511                          (symbol-value (car elem))))
512             (throw 'found (cons (cadr elem) (caddr elem)))))))))
513
514 (defun gnus-inews-add-send-actions (winconf buffer article
515                                             &optional config yanked)
516   (gnus-make-local-hook 'message-sent-hook)
517   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
518                                  'gnus-inews-do-gcc) nil t)
519   (when gnus-agent
520     (gnus-make-local-hook 'message-header-hook)
521     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
522   (setq message-post-method
523         `(lambda (arg)
524            (gnus-post-method arg ,gnus-newsgroup-name)))
525   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
526   (message-add-action
527    `(when (gnus-buffer-exists-p ,buffer)
528       (set-window-configuration ,winconf))
529    'exit 'postpone 'kill)
530   (let ((to-be-marked (cond
531                        (yanked
532                         (mapcar
533                          (lambda (x) (if (listp x) (car x) x)) yanked))
534                        (article (if (listp article) article (list article)))
535                        (t nil))))
536     (message-add-action
537      `(when (gnus-buffer-exists-p ,buffer)
538         (save-excursion
539           (set-buffer ,buffer)
540           ,(when to-be-marked
541              (if (eq config 'forward)
542                  `(gnus-summary-mark-article-as-forwarded ',to-be-marked)
543                `(gnus-summary-mark-article-as-replied ',to-be-marked)))))
544      'send)))
545
546 (put 'gnus-setup-message 'lisp-indent-function 1)
547 (put 'gnus-setup-message 'edebug-form-spec '(form body))
548
549 ;;; Post news commands of Gnus group mode and summary mode
550
551 (defun gnus-group-mail (&optional arg)
552   "Start composing a mail.
553 If ARG, use the group under the point to find a posting style.
554 If ARG is 1, prompt for a group name to find the posting style."
555   (interactive "P")
556   ;; We can't `let' gnus-newsgroup-name here, since that leads
557   ;; to local variables leaking.
558   (let ((group gnus-newsgroup-name)
559         ;; make sure last viewed article doesn't affect posting styles:
560         (gnus-article-copy)
561         (buffer (current-buffer)))
562     (unwind-protect
563         (progn
564           (setq gnus-newsgroup-name
565                 (if arg
566                     (if (= 1 (prefix-numeric-value arg))
567                         (completing-read "Use posting style of group: "
568                                          gnus-active-hashtb nil
569                                          (gnus-read-active-file-p))
570                       (gnus-group-group-name))
571                   ""))
572           ;; #### see comment in gnus-setup-message -- drv
573           (gnus-setup-message 'message (message-mail)))
574       (save-excursion
575         (set-buffer buffer)
576         (setq gnus-newsgroup-name group)))))
577
578 (defun gnus-group-news (&optional arg)
579   "Start composing a news.
580 If ARG, post to group under point.
581 If ARG is 1, prompt for group name to post to.
582
583 This function prepares a news even when using mail groups.  This is useful
584 for posting messages to mail groups without actually sending them over the
585 network.  The corresponding back end must have a 'request-post method."
586   (interactive "P")
587   ;; We can't `let' gnus-newsgroup-name here, since that leads
588   ;; to local variables leaking.
589   (let ((group gnus-newsgroup-name)
590         ;; make sure last viewed article doesn't affect posting styles:
591         (gnus-article-copy)
592         (buffer (current-buffer)))
593     (unwind-protect
594         (progn
595           (setq gnus-newsgroup-name
596                 (if arg
597                     (if (= 1 (prefix-numeric-value arg))
598                         (completing-read "Use group: "
599                                          gnus-active-hashtb nil
600                                          (gnus-read-active-file-p))
601                       (gnus-group-group-name))
602                   ""))
603           ;; #### see comment in gnus-setup-message -- drv
604           (gnus-setup-message 'message
605             (message-news (gnus-group-real-name gnus-newsgroup-name))))
606       (save-excursion
607         (set-buffer buffer)
608         (setq gnus-newsgroup-name group)))))
609
610 (defun gnus-group-post-news (&optional arg)
611   "Start composing a message (a news by default).
612 If ARG, post to group under point.  If ARG is 1, prompt for group name.
613 Depending on the selected group, the message might be either a mail or
614 a news."
615   (interactive "P")
616   ;; Bind this variable here to make message mode hooks work ok.
617   (let ((gnus-newsgroup-name
618          (if arg
619              (if (= 1 (prefix-numeric-value arg))
620                  (completing-read "Newsgroup: " gnus-active-hashtb nil
621                                   (gnus-read-active-file-p))
622                (gnus-group-group-name))
623            ""))
624         ;; make sure last viewed article doesn't affect posting styles:
625         (gnus-article-copy))
626     (gnus-post-news 'post gnus-newsgroup-name nil nil nil nil
627                     (string= gnus-newsgroup-name ""))))
628
629 (defun gnus-summary-mail-other-window (&optional arg)
630   "Start composing a mail in another window.
631 Use the posting of the current group by default.
632 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
633 posting style."
634   (interactive "P")
635   ;; We can't `let' gnus-newsgroup-name here, since that leads
636   ;; to local variables leaking.
637   (let ((group gnus-newsgroup-name)
638         ;; make sure last viewed article doesn't affect posting styles:
639         (gnus-article-copy)
640         (buffer (current-buffer)))
641     (unwind-protect
642         (progn
643           (setq gnus-newsgroup-name
644                 (if arg
645                     (if (= 1 (prefix-numeric-value arg))
646                         (completing-read "Use group: "
647                                          gnus-active-hashtb nil
648                                          (gnus-read-active-file-p))
649                       "")
650                   gnus-newsgroup-name))
651           ;; #### see comment in gnus-setup-message -- drv
652           (gnus-setup-message 'message (message-mail)))
653       (save-excursion
654         (set-buffer buffer)
655         (setq gnus-newsgroup-name group)))))
656
657 (defun gnus-summary-news-other-window (&optional arg)
658   "Start composing a news in another window.
659 Post to the current group by default.
660 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
661
662 This function prepares a news even when using mail groups.  This is useful
663 for posting messages to mail groups without actually sending them over the
664 network.  The corresponding back end must have a 'request-post method."
665   (interactive "P")
666   ;; We can't `let' gnus-newsgroup-name here, since that leads
667   ;; to local variables leaking.
668   (let ((group gnus-newsgroup-name)
669         ;; make sure last viewed article doesn't affect posting styles:
670         (gnus-article-copy)
671         (buffer (current-buffer)))
672     (unwind-protect
673         (progn
674           (setq gnus-newsgroup-name
675                 (if arg
676                     (if (= 1 (prefix-numeric-value arg))
677                         (completing-read "Use group: "
678                                          gnus-active-hashtb nil
679                                          (gnus-read-active-file-p))
680                       "")
681                   gnus-newsgroup-name))
682           ;; #### see comment in gnus-setup-message -- drv
683           (gnus-setup-message 'message
684             (progn
685               (message-news (gnus-group-real-name gnus-newsgroup-name))
686               (set (make-local-variable 'gnus-discouraged-post-methods)
687                    (remove
688                     (car (gnus-find-method-for-group gnus-newsgroup-name))
689                     gnus-discouraged-post-methods)))))
690       (save-excursion
691         (set-buffer buffer)
692         (setq gnus-newsgroup-name group)))))
693
694 (defun gnus-summary-post-news (&optional arg)
695   "Start composing a message.  Post to the current group by default.
696 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
697 Depending on the selected group, the message might be either a mail or
698 a news."
699   (interactive "P")
700   ;; Bind this variable here to make message mode hooks work ok.
701   (let ((gnus-newsgroup-name
702          (if arg
703              (if (= 1 (prefix-numeric-value arg))
704                  (completing-read "Newsgroup: " gnus-active-hashtb nil
705                                   (gnus-read-active-file-p))
706                "")
707            gnus-newsgroup-name))
708         ;; make sure last viewed article doesn't affect posting styles:
709         (gnus-article-copy))
710     (gnus-post-news 'post gnus-newsgroup-name)))
711
712
713 (defun gnus-summary-followup (yank &optional force-news)
714   "Compose a followup to an article.
715 If prefix argument YANK is non-nil, the original article is yanked
716 automatically.
717 YANK is a list of elements, where the car of each element is the
718 article number, and the cdr is the string to be yanked."
719   (interactive
720    (list (and current-prefix-arg
721               (gnus-summary-work-articles 1))))
722   (when yank
723     (gnus-summary-goto-subject
724      (if (listp (car yank))
725          (caar yank)
726        (car yank))))
727   (save-window-excursion
728     (gnus-summary-select-article))
729   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
730         (gnus-newsgroup-name gnus-newsgroup-name))
731     ;; Send a followup.
732     (gnus-post-news nil gnus-newsgroup-name
733                     headers gnus-article-buffer
734                     yank nil force-news)
735     (gnus-summary-handle-replysign)))
736
737 (defun gnus-summary-followup-with-original (n &optional force-news)
738   "Compose a followup to an article and include the original article.
739 The text in the region will be yanked.  If the region isn't
740 active, the entire article will be yanked."
741   (interactive "P")
742   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
743
744 (defun gnus-summary-followup-to-mail (&optional arg)
745   "Followup to the current mail message via news."
746   (interactive
747    (list (and current-prefix-arg
748               (gnus-summary-work-articles 1))))
749   (gnus-summary-followup arg t))
750
751 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
752   "Followup to the current mail message via news."
753   (interactive "P")
754   (gnus-summary-followup (gnus-summary-work-articles arg) t))
755
756 (defun gnus-inews-yank-articles (articles)
757   (let (beg article yank-string)
758     (message-goto-body)
759     (while (setq article (pop articles))
760       (when (listp article)
761         (setq yank-string (nth 1 article)
762               article (nth 0 article)))
763       (save-window-excursion
764         (set-buffer gnus-summary-buffer)
765         (gnus-summary-select-article nil nil nil article)
766         (gnus-summary-remove-process-mark article))
767       (gnus-copy-article-buffer nil yank-string)
768       (let ((message-reply-buffer gnus-article-copy)
769             (message-reply-headers
770              ;; The headers are decoded.
771              (with-current-buffer gnus-article-copy
772                (save-restriction
773                  (nnheader-narrow-to-headers)
774                  (nnheader-parse-naked-head)))))
775         (message-yank-original)
776         (setq beg (or beg (mark t))))
777       (when articles
778         (insert "\n")))
779     (push-mark)
780     (goto-char beg)))
781
782 (defun gnus-summary-cancel-article (&optional n symp)
783   "Cancel an article you posted.
784 Uses the process-prefix convention.  If given the symbolic
785 prefix `a', cancel using the standard posting method; if not
786 post using the current select method."
787   (interactive (gnus-interactive "P\ny"))
788   (let ((message-post-method
789          `(lambda (arg)
790             (gnus-post-method (eq ',symp 'a) ,gnus-newsgroup-name))))
791     (dolist (article (gnus-summary-work-articles n))
792       (when (gnus-summary-select-article t nil nil article)
793         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
794                 (message-cancel-news))
795           (gnus-summary-mark-as-read article gnus-canceled-mark)
796           (gnus-cache-remove-article 1))
797         (gnus-article-hide-headers-if-wanted))
798       (gnus-summary-remove-process-mark article))))
799
800 (defun gnus-summary-supersede-article ()
801   "Compose an article that will supersede a previous article.
802 This is done simply by taking the old article and adding a Supersedes
803 header line with the old Message-ID."
804   (interactive)
805   (let ((article (gnus-summary-article-number)))
806     (gnus-setup-message 'reply-yank
807       (gnus-summary-select-article t)
808       (set-buffer gnus-original-article-buffer)
809       (message-supersede)
810       (push
811        `((lambda ()
812            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
813              (save-excursion
814                (set-buffer ,gnus-summary-buffer)
815                (gnus-cache-possibly-remove-article ,article nil nil nil t)
816                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
817        message-send-actions))))
818
819 \f
820
821 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
822   ;; make a copy of the article buffer with all text properties removed
823   ;; this copy is in the buffer gnus-article-copy.
824   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
825   ;; this buffer should be passed to all mail/news reply/post routines.
826   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
827   (save-excursion
828     (set-buffer gnus-article-copy)
829     (mm-enable-multibyte))
830   (let ((article-buffer (or article-buffer gnus-article-buffer))
831         end beg)
832     (if (not (and (get-buffer article-buffer)
833                   (gnus-buffer-exists-p article-buffer)))
834         (error "Can't find any article buffer")
835       (save-excursion
836         (set-buffer article-buffer)
837         (let ((gnus-newsgroup-charset (or gnus-article-charset
838                                           gnus-newsgroup-charset))
839               (gnus-newsgroup-ignored-charsets
840                (or gnus-article-ignored-charsets
841                    gnus-newsgroup-ignored-charsets)))
842           (save-restriction
843             ;; Copy over the (displayed) article buffer, delete
844             ;; hidden text and remove text properties.
845             (widen)
846             (copy-to-buffer gnus-article-copy (point-min) (point-max))
847             (set-buffer gnus-article-copy)
848             (when yank-string
849               (message-goto-body)
850               (delete-region (point) (point-max))
851               (insert yank-string))
852             (gnus-article-delete-text-of-type 'annotation)
853             (gnus-remove-text-with-property 'gnus-prev)
854             (gnus-remove-text-with-property 'gnus-next)
855             (gnus-remove-text-with-property 'gnus-decoration)
856             (insert
857              (prog1
858                  (buffer-substring-no-properties (point-min) (point-max))
859                (erase-buffer)))
860             ;; Find the original headers.
861             (set-buffer gnus-original-article-buffer)
862             (goto-char (point-min))
863             (while (looking-at message-unix-mail-delimiter)
864               (forward-line 1))
865             (let ((mail-header-separator ""))
866               (setq beg (point)
867                     end (or (message-goto-body)
868                             ;; There may be just a header.
869                             (point-max))))
870             ;; Delete the headers from the displayed articles.
871             (set-buffer gnus-article-copy)
872             (let ((mail-header-separator ""))
873               (delete-region (goto-char (point-min))
874                              (or (message-goto-body) (point-max))))
875             ;; Insert the original article headers.
876             (insert-buffer-substring gnus-original-article-buffer beg end)
877             ;; Decode charsets.
878             (let ((gnus-article-decode-hook
879                    (delq 'article-decode-charset
880                          (copy-sequence gnus-article-decode-hook))))
881               (run-hooks 'gnus-article-decode-hook)))))
882       gnus-article-copy)))
883
884 (defun gnus-post-news (post &optional group header article-buffer yank subject
885                             force-news)
886   (when article-buffer
887     (gnus-copy-article-buffer))
888   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
889         (gnus-article-yanked-articles yank)
890         (add-to-list gnus-add-to-list))
891     (gnus-setup-message (cond (yank 'reply-yank)
892                               (article-buffer 'reply)
893                               (t 'message))
894       (let* ((group (or group gnus-newsgroup-name))
895              (charset (gnus-group-name-charset nil group))
896              (pgroup group)
897              to-address to-group mailing-list to-list
898              newsgroup-p)
899         (when group
900           (setq to-address (gnus-parameter-to-address group)
901                 to-group (gnus-group-find-parameter group 'to-group)
902                 to-list (gnus-parameter-to-list group)
903                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
904                 mailing-list (when gnus-mailing-list-groups
905                                (string-match gnus-mailing-list-groups group))
906                 group (gnus-group-name-decode (gnus-group-real-name group)
907                                               charset)))
908         (if (or (and to-group
909                      (gnus-news-group-p to-group))
910                 newsgroup-p
911                 force-news
912                 (and (gnus-news-group-p
913                       (or pgroup gnus-newsgroup-name)
914                       (or header gnus-current-article))
915                      (not mailing-list)
916                      (not to-list)
917                      (not to-address)))
918             ;; This is news.
919             (if post
920                 (message-news
921                  (or to-group
922                      (and (not (gnus-virtual-group-p pgroup)) group)))
923               (set-buffer gnus-article-copy)
924               (gnus-msg-treat-broken-reply-to)
925               (message-followup (if (or newsgroup-p force-news)
926                                     (if (save-restriction
927                                           (article-narrow-to-head)
928                                           (message-fetch-field "newsgroups"))
929                                         nil
930                                       "")
931                                   to-group)))
932           ;; The is mail.
933           (if post
934               (progn
935                 (message-mail (or to-address to-list))
936                 ;; Arrange for mail groups that have no `to-address' to
937                 ;; get that when the user sends off the mail.
938                 (when (and (not to-list)
939                            (not to-address)
940                            add-to-list)
941                   (push (list 'gnus-inews-add-to-address pgroup)
942                         message-send-actions)))
943             (set-buffer gnus-article-copy)
944             (gnus-msg-treat-broken-reply-to)
945             (message-wide-reply to-address)))
946         (when yank
947           (gnus-inews-yank-articles yank))))))
948
949 (defun gnus-msg-treat-broken-reply-to (&optional force)
950   "Remove the Reply-to header if broken-reply-to."
951   (when (or force
952             (gnus-group-find-parameter
953              gnus-newsgroup-name 'broken-reply-to))
954     (save-restriction
955       (message-narrow-to-head)
956       (message-remove-header "reply-to"))))
957
958 (defun gnus-post-method (arg group &optional silent)
959   "Return the posting method based on GROUP and ARG.
960 If SILENT, don't prompt the user."
961   (let ((gnus-post-method (or (gnus-parameter-post-method group)
962                               gnus-post-method))
963         (group-method (gnus-find-method-for-group group)))
964     (cond
965      ;; If the group-method is nil (which shouldn't happen) we use
966      ;; the default method.
967      ((null group-method)
968       (or (and (listp gnus-post-method) ;If not current/native/nil
969                (not (listp (car gnus-post-method))) ; and not a list of methods
970                gnus-post-method)        ;then use it.
971           gnus-select-method
972           message-post-method))
973      ;; We want the inverse of the default
974      ((and arg (not (eq arg 0)))
975       (if (eq gnus-post-method 'current)
976           gnus-select-method
977         group-method))
978      ;; We query the user for a post method.
979      ((or arg
980           (and (listp gnus-post-method)
981                (listp (car gnus-post-method))))
982       (let* ((methods
983               ;; Collect all methods we know about.
984               (append
985                (when (listp gnus-post-method)
986                  (if (listp (car gnus-post-method))
987                      gnus-post-method
988                    (list gnus-post-method)))
989                gnus-secondary-select-methods
990                (mapcar 'cdr gnus-server-alist)
991                (mapcar 'car gnus-opened-servers)
992                (list gnus-select-method)
993                (list group-method)))
994              method-alist post-methods method)
995         ;; Weed out all mail methods.
996         (while methods
997           (setq method (gnus-server-get-method "" (pop methods)))
998           (when (and (or (gnus-method-option-p method 'post)
999                          (gnus-method-option-p method 'post-mail))
1000                      (not (member method post-methods)))
1001             (push method post-methods)))
1002         ;; Create a name-method alist.
1003         (setq method-alist
1004               (mapcar
1005                (lambda (m)
1006                  (if (equal (cadr m) "")
1007                      (list (symbol-name (car m)) m)
1008                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
1009                post-methods))
1010         ;; Query the user.
1011         (cadr
1012          (assoc
1013           (setq gnus-last-posting-server
1014                 (if (and silent
1015                          gnus-last-posting-server)
1016                     ;; Just use the last value.
1017                     gnus-last-posting-server
1018                   (completing-read
1019                    "Posting method: " method-alist nil t
1020                    (cons (or gnus-last-posting-server "") 0))))
1021           method-alist))))
1022      ;; Override normal method.
1023      ((and (eq gnus-post-method 'current)
1024            (not (memq (car group-method) gnus-discouraged-post-methods))
1025            (gnus-get-function group-method 'request-post t))
1026       (assert (not arg))
1027       group-method)
1028      ;; Use gnus-post-method.
1029      ((listp gnus-post-method)          ;A method...
1030       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
1031       gnus-post-method)
1032      ;; Use the normal select method (nil or native).
1033      (t gnus-select-method))))
1034
1035 \f
1036
1037 (defun gnus-extended-version ()
1038   "Stringified Gnus version and Emacs version.
1039 See the variable `gnus-user-agent'."
1040   (interactive)
1041   (let* ((float-output-format nil)
1042          (gnus-v
1043           (concat "Gnus/"
1044                   (prin1-to-string (gnus-continuum-version gnus-version) t)
1045                   " (" gnus-version ")"))
1046          (emacs-v (gnus-emacs-version)))
1047     (if (stringp gnus-user-agent)
1048         gnus-user-agent
1049       (concat gnus-v
1050               (when emacs-v
1051                 (concat " " emacs-v))))))
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 Uses the process/prefix convention."
1169   (interactive "P")
1170   (gnus-summary-reply-with-original n t))
1171
1172 (defun gnus-summary-very-wide-reply (&optional yank)
1173   "Start composing a very wide reply mail to the current message.
1174 If prefix argument YANK is non-nil, the original article is yanked
1175 automatically."
1176   (interactive
1177    (list (and current-prefix-arg
1178               (gnus-summary-work-articles 1))))
1179   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
1180
1181 (defun gnus-summary-very-wide-reply-with-original (n)
1182   "Start composing a very wide reply mail to the current message.
1183 The original article will be yanked."
1184   (interactive "P")
1185   (gnus-summary-reply
1186    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1187
1188 (defun gnus-summary-mail-forward (&optional arg post)
1189   "Forward the current message(s) to another user.
1190 If process marks exist, forward all marked messages;
1191 if ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
1192 if ARG is 1, decode the message and forward directly inline;
1193 if ARG is 2, forward message as an rfc822 MIME section;
1194 if ARG is 3, decode message and forward as an rfc822 MIME section;
1195 if ARG is 4, forward message directly inline;
1196 otherwise, use flipped `message-forward-as-mime'.
1197 If POST, post instead of mail.
1198 For the `inline' alternatives, also see the variable
1199 `message-forward-ignored-headers'."
1200   (interactive "P")
1201   (if (cdr (gnus-summary-work-articles nil))
1202       ;; Process marks are given.
1203       (gnus-uu-digest-mail-forward arg post)
1204     ;; No process marks.
1205     (let ((message-forward-as-mime message-forward-as-mime)
1206           (message-forward-show-mml message-forward-show-mml))
1207       (cond
1208        ((null arg))
1209        ((eq arg 1)
1210         (setq message-forward-as-mime nil
1211               message-forward-show-mml t))
1212        ((eq arg 2)
1213         (setq message-forward-as-mime t
1214               message-forward-show-mml nil))
1215        ((eq arg 3)
1216         (setq message-forward-as-mime t
1217               message-forward-show-mml t))
1218        ((eq arg 4)
1219         (setq message-forward-as-mime nil
1220               message-forward-show-mml nil))
1221        (t
1222         (setq message-forward-as-mime (not message-forward-as-mime))))
1223       (let* ((gnus-article-reply (gnus-summary-article-number))
1224              (gnus-article-yanked-articles (list gnus-article-reply)))
1225         (gnus-setup-message 'forward
1226           (gnus-summary-select-article)
1227           (let ((mail-parse-charset
1228                  (or (and (gnus-buffer-live-p gnus-article-buffer)
1229                           (with-current-buffer gnus-article-buffer
1230                             gnus-article-charset))
1231                      gnus-newsgroup-charset))
1232                 (mail-parse-ignored-charsets
1233                  gnus-newsgroup-ignored-charsets))
1234             (set-buffer gnus-original-article-buffer)
1235             (message-forward post)))))))
1236
1237 (defun gnus-summary-resend-message (address n)
1238   "Resend the current article to ADDRESS."
1239   (interactive
1240    (list (message-read-from-minibuffer
1241           "Resend message(s) to: "
1242           (when (and gnus-summary-resend-default-address
1243                      (gnus-buffer-live-p gnus-original-article-buffer))
1244             ;; If some other article is currently selected, the
1245             ;; initial-contents is wrong. Whatever, it is just the
1246             ;; initial-contents.
1247             (with-current-buffer gnus-original-article-buffer
1248               (nnmail-fetch-field "to"))))
1249          current-prefix-arg))
1250   (dolist (article (gnus-summary-work-articles n))
1251     (gnus-summary-select-article nil nil nil article)
1252     (save-excursion
1253       (set-buffer gnus-original-article-buffer)
1254       (message-resend address))
1255     (gnus-summary-mark-article-as-forwarded article)))
1256
1257 ;; From: Matthieu Moy <Matthieu.Moy@imag.fr>
1258 (defun gnus-summary-resend-message-edit ()
1259   "Resend an article that has already been sent.
1260 A new buffer will be created to allow the user to modify body and
1261 contents of the message, and then, everything will happen as when
1262 composing a new message."
1263   (interactive)
1264   (let ((article (gnus-summary-article-number)))
1265     (gnus-setup-message 'reply-yank
1266       (gnus-summary-select-article t)
1267       (set-buffer gnus-original-article-buffer)
1268       (let ((cur (current-buffer))
1269             (to (message-fetch-field "to")))
1270         ;; Get a normal message buffer.
1271         (message-pop-to-buffer (message-buffer-name "Resend" to))
1272         (insert-buffer-substring cur)
1273         (mime-to-mml)
1274         (message-narrow-to-head-1)
1275         ;; Gnus will generate a new one when sending.
1276         (message-remove-header "Message-ID")
1277         (message-remove-header message-ignored-resent-headers t)
1278         ;; Remove unwanted headers.
1279         (goto-char (point-max))
1280         (insert mail-header-separator)
1281         (goto-char (point-min))
1282         (when (re-search-forward "^To:\\|^Newsgroups:" nil 'move)
1283           (forward-char 1))
1284         (widen)))))
1285
1286 (defun gnus-summary-post-forward (&optional arg)
1287   "Forward the current article to a newsgroup.
1288 See `gnus-summary-mail-forward' for ARG."
1289   (interactive "P")
1290   (gnus-summary-mail-forward arg t))
1291
1292 (defvar gnus-nastygram-message
1293   "The following article was inappropriately posted to %s.\n\n"
1294   "Format string to insert in nastygrams.
1295 The current group name will be inserted at \"%s\".")
1296
1297 (defun gnus-summary-mail-nastygram (n)
1298   "Send a nastygram to the author of the current article."
1299   (interactive "P")
1300   (when (or gnus-expert-user
1301             (gnus-y-or-n-p
1302              "Really send a nastygram to the author of the current article? "))
1303     (let ((group gnus-newsgroup-name))
1304       (gnus-summary-reply-with-original n)
1305       (set-buffer gnus-message-buffer)
1306       (message-goto-body)
1307       (insert (format gnus-nastygram-message group))
1308       (message-send-and-exit))))
1309
1310 (defun gnus-summary-mail-crosspost-complaint (n)
1311   "Send a complaint about crossposting to the current article(s)."
1312   (interactive "P")
1313   (dolist (article (gnus-summary-work-articles n))
1314     (set-buffer gnus-summary-buffer)
1315     (gnus-summary-goto-subject article)
1316     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1317           newsgroups followup-to)
1318       (gnus-summary-select-article)
1319       (set-buffer gnus-original-article-buffer)
1320       (if (and (<= (length (message-tokenize-header
1321                             (setq newsgroups
1322                                   (mail-fetch-field "newsgroups"))
1323                             ", "))
1324                    1)
1325                (or (not (setq followup-to (mail-fetch-field "followup-to")))
1326                    (not (member group (message-tokenize-header
1327                                        followup-to ", ")))))
1328           (if followup-to
1329               (gnus-message 1 "Followup-to restricted")
1330             (gnus-message 1 "Not a crossposted article"))
1331         (set-buffer gnus-summary-buffer)
1332         (gnus-summary-reply-with-original 1)
1333         (set-buffer gnus-message-buffer)
1334         (message-goto-body)
1335         (insert (format gnus-crosspost-complaint newsgroups group))
1336         (message-goto-subject)
1337         (re-search-forward " *$")
1338         (replace-match " (crosspost notification)" t t)
1339         (gnus-deactivate-mark)
1340         (when (gnus-y-or-n-p "Send this complaint? ")
1341           (message-send-and-exit))))))
1342
1343 (defun gnus-mail-parse-comma-list ()
1344   (let (accumulated
1345         beg)
1346     (skip-chars-forward " ")
1347     (while (not (eobp))
1348       (setq beg (point))
1349       (skip-chars-forward "^,")
1350       (while (zerop
1351               (save-excursion
1352                 (save-restriction
1353                   (let ((i 0))
1354                     (narrow-to-region beg (point))
1355                     (goto-char beg)
1356                     (logand (progn
1357                               (while (search-forward "\"" nil t)
1358                                 (incf i))
1359                               (if (zerop i) 2 i))
1360                             2)))))
1361         (skip-chars-forward ",")
1362         (skip-chars-forward "^,"))
1363       (skip-chars-backward " ")
1364       (push (buffer-substring beg (point))
1365             accumulated)
1366       (skip-chars-forward "^,")
1367       (skip-chars-forward ", "))
1368     accumulated))
1369
1370 (defun gnus-inews-add-to-address (group)
1371   (let ((to-address (mail-fetch-field "to")))
1372     (when (and to-address
1373                (gnus-alive-p))
1374       ;; This mail group doesn't have a `to-list', so we add one
1375       ;; here.  Magic!
1376       (when (gnus-y-or-n-p
1377              (format "Do you want to add this as `to-list': %s? " to-address))
1378         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1379
1380 (defun gnus-put-message ()
1381   "Put the current message in some group and return to Gnus."
1382   (interactive)
1383   (let ((reply gnus-article-reply)
1384         (winconf gnus-prev-winconf)
1385         (group gnus-newsgroup-name))
1386     (unless (and group
1387                  (not (gnus-group-read-only-p group)))
1388       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1389
1390     (when (gnus-group-entry group)
1391       (error "No such group: %s" group))
1392     (save-excursion
1393       (save-restriction
1394         (widen)
1395         (message-narrow-to-headers)
1396         (let ((gnus-deletable-headers nil))
1397           (message-generate-headers
1398            (if (message-news-p)
1399                message-required-news-headers
1400              message-required-mail-headers)))
1401         (goto-char (point-max))
1402         (if (string-match " " group)
1403             (insert "Gcc: \"" group "\"\n")
1404           (insert "Gcc: " group "\n"))
1405         (widen)))
1406     (gnus-inews-do-gcc)
1407     (when (and (get-buffer gnus-group-buffer)
1408                (gnus-buffer-exists-p (car-safe reply))
1409                (cdr reply))
1410       (set-buffer (car reply))
1411       (gnus-summary-mark-article-as-replied (cdr reply)))
1412     (when winconf
1413       (set-window-configuration winconf))))
1414
1415 (defun gnus-article-mail (yank)
1416   "Send a reply to the address near point.
1417 If YANK is non-nil, include the original article."
1418   (interactive "P")
1419   (let ((address
1420          (buffer-substring
1421           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1422           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1423     (when address
1424       (gnus-msg-mail address)
1425       (when yank
1426         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1427
1428 (defvar nntp-server-type)
1429 (defun gnus-bug ()
1430   "Send a bug report to the Gnus maintainers."
1431   (interactive)
1432   (unless (gnus-alive-p)
1433     (error "Gnus has been shut down"))
1434   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1435     (unless (message-mail-user-agent)
1436       (delete-other-windows)
1437       (when gnus-bug-create-help-buffer
1438         (switch-to-buffer "*Gnus Help Bug*")
1439         (erase-buffer)
1440         (insert gnus-bug-message)
1441         (goto-char (point-min)))
1442       (message-pop-to-buffer "*Gnus Bug*"))
1443     (let ((message-this-is-mail t))
1444       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1445     (when gnus-bug-create-help-buffer
1446       (push `(gnus-bug-kill-buffer) message-send-actions))
1447     (goto-char (point-min))
1448     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1449     (forward-line 1)
1450     (insert (gnus-version) "\n"
1451             (emacs-version) "\n")
1452     (when (and (boundp 'nntp-server-type)
1453                (stringp nntp-server-type))
1454       (insert nntp-server-type))
1455     (insert "\n\n\n\n\n")
1456     (let (text)
1457       (save-excursion
1458         (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
1459         (erase-buffer)
1460         (gnus-debug)
1461         (setq text (buffer-string)))
1462       (insert "<#part type=application/emacs-lisp disposition=inline description=\"User settings\">\n" text "\n<#/part>"))
1463     (goto-char (point-min))
1464     (search-forward "Subject: " nil t)
1465     (message "")))
1466
1467 (defun gnus-bug-kill-buffer ()
1468   (when (get-buffer "*Gnus Help Bug*")
1469     (kill-buffer "*Gnus Help Bug*")))
1470
1471 (defun gnus-summary-yank-message (buffer n)
1472   "Yank the current article into a composed message."
1473   (interactive
1474    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1475          current-prefix-arg))
1476   (gnus-summary-iterate n
1477     (let ((gnus-inhibit-treatment t))
1478       (gnus-summary-select-article))
1479     (save-excursion
1480       (set-buffer buffer)
1481       (message-yank-buffer gnus-article-buffer))))
1482
1483 (defun gnus-debug ()
1484   "Attempts to go through the Gnus source file and report what variables have been changed.
1485 The source file has to be in the Emacs load path."
1486   (interactive)
1487   (let ((files gnus-debug-files)
1488         (point (point))
1489         file expr olist sym)
1490     (gnus-message 4 "Please wait while we snoop your variables...")
1491     (sit-for 0)
1492     ;; Go through all the files looking for non-default values for variables.
1493     (save-excursion
1494       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1495       (while files
1496         (erase-buffer)
1497         (when (and (setq file (locate-library (pop files)))
1498                    (file-exists-p file))
1499           (insert-file-contents file)
1500           (goto-char (point-min))
1501           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1502               (gnus-message 4 "Malformed sources in file %s" file)
1503             (narrow-to-region (point-min) (point))
1504             (goto-char (point-min))
1505             (while (setq expr (ignore-errors (read (current-buffer))))
1506               (ignore-errors
1507                 (and (or (eq (car expr) 'defvar)
1508                          (eq (car expr) 'defcustom))
1509                      (stringp (nth 3 expr))
1510                      (not (memq (nth 1 expr) gnus-debug-exclude-variables))
1511                      (or (not (boundp (nth 1 expr)))
1512                          (not (equal (eval (nth 2 expr))
1513                                      (symbol-value (nth 1 expr)))))
1514                      (push (nth 1 expr) olist)))))))
1515       (kill-buffer (current-buffer)))
1516     (when (setq olist (nreverse olist))
1517       (insert "------------------ Environment follows ------------------\n\n"))
1518     (while olist
1519       (if (boundp (car olist))
1520           (ignore-errors
1521            (gnus-pp
1522             `(setq ,(car olist)
1523                    ,(if (or (consp (setq sym (symbol-value (car olist))))
1524                             (and (symbolp sym)
1525                                  (not (or (eq sym nil)
1526                                           (eq sym t)))))
1527                         (list 'quote (symbol-value (car olist)))
1528                       (symbol-value (car olist))))))
1529         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1530       (setq olist (cdr olist)))
1531     (insert "\n\n")
1532     ;; Remove any control chars - they seem to cause trouble for some
1533     ;; mailers.  (Byte-compiled output from the stuff above.)
1534     (goto-char point)
1535     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1536       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1537                      t t))))
1538
1539 ;;; Treatment of rejected articles.
1540 ;;; Bounced mail.
1541
1542 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1543   "Re-mail the current message.
1544 This only makes sense if the current message is a bounce message that
1545 contains some mail you have written which has been bounced back to
1546 you.
1547 If FETCH, try to fetch the article that this is a reply to, if indeed
1548 this is a reply."
1549   (interactive "P")
1550   (gnus-summary-select-article t)
1551   (set-buffer gnus-original-article-buffer)
1552   (gnus-setup-message 'compose-bounce
1553     (let* ((references (mail-fetch-field "references"))
1554            (parent (and references (gnus-parent-id references))))
1555       (message-bounce)
1556       ;; If there are references, we fetch the article we answered to.
1557       (and fetch parent
1558            (gnus-summary-refer-article parent)
1559            (gnus-summary-show-all-headers)))))
1560
1561 ;;; Gcc handling.
1562
1563 (defun gnus-inews-group-method (group)
1564   (cond
1565    ;; If the group doesn't exist, we assume
1566    ;; it's an archive group...
1567    ((and (null (gnus-get-info group))
1568          (eq (car (gnus-server-to-method gnus-message-archive-method))
1569              (car (gnus-server-to-method (gnus-group-method group)))))
1570     gnus-message-archive-method)
1571    ;; Use the method.
1572    ((gnus-info-method (gnus-get-info group))
1573     (gnus-info-method (gnus-get-info group)))
1574    ;; Find the method.
1575    (t (gnus-server-to-method (gnus-group-method group)))))
1576
1577 ;; Do Gcc handling, which copied the message over to some group.
1578 (defun gnus-inews-do-gcc (&optional gcc)
1579   (interactive)
1580   (save-excursion
1581     (save-restriction
1582       (message-narrow-to-headers)
1583       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1584             (cur (current-buffer))
1585             groups group method group-art
1586             mml-externalize-attachments)
1587         (when gcc
1588           (message-remove-header "gcc")
1589           (widen)
1590           (setq groups (message-unquote-tokens
1591                         (message-tokenize-header gcc " ,")))
1592           ;; Copy the article over to some group(s).
1593           (while (setq group (pop groups))
1594             (unless (gnus-check-server
1595                      (setq method (gnus-inews-group-method group)))
1596               (error "Can't open server %s" (if (stringp method) method
1597                                               (car method))))
1598             (unless (gnus-request-group group nil method)
1599               (gnus-request-create-group group method))
1600             (setq mml-externalize-attachments
1601                   (if (stringp gnus-gcc-externalize-attachments)
1602                       (string-match gnus-gcc-externalize-attachments group)
1603                     gnus-gcc-externalize-attachments))
1604             (save-excursion
1605               (nnheader-set-temp-buffer " *acc*")
1606               (insert-buffer-substring cur)
1607               (message-encode-message-body)
1608               (save-restriction
1609                 (message-narrow-to-headers)
1610                 (let* ((mail-parse-charset message-default-charset)
1611                        (newsgroups-field (save-restriction
1612                                            (message-narrow-to-headers-or-head)
1613                                            (message-fetch-field "Newsgroups")))
1614                        (followup-field (save-restriction
1615                                          (message-narrow-to-headers-or-head)
1616                                          (message-fetch-field "Followup-To")))
1617                        ;; BUG: We really need to get the charset for
1618                        ;; each name in the Newsgroups and Followup-To
1619                        ;; lines to allow crossposting between group
1620                        ;; namess with incompatible character sets.
1621                        ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
1622                        (group-field-charset
1623                         (gnus-group-name-charset
1624                          method (or newsgroups-field "")))
1625                        (followup-field-charset
1626                         (gnus-group-name-charset
1627                          method (or followup-field "")))
1628                        (rfc2047-header-encoding-alist
1629                         (append
1630                          (when group-field-charset
1631                            (list (cons "Newsgroups" group-field-charset)))
1632                          (when followup-field-charset
1633                            (list (cons "Followup-To" followup-field-charset)))
1634                          rfc2047-header-encoding-alist)))
1635                   (mail-encode-encoded-word-buffer)))
1636               (goto-char (point-min))
1637               (when (re-search-forward
1638                      (concat "^" (regexp-quote mail-header-separator) "$")
1639                      nil t)
1640                 (replace-match "" t t ))
1641               (when (or (not (gnus-check-backend-function
1642                               'request-accept-article group))
1643                         (not (setq group-art
1644                                    (gnus-request-accept-article
1645                                     group method t t))))
1646                 (gnus-message 1 "Couldn't store article in group %s: %s"
1647                               group (gnus-status-message method)))
1648               (when (and group-art
1649                          ;; FIXME: Should gcc-mark-as-read work when
1650                          ;; Gnus is not running?
1651                          (gnus-alive-p)
1652                          (or gnus-gcc-mark-as-read
1653                              (and
1654                               (boundp 'gnus-inews-mark-gcc-as-read)
1655                               (symbol-value '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                    ((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            ((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                                 ((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                    ;; In nndoc groups, we use the parent group name
1745                    ;; instead of the current group.
1746                    (let ((group (or (gnus-group-find-parameter
1747                                      gnus-newsgroup-name 'parent-group)
1748                                     group)))
1749                      (if (string-match " " group)
1750                          (concat "\"" group "\"")
1751                        group))))
1752                 (if (not (eq gcc-self-val 'none))
1753                     (insert "\n")
1754                   (gnus-delete-line)))
1755             ;; Use the list of groups.
1756             (while (setq name (pop groups))
1757               (let ((str (if (string-match ":" name)
1758                              name
1759                            (gnus-group-prefixed-name
1760                             name gnus-message-archive-method))))
1761                 (insert (if (string-match " " str)
1762                             (concat "\"" str "\"")
1763                           str)))
1764               (when groups
1765                 (insert " ")))
1766             (insert "\n")))))))
1767
1768 (defun gnus-mailing-list-followup-to ()
1769   "Look at the headers in the current buffer and return a Mail-Followup-To address."
1770   (let ((x-been-there (gnus-fetch-original-field "x-beenthere"))
1771         (list-post (gnus-fetch-original-field "list-post")))
1772     (when (and list-post
1773                (string-match "mailto:\\([^>]+\\)" list-post))
1774       (setq list-post (match-string 1 list-post)))
1775     (or list-post
1776         x-been-there)))
1777
1778 ;;; Posting styles.
1779
1780 (defun gnus-configure-posting-styles (&optional group-name)
1781   "Configure posting styles according to `gnus-posting-styles'."
1782   (unless gnus-inhibit-posting-styles
1783     (let ((group (or group-name gnus-newsgroup-name ""))
1784           (styles gnus-posting-styles)
1785           style match attribute value v results
1786           filep name address element)
1787       ;; If the group has a posting-style parameter, add it at the end with a
1788       ;; regexp matching everything, to be sure it takes precedence over all
1789       ;; the others.
1790       (when gnus-newsgroup-name
1791         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1792           (when tmp-style
1793             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1794       ;; Go through all styles and look for matches.
1795       (dolist (style styles)
1796         (setq match (pop style))
1797         (goto-char (point-min))
1798         (when (cond
1799                ((stringp match)
1800                 ;; Regexp string match on the group name.
1801                 (string-match match group))
1802                ((eq match 'header)
1803                 ;; Obsolete format of header match.
1804                 (and (gnus-buffer-live-p gnus-article-copy)
1805                      (with-current-buffer gnus-article-copy
1806                        (save-restriction
1807                          (nnheader-narrow-to-headers)
1808                          (let ((header (message-fetch-field (pop style))))
1809                            (and header
1810                                 (string-match (pop style) header)))))))
1811                ((or (symbolp match)
1812                     (functionp match))
1813                 (cond
1814                  ((functionp match)
1815                   ;; Function to be called.
1816                   (funcall match))
1817                  ((boundp match)
1818                   ;; Variable to be checked.
1819                   (symbol-value match))))
1820                ((listp match)
1821                 (cond
1822                  ((eq (car match) 'header)
1823                   ;; New format of header match.
1824                   (and (gnus-buffer-live-p gnus-article-copy)
1825                        (with-current-buffer gnus-article-copy
1826                          (save-restriction
1827                            (nnheader-narrow-to-headers)
1828                            (let ((header (message-fetch-field (nth 1 match))))
1829                              (and header
1830                                   (string-match (nth 2 match) header)))))))
1831                  (t
1832                   ;; This is a form to be evaled.
1833                   (eval match)))))
1834           ;; We have a match, so we set the variables.
1835           (dolist (attribute style)
1836             (setq element (pop attribute)
1837                   filep nil)
1838             (setq value
1839                   (cond
1840                    ((eq (car attribute) :file)
1841                     (setq filep t)
1842                     (cadr attribute))
1843                    ((eq (car attribute) :value)
1844                     (cadr attribute))
1845                    (t
1846                     (car attribute))))
1847             ;; We get the value.
1848             (setq v
1849                   (cond
1850                    ((stringp value)
1851                     value)
1852                    ((or (symbolp value)
1853                         (functionp value))
1854                     (cond ((functionp value)
1855                            (funcall value))
1856                           ((boundp value)
1857                            (symbol-value value))))
1858                    ((listp value)
1859                     (eval value))))
1860             ;; Translate obsolescent value.
1861             (cond
1862              ((eq element 'signature-file)
1863               (setq element 'signature
1864                     filep t))
1865              ((eq element 'x-face-file)
1866               (setq element 'x-face
1867                     filep t)))
1868             ;; Get the contents of file elems.
1869             (when (and filep v)
1870               (setq v (with-temp-buffer
1871                         (insert-file-contents v)
1872                         (goto-char (point-max))
1873                         (while (bolp)
1874                           (delete-char -1))
1875                         (buffer-string))))
1876             (setq results (delq (assoc element results) results))
1877             (push (cons element v) results))))
1878       ;; Now we have all the styles, so we insert them.
1879       (setq name (assq 'name results)
1880             address (assq 'address results))
1881       (setq results (delq name (delq address results)))
1882       (gnus-make-local-hook 'message-setup-hook)
1883       (setq results (sort results (lambda (x y)
1884                                     (string-lessp (car x) (car y)))))
1885       (dolist (result results)
1886         (add-hook 'message-setup-hook
1887                   (cond
1888                    ((eq 'eval (car result))
1889                     'ignore)
1890                    ((eq 'body (car result))
1891                     `(lambda ()
1892                        (save-excursion
1893                          (message-goto-body)
1894                          (insert ,(cdr result)))))
1895                    ((eq 'signature (car result))
1896                     (set (make-local-variable 'message-signature) nil)
1897                     (set (make-local-variable 'message-signature-file) nil)
1898                     (if (not (cdr result))
1899                         'ignore
1900                       `(lambda ()
1901                          (save-excursion
1902                            (let ((message-signature ,(cdr result)))
1903                              (when message-signature
1904                                (message-insert-signature)))))))
1905                    (t
1906                     (let ((header
1907                            (if (symbolp (car result))
1908                                (capitalize (symbol-name (car result)))
1909                              (car result))))
1910                       `(lambda ()
1911                          (save-excursion
1912                            (message-remove-header ,header)
1913                            (let ((value ,(cdr result)))
1914                              (when value
1915                                (message-goto-eoh)
1916                                (insert ,header ": " value)
1917                                (unless (bolp)
1918                                  (insert "\n")))))))))
1919                   nil 'local))
1920       (when (or name address)
1921         (add-hook 'message-setup-hook
1922                   `(lambda ()
1923                      (set (make-local-variable 'user-mail-address)
1924                           ,(or (cdr address) user-mail-address))
1925                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1926                            (user-mail-address
1927                             ,(or (cdr address) user-mail-address)))
1928                        (save-excursion
1929                          (message-remove-header "From")
1930                          (message-goto-eoh)
1931                          (insert "From: " (message-make-from) "\n"))))
1932                   nil 'local)))))
1933
1934 ;;; Allow redefinition of functions.
1935
1936 (gnus-ems-redefine)
1937
1938 (provide 'gnus-msg)
1939
1940 ;;; arch-tag: 9f22b2f5-1c0a-49de-916e-4c88e984852b
1941 ;;; gnus-msg.el ends here