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