diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/irc.js | 67 | ||||
-rw-r--r-- | src/main.js | 162 |
2 files changed, 229 insertions, 0 deletions
diff --git a/src/lib/irc.js b/src/lib/irc.js new file mode 100644 index 00000000..5f904a74 --- /dev/null +++ b/src/lib/irc.js @@ -0,0 +1,67 @@ + +var Client = function (config) { + var client = this; + var net = require('net'); + var sys = require('sys'); + var log = function (x) { + sys.puts('TCP server: ' + x); + }; + + client.connect = function (callback) { + var stream = net.createConnection(config.port, config.server); + stream.on('connect', function () { + stream.write( + 'NICK ' + config.nick + '\n' + + 'USER ' + config.nick + ' 0 *:Karl Koch\n' + + 'JOIN ' + config.channel + '\n' + ); + //client.write = function (text) { + // stream.write('PRIVMSG ' + config.channel + ' :' + text); + //}; + client.write = msg_start_send; + callback(); + }); + //stream.on('secure', function () { + //}); + + var msg = []; + + var msg_start_send = function (x) { + client.write = msg_append; + setTimeout(function () { + var x = msg.join('\n') + '\n'; + msg = []; + client.write = msg_start_send; + stream.write('PRIVMSG ' + config.channel + ' :' + x); + }, 1000); + }; + + var msg_append = function (x) { + msg[msg.length] = x; + }; + + + stream.on('data', function (data) { + data = String(data); + log('[35m' + data + '[m'); + if (data.substring(0,4) === 'PING') { + log('PONG!'); + stream.write('PONG ' + data.substring(4)); + } + }); + //stream.on('end', function () { + //}); + //stream.on('timeout', function () { + //}); + //stream.on('drain', function () { + //}); + //stream.on('error', function (exception) { + //}); + //stream.on('clonse', function (exception) { + //}); + }; +}; + +exports.createClient = function (config) { + return new Client(config); +}; diff --git a/src/main.js b/src/main.js new file mode 100644 index 00000000..b9e67a19 --- /dev/null +++ b/src/main.js @@ -0,0 +1,162 @@ + +var connect = require('genericore').connect; + +var config = { + irc: {} +}; + +connect(config.irc, { + debug: function (message) { + }, + ready: function (client) { + + } +}); + + + +// { +// userName: 'nodebot', +// realName: 'nodeJS IRC client', +// port: 6667, +// debug: false, +// showErrors: false, +// autoRejoin: true, +// channels: [], +// secure: false +// } + +var config = { + "irc": { + "nick": "a43243afds", + "server": "irc.freenode.net", + "port": 6667, + "channel": "#genericoredump" + }, + "amqp": { + "reconnect_timeout": 10000, + "connection": { + "host": "141.31.8.11", + "port": 5672, + "login": "shack", + "password": "shackit", + "vhost": "/" + }, + "exchange": { + "name": "log", + "options": { + "type": "fanout", + "passive": false, + "durable": false, + "auto_delete": false, + "internal": false, + "nowait": false + } + }, + "queue": { + "name": "irclog2", + "options": { + "passive": false, + "durable": false, + "exclusive": false, + "autoDelete": false, + "nowait": false + } + } + } +}; + +//var irc = require('./lib/irc'); +var irc = require('./lib/irc').createClient(config.irc); +var amqp = require('amqp'); + +// TODO var amqp = require('./lib/amqp').createClient(config.amqp); +// where createClient will bind to all connected (exchange,queue) pairs +// irc.connect({ +// ready: function () { +// amqp.connect({ +// message: function (message) { +// console.log(message); +// irc.privmsg(config.irc.channel, message.data); +// } +// }); +// } +// }); + +// TODO call back when joined +irc.connect(function () { + var connection = amqp.createConnection(config.amqp.connection); + connection.on('ready', function () { + var queue = connection.queue(config.amqp.queue.name, config.amqp.queue.options); + + queue.bind(config.amqp.exchange.name, config.amqp.queue.name); + + console.log('receiving messages'); + queue.subscribe(function (message) { + console.log(message.data); + irc.write(message.data); + }); + }); +}); + +// amqp.connect(function () { +// amqp.connection.exchange("log", config.amqp.exchange.options).on( +// 'open', function () { +// log = function (message) { +// exchange.publish(config.amqp.exchange.name, message); +// }; +// } +// ); +// +// tcp.serve(function (message) { +// var data = parse(message); +// log('[mailsrc,tcp] incoming: ' + data['Header-Fields']['Subject']); +// console.log('publishing: ' + data['Header-Fields'].From); +// amqp.publish({ type: 'mail', subtype: 0, data: data }); +// }); +// }); +// +// +// var client = new irc.Client(config.server, config.nick, { +// channels: [config.channel], +// }); +// +// client.on('error', function (err) { +// console.log('>>>\n' + require('sys').inspect(err)); +// }); +// +// +// var amqp = require('amqp'); +// client.join(config.channel, function () { +// +// var connection = amqp.createConnection(config.amqp.connection); +// +// // Wait for connection to become established. +// connection.on('ready', function () { +// // Create a queue and bind to all messages. +// // Use the default 'amq.topic' exchange +// var q = connection.queue(config.amqp.queue.name, config.amqp.queue); +// // Catch all messages +// q.bind(config.amqp.exchange.name, config.amqp.queue.name); +// +// // Receive messages +// console.log('receiving messages'); +// q.subscribe(function (message) { +// // Print messages to stdout +// console.log(message); +// client.say(config.channel, message.data); +// }); +// }); +// }); + + + + + +// client.on('pm', function (from, message) { +// sys.puts(from + ' => ME: ' + message); +// }); +// +// client.on('message#yourchannel', function (from, message) { +// sys.puts(from + ' => #yourchannel: ' + message); +// }); |