Synch to No Gnus 200402270146.
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;      Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;;      Katsumi Yamaoka  <yamaoka@jpl.org>
10 ;;      Kiyokazu SUTO    <suto@merry.xmath.ous.ac.jp>
11 ;; Keywords: mail, news, MIME
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'static))
36
37 (require 'gnus)
38 (require 'gnus-ems)
39 (require 'message)
40 (require 'gnus-art)
41 (require 'gnus-util)
42
43 (defcustom gnus-post-method 'current
44   "*Preferred method for posting USENET news.
45
46 If this variable is `current' (which is the default), Gnus will use
47 the \"current\" select method when posting.  If it is `native', Gnus
48 will use the native select method when posting.
49
50 This method will not be used in mail groups and the like, only in
51 \"real\" newsgroups.
52
53 If not `native' nor `current', the value must be a valid method as discussed
54 in the documentation of `gnus-select-method'.  It can also be a list of
55 methods.  If that is the case, the user will be queried for what select
56 method to use when posting."
57   :group 'gnus-group-foreign
58   :link '(custom-manual "(gnus)Posting Server")
59   :type `(choice (const native)
60                  (const current)
61                  (sexp :tag "Methods" ,gnus-select-method)))
62
63 (defcustom gnus-outgoing-message-group nil
64   "*All outgoing messages will be put in this group.
65 If you want to store all your outgoing mail and articles in the group
66 \"nnml:archive\", you set this variable to that value.  This variable
67 can also be a list of group names.
68
69 If you want to have greater control over what group to put each
70 message in, you can set this variable to a function that checks the
71 current newsgroup name and then returns a suitable group name (or list
72 of names)."
73   :group 'gnus-message
74   :type '(choice (string :tag "Group")
75                  (function)))
76
77 (defcustom gnus-mailing-list-groups nil
78   "*If non-nil a regexp matching groups that are really mailing lists.
79 This is useful when you're reading a mailing list that has been
80 gatewayed to a newsgroup, and you want to followup to an article in
81 the group."
82   :group 'gnus-message
83   :type '(choice (regexp)
84                  (const nil)))
85
86 (defcustom gnus-add-to-list nil
87   "*If non-nil, add a `to-list' parameter automatically."
88   :group 'gnus-message
89   :type 'boolean)
90
91 (defcustom gnus-crosspost-complaint
92   "Hi,
93
94 You posted the article below with the following Newsgroups header:
95
96 Newsgroups: %s
97
98 The %s group, at least, was an inappropriate recipient
99 of this message.  Please trim your Newsgroups header to exclude this
100 group before posting in the future.
101
102 Thank you.
103
104 "
105   "Format string to be inserted when complaining about crossposts.
106 The first %s will be replaced by the Newsgroups header;
107 the second with the current group name."
108   :group 'gnus-message
109   :type 'string)
110
111 (defcustom gnus-message-setup-hook nil
112   "Hook run after setting up a message buffer."
113   :group 'gnus-message
114   :type 'hook)
115
116 (defcustom gnus-bug-create-help-buffer t
117   "*Should we create the *Gnus Help Bug* buffer?"
118   :group 'gnus-message
119   :type 'boolean)
120
121 (defcustom gnus-posting-styles nil
122   "*Alist of styles to use when posting.
123 See Info node `(gnus)Posting Styles'."
124   :group 'gnus-message
125   :link '(custom-manual "(gnus)Posting Styles")
126   :type '(repeat (cons (choice (regexp)
127                                (variable)
128                                (list (const header)
129                                      (string :tag "Header")
130                                      (regexp :tag "Regexp"))
131                                (function)
132                                (sexp))
133                        (repeat (list
134                                 (choice (const signature)
135                                         (const signature-file)
136                                         (const organization)
137                                         (const address)
138                                         (const x-face-file)
139                                         (const name)
140                                         (const body)
141                                         (const import)
142                                         (symbol)
143                                         (string :tag "Header"))
144                                 (choice (string)
145                                         (function)
146                                         (variable)
147                                         (sexp)))))))
148
149 (defcustom gnus-named-posting-styles nil
150   "Alist mapping names to the user-defined posting styles."
151   :group 'gnus-message
152   :type '(repeat (cons string
153                        (repeat (list
154                                 (choice (const signature)
155                                         (const signature-file)
156                                         (const organization)
157                                         (const address)
158                                         (const x-face-file)
159                                         (const name)
160                                         (const body)
161                                         (const import)
162                                         (symbol)
163                                         (string :tag "Header"))
164                                 (choice (string)
165                                         (function)
166                                         (variable)
167                                         (sexp)))))))
168
169 (defcustom gnus-gcc-mark-as-read nil
170   "If non-nil, automatically mark Gcc articles as read."
171   :version "21.1"
172   :group 'gnus-message
173   :type 'boolean)
174
175 (make-obsolete-variable 'gnus-inews-mark-gcc-as-read
176                         'gnus-gcc-mark-as-read)
177
178 (defcustom gnus-gcc-externalize-attachments nil
179   "Should local-file attachments be included as external parts in Gcc copies?
180 If it is `all', attach files as external parts;
181 if a regexp and matches the Gcc group name, attach files as external parts;
182 if nil, attach files as normal parts."
183   :version "21.1"
184   :group 'gnus-message
185   :type '(choice (const nil :tag "None")
186                  (const all :tag "Any")
187                  (string :tag "Regexp")))
188
189 (gnus-define-group-parameter
190  posting-charset-alist
191  :type list
192  :function-document
193  "Return the permitted unencoded charsets for posting of GROUP."
194  :variable gnus-group-posting-charset-alist
195  :variable-default
196  '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
197    ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
198    (message-this-is-mail nil nil)
199    (message-this-is-news nil t))
200  :variable-document
201  "Alist of regexps and permitted unencoded charsets for posting.
202 Each element of the alist has the form (TEST HEADER BODY-LIST), where
203 TEST is either a regular expression matching the newsgroup header or a
204 variable to query,
205 HEADER is the charset which may be left unencoded in the header (nil
206 means encode all charsets),
207 BODY-LIST is a list of charsets which may be encoded using 8bit
208 content-transfer encoding in the body, or one of the special values
209 nil (always encode using quoted-printable) or t (always use 8bit).
210
211 Note that any value other than nil for HEADER infringes some RFCs, so
212 use this option with care."
213  :variable-group gnus-charset
214  :variable-type
215  '(repeat (list :tag "Permitted unencoded charsets"
216                 (choice :tag "Where"
217                         (regexp :tag "Group")
218                         (const :tag "Mail message" :value message-this-is-mail)
219                         (const :tag "News article" :value message-this-is-news))
220                 (choice :tag "Header"
221                         (const :tag "None" nil)
222                         (symbol :tag "Charset"))
223                 (choice :tag "Body"
224                         (const :tag "Any" :value t)
225                         (const :tag "None" :value nil)
226                         (repeat :tag "Charsets"
227                                 (symbol :tag "Charset")))))
228  :parameter-type '(choice :tag "Permitted unencoded charsets"
229                           :value nil
230                           (repeat (symbol)))
231  :parameter-document       "\
232 List of charsets that are permitted to be unencoded.")
233
234 (defcustom gnus-debug-files
235   '("gnus.el" "gnus-sum.el" "gnus-group.el"
236     "gnus-art.el" "gnus-start.el" "gnus-async.el"
237     "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
238     "gnus-agent.el" "gnus-cache.el" "gnus-srvr.el"
239     "mm-util.el" "mm-decode.el" "nnmail.el" "nntp.el" "message.el")
240   "Files whose variables will be reported in `gnus-bug'."
241   :version "21.1"
242   :group 'gnus-message
243   :type '(repeat (string :tag "File")))
244
245 (defcustom gnus-debug-exclude-variables
246   '(mm-mime-mule-charset-alist
247     nnmail-split-fancy message-minibuffer-local-map)
248   "Variables that should not be reported in `gnus-bug'."
249   :version "21.1"
250   :group 'gnus-message
251   :type '(repeat (symbol :tag "Variable")))
252
253 (defcustom gnus-discouraged-post-methods
254   '(nndraft nnml nnimap nnmaildir nnmh nnfolder nndir)
255   "A list of back ends that are not used in \"real\" newsgroups.
256 This variable is used only when `gnus-post-method' is `current'."
257   :version "21.3"
258   :group 'gnus-group-foreign
259   :type '(repeat (symbol :tag "Back end")))
260
261 (defcustom gnus-message-replysign
262   nil
263   "Automatically sign replies to signed messages.
264 See also the `mml-default-sign-method' variable."
265   :group 'gnus-message
266   :type 'boolean)
267
268 (defcustom gnus-message-replyencrypt
269   nil
270   "Automatically encrypt replies to encrypted messages.
271 See also the `mml-default-encrypt-method' variable."
272   :group 'gnus-message
273   :type 'boolean)
274
275 (defcustom gnus-message-replysignencrypted
276   t
277   "Setting this causes automatically encrypted messages to also be signed."
278   :group 'gnus-message
279   :type 'boolean)
280
281 (defcustom gnus-confirm-mail-reply-to-news nil
282   "If non-nil, Gnus requests confirmation when replying to news.
283 This is done because new users often reply by mistake when reading
284 news.
285 This can also be a function receiving the group name as the only
286 parameter which should return non-nil iff a confirmation is needed, or
287 a regexp, in which case a confirmation is asked for iff the group name
288 matches the regexp."
289   :group 'gnus-message
290   :type '(choice (const :tag "No" nil)
291                  (const :tag "Yes" t)
292                  (regexp :tag "Iff group matches regexp")
293                  (function :tag "Iff function evaluates to non-nil")))
294
295 (defcustom gnus-confirm-treat-mail-like-news
296   nil
297   "If non-nil, Gnus will treat mail like news with regard to confirmation
298 when replying by mail.  See the `gnus-confirm-mail-reply-to-news' variable
299 for fine-tuning this.
300 If nil, Gnus will never ask for confirmation if replying to mail."
301   :group 'gnus-message
302   :type 'boolean)
303
304 (defcustom gnus-summary-resend-default-address t
305   "If non-nil, Gnus tries to suggest a default address to resend to.
306 If nil, the address field will always be empty after invoking
307 `gnus-summary-resend-message'."
308   :group 'gnus-message
309   :type 'boolean)
310
311 ;;; Internal variables.
312
313 (defvar gnus-inhibit-posting-styles nil
314   "Inhibit the use of posting styles.")
315
316 (defvar gnus-article-yanked-articles nil)
317 (defvar gnus-message-buffer "*Mail Gnus*")
318 (defvar gnus-article-copy nil)
319 (defvar gnus-check-before-posting nil)
320 (defvar gnus-last-posting-server nil)
321 (defvar gnus-message-group-art nil)
322
323 (defvar gnus-msg-force-broken-reply-to nil)
324
325 (defconst gnus-bug-message
326   (format "Sending a bug report to the Gnus Towers.
327 ========================================
328
329 This gnus is the %s%s.
330 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
331 Developers. (the addresses below are mailing list addresses)
332
333 ========================================
334
335 The buffer below is a mail buffer.  When you press `C-c C-c', it will
336 be sent to the Gnus Bug Exterminators.
337
338 The thing near the bottom of the buffer is how the environment
339 settings will be included in the mail.  Please do not delete that.
340 They will tell the Bug People what your environment is, so that it
341 will be easier to locate the bugs.
342
343 If you have found a bug that makes Emacs go \"beep\", set
344 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
345 and include the backtrace in your bug report.
346
347 Please describe the bug in annoying, painstaking detail.
348
349 Thank you for your help in stamping out bugs.
350 "
351           gnus-product-name
352           (if (string= gnus-product-name "Semi-gnus")
353               ""
354             ", a modified version of Semi-gnus")))
355
356 (eval-and-compile
357   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
358   (autoload 'news-setup "rnewspost")
359   (autoload 'news-reply-mode "rnewspost")
360   (autoload 'rmail-dont-reply-to "mail-utils")
361   (autoload 'rmail-output "rmailout"))
362
363 \f
364 ;;;
365 ;;; Gnus Posting Functions
366 ;;;
367
368 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
369   "p" gnus-summary-post-news
370   "i" gnus-summary-news-other-window
371   "f" gnus-summary-followup
372   "F" gnus-summary-followup-with-original
373   "c" gnus-summary-cancel-article
374   "s" gnus-summary-supersede-article
375   "r" gnus-summary-reply
376   "y" gnus-summary-yank-message
377   "R" gnus-summary-reply-with-original
378   "w" gnus-summary-wide-reply
379   "W" gnus-summary-wide-reply-with-original
380   "v" gnus-summary-very-wide-reply
381   "V" gnus-summary-very-wide-reply-with-original
382   "n" gnus-summary-followup-to-mail
383   "N" gnus-summary-followup-to-mail-with-original
384   "m" gnus-summary-mail-other-window
385   "u" gnus-uu-post-news
386   "\M-c" gnus-summary-mail-crosspost-complaint
387   "Br" gnus-summary-reply-broken-reply-to
388   "BR" gnus-summary-reply-broken-reply-to-with-original
389   "om" gnus-summary-mail-forward
390   "op" gnus-summary-post-forward
391   "Om" gnus-summary-digest-mail-forward
392   "Op" gnus-summary-digest-post-forward
393   "P" gnus-summary-execute-command-with-posting-style)
394
395 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
396   "b" gnus-summary-resend-bounced-mail
397   ;; "c" gnus-summary-send-draft
398   "r" gnus-summary-resend-message
399   "e" gnus-summary-resend-message-edit)
400
401 ;;; Internal functions.
402
403 (defun gnus-inews-make-draft ()
404   `(lambda ()
405      (gnus-inews-make-draft-meta-information
406       ,gnus-newsgroup-name ',gnus-article-reply)))
407
408 (defvar gnus-article-reply nil)
409 (defmacro gnus-setup-message (config &rest forms)
410   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
411         (buffer (make-symbol "gnus-setup-message-buffer"))
412         (article (make-symbol "gnus-setup-message-article"))
413         (yanked (make-symbol "gnus-setup-yanked-articles"))
414         (group (make-symbol "gnus-setup-message-group")))
415     `(let ((,winconf (current-window-configuration))
416            (,buffer (buffer-name (current-buffer)))
417            (,article gnus-article-reply)
418            (,yanked gnus-article-yanked-articles)
419            (,group gnus-newsgroup-name)
420            (message-header-setup-hook
421             (copy-sequence message-header-setup-hook))
422            (message-mode-hook (copy-sequence message-mode-hook))
423            (message-startup-parameter-alist
424             '((reply-buffer . gnus-copy-article-buffer)
425               (original-buffer . gnus-original-article-buffer)
426               (user-agent . Gnus))))
427        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
428        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
429        ;; #### FIXME: for a reason that I did not manage to identify yet,
430        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
431        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
432        ;; After evaluation of @forms below, it gets the value we actually want
433        ;; to override, and the posting styles are used. For that reason, I've
434        ;; added an optional argument to `gnus-configure-posting-styles' to
435        ;; make sure that the correct value for the group name is used. -- drv
436        (add-hook 'message-mode-hook
437                  (lambda ()
438                    (gnus-configure-posting-styles ,group)))
439        (gnus-pull ',(intern gnus-draft-meta-information-header)
440                   message-required-headers)
441        (when (and ,group
442                   (not (string= ,group "")))
443          (push (cons
444                 (intern gnus-draft-meta-information-header)
445                 (gnus-inews-make-draft))
446                message-required-headers))
447        (unwind-protect
448            (progn
449              ,@forms)
450          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config
451                                       ,yanked)
452          (setq gnus-message-buffer (current-buffer))
453          (set (make-local-variable 'gnus-message-group-art)
454               (cons ,group ,article))
455          (set (make-local-variable 'gnus-newsgroup-name) ,group)
456          (gnus-maybe-setup-default-charset)
457          (gnus-run-hooks 'gnus-message-setup-hook))
458        (message-hide-headers)
459        (gnus-add-buffer)
460        (gnus-configure-windows ,config t)
461        (run-hooks 'post-command-hook)
462        (set-buffer-modified-p nil))))
463
464 (defun gnus-inews-make-draft-meta-information (group article)
465   (concat "(\"" group "\" "
466           (if article (number-to-string
467                        (if (listp article)
468                            (car article)
469                          article)) "\"\"")
470           ")"))
471
472 ;;;###autoload
473 (defun gnus-msg-mail (&optional to subject other-headers continue
474                                 switch-action yank-action send-actions)
475   "Start editing a mail message to be sent.
476 Like `message-mail', but with Gnus paraphernalia, particularly the
477 Gcc: header for archiving purposes."
478   (interactive)
479   (let ((buf (current-buffer))
480         mail-buf)
481     (gnus-setup-message 'message
482       (message-mail to subject other-headers continue
483                     nil yank-action send-actions))
484     (when switch-action
485       (setq mail-buf (current-buffer))
486       (switch-to-buffer buf)
487       (apply switch-action mail-buf nil)))
488   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
489   t)
490
491 (defvar save-selected-window-window)
492
493 ;;;###autoload
494 (defun gnus-button-mailto (address)
495   "Mail to ADDRESS."
496   (set-buffer (gnus-copy-article-buffer))
497   (gnus-setup-message 'message
498     (message-reply address))
499   (and (boundp 'save-selected-window-window)
500        (not (window-live-p save-selected-window-window))
501        (setq save-selected-window-window (selected-window))))
502
503 ;;;###autoload
504 (defun gnus-button-reply (&optional to-address wide)
505   "Like `message-reply'."
506   (interactive)
507   (gnus-setup-message 'message
508     (message-reply to-address wide))
509   (and (boundp 'save-selected-window-window)
510        (not (window-live-p save-selected-window-window))
511        (setq save-selected-window-window (selected-window))))
512
513 ;;;###autoload
514 (define-mail-user-agent 'gnus-user-agent
515   'gnus-msg-mail 'message-send-and-exit
516   'message-kill-buffer 'message-send-hook)
517
518 (defun gnus-setup-posting-charset (group)
519   (let ((alist gnus-group-posting-charset-alist)
520         (group (or group ""))
521         elem)
522     (when group
523       (catch 'found
524         (while (setq elem (pop alist))
525           (when (or (and (stringp (car elem))
526                          (string-match (car elem) group))
527                     (and (functionp (car elem))
528                          (funcall (car elem) group))
529                     (and (symbolp (car elem))
530                          (symbol-value (car elem))))
531             (throw 'found (cons (cadr elem) (caddr elem)))))))))
532
533 (defun gnus-inews-add-send-actions (winconf buffer article
534                                             &optional config yanked)
535   (gnus-make-local-hook 'message-sent-hook)
536   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
537                                  'gnus-inews-do-gcc) nil t)
538   (when gnus-agent
539     (gnus-make-local-hook 'message-header-hook)
540     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
541   (setq message-post-method
542         `(lambda (arg)
543            (gnus-post-method arg ,gnus-newsgroup-name)))
544   (setq message-user-agent (gnus-message-make-user-agent))
545   (unless message-use-multi-frames
546     (message-add-action
547      `(if (gnus-buffer-exists-p ,buffer)
548           (set-window-configuration ,winconf))
549      'exit 'postpone 'kill))
550   (let ((to-be-marked (cond
551                        (yanked
552                         (mapcar
553                          (lambda (x) (if (listp x) (car x) x)) yanked))
554                        (article (if (listp article) article (list article)))
555                        (t nil))))
556     (message-add-action
557      `(when (gnus-buffer-exists-p ,buffer)
558         (save-excursion
559           (set-buffer ,buffer)
560           ,(when to-be-marked
561              (if (eq config 'forward)
562                  `(gnus-summary-mark-article-as-forwarded ',to-be-marked)
563                `(gnus-summary-mark-article-as-replied ',to-be-marked)))))
564      'send)))
565
566 (put 'gnus-setup-message 'lisp-indent-function 1)
567 (put 'gnus-setup-message 'edebug-form-spec '(form body))
568
569 ;;; Post news commands of Gnus group mode and summary mode
570
571 (defun gnus-group-mail (&optional arg)
572   "Start composing a mail.
573 If ARG, use the group under the point to find a posting style.
574 If ARG is 1, prompt for a group name to find the posting style."
575   (interactive "P")
576   ;; We can't `let' gnus-newsgroup-name here, since that leads
577   ;; to local variables leaking.
578   (let ((group gnus-newsgroup-name)
579         ;; make sure last viewed article doesn't affect posting styles:
580         (gnus-article-copy)
581         (buffer (current-buffer)))
582     (unwind-protect
583         (progn
584           (setq gnus-newsgroup-name
585                 (if arg
586                     (if (= 1 (prefix-numeric-value arg))
587                         (completing-read "Use posting style of group: "
588                                          gnus-active-hashtb nil
589                                          (gnus-read-active-file-p))
590                       (gnus-group-group-name))
591                   ""))
592           ;; #### see comment in gnus-setup-message -- drv
593           (gnus-setup-message 'message (message-mail)))
594       (save-excursion
595         (set-buffer buffer)
596         (setq gnus-newsgroup-name group)))))
597
598 (defun gnus-group-news (&optional arg)
599   "Start composing a news.
600 If ARG, post to group under point.
601 If ARG is 1, prompt for group name to post to.
602
603 This function prepares a news even when using mail groups.  This is useful
604 for posting messages to mail groups without actually sending them over the
605 network.  The corresponding back end must have a 'request-post method."
606   (interactive "P")
607   ;; We can't `let' gnus-newsgroup-name here, since that leads
608   ;; to local variables leaking.
609   (let ((group gnus-newsgroup-name)
610         ;; make sure last viewed article doesn't affect posting styles:
611         (gnus-article-copy)
612         (buffer (current-buffer)))
613     (unwind-protect
614         (progn
615           (setq gnus-newsgroup-name
616                 (if arg
617                     (if (= 1 (prefix-numeric-value arg))
618                         (completing-read "Use group: "
619                                          gnus-active-hashtb nil
620                                          (gnus-read-active-file-p))
621                       (gnus-group-group-name))
622                   ""))
623           ;; #### see comment in gnus-setup-message -- drv
624           (gnus-setup-message 'message
625             (message-news (gnus-group-real-name gnus-newsgroup-name))))
626       (save-excursion
627         (set-buffer buffer)
628         (setq gnus-newsgroup-name group)))))
629
630 (defun gnus-group-post-news (&optional arg)
631   "Start composing a message (a news by default).
632 If ARG, post to group under point.  If ARG is 1, prompt for group name.
633 Depending on the selected group, the message might be either a mail or
634 a news."
635   (interactive "P")
636   ;; Bind this variable here to make message mode hooks work ok.
637   (let ((gnus-newsgroup-name
638          (if arg
639              (if (= 1 (prefix-numeric-value arg))
640                  (completing-read "Newsgroup: " gnus-active-hashtb nil
641                                   (gnus-read-active-file-p))
642                (gnus-group-group-name))
643            ""))
644         ;; make sure last viewed article doesn't affect posting styles:
645         (gnus-article-copy))
646     (gnus-post-news 'post gnus-newsgroup-name nil nil nil nil
647                     (string= gnus-newsgroup-name ""))))
648
649 (defun gnus-summary-mail-other-window (&optional arg)
650   "Start composing a mail in another window.
651 Use the posting of the current group by default.
652 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
653 posting style."
654   (interactive "P")
655   ;; We can't `let' gnus-newsgroup-name here, since that leads
656   ;; to local variables leaking.
657   (let ((group gnus-newsgroup-name)
658         ;; make sure last viewed article doesn't affect posting styles:
659         (gnus-article-copy)
660         (buffer (current-buffer)))
661     (unwind-protect
662         (progn
663           (setq gnus-newsgroup-name
664                 (if arg
665                     (if (= 1 (prefix-numeric-value arg))
666                         (completing-read "Use group: "
667                                          gnus-active-hashtb nil
668                                          (gnus-read-active-file-p))
669                       "")
670                   gnus-newsgroup-name))
671           ;; #### see comment in gnus-setup-message -- drv
672           (gnus-setup-message 'message (message-mail)))
673       (save-excursion
674         (set-buffer buffer)
675         (setq gnus-newsgroup-name group)))))
676
677 (defun gnus-summary-news-other-window (&optional arg)
678   "Start composing a news in another window.
679 Post to the current group by default.
680 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
681
682 This function prepares a news even when using mail groups.  This is useful
683 for posting messages to mail groups without actually sending them over the
684 network.  The corresponding back end must have a 'request-post method."
685   (interactive "P")
686   ;; We can't `let' gnus-newsgroup-name here, since that leads
687   ;; to local variables leaking.
688   (let ((group gnus-newsgroup-name)
689         ;; make sure last viewed article doesn't affect posting styles:
690         (gnus-article-copy)
691         (buffer (current-buffer)))
692     (unwind-protect
693         (progn
694           (setq gnus-newsgroup-name
695                 (if arg
696                     (if (= 1 (prefix-numeric-value arg))
697                         (completing-read "Use group: "
698                                          gnus-active-hashtb nil
699                                          (gnus-read-active-file-p))
700                       "")
701                   gnus-newsgroup-name))
702           ;; #### see comment in gnus-setup-message -- drv
703           (gnus-setup-message 'message
704             (progn
705               (message-news (gnus-group-real-name gnus-newsgroup-name))
706               (set (make-local-variable 'gnus-discouraged-post-methods)
707                    (remove
708                     (car (gnus-find-method-for-group gnus-newsgroup-name))
709                     gnus-discouraged-post-methods)))))
710       (save-excursion
711         (set-buffer buffer)
712         (setq gnus-newsgroup-name group)))))
713
714 (defun gnus-summary-post-news (&optional arg)
715   "Start composing a message.  Post to the current group by default.
716 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
717 Depending on the selected group, the message might be either a mail or
718 a news."
719   (interactive "P")
720   ;; Bind this variable here to make message mode hooks work ok.
721   (let ((gnus-newsgroup-name
722          (if arg
723              (if (= 1 (prefix-numeric-value arg))
724                  (completing-read "Newsgroup: " gnus-active-hashtb nil
725                                   (gnus-read-active-file-p))
726                "")
727            gnus-newsgroup-name))
728         ;; make sure last viewed article doesn't affect posting styles:
729         (gnus-article-copy))
730     (gnus-post-news 'post gnus-newsgroup-name)))
731
732
733 (defun gnus-summary-followup (yank &optional force-news)
734   "Compose a followup to an article.
735 If prefix argument YANK is non-nil, the original article is yanked
736 automatically.
737 YANK is a list of elements, where the car of each element is the
738 article number, and the cdr is the string to be yanked."
739   (interactive
740    (list (and current-prefix-arg
741               (gnus-summary-work-articles 1))))
742   (when yank
743     (gnus-summary-goto-subject
744      (if (listp (car yank))
745          (caar yank)
746        (car yank))))
747   (save-window-excursion
748     (gnus-summary-select-article))
749   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
750         (gnus-newsgroup-name gnus-newsgroup-name))
751     ;; Send a followup.
752     (gnus-post-news nil gnus-newsgroup-name
753                     headers gnus-article-buffer
754                     yank nil force-news)
755     (gnus-summary-handle-replysign)))
756
757 (defun gnus-summary-followup-with-original (n &optional force-news)
758   "Compose a followup to an article and include the original article."
759   (interactive "P")
760   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
761
762 (defun gnus-summary-followup-to-mail (&optional arg)
763   "Followup to the current mail message via news."
764   (interactive
765    (list (and current-prefix-arg
766               (gnus-summary-work-articles 1))))
767   (gnus-summary-followup arg t))
768
769 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
770   "Followup to the current mail message via news."
771   (interactive "P")
772   (gnus-summary-followup (gnus-summary-work-articles arg) t))
773
774 (defun gnus-inews-yank-articles (articles)
775   (let (beg article yank-string
776             (more-than-one (cdr articles))
777             (cur (current-buffer))
778             refs window)
779     (message-goto-body)
780     (while (setq article (pop articles))
781       (when (listp article)
782         (setq yank-string (nth 1 article)
783               article (nth 0 article)))
784       (save-window-excursion
785         (set-buffer gnus-summary-buffer)
786         (gnus-summary-select-article nil nil nil article)
787         (gnus-summary-remove-process-mark article))
788
789       ;; Gathering references.
790       (when more-than-one
791         (setq refs (message-list-references
792                     refs
793                     (mail-header-references gnus-current-headers)
794                     (mail-header-message-id gnus-current-headers)))
795         (when message-use-multi-frames
796           (when (setq window (get-buffer-window cur t))
797             (select-frame (window-frame window)))))
798
799       (gnus-copy-article-buffer nil yank-string)
800       (let ((message-reply-buffer gnus-article-copy)
801             (message-reply-headers
802              ;; The headers are decoded.
803              (with-current-buffer gnus-article-copy
804                (save-restriction
805                  (nnheader-narrow-to-headers)
806                  (nnheader-parse-naked-head)))))
807         (message-yank-original)
808         (setq beg (or beg (mark t))))
809       (when articles
810         (insert "\n")))
811     (push-mark)
812
813     ;; Replace with the gathered references.
814     (when refs
815       (push-mark beg)
816       (save-restriction
817         (message-narrow-to-headers)
818         (let ((case-fold-search t))
819           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
820               (replace-match "")
821             (goto-char (point-max))))
822         (mail-header-format
823          (list (or (assq 'References message-header-format-alist)
824                    '(References . message-shorten-references)))
825          (list (cons 'References
826                      (mapconcat 'identity (nreverse refs) " "))))
827         (backward-delete-char 1))
828       (setq beg (mark t))
829       (pop-mark))
830
831     (goto-char beg)))
832
833 (defun gnus-summary-cancel-article (&optional n symp)
834   "Cancel an article you posted.
835 Uses the process-prefix convention.  If given the symbolic
836 prefix `a', cancel using the standard posting method; if not
837 post using the current select method."
838   (interactive (gnus-interactive "P\ny"))
839   (let ((articles (gnus-summary-work-articles n))
840         (message-post-method
841          `(lambda (arg)
842             (gnus-post-method (eq ',symp 'a) ,gnus-newsgroup-name)))
843         article)
844     (while (setq article (pop articles))
845       (when (gnus-summary-select-article t nil nil article)
846         (when (gnus-eval-in-buffer-window gnus-article-buffer
847                 (save-excursion
848                   (set-buffer gnus-original-article-buffer)
849                   (message-cancel-news)))
850           (gnus-summary-mark-as-read article gnus-canceled-mark)
851           (gnus-cache-remove-article 1))
852         (gnus-article-hide-headers-if-wanted))
853       (gnus-summary-remove-process-mark article))))
854
855 (defun gnus-summary-supersede-article ()
856   "Compose an article that will supersede a previous article.
857 This is done simply by taking the old article and adding a Supersedes
858 header line with the old Message-ID."
859   (interactive)
860   (let ((article (gnus-summary-article-number)))
861     (gnus-setup-message 'reply-yank
862       (gnus-summary-select-article t)
863       (set-buffer gnus-original-article-buffer)
864       (message-supersede)
865       (push
866        `((lambda ()
867            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
868              (save-excursion
869                (set-buffer ,gnus-summary-buffer)
870                (gnus-cache-possibly-remove-article ,article nil nil nil t)
871                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
872        message-send-actions))))
873
874 \f
875
876 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
877   ;; make a copy of the article buffer with all text properties removed
878   ;; this copy is in the buffer gnus-article-copy.
879   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
880   ;; this buffer should be passed to all mail/news reply/post routines.
881   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
882   (save-excursion
883     (set-buffer gnus-article-copy)
884     (set-buffer-multibyte t))
885   (let ((article-buffer (or article-buffer gnus-article-buffer))
886         end beg)
887     (if (not (and (get-buffer article-buffer)
888                   (gnus-buffer-exists-p article-buffer)))
889         (error "Can't find any article buffer")
890       (save-excursion
891         (set-buffer article-buffer)
892         (let ((gnus-newsgroup-charset (or gnus-article-charset
893                                           gnus-newsgroup-charset))
894               (gnus-newsgroup-ignored-charsets
895                (or gnus-article-ignored-charsets
896                    gnus-newsgroup-ignored-charsets)))
897           (save-restriction
898             ;; Copy over the (displayed) article buffer, delete
899             ;; hidden text and remove text properties.
900             (widen)
901             (copy-to-buffer gnus-article-copy (point-min) (point-max))
902             (set-buffer gnus-article-copy)
903             ;; There's invisible and intangible text in T-gnus.  Especially,
904             ;; if there is a boundary line (X-Boundary: ------------------),
905             ;; in the end of a header, it will cause a serious problem.
906             (add-text-properties (point-min) (point-max)
907                                  '(invisible nil intangible nil))
908             (when yank-string
909               (message-goto-body)
910               (delete-region (point) (point-max))
911               (insert yank-string))
912             ;; Encode bitmap smileys to ordinary text.
913             ;; Possibly, the original text might be restored.
914             (static-unless (featurep 'xemacs)
915               (when (featurep 'smiley-mule)
916                 (smiley-encode-buffer)))
917             (gnus-article-delete-text-of-type 'annotation)
918             (gnus-remove-text-with-property 'gnus-prev)
919             (gnus-remove-text-with-property 'gnus-next)
920             (gnus-remove-text-with-property 'gnus-decoration)
921             (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
922             (insert
923              (prog1
924                  (static-if (featurep 'xemacs)
925                      ;; Revome smiley extents for (possibly) XEmacs 21.1.
926                      (format "%s"
927                              (buffer-substring-no-properties (point-min)
928                                                              (point-max)))
929                    (buffer-substring-no-properties (point-min) (point-max)))
930                (erase-buffer)))
931             ;; Find the original headers.
932             (set-buffer gnus-original-article-buffer)
933             (goto-char (point-min))
934             (while (looking-at message-unix-mail-delimiter)
935               (forward-line 1))
936             (let ((mail-header-separator ""))
937               (setq beg (point)
938                     end (or (message-goto-body)
939                             ;; There may be just a header.
940                             (point-max))))
941             ;; Delete the headers from the displayed articles.
942             (set-buffer gnus-article-copy)
943             (let ((mail-header-separator ""))
944               (delete-region (goto-char (point-min))
945                              (or (message-goto-body) (point-max))))
946             ;; Insert the original article headers.
947             (insert-buffer-substring gnus-original-article-buffer beg end)
948             ;; Decode charsets.
949             (let ((gnus-article-decode-hook
950                    (delq 'article-decode-charset
951                          (copy-sequence gnus-article-decode-hook))))
952               ;; Needed for T-gnus.
953               (add-hook 'gnus-article-decode-hook
954                         'article-decode-encoded-words)
955               (run-hooks 'gnus-article-decode-hook)))))
956       gnus-article-copy)))
957
958 (defun gnus-post-news (post &optional group header article-buffer yank subject
959                             force-news)
960   (when article-buffer
961     (gnus-copy-article-buffer))
962   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
963         (gnus-article-yanked-articles yank)
964         (add-to-list gnus-add-to-list))
965     (gnus-setup-message (cond (yank 'reply-yank)
966                               (article-buffer 'reply)
967                               (t 'message))
968       (let* ((group (or group gnus-newsgroup-name))
969              (charset (gnus-group-name-charset nil group))
970              (pgroup group)
971              to-address to-group mailing-list to-list
972              newsgroup-p)
973         (when group
974           (setq to-address (gnus-parameter-to-address group)
975                 to-group (gnus-group-find-parameter group 'to-group)
976                 to-list (gnus-parameter-to-list group)
977                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
978                 mailing-list (when gnus-mailing-list-groups
979                                (string-match gnus-mailing-list-groups group))
980                 group (gnus-group-name-decode (gnus-group-real-name group)
981                                               charset)))
982         (if (or (and to-group
983                      (gnus-news-group-p to-group))
984                 newsgroup-p
985                 force-news
986                 (and (gnus-news-group-p
987                       (or pgroup gnus-newsgroup-name)
988                       (or header gnus-current-article))
989                      (not mailing-list)
990                      (not to-list)
991                      (not to-address)))
992             ;; This is news.
993             (if post
994                 (message-news
995                  (or to-group
996                      (and (not (gnus-virtual-group-p pgroup)) group)))
997               (set-buffer gnus-article-copy)
998               (gnus-msg-treat-broken-reply-to)
999               (message-followup (if (or newsgroup-p force-news)
1000                                     (if (save-restriction
1001                                           (article-narrow-to-head)
1002                                           (message-fetch-field "newsgroups"))
1003                                         nil
1004                                       "")
1005                                   to-group)))
1006           ;; The is mail.
1007           (if post
1008               (progn
1009                 (message-mail (or to-address to-list))
1010                 ;; Arrange for mail groups that have no `to-address' to
1011                 ;; get that when the user sends off the mail.
1012                 (when (and (not to-list)
1013                            (not to-address)
1014                            add-to-list)
1015                   (push (list 'gnus-inews-add-to-address pgroup)
1016                         message-send-actions)))
1017             (set-buffer gnus-article-copy)
1018             (gnus-msg-treat-broken-reply-to)
1019             (message-wide-reply to-address)))
1020         (when yank
1021           (gnus-inews-yank-articles yank))))))
1022
1023 (defun gnus-msg-treat-broken-reply-to (&optional force)
1024   "Remove the Reply-to header if broken-reply-to."
1025   (when (or force
1026             (gnus-group-find-parameter
1027              gnus-newsgroup-name 'broken-reply-to))
1028     (save-restriction
1029       (message-narrow-to-head)
1030       (message-remove-header "reply-to"))))
1031
1032 (defun gnus-post-method (arg group &optional silent)
1033   "Return the posting method based on GROUP and ARG.
1034 If SILENT, don't prompt the user."
1035   (let ((gnus-post-method (or (gnus-parameter-post-method group)
1036                               gnus-post-method))
1037         (group-method (gnus-find-method-for-group group)))
1038     (cond
1039      ;; If the group-method is nil (which shouldn't happen) we use
1040      ;; the default method.
1041      ((null group-method)
1042       (or (and (listp gnus-post-method) ;If not current/native/nil
1043                (not (listp (car gnus-post-method))) ; and not a list of methods
1044                gnus-post-method)        ;then use it.
1045           gnus-select-method
1046           message-post-method))
1047      ;; We want the inverse of the default
1048      ((and arg (not (eq arg 0)))
1049       (if (eq gnus-post-method 'current)
1050           gnus-select-method
1051         group-method))
1052      ;; We query the user for a post method.
1053      ((or arg
1054           (and (listp gnus-post-method)
1055                (listp (car gnus-post-method))))
1056       (let* ((methods
1057               ;; Collect all methods we know about.
1058               (append
1059                (when (listp gnus-post-method)
1060                  (if (listp (car gnus-post-method))
1061                      gnus-post-method
1062                    (list gnus-post-method)))
1063                gnus-secondary-select-methods
1064                (mapcar 'cdr gnus-server-alist)
1065                (mapcar 'car gnus-opened-servers)
1066                (list gnus-select-method)
1067                (list group-method)))
1068              method-alist post-methods method)
1069         ;; Weed out all mail methods.
1070         (while methods
1071           (setq method (gnus-server-get-method "" (pop methods)))
1072           (when (and (or (gnus-method-option-p method 'post)
1073                          (gnus-method-option-p method 'post-mail))
1074                      (not (member method post-methods)))
1075             (push method post-methods)))
1076         ;; Create a name-method alist.
1077         (setq method-alist
1078               (mapcar
1079                (lambda (m)
1080                  (if (equal (cadr m) "")
1081                      (list (symbol-name (car m)) m)
1082                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
1083                post-methods))
1084         ;; Query the user.
1085         (cadr
1086          (assoc
1087           (setq gnus-last-posting-server
1088                 (if (and silent
1089                          gnus-last-posting-server)
1090                     ;; Just use the last value.
1091                     gnus-last-posting-server
1092                   (completing-read
1093                    "Posting method: " method-alist nil t
1094                    (cons (or gnus-last-posting-server "") 0))))
1095           method-alist))))
1096      ;; Override normal method.
1097      ((and (eq gnus-post-method 'current)
1098            (not (memq (car group-method) gnus-discouraged-post-methods))
1099            (gnus-get-function group-method 'request-post t))
1100       (assert (not arg))
1101       group-method)
1102      ;; Use gnus-post-method.
1103      ((listp gnus-post-method)          ;A method...
1104       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
1105       gnus-post-method)
1106      ;; Use the normal select method (nil or native).
1107      (t gnus-select-method))))
1108
1109 \f
1110 ;; Dummies to avoid byte-compile warning.
1111 (eval-when-compile
1112   (defvar xemacs-codename))
1113
1114 (defun gnus-message-make-user-agent (&optional include-mime-info max-column
1115                                                  newline-product)
1116   "Return a user-agent info.  If INCLUDE-MIME-INFO is non-nil and the
1117 variable `mime-edit-user-agent-value' is bound, the value will be
1118 included in the return value, and `gnus-user-agent' is ignored.  If
1119 MAX-COLUMN is specified, the return value will be folded up as it were
1120 filled.  NEWLINE-PRODUCT specifies whether a newline should be
1121 inserted in front of each product-token.  If the value is t or `hard',
1122 it works strictly.  Otherwise, if it is non-nil (e.g. `soft'), it
1123 works semi-strictly.
1124
1125 Here is an odd example, which inserts a User-Agent: header when you
1126 begin to compose a message:
1127
1128 \(add-hook 'gnus-message-setup-hook
1129           (lambda nil
1130             (setq message-user-agent nil)
1131             (save-excursion
1132               (save-restriction
1133                 (message-narrow-to-headers)
1134                 (goto-char (point-max))
1135                 (insert \"User-Agent: \"
1136                         (gnus-message-make-user-agent t 76 'soft)
1137                         \"\\n\")))))
1138 "
1139   (let ((gnus-v (gnus-extended-version))
1140         user-agent)
1141     (cond ((and include-mime-info
1142                 (boundp 'mime-edit-user-agent-value))
1143            (setq user-agent (concat gnus-v " " mime-edit-user-agent-value)))
1144           ((eq gnus-user-agent 'gnus-mime-edit)
1145            (setq user-agent
1146                  (if (boundp 'mime-edit-user-agent-value)
1147                      (concat gnus-v " " mime-edit-user-agent-value)
1148                    gnus-v)))
1149           (t
1150            (setq user-agent (if (stringp gnus-user-agent)
1151                                 gnus-user-agent
1152                               (concat gnus-v
1153                                       (let ((emacs-v (gnus-emacs-version)))
1154                                         (when emacs-v
1155                                           (concat " " emacs-v))))))))
1156     (when max-column
1157       (unless (natnump max-column)
1158         (setq max-column 76))
1159       (with-temp-buffer
1160         (set-buffer-multibyte t)
1161         (insert (mapconcat 'identity (split-string user-agent) " "))
1162         (goto-char (point-min))
1163         (let ((bol t)
1164               start agent agents width element swidth)
1165           (while (re-search-forward "\\([^ ]+\\) ?" nil t)
1166             (setq start (match-beginning 0))
1167             (if (eq (char-after start) ?\()
1168                 (progn
1169                   (goto-char start)
1170                   (forward-list)
1171                   (push (buffer-substring start (point)) agent))
1172               (when agent
1173                 (push (nreverse agent) agents))
1174               (setq agent (list (match-string 1)))))
1175           (when agent
1176             (push (nreverse agent) agents))
1177           (setq agents (nreverse agents))
1178           (if (> (+ 12 (string-width (caar agents))) max-column)
1179               (setq user-agent "\n"
1180                     width 0)
1181             (setq user-agent ""
1182                   width 11))
1183           (while agents
1184             (setq agent (car agents)
1185                   agents (cdr agents))
1186             (when (and (not bol)
1187                        (or (memq newline-product '(t hard))
1188                            (and newline-product
1189                                 (> (+ width 1
1190                                       (string-width (mapconcat 'identity
1191                                                                agent " ")))
1192                                    max-column))))
1193               (setq user-agent (concat user-agent "\n")
1194                     width 0
1195                     bol t))
1196             (while agent
1197               (setq element (car agent)
1198                     swidth (string-width element)
1199                     agent (cdr agent))
1200               (if bol
1201                   (setq user-agent (if (member user-agent '("" "\n"))
1202                                        (concat user-agent element)
1203                                      (concat user-agent " " element))
1204                         width (+ width 1 swidth)
1205                         bol nil)
1206                 (if (> (+ width 1 swidth) max-column)
1207                     (setq user-agent (concat user-agent "\n " element)
1208                           width (1+ swidth))
1209                   (setq user-agent (concat user-agent " " element)
1210                         width (+ width 1 swidth)))))))))
1211     user-agent))
1212
1213 \f
1214 ;;;
1215 ;;; Gnus Mail Functions
1216 ;;;
1217
1218 ;;; Mail reply commands of Gnus summary mode
1219
1220 (defun gnus-summary-reply (&optional yank wide very-wide)
1221   "Start composing a mail reply to the current message.
1222 If prefix argument YANK is non-nil, the original article is yanked
1223 automatically.
1224 If WIDE, make a wide reply.
1225 If VERY-WIDE, make a very wide reply."
1226   (interactive
1227    (list (and current-prefix-arg
1228               (gnus-summary-work-articles 1))))
1229   ;; Allow user to require confirmation before replying by mail to the
1230   ;; author of a news article (or mail message).
1231   (when (or
1232             (not (or (gnus-news-group-p gnus-newsgroup-name)
1233                      gnus-confirm-treat-mail-like-news))
1234             (not (cond ((stringp gnus-confirm-mail-reply-to-news)
1235                         (string-match gnus-confirm-mail-reply-to-news
1236                                       gnus-newsgroup-name))
1237                        ((functionp gnus-confirm-mail-reply-to-news)
1238                         (funcall gnus-confirm-mail-reply-to-news gnus-newsgroup-name))
1239                        (t gnus-confirm-mail-reply-to-news)))
1240             (y-or-n-p "Really reply by mail to article author? "))
1241     (let* ((article
1242             (if (listp (car yank))
1243                 (caar yank)
1244               (car yank)))
1245            (gnus-article-reply (or article (gnus-summary-article-number)))
1246            (gnus-article-yanked-articles yank)
1247            (headers ""))
1248       ;; Stripping headers should be specified with mail-yank-ignored-headers.
1249       (when yank
1250         (gnus-summary-goto-subject article))
1251       (gnus-setup-message (if yank 'reply-yank 'reply)
1252         (if (not very-wide)
1253             (gnus-summary-select-article)
1254           (dolist (article very-wide)
1255             (gnus-summary-select-article nil nil nil article)
1256             (save-excursion
1257               (set-buffer (gnus-copy-article-buffer))
1258               (gnus-msg-treat-broken-reply-to)
1259               (save-restriction
1260                 (message-narrow-to-head)
1261                 (setq headers (concat headers (buffer-string)))))))
1262         (set-buffer (gnus-copy-article-buffer))
1263         (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
1264         (save-restriction
1265           (message-narrow-to-head)
1266           (when very-wide
1267             (erase-buffer)
1268             (insert headers))
1269           (goto-char (point-max)))
1270         (message-reply nil wide)
1271         (when yank
1272           (gnus-inews-yank-articles yank))
1273         (gnus-summary-handle-replysign)))))
1274
1275 (defun gnus-summary-handle-replysign ()
1276   "Check the various replysign variables and take action accordingly."
1277   (when nil;;(or gnus-message-replysign gnus-message-replyencrypt)
1278     (let (signed encrypted)
1279       (save-excursion
1280         (set-buffer gnus-article-buffer)
1281         (setq signed (memq 'signed gnus-article-wash-types))
1282         (setq encrypted (memq 'encrypted gnus-article-wash-types)))
1283       (cond ((and gnus-message-replyencrypt encrypted)
1284              (mml-secure-message mml-default-encrypt-method
1285                                  (if gnus-message-replysignencrypted
1286                                      'signencrypt
1287                                    'encrypt)))
1288             ((and gnus-message-replysign signed)
1289              (mml-secure-message mml-default-sign-method 'sign))))))
1290
1291 (defun gnus-summary-reply-with-original (n &optional wide)
1292   "Start composing a reply mail to the current message.
1293 The original article will be yanked."
1294   (interactive "P")
1295   (gnus-summary-reply (gnus-summary-work-articles n) wide))
1296
1297 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
1298   "Like `gnus-summary-reply' except removing reply-to field.
1299 If prefix argument YANK is non-nil, the original article is yanked
1300 automatically.
1301 If WIDE, make a wide reply.
1302 If VERY-WIDE, make a very wide reply."
1303   (interactive
1304    (list (and current-prefix-arg
1305               (gnus-summary-work-articles 1))))
1306   (let ((gnus-msg-force-broken-reply-to t))
1307     (gnus-summary-reply yank wide very-wide)))
1308
1309 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
1310   "Like `gnus-summary-reply-with-original' except removing reply-to field.
1311 The original article will be yanked."
1312   (interactive "P")
1313   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
1314
1315 (defun gnus-summary-wide-reply (&optional yank)
1316   "Start composing a wide reply mail to the current message.
1317 If prefix argument YANK is non-nil, the original article is yanked
1318 automatically."
1319   (interactive
1320    (list (and current-prefix-arg
1321               (gnus-summary-work-articles 1))))
1322   (gnus-summary-reply yank t))
1323
1324 (defun gnus-summary-wide-reply-with-original (n)
1325   "Start composing a wide reply mail to the current message.
1326 The original article will be yanked.
1327 Uses the process/prefix convention."
1328   (interactive "P")
1329   (gnus-summary-reply-with-original n t))
1330
1331 (defun gnus-summary-very-wide-reply (&optional yank)
1332   "Start composing a very wide reply mail to the current message.
1333 If prefix argument YANK is non-nil, the original article is yanked
1334 automatically."
1335   (interactive
1336    (list (and current-prefix-arg
1337               (gnus-summary-work-articles 1))))
1338   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
1339
1340 (defun gnus-summary-very-wide-reply-with-original (n)
1341   "Start composing a very wide reply mail to the current message.
1342 The original article will be yanked."
1343   (interactive "P")
1344   (gnus-summary-reply
1345    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1346
1347 (defun gnus-summary-mail-forward (&optional full-headers post)
1348   "Forward the current message(s) to another user.
1349 If process marks exist, forward all marked messages;
1350 If FULL-HEADERS (the prefix), include full headers when forwarding.
1351
1352 Note that this function definition for T-gnus is totally different
1353 from the original Gnus."
1354   (interactive "P")
1355   (if (cdr (gnus-summary-work-articles nil))
1356       ;; Process marks are given.
1357       (gnus-summary-digest-mail-forward nil post)
1358     ;; No process marks.
1359     (let* ((gnus-article-reply (gnus-summary-article-number))
1360            (gnus-article-yanked-articles (list gnus-article-reply))
1361            charset
1362            (message-included-forward-headers
1363             (if full-headers "" message-included-forward-headers)))
1364       (gnus-setup-message 'forward
1365         (gnus-summary-select-article)
1366         (setq charset default-mime-charset)
1367         (set-buffer gnus-original-article-buffer)
1368         (make-local-variable 'default-mime-charset)
1369         (setq default-mime-charset charset)
1370         (message-forward post)))))
1371
1372 (defun gnus-summary-digest-mail-forward (&optional n post)
1373   "Digests and forwards all articles in this series.
1374 If N is a positive number, forward the N next articles.
1375 If N is a negative number, forward the N previous articles.
1376 If N is nil and any articles have been marked with the process mark,
1377 forward those articles instead.
1378 Optional POST will use news to forward instead of mail."
1379   (interactive "P")
1380   (let* ((articles (gnus-summary-work-articles n))
1381          (gnus-article-yanked-articles (copy-sequence articles))
1382          (topics "Topics:\n")
1383          subject article frame)
1384     (when (car articles)
1385       (gnus-setup-message 'forward
1386         (gnus-summary-select-article)
1387         (if (cdr articles)
1388             (setq articles (sort articles '<)
1389                   subject "Digested Articles")
1390           (with-current-buffer gnus-original-article-buffer
1391             (setq subject (message-make-forward-subject))))
1392         (if post
1393             (message-news nil subject)
1394           (message-mail nil subject))
1395         (when (and message-use-multi-frames (cdr articles))
1396           (setq frame (window-frame (get-buffer-window (current-buffer)))))
1397         (message-goto-body)
1398         (while (setq article (pop articles))
1399           (save-window-excursion
1400             (set-buffer gnus-summary-buffer)
1401             (gnus-summary-select-article nil nil nil article)
1402             (setq topics (concat topics "    "
1403                                  (mail-header-subject gnus-current-headers)
1404                                  "\n"))
1405             (gnus-summary-remove-process-mark article))
1406           (when frame
1407             (select-frame frame))
1408           (insert (mime-make-tag "message" "rfc822") "\n")
1409           (narrow-to-region (point) (point))
1410           (insert-buffer-substring gnus-original-article-buffer)
1411           (save-restriction
1412             (article-narrow-to-head)
1413             (message-remove-header message-included-forward-headers t nil t))
1414           (goto-char (point-max))
1415           (widen))
1416         (push-mark)
1417         (message-goto-body)
1418         (insert topics)
1419         (message-goto-body)
1420         (mime-edit-enclose-digest-region (point)(mark t))))))
1421
1422 (defun gnus-summary-digest-post-forward (&optional n)
1423   "Digest and forwards all articles in this series to a newsgroup.
1424 If N is a positive number, forward the N next articles.
1425 If N is a negative number, forward the N previous articles.
1426 If N is nil and any articles have been marked with the process mark,
1427 forward those articles instead."
1428   (interactive "P")
1429   (gnus-summary-digest-mail-forward n t))
1430
1431 (defun gnus-summary-resend-message (address n)
1432   "Resend the current article to ADDRESS."
1433   (interactive
1434    (list (message-read-from-minibuffer
1435           "Resend message(s) to: "
1436           (when (and gnus-summary-resend-default-address
1437                      (gnus-buffer-live-p gnus-original-article-buffer))
1438             ;; If some other article is currently selected, the
1439             ;; initial-contents is wrong. Whatever, it is just the
1440             ;; initial-contents.
1441             (with-current-buffer gnus-original-article-buffer
1442               (nnmail-fetch-field "to"))))
1443          current-prefix-arg))
1444   (let ((articles (gnus-summary-work-articles n))
1445         article)
1446     (while (setq article (pop articles))
1447       (gnus-summary-select-article nil nil nil article)
1448       (save-excursion
1449         (set-buffer gnus-original-article-buffer)
1450         (message-resend address))
1451       (gnus-summary-mark-article-as-forwarded article))))
1452
1453 ;; From: Matthieu Moy <Matthieu.Moy@imag.fr>
1454 (defun gnus-summary-resend-message-edit ()
1455   "Resend an article that has already been sent.
1456 A new buffer will be created to allow the user to modify body and
1457 contents of the message, and then, everything will happen as when
1458 composing a new message."
1459   (interactive)
1460   (let ((article (gnus-summary-article-number)))
1461     (gnus-setup-message 'reply-yank
1462       (gnus-summary-select-article t)
1463       (set-buffer gnus-original-article-buffer)
1464       (let ((cur (current-buffer))
1465             (to (message-fetch-field "to")))
1466         ;; Get a normal message buffer.
1467         (message-pop-to-buffer (message-buffer-name "Resend" to))
1468         (insert-buffer-substring cur)
1469
1470         ;; T-gnus change: Use MIME-Edit to recompose a message.
1471         ;;(mime-to-mml)
1472         (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
1473           (fset 'mime-edit-decode-single-part-in-buffer
1474                 (lambda (&rest args)
1475                   (if (let ((content-type (car args)))
1476                         (and (eq 'message (mime-content-type-primary-type
1477                                            content-type))
1478                              (eq 'rfc822 (mime-content-type-subtype
1479                                           content-type))))
1480                       (setcar (cdr args) 'not-decode-text))
1481                   (apply ofn args)))
1482           (unwind-protect
1483               (mime-edit-again nil t)
1484             (fset 'mime-edit-decode-single-part-in-buffer ofn)))
1485         (message-narrow-to-head-1)
1486         (insert "From: " (message-make-from) "\n")
1487         (while (re-search-forward "^From:" nil t)
1488           (beginning-of-line)
1489           (insert "Original-"))
1490         (message-remove-header "^>From[\t ]" t)
1491
1492         ;; Gnus will generate a new one when sending.
1493         (message-remove-header "Message-ID")
1494         (message-remove-header message-ignored-resent-headers t)
1495         ;; Remove unwanted headers.
1496         (goto-char (point-max))
1497         (insert mail-header-separator)
1498         (goto-char (point-min))
1499         (when (re-search-forward "^To:\\|^Newsgroups:" nil 'move)
1500           (forward-char 1))
1501         (widen)))))
1502
1503 (defun gnus-summary-post-forward (&optional full-headers)
1504   "Forward the current article to a newsgroup.
1505 If FULL-HEADERS (the prefix), include full headers when forwarding."
1506   (interactive "P")
1507   (gnus-summary-mail-forward full-headers t))
1508
1509 (defvar gnus-nastygram-message
1510   "The following article was inappropriately posted to %s.\n\n"
1511   "Format string to insert in nastygrams.
1512 The current group name will be inserted at \"%s\".")
1513
1514 (defun gnus-summary-mail-nastygram (n)
1515   "Send a nastygram to the author of the current article."
1516   (interactive "P")
1517   (when (or gnus-expert-user
1518             (gnus-y-or-n-p
1519              "Really send a nastygram to the author of the current article? "))
1520     (let ((group gnus-newsgroup-name))
1521       (gnus-summary-reply-with-original n)
1522       (set-buffer gnus-message-buffer)
1523       (message-goto-body)
1524       (insert (format gnus-nastygram-message group))
1525       (message-send-and-exit))))
1526
1527 (defun gnus-summary-mail-crosspost-complaint (n)
1528   "Send a complaint about crossposting to the current article(s)."
1529   (interactive "P")
1530   (let ((articles (gnus-summary-work-articles n))
1531         article)
1532     (while (setq article (pop articles))
1533       (set-buffer gnus-summary-buffer)
1534       (gnus-summary-goto-subject article)
1535       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1536             newsgroups followup-to)
1537         (gnus-summary-select-article)
1538         (set-buffer gnus-original-article-buffer)
1539         (if (and (<= (length (message-tokenize-header
1540                               (setq newsgroups
1541                                     (mail-fetch-field "newsgroups"))
1542                               ", "))
1543                      1)
1544                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1545                      (not (member group (message-tokenize-header
1546                                          followup-to ", ")))))
1547             (if followup-to
1548                 (gnus-message 1 "Followup-to restricted")
1549               (gnus-message 1 "Not a crossposted article"))
1550           (set-buffer gnus-summary-buffer)
1551           (gnus-summary-reply-with-original 1)
1552           (set-buffer gnus-message-buffer)
1553           (message-goto-body)
1554           (insert (format gnus-crosspost-complaint newsgroups group))
1555           (message-goto-subject)
1556           (re-search-forward " *$")
1557           (replace-match " (crosspost notification)" t t)
1558           (gnus-deactivate-mark)
1559           (when (gnus-y-or-n-p "Send this complaint? ")
1560             (message-send-and-exit)))))))
1561
1562 (defun gnus-mail-parse-comma-list ()
1563   (let (accumulated
1564         beg)
1565     (skip-chars-forward " ")
1566     (while (not (eobp))
1567       (setq beg (point))
1568       (skip-chars-forward "^,")
1569       (while (zerop
1570               (save-excursion
1571                 (save-restriction
1572                   (let ((i 0))
1573                     (narrow-to-region beg (point))
1574                     (goto-char beg)
1575                     (logand (progn
1576                               (while (search-forward "\"" nil t)
1577                                 (incf i))
1578                               (if (zerop i) 2 i))
1579                             2)))))
1580         (skip-chars-forward ",")
1581         (skip-chars-forward "^,"))
1582       (skip-chars-backward " ")
1583       (push (buffer-substring beg (point))
1584             accumulated)
1585       (skip-chars-forward "^,")
1586       (skip-chars-forward ", "))
1587     accumulated))
1588
1589 (defun gnus-inews-add-to-address (group)
1590   (let ((to-address (mail-fetch-field "to")))
1591     (when (and to-address
1592                (gnus-alive-p))
1593       ;; This mail group doesn't have a `to-list', so we add one
1594       ;; here.  Magic!
1595       (when (gnus-y-or-n-p
1596              (format "Do you want to add this as `to-list': %s? " to-address))
1597         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1598
1599 (defun gnus-put-message ()
1600   "Put the current message in some group and return to Gnus."
1601   (interactive)
1602   (let ((reply gnus-article-reply)
1603         (winconf gnus-prev-winconf)
1604         (group gnus-newsgroup-name))
1605     (unless (and group
1606                  (not (gnus-group-read-only-p group)))
1607       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1608
1609     (when (gnus-group-entry group)
1610       (error "No such group: %s" group))
1611     (save-excursion
1612       (save-restriction
1613         (widen)
1614         (message-narrow-to-headers)
1615         (let ((gnus-deletable-headers nil))
1616           (message-generate-headers
1617            (if (message-news-p)
1618                message-required-news-headers
1619              message-required-mail-headers)))
1620         (goto-char (point-max))
1621         (if (string-match " " group)
1622             (insert "Gcc: \"" group "\"\n")
1623           (insert "Gcc: " group "\n"))
1624         (widen)))
1625     (gnus-inews-do-gcc)
1626     (when (and (get-buffer gnus-group-buffer)
1627                (gnus-buffer-exists-p (car-safe reply))
1628                (cdr reply))
1629       (set-buffer (car reply))
1630       (gnus-summary-mark-article-as-replied (cdr reply)))
1631     (when winconf
1632       (set-window-configuration winconf))))
1633
1634 (defun gnus-article-mail (yank)
1635   "Send a reply to the address near point.
1636 If YANK is non-nil, include the original article."
1637   (interactive "P")
1638   (let ((address
1639          (buffer-substring
1640           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1641           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1642     (when address
1643       (gnus-msg-mail address)
1644       (when yank
1645         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1646
1647 (defvar nntp-server-type)
1648 (defun gnus-bug ()
1649   "Send a bug report to the Gnus maintainers."
1650   (interactive)
1651   (unless (gnus-alive-p)
1652     (error "Gnus has been shut down"))
1653   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1654     (unless (message-mail-user-agent)
1655       (message-pop-to-buffer "*Gnus Bug*")
1656       (delete-other-windows)
1657       (when gnus-bug-create-help-buffer
1658         (switch-to-buffer "*Gnus Help Bug*")
1659         (erase-buffer)
1660         (insert gnus-bug-message)
1661         (goto-char (point-min))
1662         (sit-for 0)
1663         (set-buffer "*Gnus Bug*")))
1664     (let ((message-this-is-mail t))
1665       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1666     (when gnus-bug-create-help-buffer
1667       (push `(gnus-bug-kill-buffer) message-send-actions))
1668     (goto-char (point-min))
1669     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1670     (forward-line 1)
1671     (insert gnus-product-name " " gnus-version-number
1672             " (r" gnus-revision-number ") "
1673             "based on " gnus-original-product-name " v"
1674             gnus-original-version-number "\n"
1675             (emacs-version) "\n")
1676     (when (and (boundp 'nntp-server-type)
1677                (stringp nntp-server-type))
1678       (insert nntp-server-type))
1679     (insert "\n\n\n\n\n")
1680     (let (mime-content-types)
1681       (mime-edit-insert-tag
1682        "application" "emacs-lisp"
1683        "\nContent-Disposition: inline\nContent-Description: User settings"))
1684     (insert (with-temp-buffer
1685               (gnus-debug)
1686               (buffer-string)))
1687     (let (mime-content-types)
1688       (mime-edit-insert-tag "text" "plain"))
1689     (goto-char (point-min))
1690     (search-forward "Subject: " nil t)
1691     (message "")))
1692
1693 (defun gnus-bug-kill-buffer ()
1694   (when (get-buffer "*Gnus Help Bug*")
1695     (kill-buffer "*Gnus Help Bug*")))
1696
1697 (defun gnus-summary-yank-message (buffer n)
1698   "Yank the current article into a composed message."
1699   (interactive
1700    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1701          current-prefix-arg))
1702   (when (gnus-buffer-live-p buffer)
1703     (let ((summary-frame (selected-frame))
1704           (message-frame (when (static-if (featurep 'xemacs)
1705                                    (device-on-window-system-p)
1706                                  window-system)
1707                            (let ((window (get-buffer-window buffer t)))
1708                              (when window
1709                                (window-frame window)))))
1710           (separator (concat "^" (regexp-quote mail-header-separator)
1711                              "\n")))
1712       (gnus-summary-iterate n
1713         (gnus-summary-select-article)
1714         (gnus-copy-article-buffer)
1715         (when (frame-live-p message-frame)
1716           (raise-frame message-frame)
1717           (select-frame message-frame))
1718         (with-current-buffer buffer
1719           (when (save-excursion
1720                   (beginning-of-line)
1721                   (let (case-fold-search)
1722                     (and (not (re-search-backward separator nil t))
1723                          (re-search-forward separator nil t))))
1724             (goto-char (match-end 0)))
1725           (message-yank-buffer gnus-article-copy))
1726         (select-frame summary-frame))
1727       (when (frame-live-p message-frame)
1728         (select-frame message-frame)))))
1729
1730 (defun gnus-debug ()
1731   "Attempts to go through the Gnus source file and report what variables have been changed.
1732 The source file has to be in the Emacs load path."
1733   (interactive)
1734   (let ((files gnus-debug-files)
1735         (point (point))
1736         file expr olist sym)
1737     (gnus-message 4 "Please wait while we snoop your variables...")
1738     ;; Go through all the files looking for non-default values for variables.
1739     (save-excursion
1740       (sit-for 0)
1741       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1742       (while files
1743         (erase-buffer)
1744         (when (and (setq file (locate-library (pop files)))
1745                    (file-exists-p file))
1746           (insert-file-contents file)
1747           (goto-char (point-min))
1748           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1749               (gnus-message 4 "Malformed sources in file %s" file)
1750             (narrow-to-region (point-min) (point))
1751             (goto-char (point-min))
1752             (while (setq expr (ignore-errors (read (current-buffer))))
1753               (ignore-errors
1754                 (and (memq (car expr) '(defvar defcustom defvoo))
1755                      (stringp (nth 3 expr))
1756                      (not (memq (nth 1 expr) gnus-debug-exclude-variables))
1757                      (or (not (boundp (nth 1 expr)))
1758                          (not (equal (eval (nth 2 expr))
1759                                      (symbol-value (nth 1 expr)))))
1760                      (push (nth 1 expr) olist)))))))
1761       (kill-buffer (current-buffer)))
1762     (when (setq olist (nreverse olist))
1763       (insert ";----------------- Environment follows ------------------\n\n"))
1764     (while olist
1765       (if (boundp (car olist))
1766           (ignore-errors
1767             (pp `(setq ,(car olist)
1768                        ,(if (or (consp (setq sym (symbol-value (car olist))))
1769                                 (and (symbolp sym)
1770                                      (not (or (eq sym nil)
1771                                               (eq sym t)))))
1772                             (list 'quote (symbol-value (car olist)))
1773                           (symbol-value (car olist))))
1774                 (current-buffer)))
1775         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1776       (setq olist (cdr olist)))
1777     ;; Remove any control chars - they seem to cause trouble for some
1778     ;; mailers.  (Byte-compiled output from the stuff above.)
1779     (goto-char point)
1780     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1781       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1782                      t t))
1783     ;; Break MIME tags purposely.
1784     (goto-char point)
1785     (while (re-search-forward mime-edit-tag-regexp nil t)
1786       (goto-char (1+ (match-beginning 0)))
1787       (insert "X"))))
1788
1789 ;;; Treatment of rejected articles.
1790 ;;; Bounced mail.
1791
1792 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1793   "Re-mail the current message.
1794 This only makes sense if the current message is a bounce message that
1795 contains some mail you have written which has been bounced back to
1796 you.
1797 If FETCH, try to fetch the article that this is a reply to, if indeed
1798 this is a reply."
1799   (interactive "P")
1800   (gnus-summary-select-article t)
1801   (set-buffer gnus-original-article-buffer)
1802   (gnus-setup-message 'compose-bounce
1803     (let* ((references (mail-fetch-field "references"))
1804            (parent (and references (gnus-parent-id references))))
1805       (message-bounce)
1806       ;; If there are references, we fetch the article we answered to.
1807       (and fetch parent
1808            (gnus-summary-refer-article parent)
1809            (gnus-summary-show-all-headers)))))
1810
1811 ;;; Gcc handling.
1812
1813 (defun gnus-inews-group-method (group)
1814   (cond
1815    ;; If the group doesn't exist, we assume
1816    ;; it's an archive group...
1817    ((and (null (gnus-get-info group))
1818          (eq (car (gnus-server-to-method gnus-message-archive-method))
1819              (car (gnus-server-to-method (gnus-group-method group)))))
1820     gnus-message-archive-method)
1821    ;; Use the method.
1822    ((gnus-info-method (gnus-get-info group))
1823     (gnus-info-method (gnus-get-info group)))
1824    ;; Find the method.
1825    (t (gnus-server-to-method (gnus-group-method group)))))
1826
1827 ;; Do Gcc handling, which copied the message over to some group.
1828 (defun gnus-inews-do-gcc (&optional gcc)
1829   (interactive)
1830   (save-excursion
1831     (save-restriction
1832       (message-narrow-to-headers)
1833       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1834             (coding-system-for-write 'raw-text)
1835             groups group method group-art
1836             mml-externalize-attachments)
1837         (when gcc
1838           (message-remove-header "gcc")
1839           (widen)
1840           (setq groups (message-unquote-tokens
1841                         (message-tokenize-header gcc " ,")))
1842           ;; Copy the article over to some group(s).
1843           (while (setq group (pop groups))
1844             (unless (gnus-check-server
1845                      (setq method (gnus-inews-group-method group)))
1846               (error "Can't open server %s" (if (stringp method) method
1847                                               (car method))))
1848             (unless (gnus-request-group group nil method)
1849               (gnus-request-create-group group method))
1850             (setq mml-externalize-attachments
1851                   (if (stringp gnus-gcc-externalize-attachments)
1852                       (string-match gnus-gcc-externalize-attachments group)
1853                     gnus-gcc-externalize-attachments))
1854             (save-excursion
1855               (nnheader-set-temp-buffer " *acc*")
1856               (insert-buffer-substring message-encoding-buffer)
1857               (gnus-run-hooks 'gnus-before-do-gcc-hook)
1858               (goto-char (point-min))
1859               (when (re-search-forward
1860                      (concat "^" (regexp-quote mail-header-separator) "$")
1861                      nil t)
1862                 (replace-match "" t t ))
1863               (unless (setq group-art
1864                             (gnus-request-accept-article group method t t))
1865                 (gnus-message 1 "Couldn't store article in group %s: %s"
1866                               group (gnus-status-message method))
1867                 (sit-for 2))
1868               (when (and group-art
1869                          ;; FIXME: Should gcc-mark-as-read work when
1870                          ;; Gnus is not running?
1871                          (gnus-alive-p)
1872                          (or gnus-gcc-mark-as-read
1873                              (and
1874                               (boundp 'gnus-inews-mark-gcc-as-read)
1875                               (symbol-value 'gnus-inews-mark-gcc-as-read))))
1876                 (gnus-group-mark-article-read group (cdr group-art)))
1877               (kill-buffer (current-buffer)))))))))
1878
1879 (defun gnus-inews-insert-gcc ()
1880   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1881   (save-excursion
1882     (save-restriction
1883       (message-narrow-to-headers)
1884       (let* ((group gnus-outgoing-message-group)
1885              (gcc (cond
1886                    ((functionp group)
1887                     (funcall group))
1888                    ((or (stringp group) (list group))
1889                     group))))
1890         (when gcc
1891           (insert "Gcc: "
1892                   (if (stringp gcc)
1893                       (if (string-match " " gcc)
1894                           (concat "\"" gcc "\"")
1895                         gcc)
1896                     (mapconcat (lambda (group)
1897                                  (if (string-match " " group)
1898                                      (concat "\"" group "\"")
1899                                    group))
1900                                gcc " "))
1901                   "\n"))))))
1902
1903 (defun gnus-inews-insert-archive-gcc (&optional group)
1904   "Insert the Gcc to say where the article is to be archived."
1905   (let* ((var gnus-message-archive-group)
1906          (group (or group gnus-newsgroup-name ""))
1907          (gcc-self-val
1908           (and gnus-newsgroup-name
1909                (not (equal gnus-newsgroup-name ""))
1910                (gnus-group-find-parameter
1911                 gnus-newsgroup-name 'gcc-self)))
1912          result
1913          (groups
1914           (cond
1915            ((null gnus-message-archive-method)
1916             ;; Ignore.
1917             nil)
1918            ((stringp var)
1919             ;; Just a single group.
1920             (list var))
1921            ((null var)
1922             ;; We don't want this.
1923             nil)
1924            ((and (listp var) (stringp (car var)))
1925             ;; A list of groups.
1926             var)
1927            ((functionp var)
1928             ;; A function.
1929             (funcall var group))
1930            (t
1931             ;; An alist of regexps/functions/forms.
1932             (while (and var
1933                         (not
1934                          (setq result
1935                                (cond
1936                                 ((stringp (caar var))
1937                                  ;; Regexp.
1938                                  (when (string-match (caar var) group)
1939                                    (cdar var)))
1940                                 ((functionp (car var))
1941                                  ;; Function.
1942                                  (funcall (car var) group))
1943                                 (t
1944                                  (eval (car var)))))))
1945               (setq var (cdr var)))
1946             result)))
1947          name)
1948     (when (or groups gcc-self-val)
1949       (when (stringp groups)
1950         (setq groups (list groups)))
1951       (save-excursion
1952         (save-restriction
1953           (message-narrow-to-headers)
1954           (goto-char (point-max))
1955           (insert "Gcc: ")
1956           (if gcc-self-val
1957               ;; Use the `gcc-self' param value instead.
1958               (progn
1959                 (insert
1960                  (if (stringp gcc-self-val)
1961                      (if (string-match " " gcc-self-val)
1962                          (concat "\"" gcc-self-val "\"")
1963                        gcc-self-val)
1964                    ;; In nndoc groups, we use the parent group name
1965                    ;; instead of the current group.
1966                    (let ((group (or (gnus-group-find-parameter
1967                                      gnus-newsgroup-name 'parent-group)
1968                                     group)))
1969                      (if (string-match " " group)
1970                          (concat "\"" group "\"")
1971                        group))))
1972                 (if (not (eq gcc-self-val 'none))
1973                     (insert "\n")
1974                   (gnus-delete-line)))
1975             ;; Use the list of groups.
1976             (while (setq name (pop groups))
1977               (let ((str (if (string-match ":" name)
1978                              name
1979                            (gnus-group-prefixed-name
1980                             name gnus-message-archive-method))))
1981                 (insert (if (string-match " " str)
1982                             (concat "\"" str "\"")
1983                           str)))
1984               (when groups
1985                 (insert " ")))
1986             (insert "\n")))))))
1987
1988 (defun gnus-mailing-list-followup-to ()
1989   "Look at the headers in the current buffer and return a Mail-Followup-To address."
1990   (let ((x-been-there (gnus-fetch-original-field "x-beenthere"))
1991         (list-post (gnus-fetch-original-field "list-post")))
1992     (when (and list-post
1993                (string-match "mailto:\\([^>]+\\)" list-post))
1994       (setq list-post (match-string 1 list-post)))
1995     (or list-post
1996         x-been-there)))
1997
1998 ;;; Posting styles.
1999
2000 (defun gnus-configure-posting-styles (&optional group-name)
2001   "Configure posting styles according to `gnus-posting-styles'."
2002   (unless gnus-inhibit-posting-styles
2003     (let ((group (or group-name gnus-newsgroup-name ""))
2004           (styles gnus-posting-styles)
2005           style match attribute results
2006           name address)
2007       ;; If the group has a posting-style parameter, add it at the end with a
2008       ;; regexp matching everything, to be sure it takes precedence over all
2009       ;; the others.
2010       (when gnus-newsgroup-name
2011         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
2012           (when tmp-style
2013             (setq styles (append styles (list (cons ".*" tmp-style)))))))
2014       ;; Go through all styles and look for matches.
2015       (dolist (style styles)
2016         (setq match (pop style))
2017         (goto-char (point-min))
2018         (when (cond
2019                ((stringp match)
2020                 ;; Regexp string match on the group name.
2021                 (string-match match group))
2022                ((eq match 'header)
2023                 ;; Obsolete format of header match.
2024                 (and (gnus-buffer-live-p gnus-article-copy)
2025                      (with-current-buffer gnus-article-copy
2026                        (let ((header (message-fetch-field (pop style))))
2027                          (and header
2028                               (string-match (pop style) header))))))
2029                ((or (symbolp match)
2030                     (functionp match))
2031                 (cond
2032                  ((functionp match)
2033                   ;; Function to be called.
2034                   (funcall match))
2035                  ((boundp match)
2036                   ;; Variable to be checked.
2037                   (symbol-value match))))
2038                ((listp match)
2039                 (cond
2040                  ((eq (car match) 'header)
2041                   ;; New format of header match.
2042                   (and (gnus-buffer-live-p gnus-article-copy)
2043                        (with-current-buffer gnus-article-copy
2044                          (let ((header (message-fetch-field (nth 1 match))))
2045                            (and header
2046                                 (string-match (nth 2 match) header))))))
2047                  (t
2048                   ;; This is a form to be evaled.
2049                   (eval match)))))
2050           ;; We have a match, so we set the variables.
2051           (setq style (gnus-configure-posting-style style nil))
2052           (dolist (attribute style)
2053             (setq results (delq (assoc (car attribute) results) results))
2054             (push attribute results))))
2055       ;; Now we have all the styles, so we insert them.
2056       (setq name (assq 'name results)
2057             address (assq 'address results))
2058       (setq results (delq name (delq address results)))
2059       (gnus-make-local-hook 'message-setup-hook)
2060       (setq results (sort results (lambda (x y)
2061                                     (string-lessp (car x) (car y)))))
2062       (dolist (result results)
2063         (add-hook 'message-setup-hook
2064                   (cond
2065                    ((eq 'eval (car result))
2066                     'ignore)
2067                    ((eq 'body (car result))
2068                     `(lambda ()
2069                        (save-excursion
2070                          (message-goto-body)
2071                          (insert ,(cdr result)))))
2072                    ((eq 'signature (car result))
2073                     (set (make-local-variable 'message-signature) nil)
2074                     (set (make-local-variable 'message-signature-file) nil)
2075                     (if (not (cdr result))
2076                         'ignore
2077                       `(lambda ()
2078                          (save-excursion
2079                            (let ((message-signature ,(cdr result)))
2080                              (when message-signature
2081                                (message-insert-signature)))))))
2082                    (t
2083                     (let ((header
2084                            (if (symbolp (car result))
2085                                (capitalize (symbol-name (car result)))
2086                              (car result))))
2087                       `(lambda ()
2088                          (save-excursion
2089                            (message-remove-header ,header)
2090                            (let ((value ,(cdr result)))
2091                              (when value
2092                                (message-goto-eoh)
2093                                (insert ,header ": " value)
2094                                (unless (bolp)
2095                                  (insert "\n")))))))))
2096                   nil 'local))
2097       (when (or name address)
2098         (add-hook 'message-setup-hook
2099                   `(lambda ()
2100                      (set (make-local-variable 'user-mail-address)
2101                           ,(or (cdr address) user-mail-address))
2102                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
2103                            (user-mail-address
2104                             ,(or (cdr address) user-mail-address)))
2105                        (save-excursion
2106                          (message-remove-header "From")
2107                          (message-goto-eoh)
2108                          (insert "From: " (message-make-from) "\n"))))
2109                   nil 'local)))))
2110
2111 ;; splitted from gnus-configure-posting-styles to allow recursive traversal.
2112 (defun gnus-configure-posting-style (style stack)
2113   "Parse one posting style STYLE and returns the value as an alist."
2114   (let (results element variable filep value v)
2115     (dolist (attribute style)
2116       (setq element (pop attribute)
2117             variable nil
2118             filep nil)
2119       (setq value
2120             (cond
2121              ((eq (car attribute) ':file)
2122               (setq filep t)
2123               (cadr attribute))
2124              ((eq (car attribute) :value)
2125               (cadr attribute))
2126              (t
2127               (car attribute))))
2128       ;; We get the value.
2129       (setq v
2130             (cond
2131              ((stringp value)
2132               value)
2133              ((or (symbolp value)
2134                   (functionp value))
2135               (cond ((functionp value)
2136                      (funcall value))
2137                     ((boundp value)
2138                      (symbol-value value))))
2139              ((listp value)
2140               (eval value))))
2141       ;; Translate obsolescent value.
2142       (cond
2143        ((eq element 'signature-file)
2144         (setq element 'signature
2145               filep t))
2146        ((eq element 'x-face-file)
2147         (setq element 'x-face
2148               filep t)))
2149       ;; Get the contents of file elems.
2150       (when (and filep v)
2151         (setq v (with-temp-buffer
2152                   (insert-file-contents v)
2153                   (goto-char (point-max))
2154                   (while (bolp)
2155                     (delete-char -1))
2156                   (buffer-string))))
2157       (if (eq element 'import)
2158           (progn
2159             (if (member v stack)
2160                 (error "Circular import of \"%s\"" v))
2161             (setq results
2162                   (nconc (nreverse (gnus-configure-posting-style
2163                                     (cdr (assoc v gnus-named-posting-styles))
2164                                     (cons v stack)))
2165                          results)))
2166         (push (cons element v) results)))
2167     (nreverse results)))
2168
2169 (defun gnus-summary-execute-command-with-posting-style (style command)
2170   "Temporarily select a posting-style named STYLE and execute COMMAND."
2171   (interactive
2172    (let ((style (completing-read "Posting style: "
2173                                  gnus-named-posting-styles nil t)))
2174      (list style
2175            (key-binding
2176             (read-key-sequence
2177              (format "Command to execute with %s: " style))))))
2178   (let ((gnus-posting-styles (list (list ".*" (list 'import style)))))
2179     (call-interactively command)))
2180
2181
2182 ;;; @ for MIME Edit mode
2183 ;;;
2184
2185 (defun gnus-maybe-setup-default-charset ()
2186   (let ((charset
2187          (and (boundp 'gnus-summary-buffer)
2188               (buffer-live-p gnus-summary-buffer)
2189               (save-excursion
2190                 (set-buffer gnus-summary-buffer)
2191                 default-mime-charset))))
2192     (if charset
2193         (progn
2194           (make-local-variable 'default-mime-charset)
2195           (setq default-mime-charset charset)
2196           ))))
2197
2198
2199 ;;; @ for MIME view mode
2200 ;;;
2201
2202 (defun gnus-following-method (buf)
2203   (gnus-setup-message 'reply-yank
2204     (set-buffer buf)
2205     (if (message-news-p)
2206         (message-followup)
2207       (message-reply nil 'wide))
2208     (let ((message-reply-buffer buf))
2209       (message-yank-original))
2210     (message-goto-body))
2211   (kill-buffer buf))
2212
2213
2214 ;;; Allow redefinition of functions.
2215
2216 (gnus-ems-redefine)
2217
2218 (provide 'gnus-msg)
2219
2220 ;;; gnus-msg.el ends here