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