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