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