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