Synch with `t-gnus-6_14'.
[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             ;; Encode bitmap smileys to ordinary text.
500             ;; Possibly, the original text might be restored.
501             (static-unless (featurep 'xemacs)
502               (when (featurep 'smiley-mule)
503                 (smiley-encode-buffer)))
504             (gnus-article-delete-text-of-type 'annotation)
505             (gnus-remove-text-with-property 'gnus-prev)
506             (gnus-remove-text-with-property 'gnus-next)
507             (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
508             (insert
509              (prog1
510                  (buffer-substring-no-properties (point-min) (point-max))
511                (erase-buffer))))
512           ;; Find the original headers.
513           (set-buffer gnus-original-article-buffer)
514           (goto-char (point-min))
515           (while (looking-at message-unix-mail-delimiter)
516             (forward-line 1))
517           (setq beg (point))
518           (setq end (or (search-forward "\n\n" nil t) (point)))
519           ;; Delete the headers from the displayed articles.
520           (set-buffer gnus-article-copy)
521           (delete-region (goto-char (point-min))
522                          (or (search-forward "\n\n" nil t) (point-max)))
523           ;; Insert the original article headers.
524           (insert-buffer-substring gnus-original-article-buffer beg end)
525           (article-decode-encoded-words)))
526       gnus-article-copy)))
527
528 (defun gnus-post-news (post &optional group header article-buffer yank subject
529                             force-news)
530   (when article-buffer
531     (gnus-copy-article-buffer))
532   (let ((gnus-article-reply article-buffer)
533         (add-to-list gnus-add-to-list))
534     (gnus-setup-message (cond (yank 'reply-yank)
535                               (article-buffer 'reply)
536                               (t 'message))
537       (let* ((group (or group gnus-newsgroup-name))
538              (charset (gnus-group-name-charset nil group))
539              (pgroup group)
540              to-address to-group mailing-list to-list
541              newsgroup-p)
542         (when group
543           (setq to-address (gnus-group-find-parameter group 'to-address)
544                 to-group (gnus-group-find-parameter group 'to-group)
545                 to-list (gnus-group-find-parameter group 'to-list)
546                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
547                 mailing-list (when gnus-mailing-list-groups
548                                (string-match gnus-mailing-list-groups group))
549                 group (gnus-group-name-decode (gnus-group-real-name group)
550                                               charset)))
551         (if (or (and to-group
552                      (gnus-news-group-p to-group))
553                 newsgroup-p
554                 force-news
555                 (and (gnus-news-group-p
556                       (or pgroup gnus-newsgroup-name)
557                       (if header (mail-header-number header)
558                         gnus-current-article))
559                      (not mailing-list)
560                      (not to-list)
561                      (not to-address)))
562             ;; This is news.
563             (if post
564                 (message-news (or to-group group))
565               (set-buffer gnus-article-copy)
566               (gnus-msg-treat-broken-reply-to)
567               (message-followup (if (or newsgroup-p force-news) nil to-group)))
568           ;; The is mail.
569           (if post
570               (progn
571                 (message-mail (or to-address to-list))
572                 ;; Arrange for mail groups that have no `to-address' to
573                 ;; get that when the user sends off the mail.
574                 (when (and (not to-list)
575                            (not to-address)
576                            add-to-list)
577                   (push (list 'gnus-inews-add-to-address pgroup)
578                         message-send-actions)))
579             (set-buffer gnus-article-copy)
580             (gnus-msg-treat-broken-reply-to)
581             (message-wide-reply to-address)))
582         (when yank
583           (gnus-inews-yank-articles yank))))))
584
585 (defun gnus-msg-treat-broken-reply-to ()
586   "Remove the Reply-to header iff broken-reply-to."
587   (when (gnus-group-find-parameter
588          gnus-newsgroup-name 'broken-reply-to)
589     (save-restriction
590       (message-narrow-to-head)
591       (message-remove-header "reply-to"))))
592
593 (defun gnus-post-method (arg group &optional silent)
594   "Return the posting method based on GROUP and ARG.
595 If SILENT, don't prompt the user."
596   (let ((group-method (gnus-find-method-for-group group)))
597     (cond
598      ;; If the group-method is nil (which shouldn't happen) we use
599      ;; the default method.
600      ((null group-method)
601       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
602           gnus-select-method message-post-method))
603      ;; We want the inverse of the default
604      ((and arg (not (eq arg 0)))
605       (if (eq gnus-post-method 'active)
606           gnus-select-method
607         group-method))
608      ;; We query the user for a post method.
609      ((or arg
610           (and gnus-post-method
611                (not (eq gnus-post-method 'current))
612                (listp (car gnus-post-method))))
613       (let* ((methods
614               ;; Collect all methods we know about.
615               (append
616                (when (and gnus-post-method
617                           (not (eq gnus-post-method 'current)))
618                  (if (listp (car gnus-post-method))
619                      gnus-post-method
620                    (list gnus-post-method)))
621                gnus-secondary-select-methods
622                (mapcar 'cdr gnus-server-alist)
623                (mapcar 'car gnus-opened-servers)
624                (list gnus-select-method)
625                (list group-method)))
626              method-alist post-methods method)
627         ;; Weed out all mail methods.
628         (while methods
629           (setq method (gnus-server-get-method "" (pop methods)))
630           (when (and (or (gnus-method-option-p method 'post)
631                          (gnus-method-option-p method 'post-mail))
632                      (not (member method post-methods)))
633             (push method post-methods)))
634         ;; Create a name-method alist.
635         (setq method-alist
636               (mapcar
637                (lambda (m)
638                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
639                post-methods))
640         ;; Query the user.
641         (cadr
642          (assoc
643           (setq gnus-last-posting-server
644                 (if (and silent
645                          gnus-last-posting-server)
646                     ;; Just use the last value.
647                     gnus-last-posting-server
648                   (completing-read
649                    "Posting method: " method-alist nil t
650                    (cons (or gnus-last-posting-server "") 0))))
651           method-alist))))
652      ;; Override normal method.
653      ((and (eq gnus-post-method 'current)
654            (not (eq (car group-method) 'nndraft))
655            (gnus-get-function group-method 'request-post t)
656            (not arg))
657       group-method)
658      ((and gnus-post-method
659            (not (eq gnus-post-method 'current)))
660       gnus-post-method)
661      ;; Use the normal select method.
662      (t gnus-select-method))))
663
664 \f
665
666 (defun gnus-message-make-user-agent (&optional include-mime-info max-column)
667   "Return user-agent info.
668 INCLUDE-MIME-INFO the optional first argument if it is non-nil and the variable
669   `mime-edit-user-agent-value' exists, the return value will include it.
670 MAX-COLUMN the optional second argument if it is specified, the return value
671   will be folded up in the proper way."
672   (let ((user-agent (if (and include-mime-info
673                              (boundp 'mime-edit-user-agent-value))
674                         (concat (gnus-extended-version)
675                                 " "
676                                 mime-edit-user-agent-value)
677                       (gnus-extended-version))))
678     (if max-column
679         (let (boundary)
680           (unless (natnump max-column) (setq max-column 76))
681           (with-temp-buffer
682             (insert "            " user-agent)
683             (goto-char 13)
684             (while (re-search-forward "[\n\t ]+" nil t)
685               (replace-match " "))
686             (goto-char 13)
687             (while (re-search-forward "[^ ()/]+\\(/[^ ()/]+\\)? ?" nil t)
688               (while (eq ?\( (char-after (point)))
689                 (forward-list)
690                 (skip-chars-forward " "))
691               (skip-chars-backward " ")
692               (if (> (current-column) max-column)
693                   (progn
694                     (if (or (not boundary) (eq ?\n (char-after boundary)))
695                         (progn
696                           (setq boundary (point))
697                           (unless (eobp)
698                             (delete-char 1)
699                             (insert "\n ")))
700                       (goto-char boundary)
701                       (delete-char 1)
702                       (insert "\n ")))
703                 (setq boundary (point))))
704             (buffer-substring 13 (point-max))))
705       user-agent)))
706
707 \f
708 ;;;
709 ;;; Gnus Mail Functions
710 ;;;
711
712 ;;; Mail reply commands of Gnus summary mode
713
714 (defun gnus-summary-reply (&optional yank wide)
715   "Start composing a reply mail to the current message.
716 If prefix argument YANK is non-nil, the original article is yanked
717 automatically."
718   (interactive
719    (list (and current-prefix-arg
720               (gnus-summary-work-articles 1))))
721   ;; Stripping headers should be specified with mail-yank-ignored-headers.
722   (when yank
723     (gnus-summary-goto-subject (car yank)))
724   (let ((gnus-article-reply t))
725     (gnus-setup-message (if yank 'reply-yank 'reply)
726       (gnus-summary-select-article)
727       (set-buffer (gnus-copy-article-buffer))
728       (gnus-msg-treat-broken-reply-to)
729       (message-reply nil wide)
730       (when yank
731         (gnus-inews-yank-articles yank)))))
732
733 (defun gnus-summary-reply-with-original (n &optional wide)
734   "Start composing a reply mail to the current message.
735 The original article will be yanked."
736   (interactive "P")
737   (gnus-summary-reply (gnus-summary-work-articles n) wide))
738
739 (defun gnus-summary-wide-reply (&optional yank)
740   "Start composing a wide reply mail to the current message.
741 If prefix argument YANK is non-nil, the original article is yanked
742 automatically."
743   (interactive
744    (list (and current-prefix-arg
745               (gnus-summary-work-articles 1))))
746   (gnus-summary-reply yank t))
747
748 (defun gnus-summary-wide-reply-with-original (n)
749   "Start composing a wide reply mail to the current message.
750 The original article will be yanked."
751   (interactive "P")
752   (gnus-summary-reply-with-original n t))
753
754 (defun gnus-summary-mail-forward (&optional full-headers post)
755   "Forward the current message to another user.
756 If FULL-HEADERS (the prefix), include full headers when forwarding."
757   (interactive "P")
758   (gnus-setup-message 'forward
759     (gnus-summary-select-article)
760     (let ((charset default-mime-charset))
761       (set-buffer gnus-original-article-buffer)
762       (make-local-variable 'default-mime-charset)
763       (setq default-mime-charset charset)
764       )
765     (let ((message-included-forward-headers
766            (if full-headers "" message-included-forward-headers)))
767       (message-forward post))))
768
769 ;;;;; XXX: generate Subject and ``Topics''?
770 ;;(defun gnus-summary-mail-digest (&optional n post)
771 ;;  "Digests and forwards all articles in this series."
772 ;;  (interactive "P")
773 ;;  (let ((subject "Digested Articles")
774 ;;      (articles (gnus-summary-work-articles n))
775 ;;      article frame)
776 ;;    (gnus-setup-message 'forward
777 ;;      (gnus-summary-select-article)
778 ;;      (if post (message-news nil subject) (message-mail nil subject))
779 ;;      (when (and message-use-multi-frames (cdr articles))
780 ;;      (setq frame (window-frame (get-buffer-window (current-buffer)))))
781 ;;      (message-goto-body)
782 ;;      (while (setq article (pop articles))
783 ;;      (save-window-excursion
784 ;;        (set-buffer gnus-summary-buffer)
785 ;;        (gnus-summary-select-article nil nil nil article)
786 ;;        (gnus-summary-remove-process-mark article))
787 ;;      (when frame
788 ;;        (select-frame frame))
789 ;;      (insert (mime-make-tag "message" "rfc822") "\n")
790 ;;      (insert-buffer-substring gnus-original-article-buffer))
791 ;;      (push-mark)
792 ;;      (message-goto-body)
793 ;;      (mime-edit-enclose-digest-region (point)(mark t)))))
794 ;;
795 ;;(defun gnus-summary-post-digest (&optional n)
796 ;;  "Digest and forwards all articles in this series to a newsgroup."
797 ;;  (interactive "P")
798 ;;  (gnus-summary-mail-digest n t))
799
800 (defun gnus-summary-resend-message (address n)
801   "Resend the current article to ADDRESS."
802   (interactive "sResend message(s) to: \nP")
803   (let ((articles (gnus-summary-work-articles n))
804         article)
805     (while (setq article (pop articles))
806       (gnus-summary-select-article nil nil nil article)
807       (save-excursion
808         (set-buffer gnus-original-article-buffer)
809         (message-resend address)))))
810
811 (defun gnus-summary-post-forward (&optional full-headers)
812   "Forward the current article to a newsgroup.
813 If FULL-HEADERS (the prefix), include full headers when forwarding."
814   (interactive "P")
815   (gnus-summary-mail-forward full-headers t))
816
817 (defvar gnus-nastygram-message
818   "The following article was inappropriately posted to %s.\n\n"
819   "Format string to insert in nastygrams.
820 The current group name will be inserted at \"%s\".")
821
822 (defun gnus-summary-mail-nastygram (n)
823   "Send a nastygram to the author of the current article."
824   (interactive "P")
825   (when (or gnus-expert-user
826             (gnus-y-or-n-p
827              "Really send a nastygram to the author of the current article? "))
828     (let ((group gnus-newsgroup-name))
829       (gnus-summary-reply-with-original n)
830       (set-buffer gnus-message-buffer)
831       (message-goto-body)
832       (insert (format gnus-nastygram-message group))
833       (message-send-and-exit))))
834
835 (defun gnus-summary-mail-crosspost-complaint (n)
836   "Send a complaint about crossposting to the current article(s)."
837   (interactive "P")
838   (let ((articles (gnus-summary-work-articles n))
839         article)
840     (while (setq article (pop articles))
841       (set-buffer gnus-summary-buffer)
842       (gnus-summary-goto-subject article)
843       (let ((group (gnus-group-real-name gnus-newsgroup-name))
844             newsgroups followup-to)
845         (gnus-summary-select-article)
846         (set-buffer gnus-original-article-buffer)
847         (if (and (<= (length (message-tokenize-header
848                               (setq newsgroups
849                                     (mail-fetch-field "newsgroups"))
850                               ", "))
851                      1)
852                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
853                      (not (member group (message-tokenize-header
854                                          followup-to ", ")))))
855             (if followup-to
856                 (gnus-message 1 "Followup-to restricted")
857               (gnus-message 1 "Not a crossposted article"))
858           (set-buffer gnus-summary-buffer)
859           (gnus-summary-reply-with-original 1)
860           (set-buffer gnus-message-buffer)
861           (message-goto-body)
862           (insert (format gnus-crosspost-complaint newsgroups group))
863           (message-goto-subject)
864           (re-search-forward " *$")
865           (replace-match " (crosspost notification)" t t)
866           (gnus-deactivate-mark)
867           (when (gnus-y-or-n-p "Send this complaint? ")
868             (message-send-and-exit)))))))
869
870 (defun gnus-summary-mail-other-window ()
871   "Compose mail in other window."
872   (interactive)
873   (gnus-setup-message 'message
874     (message-mail)))
875
876 (defun gnus-mail-parse-comma-list ()
877   (let (accumulated
878         beg)
879     (skip-chars-forward " ")
880     (while (not (eobp))
881       (setq beg (point))
882       (skip-chars-forward "^,")
883       (while (zerop
884               (save-excursion
885                 (save-restriction
886                   (let ((i 0))
887                     (narrow-to-region beg (point))
888                     (goto-char beg)
889                     (logand (progn
890                               (while (search-forward "\"" nil t)
891                                 (incf i))
892                               (if (zerop i) 2 i))
893                             2)))))
894         (skip-chars-forward ",")
895         (skip-chars-forward "^,"))
896       (skip-chars-backward " ")
897       (push (buffer-substring beg (point))
898             accumulated)
899       (skip-chars-forward "^,")
900       (skip-chars-forward ", "))
901     accumulated))
902
903 (defun gnus-inews-add-to-address (group)
904   (let ((to-address (mail-fetch-field "to")))
905     (when (and to-address
906                (gnus-alive-p))
907       ;; This mail group doesn't have a `to-list', so we add one
908       ;; here.  Magic!
909       (when (gnus-y-or-n-p
910              (format "Do you want to add this as `to-list': %s " to-address))
911         (gnus-group-add-parameter group (cons 'to-list to-address))))))
912
913 (defun gnus-put-message ()
914   "Put the current message in some group and return to Gnus."
915   (interactive)
916   (let ((reply gnus-article-reply)
917         (winconf gnus-prev-winconf)
918         (group gnus-newsgroup-name))
919
920     (or (and group (not (gnus-group-read-only-p group)))
921         (setq group (read-string "Put in group: " nil
922                                  (gnus-writable-groups))))
923     (when (gnus-gethash group gnus-newsrc-hashtb)
924       (error "No such group: %s" group))
925
926     (save-excursion
927       (save-restriction
928         (widen)
929         (message-narrow-to-headers)
930         (let (gnus-deletable-headers)
931           (if (message-news-p)
932               (message-generate-headers message-required-news-headers)
933             (message-generate-headers message-required-mail-headers)))
934         (goto-char (point-max))
935         (insert "Gcc: " group "\n")
936         (widen)))
937
938     (gnus-inews-do-gcc)
939
940     (when (get-buffer gnus-group-buffer)
941       (when (gnus-buffer-exists-p (car-safe reply))
942         (set-buffer (car reply))
943         (and (cdr reply)
944              (gnus-summary-mark-article-as-replied
945               (cdr reply))))
946       (when winconf
947         (set-window-configuration winconf)))))
948
949 (defun gnus-article-mail (yank)
950   "Send a reply to the address near point.
951 If YANK is non-nil, include the original article."
952   (interactive "P")
953   (let ((address
954          (buffer-substring
955           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
956           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
957     (when address
958       (message-reply address)
959       (when yank
960         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
961
962 (defvar nntp-server-type)
963 (defun gnus-bug ()
964   "Send a bug report to the Gnus maintainers."
965   (interactive)
966   (unless (gnus-alive-p)
967     (error "Gnus has been shut down"))
968   (gnus-setup-message 'bug
969     (delete-other-windows)
970     (when gnus-bug-create-help-buffer
971       (switch-to-buffer "*Gnus Help Bug*")
972       (erase-buffer)
973       (insert gnus-bug-message)
974       (goto-char (point-min)))
975     (message-pop-to-buffer "*Gnus Bug*")
976     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
977     (when gnus-bug-create-help-buffer
978       (push `(gnus-bug-kill-buffer) message-send-actions))
979     (goto-char (point-min))
980     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
981     (forward-line 1)
982     (insert gnus-product-name " " gnus-version-number
983             " (r" gnus-revision-number ") "
984             "based on " gnus-original-product-name " v"
985             gnus-original-version-number "\n"
986             (emacs-version) "\n")
987     (when (and (boundp 'nntp-server-type)
988                (stringp nntp-server-type))
989       (insert nntp-server-type))
990     (insert "\n\n\n\n\n")
991     (let (mime-content-types)
992       (mime-edit-insert-tag "text" "plain" "; type=emacs-lisp"))
993     (insert (with-temp-buffer
994               (gnus-debug)
995               (buffer-string)))
996     (let (mime-content-types)
997       (mime-edit-insert-tag "text" "plain"))
998     (goto-char (point-min))
999     (search-forward "Subject: " nil t)
1000     (message "")))
1001
1002 (defun gnus-bug-kill-buffer ()
1003   (when (get-buffer "*Gnus Help Bug*")
1004     (kill-buffer "*Gnus Help Bug*")))
1005
1006 (defun gnus-summary-yank-message (buffer n)
1007   "Yank the current article into a composed message."
1008   (interactive
1009    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1010          current-prefix-arg))
1011   (when (gnus-buffer-live-p buffer)
1012     (let ((summary-frame (selected-frame))
1013           (message-frame (when (static-if (featurep 'xemacs)
1014                                    (device-on-window-system-p)
1015                                  window-system)
1016                            (let ((window (get-buffer-window buffer t)))
1017                              (when window
1018                                (window-frame window)))))
1019           (separator (concat "^" (regexp-quote mail-header-separator)
1020                              "\n")))
1021       (gnus-summary-iterate n
1022         (gnus-summary-select-article)
1023         (gnus-copy-article-buffer)
1024         (when (frame-live-p message-frame)
1025           (raise-frame message-frame)
1026           (select-frame message-frame))
1027         (with-current-buffer buffer
1028           (when (save-excursion
1029                   (beginning-of-line)
1030                   (let (case-fold-search)
1031                     (and (not (re-search-backward separator nil t))
1032                          (re-search-forward separator nil t))))
1033             (goto-char (match-end 0)))
1034           (message-yank-buffer gnus-article-copy))
1035         (select-frame summary-frame))
1036       (when (frame-live-p message-frame)
1037         (select-frame message-frame)))))
1038
1039 (defun gnus-debug ()
1040   "Attempts to go through the Gnus source file and report what variables have been changed.
1041 The source file has to be in the Emacs load path."
1042   (interactive)
1043   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1044                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1045                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1046                  "nnmail.el" "nntp.el" "message.el"))
1047         (point (point))
1048         file expr olist sym)
1049     (gnus-message 4 "Please wait while we snoop your variables...")
1050     (sit-for 0)
1051     ;; Go through all the files looking for non-default values for variables.
1052     (save-excursion
1053       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1054       (while files
1055         (erase-buffer)
1056         (when (and (setq file (locate-library (pop files)))
1057                    (file-exists-p file))
1058           (insert-file-contents file)
1059           (goto-char (point-min))
1060           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1061               (gnus-message 4 "Malformed sources in file %s" file)
1062             (narrow-to-region (point-min) (point))
1063             (goto-char (point-min))
1064             (while (setq expr (ignore-errors (read (current-buffer))))
1065               (ignore-errors
1066                 (and (memq (car expr) '(defvar defcustom defvoo))
1067                      (stringp (nth 3 expr))
1068                      (or (not (boundp (nth 1 expr)))
1069                          (not (equal (eval (nth 2 expr))
1070                                      (symbol-value (nth 1 expr)))))
1071                      (push (nth 1 expr) olist)))))))
1072       (kill-buffer (current-buffer)))
1073     (when (setq olist (nreverse olist))
1074       (insert ";----------------- Environment follows ------------------\n\n"))
1075     (while olist
1076       (if (boundp (car olist))
1077           (condition-case ()
1078               (pp `(setq ,(car olist)
1079                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1080                                   (and (symbolp sym)
1081                                        (not (or (eq sym nil)
1082                                                 (eq sym t)))))
1083                               (list 'quote (symbol-value (car olist)))
1084                             (symbol-value (car olist))))
1085                   (current-buffer))
1086             (error
1087              (format "(setq %s 'whatever)\n" (car olist))))
1088         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1089       (setq olist (cdr olist)))
1090     ;; Remove any control chars - they seem to cause trouble for some
1091     ;; mailers.  (Byte-compiled output from the stuff above.)
1092     (goto-char point)
1093     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1094       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1095                      t t))
1096     ;; Break MIME tags purposely.
1097     (goto-char point)
1098     (while (re-search-forward mime-edit-tag-regexp nil t)
1099       (goto-char (1+ (match-beginning 0)))
1100       (insert "X"))))
1101
1102 ;;; Treatment of rejected articles.
1103 ;;; Bounced mail.
1104
1105 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1106   "Re-mail the current message.
1107 This only makes sense if the current message is a bounce message than
1108 contains some mail you have written which has been bounced back to
1109 you.
1110 If FETCH, try to fetch the article that this is a reply to, if indeed
1111 this is a reply."
1112   (interactive "P")
1113   (gnus-summary-select-article t)
1114   (set-buffer gnus-original-article-buffer)
1115   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1116     (gnus-setup-message 'compose-bounce
1117       (let* ((references (mail-fetch-field "references"))
1118              (parent (and references (gnus-parent-id references))))
1119         (message-bounce)
1120         ;; If there are references, we fetch the article we answered to.
1121         (and fetch parent
1122              (gnus-summary-refer-article parent)
1123              (gnus-summary-show-all-headers))))))
1124
1125 ;;; Gcc handling.
1126
1127 ;; Do Gcc handling, which copied the message over to some group.
1128 (defun gnus-inews-do-gcc (&optional gcc)
1129   (interactive)
1130   (when (gnus-alive-p)
1131     (save-excursion
1132       (save-restriction
1133         (message-narrow-to-headers)
1134         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1135               (coding-system-for-write 'raw-text)
1136               (output-coding-system 'raw-text)
1137               groups group method)
1138           (when gcc
1139             (message-remove-header "gcc")
1140             (widen)
1141             (setq groups (message-unquote-tokens
1142                           (message-tokenize-header gcc " ,")))
1143             ;; Copy the article over to some group(s).
1144             (while (setq group (pop groups))
1145               (gnus-check-server
1146                (setq method
1147                      (cond ((and (null (gnus-get-info group))
1148                                  (eq (car gnus-message-archive-method)
1149                                      (car
1150                                       (gnus-server-to-method
1151                                        (gnus-group-method group)))))
1152                             ;; If the group doesn't exist, we assume
1153                             ;; it's an archive group...
1154                             gnus-message-archive-method)
1155                            ;; Use the method.
1156                            ((gnus-info-method (gnus-get-info group))
1157                             (gnus-info-method (gnus-get-info group)))
1158                            ;; Find the method.
1159                            (t (gnus-group-method group)))))
1160               (gnus-check-server method)
1161               (unless (gnus-request-group group t method)
1162                 (gnus-request-create-group group method))
1163               (save-excursion
1164                 (nnheader-set-temp-buffer " *acc*")
1165                 (insert-buffer-substring message-encoding-buffer)
1166                 (gnus-run-hooks 'gnus-before-do-gcc-hook)
1167                 (goto-char (point-min))
1168                 (when (re-search-forward
1169                        (concat "^" (regexp-quote mail-header-separator) "$")
1170                        nil t)
1171                   (replace-match "" t t ))
1172                 (unless (gnus-request-accept-article group method t t)
1173                   (gnus-message 1 "Couldn't store article in group %s: %s"
1174                                 group (gnus-status-message method))
1175                   (sit-for 2))
1176                 (kill-buffer (current-buffer))))))))))
1177
1178 (defun gnus-inews-insert-gcc ()
1179   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1180   (save-excursion
1181     (save-restriction
1182       (message-narrow-to-headers)
1183       (let* ((group gnus-outgoing-message-group)
1184              (gcc (cond
1185                    ((gnus-functionp group)
1186                     (funcall group))
1187                    ((or (stringp group) (list group))
1188                     group))))
1189         (when gcc
1190           (insert "Gcc: "
1191                   (if (stringp gcc) gcc
1192                     (mapconcat 'identity gcc " "))
1193                   "\n"))))))
1194
1195 (defun gnus-inews-insert-archive-gcc (&optional group)
1196   "Insert the Gcc to say where the article is to be archived."
1197   (let* ((var gnus-message-archive-group)
1198          (group (or group gnus-newsgroup-name ""))
1199          (gcc-self-val
1200           (and gnus-newsgroup-name
1201                (not (equal gnus-newsgroup-name ""))
1202                (gnus-group-find-parameter
1203                 gnus-newsgroup-name 'gcc-self)))
1204          result
1205          (groups
1206           (cond
1207            ((null gnus-message-archive-method)
1208             ;; Ignore.
1209             nil)
1210            ((stringp var)
1211             ;; Just a single group.
1212             (list var))
1213            ((null var)
1214             ;; We don't want this.
1215             nil)
1216            ((and (listp var) (stringp (car var)))
1217             ;; A list of groups.
1218             var)
1219            ((gnus-functionp var)
1220             ;; A function.
1221             (funcall var group))
1222            (t
1223             ;; An alist of regexps/functions/forms.
1224             (while (and var
1225                         (not
1226                          (setq result
1227                                (cond
1228                                 ((stringp (caar var))
1229                                  ;; Regexp.
1230                                  (when (string-match (caar var) group)
1231                                    (cdar var)))
1232                                 ((gnus-functionp (car var))
1233                                  ;; Function.
1234                                  (funcall (car var) group))
1235                                 (t
1236                                  (eval (car var)))))))
1237               (setq var (cdr var)))
1238             result)))
1239          name)
1240     (when (or groups gcc-self-val)
1241       (when (stringp groups)
1242         (setq groups (list groups)))
1243       (save-excursion
1244         (save-restriction
1245           (message-narrow-to-headers)
1246           (goto-char (point-max))
1247           (insert "Gcc: ")
1248           (if gcc-self-val
1249               ;; Use the `gcc-self' param value instead.
1250               (progn
1251                 (insert
1252                  (if (stringp gcc-self-val)
1253                      gcc-self-val
1254                    group))
1255                 (if (not (eq gcc-self-val 'none))
1256                     (insert "\n")
1257                   (progn
1258                     (beginning-of-line)
1259                     (kill-line))))
1260             ;; Use the list of groups.
1261             (while (setq name (pop groups))
1262               (insert (if (string-match ":" name)
1263                           name
1264                         (gnus-group-prefixed-name
1265                          name gnus-message-archive-method)))
1266               (when groups
1267                 (insert " ")))
1268             (insert "\n")))))))
1269
1270 ;;; Posting styles.
1271
1272 (defun gnus-configure-posting-styles ()
1273   "Configure posting styles according to `gnus-posting-styles'."
1274   (unless gnus-inhibit-posting-styles
1275     (let ((group (or gnus-newsgroup-name ""))
1276           (styles gnus-posting-styles)
1277           style match variable attribute value v results
1278           filep name address element)
1279       ;; If the group has a posting-style parameter, add it at the end with a
1280       ;; regexp matching everything, to be sure it takes precedence over all
1281       ;; the others.
1282       (when gnus-newsgroup-name
1283         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1284           (when tmp-style
1285             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1286       ;; Go through all styles and look for matches.
1287       (dolist (style styles)
1288         (setq match (pop style))
1289         (goto-char (point-min))
1290         (when (cond
1291                ((stringp match)
1292                 ;; Regexp string match on the group name.
1293                 (string-match match group))
1294                ((eq match 'header)
1295                 (let ((header (message-fetch-field (pop style))))
1296                   (and header
1297                        (string-match (pop style) header))))
1298                ((or (symbolp match)
1299                     (gnus-functionp match))
1300                 (cond
1301                  ((gnus-functionp match)
1302                   ;; Function to be called.
1303                   (funcall match))
1304                  ((boundp match)
1305                   ;; Variable to be checked.
1306                   (symbol-value match))))
1307                ((listp match)
1308                 ;; This is a form to be evaled.
1309                 (eval match)))
1310           ;; We have a match, so we set the variables.
1311           (dolist (attribute style)
1312             (setq element (pop attribute)
1313                   variable nil
1314                   filep nil)
1315             (setq value
1316                   (cond
1317                    ((eq (car attribute) ':file)
1318                     (setq filep t)
1319                     (cadr attribute))
1320                    ((eq (car attribute) :value)
1321                     (cadr attribute))
1322                    (t
1323                     (car attribute))))
1324             ;; We get the value.
1325             (setq v
1326                   (cond
1327                    ((stringp value)
1328                     value)
1329                    ((or (symbolp value)
1330                         (gnus-functionp value))
1331                     (cond ((gnus-functionp value)
1332                            (funcall value))
1333                           ((boundp value)
1334                            (symbol-value value))))
1335                    ((listp value)
1336                     (eval value))))
1337             ;; Translate obsolescent value.
1338             (when (eq element 'signature-file)
1339               (setq element 'signature
1340                     filep t))
1341             ;; Get the contents of file elems.
1342             (when (and filep v)
1343               (setq v (with-temp-buffer
1344                         (insert-file-contents v)
1345                         (buffer-string))))
1346             (setq results (delq (assoc element results) results))
1347             (push (cons element v) results))))
1348       ;; Now we have all the styles, so we insert them.
1349       (setq name (assq 'name results)
1350             address (assq 'address results))
1351       (setq results (delq name (delq address results)))
1352       (make-local-variable 'message-setup-hook)
1353       (dolist (result results)
1354         (add-hook 'message-setup-hook
1355                   (cond
1356                    ((eq 'eval (car result))
1357                     'ignore)
1358                    ((eq 'body (car result))
1359                     `(lambda ()
1360                        (save-excursion
1361                          (message-goto-body)
1362                          (insert ,(cdr result)))))
1363                    ((eq 'signature (car result))
1364                     (set (make-local-variable 'message-signature) nil)
1365                     (set (make-local-variable 'message-signature-file) nil)
1366                     (if (not (cdr result))
1367                         'ignore
1368                       `(lambda ()
1369                          (save-excursion
1370                            (let ((message-signature ,(cdr result)))
1371                              (when message-signature
1372                                (message-insert-signature)))))))
1373                    (t
1374                     (let ((header
1375                            (if (symbolp (car result))
1376                                (capitalize (symbol-name (car result)))
1377                              (car result))))
1378                       `(lambda ()
1379                          (save-excursion
1380                            (message-remove-header ,header)
1381                            (let ((value ,(cdr result)))
1382                              (when value
1383                                (message-goto-eoh)
1384                                (insert ,header ": " value "\n"))))))))))
1385       (when (or name address)
1386         (add-hook 'message-setup-hook
1387                   `(lambda ()
1388                      (set (make-local-variable 'user-mail-address)
1389                           ,(or (cdr address) user-mail-address))
1390                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1391                            (user-mail-address
1392                             ,(or (cdr address) user-mail-address)))
1393                        (save-excursion
1394                          (message-remove-header "From")
1395                          (message-goto-eoh)
1396                          (insert "From: " (message-make-from) "\n")))))))))
1397
1398
1399 ;;; @ for MIME Edit mode
1400 ;;;
1401
1402 (defun gnus-maybe-setup-default-charset ()
1403   (let ((charset
1404          (and (boundp 'gnus-summary-buffer)
1405               (buffer-live-p gnus-summary-buffer)
1406               (save-excursion
1407                 (set-buffer gnus-summary-buffer)
1408                 default-mime-charset))))
1409     (if charset
1410         (progn
1411           (make-local-variable 'default-mime-charset)
1412           (setq default-mime-charset charset)
1413           ))))
1414
1415
1416 ;;; @ for MIME view mode
1417 ;;;
1418
1419 (defun gnus-following-method (buf)
1420   (gnus-setup-message 'reply-yank
1421     (set-buffer buf)
1422     (if (message-news-p)
1423         (message-followup)
1424       (message-reply nil 'wide))
1425     (let ((message-reply-buffer buf))
1426       (message-yank-original))
1427     (message-goto-body))
1428   (kill-buffer buf))
1429
1430
1431 ;;; Allow redefinition of functions.
1432
1433 (gnus-ems-redefine)
1434
1435 (provide 'gnus-msg)
1436
1437 ;;; gnus-msg.el ends here