2013-08-29 23:12:45 +02:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
moment = require('moment-timezone');
|
2013-01-14 22:03:47 +01:00
|
|
|
|
|
|
|
var commands = function(dbot) {
|
|
|
|
var commands = {
|
|
|
|
'~alias': function(event) {
|
2013-06-27 21:19:13 +02:00
|
|
|
var nick = event.params[1].trim() || event.user;
|
2013-03-24 17:04:01 +01:00
|
|
|
this.api.resolveUser(event.server, nick, function(user) {
|
|
|
|
if(user) {
|
|
|
|
if(nick == user.primaryNick) {
|
|
|
|
var aliases = _.first(user.aliases, 10);
|
|
|
|
var including = 'including: ' + aliases.join(', ') + '.';
|
2013-01-14 22:03:47 +01:00
|
|
|
|
2013-03-24 17:04:01 +01:00
|
|
|
if(user.aliases.length != 0) {
|
|
|
|
event.reply(dbot.t('primary', {
|
|
|
|
'user': nick,
|
2013-03-25 14:55:47 +01:00
|
|
|
'currentNick': user.currentNick,
|
2013-03-24 17:04:01 +01:00
|
|
|
'count': user.aliases.length,
|
|
|
|
}) + including);
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('primary', {
|
|
|
|
'user': nick,
|
2013-03-25 14:55:47 +01:00
|
|
|
'currentNick': user.currentNick,
|
2013-03-24 17:04:01 +01:00
|
|
|
'count': user.aliases.length
|
|
|
|
}).slice(0, -2) + ".");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('alias', {
|
|
|
|
'alias': nick,
|
|
|
|
'user': user.primaryNick
|
|
|
|
}));
|
|
|
|
}
|
2013-01-20 19:26:07 +01:00
|
|
|
} else {
|
2013-03-24 17:04:01 +01:00
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
|
2013-01-20 19:19:44 +01:00
|
|
|
}
|
2013-03-24 17:04:01 +01:00
|
|
|
});
|
2013-01-14 22:03:47 +01:00
|
|
|
},
|
2013-08-29 23:12:45 +02:00
|
|
|
|
|
|
|
'~timezone': function(event) {
|
|
|
|
if(event.params[1]) {
|
|
|
|
try {
|
|
|
|
moment().tz(event.params[1]);
|
|
|
|
} catch(err) {
|
|
|
|
return event.reply('Invalid timezone. See http://momentjs.com/timezone/');
|
|
|
|
}
|
|
|
|
|
|
|
|
event.rUser.timezone = event.params[1];
|
|
|
|
this.db.save('users', event.rUser.id, event.rUser, function() {
|
|
|
|
event.reply('Timezone updated.');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply('Current timezone: ' + event.rUser.timezone);
|
|
|
|
}
|
|
|
|
},
|
2013-01-14 22:03:47 +01:00
|
|
|
|
2013-10-20 19:47:40 +02:00
|
|
|
'~time': function(event) {
|
2013-10-20 19:50:19 +02:00
|
|
|
var nick = event.params[1].trim();
|
2013-10-20 19:47:40 +02:00
|
|
|
this.api.resolveUser(event.server, nick, function(user) {
|
|
|
|
if(user) {
|
|
|
|
if(user.timezone) {
|
|
|
|
var fDate = moment.tz(user.timezone).format('LLL');
|
|
|
|
event.reply('The time for ' + user.primaryNick + ' is: ' + fDate);
|
|
|
|
} else {
|
|
|
|
event.reply(user.primaryNick + ' doesn\'t have a timezone set. They can do this with ~timezone.');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-28 22:22:56 +02:00
|
|
|
'~setmobilealias': function(event) {
|
|
|
|
if(_.include(event.rUser.aliases, event.params[1])) {
|
|
|
|
if(!_.has(event.rUser, 'mobile')) event.rUser.mobile = [];
|
|
|
|
if(!_.include(event.rUser.mobile, event.params[1])) {
|
|
|
|
event.rUser.mobile.push(event.params[1]);
|
|
|
|
this.db.save('users', event.rUser.id, event.rUser, function(err) {
|
|
|
|
event.reply(dbot.t('added_mobile_alias', { 'alias': event.params[1] }));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('already_mobile', { 'alias': event.params[1] }));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': event.params[1] }));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-20 13:21:04 +02:00
|
|
|
'~addalias': function(event) {
|
|
|
|
var nick = event.input[1],
|
|
|
|
alias = event.input[2];
|
|
|
|
|
|
|
|
this.api.resolveUser(event.server, nick, function(user) {
|
|
|
|
if(user) {
|
|
|
|
if(!_.include(user.aliases, alias)) {
|
|
|
|
user.aliases.push(alias);
|
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
event.reply(dbot.t('alias_added', {
|
|
|
|
'user': user.primaryNick,
|
|
|
|
'alias': alias
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('alias_exists', { 'alias': alias }));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': nick }));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2013-08-17 19:23:43 +02:00
|
|
|
'~removealias': function(event) {
|
|
|
|
var alias = event.params[1];
|
|
|
|
|
|
|
|
this.api.resolveUser(event.server, alias, function(user) {
|
|
|
|
if(user) {
|
|
|
|
user.aliases = _.without(user.aliases, alias);
|
2013-08-28 22:22:56 +02:00
|
|
|
user.mobile = _.without(user.mobile, alias);
|
2013-08-17 19:23:43 +02:00
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
event.reply(dbot.t('alias_removed', {
|
|
|
|
'primary': user.primaryNick,
|
|
|
|
'alias': alias
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} else {
|
2013-08-18 21:55:06 +02:00
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': alias }));
|
2013-08-17 19:23:43 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
'~setaliasparent': function(event) {
|
2013-03-24 17:04:01 +01:00
|
|
|
var newPrimary = event.params[1].trim();
|
|
|
|
this.api.resolveUser(event.server, newPrimary, function(user) {
|
2013-03-24 17:59:26 +01:00
|
|
|
if(user && user.primaryNick != newPrimary) {
|
2013-03-24 17:04:01 +01:00
|
|
|
var newAlias = user.primaryNick;
|
|
|
|
user.primaryNick = newPrimary;
|
2013-03-24 17:59:26 +01:00
|
|
|
user.aliases = _.without(user.aliases, newPrimary);
|
|
|
|
user.aliases.push(newAlias);
|
2013-01-14 22:03:47 +01:00
|
|
|
|
2013-03-24 17:04:01 +01:00
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
if(!err) {
|
2013-03-24 17:59:26 +01:00
|
|
|
event.reply(dbot.t('aliasparentset', {
|
|
|
|
'newParent': newPrimary,
|
|
|
|
'newAlias': newAlias
|
|
|
|
}));
|
2013-03-24 17:04:01 +01:00
|
|
|
dbot.api.event.emit('~setaliasparent', {
|
|
|
|
'server': event.server,
|
|
|
|
'alias': newAlias
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2013-08-28 22:22:56 +02:00
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': newPrimary }));
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-01-14 22:03:47 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~mergeusers': function(event) {
|
|
|
|
var primaryUser = event.params[1];
|
|
|
|
var secondaryUser = event.params[2];
|
|
|
|
|
2013-03-24 17:04:01 +01:00
|
|
|
this.api.resolveUser(event.server, primaryUser, function(user) {
|
|
|
|
if(user) {
|
2013-05-26 14:37:37 +02:00
|
|
|
this.api.resolveUser(event.server, secondaryUser, function(oldUser) {
|
2013-05-26 15:29:21 +02:00
|
|
|
if(oldUser) {
|
2013-05-26 14:37:37 +02:00
|
|
|
user.aliases.push(oldUser.primaryNick);
|
2013-05-26 15:29:21 +02:00
|
|
|
user.aliases = _.union(user.aliases, oldUser.aliases);
|
2013-08-17 19:09:55 +02:00
|
|
|
this.internalAPI.mergeChannelUsers(oldUser, user);
|
2013-05-26 14:37:37 +02:00
|
|
|
this.db.del('users', oldUser.id, function(err) {
|
2013-03-24 17:04:01 +01:00
|
|
|
if(!err) {
|
2013-08-24 20:32:52 +02:00
|
|
|
this.db.save('user_redirs', oldUser.id, user.id, function() {});
|
2013-03-24 17:04:01 +01:00
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
event.reply(dbot.t('merged_users', {
|
|
|
|
'old_user': secondaryUser,
|
|
|
|
'new_user': primaryUser
|
|
|
|
}));
|
2013-05-26 15:11:09 +02:00
|
|
|
dbot.api.event.emit('~mergeusers', [
|
|
|
|
event.server,
|
|
|
|
oldUser,
|
|
|
|
user
|
|
|
|
]);
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
2013-03-24 17:59:26 +01:00
|
|
|
}.bind(this));
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
2013-03-24 17:59:26 +01:00
|
|
|
}.bind(this));
|
2013-03-24 17:04:01 +01:00
|
|
|
} else {
|
2013-05-26 14:37:37 +02:00
|
|
|
event.reply(dbot.t('unprimary_error', { 'nick': secondaryUser }));
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
2013-03-24 17:59:26 +01:00
|
|
|
}.bind(this));
|
2013-03-24 17:04:01 +01:00
|
|
|
} else {
|
2013-05-26 14:37:37 +02:00
|
|
|
event.reply(dbot.t('unprimary_error', { 'nick': primaryUser }));
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
2013-03-24 17:59:26 +01:00
|
|
|
}.bind(this));
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
};
|
2013-03-21 07:58:40 +01:00
|
|
|
|
|
|
|
commands['~alias'].regex = [/^~alias ([\d\w[\]{}^|\\`_-]+?)/, 2];
|
2013-10-20 19:48:58 +02:00
|
|
|
commands['~time'].regex = [/^~time ([\d\w[\]{}^|\\`_-]+?)/, 2];
|
2013-03-21 07:58:40 +01:00
|
|
|
commands['~setaliasparent'].regex = [/^~setaliasparent ([\d\w[\]{}^|\\`_-]+?)/, 2];
|
|
|
|
commands['~mergeusers'].regex = [/^~mergeusers ([\d\w[\]{}^|\\`_-]+?)\s*?([\d\w[\]{}^|\\`_-]+?)/, 3];
|
2013-04-20 13:21:04 +02:00
|
|
|
commands['~addalias'].regex = [/^~addalias ([\d\w[\]{}^|\\`_-]+?) ([\d\w[\]{}^|\\`_-]+?)$/, 3];
|
2013-03-21 07:58:40 +01:00
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
commands['~setaliasparent'].access = 'moderator';
|
|
|
|
commands['~mergeusers'].access = 'moderator';
|
2013-04-20 13:21:04 +02:00
|
|
|
commands['~addalias'].access = 'moderator';
|
2013-08-17 19:23:43 +02:00
|
|
|
commands['~removealias'].access = 'moderator';
|
2013-01-14 22:03:47 +01:00
|
|
|
|
|
|
|
return commands;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return commands(dbot);
|
|
|
|
};
|