fe632fc4276096bc6ec2a931bc8b8c0d6cf4b3db
[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       (goto-char (point-min))
656       (when (looking-at "From ")
657         (replace-match "X-From-Line: ") )
658       (run-hooks 'gnus-article-decode-hook)
659       (message-forward post))))
660
661 (defun gnus-summary-resend-message (address n)
662   "Resend the current article to ADDRESS."
663   (interactive "sResend message(s) to: \nP")
664   (let ((articles (gnus-summary-work-articles n))
665         article)
666     (while (setq article (pop articles))
667       (gnus-summary-select-article nil nil nil article)
668       (save-excursion
669         (set-buffer gnus-original-article-buffer)
670         (message-resend address)))))
671
672 (defun gnus-summary-post-forward (&optional full-headers)
673   "Forward the current article to a newsgroup.
674 If FULL-HEADERS (the prefix), include full headers when forwarding."
675   (interactive "P")
676   (gnus-summary-mail-forward full-headers t))
677
678 (defvar gnus-nastygram-message
679   "The following article was inappropriately posted to %s.\n\n"
680   "Format string to insert in nastygrams.
681 The current group name will be inserted at \"%s\".")
682
683 (defun gnus-summary-mail-nastygram (n)
684   "Send a nastygram to the author of the current article."
685   (interactive "P")
686   (when (or gnus-expert-user
687             (gnus-y-or-n-p
688              "Really send a nastygram to the author of the current article? "))
689     (let ((group gnus-newsgroup-name))
690       (gnus-summary-reply-with-original n)
691       (set-buffer gnus-message-buffer)
692       (message-goto-body)
693       (insert (format gnus-nastygram-message group))
694       (message-send-and-exit))))
695
696 (defun gnus-summary-mail-crosspost-complaint (n)
697   "Send a complaint about crossposting to the current article(s)."
698   (interactive "P")
699   (let ((articles (gnus-summary-work-articles n))
700         article)
701     (while (setq article (pop articles))
702       (set-buffer gnus-summary-buffer)
703       (gnus-summary-goto-subject article)
704       (let ((group (gnus-group-real-name gnus-newsgroup-name))
705             newsgroups followup-to)
706         (gnus-summary-select-article)
707         (set-buffer gnus-original-article-buffer)
708         (if (and (<= (length (message-tokenize-header
709                               (setq newsgroups
710                                     (mail-fetch-field "newsgroups"))
711                               ", "))
712                      1)
713                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
714                      (not (member group (message-tokenize-header
715                                          followup-to ", ")))))
716             (if followup-to
717                 (gnus-message 1 "Followup-to restricted")
718               (gnus-message 1 "Not a crossposted article"))
719           (set-buffer gnus-summary-buffer)
720           (gnus-summary-reply-with-original 1)
721           (set-buffer gnus-message-buffer)
722           (message-goto-body)
723           (insert (format gnus-crosspost-complaint newsgroups group))
724           (message-goto-subject)
725           (re-search-forward " *$")
726           (replace-match " (crosspost notification)" t t)
727           (gnus-deactivate-mark)
728           (when (gnus-y-or-n-p "Send this complaint? ")
729             (message-send-and-exit)))))))
730
731 (defun gnus-summary-mail-other-window ()
732   "Compose mail in other window."
733   (interactive)
734   (gnus-setup-message 'message
735     (message-mail)))
736
737 (defun gnus-mail-parse-comma-list ()
738   (let (accumulated
739         beg)
740     (skip-chars-forward " ")
741     (while (not (eobp))
742       (setq beg (point))
743       (skip-chars-forward "^,")
744       (while (zerop
745               (save-excursion
746                 (save-restriction
747                   (let ((i 0))
748                     (narrow-to-region beg (point))
749                     (goto-char beg)
750                     (logand (progn
751                               (while (search-forward "\"" nil t)
752                                 (incf i))
753                               (if (zerop i) 2 i))
754                             2)))))
755         (skip-chars-forward ",")
756         (skip-chars-forward "^,"))
757       (skip-chars-backward " ")
758       (push (buffer-substring beg (point))
759             accumulated)
760       (skip-chars-forward "^,")
761       (skip-chars-forward ", "))
762     accumulated))
763
764 (defun gnus-inews-add-to-address (group)
765   (let ((to-address (mail-fetch-field "to")))
766     (when (and to-address
767                (gnus-alive-p))
768       ;; This mail group doesn't have a `to-list', so we add one
769       ;; here.  Magic!
770       (when (gnus-y-or-n-p
771              (format "Do you want to add this as `to-list': %s " to-address))
772         (gnus-group-add-parameter group (cons 'to-list to-address))))))
773
774 (defun gnus-put-message ()
775   "Put the current message in some group and return to Gnus."
776   (interactive)
777   (let ((reply gnus-article-reply)
778         (winconf gnus-prev-winconf)
779         (group gnus-newsgroup-name))
780
781     (or (and group (not (gnus-group-read-only-p group)))
782         (setq group (read-string "Put in group: " nil
783                                  (gnus-writable-groups))))
784     (when (gnus-gethash group gnus-newsrc-hashtb)
785       (error "No such group: %s" group))
786
787     (save-excursion
788       (save-restriction
789         (widen)
790         (message-narrow-to-headers)
791         (let (gnus-deletable-headers)
792           (if (message-news-p)
793               (message-generate-headers message-required-news-headers)
794             (message-generate-headers message-required-mail-headers)))
795         (goto-char (point-max))
796         (insert "Gcc: " group "\n")
797         (widen)))
798
799     (gnus-inews-do-gcc)
800
801     (when (get-buffer gnus-group-buffer)
802       (when (gnus-buffer-exists-p (car-safe reply))
803         (set-buffer (car reply))
804         (and (cdr reply)
805              (gnus-summary-mark-article-as-replied
806               (cdr reply))))
807       (when winconf
808         (set-window-configuration winconf)))))
809
810 (defun gnus-article-mail (yank)
811   "Send a reply to the address near point.
812 If YANK is non-nil, include the original article."
813   (interactive "P")
814   (let ((address
815          (buffer-substring
816           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
817           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
818     (when address
819       (message-reply address)
820       (when yank
821         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
822
823 (defvar nntp-server-type)
824 (defun gnus-bug ()
825   "Send a bug report to the Gnus maintainers."
826   (interactive)
827   (unless (gnus-alive-p)
828     (error "Gnus has been shut down"))
829   (gnus-setup-message 'bug
830     (delete-other-windows)
831     (when gnus-bug-create-help-buffer
832       (switch-to-buffer "*Gnus Help Bug*")
833       (erase-buffer)
834       (insert gnus-bug-message)
835       (goto-char (point-min)))
836     (message-pop-to-buffer "*Gnus Bug*")
837     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
838     (when gnus-bug-create-help-buffer
839       (push `(gnus-bug-kill-buffer) message-send-actions))
840     (goto-char (point-min))
841     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
842     (forward-line 1)
843     (insert (gnus-version) "\n"
844             (emacs-version) "\n")
845     (when (and (boundp 'nntp-server-type)
846                (stringp nntp-server-type))
847       (insert nntp-server-type))
848     (insert "\n\n\n\n\n")
849     (save-excursion
850       (set-buffer (gnus-get-buffer-create " *gnus environment info*"))
851       (gnus-debug))
852     (insert "<#part type=application/x-emacs-lisp buffer=\" *gnus environment info*\" disposition=inline description=\"User settings\"><#/part>")
853     (goto-char (point-min))
854     (search-forward "Subject: " nil t)
855     (message "")))
856
857 (defun gnus-bug-kill-buffer ()
858   (when (get-buffer "*Gnus Help Bug*")
859     (kill-buffer "*Gnus Help Bug*")))
860
861 (defun gnus-debug ()
862   "Attempts to go through the Gnus source file and report what variables have been changed.
863 The source file has to be in the Emacs load path."
864   (interactive)
865   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
866                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
867                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
868                  "nnmail.el" "message.el"))
869         (point (point))
870         file expr olist sym)
871     (gnus-message 4 "Please wait while we snoop your variables...")
872     (sit-for 0)
873     ;; Go through all the files looking for non-default values for variables.
874     (save-excursion
875       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
876       (while files
877         (erase-buffer)
878         (when (and (setq file (locate-library (pop files)))
879                    (file-exists-p file))
880           (insert-file-contents file)
881           (goto-char (point-min))
882           (if (not (re-search-forward "^;;* *Internal variables" nil t))
883               (gnus-message 4 "Malformed sources in file %s" file)
884             (narrow-to-region (point-min) (point))
885             (goto-char (point-min))
886             (while (setq expr (ignore-errors (read (current-buffer))))
887               (ignore-errors
888                 (and (or (eq (car expr) 'defvar)
889                          (eq (car expr) 'defcustom))
890                      (stringp (nth 3 expr))
891                      (or (not (boundp (nth 1 expr)))
892                          (not (equal (eval (nth 2 expr))
893                                      (symbol-value (nth 1 expr)))))
894                      (push (nth 1 expr) olist)))))))
895       (kill-buffer (current-buffer)))
896     (when (setq olist (nreverse olist))
897       (insert "------------------ Environment follows ------------------\n\n"))
898     (while olist
899       (if (boundp (car olist))
900           (condition-case ()
901               (pp `(setq ,(car olist)
902                          ,(if (or (consp (setq sym (symbol-value (car olist))))
903                                   (and (symbolp sym)
904                                        (not (or (eq sym nil)
905                                                 (eq sym t)))))
906                               (list 'quote (symbol-value (car olist)))
907                             (symbol-value (car olist))))
908                   (current-buffer))
909             (error
910              (format "(setq %s 'whatever)\n" (car olist))))
911         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
912       (setq olist (cdr olist)))
913     (insert "\n\n")
914     ;; Remove any control chars - they seem to cause trouble for some
915     ;; mailers.  (Byte-compiled output from the stuff above.)
916     (goto-char point)
917     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
918       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
919                      t t))))
920
921 ;;; Treatment of rejected articles.
922 ;;; Bounced mail.
923
924 (defun gnus-summary-resend-bounced-mail (&optional fetch)
925   "Re-mail the current message.
926 This only makes sense if the current message is a bounce message than
927 contains some mail you have written which has been bounced back to
928 you.
929 If FETCH, try to fetch the article that this is a reply to, if indeed
930 this is a reply."
931   (interactive "P")
932   (gnus-summary-select-article t)
933   (set-buffer gnus-original-article-buffer)
934   (gnus-setup-message 'compose-bounce
935     (let* ((references (mail-fetch-field "references"))
936            (parent (and references (gnus-parent-id references))))
937       (message-bounce)
938       ;; If there are references, we fetch the article we answered to.
939       (and fetch parent
940            (gnus-summary-refer-article parent)
941            (gnus-summary-show-all-headers)))))
942
943 ;;; Gcc handling.
944
945 ;; Do Gcc handling, which copied the message over to some group.
946 (defun gnus-inews-do-gcc (&optional gcc)
947   (interactive)
948   (when (gnus-alive-p)
949     (save-excursion
950       (save-restriction
951         (message-narrow-to-headers)
952         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
953               (cur (current-buffer))
954               groups group method)
955           (when gcc
956             (message-remove-header "gcc")
957             (widen)
958             (setq groups (message-tokenize-header gcc " ,"))
959             ;; Copy the article over to some group(s).
960             (while (setq group (pop groups))
961               (gnus-check-server
962                (setq method
963                      (cond ((and (null (gnus-get-info group))
964                                  (eq (car gnus-message-archive-method)
965                                      (car
966                                       (gnus-server-to-method
967                                        (gnus-group-method group)))))
968                             ;; If the group doesn't exist, we assume
969                             ;; it's an archive group...
970                             gnus-message-archive-method)
971                            ;; Use the method.
972                            ((gnus-info-method (gnus-get-info group))
973                             (gnus-info-method (gnus-get-info group)))
974                            ;; Find the method.
975                            (t (gnus-group-method group)))))
976               (gnus-check-server method)
977               (unless (gnus-request-group group t method)
978                 (gnus-request-create-group group method))
979               (save-excursion
980                 (nnheader-set-temp-buffer " *acc*")
981                 (insert-buffer-substring cur)
982                 (message-encode-message-body)
983                 (save-restriction
984                   (message-narrow-to-headers)
985                   (mail-encode-encoded-word-buffer))
986                 (goto-char (point-min))
987                 (when (re-search-forward
988                        (concat "^" (regexp-quote mail-header-separator) "$")
989                        nil t)
990                   (replace-match "" t t ))
991                 (unless (gnus-request-accept-article group method t t)
992                   (gnus-message 1 "Couldn't store article in group %s: %s"
993                                 group (gnus-status-message method))
994                   (sit-for 2))
995                 (kill-buffer (current-buffer))))))))))
996
997 (defun gnus-inews-insert-gcc ()
998   "Insert Gcc headers based on `gnus-outgoing-message-group'."
999   (save-excursion
1000     (save-restriction
1001       (message-narrow-to-headers)
1002       (let* ((group gnus-outgoing-message-group)
1003              (gcc (cond
1004                    ((gnus-functionp group)
1005                     (funcall group))
1006                    ((or (stringp group) (list group))
1007                     group))))
1008         (when gcc
1009           (insert "Gcc: "
1010                   (if (stringp gcc) gcc
1011                     (mapconcat 'identity gcc " "))
1012                   "\n"))))))
1013
1014 (defun gnus-inews-insert-archive-gcc (&optional group)
1015   "Insert the Gcc to say where the article is to be archived."
1016   (let* ((var gnus-message-archive-group)
1017          (group (or group gnus-newsgroup-name ""))
1018          (gcc-self-val
1019           (and gnus-newsgroup-name
1020                (gnus-group-find-parameter
1021                 gnus-newsgroup-name 'gcc-self)))
1022          result
1023          (groups
1024           (cond
1025            ((null gnus-message-archive-method)
1026             ;; Ignore.
1027             nil)
1028            ((stringp var)
1029             ;; Just a single group.
1030             (list var))
1031            ((null var)
1032             ;; We don't want this.
1033             nil)
1034            ((and (listp var) (stringp (car var)))
1035             ;; A list of groups.
1036             var)
1037            ((gnus-functionp var)
1038             ;; A function.
1039             (funcall var group))
1040            (t
1041             ;; An alist of regexps/functions/forms.
1042             (while (and var
1043                         (not
1044                          (setq result
1045                                (cond
1046                                 ((stringp (caar var))
1047                                  ;; Regexp.
1048                                  (when (string-match (caar var) group)
1049                                    (cdar var)))
1050                                 ((gnus-functionp (car var))
1051                                  ;; Function.
1052                                  (funcall (car var) group))
1053                                 (t
1054                                  (eval (car var)))))))
1055               (setq var (cdr var)))
1056             result)))
1057          name)
1058     (when (or groups gcc-self-val)
1059       (when (stringp groups)
1060         (setq groups (list groups)))
1061       (save-excursion
1062         (save-restriction
1063           (message-narrow-to-headers)
1064           (goto-char (point-max))
1065           (insert "Gcc: ")
1066           (if gcc-self-val
1067               ;; Use the `gcc-self' param value instead.
1068               (progn
1069                 (insert
1070                  (if (stringp gcc-self-val)
1071                      gcc-self-val
1072                    group))
1073                 (if (not (eq gcc-self-val 'none))
1074                     (insert "\n")
1075                   (progn
1076                     (beginning-of-line)
1077                     (kill-line))))
1078             ;; Use the list of groups.
1079             (while (setq name (pop groups))
1080               (insert (if (string-match ":" name)
1081                           name
1082                         (gnus-group-prefixed-name
1083                          name gnus-message-archive-method)))
1084               (when groups
1085                 (insert " ")))
1086             (insert "\n")))))))
1087
1088 ;;; Posting styles.
1089
1090 (defun gnus-configure-posting-styles ()
1091   "Configure posting styles according to `gnus-posting-styles'."
1092   (unless gnus-inhibit-posting-styles
1093     (let ((group (or gnus-newsgroup-name ""))
1094           (styles gnus-posting-styles)
1095           style match variable attribute value v styles results
1096           filep name address element)
1097       ;; If the group has a posting-style parameter, add it at the end with a
1098       ;; regexp matching everything, to be sure it takes precedence over all
1099       ;; the others.
1100       (when gnus-newsgroup-name
1101         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1102           (when tmp-style
1103             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1104       ;; Go through all styles and look for matches.
1105       (dolist (style styles)
1106         (setq match (pop style))
1107         (when (cond
1108                ((stringp match)
1109                 ;; Regexp string match on the group name.
1110                 (string-match match group))
1111                ((or (symbolp match)
1112                     (gnus-functionp match))
1113                 (cond
1114                  ((gnus-functionp match)
1115                   ;; Function to be called.
1116                   (funcall match))
1117                  ((boundp match)
1118                   ;; Variable to be checked.
1119                   (symbol-value match))))
1120                ((listp match)
1121                 ;; This is a form to be evaled.
1122                 (eval match)))
1123           ;; We have a match, so we set the variables.
1124           (dolist (attribute style)
1125             (setq element (pop attribute)
1126                   variable nil
1127                   filep nil)
1128             (setq value
1129                   (cond
1130                    ((eq (car attribute) :file)
1131                     (setq filep t)
1132                     (cadr attribute))
1133                    ((eq (car attribute) :value)
1134                     (cadr attribute))
1135                    (t
1136                     (car attribute))))
1137             ;; We get the value.
1138             (setq v
1139                   (cond
1140                    ((stringp value)
1141                     value)
1142                    ((or (symbolp value)
1143                         (gnus-functionp value))
1144                     (cond ((gnus-functionp value)
1145                            (funcall value))
1146                           ((boundp value)
1147                            (symbol-value value))))
1148                    ((listp value)
1149                     (eval value))))
1150             ;; Translate obsolescent value.
1151             (when (eq element 'signature-file)
1152               (setq element 'signature
1153                     filep t))
1154             ;; Get the contents of file elems.
1155             (when filep
1156               (setq v (with-temp-buffer
1157                         (insert-file-contents v)
1158                         (buffer-string))))
1159             (setq results (delq (assoc element results) results))
1160             (push (cons element v) results))))
1161       ;; Now we have all the styles, so we insert them.
1162       (setq name (assq 'name results)
1163             address (assq 'address results))
1164       (setq results (delq name (delq address results)))
1165       (make-local-variable 'message-setup-hook)
1166       (dolist (result results)
1167         (when (cdr result)
1168           (add-hook 'message-setup-hook
1169                     (cond
1170                      ((eq 'body (car result))
1171                       `(lambda ()
1172                          (save-excursion
1173                            (message-goto-body)
1174                            (insert ,(cdr result)))))
1175                      ((eq 'signature (car result))
1176                       (set (make-local-variable 'message-signature) nil)
1177                       (set (make-local-variable 'message-signature-file) nil)
1178                       `(lambda ()
1179                          (save-excursion
1180                            (let ((message-signature ,(cdr result)))
1181                              (message-insert-signature)))))
1182                      (t
1183                       (let ((header
1184                              (if (symbolp (car result))
1185                                  (capitalize (symbol-name (car result)))
1186                                (car result))))
1187                         `(lambda ()
1188                            (save-excursion
1189                              (message-remove-header ,header)
1190                              (message-goto-eoh)
1191                              (insert ,header ": " ,(cdr result) "\n")))))))))
1192       (when (or name address)
1193         (add-hook 'message-setup-hook
1194                   `(lambda ()
1195                      (let ((user-full-name ,(or (cdr name) user-full-name))
1196                            (user-mail-address
1197                             ,(or (cdr address) user-mail-address)))
1198                        (save-excursion
1199                          (message-remove-header "From")
1200                          (message-goto-eoh)
1201                          (insert "From: " (message-make-from) "\n")))))))))
1202
1203 ;;; Allow redefinition of functions.
1204
1205 (gnus-ems-redefine)
1206
1207 (provide 'gnus-msg)
1208
1209 ;;; gnus-msg.el ends here