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