*** 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          (make-local-variable 'gnus-newsgroup-name)
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   (let ((gnus-newsgroup-name
237          (if arg
238              (if (= 1 (prefix-numeric-value arg))
239                  (completing-read "Use posting style of group: "
240                                   gnus-active-hashtb nil
241                                   (gnus-read-active-file-p))
242                (gnus-group-group-name))
243            "")))
244     (gnus-setup-message 'message (message-mail))))
245
246 (defun gnus-group-post-news (&optional arg)
247   "Start composing a news message.
248 If ARG, post to the group under point.
249 If ARG is 1, prompt for a group name."
250   (interactive "P")
251   ;; Bind this variable here to make message mode hooks work ok.
252   (let ((gnus-newsgroup-name
253          (if arg
254              (if (= 1 (prefix-numeric-value arg))
255                  (completing-read "Newsgroup: " gnus-active-hashtb nil
256                                   (gnus-read-active-file-p))
257                (gnus-group-group-name))
258            "")))
259     (gnus-post-news 'post gnus-newsgroup-name)))
260
261 (defun gnus-summary-post-news ()
262   "Start composing a news message."
263   (interactive)
264   (gnus-post-news 'post gnus-newsgroup-name))
265
266 (defun gnus-summary-followup (yank &optional force-news)
267   "Compose a followup to an article.
268 If prefix argument YANK is non-nil, original article is yanked automatically."
269   (interactive
270    (list (and current-prefix-arg
271               (gnus-summary-work-articles 1))))
272   (when yank
273     (gnus-summary-goto-subject (car yank)))
274   (save-window-excursion
275     (gnus-summary-select-article))
276   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
277         (gnus-newsgroup-name gnus-newsgroup-name))
278     ;; Send a followup.
279     (gnus-post-news nil gnus-newsgroup-name
280                     headers gnus-article-buffer
281                     yank nil force-news)))
282
283 (defun gnus-summary-followup-with-original (n &optional force-news)
284   "Compose a followup to an article and include the original article."
285   (interactive "P")
286   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
287
288 (defun gnus-summary-followup-to-mail (&optional arg)
289   "Followup to the current mail message via news."
290   (interactive
291    (list (and current-prefix-arg
292               (gnus-summary-work-articles 1))))
293   (gnus-summary-followup arg t))
294
295 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
296   "Followup to the current mail message via news."
297   (interactive "P")
298   (gnus-summary-followup (gnus-summary-work-articles arg) t))
299
300 (defun gnus-inews-yank-articles (articles)
301   (let (beg article)
302     (message-goto-body)
303     (while (setq article (pop articles))
304       (save-window-excursion
305         (set-buffer gnus-summary-buffer)
306         (gnus-summary-select-article nil nil nil article)
307         (gnus-summary-remove-process-mark article))
308       (gnus-copy-article-buffer)
309       (let ((message-reply-buffer gnus-article-copy)
310             (message-reply-headers gnus-current-headers))
311         (message-yank-original)
312         (setq beg (or beg (mark t))))
313       (when articles
314         (insert "\n")))
315     (push-mark)
316     (goto-char beg)))
317
318 (defun gnus-summary-cancel-article (&optional n symp)
319   "Cancel an article you posted.
320 Uses the process-prefix convention.  If given the symbolic
321 prefix `a', cancel using the standard posting method; if not
322 post using the current select method."
323   (interactive (gnus-interactive "P\ny"))
324   (let ((articles (gnus-summary-work-articles n))
325         (message-post-method
326          `(lambda (arg)
327             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
328         article)
329     (while (setq article (pop articles))
330       (when (gnus-summary-select-article t nil nil article)
331         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
332                 (message-cancel-news))
333           (gnus-summary-mark-as-read article gnus-canceled-mark)
334           (gnus-cache-remove-article 1))
335         (gnus-article-hide-headers-if-wanted))
336       (gnus-summary-remove-process-mark article))))
337
338 (defun gnus-summary-supersede-article ()
339   "Compose an article that will supersede a previous article.
340 This is done simply by taking the old article and adding a Supersedes
341 header line with the old Message-ID."
342   (interactive)
343   (let ((article (gnus-summary-article-number)))
344     (gnus-setup-message 'reply-yank
345       (gnus-summary-select-article t)
346       (set-buffer gnus-original-article-buffer)
347       (message-supersede)
348       (push
349        `((lambda ()
350            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
351              (save-excursion
352                (set-buffer ,gnus-summary-buffer)
353                (gnus-cache-possibly-remove-article ,article nil nil nil t)
354                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
355        message-send-actions))))
356
357 \f
358
359 (defun gnus-copy-article-buffer (&optional article-buffer)
360   ;; make a copy of the article buffer with all text properties removed
361   ;; this copy is in the buffer gnus-article-copy.
362   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
363   ;; this buffer should be passed to all mail/news reply/post routines.
364   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
365   (save-excursion
366     (set-buffer gnus-article-copy)
367     (mm-enable-multibyte))
368   (let ((article-buffer (or article-buffer gnus-article-buffer))
369         end beg)
370     (if (not (and (get-buffer article-buffer)
371                   (gnus-buffer-exists-p article-buffer)))
372         (error "Can't find any article buffer")
373       (save-excursion
374         (set-buffer article-buffer)
375         (save-restriction
376           ;; Copy over the (displayed) article buffer, delete
377           ;; hidden text and remove text properties.
378           (widen)
379           (copy-to-buffer gnus-article-copy (point-min) (point-max))
380           (set-buffer gnus-article-copy)
381           (gnus-article-delete-text-of-type 'annotation)
382           (gnus-remove-text-with-property 'gnus-prev)
383           (gnus-remove-text-with-property 'gnus-next)
384           (insert
385            (prog1
386                (format "%s" (buffer-string))
387              (erase-buffer)))
388           ;; Find the original headers.
389           (set-buffer gnus-original-article-buffer)
390           (goto-char (point-min))
391           (while (looking-at message-unix-mail-delimiter)
392             (forward-line 1))
393           (setq beg (point))
394           (setq end (or (search-forward "\n\n" nil t) (point)))
395           ;; Delete the headers from the displayed articles.
396           (set-buffer gnus-article-copy)
397           (delete-region (goto-char (point-min))
398                          (or (search-forward "\n\n" nil t) (point)))
399           ;; Insert the original article headers.
400           (insert-buffer-substring gnus-original-article-buffer beg end)
401           (article-decode-encoded-words)))
402       gnus-article-copy)))
403
404 (defun gnus-post-news (post &optional group header article-buffer yank subject
405                             force-news)
406   (when article-buffer
407     (gnus-copy-article-buffer))
408   (let ((gnus-article-reply article-buffer)
409         (add-to-list gnus-add-to-list))
410     (gnus-setup-message (cond (yank 'reply-yank)
411                               (article-buffer 'reply)
412                               (t 'message))
413       (let* ((group (or group gnus-newsgroup-name))
414              (pgroup group)
415              to-address to-group mailing-list to-list
416              newsgroup-p)
417         (when group
418           (setq to-address (gnus-group-find-parameter group 'to-address)
419                 to-group (gnus-group-find-parameter group 'to-group)
420                 to-list (gnus-group-find-parameter group 'to-list)
421                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
422                 mailing-list (when gnus-mailing-list-groups
423                                (string-match gnus-mailing-list-groups group))
424                 group (gnus-group-real-name group)))
425         (if (or (and to-group
426                      (gnus-news-group-p to-group))
427                 newsgroup-p
428                 force-news
429                 (and (gnus-news-group-p
430                       (or pgroup gnus-newsgroup-name)
431                       (if header (mail-header-number header)
432                         gnus-current-article))
433                      (not mailing-list)
434                      (not to-list)
435                      (not to-address)))
436             ;; This is news.
437             (if post
438                 (message-news (or to-group group))
439               (set-buffer gnus-article-copy)
440               (gnus-msg-treat-broken-reply-to)
441               (message-followup (if (or newsgroup-p force-news) nil to-group)))
442           ;; The is mail.
443           (if post
444               (progn
445                 (message-mail (or to-address to-list))
446                 ;; Arrange for mail groups that have no `to-address' to
447                 ;; get that when the user sends off the mail.
448                 (when (and (not to-list)
449                            (not to-address)
450                            add-to-list)
451                   (push (list 'gnus-inews-add-to-address pgroup)
452                         message-send-actions)))
453             (set-buffer gnus-article-copy)
454             (gnus-msg-treat-broken-reply-to)
455             (message-wide-reply to-address)))
456         (when yank
457           (gnus-inews-yank-articles yank))))))
458
459 (defun gnus-msg-treat-broken-reply-to ()
460   "Remove the Reply-to header iff broken-reply-to."
461   (when (gnus-group-find-parameter
462          gnus-newsgroup-name 'broken-reply-to)
463     (save-restriction
464       (message-narrow-to-head)
465       (message-remove-header "reply-to"))))
466
467 (defun gnus-post-method (arg group &optional silent)
468   "Return the posting method based on GROUP and ARG.
469 If SILENT, don't prompt the user."
470   (let ((group-method (gnus-find-method-for-group group)))
471     (cond
472      ;; If the group-method is nil (which shouldn't happen) we use
473      ;; the default method.
474      ((null group-method)
475       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
476                gnus-select-method message-post-method))
477      ;; We want the inverse of the default
478      ((and arg (not (eq arg 0)))
479       (if (eq gnus-post-method 'active)
480           gnus-select-method
481         group-method))
482      ;; We query the user for a post method.
483      ((or arg
484           (and gnus-post-method
485                (not (eq gnus-post-method 'current))
486                (listp (car gnus-post-method))))
487       (let* ((methods
488               ;; Collect all methods we know about.
489               (append
490                (when (and gnus-post-method
491                           (not (eq gnus-post-method 'current)))
492                  (if (listp (car gnus-post-method))
493                      gnus-post-method
494                    (list gnus-post-method)))
495                gnus-secondary-select-methods
496                (mapcar 'cdr gnus-server-alist)
497                (mapcar 'car gnus-opened-servers)
498                (list gnus-select-method)
499                (list group-method)))
500              method-alist post-methods method)
501         ;; Weed out all mail methods.
502         (while methods
503           (setq method (gnus-server-get-method "" (pop methods)))
504           (when (and (or (gnus-method-option-p method 'post)
505                          (gnus-method-option-p method 'post-mail))
506                      (not (member method post-methods)))
507             (push method post-methods)))
508         ;; Create a name-method alist.
509         (setq method-alist
510               (mapcar
511                (lambda (m)
512                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
513                post-methods))
514         ;; Query the user.
515         (cadr
516          (assoc
517           (setq gnus-last-posting-server
518                 (if (and silent
519                          gnus-last-posting-server)
520                     ;; Just use the last value.
521                     gnus-last-posting-server
522                   (completing-read
523                    "Posting method: " method-alist nil t
524                    (cons (or gnus-last-posting-server "") 0))))
525           method-alist))))
526      ;; Override normal method.
527      ((and (eq gnus-post-method 'current)
528            (not (eq (car group-method) 'nndraft))
529            (not arg))
530       group-method)
531      ((and gnus-post-method
532            (not (eq gnus-post-method 'current)))
533       gnus-post-method)
534      ;; Use the normal select method.
535      (t gnus-select-method))))
536
537 \f
538
539 ;; Dummies to avoid byte-compile warning.
540 (defvar nnspool-rejected-article-hook)
541 (defvar xemacs-codename)
542
543 (defun gnus-extended-version ()
544   "Stringified Gnus version and Emacs version."
545   (interactive)
546   (concat
547    "Gnus/" (prin1-to-string (gnus-continuum-version gnus-version) t)
548    " (" gnus-version ")"
549    " "
550    (cond
551     ((string-match "^\\([0-9]+\\.[0-9]+\\)\\.[.0-9]+$" emacs-version)
552      (concat "Emacs/" (match-string 1 emacs-version)))
553     ((string-match "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
554                    emacs-version)
555      (concat (match-string 1 emacs-version)
556              (format "/%d.%d" emacs-major-version emacs-minor-version)
557              (if (match-beginning 3)
558                  (match-string 3 emacs-version)
559                "")
560              (if (boundp 'xemacs-codename)
561                  (concat " (" xemacs-codename ")")
562                "")))
563     (t emacs-version))))
564
565 \f
566 ;;;
567 ;;; Gnus Mail Functions
568 ;;;
569
570 ;;; Mail reply commands of Gnus summary mode
571
572 (defun gnus-summary-reply (&optional yank wide)
573   "Start composing a reply mail to the current message.
574 If prefix argument YANK is non-nil, the original article is yanked
575 automatically."
576   (interactive
577    (list (and current-prefix-arg
578               (gnus-summary-work-articles 1))))
579   ;; Stripping headers should be specified with mail-yank-ignored-headers.
580   (when yank
581     (gnus-summary-goto-subject (car yank)))
582   (let ((gnus-article-reply t))
583     (gnus-setup-message (if yank 'reply-yank 'reply)
584       (gnus-summary-select-article)
585       (set-buffer (gnus-copy-article-buffer))
586       (gnus-msg-treat-broken-reply-to)
587       (message-reply nil wide)
588       (when yank
589         (gnus-inews-yank-articles yank)))))
590
591 (defun gnus-summary-reply-with-original (n &optional wide)
592   "Start composing a reply mail to the current message.
593 The original article will be yanked."
594   (interactive "P")
595   (gnus-summary-reply (gnus-summary-work-articles n) wide))
596
597 (defun gnus-summary-wide-reply (&optional yank)
598   "Start composing a wide reply mail to the current message.
599 If prefix argument YANK is non-nil, the original article is yanked
600 automatically."
601   (interactive
602    (list (and current-prefix-arg
603               (gnus-summary-work-articles 1))))
604   (gnus-summary-reply yank t))
605
606 (defun gnus-summary-wide-reply-with-original (n)
607   "Start composing a wide reply mail to the current message.
608 The original article will be yanked."
609   (interactive "P")
610   (gnus-summary-reply-with-original n t))
611
612 (defun gnus-summary-mail-forward (&optional full-headers post)
613   "Forward the current message to another user.
614 If FULL-HEADERS (the prefix), include full headers when forwarding."
615   (interactive "P")
616   (gnus-setup-message 'forward
617     (gnus-summary-select-article)
618     (let (text)
619       (save-excursion
620         (set-buffer gnus-original-article-buffer)
621         (setq text (buffer-string)))
622       (set-buffer (gnus-get-buffer-create " *Gnus forward*"))
623       (insert text)
624       (run-hooks 'gnus-article-decode-hook)
625       (let ((message-included-forward-headers
626              (if full-headers "" message-included-forward-headers)))
627         (message-forward post)))))
628
629 (defun gnus-summary-resend-message (address n)
630   "Resend the current article to ADDRESS."
631   (interactive "sResend message(s) to: \nP")
632   (let ((articles (gnus-summary-work-articles n))
633         article)
634     (while (setq article (pop articles))
635       (gnus-summary-select-article nil nil nil article)
636       (save-excursion
637         (set-buffer gnus-original-article-buffer)
638         (message-resend address)))))
639
640 (defun gnus-summary-post-forward (&optional full-headers)
641   "Forward the current article to a newsgroup.
642 If FULL-HEADERS (the prefix), include full headers when forwarding."
643   (interactive "P")
644   (gnus-summary-mail-forward full-headers t))
645
646 (defvar gnus-nastygram-message
647   "The following article was inappropriately posted to %s.\n\n"
648   "Format string to insert in nastygrams.
649 The current group name will be inserted at \"%s\".")
650
651 (defun gnus-summary-mail-nastygram (n)
652   "Send a nastygram to the author of the current article."
653   (interactive "P")
654   (when (or gnus-expert-user
655             (gnus-y-or-n-p
656              "Really send a nastygram to the author of the current article? "))
657     (let ((group gnus-newsgroup-name))
658       (gnus-summary-reply-with-original n)
659       (set-buffer gnus-message-buffer)
660       (message-goto-body)
661       (insert (format gnus-nastygram-message group))
662       (message-send-and-exit))))
663
664 (defun gnus-summary-mail-crosspost-complaint (n)
665   "Send a complaint about crossposting to the current article(s)."
666   (interactive "P")
667   (let ((articles (gnus-summary-work-articles n))
668         article)
669     (while (setq article (pop articles))
670       (set-buffer gnus-summary-buffer)
671       (gnus-summary-goto-subject article)
672       (let ((group (gnus-group-real-name gnus-newsgroup-name))
673             newsgroups followup-to)
674         (gnus-summary-select-article)
675         (set-buffer gnus-original-article-buffer)
676         (if (and (<= (length (message-tokenize-header
677                               (setq newsgroups
678                                     (mail-fetch-field "newsgroups"))
679                               ", "))
680                      1)
681                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
682                      (not (member group (message-tokenize-header
683                                          followup-to ", ")))))
684             (if followup-to
685                 (gnus-message 1 "Followup-to restricted")
686               (gnus-message 1 "Not a crossposted article"))
687           (set-buffer gnus-summary-buffer)
688           (gnus-summary-reply-with-original 1)
689           (set-buffer gnus-message-buffer)
690           (message-goto-body)
691           (insert (format gnus-crosspost-complaint newsgroups group))
692           (message-goto-subject)
693           (re-search-forward " *$")
694           (replace-match " (crosspost notification)" t t)
695           (gnus-deactivate-mark)
696           (when (gnus-y-or-n-p "Send this complaint? ")
697             (message-send-and-exit)))))))
698
699 (defun gnus-summary-mail-other-window ()
700   "Compose mail in other window."
701   (interactive)
702   (gnus-setup-message 'message
703     (message-mail)))
704
705 (defun gnus-mail-parse-comma-list ()
706   (let (accumulated
707         beg)
708     (skip-chars-forward " ")
709     (while (not (eobp))
710       (setq beg (point))
711       (skip-chars-forward "^,")
712       (while (zerop
713               (save-excursion
714                 (save-restriction
715                   (let ((i 0))
716                     (narrow-to-region beg (point))
717                     (goto-char beg)
718                     (logand (progn
719                               (while (search-forward "\"" nil t)
720                                 (incf i))
721                               (if (zerop i) 2 i))
722                             2)))))
723         (skip-chars-forward ",")
724         (skip-chars-forward "^,"))
725       (skip-chars-backward " ")
726       (push (buffer-substring beg (point))
727             accumulated)
728       (skip-chars-forward "^,")
729       (skip-chars-forward ", "))
730     accumulated))
731
732 (defun gnus-inews-add-to-address (group)
733   (let ((to-address (mail-fetch-field "to")))
734     (when (and to-address
735                (gnus-alive-p))
736       ;; This mail group doesn't have a `to-list', so we add one
737       ;; here.  Magic!
738       (when (gnus-y-or-n-p
739              (format "Do you want to add this as `to-list': %s " to-address))
740         (gnus-group-add-parameter group (cons 'to-list to-address))))))
741
742 (defun gnus-put-message ()
743   "Put the current message in some group and return to Gnus."
744   (interactive)
745   (let ((reply gnus-article-reply)
746         (winconf gnus-prev-winconf)
747         (group gnus-newsgroup-name))
748
749     (or (and group (not (gnus-group-read-only-p group)))
750         (setq group (read-string "Put in group: " nil
751                                  (gnus-writable-groups))))
752     (when (gnus-gethash group gnus-newsrc-hashtb)
753       (error "No such group: %s" group))
754
755     (save-excursion
756       (save-restriction
757         (widen)
758         (message-narrow-to-headers)
759         (let (gnus-deletable-headers)
760           (if (message-news-p)
761               (message-generate-headers message-required-news-headers)
762             (message-generate-headers message-required-mail-headers)))
763         (goto-char (point-max))
764         (insert "Gcc: " group "\n")
765         (widen)))
766
767     (gnus-inews-do-gcc)
768
769     (when (get-buffer gnus-group-buffer)
770       (when (gnus-buffer-exists-p (car-safe reply))
771         (set-buffer (car reply))
772         (and (cdr reply)
773              (gnus-summary-mark-article-as-replied
774               (cdr reply))))
775       (when winconf
776         (set-window-configuration winconf)))))
777
778 (defun gnus-article-mail (yank)
779   "Send a reply to the address near point.
780 If YANK is non-nil, include the original article."
781   (interactive "P")
782   (let ((address
783          (buffer-substring
784           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
785           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
786     (when address
787       (message-reply address)
788       (when yank
789         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
790
791 (defvar nntp-server-type)
792 (defun gnus-bug ()
793   "Send a bug report to the Gnus maintainers."
794   (interactive)
795   (unless (gnus-alive-p)
796     (error "Gnus has been shut down"))
797   (gnus-setup-message 'bug
798     (delete-other-windows)
799     (when gnus-bug-create-help-buffer
800       (switch-to-buffer "*Gnus Help Bug*")
801       (erase-buffer)
802       (insert gnus-bug-message)
803       (goto-char (point-min)))
804     (message-pop-to-buffer "*Gnus Bug*")
805     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
806     (when gnus-bug-create-help-buffer
807       (push `(gnus-bug-kill-buffer) message-send-actions))
808     (goto-char (point-min))
809     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
810     (forward-line 1)
811     (insert (gnus-version) "\n"
812             (emacs-version) "\n")
813     (when (and (boundp 'nntp-server-type)
814                (stringp nntp-server-type))
815       (insert nntp-server-type))
816     (insert "\n\n\n\n\n")
817     (gnus-debug)
818     (goto-char (point-min))
819     (search-forward "Subject: " nil t)
820     (message "")))
821
822 (defun gnus-bug-kill-buffer ()
823   (when (get-buffer "*Gnus Help Bug*")
824     (kill-buffer "*Gnus Help Bug*")))
825
826 (defun gnus-debug ()
827   "Attempts to go through the Gnus source file and report what variables have been changed.
828 The source file has to be in the Emacs load path."
829   (interactive)
830   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
831                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
832                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
833                  "nnmail.el" "message.el"))
834         (point (point))
835         file expr olist sym)
836     (gnus-message 4 "Please wait while we snoop your variables...")
837     (sit-for 0)
838     ;; Go through all the files looking for non-default values for variables.
839     (save-excursion
840       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
841       (while files
842         (erase-buffer)
843         (when (and (setq file (locate-library (pop files)))
844                    (file-exists-p file))
845           (insert-file-contents file)
846           (goto-char (point-min))
847           (if (not (re-search-forward "^;;* *Internal variables" nil t))
848               (gnus-message 4 "Malformed sources in file %s" file)
849             (narrow-to-region (point-min) (point))
850             (goto-char (point-min))
851             (while (setq expr (ignore-errors (read (current-buffer))))
852               (ignore-errors
853                 (and (or (eq (car expr) 'defvar)
854                          (eq (car expr) 'defcustom))
855                      (stringp (nth 3 expr))
856                      (or (not (boundp (nth 1 expr)))
857                          (not (equal (eval (nth 2 expr))
858                                      (symbol-value (nth 1 expr)))))
859                      (push (nth 1 expr) olist)))))))
860       (kill-buffer (current-buffer)))
861     (when (setq olist (nreverse olist))
862       (insert "------------------ Environment follows ------------------\n\n"))
863     (while olist
864       (if (boundp (car olist))
865           (condition-case ()
866               (pp `(setq ,(car olist)
867                          ,(if (or (consp (setq sym (symbol-value (car olist))))
868                                   (and (symbolp sym)
869                                        (not (or (eq sym nil)
870                                                 (eq sym t)))))
871                               (list 'quote (symbol-value (car olist)))
872                             (symbol-value (car olist))))
873                   (current-buffer))
874             (error
875              (format "(setq %s 'whatever)\n" (car olist))))
876         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
877       (setq olist (cdr olist)))
878     (insert "\n\n")
879     ;; Remove any control chars - they seem to cause trouble for some
880     ;; mailers.  (Byte-compiled output from the stuff above.)
881     (goto-char point)
882     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
883       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
884                      t t))))
885
886 ;;; Treatment of rejected articles.
887 ;;; Bounced mail.
888
889 (defun gnus-summary-resend-bounced-mail (&optional fetch)
890   "Re-mail the current message.
891 This only makes sense if the current message is a bounce message than
892 contains some mail you have written which has been bounced back to
893 you.
894 If FETCH, try to fetch the article that this is a reply to, if indeed
895 this is a reply."
896   (interactive "P")
897   (gnus-summary-select-article t)
898   (set-buffer gnus-original-article-buffer)
899   (gnus-setup-message 'compose-bounce
900     (let* ((references (mail-fetch-field "references"))
901            (parent (and references (gnus-parent-id references))))
902       (message-bounce)
903       ;; If there are references, we fetch the article we answered to.
904       (and fetch parent
905            (gnus-summary-refer-article parent)
906            (gnus-summary-show-all-headers)))))
907
908 ;;; Gcc handling.
909
910 ;; Do Gcc handling, which copied the message over to some group.
911 (defun gnus-inews-do-gcc (&optional gcc)
912   (interactive)
913   (when (gnus-alive-p)
914     (save-excursion
915       (save-restriction
916         (message-narrow-to-headers)
917         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
918               (cur (current-buffer))
919               groups group method)
920           (when gcc
921             (message-remove-header "gcc")
922             (widen)
923             (setq groups (message-tokenize-header gcc " ,"))
924             ;; Copy the article over to some group(s).
925             (while (setq group (pop groups))
926               (gnus-check-server
927                (setq method
928                      (cond ((and (null (gnus-get-info group))
929                                  (eq (car gnus-message-archive-method)
930                                      (car
931                                       (gnus-server-to-method
932                                        (gnus-group-method group)))))
933                             ;; If the group doesn't exist, we assume
934                             ;; it's an archive group...
935                             gnus-message-archive-method)
936                            ;; Use the method.
937                            ((gnus-info-method (gnus-get-info group))
938                             (gnus-info-method (gnus-get-info group)))
939                            ;; Find the method.
940                            (t (gnus-group-method group)))))
941               (gnus-check-server method)
942               (unless (gnus-request-group group t method)
943                 (gnus-request-create-group group method))
944               (save-excursion
945                 (nnheader-set-temp-buffer " *acc*")
946                 (insert-buffer-substring cur)
947                 (goto-char (point-min))
948                 (when (re-search-forward
949                        (concat "^" (regexp-quote mail-header-separator) "$")
950                        nil t)
951                   (replace-match "" t t ))
952                 (unless (gnus-request-accept-article group method t t)
953                   (gnus-message 1 "Couldn't store article in group %s: %s"
954                                 group (gnus-status-message method))
955                   (sit-for 2))
956                 (kill-buffer (current-buffer))))))))))
957
958 (defun gnus-inews-insert-gcc ()
959   "Insert Gcc headers based on `gnus-outgoing-message-group'."
960   (save-excursion
961     (save-restriction
962       (message-narrow-to-headers)
963       (let* ((group gnus-outgoing-message-group)
964              (gcc (cond
965                    ((gnus-functionp group)
966                     (funcall group))
967                    ((or (stringp group) (list group))
968                     group))))
969         (when gcc
970           (insert "Gcc: "
971                   (if (stringp gcc) gcc
972                     (mapconcat 'identity gcc " "))
973                   "\n"))))))
974
975 (defun gnus-inews-insert-archive-gcc (&optional group)
976   "Insert the Gcc to say where the article is to be archived."
977   (let* ((var gnus-message-archive-group)
978          (group (or group gnus-newsgroup-name ""))
979          (gcc-self-val
980           (and gnus-newsgroup-name
981                (gnus-group-find-parameter
982                 gnus-newsgroup-name 'gcc-self)))
983          result
984          (groups
985           (cond
986            ((null gnus-message-archive-method)
987             ;; Ignore.
988             nil)
989            ((stringp var)
990             ;; Just a single group.
991             (list var))
992            ((null var)
993             ;; We don't want this.
994             nil)
995            ((and (listp var) (stringp (car var)))
996             ;; A list of groups.
997             var)
998            ((gnus-functionp var)
999             ;; A function.
1000             (funcall var group))
1001            (t
1002             ;; An alist of regexps/functions/forms.
1003             (while (and var
1004                         (not
1005                          (setq result
1006                                (cond
1007                                 ((stringp (caar var))
1008                                  ;; Regexp.
1009                                  (when (string-match (caar var) group)
1010                                    (cdar var)))
1011                                 ((gnus-functionp (car var))
1012                                  ;; Function.
1013                                  (funcall (car var) group))
1014                                 (t
1015                                  (eval (car var)))))))
1016               (setq var (cdr var)))
1017             result)))
1018          name)
1019     (when (or groups gcc-self-val)
1020       (when (stringp groups)
1021         (setq groups (list groups)))
1022       (save-excursion
1023         (save-restriction
1024           (message-narrow-to-headers)
1025           (goto-char (point-max))
1026           (insert "Gcc: ")
1027           (if gcc-self-val
1028               ;; Use the `gcc-self' param value instead.
1029               (progn
1030                 (insert
1031                  (if (stringp gcc-self-val)
1032                      gcc-self-val
1033                    group))
1034                 (if (not (eq gcc-self-val 'none))
1035                     (insert "\n")
1036                   (progn
1037                     (beginning-of-line)
1038                     (kill-line))))
1039             ;; Use the list of groups.
1040             (while (setq name (pop groups))
1041               (insert (if (string-match ":" name)
1042                           name
1043                         (gnus-group-prefixed-name
1044                          name gnus-message-archive-method)))
1045               (when groups
1046                 (insert " ")))
1047             (insert "\n")))))))
1048
1049 ;;; Posting styles.
1050
1051 (defvar gnus-message-style-insertions nil)
1052
1053 (defun gnus-configure-posting-styles ()
1054   "Configure posting styles according to `gnus-posting-styles'."
1055   (unless gnus-inhibit-posting-styles
1056     (let ((styles gnus-posting-styles)
1057           (gnus-newsgroup-name (or gnus-newsgroup-name ""))
1058           style match variable attribute value value-value)
1059       (make-local-variable 'gnus-message-style-insertions)
1060       ;; Go through all styles and look for matches.
1061       (while styles
1062         (setq style (pop styles)
1063               match (pop style))
1064         (when (cond ((stringp match)
1065                      ;; Regexp string match on the group name.
1066                      (string-match match gnus-newsgroup-name))
1067                     ((or (symbolp match)
1068                          (gnus-functionp match))
1069                      (cond ((gnus-functionp match)
1070                             ;; Function to be called.
1071                             (funcall match))
1072                            ((boundp match)
1073                             ;; Variable to be checked.
1074                             (symbol-value match))))
1075                     ((listp match)
1076                      ;; This is a form to be evaled.
1077                      (eval match)))
1078           ;; We have a match, so we set the variables.
1079           (while style
1080             (setq attribute (pop style)
1081                   value (cadr attribute)
1082                   variable nil)
1083             ;; We find the variable that is to be modified.
1084             (if (and (not (stringp (car attribute)))
1085                      (not (eq 'body (car attribute)))
1086                      (not (setq variable
1087                                 (cdr (assq (car attribute)
1088                                            gnus-posting-style-alist)))))
1089                 (message "Couldn't find attribute %s" (car attribute))
1090               ;; We get the value.
1091               (setq value-value
1092                     (cond ((stringp value)
1093                            value)
1094                           ((or (symbolp value)
1095                                (gnus-functionp value))
1096                            (cond ((gnus-functionp value)
1097                                   (funcall value))
1098                                  ((boundp value)
1099                                   (symbol-value value))))
1100                           ((listp value)
1101                            (eval value))))
1102               (if variable
1103                   ;; This is an ordinary variable.
1104                   (set (make-local-variable variable) value-value)
1105                 ;; This is either a body or a header to be inserted in the
1106                 ;; message.
1107                 (when value-value
1108                   (let ((attr (car attribute)))
1109                     (make-local-variable 'message-setup-hook)
1110                     (if (eq 'body attr)
1111                         (add-hook 'message-setup-hook
1112                                   `(lambda ()
1113                                      (save-excursion
1114                                        (message-goto-body)
1115                                        (insert ,value-value))))
1116                       (add-hook 'message-setup-hook
1117                                 'gnus-message-insert-stylings)
1118                       (push (cons (if (stringp attr) attr
1119                                     (symbol-name attr))
1120                                   value-value)
1121                             gnus-message-style-insertions))))))))))))
1122
1123 (defun gnus-message-insert-stylings ()
1124   (let (val)
1125     (save-excursion
1126       (message-goto-eoh)
1127       (while (setq val (pop gnus-message-style-insertions))
1128         (when (cdr val)
1129           (insert (car val) ": " (cdr val) "\n"))
1130         (gnus-pull (car val) gnus-message-style-insertions)))))
1131
1132 ;;; Allow redefinition of functions.
1133
1134 (gnus-ems-redefine)
1135
1136 (provide 'gnus-msg)
1137
1138 ;;; gnus-msg.el ends here