Skip to content

Commit

Permalink
Now listen to actual irc channels and insert into database
Browse files Browse the repository at this point in the history
  • Loading branch information
alFReD-NSH committed Mar 10, 2014
1 parent 4c661be commit fc5d7c5
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 5 deletions.
1 change: 1 addition & 0 deletions .meteor/packages
Expand Up @@ -7,3 +7,4 @@ standard-app-packages
insecure
preserve-inputs
coffeescript
npm
2 changes: 1 addition & 1 deletion .meteor/release
@@ -1 +1 @@
0.7.0.1
0.7.1.2
4 changes: 2 additions & 2 deletions client/index.coffee
Expand Up @@ -3,10 +3,10 @@ window.Messages = new Meteor.Collection('messages')
Template.chatTemplate.messages = ->
Messages.find()

Meteor.subscribe('channel', 'default')
Meteor.subscribe('channel', '#node.js')

$ ->
$('#channel').keyup (event) ->
value = event.target.value
if (value)
Meteor.subscribe('channel', value)
Meteor.subscribe('channel', value, $('#user').val())
3 changes: 3 additions & 0 deletions packages.json
@@ -0,0 +1,3 @@
{
"irc" : "0.3.6"
}
1 change: 1 addition & 0 deletions packages/npm/.gitignore
@@ -0,0 +1 @@
.build*
1 change: 1 addition & 0 deletions packages/npm/.npm/package/.gitignore
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions packages/npm/.npm/package/README
@@ -0,0 +1,7 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.

You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.
7 changes: 7 additions & 0 deletions packages/npm/.npm/package/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions packages/npm/index.js
@@ -0,0 +1,88 @@
var Future = Npm.require('fibers/future');
Async = {};

Meteor.require = function(moduleName) {
var module = Npm.require(moduleName);
return module;
};

Async.runSync = Meteor.sync = function(asynFunction) {
var future = new Future();
var sent = false;
var payload;

var wrappedAsyncFunction = Meteor.bindEnvironment(asynFunction, function(err) {
console.error('Error inside the Async.runSync: ' + err.message);
returnFuture(err);
});

setTimeout(function() {
wrappedAsyncFunction(returnFuture);
}, 0);

future.wait();
sent = true;

function returnFuture(error, result) {
if(!sent) {
payload = { result: result, error: error};
future.return();
}
}

return payload;
};

Async.wrap = function(arg1, arg2) {
if(typeof arg1 == 'function') {
var func = arg1;
return wrapFunction(func);
} else if(typeof arg1 == 'object' && typeof arg2 == 'string') {
var obj = arg1;
var funcName = arg2;
return wrapObject(obj, [funcName])[funcName];
} else if(typeof arg1 == 'object' && arg2 instanceof Array) {
var obj = arg1;
var funcNameList = arg2;
return wrapObject(obj, funcNameList);
} else {
throw new Error('unsupported argument list');
}

function wrapObject(obj, funcNameList) {
var returnObj = {};
funcNameList.forEach(function(funcName) {
if(obj[funcName]) {
var func = obj[funcName].bind(obj);
returnObj[funcName] = wrapFunction(func);
} else {
throw new Error('instance method not exists: ' + funcName);
}
});
return returnObj;
}

function wrapFunction(func) {
return function() {
var args = arguments;
response = Meteor.sync(function(done) {
Array.prototype.push.call(args, done);
func.apply(null, args);
});

if(response.error) {
//we need to wrap a new error here something throw error object comes with response does not
//print the correct error to the console, if there is not try catch block
var error = new Error(response.error.message);
for(var key in response.error) {
if(error[key] === undefined) {
error[key] = response.error[key];
}
}
throw error;
} else {
return response.result;
}
};
}
};
40 changes: 40 additions & 0 deletions packages/npm/package.js
@@ -0,0 +1,40 @@
var path = Npm.require('path');
var fs = Npm.require('fs');
var packagesJsonFile = path.resolve('./packages.json');

//creating `packages.json` file for the first-time if not exists
if(!fs.existsSync(packagesJsonFile)) {
fs.writeFileSync(packagesJsonFile, '{\n \n}')
}

try {
var fileContent = fs.readFileSync(packagesJsonFile);
var packages = JSON.parse(fileContent.toString());
Npm.depends(packages);
} catch(ex) {
console.error('ERROR: packages.json parsing error [ ' + ex.message + ' ]');
}

