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