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