|
From: lawrence m. <we...@gm...> - 2003-11-03 22:42:38
|
Since I'm meant to be doing proper work, I'm looking for ways to
procrastinate :).
I notice that most of ERC's CTCP handlers are defined almost
identically, so I wondered if it might be useful to create a
define-ctcp-handler. Here's a first stab at it. It only
handles simple cases at the moment, if people think it's worth
persuing I might make it try and handle complicated cases too.
(defmacro define-erc-ctcp-handler (command format-string &rest format-args)
"Define an ERC CTCP handler for COMMAND.
This creates functions and variables for dealing with CTCP requests
and responses.
FORMAT-STRING should be a string containing the response to be sent,
it should contain as many format directives as there are FORMAT-ARGS."
(let* ((command-name (upcase (symbol-name command)))
(query-hook (intern (format "erc-ctcp-query-%s-hook"
command-name)))
(query (intern (format "erc-ctcp-query-%s"
command-name)))
(reply-hook (intern (format "erc-ctcp-reply-%s-hook"
command-name)))
(reply (intern (format "erc-ctcp-reply-%s"
command-name)))
(reply-msg (intern (format "CTCP-%s" command-name)))
(query-msg (format "%s %s" command format-string)))
`(progn
(defvar ,query-hook (list ,query))
(defun ,query (proc nick login host to msg)
(unless erc-disable-ctcp-replies
(erc-send-ctcp-notice
nick (format ,query-msg ,@format-args)))
nil)
(defvar ,reply-hook (list ,reply))
(defun ,reply (proc nick login host to msg)
(erc-display-message
nil 'notice nil
',reply-msg
?n nick ?m msg)
nil))))
Sample usage:
(define-erc-ctcp-handler FNORD
"%s is a member of the FNORD society" (erc-current-nick))
This expands into:
(progn
(defvar erc-ctcp-query-FNORD-hook
(list erc-ctcp-query-FNORD))
(defun erc-ctcp-query-FNORD
(proc nick login host to msg)
(unless erc-disable-ctcp-replies
(erc-send-ctcp-notice nick
(format "FNORD %s is a member of the FNORD society"
(erc-current-nick))))
nil)
(defvar erc-ctcp-reply-FNORD-hook
(list erc-ctcp-reply-FNORD))
(defun erc-ctcp-reply-FNORD
(proc nick login host to msg)
(erc-display-message nil 'notice nil 'CTCP-FNORD 110 nick 109 msg)
nil))
--
lawrence mitchell <we...@gm...>
|