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