Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions chat.sql
Original file line number Diff line number Diff line change
@@ -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);

100 changes: 0 additions & 100 deletions client/index.html

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"express": "~3.2.4",
"socket.io": "~0.9.14"
}
}
}
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
107 changes: 81 additions & 26 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -13,58 +17,110 @@

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'
if session['uuid'] in users:
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'
Expand All @@ -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)))

socketio.run(app, host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)))
Loading