From 89c995fb39b3adbece1061024285714a5825a11e Mon Sep 17 00:00:00 2001 From: sostler Date: Mon, 9 Nov 2009 20:29:47 -0500 Subject: Fix CRLF --- bin/repl.bat | 4 +- bin/run | 4 +- bin/run.bat | 4 +- src/site.clj | 143 ++++++++++++----------- static/index.html | 46 ++++---- static/pichat.css | 104 ++++++++--------- static/pichat.js | 344 +++++++++++++++++++++++++++--------------------------- static/reset.css | 106 ++++++++--------- static/test.html | 150 ++++++++++++------------ 9 files changed, 453 insertions(+), 452 deletions(-) mode change 100755 => 100644 src/site.clj diff --git a/bin/repl.bat b/bin/repl.bat index 25bb737..74ebcfb 100755 --- a/bin/repl.bat +++ b/bin/repl.bat @@ -1,3 +1,3 @@ -REM Windows REPL script - +REM Windows REPL script + java -cp lib/commons-io-1.4.jar;lib/commons-fileupload-1.2.1.jar;lib/commons-codec-1.3.jar;lib/clojure.jar;lib/clojure-contrib.jar;lib/compojure.jar;lib/jetty-6.1.14.jar;lib/jetty-util-6.1.14.jar;lib/servlet-api-2.5-6.1.14.jar;lib/jline-0.9.94.jar jline.ConsoleRunner clojure.lang.Repl %1 \ No newline at end of file diff --git a/bin/run b/bin/run index c2af849..9b5149c 100755 --- a/bin/run +++ b/bin/run @@ -1,3 +1,3 @@ -#!/bin/sh - +#!/bin/sh + java -cp .:lib/commons-io-1.4.jar:lib/commons-fileupload-1.2.1.jar:lib/commons-codec-1.3.jar:lib/jline-0.9.94.jar:lib/clojure.jar:lib/clojure-contrib.jar:lib/compojure.jar:lib/jetty-6.1.14.jar:lib/jetty-util-6.1.14.jar:lib/servlet-api-2.5-6.1.14.jar:classes jline.ConsoleRunner clojure.lang.Script \ No newline at end of file diff --git a/bin/run.bat b/bin/run.bat index bec30c8..2157268 100755 --- a/bin/run.bat +++ b/bin/run.bat @@ -1,3 +1,3 @@ -REM Windows runner script - +REM Windows runner script + java -cp lib/commons-io-1.4.jar;lib/commons-fileupload-1.2.1.jar;lib/commons-codec-1.3.jar;lib/clojure.jar;lib/clojure-contrib.jar;lib/compojure.jar;lib/jetty-6.1.14.jar;lib/jetty-util-6.1.14.jar;lib/servlet-api-2.5-6.1.14.jar clojure.lang.Script %1 \ No newline at end of file diff --git a/src/site.clj b/src/site.clj old mode 100755 new mode 100644 index 33b1bb3..1206a33 --- a/src/site.clj +++ b/src/site.clj @@ -1,71 +1,72 @@ -; site.clj - -(ns pichat - (:import java.lang.System) - (:use compojure - clojure.contrib.json.write)) - -(defstruct user-struct :nick :last-seen) -(defstruct message-struct :nick :content :timestamp) - -(def users (ref {})) -(def messages (ref [])) - -(defn resp-error [message] - {:status 400 :headers {} :body message}) - -(defn resp-success [message] - {:status 200 :headers {} :body (json-str message)}) - -(defn join-success [nick] - (alter users assoc nick (struct user-struct nick (System/currentTimeMillis))) - (let [users (keys @users) - messages (take 20 @messages) - data {"users" users "messages" messages}] - [(session-assoc :nick nick) - (resp-success data)])) - -(defn try-join [params] - (let [nick (params :nick)] - (dosync - (if (contains? @users nick) - (resp-error "NICK_TAKEN") - (join-success nick))))) - -(defn new-messages [since] - (reverse (take-while (fn [m] (> (m :timestamp) since)) @messages))) - -(defn refresh [nick] - (dosync - (let [last-seen (get-in @users [nick :last-seen])] - (alter users assoc-in [nick :last-seen] (System/currentTimeMillis)) - (resp-success {"messages" (new-messages last-seen)})))) - -(defn swap [f] - (fn [& more] (apply f (reverse more)))) - -(defn msg [session params] - (dosync - (let [nick (session :nick) - content (params :content) - msg (struct message-struct nick content (System/currentTimeMillis))] - (if (contains? @users nick) - (do (alter messages (swap cons) msg) - (resp-success "OK")) - (resp-error "UNKNOWN_USER"))))) - -(defroutes pichat - (GET "/" (serve-file "static" "index.html")) - (GET "/static/*" (or (serve-file "static" (params :*)) - :next)) - (GET "/join" (try-join params)) - (GET "/refresh" (refresh (session :nick))) - (GET "/msg" (msg session params)) - (ANY "*" [404 "Page not found"])) - -(decorate pichat - (with-mimetypes) - (with-session {:type :memory, :expires (* 60 60)})) - -(run-server {:port 8080} - "/*" (servlet pichat)) +; site.clj + +(ns pichat + (:import java.lang.System) + (:use compojure + clojure.contrib.json.write)) + +(defstruct user-struct :nick :last-seen) +(defstruct message-struct :nick :content :timestamp) + +(def users (ref {})) +(def messages (ref [])) + +(defn resp-error [message] + {:status 400 :headers {} :body message}) + +(defn resp-success [message] + {:status 200 :headers {} :body (json-str message)}) + +(defn join-success [nick] + (alter users assoc nick (struct user-struct nick (System/currentTimeMillis))) + (let [users (keys @users) + messages (take 20 @messages) + data {"users" users "messages" messages}] + [(session-assoc :nick nick) + (resp-success data)])) + +(defn try-join [params] + (let [nick (params :nick)] + (dosync + (if (contains? @users nick) + (resp-error "NICK_TAKEN") + (join-success nick))))) + +(defn new-messages [since] + (reverse (take-while (fn [m] (> (m :timestamp) since)) @messages))) + +(defn refresh [nick] + (dosync + (let [last-seen (get-in @users [nick :last-seen])] + (alter users assoc-in [nick :last-seen] (System/currentTimeMillis)) + (resp-success {"messages" (new-messages last-seen)})))) + +(defn swap [f] + (fn [& more] (apply f (reverse more)))) + +(defn msg [session params] + (dosync + (let [nick (session :nick) + content (params :content) + msg (struct message-struct nick content (System/currentTimeMillis))] + (if (contains? @users nick) + (do (alter messages (swap cons) msg) + (resp-success "OK")) + (resp-error "UNKNOWN_USER"))))) + +(defroutes pichat + (GET "/" (serve-file "static" "index.html")) + (GET "/static/*" (or (serve-file "static" (params :*)) + :next)) + (GET "/join" (try-join params)) + (GET "/refresh" (refresh (session :nick))) + (GET "/msg" (msg session params)) + (ANY "*" [404 "Page not found"])) + +(decorate pichat + (with-mimetypes) + (with-session {:type :memory, :expires (* 60 60)})) + +(run-server {:port 80} + "/*" (servlet pichat)) + diff --git a/static/index.html b/static/index.html index bd75ab1..cee1d91 100755 --- a/static/index.html +++ b/static/index.html @@ -1,23 +1,23 @@ - - - Pichat - - - - - - - -
-

