Sync.
[elisp/gnus.git-] / lisp / gnus-msg.el
1 ;;; gnus-msg.el --- mail and post interface for Semi-gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;;      Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
9 ;;      Katsumi Yamaoka  <yamaoka@jpl.org>
10 ;;      Kiyokazu SUTO    <suto@merry.xmath.ous.ac.jp>
11 ;; Keywords: mail, news, MIME
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 ;; Boston, MA 02111-1307, USA.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35 (eval-when-compile (require 'static))
36
37 (require 'gnus)
38 (require 'gnus-ems)
39 (require 'message)
40 (require 'gnus-art)
41
42 (defcustom gnus-post-method 'current
43   "*Preferred method for posting USENET news.
44
45 If this variable is `current', Gnus will use the \"current\" select
46 method when posting.  If it is nil (which is the default), Gnus will
47 use the native select method when posting.
48
49 This method will not be used in mail groups and the like, only in
50 \"real\" newsgroups.
51
52 If not nil nor `native', the value must be a valid method as discussed
53 in the documentation of `gnus-select-method'.  It can also be a list of
54 methods.  If that is the case, the user will be queried for what select
55 method to use when posting."
56   :group 'gnus-group-foreign
57   :type `(choice (const nil)
58                  (const current)
59                  (const native)
60                  (sexp :tag "Methods" ,gnus-select-method)))
61
62 (defvar gnus-outgoing-message-group nil
63   "*All outgoing messages will be put in this group.
64 If you want to store all your outgoing mail and articles in the group
65 \"nnml:archive\", you set this variable to that value.  This variable
66 can also be a list of group names.
67
68 If you want to have greater control over what group to put each
69 message in, you can set this variable to a function that checks the
70 current newsgroup name and then returns a suitable group name (or list
71 of names).")
72
73 (defvar gnus-mailing-list-groups nil
74   "*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
79 (defvar gnus-add-to-list nil
80   "*If non-nil, add a `to-list' parameter automatically.")
81
82 (defvar gnus-crosspost-complaint
83   "Hi,
84
85 You posted the article below with the following Newsgroups header:
86
87 Newsgroups: %s
88
89 The %s group, at least, was an inappropriate recipient
90 of this message.  Please trim your Newsgroups header to exclude this
91 group before posting in the future.
92
93 Thank you.
94
95 "
96   "Format string to be inserted when complaining about crossposts.
97 The first %s will be replaced by the Newsgroups header;
98 the second with the current group name.")
99
100 (defvar gnus-message-setup-hook '(gnus-maybe-setup-default-charset)
101   "Hook run after setting up a message buffer.")
102
103 (defvar gnus-bug-create-help-buffer t
104   "*Should we create the *Gnus Help Bug* buffer?")
105
106 (defvar gnus-posting-styles nil
107   "*Alist of styles to use when posting.")
108
109 (defcustom gnus-group-posting-charset-alist
110   '(("^\\(no\\|fr\\|dk\\)\\.[^,]*\\(,[ \t\n]*\\(no\\|fr\\|dk\\)\\.[^,]*\\)*$" iso-8859-1 (iso-8859-1))
111     ("^\\(fido7\\|relcom\\)\\.[^,]*\\(,[ \t\n]*\\(fido7\\|relcom\\)\\.[^,]*\\)*$" koi8-r (koi8-r))
112     (message-this-is-mail nil nil)
113     (message-this-is-news nil t))
114   "Alist of regexps and permitted unencoded charsets for posting.
115 Each element of the alist has the form (TEST HEADER BODY-LIST), where
116 TEST is either a regular expression matching the newsgroup header or a
117 variable to query,
118 HEADER is the charset which may be left unencoded in the header (nil
119 means encode all charsets),
120 BODY-LIST is a list of charsets which may be encoded using 8bit
121 content-transfer encoding in the body, or one of the special values
122 nil (always encode using quoted-printable) or t (always use 8bit).
123
124 Note that any value other tha nil for HEADER infringes some RFCs, so
125 use this option with care."
126   :type '(repeat (list :tag "Permitted unencoded charsets"
127                   (choice :tag "Where"
128                    (regexp :tag "Group")
129                    (const :tag "Mail message" :value message-this-is-mail)
130                    (const :tag "News article" :value message-this-is-news))
131                   (choice :tag "Header"
132                    (const :tag "None" nil)
133                    (symbol :tag "Charset"))
134                   (choice :tag "Body"
135                           (const :tag "Any" :value t)
136                           (const :tag "None" :value nil)
137                           (repeat :tag "Charsets"
138                                   (symbol :tag "Charset")))))
139   :group 'gnus-charset)
140
141 ;;; Internal variables.
142
143 (defvar gnus-inhibit-posting-styles nil
144   "Inhibit the use of posting styles.")
145
146 (defvar gnus-message-buffer "*Mail Gnus*")
147 (defvar gnus-article-copy nil)
148 (defvar gnus-last-posting-server nil)
149 (defvar gnus-message-group-art nil)
150
151 (defconst gnus-bug-message
152   (format "Sending a bug report to the Gnus Towers.
153 ========================================
154
155 This gnus is the %s%s.
156 If you think the bug is a Semi-gnus bug, send a bug report to Semi-gnus
157 Developers. (the addresses below are mailing list addresses)
158
159 ========================================
160
161 The buffer below is a mail buffer.  When you press `C-c C-c', it will
162 be sent to the Gnus Bug Exterminators.
163
164 The thing near the bottom of the buffer is how the environment
165 settings will be included in the mail.  Please do not delete that.
166 They will tell the Bug People what your environment is, so that it
167 will be easier to locate the bugs.
168
169 If you have found a bug that makes Emacs go \"beep\", set
170 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
171 and include the backtrace in your bug report.
172
173 Please describe the bug in annoying, painstaking detail.
174
175 Thank you for your help in stamping out bugs.
176 "
177           gnus-product-name
178           (if (string= gnus-product-name "Semi-gnus")
179               ""
180             ", a modified version of Semi-gnus")))
181
182 (eval-and-compile
183   (autoload 'gnus-uu-post-news "gnus-uu" nil t)
184   (autoload 'news-setup "rnewspost")
185   (autoload 'news-reply-mode "rnewspost")
186   (autoload 'rmail-dont-reply-to "mail-utils")
187   (autoload 'rmail-output "rmailout"))
188
189 \f
190 ;;;
191 ;;; Gnus Posting Functions
192 ;;;
193
194 (gnus-define-keys (gnus-summary-send-map "S" gnus-summary-mode-map)
195   "p" gnus-summary-post-news
196   "f" gnus-summary-followup
197   "F" gnus-summary-followup-with-original
198   "c" gnus-summary-cancel-article
199   "s" gnus-summary-supersede-article
200   "r" gnus-summary-reply
201   "y" gnus-summary-yank-message
202   "R" gnus-summary-reply-with-original
203   "w" gnus-summary-wide-reply
204   "W" gnus-summary-wide-reply-with-original
205   "n" gnus-summary-followup-to-mail
206   "N" gnus-summary-followup-to-mail-with-original
207   "m" gnus-summary-mail-other-window
208   "u" gnus-uu-post-news
209   "\M-c" gnus-summary-mail-crosspost-complaint
210   "om" gnus-summary-mail-forward
211   "op" gnus-summary-post-forward
212   "Om" gnus-summary-mail-digest
213   "Op" gnus-summary-post-digest)
214
215 (gnus-define-keys (gnus-send-bounce-map "D" gnus-summary-send-map)
216   "b" gnus-summary-resend-bounced-mail
217   ;; "c" gnus-summary-send-draft
218   "r" gnus-summary-resend-message)
219
220 ;;; Internal functions.
221
222 (defvar gnus-article-reply nil)
223 (defmacro gnus-setup-message (config &rest forms)
224   (let ((winconf (make-symbol "gnus-setup-message-winconf"))
225         (buffer (make-symbol "gnus-setup-message-buffer"))
226         (article (make-symbol "gnus-setup-message-article"))
227         (group (make-symbol "gnus-setup-message-group")))
228     `(let ((,winconf (current-window-configuration))
229            (,buffer (buffer-name (current-buffer)))
230            (,article (and gnus-article-reply (gnus-summary-article-number)))
231            (,group gnus-newsgroup-name)
232            (message-header-setup-hook
233             (copy-sequence message-header-setup-hook))
234            (message-mode-hook (copy-sequence message-mode-hook))
235            (message-startup-parameter-alist
236             '((reply-buffer . gnus-copy-article-buffer)
237               (original-buffer . gnus-original-article-buffer)
238               (user-agent . Gnus))))
239        (add-hook 'message-header-setup-hook 'gnus-inews-insert-gcc)
240        (add-hook 'message-header-setup-hook 'gnus-inews-insert-archive-gcc)
241        (add-hook 'message-mode-hook 'gnus-configure-posting-styles)
242        (unwind-protect
243            (progn
244              ,@forms)
245          (gnus-inews-add-send-actions ,winconf ,buffer ,article)
246          (setq gnus-message-buffer (current-buffer))
247          (set (make-local-variable 'gnus-message-group-art)
248               (cons ,group ,article))
249          (set (make-local-variable 'gnus-newsgroup-name) ,group)
250          (gnus-run-hooks 'gnus-message-setup-hook))
251        (gnus-add-buffer)
252        (gnus-configure-windows ,config t)
253        (set-buffer-modified-p nil))))
254
255 (defun gnus-setup-posting-charset (group)
256   (let ((alist gnus-group-posting-charset-alist)
257         (group (or group ""))
258         elem)
259     (when group
260       (catch 'found
261         (while (setq elem (pop alist))
262           (when (or (and (stringp (car elem))
263                          (string-match (car elem) group))
264                     (and (gnus-functionp (car elem))
265                          (funcall (car elem) group))
266                     (and (symbolp (car elem))
267                          (symbol-value (car elem))))
268             (throw 'found (cons (cadr elem) (caddr elem)))))))))
269
270 (defun gnus-inews-add-send-actions (winconf buffer article)
271   (make-local-hook 'message-sent-hook)
272   (add-hook 'message-sent-hook 'gnus-inews-do-gcc nil t)
273   (setq message-post-method
274         `(lambda (arg)
275            (gnus-post-method arg ,gnus-newsgroup-name)))
276   (setq message-user-agent (gnus-extended-version))
277   (when (not message-use-multi-frames)
278     (message-add-action
279      `(set-window-configuration ,winconf) 'exit 'postpone 'kill))
280   (message-add-action
281    `(when (gnus-buffer-exists-p ,buffer)
282       (save-excursion
283         (set-buffer ,buffer)
284         ,(when article
285            `(gnus-summary-mark-article-as-replied ,article))))
286    'send))
287
288 (put 'gnus-setup-message 'lisp-indent-function 1)
289 (put 'gnus-setup-message 'edebug-form-spec '(form body))
290
291 ;;; Post news commands of Gnus group mode and summary mode
292
293 (defun gnus-group-mail (&optional arg)
294   "Start composing a mail.
295 If ARG, use the group under the point to find a posting style.
296 If ARG is 1, prompt for a group name to find the posting style."
297   (interactive "P")
298   ;; We can't `let' gnus-newsgroup-name here, since that leads
299   ;; to local variables leaking.
300   (let ((group gnus-newsgroup-name)
301         (buffer (current-buffer)))
302     (unwind-protect
303         (progn
304           (setq gnus-newsgroup-name
305                 (if arg
306                     (if (= 1 (prefix-numeric-value arg))
307                         (completing-read "Use posting style of group: "
308                                          gnus-active-hashtb nil
309                                          (gnus-read-active-file-p))
310                       (gnus-group-group-name))
311                   ""))
312           (gnus-setup-message 'message (message-mail)))
313       (save-excursion
314         (set-buffer buffer)
315         (setq gnus-newsgroup-name group)))))
316
317 (defun gnus-group-post-news (&optional arg)
318   "Start composing a news message.
319 If ARG, post to the group under point.
320 If ARG is 1, prompt for a group name."
321   (interactive "P")
322   ;; Bind this variable here to make message mode hooks work ok.
323   (let ((gnus-newsgroup-name
324          (if arg
325              (if (= 1 (prefix-numeric-value arg))
326                  (completing-read "Newsgroup: " gnus-active-hashtb nil
327                                   (gnus-read-active-file-p))
328                (gnus-group-group-name))
329            "")))
330     (gnus-post-news 'post gnus-newsgroup-name)))
331
332 (defun gnus-summary-post-news ()
333   "Start composing a news message."
334   (interactive)
335   (gnus-post-news 'post gnus-newsgroup-name))
336
337 (defun gnus-summary-followup (yank &optional force-news)
338   "Compose a followup to an article.
339 If prefix argument YANK is non-nil, original article is yanked automatically."
340   (interactive
341    (list (and current-prefix-arg
342               (gnus-summary-work-articles 1))))
343   (when yank
344     (gnus-summary-goto-subject (car yank)))
345   (save-window-excursion
346     (gnus-summary-select-article))
347   (let ((headers (gnus-summary-article-header (gnus-summary-article-number)))
348         (gnus-newsgroup-name gnus-newsgroup-name))
349     ;; Send a followup.
350     (gnus-post-news nil gnus-newsgroup-name
351                     headers gnus-article-buffer
352                     yank nil force-news)))
353
354 (defun gnus-summary-followup-with-original (n &optional force-news)
355   "Compose a followup to an article and include the original article."
356   (interactive "P")
357   (gnus-summary-followup (gnus-summary-work-articles n) force-news))
358
359 (defun gnus-summary-followup-to-mail (&optional arg)
360   "Followup to the current mail message via news."
361   (interactive
362    (list (and current-prefix-arg
363               (gnus-summary-work-articles 1))))
364   (gnus-summary-followup arg t))
365
366 (defun gnus-summary-followup-to-mail-with-original (&optional arg)
367   "Followup to the current mail message via news."
368   (interactive "P")
369   (gnus-summary-followup (gnus-summary-work-articles arg) t))
370
371 (defun gnus-inews-yank-articles (articles)
372   (let* ((more-than-one (cdr articles))
373          (frame (when (and message-use-multi-frames more-than-one)
374                   (window-frame (get-buffer-window (current-buffer)))))
375          refs beg article)
376     (message-goto-body)
377     (while (setq article (pop articles))
378       (save-window-excursion
379         (set-buffer gnus-summary-buffer)
380         (gnus-summary-select-article nil nil nil article)
381         (gnus-summary-remove-process-mark article))
382       (when frame
383         (select-frame frame))
384
385       ;; Gathering references.
386       (when more-than-one
387         (setq refs (message-list-references
388                     refs
389                     (mail-header-references gnus-current-headers)
390                     (mail-header-message-id gnus-current-headers))))
391
392       (gnus-copy-article-buffer)
393       (let ((message-reply-buffer gnus-article-copy)
394             (message-reply-headers gnus-current-headers))
395         (message-yank-original)
396         (setq beg (or beg (mark t))))
397       (when articles
398         (insert "\n")))
399     (push-mark)
400
401     ;; Replace with the gathered references.
402     (when refs
403       (push-mark beg)
404       (save-restriction
405         (message-narrow-to-headers)
406         (let ((case-fold-search t))
407           (if (re-search-forward "^References:\\([\t ]+.+\n\\)+" nil t)
408               (replace-match "")
409             (goto-char (point-max))))
410         (mail-header-format
411          (list (or (assq 'References message-header-format-alist)
412                    '(References . message-shorten-references)))
413          (list (cons 'References
414                      (mapconcat 'identity (nreverse refs) " "))))
415         (backward-delete-char 1))
416       (setq beg (mark t))
417       (pop-mark))
418
419     (goto-char beg)))
420
421 (defun gnus-summary-cancel-article (&optional n symp)
422   "Cancel an article you posted.
423 Uses the process-prefix convention.  If given the symbolic
424 prefix `a', cancel using the standard posting method; if not
425 post using the current select method."
426   (interactive (gnus-interactive "P\ny"))
427   (let ((articles (gnus-summary-work-articles n))
428         (message-post-method
429          `(lambda (arg)
430             (gnus-post-method (not (eq symp 'a)) ,gnus-newsgroup-name)))
431         article)
432     (while (setq article (pop articles))
433       (when (gnus-summary-select-article t nil nil article)
434         (when (gnus-eval-in-buffer-window gnus-article-buffer
435                 (save-excursion
436                   (set-buffer gnus-original-article-buffer)
437                   (message-cancel-news)))
438           (gnus-summary-mark-as-read article gnus-canceled-mark)
439           (gnus-cache-remove-article 1))
440         (gnus-article-hide-headers-if-wanted))
441       (gnus-summary-remove-process-mark article))))
442
443 (defun gnus-summary-supersede-article ()
444   "Compose an article that will supersede a previous article.
445 This is done simply by taking the old article and adding a Supersedes
446 header line with the old Message-ID."
447   (interactive)
448   (let ((article (gnus-summary-article-number))
449         (gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
450     (gnus-setup-message 'reply-yank
451       (gnus-summary-select-article t)
452       (set-buffer gnus-original-article-buffer)
453       (message-supersede)
454       (push
455        `((lambda ()
456            (when (gnus-buffer-exists-p ,gnus-summary-buffer)
457              (save-excursion
458                (set-buffer ,gnus-summary-buffer)
459                (gnus-cache-possibly-remove-article ,article nil nil nil t)
460                (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
461        message-send-actions))))
462
463 \f
464
465 (defun gnus-copy-article-buffer (&optional article-buffer)
466   ;; make a copy of the article buffer with all text properties removed
467   ;; this copy is in the buffer gnus-article-copy.
468   ;; if ARTICLE-BUFFER is nil, gnus-article-buffer is used
469   ;; this buffer should be passed to all mail/news reply/post routines.
470   (setq gnus-article-copy (gnus-get-buffer-create " *gnus article copy*"))
471   (let ((article-buffer (or article-buffer gnus-article-buffer))
472         end beg)
473     (if (not (and (get-buffer article-buffer)
474                   (gnus-buffer-exists-p article-buffer)))
475         (error "Can't find any article buffer")
476       (save-excursion
477         (set-buffer article-buffer)
478         (save-restriction
479           ;; Copy over the (displayed) article buffer, delete
480           ;; hidden text and remove text properties.
481           (widen)
482           (let ((inhibit-read-only t))
483             (copy-to-buffer gnus-article-copy (point-min) (point-max))
484             (set-buffer gnus-article-copy)
485             (gnus-article-delete-text-of-type 'annotation)
486             (gnus-remove-text-with-property 'gnus-prev)
487             (gnus-remove-text-with-property 'gnus-next)
488             (gnus-remove-text-with-property 'x-face-mule-bitmap-image)
489             (insert
490              (prog1
491                  (format "%s" (buffer-string))
492                (erase-buffer)))
493             )
494           ;; Find the original headers.
495           (set-buffer gnus-original-article-buffer)
496           (goto-char (point-min))
497           (while (looking-at message-unix-mail-delimiter)
498             (forward-line 1))
499           (setq beg (point))
500           (setq end (or (search-forward "\n\n" nil t) (point)))
501           ;; Delete the headers from the displayed articles.
502           (set-buffer gnus-article-copy)
503           (delete-region (goto-char (point-min))
504                          (or (search-forward "\n\n" nil t) (point-max)))
505           ;; Encode bitmap smileys to ordinary text.
506           (static-unless (featurep 'xemacs)
507             (when (featurep 'smiley-mule)
508               (smiley-encode-buffer)))
509           ;; Insert the original article headers.
510           (insert-buffer-substring gnus-original-article-buffer beg end)
511           (article-decode-encoded-words)))
512       gnus-article-copy)))
513
514 (defun gnus-post-news (post &optional group header article-buffer yank subject
515                             force-news)
516   (when article-buffer
517     (gnus-copy-article-buffer))
518   (let ((gnus-article-reply article-buffer)
519         (add-to-list gnus-add-to-list))
520     (gnus-setup-message (cond (yank 'reply-yank)
521                               (article-buffer 'reply)
522                               (t 'message))
523       (let* ((group (or group gnus-newsgroup-name))
524              (pgroup group)
525              to-address to-group mailing-list to-list
526              newsgroup-p)
527         (when group
528           (setq to-address (gnus-group-find-parameter group 'to-address)
529                 to-group (gnus-group-find-parameter group 'to-group)
530                 to-list (gnus-group-find-parameter group 'to-list)
531                 newsgroup-p (gnus-group-find-parameter group 'newsgroup)
532                 mailing-list (when gnus-mailing-list-groups
533                                (string-match gnus-mailing-list-groups group))
534                 group (gnus-group-real-name group)))
535         (if (or (and to-group
536                      (gnus-news-group-p to-group))
537                 newsgroup-p
538                 force-news
539                 (and (gnus-news-group-p
540                       (or pgroup gnus-newsgroup-name)
541                       (if header (mail-header-number header)
542                         gnus-current-article))
543                      (not mailing-list)
544                      (not to-list)
545                      (not to-address)))
546             ;; This is news.
547             (if post
548                 (message-news (or to-group group))
549               (set-buffer gnus-article-copy)
550               (gnus-msg-treat-broken-reply-to)
551               (message-followup (if (or newsgroup-p force-news) nil to-group)))
552           ;; The is mail.
553           (if post
554               (progn
555                 (message-mail (or to-address to-list))
556                 ;; Arrange for mail groups that have no `to-address' to
557                 ;; get that when the user sends off the mail.
558                 (when (and (not to-list)
559                            (not to-address)
560                            add-to-list)
561                   (push (list 'gnus-inews-add-to-address pgroup)
562                         message-send-actions)))
563             (set-buffer gnus-article-copy)
564             (gnus-msg-treat-broken-reply-to)
565             (message-wide-reply to-address)))
566         (when yank
567           (gnus-inews-yank-articles yank))))))
568
569 (defun gnus-msg-treat-broken-reply-to ()
570   "Remove the Reply-to header iff broken-reply-to."
571   (when (gnus-group-find-parameter
572          gnus-newsgroup-name 'broken-reply-to)
573     (save-restriction
574       (message-narrow-to-head)
575       (message-remove-header "reply-to"))))
576
577 (defun gnus-post-method (arg group &optional silent)
578   "Return the posting method based on GROUP and ARG.
579 If SILENT, don't prompt the user."
580   (let ((group-method (gnus-find-method-for-group group)))
581     (cond
582      ;; If the group-method is nil (which shouldn't happen) we use
583      ;; the default method.
584      ((null group-method)
585       (or (and (null (eq gnus-post-method 'active)) gnus-post-method)
586           gnus-select-method message-post-method))
587      ;; We want the inverse of the default
588      ((and arg (not (eq arg 0)))
589       (if (eq gnus-post-method 'active)
590           gnus-select-method
591         group-method))
592      ;; We query the user for a post method.
593      ((or arg
594           (and gnus-post-method
595                (not (eq gnus-post-method 'current))
596                (listp (car gnus-post-method))))
597       (let* ((methods
598               ;; Collect all methods we know about.
599               (append
600                (when (and gnus-post-method
601                           (not (eq gnus-post-method 'current)))
602                  (if (listp (car gnus-post-method))
603                      gnus-post-method
604                    (list gnus-post-method)))
605                gnus-secondary-select-methods
606                (mapcar 'cdr gnus-server-alist)
607                (mapcar 'car gnus-opened-servers)
608                (list gnus-select-method)
609                (list group-method)))
610              method-alist post-methods method)
611         ;; Weed out all mail methods.
612         (while methods
613           (setq method (gnus-server-get-method "" (pop methods)))
614           (when (and (or (gnus-method-option-p method 'post)
615                          (gnus-method-option-p method 'post-mail))
616                      (not (member method post-methods)))
617             (push method post-methods)))
618         ;; Create a name-method alist.
619         (setq method-alist
620               (mapcar
621                (lambda (m)
622                  (list (concat (cadr m) " (" (symbol-name (car m)) ")") m))
623                post-methods))
624         ;; Query the user.
625         (cadr
626          (assoc
627           (setq gnus-last-posting-server
628                 (if (and silent
629                          gnus-last-posting-server)
630                     ;; Just use the last value.
631                     gnus-last-posting-server
632                   (completing-read
633                    "Posting method: " method-alist nil t
634                    (cons (or gnus-last-posting-server "") 0))))
635           method-alist))))
636      ;; Override normal method.
637      ((and (eq gnus-post-method 'current)
638            (not (eq (car group-method) 'nndraft))
639            (gnus-get-function group-method 'request-post t)
640            (not arg))
641       group-method)
642      ((and gnus-post-method
643            (not (eq gnus-post-method 'current)))
644       gnus-post-method)
645      ;; Use the normal select method.
646      (t gnus-select-method))))
647
648 \f
649
650 (defun gnus-message-make-user-agent (&optional include-mime-info max-column)
651   "Return user-agent info.
652 INCLUDE-MIME-INFO the optional first argument if it is non-nil and the variable
653   `mime-edit-user-agent-value' exists, the return value will include it.
654 MAX-COLUMN the optional second argument if it is specified, the return value
655   will be folded up in the proper way."
656   (let ((user-agent (if (and include-mime-info
657                              (boundp 'mime-edit-user-agent-value))
658                         (concat (gnus-extended-version)
659                                 " "
660                                 mime-edit-user-agent-value)
661                       (gnus-extended-version))))
662     (if max-column
663         (let (boundary)
664           (unless (natnump max-column) (setq max-column 76))
665           (with-temp-buffer
666             (insert "            " user-agent)
667             (goto-char 13)
668             (while (re-search-forward "[\n\t ]+" nil t)
669               (replace-match " "))
670             (goto-char 13)
671             (while (re-search-forward "[^ ()/]+\\(/[^ ()/]+\\)? ?" nil t)
672               (while (eq ?\( (char-after (point)))
673                 (forward-list)
674                 (skip-chars-forward " "))
675               (skip-chars-backward " ")
676               (if (> (current-column) max-column)
677                   (progn
678                     (if (or (not boundary) (eq ?\n (char-after boundary)))
679                         (progn
680                           (setq boundary (point))
681                           (unless (eobp)
682                             (delete-char 1)
683                             (insert "\n ")))
684                       (goto-char boundary)
685                       (delete-char 1)
686                       (insert "\n ")))
687                 (setq boundary (point))))
688             (buffer-substring 13 (point-max))))
689       user-agent)))
690
691 \f
692 ;;;
693 ;;; Gnus Mail Functions
694 ;;;
695
696 ;;; Mail reply commands of Gnus summary mode
697
698 (defun gnus-summary-reply (&optional yank wide)
699   "Start composing a reply mail to the current message.
700 If prefix argument YANK is non-nil, the original article is yanked
701 automatically."
702   (interactive
703    (list (and current-prefix-arg
704               (gnus-summary-work-articles 1))))
705   ;; Stripping headers should be specified with mail-yank-ignored-headers.
706   (when yank
707     (gnus-summary-goto-subject (car yank)))
708   (let ((gnus-article-reply t))
709     (gnus-setup-message (if yank 'reply-yank 'reply)
710       (gnus-summary-select-article)
711       (set-buffer (gnus-copy-article-buffer))
712       (gnus-msg-treat-broken-reply-to)
713       (message-reply nil wide)
714       (when yank
715         (gnus-inews-yank-articles yank)))))
716
717 (defun gnus-summary-reply-with-original (n &optional wide)
718   "Start composing a reply mail to the current message.
719 The original article will be yanked."
720   (interactive "P")
721   (gnus-summary-reply (gnus-summary-work-articles n) wide))
722
723 (defun gnus-summary-wide-reply (&optional yank)
724   "Start composing a wide reply mail to the current message.
725 If prefix argument YANK is non-nil, the original article is yanked
726 automatically."
727   (interactive
728    (list (and current-prefix-arg
729               (gnus-summary-work-articles 1))))
730   (gnus-summary-reply yank t))
731
732 (defun gnus-summary-wide-reply-with-original (n)
733   "Start composing a wide reply mail to the current message.
734 The original article will be yanked."
735   (interactive "P")
736   (gnus-summary-reply-with-original n t))
737
738 (defun gnus-summary-mail-forward (&optional full-headers post)
739   "Forward the current message to another user.
740 If FULL-HEADERS (the prefix), include full headers when forwarding."
741   (interactive "P")
742   (gnus-setup-message 'forward
743     (gnus-summary-select-article)
744     (let ((charset default-mime-charset))
745       (set-buffer gnus-original-article-buffer)
746       (make-local-variable 'default-mime-charset)
747       (setq default-mime-charset charset)
748       )
749     (let ((message-included-forward-headers
750            (if full-headers "" message-included-forward-headers)))
751       (message-forward post))))
752
753 ;;; XXX: generate Subject and ``Topics''?
754 (defun gnus-summary-mail-digest (&optional n post)
755   "Digests and forwards all articles in this series."
756   (interactive "P")
757   (let ((subject "Digested Articles")
758         (articles (gnus-summary-work-articles n))
759         article frame)
760     (gnus-setup-message 'forward
761       (gnus-summary-select-article)
762       (if post (message-news nil subject) (message-mail nil subject))
763       (when (and message-use-multi-frames (cdr articles))
764         (setq frame (window-frame (get-buffer-window (current-buffer)))))
765       (message-goto-body)
766       (while (setq article (pop articles))
767         (save-window-excursion
768           (set-buffer gnus-summary-buffer)
769           (gnus-summary-select-article nil nil nil article)
770           (gnus-summary-remove-process-mark article))
771         (when frame
772           (select-frame frame))
773         (insert (mime-make-tag "message" "rfc822") "\n")
774         (insert-buffer-substring gnus-original-article-buffer))
775       (push-mark)
776       (message-goto-body)
777       (mime-edit-enclose-digest-region (point)(mark t)))))
778
779 (defun gnus-summary-post-digest (&optional n)
780   "Digest and forwards all articles in this series to a newsgroup."
781   (interactive "P")
782   (gnus-summary-mail-digest n t))
783
784 (defun gnus-summary-resend-message (address n)
785   "Resend the current article to ADDRESS."
786   (interactive "sResend message(s) to: \nP")
787   (let ((articles (gnus-summary-work-articles n))
788         article)
789     (while (setq article (pop articles))
790       (gnus-summary-select-article nil nil nil article)
791       (save-excursion
792         (set-buffer gnus-original-article-buffer)
793         (message-resend address)))))
794
795 (defun gnus-summary-post-forward (&optional full-headers)
796   "Forward the current article to a newsgroup.
797 If FULL-HEADERS (the prefix), include full headers when forwarding."
798   (interactive "P")
799   (gnus-summary-mail-forward full-headers t))
800
801 (defvar gnus-nastygram-message
802   "The following article was inappropriately posted to %s.\n\n"
803   "Format string to insert in nastygrams.
804 The current group name will be inserted at \"%s\".")
805
806 (defun gnus-summary-mail-nastygram (n)
807   "Send a nastygram to the author of the current article."
808   (interactive "P")
809   (when (or gnus-expert-user
810             (gnus-y-or-n-p
811              "Really send a nastygram to the author of the current article? "))
812     (let ((group gnus-newsgroup-name))
813       (gnus-summary-reply-with-original n)
814       (set-buffer gnus-message-buffer)
815       (message-goto-body)
816       (insert (format gnus-nastygram-message group))
817       (message-send-and-exit))))
818
819 (defun gnus-summary-mail-crosspost-complaint (n)
820   "Send a complaint about crossposting to the current article(s)."
821   (interactive "P")
822   (let ((articles (gnus-summary-work-articles n))
823         article)
824     (while (setq article (pop articles))
825       (set-buffer gnus-summary-buffer)
826       (gnus-summary-goto-subject article)
827       (let ((group (gnus-group-real-name gnus-newsgroup-name))
828             newsgroups followup-to)
829         (gnus-summary-select-article)
830         (set-buffer gnus-original-article-buffer)
831         (if (and (<= (length (message-tokenize-header
832                               (setq newsgroups
833                                     (mail-fetch-field "newsgroups"))
834                               ", "))
835                      1)
836                  (or (not (setq followup-to (mail-fetch-field "followup-to")))
837                      (not (member group (message-tokenize-header
838                                          followup-to ", ")))))
839             (if followup-to
840                 (gnus-message 1 "Followup-to restricted")
841               (gnus-message 1 "Not a crossposted article"))
842           (set-buffer gnus-summary-buffer)
843           (gnus-summary-reply-with-original 1)
844           (set-buffer gnus-message-buffer)
845           (message-goto-body)
846           (insert (format gnus-crosspost-complaint newsgroups group))
847           (message-goto-subject)
848           (re-search-forward " *$")
849           (replace-match " (crosspost notification)" t t)
850           (gnus-deactivate-mark)
851           (when (gnus-y-or-n-p "Send this complaint? ")
852             (message-send-and-exit)))))))
853
854 (defun gnus-summary-mail-other-window ()
855   "Compose mail in other window."
856   (interactive)
857   (gnus-setup-message 'message
858     (message-mail)))
859
860 (defun gnus-mail-parse-comma-list ()
861   (let (accumulated
862         beg)
863     (skip-chars-forward " ")
864     (while (not (eobp))
865       (setq beg (point))
866       (skip-chars-forward "^,")
867       (while (zerop
868               (save-excursion
869                 (save-restriction
870                   (let ((i 0))
871                     (narrow-to-region beg (point))
872                     (goto-char beg)
873                     (logand (progn
874                               (while (search-forward "\"" nil t)
875                                 (incf i))
876                               (if (zerop i) 2 i))
877                             2)))))
878         (skip-chars-forward ",")
879         (skip-chars-forward "^,"))
880       (skip-chars-backward " ")
881       (push (buffer-substring beg (point))
882             accumulated)
883       (skip-chars-forward "^,")
884       (skip-chars-forward ", "))
885     accumulated))
886
887 (defun gnus-inews-add-to-address (group)
888   (let ((to-address (mail-fetch-field "to")))
889     (when (and to-address
890                (gnus-alive-p))
891       ;; This mail group doesn't have a `to-list', so we add one
892       ;; here.  Magic!
893       (when (gnus-y-or-n-p
894              (format "Do you want to add this as `to-list': %s " to-address))
895         (gnus-group-add-parameter group (cons 'to-list to-address))))))
896
897 (defun gnus-put-message ()
898   "Put the current message in some group and return to Gnus."
899   (interactive)
900   (let ((reply gnus-article-reply)
901         (winconf gnus-prev-winconf)
902         (group gnus-newsgroup-name))
903
904     (or (and group (not (gnus-group-read-only-p group)))
905         (setq group (read-string "Put in group: " nil
906                                  (gnus-writable-groups))))
907     (when (gnus-gethash group gnus-newsrc-hashtb)
908       (error "No such group: %s" group))
909
910     (save-excursion
911       (save-restriction
912         (widen)
913         (message-narrow-to-headers)
914         (let (gnus-deletable-headers)
915           (if (message-news-p)
916               (message-generate-headers message-required-news-headers)
917             (message-generate-headers message-required-mail-headers)))
918         (goto-char (point-max))
919         (insert "Gcc: " group "\n")
920         (widen)))
921
922     (gnus-inews-do-gcc)
923
924     (when (get-buffer gnus-group-buffer)
925       (when (gnus-buffer-exists-p (car-safe reply))
926         (set-buffer (car reply))
927         (and (cdr reply)
928              (gnus-summary-mark-article-as-replied
929               (cdr reply))))
930       (when winconf
931         (set-window-configuration winconf)))))
932
933 (defun gnus-article-mail (yank)
934   "Send a reply to the address near point.
935 If YANK is non-nil, include the original article."
936   (interactive "P")
937   (let ((address
938          (buffer-substring
939           (save-excursion (re-search-backward "[ \t\n]" nil t) (1+ (point)))
940           (save-excursion (re-search-forward "[ \t\n]" nil t) (1- (point))))))
941     (when address
942       (message-reply address)
943       (when yank
944         (gnus-inews-yank-articles (list (cdr gnus-article-current)))))))
945
946 (defvar nntp-server-type)
947 (defun gnus-bug ()
948   "Send a bug report to the Gnus maintainers."
949   (interactive)
950   (unless (gnus-alive-p)
951     (error "Gnus has been shut down"))
952   (gnus-setup-message 'bug
953     (delete-other-windows)
954     (when gnus-bug-create-help-buffer
955       (switch-to-buffer "*Gnus Help Bug*")
956       (erase-buffer)
957       (insert gnus-bug-message)
958       (goto-char (point-min)))
959     (message-pop-to-buffer "*Gnus Bug*")
960     (message-setup `((To . ,gnus-maintainer) (Subject . "")))
961     (when gnus-bug-create-help-buffer
962       (push `(gnus-bug-kill-buffer) message-send-actions))
963     (goto-char (point-min))
964     (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
965     (forward-line 1)
966     (insert gnus-product-name " " gnus-version-number
967             " (r" gnus-revision-number ") "
968             "based on " gnus-original-product-name " v"
969             gnus-original-version-number "\n"
970             (emacs-version) "\n")
971     (when (and (boundp 'nntp-server-type)
972                (stringp nntp-server-type))
973       (insert nntp-server-type))
974     (insert "\n\n\n\n\n")
975     (let (mime-content-types)
976       (mime-edit-insert-tag "text" "plain" "; type=emacs-lisp"))
977     (insert (with-temp-buffer
978               (gnus-debug)
979               (buffer-string)))
980     (goto-char (point-min))
981     (search-forward "Subject: " nil t)
982     (message "")))
983
984 (defun gnus-bug-kill-buffer ()
985   (when (get-buffer "*Gnus Help Bug*")
986     (kill-buffer "*Gnus Help Bug*")))
987
988 (defun gnus-summary-yank-message (buffer n)
989   "Yank the current article into a composed message."
990   (interactive
991    (list (completing-read "Buffer: " (mapcar 'list (message-buffers)) nil t)
992          current-prefix-arg))
993   (when (gnus-buffer-live-p buffer)
994     (let ((summary-frame (selected-frame))
995           (message-frame (when (static-if (featurep 'xemacs)
996                                    (device-on-window-system-p)
997                                  window-system)
998                            (let ((window (get-buffer-window buffer t)))
999                              (when window
1000                                (window-frame window)))))
1001           (separator (concat "^" (regexp-quote mail-header-separator)
1002                              "\n")))
1003       (gnus-summary-iterate n
1004         (gnus-summary-select-article)
1005         (gnus-copy-article-buffer)
1006         (when (frame-live-p message-frame)
1007           (raise-frame message-frame)
1008           (select-frame message-frame))
1009         (with-current-buffer buffer
1010           (when (save-excursion
1011                   (beginning-of-line)
1012                   (let (case-fold-search)
1013                     (and (not (re-search-backward separator nil t))
1014                          (re-search-forward separator nil t))))
1015             (goto-char (match-end 0)))
1016           (message-yank-buffer gnus-article-copy))
1017         (select-frame summary-frame))
1018       (when (frame-live-p message-frame)
1019         (select-frame message-frame)))))
1020
1021 (defun gnus-debug ()
1022   "Attempts to go through the Gnus source file and report what variables have been changed.
1023 The source file has to be in the Emacs load path."
1024   (interactive)
1025   (let ((files '("gnus.el" "gnus-sum.el" "gnus-group.el"
1026                  "gnus-art.el" "gnus-start.el" "gnus-async.el"
1027                  "gnus-msg.el" "gnus-score.el" "gnus-win.el" "gnus-topic.el"
1028                  "nnmail.el" "message.el"))
1029         (point (point))
1030         file expr olist sym)
1031     (gnus-message 4 "Please wait while we snoop your variables...")
1032     (sit-for 0)
1033     ;; Go through all the files looking for non-default values for variables.
1034     (save-excursion
1035       (set-buffer (gnus-get-buffer-create " *gnus bug info*"))
1036       (while files
1037         (erase-buffer)
1038         (when (and (setq file (locate-library (pop files)))
1039                    (file-exists-p file))
1040           (insert-file-contents file)
1041           (goto-char (point-min))
1042           (if (not (re-search-forward "^;;* *Internal variables" nil t))
1043               (gnus-message 4 "Malformed sources in file %s" file)
1044             (narrow-to-region (point-min) (point))
1045             (goto-char (point-min))
1046             (while (setq expr (ignore-errors (read (current-buffer))))
1047               (ignore-errors
1048                 (and (or (eq (car expr) 'defvar)
1049                          (eq (car expr) 'defcustom))
1050                      (stringp (nth 3 expr))
1051                      (or (not (boundp (nth 1 expr)))
1052                          (not (equal (eval (nth 2 expr))
1053                                      (symbol-value (nth 1 expr)))))
1054                      (push (nth 1 expr) olist)))))))
1055       (kill-buffer (current-buffer)))
1056     (when (setq olist (nreverse olist))
1057       (insert "------------------ Environment follows ------------------\n\n"))
1058     (while olist
1059       (if (boundp (car olist))
1060           (condition-case ()
1061               (pp `(setq ,(car olist)
1062                          ,(if (or (consp (setq sym (symbol-value (car olist))))
1063                                   (and (symbolp sym)
1064                                        (not (or (eq sym nil)
1065                                                 (eq sym t)))))
1066                               (list 'quote (symbol-value (car olist)))
1067                             (symbol-value (car olist))))
1068                   (current-buffer))
1069             (error
1070              (format "(setq %s 'whatever)\n" (car olist))))
1071         (insert ";; (makeunbound '" (symbol-name (car olist)) ")\n"))
1072       (setq olist (cdr olist)))
1073     (insert "\n\n")
1074     ;; Remove any control chars - they seem to cause trouble for some
1075     ;; mailers.  (Byte-compiled output from the stuff above.)
1076     (goto-char point)
1077     (while (re-search-forward "[\000-\010\013-\037\200-\237]" nil t)
1078       (replace-match (format "\\%03o" (string-to-char (match-string 0)))
1079                      t t))))
1080
1081 ;;; Treatment of rejected articles.
1082 ;;; Bounced mail.
1083
1084 (defun gnus-summary-resend-bounced-mail (&optional fetch)
1085   "Re-mail the current message.
1086 This only makes sense if the current message is a bounce message than
1087 contains some mail you have written which has been bounced back to
1088 you.
1089 If FETCH, try to fetch the article that this is a reply to, if indeed
1090 this is a reply."
1091   (interactive "P")
1092   (gnus-summary-select-article t)
1093   (set-buffer gnus-original-article-buffer)
1094   (let ((gnus-message-setup-hook '(gnus-maybe-setup-default-charset)))
1095     (gnus-setup-message 'compose-bounce
1096       (let* ((references (mail-fetch-field "references"))
1097              (parent (and references (gnus-parent-id references))))
1098         (message-bounce)
1099         ;; If there are references, we fetch the article we answered to.
1100         (and fetch parent
1101              (gnus-summary-refer-article parent)
1102              (gnus-summary-show-all-headers))))))
1103
1104 ;;; Gcc handling.
1105
1106 ;; Do Gcc handling, which copied the message over to some group.
1107 (defun gnus-inews-do-gcc (&optional gcc)
1108   (interactive)
1109   (when (gnus-alive-p)
1110     (save-excursion
1111       (save-restriction
1112         (message-narrow-to-headers)
1113         (let ((gcc (or gcc (mail-fetch-field "gcc" nil t)))
1114               (coding-system-for-write 'raw-text)
1115               (output-coding-system 'raw-text)
1116               groups group method)
1117           (when gcc
1118             (message-remove-header "gcc")
1119             (widen)
1120             (setq groups (message-tokenize-header gcc " ,"))
1121             ;; Copy the article over to some group(s).
1122             (while (setq group (pop groups))
1123               (gnus-check-server
1124                (setq method
1125                      (cond ((and (null (gnus-get-info group))
1126                                  (eq (car gnus-message-archive-method)
1127                                      (car
1128                                       (gnus-server-to-method
1129                                        (gnus-group-method group)))))
1130                             ;; If the group doesn't exist, we assume
1131                             ;; it's an archive group...
1132                             gnus-message-archive-method)
1133                            ;; Use the method.
1134                            ((gnus-info-method (gnus-get-info group))
1135                             (gnus-info-method (gnus-get-info group)))
1136                            ;; Find the method.
1137                            (t (gnus-group-method group)))))
1138               (gnus-check-server method)
1139               (unless (gnus-request-group group t method)
1140                 (gnus-request-create-group group method))
1141               (save-excursion
1142                 (nnheader-set-temp-buffer " *acc*")
1143                 (insert-buffer-substring message-encoding-buffer)
1144                 (gnus-run-hooks 'gnus-before-do-gcc-hook)
1145                 (goto-char (point-min))
1146                 (when (re-search-forward
1147                        (concat "^" (regexp-quote mail-header-separator) "$")
1148                        nil t)
1149                   (replace-match "" t t ))
1150                 (unless (gnus-request-accept-article group method t t)
1151                   (gnus-message 1 "Couldn't store article in group %s: %s"
1152                                 group (gnus-status-message method))
1153                   (sit-for 2))
1154                 (kill-buffer (current-buffer))))))))))
1155
1156 (defun gnus-inews-insert-gcc ()
1157   "Insert Gcc headers based on `gnus-outgoing-message-group'."
1158   (save-excursion
1159     (save-restriction
1160       (message-narrow-to-headers)
1161       (let* ((group gnus-outgoing-message-group)
1162              (gcc (cond
1163                    ((gnus-functionp group)
1164                     (funcall group))
1165                    ((or (stringp group) (list group))
1166                     group))))
1167         (when gcc
1168           (insert "Gcc: "
1169                   (if (stringp gcc) gcc
1170                     (mapconcat 'identity gcc " "))
1171                   "\n"))))))
1172
1173 (defun gnus-inews-insert-archive-gcc (&optional group)
1174   "Insert the Gcc to say where the article is to be archived."
1175   (let* ((var gnus-message-archive-group)
1176          (group (or group gnus-newsgroup-name ""))
1177          (gcc-self-val
1178           (and gnus-newsgroup-name
1179                (not (equal gnus-newsgroup-name ""))
1180                (gnus-group-find-parameter
1181                 gnus-newsgroup-name 'gcc-self)))
1182          result
1183          (groups
1184           (cond
1185            ((null gnus-message-archive-method)
1186             ;; Ignore.
1187             nil)
1188            ((stringp var)
1189             ;; Just a single group.
1190             (list var))
1191            ((null var)
1192             ;; We don't want this.
1193             nil)
1194            ((and (listp var) (stringp (car var)))
1195             ;; A list of groups.
1196             var)
1197            ((gnus-functionp var)
1198             ;; A function.
1199             (funcall var group))
1200            (t
1201             ;; An alist of regexps/functions/forms.
1202             (while (and var
1203                         (not
1204                          (setq result
1205                                (cond
1206                                 ((stringp (caar var))
1207                                  ;; Regexp.
1208                                  (when (string-match (caar var) group)
1209                                    (cdar var)))
1210                                 ((gnus-functionp (car var))
1211                                  ;; Function.
1212                                  (funcall (car var) group))
1213                                 (t
1214                                  (eval (car var)))))))
1215               (setq var (cdr var)))
1216             result)))
1217          name)
1218     (when (or groups gcc-self-val)
1219       (when (stringp groups)
1220         (setq groups (list groups)))
1221       (save-excursion
1222         (save-restriction
1223           (message-narrow-to-headers)
1224           (goto-char (point-max))
1225           (insert "Gcc: ")
1226           (if gcc-self-val
1227               ;; Use the `gcc-self' param value instead.
1228               (progn
1229                 (insert
1230                  (if (stringp gcc-self-val)
1231                      gcc-self-val
1232                    group))
1233                 (if (not (eq gcc-self-val 'none))
1234                     (insert "\n")
1235                   (progn
1236                     (beginning-of-line)
1237                     (kill-line))))
1238             ;; Use the list of groups.
1239             (while (setq name (pop groups))
1240               (insert (if (string-match ":" name)
1241                           name
1242                         (gnus-group-prefixed-name
1243                          name gnus-message-archive-method)))
1244               (when groups
1245                 (insert " ")))
1246             (insert "\n")))))))
1247
1248 ;;; Posting styles.
1249
1250 (defun gnus-configure-posting-styles ()
1251   "Configure posting styles according to `gnus-posting-styles'."
1252   (unless gnus-inhibit-posting-styles
1253     (let ((group (or gnus-newsgroup-name ""))
1254           (styles gnus-posting-styles)
1255           style match variable attribute value v results
1256           filep name address element)
1257       ;; If the group has a posting-style parameter, add it at the end with a
1258       ;; regexp matching everything, to be sure it takes precedence over all
1259       ;; the others.
1260       (when gnus-newsgroup-name
1261         (let ((tmp-style (gnus-group-find-parameter group 'posting-style t)))
1262           (when tmp-style
1263             (setq styles (append styles (list (cons ".*" tmp-style)))))))
1264       ;; Go through all styles and look for matches.
1265       (dolist (style styles)
1266         (setq match (pop style))
1267         (goto-char (point-min))
1268         (when (cond
1269                ((stringp match)
1270                 ;; Regexp string match on the group name.
1271                 (string-match match group))
1272                ((eq match 'header)
1273                 (let ((header (message-fetch-field (pop style))))
1274                   (and header
1275                        (string-match (pop style) header))))
1276                ((or (symbolp match)
1277                     (gnus-functionp match))
1278                 (cond
1279                  ((gnus-functionp match)
1280                   ;; Function to be called.
1281                   (funcall match))
1282                  ((boundp match)
1283                   ;; Variable to be checked.
1284                   (symbol-value match))))
1285                ((listp match)
1286                 ;; This is a form to be evaled.
1287                 (eval match)))
1288           ;; We have a match, so we set the variables.
1289           (dolist (attribute style)
1290             (setq element (pop attribute)
1291                   variable nil
1292                   filep nil)
1293             (setq value
1294                   (cond
1295                    ((eq (car attribute) ':file)
1296                     (setq filep t)
1297                     (cadr attribute))
1298                    ((eq (car attribute) :value)
1299                     (cadr attribute))
1300                    (t
1301                     (car attribute))))
1302             ;; We get the value.
1303             (setq v
1304                   (cond
1305                    ((stringp value)
1306                     value)
1307                    ((or (symbolp value)
1308                         (gnus-functionp value))
1309                     (cond ((gnus-functionp value)
1310                            (funcall value))
1311                           ((boundp value)
1312                            (symbol-value value))))
1313                    ((listp value)
1314                     (eval value))))
1315             ;; Translate obsolescent value.
1316             (when (eq element 'signature-file)
1317               (setq element 'signature
1318                     filep t))
1319             ;; Get the contents of file elems.
1320             (when (and filep v)
1321               (setq v (with-temp-buffer
1322                         (insert-file-contents v)
1323                         (buffer-string))))
1324             (setq results (delq (assoc element results) results))
1325             (push (cons element v) results))))
1326       ;; Now we have all the styles, so we insert them.
1327       (setq name (assq 'name results)
1328             address (assq 'address results))
1329       (setq results (delq name (delq address results)))
1330       (make-local-variable 'message-setup-hook)
1331       (dolist (result results)
1332         (add-hook 'message-setup-hook
1333                   (cond
1334                    ((eq 'eval (car result))
1335                     'ignore)
1336                    ((eq 'body (car result))
1337                     `(lambda ()
1338                        (save-excursion
1339                          (message-goto-body)
1340                          (insert ,(cdr result)))))
1341                    ((eq 'signature (car result))
1342                     (set (make-local-variable 'message-signature) nil)
1343                     (set (make-local-variable 'message-signature-file) nil)
1344                     (if (not (cdr result))
1345                         'ignore
1346                       `(lambda ()
1347                          (save-excursion
1348                            (let ((message-signature ,(cdr result)))
1349                              (when message-signature
1350                                (message-insert-signature)))))))
1351                    (t
1352                     (let ((header
1353                            (if (symbolp (car result))
1354                                (capitalize (symbol-name (car result)))
1355                              (car result))))
1356                       `(lambda ()
1357                          (save-excursion
1358                            (message-remove-header ,header)
1359                            (let ((value ,(cdr result)))
1360                              (when value
1361                                (message-goto-eoh)
1362                                (insert ,header ": " value "\n"))))))))))
1363       (when (or name address)
1364         (add-hook 'message-setup-hook
1365                   `(lambda ()
1366                      (set (make-local-variable 'user-mail-address)
1367                           ,(or (cdr address) user-mail-address))
1368                      (let ((user-full-name ,(or (cdr name) (user-full-name)))
1369                            (user-mail-address
1370                             ,(or (cdr address) user-mail-address)))
1371                        (save-excursion
1372                          (message-remove-header "From")
1373                          (message-goto-eoh)
1374                          (insert "From: " (message-make-from) "\n")))))))))
1375
1376
1377 ;;; @ for MIME Edit mode
1378 ;;;
1379
1380 (defun gnus-maybe-setup-default-charset ()
1381   (let ((charset
1382          (and (boundp 'gnus-summary-buffer)
1383               (buffer-live-p gnus-summary-buffer)
1384               (save-excursion
1385                 (set-buffer gnus-summary-buffer)
1386                 default-mime-charset))))
1387     (if charset
1388         (progn
1389           (make-local-variable 'default-mime-charset)
1390           (setq default-mime-charset charset)
1391           ))))
1392
1393
1394 ;;; @ for MIME view mode
1395 ;;;
1396
1397 (defun gnus-following-method (buf)
1398   (gnus-setup-message 'reply-yank
1399     (set-buffer buf)
1400     (if (message-news-p)
1401         (message-followup)
1402       (message-reply nil 'wide))
1403     (let ((message-reply-buffer buf))
1404       (message-yank-original))
1405     (message-goto-body))
1406   (kill-buffer buf))
1407
1408
1409 ;;; Allow redefinition of functions.
1410
1411 (gnus-ems-redefine)
1412
1413 (provide 'gnus-msg)
1414
1415 ;;; gnus-msg.el ends here