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