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