2001-02-17 13:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[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
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-ems)
34 (require 'message)
35 (require 'gnus-art)
36
37 (defcustom gnus-post-method 'current
38   "*Preferred method for posting USENET news.
39
40 If this variable is `current', Gnus will use the \"current\" select
41 method when posting.  If it is nil (which is the default), Gnus will
42 use the native select method when posting.
43
44 This method will not be used in mail groups and the like, only in
45 \"real\" newsgroups.
46
47 If not nil nor `native', the value must be a valid method as discussed
48 in the documentation of `gnus-select-method'.  It can also be a list of
49 methods.  If that is the case, the user will be queried for what select
50 method to use when posting."
51   :group 'gnus-group-foreign
52   :type `(choice (const nil)
53                  (const current)
54                  (const native)
55                  (sexp :tag "Methods" ,gnus-select-method)))
56
57 (defvar gnus-outgoing-message-group nil
58   "*All outgoing messages will be put in this group.
59 If you want to store all your outgoing mail and articles in the group
60 \"nnml:archive\", you set this variable to that value.  This variable
61 can also be a list of group names.
62
63 If you want to have greater control over what group to put each
64 message in, you can set this variable to a function that checks the
65 current newsgroup name and then returns a suitable group name (or list
66 of names).")
67
68 (defvar gnus-mailing-list-groups nil
69   "*Regexp matching groups that are really mailing lists.
70 This is useful when you're reading a mailing list that has been
71 gatewayed to a newsgroup, and you want to followup to an article in
72 the group.")
73
74 (defvar gnus-add-to-list nil
75   "*If non-nil, add a `to-list' parameter automatically.")
76
77 (defvar gnus-crosspost-complaint
78   "Hi,
79
80 You posted the article below with the following Newsgroups header:
81
82 Newsgroups: %s
83
84 The %s group, at least, was an inappropriate recipient
85 of this message.  Please trim your Newsgroups header to exclude this
86 group before posting in the future.
87
88 Thank you.
89
90 "
91   "Format string to be inserted when complaining about crossposts.
92 The first %s will be replaced by the Newsgroups header;
93 the second with the current group name.")
94
95 (defvar gnus-message-setup-hook nil
96   "Hook run after setting up a message buffer.")
97
98 (defvar gnus-bug-create-help-buffer t
99   "*Should we create the *Gnus Help Bug* buffer?")
100
101 (defvar gnus-posting-styles nil
102   "*Alist of styles to use when posting.")
103
104 (defvar gnus-inews-mark-gcc-as-read nil
105   "If non-nil, automatically mark Gcc articles as read.")
106
107 (defcustom gnus-group-posting-charset-alist
108   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
109     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
110     (message-this-is-mail nil nil)
111     (message-this-is-news nil t))
112   "Alist of regexps and permitted unencoded charsets for posting.
113 Each element of the alist has the form (TEST HEADER BODY-LIST), where
114 TEST is either a regular expression matching the newsgroup header or a
115 variable to query,
116 HEADER is the charset which may be left unencoded in the header (nil
117 means encode all charsets),
118 BODY-LIST is a list of charsets which may be encoded using 8bit
119 content-transfer encoding in the body, or one of the special values
120 nil (always encode using quoted-printable) or t (always use 8bit).
121
122 Note that any value other than nil for HEADER infringes some RFCs, so
123 use this option with care."
124   :type '(repeat (list :tag "Permitted unencoded charsets"
125                   (choice :tag "Where"
126                    (regexp :tag "Group")
127                    (const :tag "Mail message" :value message-this-is-mail)
128                    (const :tag "News article" :value message-this-is-news))
129                   (choice :tag "Header"
130                    (const :tag "None" nil)
131                    (symbol :tag "Charset"))
132                   (choice :tag "Body"
133                           (const :tag "Any" :value t)
134                           (const :tag "None" :value nil)
135                           (repeat :tag "Charsets"
136                                   (symbol :tag "Charset")))))
137   :group 'gnus-charset)
138
139 ;;; Internal variables.
140
141 (defvar gnus-inhibit-posting-styles nil
142   "Inhibit the use of posting styles.")
143
144 (defvar gnus-message-buffer "*Mail Gnus*")
145 (defvar gnus-article-copy nil)
146 (defvar gnus-check-before-posting nil)
147 (defvar gnus-last-posting-server nil)
148 (defvar gnus-message-group-art nil)
149
150 (defconst gnus-bug-message
151   "Sending a bug report to the Gnus Towers.
152 ========================================
153
154 The buffer below is a mail buffer.  When you press `C-c C-c', it will
155 be sent to the Gnus Bug Exterminators.
156
157 The thing near the bottom of the buffer is how the environment
158 settings will be included in the mail.  Please do not delete that.
159 They will tell the Bug People what your environment is, so that it
160 will be easier to locate the bugs.
161
162 If you have found a bug that makes Emacs go \"beep\", set
163 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
164 and include the backtrace in your bug report.
165
166 Please describe the bug in annoying, painstaking detail.
167
168 Thank you for your help in stamping out bugs.
169 ")
170
171 (eval-and-compile
172   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
173   (autoload 'news-setup "rnewspost")
174   (autoload 'news-reply-mode "rnewspost")
175   (autoload 'rmail-dont-reply-to "mail-utils")
176   (autoload 'rmail-output "rmailout"))
177
178 \f
179 ;;;
180 ;;; Gnus Posting Functions
181 ;;;
182
183 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
184   "p" gnus-summary-post-news
185   "f" gnus-summary-followup
186   "F" gnus-summary-followup-with-original
187   "c" gnus-summary-cancel-article
188   "s" gnus-summary-supersede-article
189   "r" gnus-summary-reply
190   "y" gnus-summary-yank-message
191   "R" gnus-summary-reply-with-original
192   "w" gnus-summary-wide-reply
193   "W" gnus-summary-wide-reply-with-original
194   "n" gnus-summary-followup-to-mail
195   "N" gnus-summary-followup-to-mail-with-original
196   "m" gnus-summary-mail-other-window
197   "u" gnus-uu-post-news
198   "\M-c" gnus-summary-mail-crosspost-complaint
199   "om" gnus-summary-mail-forward
200   "op" gnus-summary-post-forward
201   "Om" gnus-uu-digest-mail-forward
202   "Op" gnus-uu-digest-post-forward)
203
204 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
205   "b" gnus-summary-resend-bounced-mail
206   ;; "c" gnus-summary-send-draft
207   "r" gnus-summary-resend-message)
208
209 ;;; Internal functions.
210
211 (defvar gnus-article-reply nil)
212 (defmacro gnus-setup-message (config &rest forms)
213   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
214         (buffer (make-symbol "gnus-setup-message-buffer"))
215         (article (make-symbol "gnus-setup-message-article"))
216         (group (make-symbol "gnus-setup-message-group")))
217     `(let ((,winconf (current-window-configuration))
218            (,buffer (buffer-name (current-buffer)))
219            (,article (and gnus-article-reply (gnus-summary-article-number)))
220            (,group gnus-newsgroup-name)
221            (message-header-setup-hook
222             (copy-sequence message-header-setup-hook))
223            (mbl mml-buffer-list)
224            (message-mode-hook (copy-sequence message-mode-hook)))
225        (setq mml-buffer-list nil)
226        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
227        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
228        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
229        (unwind-protect
230            (progn
231              ,@forms)
232          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
233          (gnus-inews-insert-draft-meta-information ,group ,article)
234          (setq gnus-message-buffer (current-buffer))
235          (set (make-local-variable 'gnus-message-group-art)
236               (cons ,group ,article))
237          (set (make-local-variable 'gnus-newsgroup-name) ,group)
238          (gnus-run-hooks 'gnus-message-setup-hook)
239          (if (eq major-mode 'message-mode)
240              (let ((mbl1 mml-buffer-list))
241                (setq mml-buffer-list mbl)  ;; Global value
242                (set (make-local-variable 'mml-buffer-list) mbl1);; Local value
243                (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))
244            (mml-destroy-buffers)
245            (setq mml-buffer-list mbl)))
246        (gnus-add-buffer)
247        (gnus-configure-windows ,config t)
248        (set-buffer-modified-p nil))))
249
250 (defun gnus-inews-insert-draft-meta-information (group article)
251   (save-excursion
252     (when (and group
253                (not (string= group ""))
254                (not (message-fetch-field gnus-draft-meta-information-header)))
255       (goto-char (point-min))
256       (insert gnus-draft-meta-information-header ": (\"" group "\" "
257               (if article (number-to-string article) "\"\"") ")\n"))))
258
259 ;;;###autoload
260 (defun gnus-msg-mail (&optional to subject other-headers continue
261                                 switch-action yank-action send-actions)
262   "Start editing a mail message to be sent.
263 Like `message-mail', but with Gnus paraphernalia, particularly the
264 Gcc: header for archiving purposes."
265   (interactive)
266   (let ((buf (current-buffer))
267         mail-buf)
268     (gnus-setup-message 'message
269       (message-mail to subject other-headers continue
270                     nil yank-action send-actions))
271     (when switch-action
272       (setq mail-buf (current-buffer))
273       (switch-to-buffer buf)
274       (apply switch-action mail-buf nil)))
275   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
276   t)
277
278 ;;;###autoload
279 (defun gnus-button-mailto (address)
280   "Mail to ADDRESS."
281   (set-buffer (gnus-copy-article-buffer))
282   (gnus-setup-message 'message
283     (message-reply address)))
284
285 ;;;###autoload
286 (defun gnus-button-reply (&optional to-address wide)
287   "Like `message-reply'."
288   (interactive)
289   (gnus-setup-message 'message
290     (message-reply to-address wide)))
291
292 ;;;###autoload
293 (define-mail-user-agent 'gnus-user-agent
294   'gnus-msg-mail 'message-send-and-exit
295   'message-kill-buffer 'message-send-hook)
296
297 (defun gnus-setup-posting-charset (group)
298   (let ((alist gnus-group-posting-charset-alist)
299         (group (or group ""))
300         elem)
301     (when group
302       (catch 'found
303         (while (setq elem (pop alist))
304           (when (or (and (stringp (car elem))
305                          (string-match (car elem) group))
306                     (and (gnus-functionp (car elem))
307                          (funcall (car elem) group))
308                     (and (symbolp (car elem))
309                          (symbol-value (car elem))))
310             (throw 'found (cons (cadr elem) (caddr elem)))))))))
311
312 (defun gnus-inews-add-send-actions (winconf buffer article)
313   (make-local-hook 'message-sent-hook)
314   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
315                                  'gnus-inews-do-gcc) nil t)
316   (when gnus-agent
317     (make-local-hook 'message-header-hook)
318     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
319   (setq message-post-method
320         `(lambda (arg)
321            (gnus-post-method arg ,gnus-newsgroup-name)))
322   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
323   (message-add-action
324    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
325   (message-add-action
326    `(when (gnus-buffer-exists-p ,buffer)
327       (save-excursion
328         (set-buffer ,buffer)
329         ,(when article
330            `(gnus-summary-mark-article-as-replied ,article))))
331    'send))
332
333 (put 'gnus-setup-message 'lisp-indent-function 1)
334 (put 'gnus-setup-message 'edebug-form-spec '(form body))
335
336 ;;; Post news commands of Gnus group mode and summary mode
337
338 (defun gnus-group-mail (&optional arg)
339   "Start composing a mail.
340 If ARG, use the group under the point to find a posting style.
341 If ARG is 1, prompt for a group name to find the posting style."
342   (interactive "P")
343   ;; We can't `let' gnus-newsgroup-name here, since that leads
344   ;; to local variables leaking.
345   (let ((group gnus-newsgroup-name)
346         (buffer (current-buffer)))
347     (unwind-protect
348         (progn
349           (setq gnus-newsgroup-name
350                 (if arg
351                     (if (= 1 (prefix-numeric-value arg))
352                         (completing-read "Use posting style of group: "
353                                          gnus-active-hashtb nil
354                                          (gnus-read-active-file-p))
355                       (gnus-group-group-name))
356                   ""))
357           (gnus-setup-message 'message (message-mail)))
358       (save-excursion
359         (set-buffer buffer)
360         (setq gnus-newsgroup-name group)))))
361
362 (defun gnus-group-post-news (&optional arg)
363   "Start composing a news message.
364 If ARG, post to the group under point.
365 If ARG is 1, prompt for a group name."
366   (interactive "P")
367   ;; Bind this variable here to make message mode hooks work ok.
368   (let ((gnus-newsgroup-name
369          (if arg
370              (if (= 1 (prefix-numeric-value arg))
371                  (completing-read "Newsgroup: " gnus-active-hashtb nil
372                                   (gnus-read-active-file-p))
373                (gnus-group-group-name))
374            "")))
375     (gnus-post-news 'post gnus-newsgroup-name)))
376
377 (defun gnus-summary-post-news ()
378   "Start composing a news message."
379   (interactive)
380   (gnus-post-news 'post gnus-newsgroup-name))
381
382 (defun gnus-summary-followup (yank &optional force-news)
383   "Compose a followup to an article.
384 If prefix argument YANK is non-nil, original article is yanked automatically."
385   (interactive
386    (list (and current-prefix-arg
387               (gnus-summary-work-articles 1))))
388   (when yank
389     (gnus-summary-goto-subject (car yank)))
390   (save-window-excursion
391     (gnus-summary-select-article))
392   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
393         (gnus-newsgroup-name gnus-newsgroup-name))
394     ;; Send a followup.
395     (gnus-post-news nil gnus-newsgroup-name
396                     headers gnus-article-buffer
397                     yank nil force-news)))
398
399 (defun gnus-summary-followup-with-original (n &optional force-news)
400   "Compose a followup to an article and include the original article."
401   (interactive "P")
402   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
403
404 (defun gnus-summary-followup-to-mail (&optional arg)
405   "Followup to the current mail message via news."
406   (interactive
407    (list (and current-prefix-arg
408               (gnus-summary-work-articles 1))))
409   (gnus-summary-followup arg t))
410
411 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
412   "Followup to the current mail message via news."
413   (interactive "P")
414   (gnus-summary-followup (gnus-summary-work-articles arg) t))
415
416 (defun gnus-inews-yank-articles (articles)
417   (let (beg article)
418     (message-goto-body)
419     (while (setq article (pop articles))
420       (save-window-excursion
421         (set-buffer gnus-summary-buffer)
422         (gnus-summary-select-article nil nil nil article)
423         (gnus-summary-remove-process-mark article))
424       (gnus-copy-article-buffer)
425       (let ((message-reply-buffer gnus-article-copy)
426             (message-reply-headers
427              (with-current-buffer gnus-article-copy
428                ;; The headers are decoded.
429                (nnheader-parse-head t))))
430         (message-yank-original)
431         (setq beg (or beg (mark t))))
432       (when articles
433         (insert "\n")))
434     (push-mark)
435     (goto-char beg)))
436
437 (defun gnus-summary-cancel-article (&optional n symp)
438   "Cancel an article you posted.
439 Uses the process-prefix convention.  If given the symbolic
440 prefix `a', cancel using the standard posting method; if not
441 post using the current select method."
442   (interactive (gnus-interactive "P\ny"))
443   (let ((articles (gnus-summary-work-articles n))
444         (message-post-method
445          `(lambda (arg)
446             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
447         article)
448     (while (setq article (pop articles))
449       (when (gnus-summary-select-article t nil nil article)
450         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
451                 (message-cancel-news))
452           (gnus-summary-mark-as-read article gnus-canceled-mark)
453           (gnus-cache-remove-article 1))
454         (gnus-article-hide-headers-if-wanted))
455       (gnus-summary-remove-process-mark article))))
456
457 (defun gnus-summary-supersede-article ()
458   "Compose an article that will supersede a previous article.
459 This is done simply by taking the old article and adding a Supersedes
460 header line with the old Message-ID."
461   (interactive)
462   (let ((article (gnus-summary-article-number)))
463     (gnus-setup-message 'reply-yank
464       (gnus-summary-select-article t)
465       (set-buffer gnus-original-article-buffer)
466       (message-supersede)
467       (push
468        `((lambda ()
469            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
470              (save-excursion
471                (set-buffer ,gnus-summary-buffer)
472                (gnus-cache-possibly-remove-article ,article nil nil nil t)
473                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
474        message-send-actions))))
475
476 \f
477
478 (defun gnus-copy-article-buffer (&optional article-buffer)
479   ;; make a copy of the article buffer with all text properties removed
480   ;; this copy is in the buffer gnus-article-copy.
481   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
482   ;; this buffer should be passed to all mail/news reply/post routines.
483   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
484   (save-excursion
485     (set-buffer gnus-article-copy)
486     (mm-enable-multibyte))
487   (let ((article-buffer (or article-buffer gnus-article-buffer))
488         end beg)
489     (if (not (and (get-buffer article-buffer)
490                   (gnus-buffer-exists-p article-buffer)))
491         (error "Can't find any article buffer")
492       (save-excursion
493         (set-buffer article-buffer)
494         (let ((gnus-newsgroup-charset (or gnus-article-charset
495                                           gnus-newsgroup-charset))
496               (gnus-newsgroup-ignored-charsets
497                (or gnus-article-ignored-charsets
498                    gnus-newsgroup-ignored-charsets)))
499           (save-restriction
500             ;; Copy over the (displayed) article buffer, delete
501             ;; hidden text and remove text properties.
502             (widen)
503             (copy-to-buffer gnus-article-copy (point-min) (point-max))
504             (set-buffer gnus-article-copy)
505             (gnus-article-delete-text-of-type 'annotation)
506             (gnus-remove-text-with-property 'gnus-prev)
507             (gnus-remove-text-with-property 'gnus-next)
508             (insert
509              (prog1
510                  (buffer-substring-no-properties (point-min) (point-max))
511                (erase-buffer)))
512             ;; Find the original headers.
513             (set-buffer gnus-original-article-buffer)
514             (goto-char (point-min))
515             (while (looking-at message-unix-mail-delimiter)
516               (forward-line 1))
517             (setq beg (point))
518             (setq end (or (search-forward "\n\n" nil t) (point)))
519             ;; Delete the headers from the displayed articles.
520             (set-buffer gnus-article-copy)
521             (delete-region (goto-char (point-min))
522                            (or (search-forward "\n\n" nil t) (point-max)))
523             ;; Insert the original article headers.
524             (insert-buffer-substring gnus-original-article-buffer beg end)
525             (article-decode-encoded-words))))
526       gnus-article-copy)))
527
528 (defun gnus-post-news (post &optional group header article-buffer yank subject
529                             force-news)
530   (when article-buffer
531     (gnus-copy-article-buffer))
532   (let ((gnus-article-reply article-buffer)
533         (add-to-list gnus-add-to-list))
534     (gnus-setup-message (cond (yank 'reply-yank)
535                               (article-buffer 'reply)
536                               (t 'message))
537       (let* ((group (or group gnus-newsgroup-name))
538              (charset (gnus-group-name-charset nil group))
539              (pgroup group)
540              to-address to-group mailing-list to-list
541              newsgroup-p)
542         (when group
543           (setq to-address (gnus-parameter-to-address group)
544                 to-group (gnus-group-find-parameter group 'to-group)
545                 to-list (gnus-parameter-to-list group)
546                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
547                 mailing-list (when gnus-mailing-list-groups
548                                (string-match gnus-mailing-list-groups group))
549                 group (gnus-group-name-decode (gnus-group-real-name group)
550                                               charset)))
551         (if (or (and to-group
552                      (gnus-news-group-p to-group))
553                 newsgroup-p
554                 force-news
555                 (and (gnus-news-group-p
556                       (or pgroup gnus-newsgroup-name)
557                       (if header (mail-header-number header)
558                         gnus-current-article))
559                      (not mailing-list)
560                      (not to-list)
561                      (not to-address)))
562             ;; This is news.
563             (if post
564                 (message-news (or to-group group))
565               (set-buffer gnus-article-copy)
566               (gnus-msg-treat-broken-reply-to)
567               (message-followup (if (or newsgroup-p force-news) nil to-group)))
568           ;; The is mail.
569           (if post
570               (progn
571                 (message-mail (or to-address to-list))
572                 ;; Arrange for mail groups that have no `to-address' to
573                 ;; get that when the user sends off the mail.
574                 (when (and (not to-list)
575                            (not to-address)
576                            add-to-list)
577                   (push (list 'gnus-inews-add-to-address pgroup)
578                         message-send-actions)))
579             (set-buffer gnus-article-copy)
580             (gnus-msg-treat-broken-reply-to)
581             (message-wide-reply to-address)))
582         (when yank
583           (gnus-inews-yank-articles yank))))))
584
585 (defun gnus-msg-treat-broken-reply-to ()
586   "Remove the Reply-to header iff broken-reply-to."
587   (when (gnus-group-find-parameter
588          gnus-newsgroup-name 'broken-reply-to)
589     (save-restriction
590       (message-narrow-to-head)
591       (message-remove-header "reply-to"))))
592
593 (defun gnus-post-method (arg group &optional silent)
594   "Return the posting method based on GROUP and ARG.
595 If SILENT, don't prompt the user."
596   (let ((group-method (gnus-find-method-for-group group)))
597     (cond
598      ;; If the group-method is nil (which shouldn't happen) we use
599      ;; the default method.
600      ((null group-method)
601       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
602           gnus-select-method message-post-method))
603      ;; We want the inverse of the default
604      ((and arg (not (eq arg 0)))
605       (if (eq gnus-post-method 'active)
606           gnus-select-method
607         group-method))
608      ;; We query the user for a post method.
609      ((or arg
610           (and gnus-post-method
611                (not (eq gnus-post-method 'current))
612                (listp (car gnus-post-method))))
613       (let* ((methods
614               ;; Collect all methods we know about.
615               (append
616                (when (and gnus-post-method
617                           (not (eq gnus-post-method 'current)))
618                  (if (listp (car gnus-post-method))
619                      gnus-post-method
620                    (list gnus-post-method)))
621                gnus-secondary-select-methods
622                (mapcar 'cdr gnus-server-alist)
623                (mapcar 'car gnus-opened-servers)
624                (list gnus-select-method)
625                (list group-method)))
626              method-alist post-methods method)
627         ;; Weed out all mail methods.
628         (while methods
629           (setq method (gnus-server-get-method "" (pop methods)))
630           (when (and (or (gnus-method-option-p method 'post)
631                          (gnus-method-option-p method 'post-mail))
632                      (not (member method post-methods)))
633             (push method post-methods)))
634         ;; Create a name-method alist.
635         (setq method-alist
636               (mapcar
637                (lambda (m)
638                  (if (equal (cadr m) "")
639                      (list (symbol-name (car m)) m)
640                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
641                post-methods))
642         ;; Query the user.
643         (cadr
644          (assoc
645           (setq gnus-last-posting-server
646                 (if (and silent
647                          gnus-last-posting-server)
648                     ;; Just use the last value.
649                     gnus-last-posting-server
650                   (completing-read
651                    "Posting method: " method-alist nil t
652                    (cons (or gnus-last-posting-server "") 0))))
653           method-alist))))
654      ;; Override normal method.
655      ((and (eq gnus-post-method 'current)
656            (not (eq (car group-method) 'nndraft))
657            (gnus-get-function group-method 'request-post t)
658            (not arg))
659       group-method)
660      ((and gnus-post-method
661            (not (eq gnus-post-method 'current)))
662       gnus-post-method)
663      ;; Use the normal select method.
664      (t gnus-select-method))))
665
666 \f
667
668 ;; Dummies to avoid byte-compile warning.
669 (eval-when-compile
670   (defvar nnspool-rejected-article-hook)
671   (defvar xemacs-codename))
672
673 (defun gnus-extended-version ()
674   "Stringified Gnus version and Emacs version."
675   (interactive)
676   (concat
677    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
678    " (" gnus-version ")"
679    " "
680    (cond
681     ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
682      (concat "Emacs/" (match-string 1 emacs-version)))
683     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
684                    emacs-version)
685      (concat (match-string 1 emacs-version)
686              (format "/%d.%d" emacs-major-version emacs-minor-version)
687              (if (match-beginning 3)
688                  (match-string 3 emacs-version)
689                "")
690              (if (boundp 'xemacs-codename)
691                  (concat " (" xemacs-codename ")")
692                "")))
693     (t emacs-version))))
694
695 \f
696 ;;;
697 ;;; Gnus Mail Functions
698 ;;;
699
700 ;;; Mail reply commands of Gnus summary mode
701
702 (defun gnus-summary-reply (&optional yank wide)
703   "Start composing a reply mail to the current message.
704 If prefix argument YANK is non-nil, the original article is yanked
705 automatically."
706   (interactive
707    (list (and current-prefix-arg
708               (gnus-summary-work-articles 1))))
709   ;; Stripping headers should be specified with mail-yank-ignored-headers.
710   (when yank
711     (gnus-summary-goto-subject (car yank)))
712   (let ((gnus-article-reply t))
713     (gnus-setup-message (if yank 'reply-yank 'reply)
714       (gnus-summary-select-article)
715       (set-buffer (gnus-copy-article-buffer))
716       (gnus-msg-treat-broken-reply-to)
717       (save-restriction
718         (message-narrow-to-head)
719         (goto-char (point-max)))
720       (mml-quote-region (point) (point-max))
721       (message-reply nil wide)
722       (when yank
723         (gnus-inews-yank-articles yank)))))
724
725 (defun gnus-summary-reply-with-original (n &optional wide)
726   "Start composing a reply mail to the current message.
727 The original article will be yanked."
728   (interactive "P")
729   (gnus-summary-reply (gnus-summary-work-articles n) wide))
730
731 (defun gnus-summary-wide-reply (&optional yank)
732   "Start composing a wide reply mail to the current message.
733 If prefix argument YANK is non-nil, the original article is yanked
734 automatically."
735   (interactive
736    (list (and current-prefix-arg
737               (gnus-summary-work-articles 1))))
738   (gnus-summary-reply yank t))
739
740 (defun gnus-summary-wide-reply-with-original (n)
741   "Start composing a wide reply mail to the current message.
742 The original article will be yanked."
743   (interactive "P")
744   (gnus-summary-reply-with-original n t))
745
746 (defun gnus-summary-mail-forward (&optional arg post)
747   "Forward the current message to another user.
748 If ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
749 if ARG is 1, decode the message and forward directly inline;
750 if ARG is 2, forward message as an rfc822 MIME section;
751 if ARG is 3, decode message and forward as an rfc822 MIME section;
752 if ARG is 4, forward message directly inline;
753 otherwise, use flipped `message-forward-as-mime'.
754 If POST, post instead of mail."
755   (interactive "P")
756   (let ((message-forward-as-mime message-forward-as-mime)
757         (message-forward-show-mml message-forward-show-mml))
758     (cond
759      ((null arg))
760      ((eq arg 1) (setq message-forward-as-mime nil
761                        message-forward-show-mml t))
762      ((eq arg 2) (setq message-forward-as-mime t
763                        message-forward-show-mml nil))
764      ((eq arg 3) (setq message-forward-as-mime t
765                        message-forward-show-mml t))
766      ((eq arg 4) (setq message-forward-as-mime nil
767                        message-forward-show-mml nil))
768      (t (setq message-forward-as-mime (not message-forward-as-mime))))
769     (gnus-setup-message 'forward
770       (gnus-summary-select-article)
771       (let ((mail-parse-charset gnus-newsgroup-charset)
772             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
773         (set-buffer gnus-original-article-buffer)
774         (message-forward post)))))
775
776 (defun gnus-summary-resend-message (address n)
777   "Resend the current article to ADDRESS."
778   (interactive
779    (list (message-read-from-minibuffer "Resend message(s) to: ")
780          current-prefix-arg))
781   (let ((articles (gnus-summary-work-articles n))
782         article)
783     (while (setq article (pop articles))
784       (gnus-summary-select-article nil nil nil article)
785       (save-excursion
786         (set-buffer gnus-original-article-buffer)
787         (message-resend address)))))
788
789 (defun gnus-summary-post-forward (&optional arg)
790   "Forward the current article to a newsgroup.
791 See `gnus-summary-mail-forward' for ARG."
792   (interactive "P")
793   (gnus-summary-mail-forward arg t))
794
795 (defvar gnus-nastygram-message
796   "The following article was inappropriately posted to %s.\n\n"
797   "Format string to insert in nastygrams.
798 The current group name will be inserted at \"%s\".")
799
800 (defun gnus-summary-mail-nastygram (n)
801   "Send a nastygram to the author of the current article."
802   (interactive "P")
803   (when (or gnus-expert-user
804             (gnus-y-or-n-p
805              "Really send a nastygram to the author of the current article? "))
806     (let ((group gnus-newsgroup-name))
807       (gnus-summary-reply-with-original n)
808       (set-buffer gnus-message-buffer)
809       (message-goto-body)
810       (insert (format gnus-nastygram-message group))
811       (message-send-and-exit))))
812
813 (defun gnus-summary-mail-crosspost-complaint (n)
814   "Send a complaint about crossposting to the current article(s)."
815   (interactive "P")
816   (let ((articles (gnus-summary-work-articles n))
817         article)
818     (while (setq article (pop articles))
819       (set-buffer gnus-summary-buffer)
820       (gnus-summary-goto-subject article)
821       (let ((group (gnus-group-real-name gnus-newsgroup-name))
822             newsgroups followup-to)
823         (gnus-summary-select-article)
824         (set-buffer gnus-original-article-buffer)
825         (if (and (<= (length (message-tokenize-header
826                               (setq newsgroups
827                                     (mail-fetch-field "newsgroups"))
828                               ", "))
829                      1)
830                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
831                      (not (member group (message-tokenize-header
832                                          followup-to ", ")))))
833             (if followup-to
834                 (gnus-message 1 "Followup-to restricted")
835               (gnus-message 1 "Not a crossposted article"))
836           (set-buffer gnus-summary-buffer)
837           (gnus-summary-reply-with-original 1)
838           (set-buffer gnus-message-buffer)
839           (message-goto-body)
840           (insert (format gnus-crosspost-complaint newsgroups group))
841           (message-goto-subject)
842           (re-search-forward " *$")
843           (replace-match " (crosspost notification)" t t)
844           (gnus-deactivate-mark)
845           (when (gnus-y-or-n-p "Send this complaint? ")
846             (message-send-and-exit)))))))
847
848 (defun gnus-summary-mail-other-window ()
849   "Compose mail in other window."
850   (interactive)
851   (gnus-setup-message 'message
852     (message-mail)))
853
854 (defun gnus-mail-parse-comma-list ()
855   (let (accumulated
856         beg)
857     (skip-chars-forward " ")
858     (while (not (eobp))
859       (setq beg (point))
860       (skip-chars-forward "^,")
861       (while (zerop
862               (save-excursion
863                 (save-restriction
864                   (let ((i 0))
865                     (narrow-to-region beg (point))
866                     (goto-char beg)
867                     (logand (progn
868                               (while (search-forward "\"" nil t)
869                                 (incf i))
870                               (if (zerop i) 2 i))
871                             2)))))
872         (skip-chars-forward ",")
873         (skip-chars-forward "^,"))
874       (skip-chars-backward " ")
875       (push (buffer-substring beg (point))
876             accumulated)
877       (skip-chars-forward "^,")
878       (skip-chars-forward ", "))
879     accumulated))
880
881 (defun gnus-inews-add-to-address (group)
882   (let ((to-address (mail-fetch-field "to")))
883     (when (and to-address
884                (gnus-alive-p))
885       ;; This mail group doesn't have a `to-list', so we add one
886       ;; here.  Magic!
887       (when (gnus-y-or-n-p
888              (format "Do you want to add this as `to-list': %s " to-address))
889         (gnus-group-add-parameter group (cons 'to-list to-address))))))
890
891 (defun gnus-put-message ()
892   "Put the current message in some group and return to Gnus."
893   (interactive)
894   (let ((reply gnus-article-reply)
895         (winconf gnus-prev-winconf)
896         (group gnus-newsgroup-name))
897
898     (or (and group (not (gnus-group-read-only-p group)))
899         (setq group (read-string "Put in group: " nil
900                                  (gnus-writable-groups))))
901     (when (gnus-gethash group gnus-newsrc-hashtb)
902       (error "No such group: %s" group))
903
904     (save-excursion
905       (save-restriction
906         (widen)
907         (message-narrow-to-headers)
908         (let (gnus-deletable-headers)
909           (if (message-news-p)
910               (message-generate-headers message-required-news-headers)
911             (message-generate-headers message-required-mail-headers)))
912         (goto-char (point-max))
913         (insert "Gcc: " group "\n")
914         (widen)))
915
916     (gnus-inews-do-gcc)
917
918     (when (get-buffer gnus-group-buffer)
919       (when (gnus-buffer-exists-p (car-safe reply))
920         (set-buffer (car reply))
921         (and (cdr reply)
922              (gnus-summary-mark-article-as-replied
923               (cdr reply))))
924       (when winconf
925         (set-window-configuration winconf)))))
926
927 (defun gnus-article-mail (yank)
928   "Send a reply to the address near point.
929 If YANK is non-nil, include the original article."
930   (interactive "P")
931   (let ((address
932          (buffer-substring
933           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
934           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
935     (when address
936       (message-reply address)
937       (when yank
938         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
939
940 (defvar nntp-server-type)
941 (defun gnus-bug ()
942   "Send a bug report to the Gnus maintainers."
943   (interactive)
944   (unless (gnus-alive-p)
945     (error "Gnus has been shut down"))
946   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
947     (unless (message-mail-user-agent)
948       (delete-other-windows)
949       (when gnus-bug-create-help-buffer
950         (switch-to-buffer "*Gnus Help Bug*")
951         (erase-buffer)
952         (insert gnus-bug-message)
953         (goto-char (point-min)))
954       (message-pop-to-buffer "*Gnus Bug*"))
955     (let ((message-this-is-mail t))
956       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
957     (when gnus-bug-create-help-buffer
958       (push `(gnus-bug-kill-buffer) message-send-actions))
959     (goto-char (point-min))
960     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
961     (forward-line 1)
962     (insert (gnus-version) "\n"
963             (emacs-version) "\n")
964     (when (and (boundp 'nntp-server-type)
965                (stringp nntp-server-type))
966       (insert nntp-server-type))
967     (insert "\n\n\n\n\n")
968     (let (text)
969       (save-excursion
970         (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
971         (gnus-debug)
972         (setq text (buffer-string)))
973       (insert "<#part type=application/x-emacs-lisp disposition=inline description=\"User settings\">\n" text "\n<#/part>"))
974     (goto-char (point-min))
975     (search-forward "Subject: " nil t)
976     (message "")))
977
978 (defun gnus-bug-kill-buffer ()
979   (when (get-buffer "*Gnus Help Bug*")
980     (kill-buffer "*Gnus Help Bug*")))
981
982 (defun gnus-summary-yank-message (buffer n)
983   "Yank the current article into a composed message."
984   (interactive
985    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
986          current-prefix-arg))
987   (gnus-summary-iterate n
988     (let ((gnus-display-mime-function nil)
989           (gnus-inhibit-treatment t))
990       (gnus-summary-select-article))
991     (save-excursion
992       (set-buffer buffer)
993       (message-yank-buffer gnus-article-buffer))))
994
995 (defun gnus-debug ()
996   "Attempts to go through the Gnus source file and report what variables have been changed.
997 The source file has to be in the Emacs load path."
998   (interactive)
999   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1000                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1001                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1002                  "nnmail.el" "message.el"))
1003         (point (point))
1004         file expr olist sym)
1005     (gnus-message 4 "Please wait while we snoop your variables...")
1006     (sit-for 0)
1007     ;; Go through all the files looking for non-default values for variables.
1008     (save-excursion
1009       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1010       (while files
1011         (erase-buffer)
1012         (when (and (setq file (locate-library (pop files)))
1013                    (file-exists-p file))
1014           (insert-file-contents file)
1015           (goto-char (point-min))
1016           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1017               (gnus-message 4 "Malformed sources in file %s" file)
1018             (narrow-to-region (point-min) (point))
1019             (goto-char (point-min))
1020             (while (setq expr (ignore-errors (read (current-buffer))))
1021               (ignore-errors
1022                 (and (or (eq (car expr) 'defvar)
1023                          (eq (car expr) 'defcustom))
1024                      (stringp (nth 3 expr))
1025                      (or (not (boundp (nth 1 expr)))
1026                          (not (equal (eval (nth 2 expr))
1027                                      (symbol-value (nth 1 expr)))))
1028                      (push (nth 1 expr) olist)))))))
1029       (kill-buffer (current-buffer)))
1030     (when (setq olist (nreverse olist))
1031       (insert "------------------ Environment follows ------------------\n\n"))
1032     (while olist
1033       (if (boundp (car olist))
1034           (condition-case ()
1035               (pp `(setq ,(car olist)
1036                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1037                                   (and (symbolp sym)
1038                                        (not (or (eq sym nil)
1039                                                 (eq sym t)))))
1040                               (list 'quote (symbol-value (car olist)))
1041                             (symbol-value (car olist))))
1042                   (current-buffer))
1043             (error
1044              (format "(setq %s 'whatever)\n" (car olist))))
1045         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1046       (setq olist (cdr olist)))
1047     (insert "\n\n")
1048     ;; Remove any control chars - they seem to cause trouble for some
1049     ;; mailers.  (Byte-compiled output from the stuff above.)
1050     (goto-char point)
1051     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1052       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1053                      t t))))
1054
1055 ;;; Treatment of rejected articles.
1056 ;;; Bounced mail.
1057
1058 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1059   "Re-mail the current message.
1060 This only makes sense if the current message is a bounce message than
1061 contains some mail you have written which has been bounced back to
1062 you.
1063 If FETCH, try to fetch the article that this is a reply to, if indeed
1064 this is a reply."
1065   (interactive "P")
1066   (gnus-summary-select-article t)
1067   (set-buffer gnus-original-article-buffer)
1068   (gnus-setup-message 'compose-bounce
1069     (let* ((references (mail-fetch-field "references"))
1070            (parent (and references (gnus-parent-id references))))
1071       (message-bounce)
1072       ;; If there are references, we fetch the article we answered to.
1073       (and fetch parent
1074            (gnus-summary-refer-article parent)
1075            (gnus-summary-show-all-headers)))))
1076
1077 ;;; Gcc handling.
1078
1079 (defun gnus-inews-group-method (group)
1080   (cond ((and (null (gnus-get-info group))
1081               (eq (car gnus-message-archive-method)
1082                   (car
1083                    (gnus-server-to-method
1084                     (gnus-group-method group)))))
1085          ;; If the group doesn't exist, we assume
1086          ;; it's an archive group...
1087          gnus-message-archive-method)
1088         ;; Use the method.
1089         ((gnus-info-method (gnus-get-info group))
1090          (gnus-info-method (gnus-get-info group)))
1091         ;; Find the method.
1092         (t (gnus-group-method group))))
1093
1094 ;; Do Gcc handling, which copied the message over to some group.
1095 (defun gnus-inews-do-gcc (&optional gcc)
1096   (interactive)
1097   (when (gnus-alive-p)
1098     (save-excursion
1099       (save-restriction
1100         (message-narrow-to-headers)
1101         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1102               (cur (current-buffer))
1103               groups group method group-art)
1104           (when gcc
1105             (message-remove-header "gcc")
1106             (widen)
1107             (setq groups (message-unquote-tokens
1108                           (message-tokenize-header gcc " ,")))
1109             ;; Copy the article over to some group(s).
1110             (while (setq group (pop groups))
1111               (gnus-check-server
1112                (setq method (gnus-inews-group-method group)))
1113               (unless (gnus-request-group group t method)
1114                 (gnus-request-create-group group method))
1115               (save-excursion
1116                 (nnheader-set-temp-buffer " *acc*")
1117                 (insert-buffer-substring cur)
1118                 (message-encode-message-body)
1119                 (save-restriction
1120                   (message-narrow-to-headers)
1121                   (let ((mail-parse-charset message-default-charset)
1122                         (rfc2047-header-encoding-alist
1123                          (cons '("Newsgroups" . default)
1124                                rfc2047-header-encoding-alist)))
1125                     (mail-encode-encoded-word-buffer)))
1126                 (goto-char (point-min))
1127                 (when (re-search-forward
1128                        (concat "^" (regexp-quote mail-header-separator) "$")
1129                        nil t)
1130                   (replace-match "" t t ))
1131                 (unless (setq group-art
1132                               (gnus-request-accept-article group method t t))
1133                   (gnus-message 1 "Couldn't store article in group %s: %s"
1134                                 group (gnus-status-message method))
1135                   (sit-for 2))
1136                 (when (and group-art gnus-inews-mark-gcc-as-read)
1137                   (let ((active (gnus-active group)))
1138                     (if active
1139                         (if (< (cdr active) (cdr group-art))
1140                             (gnus-set-active group (cons (car active)
1141                                                          (cdr group-art))))
1142                       (gnus-activate-group group)))
1143                   (let ((buffer (gnus-summary-buffer-name group))
1144                         (mark gnus-read-mark)
1145                         (article (cdr group-art)))
1146                     (unless
1147                         (and
1148                          (get-buffer buffer)
1149                          (with-current-buffer buffer
1150                            (when gnus-newsgroup-prepared
1151                              (when (and gnus-newsgroup-auto-expire
1152                                         (memq mark gnus-auto-expirable-marks))
1153                                (setq mark gnus-expirable-mark))
1154                              (setq mark (gnus-request-update-mark
1155                                          group article mark))
1156                              (gnus-mark-article-as-read article mark)
1157                              (setq gnus-newsgroup-active (gnus-active group))
1158                              t)))
1159                       (gnus-group-make-articles-read group
1160                                                      (list article))
1161                       (when (gnus-group-auto-expirable-p group)
1162                         (gnus-add-marked-articles
1163                          group 'expire (list article))))))
1164                 (kill-buffer (current-buffer))))))))))
1165
1166 (defun gnus-inews-insert-gcc ()
1167   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1168   (save-excursion
1169     (save-restriction
1170       (message-narrow-to-headers)
1171       (let* ((group gnus-outgoing-message-group)
1172              (gcc (cond
1173                    ((gnus-functionp group)
1174                     (funcall group))
1175                    ((or (stringp group) (list group))
1176                     group))))
1177         (when gcc
1178           (insert "Gcc: "
1179                   (if (stringp gcc) gcc
1180                     (mapconcat 'identity gcc " "))
1181                   "\n"))))))
1182
1183 (defun gnus-inews-insert-archive-gcc (&optional group)
1184   "Insert the Gcc to say where the article is to be archived."
1185   (let* ((var gnus-message-archive-group)
1186          (group (or group gnus-newsgroup-name ""))
1187          (gcc-self-val
1188           (and gnus-newsgroup-name
1189                (not (equal gnus-newsgroup-name ""))
1190                (gnus-group-find-parameter
1191                 gnus-newsgroup-name 'gcc-self)))
1192          result
1193          (groups
1194           (cond
1195            ((null gnus-message-archive-method)
1196             ;; Ignore.
1197             nil)
1198            ((stringp var)
1199             ;; Just a single group.
1200             (list var))
1201            ((null var)
1202             ;; We don't want this.
1203             nil)
1204            ((and (listp var) (stringp (car var)))
1205             ;; A list of groups.
1206             var)
1207            ((gnus-functionp var)
1208             ;; A function.
1209             (funcall var group))
1210            (t
1211             ;; An alist of regexps/functions/forms.
1212             (while (and var
1213                         (not
1214                          (setq result
1215                                (cond
1216                                 ((stringp (caar var))
1217                                  ;; Regexp.
1218                                  (when (string-match (caar var) group)
1219                                    (cdar var)))
1220                                 ((gnus-functionp (car var))
1221                                  ;; Function.
1222                                  (funcall (car var) group))
1223                                 (t
1224                                  (eval (car var)))))))
1225               (setq var (cdr var)))
1226             result)))
1227          name)
1228     (when (or groups gcc-self-val)
1229       (when (stringp groups)
1230         (setq groups (list groups)))
1231       (save-excursion
1232         (save-restriction
1233           (message-narrow-to-headers)
1234           (goto-char (point-max))
1235           (insert "Gcc: ")
1236           (if gcc-self-val
1237               ;; Use the `gcc-self' param value instead.
1238               (progn
1239                 (insert
1240                  (if (stringp gcc-self-val)
1241                      gcc-self-val
1242                    group))
1243                 (if (not (eq gcc-self-val 'none))
1244                     (insert "\n")
1245                   (progn
1246                     (beginning-of-line)
1247                     (kill-line))))
1248             ;; Use the list of groups.
1249             (while (setq name (pop groups))
1250               (insert (if (string-match ":" name)
1251                           name
1252                         (gnus-group-prefixed-name
1253                          name gnus-message-archive-method)))
1254               (when groups
1255                 (insert " ")))
1256             (insert "\n")))))))
1257
1258 ;;; Posting styles.
1259
1260 (defun gnus-configure-posting-styles ()
1261   "Configure posting styles according to `gnus-posting-styles'."
1262   (unless gnus-inhibit-posting-styles
1263     (let ((group (or gnus-newsgroup-name ""))
1264           (styles gnus-posting-styles)
1265           style match variable attribute value v results
1266           filep name address element)
1267       ;; If the group has a posting-style parameter, add it at the end with a
1268       ;; regexp matching everything, to be sure it takes precedence over all
1269       ;; the others.
1270       (when gnus-newsgroup-name
1271         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1272           (when tmp-style
1273             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1274       ;; Go through all styles and look for matches.
1275       (dolist (style styles)
1276         (setq match (pop style))
1277         (goto-char (point-min))
1278         (when (cond
1279                ((stringp match)
1280                 ;; Regexp string match on the group name.
1281                 (string-match match group))
1282                ((eq match 'header)
1283                 (and (gnus-buffer-live-p gnus-article-copy)
1284                      (with-current-buffer gnus-article-copy
1285                        (let ((header (message-fetch-field (pop style))))
1286                          (and header
1287                               (string-match (pop style) header))))))
1288                ((or (symbolp match)
1289                     (gnus-functionp match))
1290                 (cond
1291                  ((gnus-functionp match)
1292                   ;; Function to be called.
1293                   (funcall match))
1294                  ((boundp match)
1295                   ;; Variable to be checked.
1296                   (symbol-value match))))
1297                ((listp match)
1298                 ;; This is a form to be evaled.
1299                 (eval match)))
1300           ;; We have a match, so we set the variables.
1301           (dolist (attribute style)
1302             (setq element (pop attribute)
1303                   variable nil
1304                   filep nil)
1305             (setq value
1306                   (cond
1307                    ((eq (car attribute) :file)
1308                     (setq filep t)
1309                     (cadr attribute))
1310                    ((eq (car attribute) :value)
1311                     (cadr attribute))
1312                    (t
1313                     (car attribute))))
1314             ;; We get the value.
1315             (setq v
1316                   (cond
1317                    ((stringp value)
1318                     value)
1319                    ((or (symbolp value)
1320                         (gnus-functionp value))
1321                     (cond ((gnus-functionp value)
1322                            (funcall value))
1323                           ((boundp value)
1324                            (symbol-value value))))
1325                    ((listp value)
1326                     (eval value))))
1327             ;; Translate obsolescent value.
1328             (when (eq element 'signature-file)
1329               (setq element 'signature
1330                     filep t))
1331             ;; Get the contents of file elems.
1332             (when (and filep v)
1333               (setq v (with-temp-buffer
1334                         (insert-file-contents v)
1335                         (buffer-string))))
1336             (setq results (delq (assoc element results) results))
1337             (push (cons element v) results))))
1338       ;; Now we have all the styles, so we insert them.
1339       (setq name (assq 'name results)
1340             address (assq 'address results))
1341       (setq results (delq name (delq address results)))
1342       (make-local-variable 'message-setup-hook)
1343       (dolist (result results)
1344         (add-hook 'message-setup-hook
1345                   (cond
1346                    ((eq 'eval (car result))
1347                     'ignore)
1348                    ((eq 'body (car result))
1349                     `(lambda ()
1350                        (save-excursion
1351                          (message-goto-body)
1352                          (insert ,(cdr result)))))
1353                    ((eq 'signature (car result))
1354                     (set (make-local-variable 'message-signature) nil)
1355                     (set (make-local-variable 'message-signature-file) nil)
1356                     (if (not (cdr result))
1357                         'ignore
1358                       `(lambda ()
1359                          (save-excursion
1360                            (let ((message-signature ,(cdr result)))
1361                              (when message-signature
1362                                (message-insert-signature)))))))
1363                    (t
1364                     (let ((header
1365                            (if (symbolp (car result))
1366                                (capitalize (symbol-name (car result)))
1367                              (car result))))
1368                       `(lambda ()
1369                          (save-excursion
1370                            (message-remove-header ,header)
1371                            (let ((value ,(cdr result)))
1372                              (when value
1373                                (message-goto-eoh)
1374                                (insert ,header ": " value "\n"))))))))))
1375       (when (or name address)
1376         (add-hook 'message-setup-hook
1377                   `(lambda ()
1378                      (set (make-local-variable 'user-mail-address)
1379                           ,(or (cdr address) user-mail-address))
1380                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1381                            (user-mail-address
1382                             ,(or (cdr address) user-mail-address)))
1383                        (save-excursion
1384                          (message-remove-header "From")
1385                          (message-goto-eoh)
1386                          (insert "From: " (message-make-from) "\n")))))))))
1387
1388 ;;; Allow redefinition of functions.
1389
1390 (gnus-ems-redefine)
1391
1392 (provide 'gnus-msg)
1393
1394 ;;; gnus-msg.el ends here