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