From ce13508bb0895e4f1dd310cd487e799f75fec5bb Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 2 Apr 2013 00:29:02 +0100 Subject: [PATCH] Network events + panels moved into model_network --- client/assets/dev/app.js | 25 +- client/assets/dev/build.js | 1 + client/assets/dev/model_application.js | 495 +-------------------- client/assets/dev/model_gateway.js | 12 + client/assets/dev/model_network.js | 588 +++++++++++++++++++++++++ client/assets/dev/model_panellist.js | 14 +- 6 files changed, 647 insertions(+), 488 deletions(-) create mode 100644 client/assets/dev/model_network.js diff --git a/client/assets/dev/app.js b/client/assets/dev/app.js index 046eb3f..46d5b94 100644 --- a/client/assets/dev/app.js +++ b/client/assets/dev/app.js @@ -26,27 +26,42 @@ _kiwi.global = { // Event managers for plugins components: { - EventComponent: function(event_source) { + EventComponent: function(event_source, proxy_event_name) { function proxyEvent(event_name, event_data) { + if (proxy_event_name !== 'all') { + event_data = event_name.event_data; + event_name = event_name.event_name + } +//console.log(proxy_event_name, event_name, event_data); this.trigger(event_name, event_data); } + // The event we are to proxy + proxy_event_name = proxy_event_name || 'all'; + + _.extend(this, Backbone.Events); this._source = event_source; // Proxy the events to this dispatcher - event_source.on('all', proxyEvent, this); + event_source.on(proxy_event_name, proxyEvent, this); // Clean up this object this.dispose = function () { - event_source.off('all', proxyEvent); + event_source.off(proxy_event_name, proxyEvent); this.off(); delete this.event_source; }; }, - Network: function() { - var obj = new this.EventComponent(_kiwi.gateway); + Network: function(connection_id) { + var connection_event; + + if (typeof connection_id !== 'undefined') { + connection_event = 'connection:' + connection_id.toString(); + } + + var obj = new this.EventComponent(_kiwi.gateway, connection_event); var funcs = { kiwi: 'kiwi', raw: 'raw', kick: 'kick', topic: 'topic', part: 'part', join: 'join', action: 'action', ctcp: 'ctcp', diff --git a/client/assets/dev/build.js b/client/assets/dev/build.js index e856a85..723db16 100644 --- a/client/assets/dev/build.js +++ b/client/assets/dev/build.js @@ -32,6 +32,7 @@ var src = concat([ __dirname + '/app.js', __dirname + '/model_application.js', __dirname + '/model_gateway.js', + __dirname + '/model_network.js', __dirname + '/model_member.js', __dirname + '/model_memberlist.js', __dirname + '/model_panel.js', diff --git a/client/assets/dev/model_application.js b/client/assets/dev/model_application.js index ba9313c..17815c3 100644 --- a/client/assets/dev/model_application.js +++ b/client/assets/dev/model_application.js @@ -34,6 +34,12 @@ _kiwi.model.Application = function () { // Best guess at where the kiwi server is this.detectKiwiServer(); + + // Holds instances of model_network + this.connections = []; + + // The active network (reference to a this.connections element) + this.active_connection = null; }; this.start = function () { @@ -107,10 +113,17 @@ _kiwi.model.Application = function () { this.view = new _kiwi.view.Application({model: this, el: this.get('container')}); /** - * Set the UI components up + * This is temporary. + * While multiple server support is being worked on, + * we will keep this single server variable here until + * It all gets moved over */ - this.panels = new _kiwi.model.PanelList(); + _kiwi.app.connections[0] = new _kiwi.model.Network(0); + this.panels = _kiwi.app.connections[0].panels; + /** + * Set the UI components up + */ this.controlbox = new _kiwi.view.ControlBox({el: $('#controlbox')[0]}); this.bindControllboxCommands(this.controlbox); @@ -281,14 +294,9 @@ _kiwi.model.Application = function () { this.bindGatewayCommands = function (gw) { - gw.on('onmotd', function (event) { - that.panels.server.addMsg(_kiwi.gateway.get('name'), event.msg, 'motd'); - }); - - gw.on('onconnect', function (event) { that.view.barsShow(); - + if (auto_connect_details.channel) { that.controlbox.processInput('/JOIN ' + auto_connect_details.channel + ' ' + auto_connect_details.channel_key); } @@ -332,477 +340,6 @@ _kiwi.model.Application = function () { }); })(); - - gw.on('onjoin', function (event) { - var c, members, user; - c = that.panels.getByName(event.channel); - if (!c) { - c = new _kiwi.model.Channel({name: event.channel}); - that.panels.add(c); - } - - members = c.get('members'); - if (!members) return; - - user = new _kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname}); - members.add(user); - // TODO: highlight the new channel in some way - }); - - - gw.on('onpart', function (event) { - var channel, members, user, - part_options = {}; - - part_options.type = 'part'; - part_options.message = event.message || ''; - - channel = that.panels.getByName(event.channel); - if (!channel) return; - - // If this is us, close the panel - if (event.nick === _kiwi.gateway.get('nick')) { - channel.close(); - return; - } - - members = channel.get('members'); - if (!members) return; - - user = members.getByNick(event.nick); - if (!user) return; - - members.remove(user, part_options); - }); - - - gw.on('onquit', function (event) { - var member, members, - quit_options = {}; - - quit_options.type = 'quit'; - quit_options.message = event.message || ''; - - $.each(that.panels.models, function (index, panel) { - if (!panel.isChannel()) return; - - member = panel.get('members').getByNick(event.nick); - if (member) { - panel.get('members').remove(member, quit_options); - } - }); - }); - - - gw.on('onkick', function (event) { - var channel, members, user, - part_options = {}; - - part_options.type = 'kick'; - part_options.by = event.nick; - part_options.message = event.message || ''; - - channel = that.panels.getByName(event.channel); - if (!channel) return; - - members = channel.get('members'); - if (!members) return; - - user = members.getByNick(event.kicked); - if (!user) return; - - members.remove(user, part_options); - - if (event.kicked === _kiwi.gateway.get('nick')) { - members.reset([]); - } - - }); - - - gw.on('onmsg', function (event) { - var panel, - is_pm = (event.channel == _kiwi.gateway.get('nick')); - - // An ignored user? don't do anything with it - if (gw.isNickIgnored(event.nick)) { - return; - } - - if (is_pm) { - // If a panel isn't found for this PM, create one - panel = that.panels.getByName(event.nick); - if (!panel) { - panel = new _kiwi.model.Query({name: event.nick}); - that.panels.add(panel); - } - - } else { - // If a panel isn't found for this channel, reroute to the - // server panel - panel = that.panels.getByName(event.channel); - if (!panel) { - panel = that.panels.server; - } - } - - panel.addMsg(event.nick, event.msg); - }); - - - gw.on('onctcp_request', function (event) { - // An ignored user? don't do anything with it - if (gw.isNickIgnored(event.nick)) { - return; - } - - // Reply to a TIME ctcp - if (event.msg.toUpperCase() === 'TIME') { - gw.ctcp(false, event.type, event.nick, (new Date()).toString()); - } - }); - - - gw.on('onctcp_response', function (event) { - // An ignored user? don't do anything with it - if (gw.isNickIgnored(event.nick)) { - return; - } - - that.panels.server.addMsg('[' + event.nick + ']', 'CTCP ' + event.msg); - }); - - - gw.on('onnotice', function (event) { - var panel; - - // An ignored user? don't do anything with it - if (event.nick && gw.isNickIgnored(event.nick)) { - return; - } - - // Find a panel for the destination(channel) or who its from - panel = that.panels.getByName(event.target) || that.panels.getByName(event.nick); - if (!panel) { - panel = that.panels.server; - } - - panel.addMsg('[' + (event.nick||'') + ']', event.msg); - }); - - - gw.on('onaction', function (event) { - var panel, - is_pm = (event.channel == _kiwi.gateway.get('nick')); - - // An ignored user? don't do anything with it - if (gw.isNickIgnored(event.nick)) { - return; - } - - if (is_pm) { - // If a panel isn't found for this PM, create one - panel = that.panels.getByName(event.nick); - if (!panel) { - panel = new _kiwi.model.Channel({name: event.nick}); - that.panels.add(panel); - } - - } else { - // If a panel isn't found for this channel, reroute to the - // server panel - panel = that.panels.getByName(event.channel); - if (!panel) { - panel = that.panels.server; - } - } - - panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action'); - }); - - - gw.on('ontopic', function (event) { - var c; - c = that.panels.getByName(event.channel); - if (!c) return; - - // Set the channels topic - c.set('topic', event.topic); - - // If this is the active channel, update the topic bar too - if (c.get('name') === _kiwi.app.panels.active.get('name')) { - that.topicbar.setCurrentTopic(event.topic); - } - }); - - - gw.on('ontopicsetby', function (event) { - var c, when; - c = that.panels.getByName(event.channel); - if (!c) return; - - when = formatDate(new Date(event.when * 1000)); - c.addMsg('', 'Topic set by ' + event.nick + ' at ' + when, 'topic'); - }); - - - gw.on('onuserlist', function (event) { - var channel; - channel = that.panels.getByName(event.channel); - - // If we didn't find a channel for this, may aswell leave - if (!channel) return; - - channel.temp_userlist = channel.temp_userlist || []; - _.each(event.users, function (item) { - var user = new _kiwi.model.Member({nick: item.nick, modes: item.modes}); - channel.temp_userlist.push(user); - }); - }); - - - gw.on('onuserlist_end', function (event) { - var channel; - channel = that.panels.getByName(event.channel); - - // If we didn't find a channel for this, may aswell leave - if (!channel) return; - - // Update the members list with the new list - channel.get('members').reset(channel.temp_userlist || []); - - // Clear the temporary userlist - delete channel.temp_userlist; - }); - - - gw.on('onmode', function (event) { - var channel, i, prefixes, members, member, find_prefix; - - // Build a nicely formatted string to be displayed to a regular human - function friendlyModeString (event_modes, alt_target) { - var modes = {}, return_string; - - // If no default given, use the main event info - if (!event_modes) { - event_modes = event.modes; - alt_target = event.target; - } - - // Reformat the mode object to make it easier to work with - _.each(event_modes, function (mode){ - var param = mode.param || alt_target || ''; - - // Make sure we have some modes for this param - if (!modes[param]) { - modes[param] = {'+':'', '-':''}; - } - - modes[param][mode.mode[0]] += mode.mode.substr(1); - }); - - // Put the string together from each mode - return_string = []; - _.each(modes, function (modeset, param) { - var str = ''; - if (modeset['+']) str += '+' + modeset['+']; - if (modeset['-']) str += '-' + modeset['-']; - return_string.push(str + ' ' + param); - }); - return_string = return_string.join(', '); - - return return_string; - } - - - channel = that.panels.getByName(event.target); - if (channel) { - prefixes = _kiwi.gateway.get('user_prefixes'); - find_prefix = function (p) { - return event.modes[i].mode[1] === p.mode; - }; - for (i = 0; i < event.modes.length; i++) { - if (_.any(prefixes, find_prefix)) { - if (!members) { - members = channel.get('members'); - } - member = members.getByNick(event.modes[i].param); - if (!member) { - console.log('MODE command recieved for unknown member %s on channel %s', event.modes[i].param, event.target); - return; - } else { - if (event.modes[i].mode[0] === '+') { - member.addMode(event.modes[i].mode[1]); - } else if (event.modes[i].mode[0] === '-') { - member.removeMode(event.modes[i].mode[1]); - } - members.sort(); - //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode'); - } - } else { - // Channel mode being set - // TODO: Store this somewhere? - //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode'); - } - } - - channel.addMsg('', '== ' + event.nick + ' sets mode ' + friendlyModeString(), 'action mode'); - } else { - // This is probably a mode being set on us. - if (event.target.toLowerCase() === _kiwi.gateway.get("nick").toLowerCase()) { - that.panels.server.addMsg('', '== ' + event.nick + ' set mode ' + friendlyModeString(), 'action mode'); - } else { - console.log('MODE command recieved for unknown target %s: ', event.target, event); - } - } - }); - - - gw.on('onnick', function (event) { - var member; - - $.each(that.panels.models, function (index, panel) { - if (!panel.isChannel()) return; - - member = panel.get('members').getByNick(event.nick); - if (member) { - member.set('nick', event.newnick); - panel.addMsg('', '== ' + event.nick + ' is now known as ' + event.newnick, 'action nick'); - } - }); - }); - - - gw.on('onwhois', function (event) { - /*globals secondsToTime */ - var logon_date, idle_time = '', panel; - - if (event.end) { - return; - } - - if (typeof event.idle !== 'undefined') { - idle_time = secondsToTime(parseInt(event.idle, 10)); - idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0"); - } - - panel = _kiwi.app.panels.active; - if (event.ident) { - panel.addMsg(event.nick, event.nick + ' [' + event.nick + '!' + event.ident + '@' + event.host + '] * ' + event.msg, 'whois'); - } else if (event.chans) { - panel.addMsg(event.nick, 'Channels: ' + event.chans, 'whois'); - } else if (event.irc_server) { - panel.addMsg(event.nick, 'Connected to server: ' + event.irc_server, 'whois'); - } else if (event.msg) { - panel.addMsg(event.nick, event.msg, 'whois'); - } else if (event.logon) { - logon_date = new Date(); - logon_date.setTime(event.logon * 1000); - logon_date = formatDate(logon_date); - - panel.addMsg(event.nick, 'idle for ' + idle_time + ', signed on ' + logon_date, 'whois'); - } else { - panel.addMsg(event.nick, 'idle for ' + idle_time, 'whois'); - } - }); - - gw.on('onaway', function (event) { - $.each(that.panels.models, function (index, panel) { - if (!panel.isChannel()) return; - - member = panel.get('members').getByNick(event.nick); - if (member) { - member.set('away', !(!event.trailing)); - } - }); - }); - - - gw.on('onlist_start', function (data) { - if (_kiwi.app.channel_list) { - _kiwi.app.channel_list.close(); - delete _kiwi.app.channel_list; - } - - var panel = new _kiwi.model.Applet(), - applet = new _kiwi.applets.Chanlist(); - - panel.load(applet); - - _kiwi.app.panels.add(panel); - panel.view.show(); - - _kiwi.app.channel_list = applet; - }); - - - gw.on('onlist_channel', function (data) { - // TODO: Put this listener within the applet itself - _kiwi.app.channel_list.addChannel(data.chans); - }); - - - gw.on('onlist_end', function (data) { - // TODO: Put this listener within the applet itself - delete _kiwi.app.channel_list; - }); - - - gw.on('onirc_error', function (data) { - var panel, tmp; - - if (data.channel !== undefined && !(panel = _kiwi.app.panels.getByName(data.channel))) { - panel = _kiwi.app.panels.server; - } - - switch (data.error) { - case 'banned_from_channel': - panel.addMsg(' ', '== You are banned from ' + data.channel + '. ' + data.reason, 'status'); - _kiwi.app.message.text('You are banned from ' + data.channel + '. ' + data.reason); - break; - case 'bad_channel_key': - panel.addMsg(' ', '== Bad channel key for ' + data.channel, 'status'); - _kiwi.app.message.text('Bad channel key or password for ' + data.channel); - break; - case 'invite_only_channel': - panel.addMsg(' ', '== ' + data.channel + ' is invite only.', 'status'); - _kiwi.app.message.text(data.channel + ' is invite only'); - break; - case 'channel_is_full': - panel.addMsg(' ', '== ' + data.channel + ' is full.', 'status'); - _kiwi.app.message.text(data.channel + ' is full'); - break; - case 'chanop_privs_needed': - panel.addMsg(' ', '== ' + data.reason, 'status'); - _kiwi.app.message.text(data.reason + ' (' + data.channel + ')'); - break; - case 'no_such_nick': - tmp = _kiwi.app.panels.getByName(data.nick); - if (tmp) { - tmp.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status'); - } else { - _kiwi.app.panels.server.addMsg(' ', '== ' + data.nick + ': ' + data.reason, 'status'); - } - break; - case 'nickname_in_use': - _kiwi.app.panels.server.addMsg(' ', '== The nickname ' + data.nick + ' is already in use. Please select a new nickname', 'status'); - if (_kiwi.app.panels.server !== _kiwi.app.panels.active) { - _kiwi.app.message.text('The nickname "' + data.nick + '" is already in use. Please select a new nickname'); - } - - // Only show the nickchange component if the controlbox is open - if (that.controlbox.$el.css('display') !== 'none') { - (new _kiwi.view.NickChangeBox()).render(); - } - - case 'password_mismatch': - _kiwi.app.panels.server.addMsg(' ', '== Incorrect password given', 'status'); - break; - default: - // We don't know what data contains, so don't do anything with it. - //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status'); - } - }); }; diff --git a/client/assets/dev/model_gateway.js b/client/assets/dev/model_gateway.js index 19fd043..0a4bec9 100644 --- a/client/assets/dev/model_gateway.js +++ b/client/assets/dev/model_gateway.js @@ -257,6 +257,7 @@ _kiwi.model.Gateway = function () { */ this.parse = function (command, data) { //console.log('gateway event', command, data); + if (command !== undefined) { that.trigger('on' + command, data); @@ -279,6 +280,9 @@ _kiwi.model.Gateway = function () { break; case 'connect': + //if (!_kiwi.app.connections[data.server]) { + // _kiwi.app.connections[data.server] = new _kiwi.model.Network(data.server); + //} that.set('nick', data.nick); break; @@ -301,6 +305,14 @@ _kiwi.model.Gateway = function () { break; } } + + + if (typeof data.server !== 'undefined') { + that.trigger('connection:' + data.server.toString(), { + event_name: command, + event_data: data + }); + } }; /** diff --git a/client/assets/dev/model_network.js b/client/assets/dev/model_network.js new file mode 100644 index 0000000..02a2eb2 --- /dev/null +++ b/client/assets/dev/model_network.js @@ -0,0 +1,588 @@ +(function () { + + _kiwi.model.Network = Backbone.Model.extend({ + defaults: { + connection_id: 0, + /** + * The name of the network + * @type String + */ + name: 'Network', + + /** + * The address (URL) of the network + * @type String + */ + address: '', + + /** + * The current nickname + * @type String + */ + nick: '', + + /** + * The channel prefix for this network + * @type String + */ + channel_prefix: '#', + + /** + * The user prefixes for channel owner/admin/op/voice etc. on this network + * @type Array + */ + user_prefixes: ['~', '&', '@', '+'] + }, + + + initialize: function () { + this.gateway = _kiwi.global.components.Network(this.get('connection_id')); + this.bindGatewayEvents(); + + this.panels = new _kiwi.model.PanelList(); + }, + + + bindGatewayEvents: function () { + //this.gateway.on('all', function() {console.log('ALL', arguments);}); + + this.gateway.on('connect', function(event) { + this.set('nick', event.nick); + }, this); + + this.gateway.on('nick', function(event) { + if (event.nick === event.get('nick')) { + this.set('nick', event.newnick); + } + }, this); + + this.gateway.on('options', onOptions, this); + this.gateway.on('motd', onMotd, this); + this.gateway.on('join', onJoin, this); + this.gateway.on('part', onPart, this); + this.gateway.on('quit', onQuit, this); + this.gateway.on('kick', onKick, this); + this.gateway.on('msg', onMsg, this); + this.gateway.on('ctcp_request', onCtcpRequest, this); + this.gateway.on('ctcp_response', onCtcpResponse, this); + this.gateway.on('notice', onNotice, this); + this.gateway.on('action', onAction, this); + this.gateway.on('topic', onTopic, this); + this.gateway.on('topicsetby', onTopicSetBy, this); + this.gateway.on('userlist', onUserlist, this); + this.gateway.on('userlist_end', onUserlistEnd, this); + this.gateway.on('mode', onMode, this); + this.gateway.on('whois', onWhois, this); + this.gateway.on('away', onAway, this); + this.gateway.on('list_start', onListStart, this); + this.gateway.on('list_channel', onListChannel, this); + this.gateway.on('list_end', onListEnd, this); + } + }); + + + + function onOptions(event) { + var that = this; + + $.each(event.options, function (name, value) { + switch (name) { + case 'CHANTYPES': + that.set('channel_prefix', value.join('')); + break; + case 'NETWORK': + that.set('name', value); + break; + case 'PREFIX': + that.set('user_prefixes', value); + break; + } + }); + + this.set('cap', event.cap); + } + + + + function onMotd(event) { + this.panels.server.addMsg(this.get('name'), event.msg, 'motd'); + } + + + + function onJoin(event) { + var c, members, user; + c = this.panels.getByName(event.channel); + if (!c) { + c = new _kiwi.model.Channel({name: event.channel}); + this.panels.add(c); + } + + members = c.get('members'); + if (!members) return; + + user = new _kiwi.model.Member({nick: event.nick, ident: event.ident, hostname: event.hostname}); + members.add(user); + } + + + + function onPart(event) { + var channel, members, user, + part_options = {}; + + part_options.type = 'part'; + part_options.message = event.message || ''; + + channel = this.panels.getByName(event.channel); + if (!channel) return; + + // If this is us, close the panel + if (event.nick === this.get('nick')) { + channel.close(); + return; + } + + members = channel.get('members'); + if (!members) return; + + user = members.getByNick(event.nick); + if (!user) return; + + members.remove(user, part_options); + } + + + + function onQuit(event) { + var member, members, + quit_options = {}; + + quit_options.type = 'quit'; + quit_options.message = event.message || ''; + + $.each(this.panels.models, function (index, panel) { + if (!panel.isChannel()) return; + + member = panel.get('members').getByNick(event.nick); + if (member) { + panel.get('members').remove(member, quit_options); + } + }); + } + + + + function onKick(event) { + var channel, members, user, + part_options = {}; + + part_options.type = 'kick'; + part_options.by = event.nick; + part_options.message = event.message || ''; + + channel = this.panels.getByName(event.channel); + if (!channel) return; + + members = channel.get('members'); + if (!members) return; + + user = members.getByNick(event.kicked); + if (!user) return; + + members.remove(user, part_options); + + if (event.kicked === this.get('nick')) { + members.reset([]); + } + } + + + + function onMsg(event) { + var panel, + is_pm = (event.channel == this.get('nick')); + + // An ignored user? don't do anything with it + if (_kiwi.gateway.isNickIgnored(event.nick)) { + return; + } + + if (is_pm) { + // If a panel isn't found for this PM, create one + panel = this.panels.getByName(event.nick); + if (!panel) { + panel = new _kiwi.model.Query({name: event.nick}); + this.panels.add(panel); + } + + } else { + // If a panel isn't found for this channel, reroute to the + // server panel + panel = this.panels.getByName(event.channel); + if (!panel) { + panel = this.panels.server; + } + } + + panel.addMsg(event.nick, event.msg); + } + + + + function onCtcpRequest(event) { + // An ignored user? don't do anything with it + if (_kiwi.gateway.isNickIgnored(event.nick)) { + return; + } + + // Reply to a TIME ctcp + if (event.msg.toUpperCase() === 'TIME') { + this.gateway.ctcp(false, event.type, event.nick, (new Date()).toString()); + } + } + + + + function onCtcpResponse(event) { + // An ignored user? don't do anything with it + if (_kiwi.gateway.isNickIgnored(event.nick)) { + return; + } + + this.panels.server.addMsg('[' + event.nick + ']', 'CTCP ' + event.msg); + } + + + + function onNotice(event) { + var panel; + + // An ignored user? don't do anything with it + if (event.nick && _kiwi.gateway.isNickIgnored(event.nick)) { + return; + } + + // Find a panel for the destination(channel) or who its from + panel = this.panels.getByName(event.target) || this.panels.getByName(event.nick); + if (!panel) { + panel = this.panels.server; + } + + panel.addMsg('[' + (event.nick||'') + ']', event.msg); + } + + + + function onAction(event) { + var panel, + is_pm = (event.channel == this.get('nick')); + + // An ignored user? don't do anything with it + if (_kiwi.gateway.isNickIgnored(event.nick)) { + return; + } + + if (is_pm) { + // If a panel isn't found for this PM, create one + panel = this.panels.getByName(event.nick); + if (!panel) { + panel = new _kiwi.model.Channel({name: event.nick}); + this.panels.add(panel); + } + + } else { + // If a panel isn't found for this channel, reroute to the + // server panel + panel = this.panels.getByName(event.channel); + if (!panel) { + panel = this.panels.server; + } + } + + panel.addMsg('', '* ' + event.nick + ' ' + event.msg, 'action'); + } + + + + function onTopic(event) { + var c; + c = this.panels.getByName(event.channel); + if (!c) return; + + // Set the channels topic + c.set('topic', event.topic); + + // If this is the active channel, update the topic bar too + if (c.get('name') === this.panels.active.get('name')) { + _kiwi.app.topicbar.setCurrentTopic(event.topic); + } + } + + + + function onTopicSetBy(event) { + var c, when; + c = this.panels.getByName(event.channel); + if (!c) return; + + when = formatDate(new Date(event.when * 1000)); + c.addMsg('', 'Topic set by ' + event.nick + ' at ' + when, 'topic'); + } + + + + function onUserlist(event) { + var channel; + channel = this.panels.getByName(event.channel); + + // If we didn't find a channel for this, may aswell leave + if (!channel) return; + + channel.temp_userlist = channel.temp_userlist || []; + _.each(event.users, function (item) { + var user = new _kiwi.model.Member({nick: item.nick, modes: item.modes}); + channel.temp_userlist.push(user); + }); + } + + + + function onUserlistEnd(event) { + var channel; + channel = this.panels.getByName(event.channel); + + // If we didn't find a channel for this, may aswell leave + if (!channel) return; + + // Update the members list with the new list + channel.get('members').reset(channel.temp_userlist || []); + + // Clear the temporary userlist + delete channel.temp_userlist; + } + + + + function onMode(event) { + var channel, i, prefixes, members, member, find_prefix; + + // Build a nicely formatted string to be displayed to a regular human + function friendlyModeString (event_modes, alt_target) { + var modes = {}, return_string; + + // If no default given, use the main event info + if (!event_modes) { + event_modes = event.modes; + alt_target = event.target; + } + + // Reformat the mode object to make it easier to work with + _.each(event_modes, function (mode){ + var param = mode.param || alt_target || ''; + + // Make sure we have some modes for this param + if (!modes[param]) { + modes[param] = {'+':'', '-':''}; + } + + modes[param][mode.mode[0]] += mode.mode.substr(1); + }); + + // Put the string together from each mode + return_string = []; + _.each(modes, function (modeset, param) { + var str = ''; + if (modeset['+']) str += '+' + modeset['+']; + if (modeset['-']) str += '-' + modeset['-']; + return_string.push(str + ' ' + param); + }); + return_string = return_string.join(', '); + + return return_string; + } + + + channel = this.panels.getByName(event.target); + if (channel) { + prefixes = this.get('user_prefixes'); + find_prefix = function (p) { + return event.modes[i].mode[1] === p.mode; + }; + for (i = 0; i < event.modes.length; i++) { + if (_.any(prefixes, find_prefix)) { + if (!members) { + members = channel.get('members'); + } + member = members.getByNick(event.modes[i].param); + if (!member) { + console.log('MODE command recieved for unknown member %s on channel %s', event.modes[i].param, event.target); + return; + } else { + if (event.modes[i].mode[0] === '+') { + member.addMode(event.modes[i].mode[1]); + } else if (event.modes[i].mode[0] === '-') { + member.removeMode(event.modes[i].mode[1]); + } + members.sort(); + //channel.addMsg('', '== ' + event.nick + ' set mode ' + event.modes[i].mode + ' ' + event.modes[i].param, 'action mode'); + } + } else { + // Channel mode being set + // TODO: Store this somewhere? + //channel.addMsg('', 'CHANNEL === ' + event.nick + ' set mode ' + event.modes[i].mode + ' on ' + event.target, 'action mode'); + } + } + + channel.addMsg('', '== ' + event.nick + ' sets mode ' + friendlyModeString(), 'action mode'); + } else { + // This is probably a mode being set on us. + if (event.target.toLowerCase() === this.get("nick").toLowerCase()) { + this.panels.server.addMsg('', '== ' + event.nick + ' set mode ' + friendlyModeString(), 'action mode'); + } else { + console.log('MODE command recieved for unknown target %s: ', event.target, event); + } + } + } + + + + function onWhois(event) { + var logon_date, idle_time = '', panel; + + if (event.end) + return; + + if (typeof event.idle !== 'undefined') { + idle_time = secondsToTime(parseInt(event.idle, 10)); + idle_time = idle_time.h.toString().lpad(2, "0") + ':' + idle_time.m.toString().lpad(2, "0") + ':' + idle_time.s.toString().lpad(2, "0"); + } + + panel = _kiwi.app.panels.active; + if (event.ident) { + panel.addMsg(event.nick, event.nick + ' [' + event.nick + '!' + event.ident + '@' + event.host + '] * ' + event.msg, 'whois'); + } else if (event.chans) { + panel.addMsg(event.nick, 'Channels: ' + event.chans, 'whois'); + } else if (event.irc_server) { + panel.addMsg(event.nick, 'Connected to server: ' + event.irc_server, 'whois'); + } else if (event.msg) { + panel.addMsg(event.nick, event.msg, 'whois'); + } else if (event.logon) { + logon_date = new Date(); + logon_date.setTime(event.logon * 1000); + logon_date = formatDate(logon_date); + + panel.addMsg(event.nick, 'idle for ' + idle_time + ', signed on ' + logon_date, 'whois'); + } else { + panel.addMsg(event.nick, 'idle for ' + idle_time, 'whois'); + } + } + + + + function onAway(event) { + $.each(this.panels.models, function (index, panel) { + if (!panel.isChannel()) return; + + member = panel.get('members').getByNick(event.nick); + if (member) { + member.set('away', !(!event.trailing)); + } + }); + } + + + + function onListStart(event) { + if (_kiwi.app.channel_list) { + _kiwi.app.channel_list.close(); + delete _kiwi.app.channel_list; + } + + var panel = new _kiwi.model.Applet(), + applet = new _kiwi.applets.Chanlist(); + + panel.load(applet); + + _kiwi.app.panels.add(panel); + panel.view.show(); + + _kiwi.app.channel_list = applet; + } + + + + function onListChannel(event) { + // TODO: Put this listener within the applet itself + _kiwi.app.channel_list.addChannel(event.chans); + } + + + + function onListEnd(event) { + // TODO: Put this listener within the applet itself + delete _kiwi.app.channel_list; + } + + + + function onIrcError(event) { + var panel, tmp; + + if (event.channel !== undefined && !(panel = _kiwi.app.panels.getByName(event.channel))) { + panel = this.panels.server; + } + + switch (event.error) { + case 'banned_from_channel': + panel.addMsg(' ', '== You are banned from ' + event.channel + '. ' + event.reason, 'status'); + _kiwi.app.message.text('You are banned from ' + event.channel + '. ' + event.reason); + break; + case 'bad_channel_key': + panel.addMsg(' ', '== Bad channel key for ' + event.channel, 'status'); + _kiwi.app.message.text('Bad channel key or password for ' + event.channel); + break; + case 'invite_only_channel': + panel.addMsg(' ', '== ' + event.channel + ' is invite only.', 'status'); + _kiwi.app.message.text(event.channel + ' is invite only'); + break; + case 'channel_is_full': + panel.addMsg(' ', '== ' + event.channel + ' is full.', 'status'); + _kiwi.app.message.text(event.channel + ' is full'); + break; + case 'chanop_privs_needed': + panel.addMsg(' ', '== ' + event.reason, 'status'); + _kiwi.app.message.text(event.reason + ' (' + event.channel + ')'); + break; + case 'no_such_nick': + tmp = this.panels.getByName(event.nick); + if (tmp) { + tmp.addMsg(' ', '== ' + event.nick + ': ' + event.reason, 'status'); + } else { + this.panels.server.addMsg(' ', '== ' + event.nick + ': ' + event.reason, 'status'); + } + break; + case 'nickname_in_use': + this.panels.server.addMsg(' ', '== The nickname ' + event.nick + ' is already in use. Please select a new nickname', 'status'); + if (this.panels.server !== thia.panels.active) { + _kiwi.app.message.text('The nickname "' + event.nick + '" is already in use. Please select a new nickname'); + } + + // Only show the nickchange component if the controlbox is open + if (that.controlbox.$el.css('display') !== 'none') { + (new _kiwi.view.NickChangeBox()).render(); + } + + break; + + case 'password_mismatch': + this.panels.server.addMsg(' ', '== Incorrect password given', 'status'); + break; + default: + // We don't know what data contains, so don't do anything with it. + //_kiwi.front.tabviews.server.addMsg(null, ' ', '== ' + data, 'status'); + } + } +} + +)(); \ No newline at end of file diff --git a/client/assets/dev/model_panellist.js b/client/assets/dev/model_panellist.js index d95d58f..cd43e19 100644 --- a/client/assets/dev/model_panellist.js +++ b/client/assets/dev/model_panellist.js @@ -2,14 +2,16 @@ _kiwi.model.PanelList = Backbone.Collection.extend({ model: _kiwi.model.Panel, comparator: function (chan) { - return chan.get("name"); + return chan.get('name'); }, initialize: function () { - this.view = new _kiwi.view.Tabs({"el": $('#tabs')[0], "model": this}); + this.view = new _kiwi.view.Tabs({el: $('#tabs')[0], model: this}); // Automatically create a server tab - this.add(new _kiwi.model.Server({'name': _kiwi.gateway.get('name')})); - this.server = this.getByName(_kiwi.gateway.get('name')); + var server_panel = new _kiwi.model.Server({name: 'Server'}); + + this.add(server_panel); + this.server = server_panel; // Holds the active panel this.active = null; @@ -20,8 +22,12 @@ _kiwi.model.PanelList = Backbone.Collection.extend({ }, this); }, + + + getByName: function (name) { if (typeof name !== 'string') return; + return this.find(function (c) { return name.toLowerCase() === c.get('name').toLowerCase(); }); -- 2.25.1