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