64496f2207486433605d97ff6ea0f84ff88b995d
[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 mistake 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         ;; make sure last viewed article doesn't affect posting styles:
494         (gnus-article-copy)
495         (buffer (current-buffer)))
496     (unwind-protect
497         (progn
498           (setq gnus-newsgroup-name
499                 (if arg
500                     (if (= 1 (prefix-numeric-value arg))
501                         (completing-read "Use posting style of group: "
502                                          gnus-active-hashtb nil
503                                          (gnus-read-active-file-p))
504                       (gnus-group-group-name))
505                   ""))
506           ;; #### see comment in gnus-setup-message -- drv
507           (gnus-setup-message 'message (message-mail)))
508       (save-excursion
509         (set-buffer buffer)
510         (setq gnus-newsgroup-name group)))))
511
512 (defun gnus-group-news (&optional arg)
513   "Start composing a news.
514 If ARG, post to group under point.
515 If ARG is 1, prompt for group name to post to.
516
517 This function prepares a news even when using mail groups.  This is useful
518 for posting messages to mail groups without actually sending them over the
519 network.  The corresponding backend must have a 'request-post method."
520   (interactive "P")
521   ;; We can't `let' gnus-newsgroup-name here, since that leads
522   ;; to local variables leaking.
523   (let ((group gnus-newsgroup-name)
524         ;; make sure last viewed article doesn't affect posting styles:
525         (gnus-article-copy)
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                       (gnus-group-group-name))
536                   ""))
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-group-post-news (&optional arg)
545   "Start composing a message (a news by default).
546 If ARG, post to group under point.  If ARG is 1, prompt for group name.
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                (gnus-group-group-name))
557            ""))
558         ;; make sure last viewed article doesn't affect posting styles:
559         (gnus-article-copy))
560     (gnus-post-news 'post gnus-newsgroup-name)))
561
562 (defun gnus-summary-mail-other-window (&optional arg)
563   "Start composing a mail in another window.
564 Use the posting of the current group by default.
565 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
566 posting style."
567   (interactive "P")
568   ;; We can't `let' gnus-newsgroup-name here, since that leads
569   ;; to local variables leaking.
570   (let ((group gnus-newsgroup-name)
571         ;; make sure last viewed article doesn't affect posting styles:
572         (gnus-article-copy)
573         (buffer (current-buffer)))
574     (unwind-protect
575         (progn
576           (setq gnus-newsgroup-name
577                 (if arg
578                     (if (= 1 (prefix-numeric-value arg))
579                         (completing-read "Use group: "
580                                          gnus-active-hashtb nil
581                                          (gnus-read-active-file-p))
582                       "")
583                   gnus-newsgroup-name))
584           ;; #### see comment in gnus-setup-message -- drv
585           (gnus-setup-message 'message (message-mail)))
586       (save-excursion
587         (set-buffer buffer)
588         (setq gnus-newsgroup-name group)))))
589
590 (defun gnus-summary-news-other-window (&optional arg)
591   "Start composing a news in another window.
592 Post to the current group by default.
593 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
594
595 This function prepares a news even when using mail groups.  This is useful
596 for posting messages to mail groups without actually sending them over the
597 network.  The corresponding backend must have a 'request-post method."
598   (interactive "P")
599   ;; We can't `let' gnus-newsgroup-name here, since that leads
600   ;; to local variables leaking.
601   (let ((group gnus-newsgroup-name)
602         ;; make sure last viewed article doesn't affect posting styles:
603         (gnus-article-copy)
604         (buffer (current-buffer)))
605     (unwind-protect
606         (progn
607           (setq gnus-newsgroup-name
608                 (if arg
609                     (if (= 1 (prefix-numeric-value arg))
610                         (completing-read "Use group: "
611                                          gnus-active-hashtb nil
612                                          (gnus-read-active-file-p))
613                       "")
614                   gnus-newsgroup-name))
615           ;; #### see comment in gnus-setup-message -- drv
616           (gnus-setup-message 'message
617             (message-news (gnus-group-real-name gnus-newsgroup-name))))
618       (save-excursion
619         (set-buffer buffer)
620         (setq gnus-newsgroup-name group)))))
621
622 (defun gnus-summary-post-news (&optional arg)
623   "Start composing a message.  Post to the current group by default.
624 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
625 Depending on the selected group, the message might be either a mail or
626 a news."
627   (interactive "P")
628   ;; Bind this variable here to make message mode hooks work ok.
629   (let ((gnus-newsgroup-name
630          (if arg
631              (if (= 1 (prefix-numeric-value arg))
632                  (completing-read "Newsgroup: " gnus-active-hashtb nil
633                                   (gnus-read-active-file-p))
634                "")
635            gnus-newsgroup-name))
636         ;; make sure last viewed article doesn't affect posting styles:
637         (gnus-article-copy))
638     (gnus-post-news 'post gnus-newsgroup-name)))
639
640
641 (defun gnus-summary-followup (yank &optional force-news)
642   "Compose a followup to an article.
643 If prefix argument YANK is non-nil, the original article is yanked
644 automatically.
645 YANK is a list of elements, where the car of each element is the
646 article number, and the two following numbers is the region to be
647 yanked."
648   (interactive
649    (list (and current-prefix-arg
650               (gnus-summary-work-articles 1))))
651   (when yank
652     (gnus-summary-goto-subject
653      (if (listp (car yank))
654          (caar yank)
655        (car yank))))
656   (save-window-excursion
657     (gnus-summary-select-article))
658   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
659         (gnus-newsgroup-name gnus-newsgroup-name))
660     ;; Send a followup.
661     (gnus-post-news nil gnus-newsgroup-name
662                     headers gnus-article-buffer
663                     yank nil force-news)
664     (gnus-summary-handle-replysign)))
665
666 (defun gnus-summary-followup-with-original (n &optional force-news)
667   "Compose a followup to an article and include the original article."
668   (interactive "P")
669   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
670
671 (defun gnus-summary-followup-to-mail (&optional arg)
672   "Followup to the current mail message via news."
673   (interactive
674    (list (and current-prefix-arg
675               (gnus-summary-work-articles 1))))
676   (gnus-summary-followup arg t))
677
678 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
679   "Followup to the current mail message via news."
680   (interactive "P")
681   (gnus-summary-followup (gnus-summary-work-articles arg) t))
682
683 (defun gnus-inews-yank-articles (articles)
684   (let (beg article yank-string)
685     (message-goto-body)
686     (while (setq article (pop articles))
687       (when (listp article)
688         (setq yank-string (nth 1 article)
689               article (nth 0 article)))
690       (save-window-excursion
691         (set-buffer gnus-summary-buffer)
692         (gnus-summary-select-article nil nil nil article)
693         (gnus-summary-remove-process-mark article))
694       (gnus-copy-article-buffer nil yank-string)
695       (let ((message-reply-buffer gnus-article-copy)
696             (message-reply-headers
697              ;; The headers are decoded.
698              (with-current-buffer gnus-article-copy
699                (save-restriction
700                  (nnheader-narrow-to-headers)
701                  (ietf-drums-unfold-fws)
702                  (nnheader-parse-head t)))))
703         (message-yank-original)
704         (setq beg (or beg (mark t))))
705       (when articles
706         (insert "\n")))
707     (push-mark)
708     (goto-char beg)))
709
710 (defun gnus-summary-cancel-article (&optional n symp)
711   "Cancel an article you posted.
712 Uses the process-prefix convention.  If given the symbolic
713 prefix `a', cancel using the standard posting method; if not
714 post using the current select method."
715   (interactive (gnus-interactive "P\ny"))
716   (let ((articles (gnus-summary-work-articles n))
717         (message-post-method
718          `(lambda (arg)
719             (gnus-post-method (eq ',symp 'a) ,gnus-newsgroup-name)))
720         article)
721     (while (setq article (pop articles))
722       (when (gnus-summary-select-article t nil nil article)
723         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
724                 (message-cancel-news))
725           (gnus-summary-mark-as-read article gnus-canceled-mark)
726           (gnus-cache-remove-article 1))
727         (gnus-article-hide-headers-if-wanted))
728       (gnus-summary-remove-process-mark article))))
729
730 (defun gnus-summary-supersede-article ()
731   "Compose an article that will supersede a previous article.
732 This is done simply by taking the old article and adding a Supersedes
733 header line with the old Message-ID."
734   (interactive)
735   (let ((article (gnus-summary-article-number)))
736     (gnus-setup-message 'reply-yank
737       (gnus-summary-select-article t)
738       (set-buffer gnus-original-article-buffer)
739       (message-supersede)
740       (push
741        `((lambda ()
742            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
743              (save-excursion
744                (set-buffer ,gnus-summary-buffer)
745                (gnus-cache-possibly-remove-article ,article nil nil nil t)
746                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
747        message-send-actions))))
748
749 \f
750
751 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
752   ;; make a copy of the article buffer with all text properties removed
753   ;; this copy is in the buffer gnus-article-copy.
754   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
755   ;; this buffer should be passed to all mail/news reply/post routines.
756   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
757   (save-excursion
758     (set-buffer gnus-article-copy)
759     (mm-enable-multibyte))
760   (let ((article-buffer (or article-buffer gnus-article-buffer))
761         end beg)
762     (if (not (and (get-buffer article-buffer)
763                   (gnus-buffer-exists-p article-buffer)))
764         (error "Can't find any article buffer")
765       (save-excursion
766         (set-buffer article-buffer)
767         (let ((gnus-newsgroup-charset (or gnus-article-charset
768                                           gnus-newsgroup-charset))
769               (gnus-newsgroup-ignored-charsets
770                (or gnus-article-ignored-charsets
771                    gnus-newsgroup-ignored-charsets)))
772           (save-restriction
773             ;; Copy over the (displayed) article buffer, delete
774             ;; hidden text and remove text properties.
775             (widen)
776             (copy-to-buffer gnus-article-copy (point-min) (point-max))
777             (set-buffer gnus-article-copy)
778             (when yank-string
779               (message-goto-body)
780               (delete-region (point) (point-max))
781               (insert yank-string))
782             (gnus-article-delete-text-of-type 'annotation)
783             (gnus-remove-text-with-property 'gnus-prev)
784             (gnus-remove-text-with-property 'gnus-next)
785             (gnus-remove-text-with-property 'gnus-decoration)
786             (insert
787              (prog1
788                  (buffer-substring-no-properties (point-min) (point-max))
789                (erase-buffer)))
790             ;; Find the original headers.
791             (set-buffer gnus-original-article-buffer)
792             (goto-char (point-min))
793             (while (looking-at message-unix-mail-delimiter)
794               (forward-line 1))
795             (setq beg (point)
796                   end (or (message-goto-body) beg))
797             ;; Delete the headers from the displayed articles.
798             (set-buffer gnus-article-copy)
799             (delete-region (goto-char (point-min))
800                            (or (message-goto-body) (point-max)))
801             ;; Insert the original article headers.
802             (insert-buffer-substring gnus-original-article-buffer beg end)
803             ;; Decode charsets.
804             (let ((gnus-article-decode-hook
805                    (delq 'article-decode-charset
806                          (copy-sequence gnus-article-decode-hook))))
807               (run-hooks 'gnus-article-decode-hook)))))
808       gnus-article-copy)))
809
810 (defun gnus-post-news (post &optional group header article-buffer yank subject
811                             force-news)
812   (when article-buffer
813     (gnus-copy-article-buffer))
814   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
815         (add-to-list gnus-add-to-list))
816     (gnus-setup-message (cond (yank 'reply-yank)
817                               (article-buffer 'reply)
818                               (t 'message))
819       (let* ((group (or group gnus-newsgroup-name))
820              (charset (gnus-group-name-charset nil group))
821              (pgroup group)
822              to-address to-group mailing-list to-list
823              newsgroup-p)
824         (when group
825           (setq to-address (gnus-parameter-to-address group)
826                 to-group (gnus-group-find-parameter group 'to-group)
827                 to-list (gnus-parameter-to-list group)
828                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
829                 mailing-list (when gnus-mailing-list-groups
830                                (string-match gnus-mailing-list-groups group))
831                 group (gnus-group-name-decode (gnus-group-real-name group)
832                                               charset)))
833         (if (or (and to-group
834                      (gnus-news-group-p to-group))
835                 newsgroup-p
836                 force-news
837                 (and (gnus-news-group-p
838                       (or pgroup gnus-newsgroup-name)
839                       (or header gnus-current-article))
840                      (not mailing-list)
841                      (not to-list)
842                      (not to-address)))
843             ;; This is news.
844             (if post
845                 (message-news (or to-group group))
846               (set-buffer gnus-article-copy)
847               (gnus-msg-treat-broken-reply-to)
848               (message-followup (if (or newsgroup-p force-news)
849                                     (if (save-restriction
850                                           (article-narrow-to-head)
851                                           (message-fetch-field "newsgroups"))
852                                         nil
853                                       "")
854                                   to-group)))
855           ;; The is mail.
856           (if post
857               (progn
858                 (message-mail (or to-address to-list))
859                 ;; Arrange for mail groups that have no `to-address' to
860                 ;; get that when the user sends off the mail.
861                 (when (and (not to-list)
862                            (not to-address)
863                            add-to-list)
864                   (push (list 'gnus-inews-add-to-address pgroup)
865                         message-send-actions)))
866             (set-buffer gnus-article-copy)
867             (gnus-msg-treat-broken-reply-to)
868             (message-wide-reply to-address)))
869         (when yank
870           (gnus-inews-yank-articles yank))))))
871
872 (defun gnus-msg-treat-broken-reply-to (&optional force)
873   "Remove the Reply-to header iff broken-reply-to."
874   (when (or force
875             (gnus-group-find-parameter
876              gnus-newsgroup-name 'broken-reply-to))
877     (save-restriction
878       (message-narrow-to-head)
879       (message-remove-header "reply-to"))))
880
881 (defun gnus-post-method (arg group &optional silent)
882   "Return the posting method based on GROUP and ARG.
883 If SILENT, don't prompt the user."
884   (let ((gnus-post-method (or (gnus-parameter-post-method group)
885                               gnus-post-method))
886         (group-method (gnus-find-method-for-group group)))
887     (cond
888      ;; If the group-method is nil (which shouldn't happen) we use
889      ;; the default method.
890      ((null group-method)
891       (or (and (listp gnus-post-method) ;If not current/native/nil
892                (not (listp (car gnus-post-method))) ; and not a list of methods
893                gnus-post-method)        ;then use it.
894           gnus-select-method
895           message-post-method))
896      ;; We want the inverse of the default
897      ((and arg (not (eq arg 0)))
898       (if (eq gnus-post-method 'current)
899           gnus-select-method
900         group-method))
901      ;; We query the user for a post method.
902      ((or arg
903           (and (listp gnus-post-method)
904                (listp (car gnus-post-method))))
905       (let* ((methods
906               ;; Collect all methods we know about.
907               (append
908                (when (listp gnus-post-method)
909                  (if (listp (car gnus-post-method))
910                      gnus-post-method
911                    (list gnus-post-method)))
912                gnus-secondary-select-methods
913                (mapcar 'cdr gnus-server-alist)
914                (mapcar 'car gnus-opened-servers)
915                (list gnus-select-method)
916                (list group-method)))
917              method-alist post-methods method)
918         ;; Weed out all mail methods.
919         (while methods
920           (setq method (gnus-server-get-method "" (pop methods)))
921           (when (and (or (gnus-method-option-p method 'post)
922                          (gnus-method-option-p method 'post-mail))
923                      (not (member method post-methods)))
924             (push method post-methods)))
925         ;; Create a name-method alist.
926         (setq method-alist
927               (mapcar
928                (lambda (m)
929                  (if (equal (cadr m) "")
930                      (list (symbol-name (car m)) m)
931                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
932                post-methods))
933         ;; Query the user.
934         (cadr
935          (assoc
936           (setq gnus-last-posting-server
937                 (if (and silent
938                          gnus-last-posting-server)
939                     ;; Just use the last value.
940                     gnus-last-posting-server
941                   (completing-read
942                    "Posting method: " method-alist nil t
943                    (cons (or gnus-last-posting-server "") 0))))
944           method-alist))))
945      ;; Override normal method.
946      ((and (eq gnus-post-method 'current)
947            (not (memq (car group-method) gnus-discouraged-post-methods))
948            (gnus-get-function group-method 'request-post t))
949       (assert (not arg))
950       group-method)
951      ;; Use gnus-post-method.
952      ((listp gnus-post-method)          ;A method...
953       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
954       gnus-post-method)
955      ;; Use the normal select method (nil or native).
956      (t gnus-select-method))))
957
958 \f
959
960 ;; Dummies to avoid byte-compile warning.
961 (eval-when-compile
962   (defvar nnspool-rejected-article-hook)
963   (defvar xemacs-codename))
964
965 (defun gnus-extended-version ()
966   "Stringified Gnus version and Emacs version."
967   (interactive)
968   (concat
969    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
970    " (" gnus-version ")"
971    " "
972    (cond
973     ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
974      (concat "Emacs/" (match-string 1 emacs-version)
975              " (" system-configuration ")"))
976     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
977                    emacs-version)
978      (concat (match-string 1 emacs-version)
979              (format "/%d.%d" emacs-major-version emacs-minor-version)
980              (if (match-beginning 3)
981                  (match-string 3 emacs-version)
982                "")
983              (if (boundp 'xemacs-codename)
984                  (concat " (" xemacs-codename ", " system-configuration ")")
985                "")))
986     (t emacs-version))))
987
988 \f
989 ;;;
990 ;;; Gnus Mail Functions
991 ;;;
992
993 ;;; Mail reply commands of Gnus summary mode
994
995 (defun gnus-summary-reply (&optional yank wide very-wide)
996   "Start composing a mail reply to the current message.
997 If prefix argument YANK is non-nil, the original article is yanked
998 automatically.
999 If WIDE, make a wide reply.
1000 If VERY-WIDE, make a very wide reply."
1001   (interactive
1002    (list (and current-prefix-arg
1003               (gnus-summary-work-articles 1))))
1004   ;; Allow user to require confirmation before replying by mail to the
1005   ;; author of a news article.
1006   (when (or (not (gnus-news-group-p gnus-newsgroup-name))
1007             (not gnus-confirm-mail-reply-to-news)
1008             (y-or-n-p "Really reply by mail to article author? "))
1009     (let* ((article
1010             (if (listp (car yank))
1011                 (caar yank)
1012               (car yank)))
1013            (gnus-article-reply (or article (gnus-summary-article-number)))
1014            (headers ""))
1015       ;; Stripping headers should be specified with mail-yank-ignored-headers.
1016       (when yank
1017         (gnus-summary-goto-subject article))
1018       (gnus-setup-message (if yank 'reply-yank 'reply)
1019         (if (not very-wide)
1020             (gnus-summary-select-article)
1021           (dolist (article very-wide)
1022             (gnus-summary-select-article nil nil nil article)
1023             (save-excursion
1024               (set-buffer (gnus-copy-article-buffer))
1025               (gnus-msg-treat-broken-reply-to)
1026               (save-restriction
1027                 (message-narrow-to-head)
1028                 (setq headers (concat headers (buffer-string)))))))
1029         (set-buffer (gnus-copy-article-buffer))
1030         (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
1031         (save-restriction
1032           (message-narrow-to-head)
1033           (when very-wide
1034             (erase-buffer)
1035             (insert headers))
1036           (goto-char (point-max)))
1037         (mml-quote-region (point) (point-max))
1038         (message-reply nil wide)
1039         (when yank
1040           (gnus-inews-yank-articles yank))
1041         (gnus-summary-handle-replysign)))))
1042
1043 (defun gnus-summary-handle-replysign ()
1044   "Check the various replysign variables and take action accordingly."
1045   (when (or gnus-message-replysign gnus-message-replyencrypt)
1046     (let (signed encrypted)
1047       (save-excursion
1048         (set-buffer gnus-article-buffer)
1049         (setq signed (memq 'signed gnus-article-wash-types))
1050         (setq encrypted (memq 'encrypted gnus-article-wash-types)))
1051       (cond ((and gnus-message-replysign signed)
1052              (mml-secure-message mml-default-sign-method 'sign))
1053             ((and gnus-message-replyencrypt encrypted)
1054              (mml-secure-message mml-default-encrypt-method
1055                                  (if gnus-message-replysignencrypted
1056                                      'signencrypt
1057                                    'encrypt)))))))
1058
1059 (defun gnus-summary-reply-with-original (n &optional wide)
1060   "Start composing a reply mail to the current message.
1061 The original article will be yanked."
1062   (interactive "P")
1063   (gnus-summary-reply (gnus-summary-work-articles n) wide))
1064
1065 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
1066   "Like `gnus-summary-reply' except removing reply-to field.
1067 If prefix argument YANK is non-nil, the original article is yanked
1068 automatically.
1069 If WIDE, make a wide reply.
1070 If VERY-WIDE, make a very wide reply."
1071   (interactive
1072    (list (and current-prefix-arg
1073               (gnus-summary-work-articles 1))))
1074   (let ((gnus-msg-force-broken-reply-to t))
1075     (gnus-summary-reply yank wide very-wide)))
1076
1077 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
1078   "Like `gnus-summary-reply-with-original' except removing reply-to field.
1079 The original article will be yanked."
1080   (interactive "P")
1081   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
1082
1083 (defun gnus-summary-wide-reply (&optional yank)
1084   "Start composing a wide reply mail to the current message.
1085 If prefix argument YANK is non-nil, the original article is yanked
1086 automatically."
1087   (interactive
1088    (list (and current-prefix-arg
1089               (gnus-summary-work-articles 1))))
1090   (gnus-summary-reply yank t))
1091
1092 (defun gnus-summary-wide-reply-with-original (n)
1093   "Start composing a wide reply mail to the current message.
1094 The original article will be yanked."
1095   (interactive "P")
1096   (gnus-summary-reply-with-original n t))
1097
1098 (defun gnus-summary-very-wide-reply (&optional yank)
1099   "Start composing a very wide reply mail to the current message.
1100 If prefix argument YANK is non-nil, the original article is yanked
1101 automatically."
1102   (interactive
1103    (list (and current-prefix-arg
1104               (gnus-summary-work-articles 1))))
1105   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
1106
1107 (defun gnus-summary-very-wide-reply-with-original (n)
1108   "Start composing a very wide reply mail to the current message.
1109 The original article will be yanked."
1110   (interactive "P")
1111   (gnus-summary-reply
1112    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1113
1114 (defun gnus-summary-mail-forward (&optional arg post)
1115   "Forward the current message(s) to another user.
1116 If process marks exist, forward all marked messages;
1117 if ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
1118 if ARG is 1, decode the message and forward directly inline;
1119 if ARG is 2, forward message as an rfc822 MIME section;
1120 if ARG is 3, decode message and forward as an rfc822 MIME section;
1121 if ARG is 4, forward message directly inline;
1122 otherwise, use flipped `message-forward-as-mime'.
1123 If POST, post instead of mail.
1124 For the `inline' alternatives, also see the variable
1125 `message-forward-ignored-headers'."
1126   (interactive "P")
1127   (if (null (cdr (gnus-summary-work-articles nil)))
1128       (let ((message-forward-as-mime message-forward-as-mime)
1129             (message-forward-show-mml message-forward-show-mml))
1130         (cond
1131          ((null arg))
1132          ((eq arg 1)
1133           (setq message-forward-as-mime nil
1134                 message-forward-show-mml t))
1135          ((eq arg 2)
1136           (setq message-forward-as-mime t
1137                 message-forward-show-mml nil))
1138          ((eq arg 3)
1139           (setq message-forward-as-mime t
1140                 message-forward-show-mml t))
1141          ((eq arg 4)
1142           (setq message-forward-as-mime nil
1143                 message-forward-show-mml nil))
1144          (t
1145           (setq message-forward-as-mime (not message-forward-as-mime))))
1146         (let ((gnus-article-reply (gnus-summary-article-number)))
1147           (gnus-setup-message 'forward
1148             (gnus-summary-select-article)
1149             (let ((mail-parse-charset
1150                    (or (and (gnus-buffer-live-p gnus-article-buffer)
1151                             (with-current-buffer gnus-article-buffer
1152                               gnus-article-charset))
1153                        gnus-newsgroup-charset))
1154                   (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
1155               (set-buffer gnus-original-article-buffer)
1156               (message-forward post)))))
1157     (gnus-uu-digest-mail-forward arg post)))
1158
1159 (defun gnus-summary-resend-message (address n)
1160   "Resend the current article to ADDRESS."
1161   (interactive
1162    (list (message-read-from-minibuffer
1163           "Resend message(s) to: "
1164           (when (gnus-buffer-live-p gnus-original-article-buffer)
1165             ;; If some other article is currently selected, the
1166             ;; initial-contents is wrong. Whatever, it is just the
1167             ;; initial-contents.
1168             (with-current-buffer gnus-original-article-buffer
1169               (nnmail-fetch-field "to"))))
1170          current-prefix-arg))
1171   (let ((articles (gnus-summary-work-articles n))
1172         article)
1173     (while (setq article (pop articles))
1174       (gnus-summary-select-article nil nil nil article)
1175       (save-excursion
1176         (set-buffer gnus-original-article-buffer)
1177         (message-resend address))
1178       (gnus-summary-mark-article-as-forwarded article))))
1179
1180 ;; From: Matthieu Moy <Matthieu.Moy@imag.fr>
1181 (defun gnus-summary-resend-message-edit ()
1182   "Resend an article that has already been sent.
1183 A new buffer will be created to allow the user to modify body and
1184 contents of the message, and then, everything will happen as when
1185 composing a new message."
1186   (interactive)
1187   (let ((article (gnus-summary-article-number)))
1188     (gnus-setup-message 'reply-yank
1189       (gnus-summary-select-article t)
1190       (set-buffer gnus-original-article-buffer)
1191       (let ((cur (current-buffer))
1192             (to (message-fetch-field "to")))
1193         ;; Get a normal message buffer.
1194         (message-pop-to-buffer (message-buffer-name "Resend" to))
1195         (insert-buffer-substring cur)
1196         (mime-to-mml)
1197         (message-narrow-to-head-1)
1198         ;; Gnus will generate a new one when sending.
1199         (message-remove-header "Message-ID")
1200         (message-remove-header message-ignored-resent-headers t)
1201         ;; Remove unwanted headers.
1202         (goto-char (point-max))
1203         (insert mail-header-separator)
1204         (goto-char (point-min))
1205         (re-search-forward "^To:\\|^Newsgroups:" nil 'move)
1206         (forward-char 1)
1207         (widen)))))
1208
1209 (defun gnus-summary-post-forward (&optional arg)
1210   "Forward the current article to a newsgroup.
1211 See `gnus-summary-mail-forward' for ARG."
1212   (interactive "P")
1213   (gnus-summary-mail-forward arg t))
1214
1215 (defvar gnus-nastygram-message
1216   "The following article was inappropriately posted to %s.\n\n"
1217   "Format string to insert in nastygrams.
1218 The current group name will be inserted at \"%s\".")
1219
1220 (defun gnus-summary-mail-nastygram (n)
1221   "Send a nastygram to the author of the current article."
1222   (interactive "P")
1223   (when (or gnus-expert-user
1224             (gnus-y-or-n-p
1225              "Really send a nastygram to the author of the current article? "))
1226     (let ((group gnus-newsgroup-name))
1227       (gnus-summary-reply-with-original n)
1228       (set-buffer gnus-message-buffer)
1229       (message-goto-body)
1230       (insert (format gnus-nastygram-message group))
1231       (message-send-and-exit))))
1232
1233 (defun gnus-summary-mail-crosspost-complaint (n)
1234   "Send a complaint about crossposting to the current article(s)."
1235   (interactive "P")
1236   (let ((articles (gnus-summary-work-articles n))
1237         article)
1238     (while (setq article (pop articles))
1239       (set-buffer gnus-summary-buffer)
1240       (gnus-summary-goto-subject article)
1241       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1242             newsgroups followup-to)
1243         (gnus-summary-select-article)
1244         (set-buffer gnus-original-article-buffer)
1245         (if (and (<= (length (message-tokenize-header
1246                               (setq newsgroups
1247                                     (mail-fetch-field "newsgroups"))
1248                               ", "))
1249                      1)
1250                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1251                      (not (member group (message-tokenize-header
1252                                          followup-to ", ")))))
1253             (if followup-to
1254                 (gnus-message 1 "Followup-to restricted")
1255               (gnus-message 1 "Not a crossposted article"))
1256           (set-buffer gnus-summary-buffer)
1257           (gnus-summary-reply-with-original 1)
1258           (set-buffer gnus-message-buffer)
1259           (message-goto-body)
1260           (insert (format gnus-crosspost-complaint newsgroups group))
1261           (message-goto-subject)
1262           (re-search-forward " *$")
1263           (replace-match " (crosspost notification)" t t)
1264           (gnus-deactivate-mark)
1265           (when (gnus-y-or-n-p "Send this complaint? ")
1266             (message-send-and-exit)))))))
1267
1268 (defun gnus-mail-parse-comma-list ()
1269   (let (accumulated
1270         beg)
1271     (skip-chars-forward " ")
1272     (while (not (eobp))
1273       (setq beg (point))
1274       (skip-chars-forward "^,")
1275       (while (zerop
1276               (save-excursion
1277                 (save-restriction
1278                   (let ((i 0))
1279                     (narrow-to-region beg (point))
1280                     (goto-char beg)
1281                     (logand (progn
1282                               (while (search-forward "\"" nil t)
1283                                 (incf i))
1284                               (if (zerop i) 2 i))
1285                             2)))))
1286         (skip-chars-forward ",")
1287         (skip-chars-forward "^,"))
1288       (skip-chars-backward " ")
1289       (push (buffer-substring beg (point))
1290             accumulated)
1291       (skip-chars-forward "^,")
1292       (skip-chars-forward ", "))
1293     accumulated))
1294
1295 (defun gnus-inews-add-to-address (group)
1296   (let ((to-address (mail-fetch-field "to")))
1297     (when (and to-address
1298                (gnus-alive-p))
1299       ;; This mail group doesn't have a `to-list', so we add one
1300       ;; here.  Magic!
1301       (when (gnus-y-or-n-p
1302              (format "Do you want to add this as `to-list': %s " to-address))
1303         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1304
1305 (defun gnus-put-message ()
1306   "Put the current message in some group and return to Gnus."
1307   (interactive)
1308   (let ((reply gnus-article-reply)
1309         (winconf gnus-prev-winconf)
1310         (group gnus-newsgroup-name))
1311     (unless (and group
1312                  (not (gnus-group-read-only-p group)))
1313       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1314
1315     (when (gnus-gethash group gnus-newsrc-hashtb)
1316       (error "No such group: %s" group))
1317     (save-excursion
1318       (save-restriction
1319         (widen)
1320         (message-narrow-to-headers)
1321         (let ((gnus-deletable-headers nil))
1322           (message-generate-headers
1323            (if (message-news-p)
1324                message-required-news-headers
1325              message-required-mail-headers)))
1326         (goto-char (point-max))
1327         (insert "Gcc: " group "\n")
1328         (widen)))
1329     (gnus-inews-do-gcc)
1330     (when (and (get-buffer gnus-group-buffer)
1331                (gnus-buffer-exists-p (car-safe reply))
1332                (cdr reply))
1333       (set-buffer (car reply))
1334       (gnus-summary-mark-article-as-replied (cdr reply)))
1335     (when winconf
1336       (set-window-configuration winconf))))
1337
1338 (defun gnus-article-mail (yank)
1339   "Send a reply to the address near point.
1340 If YANK is non-nil, include the original article."
1341   (interactive "P")
1342   (let ((address
1343          (buffer-substring
1344           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1345           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1346     (when address
1347       (gnus-msg-mail address)
1348       (when yank
1349         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1350
1351 (defvar nntp-server-type)
1352 (defun gnus-bug ()
1353   "Send a bug report to the Gnus maintainers."
1354   (interactive)
1355   (unless (gnus-alive-p)
1356     (error "Gnus has been shut down"))
1357   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1358     (unless (message-mail-user-agent)
1359       (delete-other-windows)
1360       (when gnus-bug-create-help-buffer
1361         (switch-to-buffer "*Gnus Help Bug*")
1362         (erase-buffer)
1363         (insert gnus-bug-message)
1364         (goto-char (point-min)))
1365       (message-pop-to-buffer "*Gnus Bug*"))
1366     (let ((message-this-is-mail t))
1367       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1368     (when gnus-bug-create-help-buffer
1369       (push `(gnus-bug-kill-buffer) message-send-actions))
1370     (goto-char (point-min))
1371     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1372     (forward-line 1)
1373     (insert (gnus-version) "\n"
1374             (emacs-version) "\n")
1375     (when (and (boundp 'nntp-server-type)
1376                (stringp nntp-server-type))
1377       (insert nntp-server-type))
1378     (insert "\n\n\n\n\n")
1379     (let (text)
1380       (save-excursion
1381         (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
1382         (erase-buffer)
1383         (gnus-debug)
1384         (setq text (buffer-string)))
1385       (insert "<#part type=application/emacs-lisp disposition=inline description=\"User settings\">\n" text "\n<#/part>"))
1386     (goto-char (point-min))
1387     (search-forward "Subject: " nil t)
1388     (message "")))
1389
1390 (defun gnus-bug-kill-buffer ()
1391   (when (get-buffer "*Gnus Help Bug*")
1392     (kill-buffer "*Gnus Help Bug*")))
1393
1394 (defun gnus-summary-yank-message (buffer n)
1395   "Yank the current article into a composed message."
1396   (interactive
1397    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1398          current-prefix-arg))
1399   (gnus-summary-iterate n
1400     (let ((gnus-display-mime-function nil)
1401           (gnus-inhibit-treatment t))
1402       (gnus-summary-select-article))
1403     (save-excursion
1404       (set-buffer buffer)
1405       (message-yank-buffer gnus-article-buffer))))
1406
1407 (defun gnus-debug ()
1408   "Attempts to go through the Gnus source file and report what variables have been changed.
1409 The source file has to be in the Emacs load path."
1410   (interactive)
1411   (let ((files gnus-debug-files)
1412         (point (point))
1413         file expr olist sym)
1414     (gnus-message 4 "Please wait while we snoop your variables...")
1415     (sit-for 0)
1416     ;; Go through all the files looking for non-default values for variables.
1417     (save-excursion
1418       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1419       (while files
1420         (erase-buffer)
1421         (when (and (setq file (locate-library (pop files)))
1422                    (file-exists-p file))
1423           (insert-file-contents file)
1424           (goto-char (point-min))
1425           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1426               (gnus-message 4 "Malformed sources in file %s" file)
1427             (narrow-to-region (point-min) (point))
1428             (goto-char (point-min))
1429             (while (setq expr (ignore-errors (read (current-buffer))))
1430               (ignore-errors
1431                 (and (or (eq (car expr) 'defvar)
1432                          (eq (car expr) 'defcustom))
1433                      (stringp (nth 3 expr))
1434                      (not (memq (nth 1 expr) gnus-debug-exclude-variables))
1435                      (or (not (boundp (nth 1 expr)))
1436                          (not (equal (eval (nth 2 expr))
1437                                      (symbol-value (nth 1 expr)))))
1438                      (push (nth 1 expr) olist)))))))
1439       (kill-buffer (current-buffer)))
1440     (when (setq olist (nreverse olist))
1441       (insert "------------------ Environment follows ------------------\n\n"))
1442     (while olist
1443       (if (boundp (car olist))
1444           (condition-case ()
1445               (pp `(setq ,(car olist)
1446                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1447                                   (and (symbolp sym)
1448                                        (not (or (eq sym nil)
1449                                                 (eq sym t)))))
1450                               (list 'quote (symbol-value (car olist)))
1451                             (symbol-value (car olist))))
1452                   (current-buffer))
1453             (error
1454              (format "(setq %s 'whatever)\n" (car olist))))
1455         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1456       (setq olist (cdr olist)))
1457     (insert "\n\n")
1458     ;; Remove any control chars - they seem to cause trouble for some
1459     ;; mailers.  (Byte-compiled output from the stuff above.)
1460     (goto-char point)
1461     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1462       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1463                      t t))))
1464
1465 ;;; Treatment of rejected articles.
1466 ;;; Bounced mail.
1467
1468 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1469   "Re-mail the current message.
1470 This only makes sense if the current message is a bounce message than
1471 contains some mail you have written which has been bounced back to
1472 you.
1473 If FETCH, try to fetch the article that this is a reply to, if indeed
1474 this is a reply."
1475   (interactive "P")
1476   (gnus-summary-select-article t)
1477   (set-buffer gnus-original-article-buffer)
1478   (gnus-setup-message 'compose-bounce
1479     (let* ((references (mail-fetch-field "references"))
1480            (parent (and references (gnus-parent-id references))))
1481       (message-bounce)
1482       ;; If there are references, we fetch the article we answered to.
1483       (and fetch parent
1484            (gnus-summary-refer-article parent)
1485            (gnus-summary-show-all-headers)))))
1486
1487 ;;; Gcc handling.
1488
1489 (defun gnus-inews-group-method (group)
1490   (cond
1491    ;; If the group doesn't exist, we assume
1492    ;; it's an archive group...
1493    ((and (null (gnus-get-info group))
1494          (eq (car (gnus-server-to-method gnus-message-archive-method))
1495              (car (gnus-server-to-method (gnus-group-method group)))))
1496     gnus-message-archive-method)
1497    ;; Use the method.
1498    ((gnus-info-method (gnus-get-info group))
1499     (gnus-info-method (gnus-get-info group)))
1500    ;; Find the method.
1501    (t (gnus-server-to-method (gnus-group-method group)))))
1502
1503 ;; Do Gcc handling, which copied the message over to some group.
1504 (defun gnus-inews-do-gcc (&optional gcc)
1505   (interactive)
1506   (save-excursion
1507     (save-restriction
1508       (message-narrow-to-headers)
1509       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1510             (cur (current-buffer))
1511             groups group method group-art
1512             mml-externalize-attachments)
1513         (when gcc
1514           (message-remove-header "gcc")
1515           (widen)
1516           (setq groups (message-unquote-tokens
1517                         (message-tokenize-header gcc " ,")))
1518           ;; Copy the article over to some group(s).
1519           (while (setq group (pop groups))
1520             (unless (gnus-check-server
1521                      (setq method (gnus-inews-group-method group)))
1522               (error "Can't open server %s" (if (stringp method) method
1523                                               (car method))))
1524             (unless (gnus-request-group group nil method)
1525               (gnus-request-create-group group method))
1526             (setq mml-externalize-attachments
1527                   (if (stringp gnus-gcc-externalize-attachments)
1528                       (string-match gnus-gcc-externalize-attachments group)
1529                     gnus-gcc-externalize-attachments))
1530             (save-excursion
1531               (nnheader-set-temp-buffer " *acc*")
1532               (insert-buffer-substring cur)
1533               (message-encode-message-body)
1534               (save-restriction
1535                 (message-narrow-to-headers)
1536                 (let* ((mail-parse-charset message-default-charset)
1537                        (newsgroups-field (save-restriction
1538                                            (message-narrow-to-headers-or-head)
1539                                            (message-fetch-field "Newsgroups")))
1540                        (followup-field (save-restriction
1541                                          (message-narrow-to-headers-or-head)
1542                                          (message-fetch-field "Followup-To")))
1543                        ;; BUG: We really need to get the charset for
1544                        ;; each name in the Newsgroups and Followup-To
1545                        ;; lines to allow crossposting between group
1546                        ;; namess with incompatible character sets.
1547                        ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
1548                        (group-field-charset
1549                         (gnus-group-name-charset
1550                          method (or newsgroups-field "")))
1551                        (followup-field-charset
1552                         (gnus-group-name-charset
1553                          method (or followup-field "")))
1554                        (rfc2047-header-encoding-alist
1555                         (append
1556                          (when group-field-charset
1557                            (list (cons "Newsgroups" group-field-charset)))
1558                          (when followup-field-charset
1559                            (list (cons "Followup-To" followup-field-charset)))
1560                          rfc2047-header-encoding-alist)))
1561                   (mail-encode-encoded-word-buffer)))
1562               (goto-char (point-min))
1563               (when (re-search-forward
1564                      (concat "^" (regexp-quote mail-header-separator) "$")
1565                      nil t)
1566                 (replace-match "" t t ))
1567               (unless (setq group-art
1568                             (gnus-request-accept-article group method t t))
1569                 (gnus-message 1 "Couldn't store article in group %s: %s"
1570                               group (gnus-status-message method))
1571                 (sit-for 2))
1572               (when (and group-art
1573                          (or gnus-gcc-mark-as-read
1574                              gnus-inews-mark-gcc-as-read))
1575                 (gnus-group-mark-article-read group (cdr group-art)))
1576               (kill-buffer (current-buffer)))))))))
1577
1578 (defun gnus-inews-insert-gcc ()
1579   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1580   (save-excursion
1581     (save-restriction
1582       (message-narrow-to-headers)
1583       (let* ((group gnus-outgoing-message-group)
1584              (gcc (cond
1585                    ((gnus-functionp group)
1586                     (funcall group))
1587                    ((or (stringp group) (list group))
1588                     group))))
1589         (when gcc
1590           (insert "Gcc: "
1591                   (if (stringp gcc) gcc
1592                     (mapconcat 'identity gcc " "))
1593                   "\n"))))))
1594
1595 (defun gnus-inews-insert-archive-gcc (&optional group)
1596   "Insert the Gcc to say where the article is to be archived."
1597   (let* ((var gnus-message-archive-group)
1598          (group (or group gnus-newsgroup-name ""))
1599          (gcc-self-val
1600           (and gnus-newsgroup-name
1601                (not (equal gnus-newsgroup-name ""))
1602                (gnus-group-find-parameter
1603                 gnus-newsgroup-name 'gcc-self)))
1604          result
1605          (groups
1606           (cond
1607            ((null gnus-message-archive-method)
1608             ;; Ignore.
1609             nil)
1610            ((stringp var)
1611             ;; Just a single group.
1612             (list var))
1613            ((null var)
1614             ;; We don't want this.
1615             nil)
1616            ((and (listp var) (stringp (car var)))
1617             ;; A list of groups.
1618             var)
1619            ((gnus-functionp var)
1620             ;; A function.
1621             (funcall var group))
1622            (t
1623             ;; An alist of regexps/functions/forms.
1624             (while (and var
1625                         (not
1626                          (setq result
1627                                (cond
1628                                 ((stringp (caar var))
1629                                  ;; Regexp.
1630                                  (when (string-match (caar var) group)
1631                                    (cdar var)))
1632                                 ((gnus-functionp (car var))
1633                                  ;; Function.
1634                                  (funcall (car var) group))
1635                                 (t
1636                                  (eval (car var)))))))
1637               (setq var (cdr var)))
1638             result)))
1639          name)
1640     (when (or groups gcc-self-val)
1641       (when (stringp groups)
1642         (setq groups (list groups)))
1643       (save-excursion
1644         (save-restriction
1645           (message-narrow-to-headers)
1646           (goto-char (point-max))
1647           (insert "Gcc: ")
1648           (if gcc-self-val
1649               ;; Use the `gcc-self' param value instead.
1650               (progn
1651                 (insert
1652                  (if (stringp gcc-self-val)
1653                      gcc-self-val
1654                    group))
1655                 (if (not (eq gcc-self-val 'none))
1656                     (insert "\n")
1657                   (progn
1658                     (beginning-of-line)
1659                     (kill-line))))
1660             ;; Use the list of groups.
1661             (while (setq name (pop groups))
1662               (insert (if (string-match ":" name)
1663                           name
1664                         (gnus-group-prefixed-name
1665                          name gnus-message-archive-method)))
1666               (when groups
1667                 (insert " ")))
1668             (insert "\n")))))))
1669
1670 ;;; Posting styles.
1671
1672 (defun gnus-configure-posting-styles (&optional group-name)
1673   "Configure posting styles according to `gnus-posting-styles'."
1674   (unless gnus-inhibit-posting-styles
1675     (let ((group (or group-name gnus-newsgroup-name ""))
1676           (styles gnus-posting-styles)
1677           style match variable attribute value v results
1678           filep name address element)
1679       ;; If the group has a posting-style parameter, add it at the end with a
1680       ;; regexp matching everything, to be sure it takes precedence over all
1681       ;; the others.
1682       (when gnus-newsgroup-name
1683         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1684           (when tmp-style
1685             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1686       ;; Go through all styles and look for matches.
1687       (dolist (style styles)
1688         (setq match (pop style))
1689         (goto-char (point-min))
1690         (when (cond
1691                ((stringp match)
1692                 ;; Regexp string match on the group name.
1693                 (string-match match group))
1694                ((eq match 'header)
1695                 ;; Obsolete format of header match.
1696                 (and (gnus-buffer-live-p gnus-article-copy)
1697                      (with-current-buffer gnus-article-copy
1698                        (let ((header (message-fetch-field (pop style))))
1699                          (and header
1700                               (string-match (pop style) header))))))
1701                ((or (symbolp match)
1702                     (gnus-functionp match))
1703                 (cond
1704                  ((gnus-functionp match)
1705                   ;; Function to be called.
1706                   (funcall match))
1707                  ((boundp match)
1708                   ;; Variable to be checked.
1709                   (symbol-value match))))
1710                ((listp match)
1711                 (cond
1712                  ((eq (car match) 'header)
1713                   ;; New format of header match.
1714                   (and (gnus-buffer-live-p gnus-article-copy)
1715                        (with-current-buffer gnus-article-copy
1716                          (let ((header (message-fetch-field (nth 1 match))))
1717                            (and header
1718                                 (string-match (nth 2 match) header))))))
1719                  (t
1720                   ;; This is a form to be evaled.
1721                   (eval match)))))
1722           ;; We have a match, so we set the variables.
1723           (dolist (attribute style)
1724             (setq element (pop attribute)
1725                   variable nil
1726                   filep nil)
1727             (setq value
1728                   (cond
1729                    ((eq (car attribute) :file)
1730                     (setq filep t)
1731                     (cadr attribute))
1732                    ((eq (car attribute) :value)
1733                     (cadr attribute))
1734                    (t
1735                     (car attribute))))
1736             ;; We get the value.
1737             (setq v
1738                   (cond
1739                    ((stringp value)
1740                     value)
1741                    ((or (symbolp value)
1742                         (gnus-functionp value))
1743                     (cond ((gnus-functionp value)
1744                            (funcall value))
1745                           ((boundp value)
1746                            (symbol-value value))))
1747                    ((listp value)
1748                     (eval value))))
1749             ;; Translate obsolescent value.
1750             (cond
1751              ((eq element 'signature-file)
1752               (setq element 'signature
1753                     filep t))
1754              ((eq element 'x-face-file)
1755               (setq element 'x-face
1756                     filep t)))
1757             ;; Get the contents of file elems.
1758             (when (and filep v)
1759               (setq v (with-temp-buffer
1760                         (insert-file-contents v)
1761                         (goto-char (point-max))
1762                         (while (bolp)
1763                           (delete-char -1))
1764                         (buffer-string))))
1765             (setq results (delq (assoc element results) results))
1766             (push (cons element v) results))))
1767       ;; Now we have all the styles, so we insert them.
1768       (setq name (assq 'name results)
1769             address (assq 'address results))
1770       (setq results (delq name (delq address results)))
1771       ;; make-local-hook is not obsolete in Emacs 20 or XEmacs.
1772       (make-local-hook 'message-setup-hook)
1773       (dolist (result results)
1774         (add-hook 'message-setup-hook
1775                   (cond
1776                    ((eq 'eval (car result))
1777                     'ignore)
1778                    ((eq 'body (car result))
1779                     `(lambda ()
1780                        (save-excursion
1781                          (message-goto-body)
1782                          (insert ,(cdr result)))))
1783                    ((eq 'signature (car result))
1784                     (set (make-local-variable 'message-signature) nil)
1785                     (set (make-local-variable 'message-signature-file) nil)
1786                     (if (not (cdr result))
1787                         'ignore
1788                       `(lambda ()
1789                          (save-excursion
1790                            (let ((message-signature ,(cdr result)))
1791                              (when message-signature
1792                                (message-insert-signature)))))))
1793                    (t
1794                     (let ((header
1795                            (if (symbolp (car result))
1796                                (capitalize (symbol-name (car result)))
1797                              (car result))))
1798                       `(lambda ()
1799                          (save-excursion
1800                            (message-remove-header ,header)
1801                            (let ((value ,(cdr result)))
1802                              (when value
1803                                (message-goto-eoh)
1804                                (insert ,header ": " value "\n"))))))))
1805                   nil 'local))
1806       (when (or name address)
1807         (add-hook 'message-setup-hook
1808                   `(lambda ()
1809                      (set (make-local-variable 'user-mail-address)
1810                           ,(or (cdr address) user-mail-address))
1811                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1812                            (user-mail-address
1813                             ,(or (cdr address) user-mail-address)))
1814                        (save-excursion
1815                          (message-remove-header "From")
1816                          (message-goto-eoh)
1817                          (insert "From: " (message-make-from) "\n"))))
1818                   nil 'local)))))
1819
1820 ;;; Allow redefinition of functions.
1821
1822 (gnus-ems-redefine)
1823
1824 (provide 'gnus-msg)
1825
1826 ;;; gnus-msg.el ends here