Package.describe({
summary: "complete npm integration/support for Meteor"
});

Package.on_use(function (api, where) {
api.export('Async');

var packagesFile = './.meteor/packages';
if(fs.existsSync(packagesFile) && isNewerMeteor) {
api.add_files(['index.js', '../../packages.json'], 'server');
} else {
api.add_files(['index.js'], 'server');
}

function isNewerMeteor() {
return fs.readFileSync(packagesFile, 'utf8').match(/\nstandard-app-packages/);
}
});

Package.on_test(function (api) {
api.use(['tinytest']);
api.add_files(['index.js', 'test.js'], 'server');
});
119 changes: 119 additions & 0 deletions packages/npm/test.js
@@ -0,0 +1,119 @@
Tinytest.add('Async.runSync - with done()', function(test) {
var output = Async.runSync(function(done) {
setTimeout(function() {
done(null, 10001);
}, 10);
});

test.equal(output.result, 10001);
test.equal(output.error, null);
});

Tinytest.add('Async.runSync - with error()', function(test) {
var output = Async.runSync(function(done) {
setTimeout(function() {
done({message: 'error-message', code: 402});
}, 10);
});

test.equal(output.result, undefined);
test.equal(output.error.code, 402);
});

Tinytest.add('Async.runSync - with error in the callback', function(test) {
var output = Async.runSync(function(done) {
throw new Error('SOME_ERROR');
});

test.equal(output.result, undefined);
test.equal(output.error.message, 'SOME_ERROR');
});

Tinytest.add('Async.wrap function mode - success', function(test) {
function wait(timeout, callback) {
setTimeout(function() {
callback(null, 'okay');
}, timeout);
};

var enclosedWait = Async.wrap(wait);
var output = enclosedWait(100);

test.equal(output, 'okay');
});

Tinytest.add('Async.wrap function mode - error', function(test) {
function wait(timeout, callback) {
setTimeout(function() {
var error = new Error('THE_ERROR');
error.code = 500;
callback(error);
}, timeout);
};

var enclosedWait = Async.wrap(wait);
try {
enclosedWait(100);
test.fail('there must be an error');
} catch(err) {
test.ok(err.message.match('THE_ERROR'));
test.equal(err.code, 500);
}

});

Tinytest.add('Async.wrap object mode - success', function(test) {
function Wait() {
this.start = function(timeout, callback) {
setTimeout(function() {
callback(null, 'okay');
}, timeout);
};
}

var wait = new Wait();

var enclosedWait = Async.wrap(wait, 'start');

var output = enclosedWait(100);
test.equal(output, 'okay');
});

Tinytest.add('Async.wrap object mode - funcName not exists', function(test) {
function Wait() {
this.start = function(timeout, callback) {
setTimeout(function() {
callback(null, 'okay');
}, timeout);
};
}

var wait = new Wait();
try {
var enclosedWait = Async.wrap(wait, 'startz');
test.fail('shoud throw an error');
} catch(ex) {

}
});

Tinytest.add('Async.wrap object mode - multi function mode', function(test) {
function Wait() {
this.start = function(timeout, callback) {
setTimeout(function() {
callback(null, 'okay');
}, timeout);
};

this.start2 = function(timeout, callback) {
setTimeout(function() {
callback(null, 'okay');
}, timeout);
};
}

var wait = new Wait();
var enclosedWait = Async.wrap(wait, ['start', 'start2']);
enclosedWait.start(100);
enclosedWait.start2(100);
});
22 changes: 20 additions & 2 deletions server/index.coffee
@@ -1,4 +1,22 @@
Messages = new Meteor.Collection('messages')
Meteor.publish 'channel', (channel) ->
if channel
irc = Meteor.require 'irc'
clients = {}
Meteor.publish 'channel', (channel, nick) ->
if channel && nick
listen channel, nick
Messages.find(channel : channel)
listen = (channel, nick) ->
console.log(channel, nick)
client = clients[nick] = new irc.Client 'chat.freenode.net', nick, {
channels : [channel]
}
client.on 'error', console.log
client.on 'message', (from, to, message) ->
console.log(from, to, message)
Fiber(->
Messages.insert {
from,
message
channel : to,
}
)

0 comments on commit fc5d7c5

Please sign in to comment.