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