Synch with Oort Gnus.
[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          (frame (when (and message-use-multi-frames more-than-one)
438                   (window-frame (get-buffer-window (current-buffer)))))
439          refs beg article)
440     (message-goto-body)
441     (while (setq article (pop articles))
442       (save-window-excursion
443         (set-buffer gnus-summary-buffer)
444         (gnus-summary-select-article nil nil nil article)
445         (gnus-summary-remove-process-mark article))
446       (when frame
447         (select-frame frame))
448
449       ;; Gathering references.
450       (when more-than-one
451         (setq refs (message-list-references
452                     refs
453                     (mail-header-references gnus-current-headers)
454                     (mail-header-message-id gnus-current-headers))))
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 (search-forward "\n\n" nil t) (point)))
582             ;; Delete the headers from the displayed articles.
583             (set-buffer gnus-article-copy)
584             (delete-region (goto-char (point-min))
585                            (or (search-forward "\n\n" nil t) (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) "" to-group)))
631           ;; The is mail.
632           (if post
633               (progn
634                 (message-mail (or to-address to-list))
635                 ;; Arrange for mail groups that have no `to-address' to
636                 ;; get that when the user sends off the mail.
637                 (when (and (not to-list)
638                            (not to-address)
639                            add-to-list)
640                   (push (list 'gnus-inews-add-to-address pgroup)
641                         message-send-actions)))
642             (set-buffer gnus-article-copy)
643             (gnus-msg-treat-broken-reply-to)
644             (message-wide-reply to-address)))
645         (when yank
646           (gnus-inews-yank-articles yank))))))
647
648 (defun gnus-msg-treat-broken-reply-to ()
649   "Remove the Reply-to header iff broken-reply-to."
650   (when (gnus-group-find-parameter
651          gnus-newsgroup-name 'broken-reply-to)
652     (save-restriction
653       (message-narrow-to-head)
654       (message-remove-header "reply-to"))))
655
656 (defun gnus-post-method (arg group &optional silent)
657   "Return the posting method based on GROUP and ARG.
658 If SILENT, don't prompt the user."
659   (let ((group-method (gnus-find-method-for-group group)))
660     (cond
661      ;; If the group-method is nil (which shouldn't happen) we use
662      ;; the default method.
663      ((null group-method)
664       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
665           gnus-select-method message-post-method))
666      ;; We want the inverse of the default
667      ((and arg (not (eq arg 0)))
668       (if (eq gnus-post-method 'active)
669           gnus-select-method
670         group-method))
671      ;; We query the user for a post method.
672      ((or arg
673           (and gnus-post-method
674                (not (eq gnus-post-method 'current))
675                (listp (car gnus-post-method))))
676       (let* ((methods
677               ;; Collect all methods we know about.
678               (append
679                (when (and gnus-post-method
680                           (not (eq gnus-post-method 'current)))
681                  (if (listp (car gnus-post-method))
682                      gnus-post-method
683                    (list gnus-post-method)))
684                gnus-secondary-select-methods
685                (mapcar 'cdr gnus-server-alist)
686                (mapcar 'car gnus-opened-servers)
687                (list gnus-select-method)
688                (list group-method)))
689              method-alist post-methods method)
690         ;; Weed out all mail methods.
691         (while methods
692           (setq method (gnus-server-get-method "" (pop methods)))
693           (when (and (or (gnus-method-option-p method 'post)
694                          (gnus-method-option-p method 'post-mail))
695                      (not (member method post-methods)))
696             (push method post-methods)))
697         ;; Create a name-method alist.
698         (setq method-alist
699               (mapcar
700                (lambda (m)
701                  (if (equal (cadr m) "")
702                      (list (symbol-name (car m)) m)
703                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
704                post-methods))
705         ;; Query the user.
706         (cadr
707          (assoc
708           (setq gnus-last-posting-server
709                 (if (and silent
710                          gnus-last-posting-server)
711                     ;; Just use the last value.
712                     gnus-last-posting-server
713                   (completing-read
714                    "Posting method: " method-alist nil t
715                    (cons (or gnus-last-posting-server "") 0))))
716           method-alist))))
717      ;; Override normal method.
718      ((and (eq gnus-post-method 'current)
719            (not (eq (car group-method) 'nndraft))
720            (gnus-get-function group-method 'request-post t)
721            (not arg))
722       group-method)
723      ((and gnus-post-method
724            (not (eq gnus-post-method 'current)))
725       gnus-post-method)
726      ;; Use the normal select method.
727      (t gnus-select-method))))
728
729 \f
730
731 (defun gnus-message-make-user-agent (&optional include-mime-info max-column)
732   "Return user-agent info.
733 INCLUDE-MIME-INFO the optional first argument if it is non-nil and the variable
734   `mime-edit-user-agent-value' exists, the return value will include it.
735 MAX-COLUMN the optional second argument if it is specified, the return value
736   will be folded up in the proper way."
737   (let ((user-agent (if (and include-mime-info
738                              (boundp 'mime-edit-user-agent-value))
739                         (concat (gnus-extended-version)
740                                 " "
741                                 mime-edit-user-agent-value)
742                       (gnus-extended-version))))
743     (if max-column
744         (let (boundary)
745           (unless (natnump max-column) (setq max-column 76))
746           (with-temp-buffer
747             (insert "            " user-agent)
748             (goto-char 13)
749             (while (re-search-forward "[\n\t ]+" nil t)
750               (replace-match " "))
751             (goto-char 13)
752             (while (re-search-forward "[^ ()/]+\\(/[^ ()/]+\\)? ?" nil t)
753               (while (eq ?\( (char-after (point)))
754                 (forward-list)
755                 (skip-chars-forward " "))
756               (skip-chars-backward " ")
757               (if (> (current-column) max-column)
758                   (progn
759                     (if (or (not boundary) (eq ?\n (char-after boundary)))
760                         (progn
761                           (setq boundary (point))
762                           (unless (eobp)
763                             (delete-char 1)
764                             (insert "\n ")))
765                       (goto-char boundary)
766                       (delete-char 1)
767                       (insert "\n ")))
768                 (setq boundary (point))))
769             (buffer-substring 13 (point-max))))
770       user-agent)))
771
772 \f
773 ;;;
774 ;;; Gnus Mail Functions
775 ;;;
776
777 ;;; Mail reply commands of Gnus summary mode
778
779 (defun gnus-summary-reply (&optional yank wide very-wide)
780   "Start composing a mail reply to the current message.
781 If prefix argument YANK is non-nil, the original article is yanked
782 automatically.
783 If WIDE, make a wide reply.
784 If VERY-WIDE, make a very wide reply."
785   (interactive
786    (list (and current-prefix-arg
787               (gnus-summary-work-articles 1))))
788   ;; Stripping headers should be specified with mail-yank-ignored-headers.
789   (when yank
790     (gnus-summary-goto-subject (car yank)))
791   (let ((gnus-article-reply (or yank (gnus-summary-article-number)))
792         (headers ""))
793     (gnus-setup-message (if yank 'reply-yank 'reply)
794       (if (not very-wide)
795           (gnus-summary-select-article)
796         (dolist (article very-wide)
797           (gnus-summary-select-article nil nil nil article)
798           (save-excursion
799             (set-buffer (gnus-copy-article-buffer))
800             (gnus-msg-treat-broken-reply-to)
801             (save-restriction
802               (message-narrow-to-head)
803               (setq headers (concat headers (buffer-string)))))))
804       (set-buffer (gnus-copy-article-buffer))
805       (gnus-msg-treat-broken-reply-to)
806       (save-restriction
807         (message-narrow-to-head)
808         (when very-wide
809           (erase-buffer)
810           (insert headers))
811         (goto-char (point-max)))
812       (message-reply nil wide)
813       (when yank
814         (gnus-inews-yank-articles yank)))))
815
816 (defun gnus-summary-reply-with-original (n &optional wide)
817   "Start composing a reply mail to the current message.
818 The original article will be yanked."
819   (interactive "P")
820   (gnus-summary-reply (gnus-summary-work-articles n) wide))
821
822 (defun gnus-summary-wide-reply (&optional yank)
823   "Start composing a wide reply mail to the current message.
824 If prefix argument YANK is non-nil, the original article is yanked
825 automatically."
826   (interactive
827    (list (and current-prefix-arg
828               (gnus-summary-work-articles 1))))
829   (gnus-summary-reply yank t))
830
831 (defun gnus-summary-wide-reply-with-original (n)
832   "Start composing a wide reply mail to the current message.
833 The original article will be yanked."
834   (interactive "P")
835   (gnus-summary-reply-with-original n t))
836
837 (defun gnus-summary-very-wide-reply (&optional yank)
838   "Start composing a very wide reply mail to the current message.
839 If prefix argument YANK is non-nil, the original article is yanked
840 automatically."
841   (interactive
842    (list (and current-prefix-arg
843               (gnus-summary-work-articles 1))))
844   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
845
846 (defun gnus-summary-very-wide-reply-with-original (n)
847   "Start composing a very wide reply mail to the current message.
848 The original article will be yanked."
849   (interactive "P")
850   (gnus-summary-reply
851    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
852
853 (defun gnus-summary-mail-forward (&optional full-headers post)
854   "Forward the current message to another user.
855 If FULL-HEADERS (the prefix), include full headers when forwarding."
856   (interactive "P")
857   (gnus-setup-message 'forward
858     (gnus-summary-select-article)
859     (let ((charset default-mime-charset))
860       (set-buffer gnus-original-article-buffer)
861       (make-local-variable 'default-mime-charset)
862       (setq default-mime-charset charset))
863     (let ((message-included-forward-headers
864            (if full-headers "" message-included-forward-headers)))
865       (message-forward post))))
866
867 (defun gnus-summary-digest-mail-forward (&optional n post)
868   "Digests and forwards all articles in this series.
869 If N is a positive number, forward the N next articles.
870 If N is a negative number, forward the N previous articles.
871 If N is nil and any articles have been marked with the process mark,
872 forward those articles instead.
873 Optional POST will use news to forward instead of mail."
874   (interactive "P")
875   (let ((articles (gnus-summary-work-articles n))
876         (topics "Topics:\n")
877         subject article frame)
878     (when (car articles)
879       (gnus-setup-message 'forward
880         (gnus-summary-select-article)
881         (if (cdr articles)
882             (setq articles (sort articles '<)
883                   subject "Digested Articles")
884           (with-current-buffer gnus-original-article-buffer
885             (setq subject (message-make-forward-subject))))
886         (if post
887             (message-news nil subject)
888           (message-mail nil subject))
889         (when (and message-use-multi-frames (cdr articles))
890           (setq frame (window-frame (get-buffer-window (current-buffer)))))
891         (message-goto-body)
892         (while (setq article (pop articles))
893           (save-window-excursion
894             (set-buffer gnus-summary-buffer)
895             (gnus-summary-select-article nil nil nil article)
896             (setq topics (concat topics "    "
897                                  (mail-header-subject gnus-current-headers)
898                                  "\n"))
899             (gnus-summary-remove-process-mark article))
900           (when frame
901             (select-frame frame))
902           (insert (mime-make-tag "message" "rfc822") "\n")
903           (narrow-to-region (point) (point))
904           (insert-buffer-substring gnus-original-article-buffer)
905           (save-restriction
906             (article-narrow-to-head)
907             (message-remove-header message-included-forward-headers t nil t))
908           (goto-char (point-max))
909           (widen))
910         (push-mark)
911         (message-goto-body)
912         (insert topics)
913         (message-goto-body)
914         (mime-edit-enclose-digest-region (point)(mark t))))))
915
916 (defun gnus-summary-digest-post-forward (&optional n)
917   "Digest and forwards all articles in this series to a newsgroup.
918 If N is a positive number, forward the N next articles.
919 If N is a negative number, forward the N previous articles.
920 If N is nil and any articles have been marked with the process mark,
921 forward those articles instead."
922   (interactive "P")
923   (gnus-summary-digest-mail-forward n t))
924
925 (defun gnus-summary-resend-message (address n)
926   "Resend the current article to ADDRESS."
927   (interactive
928    (list (message-read-from-minibuffer "Resend message(s) to: ")
929          current-prefix-arg))
930   (let ((articles (gnus-summary-work-articles n))
931         article)
932     (while (setq article (pop articles))
933       (gnus-summary-select-article nil nil nil article)
934       (save-excursion
935         (set-buffer gnus-original-article-buffer)
936         (message-resend address))
937       (gnus-summary-mark-article-as-forwarded article))))
938
939 (defun gnus-summary-post-forward (&optional full-headers)
940   "Forward the current article to a newsgroup.
941 If FULL-HEADERS (the prefix), include full headers when forwarding."
942   (interactive "P")
943   (gnus-summary-mail-forward full-headers t))
944
945 (defvar gnus-nastygram-message
946   "The following article was inappropriately posted to %s.\n\n"
947   "Format string to insert in nastygrams.
948 The current group name will be inserted at \"%s\".")
949
950 (defun gnus-summary-mail-nastygram (n)
951   "Send a nastygram to the author of the current article."
952   (interactive "P")
953   (when (or gnus-expert-user
954             (gnus-y-or-n-p
955              "Really send a nastygram to the author of the current article? "))
956     (let ((group gnus-newsgroup-name))
957       (gnus-summary-reply-with-original n)
958       (set-buffer gnus-message-buffer)
959       (message-goto-body)
960       (insert (format gnus-nastygram-message group))
961       (message-send-and-exit))))
962
963 (defun gnus-summary-mail-crosspost-complaint (n)
964   "Send a complaint about crossposting to the current article(s)."
965   (interactive "P")
966   (let ((articles (gnus-summary-work-articles n))
967         article)
968     (while (setq article (pop articles))
969       (set-buffer gnus-summary-buffer)
970       (gnus-summary-goto-subject article)
971       (let ((group (gnus-group-real-name gnus-newsgroup-name))
972             newsgroups followup-to)
973         (gnus-summary-select-article)
974         (set-buffer gnus-original-article-buffer)
975         (if (and (<= (length (message-tokenize-header
976                               (setq newsgroups
977                                     (mail-fetch-field "newsgroups"))
978                               ", "))
979                      1)
980                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
981                      (not (member group (message-tokenize-header
982                                          followup-to ", ")))))
983             (if followup-to
984                 (gnus-message 1 "Followup-to restricted")
985               (gnus-message 1 "Not a crossposted article"))
986           (set-buffer gnus-summary-buffer)
987           (gnus-summary-reply-with-original 1)
988           (set-buffer gnus-message-buffer)
989           (message-goto-body)
990           (insert (format gnus-crosspost-complaint newsgroups group))
991           (message-goto-subject)
992           (re-search-forward " *$")
993           (replace-match " (crosspost notification)" t t)
994           (gnus-deactivate-mark)
995           (when (gnus-y-or-n-p "Send this complaint? ")
996             (message-send-and-exit)))))))
997
998 (defun gnus-summary-mail-other-window ()
999   "Compose mail in other window."
1000   (interactive)
1001   (gnus-setup-message 'message
1002     (message-mail)))
1003
1004 (defun gnus-mail-parse-comma-list ()
1005   (let (accumulated
1006         beg)
1007     (skip-chars-forward " ")
1008     (while (not (eobp))
1009       (setq beg (point))
1010       (skip-chars-forward "^,")
1011       (while (zerop
1012               (save-excursion
1013                 (save-restriction
1014                   (let ((i 0))
1015                     (narrow-to-region beg (point))
1016                     (goto-char beg)
1017                     (logand (progn
1018                               (while (search-forward "\"" nil t)
1019                                 (incf i))
1020                               (if (zerop i) 2 i))
1021                             2)))))
1022         (skip-chars-forward ",")
1023         (skip-chars-forward "^,"))
1024       (skip-chars-backward " ")
1025       (push (buffer-substring beg (point))
1026             accumulated)
1027       (skip-chars-forward "^,")
1028       (skip-chars-forward ", "))
1029     accumulated))
1030
1031 (defun gnus-inews-add-to-address (group)
1032   (let ((to-address (mail-fetch-field "to")))
1033     (when (and to-address
1034                (gnus-alive-p))
1035       ;; This mail group doesn't have a `to-list', so we add one
1036       ;; here.  Magic!
1037       (when (gnus-y-or-n-p
1038              (format "Do you want to add this as `to-list': %s " to-address))
1039         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1040
1041 (defun gnus-put-message ()
1042   "Put the current message in some group and return to Gnus."
1043   (interactive)
1044   (let ((reply gnus-article-reply)
1045         (winconf gnus-prev-winconf)
1046         (group gnus-newsgroup-name))
1047     (unless (and group
1048                  (not (gnus-group-read-only-p group)))
1049       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1050
1051     (when (gnus-gethash group gnus-newsrc-hashtb)
1052       (error "No such group: %s" group))
1053     (save-excursion
1054       (save-restriction
1055         (widen)
1056         (message-narrow-to-headers)
1057         (let ((gnus-deletable-headers nil))
1058           (message-generate-headers
1059            (if (message-news-p)
1060                message-required-news-headers
1061              message-required-mail-headers)))
1062         (goto-char (point-max))
1063         (insert "Gcc: " group "\n")
1064         (widen)))
1065     (gnus-inews-do-gcc)
1066     (when (and (get-buffer gnus-group-buffer)
1067                (gnus-buffer-exists-p (car-safe reply))
1068                (cdr reply))
1069       (set-buffer (car reply))
1070       (gnus-summary-mark-article-as-replied (cdr reply)))
1071     (when winconf
1072       (set-window-configuration winconf))))
1073
1074 (defun gnus-article-mail (yank)
1075   "Send a reply to the address near point.
1076 If YANK is non-nil, include the original article."
1077   (interactive "P")
1078   (let ((address
1079          (buffer-substring
1080           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1081           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1082     (when address
1083       (message-reply address)
1084       (when yank
1085         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1086
1087 (defvar nntp-server-type)
1088 (defun gnus-bug ()
1089   "Send a bug report to the Gnus maintainers."
1090   (interactive)
1091   (unless (gnus-alive-p)
1092     (error "Gnus has been shut down"))
1093   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1094     (unless (message-mail-user-agent)
1095       (message-pop-to-buffer "*Gnus Bug*")
1096       (delete-other-windows)
1097       (when gnus-bug-create-help-buffer
1098         (switch-to-buffer "*Gnus Help Bug*")
1099         (erase-buffer)
1100         (insert gnus-bug-message)
1101         (goto-char (point-min))
1102         (sit-for 0)
1103         (set-buffer "*Gnus Bug*")))
1104     (let ((message-this-is-mail t))
1105       (message-setup `((To . ,gnus-maintainer) (Subject . ""))))
1106     (when gnus-bug-create-help-buffer
1107       (push `(gnus-bug-kill-buffer) message-send-actions))
1108     (goto-char (point-min))
1109     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
1110     (forward-line 1)
1111     (insert gnus-product-name " " gnus-version-number
1112             " (r" gnus-revision-number ") "
1113             "based on " gnus-original-product-name " v"
1114             gnus-original-version-number "\n"
1115             (emacs-version) "\n")
1116     (when (and (boundp 'nntp-server-type)
1117                (stringp nntp-server-type))
1118       (insert nntp-server-type))
1119     (insert "\n\n\n\n\n")
1120     (let (mime-content-types)
1121       (mime-edit-insert-tag "text" "plain" "; type=emacs-lisp"))
1122     (insert (with-temp-buffer
1123               (gnus-debug)
1124               (buffer-string)))
1125     (let (mime-content-types)
1126       (mime-edit-insert-tag "text" "plain"))
1127     (goto-char (point-min))
1128     (search-forward "Subject: " nil t)
1129     (message "")))
1130
1131 (defun gnus-bug-kill-buffer ()
1132   (when (get-buffer "*Gnus Help Bug*")
1133     (kill-buffer "*Gnus Help Bug*")))
1134
1135 (defun gnus-summary-yank-message (buffer n)
1136   "Yank the current article into a composed message."
1137   (interactive
1138    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
1139          current-prefix-arg))
1140   (when (gnus-buffer-live-p buffer)
1141     (let ((summary-frame (selected-frame))
1142           (message-frame (when (static-if (featurep 'xemacs)
1143                                    (device-on-window-system-p)
1144                                  window-system)
1145                            (let ((window (get-buffer-window buffer t)))
1146                              (when window
1147                                (window-frame window)))))
1148           (separator (concat "^" (regexp-quote mail-header-separator)
1149                              "\n")))
1150       (gnus-summary-iterate n
1151         (gnus-summary-select-article)
1152         (gnus-copy-article-buffer)
1153         (when (frame-live-p message-frame)
1154           (raise-frame message-frame)
1155           (select-frame message-frame))
1156         (with-current-buffer buffer
1157           (when (save-excursion
1158                   (beginning-of-line)
1159                   (let (case-fold-search)
1160                     (and (not (re-search-backward separator nil t))
1161                          (re-search-forward separator nil t))))
1162             (goto-char (match-end 0)))
1163           (message-yank-buffer gnus-article-copy))
1164         (select-frame summary-frame))
1165       (when (frame-live-p message-frame)
1166         (select-frame message-frame)))))
1167
1168 (defun gnus-debug ()
1169   "Attempts to go through the Gnus source file and report what variables have been changed.
1170 The source file has to be in the Emacs load path."
1171   (interactive)
1172   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1173                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1174                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1175                  "nnmail.el" "nntp.el" "message.el"))
1176         (point (point))
1177         file expr olist sym)
1178     (gnus-message 4 "Please wait while we snoop your variables...")
1179     ;; Go through all the files looking for non-default values for variables.
1180     (save-excursion
1181       (sit-for 0)
1182       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1183       (while files
1184         (erase-buffer)
1185         (when (and (setq file (locate-library (pop files)))
1186                    (file-exists-p file))
1187           (insert-file-contents file)
1188           (goto-char (point-min))
1189           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1190               (gnus-message 4 "Malformed sources in file %s" file)
1191             (narrow-to-region (point-min) (point))
1192             (goto-char (point-min))
1193             (while (setq expr (ignore-errors (read (current-buffer))))
1194               (ignore-errors
1195                 (and (memq (car expr) '(defvar defcustom defvoo))
1196                      (stringp (nth 3 expr))
1197                      (or (not (boundp (nth 1 expr)))
1198                          (not (equal (eval (nth 2 expr))
1199                                      (symbol-value (nth 1 expr)))))
1200                      (push (nth 1 expr) olist)))))))
1201       (kill-buffer (current-buffer)))
1202     (when (setq olist (nreverse olist))
1203       (insert ";----------------- Environment follows ------------------\n\n"))
1204     (while olist
1205       (if (boundp (car olist))
1206           (condition-case ()
1207               (pp `(setq ,(car olist)
1208                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1209                                   (and (symbolp sym)
1210                                        (not (or (eq sym nil)
1211                                                 (eq sym t)))))
1212                               (list 'quote (symbol-value (car olist)))
1213                             (symbol-value (car olist))))
1214                   (current-buffer))
1215             (error
1216              (format "(setq %s 'whatever)\n" (car olist))))
1217         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1218       (setq olist (cdr olist)))
1219     ;; Remove any control chars - they seem to cause trouble for some
1220     ;; mailers.  (Byte-compiled output from the stuff above.)
1221     (goto-char point)
1222     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1223       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1224                      t t))
1225     ;; Break MIME tags purposely.
1226     (goto-char point)
1227     (while (re-search-forward mime-edit-tag-regexp nil t)
1228       (goto-char (1+ (match-beginning 0)))
1229       (insert "X"))))
1230
1231 ;;; Treatment of rejected articles.
1232 ;;; Bounced mail.
1233
1234 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1235   "Re-mail the current message.
1236 This only makes sense if the current message is a bounce message than
1237 contains some mail you have written which has been bounced back to
1238 you.
1239 If FETCH, try to fetch the article that this is a reply to, if indeed
1240 this is a reply."
1241   (interactive "P")
1242   (gnus-summary-select-article t)
1243   (set-buffer gnus-original-article-buffer)
1244   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1245     (gnus-setup-message 'compose-bounce
1246       (let* ((references (mail-fetch-field "references"))
1247              (parent (and references (gnus-parent-id references))))
1248         (message-bounce)
1249         ;; If there are references, we fetch the article we answered to.
1250         (and fetch parent
1251              (gnus-summary-refer-article parent)
1252              (gnus-summary-show-all-headers))))))
1253
1254 ;;; Gcc handling.
1255
1256 (defun gnus-inews-group-method (group)
1257   (cond ((and (null (gnus-get-info group))
1258               (eq (car gnus-message-archive-method)
1259                   (car
1260                    (gnus-server-to-method
1261                     (gnus-group-method group)))))
1262          ;; If the group doesn't exist, we assume
1263          ;; it's an archive group...
1264          gnus-message-archive-method)
1265         ;; Use the method.
1266         ((gnus-info-method (gnus-get-info group))
1267          (gnus-info-method (gnus-get-info group)))
1268         ;; Find the method.
1269         (t (gnus-group-method group))))
1270
1271 ;; Do Gcc handling, which copied the message over to some group.
1272 (defun gnus-inews-do-gcc (&optional gcc)
1273   (interactive)
1274   (when (gnus-alive-p)
1275     (save-excursion
1276       (save-restriction
1277         (message-narrow-to-headers)
1278         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1279               (coding-system-for-write 'raw-text)
1280               (output-coding-system 'raw-text)
1281               groups group method group-art)
1282           (when gcc
1283             (message-remove-header "gcc")
1284             (widen)
1285             (setq groups (message-unquote-tokens
1286                           (message-tokenize-header gcc " ,")))
1287             ;; Copy the article over to some group(s).
1288             (while (setq group (pop groups))
1289               (gnus-check-server
1290                (setq method (gnus-inews-group-method group)))
1291               (unless (gnus-request-group group nil method)
1292                 (gnus-request-create-group group method))
1293               (save-excursion
1294                 (nnheader-set-temp-buffer " *acc*")
1295                 (insert-buffer-substring message-encoding-buffer)
1296                 (gnus-run-hooks 'gnus-before-do-gcc-hook)
1297                 (goto-char (point-min))
1298                 (when (re-search-forward
1299                        (concat "^" (regexp-quote mail-header-separator) "$")
1300                        nil t)
1301                   (replace-match "" t t ))
1302                 (unless (setq group-art
1303                               (gnus-request-accept-article group method t t))
1304                   (gnus-message 1 "Couldn't store article in group %s: %s"
1305                                 group (gnus-status-message method))
1306                   (sit-for 2))
1307                 (when (and group-art gnus-inews-mark-gcc-as-read)
1308                   (gnus-group-mark-article-read group (cdr group-art)))
1309                 (kill-buffer (current-buffer))))))))))
1310
1311 (defun gnus-inews-insert-gcc ()
1312   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1313   (save-excursion
1314     (save-restriction
1315       (message-narrow-to-headers)
1316       (let* ((group gnus-outgoing-message-group)
1317              (gcc (cond
1318                    ((gnus-functionp group)
1319                     (funcall group))
1320                    ((or (stringp group) (list group))
1321                     group))))
1322         (when gcc
1323           (insert "Gcc: "
1324                   (if (stringp gcc) gcc
1325                     (mapconcat 'identity gcc " "))
1326                   "\n"))))))
1327
1328 (defun gnus-inews-insert-archive-gcc (&optional group)
1329   "Insert the Gcc to say where the article is to be archived."
1330   (let* ((var gnus-message-archive-group)
1331          (group (or group gnus-newsgroup-name ""))
1332          (gcc-self-val
1333           (and gnus-newsgroup-name
1334                (not (equal gnus-newsgroup-name ""))
1335                (gnus-group-find-parameter
1336                 gnus-newsgroup-name 'gcc-self)))
1337          result
1338          (groups
1339           (cond
1340            ((null gnus-message-archive-method)
1341             ;; Ignore.
1342             nil)
1343            ((stringp var)
1344             ;; Just a single group.
1345             (list var))
1346            ((null var)
1347             ;; We don't want this.
1348             nil)
1349            ((and (listp var) (stringp (car var)))
1350             ;; A list of groups.
1351             var)
1352            ((gnus-functionp var)
1353             ;; A function.
1354             (funcall var group))
1355            (t
1356             ;; An alist of regexps/functions/forms.
1357             (while (and var
1358                         (not
1359                          (setq result
1360                                (cond
1361                                 ((stringp (caar var))
1362                                  ;; Regexp.
1363                                  (when (string-match (caar var) group)
1364                                    (cdar var)))
1365                                 ((gnus-functionp (car var))
1366                                  ;; Function.
1367                                  (funcall (car var) group))
1368                                 (t
1369                                  (eval (car var)))))))
1370               (setq var (cdr var)))
1371             result)))
1372          name)
1373     (when (or groups gcc-self-val)
1374       (when (stringp groups)
1375         (setq groups (list groups)))
1376       (save-excursion
1377         (save-restriction
1378           (message-narrow-to-headers)
1379           (goto-char (point-max))
1380           (insert "Gcc: ")
1381           (if gcc-self-val
1382               ;; Use the `gcc-self' param value instead.
1383               (progn
1384                 (insert
1385                  (if (stringp gcc-self-val)
1386                      gcc-self-val
1387                    group))
1388                 (if (not (eq gcc-self-val 'none))
1389                     (insert "\n")
1390                   (progn
1391                     (beginning-of-line)
1392                     (kill-line))))
1393             ;; Use the list of groups.
1394             (while (setq name (pop groups))
1395               (insert (if (string-match ":" name)
1396                           name
1397                         (gnus-group-prefixed-name
1398                          name gnus-message-archive-method)))
1399               (when groups
1400                 (insert " ")))
1401             (insert "\n")))))))
1402
1403 ;;; Posting styles.
1404
1405 (defun gnus-configure-posting-styles ()
1406   "Configure posting styles according to `gnus-posting-styles'."
1407   (unless gnus-inhibit-posting-styles
1408     (let ((group (or gnus-newsgroup-name ""))
1409           (styles gnus-posting-styles)
1410           style match variable attribute value v results
1411           filep name address element)
1412       ;; If the group has a posting-style parameter, add it at the end with a
1413       ;; regexp matching everything, to be sure it takes precedence over all
1414       ;; the others.
1415       (when gnus-newsgroup-name
1416         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1417           (when tmp-style
1418             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1419       ;; Go through all styles and look for matches.
1420       (dolist (style styles)
1421         (setq match (pop style))
1422         (goto-char (point-min))
1423         (when (cond
1424                ((stringp match)
1425                 ;; Regexp string match on the group name.
1426                 (string-match match group))
1427                ((eq match 'header)
1428                 (and (gnus-buffer-live-p gnus-article-copy)
1429                      (with-current-buffer gnus-article-copy
1430                        (let ((header (message-fetch-field (pop style))))
1431                          (and header
1432                               (string-match (pop style) header))))))
1433                ((or (symbolp match)
1434                     (gnus-functionp match))
1435                 (cond
1436                  ((gnus-functionp match)
1437                   ;; Function to be called.
1438                   (funcall match))
1439                  ((boundp match)
1440                   ;; Variable to be checked.
1441                   (symbol-value match))))
1442                ((listp match)
1443                 ;; This is a form to be evaled.
1444                 (eval match)))
1445           ;; We have a match, so we set the variables.
1446           (dolist (attribute style)
1447             (setq element (pop attribute)
1448                   variable nil
1449                   filep nil)
1450             (setq value
1451                   (cond
1452                    ((eq (car attribute) ':file)
1453                     (setq filep t)
1454                     (cadr attribute))
1455                    ((eq (car attribute) :value)
1456                     (cadr attribute))
1457                    (t
1458                     (car attribute))))
1459             ;; We get the value.
1460             (setq v
1461                   (cond
1462                    ((stringp value)
1463                     value)
1464                    ((or (symbolp value)
1465                         (gnus-functionp value))
1466                     (cond ((gnus-functionp value)
1467                            (funcall value))
1468                           ((boundp value)
1469                            (symbol-value value))))
1470                    ((listp value)
1471                     (eval value))))
1472             ;; Translate obsolescent value.
1473             (when (eq element 'signature-file)
1474               (setq element 'signature
1475                     filep t))
1476             ;; Get the contents of file elems.
1477             (when (and filep v)
1478               (setq v (with-temp-buffer
1479                         (insert-file-contents v)
1480                         (buffer-string))))
1481             (setq results (delq (assoc element results) results))
1482             (push (cons element v) results))))
1483       ;; Now we have all the styles, so we insert them.
1484       (setq name (assq 'name results)
1485             address (assq 'address results))
1486       (setq results (delq name (delq address results)))
1487       (make-local-variable 'message-setup-hook)
1488       (dolist (result results)
1489         (add-hook 'message-setup-hook
1490                   (cond
1491                    ((eq 'eval (car result))
1492                     'ignore)
1493                    ((eq 'body (car result))
1494                     `(lambda ()
1495                        (save-excursion
1496                          (message-goto-body)
1497                          (insert ,(cdr result)))))
1498                    ((eq 'signature (car result))
1499                     (set (make-local-variable 'message-signature) nil)
1500                     (set (make-local-variable 'message-signature-file) nil)
1501                     (if (not (cdr result))
1502                         'ignore
1503                       `(lambda ()
1504                          (save-excursion
1505                            (let ((message-signature ,(cdr result)))
1506                              (when message-signature
1507                                (message-insert-signature)))))))
1508                    (t
1509                     (let ((header
1510                            (if (symbolp (car result))
1511                                (capitalize (symbol-name (car result)))
1512                              (car result))))
1513                       `(lambda ()
1514                          (save-excursion
1515                            (message-remove-header ,header)
1516                            (let ((value ,(cdr result)))
1517                              (when value
1518                                (message-goto-eoh)
1519                                (insert ,header ": " value "\n"))))))))))
1520       (when (or name address)
1521         (add-hook 'message-setup-hook
1522                   `(lambda ()
1523                      (set (make-local-variable 'user-mail-address)
1524                           ,(or (cdr address) user-mail-address))
1525                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1526                            (user-mail-address
1527                             ,(or (cdr address) user-mail-address)))
1528                        (save-excursion
1529                          (message-remove-header "From")
1530                          (message-goto-eoh)
1531                          (insert "From: " (message-make-from) "\n")))))))))
1532
1533
1534 ;;; @ for MIME Edit mode
1535 ;;;
1536
1537 (defun gnus-maybe-setup-default-charset ()
1538   (let ((charset
1539          (and (boundp 'gnus-summary-buffer)
1540               (buffer-live-p gnus-summary-buffer)
1541               (save-excursion
1542                 (set-buffer gnus-summary-buffer)
1543                 default-mime-charset))))
1544     (if charset
1545         (progn
1546           (make-local-variable 'default-mime-charset)
1547           (setq default-mime-charset charset)
1548           ))))
1549
1550
1551 ;;; @ for MIME view mode
1552 ;;;
1553
1554 (defun gnus-following-method (buf)
1555   (gnus-setup-message 'reply-yank
1556     (set-buffer buf)
1557     (if (message-news-p)
1558         (message-followup)
1559       (message-reply nil 'wide))
1560     (let ((message-reply-buffer buf))
1561       (message-yank-original))
1562     (message-goto-body))
1563   (kill-buffer buf))
1564
1565
1566 ;;; Allow redefinition of functions.
1567
1568 (gnus-ems-redefine)
1569
1570 (provide 'gnus-msg)
1571
1572 ;;; gnus-msg.el ends here