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