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