Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/irclj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@

(defn quit
[irc]
(dosync (alter irc assoc :reconnect? false))
(connection/write-irc-line irc "QUIT"))

;; We fire our raw-log callback for the lines we read from IRC as well.
Expand All @@ -119,16 +120,27 @@
(events/fire irc :raw-log :read line)
(process/process-line (parser/parse line) irc))

(defn- reconnect [irc port ssl? timeout]
(let [host (:network @irc)]
(prn "Reconnecting...")
(dosync
(alter irc assoc
:connection (connection/create-connection host port ssl?)
:ready? (promise)))
(connection/set-timeout irc timeout)
(connection/register-connection irc)))

(defn connect
"Connect to IRC. Connects in another thread and returns a big fat ref of
data about the connection, you, and IRC in general."
[host port nick &
{:keys [pass timeout real-name mode username callbacks ssl?]
{:keys [pass timeout real-name mode username callbacks ssl? reconnect?]
:or {real-name "irclj", mode 0, ssl? false
callbacks {:raw-log events/stdout-callback}}
:as all}]
(let [{:keys [in] :as connection} (connection/create-connection host port ssl?)
irc (ref {:connection connection
:reconnect? reconnect?
:shutdown? false
:prefixes {}
:pass pass
Expand All @@ -149,7 +161,12 @@
(if-let [line (first lines)]
(do (process irc line)
(recur (rest lines)))
(events/fire irc :on-shutdown)))
(if (:reconnect? @irc)
(do (reconnect irc port ssl? timeout)
(.start (Thread. (fn [] (when (:ready? @irc)
(events/fire irc :on-reconnect)))))
(recur (connection/safe-line-seq (get-in @irc [:connection :in]))))
(events/fire irc :on-shutdown))))
(catch Exception e
(deliver (:ready? @irc) false) ;; unblock the promise
(events/fire irc :on-exception e)
Expand All @@ -160,4 +177,5 @@
(defn kill
"Close the socket associated with an IRC connection."
[irc]
(dosync (alter irc assoc :reconnect? false))
(.close (get-in @irc [:connection :socket])))