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