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