Welcome to Pichat!

- - - -
- - + + + Pichat + + + + + + + +
+

Welcome to Pichat!

+ + + +
+ + diff --git a/static/pichat.css b/static/pichat.css index 383ba7a..7bbb457 100755 --- a/static/pichat.css +++ b/static/pichat.css @@ -1,53 +1,53 @@ -/* pichat.css */ - -html, body { - padding: 1em; -} - -#content, #chatbox { - position: relative; -} - -#messagePane { - border: 1px solid green; - height: 600px; - padding: 5px; - position: absolute; - width: 540px; -} - -#messageList { - height: 575px; - width: 540px; - overflow-y: auto; - overflow-x: hidden; - border-bottom: 1px solid grey; -} - -#msgInputDiv { - height: 15px; -} - -#msgInput { - width: 85%; - margin-right: 5px; -} - -#msgSubmit { - width: 14%; -} - -.msgDiv { - width: 520px; -} - -#userList { - overflow: auto; - border: 1px solid blue; - height: 600px; - margin: 0px; - position: absolute; - padding: 5px; - left: 550px; - width: 150px; +/* pichat.css */ + +html, body { + padding: 1em; +} + +#content, #chatbox { + position: relative; +} + +#messagePane { + border: 1px solid green; + height: 600px; + padding: 5px; + position: absolute; + width: 540px; +} + +#messageList { + height: 575px; + width: 540px; + overflow-y: auto; + overflow-x: hidden; + border-bottom: 1px solid grey; +} + +#msgInputDiv { + height: 15px; +} + +#msgInput { + width: 85%; + margin-right: 5px; +} + +#msgSubmit { + width: 14%; +} + +.msgDiv { + width: 520px; +} + +#userList { + overflow: auto; + border: 1px solid blue; + height: 600px; + margin: 0px; + position: absolute; + padding: 5px; + left: 550px; + width: 150px; } \ No newline at end of file diff --git a/static/pichat.js b/static/pichat.js index 5567ea8..99bf1b5 100755 --- a/static/pichat.js +++ b/static/pichat.js @@ -1,172 +1,172 @@ -// pichat.js - -var Nick = null; - -function handleJoinError(resp) { - var respText = resp.responseText ? resp.responseText.trim() : false; - if (respText == 'NICK_TAKEN') { - alert("Nick '" + Nick + "' was taken! Please choose another."); - } else if (respText) { - alert("Cannot join! (" + respText + ")"); - } else { - alert("Cannot join! Please try again later."); - } -} - -function handleMsgError(resp) { - var respText = resp.responseText ? resp.responseText.trim() : false; - if (respText == 'UNKNOWN_USER') { - alert("Can't send message! Please login."); - } else if (respText) { - alert("Cannot send message! (" + respText + ")"); - } else { - alert("Cannot send message!"); - } -} - -function join() { - $('#join, #nick').attr('disabled', true); - $('#loginspinner').show(); - Nick = $('#nick').val(); - - onSuccess = function(json) { - generateChatInterface(json.users, json.messages); - }; - - onError = function(resp, textStatus, errorThrown) { - $('#join, #nick').attr('disabled', false); - $('#loginspinner').hide(); - handleJoinError(resp); - }; - - $.ajax({ - type: 'GET', - timeout: 5000, - url: 'join', - data: {'nick': Nick }, - cache: false, - dataType: 'json', - success: onSuccess, - error: onError - }); -} - -function buildUserDiv(user) { - return '
' + user + '
'; -} - -// http://snippets.dzone.com/posts/show/6995 -var URLRegex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i; - -function buildMessageDiv(msg) { - var match = URLRegex.exec(msg.content) - if (match) { - return '
' + msg.nick + ': ' - + '' - + '
'; - } else { - return '
' + msg.nick + ": " - + msg.content + "
"; - } -} - -function buildChatInterface(users, messages) { - var userList = '
' - + $.map(users, buildUserDiv).join('') + '
'; - var messageList = '
' - + '
' - + $.map(messages, buildMessageDiv).join('') - + '
' - + '
' - + '' - + '' - + '
' - + '
'; - return '

