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