Don't use old-style backquote in doc/ptexinfmt.el.
[riece] / lisp / riece-hangman.el
index bc48b76..426935e 100644 (file)
@@ -1,4 +1,4 @@
-;;; riece-hangman.el --- hangman
+;;; riece-hangman.el --- allow channel members to play the hangman game
 ;; Copyright (C) 1998-2004 Daiki Ueno
 
 ;; Author: Daiki Ueno <ueno@unixuser.org>
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
-;; To use, add the following line to your ~/.riece/init.el:
-;; (add-to-list 'riece-addons 'riece-hangman)
+;; NOTE: This is an add-on module for Riece.
 
 ;;; Code:
 
 (require 'riece-server)
 
 (defgroup riece-hangman nil
-  "Interface to hangman.el"
+  "Allow channel members to play the hangman game."
   :prefix "riece-"
   :group 'riece)
 
-(defcustom riece-hangman-hello-regexp "^, hangman"
+(defcustom riece-hangman-hello-regexp "^,hangman$"
   "Pattern of string to start the game."
   :type 'string
   :group 'riece-hangman)
 
-(defcustom riece-hangman-bye-regexp "^, bye hangman"
+(defcustom riece-hangman-bye-regexp "^,hangman bye$"
   "Pattern of string to end the game."
   :type 'string
   :group 'riece-hangman)
 (defvar riece-hangman-player-context-alist nil)
 (defvar riece-hangman-words-buffer nil)
 
+(defconst riece-hangman-description
+  "Allow channel members to play the hangman game.")
+
+(put 'riece-hangman 'riece-addon-default-disabled t)
+
 (defun riece-hangman-make-context (word)
   "Make an instance of player context object.
 This function is for internal use only."
@@ -89,17 +93,16 @@ This function is for internal use only."
 (defun riece-hangman-word ()
   "Return random word.
 The wordlist is read from `riece-hangman-words-file'."
-  (unless riece-hangman-words-buffer
+  (unless (and riece-hangman-words-buffer
+              (buffer-name riece-hangman-words-buffer))
     (setq riece-hangman-words-buffer (generate-new-buffer " *riece-hangman*"))
-    (save-excursion
-      (set-buffer riece-hangman-words-buffer)
+    (with-current-buffer riece-hangman-words-buffer
       (buffer-disable-undo)
       (insert-file-contents riece-hangman-words-file)
       (let ((case-fold-search nil))
-       (delete-non-matching-lines "^[a-z][a-z][a-z][a-z][a-z][a-z]"))))
-  (save-excursion
-    (set-buffer riece-hangman-words-buffer)
-    (goto-char (% (1+ (random)) (buffer-size)))
+       (delete-non-matching-lines "^[a-z][a-z][a-z][a-z][a-z][a-z]+"))))
+  (with-current-buffer riece-hangman-words-buffer
+    (goto-char (1+ (random (buffer-size))))
     (if (eobp)
        (beginning-of-line -1)
       (beginning-of-line))
@@ -133,86 +136,99 @@ The wordlist is read from `riece-hangman-words-file'."
               "")))))
 
 (defun riece-hangman-after-privmsg-hook (prefix string)
-  (let* ((user (riece-prefix-nickname prefix))
-        (parameters (riece-split-parameters string))
-        (targets (split-string (car parameters) ","))
-        (message (nth 1 parameters))
-        case-fold-search
-        pointer word guessed index)
-    (if (string-match riece-hangman-hello-regexp message)
-       (if (riece-identity-assoc user riece-hangman-player-context-alist t)
-           (riece-hangman-reply
-            (car targets)
-            (format "%s: You are already playing the game." user))
-         (let ((context (riece-hangman-make-context (riece-hangman-word))))
-           (setq riece-hangman-player-context-alist
-                 (cons (cons user context)
-                       riece-hangman-player-context-alist))
-           (riece-hangman-reply-with-context user (car targets) context)))
-      (if (string-match riece-hangman-bye-regexp message)
-         (when (setq pointer (riece-identity-assoc
-                              user riece-hangman-player-context-alist t))
-           (setq riece-hangman-player-context-alist
-                 (delq pointer riece-hangman-player-context-alist))
-           (riece-hangman-reply
-            (car targets)
-            (format "%s: Sorry, the word was \"%s\""
-                    user
-                    (riece-hangman-context-word (cdr pointer)))))
-       (if (setq pointer (riece-identity-assoc
-                          user riece-hangman-player-context-alist t))
-           (if (or (/= (length message) 1)
-                   (not (string-match "[a-z]" message)))
+  (if (get 'riece-hangman 'riece-addon-enabled)
+      (let* ((user (riece-prefix-nickname prefix))
+            (parameters (riece-split-parameters string))
+            (targets (split-string (car parameters) ","))
+            (message (nth 1 parameters))
+            case-fold-search
+            pointer word guessed index)
+       (if (string-match riece-hangman-hello-regexp message)
+           (if (riece-identity-assoc user riece-hangman-player-context-alist
+                                     t)
+               (riece-hangman-reply
+                (car targets)
+                (format "%s: You are already playing the game." user))
+             (let ((context (riece-hangman-make-context
+                             (riece-hangman-word))))
+               (setq riece-hangman-player-context-alist
+                     (cons (cons user context)
+                           riece-hangman-player-context-alist))
+               (riece-hangman-reply-with-context user (car targets) context)))
+         (if (string-match riece-hangman-bye-regexp message)
+             (when (setq pointer (riece-identity-assoc
+                                  user riece-hangman-player-context-alist t))
+               (setq riece-hangman-player-context-alist
+                     (delq pointer riece-hangman-player-context-alist))
                (riece-hangman-reply
                 (car targets)
-                (format "%s: Not a valid guess: %s" user message))
-             (if (memq (aref message 0)
-                       (riece-hangman-context-guessed (cdr pointer)))
-                 (riece-hangman-reply (car targets)
-                                      (format "%s: Already guessed '%c'"
-                                              user (aref message 0)))
-               (setq guessed (riece-hangman-context-set-guessed
-                              (cdr pointer)
-                              (cons (aref message 0)
-                                    (riece-hangman-context-guessed
-                                     (cdr pointer))))
-                     word (riece-hangman-context-word (cdr pointer)))
-               (unless (catch 'found
-                         (setq index 0)
-                         (while (< index (length word))
-                           (if (eq (aref word index) (aref message 0))
-                               (throw 'found t))
-                           (setq index (1+ index))))
-                 (riece-hangman-context-set-missed-count
-                  (cdr pointer)
-                  (1+ (riece-hangman-context-missed-count (cdr pointer)))))
-               (if (>= (riece-hangman-context-missed-count (cdr pointer)) 7)
-                   (progn
-                     (riece-hangman-reply
-                      (car targets)
-                      (format "%s: Sorry, the word was \"%s\""
-                              user
-                              (riece-hangman-context-word (cdr pointer))))
-                     (setq riece-hangman-player-context-alist
-                           (delq pointer
-                                 riece-hangman-player-context-alist)))
-                 (if (catch 'missing
-                       (setq index 0)
-                       (while (< index (length word))
-                         (unless (memq (aref word index) guessed)
-                           (throw 'missing t))
-                         (setq index (1+ index))))
-                     (riece-hangman-reply-with-context user (car targets)
-                                                       (cdr pointer))
-                   (riece-hangman-reply (car targets)
-                                        (format "%s: You got it!" user))
-                   (setq riece-hangman-player-context-alist
-                         (delq pointer
-                               riece-hangman-player-context-alist)))))))))))
+                (format "%s: Sorry, the word was \"%s\""
+                        user
+                        (riece-hangman-context-word (cdr pointer)))))
+           (if (setq pointer (riece-identity-assoc
+                              user riece-hangman-player-context-alist t))
+               (if (or (/= (length message) 1)
+                       (not (string-match "[a-z]" message)))
+                   (riece-hangman-reply
+                    (car targets)
+                    (format "%s: Not a valid guess: %s" user message))
+                 (if (memq (aref message 0)
+                           (riece-hangman-context-guessed (cdr pointer)))
+                     (riece-hangman-reply (car targets)
+                                          (format "%s: Already guessed '%c'"
+                                                  user (aref message 0)))
+                   (setq guessed (riece-hangman-context-set-guessed
+                                  (cdr pointer)
+                                  (cons (aref message 0)
+                                        (riece-hangman-context-guessed
+                                         (cdr pointer))))
+                         word (riece-hangman-context-word (cdr pointer)))
+                   (unless (catch 'found
+                             (setq index 0)
+                             (while (< index (length word))
+                               (if (eq (aref word index) (aref message 0))
+                                   (throw 'found t))
+                               (setq index (1+ index))))
+                     (riece-hangman-context-set-missed-count
+                      (cdr pointer)
+                      (1+ (riece-hangman-context-missed-count
+                           (cdr pointer)))))
+                   (if (>= (riece-hangman-context-missed-count (cdr pointer))
+                           7)
+                       (progn
+                         (riece-hangman-reply
+                          (car targets)
+                          (format "%s: Sorry, the word was \"%s\""
+                                  user
+                                  (riece-hangman-context-word (cdr pointer))))
+                         (setq riece-hangman-player-context-alist
+                               (delq pointer
+                                     riece-hangman-player-context-alist)))
+                     (if (catch 'missing
+                           (setq index 0)
+                           (while (< index (length word))
+                             (unless (memq (aref word index) guessed)
+                               (throw 'missing t))
+                             (setq index (1+ index))))
+                         (riece-hangman-reply-with-context user (car targets)
+                                                           (cdr pointer))
+                       (riece-hangman-reply (car targets)
+                                            (format "%s: You got it! (%s)"
+                                                    user word))
+                       (setq riece-hangman-player-context-alist
+                             (delq
+                              pointer
+                              riece-hangman-player-context-alist))))))))))))
 
 (defun riece-hangman-insinuate ()
   (add-hook 'riece-after-privmsg-hook 'riece-hangman-after-privmsg-hook))
 
+(defun riece-hangman-uninstall ()
+  (remove-hook 'riece-after-privmsg-hook 'riece-hangman-after-privmsg-hook))
+
+(defun riece-hangman-enable ()
+  (random t))
+
 (provide 'riece-hangman)
 
 ;;; riece-hangman.el ends here