summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/chat/chat.js183
-rw-r--r--server/chat/workers/messageParser.js145
-rw-r--r--server/http/http.js125
3 files changed, 453 insertions, 0 deletions
diff --git a/server/chat/chat.js b/server/chat/chat.js
new file mode 100644
index 0000000..a1242c2
--- /dev/null
+++ b/server/chat/chat.js
@@ -0,0 +1,183 @@
+var Config = require('../../config/chat.js'),
+ conf = new Config(),
+ io = require('socket.io').listen(conf.port),
+ url = require('url'),
+ http = require("http"),
+ Worker = require('webworker-threads').Worker;
+
+io.set('transports', ['websocket']);
+io.enable('browser client minification');
+io.enable('browser client etag');
+io.enable('browser client gzip');
+io.set('log level', 1);
+
+var Chat = function(socket){
+ function handleSet(data){
+ socket.handle=data.handle;
+ userOnline();
+ }
+
+ function userOnline(){
+ socket.broadcast.emit(
+ 'online',
+ {
+ id : socket.id,
+ handle : socket.handle
+ }
+ )
+ }
+
+ function userOffline(){
+ if(!socket.handle)
+ return;
+
+ socket.broadcast.emit(
+ 'offline',
+ {
+ id : socket.id,
+ handle : socket.handle
+ }
+ )
+ }
+
+ function joinRoom(data) {
+ socket.join(
+ data.room
+ );
+ socket.room=data.room;
+ //Emit event to room that someone has joined
+ }
+
+ function leaveRoom(data) {
+ socket.leave(
+ data.room
+ );
+ socket.room=null;
+ //Emit event to room that someone has left
+ }
+
+ function toRoom(data) {
+ if(!socket.room)
+ return;
+
+ data.handle=socket.handle;
+
+ var messageParser = new Worker(
+ __dirname+'/workers/messageParser.js'
+ );
+
+ messageParser.onmessage=function(msg){
+ if(msg.data.fetchAudio){
+
+ http.get(
+ msg.data.fetchAudio,
+ function(response) {
+ var body='';
+
+ response.setEncoding('binary');
+ response.on(
+ 'data',
+ function (data) {
+ body+=data;
+ }
+ );
+ response.on(
+ 'end',
+ function(){
+ io.sockets.in(
+ socket.room
+ ).emit(
+ 'playlist.add',
+ {
+ url : msg.data.fetchAudio,
+ file: new Buffer(body,'binary').toString('base64')
+ }
+ );
+ }
+ );
+ }
+ );
+ return;
+ }
+ data.msg=msg.data;
+ io.sockets.in(
+ socket.room
+ ).emit(
+ 'room.chat',
+ data
+ );
+ };
+ messageParser.postMessage(
+ {
+ msg : data.msg
+ }
+ );
+
+ }
+
+ function toUser(data) {
+ io.sockets.manager[data.to.id].emit(
+ 'user.message',
+ {
+ from:{
+ id : socket.id,
+ handle : socket.handle
+ },
+ data:data
+ }
+ )
+ }
+
+ return {
+ set : {
+ handle : handleSet
+ },
+ room : {
+ join : joinRoom,
+ leave: leaveRoom,
+ chat : toRoom
+ },
+ user : {
+ online : userOnline,
+ offline : userOffline,
+ chat : toUser
+ }
+ }
+}
+
+io.sockets.on(
+ 'connection',
+ function (socket) {
+ var chat=new Chat(socket);
+
+ socket.on(
+ 'set.handle',
+ chat.set.handle
+ );
+
+ socket.on(
+ 'room.join',
+ chat.room.join
+ );
+
+ socket.on(
+ 'room.leave',
+ chat.room.leave
+ );
+
+ socket.on(
+ 'room.chat',
+ chat.room.chat
+ );
+
+ socket.on(
+ 'user.chat',
+ chat.user.chat
+ );
+
+ socket.on(
+ 'disconnect',
+ chat.user.offline
+ );
+ }
+); \ No newline at end of file
diff --git a/server/chat/workers/messageParser.js b/server/chat/workers/messageParser.js
new file mode 100644
index 0000000..3831f0e
--- /dev/null
+++ b/server/chat/workers/messageParser.js
@@ -0,0 +1,145 @@
+var URLExp = /(https?:\/\/[^\s,]+)/ig,
+ emailExp= /\"([^\"]+)\"\s+\<([^\>]+)\>/ig;
+
+var types={
+ img : {
+ jpg : true,
+ jpeg: true,
+ gif : true,
+ png : true,
+ bmp : true,
+ svg : true,
+ tiff: true,
+ webp: true,
+ wbmp: true
+ },
+ audio : {
+ mp3 : true,
+ wav : true,
+ aac : true
+ },
+ video : {
+ ogg : true,
+ webm: true,
+ mp4 : true
+ }
+}
+
+function convertToHTML(url){
+ if(url.match('@'))
+ return [
+ '<a href="maito:',
+ url,
+ '">',
+ url,
+ '</a>'
+ ].join('');
+
+ var data=url.split('.');
+
+ var extention=data.slice(-1);
+
+ if(types.img[extention])
+ return formatToImg(url,extention);
+
+ if(types.video[extention])
+ return formatToVideo(url,extention);
+
+ if(types.audio[extention]){
+ postMessage(
+ {
+ fetchAudio:url
+ }
+ );
+ return formatToAudio(url);
+ }
+
+ return formatToIFrame(url);
+}
+
+function formatToImg(url,extention){
+ return [
+ '<img class="message-img" src="',
+ url,
+ '"/>'
+ ].join('');
+}
+
+function formatToAudio(url,extention){
+ return [
+ '<hr>',
+ '<audio class="message-audio" controls><source src="',
+ url,
+ '"></audio>',
+ '<hr>'
+ ].join('');
+}
+
+function formatToVideo(url,extention){
+ return [
+ '<hr>',
+ '<video class="message-video" controls><source src="',
+ url,
+ '"></video>',
+ '<hr>'
+ ].join('');
+}
+
+function formatToEmail(url){
+ return [
+ '<a class="message-email" href="maito:',
+ url,
+ '">',
+ url,
+ '</a>'
+ ].join('');
+}
+
+function formatToLink(url){
+ return [
+ '<a class="message-link" href="',
+ url,
+ '">',
+ url,
+ '</a>'
+ ].join('');
+}
+
+function formatToIFrame(url){
+ return [
+ '<hr>',
+ url,
+ '<iFrame class="message-iframe" src="',
+ url,
+ '"></iFrame>',
+ '<hr>'
+ ].join('');
+}
+
+onmessage=function(data){
+ var msg = data.data.msg,
+ urls = msg.match(URLExp),
+ emails = msg.match(emailExp);
+
+ if(!urls){
+ postMessage(msg);
+ return;
+ }
+
+ for(i in urls){
+ msg=msg.replace(
+ urls[i],
+ convertToHTML(urls[i])
+ )
+ }
+
+ for(i in emails){
+ msg=msg.replace(
+ emails[i],
+ convertToHTML(emails[i])
+ )
+ }
+
+ postMessage(msg);
+ close();
+} \ No newline at end of file
diff --git a/server/http/http.js b/server/http/http.js
new file mode 100644
index 0000000..1fa985d
--- /dev/null
+++ b/server/http/http.js
@@ -0,0 +1,125 @@
+var http = require('http'),
+ url = require('url'),
+ path = require('path'),
+ fs = require('fs'),
+ Config = require('../../config/http.js'),
+ conf = new Config();
+
+http.createServer(
+ requestRecieved
+).listen(conf.port);
+
+function serveFile(filename,exists,response) {
+ if(!exists) {
+ serve(
+ response,
+ '404 MIA',
+ 404
+ );
+ return;
+ }
+
+ var contentType = path.extname(filename).slice(1);
+
+ //Only serve specified file types
+ if(!conf.contentType){
+ serve(
+ response,
+ '415 File type not supported',
+ 415
+ );
+ return;
+ }
+
+ //Deny restricted file types
+ if(!conf.contentType[contentType]){
+ serve(
+ response,
+ '415 File type not supported',
+ 415
+ );
+ return;
+ }
+
+ //Do not allow access to Dirs or restricted file types
+ if (
+ fs.statSync(filename).isDirectory() ||
+ conf.restrictedType[contentType]
+ ){
+ serve(
+ response,
+ '403 Access Denied',
+ 403
+ );
+ return;
+ }
+
+ fs.readFile(
+ filename,
+ 'binary',
+ function(err, file) {
+ if(err) {
+ serve(
+ response,
+ '500 '+err,
+ 500
+ );
+ return;
+ }
+
+ var headers = {
+ 'Content-Type' : conf.contentType[contentType]
+ }
+ serve(
+ response,
+ file,
+ 200,
+ headers,
+ 'binary'
+ );
+ return;
+ }
+ );
+}
+
+function serve(response,body,status,headers,encoding){
+ //defaults to 200
+ if(!status)
+ status=200;
+
+ //defaults to text/plain
+ if(!headers)
+ headers={
+ 'Content-type':'text/plain'
+ }
+
+ //defaults to utf8
+ if(!encoding)
+ encoding='utf8';
+
+ response.writeHead(
+ status,
+ headers
+ );
+ response.write(body,encoding);
+ response.end();
+ return;
+}
+
+function requestRecieved(request,response){
+ var uri = url.parse(request.url).pathname;
+ if (uri=='/')
+ uri='/'+conf.server.index;
+
+ var filename = path.join(
+ conf.rootDIR,
+ uri
+ );
+
+ fs.exists(
+ filename,
+ function(exists){
+ serveFile(filename,exists,response)
+ }
+ );
+} \ No newline at end of file