Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns cljbot.irc
- (:use [cljbot.config]
- [cljbot.utils])
- (:import (java.net Socket)
- (java.io PrintWriter InputStreamReader BufferedReader)))
- (defn write
- "Sends a message through the supplied connection map."
- [connection message]
- (doto (connection :output)
- (.print message)
- (.flush)))
- (defn writeln
- "Sends an arbitrary number of lines through the supplied connection map."
- [connection & lines]
- (doseq [line lines]
- (println ">" line)
- (write connection
- (str line
- ; (-> connection :host-info :line-sep)
- "\r\n"))))
- (defn recv
- "Receives a message from the supplied connection map."
- [connection]
- (.readLine (connection :input)))
- (defn recvlns
- "Receives a message from the supplied connection map, splitting it into separate lines."
- [connection]
- (clojure.string/split (recv connection) #"\r\n"))
- (defn connect
- "Returns an agent containing a connection map."
- [server]
- (def connection (Socket. ((first server) :name) ((first server) :port)))
- (agent
- {:socket connection
- :input (BufferedReader. (InputStreamReader. (.getInputStream connection)))
- :output (PrintWriter. (.getOutputStream connection))
- :host-info (first server)
- :channels (rest server)}))
- (defn make-connections
- "Attempts to establish a connection to each server specified in the config file."
- []
- (map connect (cfg :servers)))
- (defn ping-respond
- "If message line is a PING request, sends back a PONG request through the supplied connection map."
- [connection line]
- (writeln connection (str "PONG" (re-find #" :.*" line))))
- (defn set-user
- "Sets the USER and NICK of the IRC bot."
- [connection]
- (let [bot-nick (first (cfg :nick))]
- (writeln connection
- (str "NICK "
- bot-nick)
- (str "USER "
- bot-nick
- " 0 * :"
- bot-nick))))
- (defn set-mode
- "Sets the MODE of the IRC bot."
- [connection]
- (writeln connection (str "MODE "
- (first (cfg :nick))
- " +B")))
- (defn channel-join
- "Joins the given channels on the supplied connection agent."
- [connection & channels]
- (writeln connection (str "JOIN " (clojure.string/join "," channels))))
- (defn channel-join-default
- "Joins the channels specified in config.clj for the supplied connection agent server."
- [connection]
- (apply channel-join connection (connection :channels)))
- (defn init-connection
- "Initializes a connection to a server."
- [connection-agent]
- (defn process-line [line]
- (println line)
- (let [line-words (words line)]
- (cond
- (not (nil? (re-find #"^PING" line))) (ping-respond connection line)
- (= (second line-words) "001") (-> connection
- (set-mode)
- (channel-join-default)))))
- (defn init-loop [connection]
- (loop [lines (recvlns connection)]
- (apply process-line lines)
- (recur (recvlns connection)))
- connection)
- (send connection-agent (fn [connection]
- (set-user connection)
- connection))
- (send connection-agent init-loop))
- (defn init-connections
- "Initializes a sequence of connection agents."
- [connections]
- (doseq [connection-agents connections] (init-connection connection-agents)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement