aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/erc/erc-chess.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/erc/erc-chess.el')
-rw-r--r--lisp/erc/erc-chess.el181
1 files changed, 181 insertions, 0 deletions
diff --git a/lisp/erc/erc-chess.el b/lisp/erc/erc-chess.el
new file mode 100644
index 00000000000..94715439c99
--- /dev/null
+++ b/lisp/erc/erc-chess.el
@@ -0,0 +1,181 @@
1;;; erc-chess.el --- CTCP chess playing support for ERC
2
3;; Copyright (C) 2002, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5;; Author: Mario Lang <mlang@delysid.org>
6;; Keywords: games, comm
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 3, 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., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
24
25;;; Commentary:
26
27;; This module requires chess.el by John Wiegley.
28;; You need to have chess.el installed (load-path properly set)
29
30;;; Code:
31
32(require 'erc)
33(require 'chess-network)
34(require 'chess-display)
35(require 'chess)
36
37;;;; Variables
38
39(defgroup erc-chess nil
40 "Playing chess over IRC."
41 :group 'erc)
42
43(defcustom erc-chess-verbose-flag nil
44 "*If non-nil, inform about bogus CTCP CHESS messages in the server buffer."
45 :group 'erc-chess
46 :type 'boolean)
47
48(defcustom erc-chess-debug-flag t
49 "*If non-nil, print all chess CTCP messages received in the server buffer."
50 :group 'erc-chess
51 :type 'boolean)
52
53;;;###autoload
54(defvar erc-ctcp-query-CHESS-hook '(erc-chess-ctcp-query-handler))
55
56(defvar erc-chess-alist nil
57 "Alist of chess sessions. It has the form of (NICK ENGINE)")
58(make-variable-buffer-local 'erc-chess-alist)
59
60(defvar erc-chess-regexp-alist chess-network-regexp-alist)
61(defvar erc-chess-partner)
62(make-variable-buffer-local 'erc-chess-partner)
63
64;;;; Catalog messages
65
66(erc-define-catalog
67 'english
68 '((ctcp-chess-debug . "CTCPchess: %n (%u@%h) sent: '%m'")
69 (ctcp-chess-quit . "Chess game with %n (%u@%h) quit")))
70
71
72(defun erc-chess-response-handler (event &rest args)
73 (when (and (eq event 'accept)
74 (eq chess-engine-pending-offer 'match))
75 (let ((display (chess-game-data (chess-engine-game nil) 'display)))
76 (chess-display-enable-popup display)
77 (chess-display-popup display)))
78
79 (apply 'chess-engine-default-handler event args))
80
81
82(defun erc-chess-handler (game event &rest args)
83 "Handle erc-chess events.
84This is the main handler for the erc-chess module."
85 (cond
86 ((eq event 'initialize)
87 (setq erc-chess-partner (car args))
88 (setq erc-server-process (nth 1 args))
89 t)
90
91 ((eq event 'send)
92 ;; Transmit the string given in `(car args)' to the nick
93 ;; saved in `erc-chess-partner'.
94 (let ((nick erc-chess-partner)
95 (msg (substring (car args) 0 (1- (length (car args))))))
96 (erc-with-server-buffer
97 (erc-send-ctcp-message nick (concat "CHESS " msg) t))))
98
99 (t
100 (cond
101 ((eq event 'accept)
102 (let ((display (chess-game-data (chess-engine-game nil) 'display)))
103 (chess-display-enable-popup display)
104 (chess-display-popup display)))
105
106 ((eq event 'destroy)
107 (let* ((buf (process-buffer erc-server-process))
108 (nick (erc-downcase erc-chess-partner))
109 (engine (current-buffer)))
110 (erc-with-server-buffer
111 (let ((elt (assoc nick erc-chess-alist)))
112 (when (and elt (eq (nth 1 elt) engine))
113 (message "Removed from erc-chess-alist in destroy event")
114 (setq erc-chess-alist (delq elt erc-chess-alist))))))))
115
116 ;; Pass all other events down to chess-network
117 (apply 'chess-network-handler game event args))))
118
119;;;; Game initialisation
120
121(defun erc-chess-engine-create (nick)
122 "Initialize a game for a particular nick.
123This function adds to `erc-chess-alist' too."
124 ;; Maybe move that into the connect callback?
125 (let* ((objects (chess-session 'erc-chess t 'erc-chess-response-handler
126 nick erc-server-process))
127 (engine (car objects))
128 (display (cadr objects)))
129 (when engine
130 (if display
131 (chess-game-set-data (chess-display-game display)
132 'display display))
133 (push (list (erc-downcase nick) engine) erc-chess-alist)
134 engine)))
135
136;;;; IRC /commands
137
138;;;###autoload
139(defun erc-cmd-CHESS (line &optional force)
140 "Initiate a chess game via CTCP to NICK.
141NICK should be the first and only arg to /chess"
142 (cond
143 ((string-match (concat "^\\s-*\\(" erc-valid-nick-regexp "\\)\\s-*$") line)
144 (let ((nick (match-string 1 line)))
145 (erc-with-server-buffer
146 (if (assoc (erc-downcase nick) erc-chess-alist)
147 ;; Maybe check for correctly connected game, and switch here.
148 (erc-display-message
149 nil 'notice 'active
150 (concat "Invitation for a game already sent to " nick))
151 (with-current-buffer (erc-chess-engine-create nick)
152 (erc-chess-handler nil 'match)
153 t)))))
154 (t nil)))
155
156;;; CTCP handler
157;;;###autoload
158(defun erc-chess-ctcp-query-handler (proc nick login host to msg)
159 (if erc-chess-debug-flag
160 (erc-display-message
161 nil 'notice (current-buffer)
162 'ctcp-chess-debug ?n nick ?m msg ?u login ?h host))
163 (when (string-match "^CHESS\\s-+\\(.*\\)$" msg)
164 (let ((str (concat (match-string 1 msg) "\n"))
165 (elt (assoc (erc-downcase nick) erc-chess-alist)))
166 (if (not elt)
167 (chess-engine-submit (erc-chess-engine-create nick) str)
168 (if (buffer-live-p (nth 1 elt))
169 (chess-engine-submit (nth 1 elt) str)
170 (setq erc-chess-alist (delq elt erc-chess-alist)))))))
171
172(provide 'erc-chess)
173
174;;; erc-chess.el ends here
175;;
176;; Local Variables:
177;; indent-tabs-mode: t
178;; tab-width: 8
179;; End:
180
181;; arch-tag: beb148d1-db16-48da-8145-9f3a7ff27b7b