2013-03-24 12:09:40 +01:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
uuid = require('node-uuid');
|
|
|
|
databank = require('databank'),
|
|
|
|
AlreadyExistsError = databank.AlreadyExistsError,
|
|
|
|
NoSuchThingError = databank.NoSuchThingError,
|
|
|
|
NotImplementedError = databank.NotImplementedError;
|
2013-01-14 22:03:47 +01:00
|
|
|
|
|
|
|
var api = function(dbot) {
|
2013-01-17 18:44:29 +01:00
|
|
|
var escapeRegexen = function(str) {
|
|
|
|
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
|
|
|
|
};
|
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
var api = {
|
2013-03-24 12:09:40 +01:00
|
|
|
'resolveUser': function(server, nick, callback) {
|
2013-03-22 12:44:27 +01:00
|
|
|
if(this.api.isPrimaryUser(nick)) {
|
2013-03-24 12:09:40 +01:00
|
|
|
callback(nick);
|
2013-03-22 12:44:27 +01:00
|
|
|
} else {
|
2013-03-24 12:09:40 +01:00
|
|
|
var user = false;
|
2013-03-22 12:44:27 +01:00
|
|
|
this.db.search('user', { 'server': server }, function(user) {
|
|
|
|
if(_.include(user.aliases, nick)) user = user.primaryNick;
|
2013-03-24 12:09:40 +01:00
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(user);
|
2013-03-22 12:44:27 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'getRandomChannelUser': function(server, channel, callback) {
|
|
|
|
var channel;
|
|
|
|
this.db.search('channel_users', {
|
|
|
|
'server': server,
|
|
|
|
'channel': channel
|
|
|
|
}, function(result) {
|
|
|
|
channel = result;
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
if(!_.isUndefined(channel.users)) {
|
|
|
|
callback(channel.users[_.random(0, channel.users.length - 1)]);
|
|
|
|
} else {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-01-26 20:21:09 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'getServerUsers': function(server, callback) {
|
2013-03-22 12:44:27 +01:00
|
|
|
var users = [];
|
2013-03-24 12:09:40 +01:00
|
|
|
this.db.search('users', { 'server': server }, function(user) {
|
|
|
|
users.push(user);
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(users);
|
2013-03-22 12:44:27 +01:00
|
|
|
}
|
|
|
|
});
|
2013-01-22 01:00:11 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'getAllUsers': function(callback) {
|
|
|
|
var users = [];
|
|
|
|
this.db.scan('users', function(user) {
|
|
|
|
users.push(user);
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(users);
|
|
|
|
}
|
|
|
|
});
|
2013-01-22 01:24:12 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'isKnownUser': function(server, nick, callback) {
|
|
|
|
this.api.resolveUser(server, nick, function(isKnown) {
|
|
|
|
if(isKnown == false) {
|
|
|
|
callback(false);
|
|
|
|
} else {
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
});
|
2013-01-14 22:03:47 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'isPrimaryUser': function(server, nick, callback) {
|
|
|
|
var isPrimaryUser = false;
|
|
|
|
this.db.search('users', {
|
|
|
|
'server': server,
|
|
|
|
'primaryNick': nick
|
|
|
|
}, function(user) {
|
|
|
|
isPrimaryUser = true;
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(isPrimaryUser);
|
|
|
|
}
|
|
|
|
});
|
2013-01-14 22:03:47 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'getAliases': function(server, nick, callback) {
|
|
|
|
var aliases;
|
|
|
|
this.db.search('users', {
|
|
|
|
'server': server,
|
|
|
|
'primaryNick': nick
|
|
|
|
}, function(result) {
|
|
|
|
aliases = result.aliases;
|
|
|
|
}, function(err) {
|
|
|
|
callback(aliases);
|
|
|
|
});
|
2013-01-15 20:09:54 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'isOnline': function(server, user, channel, callback) {
|
2013-01-15 20:09:54 +01:00
|
|
|
var user = this.api.resolveUser(server, user, useLowerCase);
|
|
|
|
var possiNicks = [user].concat(this.api.getAliases(server, user));
|
2013-01-24 01:23:26 +01:00
|
|
|
|
2013-01-24 01:30:22 +01:00
|
|
|
if(!_.has(dbot.instance.connections[server].channels, channel)) return false;
|
2013-01-15 20:09:54 +01:00
|
|
|
var onlineNicks = dbot.instance.connections[server].channels[channel].nicks;
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
var isOnline = _.any(onlineNicks, function(nick) {
|
2013-01-15 20:09:54 +01:00
|
|
|
nick = nick.name;
|
|
|
|
return _.include(possiNicks, nick);
|
|
|
|
}, this);
|
2013-03-24 12:09:40 +01:00
|
|
|
|
|
|
|
callback(isOnline);
|
2013-01-20 20:04:12 +01:00
|
|
|
},
|
|
|
|
|
2013-03-24 12:09:40 +01:00
|
|
|
'isChannelUser': function(server, user, channel) {
|
2013-01-20 20:04:12 +01:00
|
|
|
var knownUsers = this.getServerUsers(server);
|
|
|
|
var user = this.api.resolveUser(server, user, useLowerCase);
|
|
|
|
|
|
|
|
if(!_.has(knownUsers.channelUsers, channel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _.include(knownUsers.channelUsers[channel], user);
|
2013-01-14 22:03:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return api;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return api(dbot);
|
|
|
|
};
|