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