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