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