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