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