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