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