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