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