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