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