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