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