SHARE
TWEET

Untitled

a guest Jan 23rd, 2014 10 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns cljbot.irc
  2.   (:use [cljbot.config]
  3.         [cljbot.utils])
  4.   (:import (java.net Socket)
  5.            (java.io PrintWriter InputStreamReader BufferedReader)))
  6.  
  7. (defn write
  8.   "Sends a message through the supplied connection map."
  9.   [connection message]
  10.   (doto (connection :output)
  11.     (.print message)
  12.     (.flush)))
  13.  
  14. (defn writeln
  15.   "Sends an arbitrary number of lines through the supplied connection map."
  16.   [connection & lines]
  17.   (doseq [line lines]
  18.     (println ">" line)
  19.     (write connection
  20.            (str line
  21.                 ; (-> connection :host-info :line-sep)
  22.                 "\r\n"))))
  23.  
  24. (defn recv
  25.   "Receives a message from the supplied connection map."
  26.   [connection]
  27.   (.readLine (connection :input)))
  28.  
  29. (defn recvlns
  30.   "Receives a message from the supplied connection map, splitting it into separate lines."
  31.   [connection]
  32.   (clojure.string/split (recv connection) #"\r\n"))
  33.  
  34. (defn connect
  35.   "Returns an agent containing a connection map."
  36.   [server]
  37.   (def connection (Socket. ((first server) :name) ((first server) :port)))
  38.   (agent
  39.     {:socket connection
  40.      :input (BufferedReader. (InputStreamReader. (.getInputStream connection)))
  41.      :output (PrintWriter. (.getOutputStream connection))
  42.      :host-info (first server)
  43.      :channels (rest server)}))
  44.  
  45. (defn make-connections
  46.   "Attempts to establish a connection to each server specified in the config file."
  47.   []
  48.   (map connect (cfg :servers)))
  49.  
  50. (defn ping-respond
  51.   "If message line is a PING request, sends back a PONG request through the supplied connection map."
  52.   [connection line]
  53.   (writeln connection (str "PONG" (re-find #" :.*" line))))
  54.  
  55. (defn set-user
  56.   "Sets the USER and NICK of the IRC bot."
  57.   [connection]
  58.   (let [bot-nick (first (cfg :nick))]
  59.     (writeln connection
  60.              (str "NICK "
  61.                   bot-nick)
  62.              (str "USER "
  63.                   bot-nick
  64.                   " 0 * :"
  65.                   bot-nick))))
  66.  
  67. (defn set-mode
  68.   "Sets the MODE of the IRC bot."
  69.   [connection]
  70.   (writeln connection (str "MODE "
  71.                            (first (cfg :nick))
  72.                            " +B")))
  73.  
  74. (defn channel-join
  75.   "Joins the given channels on the supplied connection agent."
  76.   [connection & channels]
  77.   (writeln connection (str "JOIN " (clojure.string/join "," channels))))
  78.  
  79. (defn channel-join-default
  80.   "Joins the channels specified in config.clj for the supplied connection agent server."
  81.   [connection]
  82.   (apply channel-join connection (connection :channels)))
  83.  
  84. (defn init-connection
  85.   "Initializes a connection to a server."
  86.   [connection-agent]
  87.   (defn process-line [line]
  88.     (println line)
  89.     (let [line-words (words line)]
  90.       (cond
  91.         (not (nil? (re-find #"^PING" line))) (ping-respond connection line)
  92.         (= (second line-words) "001") (-> connection
  93.                                               (set-mode)
  94.                                               (channel-join-default)))))
  95.   (defn init-loop [connection]
  96.     (loop [lines (recvlns connection)]
  97.       (apply process-line lines)
  98.       (recur (recvlns connection)))
  99.     connection)
  100.   (send connection-agent (fn [connection]
  101.                            (set-user connection)
  102.                            connection))
  103.   (send connection-agent init-loop))
  104.  
  105. (defn init-connections
  106.   "Initializes a sequence of connection agents."
  107.   [connections]
  108.   (doseq [connection-agents connections] (init-connection connection-agents)))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top