forked from GitHub/dbot
users module almost fully operational with databank (few kinks in moderation commands still) [#272]
This commit is contained in:
parent
ef9f5f2646
commit
3814df98f6
@ -14,7 +14,8 @@ var api = function(dbot) {
|
|||||||
// Return a user record given a primary nick or an alias
|
// Return a user record given a primary nick or an alias
|
||||||
'resolveUser': function(server, nick, callback) {
|
'resolveUser': function(server, nick, callback) {
|
||||||
var user = false;
|
var user = false;
|
||||||
this.db.search('user', { 'server': server }, function(result) {
|
this.db.search('users', { 'server': server }, function(result) {
|
||||||
|
console.log(result.primaryNick);
|
||||||
if(result.primaryNick == nick || _.include(result.aliases, nick)) {
|
if(result.primaryNick == nick || _.include(result.aliases, nick)) {
|
||||||
user = result;
|
user = result;
|
||||||
}
|
}
|
||||||
@ -25,11 +26,11 @@ var api = function(dbot) {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
'getChannel': function(server, channel, callback) {
|
'getChannel': function(server, channelName, callback) {
|
||||||
var channel = false;
|
var channel = false;
|
||||||
this.db.search('channel_users', {
|
this.db.search('channel_users', {
|
||||||
'server': server,
|
'server': server,
|
||||||
'name': channel
|
'name': channelName
|
||||||
}, function(result) {
|
}, function(result) {
|
||||||
channel = result;
|
channel = result;
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
|
@ -3,97 +3,94 @@ var _ = require('underscore')._;
|
|||||||
var commands = function(dbot) {
|
var commands = function(dbot) {
|
||||||
var commands = {
|
var commands = {
|
||||||
'~alias': function(event) {
|
'~alias': function(event) {
|
||||||
var knownUsers = this.getServerUsers(event.server),
|
var nick = event.params[1].trim();
|
||||||
alias = event.params[1].trim();
|
this.api.resolveUser(event.server, nick, function(user) {
|
||||||
|
if(user) {
|
||||||
if(_.include(knownUsers.users, alias)) {
|
if(nick == user.primaryNick) {
|
||||||
var aliases = this.api.getAliases(event.server, alias);
|
var aliases = _.first(user.aliases, 10);
|
||||||
var aliasCount = aliases.length;
|
|
||||||
|
|
||||||
if(aliasCount != 0) {
|
|
||||||
var aliases = _.first(aliases, 10);
|
|
||||||
var including = 'including: ' + aliases.join(', ') + '.';
|
var including = 'including: ' + aliases.join(', ') + '.';
|
||||||
|
|
||||||
|
if(user.aliases.length != 0) {
|
||||||
event.reply(dbot.t('primary', {
|
event.reply(dbot.t('primary', {
|
||||||
'user': alias,
|
'user': nick,
|
||||||
'count': aliasCount
|
'count': user.aliases.length,
|
||||||
}) + including);
|
}) + including);
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('primary', {
|
event.reply(dbot.t('primary', {
|
||||||
'user': alias,
|
'user': nick,
|
||||||
'count': aliasCount
|
'count': user.aliases.length
|
||||||
}).slice(0, -2) + ".");
|
}).slice(0, -2) + ".");
|
||||||
}
|
}
|
||||||
} else if(_.has(knownUsers.aliases, alias)) {
|
|
||||||
event.reply(dbot.t('alias', {
|
|
||||||
'alias': alias,
|
|
||||||
'user': knownUsers.aliases[alias]
|
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('unknown_alias', { 'alias': alias }));
|
event.reply(dbot.t('alias', {
|
||||||
|
'alias': nick,
|
||||||
|
'user': user.primaryNick
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
'~setaliasparent': function(event) {
|
'~setaliasparent': function(event) {
|
||||||
var knownUsers = this.getServerUsers(event.server);
|
var newPrimary = event.params[1].trim();
|
||||||
var newParent = event.params[1];
|
this.api.resolveUser(event.server, newPrimary, function(user) {
|
||||||
|
if(user) {
|
||||||
|
var newAlias = user.primaryNick;
|
||||||
|
user.primaryNick = newPrimary;
|
||||||
|
user.aliases = _.without(user.aliases, newPrimary).push(newAlias);
|
||||||
|
this.internalAPI.updateChannelPrimaryUser(event.server, newAlias, newPrimary);
|
||||||
|
|
||||||
if(_.has(knownUsers.aliases, newParent)) {
|
this.db.save('users', user.id, user, function(err) {
|
||||||
var newAlias = knownUsers.aliases[newParent];
|
if(!err) {
|
||||||
|
dbot.api.event.emit('~setaliasparent', {
|
||||||
// Replace user entry
|
|
||||||
knownUsers.users = _.without(knownUsers.users, newAlias);
|
|
||||||
knownUsers.users.push(newParent);
|
|
||||||
|
|
||||||
// Replace channels entries with new primary user
|
|
||||||
this.updateChannels(event, newAlias, newParent);
|
|
||||||
|
|
||||||
// Remove alias for new parent & add alias for new alias
|
|
||||||
delete knownUsers.aliases[newParent];
|
|
||||||
knownUsers.aliases[newAlias] = newParent;
|
|
||||||
|
|
||||||
// Update aliases to point to new primary user
|
|
||||||
this.updateAliases(event, newAlias, newParent);
|
|
||||||
|
|
||||||
event.reply(dbot.t('aliasparentset', {
|
|
||||||
'newParent': newParent,
|
|
||||||
'newAlias': newAlias
|
|
||||||
}));
|
|
||||||
|
|
||||||
return {
|
|
||||||
'server': event.server,
|
'server': event.server,
|
||||||
'alias': newAlias
|
'alias': newAlias
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('unknown_alias', { 'alias': newParent }));
|
event.reply(dbot.t('unknown_alias', { 'alias': newParent }));
|
||||||
}
|
}
|
||||||
return false;
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
'~mergeusers': function(event) {
|
'~mergeusers': function(event) {
|
||||||
var knownUsers = this.getServerUsers(event.server);
|
|
||||||
var primaryUser = event.params[1];
|
var primaryUser = event.params[1];
|
||||||
var secondaryUser = event.params[2];
|
var secondaryUser = event.params[2];
|
||||||
|
|
||||||
if(_.include(knownUsers.users, primaryUser) && _.include(knownUsers.users, secondaryUser)) {
|
this.api.resolveUser(event.server, primaryUser, function(user) {
|
||||||
knownUsers.users = _.without(knownUsers.users, secondaryUser);
|
if(user) {
|
||||||
knownUsers.aliases[secondaryUser] = primaryUser;
|
this.api.resolveUser(event.server, secondaryUser, function(secondaryUser) {
|
||||||
this.updateAliases(event, secondaryUser, primaryUser);
|
if(secondaryUser) {
|
||||||
|
user.aliases.push(secondaryUser.primaryNick);
|
||||||
|
user.aliases.concat(secondaryUser.aliases);
|
||||||
|
this.db.del('users', secondaryUser.id, function(err) {
|
||||||
|
if(!err) {
|
||||||
|
this.db.save('users', user.id, user, function(err) {
|
||||||
|
if(!err) {
|
||||||
this.updateChannels(event, secondaryUser, primaryUser);
|
this.updateChannels(event, secondaryUser, primaryUser);
|
||||||
|
|
||||||
event.reply(dbot.t('merged_users', {
|
event.reply(dbot.t('merged_users', {
|
||||||
'old_user': secondaryUser,
|
'old_user': secondaryUser,
|
||||||
'new_user': primaryUser
|
'new_user': primaryUser
|
||||||
}));
|
}));
|
||||||
|
dbot.api.event.emit('~mergeusers', {
|
||||||
return {
|
|
||||||
'server': event.server,
|
'server': event.server,
|
||||||
'secondary': secondaryUser
|
'secondary': secondaryUser
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('unprimary_error'));
|
event.reply(dbot.t('unprimary_error'));
|
||||||
}
|
}
|
||||||
return false;
|
});
|
||||||
|
} else {
|
||||||
|
event.reply(dbot.t('unprimary_error'));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,28 +2,12 @@
|
|||||||
* Name: Users
|
* Name: Users
|
||||||
* Description: Track known users
|
* Description: Track known users
|
||||||
*/
|
*/
|
||||||
var _ = require('underscore')._;
|
var _ = require('underscore')._,
|
||||||
|
uuid = require('node-uuid');
|
||||||
|
|
||||||
var users = function(dbot) {
|
var users = function(dbot) {
|
||||||
this.knownUsers = dbot.db.knownUsers;
|
|
||||||
|
|
||||||
this.updateAliases = function(event, oldUser, newUser) {
|
|
||||||
var knownUsers = this.getServerUsers(event.server);
|
|
||||||
_.each(knownUsers.aliases, function(user, alias) {
|
|
||||||
if(user == oldUser) {
|
|
||||||
knownUsers.aliases[alias] = newUser;
|
|
||||||
}
|
|
||||||
}, this);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.updateChannels = function(event, oldUser, newUser) {
|
|
||||||
var channelUsers = this.getServerUsers(event.server).channelUsers;
|
|
||||||
channelUsers = _.each(channelUsers, function(channel, channelName) {
|
|
||||||
channelUsers[channelName] = _.without(channel, oldUser);
|
|
||||||
channelUsers[channelName].push(newUser);
|
|
||||||
}, this);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
/*** Internal API ***/
|
||||||
this.internalAPI = {
|
this.internalAPI = {
|
||||||
'createUser': function(server, nick, channel, callback) {
|
'createUser': function(server, nick, channel, callback) {
|
||||||
var id = uuid.v4();
|
var id = uuid.v4();
|
||||||
@ -36,14 +20,17 @@ var users = function(dbot) {
|
|||||||
'aliases': []
|
'aliases': []
|
||||||
}, function(err, result) {
|
}, function(err, result) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
dbot.api.event.emit('new_user', [ user ]);
|
dbot.api.event.emit('new_user', [ result ]);
|
||||||
callback(result);
|
callback(result);
|
||||||
|
dbot.say('aberwiki', '#realitest', result.server);
|
||||||
|
dbot.say('aberwiki', '#realitest', result.primaryNick);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
}.bind(this),
|
||||||
|
|
||||||
'addChannelUser': function(user, channelName) {
|
'addChannelUser': function(user, channelName) {
|
||||||
user.channels.push(channelName);
|
user.channels.push(channelName);
|
||||||
|
dbot.say('aberwiki', '#realitest', user.id);
|
||||||
this.db.save('users', user.id, user, function(err) {
|
this.db.save('users', user.id, user, function(err) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
this.api.getChannel(user.server, channelName, function(channel) {
|
this.api.getChannel(user.server, channelName, function(channel) {
|
||||||
@ -56,46 +43,68 @@ var users = function(dbot) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}.bind(this),
|
||||||
|
|
||||||
|
'updateChannelPrimaryUser': function(server, oldUser, newUser) {
|
||||||
|
this.db.search('channel_users', { 'server': server }, function(channel) {
|
||||||
|
channel.users = _.without(channel.users, oldUser);
|
||||||
|
if(!_.include(channel.users, newUser)) channel.users.push(newUser);
|
||||||
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
||||||
|
if(err) {
|
||||||
|
// QQ
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}.bind(this), function(err) {
|
||||||
|
if(err) {
|
||||||
|
// QQ
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}.bind(this)
|
||||||
};
|
};
|
||||||
|
|
||||||
this.listener = function(event) {
|
this.listener = function(event) {
|
||||||
if(event.action == 'JOIN' && nick != dbot.config.name) {
|
if(event.action == 'JOIN' && event.user != dbot.config.name) {
|
||||||
this.api.resolveUser(event.server, event.user, function(user) {
|
this.api.resolveUser(event.server, event.user, function(user) {
|
||||||
if(!user) { // User does not yet exist
|
if(!user) { // User does not yet exist
|
||||||
this.internalAPI.createUser(event.server, event.user, event.channel, function(result) {
|
this.internalAPI.createUser(event.server, event.user, event.channel, function(result) {
|
||||||
user = result;
|
user = result;
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
||||||
this.internalAPI.addChannelUser(user, event.channel);
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
||||||
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}.bind(this));
|
||||||
} else if(event.action == 'NICK') {
|
} else if(event.action == 'NICK') {
|
||||||
if(!this.api.isKnownUser(event.newNick)) {
|
this.api.isKnownUser(event.server, event.newNick, function(isKnown) {
|
||||||
|
if(!isKnown) {
|
||||||
this.api.resolveUser(event.server, event.user, function(user) {
|
this.api.resolveUser(event.server, event.user, function(user) {
|
||||||
|
dbot.say('aberwiki', '#realitest', event.newNick);
|
||||||
user.aliases.push(event.newNick);
|
user.aliases.push(event.newNick);
|
||||||
this.db.save('users', user.id, function(err) {
|
this.db.save('users', user.id, user, function(err) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
dbot.api.event.emit('new_user_alias', [ user, event.newNick ]);
|
dbot.api.event.emit('new_user_alias', [ user, event.newNick ]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
}.bind(this));
|
||||||
}
|
}
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
this.on = ['JOIN', 'NICK'];
|
this.on = ['JOIN', 'NICK'];
|
||||||
|
|
||||||
this.onLoad = function() {
|
this.onLoad = function() {
|
||||||
dbot.instance.addListener('366', 'users', function(event) {
|
dbot.instance.addListener('366', 'users', function(event) {
|
||||||
this.api.getChannel(event.server, event.channel, function(channel) {
|
this.api.getChannel(event.server, event.channel.name, function(channel) {
|
||||||
if(!channel) { // Channel does not yet exist
|
if(!channel) { // Channel does not yet exist
|
||||||
var id = uuid.v4();
|
var id = uuid.v4();
|
||||||
this.db.create('channel_users', id, {
|
this.db.create('channel_users', id, {
|
||||||
'uuid': id,
|
'uuid': id,
|
||||||
'server': event.server,
|
'server': event.server,
|
||||||
'name': event.channel,
|
'name': event.channel.name,
|
||||||
'users': []
|
'users': []
|
||||||
}, function(err, result) {
|
}, function(err, result) {
|
||||||
if(!err) {
|
if(!err) {
|
||||||
@ -106,25 +115,29 @@ var users = function(dbot) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_.each(event.channel.nicks, function(nick) {
|
_.each(event.channel.nicks, function(nick) {
|
||||||
|
var nick = nick.name;
|
||||||
this.api.resolveUser(event.server, nick, function(user) {
|
this.api.resolveUser(event.server, nick, function(user) {
|
||||||
if(!user) {
|
if(!user) {
|
||||||
this.internalAPI.createUser(event.server, nick, event.channel, function(result) {
|
this.internalAPI.createUser(event.server, nick, event.channel, function(result) {
|
||||||
user = result;
|
user = result;
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!_.include(user.channels, event.channel)) {
|
if(!_.include(user.channels, event.channel)) {
|
||||||
this.internalAPI.addChannelUser(user, event.channel);
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
if(!_.include(user.channels, event.channel)) {
|
||||||
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
}, this);
|
}, this);
|
||||||
});
|
}.bind(this));
|
||||||
|
}.bind(this));
|
||||||
|
|
||||||
var connections = dbot.instance.connections;
|
var connections = dbot.instance.connections;
|
||||||
_.each(connections, function(connection) {
|
_.each(connections, function(connection) {
|
||||||
connection.updateNickLists();
|
connection.updateNickLists();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user