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