Notices into the active panel
[KiwiIRC.git] / client / assets / dev / model_network.js
index 56f64ccf9c56b0d8e9feb3ba797d6a26074167f5..a085327bc0aafc6bf3736025eb537a4b410a582b 100644 (file)
             this.gateway = _kiwi.global.components.Network(this.get('connection_id'));
             this.bindGatewayEvents();
 
-            this.panels = new _kiwi.model.PanelList(this);
+            // Create our panel list (tabs)
+            this.panels = new _kiwi.model.PanelList([], this);
+            //this.panels.network = this;
+
+            // Automatically create a server tab
+            var server_panel = new _kiwi.model.Server({name: 'Server'});
+            this.panels.add(server_panel);
+            this.panels.server = this.panels.active = server_panel;
         },
 
 
         bindGatewayEvents: function () {
-            //this.gateway.on('all', function() {console.log('ALL', arguments);});
+            //this.gateway.on('all', function() {console.log('ALL', this.get('connection_id'), arguments);});
 
-            this.gateway.on('connect', function(event) {
-                this.set('nick', event.nick);
-            }, this);
+            this.gateway.on('connect', onConnect, this);
+            this.gateway.on('disconnect', onDisconnect, this);
 
             this.gateway.on('nick', function(event) {
-                if (event.nick === event.get('nick')) {
+                if (event.nick === this.get('nick')) {
                     this.set('nick', event.newnick);
                 }
             }, this);
@@ -63,6 +69,7 @@
             this.gateway.on('quit', onQuit, this);
             this.gateway.on('kick', onKick, this);
             this.gateway.on('msg', onMsg, this);
+            this.gateway.on('nick', onNick, this);
             this.gateway.on('ctcp_request', onCtcpRequest, this);
             this.gateway.on('ctcp_response', onCtcpResponse, this);
             this.gateway.on('notice', onNotice, 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);
+        },
+
+
+        /**
+         * Create panels and join the channel
+         * This will not wait for the join event to create a panel. This
+         * increases responsiveness in case of network lag
+         */
+        createAndJoinChannels: function (channels) {
+            var that = this,
+                panels = [];
+
+            // Multiple channels may come as comma-delimited 
+            if (typeof channels === 'string') {
+                channels = channels.split(',');
+            }
+
+            $.each(channels, function (index, channel_name_key) {
+                // We may have a channel key so split it off
+                var spli = channel_name_key.trim().split(' '),
+                    channel_name = spli[0],
+                    channel_key = spli[1] || '';
+
+                // Trim any whitespace off the name
+                channel_name = channel_name.trim();
+
+                // If not a valid channel name, display a warning
+                if (!_kiwi.app.isChannelName(channel_name)) {
+                    that.panels.server.addMsg('', channel_name + ' is not a valid channel name');
+                    _kiwi.app.message.text(channel_name + ' is not a valid channel name', {timeout: 5000});
+                    return;
+                }
+
+                // Check if we have the panel already. If not, create it
+                channel = that.panels.getByName(channel_name);
+                if (!channel) {
+                    channel = new _kiwi.model.Channel({name: channel_name});
+                    that.panels.add(channel);
+                }
+
+                panels.push(channel);
+
+                that.gateway.join(channel_name, channel_key);
+            });
+
+            return panels;
         }
     });
 
 
+    
+    function onDisconnect(event) {
+        $.each(this.panels.models, function (index, panel) {
+            panel.addMsg('', 'Disconnected from the IRC network', 'action quit');
+        });
+    }
+
+
+
+    function onConnect(event) {
+        var panels, channel_names;
+
+        // Update our nick with what the network gave us
+        this.set('nick', event.nick);
+
+        // Auto joining channels
+        if (this.auto_join && this.auto_join.channel) {
+            panels = this.createAndJoinChannels(this.auto_join.channel + ' ' + (this.auto_join.channel_key || ''));
+
+            // Show the last channel if we have one
+            if (panels)
+                panels[panels.length - 1].view.show();
+        }
+    }
+
+
 
     function onOptions(event) {
         var that = this;
 
 
 
+    function onNick(event) {
+        var member;
+
+        $.each(this.panels.models, function (index, panel) {
+            if (panel.get('name') == event.nick)
+                panel.set('name', event.newnick);
+
+            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');
+            }
+        });
+    }
+
+
+
     function onCtcpRequest(event) {
         // An ignored user? don't do anything with it
         if (_kiwi.gateway.isNickIgnored(event.nick)) {
 
         // Reply to a TIME ctcp
         if (event.msg.toUpperCase() === 'TIME') {
-            this.gateway.ctcp(false, event.type, event.nick, (new Date()).toString());
+            this.gateway.ctcp(null, false, event.type, event.nick, (new Date()).toString());
         }
     }
 
         }
 
         panel.addMsg('[' + (event.nick||'') + ']', event.msg);
+
+        // Show this notice to the active panel if it didn't have a set target
+        if (panel === this.panels.server)
+            _kiwi.app.panels().active.addMsg('[' + (event.nick||'') + ']', event.msg);
     }
 
 
             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;
+        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) {
             logon_date = formatDate(logon_date);
 
             panel.addMsg(event.nick, 'idle for ' + idle_time + ', signed on ' + logon_date, 'whois');
+        } else if (event.away_reason) {
+            panel.addMsg(event.nick, 'Away: ' + event.away_reason, 'whois');
         } else {
             panel.addMsg(event.nick, 'idle for ' + idle_time, 'whois');
         }
 
 
     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;
+        var chanlist = _kiwi.model.Applet.loadOnce('kiwi_chanlist');
+        chanlist.view.show();
     }