Synch with Oort Gnus.
[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
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 nil, Gnus will
47 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 nil nor `native', 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   :type `(choice (const nil)
58                  (const current)
59                  (const native)
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 '(gnus-maybe-setup-default-charset)
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   :group 'gnus-message
122   :type '(repeat (cons (choice (regexp)
123                                (function)
124                                (variable)
125                                (sexp))
126                        (repeat (list
127                                 (choice (const signature)
128                                         (const signature-file)
129                                         (const organization)
130                                         (const address)
131                                         (const name)
132                                         (const body)
133                                         (string :tag "Header"))
134                                 (choice (string)
135                                         (function)
136                                         (variable)
137                                         (sexp)))))))
138
139 (defcustom gnus-inews-mark-gcc-as-read nil
140   "If non-nil, automatically mark Gcc articles as read."
141   :group 'gnus-message
142   :type 'boolean)
143
144 (defcustom gnus-group-posting-charset-alist
145   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
146     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
147     (message-this-is-mail nil nil)
148     (message-this-is-news nil t))
149   "Alist of regexps and permitted unencoded charsets for posting.
150 Each element of the alist has the form (TEST HEADER BODY-LIST), where
151 TEST is either a regular expression matching the newsgroup header or a
152 variable to query,
153 HEADER is the charset which may be left unencoded in the header (nil
154 means encode all charsets),
155 BODY-LIST is a list of charsets which may be encoded using 8bit
156 content-transfer encoding in the body, or one of the special values
157 nil (always encode using quoted-printable) or t (always use 8bit).
158
159 Note that any value other than nil for HEADER infringes some RFCs, so
160 use this option with care."
161   :type '(repeat (list :tag "Permitted unencoded charsets"
162                        (choice :tag "Where"
163                                (regexp :tag "Group")
164                                (const :tag "Mail message"
165                                       :value message-this-is-mail)
166                                (const :tag "News article"
167                                       :value message-this-is-news))
168                        (choice :tag "Header"
169                                (const :tag "None" nil)
170                                (symbol :tag "Charset"))
171                        (choice :tag "Body"
172                                (const :tag "Any" :value t)
173                                (const :tag "None" :value nil)
174                                (repeat :tag "Charsets"
175                                        (symbol :tag "Charset")))))
176   :group 'gnus-charset)
177
178 ;;; Internal variables.
179
180 (defvar gnus-inhibit-posting-styles nil
181   "Inhibit the use of posting styles.")
182
183 (defvar gnus-message-buffer "*Mail Gnus*")
184 (defvar gnus-article-copy nil)
185 (defvar gnus-check-before-posting nil)
186 (defvar gnus-last-posting-server nil)
187 (defvar gnus-message-group-art nil)
188
189 (defvar gnus-msg-force-broken-reply-to nil)
190
191 (defconst gnus-bug-message
192   (format "Sending a bug report to the Gnus Towers.
193 ========================================
194
195 This gnus is the %s%s.
196 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
197 Developers. (the addresses below are mailing list addresses)
198
199 ========================================
200
201 The buffer below is a mail buffer.  When you press `C-c C-c', it will
202 be sent to the Gnus Bug Exterminators.
203
204 The thing near the bottom of the buffer is how the environment
205 settings will be included in the mail.  Please do not delete that.
206 They will tell the Bug People what your environment is, so that it
207 will be easier to locate the bugs.
208
209 If you have found a bug that makes Emacs go \"beep\", set
210 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
211 and include the backtrace in your bug report.
212
213 Please describe the bug in annoying, painstaking detail.
214
215 Thank you for your help in stamping out bugs.
216 "
217           gnus-product-name
218           (if (string= gnus-product-name "Semi-gnus")
219               ""
220             ", a modified version of Semi-gnus")))
221
222 (eval-and-compile
223   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
224   (autoload 'news-setup "rnewspost")
225   (autoload 'news-reply-mode "rnewspost")
226   (autoload 'rmail-dont-reply-to "mail-utils")
227   (autoload 'rmail-output "rmailout"))
228
229 \f
230 ;;;
231 ;;; Gnus Posting Functions
232 ;;;
233
234 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
235   "p" gnus-summary-post-news
236   "f" gnus-summary-followup
237   "F" gnus-summary-followup-with-original
238   "c" gnus-summary-cancel-article
239   "s" gnus-summary-supersede-article
240   "r" gnus-summary-reply
241   "y" gnus-summary-yank-message
242   "R" gnus-summary-reply-with-original
243   "w" gnus-summary-wide-reply
244   "W" gnus-summary-wide-reply-with-original
245   "v" gnus-summary-very-wide-reply
246   "V" gnus-summary-very-wide-reply-with-original
247   "n" gnus-summary-followup-to-mail
248   "N" gnus-summary-followup-to-mail-with-original
249   "m" gnus-summary-mail-other-window
250   "u" gnus-uu-post-news
251   "\M-c" gnus-summary-mail-crosspost-complaint
252   "Br" gnus-summary-reply-broken-reply-to
253   "BR" gnus-summary-reply-broken-reply-to-with-original
254   "om" gnus-summary-mail-forward
255   "op" gnus-summary-post-forward
256   "Om" gnus-summary-digest-mail-forward
257   "Op" gnus-summary-digest-post-forward)
258
259 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
260   "b" gnus-summary-resend-bounced-mail
261   ;; "c" gnus-summary-send-draft
262   "r" gnus-summary-resend-message)
263
264 ;;; Internal functions.
265
266 (defvar gnus-article-reply nil)
267 (defmacro gnus-setup-message (config &rest forms)
268   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
269         (buffer (make-symbol "gnus-setup-message-buffer"))
270         (article (make-symbol "gnus-setup-message-article"))
271         (group (make-symbol "gnus-setup-message-group")))
272     `(let ((,winconf (current-window-configuration))
273            (,buffer (buffer-name (current-buffer)))
274            (,article gnus-article-reply)
275            (,group gnus-newsgroup-name)
276            (message-header-setup-hook
277             (copy-sequence message-header-setup-hook))
278            (message-mode-hook (copy-sequence message-mode-hook))
279            (message-startup-parameter-alist
280             '((reply-buffer . gnus-copy-article-buffer)
281               (original-buffer . gnus-original-article-buffer)
282               (user-agent . Gnus))))
283        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
284        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
285        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
286        (unwind-protect
287            (progn
288              ,@forms)
289          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config)
290          (gnus-inews-insert-draft-meta-information ,group ,article)
291          (setq gnus-message-buffer (current-buffer))
292          (set (make-local-variable 'gnus-message-group-art)
293               (cons ,group ,article))
294          (set (make-local-variable 'gnus-newsgroup-name) ,group)
295          (gnus-run-hooks 'gnus-message-setup-hook))
296        (gnus-add-buffer)
297        (gnus-configure-windows ,config t)
298        (set-buffer-modified-p nil))))
299
300 (defun gnus-inews-insert-draft-meta-information (group article)
301   (save-excursion
302     (when (and group
303                (not (string= group ""))
304                (not (message-fetch-field gnus-draft-meta-information-header)))
305       (goto-char (point-min))
306       (insert gnus-draft-meta-information-header ": (\"" group "\" "
307               (if article (number-to-string
308                            (if (listp article)
309                                (car article)
310                              article)) "\"\"")
311               ")\n"))))
312
313 ;;;###autoload
314 (defun gnus-msg-mail (&optional to subject other-headers continue
315                                 switch-action yank-action send-actions)
316   "Start editing a mail message to be sent.
317 Like `message-mail', but with Gnus paraphernalia, particularly the
318 Gcc: header for archiving purposes."
319   (interactive)
320   (let ((buf (current-buffer))
321         mail-buf)
322     (gnus-setup-message 'message
323       (message-mail to subject other-headers continue
324                     nil yank-action send-actions))
325     (when switch-action
326       (setq mail-buf (current-buffer))
327       (switch-to-buffer buf)
328       (apply switch-action mail-buf nil)))
329   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
330   t)
331
332 (defvar save-selected-window-window)
333
334 ;;;###autoload
335 (defun gnus-button-mailto (address)
336   "Mail to ADDRESS."
337   (set-buffer (gnus-copy-article-buffer))
338   (gnus-setup-message 'message
339     (message-reply address))
340   (and (boundp 'save-selected-window-window)
341        (not (window-live-p save-selected-window-window))
342        (setq save-selected-window-window (selected-window))))
343
344 ;;;###autoload
345 (defun gnus-button-reply (&optional to-address wide)
346   "Like `message-reply'."
347   (interactive)
348   (gnus-setup-message 'message
349     (message-reply to-address wide))
350   (and (boundp 'save-selected-window-window)
351        (not (window-live-p save-selected-window-window))
352        (setq save-selected-window-window (selected-window))))
353
354 ;;;###autoload
355 (define-mail-user-agent 'gnus-user-agent
356   'gnus-msg-mail 'message-send-and-exit
357   'message-kill-buffer 'message-send-hook)
358
359 (defun gnus-setup-posting-charset (group)
360   (let ((alist gnus-group-posting-charset-alist)
361         (group (or group ""))
362         elem)
363     (when group
364       (catch 'found
365         (while (setq elem (pop alist))
366           (when (or (and (stringp (car elem))
367                          (string-match (car elem) group))
368                     (and (gnus-functionp (car elem))
369                          (funcall (car elem) group))
370                     (and (symbolp (car elem))
371                          (symbol-value (car elem))))
372             (throw 'found (cons (cadr elem) (caddr elem)))))))))
373
374 (defun gnus-inews-add-send-actions (winconf buffer article &optional config)
375   (make-local-hook 'message-sent-hook)
376   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
377                                  'gnus-inews-do-gcc) nil t)
378   (when gnus-agent
379     (make-local-hook 'message-header-hook)
380     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
381   (setq message-post-method
382         `(lambda (arg)
383            (gnus-post-method arg ,gnus-newsgroup-name)))
384   (setq message-user-agent (gnus-extended-version))
385   (when (not message-use-multi-frames)
386     (message-add-action
387      `(set-window-configuration ,winconf) 'exit 'postpone 'kill))
388   (message-add-action
389    `(when (gnus-buffer-exists-p ,buffer)
390       (save-excursion
391         (set-buffer ,buffer)
392         ,(when article
393            (if (eq config 'forward)
394                `(gnus-summary-mark-article-as-forwarded ',article)
395              `(gnus-summary-mark-article-as-replied ',article)))))
396    'send))
397
398 (put 'gnus-setup-message 'lisp-indent-function 1)
399 (put 'gnus-setup-message 'edebug-form-spec '(form body))
400
401 ;;; Post news commands of Gnus group mode and summary mode
402
403 (defun gnus-group-mail (&optional arg)
404   "Start composing a mail.
405 If ARG, use the group under the point to find a posting style.
406 If ARG is 1, prompt for a group name to find the posting style."
407   (interactive "P")
408   ;; We can't `let' gnus-newsgroup-name here, since that leads
409   ;; to local variables leaking.
410   (let ((group gnus-newsgroup-name)
411         (buffer (current-buffer)))
412     (unwind-protect
413         (progn
414           (setq gnus-newsgroup-name
415                 (if arg
416                     (if (= 1 (prefix-numeric-value arg))
417                         (completing-read "Use posting style of group: "
418                                          gnus-active-hashtb nil
419                                          (gnus-read-active-file-p))
420                       (gnus-group-group-name))
421                   ""))
422           (gnus-setup-message 'message (message-mail)))
423       (save-excursion
424         (set-buffer buffer)
425         (setq gnus-newsgroup-name group)))))
426
427 (defun gnus-group-post-news (&optional arg)
428   "Start composing a news message.
429 If ARG, post to the group under point.
430 If ARG is 1, prompt for a group name."
431   (interactive "P")
432   ;; Bind this variable here to make message mode hooks work ok.
433   (let ((gnus-newsgroup-name
434          (if arg
435              (if (= 1 (prefix-numeric-value arg))
436                  (completing-read "Newsgroup: " gnus-active-hashtb nil
437                                   (gnus-read-active-file-p))
438                (gnus-group-group-name))
439            "")))
440     (gnus-post-news 'post gnus-newsgroup-name)))
441
442 (defun gnus-summary-post-news ()
443   "Start composing a news message."
444   (interactive)
445   (gnus-post-news 'post gnus-newsgroup-name))
446
447 (defun gnus-summary-followup (yank &optional force-news)
448   "Compose a followup to an article.
449 If prefix argument YANK is non-nil, original article is yanked automatically."
450   (interactive
451    (list (and current-prefix-arg
452               (gnus-summary-work-articles 1))))
453   (when yank
454     (gnus-summary-goto-subject (car yank)))
455   (save-window-excursion
456     (gnus-summary-select-article))
457   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
458         (gnus-newsgroup-name gnus-newsgroup-name))
459     ;; Send a followup.
460     (gnus-post-news nil gnus-newsgroup-name
461                     headers gnus-article-buffer
462                     yank nil force-news)))
463
464 (defun gnus-summary-followup-with-original (n &optional force-news)
465   "Compose a followup to an article and include the original article."
466   (interactive "P")
467   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
468
469 (defun gnus-summary-followup-to-mail (&optional arg)
470   "Followup to the current mail message via news."
471   (interactive
472    (list (and current-prefix-arg
473               (gnus-summary-work-articles 1))))
474   (gnus-summary-followup arg t))
475
476 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
477   "Followup to the current mail message via news."
478   (interactive "P")
479   (gnus-summary-followup (gnus-summary-work-articles arg) t))
480
481 (defun gnus-inews-yank-articles (articles)
482   (let ((more-than-one (cdr articles))
483         (cur (current-buffer))
484         refs beg article window)
485     (message-goto-body)
486     (while (setq article (pop articles))
487       (save-window-excursion
488         (set-buffer gnus-summary-buffer)
489         (gnus-summary-select-article nil nil nil article)
490         (gnus-summary-remove-process-mark article))
491
492       ;; Gathering references.
493       (when more-than-one
494         (setq refs (message-list-references
495                     refs
496                     (mail-header-references gnus-current-headers)
497                     (mail-header-message-id gnus-current-headers)))
498         (when message-use-multi-frames
499           (when (setq window (get-buffer-window cur t))
500             (select-frame (window-frame window)))))
501
502       (gnus-copy-article-buffer)
503       (let ((message-reply-buffer gnus-article-copy)
504             (message-reply-headers
505              (with-current-buffer gnus-article-copy
506                ;; The headers are decoded.
507                (nnheader-parse-head t))))
508         (message-yank-original)
509         (setq beg (or beg (mark t))))
510       (when articles
511         (insert "\n")))
512     (push-mark)
513
514     ;; Replace with the gathered references.
515     (when refs
516       (push-mark beg)
517       (save-restriction
518         (message-narrow-to-headers)
519         (let ((case-fold-search t))
520           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
521               (replace-match "")
522             (goto-char (point-max))))
523         (mail-header-format
524          (list (or (assq 'References message-header-format-alist)
525                    '(References . message-shorten-references)))
526          (list (cons 'References
527                      (mapconcat 'identity (nreverse refs) " "))))
528         (backward-delete-char 1))
529       (setq beg (mark t))
530       (pop-mark))
531
532     (goto-char beg)))
533
534 (defun gnus-summary-cancel-article (&optional n symp)
535   "Cancel an article you posted.
536 Uses the process-prefix convention.  If given the symbolic
537 prefix `a', cancel using the standard posting method; if not
538 post using the current select method."
539   (interactive (gnus-interactive "P\ny"))
540   (let ((articles (gnus-summary-work-articles n))
541         (message-post-method
542          `(lambda (arg)
543             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
544         article)
545     (while (setq article (pop articles))
546       (when (gnus-summary-select-article t nil nil article)
547         (when (gnus-eval-in-buffer-window gnus-article-buffer
548                 (save-excursion
549                   (set-buffer gnus-original-article-buffer)
550                   (message-cancel-news)))
551           (gnus-summary-mark-as-read article gnus-canceled-mark)
552           (gnus-cache-remove-article 1))
553         (gnus-article-hide-headers-if-wanted))
554       (gnus-summary-remove-process-mark article))))
555
556 (defun gnus-summary-supersede-article ()
557   "Compose an article that will supersede a previous article.
558 This is done simply by taking the old article and adding a Supersedes
559 header line with the old Message-ID."
560   (interactive)
561   (let ((article (gnus-summary-article-number))
562         (gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
563     (gnus-setup-message 'reply-yank
564       (gnus-summary-select-article t)
565       (set-buffer gnus-original-article-buffer)
566       (message-supersede)
567       (push
568        `((lambda ()
569            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
570              (save-excursion
571                (set-buffer ,gnus-summary-buffer)
572                (gnus-cache-possibly-remove-article ,article nil nil nil t)
573                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
574        message-send-actions))))
575
576 \f
577
578 (defun gnus-copy-article-buffer (&optional article-buffer)
579   ;; make a copy of the article buffer with all text properties removed
580   ;; this copy is in the buffer gnus-article-copy.
581   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
582   ;; this buffer should be passed to all mail/news reply/post routines.
583   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
584   (let ((article-buffer (or article-buffer gnus-article-buffer))
585         end beg)
586     (if (not (and (get-buffer article-buffer)
587                   (gnus-buffer-exists-p article-buffer)))
588         (error "Can't find any article buffer")
589       (save-excursion
590         (set-buffer article-buffer)
591         (let ((gnus-newsgroup-charset (or gnus-article-charset
592                                           gnus-newsgroup-charset))
593               (gnus-newsgroup-ignored-charsets
594                (or gnus-article-ignored-charsets
595                    gnus-newsgroup-ignored-charsets)))
596           (save-restriction
597             ;; Copy over the (displayed) article buffer, delete
598             ;; hidden text and remove text properties.
599             (widen)
600             (let ((inhibit-read-only t))
601               (copy-to-buffer gnus-article-copy (point-min) (point-max))
602               (set-buffer gnus-article-copy)
603               ;; Encode bitmap smileys to ordinary text.
604               ;; Possibly, the original text might be restored.
605               (static-unless (featurep 'xemacs)
606                 (when (featurep 'smiley-mule)
607                   (smiley-encode-buffer)))
608               (gnus-article-delete-text-of-type 'annotation)
609               (gnus-remove-text-with-property 'gnus-prev)
610               (gnus-remove-text-with-property 'gnus-next)
611               (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
612               (insert
613                (prog1
614                    (static-if (featurep 'xemacs)
615                        ;; Revome smiley extents for (possibly) XEmacs 21.1.
616                        (format "%s"
617                                (buffer-substring-no-properties (point-min)
618                                                                (point-max)))
619                      (buffer-substring-no-properties (point-min) (point-max)))
620                  (erase-buffer))))
621             ;; Find the original headers.
622             (set-buffer gnus-original-article-buffer)
623             (goto-char (point-min))
624             (while (looking-at message-unix-mail-delimiter)
625               (forward-line 1))
626             (setq beg (point))
627             (setq end (or (message-goto-body) beg))
628             ;; Delete the headers from the displayed articles.
629             (set-buffer gnus-article-copy)
630             (delete-region (goto-char (point-min))
631                            (or (message-goto-body) (point-max)))
632             ;; Insert the original article headers.
633             (insert-buffer-substring gnus-original-article-buffer beg end)
634             ;; Decode charsets.
635             (let ((gnus-article-decode-hook
636                    (delq 'article-decode-charset gnus-article-decode-hook)))
637               ;; Needed for T-gnus.
638               (add-hook 'gnus-article-decode-hook
639                         'article-decode-encoded-words)
640               (run-hooks 'gnus-article-decode-hook)))))
641       gnus-article-copy)))
642
643 (defun gnus-post-news (post &optional group header article-buffer yank subject
644                             force-news)
645   (when article-buffer
646     (gnus-copy-article-buffer))
647   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
648         (add-to-list gnus-add-to-list))
649     (gnus-setup-message (cond (yank 'reply-yank)
650                               (article-buffer 'reply)
651                               (t 'message))
652       (let* ((group (or group gnus-newsgroup-name))
653              (charset (gnus-group-name-charset nil group))
654              (pgroup group)
655              to-address to-group mailing-list to-list
656              newsgroup-p)
657         (when group
658           (setq to-address (gnus-parameter-to-address group)
659                 to-group (gnus-group-find-parameter group 'to-group)
660                 to-list (gnus-parameter-to-list group)
661                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
662                 mailing-list (when gnus-mailing-list-groups
663                                (string-match gnus-mailing-list-groups group))
664                 group (gnus-group-name-decode (gnus-group-real-name group)
665                                               charset)))
666         (if (or (and to-group
667                      (gnus-news-group-p to-group))
668                 newsgroup-p
669                 force-news
670                 (and (gnus-news-group-p
671                       (or pgroup gnus-newsgroup-name)
672                       (if header (mail-header-number header)
673                         gnus-current-article))
674                      (not mailing-list)
675                      (not to-list)
676                      (not to-address)))
677             ;; This is news.
678             (if post
679                 (message-news (or to-group group))
680               (set-buffer gnus-article-copy)
681               (gnus-msg-treat-broken-reply-to)
682               (message-followup (if (or newsgroup-p force-news)
683                                     (if (save-restriction
684                                           (article-narrow-to-head)
685                                           (message-fetch-field "newsgroups"))
686                                         nil
687                                       "")
688                                   to-group)))
689           ;; The is mail.
690           (if post
691               (progn
692                 (message-mail (or to-address to-list))
693                 ;; Arrange for mail groups that have no `to-address' to
694                 ;; get that when the user sends off the mail.
695                 (when (and (not to-list)
696                            (not to-address)
697                            add-to-list)
698                   (push (list 'gnus-inews-add-to-address pgroup)
699                         message-send-actions)))
700             (set-buffer gnus-article-copy)
701             (gnus-msg-treat-broken-reply-to)
702             (message-wide-reply to-address)))
703         (when yank
704           (gnus-inews-yank-articles yank))))))
705
706 (defun gnus-msg-treat-broken-reply-to (&optional force)
707   "Remove the Reply-to header iff broken-reply-to."
708   (when (or force
709             (gnus-group-find-parameter
710              gnus-newsgroup-name 'broken-reply-to))
711     (save-restriction
712       (message-narrow-to-head)
713       (message-remove-header "reply-to"))))
714
715 (defun gnus-post-method (arg group &optional silent)
716   "Return the posting method based on GROUP and ARG.
717 If SILENT, don't prompt the user."
718   (let ((gnus-post-method (or (gnus-parameter-post-method group)
719                               gnus-post-method))
720         (group-method (gnus-find-method-for-group group)))
721     (cond
722      ;; If the group-method is nil (which shouldn't happen) we use
723      ;; the default method.
724      ((null group-method)
725       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
726           gnus-select-method message-post-method))
727      ;; We want the inverse of the default
728      ((and arg (not (eq arg 0)))
729       (if (eq gnus-post-method 'active)
730           gnus-select-method
731         group-method))
732      ;; We query the user for a post method.
733      ((or arg
734           (and gnus-post-method
735                (not (eq gnus-post-method 'current))
736                (listp (car gnus-post-method))))
737       (let* ((methods
738               ;; Collect all methods we know about.
739               (append
740                (when (and gnus-post-method
741                           (not (eq gnus-post-method 'current)))
742                  (if (listp (car gnus-post-method))
743                      gnus-post-method
744                    (list gnus-post-method)))
745                gnus-secondary-select-methods
746                (mapcar 'cdr gnus-server-alist)
747                (mapcar 'car gnus-opened-servers)
748                (list gnus-select-method)
749                (list group-method)))
750              method-alist post-methods method)
751         ;; Weed out all mail methods.
752         (while methods
753           (setq method (gnus-server-get-method "" (pop methods)))
754           (when (and (or (gnus-method-option-p method 'post)
755                          (gnus-method-option-p method 'post-mail))
756                      (not (member method post-methods)))
757             (push method post-methods)))
758         ;; Create a name-method alist.
759         (setq method-alist
760               (mapcar
761                (lambda (m)
762                  (if (equal (cadr m) "")
763                      (list (symbol-name (car m)) m)
764                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
765                post-methods))
766         ;; Query the user.
767         (cadr
768          (assoc
769           (setq gnus-last-posting-server
770                 (if (and silent
771                          gnus-last-posting-server)
772                     ;; Just use the last value.
773                     gnus-last-posting-server
774                   (completing-read
775                    "Posting method: " method-alist nil t
776                    (cons (or gnus-last-posting-server "") 0))))
777           method-alist))))
778      ;; Override normal method.
779      ((and (eq gnus-post-method 'current)
780            (not (eq (car group-method) 'nndraft))
781            (gnus-get-function group-method 'request-post t)
782            (not arg))
783       group-method)
784      ((and gnus-post-method
785            (not (eq gnus-post-method 'current)))
786       gnus-post-method)
787      ;; Use the normal select method.
788      (t gnus-select-method))))
789
790 \f
791 (defun gnus-message-make-user-agent (&optional include-mime-info max-column
792                                                  newline-product)
793   "Return a user-agent info.  If INCLUDE-MIME-INFO is non-nil and the
794 variable `mime-edit-user-agent-value' is bound, the value will be
795 included in the return value.  If MAX-COLUMN is specified, the return
796 value will be folded up as it were filled.  NEWLINE-PRODUCT specifies
797 whether a newline should be inserted in front of each product-token.
798 If the value is t or `hard', it works strictly.  Otherwise, if it is
799 non-nil (e.g. `soft'), it works semi-strictly.
800
801 Here is an example of how to use this function:
802
803 \(add-hook 'gnus-message-setup-hook
804           (lambda nil
805             (setq message-user-agent nil)
806             (save-excursion
807               (save-restriction
808                 (message-narrow-to-headers)
809                 (goto-char (point-max))
810                 (insert \"User-Agent: \"
811                         (gnus-message-make-user-agent t 76 'soft)
812                         \"\\n\")))))
813 "
814   (let ((user-agent (if (and include-mime-info
815                              (boundp 'mime-edit-user-agent-value))
816                         (concat (gnus-extended-version)
817                                 " "
818                                 mime-edit-user-agent-value)
819                       (gnus-extended-version))))
820     (when max-column
821       (unless (natnump max-column)
822         (setq max-column 76))
823       (with-temp-buffer
824         (set-buffer-multibyte t)
825         (insert (mapconcat 'identity (split-string user-agent) " "))
826         (goto-char (point-min))
827         (let ((bol t)
828               start agent agents width element swidth)
829           (while (re-search-forward "\\([^ ]+\\) ?" nil t)
830             (setq start (match-beginning 0))
831             (if (eq (char-after start) ?\()
832                 (progn
833                   (goto-char start)
834                   (forward-list)
835                   (push (buffer-substring start (point)) agent))
836               (when agent
837                 (push (nreverse agent) agents))
838               (setq agent (list (match-string 1)))))
839           (when agent
840             (push (nreverse agent) agents))
841           (setq agents (nreverse agents))
842           (if (> (+ 12 (string-width (caar agents))) max-column)
843               (setq user-agent "\n"
844                     width 0)
845             (setq user-agent ""
846                   width 11))
847           (while agents
848             (setq agent (car agents)
849                   agents (cdr agents))
850             (when (and (not bol)
851                        (or (memq newline-product '(t hard))
852                            (and newline-product
853                                 (> (+ width 1
854                                       (string-width (mapconcat 'identity
855                                                                agent " ")))
856                                    max-column))))
857               (setq user-agent (concat user-agent "\n")
858                     width 0
859                     bol t))
860             (while agent
861               (setq element (car agent)
862                     swidth (string-width element)
863                     agent (cdr agent))
864               (if bol
865                   (setq user-agent (if (member user-agent '("" "\n"))
866                                        (concat user-agent element)
867                                      (concat user-agent " " element))
868                         width (+ width 1 swidth)
869                         bol nil)
870                 (if (> (+ width 1 swidth) max-column)
871                     (setq user-agent (concat user-agent "\n " element)
872                           width (1+ swidth))
873                   (setq user-agent (concat user-agent " " element)
874                         width (+ width 1 swidth)))))))))
875     user-agent))
876
877 \f
878 ;;;
879 ;;; Gnus Mail Functions
880 ;;;
881
882 ;;; Mail reply commands of Gnus summary mode
883
884 (defun gnus-summary-reply (&optional yank wide very-wide)
885   "Start composing a mail reply to the current message.
886 If prefix argument YANK is non-nil, the original article is yanked
887 automatically.
888 If WIDE, make a wide reply.
889 If VERY-WIDE, make a very wide reply."
890   (interactive
891    (list (and current-prefix-arg
892               (gnus-summary-work-articles 1))))
893   ;; Stripping headers should be specified with mail-yank-ignored-headers.
894   (when yank
895     (gnus-summary-goto-subject (car yank)))
896   (let ((gnus-article-reply (or yank (gnus-summary-article-number)))
897         (headers ""))
898     (gnus-setup-message (if yank 'reply-yank 'reply)
899       (if (not very-wide)
900           (gnus-summary-select-article)
901         (dolist (article very-wide)
902           (gnus-summary-select-article nil nil nil article)
903           (save-excursion
904             (set-buffer (gnus-copy-article-buffer))
905             (gnus-msg-treat-broken-reply-to)
906             (save-restriction
907               (message-narrow-to-head)
908               (setq headers (concat headers (buffer-string)))))))
909       (set-buffer (gnus-copy-article-buffer))
910       (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
911       (save-restriction
912         (message-narrow-to-head)
913         (when very-wide
914           (erase-buffer)
915           (insert headers))
916         (goto-char (point-max)))
917       (message-reply nil wide)
918       (when yank
919         (gnus-inews-yank-articles yank)))))
920
921 (defun gnus-summary-reply-with-original (n &optional wide)
922   "Start composing a reply mail to the current message.
923 The original article will be yanked."
924   (interactive "P")
925   (gnus-summary-reply (gnus-summary-work-articles n) wide))
926
927 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
928   "Like `gnus-summary-reply' except removing reply-to field.
929 If prefix argument YANK is non-nil, the original article is yanked
930 automatically.
931 If WIDE, make a wide reply.
932 If VERY-WIDE, make a very wide reply."
933   (interactive
934    (list (and current-prefix-arg
935               (gnus-summary-work-articles 1))))
936   (let ((gnus-msg-force-broken-reply-to t))
937     (gnus-summary-reply yank wide very-wide)))
938
939 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
940   "Like `gnus-summary-reply-with-original' except removing reply-to field.
941 The original article will be yanked."
942   (interactive "P")
943   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
944
945 (defun gnus-summary-wide-reply (&optional yank)
946   "Start composing a wide reply mail to the current message.
947 If prefix argument YANK is non-nil, the original article is yanked
948 automatically."
949   (interactive
950    (list (and current-prefix-arg
951               (gnus-summary-work-articles 1))))
952   (gnus-summary-reply yank t))
953
954 (defun gnus-summary-wide-reply-with-original (n)
955   "Start composing a wide reply mail to the current message.
956 The original article will be yanked."
957   (interactive "P")
958   (gnus-summary-reply-with-original n t))
959
960 (defun gnus-summary-very-wide-reply (&optional yank)
961   "Start composing a very wide reply mail to the current message.
962 If prefix argument YANK is non-nil, the original article is yanked
963 automatically."
964   (interactive
965    (list (and current-prefix-arg
966               (gnus-summary-work-articles 1))))
967   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
968
969 (defun gnus-summary-very-wide-reply-with-original (n)
970   "Start composing a very wide reply mail to the current message.
971 The original article will be yanked."
972   (interactive "P")
973   (gnus-summary-reply
974    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
975
976 (defun gnus-summary-mail-forward (&optional full-headers post)
977   "Forward the current message to another user.
978 If FULL-HEADERS (the prefix), include full headers when forwarding."
979   (interactive "P")
980   (gnus-setup-message 'forward
981     (gnus-summary-select-article)
982     (let ((charset default-mime-charset))
983       (set-buffer gnus-original-article-buffer)
984       (make-local-variable 'default-mime-charset)
985       (setq default-mime-charset charset))
986     (let ((message-included-forward-headers
987            (if full-headers "" message-included-forward-headers)))
988       (message-forward post))))
989
990 (defun gnus-summary-digest-mail-forward (&optional n post)
991   "Digests and forwards all articles in this series.
992 If N is a positive number, forward the N next articles.
993 If N is a negative number, forward the N previous articles.
994 If N is nil and any articles have been marked with the process mark,
995 forward those articles instead.
996 Optional POST will use news to forward instead of mail."
997   (interactive "P")
998   (let ((articles (gnus-summary-work-articles n))
999         (topics "Topics:\n")
1000         subject article frame)
1001     (when (car articles)
1002       (gnus-setup-message 'forward
1003         (gnus-summary-select-article)
1004         (if (cdr articles)
1005             (setq articles (sort articles '<)
1006                   subject "Digested Articles")
1007           (with-current-buffer gnus-original-article-buffer
1008             (setq subject (message-make-forward-subject))))
1009         (if post
1010             (message-news nil subject)
1011           (message-mail nil subject))
1012         (when (and message-use-multi-frames (cdr articles))
1013           (setq frame (window-frame (get-buffer-window (current-buffer)))))
1014         (message-goto-body)
1015         (while (setq article (pop articles))
1016           (save-window-excursion
1017             (set-buffer gnus-summary-buffer)
1018             (gnus-summary-select-article nil nil nil article)
1019             (setq topics (concat topics "    "
1020                                  (mail-header-subject gnus-current-headers)
1021                                  "\n"))
1022             (gnus-summary-remove-process-mark article))
1023           (when frame
1024             (select-frame frame))
1025           (insert (mime-make-tag "message" "rfc822") "\n")
1026           (narrow-to-region (point) (point))
1027           (insert-buffer-substring gnus-original-article-buffer)
1028           (save-restriction
1029             (article-narrow-to-head)
1030             (message-remove-header message-included-forward-headers t nil t))
1031           (goto-char (point-max))
1032           (widen))
1033         (push-mark)
1034         (message-goto-body)
1035         (insert topics)
1036         (message-goto-body)
1037         (mime-edit-enclose-digest-region (point)(mark t))))))
1038
1039 (defun gnus-summary-digest-post-forward (&optional n)
1040   "Digest and forwards all articles in this series to a newsgroup.
1041 If N is a positive number, forward the N next articles.
1042 If N is a negative number, forward the N previous articles.
1043 If N is nil and any articles have been marked with the process mark,
1044 forward those articles instead."
1045   (interactive "P")
1046   (gnus-summary-digest-mail-forward n t))
1047
1048 (defun gnus-summary-resend-message (address n)
1049   "Resend the current article to ADDRESS."
1050   (interactive
1051    (list (message-read-from-minibuffer "Resend message(s) to: ")
1052          current-prefix-arg))
1053   (let ((articles (gnus-summary-work-articles n))
1054         article)
1055     (while (setq article (pop articles))
1056       (gnus-summary-select-article nil nil nil article)
1057       (save-excursion
1058         (set-buffer gnus-original-article-buffer)
1059         (message-resend address))
1060       (gnus-summary-mark-article-as-forwarded article))))
1061
1062 (defun gnus-summary-post-forward (&optional full-headers)
1063   "Forward the current article to a newsgroup.
1064 If FULL-HEADERS (the prefix), include full headers when forwarding."
1065   (interactive "P")
1066   (gnus-summary-mail-forward full-headers t))
1067
1068 (defvar gnus-nastygram-message
1069   "The following article was inappropriately posted to %s.\n\n"
1070   "Format string to insert in nastygrams.
1071 The current group name will be inserted at \"%s\".")
1072
1073 (defun gnus-summary-mail-nastygram (n)
1074   "Send a nastygram to the author of the current article."
1075   (interactive "P")
1076   (when (or gnus-expert-user
1077             (gnus-y-or-n-p
1078              "Really send a nastygram to the author of the current article? "))
1079     (let ((group gnus-newsgroup-name))
1080       (gnus-summary-reply-with-original n)
1081       (set-buffer gnus-message-buffer)
1082       (message-goto-body)
1083       (insert (format gnus-nastygram-message group))
1084       (message-send-and-exit))))
1085
1086 (defun gnus-summary-mail-crosspost-complaint (n)
1087   "Send a complaint about crossposting to the current article(s)."
1088   (interactive "P")
1089   (let ((articles (gnus-summary-work-articles n))
1090         article)
1091     (while (setq article (pop articles))
1092       (set-buffer gnus-summary-buffer)
1093       (gnus-summary-goto-subject article)
1094       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1095             newsgroups followup-to)
1096         (gnus-summary-select-article)
1097         (set-buffer gnus-original-article-buffer)
1098         (if (and (<= (length (message-tokenize-header
1099                               (setq newsgroups
1100                                     (mail-fetch-field "newsgroups"))
1101                               ", "))
1102                      1)
1103                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1104                      (not (member group (message-tokenize-header
1105                                          followup-to ", ")))))
1106             (if followup-to
1107                 (gnus-message 1 "Followup-to restricted")
1108               (gnus-message 1 "Not a crossposted article"))
1109           (set-buffer gnus-summary-buffer)
1110           (gnus-summary-reply-with-original 1)
1111           (set-buffer gnus-message-buffer)
1112           (message-goto-body)
1113           (insert (format gnus-crosspost-complaint newsgroups group))
1114           (message-goto-subject)
1115           (re-search-forward " *$")
1116           (replace-match " (crosspost notification)" t t)
1117           (gnus-deactivate-mark)
1118           (when (gnus-y-or-n-p "Send this complaint? ")
1119             (message-send-and-exit)))))))
1120
1121 (defun gnus-summary-mail-other-window ()
1122   "Compose mail in other window."
1123   (interactive)
1124   (gnus-setup-message 'message
1125     (message-mail)))
1126
1127 (defun gnus-mail-parse-comma-list ()
1128   (let (accumulated
1129         beg)
1130     (skip-chars-forward " ")
1131     (while (not (eobp))
1132       (setq beg (point))
1133       (skip-chars-forward "^,")
1134       (while (zerop
1135               (save-excursion
1136                 (save-restriction
1137                   (let ((i 0))
1138                     (narrow-to-region beg (point))
1139                     (goto-char beg)
1140                     (logand (progn
1141                               (while (search-forward "\"" nil t)
1142                                 (incf i))
1143                               (if (zerop i) 2 i))
1144                             2)))))
1145         (skip-chars-forward ",")
1146         (skip-chars-forward "^,"))
1147       (skip-chars-backward " ")
1148       (push (buffer-substring beg (point))
1149             accumulated)
1150       (skip-chars-forward "^,")
1151       (skip-chars-forward ", "))
1152     accumulated))
1153
1154 (defun gnus-inews-add-to-address (group)
1155   (let ((to-address (mail-fetch-field "to")))
1156     (when (and to-address
1157                (gnus-alive-p))
1158       ;; This mail group doesn't have a `to-list', so we add one
1159       ;; here.  Magic!
1160       (when (gnus-y-or-n-p
1161              (format "Do you want to add this as `to-list': %s " to-address))
1162         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1163
1164 (defun gnus-put-message ()
1165   "Put the current message in some group and return to Gnus."
1166   (interactive)
1167   (let ((reply gnus-article-reply)
1168         (winconf gnus-prev-winconf)
1169         (group gnus-newsgroup-name))
1170     (unless (and group
1171                  (not (gnus-group-read-only-p group)))
1172       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1173
1174     (when (gnus-gethash group gnus-newsrc-hashtb)
1175       (error "No such group: %s" group))
1176     (save-excursion
1177       (save-restriction
1178         (widen)
1179         (message-narrow-to-headers)
1180         (let ((gnus-deletable-headers nil))
1181           (message-generate-headers
1182            (if (message-news-p)
1183                message-required-news-headers
1184              message-required-mail-headers)))
1185         (goto-char (point-max))
1186         (insert "Gcc: " group "\n")
1187         (widen)))
1188     (gnus-inews-do-gcc)
1189     (when (and (get-buffer gnus-group-buffer)
1190                (gnus-buffer-exists-p (car-safe reply))
1191                (cdr reply))
1192       (set-buffer (car reply))
1193       (gnus-summary-mark-article-as-replied (cdr reply)))
1194     (when winconf
1195       (set-window-configuration winconf))))
1196
1197 (defun gnus-article-mail (yank)
1198   "Send a reply to the address near point.
1199 If YANK is non-nil, include the original article."
1200   (interactive "P")
1201   (let ((address
1202          (buffer-substring
1203           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1204           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1205     (when address
1206       (message-reply address)
1207       (when yank
1208         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1209
1210 (defvar nntp-server-type)
1211 (defun gnus-bug ()
1212   "Send a bug report to the Gnus maintainers."
1213   (interactive)
1214   (unless (gnus-alive-p)
1215     (error "Gnus has been shut down"))
1216   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1217     (unless (message-mail-user-agent)
1218       (message-pop-to-buffer "*Gnus Bug*")
1219       (delete-other-windows)
1220       (when gnus-bug-create-help-buffer
1221         (switch-to-buffer "*Gnus Help Bug*")
1222         (erase-buffer)
1223         (insert gnus-bug-message)
1224         (goto-char (point-min))
1225         (sit-for 0)
1226         (set-buffer "*Gnus Bug*")))
1227     (let ((message-this-is-mail t))
1228       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1229     (when gnus-bug-create-help-buffer
1230       (push `(gnus-bug-kill-buffer) message-send-actions))
1231     (goto-char (point-min))
1232     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1233     (forward-line 1)
1234     (insert gnus-product-name " " gnus-version-number
1235             " (r" gnus-revision-number ") "
1236             "based on " gnus-original-product-name " v"
1237             gnus-original-version-number "\n"
1238             (emacs-version) "\n")
1239     (when (and (boundp 'nntp-server-type)
1240                (stringp nntp-server-type))
1241       (insert nntp-server-type))
1242     (insert "\n\n\n\n\n")
1243     (let (mime-content-types)
1244       (mime-edit-insert-tag "text" "plain" "; type=emacs-lisp"))
1245     (insert (with-temp-buffer
1246               (gnus-debug)
1247               (buffer-string)))
1248     (let (mime-content-types)
1249       (mime-edit-insert-tag "text" "plain"))
1250     (goto-char (point-min))
1251     (search-forward "Subject: " nil t)
1252     (message "")))
1253
1254 (defun gnus-bug-kill-buffer ()
1255   (when (get-buffer "*Gnus Help Bug*")
1256     (kill-buffer "*Gnus Help Bug*")))
1257
1258 (defun gnus-summary-yank-message (buffer n)
1259   "Yank the current article into a composed message."
1260   (interactive
1261    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1262          current-prefix-arg))
1263   (when (gnus-buffer-live-p buffer)
1264     (let ((summary-frame (selected-frame))
1265           (message-frame (when (static-if (featurep 'xemacs)
1266                                    (device-on-window-system-p)
1267                                  window-system)
1268                            (let ((window (get-buffer-window buffer t)))
1269                              (when window
1270                                (window-frame window)))))
1271           (separator (concat "^" (regexp-quote mail-header-separator)
1272                              "\n")))
1273       (gnus-summary-iterate n
1274         (gnus-summary-select-article)
1275         (gnus-copy-article-buffer)
1276         (when (frame-live-p message-frame)
1277           (raise-frame message-frame)
1278           (select-frame message-frame))
1279         (with-current-buffer buffer
1280           (when (save-excursion
1281                   (beginning-of-line)
1282                   (let (case-fold-search)
1283                     (and (not (re-search-backward separator nil t))
1284                          (re-search-forward separator nil t))))
1285             (goto-char (match-end 0)))
1286           (message-yank-buffer gnus-article-copy))
1287         (select-frame summary-frame))
1288       (when (frame-live-p message-frame)
1289         (select-frame message-frame)))))
1290
1291 (defun gnus-debug ()
1292   "Attempts to go through the Gnus source file and report what variables have been changed.
1293 The source file has to be in the Emacs load path."
1294   (interactive)
1295   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1296                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1297                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1298                  "nnmail.el" "nntp.el" "message.el"))
1299         (point (point))
1300         file expr olist sym)
1301     (gnus-message 4 "Please wait while we snoop your variables...")
1302     ;; Go through all the files looking for non-default values for variables.
1303     (save-excursion
1304       (sit-for 0)
1305       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1306       (while files
1307         (erase-buffer)
1308         (when (and (setq file (locate-library (pop files)))
1309                    (file-exists-p file))
1310           (insert-file-contents file)
1311           (goto-char (point-min))
1312           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1313               (gnus-message 4 "Malformed sources in file %s" file)
1314             (narrow-to-region (point-min) (point))
1315             (goto-char (point-min))
1316             (while (setq expr (ignore-errors (read (current-buffer))))
1317               (ignore-errors
1318                 (and (memq (car expr) '(defvar defcustom defvoo))
1319                      (stringp (nth 3 expr))
1320                      (or (not (boundp (nth 1 expr)))
1321                          (not (equal (eval (nth 2 expr))
1322                                      (symbol-value (nth 1 expr)))))
1323                      (push (nth 1 expr) olist)))))))
1324       (kill-buffer (current-buffer)))
1325     (when (setq olist (nreverse olist))
1326       (insert ";----------------- Environment follows ------------------\n\n"))
1327     (while olist
1328       (if (boundp (car olist))
1329           (condition-case ()
1330               (pp `(setq ,(car olist)
1331                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1332                                   (and (symbolp sym)
1333                                        (not (or (eq sym nil)
1334                                                 (eq sym t)))))
1335                               (list 'quote (symbol-value (car olist)))
1336                             (symbol-value (car olist))))
1337                   (current-buffer))
1338             (error
1339              (format "(setq %s 'whatever)\n" (car olist))))
1340         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1341       (setq olist (cdr olist)))
1342     ;; Remove any control chars - they seem to cause trouble for some
1343     ;; mailers.  (Byte-compiled output from the stuff above.)
1344     (goto-char point)
1345     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1346       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1347                      t t))
1348     ;; Break MIME tags purposely.
1349     (goto-char point)
1350     (while (re-search-forward mime-edit-tag-regexp nil t)
1351       (goto-char (1+ (match-beginning 0)))
1352       (insert "X"))))
1353
1354 ;;; Treatment of rejected articles.
1355 ;;; Bounced mail.
1356
1357 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1358   "Re-mail the current message.
1359 This only makes sense if the current message is a bounce message than
1360 contains some mail you have written which has been bounced back to
1361 you.
1362 If FETCH, try to fetch the article that this is a reply to, if indeed
1363 this is a reply."
1364   (interactive "P")
1365   (gnus-summary-select-article t)
1366   (set-buffer gnus-original-article-buffer)
1367   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1368     (gnus-setup-message 'compose-bounce
1369       (let* ((references (mail-fetch-field "references"))
1370              (parent (and references (gnus-parent-id references))))
1371         (message-bounce)
1372         ;; If there are references, we fetch the article we answered to.
1373         (and fetch parent
1374              (gnus-summary-refer-article parent)
1375              (gnus-summary-show-all-headers))))))
1376
1377 ;;; Gcc handling.
1378
1379 (defun gnus-inews-group-method (group)
1380   (cond
1381    ;; If the group doesn't exist, we assume
1382    ;; it's an archive group...
1383    ((and (null (gnus-get-info group))
1384          (eq (car (gnus-server-to-method gnus-message-archive-method))
1385              (car (gnus-server-to-method (gnus-group-method group)))))
1386     gnus-message-archive-method)
1387    ;; Use the method.
1388    ((gnus-info-method (gnus-get-info group))
1389     (gnus-info-method (gnus-get-info group)))
1390    ;; Find the method.
1391    (t (gnus-server-to-method (gnus-group-method group)))))
1392
1393 ;; Do Gcc handling, which copied the message over to some group.
1394 (defun gnus-inews-do-gcc (&optional gcc)
1395   (interactive)
1396   (save-excursion
1397     (save-restriction
1398       (message-narrow-to-headers)
1399       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1400             (coding-system-for-write 'raw-text)
1401             (output-coding-system 'raw-text)
1402             groups group method group-art)
1403         (when gcc
1404           (message-remove-header "gcc")
1405           (widen)
1406           (setq groups (message-unquote-tokens
1407                         (message-tokenize-header gcc " ,")))
1408           ;; Copy the article over to some group(s).
1409           (while (setq group (pop groups))
1410             (unless (gnus-check-server
1411                      (setq method (gnus-inews-group-method group)))
1412               (error "Can't open server %s" (if (stringp method) method
1413                                               (car method))))
1414             (unless (gnus-request-group group nil method)
1415               (gnus-request-create-group group method))
1416             (save-excursion
1417               (nnheader-set-temp-buffer " *acc*")
1418               (insert-buffer-substring message-encoding-buffer)
1419               (gnus-run-hooks 'gnus-before-do-gcc-hook)
1420               (goto-char (point-min))
1421               (when (re-search-forward
1422                      (concat "^" (regexp-quote mail-header-separator) "$")
1423                      nil t)
1424                 (replace-match "" t t ))
1425               (unless (setq group-art
1426                             (gnus-request-accept-article group method t t))
1427                 (gnus-message 1 "Couldn't store article in group %s: %s"
1428                               group (gnus-status-message method))
1429                 (sit-for 2))
1430               (when (and group-art gnus-inews-mark-gcc-as-read)
1431                 (gnus-group-mark-article-read group (cdr group-art)))
1432               (kill-buffer (current-buffer)))))))))
1433
1434 (defun gnus-inews-insert-gcc ()
1435   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1436   (save-excursion
1437     (save-restriction
1438       (message-narrow-to-headers)
1439       (let* ((group gnus-outgoing-message-group)
1440              (gcc (cond
1441                    ((gnus-functionp group)
1442                     (funcall group))
1443                    ((or (stringp group) (list group))
1444                     group))))
1445         (when gcc
1446           (insert "Gcc: "
1447                   (if (stringp gcc) gcc
1448                     (mapconcat 'identity gcc " "))
1449                   "\n"))))))
1450
1451 (defun gnus-inews-insert-archive-gcc (&optional group)
1452   "Insert the Gcc to say where the article is to be archived."
1453   (let* ((var gnus-message-archive-group)
1454          (group (or group gnus-newsgroup-name ""))
1455          (gcc-self-val
1456           (and gnus-newsgroup-name
1457                (not (equal gnus-newsgroup-name ""))
1458                (gnus-group-find-parameter
1459                 gnus-newsgroup-name 'gcc-self)))
1460          result
1461          (groups
1462           (cond
1463            ((null gnus-message-archive-method)
1464             ;; Ignore.
1465             nil)
1466            ((stringp var)
1467             ;; Just a single group.
1468             (list var))
1469            ((null var)
1470             ;; We don't want this.
1471             nil)
1472            ((and (listp var) (stringp (car var)))
1473             ;; A list of groups.
1474             var)
1475            ((gnus-functionp var)
1476             ;; A function.
1477             (funcall var group))
1478            (t
1479             ;; An alist of regexps/functions/forms.
1480             (while (and var
1481                         (not
1482                          (setq result
1483                                (cond
1484                                 ((stringp (caar var))
1485                                  ;; Regexp.
1486                                  (when (string-match (caar var) group)
1487                                    (cdar var)))
1488                                 ((gnus-functionp (car var))
1489                                  ;; Function.
1490                                  (funcall (car var) group))
1491                                 (t
1492                                  (eval (car var)))))))
1493               (setq var (cdr var)))
1494             result)))
1495          name)
1496     (when (or groups gcc-self-val)
1497       (when (stringp groups)
1498         (setq groups (list groups)))
1499       (save-excursion
1500         (save-restriction
1501           (message-narrow-to-headers)
1502           (goto-char (point-max))
1503           (insert "Gcc: ")
1504           (if gcc-self-val
1505               ;; Use the `gcc-self' param value instead.
1506               (progn
1507                 (insert
1508                  (if (stringp gcc-self-val)
1509                      gcc-self-val
1510                    group))
1511                 (if (not (eq gcc-self-val 'none))
1512                     (insert "\n")
1513                   (progn
1514                     (beginning-of-line)
1515                     (kill-line))))
1516             ;; Use the list of groups.
1517             (while (setq name (pop groups))
1518               (insert (if (string-match ":" name)
1519                           name
1520                         (gnus-group-prefixed-name
1521                          name gnus-message-archive-method)))
1522               (when groups
1523                 (insert " ")))
1524             (insert "\n")))))))
1525
1526 ;;; Posting styles.
1527
1528 (defun gnus-configure-posting-styles ()
1529   "Configure posting styles according to `gnus-posting-styles'."
1530   (unless gnus-inhibit-posting-styles
1531     (let ((group (or gnus-newsgroup-name ""))
1532           (styles gnus-posting-styles)
1533           style match variable attribute value v results
1534           filep name address element)
1535       ;; If the group has a posting-style parameter, add it at the end with a
1536       ;; regexp matching everything, to be sure it takes precedence over all
1537       ;; the others.
1538       (when gnus-newsgroup-name
1539         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1540           (when tmp-style
1541             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1542       ;; Go through all styles and look for matches.
1543       (dolist (style styles)
1544         (setq match (pop style))
1545         (goto-char (point-min))
1546         (when (cond
1547                ((stringp match)
1548                 ;; Regexp string match on the group name.
1549                 (string-match match group))
1550                ((eq match 'header)
1551                 (and (gnus-buffer-live-p gnus-article-copy)
1552                      (with-current-buffer gnus-article-copy
1553                        (let ((header (message-fetch-field (pop style))))
1554                          (and header
1555                               (string-match (pop style) header))))))
1556                ((or (symbolp match)
1557                     (gnus-functionp match))
1558                 (cond
1559                  ((gnus-functionp match)
1560                   ;; Function to be called.
1561                   (funcall match))
1562                  ((boundp match)
1563                   ;; Variable to be checked.
1564                   (symbol-value match))))
1565                ((listp match)
1566                 ;; This is a form to be evaled.
1567                 (eval match)))
1568           ;; We have a match, so we set the variables.
1569           (dolist (attribute style)
1570             (setq element (pop attribute)
1571                   variable nil
1572                   filep nil)
1573             (setq value
1574                   (cond
1575                    ((eq (car attribute) ':file)
1576                     (setq filep t)
1577                     (cadr attribute))
1578                    ((eq (car attribute) :value)
1579                     (cadr attribute))
1580                    (t
1581                     (car attribute))))
1582             ;; We get the value.
1583             (setq v
1584                   (cond
1585                    ((stringp value)
1586                     value)
1587                    ((or (symbolp value)
1588                         (gnus-functionp value))
1589                     (cond ((gnus-functionp value)
1590                            (funcall value))
1591                           ((boundp value)
1592                            (symbol-value value))))
1593                    ((listp value)
1594                     (eval value))))
1595             ;; Translate obsolescent value.
1596             (when (eq element 'signature-file)
1597               (setq element 'signature
1598                     filep t))
1599             ;; Get the contents of file elems.
1600             (when (and filep v)
1601               (setq v (with-temp-buffer
1602                         (insert-file-contents v)
1603                         (buffer-string))))
1604             (setq results (delq (assoc element results) results))
1605             (push (cons element v) results))))
1606       ;; Now we have all the styles, so we insert them.
1607       (setq name (assq 'name results)
1608             address (assq 'address results))
1609       (setq results (delq name (delq address results)))
1610       (make-local-variable 'message-setup-hook)
1611       (dolist (result results)
1612         (add-hook 'message-setup-hook
1613                   (cond
1614                    ((eq 'eval (car result))
1615                     'ignore)
1616                    ((eq 'body (car result))
1617                     `(lambda ()
1618                        (save-excursion
1619                          (message-goto-body)
1620                          (insert ,(cdr result)))))
1621                    ((eq 'signature (car result))
1622                     (set (make-local-variable 'message-signature) nil)
1623                     (set (make-local-variable 'message-signature-file) nil)
1624                     (if (not (cdr result))
1625                         'ignore
1626                       `(lambda ()
1627                          (save-excursion
1628                            (let ((message-signature ,(cdr result)))
1629                              (when message-signature
1630                                (message-insert-signature)))))))
1631                    (t
1632                     (let ((header
1633                            (if (symbolp (car result))
1634                                (capitalize (symbol-name (car result)))
1635                              (car result))))
1636                       `(lambda ()
1637                          (save-excursion
1638                            (message-remove-header ,header)
1639                            (let ((value ,(cdr result)))
1640                              (when value
1641                                (message-goto-eoh)
1642                                (insert ,header ": " value "\n"))))))))))
1643       (when (or name address)
1644         (add-hook 'message-setup-hook
1645                   `(lambda ()
1646                      (set (make-local-variable 'user-mail-address)
1647                           ,(or (cdr address) user-mail-address))
1648                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1649                            (user-mail-address
1650                             ,(or (cdr address) user-mail-address)))
1651                        (save-excursion
1652                          (message-remove-header "From")
1653                          (message-goto-eoh)
1654                          (insert "From: " (message-make-from) "\n")))))))))
1655
1656
1657 ;;; @ for MIME Edit mode
1658 ;;;
1659
1660 (defun gnus-maybe-setup-default-charset ()
1661   (let ((charset
1662          (and (boundp 'gnus-summary-buffer)
1663               (buffer-live-p gnus-summary-buffer)
1664               (save-excursion
1665                 (set-buffer gnus-summary-buffer)
1666                 default-mime-charset))))
1667     (if charset
1668         (progn
1669           (make-local-variable 'default-mime-charset)
1670           (setq default-mime-charset charset)
1671           ))))
1672
1673
1674 ;;; @ for MIME view mode
1675 ;;;
1676
1677 (defun gnus-following-method (buf)
1678   (gnus-setup-message 'reply-yank
1679     (set-buffer buf)
1680     (if (message-news-p)
1681         (message-followup)
1682       (message-reply nil 'wide))
1683     (let ((message-reply-buffer buf))
1684       (message-yank-original))
1685     (message-goto-body))
1686   (kill-buffer buf))
1687
1688
1689 ;;; Allow redefinition of functions.
1690
1691 (gnus-ems-redefine)
1692
1693 (provide 'gnus-msg)
1694
1695 ;;; gnus-msg.el ends here