Pichat

' + userList + messageList + '
'; -} - -function submitMessage() { - var content = $('#msgInput').val(); - var msg = { 'nick': Nick, 'content': content, 'timestamp': new Date() }; - if (content == '') { return; } - - var shouldScroll = isScrolledToBottom($('#messageList')[0]); - - $('#messageList').append($(buildMessageDiv(msg))); - $('#msgInput').val(''); - - if (shouldScroll) { - scrollToBottom($('#messageList')[0]); - } - - var onSuccess = function() {}; - var onError = function(resp, textStatus, errorThrown) { - handleMsgError(resp); - }; - - $.ajax({ - type: 'GET', - timeout: 5000, - url: 'msg', - data: {'content': content }, - cache: false, - dataType: 'json', - success: onSuccess, - error: onError - }); -} - -function ifEnter(fn) { - return function(e) { - if (e.keyCode == 13) { fn(); } - }; -} - -function isScrolledToBottom(div) { - return Math.abs(div.scrollTop - (div.scrollHeight - div.offsetHeight)) <= 3; -} - -function scrollToBottom(div) { - div.scrollTop = div.scrollHeight; -} - -function refresh() { - var onSuccess = function(json) { - if (json.messages.length == 0) { - return; - } - - var shouldScroll = isScrolledToBottom($('#messageList')[0]); - - // Ignore our own messages - var filterFunc = function(m) { return m.nick != Nick }; - var msgStr = $.map($.grep(json.messages, filterFunc), - buildMessageDiv).join(''); - $('#messageList').append(msgStr); - - if (shouldScroll) { - scrollToBottom($('#messageList')[0]); - } - }; - - var onError = function(resp, textStatus, errorThrown) {}; - - $.ajax({ - type: 'GET', - timeout: 5000, - url: 'refresh', - cache: false, - dataType: 'json', - success: onSuccess, - error: onError - }); -} - -function generateChatInterface(users, messages) { - $('#content').html(buildChatInterface(users, messages)); - $('#msgInput').keyup(ifEnter(submitMessage)); - $('#msgSubmit').click(submitMessage); - setInterval(refresh, 1000); -} - +// pichat.js + +var Nick = null; + +function handleJoinError(resp) { + var respText = resp.responseText ? resp.responseText.trim() : false; + if (respText == 'NICK_TAKEN') { + alert("Nick '" + Nick + "' was taken! Please choose another."); + } else if (respText) { + alert("Cannot join! (" + respText + ")"); + } else { + alert("Cannot join! Please try again later."); + } +} + +function handleMsgError(resp) { + var respText = resp.responseText ? resp.responseText.trim() : false; + if (respText == 'UNKNOWN_USER') { + alert("Can't send message! Please login."); + } else if (respText) { + alert("Cannot send message! (" + respText + ")"); + } else { + alert("Cannot send message!"); + } +} + +function join() { + $('#join, #nick').attr('disabled', true); + $('#loginspinner').show(); + Nick = $('#nick').val(); + + onSuccess = function(json) { + generateChatInterface(json.users, json.messages); + }; + + onError = function(resp, textStatus, errorThrown) { + $('#join, #nick').attr('disabled', false); + $('#loginspinner').hide(); + handleJoinError(resp); + }; + + $.ajax({ + type: 'GET', + timeout: 5000, + url: 'join', + data: {'nick': Nick }, + cache: false, + dataType: 'json', + success: onSuccess, + error: onError + }); +} + +function buildUserDiv(user) { + return '
' + user + '
'; +} + +// http://snippets.dzone.com/posts/show/6995 +var URLRegex = /((http\:\/\/|https\:\/\/|ftp\:\/\/)|(www\.))+(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i; + +function buildMessageDiv(msg) { + var match = URLRegex.exec(msg.content) + if (match) { + return '
' + msg.nick + ': ' + + '' + + '
'; + } else { + return '
' + msg.nick + ": " + + msg.content + "
"; + } +} + +function buildChatInterface(users, messages) { + var userList = '
' + + $.map(users, buildUserDiv).join('') + '
'; + var messageList = '
' + + '
' + + $.map(messages, buildMessageDiv).join('') + + '
' + + '
' + + '' + + '' + + '
' + + '
'; + return '

