2000-12-20 09:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-ml.el
1 ;;; gnus-ml.el --- Mailing list minor mode for Gnus
2
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4
5 ;; Author: Julien Gilles  <jgilles@free.fr>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; implement (small subset of) RFC 2369
28
29 ;;; Usage:
30
31 ;; (add-hook 'gnus-summary-mode-hook 'turn-on-gnus-mailing-list-mode)
32
33 ;;; Code:
34
35 (require 'gnus)
36 (require 'gnus-msg)
37 (eval-when-compile (require 'cl))
38
39 ;;; Mailing list minor mode
40
41 (defvar gnus-mailing-list-mode nil
42   "Minor mode for providing mailing-list commands.")
43
44 (defvar gnus-mailing-list-mode-map nil)
45
46 (defvar gnus-mailing-list-menu)
47
48 (unless gnus-mailing-list-mode-map
49   (setq gnus-mailing-list-mode-map (make-sparse-keymap))
50
51   (gnus-define-keys gnus-mailing-list-mode-map
52     "\C-nh" gnus-mailing-list-help
53     "\C-ns" gnus-mailing-list-subscribe
54     "\C-nu" gnus-mailing-list-unsubscribe
55     "\C-np" gnus-mailing-list-post
56     "\C-no" gnus-mailing-list-owner
57     "\C-na" gnus-mailing-list-archive
58     ))
59
60 (defun gnus-mailing-list-make-menu-bar ()
61   (unless (boundp 'gnus-mailing-list-menu)
62     (easy-menu-define
63      gnus-mailing-list-menu gnus-mailing-list-mode-map ""
64      '("Mailing-Lists"
65        ["Get help" gnus-mailing-list-help t]
66        ["Subscribe" gnus-mailing-list-subscribe t]
67        ["Unsubscribe" gnus-mailing-list-unsubscribe t]
68        ["Post a message" gnus-mailing-list-post t]
69        ["Mail to owner" gnus-mailing-list-owner t]
70        ["Browse archive" gnus-mailing-list-archive t]))))
71
72 ;;;###autoload
73 (defun turn-on-gnus-mailing-list-mode ()
74   (when (gnus-group-get-parameter gnus-newsgroup-name 'to-list)
75     (gnus-mailing-list-mode 1)))
76
77 ;;;###autoload
78 (defun gnus-mailing-list-insinuate (&optional force)
79   "Setup group parameters from List-Post header.
80 If FORCE is non-nil, replace the old ones."
81   (interactive "P")
82   (let ((list-post 
83          (with-current-buffer gnus-original-article-buffer
84            (gnus-fetch-field "list-post"))))
85     (if list-post
86         (if (and (not force)
87                  (gnus-group-get-parameter gnus-newsgroup-name 'to-list))
88             (gnus-message 1 "to-list is non-nil.")
89           (if (string-match "<mailto:\\([^>]*\\)>" list-post)
90               (setq list-post (match-string 1 list-post)))
91           (gnus-group-add-parameter gnus-newsgroup-name 
92                                     (cons 'to-list list-post))
93           (gnus-mailing-list-mode 1))
94       (gnus-message 1 "no list-post in this message."))))
95
96 ;;;###autoload
97 (defun gnus-mailing-list-mode (&optional arg)
98   "Minor mode for providing mailing-list commands.
99
100 \\{gnus-mailing-list-mode-map}"
101   (interactive "P")
102   (when (eq major-mode 'gnus-summary-mode)
103     (when (set (make-local-variable 'gnus-mailing-list-mode)
104                (if (null arg) (not gnus-mailing-list-mode)
105                  (> (prefix-numeric-value arg) 0)))
106       ;; Set up the menu.
107       (when (gnus-visual-p 'mailing-list-menu 'menu)
108         (gnus-mailing-list-make-menu-bar))
109       (gnus-add-minor-mode 'gnus-mailing-list-mode " Mailing-List" gnus-mailing-list-mode-map)
110       (gnus-run-hooks 'gnus-mailing-list-mode-hook))))
111
112 ;;; Commands
113
114 (defun gnus-mailing-list-help ()
115   "Get help from mailing list server."
116   (interactive)  
117   (let ((list-help 
118          (with-current-buffer gnus-original-article-buffer
119            (gnus-fetch-field "list-help"))))
120     (cond (list-help (gnus-mailing-list-message list-help))
121           (t (gnus-message 1 "no list-help in this group")))))
122
123 (defun gnus-mailing-list-subscribe ()
124   "Subscribe"
125   (interactive)
126   (let ((list-subscribe 
127          (with-current-buffer gnus-original-article-buffer
128            (gnus-fetch-field "list-subscribe"))))
129     (cond (list-subscribe (gnus-mailing-list-message list-subscribe))
130           (t (gnus-message 1 "no list-subscribe in this group")))))
131
132 (defun gnus-mailing-list-unsubscribe ()
133   "Unsubscribe"
134   (interactive)
135   (let ((list-unsubscribe 
136          (with-current-buffer gnus-original-article-buffer
137            (gnus-fetch-field "list-unsubscribe"))))
138     (cond (list-unsubscribe (gnus-mailing-list-message list-unsubscribe))
139           (t (gnus-message 1 "no list-unsubscribe in this group")))))
140
141 (defun gnus-mailing-list-post ()
142   "Post message (really useful ?)"
143   (interactive)
144   (let ((list-post 
145          (with-current-buffer gnus-original-article-buffer
146            (gnus-fetch-field "list-post"))))
147     (cond (list-post (gnus-mailing-list-message list-post))
148           (t (gnus-message 1 "no list-post in this group")))))
149
150 (defun gnus-mailing-list-owner ()
151   "Mail to the owner"
152   (interactive)
153   (let ((list-owner 
154          (with-current-buffer gnus-original-article-buffer
155            (gnus-fetch-field "list-owner"))))
156     (cond (list-owner (gnus-mailing-list-message list-owner))
157           (t (gnus-message 1 "no list-owner in this group")))))
158
159 (defun gnus-mailing-list-archive ()
160   "Browse archive"
161   (interactive)
162   (require 'browse-url)
163   (let ((list-archive 
164          (with-current-buffer gnus-original-article-buffer
165            (gnus-fetch-field "list-archive"))))
166     (cond (list-archive 
167            (if (string-match "<\\(http:[^>]*\\)>" list-archive)
168                (browse-url (match-string 1 list-archive))
169              (browse-url list-archive)))
170           (t (gnus-message 1 "no list-archive in this group")))))
171
172 ;;; Utility functions
173
174 (defun gnus-mailing-list-message (address)
175   ""
176   (let ((mailto  "")
177         (to ())
178         (subject "None")
179         (body "")
180         )
181     (cond 
182      ((string-match "<mailto:\\([^>]*\\)>" address)
183       (let ((args (match-string 1 address)))
184         (cond                                   ; with param
185          ((string-match "\\(.*\\)\\?\\(.*\\)" args)
186           (setq mailto (match-string 1 args))
187           (let ((param (match-string 2 args)))
188             (if (string-match "subject=\\([^&]*\\)" param)
189                 (setq subject (match-string 1 param)))
190             (if (string-match "body=\\([^&]*\\)" param)
191                 (setq body (match-string 1 param)))
192             (if (string-match "to=\\([^&]*\\)" param)
193                 (push (match-string 1 param) to))
194             ))   
195          (t (setq mailto args)))))                      ; without param
196      
197      ; other case <http://... to be done.
198      (t nil))
199     (gnus-setup-message 'message (message-mail mailto subject))
200     (insert body)
201     ))
202
203 (provide 'gnus-ml)
204
205 ;;; gnus-ml.el ends here