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 `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 '(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                       (or header gnus-current-article))
673                      (not mailing-list)
674                      (not to-list)
675                      (not to-address)))
676             ;; This is news.
677             (if post
678                 (message-news (or to-group group))
679               (set-buffer gnus-article-copy)
680               (gnus-msg-treat-broken-reply-to)
681               (message-followup (if (or newsgroup-p force-news)
682                                     (if (save-restriction
683                                           (article-narrow-to-head)
684                                           (message-fetch-field "newsgroups"))
685                                         nil
686                                       "")
687                                   to-group)))
688           ;; The is mail.
689           (if post
690               (progn
691                 (message-mail (or to-address to-list))
692                 ;; Arrange for mail groups that have no `to-address' to
693                 ;; get that when the user sends off the mail.
694                 (when (and (not to-list)
695                            (not to-address)
696                            add-to-list)
697                   (push (list 'gnus-inews-add-to-address pgroup)
698                         message-send-actions)))
699             (set-buffer gnus-article-copy)
700             (gnus-msg-treat-broken-reply-to)
701             (message-wide-reply to-address)))
702         (when yank
703           (gnus-inews-yank-articles yank))))))
704
705 (defun gnus-msg-treat-broken-reply-to (&optional force)
706   "Remove the Reply-to header iff broken-reply-to."
707   (when (or force
708             (gnus-group-find-parameter
709              gnus-newsgroup-name 'broken-reply-to))
710     (save-restriction
711       (message-narrow-to-head)
712       (message-remove-header "reply-to"))))
713
714 (defun gnus-post-method (arg group &optional silent)
715   "Return the posting method based on GROUP and ARG.
716 If SILENT, don't prompt the user."
717   (let ((gnus-post-method (or (gnus-parameter-post-method group)
718                               gnus-post-method))
719         (group-method (gnus-find-method-for-group group)))
720     (cond
721      ;; If the group-method is nil (which shouldn't happen) we use
722      ;; the default method.
723      ((null group-method)
724       (or (and (listp gnus-post-method) ;If not current/native/nil
725                (not (listp (car gnus-post-method))) ; and not a list of methods
726                gnus-post-method)        ;then use it.
727           gnus-select-method 
728           message-post-method))
729      ;; We want the inverse of the default
730      ((and arg (not (eq arg 0)))
731       (if (eq gnus-post-method 'current)
732           gnus-select-method
733         group-method))
734      ;; We query the user for a post method.
735      ((or arg
736           (and (listp gnus-post-method)
737                (listp (car gnus-post-method))))
738       (let* ((methods
739               ;; Collect all methods we know about.
740               (append
741                (when (listp gnus-post-method)
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       (assert (not arg))
783       group-method)
784      ;; Use gnus-post-method.
785      ((listp gnus-post-method)          ;A method...
786       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
787       gnus-post-method)
788      ;; Use the normal select method (nil or native).
789      (t gnus-select-method))))
790
791 \f
792 (defun gnus-message-make-user-agent (&optional include-mime-info max-column
793                                                  newline-product)
794   "Return a user-agent info.  If INCLUDE-MIME-INFO is non-nil and the
795 variable `mime-edit-user-agent-value' is bound, the value will be
796 included in the return value.  If MAX-COLUMN is specified, the return
797 value will be folded up as it were filled.  NEWLINE-PRODUCT specifies
798 whether a newline should be inserted in front of each product-token.
799 If the value is t or `hard', it works strictly.  Otherwise, if it is
800 non-nil (e.g. `soft'), it works semi-strictly.
801
802 Here is an example of how to use this function:
803
804 \(add-hook 'gnus-message-setup-hook
805           (lambda nil
806             (setq message-user-agent nil)
807             (save-excursion
808               (save-restriction
809                 (message-narrow-to-headers)
810                 (goto-char (point-max))
811                 (insert \"User-Agent: \"
812                         (gnus-message-make-user-agent t 76 'soft)
813                         \"\\n\")))))
814 "
815   (let ((user-agent (if (and include-mime-info
816                              (boundp 'mime-edit-user-agent-value))
817                         (concat (gnus-extended-version)
818                                 " "
819                                 mime-edit-user-agent-value)
820                       (gnus-extended-version))))
821     (when max-column
822       (unless (natnump max-column)
823         (setq max-column 76))
824       (with-temp-buffer
825         (set-buffer-multibyte t)
826         (insert (mapconcat 'identity (split-string user-agent) " "))
827         (goto-char (point-min))
828         (let ((bol t)
829               start agent agents width element swidth)
830           (while (re-search-forward "\\([^ ]+\\) ?" nil t)
831             (setq start (match-beginning 0))
832             (if (eq (char-after start) ?\()
833                 (progn
834                   (goto-char start)
835                   (forward-list)
836                   (push (buffer-substring start (point)) agent))
837               (when agent
838                 (push (nreverse agent) agents))
839               (setq agent (list (match-string 1)))))
840           (when agent
841             (push (nreverse agent) agents))
842           (setq agents (nreverse agents))
843           (if (> (+ 12 (string-width (caar agents))) max-column)
844               (setq user-agent "\n"
845                     width 0)
846             (setq user-agent ""
847                   width 11))
848           (while agents
849             (setq agent (car agents)
850                   agents (cdr agents))
851             (when (and (not bol)
852                        (or (memq newline-product '(t hard))
853                            (and newline-product
854                                 (> (+ width 1
855                                       (string-width (mapconcat 'identity
856                                                                agent " ")))
857                                    max-column))))
858               (setq user-agent (concat user-agent "\n")
859                     width 0
860                     bol t))
861             (while agent
862               (setq element (car agent)
863                     swidth (string-width element)
864                     agent (cdr agent))
865               (if bol
866                   (setq user-agent (if (member user-agent '("" "\n"))
867                                        (concat user-agent element)
868                                      (concat user-agent " " element))
869                         width (+ width 1 swidth)
870                         bol nil)
871                 (if (> (+ width 1 swidth) max-column)
872                     (setq user-agent (concat user-agent "\n " element)
873                           width (1+ swidth))
874                   (setq user-agent (concat user-agent " " element)
875                         width (+ width 1 swidth)))))))))
876     user-agent))
877
878 \f
879 ;;;
880 ;;; Gnus Mail Functions
881 ;;;
882
883 ;;; Mail reply commands of Gnus summary mode
884
885 (defun gnus-summary-reply (&optional yank wide very-wide)
886   "Start composing a mail reply to the current message.
887 If prefix argument YANK is non-nil, the original article is yanked
888 automatically.
889 If WIDE, make a wide reply.
890 If VERY-WIDE, make a very wide reply."
891   (interactive
892    (list (and current-prefix-arg
893               (gnus-summary-work-articles 1))))
894   ;; Stripping headers should be specified with mail-yank-ignored-headers.
895   (when yank
896     (gnus-summary-goto-subject (car yank)))
897   (let ((gnus-article-reply (or yank (gnus-summary-article-number)))
898         (headers ""))
899     (gnus-setup-message (if yank 'reply-yank 'reply)
900       (if (not very-wide)
901           (gnus-summary-select-article)
902         (dolist (article very-wide)
903           (gnus-summary-select-article nil nil nil article)
904           (save-excursion
905             (set-buffer (gnus-copy-article-buffer))
906             (gnus-msg-treat-broken-reply-to)
907             (save-restriction
908               (message-narrow-to-head)
909               (setq headers (concat headers (buffer-string)))))))
910       (set-buffer (gnus-copy-article-buffer))
911       (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
912       (save-restriction
913         (message-narrow-to-head)
914         (when very-wide
915           (erase-buffer)
916           (insert headers))
917         (goto-char (point-max)))
918       (message-reply nil wide)
919       (when yank
920         (gnus-inews-yank-articles yank)))))
921
922 (defun gnus-summary-reply-with-original (n &optional wide)
923   "Start composing a reply mail to the current message.
924 The original article will be yanked."
925   (interactive "P")
926   (gnus-summary-reply (gnus-summary-work-articles n) wide))
927
928 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
929   "Like `gnus-summary-reply' except removing reply-to field.
930 If prefix argument YANK is non-nil, the original article is yanked
931 automatically.
932 If WIDE, make a wide reply.
933 If VERY-WIDE, make a very wide reply."
934   (interactive
935    (list (and current-prefix-arg
936               (gnus-summary-work-articles 1))))
937   (let ((gnus-msg-force-broken-reply-to t))
938     (gnus-summary-reply yank wide very-wide)))
939
940 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
941   "Like `gnus-summary-reply-with-original' except removing reply-to field.
942 The original article will be yanked."
943   (interactive "P")
944   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
945
946 (defun gnus-summary-wide-reply (&optional yank)
947   "Start composing a wide reply mail to the current message.
948 If prefix argument YANK is non-nil, the original article is yanked
949 automatically."
950   (interactive
951    (list (and current-prefix-arg
952               (gnus-summary-work-articles 1))))
953   (gnus-summary-reply yank t))
954
955 (defun gnus-summary-wide-reply-with-original (n)
956   "Start composing a wide reply mail to the current message.
957 The original article will be yanked."
958   (interactive "P")
959   (gnus-summary-reply-with-original n t))
960
961 (defun gnus-summary-very-wide-reply (&optional yank)
962   "Start composing a very wide reply mail to the current message.
963 If prefix argument YANK is non-nil, the original article is yanked
964 automatically."
965   (interactive
966    (list (and current-prefix-arg
967               (gnus-summary-work-articles 1))))
968   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
969
970 (defun gnus-summary-very-wide-reply-with-original (n)
971   "Start composing a very wide reply mail to the current message.
972 The original article will be yanked."
973   (interactive "P")
974   (gnus-summary-reply
975    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
976
977 (defun gnus-summary-mail-forward (&optional full-headers post)
978   "Forward the current message to another user.
979 If FULL-HEADERS (the prefix), include full headers when forwarding."
980   (interactive "P")
981   (gnus-setup-message 'forward
982     (gnus-summary-select-article)
983     (let ((charset default-mime-charset))
984       (set-buffer gnus-original-article-buffer)
985       (make-local-variable 'default-mime-charset)
986       (setq default-mime-charset charset))
987     (let ((message-included-forward-headers
988            (if full-headers "" message-included-forward-headers)))
989       (message-forward post))))
990
991 (defun gnus-summary-digest-mail-forward (&optional n post)
992   "Digests and forwards all articles in this series.
993 If N is a positive number, forward the N next articles.
994 If N is a negative number, forward the N previous articles.
995 If N is nil and any articles have been marked with the process mark,
996 forward those articles instead.
997 Optional POST will use news to forward instead of mail."
998   (interactive "P")
999   (let ((articles (gnus-summary-work-articles n))
1000         (topics "Topics:\n")
1001         subject article frame)
1002     (when (car articles)
1003       (gnus-setup-message 'forward
1004         (gnus-summary-select-article)
1005         (if (cdr articles)
1006             (setq articles (sort articles '<)
1007                   subject "Digested Articles")
1008           (with-current-buffer gnus-original-article-buffer
1009             (setq subject (message-make-forward-subject))))
1010         (if post
1011             (message-news nil subject)
1012           (message-mail nil subject))
1013         (when (and message-use-multi-frames (cdr articles))
1014           (setq frame (window-frame (get-buffer-window (current-buffer)))))
1015         (message-goto-body)
1016         (while (setq article (pop articles))
1017           (save-window-excursion
1018             (set-buffer gnus-summary-buffer)
1019             (gnus-summary-select-article nil nil nil article)
1020             (setq topics (concat topics "    "
1021                                  (mail-header-subject gnus-current-headers)
1022                                  "\n"))
1023             (gnus-summary-remove-process-mark article))
1024           (when frame
1025             (select-frame frame))
1026           (insert (mime-make-tag "message" "rfc822") "\n")
1027           (narrow-to-region (point) (point))
1028           (insert-buffer-substring gnus-original-article-buffer)
1029           (save-restriction
1030             (article-narrow-to-head)
1031             (message-remove-header message-included-forward-headers t nil t))
1032           (goto-char (point-max))
1033           (widen))
1034         (push-mark)
1035         (message-goto-body)
1036         (insert topics)
1037         (message-goto-body)
1038         (mime-edit-enclose-digest-region (point)(mark t))))))
1039
1040 (defun gnus-summary-digest-post-forward (&optional n)
1041   "Digest and forwards all articles in this series to a newsgroup.
1042 If N is a positive number, forward the N next articles.
1043 If N is a negative number, forward the N previous articles.
1044 If N is nil and any articles have been marked with the process mark,
1045 forward those articles instead."
1046   (interactive "P")
1047   (gnus-summary-digest-mail-forward n t))
1048
1049 (defun gnus-summary-resend-message (address n)
1050   "Resend the current article to ADDRESS."
1051   (interactive
1052    (list (message-read-from-minibuffer "Resend message(s) to: ")
1053          current-prefix-arg))
1054   (let ((articles (gnus-summary-work-articles n))
1055         article)
1056     (while (setq article (pop articles))
1057       (gnus-summary-select-article nil nil nil article)
1058       (save-excursion
1059         (set-buffer gnus-original-article-buffer)
1060         (message-resend address))
1061       (gnus-summary-mark-article-as-forwarded article))))
1062
1063 (defun gnus-summary-post-forward (&optional full-headers)
1064   "Forward the current article to a newsgroup.
1065 If FULL-HEADERS (the prefix), include full headers when forwarding."
1066   (interactive "P")
1067   (gnus-summary-mail-forward full-headers t))
1068
1069 (defvar gnus-nastygram-message
1070   "The following article was inappropriately posted to %s.\n\n"
1071   "Format string to insert in nastygrams.
1072 The current group name will be inserted at \"%s\".")
1073
1074 (defun gnus-summary-mail-nastygram (n)
1075   "Send a nastygram to the author of the current article."
1076   (interactive "P")
1077   (when (or gnus-expert-user
1078             (gnus-y-or-n-p
1079              "Really send a nastygram to the author of the current article? "))
1080     (let ((group gnus-newsgroup-name))
1081       (gnus-summary-reply-with-original n)
1082       (set-buffer gnus-message-buffer)
1083       (message-goto-body)
1084       (insert (format gnus-nastygram-message group))
1085       (message-send-and-exit))))
1086
1087 (defun gnus-summary-mail-crosspost-complaint (n)
1088   "Send a complaint about crossposting to the current article(s)."
1089   (interactive "P")
1090   (let ((articles (gnus-summary-work-articles n))
1091         article)
1092     (while (setq article (pop articles))
1093       (set-buffer gnus-summary-buffer)
1094       (gnus-summary-goto-subject article)
1095       (let ((group (gnus-group-real-name gnus-newsgroup-name))
1096             newsgroups followup-to)
1097         (gnus-summary-select-article)
1098         (set-buffer gnus-original-article-buffer)
1099         (if (and (<= (length (message-tokenize-header
1100                               (setq newsgroups
1101                                     (mail-fetch-field "newsgroups"))
1102                               ", "))
1103                      1)
1104                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
1105                      (not (member group (message-tokenize-header
1106                                          followup-to ", ")))))
1107             (if followup-to
1108                 (gnus-message 1 "Followup-to restricted")
1109               (gnus-message 1 "Not a crossposted article"))
1110           (set-buffer gnus-summary-buffer)
1111           (gnus-summary-reply-with-original 1)
1112           (set-buffer gnus-message-buffer)
1113           (message-goto-body)
1114           (insert (format gnus-crosspost-complaint newsgroups group))
1115           (message-goto-subject)
1116           (re-search-forward " *$")
1117           (replace-match " (crosspost notification)" t t)
1118           (gnus-deactivate-mark)
1119           (when (gnus-y-or-n-p "Send this complaint? ")
1120             (message-send-and-exit)))))))
1121
1122 (defun gnus-summary-mail-other-window ()
1123   "Compose mail in other window."
1124   (interactive)
1125   (gnus-setup-message 'message
1126     (message-mail)))
1127
1128 (defun gnus-mail-parse-comma-list ()
1129   (let (accumulated
1130         beg)
1131     (skip-chars-forward " ")
1132     (while (not (eobp))
1133       (setq beg (point))
1134       (skip-chars-forward "^,")
1135       (while (zerop
1136               (save-excursion
1137                 (save-restriction
1138                   (let ((i 0))
1139                     (narrow-to-region beg (point))
1140                     (goto-char beg)
1141                     (logand (progn
1142                               (while (search-forward "\"" nil t)
1143                                 (incf i))
1144                               (if (zerop i) 2 i))
1145                             2)))))
1146         (skip-chars-forward ",")
1147         (skip-chars-forward "^,"))
1148       (skip-chars-backward " ")
1149       (push (buffer-substring beg (point))
1150             accumulated)
1151       (skip-chars-forward "^,")
1152       (skip-chars-forward ", "))
1153     accumulated))
1154
1155 (defun gnus-inews-add-to-address (group)
1156   (let ((to-address (mail-fetch-field "to")))
1157     (when (and to-address
1158                (gnus-alive-p))
1159       ;; This mail group doesn't have a `to-list', so we add one
1160       ;; here.  Magic!
1161       (when (gnus-y-or-n-p
1162              (format "Do you want to add this as `to-list': %s " to-address))
1163         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1164
1165 (defun gnus-put-message ()
1166   "Put the current message in some group and return to Gnus."
1167   (interactive)
1168   (let ((reply gnus-article-reply)
1169         (winconf gnus-prev-winconf)
1170         (group gnus-newsgroup-name))
1171     (unless (and group
1172                  (not (gnus-group-read-only-p group)))
1173       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1174
1175     (when (gnus-gethash group gnus-newsrc-hashtb)
1176       (error "No such group: %s" group))
1177     (save-excursion
1178       (save-restriction
1179         (widen)
1180         (message-narrow-to-headers)
1181         (let ((gnus-deletable-headers nil))
1182           (message-generate-headers
1183            (if (message-news-p)
1184                message-required-news-headers
1185              message-required-mail-headers)))
1186         (goto-char (point-max))
1187         (insert "Gcc: " group "\n")
1188         (widen)))
1189     (gnus-inews-do-gcc)
1190     (when (and (get-buffer gnus-group-buffer)
1191                (gnus-buffer-exists-p (car-safe reply))
1192                (cdr reply))
1193       (set-buffer (car reply))
1194       (gnus-summary-mark-article-as-replied (cdr reply)))
1195     (when winconf
1196       (set-window-configuration winconf))))
1197
1198 (defun gnus-article-mail (yank)
1199   "Send a reply to the address near point.
1200 If YANK is non-nil, include the original article."
1201   (interactive "P")
1202   (let ((address
1203          (buffer-substring
1204           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1205           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1206     (when address
1207       (message-reply address)
1208       (when yank
1209         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1210
1211 (defvar nntp-server-type)
1212 (defun gnus-bug ()
1213   "Send a bug report to the Gnus maintainers."
1214   (interactive)
1215   (unless (gnus-alive-p)
1216     (error "Gnus has been shut down"))
1217   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1218     (unless (message-mail-user-agent)
1219       (message-pop-to-buffer "*Gnus Bug*")
1220       (delete-other-windows)
1221       (when gnus-bug-create-help-buffer
1222         (switch-to-buffer "*Gnus Help Bug*")
1223         (erase-buffer)
1224         (insert gnus-bug-message)
1225         (goto-char (point-min))
1226         (sit-for 0)
1227         (set-buffer "*Gnus Bug*")))
1228     (let ((message-this-is-mail t))
1229       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1230     (when gnus-bug-create-help-buffer
1231       (push `(gnus-bug-kill-buffer) message-send-actions))
1232     (goto-char (point-min))
1233     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1234     (forward-line 1)
1235     (insert gnus-product-name " " gnus-version-number
1236             " (r" gnus-revision-number ") "
1237             "based on " gnus-original-product-name " v"
1238             gnus-original-version-number "\n"
1239             (emacs-version) "\n")
1240     (when (and (boundp 'nntp-server-type)
1241                (stringp nntp-server-type))
1242       (insert nntp-server-type))
1243     (insert "\n\n\n\n\n")
1244     (let (mime-content-types)
1245       (mime-edit-insert-tag "text" "plain" "; type=emacs-lisp"))
1246     (insert (with-temp-buffer
1247               (gnus-debug)
1248               (buffer-string)))
1249     (let (mime-content-types)
1250       (mime-edit-insert-tag "text" "plain"))
1251     (goto-char (point-min))
1252     (search-forward "Subject: " nil t)
1253     (message "")))
1254
1255 (defun gnus-bug-kill-buffer ()
1256   (when (get-buffer "*Gnus Help Bug*")
1257     (kill-buffer "*Gnus Help Bug*")))
1258
1259 (defun gnus-summary-yank-message (buffer n)
1260   "Yank the current article into a composed message."
1261   (interactive
1262    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1263          current-prefix-arg))
1264   (when (gnus-buffer-live-p buffer)
1265     (let ((summary-frame (selected-frame))
1266           (message-frame (when (static-if (featurep 'xemacs)
1267                                    (device-on-window-system-p)
1268                                  window-system)
1269                            (let ((window (get-buffer-window buffer t)))
1270                              (when window
1271                                (window-frame window)))))
1272           (separator (concat "^" (regexp-quote mail-header-separator)
1273                              "\n")))
1274       (gnus-summary-iterate n
1275         (gnus-summary-select-article)
1276         (gnus-copy-article-buffer)
1277         (when (frame-live-p message-frame)
1278           (raise-frame message-frame)
1279           (select-frame message-frame))
1280         (with-current-buffer buffer
1281           (when (save-excursion
1282                   (beginning-of-line)
1283                   (let (case-fold-search)
1284                     (and (not (re-search-backward separator nil t))
1285                          (re-search-forward separator nil t))))
1286             (goto-char (match-end 0)))
1287           (message-yank-buffer gnus-article-copy))
1288         (select-frame summary-frame))
1289       (when (frame-live-p message-frame)
1290         (select-frame message-frame)))))
1291
1292 (defun gnus-debug ()
1293   "Attempts to go through the Gnus source file and report what variables have been changed.
1294 The source file has to be in the Emacs load path."
1295   (interactive)
1296   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1297                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1298                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1299                  "nnmail.el" "nntp.el" "message.el"))
1300         (point (point))
1301         file expr olist sym)
1302     (gnus-message 4 "Please wait while we snoop your variables...")
1303     ;; Go through all the files looking for non-default values for variables.
1304     (save-excursion
1305       (sit-for 0)
1306       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1307       (while files
1308         (erase-buffer)
1309         (when (and (setq file (locate-library (pop files)))
1310                    (file-exists-p file))
1311           (insert-file-contents file)
1312           (goto-char (point-min))
1313           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1314               (gnus-message 4 "Malformed sources in file %s" file)
1315             (narrow-to-region (point-min) (point))
1316             (goto-char (point-min))
1317             (while (setq expr (ignore-errors (read (current-buffer))))
1318               (ignore-errors
1319                 (and (memq (car expr) '(defvar defcustom defvoo))
1320                      (stringp (nth 3 expr))
1321                      (or (not (boundp (nth 1 expr)))
1322                          (not (equal (eval (nth 2 expr))
1323                                      (symbol-value (nth 1 expr)))))
1324                      (push (nth 1 expr) olist)))))))
1325       (kill-buffer (current-buffer)))
1326     (when (setq olist (nreverse olist))
1327       (insert ";----------------- Environment follows ------------------\n\n"))
1328     (while olist
1329       (if (boundp (car olist))
1330           (condition-case ()
1331               (pp `(setq ,(car olist)
1332                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1333                                   (and (symbolp sym)
1334                                        (not (or (eq sym nil)
1335                                                 (eq sym t)))))
1336                               (list 'quote (symbol-value (car olist)))
1337                             (symbol-value (car olist))))
1338                   (current-buffer))
1339             (error
1340              (format "(setq %s 'whatever)\n" (car olist))))
1341         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1342       (setq olist (cdr olist)))
1343     ;; Remove any control chars - they seem to cause trouble for some
1344     ;; mailers.  (Byte-compiled output from the stuff above.)
1345     (goto-char point)
1346     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1347       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1348                      t t))
1349     ;; Break MIME tags purposely.
1350     (goto-char point)
1351     (while (re-search-forward mime-edit-tag-regexp nil t)
1352       (goto-char (1+ (match-beginning 0)))
1353       (insert "X"))))
1354
1355 ;;; Treatment of rejected articles.
1356 ;;; Bounced mail.
1357
1358 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1359   "Re-mail the current message.
1360 This only makes sense if the current message is a bounce message than
1361 contains some mail you have written which has been bounced back to
1362 you.
1363 If FETCH, try to fetch the article that this is a reply to, if indeed
1364 this is a reply."
1365   (interactive "P")
1366   (gnus-summary-select-article t)
1367   (set-buffer gnus-original-article-buffer)
1368   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1369     (gnus-setup-message 'compose-bounce
1370       (let* ((references (mail-fetch-field "references"))
1371              (parent (and references (gnus-parent-id references))))
1372         (message-bounce)
1373         ;; If there are references, we fetch the article we answered to.
1374         (and fetch parent
1375              (gnus-summary-refer-article parent)
1376              (gnus-summary-show-all-headers))))))
1377
1378 ;;; Gcc handling.
1379
1380 (defun gnus-inews-group-method (group)
1381   (cond
1382    ;; If the group doesn't exist, we assume
1383    ;; it's an archive group...
1384    ((and (null (gnus-get-info group))
1385          (eq (car (gnus-server-to-method gnus-message-archive-method))
1386              (car (gnus-server-to-method (gnus-group-method group)))))
1387     gnus-message-archive-method)
1388    ;; Use the method.
1389    ((gnus-info-method (gnus-get-info group))
1390     (gnus-info-method (gnus-get-info group)))
1391    ;; Find the method.
1392    (t (gnus-server-to-method (gnus-group-method group)))))
1393
1394 ;; Do Gcc handling, which copied the message over to some group.
1395 (defun gnus-inews-do-gcc (&optional gcc)
1396   (interactive)
1397   (save-excursion
1398     (save-restriction
1399       (message-narrow-to-headers)
1400       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1401             (coding-system-for-write 'raw-text)
1402             (output-coding-system 'raw-text)
1403             groups group method group-art)
1404         (when gcc
1405           (message-remove-header "gcc")
1406           (widen)
1407           (setq groups (message-unquote-tokens
1408                         (message-tokenize-header gcc " ,")))
1409           ;; Copy the article over to some group(s).
1410           (while (setq group (pop groups))
1411             (unless (gnus-check-server
1412                      (setq method (gnus-inews-group-method group)))
1413               (error "Can't open server %s" (if (stringp method) method
1414                                               (car method))))
1415             (unless (gnus-request-group group nil method)
1416               (gnus-request-create-group group method))
1417             (save-excursion
1418               (nnheader-set-temp-buffer " *acc*")
1419               (insert-buffer-substring message-encoding-buffer)
1420               (gnus-run-hooks 'gnus-before-do-gcc-hook)
1421               (goto-char (point-min))
1422               (when (re-search-forward
1423                      (concat "^" (regexp-quote mail-header-separator) "$")
1424                      nil t)
1425                 (replace-match "" t t ))
1426               (unless (setq group-art
1427                             (gnus-request-accept-article group method t t))
1428                 (gnus-message 1 "Couldn't store article in group %s: %s"
1429                               group (gnus-status-message method))
1430                 (sit-for 2))
1431               (when (and group-art gnus-inews-mark-gcc-as-read)
1432                 (gnus-group-mark-article-read group (cdr group-art)))
1433               (kill-buffer (current-buffer)))))))))
1434
1435 (defun gnus-inews-insert-gcc ()
1436   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1437   (save-excursion
1438     (save-restriction
1439       (message-narrow-to-headers)
1440       (let* ((group gnus-outgoing-message-group)
1441              (gcc (cond
1442                    ((gnus-functionp group)
1443                     (funcall group))
1444                    ((or (stringp group) (list group))
1445                     group))))
1446         (when gcc
1447           (insert "Gcc: "
1448                   (if (stringp gcc) gcc
1449                     (mapconcat 'identity gcc " "))
1450                   "\n"))))))
1451
1452 (defun gnus-inews-insert-archive-gcc (&optional group)
1453   "Insert the Gcc to say where the article is to be archived."
1454   (let* ((var gnus-message-archive-group)
1455          (group (or group gnus-newsgroup-name ""))
1456          (gcc-self-val
1457           (and gnus-newsgroup-name
1458                (not (equal gnus-newsgroup-name ""))
1459                (gnus-group-find-parameter
1460                 gnus-newsgroup-name 'gcc-self)))
1461          result
1462          (groups
1463           (cond
1464            ((null gnus-message-archive-method)
1465             ;; Ignore.
1466             nil)
1467            ((stringp var)
1468             ;; Just a single group.
1469             (list var))
1470            ((null var)
1471             ;; We don't want this.
1472             nil)
1473            ((and (listp var) (stringp (car var)))
1474             ;; A list of groups.
1475             var)
1476            ((gnus-functionp var)
1477             ;; A function.
1478             (funcall var group))
1479            (t
1480             ;; An alist of regexps/functions/forms.
1481             (while (and var
1482                         (not
1483                          (setq result
1484                                (cond
1485                                 ((stringp (caar var))
1486                                  ;; Regexp.
1487                                  (when (string-match (caar var) group)
1488                                    (cdar var)))
1489                                 ((gnus-functionp (car var))
1490                                  ;; Function.
1491                                  (funcall (car var) group))
1492                                 (t
1493                                  (eval (car var)))))))
1494               (setq var (cdr var)))
1495             result)))
1496          name)
1497     (when (or groups gcc-self-val)
1498       (when (stringp groups)
1499         (setq groups (list groups)))
1500       (save-excursion
1501         (save-restriction
1502           (message-narrow-to-headers)
1503           (goto-char (point-max))
1504           (insert "Gcc: ")
1505           (if gcc-self-val
1506               ;; Use the `gcc-self' param value instead.
1507               (progn
1508                 (insert
1509                  (if (stringp gcc-self-val)
1510                      gcc-self-val
1511                    group))
1512                 (if (not (eq gcc-self-val 'none))
1513                     (insert "\n")
1514                   (progn
1515                     (beginning-of-line)
1516                     (kill-line))))
1517             ;; Use the list of groups.
1518             (while (setq name (pop groups))
1519               (insert (if (string-match ":" name)
1520                           name
1521                         (gnus-group-prefixed-name
1522                          name gnus-message-archive-method)))
1523               (when groups
1524                 (insert " ")))
1525             (insert "\n")))))))
1526
1527 ;;; Posting styles.
1528
1529 (defun gnus-configure-posting-styles ()
1530   "Configure posting styles according to `gnus-posting-styles'."
1531   (unless gnus-inhibit-posting-styles
1532     (let ((group (or gnus-newsgroup-name ""))
1533           (styles gnus-posting-styles)
1534           style match variable attribute value v results
1535           filep name address element)
1536       ;; If the group has a posting-style parameter, add it at the end with a
1537       ;; regexp matching everything, to be sure it takes precedence over all
1538       ;; the others.
1539       (when gnus-newsgroup-name
1540         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1541           (when tmp-style
1542             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1543       ;; Go through all styles and look for matches.
1544       (dolist (style styles)
1545         (setq match (pop style))
1546         (goto-char (point-min))
1547         (when (cond
1548                ((stringp match)
1549                 ;; Regexp string match on the group name.
1550                 (string-match match group))
1551                ((eq match 'header)
1552                 (and (gnus-buffer-live-p gnus-article-copy)
1553                      (with-current-buffer gnus-article-copy
1554                        (let ((header (message-fetch-field (pop style))))
1555                          (and header
1556                               (string-match (pop style) header))))))
1557                ((or (symbolp match)
1558                     (gnus-functionp match))
1559                 (cond
1560                  ((gnus-functionp match)
1561                   ;; Function to be called.
1562                   (funcall match))
1563                  ((boundp match)
1564                   ;; Variable to be checked.
1565                   (symbol-value match))))
1566                ((listp match)
1567                 ;; This is a form to be evaled.
1568                 (eval match)))
1569           ;; We have a match, so we set the variables.
1570           (dolist (attribute style)
1571             (setq element (pop attribute)
1572                   variable nil
1573                   filep nil)
1574             (setq value
1575                   (cond
1576                    ((eq (car attribute) ':file)
1577                     (setq filep t)
1578                     (cadr attribute))
1579                    ((eq (car attribute) :value)
1580                     (cadr attribute))
1581                    (t
1582                     (car attribute))))
1583             ;; We get the value.
1584             (setq v
1585                   (cond
1586                    ((stringp value)
1587                     value)
1588                    ((or (symbolp value)
1589                         (gnus-functionp value))
1590                     (cond ((gnus-functionp value)
1591                            (funcall value))
1592                           ((boundp value)
1593                            (symbol-value value))))
1594                    ((listp value)
1595                     (eval value))))
1596             ;; Translate obsolescent value.
1597             (when (eq element 'signature-file)
1598               (setq element 'signature
1599                     filep t))
1600             ;; Get the contents of file elems.
1601             (when (and filep v)
1602               (setq v (with-temp-buffer
1603                         (insert-file-contents v)
1604                         (buffer-string))))
1605             (setq results (delq (assoc element results) results))
1606             (push (cons element v) results))))
1607       ;; Now we have all the styles, so we insert them.
1608       (setq name (assq 'name results)
1609             address (assq 'address results))
1610       (setq results (delq name (delq address results)))
1611       (make-local-variable 'message-setup-hook)
1612       (dolist (result results)
1613         (add-hook 'message-setup-hook
1614                   (cond
1615                    ((eq 'eval (car result))
1616                     'ignore)
1617                    ((eq 'body (car result))
1618                     `(lambda ()
1619                        (save-excursion
1620                          (message-goto-body)
1621                          (insert ,(cdr result)))))
1622                    ((eq 'signature (car result))
1623                     (set (make-local-variable 'message-signature) nil)
1624                     (set (make-local-variable 'message-signature-file) nil)
1625                     (if (not (cdr result))
1626                         'ignore
1627                       `(lambda ()
1628                          (save-excursion
1629                            (let ((message-signature ,(cdr result)))
1630                              (when message-signature
1631                                (message-insert-signature)))))))
1632                    (t
1633                     (let ((header
1634                            (if (symbolp (car result))
1635                                (capitalize (symbol-name (car result)))
1636                              (car result))))
1637                       `(lambda ()
1638                          (save-excursion
1639                            (message-remove-header ,header)
1640                            (let ((value ,(cdr result)))
1641                              (when value
1642                                (message-goto-eoh)
1643                                (insert ,header ": " value "\n"))))))))))
1644       (when (or name address)
1645         (add-hook 'message-setup-hook
1646                   `(lambda ()
1647                      (set (make-local-variable 'user-mail-address)
1648                           ,(or (cdr address) user-mail-address))
1649                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1650                            (user-mail-address
1651                             ,(or (cdr address) user-mail-address)))
1652                        (save-excursion
1653                          (message-remove-header "From")
1654                          (message-goto-eoh)
1655                          (insert "From: " (message-make-from) "\n")))))))))
1656
1657
1658 ;;; @ for MIME Edit mode
1659 ;;;
1660
1661 (defun gnus-maybe-setup-default-charset ()
1662   (let ((charset
1663          (and (boundp 'gnus-summary-buffer)
1664               (buffer-live-p gnus-summary-buffer)
1665               (save-excursion
1666                 (set-buffer gnus-summary-buffer)
1667                 default-mime-charset))))
1668     (if charset
1669         (progn
1670           (make-local-variable 'default-mime-charset)
1671           (setq default-mime-charset charset)
1672           ))))
1673
1674
1675 ;;; @ for MIME view mode
1676 ;;;
1677
1678 (defun gnus-following-method (buf)
1679   (gnus-setup-message 'reply-yank
1680     (set-buffer buf)
1681     (if (message-news-p)
1682         (message-followup)
1683       (message-reply nil 'wide))
1684     (let ((message-reply-buffer buf))
1685       (message-yank-original))
1686     (message-goto-body))
1687   (kill-buffer buf))
1688
1689
1690 ;;; Allow redefinition of functions.
1691
1692 (gnus-ems-redefine)
1693
1694 (provide 'gnus-msg)
1695
1696 ;;; gnus-msg.el ends here