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