2001-01-21 00: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 (&rest args)
261   "Start editing a mail message to be sent.
262 Like `message-mail', but with Gnus paraphernalia, particularly the
263 Gcc: header for archiving purposes."
264   (interactive)
265   (gnus-setup-message 'message
266     (apply 'message-mail args))
267   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
268   t)
269
270 ;;;###autoload
271 (defun gnus-button-mailto (address)
272   "Mail to ADDRESS."
273   (set-buffer (gnus-copy-article-buffer))
274   (gnus-setup-message 'message
275     (message-reply address)))
276
277 ;;;###autoload
278 (defun gnus-button-reply (&optional to-address wide)
279   "Like `message-reply'."
280   (interactive)
281   (gnus-setup-message 'message
282     (message-reply to-address wide)))
283
284 ;;;###autoload
285 (define-mail-user-agent 'gnus-user-agent
286   'gnus-msg-mail 'message-send-and-exit
287   'message-kill-buffer 'message-send-hook)
288
289 (defun gnus-setup-posting-charset (group)
290   (let ((alist gnus-group-posting-charset-alist)
291         (group (or group ""))
292         elem)
293     (when group
294       (catch 'found
295         (while (setq elem (pop alist))
296           (when (or (and (stringp (car elem))
297                          (string-match (car elem) group))
298                     (and (gnus-functionp (car elem))
299                          (funcall (car elem) group))
300                     (and (symbolp (car elem))
301                          (symbol-value (car elem))))
302             (throw 'found (cons (cadr elem) (caddr elem)))))))))
303
304 (defun gnus-inews-add-send-actions (winconf buffer article)
305   (make-local-hook 'message-sent-hook)
306   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
307                                  'gnus-inews-do-gcc) nil t)
308   (when gnus-agent
309     (make-local-hook 'message-header-hook)
310     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
311   (setq message-post-method
312         `(lambda (arg)
313            (gnus-post-method arg ,gnus-newsgroup-name)))
314   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
315   (message-add-action
316    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
317   (message-add-action
318    `(when (gnus-buffer-exists-p ,buffer)
319       (save-excursion
320         (set-buffer ,buffer)
321         ,(when article
322            `(gnus-summary-mark-article-as-replied ,article))))
323    'send))
324
325 (put 'gnus-setup-message 'lisp-indent-function 1)
326 (put 'gnus-setup-message 'edebug-form-spec '(form body))
327
328 ;;; Post news commands of Gnus group mode and summary mode
329
330 (defun gnus-group-mail (&optional arg)
331   "Start composing a mail.
332 If ARG, use the group under the point to find a posting style.
333 If ARG is 1, prompt for a group name to find the posting style."
334   (interactive "P")
335   ;; We can't `let' gnus-newsgroup-name here, since that leads
336   ;; to local variables leaking.
337   (let ((group gnus-newsgroup-name)
338         (buffer (current-buffer)))
339     (unwind-protect
340         (progn
341           (setq gnus-newsgroup-name
342                 (if arg
343                     (if (= 1 (prefix-numeric-value arg))
344                         (completing-read "Use posting style of group: "
345                                          gnus-active-hashtb nil
346                                          (gnus-read-active-file-p))
347                       (gnus-group-group-name))
348                   ""))
349           (gnus-setup-message 'message (message-mail)))
350       (save-excursion
351         (set-buffer buffer)
352         (setq gnus-newsgroup-name group)))))
353
354 (defun gnus-group-post-news (&optional arg)
355   "Start composing a news message.
356 If ARG, post to the group under point.
357 If ARG is 1, prompt for a group name."
358   (interactive "P")
359   ;; Bind this variable here to make message mode hooks work ok.
360   (let ((gnus-newsgroup-name
361          (if arg
362              (if (= 1 (prefix-numeric-value arg))
363                  (completing-read "Newsgroup: " gnus-active-hashtb nil
364                                   (gnus-read-active-file-p))
365                (gnus-group-group-name))
366            "")))
367     (gnus-post-news 'post gnus-newsgroup-name)))
368
369 (defun gnus-summary-post-news ()
370   "Start composing a news message."
371   (interactive)
372   (gnus-post-news 'post gnus-newsgroup-name))
373
374 (defun gnus-summary-followup (yank &optional force-news)
375   "Compose a followup to an article.
376 If prefix argument YANK is non-nil, original article is yanked automatically."
377   (interactive
378    (list (and current-prefix-arg
379               (gnus-summary-work-articles 1))))
380   (when yank
381     (gnus-summary-goto-subject (car yank)))
382   (save-window-excursion
383     (gnus-summary-select-article))
384   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
385         (gnus-newsgroup-name gnus-newsgroup-name))
386     ;; Send a followup.
387     (gnus-post-news nil gnus-newsgroup-name
388                     headers gnus-article-buffer
389                     yank nil force-news)))
390
391 (defun gnus-summary-followup-with-original (n &optional force-news)
392   "Compose a followup to an article and include the original article."
393   (interactive "P")
394   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
395
396 (defun gnus-summary-followup-to-mail (&optional arg)
397   "Followup to the current mail message via news."
398   (interactive
399    (list (and current-prefix-arg
400               (gnus-summary-work-articles 1))))
401   (gnus-summary-followup arg t))
402
403 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
404   "Followup to the current mail message via news."
405   (interactive "P")
406   (gnus-summary-followup (gnus-summary-work-articles arg) t))
407
408 (defun gnus-inews-yank-articles (articles)
409   (let (beg article)
410     (message-goto-body)
411     (while (setq article (pop articles))
412       (save-window-excursion
413         (set-buffer gnus-summary-buffer)
414         (gnus-summary-select-article nil nil nil article)
415         (gnus-summary-remove-process-mark article))
416       (gnus-copy-article-buffer)
417       (let ((message-reply-buffer gnus-article-copy)
418             (message-reply-headers
419              (with-current-buffer gnus-article-copy
420                ;; The headers are decoded.
421                (nnheader-parse-head t))))
422         (message-yank-original)
423         (setq beg (or beg (mark t))))
424       (when articles
425         (insert "\n")))
426     (push-mark)
427     (goto-char beg)))
428
429 (defun gnus-summary-cancel-article (&optional n symp)
430   "Cancel an article you posted.
431 Uses the process-prefix convention.  If given the symbolic
432 prefix `a', cancel using the standard posting method; if not
433 post using the current select method."
434   (interactive (gnus-interactive "P\ny"))
435   (let ((articles (gnus-summary-work-articles n))
436         (message-post-method
437          `(lambda (arg)
438             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
439         article)
440     (while (setq article (pop articles))
441       (when (gnus-summary-select-article t nil nil article)
442         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
443                 (message-cancel-news))
444           (gnus-summary-mark-as-read article gnus-canceled-mark)
445           (gnus-cache-remove-article 1))
446         (gnus-article-hide-headers-if-wanted))
447       (gnus-summary-remove-process-mark article))))
448
449 (defun gnus-summary-supersede-article ()
450   "Compose an article that will supersede a previous article.
451 This is done simply by taking the old article and adding a Supersedes
452 header line with the old Message-ID."
453   (interactive)
454   (let ((article (gnus-summary-article-number)))
455     (gnus-setup-message 'reply-yank
456       (gnus-summary-select-article t)
457       (set-buffer gnus-original-article-buffer)
458       (message-supersede)
459       (push
460        `((lambda ()
461            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
462              (save-excursion
463                (set-buffer ,gnus-summary-buffer)
464                (gnus-cache-possibly-remove-article ,article nil nil nil t)
465                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
466        message-send-actions))))
467
468 \f
469
470 (defun gnus-copy-article-buffer (&optional article-buffer)
471   ;; make a copy of the article buffer with all text properties removed
472   ;; this copy is in the buffer gnus-article-copy.
473   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
474   ;; this buffer should be passed to all mail/news reply/post routines.
475   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
476   (save-excursion
477     (set-buffer gnus-article-copy)
478     (mm-enable-multibyte))
479   (let ((article-buffer (or article-buffer gnus-article-buffer))
480         end beg)
481     (if (not (and (get-buffer article-buffer)
482                   (gnus-buffer-exists-p article-buffer)))
483         (error "Can't find any article buffer")
484       (save-excursion
485         (set-buffer article-buffer)
486         (let ((gnus-newsgroup-charset (or gnus-article-charset
487                                           gnus-newsgroup-charset))
488               (gnus-newsgroup-ignored-charsets
489                (or gnus-article-ignored-charsets
490                    gnus-newsgroup-ignored-charsets)))
491           (save-restriction
492             ;; Copy over the (displayed) article buffer, delete
493             ;; hidden text and remove text properties.
494             (widen)
495             (copy-to-buffer gnus-article-copy (point-min) (point-max))
496             (set-buffer gnus-article-copy)
497             (gnus-article-delete-text-of-type 'annotation)
498             (gnus-remove-text-with-property 'gnus-prev)
499             (gnus-remove-text-with-property 'gnus-next)
500             (insert
501              (prog1
502                  (buffer-substring-no-properties (point-min) (point-max))
503                (erase-buffer)))
504             ;; Find the original headers.
505             (set-buffer gnus-original-article-buffer)
506             (goto-char (point-min))
507             (while (looking-at message-unix-mail-delimiter)
508               (forward-line 1))
509             (setq beg (point))
510             (setq end (or (search-forward "\n\n" nil t) (point)))
511             ;; Delete the headers from the displayed articles.
512             (set-buffer gnus-article-copy)
513             (delete-region (goto-char (point-min))
514                            (or (search-forward "\n\n" nil t) (point-max)))
515             ;; Insert the original article headers.
516             (insert-buffer-substring gnus-original-article-buffer beg end)
517             (article-decode-encoded-words))))
518       gnus-article-copy)))
519
520 (defun gnus-post-news (post &optional group header article-buffer yank subject
521                             force-news)
522   (when article-buffer
523     (gnus-copy-article-buffer))
524   (let ((gnus-article-reply article-buffer)
525         (add-to-list gnus-add-to-list))
526     (gnus-setup-message (cond (yank 'reply-yank)
527                               (article-buffer 'reply)
528                               (t 'message))
529       (let* ((group (or group gnus-newsgroup-name))
530              (charset (gnus-group-name-charset nil group))
531              (pgroup group)
532              to-address to-group mailing-list to-list
533              newsgroup-p)
534         (when group
535           (setq to-address (gnus-group-find-parameter group 'to-address)
536                 to-group (gnus-group-find-parameter group 'to-group)
537                 to-list (gnus-group-find-parameter group 'to-list)
538                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
539                 mailing-list (when gnus-mailing-list-groups
540                                (string-match gnus-mailing-list-groups group))
541                 group (gnus-group-name-decode (gnus-group-real-name group)
542                                               charset)))
543         (if (or (and to-group
544                      (gnus-news-group-p to-group))
545                 newsgroup-p
546                 force-news
547                 (and (gnus-news-group-p
548                       (or pgroup gnus-newsgroup-name)
549                       (if header (mail-header-number header)
550                         gnus-current-article))
551                      (not mailing-list)
552                      (not to-list)
553                      (not to-address)))
554             ;; This is news.
555             (if post
556                 (message-news (or to-group group))
557               (set-buffer gnus-article-copy)
558               (gnus-msg-treat-broken-reply-to)
559               (message-followup (if (or newsgroup-p force-news) nil to-group)))
560           ;; The is mail.
561           (if post
562               (progn
563                 (message-mail (or to-address to-list))
564                 ;; Arrange for mail groups that have no `to-address' to
565                 ;; get that when the user sends off the mail.
566                 (when (and (not to-list)
567                            (not to-address)
568                            add-to-list)
569                   (push (list 'gnus-inews-add-to-address pgroup)
570                         message-send-actions)))
571             (set-buffer gnus-article-copy)
572             (gnus-msg-treat-broken-reply-to)
573             (message-wide-reply to-address)))
574         (when yank
575           (gnus-inews-yank-articles yank))))))
576
577 (defun gnus-msg-treat-broken-reply-to ()
578   "Remove the Reply-to header iff broken-reply-to."
579   (when (gnus-group-find-parameter
580          gnus-newsgroup-name 'broken-reply-to)
581     (save-restriction
582       (message-narrow-to-head)
583       (message-remove-header "reply-to"))))
584
585 (defun gnus-post-method (arg group &optional silent)
586   "Return the posting method based on GROUP and ARG.
587 If SILENT, don't prompt the user."
588   (let ((group-method (gnus-find-method-for-group group)))
589     (cond
590      ;; If the group-method is nil (which shouldn't happen) we use
591      ;; the default method.
592      ((null group-method)
593       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
594           gnus-select-method message-post-method))
595      ;; We want the inverse of the default
596      ((and arg (not (eq arg 0)))
597       (if (eq gnus-post-method 'active)
598           gnus-select-method
599         group-method))
600      ;; We query the user for a post method.
601      ((or arg
602           (and gnus-post-method
603                (not (eq gnus-post-method 'current))
604                (listp (car gnus-post-method))))
605       (let* ((methods
606               ;; Collect all methods we know about.
607               (append
608                (when (and gnus-post-method
609                           (not (eq gnus-post-method 'current)))
610                  (if (listp (car gnus-post-method))
611                      gnus-post-method
612                    (list gnus-post-method)))
613                gnus-secondary-select-methods
614                (mapcar 'cdr gnus-server-alist)
615                (mapcar 'car gnus-opened-servers)
616                (list gnus-select-method)
617                (list group-method)))
618              method-alist post-methods method)
619         ;; Weed out all mail methods.
620         (while methods
621           (setq method (gnus-server-get-method "" (pop methods)))
622           (when (and (or (gnus-method-option-p method 'post)
623                          (gnus-method-option-p method 'post-mail))
624                      (not (member method post-methods)))
625             (push method post-methods)))
626         ;; Create a name-method alist.
627         (setq method-alist
628               (mapcar
629                (lambda (m)
630                  (if (equal (cadr m) "")
631                      (list (symbol-name (car m)) m)
632                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
633                post-methods))
634         ;; Query the user.
635         (cadr
636          (assoc
637           (setq gnus-last-posting-server
638                 (if (and silent
639                          gnus-last-posting-server)
640                     ;; Just use the last value.
641                     gnus-last-posting-server
642                   (completing-read
643                    "Posting method: " method-alist nil t
644                    (cons (or gnus-last-posting-server "") 0))))
645           method-alist))))
646      ;; Override normal method.
647      ((and (eq gnus-post-method 'current)
648            (not (eq (car group-method) 'nndraft))
649            (gnus-get-function group-method 'request-post t)
650            (not arg))
651       group-method)
652      ((and gnus-post-method
653            (not (eq gnus-post-method 'current)))
654       gnus-post-method)
655      ;; Use the normal select method.
656      (t gnus-select-method))))
657
658 \f
659
660 ;; Dummies to avoid byte-compile warning.
661 (eval-when-compile
662   (defvar nnspool-rejected-article-hook)
663   (defvar xemacs-codename))
664
665 (defun gnus-extended-version ()
666   "Stringified Gnus version and Emacs version."
667   (interactive)
668   (concat
669    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
670    " (" gnus-version ")"
671    " "
672    (cond
673     ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
674      (concat "Emacs/" (match-string 1 emacs-version)))
675     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
676                    emacs-version)
677      (concat (match-string 1 emacs-version)
678              (format "/%d.%d" emacs-major-version emacs-minor-version)
679              (if (match-beginning 3)
680                  (match-string 3 emacs-version)
681                "")
682              (if (boundp 'xemacs-codename)
683                  (concat " (" xemacs-codename ")")
684                "")))
685     (t emacs-version))))
686
687 \f
688 ;;;
689 ;;; Gnus Mail Functions
690 ;;;
691
692 ;;; Mail reply commands of Gnus summary mode
693
694 (defun gnus-summary-reply (&optional yank wide)
695   "Start composing a reply mail to the current message.
696 If prefix argument YANK is non-nil, the original article is yanked
697 automatically."
698   (interactive
699    (list (and current-prefix-arg
700               (gnus-summary-work-articles 1))))
701   ;; Stripping headers should be specified with mail-yank-ignored-headers.
702   (when yank
703     (gnus-summary-goto-subject (car yank)))
704   (let ((gnus-article-reply t))
705     (gnus-setup-message (if yank 'reply-yank 'reply)
706       (gnus-summary-select-article)
707       (set-buffer (gnus-copy-article-buffer))
708       (gnus-msg-treat-broken-reply-to)
709       (save-restriction
710         (message-narrow-to-head)
711         (goto-char (point-max)))
712       (mml-quote-region (point) (point-max))
713       (message-reply nil wide)
714       (when yank
715         (gnus-inews-yank-articles yank)))))
716
717 (defun gnus-summary-reply-with-original (n &optional wide)
718   "Start composing a reply mail to the current message.
719 The original article will be yanked."
720   (interactive "P")
721   (gnus-summary-reply (gnus-summary-work-articles n) wide))
722
723 (defun gnus-summary-wide-reply (&optional yank)
724   "Start composing a wide reply mail to the current message.
725 If prefix argument YANK is non-nil, the original article is yanked
726 automatically."
727   (interactive
728    (list (and current-prefix-arg
729               (gnus-summary-work-articles 1))))
730   (gnus-summary-reply yank t))
731
732 (defun gnus-summary-wide-reply-with-original (n)
733   "Start composing a wide reply mail to the current message.
734 The original article will be yanked."
735   (interactive "P")
736   (gnus-summary-reply-with-original n t))
737
738 (defun gnus-summary-mail-forward (&optional arg post)
739   "Forward the current message to another user.
740 If ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
741 if ARG is 1, decode the message and forward directly inline;
742 if ARG is 2, forward message as an rfc822 MIME section;
743 if ARG is 3, decode message and forward as an rfc822 MIME section;
744 if ARG is 4, forward message directly inline;
745 otherwise, use flipped `message-forward-as-mime'.
746 If POST, post instead of mail."
747   (interactive "P")
748   (let ((message-forward-as-mime message-forward-as-mime)
749         (message-forward-show-mml message-forward-show-mml))
750     (cond
751      ((null arg))
752      ((eq arg 1) (setq message-forward-as-mime nil
753                        message-forward-show-mml t))
754      ((eq arg 2) (setq message-forward-as-mime t
755                        message-forward-show-mml nil))
756      ((eq arg 3) (setq message-forward-as-mime t
757                        message-forward-show-mml t))
758      ((eq arg 4) (setq message-forward-as-mime nil
759                        message-forward-show-mml nil))
760      (t (setq message-forward-as-mime (not message-forward-as-mime))))
761     (gnus-setup-message 'forward
762       (gnus-summary-select-article)
763       (let ((mail-parse-charset gnus-newsgroup-charset)
764             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
765         (set-buffer gnus-original-article-buffer)
766         (message-forward post)))))
767
768 (defun gnus-summary-resend-message (address n)
769   "Resend the current article to ADDRESS."
770   (interactive
771    (list (message-read-from-minibuffer "Resend message(s) to: ")
772          current-prefix-arg))
773   (let ((articles (gnus-summary-work-articles n))
774         article)
775     (while (setq article (pop articles))
776       (gnus-summary-select-article nil nil nil article)
777       (save-excursion
778         (set-buffer gnus-original-article-buffer)
779         (message-resend address)))))
780
781 (defun gnus-summary-post-forward (&optional arg)
782   "Forward the current article to a newsgroup.
783 See `gnus-summary-mail-forward' for ARG."
784   (interactive "P")
785   (gnus-summary-mail-forward arg t))
786
787 (defvar gnus-nastygram-message
788   "The following article was inappropriately posted to %s.\n\n"
789   "Format string to insert in nastygrams.
790 The current group name will be inserted at \"%s\".")
791
792 (defun gnus-summary-mail-nastygram (n)
793   "Send a nastygram to the author of the current article."
794   (interactive "P")
795   (when (or gnus-expert-user
796             (gnus-y-or-n-p
797              "Really send a nastygram to the author of the current article? "))
798     (let ((group gnus-newsgroup-name))
799       (gnus-summary-reply-with-original n)
800       (set-buffer gnus-message-buffer)
801       (message-goto-body)
802       (insert (format gnus-nastygram-message group))
803       (message-send-and-exit))))
804
805 (defun gnus-summary-mail-crosspost-complaint (n)
806   "Send a complaint about crossposting to the current article(s)."
807   (interactive "P")
808   (let ((articles (gnus-summary-work-articles n))
809         article)
810     (while (setq article (pop articles))
811       (set-buffer gnus-summary-buffer)
812       (gnus-summary-goto-subject article)
813       (let ((group (gnus-group-real-name gnus-newsgroup-name))
814             newsgroups followup-to)
815         (gnus-summary-select-article)
816         (set-buffer gnus-original-article-buffer)
817         (if (and (<= (length (message-tokenize-header
818                               (setq newsgroups
819                                     (mail-fetch-field "newsgroups"))
820                               ", "))
821                      1)
822                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
823                      (not (member group (message-tokenize-header
824                                          followup-to ", ")))))
825             (if followup-to
826                 (gnus-message 1 "Followup-to restricted")
827               (gnus-message 1 "Not a crossposted article"))
828           (set-buffer gnus-summary-buffer)
829           (gnus-summary-reply-with-original 1)
830           (set-buffer gnus-message-buffer)
831           (message-goto-body)
832           (insert (format gnus-crosspost-complaint newsgroups group))
833           (message-goto-subject)
834           (re-search-forward " *$")
835           (replace-match " (crosspost notification)" t t)
836           (gnus-deactivate-mark)
837           (when (gnus-y-or-n-p "Send this complaint? ")
838             (message-send-and-exit)))))))
839
840 (defun gnus-summary-mail-other-window ()
841   "Compose mail in other window."
842   (interactive)
843   (gnus-setup-message 'message
844     (message-mail)))
845
846 (defun gnus-mail-parse-comma-list ()
847   (let (accumulated
848         beg)
849     (skip-chars-forward " ")
850     (while (not (eobp))
851       (setq beg (point))
852       (skip-chars-forward "^,")
853       (while (zerop
854               (save-excursion
855                 (save-restriction
856                   (let ((i 0))
857                     (narrow-to-region beg (point))
858                     (goto-char beg)
859                     (logand (progn
860                               (while (search-forward "\"" nil t)
861                                 (incf i))
862                               (if (zerop i) 2 i))
863                             2)))))
864         (skip-chars-forward ",")
865         (skip-chars-forward "^,"))
866       (skip-chars-backward " ")
867       (push (buffer-substring beg (point))
868             accumulated)
869       (skip-chars-forward "^,")
870       (skip-chars-forward ", "))
871     accumulated))
872
873 (defun gnus-inews-add-to-address (group)
874   (let ((to-address (mail-fetch-field "to")))
875     (when (and to-address
876                (gnus-alive-p))
877       ;; This mail group doesn't have a `to-list', so we add one
878       ;; here.  Magic!
879       (when (gnus-y-or-n-p
880              (format "Do you want to add this as `to-list': %s " to-address))
881         (gnus-group-add-parameter group (cons 'to-list to-address))))))
882
883 (defun gnus-put-message ()
884   "Put the current message in some group and return to Gnus."
885   (interactive)
886   (let ((reply gnus-article-reply)
887         (winconf gnus-prev-winconf)
888         (group gnus-newsgroup-name))
889
890     (or (and group (not (gnus-group-read-only-p group)))
891         (setq group (read-string "Put in group: " nil
892                                  (gnus-writable-groups))))
893     (when (gnus-gethash group gnus-newsrc-hashtb)
894       (error "No such group: %s" group))
895
896     (save-excursion
897       (save-restriction
898         (widen)
899         (message-narrow-to-headers)
900         (let (gnus-deletable-headers)
901           (if (message-news-p)
902               (message-generate-headers message-required-news-headers)
903             (message-generate-headers message-required-mail-headers)))
904         (goto-char (point-max))
905         (insert "Gcc: " group "\n")
906         (widen)))
907
908     (gnus-inews-do-gcc)
909
910     (when (get-buffer gnus-group-buffer)
911       (when (gnus-buffer-exists-p (car-safe reply))
912         (set-buffer (car reply))
913         (and (cdr reply)
914              (gnus-summary-mark-article-as-replied
915               (cdr reply))))
916       (when winconf
917         (set-window-configuration winconf)))))
918
919 (defun gnus-article-mail (yank)
920   "Send a reply to the address near point.
921 If YANK is non-nil, include the original article."
922   (interactive "P")
923   (let ((address
924          (buffer-substring
925           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
926           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
927     (when address
928       (message-reply address)
929       (when yank
930         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
931
932 (defvar nntp-server-type)
933 (defun gnus-bug ()
934   "Send a bug report to the Gnus maintainers."
935   (interactive)
936   (unless (gnus-alive-p)
937     (error "Gnus has been shut down"))
938   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
939     (unless (message-mail-user-agent)
940       (delete-other-windows)
941       (when gnus-bug-create-help-buffer
942         (switch-to-buffer "*Gnus Help Bug*")
943         (erase-buffer)
944         (insert gnus-bug-message)
945         (goto-char (point-min)))
946       (message-pop-to-buffer "*Gnus Bug*"))
947     (let ((message-this-is-mail t))
948       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
949     (when gnus-bug-create-help-buffer
950       (push `(gnus-bug-kill-buffer) message-send-actions))
951     (goto-char (point-min))
952     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
953     (forward-line 1)
954     (insert (gnus-version) "\n"
955             (emacs-version) "\n")
956     (when (and (boundp 'nntp-server-type)
957                (stringp nntp-server-type))
958       (insert nntp-server-type))
959     (insert "\n\n\n\n\n")
960     (let (text)
961       (save-excursion
962         (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
963         (gnus-debug)
964         (setq text (buffer-string)))
965       (insert "<#part type=application/x-emacs-lisp disposition=inline description=\"User settings\">\n" text "\n<#/part>"))
966     (goto-char (point-min))
967     (search-forward "Subject: " nil t)
968     (message "")))
969
970 (defun gnus-bug-kill-buffer ()
971   (when (get-buffer "*Gnus Help Bug*")
972     (kill-buffer "*Gnus Help Bug*")))
973
974 (defun gnus-summary-yank-message (buffer n)
975   "Yank the current article into a composed message."
976   (interactive
977    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
978          current-prefix-arg))
979   (gnus-summary-iterate n
980     (let ((gnus-display-mime-function nil)
981           (gnus-inhibit-treatment t))
982       (gnus-summary-select-article))
983     (save-excursion
984       (set-buffer buffer)
985       (message-yank-buffer gnus-article-buffer))))
986
987 (defun gnus-debug ()
988   "Attempts to go through the Gnus source file and report what variables have been changed.
989 The source file has to be in the Emacs load path."
990   (interactive)
991   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
992                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
993                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
994                  "nnmail.el" "message.el"))
995         (point (point))
996         file expr olist sym)
997     (gnus-message 4 "Please wait while we snoop your variables...")
998     (sit-for 0)
999     ;; Go through all the files looking for non-default values for variables.
1000     (save-excursion
1001       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1002       (while files
1003         (erase-buffer)
1004         (when (and (setq file (locate-library (pop files)))
1005                    (file-exists-p file))
1006           (insert-file-contents file)
1007           (goto-char (point-min))
1008           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1009               (gnus-message 4 "Malformed sources in file %s" file)
1010             (narrow-to-region (point-min) (point))
1011             (goto-char (point-min))
1012             (while (setq expr (ignore-errors (read (current-buffer))))
1013               (ignore-errors
1014                 (and (or (eq (car expr) 'defvar)
1015                          (eq (car expr) 'defcustom))
1016                      (stringp (nth 3 expr))
1017                      (or (not (boundp (nth 1 expr)))
1018                          (not (equal (eval (nth 2 expr))
1019                                      (symbol-value (nth 1 expr)))))
1020                      (push (nth 1 expr) olist)))))))
1021       (kill-buffer (current-buffer)))
1022     (when (setq olist (nreverse olist))
1023       (insert "------------------ Environment follows ------------------\n\n"))
1024     (while olist
1025       (if (boundp (car olist))
1026           (condition-case ()
1027               (pp `(setq ,(car olist)
1028                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1029                                   (and (symbolp sym)
1030                                        (not (or (eq sym nil)
1031                                                 (eq sym t)))))
1032                               (list 'quote (symbol-value (car olist)))
1033                             (symbol-value (car olist))))
1034                   (current-buffer))
1035             (error
1036              (format "(setq %s 'whatever)\n" (car olist))))
1037         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1038       (setq olist (cdr olist)))
1039     (insert "\n\n")
1040     ;; Remove any control chars - they seem to cause trouble for some
1041     ;; mailers.  (Byte-compiled output from the stuff above.)
1042     (goto-char point)
1043     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1044       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1045                      t t))))
1046
1047 ;;; Treatment of rejected articles.
1048 ;;; Bounced mail.
1049
1050 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1051   "Re-mail the current message.
1052 This only makes sense if the current message is a bounce message than
1053 contains some mail you have written which has been bounced back to
1054 you.
1055 If FETCH, try to fetch the article that this is a reply to, if indeed
1056 this is a reply."
1057   (interactive "P")
1058   (gnus-summary-select-article t)
1059   (set-buffer gnus-original-article-buffer)
1060   (gnus-setup-message 'compose-bounce
1061     (let* ((references (mail-fetch-field "references"))
1062            (parent (and references (gnus-parent-id references))))
1063       (message-bounce)
1064       ;; If there are references, we fetch the article we answered to.
1065       (and fetch parent
1066            (gnus-summary-refer-article parent)
1067            (gnus-summary-show-all-headers)))))
1068
1069 ;;; Gcc handling.
1070
1071 (defun gnus-inews-group-method (group)
1072   (cond ((and (null (gnus-get-info group))
1073               (eq (car gnus-message-archive-method)
1074                   (car
1075                    (gnus-server-to-method
1076                     (gnus-group-method group)))))
1077          ;; If the group doesn't exist, we assume
1078          ;; it's an archive group...
1079          gnus-message-archive-method)
1080         ;; Use the method.
1081         ((gnus-info-method (gnus-get-info group))
1082          (gnus-info-method (gnus-get-info group)))
1083         ;; Find the method.
1084         (t (gnus-group-method group))))
1085
1086 ;; Do Gcc handling, which copied the message over to some group.
1087 (defun gnus-inews-do-gcc (&optional gcc)
1088   (interactive)
1089   (when (gnus-alive-p)
1090     (save-excursion
1091       (save-restriction
1092         (message-narrow-to-headers)
1093         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1094               (cur (current-buffer))
1095               groups group method group-art)
1096           (when gcc
1097             (message-remove-header "gcc")
1098             (widen)
1099             (setq groups (message-unquote-tokens
1100                           (message-tokenize-header gcc " ,")))
1101             ;; Copy the article over to some group(s).
1102             (while (setq group (pop groups))
1103               (gnus-check-server
1104                (setq method (gnus-inews-group-method group)))
1105               (unless (gnus-request-group group t method)
1106                 (gnus-request-create-group group method))
1107               (save-excursion
1108                 (nnheader-set-temp-buffer " *acc*")
1109                 (insert-buffer-substring cur)
1110                 (message-encode-message-body)
1111                 (save-restriction
1112                   (message-narrow-to-headers)
1113                   (let ((mail-parse-charset message-default-charset)
1114                         (rfc2047-header-encoding-alist
1115                          (cons '("Newsgroups" . default)
1116                                rfc2047-header-encoding-alist)))
1117                     (mail-encode-encoded-word-buffer)))
1118                 (goto-char (point-min))
1119                 (when (re-search-forward
1120                        (concat "^" (regexp-quote mail-header-separator) "$")
1121                        nil t)
1122                   (replace-match "" t t ))
1123                 (unless (setq group-art
1124                               (gnus-request-accept-article group method t t))
1125                   (gnus-message 1 "Couldn't store article in group %s: %s"
1126                                 group (gnus-status-message method))
1127                   (sit-for 2))
1128                 (when (and group-art gnus-inews-mark-gcc-as-read)
1129                   (let ((active (gnus-active group)))
1130                     (if active
1131                         (if (< (cdr active) (cdr group-art))
1132                             (gnus-set-active group (cons (car active)
1133                                                          (cdr group-art))))
1134                       (gnus-activate-group group)))
1135                   (let ((buffer (gnus-summary-buffer-name group))
1136                         (mark gnus-read-mark)
1137                         (article (cdr group-art)))
1138                     (unless
1139                         (and
1140                          (get-buffer buffer)
1141                          (with-current-buffer buffer
1142                            (when gnus-newsgroup-prepared
1143                              (when (and gnus-newsgroup-auto-expire
1144                                         (memq mark gnus-auto-expirable-marks))
1145                                (setq mark gnus-expirable-mark))
1146                              (setq mark (gnus-request-update-mark
1147                                          group article mark))
1148                              (gnus-mark-article-as-read article mark)
1149                              (setq gnus-newsgroup-active (gnus-active group))
1150                              t)))
1151                       (gnus-group-make-articles-read group
1152                                                      (list article))
1153                       (when (gnus-group-auto-expirable-p group)
1154                         (gnus-add-marked-articles
1155                          group 'expire (list article))))))
1156                 (kill-buffer (current-buffer))))))))))
1157
1158 (defun gnus-inews-insert-gcc ()
1159   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1160   (save-excursion
1161     (save-restriction
1162       (message-narrow-to-headers)
1163       (let* ((group gnus-outgoing-message-group)
1164              (gcc (cond
1165                    ((gnus-functionp group)
1166                     (funcall group))
1167                    ((or (stringp group) (list group))
1168                     group))))
1169         (when gcc
1170           (insert "Gcc: "
1171                   (if (stringp gcc) gcc
1172                     (mapconcat 'identity gcc " "))
1173                   "\n"))))))
1174
1175 (defun gnus-inews-insert-archive-gcc (&optional group)
1176   "Insert the Gcc to say where the article is to be archived."
1177   (let* ((var gnus-message-archive-group)
1178          (group (or group gnus-newsgroup-name ""))
1179          (gcc-self-val
1180           (and gnus-newsgroup-name
1181                (not (equal gnus-newsgroup-name ""))
1182                (gnus-group-find-parameter
1183                 gnus-newsgroup-name 'gcc-self)))
1184          result
1185          (groups
1186           (cond
1187            ((null gnus-message-archive-method)
1188             ;; Ignore.
1189             nil)
1190            ((stringp var)
1191             ;; Just a single group.
1192             (list var))
1193            ((null var)
1194             ;; We don't want this.
1195             nil)
1196            ((and (listp var) (stringp (car var)))
1197             ;; A list of groups.
1198             var)
1199            ((gnus-functionp var)
1200             ;; A function.
1201             (funcall var group))
1202            (t
1203             ;; An alist of regexps/functions/forms.
1204             (while (and var
1205                         (not
1206                          (setq result
1207                                (cond
1208                                 ((stringp (caar var))
1209                                  ;; Regexp.
1210                                  (when (string-match (caar var) group)
1211                                    (cdar var)))
1212                                 ((gnus-functionp (car var))
1213                                  ;; Function.
1214                                  (funcall (car var) group))
1215                                 (t
1216                                  (eval (car var)))))))
1217               (setq var (cdr var)))
1218             result)))
1219          name)
1220     (when (or groups gcc-self-val)
1221       (when (stringp groups)
1222         (setq groups (list groups)))
1223       (save-excursion
1224         (save-restriction
1225           (message-narrow-to-headers)
1226           (goto-char (point-max))
1227           (insert "Gcc: ")
1228           (if gcc-self-val
1229               ;; Use the `gcc-self' param value instead.
1230               (progn
1231                 (insert
1232                  (if (stringp gcc-self-val)
1233                      gcc-self-val
1234                    group))
1235                 (if (not (eq gcc-self-val 'none))
1236                     (insert "\n")
1237                   (progn
1238                     (beginning-of-line)
1239                     (kill-line))))
1240             ;; Use the list of groups.
1241             (while (setq name (pop groups))
1242               (insert (if (string-match ":" name)
1243                           name
1244                         (gnus-group-prefixed-name
1245                          name gnus-message-archive-method)))
1246               (when groups
1247                 (insert " ")))
1248             (insert "\n")))))))
1249
1250 ;;; Posting styles.
1251
1252 (defun gnus-configure-posting-styles ()
1253   "Configure posting styles according to `gnus-posting-styles'."
1254   (unless gnus-inhibit-posting-styles
1255     (let ((group (or gnus-newsgroup-name ""))
1256           (styles gnus-posting-styles)
1257           style match variable attribute value v results
1258           filep name address element)
1259       ;; If the group has a posting-style parameter, add it at the end with a
1260       ;; regexp matching everything, to be sure it takes precedence over all
1261       ;; the others.
1262       (when gnus-newsgroup-name
1263         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1264           (when tmp-style
1265             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1266       ;; Go through all styles and look for matches.
1267       (dolist (style styles)
1268         (setq match (pop style))
1269         (goto-char (point-min))
1270         (when (cond
1271                ((stringp match)
1272                 ;; Regexp string match on the group name.
1273                 (string-match match group))
1274                ((eq match 'header)
1275                 (let ((header (message-fetch-field (pop style))))
1276                   (and header
1277                        (string-match (pop style) header))))
1278                ((or (symbolp match)
1279                     (gnus-functionp match))
1280                 (cond
1281                  ((gnus-functionp match)
1282                   ;; Function to be called.
1283                   (funcall match))
1284                  ((boundp match)
1285                   ;; Variable to be checked.
1286                   (symbol-value match))))
1287                ((listp match)
1288                 ;; This is a form to be evaled.
1289                 (eval match)))
1290           ;; We have a match, so we set the variables.
1291           (dolist (attribute style)
1292             (setq element (pop attribute)
1293                   variable nil
1294                   filep nil)
1295             (setq value
1296                   (cond
1297                    ((eq (car attribute) :file)
1298                     (setq filep t)
1299                     (cadr attribute))
1300                    ((eq (car attribute) :value)
1301                     (cadr attribute))
1302                    (t
1303                     (car attribute))))
1304             ;; We get the value.
1305             (setq v
1306                   (cond
1307                    ((stringp value)
1308                     value)
1309                    ((or (symbolp value)
1310                         (gnus-functionp value))
1311                     (cond ((gnus-functionp value)
1312                            (funcall value))
1313                           ((boundp value)
1314                            (symbol-value value))))
1315                    ((listp value)
1316                     (eval value))))
1317             ;; Translate obsolescent value.
1318             (when (eq element 'signature-file)
1319               (setq element 'signature
1320                     filep t))
1321             ;; Get the contents of file elems.
1322             (when (and filep v)
1323               (setq v (with-temp-buffer
1324                         (insert-file-contents v)
1325                         (buffer-string))))
1326             (setq results (delq (assoc element results) results))
1327             (push (cons element v) results))))
1328       ;; Now we have all the styles, so we insert them.
1329       (setq name (assq 'name results)
1330             address (assq 'address results))
1331       (setq results (delq name (delq address results)))
1332       (make-local-variable 'message-setup-hook)
1333       (dolist (result results)
1334         (add-hook 'message-setup-hook
1335                   (cond
1336                    ((eq 'eval (car result))
1337                     'ignore)
1338                    ((eq 'body (car result))
1339                     `(lambda ()
1340                        (save-excursion
1341                          (message-goto-body)
1342                          (insert ,(cdr result)))))
1343                    ((eq 'signature (car result))
1344                     (set (make-local-variable 'message-signature) nil)
1345                     (set (make-local-variable 'message-signature-file) nil)
1346                     (if (not (cdr result))
1347                         'ignore
1348                       `(lambda ()
1349                          (save-excursion
1350                            (let ((message-signature ,(cdr result)))
1351                              (when message-signature
1352                                (message-insert-signature)))))))
1353                    (t
1354                     (let ((header
1355                            (if (symbolp (car result))
1356                                (capitalize (symbol-name (car result)))
1357                              (car result))))
1358                       `(lambda ()
1359                          (save-excursion
1360                            (message-remove-header ,header)
1361                            (let ((value ,(cdr result)))
1362                              (when value
1363                                (message-goto-eoh)
1364                                (insert ,header ": " value "\n"))))))))))
1365       (when (or name address)
1366         (add-hook 'message-setup-hook
1367                   `(lambda ()
1368                      (set (make-local-variable 'user-mail-address)
1369                           ,(or (cdr address) user-mail-address))
1370                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1371                            (user-mail-address
1372                             ,(or (cdr address) user-mail-address)))
1373                        (save-excursion
1374                          (message-remove-header "From")
1375                          (message-goto-eoh)
1376                          (insert "From: " (message-make-from) "\n")))))))))
1377
1378 ;;; Allow redefinition of functions.
1379
1380 (gnus-ems-redefine)
1381
1382 (provide 'gnus-msg)
1383
1384 ;;; gnus-msg.el ends here