Created
July 21, 2017 21:49
-
-
Save selfsame/9db62698b9af27c1e2fe20dd0bc13744 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 server.telnet | |
| (import | |
| [java.net ServerSocket Socket SocketException] | |
| [java.io InputStreamReader OutputStreamWriter BufferedWriter] | |
| [clojure.lang LineNumberingPushbackReader])) | |
| (def T_IAC "\u00ff");255 | |
| (def T_WILL "\u00fb");251 | |
| (def T_WONT "\u00fc");252 | |
| (def T_DO "\u00fd");253 | |
| (def T_DONT "\u00fe");254 | |
| (def T_SE "\u00f0");240 | |
| (def T_SB "\u00fa");250 | |
| (def T_CRLF "\r\n") | |
| (def T_ECHO "\u0001"); 1 | |
| (def T_LINE "\u0022"); 34 | |
| (def T_NAWS "\u001f"); 31 | |
| (def T_CSI "\u001b[") | |
| (def T_CLR "2J") | |
| (def T_ORIG "1;1H") | |
| (def T_HIDE "?25l") ; tput civis [man terminfo] | |
| (def T_SHOW "?25h") ; tput cnorm [man terminfo] | |
| (defn on-thread [f] | |
| (doto (new Thread f) (.start))) | |
| (defn create-server [accept-socket port] | |
| (let [ss (new ServerSocket port)] | |
| (on-thread #(when-not (. ss (isClosed)) | |
| (try (accept-socket (. ss (accept))) | |
| (catch SocketException e)) | |
| (recur))) ss)) | |
| (defn new-connection [s] | |
| (on-thread | |
| (fn [] | |
| (let [i (. s (getInputStream)) | |
| o (. s (getOutputStream)) | |
| ir (new InputStreamReader i) | |
| ow (new OutputStreamWriter o)] | |
| (.write ow (str T_IAC T_WONT T_LINE T_IAC T_WILL T_ECHO)) | |
| (.flush ow) | |
| (.write ow (str ansi-esc 36 "m" "hello?")) | |
| (.flush ow) | |
| (while (not (. s (isClosed))) | |
| (let [c (char (.read ir))] | |
| (prn c))))))) | |
| (def server (create-server new-connection 5073)) | |
| (def rdr (new LineNumberingPushbackReader | |
| (new InputStreamReader (. client (getInputStream))))) | |
| (def wtr (new OutputStreamWriter (. client (getOutputStream)))) | |
| '(. server (close)) |
Author
selfsame
commented
Jul 21, 2017

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment