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