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