;;; elmo-nntp-control.el

;; Copyright (C) 2001 Kenichi OKADA <okada@opaopa.org>

;; Author: Kenichi OKADA <okada@opaopa.org>

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.

;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; usage:
;;
;; (require 'elmo-nntp-control)
;;
;; M-x elmo-nntp-control-newgroup
;; alt.hoge.hoge   <-- newsgroup
;; group-admin@isc.org   <-- moderotor
;;
;; or
;; M-x elmo-nntp-control-rmgroup
;;
;; Also you use from wl-fldmgr.
;; at folder mode,
;; m a -alt.hoge.hoge
;; m d     <-- wl-fldmgr-delete (elmo-nntp-control-rmgroup)
;;
;; If you want to ommit inputing "moderator",
;; set 'elmo-nntp-moderators-list' as follows.
;;
;; (setq elmo-nntp-moderators-list
;;    '(("local\\." . "news@localhost.localdomain")
;;	("comp\\." . "group-admin@isc.org")))
;;
;; If you do not see your creating newsgroup, try next.
;; (setq elmo-nntp-list-folders-use-cache nil)
;;

;;; Code:

(require 'elmo-nntp)

(defvar elmo-nntp-control-moderators-list nil)

(defun elmo-nntp-control-newgroup (&optional newsgroup moderator)
  "Send a control message for newgroup."
  (interactive)
  (if (and (or newsgroup
	       (and (interactive-p)
		    (setq newsgroup (completing-read
				     "Newsgroup? " elmo-newsgroups-hashtb)))
	       (error "No newsgroup"))
	   (or moderator
	       (setq moderator (cdr (elmo-string-matched-assoc
				     newsgroup elmo-nntp-control-moderators-list)))
	       (and (interactive-p)
		    (setq moderator (read-from-minibuffer "moderator? ")))
	       (error "No moderator")))
      (elmo-nntp-control-send "newgroup" newsgroup moderator)))

(defun elmo-nntp-control-rmgroup (&optional newsgroup moderator)
  "Send a control message for rmgroup."
  (interactive)
  (if (and (or newsgroup
	       (and (interactive-p)
		    (setq newsgroup (completing-read
				     "Newsgroup? " elmo-newsgroups-hashtb)))
	       (error "No newsgroup"))
	   (or moderator
	       (setq moderator (cdr (elmo-string-matched-assoc
				     newsgroup elmo-nntp-control-moderators-list)))
	       (and (interactive-p)
		    (setq moderator (read-from-minibuffer "moderator? ")))
	       (error "No moderator")))
      (elmo-nntp-control-send "rmgroup" newsgroup moderator)))

(defun elmo-nntp-control-send (control newsgroup moderator)
  (save-excursion
    (let (result buf)
      ;; Make control message.
      (setq buf (set-buffer (get-buffer-create " *message control*")))
      (buffer-disable-undo (current-buffer))
      (erase-buffer)
      (insert "Newsgroups: " newsgroup "\n"
	      "From: " moderator "\n"
	      "Approved: " moderator "\n"
	      "Subject: cmsg " control " " newsgroup "\n"
	      "Control: " control " " newsgroup "\n"
	      mail-header-separator "\n"
	      "This is a request for " control ".\n")
      (message "Sending a control %s ..." control)
      (unwind-protect
	  (progn
	    (elmo-nntp-post elmo-nntp-default-server buf)
	    (message "Sending a control %s ...done" control)
	    (setq elmo-nntp-list-folders-cache nil)
	      (setq result t))
	(kill-buffer buf))
      result)))

;; not used
(luna-define-method elmo-folder-creatable-p ((folder elmo-nntp-folder))
  t)

(luna-define-method elmo-folder-create ((folder elmo-nntp-folder))
  (let ((newsgroup (elmo-nntp-folder-group-internal folder)))
    (elmo-nntp-control-newgroup newsgroup)))

(luna-define-method elmo-folder-delete ((folder elmo-nntp-folder))
  (let ((newsgroup (elmo-nntp-folder-group-internal folder)))
    (elmo-nntp-control-rmgroup newsgroup)))

(provide 'elmo-nntp-control)

;;; elmo-nntp-control.el ends here