Browse files

change hooks to replace instead of add

  • Loading branch information...
1 parent 8ed4687 commit 7db2c458067d07cacbac93c6a685e399ce288a19 @danlarkin danlarkin committed Dec 2, 2010
Showing with 17 additions and 23 deletions.
  1. +3 −5 src/subrosa/commands.clj
  2. +9 −12 src/subrosa/hooks.clj
  3. +5 −6 src/subrosa/plugins/logging.clj
View
8 src/subrosa/commands.clj
@@ -1,14 +1,12 @@
(ns subrosa.commands
(:use [subrosa.client]
- [subrosa.hooks :only [reset-get-hook! add-hook hooked? run-hook]]
+ [subrosa.hooks :only [add-hook hooked? run-hook]]
[subrosa.utils :only [interleave-all]]
[subrosa.config :only [config]]
[clojure.string :only [join]]
[clojure.contrib.condition :only [raise]])
(:import [org.jboss.netty.channel ChannelFutureListener]))
-(reset-get-hook!)
-
(defn dispatch-message [message channel]
(let [[cmd args] (seq (.split message " " 2))]
(if (hooked? cmd)
@@ -30,12 +28,12 @@
(defmacro defcommand*
"Define a command which can be called by unauthenticated users."
[cmd & fn-tail]
- `(add-hook '~cmd (fn ~@(fix-args false fn-tail))))
+ `(add-hook ::commands '~cmd (fn ~@(fix-args false fn-tail))))
(defmacro defcommand
"Define a command which requires its user to be authenticated."
[cmd & fn-tail]
- `(add-hook '~cmd (fn ~@(fix-args true fn-tail))))
+ `(add-hook ::commands '~cmd (fn ~@(fix-args true fn-tail))))
(defn valid-nick-character? [character]
(and (not (Character/isWhitespace character))
View
21 src/subrosa/hooks.clj
@@ -1,21 +1,18 @@
(ns subrosa.hooks)
-(declare get-hook)
+(defonce get-hook
+ (comp
+ (memoize
+ (fn [hook] (atom {})))
+ (memfn toLowerCase)
+ str))
-(defn reset-get-hook! []
- (def get-hook
- (comp
- (memoize
- (fn [hook] (atom #{})))
- (memfn toLowerCase)
- str)))
-
-(defn add-hook [hook fn]
- (swap! (get-hook hook) conj fn))
+(defn add-hook [tag hook fn]
+ (swap! (get-hook hook) assoc tag fn))
(defn hooked? [hook]
(not (empty? @(get-hook hook))))
(defn run-hook [hook & args]
- (doseq [listener @(get-hook hook)]
+ (doseq [[tag listener] @(get-hook hook)]
(apply listener hook args)))
View
11 src/subrosa/plugins/logging.clj
@@ -66,9 +66,8 @@
(when (config :logging :directory)
(apply log-dispatch args)))
-(add-hook 'privmsg-room-hook log)
-(add-hook 'join-hook log)
-(add-hook 'part-hook log)
-(add-hook 'quit-hook log)
-(add-hook 'nick-hook log)
-
+(add-hook ::logging 'privmsg-room-hook log)
+(add-hook ::logging 'join-hook log)
+(add-hook ::logging 'part-hook log)
+(add-hook ::logging 'quit-hook log)
+(add-hook ::logging 'nick-hook log)

0 comments on commit 7db2c45

Please sign in to comment.