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