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