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