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