Created
October 6, 2013 12:32
-
-
Save funrep/6853506 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns tob.irc | |
(:import (java.net Socket) | |
(java.io PrintWriter InputStreamReader BufferedReader))) | |
(def codetalk {:name "irc.codetalk.io" :port 6667}) | |
(def user {:name "tob the bot" :nick "tob"}) | |
(declare conn-handler) | |
(defn connect [server] | |
(let [socket (Socket. (:name server) (:port server)) | |
in (BufferedReader. (InputStreamReader. (.getInputStream socket))) | |
out (PrintWriter. (.getOutputStream socket)) | |
conn (ref {:in in :out out})] | |
(doto (Thread. #(conn-handler conn)) (.start)) | |
conn)) | |
(defn write [conn msg] | |
(doto (:out @conn) | |
(.println (str msg "\r")) | |
(.flush))) | |
(defn conn-handler [conn] | |
(while (nil? (:exit @conn)) | |
(let [msg (.readLine (:in @conn))] | |
(println msg) | |
(cond | |
(re-find #"^ERROR :Closing Link:" msg) | |
(dosync (alter conn merge {:exit true})) | |
(re-find #"^PING" msg) | |
(write conn (str "PONG " (re-find #":.*" msg))))))) | |
(defn login [conn user] | |
(write conn (str "NICK " (:nick user))) | |
(write conn (str "USER " (:nick user) " 0 * :" (:name user)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment