Merge M2Ys4U-joinfix
[KiwiIRC.git] / server / irc / commands.js
index c6fce94fa3d8c578ca2f03a9499e5110efc88374..b17ec4f3a4bff2c49643075590637afe6d8249bf 100644 (file)
@@ -14,6 +14,7 @@ var irc_numerics = {
     RPL_LUSERME:            '255',
     RPL_LOCALUSERS:         '265',
     RPL_GLOBALUSERS:        '266',
+    RPL_AWAY:               '301',
     RPL_WHOISREGNICK:       '307',
     RPL_WHOISUSER:          '311',
     RPL_WHOISSERVER:        '312',
@@ -96,7 +97,7 @@ var listeners = {
         var nick =  command.params[0];
         this.irc_connection.registered = true;
         this.cap_negotation = false;
-        this.irc_connection.emit('server:connect', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' connect', {
             nick: nick
         });
     },
@@ -125,19 +126,25 @@ var listeners = {
                 }
             }
         }
-        this.irc_connection.emit('server:options', {
+        this.irc_connection.emit('server '  + this.irc_connection.irc_host.hostname + ' options', {
             options: this.irc_connection.options,
             cap: this.irc_connection.cap.enabled
         });
     },
     'RPL_ENDOFWHOIS': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':endofwhois', {
+        this.irc_connection.emit('user ' + command.params[1] + ' endofwhois', {
             nick: command.params[1],
             msg: command.trailing
         });
     },
+    'RPL_AWAY': function (command) {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoisaway', {
+            nick: command.params[1],
+            reason: command.trailing
+        });
+    },
     'RPL_WHOISUSER': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoisuser', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoisuser', {
             nick: command.params[1],
             ident: command.params[2],
             host: command.params[3],
@@ -145,65 +152,65 @@ var listeners = {
         });
     },
     'RPL_WHOISSERVER': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoisserver', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoisserver', {
             nick: command.params[1],
             irc_server: command.params[2]
         });
     },
     'RPL_WHOISOPERATOR': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoisoperator', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoisoperator', {
             nick: command.params[1],
             msg: command.trailing
         });
     },
     'RPL_WHOISCHANNELS':       function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoischannels', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoischannels', {
             nick: command.params[1],
             chans: command.trailing
         });
     },
     'RPL_WHOISMODES': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoismodes', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoismodes', {
             nick: command.params[1],
             msg: command.trailing
         });
     },
     'RPL_WHOISIDLE': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoisidle', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoisidle', {
             nick: command.params[1],
             idle: command.params[2],
             logon: command.params[3] || undefined
         });
     },
     'RPL_WHOISREGNICK': function (command) {
-        this.irc_connection.emit('user:' + command.params[1] + ':whoisregnick', {
+        this.irc_connection.emit('user ' + command.params[1] + ' whoisregnick', {
             nick: command.params[1],
             msg: command.trailing
         });
     },
     'RPL_LISTSTART': function (command) {
-        this.irc_connection.emit('server:list_start', {});
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' list_start', {});
     },
     'RPL_LISTEND': function (command) {
-        this.irc_connection.emit('server:list_end', {});
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' list_end', {});
     },
     'RPL_LIST': function (command) {
-        this.irc_connection.emit('server:list_channel', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' list_channel', {
             channel: command.params[1],
             num_users: parseInt(command.params[2], 10),
             topic: command.trailing
         });
     },
     'RPL_MOTD': function (command) {
-        this.irc_connection.emit('server:motd', {
+        this.irc_connection.emit('server '  + this.irc_connection.irc_host.hostname + ' motd', {
             motd: command.trailing + '\n'
         });
     },
     'RPL_MOTDSTART': function (command) {
-        this.irc_connection.emit('server:motd_start', {});
+        this.irc_connection.emit('server '  + this.irc_connection.irc_host.hostname + ' motd_start', {});
     },
     'RPL_ENDOFMOTD': function (command) {
-        this.irc_connection.emit('server:motd_end', {});
+        this.irc_connection.emit('server '  + this.irc_connection.irc_host.hostname + ' motd_end', {});
     },
     'RPL_NAMEREPLY': function (command) {
         var members = command.trailing.split(' ');
@@ -228,7 +235,7 @@ var listeners = {
             member_list.push({nick: member, modes: modes});
         });
 
-        this.irc_connection.emit('channel:' + command.params[2] + ':userlist', {
+        this.irc_connection.emit('channel ' + command.params[2] + ' userlist', {
             users: member_list,
             channel: command.params[2]
         });
@@ -236,14 +243,14 @@ var listeners = {
 
     
     'RPL_ENDOFNAMES': function (command) {
-        this.irc_connection.emit('channel:' + command.params[1] + ':userlist_end', {
+        this.irc_connection.emit('channel ' + command.params[1] + ' userlist_end', {
             channel: command.params[1]
         });
     },
 
 
     'RPL_BANLIST': function (command) {
-        this.irc_connection.emit('channel:' + command.params[1] + ':banlist', {
+        this.irc_connection.emit('channel ' + command.params[1] + ' banlist', {
             channel: command.params[1],
             banned: command.params[2],
             banned_by: command.params[3],
@@ -251,24 +258,24 @@ var listeners = {
         });
     },
     'RPL_ENDOFBANLIST': function (command) {
-        this.irc_connection.emit('channel:' + command.params[1] + ':banlist_end', {
-            channel: commands.params[1]
+        this.irc_connection.emit('channel ' + command.params[1] + ' banlist_end', {
+            channel: command.params[1]
         });
     },
     'RPL_TOPIC': function (command) {
-        this.irc_connection.emit('channel:' + command.params[1] + ':topic', {
+        this.irc_connection.emit('channel ' + command.params[1] + ' topic', {
             channel: command.params[1],
             topic: command.trailing
         });
     },
     'RPL_NOTOPIC': function (command) {
-        this.irc_connection.emit('channel:' + command.params[1] + ':topic', {
+        this.irc_connection.emit('channel ' + command.params[1] + ' topic', {
             channel: command.params[1],
             topic: ''
         });
     },
     'RPL_TOPICWHOTIME': function (command) {
-        this.irc_connection.emit('channel:' + command.params[1] + ':topicsetby', {
+        this.irc_connection.emit('channel ' + command.params[1] + ' topicsetby', {
             nick: command.params[2],
             channel: command.params[1],
             when: command.params[3]
@@ -287,7 +294,7 @@ var listeners = {
             channel = command.params[0];
         }
         
-        this.irc_connection.emit('channel:' + channel + ':join', {
+        this.irc_connection.emit('channel ' + channel + ' join', {
             nick: command.nick,
             ident: command.ident,
             hostname: command.hostname,
@@ -297,7 +304,7 @@ var listeners = {
 
 
     'PART': function (command) {
-        this.irc_connection.emit('channel:' + command.params[0] + ':part', {
+        this.irc_connection.emit('channel ' + command.params[0] + ' part', {
             nick: command.nick,
             ident: command.ident,
             hostname: command.hostname,
@@ -308,7 +315,7 @@ var listeners = {
 
 
     'KICK': function (command) {
-        this.irc_connection.emit('channel:' + command.params[0] + ':kick', {
+        this.irc_connection.emit('channel ' + command.params[0] + ' kick', {
             kicked: command.params[1],
             nick: command.nick,
             ident: command.ident,
@@ -320,7 +327,7 @@ var listeners = {
 
 
     'QUIT': function (command) {
-        this.irc_connection.emit('user:' + command.nick + ':quit', {
+        this.irc_connection.emit('user ' + command.nick + ' quit', {
             nick: command.nick,
             ident: command.ident,
             hostname: command.hostname,
@@ -335,7 +342,7 @@ var listeners = {
         if ((command.trailing.charAt(0) === String.fromCharCode(1)) && (command.trailing.charAt(command.trailing.length - 1) === String.fromCharCode(1))) {
             // It's a CTCP response
             namespace = (command.params[0] == this.irc_connection.nick) ? 'user' : 'channel';
-            this.irc_connection.emit(namespace + ':' + command.params[0] + ':ctcp_response', {
+            this.irc_connection.emit(namespace + ' ' + command.params[0] + ' ctcp_response', {
                 nick: command.nick,
                 ident: command.ident,
                 hostname: command.hostname,
@@ -343,9 +350,13 @@ var listeners = {
                 msg: command.trailing.substr(1, command.trailing.length - 2)
             });
         } else {
-            namespace = (command.params[0] == this.irc_connection.nick) ? 'user' : 'channel';
-            this.irc_connection.emit(namespace + ':' + command.params[0] + ':notice', {
-                nick: command.nick,
+            namespace = (command.params[0] == this.irc_connection.nick || command.params[0] == '*') ?
+                'user' :
+                'channel';
+
+            this.irc_connection.emit(namespace + ' ' + command.params[0] + ' notice', {
+                from_server: command.prefix ? true : false,
+                nick: command.nick || command.prefix || undefined,
                 ident: command.ident,
                 hostname: command.hostname,
                 target: command.params[0],
@@ -354,7 +365,7 @@ var listeners = {
         }
     },
     'NICK': function (command) {
-        this.irc_connection.emit('user:' + command.nick + ':nick', {
+        this.irc_connection.emit('user ' + command.nick + ' nick', {
             nick: command.nick,
             ident: command.ident,
             hostname: command.hostname,
@@ -368,13 +379,13 @@ var listeners = {
         var channel = command.params[0],
             topic = command.trailing || '';
 
-        this.irc_connection.emit('channel:' + channel + ':topic', {
+        this.irc_connection.emit('channel ' + channel + ' topic', {
             nick: command.nick,
             channel: channel,
             topic: topic
         });
     },
-    'MODE': function (command) {                
+    'MODE': function (command) {
         var chanmodes = this.irc_connection.options.CHANMODES || [],
             prefixes = this.irc_connection.options.PREFIX || [],
             always_param = (chanmodes[0] || '').concat((chanmodes[1] || '')),
@@ -423,7 +434,7 @@ var listeners = {
             }
         }
         
-        event = (_.contains(this.irc_connection.options.CHANTYPES, command.params[0][0]) ? 'channel:' : 'user:') + command.params[0] + ':mode';
+        event = (_.contains(this.irc_connection.options.CHANTYPES, command.params[0][0]) ? 'channel ' : 'user ') + command.params[0] + ' mode';
         
         this.irc_connection.emit(event, {
             target: command.params[0],
@@ -449,7 +460,7 @@ var listeners = {
                 this.irc_connection.write('NOTICE ' + command.nick + ' :' + String.fromCharCode(1) + 'CLIENTINFO SOURCE VERSION TIME' + String.fromCharCode(1));
             } else {
                 namespace = (command.params[0].toLowerCase() == this.irc_connection.nick.toLowerCase()) ? 'user' : 'channel';
-                this.irc_connection.emit(namespace + ':' + command.nick + ':ctcp_request', {
+                this.irc_connection.emit(namespace + ' ' + command.nick + ' ctcp_request', {
                     nick: command.nick,
                     ident: command.ident,
                     hostname: command.hostname,
@@ -460,8 +471,8 @@ var listeners = {
             }
         } else {
             // A message to a user (private message) or to a channel?
-            namespace = (command.params[0] === this.irc_connection.nick) ? 'user:' + command.nick : 'channel:' + command.params[0];
-            this.irc_connection.emit(namespace + ':privmsg', {
+            namespace = (command.params[0] === this.irc_connection.nick) ? 'user ' + command.nick : 'channel ' + command.params[0];
+            this.irc_connection.emit(namespace + ' privmsg', {
                 nick: command.nick,
                 ident: command.ident,
                 hostname: command.hostname,
@@ -475,14 +486,17 @@ var listeners = {
         // i.e. - for disable, ~ for requires ACK, = for sticky
         var capabilities = command.trailing.replace(/[\-~=]/, '').split(' ');
         var request;
+
+        // Which capabilities we want to enable
         var want = ['multi-prefix', 'away-notify'];
-        
+
         if (this.irc_connection.password) {
             want.push('sasl');
         }
-        
+
         switch (command.params[1]) {
             case 'LS':
+                // Compute which of the available capabilities we want and request them
                 request = _.intersection(capabilities, want);
                 if (request.length > 0) {
                     this.irc_connection.cap.requested = request;
@@ -494,10 +508,12 @@ var listeners = {
                 break;
             case 'ACK':
                 if (capabilities.length > 0) {
+                    // Update list of enabled capabilities
                     this.irc_connection.cap.enabled = capabilities;
+                    // Update list of capabilities we would like to have but that aren't enabled
                     this.irc_connection.cap.requested = _.difference(this.irc_connection.cap.requested, capabilities);
                 }
-                if (this.irc_connection.cap.requested.length > 0) {
+                if (this.irc_connection.cap.enabled.length > 0) {
                     if (_.contains(this.irc_connection.cap.enabled, 'sasl')) {
                         this.irc_connection.sasl = true;
                         this.irc_connection.write('AUTHENTICATE PLAIN');
@@ -540,7 +556,7 @@ var listeners = {
         }
     },
     'AWAY': function (command) {
-        this.irc_connection.emit('user:' + command.nick + ':away', {
+        this.irc_connection.emit('user ' + command.nick + ' away', {
             nick: command.nick,
             msg: command.trailing
         });
@@ -567,82 +583,82 @@ var listeners = {
         // noop
     },
     'ERROR': function (command) {
-        this.irc_connection.emit('server:error', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' error', {
             reason: command.trailing
         });
     },
     ERR_PASSWDMISMATCH: function (command) {
-        this.irc_connection.emit('server:password_mismatch', {});
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' password_mismatch', {});
     },
     ERR_LINKCHANNEL: function (command) {
-        this.irc_connection.emit('server:channel_redirect', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' channel_redirect', {
             from: command.params[1],
             to: command.params[2]
         });
     },
     ERR_NOSUCHNICK: function (command) {
-        this.irc_connection.emit('server:no_such_nick', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' no_such_nick', {
             nick: command.params[1],
             reason: command.trailing
         });
     },
     ERR_CANNOTSENDTOCHAN: function (command) {
-        this.irc_connection.emit('server:cannot_send_to_chan', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' cannot_send_to_chan', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_TOOMANYCHANNELS: function (command) {
-        this.irc_connection.emit('server:too_many_channels', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' too_many_channels', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_USERNOTINCHANNEL: function (command) {
-        this.irc_connection.emit('server:user_not_in_channel', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' user_not_in_channel', {
             nick: command.params[0],
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_NOTONCHANNEL: function (command) {
-        this.irc_connection.emit('server:not_on_channel', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' not_on_channel', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_CHANNELISFULL: function (command) {
-            this.irc_connection.emit('server:channel_is_full', {
+            this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' channel_is_full', {
                 channel: command.params[1],
                 reason: command.trailing
             });
         },
     ERR_INVITEONLYCHAN: function (command) {
-        this.irc_connection.emit('server:invite_only_channel', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' invite_only_channel', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_BANNEDFROMCHAN: function (command) {
-        this.irc_connection.emit('server:banned_from_channel', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' banned_from_channel', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_BADCHANNELKEY: function (command) {
-        this.irc_connection.emit('server:bad_channel_key', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' bad_channel_key', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_CHANOPRIVSNEEDED: function (command) {
-        this.irc_connection.emit('server:chanop_privs_needed', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' chanop_privs_needed', {
             channel: command.params[1],
             reason: command.trailing
         });
     },
     ERR_NICKNAMEINUSE: function (command) {
-        this.irc_connection.emit('server:nickname_in_use', {
+        this.irc_connection.emit('server ' + this.irc_connection.irc_host.hostname + ' nickname_in_use', {
             nick: command.params[1],
             reason: command.trailing
         });
@@ -749,6 +765,7 @@ function genericNotice (command, msg, is_error) {
 
     this.client.sendIrcCommand('notice', {
         server: this.con_num,
+        from_server: true,
         nick: command.prefix,
         ident: '',
         hostname: '',