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