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