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