Skip to content

Commit

Permalink
Poll for users’ away status in each active channel
Browse files Browse the repository at this point in the history
  • Loading branch information
zarino committed Jul 11, 2015
1 parent e9ef053 commit 4a1ce66
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/client/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,19 @@ window.UserButtonView = window.BackchatView.extend({

ipcEvents: {
'nick:changed': function(details){
this.options.nick = details.newNick;
this.$el.text(details.newNick);
if(details.oldNick == this.options.nick){
this.options.nick = details.newNick;
this.$el.text(details.newNick);
}
},
'server:userStatus': function(details){
if(details.nick == this.options.nick){
if(details.away == true) {
this.$el.addClass('away');
} else if(details.away == false){
this.$el.removeClass('away');
}
}
}
}
});
Expand Down
55 changes: 55 additions & 0 deletions src/server/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ module.exports = ConnectionPool = (function(){
server: connectionSettings.url,
message: message
});

// Join channels from config file if requested
if(connectionSettings.autoConnect && connectionSettings.channels){
_.each(connectionSettings.channels, function(channelName){
client.join(channelName);
Expand All @@ -48,6 +50,10 @@ module.exports = ConnectionPool = (function(){
});
});
}

// Start the function that checks for user away statuses
refreshUserStatuses();

}).addListener('names', function(channel, nicks){
// console.log('[irc event]', '[names]', channel, nicks);
self.emit('irc:names', {
Expand All @@ -57,6 +63,7 @@ module.exports = ConnectionPool = (function(){
return a.localeCompare(b, 'en', { sensitivity: 'base' });
})
});

}).addListener('topic', function(channel, topic, nick, message){
// console.log('[irc event]', '[topic]', channel, topic, nick);
self.emit('irc:topic', {
Expand All @@ -65,6 +72,7 @@ module.exports = ConnectionPool = (function(){
topic: topic,
user: nick
});

}).addListener('join', function(channel, nick, message){
// console.log('[irc event]', '[join]', channel, nick);
self.emit('irc:join', {
Expand All @@ -73,6 +81,8 @@ module.exports = ConnectionPool = (function(){
user: nick,
myNick: client.nick
});
refreshUserStatuses({ oneOff: true });

}).addListener('part', function(channel, nick, reason, message){
// console.log('[irc event]', '[part]', channel, nick, reason);
self.emit('irc:part', {
Expand All @@ -82,6 +92,7 @@ module.exports = ConnectionPool = (function(){
reason: reason,
myNick: client.nick
});

}).addListener('quit', function(nick, reason, channels, message){
// console.log('[irc event]', '[quit]', nick, reason, channels);
self.emit('irc:quit', {
Expand All @@ -91,6 +102,7 @@ module.exports = ConnectionPool = (function(){
channels: channels,
myNick: client.nick
});

}).addListener('kick', function(channel, nick, by, reason, message){
console.log('[irc event]', '[kick]', channel, nick, by, reason);
self.emit('irc:kick', {
Expand All @@ -101,6 +113,7 @@ module.exports = ConnectionPool = (function(){
reason: reason,
myNick: client.nick
});

}).addListener('kill', function(nick, reason, channels, message){
console.log('[irc event]', '[kill]', nick, reason, channels);
self.emit('irc:kill', {
Expand All @@ -110,6 +123,7 @@ module.exports = ConnectionPool = (function(){
channels: channels,
myNick: client.nick
});

}).addListener('message', function(nick, to, text, message){
// console.log('[irc event]', '[message]', nick, to, text);
self.emit('irc:message', {
Expand All @@ -119,6 +133,7 @@ module.exports = ConnectionPool = (function(){
messageText: text,
myNick: client.nick
});

}).addListener('selfMessage', function(to, text){
// console.log('[irc event]', '[selfMessage]', to, text);
self.emit('irc:message', {
Expand All @@ -128,6 +143,7 @@ module.exports = ConnectionPool = (function(){
messageText: text,
myNick: client.nick
});

}).addListener('notice', function(nick, to, text, message){
console.log('[irc event]', '[notice]', nick, to, text);
self.emit('irc:notice', {
Expand All @@ -137,6 +153,7 @@ module.exports = ConnectionPool = (function(){
messageText: text,
myNick: client.nick
});

}).addListener('nick', function(oldnick, newnick, channels, message){
// console.log('[irc event]', '[nick]', oldnick, newnick, channels);
self.emit('irc:changedNick', {
Expand All @@ -146,6 +163,7 @@ module.exports = ConnectionPool = (function(){
channels: channels,
myNick: client.nick
});

}).addListener('action', function(from, to, text, message){
// console.log('[irc event]', '[action]', from, to, text);
self.emit('irc:action', {
Expand All @@ -155,21 +173,58 @@ module.exports = ConnectionPool = (function(){
actionText: text,
myNick: client.nick
});

}).addListener('whois', function(info){
// console.log('[irc event]', '[whois]', info);
self.emit('irc:whois', {
server: connectionSettings.url,
info: info
});

}).addListener('channellist_start', function(){
console.log('[irc event]', '[channellist_start]');

}).addListener('channellist_item', function(){
console.log('[irc event]', '[channellist_item]');

}).addListener('channellist', function(){
console.log('[irc event]', '[channellist]');

}).addListener('raw', function(message){
console.log('[irc event]', '[raw]', message);

if(message.command == 'rpl_whoreply'){
self.emit('irc:userStatus', {
nick: message.args[5],
away: message.args[6].startsWith('G')
});
} else if(message.command == 'rpl_nowaway'){
self.emit('irc:userStatus', {
nick: message.args[0],
away: true
});
} else if(message.command == 'rpl_unaway'){
self.emit('irc:userStatus', {
nick: message.args[0],
away: false
});
}
});

var refreshUserStatuses = function refreshUserStatuses(options){
options = _.extend({}, options);

// client.chans should be an object of channels the
// user is currently occupying, keyed by channelName.
_.each(client.chans, function(channelInfo, channelName){
client.send('WHO', channelName);
});

if(!options.oneOff){
setTimeout(refreshUserStatuses, 10000);
}
}

self.connections[connectionSettings.url] = client;
};

Expand Down
4 changes: 4 additions & 0 deletions src/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ pool.on('irc:registering', function(e){
console.log('server:whois', JSON.stringify(e, null, 2));
mainWindow.webContents.send('server:whois', e);

}).on('irc:userStatus', function(e){
console.log('server:userStatus', JSON.stringify(e, null, 2));
mainWindow.webContents.send('server:userStatus', e);

});

var getSettings = function getSettings(cb){
Expand Down

0 comments on commit 4a1ce66

Please sign in to comment.