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