Problem of the Day
A new programming or logic puzzle every Mon-Fri


Hooray for Monday!

Today's problem is a fun one. We'll be creating an IRC bot. Your bot can connect to any channel and on any network ( is a good place to start if you're unsure). The goal of this bot is to print to console everything that is said in the irc channel. For bonus points have your both listen in on two channels and print both channels to the console.



  • Driphter - 8¬†years, 11¬†months ago


    (import ( Socket)
             ( PrintWriter InputStreamReader BufferedReader))
    (defn- write
      [conn & msg]
      (doto (:out @conn)
        (.println (apply str (conj msg "\r")))
    (defn- edit-conn
      [conn key value]
      (dosync (alter conn assoc key value))
    (defn- conn-handler
      (while (not (:exit @conn))
        (let [msg (.readLine (:in @conn))]
            (re-find #"^:[^ ]* 004" msg)
            (edit-conn conn :connected true)
            (re-find #"^:[^ :]* PRIVMSG #" msg)
            (let [[_ nick chan msg] (re-find #"^:([^!]*)!.*PRIVMSG ([^ ]*) :(.*)" msg)]
              (println (str chan " " nick ": " msg)))
            (re-find #"^ERROR :Closing Link:" msg)
            (edit-conn conn :exit true)
            (re-find #"^PING" msg)
            (write conn (str "PONG " (re-find #":.*" msg)))))))
    (defn- connect
      [server port]
      (let [socket (Socket. (str server) (int port))
            in     (BufferedReader. (InputStreamReader. (.getInputStream socket)))
            out    (PrintWriter. (.getOutputStream socket))
            conn   (ref {:in in :out out})
            thread (Thread. ^Runnable #(conn-handler conn))]
        (.start thread)
        (edit-conn conn :handler thread)))
    (defn- login
      [conn nick]
      (write conn "NICK " nick)
      (write conn "USER " nick " 0 * :" nick))
    (defn- await-conn
      [conn timeout]
      (let [wait (atom 0)]
        (while (and (not (:connected @conn))
                    (not (:exit @conn)))
          (if (< @wait timeout)
            (do (Thread/sleep 100)
                (swap! wait + 100))
            (edit-conn conn :exit true)))))
    (defn- sanitize-channel
      (if-not (.startsWith channel "#")
        (str "#" channel)
    (defn- join
      [conn channel]
      (write conn "JOIN " (sanitize-channel channel)))
    (defn start-listening
      [server port nick & channels]
      (let [conn (connect server port)]
        (login conn nick)
        (println "Connecting...")
        (await-conn conn 15000)
        (println "Connected...")
        (doseq [channel channels]
          (join conn channel))))

    reply permalink

Content curated by @MaxBurstein