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