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