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