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