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