3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00

well, there's a start

This commit is contained in:
reality 2014-09-04 08:27:46 +00:00
parent 1a590bd678
commit b950426d7b
3 changed files with 225 additions and 0 deletions

65
modules/users/newapi.js Normal file
View File

@ -0,0 +1,65 @@
var _ = require('underscore')._;
var api = function(dbot) {
this.api = {
// Retrieve a user record given a server and nickname
'resolveUser': function(server, nick, callback) {
var id = nick + '.' + server;
this.api.getUser(id, function(err, result) {
if(!err) {
callback(null, result);
} else {
this.db.read('user_aliases', id, function(err, result) {
if(!err) {
this.api.getUser(result.user, callback);
} else {
callback(true, null);
}
}.bind(this));
}
}.bind(this));
},
// Retrive a user record given its ID
'getUser': function(id, callback) {
this.db.read('users', id, function(err, result) {
if(!err) {
callback(null, result);
} else {
callback(true, null);
}
});
},
// Retrieve user aliases given a user ID
'getUserAliases': function(id, callback) {
var aliases = [];
this.db.search('user_aliases', { 'user': id }, function(result) {
aliases.push(result.alias);
}, function(err) {
if(!err) {
callback(null, aliases);
} else {
callback(true, null);
}
});
},
// Retrieve a channel record given a server and a channel name
'resolveChannel': function(server, channel, callback) {
var id = channel + '.' + server;
this.api.getChannel(id, callback);
},
// Retrieve a channel record given its ID
'getChannel': function(id, callback) {
this.db.read('channels', id, function(err, result) {
if(!err) {
callback(null, result);
} else {
callback(true, null);
}
});
}
};
};

View File

@ -0,0 +1,66 @@
var _ = require('underscore')._;
var commands = function(dbot) {
this.commands = {
'~alias': function(event) {
var nick = event.input[1] || event.user;
this.api.resolveUser(event.server, nick, function(err, user) {
if(user) {
this.api.getUserAliases(user.id, function(err, aliases) {
var including = _.first(aliases, 10).join(', ');
if(nick === user.primaryNick) {
if(aliases.length === 0) {
event.reply(dbot.t('primary_no_alias', {
'user': user.primaryNick,
'currentNick': user.currentNick
}));
} else {
event.reply(dbot.t('primary', {
'user': user.primaryNick,
'currentNick': user.currentNick,
'count': aliases.length,
'including': including
}));
}
} else {
event.reply(dbot.t('alias', {
'alias': nick,
'user': user.primaryNick
}));
}
});
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
}.bind(this));
},
'~addalias': function(event) {
var nick = event.input[1],
alias = event.input[2];
this.api.resolveUser(event.server, nick, function(err, user) {
if(user) {
this.api.resolveUser(event.server, alias, function(err, aUser) {
if(!aUser) {
this.internalAPI.createAlias(alias, user, function(err) {
event.reply(dbot.t('alias_added', {
'user': user.primaryNick,
'alias': alias
}));
});
} else {
event.reply(dbot.t('alias_exists', {
'alias': alias,
'user': aUser.primaryNick
}));
}
});
} else {
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
}
}.bind(this));
}
};
};

94
modules/users/newusers.js Normal file
View File

@ -0,0 +1,94 @@
/**
* Name: Users
* Description: Track known users
*/
var _ = require('underscore')._;
var users = function(dbot) {
/*** Internal API ***/
this.internalAPI = {
// Create new user record
'createUser': function(server, nick, callback) {
var id = nick + '.' + server;
this.db.create('users', id, {
'id': id,
'server': server,
'primaryNick': nick,
'currentNick': nick
}, function(err, result) {
if(!err) {
dbot.api.event.emit('new_user', [ result ]);
callback(null, result);
} else {
callback(true, null);
}
});
},
// Add new user alias
'createAlias': function(alias, user, callback) {
var id = alias + '.' + user.server;
this.db.create('user_aliases', id, {
'id': id,
'alias': alias,
'user': user.id
}, function(err, result) {
if(!err) {
dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]);
callback(null, result);
} else {
callback(true, null);
}
});
},
'updateCurrentNick': function(user, newNick, callback) {
user.currentNick = newNick;
this.db.save('users', user.id, user, function(err, result) {
if(!err) {
dbot.api.event.emit('new_current_nick', [ user, newNick ]);
callback(null, result);
} else {
callback(true, null);
}
});
}
};
/*** Listener ***/
// Track nick changes
this.listener = function(event) {
// Update current nick
this.internalAPI.updateCurrentNick(event.rUser, event.newNick, function(){});
// Add new alias record if nick is not already claimed
this.api.resolveUser(event.server, event.newNick, function(err, user) {
if(!user) {
this.internalAPI.createAlias(event.newNick, event.rUser, function(){});
}
}.bind(this));
}.bind(this);
this.on = ['NICK'];
/*** Pre-emit ***/
this.onLoad = function() {
// Create non-existing users and update current nicks
var checkUser = function(done) {
this.api.resolveUser(event.server, event.user, function(err, user) {
if(!user) {
this.internalAPI.createUser(event.server, event.user, done);
} else {
this.internalAPI.updateCurrentNick(user, event.user, done);
}
}.bind(this));
};
dbot.instance.addPreEmitHook(function(event, callback) {
if(event.user && _.include(['JOIN', 'PRIVMSG'], event.action)) {
checkUser(callback);
}
});
};
};