Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 10th, 2012  |  syntax: Clojure  |  size: 4.17 KB  |  views: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (ns IRC-Client.core
  2.   (:use [seesaw.core])
  3.   (:require [seesaw.selector :as selector])
  4.   (:import (java.net Socket)
  5.            (java.io PrintWriter InputStreamReader BufferedReader)
  6.            (panels SettingsFrame ChatWindow)))
  7.  
  8. (defn identify [root]
  9.   (doseq [w (select root [:*])]
  10.     (if-let [n (.getName w)]
  11.       (selector/id-of! w (keyword n))))
  12.   root)
  13.  
  14. (def defaults
  15.   { :Nickname "IRC-Client-Test"
  16.     :Server "irc.freenode.net"
  17.     :Port 6667
  18.     :Channel "#clojurehu"})
  19.  
  20. (def textbox (text :multi-line? false :border 5 :margin 0))
  21. (def textboxoutput (text :multi-line? true :border 5 :margin 0 ))
  22. (def namesList (text :multi-line? true :border 5 :margin 0 ))
  23.  
  24. (defn login-form []
  25.   (let [form (identify (SettingsFrame.))]
  26.     form)
  27.   )
  28.  
  29. (defn chatform []
  30.   (let [form (identify (ChatWindow.))]
  31.     form)
  32.   )
  33.  
  34. (listen (select chatform [:#btnSend])
  35.   :action (fn [e] print("YO")))
  36.  
  37.  
  38. (declare conn-handler)
  39.  
  40. (defn make-connection[server port]
  41.   (let [socket (Socket. server port)
  42.         in (BufferedReader. (InputStreamReader. (.getInputStream socket)))
  43.         out (PrintWriter. (.getOutputStream socket))]
  44.     (atom {:in in :out out})
  45.     )
  46.   )
  47.  
  48. (defn write [conn msg]
  49.   (doto (:out @conn)
  50.     (.println (str msg "\r"))
  51.     (.flush)
  52.     )
  53.   (value! textboxoutput (str(value textboxoutput)\newline msg))
  54.   )
  55.  
  56. (defn make-namesList [msg]
  57.   (value! namesList (str (value namesList) (re-find #" :.*" msg)))
  58.   )
  59.  
  60. (defn conn-handler [conn]
  61.   (while (nil? (:exit @conn))
  62.     (let [msg (.readLine (:in @conn))]
  63.       (println msg)
  64.       (value! textboxoutput (str(value textboxoutput)\newline msg))
  65.       (cond
  66.        (re-find #"^ERROR :Closing Link:" msg)
  67.        (dosync (alter conn merge {:exit true}))
  68.        (re-find #"^PING" msg)
  69.        (write conn (str "PONG "  (re-find #":.*" msg)))
  70.        (re-find #"^.*= #" msg)
  71.        (make-namesList msg)
  72.        (re-find #"^.*JOIN #" msg)
  73.        (value! namesList (str (value namesList) (clojure.string/replace
  74.                           (clojure.string/replace(re-find #":.*!" msg) ":" "")"!" ""))
  75.                )
  76.        (re-find #"^.*QUIT" msg)
  77.        (value! namesList (clojure.string/replace(value namesList) (clojure.string/replace
  78.                           (clojure.string/replace(re-find #":.*!" msg) ":" "")"!" "") "")
  79.                )
  80.        )
  81.       )
  82.     )
  83.   )
  84.  
  85. (defn login!
  86.   [conn user]
  87.   (doto (Thread. #(conn-handler conn)) (.start))
  88.   (write conn (str "NICK " user))
  89.   (write conn (str "USER " user " 0 * :" user))
  90.   )
  91.  
  92.   (defn irc-speak! [conn channel message]
  93.     (write conn (str "PRIVMSG " channel " :" message))
  94.     )
  95.  
  96.    (defn irc-command! [conn message]
  97.     (write conn message)
  98.     )
  99.  
  100.   (defn chat-form[conn]
  101.   (frame :content (vertical-panel
  102.                                        :items [(scrollable textboxoutput)
  103.                                                :separator
  104.                                                textbox
  105.                                                :separator
  106.                                                (scrollable namesList)
  107.                                                (button :text "send" :listen [:action (fn[e] (do
  108.                                                                                               (irc-speak! conn "#clojurehu" (value textbox))
  109.                                                                                               (value! textbox "")))])
  110.                                                ]) :width 800 :height 600))
  111.  
  112.  
  113.   (defn join! [conn channel]
  114.     (write conn (str "join " channel))
  115.     (let [form  (-> (chat-form conn) show!)
  116.                     ])
  117.     )
  118.    
  119.  
  120.                              
  121.  
  122. (defn -main [& args]
  123.   (invoke-later
  124.     (let [form  (value! (login-form) defaults)
  125.           result (-> (dialog :content form :option-type :ok-cancel) pack! show!)]
  126.       (if (= :success result)
  127.         (do
  128.           (def irc(make-connection (get-in (value form )[:Server]) (Integer/parseInt(get-in (value form )[ :Port]))))
  129.           (login! irc(get-in (value form )[:Nickname]))
  130.           (join! irc (get-in (value form )[:Channel]))           )
  131.         (println "User canceled")))))
clone this paste RAW Paste Data