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