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