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