23f5baff8beffeb2ab3b975ae9650bbd9b8497e6
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;      Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;;      Katsumi Yamaoka  <yamaoka@jpl.org>
10 ;;      Kiyokazu SUTO    <suto@merry.xmath.ous.ac.jp>
11 ;; Keywords: mail, news, MIME
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'static))
36
37 (require 'gnus)
38 (require 'gnus-ems)
39 (require 'message)
40 (require 'gnus-art)
41
42 (defcustom gnus-post-method 'current
43   "*Preferred method for posting USENET news.
44
45 If this variable is `current' (which is the default), Gnus will use
46 the \"current\" select method when posting.  If it is nil, Gnus will
47 use the native select method when posting.
48
49 This method will not be used in mail groups and the like, only in
50 \"real\" newsgroups.
51
52 If not nil nor `native', the value must be a valid method as discussed
53 in the documentation of `gnus-select-method'.  It can also be a list of
54 methods.  If that is the case, the user will be queried for what select
55 method to use when posting."
56   :group 'gnus-group-foreign
57   :type `(choice (const nil)
58                  (const current)
59                  (const native)
60                  (sexp :tag "Methods" ,gnus-select-method)))
61
62 (defvar gnus-outgoing-message-group nil
63   "*All outgoing messages will be put in this group.
64 If you want to store all your outgoing mail and articles in the group
65 \"nnml:archive\", you set this variable to that value.  This variable
66 can also be a list of group names.
67
68 If you want to have greater control over what group to put each
69 message in, you can set this variable to a function that checks the
70 current newsgroup name and then returns a suitable group name (or list
71 of names).")
72
73 (defvar gnus-mailing-list-groups nil
74   "*Regexp matching groups that are really mailing lists.
75 This is useful when you're reading a mailing list that has been
76 gatewayed to a newsgroup, and you want to followup to an article in
77 the group.")
78
79 (defvar gnus-add-to-list nil
80   "*If non-nil, add a `to-list' parameter automatically.")
81
82 (defvar gnus-crosspost-complaint
83   "Hi,
84
85 You posted the article below with the following Newsgroups header:
86
87 Newsgroups: %s
88
89 The %s group, at least, was an inappropriate recipient
90 of this message.  Please trim your Newsgroups header to exclude this
91 group before posting in the future.
92
93 Thank you.
94
95 "
96   "Format string to be inserted when complaining about crossposts.
97 The first %s will be replaced by the Newsgroups header;
98 the second with the current group name.")
99
100 (defvar gnus-message-setup-hook '(gnus-maybe-setup-default-charset)
101   "Hook run after setting up a message buffer.")
102
103 (defvar gnus-bug-create-help-buffer t
104   "*Should we create the *Gnus Help Bug* buffer?")
105
106 (defvar gnus-posting-styles nil
107   "*Alist of styles to use when posting.")
108
109 (defvar gnus-inews-mark-gcc-as-read nil
110   "If non-nil, automatically mark Gcc articles as read.")
111
112 (defcustom gnus-group-posting-charset-alist
113   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
114     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
115     (message-this-is-mail nil nil)
116     (message-this-is-news nil t))
117   "Alist of regexps and permitted unencoded charsets for posting.
118 Each element of the alist has the form (TEST HEADER BODY-LIST), where
119 TEST is either a regular expression matching the newsgroup header or a
120 variable to query,
121 HEADER is the charset which may be left unencoded in the header (nil
122 means encode all charsets),
123 BODY-LIST is a list of charsets which may be encoded using 8bit
124 content-transfer encoding in the body, or one of the special values
125 nil (always encode using quoted-printable) or t (always use 8bit).
126
127 Note that any value other than nil for HEADER infringes some RFCs, so
128 use this option with care."
129   :type '(repeat (list :tag "Permitted unencoded charsets"
130                   (choice :tag "Where"
131                    (regexp :tag "Group")
132                    (const :tag "Mail message" :value message-this-is-mail)
133                    (const :tag "News article" :value message-this-is-news))
134                   (choice :tag "Header"
135                    (const :tag "None" nil)
136                    (symbol :tag "Charset"))
137                   (choice :tag "Body"
138                           (const :tag "Any" :value t)
139                           (const :tag "None" :value nil)
140                           (repeat :tag "Charsets"
141                                   (symbol :tag "Charset")))))
142   :group 'gnus-charset)
143
144 ;;; Internal variables.
145
146 (defvar gnus-inhibit-posting-styles nil
147   "Inhibit the use of posting styles.")
148
149 (defvar gnus-message-buffer "*Mail Gnus*")
150 (defvar gnus-article-copy nil)
151 (defvar gnus-check-before-posting nil)
152 (defvar gnus-last-posting-server nil)
153 (defvar gnus-message-group-art nil)
154
155 (defconst gnus-bug-message
156   (format "Sending a bug report to the Gnus Towers.
157 ========================================
158
159 This gnus is the %s%s.
160 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
161 Developers. (the addresses below are mailing list addresses)
162
163 ========================================
164
165 The buffer below is a mail buffer.  When you press `C-c C-c', it will
166 be sent to the Gnus Bug Exterminators.
167
168 The thing near the bottom of the buffer is how the environment
169 settings will be included in the mail.  Please do not delete that.
170 They will tell the Bug People what your environment is, so that it
171 will be easier to locate the bugs.
172
173 If you have found a bug that makes Emacs go \"beep\", set
174 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
175 and include the backtrace in your bug report.
176
177 Please describe the bug in annoying, painstaking detail.
178
179 Thank you for your help in stamping out bugs.
180 "
181           gnus-product-name
182           (if (string= gnus-product-name "Semi-gnus")
183               ""
184             ", a modified version of Semi-gnus")))
185
186 (eval-and-compile
187   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
188   (autoload 'news-setup "rnewspost")
189   (autoload 'news-reply-mode "rnewspost")
190   (autoload 'rmail-dont-reply-to "mail-utils")
191   (autoload 'rmail-output "rmailout"))
192
193 \f
194 ;;;
195 ;;; Gnus Posting Functions
196 ;;;
197
198 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
199   "p" gnus-summary-post-news
200   "f" gnus-summary-followup
201   "F" gnus-summary-followup-with-original
202   "c" gnus-summary-cancel-article
203   "s" gnus-summary-supersede-article
204   "r" gnus-summary-reply
205   "y" gnus-summary-yank-message
206   "R" gnus-summary-reply-with-original
207   "w" gnus-summary-wide-reply
208   "W" gnus-summary-wide-reply-with-original
209   "v" gnus-summary-very-wide-reply
210   "V" gnus-summary-very-wide-reply-with-original
211   "n" gnus-summary-followup-to-mail
212   "N" gnus-summary-followup-to-mail-with-original
213   "m" gnus-summary-mail-other-window
214   "u" gnus-uu-post-news
215   "\M-c" gnus-summary-mail-crosspost-complaint
216   "om" gnus-summary-mail-forward
217   "op" gnus-summary-post-forward
218   "Om" gnus-summary-digest-mail-forward
219   "Op" gnus-summary-digest-post-forward)
220
221 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
222   "b" gnus-summary-resend-bounced-mail
223   ;; "c" gnus-summary-send-draft
224   "r" gnus-summary-resend-message)
225
226 ;;; Internal functions.
227
228 (defvar gnus-article-reply nil)
229 (defmacro gnus-setup-message (config &rest forms)
230   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
231         (buffer (make-symbol "gnus-setup-message-buffer"))
232         (article (make-symbol "gnus-setup-message-article"))
233         (group (make-symbol "gnus-setup-message-group")))
234     `(let ((,winconf (current-window-configuration))
235            (,buffer (buffer-name (current-buffer)))
236            (,article gnus-article-reply)
237            (,group gnus-newsgroup-name)
238            (message-header-setup-hook
239             (copy-sequence message-header-setup-hook))
240            (message-mode-hook (copy-sequence message-mode-hook))
241            (message-startup-parameter-alist
242             '((reply-buffer . gnus-copy-article-buffer)
243               (original-buffer . gnus-original-article-buffer)
244               (user-agent . Gnus))))
245        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
246        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
247        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
248        (unwind-protect
249            (progn
250              ,@forms)
251          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config)
252          (gnus-inews-insert-draft-meta-information ,group ,article)
253          (setq gnus-message-buffer (current-buffer))
254          (set (make-local-variable 'gnus-message-group-art)
255               (cons ,group ,article))
256          (set (make-local-variable 'gnus-newsgroup-name) ,group)
257          (gnus-run-hooks 'gnus-message-setup-hook))
258        (gnus-add-buffer)
259        (gnus-configure-windows ,config t)
260        (set-buffer-modified-p nil))))
261
262 (defun gnus-inews-insert-draft-meta-information (group article)
263   (save-excursion
264     (when (and group
265                (not (string= group ""))
266                (not (message-fetch-field gnus-draft-meta-information-header)))
267       (goto-char (point-min))
268       (insert gnus-draft-meta-information-header ": (\"" group "\" "
269               (if article (number-to-string
270                            (if (listp article)
271                                (car article)
272                              article)) "\"\"")
273               ")\n"))))
274
275 ;;;###autoload
276 (defun gnus-msg-mail (&optional to subject other-headers continue
277                                 switch-action yank-action send-actions)
278   "Start editing a mail message to be sent.
279 Like `message-mail', but with Gnus paraphernalia, particularly the
280 Gcc: header for archiving purposes."
281   (interactive)
282   (let ((buf (current-buffer))
283         mail-buf)
284     (gnus-setup-message 'message
285       (message-mail to subject other-headers continue
286                     nil yank-action send-actions))
287     (when switch-action
288       (setq mail-buf (current-buffer))
289       (switch-to-buffer buf)
290       (apply switch-action mail-buf nil)))
291   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
292   t)
293
294 ;;;###autoload
295 (defun gnus-button-mailto (address)
296   "Mail to ADDRESS."
297   (set-buffer (gnus-copy-article-buffer))
298   (gnus-setup-message 'message
299     (message-reply address)))
300
301 ;;;###autoload
302 (defun gnus-button-reply (&optional to-address wide)
303   "Like `message-reply'."
304   (interactive)
305   (gnus-setup-message 'message
306     (message-reply to-address wide)))
307
308 ;;;###autoload
309 (define-mail-user-agent 'gnus-user-agent
310   'gnus-msg-mail 'message-send-and-exit
311   'message-kill-buffer 'message-send-hook)
312
313 (defun gnus-setup-posting-charset (group)
314   (let ((alist gnus-group-posting-charset-alist)
315         (group (or group ""))
316         elem)
317     (when group
318       (catch 'found
319         (while (setq elem (pop alist))
320           (when (or (and (stringp (car elem))
321                          (string-match (car elem) group))
322                     (and (gnus-functionp (car elem))
323                          (funcall (car elem) group))
324                     (and (symbolp (car elem))
325                          (symbol-value (car elem))))
326             (throw 'found (cons (cadr elem) (caddr elem)))))))))
327
328 (defun gnus-inews-add-send-actions (winconf buffer article &optional config)
329   (make-local-hook 'message-sent-hook)
330   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
331                                  'gnus-inews-do-gcc) nil t)
332   (when gnus-agent
333     (make-local-hook 'message-header-hook)
334     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
335   (setq message-post-method
336         `(lambda (arg)
337            (gnus-post-method arg ,gnus-newsgroup-name)))
338   (setq message-user-agent (gnus-extended-version))
339   (when (not message-use-multi-frames)
340     (message-add-action
341      `(set-window-configuration ,winconf) 'exit 'postpone 'kill))
342   (message-add-action
343    `(when (gnus-buffer-exists-p ,buffer)
344       (save-excursion
345         (set-buffer ,buffer)
346         ,(when article
347            (if (eq config 'forward)
348                `(gnus-summary-mark-article-as-forwarded ',article)
349              `(gnus-summary-mark-article-as-replied ',article)))))
350    'send))
351
352 (put 'gnus-setup-message 'lisp-indent-function 1)
353 (put 'gnus-setup-message 'edebug-form-spec '(form body))
354
355 ;;; Post news commands of Gnus group mode and summary mode
356
357 (defun gnus-group-mail (&optional arg)
358   "Start composing a mail.
359 If ARG, use the group under the point to find a posting style.
360 If ARG is 1, prompt for a group name to find the posting style."
361   (interactive "P")
362   ;; We can't `let' gnus-newsgroup-name here, since that leads
363   ;; to local variables leaking.
364   (let ((group gnus-newsgroup-name)
365         (buffer (current-buffer)))
366     (unwind-protect
367         (progn
368           (setq gnus-newsgroup-name
369                 (if arg
370                     (if (= 1 (prefix-numeric-value arg))
371                         (completing-read "Use posting style of group: "
372                                          gnus-active-hashtb nil
373                                          (gnus-read-active-file-p))
374                       (gnus-group-group-name))
375                   ""))
376           (gnus-setup-message 'message (message-mail)))
377       (save-excursion
378         (set-buffer buffer)
379         (setq gnus-newsgroup-name group)))))
380
381 (defun gnus-group-post-news (&optional arg)
382   "Start composing a news message.
383 If ARG, post to the group under point.
384 If ARG is 1, prompt for a group name."
385   (interactive "P")
386   ;; Bind this variable here to make message mode hooks work ok.
387   (let ((gnus-newsgroup-name
388          (if arg
389              (if (= 1 (prefix-numeric-value arg))
390                  (completing-read "Newsgroup: " gnus-active-hashtb nil
391                                   (gnus-read-active-file-p))
392                (gnus-group-group-name))
393            "")))
394     (gnus-post-news 'post gnus-newsgroup-name)))
395
396 (defun gnus-summary-post-news ()
397   "Start composing a news message."
398   (interactive)
399   (gnus-post-news 'post gnus-newsgroup-name))
400
401 (defun gnus-summary-followup (yank &optional force-news)
402   "Compose a followup to an article.
403 If prefix argument YANK is non-nil, original article is yanked automatically."
404   (interactive
405    (list (and current-prefix-arg
406               (gnus-summary-work-articles 1))))
407   (when yank
408     (gnus-summary-goto-subject (car yank)))
409   (save-window-excursion
410     (gnus-summary-select-article))
411   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
412         (gnus-newsgroup-name gnus-newsgroup-name))
413     ;; Send a followup.
414     (gnus-post-news nil gnus-newsgroup-name
415                     headers gnus-article-buffer
416                     yank nil force-news)))
417
418 (defun gnus-summary-followup-with-original (n &optional force-news)
419   "Compose a followup to an article and include the original article."
420   (interactive "P")
421   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
422
423 (defun gnus-summary-followup-to-mail (&optional arg)
424   "Followup to the current mail message via news."
425   (interactive
426    (list (and current-prefix-arg
427               (gnus-summary-work-articles 1))))
428   (gnus-summary-followup arg t))
429
430 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
431   "Followup to the current mail message via news."
432   (interactive "P")
433   (gnus-summary-followup (gnus-summary-work-articles arg) t))
434
435 (defun gnus-inews-yank-articles (articles)
436   (let ((more-than-one (cdr articles))
437         (cur (current-buffer))
438         refs beg article window)
439     (message-goto-body)
440     (while (setq article (pop articles))
441       (save-window-excursion
442         (set-buffer gnus-summary-buffer)
443         (gnus-summary-select-article nil nil nil article)
444         (gnus-summary-remove-process-mark article))
445
446       ;; Gathering references.
447       (when more-than-one
448         (setq refs (message-list-references
449                     refs
450                     (mail-header-references gnus-current-headers)
451                     (mail-header-message-id gnus-current-headers)))
452         (when message-use-multi-frames
453           (when (setq window (get-buffer-window cur t))
454             (select-frame (window-frame window)))))
455
456       (gnus-copy-article-buffer)
457       (let ((message-reply-buffer gnus-article-copy)
458             (message-reply-headers
459              (with-current-buffer gnus-article-copy
460                ;; The headers are decoded.
461                (nnheader-parse-head t))))
462         (message-yank-original)
463         (setq beg (or beg (mark t))))
464       (when articles
465         (insert "\n")))
466     (push-mark)
467
468     ;; Replace with the gathered references.
469     (when refs
470       (push-mark beg)
471       (save-restriction
472         (message-narrow-to-headers)
473         (let ((case-fold-search t))
474           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
475               (replace-match "")
476             (goto-char (point-max))))
477         (mail-header-format
478          (list (or (assq 'References message-header-format-alist)
479                    '(References . message-shorten-references)))
480          (list (cons 'References
481                      (mapconcat 'identity (nreverse refs) " "))))
482         (backward-delete-char 1))
483       (setq beg (mark t))
484       (pop-mark))
485
486     (goto-char beg)))
487
488 (defun gnus-summary-cancel-article (&optional n symp)
489   "Cancel an article you posted.
490 Uses the process-prefix convention.  If given the symbolic
491 prefix `a', cancel using the standard posting method; if not
492 post using the current select method."
493   (interactive (gnus-interactive "P\ny"))
494   (let ((articles (gnus-summary-work-articles n))
495         (message-post-method
496          `(lambda (arg)
497             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
498         article)
499     (while (setq article (pop articles))
500       (when (gnus-summary-select-article t nil nil article)
501         (when (gnus-eval-in-buffer-window gnus-article-buffer
502                 (save-excursion
503                   (set-buffer gnus-original-article-buffer)
504                   (message-cancel-news)))
505           (gnus-summary-mark-as-read article gnus-canceled-mark)
506           (gnus-cache-remove-article 1))
507         (gnus-article-hide-headers-if-wanted))
508       (gnus-summary-remove-process-mark article))))
509
510 (defun gnus-summary-supersede-article ()
511   "Compose an article that will supersede a previous article.
512 This is done simply by taking the old article and adding a Supersedes
513 header line with the old Message-ID."
514   (interactive)
515   (let ((article (gnus-summary-article-number))
516         (gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
517     (gnus-setup-message 'reply-yank
518       (gnus-summary-select-article t)
519       (set-buffer gnus-original-article-buffer)
520       (message-supersede)
521       (push
522        `((lambda ()
523            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
524              (save-excursion
525                (set-buffer ,gnus-summary-buffer)
526                (gnus-cache-possibly-remove-article ,article nil nil nil t)
527                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
528        message-send-actions))))
529
530 \f
531
532 (defun gnus-copy-article-buffer (&optional article-buffer)
533   ;; make a copy of the article buffer with all text properties removed
534   ;; this copy is in the buffer gnus-article-copy.
535   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
536   ;; this buffer should be passed to all mail/news reply/post routines.
537   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
538   (let ((article-buffer (or article-buffer gnus-article-buffer))
539         end beg)
540     (if (not (and (get-buffer article-buffer)
541                   (gnus-buffer-exists-p article-buffer)))
542         (error "Can't find any article buffer")
543       (save-excursion
544         (set-buffer article-buffer)
545         (let ((gnus-newsgroup-charset (or gnus-article-charset
546                                           gnus-newsgroup-charset))
547               (gnus-newsgroup-ignored-charsets
548                (or gnus-article-ignored-charsets
549                    gnus-newsgroup-ignored-charsets)))
550           (save-restriction
551             ;; Copy over the (displayed) article buffer, delete
552             ;; hidden text and remove text properties.
553             (widen)
554             (let ((inhibit-read-only t))
555               (copy-to-buffer gnus-article-copy (point-min) (point-max))
556               (set-buffer gnus-article-copy)
557               ;; Encode bitmap smileys to ordinary text.
558               ;; Possibly, the original text might be restored.
559               (static-unless (featurep 'xemacs)
560                 (when (featurep 'smiley-mule)
561                   (smiley-encode-buffer)))
562               (gnus-article-delete-text-of-type 'annotation)
563               (gnus-remove-text-with-property 'gnus-prev)
564               (gnus-remove-text-with-property 'gnus-next)
565               (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
566               (insert
567                (prog1
568                    (static-if (featurep 'xemacs)
569                        ;; Revome smiley extents for (possibly) XEmacs 21.1.
570                        (format "%s"
571                                (buffer-substring-no-properties (point-min)
572                                                                (point-max)))
573                      (buffer-substring-no-properties (point-min) (point-max)))
574                  (erase-buffer))))
575             ;; Find the original headers.
576             (set-buffer gnus-original-article-buffer)
577             (goto-char (point-min))
578             (while (looking-at message-unix-mail-delimiter)
579               (forward-line 1))
580             (setq beg (point))
581             (setq end (or (message-goto-body) beg))
582             ;; Delete the headers from the displayed articles.
583             (set-buffer gnus-article-copy)
584             (delete-region (goto-char (point-min))
585                            (or (message-goto-body) (point-max)))
586             ;; Insert the original article headers.
587             (insert-buffer-substring gnus-original-article-buffer beg end)
588             (article-decode-encoded-words))))
589       gnus-article-copy)))
590
591 (defun gnus-post-news (post &optional group header article-buffer yank subject
592                             force-news)
593   (when article-buffer
594     (gnus-copy-article-buffer))
595   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
596         (add-to-list gnus-add-to-list))
597     (gnus-setup-message (cond (yank 'reply-yank)
598                               (article-buffer 'reply)
599                               (t 'message))
600       (let* ((group (or group gnus-newsgroup-name))
601              (charset (gnus-group-name-charset nil group))
602              (pgroup group)
603              to-address to-group mailing-list to-list
604              newsgroup-p)
605         (when group
606           (setq to-address (gnus-parameter-to-address group)
607                 to-group (gnus-group-find-parameter group 'to-group)
608                 to-list (gnus-parameter-to-list group)
609                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
610                 mailing-list (when gnus-mailing-list-groups
611                                (string-match gnus-mailing-list-groups group))
612                 group (gnus-group-name-decode (gnus-group-real-name group)
613                                               charset)))
614         (if (or (and to-group
615                      (gnus-news-group-p to-group))
616                 newsgroup-p
617                 force-news
618                 (and (gnus-news-group-p
619                       (or pgroup gnus-newsgroup-name)
620                       (if header (mail-header-number header)
621                         gnus-current-article))
622                      (not mailing-list)
623                      (not to-list)
624                      (not to-address)))
625             ;; This is news.
626             (if post
627                 (message-news (or to-group group))
628               (set-buffer gnus-article-copy)
629               (gnus-msg-treat-broken-reply-to)
630               (message-followup (if (or newsgroup-p force-news)
631                                     (if (save-restriction
632                                           (article-narrow-to-head)
633                                           (message-fetch-field "newsgroups"))
634                                         nil
635                                       "")
636                                   to-group)))
637           ;; The is mail.
638           (if post
639               (progn
640                 (message-mail (or to-address to-list))
641                 ;; Arrange for mail groups that have no `to-address' to
642                 ;; get that when the user sends off the mail.
643                 (when (and (not to-list)
644                            (not to-address)
645                            add-to-list)
646                   (push (list 'gnus-inews-add-to-address pgroup)
647                         message-send-actions)))
648             (set-buffer gnus-article-copy)
649             (gnus-msg-treat-broken-reply-to)
650             (message-wide-reply to-address)))
651         (when yank
652           (gnus-inews-yank-articles yank))))))
653
654 (defun gnus-msg-treat-broken-reply-to ()
655   "Remove the Reply-to header iff broken-reply-to."
656   (when (gnus-group-find-parameter
657          gnus-newsgroup-name 'broken-reply-to)
658     (save-restriction
659       (message-narrow-to-head)
660       (message-remove-header "reply-to"))))
661
662 (defun gnus-post-method (arg group &optional silent)
663   "Return the posting method based on GROUP and ARG.
664 If SILENT, don't prompt the user."
665   (let ((group-method (gnus-find-method-for-group group)))
666     (cond
667      ;; If the group-method is nil (which shouldn't happen) we use
668      ;; the default method.
669      ((null group-method)
670       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
671           gnus-select-method message-post-method))
672      ;; We want the inverse of the default
673      ((and arg (not (eq arg 0)))
674       (if (eq gnus-post-method 'active)
675           gnus-select-method
676         group-method))
677      ;; We query the user for a post method.
678      ((or arg
679           (and gnus-post-method
680                (not (eq gnus-post-method 'current))
681                (listp (car gnus-post-method))))
682       (let* ((methods
683               ;; Collect all methods we know about.
684               (append
685                (when (and gnus-post-method
686                           (not (eq gnus-post-method 'current)))
687                  (if (listp (car gnus-post-method))
688                      gnus-post-method
689                    (list gnus-post-method)))
690                gnus-secondary-select-methods
691                (mapcar 'cdr gnus-server-alist)
692                (mapcar 'car gnus-opened-servers)
693                (list gnus-select-method)
694                (list group-method)))
695              method-alist post-methods method)
696         ;; Weed out all mail methods.
697         (while methods
698           (setq method (gnus-server-get-method "" (pop methods)))
699           (when (and (or (gnus-method-option-p method 'post)
700                          (gnus-method-option-p method 'post-mail))
701                      (not (member method post-methods)))
702             (push method post-methods)))
703         ;; Create a name-method alist.
704         (setq method-alist
705               (mapcar
706                (lambda (m)
707                  (if (equal (cadr m) "")
708                      (list (symbol-name (car m)) m)
709                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
710                post-methods))
711         ;; Query the user.
712         (cadr
713          (assoc
714           (setq gnus-last-posting-server
715                 (if (and silent
716                          gnus-last-posting-server)
717                     ;; Just use the last value.
718                     gnus-last-posting-server
719                   (completing-read
720                    "Posting method: " method-alist nil t
721                    (cons (or gnus-last-posting-server "") 0))))
722           method-alist))))
723      ;; Override normal method.
724      ((and (eq gnus-post-method 'current)
725            (not (eq (car group-method) 'nndraft))
726            (gnus-get-function group-method 'request-post t)
727            (not arg))
728       group-method)
729      ((and gnus-post-method
730            (not (eq gnus-post-method 'current)))
731       gnus-post-method)
732      ;; Use the normal select method.
733      (t gnus-select-method))))
734
735 \f
736
737 (defun gnus-message-make-user-agent (&optional include-mime-info max-column)
738   "Return user-agent info.
739 INCLUDE-MIME-INFO the optional first argument if it is non-nil and the variable
740   `mime-edit-user-agent-value' exists, the return value will include it.
741 MAX-COLUMN the optional second argument if it is specified, the return value
742   will be folded up in the proper way."
743   (let ((user-agent (if (and include-mime-info
744                              (boundp 'mime-edit-user-agent-value))
745                         (concat (gnus-extended-version)
746                                 " "
747                                 mime-edit-user-agent-value)
748                       (gnus-extended-version))))
749     (if max-column
750         (let (boundary)
751           (unless (natnump max-column) (setq max-column 76))
752           (with-temp-buffer
753             (insert "            " user-agent)
754             (goto-char 13)
755             (while (re-search-forward "[\n\t ]+" nil t)
756               (replace-match " "))
757             (goto-char 13)
758             (while (re-search-forward "[^ ()/]+\\(/[^ ()/]+\\)? ?" nil t)
759               (while (eq ?\( (char-after (point)))
760                 (forward-list)
761                 (skip-chars-forward " "))
762               (skip-chars-backward " ")
763               (if (> (current-column) max-column)
764                   (progn
765                     (if (or (not boundary) (eq ?\n (char-after boundary)))
766                         (progn
767                           (setq boundary (point))
768                           (unless (eobp)
769                             (delete-char 1)
770                             (insert "\n ")))
771                       (goto-char boundary)
772                       (delete-char 1)
773                       (insert "\n ")))
774                 (setq boundary (point))))
775             (buffer-substring 13 (point-max))))
776       user-agent)))
777
778 \f
779 ;;;
780 ;;; Gnus Mail Functions
781 ;;;
782
783 ;;; Mail reply commands of Gnus summary mode
784
785 (defun gnus-summary-reply (&optional yank wide very-wide)
786   "Start composing a mail reply to the current message.
787 If prefix argument YANK is non-nil, the original article is yanked
788 automatically.
789 If WIDE, make a wide reply.
790 If VERY-WIDE, make a very wide reply."
791   (interactive
792    (list (and current-prefix-arg
793               (gnus-summary-work-articles 1))))
794   ;; Stripping headers should be specified with mail-yank-ignored-headers.
795   (when yank
796     (gnus-summary-goto-subject (car yank)))
797   (let ((gnus-article-reply (or yank (gnus-summary-article-number)))
798         (headers ""))
799     (gnus-setup-message (if yank 'reply-yank 'reply)
800       (if (not very-wide)
801           (gnus-summary-select-article)
802         (dolist (article very-wide)
803           (gnus-summary-select-article nil nil nil article)
804           (save-excursion
805             (set-buffer (gnus-copy-article-buffer))
806             (gnus-msg-treat-broken-reply-to)
807             (save-restriction
808               (message-narrow-to-head)
809               (setq headers (concat headers (buffer-string)))))))
810       (set-buffer (gnus-copy-article-buffer))
811       (gnus-msg-treat-broken-reply-to)
812       (save-restriction
813         (message-narrow-to-head)
814         (when very-wide
815           (erase-buffer)
816           (insert headers))
817         (goto-char (point-max)))
818       (message-reply nil wide)
819       (when yank
820         (gnus-inews-yank-articles yank)))))
821
822 (defun gnus-summary-reply-with-original (n &optional wide)
823   "Start composing a reply mail to the current message.
824 The original article will be yanked."
825   (interactive "P")
826   (gnus-summary-reply (gnus-summary-work-articles n) wide))
827
828 (defun gnus-summary-wide-reply (&optional yank)
829   "Start composing a wide reply mail to the current message.
830 If prefix argument YANK is non-nil, the original article is yanked
831 automatically."
832   (interactive
833    (list (and current-prefix-arg
834               (gnus-summary-work-articles 1))))
835   (gnus-summary-reply yank t))
836
837 (defun gnus-summary-wide-reply-with-original (n)
838   "Start composing a wide reply mail to the current message.
839 The original article will be yanked."
840   (interactive "P")
841   (gnus-summary-reply-with-original n t))
842
843 (defun gnus-summary-very-wide-reply (&optional yank)
844   "Start composing a very wide reply mail to the current message.
845 If prefix argument YANK is non-nil, the original article is yanked
846 automatically."
847   (interactive
848    (list (and current-prefix-arg
849               (gnus-summary-work-articles 1))))
850   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
851
852 (defun gnus-summary-very-wide-reply-with-original (n)
853   "Start composing a very wide reply mail to the current message.
854 The original article will be yanked."
855   (interactive "P")
856   (gnus-summary-reply
857    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
858
859 (defun gnus-summary-mail-forward (&optional full-headers post)
860   "Forward the current message to another user.
861 If FULL-HEADERS (the prefix), include full headers when forwarding."
862   (interactive "P")
863   (gnus-setup-message 'forward
864     (gnus-summary-select-article)
865     (let ((charset default-mime-charset))
866       (set-buffer gnus-original-article-buffer)
867       (make-local-variable 'default-mime-charset)
868       (setq default-mime-charset charset))
869     (let ((message-included-forward-headers
870            (if full-headers "" message-included-forward-headers)))
871       (message-forward post))))
872
873 (defun gnus-summary-digest-mail-forward (&optional n post)
874   "Digests and forwards all articles in this series.
875 If N is a positive number, forward the N next articles.
876 If N is a negative number, forward the N previous articles.
877 If N is nil and any articles have been marked with the process mark,
878 forward those articles instead.
879 Optional POST will use news to forward instead of mail."
880   (interactive "P")
881   (let ((articles (gnus-summary-work-articles n))
882         (topics "Topics:\n")
883         subject article frame)
884     (when (car articles)
885       (gnus-setup-message 'forward
886         (gnus-summary-select-article)
887         (if (cdr articles)
888             (setq articles (sort articles '<)
889                   subject "Digested Articles")
890           (with-current-buffer gnus-original-article-buffer
891             (setq subject (message-make-forward-subject))))
892         (if post
893             (message-news nil subject)
894           (message-mail nil subject))
895         (when (and message-use-multi-frames (cdr articles))
896           (setq frame (window-frame (get-buffer-window (current-buffer)))))
897         (message-goto-body)
898         (while (setq article (pop articles))
899           (save-window-excursion
900             (set-buffer gnus-summary-buffer)
901             (gnus-summary-select-article nil nil nil article)
902             (setq topics (concat topics "    "
903                                  (mail-header-subject gnus-current-headers)
904                                  "\n"))
905             (gnus-summary-remove-process-mark article))
906           (when frame
907             (select-frame frame))
908           (insert (mime-make-tag "message" "rfc822") "\n")
909           (narrow-to-region (point) (point))
910           (insert-buffer-substring gnus-original-article-buffer)
911           (save-restriction
912             (article-narrow-to-head)
913             (message-remove-header message-included-forward-headers t nil t))
914           (goto-char (point-max))
915           (widen))
916         (push-mark)
917         (message-goto-body)
918         (insert topics)
919         (message-goto-body)
920         (mime-edit-enclose-digest-region (point)(mark t))))))
921
922 (defun gnus-summary-digest-post-forward (&optional n)
923   "Digest and forwards all articles in this series to a newsgroup.
924 If N is a positive number, forward the N next articles.
925 If N is a negative number, forward the N previous articles.
926 If N is nil and any articles have been marked with the process mark,
927 forward those articles instead."
928   (interactive "P")
929   (gnus-summary-digest-mail-forward n t))
930
931 (defun gnus-summary-resend-message (address n)
932   "Resend the current article to ADDRESS."
933   (interactive
934    (list (message-read-from-minibuffer "Resend message(s) to: ")
935          current-prefix-arg))
936   (let ((articles (gnus-summary-work-articles n))
937         article)
938     (while (setq article (pop articles))
939       (gnus-summary-select-article nil nil nil article)
940       (save-excursion
941         (set-buffer gnus-original-article-buffer)
942         (message-resend address))
943       (gnus-summary-mark-article-as-forwarded article))))
944
945 (defun gnus-summary-post-forward (&optional full-headers)
946   "Forward the current article to a newsgroup.
947 If FULL-HEADERS (the prefix), include full headers when forwarding."
948   (interactive "P")
949   (gnus-summary-mail-forward full-headers t))
950
951 (defvar gnus-nastygram-message
952   "The following article was inappropriately posted to %s.\n\n"
953   "Format string to insert in nastygrams.
954 The current group name will be inserted at \"%s\".")
955
956 (defun gnus-summary-mail-nastygram (n)
957   "Send a nastygram to the author of the current article."
958   (interactive "P")
959   (when (or gnus-expert-user
960             (gnus-y-or-n-p
961              "Really send a nastygram to the author of the current article? "))
962     (let ((group gnus-newsgroup-name))
963       (gnus-summary-reply-with-original n)
964       (set-buffer gnus-message-buffer)
965       (message-goto-body)
966       (insert (format gnus-nastygram-message group))
967       (message-send-and-exit))))
968
969 (defun gnus-summary-mail-crosspost-complaint (n)
970   "Send a complaint about crossposting to the current article(s)."
971   (interactive "P")
972   (let ((articles (gnus-summary-work-articles n))
973         article)
974     (while (setq article (pop articles))
975       (set-buffer gnus-summary-buffer)
976       (gnus-summary-goto-subject article)
977       (let ((group (gnus-group-real-name gnus-newsgroup-name))
978             newsgroups followup-to)
979         (gnus-summary-select-article)
980         (set-buffer gnus-original-article-buffer)
981         (if (and (<= (length (message-tokenize-header
982                               (setq newsgroups
983                                     (mail-fetch-field "newsgroups"))
984                               ", "))
985                      1)
986                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
987                      (not (member group (message-tokenize-header
988                                          followup-to ", ")))))
989             (if followup-to
990                 (gnus-message 1 "Followup-to restricted")
991               (gnus-message 1 "Not a crossposted article"))
992           (set-buffer gnus-summary-buffer)
993           (gnus-summary-reply-with-original 1)
994           (set-buffer gnus-message-buffer)
995           (message-goto-body)
996           (insert (format gnus-crosspost-complaint newsgroups group))
997           (message-goto-subject)
998           (re-search-forward " *$")
999           (replace-match " (crosspost notification)" t t)
1000           (gnus-deactivate-mark)
1001           (when (gnus-y-or-n-p "Send this complaint? ")
1002             (message-send-and-exit)))))))
1003
1004 (defun gnus-summary-mail-other-window ()
1005   "Compose mail in other window."
1006   (interactive)
1007   (gnus-setup-message 'message
1008     (message-mail)))
1009
1010 (defun gnus-mail-parse-comma-list ()
1011   (let (accumulated
1012         beg)
1013     (skip-chars-forward " ")
1014     (while (not (eobp))
1015       (setq beg (point))
1016       (skip-chars-forward "^,")
1017       (while (zerop
1018               (save-excursion
1019                 (save-restriction
1020                   (let ((i 0))
1021                     (narrow-to-region beg (point))
1022                     (goto-char beg)
1023                     (logand (progn
1024                               (while (search-forward "\"" nil t)
1025                                 (incf i))
1026                               (if (zerop i) 2 i))
1027                             2)))))
1028         (skip-chars-forward ",")
1029         (skip-chars-forward "^,"))
1030       (skip-chars-backward " ")
1031       (push (buffer-substring beg (point))
1032             accumulated)
1033       (skip-chars-forward "^,")
1034       (skip-chars-forward ", "))
1035     accumulated))
1036
1037 (defun gnus-inews-add-to-address (group)
1038   (let ((to-address (mail-fetch-field "to")))
1039     (when (and to-address
1040                (gnus-alive-p))
1041       ;; This mail group doesn't have a `to-list', so we add one
1042       ;; here.  Magic!
1043       (when (gnus-y-or-n-p
1044              (format "Do you want to add this as `to-list': %s " to-address))
1045         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1046
1047 (defun gnus-put-message ()
1048   "Put the current message in some group and return to Gnus."
1049   (interactive)
1050   (let ((reply gnus-article-reply)
1051         (winconf gnus-prev-winconf)
1052         (group gnus-newsgroup-name))
1053     (unless (and group
1054                  (not (gnus-group-read-only-p group)))
1055       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1056
1057     (when (gnus-gethash group gnus-newsrc-hashtb)
1058       (error "No such group: %s" group))
1059     (save-excursion
1060       (save-restriction
1061         (widen)
1062         (message-narrow-to-headers)
1063         (let ((gnus-deletable-headers nil))
1064           (message-generate-headers
1065            (if (message-news-p)
1066                message-required-news-headers
1067              message-required-mail-headers)))
1068         (goto-char (point-max))
1069         (insert "Gcc: " group "\n")
1070         (widen)))
1071     (gnus-inews-do-gcc)
1072     (when (and (get-buffer gnus-group-buffer)
1073                (gnus-buffer-exists-p (car-safe reply))
1074                (cdr reply))
1075       (set-buffer (car reply))
1076       (gnus-summary-mark-article-as-replied (cdr reply)))
1077     (when winconf
1078       (set-window-configuration winconf))))
1079
1080 (defun gnus-article-mail (yank)
1081   "Send a reply to the address near point.
1082 If YANK is non-nil, include the original article."
1083   (interactive "P")
1084   (let ((address
1085          (buffer-substring
1086           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1087           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1088     (when address
1089       (message-reply address)
1090       (when yank
1091         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1092
1093 (defvar nntp-server-type)
1094 (defun gnus-bug ()
1095   "Send a bug report to the Gnus maintainers."
1096   (interactive)
1097   (unless (gnus-alive-p)
1098     (error "Gnus has been shut down"))
1099   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1100     (unless (message-mail-user-agent)
1101       (message-pop-to-buffer "*Gnus Bug*")
1102       (delete-other-windows)
1103       (when gnus-bug-create-help-buffer
1104         (switch-to-buffer "*Gnus Help Bug*")
1105         (erase-buffer)
1106         (insert gnus-bug-message)
1107         (goto-char (point-min))
1108         (sit-for 0)
1109         (set-buffer "*Gnus Bug*")))
1110     (let ((message-this-is-mail t))
1111       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1112     (when gnus-bug-create-help-buffer
1113       (push `(gnus-bug-kill-buffer) message-send-actions))
1114     (goto-char (point-min))
1115     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1116     (forward-line 1)
1117     (insert gnus-product-name " " gnus-version-number
1118             " (r" gnus-revision-number ") "
1119             "based on " gnus-original-product-name " v"
1120             gnus-original-version-number "\n"
1121             (emacs-version) "\n")
1122     (when (and (boundp 'nntp-server-type)
1123                (stringp nntp-server-type))
1124       (insert nntp-server-type))
1125     (insert "\n\n\n\n\n")
1126     (let (mime-content-types)
1127       (mime-edit-insert-tag "text" "plain" "; type=emacs-lisp"))
1128     (insert (with-temp-buffer
1129               (gnus-debug)
1130               (buffer-string)))
1131     (let (mime-content-types)
1132       (mime-edit-insert-tag "text" "plain"))
1133     (goto-char (point-min))
1134     (search-forward "Subject: " nil t)
1135     (message "")))
1136
1137 (defun gnus-bug-kill-buffer ()
1138   (when (get-buffer "*Gnus Help Bug*")
1139     (kill-buffer "*Gnus Help Bug*")))
1140
1141 (defun gnus-summary-yank-message (buffer n)
1142   "Yank the current article into a composed message."
1143   (interactive
1144    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1145          current-prefix-arg))
1146   (when (gnus-buffer-live-p buffer)
1147     (let ((summary-frame (selected-frame))
1148           (message-frame (when (static-if (featurep 'xemacs)
1149                                    (device-on-window-system-p)
1150                                  window-system)
1151                            (let ((window (get-buffer-window buffer t)))
1152                              (when window
1153                                (window-frame window)))))
1154           (separator (concat "^" (regexp-quote mail-header-separator)
1155                              "\n")))
1156       (gnus-summary-iterate n
1157         (gnus-summary-select-article)
1158         (gnus-copy-article-buffer)
1159         (when (frame-live-p message-frame)
1160           (raise-frame message-frame)
1161           (select-frame message-frame))
1162         (with-current-buffer buffer
1163           (when (save-excursion
1164                   (beginning-of-line)
1165                   (let (case-fold-search)
1166                     (and (not (re-search-backward separator nil t))
1167                          (re-search-forward separator nil t))))
1168             (goto-char (match-end 0)))
1169           (message-yank-buffer gnus-article-copy))
1170         (select-frame summary-frame))
1171       (when (frame-live-p message-frame)
1172         (select-frame message-frame)))))
1173
1174 (defun gnus-debug ()
1175   "Attempts to go through the Gnus source file and report what variables have been changed.
1176 The source file has to be in the Emacs load path."
1177   (interactive)
1178   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1179                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1180                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1181                  "nnmail.el" "nntp.el" "message.el"))
1182         (point (point))
1183         file expr olist sym)
1184     (gnus-message 4 "Please wait while we snoop your variables...")
1185     ;; Go through all the files looking for non-default values for variables.
1186     (save-excursion
1187       (sit-for 0)
1188       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1189       (while files
1190         (erase-buffer)
1191         (when (and (setq file (locate-library (pop files)))
1192                    (file-exists-p file))
1193           (insert-file-contents file)
1194           (goto-char (point-min))
1195           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1196               (gnus-message 4 "Malformed sources in file %s" file)
1197             (narrow-to-region (point-min) (point))
1198             (goto-char (point-min))
1199             (while (setq expr (ignore-errors (read (current-buffer))))
1200               (ignore-errors
1201                 (and (memq (car expr) '(defvar defcustom defvoo))
1202                      (stringp (nth 3 expr))
1203                      (or (not (boundp (nth 1 expr)))
1204                          (not (equal (eval (nth 2 expr))
1205                                      (symbol-value (nth 1 expr)))))
1206                      (push (nth 1 expr) olist)))))))
1207       (kill-buffer (current-buffer)))
1208     (when (setq olist (nreverse olist))
1209       (insert ";----------------- Environment follows ------------------\n\n"))
1210     (while olist
1211       (if (boundp (car olist))
1212           (condition-case ()
1213               (pp `(setq ,(car olist)
1214                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1215                                   (and (symbolp sym)
1216                                        (not (or (eq sym nil)
1217                                                 (eq sym t)))))
1218                               (list 'quote (symbol-value (car olist)))
1219                             (symbol-value (car olist))))
1220                   (current-buffer))
1221             (error
1222              (format "(setq %s 'whatever)\n" (car olist))))
1223         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1224       (setq olist (cdr olist)))
1225     ;; Remove any control chars - they seem to cause trouble for some
1226     ;; mailers.  (Byte-compiled output from the stuff above.)
1227     (goto-char point)
1228     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1229       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1230                      t t))
1231     ;; Break MIME tags purposely.
1232     (goto-char point)
1233     (while (re-search-forward mime-edit-tag-regexp nil t)
1234       (goto-char (1+ (match-beginning 0)))
1235       (insert "X"))))
1236
1237 ;;; Treatment of rejected articles.
1238 ;;; Bounced mail.
1239
1240 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1241   "Re-mail the current message.
1242 This only makes sense if the current message is a bounce message than
1243 contains some mail you have written which has been bounced back to
1244 you.
1245 If FETCH, try to fetch the article that this is a reply to, if indeed
1246 this is a reply."
1247   (interactive "P")
1248   (gnus-summary-select-article t)
1249   (set-buffer gnus-original-article-buffer)
1250   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1251     (gnus-setup-message 'compose-bounce
1252       (let* ((references (mail-fetch-field "references"))
1253              (parent (and references (gnus-parent-id references))))
1254         (message-bounce)
1255         ;; If there are references, we fetch the article we answered to.
1256         (and fetch parent
1257              (gnus-summary-refer-article parent)
1258              (gnus-summary-show-all-headers))))))
1259
1260 ;;; Gcc handling.
1261
1262 (defun gnus-inews-group-method (group)
1263   (cond ((and (null (gnus-get-info group))
1264               (eq (car gnus-message-archive-method)
1265                   (car
1266                    (gnus-server-to-method
1267                     (gnus-group-method group)))))
1268          ;; If the group doesn't exist, we assume
1269          ;; it's an archive group...
1270          gnus-message-archive-method)
1271         ;; Use the method.
1272         ((gnus-info-method (gnus-get-info group))
1273          (gnus-info-method (gnus-get-info group)))
1274         ;; Find the method.
1275         (t (gnus-group-method group))))
1276
1277 ;; Do Gcc handling, which copied the message over to some group.
1278 (defun gnus-inews-do-gcc (&optional gcc)
1279   (interactive)
1280   (when (gnus-alive-p)
1281     (save-excursion
1282       (save-restriction
1283         (message-narrow-to-headers)
1284         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1285               (coding-system-for-write 'raw-text)
1286               (output-coding-system 'raw-text)
1287               groups group method group-art)
1288           (when gcc
1289             (message-remove-header "gcc")
1290             (widen)
1291             (setq groups (message-unquote-tokens
1292                           (message-tokenize-header gcc " ,")))
1293             ;; Copy the article over to some group(s).
1294             (while (setq group (pop groups))
1295               (gnus-check-server
1296                (setq method (gnus-inews-group-method group)))
1297               (unless (gnus-request-group group nil method)
1298                 (gnus-request-create-group group method))
1299               (save-excursion
1300                 (nnheader-set-temp-buffer " *acc*")
1301                 (insert-buffer-substring message-encoding-buffer)
1302                 (gnus-run-hooks 'gnus-before-do-gcc-hook)
1303                 (goto-char (point-min))
1304                 (when (re-search-forward
1305                        (concat "^" (regexp-quote mail-header-separator) "$")
1306                        nil t)
1307                   (replace-match "" t t ))
1308                 (unless (setq group-art
1309                               (gnus-request-accept-article group method t t))
1310                   (gnus-message 1 "Couldn't store article in group %s: %s"
1311                                 group (gnus-status-message method))
1312                   (sit-for 2))
1313                 (when (and group-art gnus-inews-mark-gcc-as-read)
1314                   (gnus-group-mark-article-read group (cdr group-art)))
1315                 (kill-buffer (current-buffer))))))))))
1316
1317 (defun gnus-inews-insert-gcc ()
1318   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1319   (save-excursion
1320     (save-restriction
1321       (message-narrow-to-headers)
1322       (let* ((group gnus-outgoing-message-group)
1323              (gcc (cond
1324                    ((gnus-functionp group)
1325                     (funcall group))
1326                    ((or (stringp group) (list group))
1327                     group))))
1328         (when gcc
1329           (insert "Gcc: "
1330                   (if (stringp gcc) gcc
1331                     (mapconcat 'identity gcc " "))
1332                   "\n"))))))
1333
1334 (defun gnus-inews-insert-archive-gcc (&optional group)
1335   "Insert the Gcc to say where the article is to be archived."
1336   (let* ((var gnus-message-archive-group)
1337          (group (or group gnus-newsgroup-name ""))
1338          (gcc-self-val
1339           (and gnus-newsgroup-name
1340                (not (equal gnus-newsgroup-name ""))
1341                (gnus-group-find-parameter
1342                 gnus-newsgroup-name 'gcc-self)))
1343          result
1344          (groups
1345           (cond
1346            ((null gnus-message-archive-method)
1347             ;; Ignore.
1348             nil)
1349            ((stringp var)
1350             ;; Just a single group.
1351             (list var))
1352            ((null var)
1353             ;; We don't want this.
1354             nil)
1355            ((and (listp var) (stringp (car var)))
1356             ;; A list of groups.
1357             var)
1358            ((gnus-functionp var)
1359             ;; A function.
1360             (funcall var group))
1361            (t
1362             ;; An alist of regexps/functions/forms.
1363             (while (and var
1364                         (not
1365                          (setq result
1366                                (cond
1367                                 ((stringp (caar var))
1368                                  ;; Regexp.
1369                                  (when (string-match (caar var) group)
1370                                    (cdar var)))
1371                                 ((gnus-functionp (car var))
1372                                  ;; Function.
1373                                  (funcall (car var) group))
1374                                 (t
1375                                  (eval (car var)))))))
1376               (setq var (cdr var)))
1377             result)))
1378          name)
1379     (when (or groups gcc-self-val)
1380       (when (stringp groups)
1381         (setq groups (list groups)))
1382       (save-excursion
1383         (save-restriction
1384           (message-narrow-to-headers)
1385           (goto-char (point-max))
1386           (insert "Gcc: ")
1387           (if gcc-self-val
1388               ;; Use the `gcc-self' param value instead.
1389               (progn
1390                 (insert
1391                  (if (stringp gcc-self-val)
1392                      gcc-self-val
1393                    group))
1394                 (if (not (eq gcc-self-val 'none))
1395                     (insert "\n")
1396                   (progn
1397                     (beginning-of-line)
1398                     (kill-line))))
1399             ;; Use the list of groups.
1400             (while (setq name (pop groups))
1401               (insert (if (string-match ":" name)
1402                           name
1403                         (gnus-group-prefixed-name
1404                          name gnus-message-archive-method)))
1405               (when groups
1406                 (insert " ")))
1407             (insert "\n")))))))
1408
1409 ;;; Posting styles.
1410
1411 (defun gnus-configure-posting-styles ()
1412   "Configure posting styles according to `gnus-posting-styles'."
1413   (unless gnus-inhibit-posting-styles
1414     (let ((group (or gnus-newsgroup-name ""))
1415           (styles gnus-posting-styles)
1416           style match variable attribute value v results
1417           filep name address element)
1418       ;; If the group has a posting-style parameter, add it at the end with a
1419       ;; regexp matching everything, to be sure it takes precedence over all
1420       ;; the others.
1421       (when gnus-newsgroup-name
1422         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1423           (when tmp-style
1424             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1425       ;; Go through all styles and look for matches.
1426       (dolist (style styles)
1427         (setq match (pop style))
1428         (goto-char (point-min))
1429         (when (cond
1430                ((stringp match)
1431                 ;; Regexp string match on the group name.
1432                 (string-match match group))
1433                ((eq match 'header)
1434                 (and (gnus-buffer-live-p gnus-article-copy)
1435                      (with-current-buffer gnus-article-copy
1436                        (let ((header (message-fetch-field (pop style))))
1437                          (and header
1438                               (string-match (pop style) header))))))
1439                ((or (symbolp match)
1440                     (gnus-functionp match))
1441                 (cond
1442                  ((gnus-functionp match)
1443                   ;; Function to be called.
1444                   (funcall match))
1445                  ((boundp match)
1446                   ;; Variable to be checked.
1447                   (symbol-value match))))
1448                ((listp match)
1449                 ;; This is a form to be evaled.
1450                 (eval match)))
1451           ;; We have a match, so we set the variables.
1452           (dolist (attribute style)
1453             (setq element (pop attribute)
1454                   variable nil
1455                   filep nil)
1456             (setq value
1457                   (cond
1458                    ((eq (car attribute) ':file)
1459                     (setq filep t)
1460                     (cadr attribute))
1461                    ((eq (car attribute) :value)
1462                     (cadr attribute))
1463                    (t
1464                     (car attribute))))
1465             ;; We get the value.
1466             (setq v
1467                   (cond
1468                    ((stringp value)
1469                     value)
1470                    ((or (symbolp value)
1471                         (gnus-functionp value))
1472                     (cond ((gnus-functionp value)
1473                            (funcall value))
1474                           ((boundp value)
1475                            (symbol-value value))))
1476                    ((listp value)
1477                     (eval value))))
1478             ;; Translate obsolescent value.
1479             (when (eq element 'signature-file)
1480               (setq element 'signature
1481                     filep t))
1482             ;; Get the contents of file elems.
1483             (when (and filep v)
1484               (setq v (with-temp-buffer
1485                         (insert-file-contents v)
1486                         (buffer-string))))
1487             (setq results (delq (assoc element results) results))
1488             (push (cons element v) results))))
1489       ;; Now we have all the styles, so we insert them.
1490       (setq name (assq 'name results)
1491             address (assq 'address results))
1492       (setq results (delq name (delq address results)))
1493       (make-local-variable 'message-setup-hook)
1494       (dolist (result results)
1495         (add-hook 'message-setup-hook
1496                   (cond
1497                    ((eq 'eval (car result))
1498                     'ignore)
1499                    ((eq 'body (car result))
1500                     `(lambda ()
1501                        (save-excursion
1502                          (message-goto-body)
1503                          (insert ,(cdr result)))))
1504                    ((eq 'signature (car result))
1505                     (set (make-local-variable 'message-signature) nil)
1506                     (set (make-local-variable 'message-signature-file) nil)
1507                     (if (not (cdr result))
1508                         'ignore
1509                       `(lambda ()
1510                          (save-excursion
1511                            (let ((message-signature ,(cdr result)))
1512                              (when message-signature
1513                                (message-insert-signature)))))))
1514                    (t
1515                     (let ((header
1516                            (if (symbolp (car result))
1517                                (capitalize (symbol-name (car result)))
1518                              (car result))))
1519                       `(lambda ()
1520                          (save-excursion
1521                            (message-remove-header ,header)
1522                            (let ((value ,(cdr result)))
1523                              (when value
1524                                (message-goto-eoh)
1525                                (insert ,header ": " value "\n"))))))))))
1526       (when (or name address)
1527         (add-hook 'message-setup-hook
1528                   `(lambda ()
1529                      (set (make-local-variable 'user-mail-address)
1530                           ,(or (cdr address) user-mail-address))
1531                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1532                            (user-mail-address
1533                             ,(or (cdr address) user-mail-address)))
1534                        (save-excursion
1535                          (message-remove-header "From")
1536                          (message-goto-eoh)
1537                          (insert "From: " (message-make-from) "\n")))))))))
1538
1539
1540 ;;; @ for MIME Edit mode
1541 ;;;
1542
1543 (defun gnus-maybe-setup-default-charset ()
1544   (let ((charset
1545          (and (boundp 'gnus-summary-buffer)
1546               (buffer-live-p gnus-summary-buffer)
1547               (save-excursion
1548                 (set-buffer gnus-summary-buffer)
1549                 default-mime-charset))))
1550     (if charset
1551         (progn
1552           (make-local-variable 'default-mime-charset)
1553           (setq default-mime-charset charset)
1554           ))))
1555
1556
1557 ;;; @ for MIME view mode
1558 ;;;
1559
1560 (defun gnus-following-method (buf)
1561   (gnus-setup-message 'reply-yank
1562     (set-buffer buf)
1563     (if (message-news-p)
1564         (message-followup)
1565       (message-reply nil 'wide))
1566     (let ((message-reply-buffer buf))
1567       (message-yank-original))
1568     (message-goto-body))
1569   (kill-buffer buf))
1570
1571
1572 ;;; Allow redefinition of functions.
1573
1574 (gnus-ems-redefine)
1575
1576 (provide 'gnus-msg)
1577
1578 ;;; gnus-msg.el ends here