Merge remote-tracking branch 'origin/no-gnus'
[gnus] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Gnus
2
3 ;; Copyright (C) 1995-2012  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 3 of the License, or
14 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-ems)
32 (require 'message)
33 (require 'gnus-art)
34 (require 'gnus-util)
35
36 (defcustom gnus-post-method 'current
37   "*Preferred method for posting USENET news.
38
39 If this variable is `current' (which is the default), Gnus will use
40 the \"current\" select method when posting.  If it is `native', Gnus
41 will use the native select method when posting.
42
43 This method will not be used in mail groups and the like, only in
44 \"real\" newsgroups.
45
46 If not `native' nor `current', the value must be a valid method as discussed
47 in the documentation of `gnus-select-method'.  It can also be a list of
48 methods.  If that is the case, the user will be queried for what select
49 method to use when posting."
50   :group 'gnus-group-foreign
51   :link '(custom-manual "(gnus)Posting Server")
52   :type `(choice (const native)
53                  (const current)
54                  (sexp :tag "Methods" ,gnus-select-method)))
55
56 (defcustom gnus-outgoing-message-group nil
57   "All outgoing messages will be put in this group.
58 If you want to store all your outgoing mail and articles in the group
59 \"nnml:archive\", you set this variable to that value.  This variable
60 can also be a list of group names.
61
62 If you want to have greater control over what group to put each
63 message in, you can set this variable to a function that checks the
64 current newsgroup name and then returns a suitable group name (or list
65 of names)."
66   :group 'gnus-message
67   :type '(choice (const nil)
68                  (function)
69                  (string :tag "Group")
70                  (repeat :tag "List of groups" (string :tag "Group"))))
71
72 (make-obsolete-variable 'gnus-outgoing-message-group 'gnus-message-archive-group "24.1")
73
74 (defcustom gnus-mailing-list-groups nil
75   "*If non-nil a regexp matching groups that are really mailing lists.
76 This is useful when you're reading a mailing list that has been
77 gatewayed to a newsgroup, and you want to followup to an article in
78 the group."
79   :group 'gnus-message
80   :type '(choice (regexp)
81                  (const nil)))
82
83 (defcustom gnus-add-to-list nil
84   "*If non-nil, add a `to-list' parameter automatically."
85   :group 'gnus-message
86   :type 'boolean)
87
88 (defcustom gnus-crosspost-complaint
89   "Hi,
90
91 You posted the article below with the following Newsgroups header:
92
93 Newsgroups: %s
94
95 The %s group, at least, was an inappropriate recipient
96 of this message.  Please trim your Newsgroups header to exclude this
97 group before posting in the future.
98
99 Thank you.
100
101 "
102   "Format string to be inserted when complaining about crossposts.
103 The first %s will be replaced by the Newsgroups header;
104 the second with the current group name."
105   :group 'gnus-message
106   :type 'string)
107
108 (defcustom gnus-message-setup-hook nil
109   "Hook run after setting up a message buffer."
110   :group 'gnus-message
111   :options '(message-remove-blank-cited-lines)
112   :type 'hook)
113
114 (defcustom gnus-bug-create-help-buffer t
115   "*Should we create the *Gnus Help Bug* buffer?"
116   :group 'gnus-message
117   :type 'boolean)
118
119 (defcustom gnus-posting-styles nil
120   "*Alist of styles to use when posting.
121 See Info node `(gnus)Posting Styles'."
122   :group 'gnus-message
123   :link '(custom-manual "(gnus)Posting Styles")
124   :type '(repeat (cons (choice (regexp)
125                                (variable)
126                                (list (const header)
127                                      (string :tag "Header")
128                                      (regexp :tag "Regexp"))
129                                (function)
130                                (sexp))
131                        (repeat (list
132                                 (choice (const signature)
133                                         (const signature-file)
134                                         (const organization)
135                                         (const address)
136                                         (const x-face-file)
137                                         (const name)
138                                         (const body)
139                                         (symbol)
140                                         (string :tag "Header"))
141                                 (choice (string)
142                                         (function)
143                                         (variable)
144                                         (sexp)))))))
145
146 (defcustom gnus-gcc-mark-as-read nil
147   "If non-nil, automatically mark Gcc articles as read."
148   :version "22.1"
149   :group 'gnus-message
150   :type 'boolean)
151
152 (make-obsolete-variable 'gnus-inews-mark-gcc-as-read
153                         'gnus-gcc-mark-as-read "Emacs 22.1")
154
155 (defcustom gnus-gcc-externalize-attachments nil
156   "Should local-file attachments be included as external parts in Gcc copies?
157 If it is `all', attach files as external parts;
158 if a regexp and matches the Gcc group name, attach files as external parts;
159 if nil, attach files as normal parts."
160   :version "22.1"
161   :group 'gnus-message
162   :type '(choice (const nil :tag "None")
163                  (const all :tag "Any")
164                  (string :tag "Regexp")))
165
166 (defcustom gnus-gcc-self-resent-messages 'no-gcc-self
167   "Like `gcc-self' group parameter, only for unmodified resent messages.
168 Applied to messages sent by `gnus-summary-resend-message'.  Non-nil
169 value of this variable takes precedence over any existing Gcc header.
170
171 If this is `none', no Gcc copy will be made.  If this is t, messages
172 resent will be Gcc'd to the current group.  If this is a string, it
173 specifies a group to which resent messages will be Gcc'd.  If this is
174 nil, Gcc will be done according to existing Gcc header(s), if any.
175 If this is `no-gcc-self', resent messages will be Gcc'd to groups that
176 existing Gcc header specifies, except for the current group."
177   :version "24.2"
178   :group 'gnus-message
179   :type '(choice (const none) (const t) string (const nil)
180                  (const no-gcc-self)))
181
182 (gnus-define-group-parameter
183  posting-charset-alist
184  :type list
185  :function-document
186  "Return the permitted unencoded charsets for posting of GROUP."
187  :variable gnus-group-posting-charset-alist
188  :variable-default
189   '(("^\\(no\\|fr\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
190     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
191     (message-this-is-mail nil nil)
192     (message-this-is-news nil t))
193  :variable-document
194   "Alist of regexps and permitted unencoded charsets for posting.
195 Each element of the alist has the form (TEST HEADER BODY-LIST), where
196 TEST is either a regular expression matching the newsgroup header or a
197 variable to query,
198 HEADER is the charset which may be left unencoded in the header (nil
199 means encode all charsets),
200 BODY-LIST is a list of charsets which may be encoded using 8bit
201 content-transfer encoding in the body, or one of the special values
202 nil (always encode using quoted-printable) or t (always use 8bit).
203
204 Note that any value other than nil for HEADER infringes some RFCs, so
205 use this option with care."
206  :variable-group gnus-charset
207  :variable-type
208  '(repeat (list :tag "Permitted unencoded charsets"
209                 (choice :tag "Where"
210                         (regexp :tag "Group")
211                         (const :tag "Mail message" :value message-this-is-mail)
212                         (const :tag "News article" :value message-this-is-news))
213                 (choice :tag "Header"
214                         (const :tag "None" nil)
215                         (symbol :tag "Charset"))
216                 (choice :tag "Body"
217                         (const :tag "Any" :value t)
218                         (const :tag "None" :value nil)
219                         (repeat :tag "Charsets"
220                                 (symbol :tag "Charset")))))
221  :parameter-type '(choice :tag "Permitted unencoded charsets"
222                           :value nil
223                           (repeat (symbol)))
224  :parameter-document       "\
225 List of charsets that are permitted to be unencoded.")
226
227 (defcustom gnus-debug-files
228   '("gnus.el" "gnus-sum.el" "gnus-group.el"
229     "gnus-art.el" "gnus-start.el" "gnus-async.el"
230     "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
231     "gnus-agent.el" "gnus-cache.el" "gnus-srvr.el"
232     "mm-util.el" "mm-decode.el" "nnmail.el" "message.el")
233   "Files whose variables will be reported in `gnus-bug'."
234   :version "22.1"
235   :group 'gnus-message
236   :type '(repeat (string :tag "File")))
237
238 (defcustom gnus-debug-exclude-variables
239   '(mm-mime-mule-charset-alist
240     nnmail-split-fancy message-minibuffer-local-map)
241   "Variables that should not be reported in `gnus-bug'."
242   :version "22.1"
243   :group 'gnus-message
244   :type '(repeat (symbol :tag "Variable")))
245
246 (defcustom gnus-discouraged-post-methods
247   '(nndraft nnml nnimap nnmaildir nnmh nnfolder nndir)
248   "A list of back ends that are not used in \"real\" newsgroups.
249 This variable is used only when `gnus-post-method' is `current'."
250   :version "22.1"
251   :group 'gnus-group-foreign
252   :type '(repeat (symbol :tag "Back end")))
253
254 (defcustom gnus-message-replysign
255   nil
256   "Automatically sign replies to signed messages.
257 See also the `mml-default-sign-method' variable."
258   :group 'gnus-message
259   :type 'boolean)
260
261 (defcustom gnus-message-replyencrypt t
262   "Automatically encrypt replies to encrypted messages.
263 See also the `mml-default-encrypt-method' variable."
264   :version "24.1"
265   :group 'gnus-message
266   :type 'boolean)
267
268 (defcustom gnus-message-replysignencrypted
269   t
270   "Setting this causes automatically encrypted messages to also be signed."
271   :group 'gnus-message
272   :type 'boolean)
273
274 (defcustom gnus-confirm-mail-reply-to-news (and gnus-novice-user
275                                                 (not gnus-expert-user))
276   "If non-nil, Gnus requests confirmation when replying to news.
277 This is done because new users often reply by mistake when reading
278 news.
279 This can also be a function receiving the group name as the only
280 parameter, which should return non-nil if a confirmation is needed; or
281 a regexp, in which case a confirmation is asked for if the group name
282 matches the regexp."
283   :version "23.1" ;; No Gnus (default changed)
284   :group 'gnus-message
285   :type '(choice (const :tag "No" nil)
286                  (const :tag "Yes" t)
287                  (regexp :tag "If group matches regexp")
288                  (function :tag "If function evaluates to non-nil")))
289
290 (defcustom gnus-confirm-treat-mail-like-news
291   nil
292   "If non-nil, Gnus will treat mail like news with regard to confirmation
293 when replying by mail.  See the `gnus-confirm-mail-reply-to-news' variable
294 for fine-tuning this.
295 If nil, Gnus will never ask for confirmation if replying to mail."
296   :version "22.1"
297   :group 'gnus-message
298   :type 'boolean)
299
300 (defcustom gnus-summary-resend-default-address t
301   "If non-nil, Gnus tries to suggest a default address to resend to.
302 If nil, the address field will always be empty after invoking
303 `gnus-summary-resend-message'."
304   :version "22.1"
305   :group 'gnus-message
306   :type 'boolean)
307
308 (defcustom gnus-message-highlight-citation
309   t ;; gnus-treat-highlight-citation ;; gnus-cite dependency
310   "Enable highlighting of different citation levels in message-mode."
311   :version "23.1" ;; No Gnus
312   :group 'gnus-cite
313   :group 'gnus-message
314   :type 'boolean)
315
316 (autoload 'gnus-message-citation-mode "gnus-cite" nil t)
317
318 ;;; Internal variables.
319
320 (defvar gnus-inhibit-posting-styles nil
321   "Inhibit the use of posting styles.")
322
323 (defvar gnus-article-yanked-articles nil)
324 (defvar gnus-message-buffer "*Mail Gnus*")
325 (defvar gnus-article-copy nil)
326 (defvar gnus-check-before-posting nil)
327 (defvar gnus-last-posting-server nil)
328 (defvar gnus-message-group-art nil)
329
330 (defvar gnus-msg-force-broken-reply-to nil)
331
332 (defconst gnus-bug-message
333   "Sending a bug report to the Gnus Towers.
334 ========================================
335
336 The buffer below is a mail buffer.  When you press `C-c C-c', it will
337 be sent to the Gnus Bug Exterminators.
338
339 The thing near the bottom of the buffer is how the environment
340 settings will be included in the mail.  Please do not delete that.
341 They will tell the Bug People what your environment is, so that it
342 will be easier to locate the bugs.
343
344 If you have found a bug that makes Emacs go \"beep\", set
345 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
346 and include the backtrace in your bug report.
347
348 Please describe the bug in annoying, painstaking detail.
349
350 Thank you for your help in stamping out bugs.
351 ")
352
353 (autoload 'gnus-uu-post-news "gnus-uu" nil t)
354
355 \f
356 ;;;
357 ;;; Gnus Posting Functions
358 ;;;
359
360 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
361   "p" gnus-summary-post-news
362   "i" gnus-summary-news-other-window
363   "f" gnus-summary-followup
364   "F" gnus-summary-followup-with-original
365   "c" gnus-summary-cancel-article
366   "s" gnus-summary-supersede-article
367   "r" gnus-summary-reply
368   "y" gnus-summary-yank-message
369   "R" gnus-summary-reply-with-original
370   "L" gnus-summary-reply-to-list-with-original
371   "w" gnus-summary-wide-reply
372   "W" gnus-summary-wide-reply-with-original
373   "v" gnus-summary-very-wide-reply
374   "V" gnus-summary-very-wide-reply-with-original
375   "n" gnus-summary-followup-to-mail
376   "N" gnus-summary-followup-to-mail-with-original
377   "m" gnus-summary-mail-other-window
378   "u" gnus-uu-post-news
379   "\M-c" gnus-summary-mail-crosspost-complaint
380   "Br" gnus-summary-reply-broken-reply-to
381   "BR" gnus-summary-reply-broken-reply-to-with-original
382   "om" gnus-summary-mail-forward
383   "op" gnus-summary-post-forward
384   "Om" gnus-uu-digest-mail-forward
385   "Op" gnus-uu-digest-post-forward)
386
387 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
388   "b" gnus-summary-resend-bounced-mail
389   ;; "c" gnus-summary-send-draft
390   "r" gnus-summary-resend-message
391   "e" gnus-summary-resend-message-edit)
392
393 ;;; Internal functions.
394
395 (defun gnus-inews-make-draft (articles)
396   `(lambda ()
397      (gnus-inews-make-draft-meta-information
398       ,(gnus-group-decoded-name gnus-newsgroup-name) ',articles)))
399
400 (defvar gnus-article-reply nil)
401 (defmacro gnus-setup-message (config &rest forms)
402   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
403         (winconf-name (make-symbol "gnus-setup-message-winconf-name"))
404         (buffer (make-symbol "gnus-setup-message-buffer"))
405         (article (make-symbol "gnus-setup-message-article"))
406         (yanked (make-symbol "gnus-setup-yanked-articles"))
407         (group (make-symbol "gnus-setup-message-group")))
408     `(let ((,winconf (current-window-configuration))
409            (,winconf-name gnus-current-window-configuration)
410            (,buffer (buffer-name (current-buffer)))
411            (,article gnus-article-reply)
412            (,yanked gnus-article-yanked-articles)
413            (,group gnus-newsgroup-name)
414            (message-header-setup-hook
415             (copy-sequence message-header-setup-hook))
416            (mbl mml-buffer-list)
417            (message-mode-hook (copy-sequence message-mode-hook)))
418        (setq mml-buffer-list nil)
419        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
420        ;; message-newsreader and message-mailer were formerly set in
421        ;; gnus-inews-add-send-actions, but this is too late when
422        ;; message-generate-headers-first is used. --ansel
423        (add-hook 'message-mode-hook
424                  (lambda nil
425                    (setq message-newsreader
426                          (setq message-mailer (gnus-extended-version)))))
427        ;; #### FIXME: for a reason that I did not manage to identify yet,
428        ;; the variable `gnus-newsgroup-name' does not honor a dynamically
429        ;; scoped or setq'ed value from a caller like `C-u gnus-summary-mail'.
430        ;; After evaluation of @forms below, it gets the value we actually want
431        ;; to override, and the posting styles are used. For that reason, I've
432        ;; added an optional argument to `gnus-configure-posting-styles' to
433        ;; make sure that the correct value for the group name is used. -- drv
434        (add-hook 'message-mode-hook
435                  (if (memq ,config '(reply-yank reply))
436                      (lambda ()
437                        (gnus-configure-posting-styles ,group))
438                    (lambda ()
439                      ;; There may be an old " *gnus article copy*" buffer.
440                      (let (gnus-article-copy)
441                        (gnus-configure-posting-styles ,group)))))
442        (gnus-alist-pull ',(intern gnus-draft-meta-information-header)
443                   message-required-headers)
444        (when (and ,group
445                   (not (string= ,group "")))
446          (push (cons
447                 (intern gnus-draft-meta-information-header)
448                 (gnus-inews-make-draft (or ,yanked ,article)))
449                message-required-headers))
450        (unwind-protect
451            (progn
452              ,@forms)
453          (gnus-inews-add-send-actions ,winconf ,buffer ,article ,config
454                                       ,yanked ,winconf-name)
455          (setq gnus-message-buffer (current-buffer))
456          (set (make-local-variable 'gnus-message-group-art)
457               (cons ,group ,article))
458          (set (make-local-variable 'gnus-newsgroup-name) ,group)
459          ;; Enable highlighting of different citation levels
460          (when gnus-message-highlight-citation
461            (gnus-message-citation-mode 1))
462          (gnus-run-hooks 'gnus-message-setup-hook)
463          (if (eq major-mode 'message-mode)
464              (let ((mbl1 mml-buffer-list))
465                (setq mml-buffer-list mbl)  ;; Global value
466                (set (make-local-variable 'mml-buffer-list) mbl1);; Local value
467                (gnus-make-local-hook 'kill-buffer-hook)
468                (gnus-make-local-hook 'change-major-mode-hook)
469                (add-hook 'change-major-mode-hook 'mml-destroy-buffers nil t)
470                (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))
471            (mml-destroy-buffers)
472            (setq mml-buffer-list mbl)))
473        (message-hide-headers)
474        (gnus-add-buffer)
475        (gnus-configure-windows ,config t)
476        (run-hooks 'post-command-hook)
477        (set-buffer-modified-p nil))))
478
479 (defun gnus-inews-make-draft-meta-information (group articles)
480   (when (numberp articles)
481     (setq articles (list articles)))
482   (concat "(\"" group "\""
483           (if articles
484               (concat " "
485                       (mapconcat
486                        (lambda (elem)
487                          (number-to-string
488                           (if (consp elem)
489                               (car elem)
490                             elem)))
491                        articles " "))
492             "")
493           ")"))
494
495 ;;;###autoload
496 (defun gnus-msg-mail (&optional to subject other-headers continue
497                       switch-action yank-action send-actions return-action)
498   "Start editing a mail message to be sent.
499 Like `message-mail', but with Gnus paraphernalia, particularly the
500 Gcc: header for archiving purposes."
501   (interactive)
502   (let ((buf (current-buffer))
503         mail-buf)
504     (gnus-setup-message 'message
505       (message-mail to subject other-headers continue
506                     nil yank-action send-actions return-action))
507     (when switch-action
508       (setq mail-buf (current-buffer))
509       (switch-to-buffer buf)
510       (apply switch-action mail-buf nil)))
511   ;; COMPOSEFUNC should return t if succeed.  Undocumented ???
512   t)
513
514 ;;;###autoload
515 (defun gnus-button-mailto (address)
516   "Mail to ADDRESS."
517   (set-buffer (gnus-copy-article-buffer))
518   (gnus-setup-message 'message
519     (message-reply address)))
520
521 ;;;###autoload
522 (defun gnus-button-reply (&optional to-address wide)
523   "Like `message-reply'."
524   (interactive)
525   (gnus-setup-message 'message
526     (message-reply to-address wide)))
527
528 ;;;###autoload
529 (define-mail-user-agent 'gnus-user-agent
530   'gnus-msg-mail 'message-send-and-exit
531   'message-kill-buffer 'message-send-hook)
532
533 (defun gnus-setup-posting-charset (group)
534   (let ((alist gnus-group-posting-charset-alist)
535         (group (or group ""))
536         elem)
537     (when group
538       (catch 'found
539         (while (setq elem (pop alist))
540           (when (or (and (stringp (car elem))
541                          (string-match (car elem) group))
542                     (and (functionp (car elem))
543                          (funcall (car elem) group))
544                     (and (symbolp (car elem))
545                          (symbol-value (car elem))))
546             (throw 'found (cons (cadr elem) (caddr elem)))))))))
547
548 (defun gnus-inews-add-send-actions (winconf buffer article
549                                             &optional config yanked
550                                             winconf-name)
551   (gnus-make-local-hook 'message-sent-hook)
552   (add-hook 'message-sent-hook (if gnus-agent 'gnus-agent-possibly-do-gcc
553                                  'gnus-inews-do-gcc) nil t)
554   (when gnus-agent
555     (gnus-make-local-hook 'message-header-hook)
556     (add-hook 'message-header-hook 'gnus-agent-possibly-save-gcc nil t))
557   (setq message-post-method
558         `(lambda (&optional arg)
559            (gnus-post-method arg ,gnus-newsgroup-name)))
560   (message-add-action
561    `(progn
562       (setq gnus-current-window-configuration ',winconf-name)
563       (when (gnus-buffer-exists-p ,buffer)
564         (set-window-configuration ,winconf)))
565    'exit 'postpone 'kill)
566   (let ((to-be-marked (cond
567                        (yanked
568                         (mapcar
569                          (lambda (x) (if (listp x) (car x) x)) yanked))
570                        (article (if (listp article) article (list article)))
571                        (t nil))))
572     (message-add-action
573      `(when (gnus-buffer-exists-p ,buffer)
574         (with-current-buffer ,buffer
575           ,(when to-be-marked
576              (if (eq config 'forward)
577                  `(gnus-summary-mark-article-as-forwarded ',to-be-marked)
578                `(gnus-summary-mark-article-as-replied ',to-be-marked)))))
579      'send)))
580
581 (put 'gnus-setup-message 'lisp-indent-function 1)
582 (put 'gnus-setup-message 'edebug-form-spec '(form body))
583
584 ;;; Post news commands of Gnus group mode and summary mode
585
586 (defun gnus-group-mail (&optional arg)
587   "Start composing a mail.
588 If ARG, use the group under the point to find a posting style.
589 If ARG is 1, prompt for a group name to find the posting style."
590   (interactive "P")
591   ;; We can't `let' gnus-newsgroup-name here, since that leads
592   ;; to local variables leaking.
593   (let ((group gnus-newsgroup-name)
594         ;; make sure last viewed article doesn't affect posting styles:
595         (gnus-article-copy)
596         (buffer (current-buffer)))
597     (unwind-protect
598         (progn
599           (setq gnus-newsgroup-name
600                 (if arg
601                     (if (= 1 (prefix-numeric-value arg))
602                         (gnus-group-completing-read
603                          "Use posting style of group"
604                          nil (gnus-read-active-file-p))
605                       (gnus-group-group-name))
606                   ""))
607           ;; #### see comment in gnus-setup-message -- drv
608           (gnus-setup-message 'message (message-mail)))
609       (with-current-buffer buffer
610         (setq gnus-newsgroup-name group)))))
611
612 (defun gnus-group-news (&optional arg)
613   "Start composing a news.
614 If ARG, post to group under point.
615 If ARG is 1, prompt for group name to post to.
616
617 This function prepares a news even when using mail groups.  This is useful
618 for posting messages to mail groups without actually sending them over the
619 network.  The corresponding back end must have a 'request-post method."
620   (interactive "P")
621   ;; We can't `let' gnus-newsgroup-name here, since that leads
622   ;; to local variables leaking.
623   (let ((group gnus-newsgroup-name)
624         ;; make sure last viewed article doesn't affect posting styles:
625         (gnus-article-copy)
626         (buffer (current-buffer)))
627     (unwind-protect
628         (progn
629           (setq gnus-newsgroup-name
630                 (if arg
631                     (if (= 1 (prefix-numeric-value arg))
632                         (gnus-group-completing-read "Use group"
633                                                     nil
634                                                     (gnus-read-active-file-p))
635                       (gnus-group-group-name))
636                   ""))
637           ;; #### see comment in gnus-setup-message -- drv
638           (gnus-setup-message 'message
639             (message-news (gnus-group-real-name gnus-newsgroup-name))))
640       (with-current-buffer buffer
641         (setq gnus-newsgroup-name group)))))
642
643 (defun gnus-group-post-news (&optional arg)
644   "Start composing a message (a news by default).
645 If ARG, post to group under point.  If ARG is 1, prompt for group name.
646 Depending on the selected group, the message might be either a mail or
647 a news."
648   (interactive "P")
649   ;; Bind this variable here to make message mode hooks work ok.
650   (let ((gnus-newsgroup-name
651          (if arg
652              (if (= 1 (prefix-numeric-value arg))
653                  (gnus-group-completing-read "Newsgroup" nil
654                                              (gnus-read-active-file-p))
655                (gnus-group-group-name))
656            ""))
657         ;; make sure last viewed article doesn't affect posting styles:
658         (gnus-article-copy))
659     (gnus-post-news 'post gnus-newsgroup-name nil nil nil nil
660                     (string= gnus-newsgroup-name ""))))
661
662 (defun gnus-summary-mail-other-window (&optional arg)
663   "Start composing a mail in another window.
664 Use the posting of the current group by default.
665 If ARG, don't do that.  If ARG is 1, prompt for group name to find the
666 posting style."
667   (interactive "P")
668   ;; We can't `let' gnus-newsgroup-name here, since that leads
669   ;; to local variables leaking.
670   (let ((group gnus-newsgroup-name)
671         ;; make sure last viewed article doesn't affect posting styles:
672         (gnus-article-copy)
673         (buffer (current-buffer)))
674     (unwind-protect
675         (progn
676           (setq gnus-newsgroup-name
677                 (if arg
678                     (if (= 1 (prefix-numeric-value arg))
679                         (gnus-group-completing-read "Use group"
680                                                     nil
681                                                     (gnus-read-active-file-p))
682                       "")
683                   gnus-newsgroup-name))
684           ;; #### see comment in gnus-setup-message -- drv
685           (gnus-setup-message 'message (message-mail)))
686       (with-current-buffer buffer
687         (setq gnus-newsgroup-name group)))))
688
689 (defun gnus-summary-news-other-window (&optional arg)
690   "Start composing a news in another window.
691 Post to the current group by default.
692 If ARG, don't do that.  If ARG is 1, prompt for group name to post to.
693
694 This function prepares a news even when using mail groups.  This is useful
695 for posting messages to mail groups without actually sending them over the
696 network.  The corresponding back end must have a 'request-post method."
697   (interactive "P")
698   ;; We can't `let' gnus-newsgroup-name here, since that leads
699   ;; to local variables leaking.
700   (let ((group gnus-newsgroup-name)
701         ;; make sure last viewed article doesn't affect posting styles:
702         (gnus-article-copy)
703         (buffer (current-buffer)))
704     (unwind-protect
705         (progn
706           (setq gnus-newsgroup-name
707                 (if arg
708                     (if (= 1 (prefix-numeric-value arg))
709                         (gnus-group-completing-read "Use group"
710                                                     nil
711                                                     (gnus-read-active-file-p))
712                       "")
713                   gnus-newsgroup-name))
714           ;; #### see comment in gnus-setup-message -- drv
715           (gnus-setup-message 'message
716             (progn
717               (message-news (gnus-group-real-name gnus-newsgroup-name))
718               (set (make-local-variable 'gnus-discouraged-post-methods)
719                    (remove
720                     (car (gnus-find-method-for-group gnus-newsgroup-name))
721                     gnus-discouraged-post-methods)))))
722       (with-current-buffer buffer
723         (setq gnus-newsgroup-name group)))))
724
725 (defun gnus-summary-post-news (&optional arg)
726   "Start composing a message.  Post to the current group by default.
727 If ARG, don't do that.  If ARG is 1, prompt for a group name to post to.
728 Depending on the selected group, the message might be either a mail or
729 a news."
730   (interactive "P")
731   ;; Bind this variable here to make message mode hooks work ok.
732   (let ((gnus-newsgroup-name
733          (if arg
734              (if (= 1 (prefix-numeric-value arg))
735                  (gnus-group-completing-read "Newsgroup" nil
736                                              (gnus-read-active-file-p))
737                "")
738            gnus-newsgroup-name))
739         ;; make sure last viewed article doesn't affect posting styles:
740         (gnus-article-copy))
741     (gnus-post-news 'post gnus-newsgroup-name)))
742
743
744 (defun gnus-summary-followup (yank &optional force-news)
745   "Compose a followup to an article.
746 If prefix argument YANK is non-nil, the original article is yanked
747 automatically.
748 YANK is a list of elements, where the car of each element is the
749 article number, and the cdr is the string to be yanked."
750   (interactive
751    (list (and current-prefix-arg
752               (gnus-summary-work-articles 1))))
753   (when yank
754     (gnus-summary-goto-subject
755      (if (listp (car yank))
756          (caar yank)
757        (car yank))))
758   (save-window-excursion
759     (gnus-summary-select-article))
760   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
761         (gnus-newsgroup-name gnus-newsgroup-name))
762     ;; Send a followup.
763     (gnus-post-news nil gnus-newsgroup-name
764                     headers gnus-article-buffer
765                     yank nil force-news)
766     (gnus-summary-handle-replysign)))
767
768 (defun gnus-summary-followup-with-original (n &optional force-news)
769   "Compose a followup to an article and include the original article.
770 The text in the region will be yanked.  If the region isn't
771 active, the entire article will be yanked."
772   (interactive "P")
773   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
774
775 (defun gnus-summary-followup-to-mail (&optional arg)
776   "Followup to the current mail message via news."
777   (interactive
778    (list (and current-prefix-arg
779               (gnus-summary-work-articles 1))))
780   (gnus-summary-followup arg t))
781
782 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
783   "Followup to the current mail message via news."
784   (interactive "P")
785   (gnus-summary-followup (gnus-summary-work-articles arg) t))
786
787 (defun gnus-inews-yank-articles (articles)
788   (let (beg article yank-string)
789     (message-goto-body)
790     (while (setq article (pop articles))
791       (when (listp article)
792         (setq yank-string (nth 1 article)
793               article (nth 0 article)))
794       (save-window-excursion
795         (set-buffer gnus-summary-buffer)
796         (gnus-summary-select-article nil nil nil article)
797         (gnus-summary-remove-process-mark article))
798       (gnus-copy-article-buffer nil yank-string)
799       (let ((message-reply-buffer gnus-article-copy)
800             (message-reply-headers
801              ;; The headers are decoded.
802              (with-current-buffer gnus-article-copy
803                (save-restriction
804                  (nnheader-narrow-to-headers)
805                  (nnheader-parse-naked-head)))))
806         (message-yank-original)
807         (message-exchange-point-and-mark)
808         (setq beg (or beg (mark t))))
809       (when articles
810         (insert "\n")))
811     (push-mark)
812     (goto-char beg)))
813
814 (defun gnus-summary-cancel-article (&optional n symp)
815   "Cancel an article you posted.
816 Uses the process-prefix convention.  If given the symbolic
817 prefix `a', cancel using the standard posting method; if not
818 post using the current select method."
819   (interactive (gnus-interactive "P\ny"))
820   (let ((message-post-method
821          `(lambda (arg)
822             (gnus-post-method (eq ',symp 'a) ,gnus-newsgroup-name))))
823     (dolist (article (gnus-summary-work-articles n))
824       (when (gnus-summary-select-article t nil nil article)
825         (when (gnus-eval-in-buffer-window gnus-original-article-buffer
826                 (message-cancel-news))
827           (gnus-summary-mark-as-read article gnus-canceled-mark)
828           (gnus-cache-remove-article 1))
829         (gnus-article-hide-headers-if-wanted))
830       (gnus-summary-remove-process-mark article))))
831
832 (defun gnus-summary-supersede-article ()
833   "Compose an article that will supersede a previous article.
834 This is done simply by taking the old article and adding a Supersedes
835 header line with the old Message-ID."
836   (interactive)
837   (let ((article (gnus-summary-article-number))
838         (mail-parse-charset gnus-newsgroup-charset))
839     (gnus-setup-message 'reply-yank
840       (gnus-summary-select-article t)
841       (set-buffer gnus-original-article-buffer)
842       (message-supersede)
843       (push
844        `((lambda ()
845            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
846              (with-current-buffer ,gnus-summary-buffer
847                (gnus-cache-possibly-remove-article ,article nil nil nil t)
848                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
849        message-send-actions)
850       ;; Add Gcc header.
851       (gnus-inews-insert-gcc))))
852
853 \f
854
855 (defun gnus-copy-article-buffer (&optional article-buffer yank-string)
856   ;; make a copy of the article buffer with all text properties removed
857   ;; this copy is in the buffer gnus-article-copy.
858   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
859   ;; this buffer should be passed to all mail/news reply/post routines.
860   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
861   (with-current-buffer gnus-article-copy
862     (mm-enable-multibyte))
863   (let ((article-buffer (or article-buffer gnus-article-buffer))
864         end beg)
865     (if (not (and (get-buffer article-buffer)
866                   (gnus-buffer-exists-p article-buffer)))
867         (error "Can't find any article buffer")
868       (with-current-buffer article-buffer
869         (let ((gnus-newsgroup-charset (or gnus-article-charset
870                                           gnus-newsgroup-charset))
871               (gnus-newsgroup-ignored-charsets
872                (or gnus-article-ignored-charsets
873                    gnus-newsgroup-ignored-charsets)))
874           (save-restriction
875             ;; Copy over the (displayed) article buffer, delete
876             ;; hidden text and remove text properties.
877             (widen)
878             (copy-to-buffer gnus-article-copy (point-min) (point-max))
879             (set-buffer gnus-article-copy)
880             (when yank-string
881               (message-goto-body)
882               (delete-region (point) (point-max))
883               (insert yank-string))
884             (gnus-article-delete-text-of-type 'annotation)
885             (gnus-article-delete-text-of-type 'multipart)
886             (gnus-remove-text-with-property 'gnus-prev)
887             (gnus-remove-text-with-property 'gnus-next)
888             (gnus-remove-text-with-property 'gnus-decoration)
889             (insert
890              (prog1
891                  (buffer-substring-no-properties (point-min) (point-max))
892                (erase-buffer)))
893             ;; Find the original headers.
894             (set-buffer gnus-original-article-buffer)
895             (goto-char (point-min))
896             (while (looking-at message-unix-mail-delimiter)
897               (forward-line 1))
898             (let ((mail-header-separator ""))
899               (setq beg (point)
900                     end (or (message-goto-body)
901                             ;; There may be just a header.
902                             (point-max))))
903             ;; Delete the headers from the displayed articles.
904             (set-buffer gnus-article-copy)
905             (let ((mail-header-separator ""))
906               (delete-region (goto-char (point-min))
907                              (or (message-goto-body) (point-max))))
908             ;; Insert the original article headers.
909             (insert-buffer-substring gnus-original-article-buffer beg end)
910             ;; Decode charsets.
911             (let ((gnus-article-decode-hook
912                    (delq 'article-decode-charset
913                          (copy-sequence gnus-article-decode-hook)))
914                   (rfc2047-quote-decoded-words-containing-tspecials t))
915               (run-hooks 'gnus-article-decode-hook)))))
916       gnus-article-copy)))
917
918 (defun gnus-post-news (post &optional group header article-buffer yank subject
919                             force-news)
920   (when article-buffer
921     (gnus-copy-article-buffer))
922   (let ((gnus-article-reply (and article-buffer (gnus-summary-article-number)))
923         (gnus-article-yanked-articles yank)
924         (add-to-list gnus-add-to-list))
925     (gnus-setup-message (cond (yank 'reply-yank)
926                               (article-buffer 'reply)
927                               (t 'message))
928       (let* ((group (or group gnus-newsgroup-name))
929              (charset (gnus-group-name-charset nil group))
930              (pgroup group)
931              to-address to-group mailing-list to-list
932              newsgroup-p)
933         (when group
934           (setq to-address (gnus-parameter-to-address group)
935                 to-group (gnus-group-find-parameter group 'to-group)
936                 to-list (gnus-parameter-to-list group)
937                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
938                 mailing-list (when gnus-mailing-list-groups
939                                (string-match gnus-mailing-list-groups group))
940                 group (gnus-group-name-decode (gnus-group-real-name group)
941                                               charset)))
942         (if (or (and to-group
943                      (gnus-news-group-p to-group))
944                 newsgroup-p
945                 force-news
946                 (and (gnus-news-group-p
947                       (or pgroup gnus-newsgroup-name)
948                       (or header gnus-current-article))
949                      (not mailing-list)
950                      (not to-list)
951                      (not to-address)))
952             ;; This is news.
953             (if post
954                 (message-news
955                  (or to-group
956                      (and (not (gnus-virtual-group-p pgroup)) group)))
957               (set-buffer gnus-article-copy)
958               (gnus-msg-treat-broken-reply-to)
959               (message-followup (if (or newsgroup-p force-news)
960                                     (if (save-restriction
961                                           (article-narrow-to-head)
962                                           (message-fetch-field "newsgroups"))
963                                         nil
964                                       "")
965                                   to-group)))
966           ;; The is mail.
967           (if post
968               (progn
969                 (message-mail (or to-address to-list))
970                 ;; Arrange for mail groups that have no `to-address' to
971                 ;; get that when the user sends off the mail.
972                 (when (and (not to-list)
973                            (not to-address)
974                            add-to-list)
975                   (push (list 'gnus-inews-add-to-address pgroup)
976                         message-send-actions)))
977             (set-buffer gnus-article-copy)
978             (gnus-msg-treat-broken-reply-to)
979             (message-wide-reply to-address)))
980         (when yank
981           (gnus-inews-yank-articles yank))))))
982
983 (defun gnus-msg-treat-broken-reply-to (&optional force)
984   "Remove the Reply-to header if broken-reply-to."
985   (when (or force
986             (gnus-group-find-parameter
987              gnus-newsgroup-name 'broken-reply-to))
988     (save-restriction
989       (message-narrow-to-head)
990       (message-remove-header "reply-to"))))
991
992 (defun gnus-post-method (arg group &optional silent)
993   "Return the posting method based on GROUP and ARG.
994 If SILENT, don't prompt the user."
995   (let ((gnus-post-method (or (gnus-parameter-post-method group)
996                               gnus-post-method))
997         (group-method (gnus-find-method-for-group group)))
998     (cond
999      ;; If the group-method is nil (which shouldn't happen) we use
1000      ;; the default method.
1001      ((null group-method)
1002       (or (and (listp gnus-post-method) ;If not current/native/nil
1003                (not (listp (car gnus-post-method))) ; and not a list of methods
1004                gnus-post-method)        ;then use it.
1005           gnus-select-method
1006           message-post-method))
1007      ;; We want the inverse of the default
1008      ((and arg (not (eq arg 0)))
1009       (if (eq gnus-post-method 'current)
1010           gnus-select-method
1011         group-method))
1012      ;; We query the user for a post method.
1013      ((or arg
1014           (and (listp gnus-post-method)
1015                (listp (car gnus-post-method))))
1016       (let* ((methods
1017               ;; Collect all methods we know about.
1018               (append
1019                (when (listp gnus-post-method)
1020                  (if (listp (car gnus-post-method))
1021                      gnus-post-method
1022                    (list gnus-post-method)))
1023                gnus-secondary-select-methods
1024                (mapcar 'cdr gnus-server-alist)
1025                (mapcar 'car gnus-opened-servers)
1026                (list gnus-select-method)
1027                (list group-method)))
1028              method-alist post-methods method)
1029         ;; Weed out all mail methods.
1030         (while methods
1031           (setq method (gnus-server-get-method "" (pop methods)))
1032           (when (and (or (gnus-method-option-p method 'post)
1033                          (gnus-method-option-p method 'post-mail))
1034                      (not (member method post-methods)))
1035             (push method post-methods)))
1036         ;; Create a name-method alist.
1037         (setq method-alist
1038               (mapcar
1039                (lambda (m)
1040                  (if (equal (cadr m) "")
1041                      (list (symbol-name (car m)) m)
1042                    (list (concat (cadr m) " (" (symbol-name (car m)) ")") m)))
1043                post-methods))
1044         ;; Query the user.
1045         (cadr
1046          (assoc
1047           (setq gnus-last-posting-server
1048                 (if (and silent
1049                          gnus-last-posting-server)
1050                     ;; Just use the last value.
1051                     gnus-last-posting-server
1052                   (gnus-completing-read
1053                    "Posting method" (mapcar 'car method-alist) t
1054                    (cons (or gnus-last-posting-server "") 0))))
1055           method-alist))))
1056      ;; Override normal method.
1057      ((and (eq gnus-post-method 'current)
1058            (not (memq (car group-method) gnus-discouraged-post-methods))
1059            (gnus-get-function group-method 'request-post t))
1060       (assert (not arg))
1061       group-method)
1062      ;; Use gnus-post-method.
1063      ((listp gnus-post-method)          ;A method...
1064       (assert (not (listp (car gnus-post-method)))) ;... not a list of methods.
1065       gnus-post-method)
1066      ;; Use the normal select method (nil or native).
1067      (t gnus-select-method))))
1068
1069 \f
1070
1071 (defun gnus-extended-version ()
1072   "Stringified Gnus version and Emacs version.
1073 See the variable `gnus-user-agent'."
1074   (interactive)
1075   (if (stringp gnus-user-agent)
1076       gnus-user-agent
1077     ;; `gnus-user-agent' is a list:
1078     (let* ((float-output-format nil)
1079            (gnus-v
1080             (when (memq 'gnus gnus-user-agent)
1081               (concat "Gnus/"
1082                       (prin1-to-string (gnus-continuum-version gnus-version) t)
1083                       " (" gnus-version ")")))
1084            (emacs-v (gnus-emacs-version)))
1085       (concat gnus-v (when (and gnus-v emacs-v) " ")
1086               emacs-v))))
1087
1088 \f
1089 ;;;
1090 ;;; Gnus Mail Functions
1091 ;;;
1092
1093 ;;; Mail reply commands of Gnus summary mode
1094
1095 (defun gnus-summary-reply (&optional yank wide very-wide)
1096   "Start composing a mail reply to the current message.
1097 If prefix argument YANK is non-nil, the original article is yanked
1098 automatically.
1099 If WIDE, make a wide reply.
1100 If VERY-WIDE, make a very wide reply."
1101   (interactive
1102    (list (and current-prefix-arg
1103               (gnus-summary-work-articles 1))))
1104   ;; Allow user to require confirmation before replying by mail to the
1105   ;; author of a news article (or mail message).
1106   (when (or (not (or (gnus-news-group-p gnus-newsgroup-name)
1107                      gnus-confirm-treat-mail-like-news))
1108             (not (cond ((stringp gnus-confirm-mail-reply-to-news)
1109                         (string-match gnus-confirm-mail-reply-to-news
1110                                       gnus-newsgroup-name))
1111                        ((functionp gnus-confirm-mail-reply-to-news)
1112                         (funcall gnus-confirm-mail-reply-to-news
1113                                  gnus-newsgroup-name))
1114                        (t gnus-confirm-mail-reply-to-news)))
1115             (if (or wide very-wide)
1116                 t ;; Ignore gnus-confirm-mail-reply-to-news for wide and very
1117                   ;; wide replies.
1118               (y-or-n-p "Really reply by mail to article author? ")))
1119     (let* ((article
1120             (if (listp (car yank))
1121                 (caar yank)
1122               (car yank)))
1123            (gnus-article-reply (or article (gnus-summary-article-number)))
1124            (gnus-article-yanked-articles yank)
1125            (headers ""))
1126       ;; Stripping headers should be specified with mail-yank-ignored-headers.
1127       (when yank
1128         (gnus-summary-goto-subject article))
1129       (gnus-setup-message (if yank 'reply-yank 'reply)
1130         (if (not very-wide)
1131             (gnus-summary-select-article)
1132           (dolist (article very-wide)
1133             (gnus-summary-select-article nil nil nil article)
1134             (with-current-buffer (gnus-copy-article-buffer)
1135               (gnus-msg-treat-broken-reply-to)
1136               (save-restriction
1137                 (message-narrow-to-head)
1138                 (setq headers (concat headers (buffer-string)))))))
1139         (set-buffer (gnus-copy-article-buffer))
1140         (gnus-msg-treat-broken-reply-to gnus-msg-force-broken-reply-to)
1141         (save-restriction
1142           (message-narrow-to-head)
1143           (when very-wide
1144             (erase-buffer)
1145             (insert headers))
1146           (goto-char (point-max)))
1147         (mml-quote-region (point) (point-max))
1148         (message-reply nil wide)
1149         (when yank
1150           (gnus-inews-yank-articles yank))
1151         (gnus-summary-handle-replysign)))))
1152
1153 (defun gnus-summary-handle-replysign ()
1154   "Check the various replysign variables and take action accordingly."
1155   (when (or gnus-message-replysign gnus-message-replyencrypt)
1156     (let (signed encrypted)
1157       (with-current-buffer gnus-article-buffer
1158         (setq signed (memq 'signed gnus-article-wash-types))
1159         (setq encrypted (memq 'encrypted gnus-article-wash-types)))
1160       (cond ((and gnus-message-replyencrypt encrypted)
1161              (mml-secure-message mml-default-encrypt-method
1162                                  (if gnus-message-replysignencrypted
1163                                      'signencrypt
1164                                    'encrypt)))
1165             ((and gnus-message-replysign signed)
1166              (mml-secure-message mml-default-sign-method 'sign))))))
1167
1168 (defun gnus-summary-reply-with-original (n &optional wide)
1169   "Start composing a reply mail to the current message.
1170 The original article will be yanked."
1171   (interactive "P")
1172   (gnus-summary-reply (gnus-summary-work-articles n) wide))
1173
1174 (defun gnus-summary-reply-to-list-with-original (n &optional wide)
1175   "Start composing a reply mail to the current message.
1176 The reply goes only to the mailing list.
1177 The original article will be yanked."
1178   (interactive "P")
1179   (let ((message-reply-to-function
1180          (lambda nil
1181            `((To . ,(gnus-mailing-list-followup-to))))))
1182     (gnus-summary-reply (gnus-summary-work-articles n) wide)))
1183
1184 (defun gnus-summary-reply-broken-reply-to (&optional yank wide very-wide)
1185   "Like `gnus-summary-reply' except removing reply-to field.
1186 If prefix argument YANK is non-nil, the original article is yanked
1187 automatically.
1188 If WIDE, make a wide reply.
1189 If VERY-WIDE, make a very wide reply."
1190   (interactive
1191    (list (and current-prefix-arg
1192               (gnus-summary-work-articles 1))))
1193   (let ((gnus-msg-force-broken-reply-to t))
1194     (gnus-summary-reply yank wide very-wide)))
1195
1196 (defun gnus-summary-reply-broken-reply-to-with-original (n &optional wide)
1197   "Like `gnus-summary-reply-with-original' except removing reply-to field.
1198 The original article will be yanked."
1199   (interactive "P")
1200   (gnus-summary-reply-broken-reply-to (gnus-summary-work-articles n) wide))
1201
1202 (defun gnus-summary-wide-reply (&optional yank)
1203   "Start composing a wide reply mail to the current message.
1204 If prefix argument YANK is non-nil, the original article is yanked
1205 automatically."
1206   (interactive
1207    (list (and current-prefix-arg
1208               (gnus-summary-work-articles 1))))
1209   (gnus-summary-reply yank t))
1210
1211 (defun gnus-summary-wide-reply-with-original (n)
1212   "Start composing a wide reply mail to the current message.
1213 The original article will be yanked.
1214 Uses the process/prefix convention."
1215   (interactive "P")
1216   (gnus-summary-reply-with-original n t))
1217
1218 (defun gnus-summary-very-wide-reply (&optional yank)
1219   "Start composing a very wide reply mail to the current message.
1220 If prefix argument YANK is non-nil, the original article is yanked
1221 automatically."
1222   (interactive
1223    (list (and current-prefix-arg
1224               (gnus-summary-work-articles 1))))
1225   (gnus-summary-reply yank t (gnus-summary-work-articles yank)))
1226
1227 (defun gnus-summary-very-wide-reply-with-original (n)
1228   "Start composing a very wide reply mail to the current message.
1229 The original article will be yanked."
1230   (interactive "P")
1231   (gnus-summary-reply
1232    (gnus-summary-work-articles n) t (gnus-summary-work-articles n)))
1233
1234 (defun gnus-summary-mail-forward (&optional arg post)
1235   "Forward the current message(s) to another user.
1236 If process marks exist, forward all marked messages;
1237 if ARG is nil, see `message-forward-as-mime' and `message-forward-show-mml';
1238 if ARG is 1, decode the message and forward directly inline;
1239 if ARG is 2, forward message as an rfc822 MIME section;
1240 if ARG is 3, decode message and forward as an rfc822 MIME section;
1241 if ARG is 4, forward message directly inline;
1242 otherwise, use flipped `message-forward-as-mime'.
1243 If POST, post instead of mail.
1244 For the \"inline\" alternatives, also see the variable
1245 `message-forward-ignored-headers'."
1246   (interactive "P")
1247   (if (cdr (gnus-summary-work-articles nil))
1248       ;; Process marks are given.
1249       (gnus-uu-digest-mail-forward nil post)
1250     ;; No process marks.
1251     (let ((message-forward-as-mime message-forward-as-mime)
1252           (message-forward-show-mml message-forward-show-mml))
1253       (cond
1254        ((null arg))
1255        ((eq arg 1)
1256         (setq message-forward-as-mime nil
1257               message-forward-show-mml t))
1258        ((eq arg 2)
1259         (setq message-forward-as-mime t
1260               message-forward-show-mml nil))
1261        ((eq arg 3)
1262         (setq message-forward-as-mime t
1263               message-forward-show-mml t))
1264        ((eq arg 4)
1265         (setq message-forward-as-mime nil
1266               message-forward-show-mml nil))
1267        (t
1268         (setq message-forward-as-mime (not message-forward-as-mime))))
1269       (let* ((gnus-article-reply (gnus-summary-article-number))
1270              (gnus-article-yanked-articles (list gnus-article-reply)))
1271         (gnus-setup-message 'forward
1272           (gnus-summary-select-article)
1273           (let ((mail-parse-charset
1274                  (or (and (gnus-buffer-live-p gnus-article-buffer)
1275                           (with-current-buffer gnus-article-buffer
1276                             gnus-article-charset))
1277                      gnus-newsgroup-charset))
1278                 (mail-parse-ignored-charsets
1279                  gnus-newsgroup-ignored-charsets))
1280             (set-buffer gnus-original-article-buffer)
1281             (message-forward post)))))))
1282
1283 (defun gnus-summary-resend-message-insert-gcc ()
1284   "Insert Gcc header according to `gnus-gcc-self-resent-messages'."
1285   (gnus-inews-insert-gcc)
1286   (let ((gcc (mapcar
1287               (lambda (group)
1288                 (mm-encode-coding-string
1289                  group
1290                  (gnus-group-name-charset (gnus-inews-group-method group)
1291                                           group)))
1292               (message-unquote-tokens
1293                (message-tokenize-header (mail-fetch-field "gcc" nil t)
1294                                         " ,")))))
1295     (message-remove-header "gcc")
1296     (when gcc
1297       (goto-char (point-max))
1298       (cond ((eq gnus-gcc-self-resent-messages  'none))
1299             ((eq gnus-gcc-self-resent-messages t)
1300              (insert "Gcc: \"" gnus-newsgroup-name "\"\n"))
1301             ((stringp gnus-gcc-self-resent-messages)
1302              (insert "Gcc: "
1303                      (mm-encode-coding-string
1304                       (if (string-match " " gnus-gcc-self-resent-messages)
1305                           (concat "\"" gnus-gcc-self-resent-messages "\"")
1306                         gnus-gcc-self-resent-messages)
1307                       (gnus-group-name-charset
1308                        (gnus-inews-group-method gnus-gcc-self-resent-messages)
1309                        gnus-gcc-self-resent-messages))
1310                      "\n"))
1311             ((null gnus-gcc-self-resent-messages)
1312              (insert "Gcc: " (mapconcat 'identity gcc ", ") "\n"))
1313             ((eq gnus-gcc-self-resent-messages 'no-gcc-self)
1314              (when (setq gcc (delete
1315                               gnus-newsgroup-name
1316                               (delete (concat "\"" gnus-newsgroup-name "\"")
1317                                       gcc)))
1318                (insert "Gcc: " (mapconcat 'identity gcc ", ") "\n")))))))
1319
1320 (defun gnus-summary-resend-message (address n)
1321   "Resend the current article to ADDRESS."
1322   (interactive
1323    (list (message-read-from-minibuffer
1324           "Resend message(s) to: "
1325           (when (and gnus-summary-resend-default-address
1326                      (gnus-buffer-live-p gnus-original-article-buffer))
1327             ;; If some other article is currently selected, the
1328             ;; initial-contents is wrong. Whatever, it is just the
1329             ;; initial-contents.
1330             (with-current-buffer gnus-original-article-buffer
1331               (nnmail-fetch-field "to"))))
1332          current-prefix-arg))
1333   (let ((message-header-setup-hook (copy-sequence message-header-setup-hook))
1334         (message-sent-hook (copy-sequence message-sent-hook)))
1335     (add-hook 'message-header-setup-hook
1336               'gnus-summary-resend-message-insert-gcc)
1337     (add-hook 'message-sent-hook (if gnus-agent
1338                                      'gnus-agent-possibly-do-gcc
1339                                    'gnus-inews-do-gcc))
1340     (dolist (article (gnus-summary-work-articles n))
1341       (gnus-summary-select-article nil nil nil article)
1342       (with-current-buffer gnus-original-article-buffer
1343         (let ((gnus-gcc-externalize-attachments nil)
1344               (message-inhibit-body-encoding t))
1345           (message-resend address)))
1346       (gnus-summary-mark-article-as-forwarded article))))
1347
1348 ;; From: Matthieu Moy <Matthieu.Moy@imag.fr>
1349 (defun gnus-summary-resend-message-edit ()
1350   "Resend an article that has already been sent.
1351 A new buffer will be created to allow the user to modify body and
1352 contents of the message, and then, everything will happen as when
1353 composing a new message."
1354   (interactive)
1355   (let ((mail-parse-charset gnus-newsgroup-charset))
1356     (gnus-setup-message 'reply-yank
1357       (gnus-summary-select-article t)
1358       (set-buffer gnus-original-article-buffer)
1359       (let ((cur (current-buffer))
1360             (to (message-fetch-field "to")))
1361         ;; Get a normal message buffer.
1362         (message-pop-to-buffer (message-buffer-name "Resend" to))
1363         (insert-buffer-substring cur)
1364         (mime-to-mml)
1365         (message-narrow-to-head-1)
1366         ;; Gnus will generate a new one when sending.
1367         (message-remove-header "Message-ID")
1368         ;; Remove unwanted headers.
1369         (message-remove-header message-ignored-resent-headers t)
1370         (goto-char (point-max))
1371         (insert mail-header-separator)
1372         ;; Add Gcc header.
1373         (gnus-inews-insert-gcc)
1374         (goto-char (point-min))
1375         (when (re-search-forward "^To:\\|^Newsgroups:" nil 'move)
1376           (forward-char 1))
1377         (widen)))))
1378
1379 (defun gnus-summary-post-forward (&optional arg)
1380   "Forward the current article to a newsgroup.
1381 See `gnus-summary-mail-forward' for ARG."
1382   (interactive "P")
1383   (gnus-summary-mail-forward arg t))
1384
1385 (defun gnus-summary-mail-crosspost-complaint (n)
1386   "Send a complaint about crossposting to the current article(s)."
1387   (interactive "P")
1388   (dolist (article (gnus-summary-work-articles n))
1389     (set-buffer gnus-summary-buffer)
1390     (gnus-summary-goto-subject article)
1391     (let ((group (gnus-group-real-name gnus-newsgroup-name))
1392           newsgroups followup-to)
1393       (gnus-summary-select-article)
1394       (set-buffer gnus-original-article-buffer)
1395       (if (and (<= (length (message-tokenize-header
1396                             (setq newsgroups
1397                                   (mail-fetch-field "newsgroups"))
1398                             ", "))
1399                    1)
1400                (or (not (setq followup-to (mail-fetch-field "followup-to")))
1401                    (not (member group (message-tokenize-header
1402                                        followup-to ", ")))))
1403           (if followup-to
1404               (gnus-message 1 "Followup-to restricted")
1405             (gnus-message 1 "Not a crossposted article"))
1406         (set-buffer gnus-summary-buffer)
1407         (gnus-summary-reply-with-original 1)
1408         (set-buffer gnus-message-buffer)
1409         (message-goto-body)
1410         (insert (format gnus-crosspost-complaint newsgroups group))
1411         (message-goto-subject)
1412         (re-search-forward " *$")
1413         (replace-match " (crosspost notification)" t t)
1414         (gnus-deactivate-mark)
1415         (when (gnus-y-or-n-p "Send this complaint? ")
1416           (message-send-and-exit))))))
1417
1418 (defun gnus-mail-parse-comma-list ()
1419   (let (accumulated
1420         beg)
1421     (skip-chars-forward " ")
1422     (while (not (eobp))
1423       (setq beg (point))
1424       (skip-chars-forward "^,")
1425       (while (zerop
1426               (save-excursion
1427                 (save-restriction
1428                   (let ((i 0))
1429                     (narrow-to-region beg (point))
1430                     (goto-char beg)
1431                     (logand (progn
1432                               (while (search-forward "\"" nil t)
1433                                 (incf i))
1434                               (if (zerop i) 2 i))
1435                             2)))))
1436         (skip-chars-forward ",")
1437         (skip-chars-forward "^,"))
1438       (skip-chars-backward " ")
1439       (push (buffer-substring beg (point))
1440             accumulated)
1441       (skip-chars-forward "^,")
1442       (skip-chars-forward ", "))
1443     accumulated))
1444
1445 (defun gnus-inews-add-to-address (group)
1446   (let ((to-address (mail-fetch-field "to")))
1447     (when (and to-address
1448                (gnus-alive-p))
1449       ;; This mail group doesn't have a `to-list', so we add one
1450       ;; here.  Magic!
1451       (when (gnus-y-or-n-p
1452              (format "Do you want to add this as `to-list': %s? " to-address))
1453         (gnus-group-add-parameter group (cons 'to-list to-address))))))
1454
1455 (defun gnus-put-message ()
1456   "Put the current message in some group and return to Gnus."
1457   (interactive)
1458   (let ((reply gnus-article-reply)
1459         (winconf gnus-prev-winconf)
1460         (group gnus-newsgroup-name))
1461     (unless (and group
1462                  (not (gnus-group-read-only-p group)))
1463       (setq group (read-string "Put in group: " nil (gnus-writable-groups))))
1464
1465     (when (gnus-group-entry group)
1466       (error "No such group: %s" group))
1467     (save-excursion
1468       (save-restriction
1469         (widen)
1470         (message-narrow-to-headers)
1471         (let ((gnus-deletable-headers nil))
1472           (message-generate-headers
1473            (if (message-news-p)
1474                message-required-news-headers
1475              message-required-mail-headers)))
1476         (goto-char (point-max))
1477         (if (string-match " " group)
1478             (insert "Gcc: \"" group "\"\n")
1479           (insert "Gcc: " group "\n"))
1480         (widen)))
1481     (gnus-inews-do-gcc)
1482     (when (and (get-buffer gnus-group-buffer)
1483                (gnus-buffer-exists-p (car-safe reply))
1484                (cdr reply))
1485       (set-buffer (car reply))
1486       (gnus-summary-mark-article-as-replied (cdr reply)))
1487     (when winconf
1488       (set-window-configuration winconf))))
1489
1490 (defun gnus-article-mail (yank)
1491   "Send a reply to the address near point.
1492 If YANK is non-nil, include the original article."
1493   (interactive "P")
1494   (let ((address
1495          (buffer-substring
1496           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
1497           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
1498     (when address
1499       (gnus-msg-mail address)
1500       (when yank
1501         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
1502
1503 (defvar nntp-server-type)
1504 (defun gnus-bug ()
1505   "Send a bug report to the Gnus maintainers."
1506   (interactive)
1507   (unless (gnus-alive-p)
1508     (error "Gnus has been shut down"))
1509   (gnus-setup-message (if (message-mail-user-agent) 'message 'bug)
1510     (unless (message-mail-user-agent)
1511       (delete-other-windows)
1512       (when gnus-bug-create-help-buffer
1513         (switch-to-buffer "*Gnus Help Bug*")
1514         (erase-buffer)
1515         (insert gnus-bug-message)
1516         (goto-char (point-min)))
1517       (message-pop-to-buffer "*Gnus Bug*"))
1518     (let ((message-this-is-mail t))
1519       (message-setup `((To . ,gnus-maintainer)
1520                        (Subject . "")
1521                        (X-Debbugs-Package
1522                         . ,(format "%s" gnus-bug-package))
1523                        (X-Debbugs-Version
1524                         . ,(format "%s" (gnus-continuum-version))))))
1525     (when gnus-bug-create-help-buffer
1526       (push `(gnus-bug-kill-buffer) message-send-actions))
1527     (goto-char (point-min))
1528     (message-goto-body)
1529     (insert "\n\n\n\n\n")
1530     (insert (gnus-version) "\n"
1531             (emacs-version) "\n")
1532     (when (and (boundp 'nntp-server-type)
1533                (stringp nntp-server-type))
1534       (insert nntp-server-type))
1535     (goto-char (point-min))
1536     (search-forward "Subject: " nil t)
1537     (message "")))
1538
1539 (defun gnus-bug-kill-buffer ()
1540   (when (get-buffer "*Gnus Help Bug*")
1541     (kill-buffer "*Gnus Help Bug*")))
1542
1543 (defun gnus-summary-yank-message (buffer n)
1544   "Yank the current article into a composed message."
1545   (interactive
1546    (list (gnus-completing-read "Buffer" (message-buffers) t)
1547          current-prefix-arg))
1548   (gnus-summary-iterate n
1549     (let ((gnus-inhibit-treatment t))
1550       (gnus-summary-select-article))
1551     (with-current-buffer buffer
1552       (message-yank-buffer gnus-article-buffer))))
1553
1554 ;;; Treatment of rejected articles.
1555 ;;; Bounced mail.
1556
1557 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1558   "Re-mail the current message.
1559 This only makes sense if the current message is a bounce message that
1560 contains some mail you have written which has been bounced back to
1561 you.
1562 If FETCH, try to fetch the article that this is a reply to, if indeed
1563 this is a reply."
1564   (interactive "P")
1565   (gnus-summary-select-article t)
1566   (let (summary-buffer parent)
1567     (if fetch
1568         (progn
1569           (setq summary-buffer (current-buffer))
1570           (set-buffer gnus-original-article-buffer)
1571           (article-goto-body)
1572           (when (re-search-forward "^References:\n?" nil t)
1573             (while (memq (char-after) '(?\t ? ))
1574               (forward-line 1))
1575             (skip-chars-backward "\t\n ")
1576             (setq parent
1577                   (gnus-parent-id (buffer-substring (match-end 0) (point))))))
1578       (set-buffer gnus-original-article-buffer))
1579     (gnus-setup-message 'compose-bounce
1580       (message-bounce)
1581       ;; Add Gcc header.
1582       (gnus-inews-insert-gcc)
1583       ;; If there are references, we fetch the article we answered to.
1584       (when parent
1585         (with-current-buffer summary-buffer
1586           (gnus-summary-refer-article parent)
1587           (gnus-summary-show-all-headers))))))
1588
1589 ;;; Gcc handling.
1590
1591 (defun gnus-inews-group-method (group)
1592   (cond
1593    ;; If the group doesn't exist, we assume
1594    ;; it's an archive group...
1595    ((and (null (gnus-get-info group))
1596          (eq (car (gnus-server-to-method gnus-message-archive-method))
1597              (car (gnus-server-to-method (gnus-group-method group)))))
1598     gnus-message-archive-method)
1599    ;; Use the method.
1600    ((gnus-info-method (gnus-get-info group))
1601     (gnus-info-method (gnus-get-info group)))
1602    ;; Find the method.
1603    (t (gnus-server-to-method (gnus-group-method group)))))
1604
1605 ;; Do Gcc handling, which copied the message over to some group.
1606 (defun gnus-inews-do-gcc (&optional gcc)
1607   (interactive)
1608   (save-excursion
1609     (save-restriction
1610       (message-narrow-to-headers)
1611       (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1612             (cur (current-buffer))
1613             groups group method group-art options
1614             mml-externalize-attachments)
1615         (when gcc
1616           (message-remove-header "gcc")
1617           (widen)
1618           (setq groups (message-unquote-tokens
1619                         (message-tokenize-header gcc " ,")))
1620           ;; Copy the article over to some group(s).
1621           (while (setq group (pop groups))
1622             (setq method (gnus-inews-group-method group)
1623                   group (mm-encode-coding-string
1624                          group
1625                          (gnus-group-name-charset method group)))
1626             (unless (gnus-check-server method)
1627               (error "Can't open server %s" (if (stringp method) method
1628                                               (car method))))
1629             (unless (gnus-request-group group t method)
1630               (gnus-request-create-group group method))
1631             (setq mml-externalize-attachments
1632                   (if (stringp gnus-gcc-externalize-attachments)
1633                       (string-match gnus-gcc-externalize-attachments group)
1634                     gnus-gcc-externalize-attachments))
1635             (save-excursion
1636               (nnheader-set-temp-buffer " *acc*")
1637               (setq message-options (with-current-buffer cur message-options))
1638               (insert-buffer-substring cur)
1639               (message-encode-message-body)
1640               (save-restriction
1641                 (message-narrow-to-headers)
1642                 (let* ((mail-parse-charset message-default-charset)
1643                        (newsgroups-field (save-restriction
1644                                            (message-narrow-to-headers-or-head)
1645                                            (message-fetch-field "Newsgroups")))
1646                        (followup-field (save-restriction
1647                                          (message-narrow-to-headers-or-head)
1648                                          (message-fetch-field "Followup-To")))
1649                        ;; BUG: We really need to get the charset for
1650                        ;; each name in the Newsgroups and Followup-To
1651                        ;; lines to allow crossposting between group
1652                        ;; names with incompatible character sets.
1653                        ;; -- Per Abrahamsen <abraham@dina.kvl.dk> 2001-10-08.
1654                        (group-field-charset
1655                         (gnus-group-name-charset
1656                          method (or newsgroups-field "")))
1657                        (followup-field-charset
1658                         (gnus-group-name-charset
1659                          method (or followup-field "")))
1660                        (rfc2047-header-encoding-alist
1661                         (append
1662                          (when group-field-charset
1663                            (list (cons "Newsgroups" group-field-charset)))
1664                          (when followup-field-charset
1665                            (list (cons "Followup-To" followup-field-charset)))
1666                          rfc2047-header-encoding-alist)))
1667                   (mail-encode-encoded-word-buffer)))
1668               (goto-char (point-min))
1669               (when (re-search-forward
1670                      (concat "^" (regexp-quote mail-header-separator) "$")
1671                      nil t)
1672                 (replace-match "" t t ))
1673               (when (or (not (gnus-check-backend-function
1674                               'request-accept-article group))
1675                         (not (setq group-art
1676                                    (gnus-request-accept-article
1677                                     group method t t))))
1678                 (gnus-message 1 "Couldn't store article in group %s: %s"
1679                               group (gnus-status-message method)))
1680               (when (stringp method)
1681                 (setq method (gnus-server-to-method method)))
1682               (when (and (listp method)
1683                          (gnus-native-method-p method))
1684                 (setq group (gnus-group-short-name group)))
1685               (when (and group-art
1686                          ;; FIXME: Should gcc-mark-as-read work when
1687                          ;; Gnus is not running?
1688                          (gnus-alive-p))
1689                 (if (or gnus-gcc-mark-as-read
1690                         (and (boundp 'gnus-inews-mark-gcc-as-read)
1691                              (symbol-value 'gnus-inews-mark-gcc-as-read)))
1692                     (gnus-group-mark-article-read group (cdr group-art))
1693                   (with-current-buffer gnus-group-buffer
1694                     (let ((gnus-group-marked (list group))
1695                           (gnus-get-new-news-hook nil)
1696                           (inhibit-read-only t))
1697                       (gnus-group-get-new-news-this-group nil t)))))
1698               (setq options message-options)
1699               (with-current-buffer cur (setq message-options options))
1700               (kill-buffer (current-buffer)))))))))
1701
1702 (defun gnus-inews-insert-gcc (&optional group)
1703   "Insert the Gcc to say where the article is to be archived."
1704   (let* ((group (or group gnus-newsgroup-name))
1705          (group (when group (gnus-group-decoded-name group)))
1706          (var (or gnus-outgoing-message-group gnus-message-archive-group))
1707          (gcc-self-val
1708           (and group (gnus-group-find-parameter group 'gcc-self)))
1709          result
1710          (groups
1711           (cond
1712            ((null gnus-message-archive-method)
1713             ;; Ignore.
1714             nil)
1715            ((stringp var)
1716             ;; Just a single group.
1717             (list var))
1718            ((null var)
1719             ;; We don't want this.
1720             nil)
1721            ((and (listp var) (stringp (car var)))
1722             ;; A list of groups.
1723             var)
1724            ((functionp var)
1725             ;; A function.
1726             (funcall var group))
1727            (t
1728             ;; An alist of regexps/functions/forms.
1729             (while (and var
1730                         (not
1731                          (setq result
1732                                (cond
1733                                 ((stringp (caar var))
1734                                  ;; Regexp.
1735                                  (when (string-match (caar var) group)
1736                                    (cdar var)))
1737                                 ((functionp (car var))
1738                                  ;; Function.
1739                                  (funcall (car var) group))
1740                                 (t
1741                                  (eval (car var)))))))
1742               (setq var (cdr var)))
1743             result)))
1744          name)
1745     (when (or groups gcc-self-val)
1746       (when (stringp groups)
1747         (setq groups (list groups)))
1748       (save-excursion
1749         (save-restriction
1750           (message-narrow-to-headers)
1751           (goto-char (point-max))
1752           (insert "Gcc: ")
1753           (if gcc-self-val
1754               ;; Use the `gcc-self' param value instead.
1755               (progn
1756                 (insert
1757                  (if (stringp gcc-self-val)
1758                      (if (string-match " " gcc-self-val)
1759                          (concat "\"" gcc-self-val "\"")
1760                        gcc-self-val)
1761                    ;; In nndoc groups, we use the parent group name
1762                    ;; instead of the current group.
1763                    (let ((group (or (gnus-group-find-parameter
1764                                      gnus-newsgroup-name 'parent-group)
1765                                     group)))
1766                      (if (string-match " " group)
1767                          (concat "\"" group "\"")
1768                        group))))
1769                 (if (not (eq gcc-self-val 'none))
1770                     (insert "\n")
1771                   (gnus-delete-line)))
1772             ;; Use the list of groups.
1773             (while (setq name (pop groups))
1774               (let ((str (if (string-match ":" name)
1775                              name
1776                            (gnus-group-prefixed-name
1777                             name gnus-message-archive-method))))
1778                 (insert (if (string-match " " str)
1779                             (concat "\"" str "\"")
1780                           str)))
1781               (when groups
1782                 (insert " ")))
1783             (insert "\n")))))))
1784
1785 (defun gnus-mailing-list-followup-to ()
1786   "Look at the headers in the current buffer and return a Mail-Followup-To address."
1787   (let ((x-been-there (gnus-fetch-original-field "x-beenthere"))
1788         (list-post (gnus-fetch-original-field "list-post")))
1789     (when (and list-post
1790                (string-match "mailto:\\([^>]+\\)" list-post))
1791       (setq list-post (match-string 1 list-post)))
1792     (or list-post
1793         x-been-there)))
1794
1795 ;;; Posting styles.
1796
1797 (defun gnus-configure-posting-styles (&optional group-name)
1798   "Configure posting styles according to `gnus-posting-styles'."
1799   (unless gnus-inhibit-posting-styles
1800     (let ((group (or group-name gnus-newsgroup-name ""))
1801           (styles (if (gnus-buffer-live-p gnus-summary-buffer)
1802                       (with-current-buffer gnus-summary-buffer
1803                         gnus-posting-styles)
1804                     gnus-posting-styles))
1805           style match attribute value v results
1806           filep name address element)
1807       ;; If the group has a posting-style parameter, add it at the end with a
1808       ;; regexp matching everything, to be sure it takes precedence over all
1809       ;; the others.
1810       (when gnus-newsgroup-name
1811         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1812           (when tmp-style
1813             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1814       ;; Go through all styles and look for matches.
1815       (dolist (style styles)
1816         (setq match (pop style))
1817         (goto-char (point-min))
1818         (when (cond
1819                ((stringp match)
1820                 ;; Regexp string match on the group name.
1821                 (string-match match group))
1822                ((eq match 'header)
1823                 ;; Obsolete format of header match.
1824                 (and (gnus-buffer-live-p gnus-article-copy)
1825                      (with-current-buffer gnus-article-copy
1826                        (save-restriction
1827                          (nnheader-narrow-to-headers)
1828                          (let ((header (message-fetch-field (pop style))))
1829                            (and header
1830                                 (string-match (pop style) header)))))))
1831                ((or (symbolp match)
1832                     (functionp match))
1833                 (cond
1834                  ((functionp match)
1835                   ;; Function to be called.
1836                   (funcall match))
1837                  ((boundp match)
1838                   ;; Variable to be checked.
1839                   (symbol-value match))))
1840                ((listp match)
1841                 (cond
1842                  ((eq (car match) 'header)
1843                   ;; New format of header match.
1844                   (and (gnus-buffer-live-p gnus-article-copy)
1845                        (with-current-buffer gnus-article-copy
1846                          (save-restriction
1847                            (nnheader-narrow-to-headers)
1848                            (let ((header (message-fetch-field (nth 1 match))))
1849                              (and header
1850                                   (string-match (nth 2 match) header)))))))
1851                  (t
1852                   ;; This is a form to be evalled.
1853                   (eval match)))))
1854           ;; We have a match, so we set the variables.
1855           (dolist (attribute style)
1856             (setq element (pop attribute)
1857                   filep nil)
1858             (setq value
1859                   (cond
1860                    ((eq (car attribute) :file)
1861                     (setq filep t)
1862                     (cadr attribute))
1863                    ((eq (car attribute) :value)
1864                     (cadr attribute))
1865                    (t
1866                     (car attribute))))
1867             ;; We get the value.
1868             (setq v
1869                   (cond
1870                    ((stringp value)
1871                     (if (and (stringp match)
1872                              (gnus-string-match-p "\\\\[&[:digit:]]" value)
1873                              (match-beginning 1))
1874                         (gnus-match-substitute-replacement value nil nil group)
1875                       value))
1876                    ((or (symbolp value)
1877                         (functionp value))
1878                     (cond ((functionp value)
1879                            (funcall value))
1880                           ((boundp value)
1881                            (symbol-value value))))
1882                    ((listp value)
1883                     (eval value))))
1884             ;; Translate obsolescent value.
1885             (cond
1886              ((eq element 'signature-file)
1887               (setq element 'signature
1888                     filep t))
1889              ((eq element 'x-face-file)
1890               (setq element 'x-face
1891                     filep t)))
1892             ;; Post-processing for the signature posting-style:
1893             (and (eq element 'signature) filep
1894                  message-signature-directory
1895                  ;; don't actually use the signature directory
1896                  ;; if message-signature-file contains a path.
1897                  (not (file-name-directory v))
1898                  (setq v (nnheader-concat message-signature-directory v)))
1899             ;; Get the contents of file elems.
1900             (when (and filep v)
1901               (setq v (with-temp-buffer
1902                         (insert-file-contents v)
1903                         (buffer-substring
1904                          (point-min)
1905                          (progn
1906                            (goto-char (point-max))
1907                            (if (zerop (skip-chars-backward "\n"))
1908                                (point)
1909                              (1+ (point))))))))
1910             (setq results (delq (assoc element results) results))
1911             (push (cons element v) results))))
1912       ;; Now we have all the styles, so we insert them.
1913       (setq name (assq 'name results)
1914             address (assq 'address results))
1915       (setq results (delq name (delq address results)))
1916       (gnus-make-local-hook 'message-setup-hook)
1917       (setq results (sort results (lambda (x y)
1918                                     (string-lessp (car x) (car y)))))
1919       (dolist (result results)
1920         (add-hook 'message-setup-hook
1921                   (cond
1922                    ((eq 'eval (car result))
1923                     'ignore)
1924                    ((eq 'body (car result))
1925                     `(lambda ()
1926                        (save-excursion
1927                          (message-goto-body)
1928                          (insert ,(cdr result)))))
1929                    ((eq 'signature (car result))
1930                     (set (make-local-variable 'message-signature) nil)
1931                     (set (make-local-variable 'message-signature-file) nil)
1932                     (if (not (cdr result))
1933                         'ignore
1934                       `(lambda ()
1935                          (save-excursion
1936                            (let ((message-signature ,(cdr result)))
1937                              (when message-signature
1938                                (message-insert-signature)))))))
1939                    (t
1940                     (let ((header
1941                            (if (symbolp (car result))
1942                                (capitalize (symbol-name (car result)))
1943                              (car result))))
1944                       `(lambda ()
1945                          (save-excursion
1946                            (message-remove-header ,header)
1947                            (let ((value ,(cdr result)))
1948                              (when value
1949                                (message-goto-eoh)
1950                                (insert ,header ": " value)
1951                                (unless (bolp)
1952                                  (insert "\n")))))))))
1953                   nil 'local))
1954       (when (or name address)
1955         (add-hook 'message-setup-hook
1956                   `(lambda ()
1957                      (set (make-local-variable 'user-mail-address)
1958                           ,(or (cdr address) user-mail-address))
1959                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1960                            (user-mail-address
1961                             ,(or (cdr address) user-mail-address)))
1962                        (save-excursion
1963                          (message-remove-header "From")
1964                          (message-goto-eoh)
1965                          (insert "From: " (message-make-from) "\n"))))
1966                   nil 'local)))))
1967
1968 ;;; Allow redefinition of functions.
1969
1970 (gnus-ems-redefine)
1971
1972 (provide 'gnus-msg)
1973
1974 ;;; gnus-msg.el ends here