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