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