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