diff --git a/.gitignore b/.gitignore index dd218dc7..32c11aff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ _build/ +*.swp diff --git a/lib/codes.js b/lib/codes.js index 2785bf32..b2b3e368 100644 --- a/lib/codes.js +++ b/lib/codes.js @@ -1,4 +1,4 @@ -module.exports = { // {{{ +window.codes = { // {{{ "001" : { "name" : "rpl_welcome", "type" : "reply" diff --git a/lib/irc.js b/lib/irc.js index f9ea02c4..6b3438fb 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -17,15 +17,43 @@ along with this library. If not, see . */ -exports.Client = Client; -var net = require('net'); -var tls = require('tls'); -var util = require('util'); - -var colors = require('./colors'); -exports.colors = colors; +window.Client = Client; +var net = { + createConnection: function (port, server) { + return { + _conn: navigator.mozTCPSocket.open(server, port), + addListener: function (evt, cb) { + if (evt === 'connect') { + evt = 'open'; + } else if (evt === 'end') { + evt = 'close'; + } + //console.log('trying to set on' + evt + ' on the connection'); + this._conn['on' + evt] = function (msg) { + //console.log('connection got an on' + evt + ' event'); + if(msg && 'data' in msg) { + cb(msg.data); + } else { + cb(); + } + }; + }, + setTimeout: function () {}, + setEncoding: function () {}, + write: function (data) { + console.log('sending: ' + data); + this._conn.send(data); + }, + }; + }, +}; +var tls = {}; +var util = { + log: console.log.bind(console), + inspect: console.log.bind(console), +}; -var replyFor = require('./codes'); +var replyFor = window.codes; function Client(server, nick, opt) { var self = this; @@ -94,6 +122,7 @@ function Client(server, nick, opt) { } self.addListener("raw", function (message) { // {{{ + //console.log('in raw event, got: ' + JSON.stringify(message)); switch ( message.command ) { case "rpl_welcome": // Set nick to whatever the server decided it really is @@ -557,12 +586,8 @@ function Client(server, nick, opt) { self.send.apply(self, ['JOIN'].concat(channel.split(' '))); }); }); - - process.EventEmitter.call(this); } -util.inherits(Client, process.EventEmitter); - Client.prototype.conn = null; Client.prototype.prefixForMode = {}; Client.prototype.modeForPrefix = {}; @@ -649,6 +674,7 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{ }); var buffer = ''; self.conn.addListener("data", function (chunk) { + console.log(chunk); buffer += chunk; var lines = buffer.split("\r\n"); buffer = lines.pop(); @@ -782,7 +808,7 @@ Client.prototype.part = function(channel, message, callback) { // {{{ if (this.opt.channels.indexOf(channel) != -1) { this.opt.channels.splice(this.opt.channels.indexOf(channel), 1); } - + if (message) { this.send('PART', channel, message); } else { @@ -861,6 +887,29 @@ Client.prototype._handleCTCP = function(from, to, text, type, message) { Client.prototype.ctcp = function(to, type, text) { return this[type === 'privmsg' ? 'say' : 'notice'](to, '\1'+text+'\1'); } +Client.prototype.addListener = function (evt, cb) { + //console.log('Listening for a ' + evt + ' event'); + document.addEventListener(evt, function (data) { + //console.log('heard a ' + evt + ' event'); + //console.log(JSON.stringify(data.detail)); + cb(data.detail.message); + }); +}; +Client.prototype.emit = function (evt, a, b, c, d, e) { + if (evt === 'connect') { + evt = 'open'; + } + //console.log('trying to emit ' + evt); + var e = new CustomEvent(evt, { detail: { message: a }}); + document.dispatchEvent(e); +}; +Client.prototype.once = function (evt, cb) { + //console.log('Listening once for a ' + evt + ' evt'); + var e = document.addEventListener(evt, function (data) { + document.removeEventListener(evt, e); + cb(data); + }); +}; /* * parseMessage(line, stripColors) @@ -924,4 +973,4 @@ function parseMessage(line, stripColors) { // {{{ return message; } // }}} -exports.parseMessage = parseMessage; +window.parseMessage = parseMessage;