From 6453c5409b40d48d6e0c0c40aa99a3a5fd971fa1 Mon Sep 17 00:00:00 2001 From: ogormanrules Date: Thu, 19 Mar 2015 09:45:16 -0400 Subject: [PATCH 1/2] Final Product Here we go. --- .gitattributes | 17 ++++++ .gitignore | 43 +++++++++++++++ chat.sql | 52 ++++++++++++++++++ package.json | 2 +- server.js | 2 +- server.py | 107 ++++++++++++++++++++++++++++--------- static/index.html | 133 ++++++++++++++++++++++++++++++---------------- 7 files changed, 282 insertions(+), 74 deletions(-) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 chat.sql diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bdb0cab --- /dev/null +++ b/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96374c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# ========================= +# Operating System Files +# ========================= + +# OSX +# ========================= + +.DS_Store +.AppleDouble +.LSOverride + +# Thumbnails +._* + +# Files that might appear on external disk +.Spotlight-V100 +.Trashes + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/chat.sql b/chat.sql new file mode 100644 index 0000000..ccb0efe --- /dev/null +++ b/chat.sql @@ -0,0 +1,52 @@ +DROP DATABASE IF EXISTS chat; +CREATE DATABASE chat; +\c chat; +CREATE EXTENSION pgcrypto; +-- +-- Table structure for table users +-- + +DROP TABLE IF EXISTS users; +CREATE TABLE users ( + id serial NOT NULL, + username varchar(25) NOT NULL default '', + password varchar(300) NOT NULL default '', + PRIMARY KEY (id) +) ; + +-- +-- Putting data into the users table +-- + +INSERT INTO users (username, password) VALUES +('ryan', crypt('og123', gen_salt('bf'))), +('peter', crypt('laser', gen_salt('bf'))), +('ghost', crypt('spooky', gen_salt('bf'))), +('ghostbuster', crpyt('wygc', gen_salt('bf'))); + + +-- +-- Table structure for table messages +-- + +DROP TABLE IF EXISTS messages; +CREATE TABLE messages ( + id serial NOT NULL, + text varchar(300) NOT NULL default '', + user_id int NOT NULL default '0', + PRIMARY KEY (id) +) ; + +-- +-- Putting data into the messages table +-- + +INSERT INTO messages (text, user_id) VALUES ('i wonder if we will pass this?', 1); +INSERT INTO messages (text, user_id) VALUES ('idk, man. this was really hard.', 2); +INSERT INTO messages (text, user_id) VALUES ('yeah, totally', 1); +INSERT INTO messages (text, user_id) VALUES ('you will never PASS!!!!', 3); +INSERT INTO messages (text, user_id) VALUES ('WTF', 2); +INSERT INTO messages (text, user_id) VALUES ('AHHHHHHHHHHHHH', 2); +INSERT INTO messages (text, user_id) VALUES ('RUN!!!', 1); +INSERT INTO messages (text, user_id) VALUES ('i aint afraid of no ghosts', 4); + diff --git a/package.json b/package.json index 722c5ba..451d568 100644 --- a/package.json +++ b/package.json @@ -10,4 +10,4 @@ "express": "~3.2.4", "socket.io": "~0.9.14" } -} +} \ No newline at end of file diff --git a/server.js b/server.js index 88085c8..cf3c3d1 100644 --- a/server.js +++ b/server.js @@ -81,4 +81,4 @@ function broadcast(event, data) { server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){ var addr = server.address(); console.log("Chat server listening at", addr.address + ":" + addr.port); -}); +}); \ No newline at end of file diff --git a/server.py b/server.py index dcb0644..6e1691f 100644 --- a/server.py +++ b/server.py @@ -2,9 +2,13 @@ import uuid from flask import Flask, session from flask.ext.socketio import SocketIO, emit +import psycopg2 +import psycopg2.extras + app = Flask(__name__, static_url_path='') app.config['SECRET_KEY'] = 'secret!' +app.debug = True socketio = SocketIO(app) @@ -13,51 +17,88 @@ def updateRoster(): names = [] - for user_id in users: - print users[user_id]['username'] - if len(users[user_id]['username'])==0: + for uid in users: + print users[uid]['username'] + if len(users[uid]['username'])==0: names.append('Anonymous') else: - names.append(users[user_id]['username']) + names.append(users[uid]['username']) print 'broadcasting names' emit('roster', names, broadcast=True) +def connectToDB(): + connectionString = 'dbname=chat user=postgres password=Calut3ch host=localhost' + try: + return psycopg2.connect(connectionString) + except: + print("Can't connect to database") @socketio.on('connect', namespace='/chat') def test_connect(): session['uuid']=uuid.uuid1() - session['username']='starter name' print 'connected' users[session['uuid']]={'username':'New User'} updateRoster() - for message in messages: - emit('message', message) - -@socketio.on('message', namespace='/chat') -def new_message(message): - #tmp = {'text':message, 'name':'testName'} - tmp = {'text':message, 'name':users[session['uuid']]['username']} - messages.append(tmp) - emit('message', tmp, broadcast=True) +@socketio.on('login', namespace='/chat') +def on_login(data): + print 'login ' + username = data['username'] + password = data['password'] + conn = connectToDB() + cur=conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + query = "select * from users where username = %s and password = crypt(%s,password)" + + cur.execute(query, (username, password)) + someone = cur.fetchone() + + if someone: + users[session['uuid']]={'username': data['username']} + session['username']=data['username'] + session['id']=someone['id'] + + text = "select text, username from messages join users on messages.user_id = users.id" + cur.execute(text) + stuff = cur.fetchall() + + for something in stuff: + something = {'text': something['text'], 'name': something['username']} + emit('message', something) + updateRoster() + else: + print 'user or password not valid' -@socketio.on('identify', namespace='/chat') -def on_identify(message): - print 'identify' + message - users[session['uuid']]={'username':message} - updateRoster() -@socketio.on('login', namespace='/chat') -def on_login(pw): - print 'login ' + pw - #users[session['uuid']]={'username':message} - #updateRoster() +@socketio.on('message', namespace='/chat') +def new_message(text): + + if 'username' in session: #if the username is logged in currently + temp = {'text':text, 'name':users[session['uuid']]['username']} + messages.append(temp) + emit('message', temp, broadcast=True) + + + #inserting new message into chat + conn = connectToDB() + cur=conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + query = "INSERT INTO messages VALUES(DEFAULT, %s, %s)" + + cur.execute(query, (text, session['id'])) + conn.commit() + + +@socketio.on('identify', namespace='/chat') +def on_identify(text): + print 'identify' + text + users[session['uuid']]={'username':text} + updateRoster() + @socketio.on('disconnect', namespace='/chat') def on_disconnect(): print 'disconnect' @@ -65,6 +106,21 @@ def on_disconnect(): del users[session['uuid']] updateRoster() + +#search function +@socketio.on('search', namespace='/chat') +def search(term): + if 'username' in session: + term = '%' + term +'%' + conn = connectToDB() + cur=conn.cursor(cursor_factory=psycopg2.extras.DictCursor) + query = "SELECT text, username FROM messages JOIN users ON messages.user_id = users.id WHERE text LIKE %s OR username LIKE %s" + cur.execute(query, (term, term)) + stuff = cur.fetchall() + for result in stuff: + result = {'text': result['text'], 'name': result['username']} + emit('result', result) + @app.route('/') def hello_world(): print 'in hello world' @@ -89,5 +145,4 @@ def static_proxy_img(path): if __name__ == '__main__': print "A" - socketio.run(app, host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080))) - \ No newline at end of file + socketio.run(app, host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080))) \ No newline at end of file diff --git a/static/index.html b/static/index.html index e43047e..6c21711 100644 --- a/static/index.html +++ b/static/index.html @@ -1,7 +1,7 @@ - Chat Example + Suck Chat Incorporated @@ -11,20 +11,25 @@ } #popupbox{ + +background: #FFFFFF; +border: solid #909090 3px; +z-index: 9; +font-family: arial; + margin: 0; margin-left: 10%; -margin-right: 70%; -margin-top: 70px; +margin-right: 60%; +margin-top: 80px; padding-top: 10px; -width: 20%; -height: 170px; -position: absolute; -background: #FFFFFF; -border: solid #909090 2px; -z-index: 9; -font-family: arial; + +width: 40%; +height: 180px; + +position: absolute; visibility: hidden; } + @@ -102,15 +120,15 @@ @@ -130,7 +148,7 @@
@@ -150,9 +168,9 @@

Chat Example

- - - + + + @@ -165,6 +183,29 @@

Chat Example

+ + + + + + + + + + + + + +
NameText
+
+
+
+ + +
+
+
+ @@ -173,4 +214,4 @@

Chat Example

- + \ No newline at end of file From 09d6b06daf7672f0446df7113cf6db3fa4adcefc Mon Sep 17 00:00:00 2001 From: ogormanrules Date: Thu, 19 Mar 2015 10:12:08 -0400 Subject: [PATCH 2/2] Delete deleted old client folder and old index.html (the original) --- client/index.html | 100 ---------------------------------------------- 1 file changed, 100 deletions(-) delete mode 100644 client/index.html diff --git a/client/index.html b/client/index.html deleted file mode 100644 index 3d6f022..0000000 --- a/client/index.html +++ /dev/null @@ -1,100 +0,0 @@ - - - - Chat Example - - - - - - - -
- - -
-
- -
-
- - - - - - - - - - - - - -
NameText
-
-
-
-
- - -
-
-
-
-
-
- - - - - -