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