2013-03-24 12:09:40 +01:00
|
|
|
var _ = require('underscore')._,
|
2013-04-10 03:28:53 +02:00
|
|
|
uuid = require('node-uuid'),
|
2013-08-25 16:56:00 +02:00
|
|
|
databank = require('databank');
|
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:34:52 +01:00
|
|
|
// Return a user record given a primary nick or an alias
|
2013-08-28 01:32:31 +02:00
|
|
|
'resolveUser': function(server, nick, callback, lc) {
|
2013-08-26 00:36:06 +02:00
|
|
|
var user = false;
|
2013-08-28 01:32:31 +02:00
|
|
|
if(lc) nick = nick.toLowerCase();
|
2013-08-25 16:56:00 +02:00
|
|
|
if(_.has(this.userCache[server], nick)) {
|
|
|
|
this.api.getUser(this.userCache[server][nick], callback);
|
|
|
|
} else {
|
|
|
|
this.db.search('users', { 'server': server }, function(result) {
|
2013-08-28 01:32:31 +02:00
|
|
|
if(lc) {
|
|
|
|
result.primaryNick = result.primaryNick.toLowerCase();
|
|
|
|
_.each(result.aliases, function(v, k) {
|
|
|
|
result.aliases[k] = v.toLowerCase();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-25 16:56:00 +02:00
|
|
|
if(result.primaryNick == nick || _.include(result.aliases, nick)) {
|
2013-08-26 00:36:06 +02:00
|
|
|
user = result;
|
2013-08-25 16:56:00 +02:00
|
|
|
}
|
2013-08-25 17:37:32 +02:00
|
|
|
}.bind(this), function(err) {
|
2014-01-04 18:59:14 +01:00
|
|
|
if(user) {
|
|
|
|
if(!_.has(this.userCache[server], nick)) {
|
|
|
|
this.userCache[server][nick] = user.id;
|
|
|
|
} else if(this.userCache[server][nick] !== user.id) {
|
|
|
|
this.userCache[server][nick] = user.id;
|
|
|
|
}
|
|
|
|
}
|
2013-08-26 00:36:06 +02:00
|
|
|
callback(user);
|
2014-01-04 18:59:14 +01:00
|
|
|
}.bind(this));
|
2013-08-25 16:56:00 +02:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
},
|
|
|
|
|
2013-08-18 17:19:15 +02:00
|
|
|
// Return many user records given primary nicks of aliases
|
|
|
|
'resolveUsers': function(server, nicks, callback) {
|
|
|
|
var users = [];
|
|
|
|
this.db.search('users', { 'server': server }, function(result) {
|
|
|
|
var pNicks = result.aliases.slice(0).unshift(result.primaryNick);
|
|
|
|
for(var i=0;i<pNicks.length;i++) {
|
|
|
|
var n = _.indexOf(nicks, pNicks[i]);
|
|
|
|
if(n != -1) {
|
|
|
|
users.push(result);
|
|
|
|
nicks = _.without(nicks, nicks[n]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(users, nicks);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-04-10 01:16:00 +02:00
|
|
|
// Return a user record given a UUID
|
|
|
|
'getUser': function(uuid, callback) {
|
2013-08-25 16:56:00 +02:00
|
|
|
this.db.read('users', uuid, function(err, user) {
|
|
|
|
if(err) user = false;
|
|
|
|
callback(user);
|
|
|
|
});
|
2013-04-10 01:16:00 +02:00
|
|
|
},
|
|
|
|
|
2013-08-17 19:09:55 +02:00
|
|
|
'resolveChannel': function(server, channelName, callback) {
|
2013-03-24 13:26:33 +01:00
|
|
|
var channel = false;
|
|
|
|
this.db.search('channel_users', {
|
|
|
|
'server': server,
|
2013-03-24 17:04:01 +01:00
|
|
|
'name': channelName
|
2013-03-24 13:26:33 +01:00
|
|
|
}, function(result) {
|
|
|
|
channel = result;
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(channel);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-17 19:09:55 +02:00
|
|
|
'getChannel': function(uuid, callback) {
|
|
|
|
this.db.read('channel_users', uuid, function(err, channel) {
|
|
|
|
if(err) channel = false;
|
|
|
|
callback(channel);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-30 04:55:20 +02:00
|
|
|
'getRandomChannelUser': function(server, cName, callback) {
|
2014-04-12 01:19:31 +02:00
|
|
|
if(_.has(dbot.instance.connections[server].channels, cName)) {
|
|
|
|
var nicks = _.keys(dbot.instance.connections[server].channels[cName].nicks);
|
|
|
|
var randomUser = nicks[_.random(0, nicks.length -1)]
|
|
|
|
callback(randomUser);
|
|
|
|
} else {
|
|
|
|
callback(false);
|
|
|
|
}
|
2013-01-26 20:21:09 +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-05-21 22:17:22 +02:00
|
|
|
'getAllChannels': function(callback) {
|
|
|
|
var channels = [];
|
|
|
|
this.db.scan('channel_users', function(channel) {
|
|
|
|
channels.push(channel);
|
|
|
|
}, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
callback(channels);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-24 12:34:52 +01:00
|
|
|
'isOnline': function(server, nick, channel, callback) {
|
|
|
|
this.api.resolveUser(server, nick, function(user) {
|
|
|
|
var possiNicks = [user].concat(user.aliases);
|
2013-01-24 01:23:26 +01:00
|
|
|
|
2013-03-24 12:34:52 +01:00
|
|
|
if(_.has(dbot.instance.connections[server].channels, channel)) {
|
|
|
|
var onlineNicks = dbot.instance.connections[server].channels[channel].nicks;
|
|
|
|
var isOnline = _.any(onlineNicks, function(nick) {
|
|
|
|
nick = nick.name;
|
|
|
|
return _.include(possiNicks, nick);
|
|
|
|
}, this);
|
2013-01-15 20:09:54 +01:00
|
|
|
|
2013-03-24 12:34:52 +01:00
|
|
|
callback(isOnline);
|
|
|
|
}
|
|
|
|
});
|
2013-05-02 20:25:10 +02: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-04-14 22:00:53 +02:00
|
|
|
api['resolveUser'].external = true;
|
|
|
|
api['resolveUser'].extMap = [ 'server', 'nick', 'callback' ];
|
|
|
|
|
|
|
|
api['getChannel'].external = true;
|
|
|
|
api['getChannel'].extMap = [ 'server', 'channel', 'callback' ];
|
|
|
|
|
2013-04-20 18:34:02 +02:00
|
|
|
api['getAllUsers'].external = true;
|
|
|
|
api['getAllUsers'].extMap = [ 'callback' ];
|
|
|
|
|
2013-05-21 22:17:22 +02:00
|
|
|
api['getAllChannels'].external = true;
|
|
|
|
api['getAllChannels'].extMap = [ 'callback' ];
|
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
return api;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return api(dbot);
|
|
|
|
};
|