Skip to content

Commit

Permalink
Merge pull request #104 from noisesmith/privmsg-pr
Browse files Browse the repository at this point in the history
correct privmsg handling
  • Loading branch information
amalloy committed Nov 15, 2014
2 parents ed86a15 + a797823 commit 328f293
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
27 changes: 18 additions & 9 deletions src/lazybot/irc.clj
Expand Up @@ -23,15 +23,24 @@
(decorate
#(fn dispatch-hooks [irc-map event]
(dosync (alter irc-map assoc :server (:network @irc-map)))
(lazybot/call-all (-> @irc-map
(assoc :bot refzors :com irc-map)
(dissoc :irc)
(assoc :event event)
(assoc :bot-nick (:nick @irc-map))
(assoc :nick (:nick event))
(assoc :message (:text event))
(assoc :channel ((:params event) 0)))
%)))
(let [bot-nick (:nick @irc-map)
in-chan ((:params event) 0)
query? (= in-chan bot-nick)
channel (if query? (:nick event) in-chan)]
(clojure.pprint/pprint
{:bot-nick bot-nick
:channel channel
:query? query?
:event event})
(lazybot/call-all (-> @irc-map
(assoc :bot refzors :com irc-map)
(dissoc :irc)
(assoc :event event
:bot-nick bot-nick
:nick (:nick event)
:message (:text event)
:channel channel))
%))))
[:001 :privmsg :quit :join]))
refzors]))

Expand Down
50 changes: 23 additions & 27 deletions src/lazybot/registry.clj
Expand Up @@ -76,33 +76,29 @@
(get-in [:config (:server com) :user-blacklist])
(contains? (.toLowerCase nick))))

(defn try-handle [{:keys [nick bot-nick channel bot message] :as com-m}]
(when-not (ignore-message? com-m)
(on-thread
(let [conf (:config @bot)
query? (= channel bot-nick)
max-ops (:max-operations conf)
response-m (if query?
;; respond to the user sending request, not our own nick
(assoc com-m :channel nick :query? true)
(assoc com-m :query? false))]
(when (or (is-command? message (:prepends conf)) query?)
(if (dosync
(let [pending (:pending-ops @bot)
permitted (< pending max-ops)]
(when permitted
(alter bot assoc :pending-ops (inc pending)))))
(try
(let [n-bmap (into response-m (split-args conf message query?))]
(thunk-timeout #((respond n-bmap) n-bmap)
30 :sec))
(catch TimeoutException _
(send-message com-m "Execution timed out."))
(catch Exception e (.printStackTrace e))
(finally
(dosync
(alter bot assoc :pending-ops (dec (:pending-ops @bot))))))
(send-message com-m "Too much is happening at once. Wait until other operations cease.")))))))
(defn try-handle
"attempt to handle input from an IRC channel"
[{:keys [nick bot-nick channel bot message event query?] :as state}]
{:pre [(= nick (:nick event))]}
(when-not (ignore-message? state)
(let [{:keys [config pending-ops]} @bot
{:keys [max-operations prepends]} config
respond? (or (is-command? message prepends)
query?)
overtaxed? (>= pending-ops max-operations)]
(when respond?
(if overtaxed?
(send-message state "Too much is happening at once. Wait until other operations cease.")
(try
(dosync (alter bot update-in [:pending-ops] dec))
(let [state (into state (split-args config message query?))]
(thunk-timeout #((respond state) state)
30 :sec))
(catch TimeoutException _
(send-message state "Execution timed out."))
(catch Exception e (.printStackTrace e))
(finally
(dosync (alter bot update-in [:pending-ops] dec)))))))))

;; ## Plugin DSL
(defn merge-with-conj [& args]
Expand Down

0 comments on commit 328f293

Please sign in to comment.