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