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