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