diff options
| author | John Wiegley <johnw@newartisans.com> | 2002-04-09 01:11:49 +0000 |
|---|---|---|
| committer | John Wiegley <johnw@newartisans.com> | 2002-04-09 01:11:49 +0000 |
| commit | a62a682bc83a3146899865d226552d47ce77648d (patch) | |
| tree | f757484e29a2b0dc6e5dc6a982605fc52dc68d41 | |
| parent | 857af45023d64bf66c6c62a3054df240ff64e292 (diff) | |
added an IRC bot engine
| -rw-r--r-- | chess-engine.el | 7 | ||||
| -rw-r--r-- | chess-irc.el | 119 | ||||
| -rw-r--r-- | chess-network.el | 8 |
3 files changed, 128 insertions, 6 deletions
diff --git a/chess-engine.el b/chess-engine.el index 1f4c9c9..ad163c0 100644 --- a/chess-engine.el +++ b/chess-engine.el @@ -26,9 +26,11 @@ (make-variable-buffer-local 'chess-engine-position) (make-variable-buffer-local 'chess-engine-game) +(defvar chess-engine-process nil) (defvar chess-engine-last-pos nil) (defvar chess-engine-working nil) +(make-variable-buffer-local 'chess-engine-process) (make-variable-buffer-local 'chess-engine-last-pos) (make-variable-buffer-local 'chess-engine-working) @@ -84,6 +86,7 @@ (when (processp proc) (unless (memq (process-status proc) '(run open)) (error "Failed to start chess engine process")) + (setq chess-engine-process proc) (set-process-buffer proc (current-buffer)) (set-process-filter proc 'chess-engine-filter)) (setq chess-engine-current-marker (point-marker))) @@ -160,7 +163,7 @@ (defun chess-engine-send (engine string) "Send the given STRING to ENGINE." (chess-with-current-buffer engine - (let ((proc (get-buffer-process (current-buffer)))) + (let ((proc chess-engine-process)) (if proc (if (memq (process-status proc) '(run open)) (process-send-string proc string) @@ -170,7 +173,7 @@ (defun chess-engine-submit (engine string) "Submit the given STRING, so ENGINE sees it in its input stream." (chess-with-current-buffer engine - (let ((proc (get-buffer-process (current-buffer)))) + (let ((proc chess-engine-process)) (if (and (processp proc) (not (memq (process-status proc) '(run open)))) (error "The engine you were using is no longer running")) diff --git a/chess-irc.el b/chess-irc.el new file mode 100644 index 0000000..7f432dd --- /dev/null +++ b/chess-irc.el @@ -0,0 +1,119 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; This transport uses an IRC bot to send/receive moves. +;; +;; $Revision$ + +(require 'chess-network) + +(defgroup chess-irc nil + "Use an IRC bot for sending/receiving moves." + :group 'chess-engine) + +(defcustom chess-irc-host "irc.openprojects.net" + "" + :type 'string + :group 'chess-irc) + +(defcustom chess-irc-port 6667 + "" + :type 'string + :group 'chess-irc) + +(defcustom chess-irc-nick "jwchess" + "" + :type 'string + :group 'chess-irc) + +(defvar chess-irc-regexp-alist chess-network-regexp-alist) + +(defvar chess-irc-process) +(defvar chess-irc-engine) +(defvar chess-irc-opponent) +(defvar chess-irc-working nil) +(defvar chess-irc-last-pos nil) + +(make-variable-buffer-local 'chess-irc-process) +(make-variable-buffer-local 'chess-irc-engine) +(make-variable-buffer-local 'chess-irc-opponent) +(make-variable-buffer-local 'chess-irc-working) +(make-variable-buffer-local 'chess-irc-last-pos) + +(defun chess-irc-handler (event &rest args) + "This is an example of a generic transport engine." + (cond + ((eq event 'initialize) + (message "Connecting to IRC server '%s:%d'..." + chess-irc-host chess-irc-port) + (let ((engine (current-buffer)) proc) + (with-current-buffer (generate-new-buffer "*chess-irc*") + (setq chess-irc-engine engine + proc (open-network-stream "*chess-irc*" (current-buffer) + chess-irc-host chess-irc-port)) + (message "Connected, now logging in as '%s'..." chess-irc-nick) + (when (and proc (eq (process-status proc) 'open)) + (process-send-string proc (format "USER %s 0 * :%s\n" + (user-login-name) + (user-full-name))) + (process-send-string proc (format "NICK %s\n" chess-irc-nick)) + (set-process-filter proc 'chess-irc-filter) + (set-process-buffer proc (current-buffer)) + (set-marker (process-mark proc) (point)) + (message "Now ready to accept protocol" chess-irc-nick))) + (setq chess-irc-process proc)) + nil) + + ((eq event 'shutdown) + (ignore-errors + (process-send-string chess-irc-process "QUIT :Goodbye\n")) + (ignore-errors + (kill-buffer (process-buffer chess-irc-process)))) + + ((eq event 'send) + (process-send-string chess-irc-process + (format "PRIVMSG %s :%s\n" + chess-irc-opponent (car args)))) + (t + (apply 'chess-network-handler event args)))) + +;; This filter translates IRC syntax into basic chess-network protocol +(defun chess-irc-filter (proc string) + (let ((buf (process-buffer proc))) + (when (buffer-live-p buf) + (with-current-buffer buf + (let ((moving (= (point) (process-mark proc)))) + (save-excursion + ;; Insert the text, advancing the marker. + (goto-char (process-mark proc)) + (insert string) + (set-marker (process-mark proc) (point))) + (if moving (goto-char (process-mark proc)))) + (unless chess-irc-working + (setq chess-irc-working t) + (unwind-protect + (progn + (if chess-irc-last-pos + (goto-char chess-irc-last-pos) + (goto-char (point-min))) + (beginning-of-line) + (while (not (eobp)) + (cond + ((looking-at + ":\\([^ \t\n!]+\\)!\\S-+ PRIVMSG \\(\\S-+\\) :\\(.+\\)") + (let ((sender (match-string 1)) + (target (match-string 2)) + (msg (match-string 3))) + (with-current-buffer chess-irc-engine + (when (and (string= chess-irc-nick target) + (or (null chess-irc-opponent) + (string= chess-irc-opponent sender))) + (unless chess-irc-opponent + (setq chess-irc-opponent sender)) + (chess-engine-submit nil (concat msg "\n"))))))) + (forward-line))) + (setq chess-irc-last-pos (point) + chess-irc-working nil))))))) + +(provide 'chess-irc) + +;;; chess-irc.el ends here diff --git a/chess-network.el b/chess-network.el index 8c4f558..af833ee 100644 --- a/chess-network.el +++ b/chess-network.el @@ -35,10 +35,10 @@ (defun chess-network-perform-move () (let* ((move (match-string 1)) (ply (chess-algebraic-to-ply (chess-engine-position nil) move))) - (unless ply - (error "Could not convert engine move: %s" move)) - (let ((chess-network-now-moving t)) - (funcall chess-engine-response-handler 'move ply)))) + (if ply + (let ((chess-network-now-moving t)) + (funcall chess-engine-response-handler 'move ply)) + (message "Received invalid move: %s" move)))) (defun chess-network-handler (event &rest args) "Initialize the network chess engine." |
