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