Pichat

' + userList + messageList + '
'; +} + +function submitMessage() { + var content = $('#msgInput').val(); + var msg = { 'nick': Nick, 'content': content, 'timestamp': new Date() }; + if (content == '') { return; } + + var shouldScroll = isScrolledToBottom($('#messageList')[0]); + + $('#messageList').append($(buildMessageDiv(msg))); + $('#msgInput').val(''); + + if (shouldScroll) { + scrollToBottom($('#messageList')[0]); + } + + var onSuccess = function() {}; + var onError = function(resp, textStatus, errorThrown) { + handleMsgError(resp); + }; + + $.ajax({ + type: 'GET', + timeout: 5000, + url: 'msg', + data: {'content': content }, + cache: false, + dataType: 'json', + success: onSuccess, + error: onError + }); +} + +function ifEnter(fn) { + return function(e) { + if (e.keyCode == 13) { fn(); } + }; +} + +function isScrolledToBottom(div) { + return Math.abs(div.scrollTop - (div.scrollHeight - div.offsetHeight)) <= 3; +} + +function scrollToBottom(div) { + div.scrollTop = div.scrollHeight; +} + +function refresh() { + var onSuccess = function(json) { + if (json.messages.length == 0) { + return; + } + + var shouldScroll = isScrolledToBottom($('#messageList')[0]); + + // Ignore our own messages + var filterFunc = function(m) { return m.nick != Nick }; + var msgStr = $.map($.grep(json.messages, filterFunc), + buildMessageDiv).join(''); + $('#messageList').append(msgStr); + + if (shouldScroll) { + scrollToBottom($('#messageList')[0]); + } + }; + + var onError = function(resp, textStatus, errorThrown) {}; + + $.ajax({ + type: 'GET', + timeout: 5000, + url: 'refresh', + cache: false, + dataType: 'json', + success: onSuccess, + error: onError + }); +} + +function generateChatInterface(users, messages) { + $('#content').html(buildChatInterface(users, messages)); + $('#msgInput').keyup(ifEnter(submitMessage)); + $('#msgSubmit').click(submitMessage); + setInterval(refresh, 1000); +} + diff --git a/static/reset.css b/static/reset.css index 8767cdd..a6a258f 100755 --- a/static/reset.css +++ b/static/reset.css @@ -1,54 +1,54 @@ -/* reset.css - From http://meyerweb.com/eric/tools/css/reset/ - v1.0 | 20080212 */ - -html, body, div, span, applet, object, iframe, -h1, h2, h3, h4, h5, h6, p, blockquote, pre, -a, abbr, acronym, address, big, cite, code, -del, dfn, em, font, img, ins, kbd, q, s, samp, -small, strike, strong, sub, sup, tt, var, -b, u, i, center, -dl, dt, dd, ol, ul, li, -fieldset, form, label, legend, -table, caption, tbody, tfoot, thead, tr, th, td { - margin: 0; - padding: 0; - border: 0; - outline: 0; - font-size: 100%; - vertical-align: baseline; - background: transparent; -} -body { - line-height: 1; -} -ol, ul { - list-style: none; -} -blockquote, q { - quotes: none; -} -blockquote:before, blockquote:after, -q:before, q:after { - content: ''; - content: none; -} - -/* remember to define focus styles! */ -:focus { - outline: 0; -} - -/* remember to highlight inserts somehow! */ -ins { - text-decoration: none; -} -del { - text-decoration: line-through; -} - -/* tables still need 'cellspacing="0"' in the markup */ -table { - border-collapse: collapse; - border-spacing: 0; +/* reset.css + From http://meyerweb.com/eric/tools/css/reset/ + v1.0 | 20080212 */ + +html, body, div, span, applet, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +a, abbr, acronym, address, big, cite, code, +del, dfn, em, font, img, ins, kbd, q, s, samp, +small, strike, strong, sub, sup, tt, var, +b, u, i, center, +dl, dt, dd, ol, ul, li, +fieldset, form, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td { + margin: 0; + padding: 0; + border: 0; + outline: 0; + font-size: 100%; + vertical-align: baseline; + background: transparent; +} +body { + line-height: 1; +} +ol, ul { + list-style: none; +} +blockquote, q { + quotes: none; +} +blockquote:before, blockquote:after, +q:before, q:after { + content: ''; + content: none; +} + +/* remember to define focus styles! */ +:focus { + outline: 0; +} + +/* remember to highlight inserts somehow! */ +ins { + text-decoration: none; +} +del { + text-decoration: line-through; +} + +/* tables still need 'cellspacing="0"' in the markup */ +table { + border-collapse: collapse; + border-spacing: 0; } \ No newline at end of file diff --git a/static/test.html b/static/test.html index 1c9bfbe..bd0779a 100755 --- a/static/test.html +++ b/static/test.html @@ -1,75 +1,75 @@ - - - - - - -
-
- I saw the best minds of my generation destroyed by -madness, starving hysterical naked, -dragging themselves through the negro streets at dawn -looking for an angry fix, -angelheaded hipsters burning for the ancient heavenly -connection to the starry dynamo in the machin- -ery of night, -who poverty and tatters and hollow-eyed and high sat -up smoking in the supernatural darkness of -cold-water flats floating across the tops of cities -contemplating jazz, -who bared their brains to Heaven under the El and -saw Mohammedan angels staggering on tene- -ment roofs illuminated, -who passed through universities with radiant cool eyes -hallucinating Arkansas and Blake-light tragedy -among the scholars of war, -who were expelled from the academies for crazy & -publishing obscene odes on the windows of the -skull, -who cowered in unshaven rooms in underwear, burn- -ing their money in wastebaskets and listening -to the Terror through the wall, -who got busted in their pubic beards returning through -Laredo with a belt of marijuana for New York, -who ate fire in paint hotels or drank turpentine in -Paradise Alley, death, or purgatoried their -torsos night after night -with dreams, with drugs, with waking nightmares, al- -cohol and cock and endless balls, -incomparable blind; streets of shuddering cloud and -lightning in the mind leaping toward poles of -
- -
- - - + + + + + + +
+
+ I saw the best minds of my generation destroyed by +madness, starving hysterical naked, +dragging themselves through the negro streets at dawn +looking for an angry fix, +angelheaded hipsters burning for the ancient heavenly +connection to the starry dynamo in the machin- +ery of night, +who poverty and tatters and hollow-eyed and high sat +up smoking in the supernatural darkness of +cold-water flats floating across the tops of cities +contemplating jazz, +who bared their brains to Heaven under the El and +saw Mohammedan angels staggering on tene- +ment roofs illuminated, +who passed through universities with radiant cool eyes +hallucinating Arkansas and Blake-light tragedy +among the scholars of war, +who were expelled from the academies for crazy & +publishing obscene odes on the windows of the +skull, +who cowered in unshaven rooms in underwear, burn- +ing their money in wastebaskets and listening +to the Terror through the wall, +who got busted in their pubic beards returning through +Laredo with a belt of marijuana for New York, +who ate fire in paint hotels or drank turpentine in +Paradise Alley, death, or purgatoried their +torsos night after night +with dreams, with drugs, with waking nightmares, al- +cohol and cock and endless balls, +incomparable blind; streets of shuddering cloud and +lightning in the mind leaping toward poles of +
+ +
+ + + -- cgit v1.2.3-70-g09d2