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