*** empty log message ***
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-ems)
33 (require 'message)
34 (require 'gnus-art)
35
36 (defcustom gnus-post-method nil
37   "*Preferred method for posting USENET news.
38
39 If this variable is `current', Gnus will use the \"current\" select
40 method when posting.  If it is nil (which is the default), Gnus will
41 use the native posting method of the server.
42
43 This method will not be used in mail groups and the like, only in
44 \"real\" newsgroups.
45
46 If not nil nor `native', the value must be a valid method as discussed
47 in the documentation of `gnus-select-method'. It can also be a list of
48 methods. If that is the case, the user will be queried for what select
49 method to use when posting."
50   :group 'gnus-group-foreign
51   :type `(choice (const nil)
52                  (const current)
53                  (const native)
54                  (sexp :tag "Methods" ,gnus-select-method)))
55
56 (defvar gnus-outgoing-message-group nil
57   "*All outgoing messages will be put in this group.
58 If you want to store all your outgoing mail and articles in the group
59 \"nnml:archive\", you set this variable to that value.  This variable
60 can also be a list of group names.
61
62 If you want to have greater control over what group to put each
63 message in, you can set this variable to a function that checks the
64 current newsgroup name and then returns a suitable group name (or list
65 of names).")
66
67 (defvar gnus-mailing-list-groups nil
68   "*Regexp matching groups that are really mailing lists.
69 This is useful when you're reading a mailing list that has been
70 gatewayed to a newsgroup, and you want to followup to an article in
71 the group.")
72
73 (defvar gnus-add-to-list nil
74   "*If non-nil, add a `to-list' parameter automatically.")
75
76 (defvar gnus-crosspost-complaint
77   "Hi,
78
79 You posted the article below with the following Newsgroups header:
80
81 Newsgroups: %s
82
83 The %s group, at least, was an inappropriate recipient
84 of this message.  Please trim your Newsgroups header to exclude this
85 group before posting in the future.
86
87 Thank you.
88
89 "
90   "Format string to be inserted when complaining about crossposts.
91 The first %s will be replaced by the Newsgroups header;
92 the second with the current group name.")
93
94 (defvar gnus-message-setup-hook nil
95   "Hook run after setting up a message buffer.")
96
97 (defvar gnus-bug-create-help-buffer t
98   "*Should we create the *Gnus Help Bug* buffer?")
99
100 (defvar gnus-posting-styles nil
101   "*Alist of styles to use when posting.")
102
103 (defvar gnus-posting-style-alist
104   '((organization . message-user-organization)
105     (signature . message-signature)
106     (signature-file . message-signature-file)
107     (address . user-mail-address)
108     (name . user-full-name))
109   "*Mapping from style parameters to variables.")
110
111 ;;; Internal variables.
112
113 (defvar gnus-inhibit-posting-styles nil
114   "Inhibit the use of posting styles.")
115
116 (defvar gnus-message-buffer "*Mail Gnus*")
117 (defvar gnus-article-copy nil)
118 (defvar gnus-last-posting-server nil)
119 (defvar gnus-message-group-art nil)
120
121 (defconst gnus-bug-message
122   "Sending a bug report to the Gnus Towers.
123 ========================================
124
125 The buffer below is a mail buffer.  When you press `C-c C-c', it will
126 be sent to the Gnus Bug Exterminators.
127
128 At the bottom of the buffer you'll see lots of variable settings.
129 Please do not delete those.  They will tell the Bug People what your
130 environment is, so that it will be easier to locate the bugs.
131
132 If you have found a bug that makes Emacs go \"beep\", set
133 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
134 and include the backtrace in your bug report.
135
136 Please describe the bug in annoying, painstaking detail.
137
138 Thank you for your help in stamping out bugs.
139 ")
140
141 (eval-and-compile
142   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
143   (autoload 'news-setup "rnewspost")
144   (autoload 'news-reply-mode "rnewspost")
145   (autoload 'rmail-dont-reply-to "mail-utils")
146   (autoload 'rmail-output "rmailout"))
147
148 \f
149 ;;;
150 ;;; Gnus Posting Functions
151 ;;;
152
153 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
154   "p" gnus-summary-post-news
155   "f" gnus-summary-followup
156   "F" gnus-summary-followup-with-original
157   "c" gnus-summary-cancel-article
158   "s" gnus-summary-supersede-article
159   "r" gnus-summary-reply
160   "R" gnus-summary-reply-with-original
161   "w" gnus-summary-wide-reply
162   "W" gnus-summary-wide-reply-with-original
163   "n" gnus-summary-followup-to-mail
164   "N" gnus-summary-followup-to-mail-with-original
165   "m" gnus-summary-mail-other-window
166   "u" gnus-uu-post-news
167   "\M-c" gnus-summary-mail-crosspost-complaint
168   "om" gnus-summary-mail-forward
169   "op" gnus-summary-post-forward
170   "Om" gnus-uu-digest-mail-forward
171   "Op" gnus-uu-digest-post-forward)
172
173 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
174   "b" gnus-summary-resend-bounced-mail
175   ;; "c" gnus-summary-send-draft
176   "r" gnus-summary-resend-message)
177
178 ;;; Internal functions.
179
180 (defvar gnus-article-reply nil)
181 (defmacro gnus-setup-message (config &rest forms)
182   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
183         (buffer (make-symbol "gnus-setup-message-buffer"))
184         (article (make-symbol "gnus-setup-message-article"))
185         (group (make-symbol "gnus-setup-message-group")))
186     `(let ((,winconf (current-window-configuration))
187            (,buffer (buffer-name (current-buffer)))
188            (,article (and gnus-article-reply (gnus-summary-article-number)))
189            (,group gnus-newsgroup-name)
190            (message-header-setup-hook
191             (copy-sequence message-header-setup-hook))
192            (message-mode-hook (copy-sequence message-mode-hook)))
193        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
194        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
195        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
196        (unwind-protect
197            (progn
198              ,@forms)
199          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
200          (setq gnus-message-buffer (current-buffer))
201          (set (make-local-variable 'gnus-message-group-art)
202               (cons ,group ,article))
203          (set (make-local-variable 'gnus-newsgroup-name) ,group)
204          (gnus-run-hooks 'gnus-message-setup-hook))
205        (gnus-add-buffer)
206        (gnus-configure-windows ,config t)
207        (set-buffer-modified-p nil))))
208
209 (defun gnus-inews-add-send-actions (winconf buffer article)
210   (make-local-hook 'message-sent-hook)
211   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
212   (setq message-post-method
213         `(lambda (arg)
214            (gnus-post-method arg ,gnus-newsgroup-name)))
215   (setq message-newsreader (setq message-mailer (gnus-extended-version)))
216   (message-add-action
217    `(set-window-configuration ,winconf) 'exit 'postpone 'kill)
218   (message-add-action
219    `(when (gnus-buffer-exists-p ,buffer)
220       (save-excursion
221         (set-buffer ,buffer)
222         ,(when article
223            `(gnus-summary-mark-article-as-replied ,article))))
224    'send))
225
226 (put 'gnus-setup-message 'lisp-indent-function 1)
227 (put 'gnus-setup-message 'edebug-form-spec '(form body))
228
229 ;;; Post news commands of Gnus group mode and summary mode
230
231 (defun gnus-group-mail (&optional arg)
232   "Start composing a mail.
233 If ARG, use the group under the point to find a posting style.
234 If ARG is 1, prompt for a group name to find the posting style."
235   (interactive "P")
236   ;; We can't `let' gnus-newsgroup-name here, since that leads
237   ;; to local variables leaking.
238   (let ((group gnus-newsgroup-name)
239         (buffer (current-buffer)))
240     (unwind-protect
241         (progn
242           (setq gnus-newsgroup-name
243                 (if arg
244                     (if (= 1 (prefix-numeric-value arg))
245                         (completing-read "Use posting style of group: "
246                                          gnus-active-hashtb nil
247                                          (gnus-read-active-file-p))
248                       (gnus-group-group-name))
249                   ""))
250           (gnus-setup-message 'message (message-mail)))
251       (save-excursion
252         (set-buffer buffer)
253         (setq gnus-newsgroup-name group)))))
254
255 (defun gnus-group-post-news (&optional arg)
256   "Start composing a news message.
257 If ARG, post to the group under point.
258 If ARG is 1, prompt for a group name."
259   (interactive "P")
260   ;; Bind this variable here to make message mode hooks work ok.
261   (let ((gnus-newsgroup-name
262          (if arg
263              (if (= 1 (prefix-numeric-value arg))
264                  (completing-read "Newsgroup: " gnus-active-hashtb nil
265                                   (gnus-read-active-file-p))
266                (gnus-group-group-name))
267            "")))
268     (gnus-post-news 'post gnus-newsgroup-name)))
269
270 (defun gnus-summary-post-news ()
271   "Start composing a news message."
272   (interactive)
273   (gnus-post-news 'post gnus-newsgroup-name))
274
275 (defun gnus-summary-followup (yank &optional force-news)
276   "Compose a followup to an article.
277 If prefix argument YANK is non-nil, original article is yanked automatically."
278   (interactive
279    (list (and current-prefix-arg
280               (gnus-summary-work-articles 1))))
281   (when yank
282     (gnus-summary-goto-subject (car yank)))
283   (save-window-excursion
284     (gnus-summary-select-article))
285   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
286         (gnus-newsgroup-name gnus-newsgroup-name))
287     ;; Send a followup.
288     (gnus-post-news nil gnus-newsgroup-name
289                     headers gnus-article-buffer
290                     yank nil force-news)))
291
292 (defun gnus-summary-followup-with-original (n &optional force-news)
293   "Compose a followup to an article and include the original article."
294   (interactive "P")
295   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
296
297 (defun gnus-summary-followup-to-mail (&optional arg)
298   "Followup to the current mail message via news."
299   (interactive
300    (list (and current-prefix-arg
301               (gnus-summary-work-articles 1))))
302   (gnus-summary-followup arg t))
303
304 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
305   "Followup to the current mail message via news."
306   (interactive "P")
307   (gnus-summary-followup (gnus-summary-work-articles arg) t))
308
309 (defun gnus-inews-yank-articles (articles)
310   (let (beg article)
311     (message-goto-body)
312     (while (setq article (pop articles))
313       (save-window-excursion
314         (set-buffer gnus-summary-buffer)
315         (gnus-summary-select-article nil nil nil article)
316         (gnus-summary-remove-process-mark article))
317       (gnus-copy-article-buffer)
318       (let ((message-reply-buffer gnus-article-copy)
319             (message-reply-headers gnus-current-headers))
320         (message-yank-original)
321         (setq beg (or beg (mark t))))
322       (when articles
323         (insert "\n")))
324     (push-mark)
325     (goto-char beg)))
326
327 (defun gnus-summary-cancel-article (&optional n symp)
328   "Cancel an article you posted.
329 Uses the process-prefix convention.  If given the symbolic
330 prefix `a', cancel using the standard posting method; if not
331 post using the current select method."
332   (interactive (gnus-interactive "P\ny"))
333   (let ((articles (gnus-summary-work-articles n))
334         (message-post-method
335          `(lambda (arg)
336             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
337         article)
338     (while (setq article (pop articles))
339       (when (gnus-summary-select-article t nil nil article)
340         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
341                 (message-cancel-news))
342           (gnus-summary-mark-as-read article gnus-canceled-mark)
343           (gnus-cache-remove-article 1))
344         (gnus-article-hide-headers-if-wanted))
345       (gnus-summary-remove-process-mark article))))
346
347 (defun gnus-summary-supersede-article ()
348   "Compose an article that will supersede a previous article.
349 This is done simply by taking the old article and adding a Supersedes
350 header line with the old Message-ID."
351   (interactive)
352   (let ((article (gnus-summary-article-number)))
353     (gnus-setup-message 'reply-yank
354       (gnus-summary-select-article t)
355       (set-buffer gnus-original-article-buffer)
356       (message-supersede)
357       (push
358        `((lambda ()
359            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
360              (save-excursion
361                (set-buffer ,gnus-summary-buffer)
362                (gnus-cache-possibly-remove-article ,article nil nil nil t)
363                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
364        message-send-actions))))
365
366 \f
367
368 (defun gnus-copy-article-buffer (&optional article-buffer)
369   ;; make a copy of the article buffer with all text properties removed
370   ;; this copy is in the buffer gnus-article-copy.
371   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
372   ;; this buffer should be passed to all mail/news reply/post routines.
373   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
374   (save-excursion
375     (set-buffer gnus-article-copy)
376     (mm-enable-multibyte))
377   (let ((article-buffer (or article-buffer gnus-article-buffer))
378         end beg)
379     (if (not (and (get-buffer article-buffer)
380                   (gnus-buffer-exists-p article-buffer)))
381         (error "Can't find any article buffer")
382       (save-excursion
383         (set-buffer article-buffer)
384         (save-restriction
385           ;; Copy over the (displayed) article buffer, delete
386           ;; hidden text and remove text properties.
387           (widen)
388           (copy-to-buffer gnus-article-copy (point-min) (point-max))
389           (set-buffer gnus-article-copy)
390           (gnus-article-delete-text-of-type 'annotation)
391           (gnus-remove-text-with-property 'gnus-prev)
392           (gnus-remove-text-with-property 'gnus-next)
393           (insert
394            (prog1
395                (format "%s" (buffer-string))
396              (erase-buffer)))
397           ;; Find the original headers.
398           (set-buffer gnus-original-article-buffer)
399           (goto-char (point-min))
400           (while (looking-at message-unix-mail-delimiter)
401             (forward-line 1))
402           (setq beg (point))
403           (setq end (or (search-forward "\n\n" nil t) (point)))
404           ;; Delete the headers from the displayed articles.
405           (set-buffer gnus-article-copy)
406           (delete-region (goto-char (point-min))
407                          (or (search-forward "\n\n" nil t) (point-max)))
408           ;; Insert the original article headers.
409           (insert-buffer-substring gnus-original-article-buffer beg end)
410           (article-decode-encoded-words)))
411       gnus-article-copy)))
412
413 (defun gnus-post-news (post &optional group header article-buffer yank subject
414                             force-news)
415   (when article-buffer
416     (gnus-copy-article-buffer))
417   (let ((gnus-article-reply article-buffer)
418         (add-to-list gnus-add-to-list))
419     (gnus-setup-message (cond (yank 'reply-yank)
420                               (article-buffer 'reply)
421                               (t 'message))
422       (let* ((group (or group gnus-newsgroup-name))
423              (pgroup group)
424              to-address to-group mailing-list to-list
425              newsgroup-p)
426         (when group
427           (setq to-address (gnus-group-find-parameter group 'to-address)
428                 to-group (gnus-group-find-parameter group 'to-group)
429                 to-list (gnus-group-find-parameter group 'to-list)
430                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
431                 mailing-list (when gnus-mailing-list-groups
432                                (string-match gnus-mailing-list-groups group))
433                 group (gnus-group-real-name group)))
434         (if (or (and to-group
435                      (gnus-news-group-p to-group))
436                 newsgroup-p
437                 force-news
438                 (and (gnus-news-group-p
439                       (or pgroup gnus-newsgroup-name)
440                       (if header (mail-header-number header)
441                         gnus-current-article))
442                      (not mailing-list)
443                      (not to-list)
444                      (not to-address)))
445             ;; This is news.
446             (if post
447                 (message-news (or to-group group))
448               (set-buffer gnus-article-copy)
449               (gnus-msg-treat-broken-reply-to)
450               (message-followup (if (or newsgroup-p force-news) nil to-group)))
451           ;; The is mail.
452           (if post
453               (progn
454                 (message-mail (or to-address to-list))
455                 ;; Arrange for mail groups that have no `to-address' to
456                 ;; get that when the user sends off the mail.
457                 (when (and (not to-list)
458                            (not to-address)
459                            add-to-list)
460                   (push (list 'gnus-inews-add-to-address pgroup)
461                         message-send-actions)))
462             (set-buffer gnus-article-copy)
463             (gnus-msg-treat-broken-reply-to)
464             (message-wide-reply to-address)))
465         (when yank
466           (gnus-inews-yank-articles yank))))))
467
468 (defun gnus-msg-treat-broken-reply-to ()
469   "Remove the Reply-to header iff broken-reply-to."
470   (when (gnus-group-find-parameter
471          gnus-newsgroup-name 'broken-reply-to)
472     (save-restriction
473       (message-narrow-to-head)
474       (message-remove-header "reply-to"))))
475
476 (defun gnus-post-method (arg group &optional silent)
477   "Return the posting method based on GROUP and ARG.
478 If SILENT, don't prompt the user."
479   (let ((group-method (gnus-find-method-for-group group)))
480     (cond
481      ;; If the group-method is nil (which shouldn't happen) we use
482      ;; the default method.
483      ((null group-method)
484       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
485                gnus-select-method message-post-method))
486      ;; We want the inverse of the default
487      ((and arg (not (eq arg 0)))
488       (if (eq gnus-post-method 'active)
489           gnus-select-method
490         group-method))
491      ;; We query the user for a post method.
492      ((or arg
493           (and gnus-post-method
494                (not (eq gnus-post-method 'current))
495                (listp (car gnus-post-method))))
496       (let* ((methods
497               ;; Collect all methods we know about.
498               (append
499                (when (and gnus-post-method
500                           (not (eq gnus-post-method 'current)))
501                  (if (listp (car gnus-post-method))
502                      gnus-post-method
503                    (list gnus-post-method)))
504                gnus-secondary-select-methods
505                (mapcar 'cdr gnus-server-alist)
506                (mapcar 'car gnus-opened-servers)
507                (list gnus-select-method)
508                (list group-method)))
509              method-alist post-methods method)
510         ;; Weed out all mail methods.
511         (while methods
512           (setq method (gnus-server-get-method "" (pop methods)))
513           (when (and (or (gnus-method-option-p method 'post)
514                          (gnus-method-option-p method 'post-mail))
515                      (not (member method post-methods)))
516             (push method post-methods)))
517         ;; Create a name-method alist.
518         (setq method-alist
519               (mapcar
520                (lambda (m)
521                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
522                post-methods))
523         ;; Query the user.
524         (cadr
525          (assoc
526           (setq gnus-last-posting-server
527                 (if (and silent
528                          gnus-last-posting-server)
529                     ;; Just use the last value.
530                     gnus-last-posting-server
531                   (completing-read
532                    "Posting method: " method-alist nil t
533                    (cons (or gnus-last-posting-server "") 0))))
534           method-alist))))
535      ;; Override normal method.
536      ((and (eq gnus-post-method 'current)
537            (not (eq (car group-method) 'nndraft))
538            (not arg))
539       group-method)
540      ((and gnus-post-method
541            (not (eq gnus-post-method 'current)))
542       gnus-post-method)
543      ;; Use the normal select method.
544      (t gnus-select-method))))
545
546 \f
547
548 ;; Dummies to avoid byte-compile warning.
549 (defvar nnspool-rejected-article-hook)
550 (defvar xemacs-codename)
551
552 (defun gnus-extended-version ()
553   "Stringified Gnus version and Emacs version."
554   (interactive)
555   (concat
556    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
557    " (" gnus-version ")"
558    " "
559    (cond
560     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
561      (concat "Emacs/" (match-string 1 emacs-version)))
562     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
563                    emacs-version)
564      (concat (match-string 1 emacs-version)
565              (format "/%d.%d" emacs-major-version emacs-minor-version)
566              (if (match-beginning 3)
567                  (match-string 3 emacs-version)
568                "")
569              (if (boundp 'xemacs-codename)
570                  (concat " (" xemacs-codename ")")
571                "")))
572     (t emacs-version))))
573
574 \f
575 ;;;
576 ;;; Gnus Mail Functions
577 ;;;
578
579 ;;; Mail reply commands of Gnus summary mode
580
581 (defun gnus-summary-reply (&optional yank wide)
582   "Start composing a reply mail to the current message.
583 If prefix argument YANK is non-nil, the original article is yanked
584 automatically."
585   (interactive
586    (list (and current-prefix-arg
587               (gnus-summary-work-articles 1))))
588   ;; Stripping headers should be specified with mail-yank-ignored-headers.
589   (when yank
590     (gnus-summary-goto-subject (car yank)))
591   (let ((gnus-article-reply t))
592     (gnus-setup-message (if yank 'reply-yank 'reply)
593       (gnus-summary-select-article)
594       (set-buffer (gnus-copy-article-buffer))
595       (gnus-msg-treat-broken-reply-to)
596       (message-reply nil wide)
597       (when yank
598         (gnus-inews-yank-articles yank)))))
599
600 (defun gnus-summary-reply-with-original (n &optional wide)
601   "Start composing a reply mail to the current message.
602 The original article will be yanked."
603   (interactive "P")
604   (gnus-summary-reply (gnus-summary-work-articles n) wide))
605
606 (defun gnus-summary-wide-reply (&optional yank)
607   "Start composing a wide reply mail to the current message.
608 If prefix argument YANK is non-nil, the original article is yanked
609 automatically."
610   (interactive
611    (list (and current-prefix-arg
612               (gnus-summary-work-articles 1))))
613   (gnus-summary-reply yank t))
614
615 (defun gnus-summary-wide-reply-with-original (n)
616   "Start composing a wide reply mail to the current message.
617 The original article will be yanked."
618   (interactive "P")
619   (gnus-summary-reply-with-original n t))
620
621 (defun gnus-summary-mail-forward (&optional full-headers post)
622   "Forward the current message to another user.
623 If FULL-HEADERS (the prefix), include full headers when forwarding."
624   (interactive "P")
625   (gnus-setup-message 'forward
626     (gnus-summary-select-article)
627     (let (text)
628       (save-excursion
629         (set-buffer gnus-original-article-buffer)
630         (setq text (buffer-string)))
631       (set-buffer (gnus-get-buffer-create " *Gnus forward*"))
632       (erase-buffer)
633       (insert text)
634       (run-hooks 'gnus-article-decode-hook)
635       (let ((message-included-forward-headers
636              (if full-headers "" message-included-forward-headers)))
637         (message-forward post)))))
638
639 (defun gnus-summary-resend-message (address n)
640   "Resend the current article to ADDRESS."
641   (interactive "sResend message(s) to: \nP")
642   (let ((articles (gnus-summary-work-articles n))
643         article)
644     (while (setq article (pop articles))
645       (gnus-summary-select-article nil nil nil article)
646       (save-excursion
647         (set-buffer gnus-original-article-buffer)
648         (message-resend address)))))
649
650 (defun gnus-summary-post-forward (&optional full-headers)
651   "Forward the current article to a newsgroup.
652 If FULL-HEADERS (the prefix), include full headers when forwarding."
653   (interactive "P")
654   (gnus-summary-mail-forward full-headers t))
655
656 (defvar gnus-nastygram-message
657   "The following article was inappropriately posted to %s.\n\n"
658   "Format string to insert in nastygrams.
659 The current group name will be inserted at \"%s\".")
660
661 (defun gnus-summary-mail-nastygram (n)
662   "Send a nastygram to the author of the current article."
663   (interactive "P")
664   (when (or gnus-expert-user
665             (gnus-y-or-n-p
666              "Really send a nastygram to the author of the current article? "))
667     (let ((group gnus-newsgroup-name))
668       (gnus-summary-reply-with-original n)
669       (set-buffer gnus-message-buffer)
670       (message-goto-body)
671       (insert (format gnus-nastygram-message group))
672       (message-send-and-exit))))
673
674 (defun gnus-summary-mail-crosspost-complaint (n)
675   "Send a complaint about crossposting to the current article(s)."
676   (interactive "P")
677   (let ((articles (gnus-summary-work-articles n))
678         article)
679     (while (setq article (pop articles))
680       (set-buffer gnus-summary-buffer)
681       (gnus-summary-goto-subject article)
682       (let ((group (gnus-group-real-name gnus-newsgroup-name))
683             newsgroups followup-to)
684         (gnus-summary-select-article)
685         (set-buffer gnus-original-article-buffer)
686         (if (and (<= (length (message-tokenize-header
687                               (setq newsgroups
688                                     (mail-fetch-field "newsgroups"))
689                               ", "))
690                      1)
691                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
692                      (not (member group (message-tokenize-header
693                                          followup-to ", ")))))
694             (if followup-to
695                 (gnus-message 1 "Followup-to restricted")
696               (gnus-message 1 "Not a crossposted article"))
697           (set-buffer gnus-summary-buffer)
698           (gnus-summary-reply-with-original 1)
699           (set-buffer gnus-message-buffer)
700           (message-goto-body)
701           (insert (format gnus-crosspost-complaint newsgroups group))
702           (message-goto-subject)
703           (re-search-forward " *$")
704           (replace-match " (crosspost notification)" t t)
705           (gnus-deactivate-mark)
706           (when (gnus-y-or-n-p "Send this complaint? ")
707             (message-send-and-exit)))))))
708
709 (defun gnus-summary-mail-other-window ()
710   "Compose mail in other window."
711   (interactive)
712   (gnus-setup-message 'message
713     (message-mail)))
714
715 (defun gnus-mail-parse-comma-list ()
716   (let (accumulated
717         beg)
718     (skip-chars-forward " ")
719     (while (not (eobp))
720       (setq beg (point))
721       (skip-chars-forward "^,")
722       (while (zerop
723               (save-excursion
724                 (save-restriction
725                   (let ((i 0))
726                     (narrow-to-region beg (point))
727                     (goto-char beg)
728                     (logand (progn
729                               (while (search-forward "\"" nil t)
730                                 (incf i))
731                               (if (zerop i) 2 i))
732                             2)))))
733         (skip-chars-forward ",")
734         (skip-chars-forward "^,"))
735       (skip-chars-backward " ")
736       (push (buffer-substring beg (point))
737             accumulated)
738       (skip-chars-forward "^,")
739       (skip-chars-forward ", "))
740     accumulated))
741
742 (defun gnus-inews-add-to-address (group)
743   (let ((to-address (mail-fetch-field "to")))
744     (when (and to-address
745                (gnus-alive-p))
746       ;; This mail group doesn't have a `to-list', so we add one
747       ;; here.  Magic!
748       (when (gnus-y-or-n-p
749              (format "Do you want to add this as `to-list': %s " to-address))
750         (gnus-group-add-parameter group (cons 'to-list to-address))))))
751
752 (defun gnus-put-message ()
753   "Put the current message in some group and return to Gnus."
754   (interactive)
755   (let ((reply gnus-article-reply)
756         (winconf gnus-prev-winconf)
757         (group gnus-newsgroup-name))
758
759     (or (and group (not (gnus-group-read-only-p group)))
760         (setq group (read-string "Put in group: " nil
761                                  (gnus-writable-groups))))
762     (when (gnus-gethash group gnus-newsrc-hashtb)
763       (error "No such group: %s" group))
764
765     (save-excursion
766       (save-restriction
767         (widen)
768         (message-narrow-to-headers)
769         (let (gnus-deletable-headers)
770           (if (message-news-p)
771               (message-generate-headers message-required-news-headers)
772             (message-generate-headers message-required-mail-headers)))
773         (goto-char (point-max))
774         (insert "Gcc: " group "\n")
775         (widen)))
776
777     (gnus-inews-do-gcc)
778
779     (when (get-buffer gnus-group-buffer)
780       (when (gnus-buffer-exists-p (car-safe reply))
781         (set-buffer (car reply))
782         (and (cdr reply)
783              (gnus-summary-mark-article-as-replied
784               (cdr reply))))
785       (when winconf
786         (set-window-configuration winconf)))))
787
788 (defun gnus-article-mail (yank)
789   "Send a reply to the address near point.
790 If YANK is non-nil, include the original article."
791   (interactive "P")
792   (let ((address
793          (buffer-substring
794           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
795           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
796     (when address
797       (message-reply address)
798       (when yank
799         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
800
801 (defvar nntp-server-type)
802 (defun gnus-bug ()
803   "Send a bug report to the Gnus maintainers."
804   (interactive)
805   (unless (gnus-alive-p)
806     (error "Gnus has been shut down"))
807   (gnus-setup-message 'bug
808     (delete-other-windows)
809     (when gnus-bug-create-help-buffer
810       (switch-to-buffer "*Gnus Help Bug*")
811       (erase-buffer)
812       (insert gnus-bug-message)
813       (goto-char (point-min)))
814     (message-pop-to-buffer "*Gnus Bug*")
815     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
816     (when gnus-bug-create-help-buffer
817       (push `(gnus-bug-kill-buffer) message-send-actions))
818     (goto-char (point-min))
819     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
820     (forward-line 1)
821     (insert (gnus-version) "\n"
822             (emacs-version) "\n")
823     (when (and (boundp 'nntp-server-type)
824                (stringp nntp-server-type))
825       (insert nntp-server-type))
826     (insert "\n\n\n\n\n")
827     (gnus-debug)
828     (goto-char (point-min))
829     (search-forward "Subject: " nil t)
830     (message "")))
831
832 (defun gnus-bug-kill-buffer ()
833   (when (get-buffer "*Gnus Help Bug*")
834     (kill-buffer "*Gnus Help Bug*")))
835
836 (defun gnus-debug ()
837   "Attempts to go through the Gnus source file and report what variables have been changed.
838 The source file has to be in the Emacs load path."
839   (interactive)
840   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
841                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
842                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
843                  "nnmail.el" "message.el"))
844         (point (point))
845         file expr olist sym)
846     (gnus-message 4 "Please wait while we snoop your variables...")
847     (sit-for 0)
848     ;; Go through all the files looking for non-default values for variables.
849     (save-excursion
850       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
851       (while files
852         (erase-buffer)
853         (when (and (setq file (locate-library (pop files)))
854                    (file-exists-p file))
855           (insert-file-contents file)
856           (goto-char (point-min))
857           (if (not (re-search-forward "^;;* *Internal variables" nil t))
858               (gnus-message 4 "Malformed sources in file %s" file)
859             (narrow-to-region (point-min) (point))
860             (goto-char (point-min))
861             (while (setq expr (ignore-errors (read (current-buffer))))
862               (ignore-errors
863                 (and (or (eq (car expr) 'defvar)
864                          (eq (car expr) 'defcustom))
865                      (stringp (nth 3 expr))
866                      (or (not (boundp (nth 1 expr)))
867                          (not (equal (eval (nth 2 expr))
868                                      (symbol-value (nth 1 expr)))))
869                      (push (nth 1 expr) olist)))))))
870       (kill-buffer (current-buffer)))
871     (when (setq olist (nreverse olist))
872       (insert "------------------ Environment follows ------------------\n\n"))
873     (while olist
874       (if (boundp (car olist))
875           (condition-case ()
876               (pp `(setq ,(car olist)
877                          ,(if (or (consp (setq sym (symbol-value (car olist))))
878                                   (and (symbolp sym)
879                                        (not (or (eq sym nil)
880                                                 (eq sym t)))))
881                               (list 'quote (symbol-value (car olist)))
882                             (symbol-value (car olist))))
883                   (current-buffer))
884             (error
885              (format "(setq %s 'whatever)\n" (car olist))))
886         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
887       (setq olist (cdr olist)))
888     (insert "\n\n")
889     ;; Remove any control chars - they seem to cause trouble for some
890     ;; mailers.  (Byte-compiled output from the stuff above.)
891     (goto-char point)
892     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
893       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
894                      t t))))
895
896 ;;; Treatment of rejected articles.
897 ;;; Bounced mail.
898
899 (defun gnus-summary-resend-bounced-mail (&optional fetch)
900   "Re-mail the current message.
901 This only makes sense if the current message is a bounce message than
902 contains some mail you have written which has been bounced back to
903 you.
904 If FETCH, try to fetch the article that this is a reply to, if indeed
905 this is a reply."
906   (interactive "P")
907   (gnus-summary-select-article t)
908   (set-buffer gnus-original-article-buffer)
909   (gnus-setup-message 'compose-bounce
910     (let* ((references (mail-fetch-field "references"))
911            (parent (and references (gnus-parent-id references))))
912       (message-bounce)
913       ;; If there are references, we fetch the article we answered to.
914       (and fetch parent
915            (gnus-summary-refer-article parent)
916            (gnus-summary-show-all-headers)))))
917
918 ;;; Gcc handling.
919
920 ;; Do Gcc handling, which copied the message over to some group.
921 (defun gnus-inews-do-gcc (&optional gcc)
922   (interactive)
923   (when (gnus-alive-p)
924     (save-excursion
925       (save-restriction
926         (message-narrow-to-headers)
927         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
928               (cur (current-buffer))
929               groups group method)
930           (when gcc
931             (message-remove-header "gcc")
932             (widen)
933             (setq groups (message-tokenize-header gcc " ,"))
934             ;; Copy the article over to some group(s).
935             (while (setq group (pop groups))
936               (gnus-check-server
937                (setq method
938                      (cond ((and (null (gnus-get-info group))
939                                  (eq (car gnus-message-archive-method)
940                                      (car
941                                       (gnus-server-to-method
942                                        (gnus-group-method group)))))
943                             ;; If the group doesn't exist, we assume
944                             ;; it's an archive group...
945                             gnus-message-archive-method)
946                            ;; Use the method.
947                            ((gnus-info-method (gnus-get-info group))
948                             (gnus-info-method (gnus-get-info group)))
949                            ;; Find the method.
950                            (t (gnus-group-method group)))))
951               (gnus-check-server method)
952               (unless (gnus-request-group group t method)
953                 (gnus-request-create-group group method))
954               (save-excursion
955                 (nnheader-set-temp-buffer " *acc*")
956                 (insert-buffer-substring cur)
957                 (goto-char (point-min))
958                 (when (re-search-forward
959                        (concat "^" (regexp-quote mail-header-separator) "$")
960                        nil t)
961                   (replace-match "" t t ))
962                 (unless (gnus-request-accept-article group method t t)
963                   (gnus-message 1 "Couldn't store article in group %s: %s"
964                                 group (gnus-status-message method))
965                   (sit-for 2))
966                 (kill-buffer (current-buffer))))))))))
967
968 (defun gnus-inews-insert-gcc ()
969   "Insert Gcc headers based on `gnus-outgoing-message-group'."
970   (save-excursion
971     (save-restriction
972       (message-narrow-to-headers)
973       (let* ((group gnus-outgoing-message-group)
974              (gcc (cond
975                    ((gnus-functionp group)
976                     (funcall group))
977                    ((or (stringp group) (list group))
978                     group))))
979         (when gcc
980           (insert "Gcc: "
981                   (if (stringp gcc) gcc
982                     (mapconcat 'identity gcc " "))
983                   "\n"))))))
984
985 (defun gnus-inews-insert-archive-gcc (&optional group)
986   "Insert the Gcc to say where the article is to be archived."
987   (let* ((var gnus-message-archive-group)
988          (group (or group gnus-newsgroup-name ""))
989          (gcc-self-val
990           (and gnus-newsgroup-name
991                (gnus-group-find-parameter
992                 gnus-newsgroup-name 'gcc-self)))
993          result
994          (groups
995           (cond
996            ((null gnus-message-archive-method)
997             ;; Ignore.
998             nil)
999            ((stringp var)
1000             ;; Just a single group.
1001             (list var))
1002            ((null var)
1003             ;; We don't want this.
1004             nil)
1005            ((and (listp var) (stringp (car var)))
1006             ;; A list of groups.
1007             var)
1008            ((gnus-functionp var)
1009             ;; A function.
1010             (funcall var group))
1011            (t
1012             ;; An alist of regexps/functions/forms.
1013             (while (and var
1014                         (not
1015                          (setq result
1016                                (cond
1017                                 ((stringp (caar var))
1018                                  ;; Regexp.
1019                                  (when (string-match (caar var) group)
1020                                    (cdar var)))
1021                                 ((gnus-functionp (car var))
1022                                  ;; Function.
1023                                  (funcall (car var) group))
1024                                 (t
1025                                  (eval (car var)))))))
1026               (setq var (cdr var)))
1027             result)))
1028          name)
1029     (when (or groups gcc-self-val)
1030       (when (stringp groups)
1031         (setq groups (list groups)))
1032       (save-excursion
1033         (save-restriction
1034           (message-narrow-to-headers)
1035           (goto-char (point-max))
1036           (insert "Gcc: ")
1037           (if gcc-self-val
1038               ;; Use the `gcc-self' param value instead.
1039               (progn
1040                 (insert
1041                  (if (stringp gcc-self-val)
1042                      gcc-self-val
1043                    group))
1044                 (if (not (eq gcc-self-val 'none))
1045                     (insert "\n")
1046                   (progn
1047                     (beginning-of-line)
1048                     (kill-line))))
1049             ;; Use the list of groups.
1050             (while (setq name (pop groups))
1051               (insert (if (string-match ":" name)
1052                           name
1053                         (gnus-group-prefixed-name
1054                          name gnus-message-archive-method)))
1055               (when groups
1056                 (insert " ")))
1057             (insert "\n")))))))
1058
1059 ;;; Posting styles.
1060
1061 (defvar gnus-message-style-insertions nil)
1062
1063 (defun gnus-configure-posting-styles ()
1064   "Configure posting styles according to `gnus-posting-styles'."
1065   (unless gnus-inhibit-posting-styles
1066     (let ((styles gnus-posting-styles)
1067           (gnus-newsgroup-name (or gnus-newsgroup-name ""))
1068           style match variable attribute value value-value)
1069       (make-local-variable 'gnus-message-style-insertions)
1070       ;; If the group has a posting-style parameter, add it at the end with a
1071       ;; regexp matching everything, to be sure it takes precedence over all
1072       ;; the others.
1073       (unless (zerop (length gnus-newsgroup-name))
1074         (let ((tmp-style (gnus-group-find-parameter
1075                           gnus-newsgroup-name 'posting-style t)))
1076           (when tmp-style
1077             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1078       ;; Go through all styles and look for matches.
1079       (while styles
1080         (setq style (pop styles)
1081               match (pop style))
1082         (when (cond
1083                ((stringp match)
1084                 ;; Regexp string match on the group name.
1085                 (string-match match gnus-newsgroup-name))
1086                ((or (symbolp match)
1087                     (gnus-functionp match))
1088                 (cond
1089                  ((gnus-functionp match)
1090                   ;; Function to be called.
1091                   (funcall match))
1092                  ((boundp match)
1093                   ;; Variable to be checked.
1094                   (symbol-value match))))
1095                ((listp match)
1096                 ;; This is a form to be evaled.
1097                 (eval match)))
1098           ;; We have a match, so we set the variables.
1099           (while style
1100             (setq attribute (pop style)
1101                   value (cadr attribute)
1102                   variable nil)
1103             ;; We find the variable that is to be modified.
1104             (if (and (not (stringp (car attribute)))
1105                      (not (eq 'body (car attribute)))
1106                      (not (setq variable
1107                                 (cdr (assq (car attribute)
1108                                            gnus-posting-style-alist)))))
1109                 (message "Couldn't find attribute %s" (car attribute))
1110               ;; We get the value.
1111               (setq value-value
1112                     (cond
1113                      ((stringp value)
1114                       value)
1115                      ((or (symbolp value)
1116                           (gnus-functionp value))
1117                       (cond ((gnus-functionp value)
1118                              (funcall value))
1119                             ((boundp value)
1120                              (symbol-value value))))
1121                      ((listp value)
1122                       (eval value))))
1123               (if variable
1124                   ;; This is an ordinary variable.
1125                   (set (make-local-variable variable) value-value)
1126                 ;; This is either a body or a header to be inserted in the
1127                 ;; message.
1128                 (let ((attr (car attribute)))
1129                   (make-local-variable 'message-setup-hook)
1130                   (if (eq 'body attr)
1131                       (add-hook 'message-setup-hook
1132                                 `(lambda ()
1133                                    (save-excursion
1134                                      (message-goto-body)
1135                                      (insert ,value-value))))
1136                     (add-hook 'message-setup-hook
1137                               'gnus-message-insert-stylings)
1138                     (push (cons (if (stringp attr) attr
1139                                   (symbol-name attr))
1140                                 value-value)
1141                           gnus-message-style-insertions)))))))))))
1142
1143 (defun gnus-message-insert-stylings ()
1144   (let (val)
1145     (save-excursion
1146       (message-goto-eoh)
1147       (while (setq val (pop gnus-message-style-insertions))
1148         (when (cdr val)
1149           (insert (car val) ": " (cdr val) "\n"))
1150         (gnus-pull (car val) gnus-message-style-insertions t)))))
1151
1152 ;;; Allow redefinition of functions.
1153
1154 (gnus-ems-redefine)
1155
1156 (provide 'gnus-msg)
1157
1158 ;;; gnus-msg.el ends here