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