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