This commit is contained in:
reality 2014-09-04 09:30:22 +00:00
parent b950426d7b
commit be07c36294
2 changed files with 99 additions and 6 deletions

View File

@ -3,7 +3,7 @@ var _ = require('underscore')._;
var commands = function(dbot) {
this.commands = {
'~alias': function(event) {
var nick = event.input[1] || event.user;
var nick = event.params[1] || event.user;
this.api.resolveUser(event.server, nick, function(err, user) {
if(user) {
this.api.getUserAliases(user.id, function(err, aliases) {
@ -37,8 +37,8 @@ var commands = function(dbot) {
},
'~addalias': function(event) {
var nick = event.input[1],
alias = event.input[2];
var nick = event.params[1],
alias = event.params[2];
this.api.resolveUser(event.server, nick, function(err, user) {
if(user) {
@ -61,6 +61,51 @@ var commands = function(dbot) {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
}.bind(this));
},
'~setaliasparent': function(event) {
var newPrimary = event.params[1];
this.api.resolveUser(event.server, newPrimary, function(user) {
if(user) {
if(user.primaryNick !== newPrimary) {
this.internalAPI.reparentUser(user, newPrimary, function() {
event.reply(dbot.t('aliasparentset', {
'newParent': newPrimary,
'newAlias': user.primaryNick
}));
});
} else {
event.reply(dbot.t('already_primary', { 'user': newPrimary }));
}
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
});
},
'~rmalias': function(event) {
var alias = event.params[1];
this.api.resolveUser(event.server, alias, function(err, user) {
if(user) { // Retrieving user record via alias proves existence of alias record
this.internalAPI.removeAlias(event.server, alias, function(err) {
event.reply(dbot.t('alias_removed', {
'primary': user.primaryNick,
'alias': alias
}));
});
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
});
}
};
this.commands['~setaliasparent'].access = 'moderator';
this.commands['~addalias'].access = 'moderator';
this.commands['~rmalias'].access = 'moderator';
this.commands['~mergeusers'].access = 'moderator';
};
exports.fetch = function(dbot) {
return commands(dbot);
};

View File

@ -43,6 +43,15 @@ var users = function(dbot) {
});
},
// Remove an alias record
'removeAlias': function(server, alias) {
var id = alias + '.' + server;
this.db.del('user_aliases', id, function(err) {
callback(err);
});
},
// Update current nick of user record
'updateCurrentNick': function(user, newNick, callback) {
user.currentNick = newNick;
this.db.save('users', user.id, user, function(err, result) {
@ -53,6 +62,35 @@ var users = function(dbot) {
callback(true, null);
}
});
},
// Merge two user records and aliases
'mergeUsers': function(oldUser, newUser, callback) {
this.db.search('user_aliases', { 'user': oldUser.id }, function(alias) {
alias.user = newUser.id;
this.db.save('user_aliases', alias.id, alias, function(){});
}.bind(this), function(){});
this.db.del('users', oldUser.id, function(err) {
if(!err) {
dbot.api.event.emit('merged_users', [
oldUser,
newUser
]);
callback(null);
} else {
callback(true);
}
});
},
// Set a new nick as the parent for a user (so just recreate and merge)
'reparentUser': function(user, newPrimary, callback) {
this.internalAPI.createUser(user.server, newPrimary, function(err, newUser) {
this.internalAPI.mergeUsers(user, newUser, function(err) {
callback(err);
});
}.bind(this));
}
};
@ -73,6 +111,7 @@ var users = function(dbot) {
this.on = ['NICK'];
/*** Pre-emit ***/
this.onLoad = function() {
// Create non-existing users and update current nicks
var checkUser = function(done) {
@ -80,15 +119,24 @@ var users = function(dbot) {
if(!user) {
this.internalAPI.createUser(event.server, event.user, done);
} else {
this.internalAPI.updateCurrentNick(user, event.user, done);
if(user.currentNick !== event.user) {
this.internalAPI.updateCurrentNick(user, event.user, done);
} else {
done(null, user);
}
}
}.bind(this));
};
dbot.instance.addPreEmitHook(function(event, callback) {
if(event.user && _.include(['JOIN', 'PRIVMSG'], event.action)) {
checkUser(callback);
checkUser(function(err, user) {
event.rUser = user;
callback(null);
});
} else {
callback(null);
}
});
};
}.bind(this);
};