updated err messages

This commit is contained in:
Andris Reinman 2017-10-01 11:03:09 +03:00
parent 19faca4388
commit cc80fdb5e9

View file

@ -990,7 +990,7 @@ class IRCConnection extends EventEmitter {
checkAuth() {
if (!this.session.auth) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'PRIVMSG', message: 'Authentication required to chat in this server' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'Authentication required to chat in this server' });
return false;
}
return true;
@ -1026,7 +1026,7 @@ class IRCConnection extends EventEmitter {
return next();
}
if (!this.session.user) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'PING', message: 'You have not registered' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
let host = params[0] || this.session.clientHostname;
@ -1106,7 +1106,7 @@ class IRCConnection extends EventEmitter {
command_JOIN(tags, prefix, params, next) {
if (!this.session.user || !this.session.nick) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'JOIN', message: 'You have not registered' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
if (!params.length) {
@ -1114,139 +1114,151 @@ class IRCConnection extends EventEmitter {
return next();
}
let channel = params[0].trim();
if (channel.length < 2 || !/^[#&]/.test(channel) || /[#&\s]/.test(channel.substr(1)) || /^[#&]\.+$/.test(channel)) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'Invalid channel name' });
return next();
}
if (!this.checkAuth()) {
return next();
}
let channelview = channel.toLowerCase().replace(/\./g, '');
let channels = params[0]
.split(',')
.map(channel => channel.trim())
.filter(channel => channel);
let sendJoinMessages = (channelData, fresh, next) => {
let idString = this.session.auth.id.toString();
if (channels.length === 1 && channels[0] === '0') {
// TODO: leave all channels
return next();
}
let eventData = {
action: 'join',
channel: channelData.channel,
session: this.session.id.toString(),
channelId: channelData._id.toString(),
user: idString,
nick: this.getFormattedName()
};
if (fresh) {
// notify channel members
this.publish([this.session.ns, '#', channelData._id].join('.'), eventData);
let channelPos = 0;
let processNext = () => {
if (channelPos >= channels.length) {
return next();
}
// notify other instances of self
this.publish([this.session.ns, '%', idString].join('.'), eventData);
this.printNickList(channelData, fresh, next);
};
let channel = channels[channelPos++];
if (channel.length < 2 || !/^[#&]/.test(channel) || /[#&\s]/.test(channel.substr(1)) || /^[#&]\.+$/.test(channel)) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'Invalid channel name' });
return setImmediate(processNext);
}
let tryCount = 0;
let tryGetChannel = () => {
db.database.collection('channels').findOne({
ns: this.session.ns,
channelview
}, {
fields: {
_id: true,
channel: true,
topic: true,
topicTime: true,
topicAuthor: true
}
}, (err, channelData) => {
if (err) {
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return next();
let channelview = channel.toLowerCase().replace(/\./g, '');
let sendJoinMessages = (channelData, fresh, done) => {
let idString = this.session.auth.id.toString();
let eventData = {
action: 'join',
channel: channelData.channel,
session: this.session.id.toString(),
channelId: channelData._id.toString(),
user: idString,
nick: this.getFormattedName()
};
if (fresh) {
// notify channel members
this.publish([this.session.ns, '#', channelData._id].join('.'), eventData);
}
if (channelData) {
return db.database.collection('channels').findOneAndUpdate({
_id: channelData._id
}, {
$addToSet: {
members: this.session.auth.id
}
}, {
returnOriginal: true
}, (err, result) => {
// notify other instances of self
this.publish([this.session.ns, '%', idString].join('.'), eventData);
this.printNickList(channelData, fresh, done);
};
let tryCount = 0;
let tryGetChannel = () => {
db.database.collection('channels').findOne({
ns: this.session.ns,
channelview
}, {
fields: {
_id: true,
channel: true,
topic: true,
topicTime: true,
topicAuthor: true
}
}, (err, channelData) => {
if (err) {
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return setImmediate(processNext);
}
if (channelData) {
return db.database.collection('channels').findOneAndUpdate({
_id: channelData._id
}, {
$addToSet: {
members: this.session.auth.id
}
}, {
returnOriginal: true
}, (err, result) => {
if (err) {
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return setImmediate(processNext);
}
if (!result || !result.value) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'Could not open channel' });
return setImmediate(processNext);
}
channelData = result.value;
this.subscribe([this.session.ns, '#', channelData._id].join('.'));
let idString = this.session.auth.id.toString();
if (!result.value.members.find(member => member.toString() === idString)) {
// new join!
return sendJoinMessages(channelData, true, processNext);
}
sendJoinMessages(channelData, false, processNext);
});
}
let time = new Date();
channelData = {
_id: new ObjectID(),
channel,
channelview,
ns: this.session.ns,
mode: [],
owner: this.session.auth.id,
members: [this.session.auth.id],
time,
topic: '',
topicTime: time,
topicAuthor: ''
};
db.database.collection('channels').insertOne(channelData, err => {
if (err) {
if (err.code === 11000 && tryCount++ < 5) {
return setTimeout(tryGetChannel, 100);
}
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return next();
return setImmediate(processNext);
}
if (!result || !result.value) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'Could not open channel' });
return next();
}
channelData = result.value;
this.subscribe([this.session.ns, '#', channelData._id].join('.'));
let idString = this.session.auth.id.toString();
if (!result.value.members.find(member => member.toString() === idString)) {
// new join!
return sendJoinMessages(channelData, true, next);
}
sendJoinMessages(channelData, false, next);
sendJoinMessages(channelData, false, processNext);
});
}
let time = new Date();
channelData = {
_id: new ObjectID(),
channel,
channelview,
ns: this.session.ns,
mode: [],
owner: this.session.auth.id,
members: [this.session.auth.id],
time,
topic: '',
topicTime: time,
topicAuthor: ''
};
db.database.collection('channels').insertOne(channelData, err => {
if (err) {
if (err.code === 11000 && tryCount++ < 5) {
return setTimeout(tryGetChannel, 100);
}
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return next();
}
this.subscribe([this.session.ns, '#', channelData._id].join('.'));
sendJoinMessages(channelData, false, next);
});
});
};
tryGetChannel();
};
tryGetChannel();
processNext();
}
command_PART(tags, prefix, params, next) {
if (!this.session.user || !this.session.nick) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'JOIN', message: 'You have not registered' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
if (!params.length) {
this.send({ verb: 'ERR_NEEDMOREPARAMS', params: 'JOIN', message: 'Not enough parameters' });
return next();
}
let channel = params[0].trim();
if (channel.length < 2 || !/^[#&]/.test(channel) || /[#&\s]/.test(channel.substr(1))) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'No such channel' });
this.send({ verb: 'ERR_NEEDMOREPARAMS', params: 'PART', message: 'Not enough parameters' });
return next();
}
@ -1254,52 +1266,78 @@ class IRCConnection extends EventEmitter {
return next();
}
db.database.collection('channels').findOneAndUpdate({
ns: this.session.ns,
channelview: channel.toLowerCase().replace(/\./g, ''),
members: this.session.auth.id
}, {
$pull: {
members: this.session.auth.id
}
}, {
returnOriginal: false
}, (err, result) => {
if (err) {
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return next();
}
if (!result || !result.value) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'No such channel' });
return next();
}
let channelData = result.value;
let eventData = {
action: 'part',
channel: channelData.channel,
session: this.session.id.toString(),
channelId: channelData._id.toString(),
user: this.session.auth.id.toString(),
nick: this.getFormattedName()
};
this.send({ source: this.getFormattedName(), verb: 'PART', target: false, message: channelData.channel });
let subscriptionKey = [this.session.ns, '#', channelData._id].join('.');
// notify channel members
this.publish(subscriptionKey, eventData);
this.unsubscribe(subscriptionKey);
let channels = params[0]
.split(',')
.map(channel => channel.trim())
.filter(channel => channel);
//let reason = params[1] || '';
if (channels.length === 1 && channels[0] === '0') {
// TODO: leave all channels
return next();
});
}
let channelPos = 0;
let processNext = () => {
if (channelPos >= channels.length) {
return next();
}
let channel = channels[channelPos++];
if (channel.length < 2 || !/^[#&]/.test(channel) || /[#&\s]/.test(channel.substr(1))) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'No such channel' });
return setImmediate(processNext);
}
db.database.collection('channels').findOneAndUpdate({
ns: this.session.ns,
channelview: channel.toLowerCase().replace(/\./g, ''),
members: this.session.auth.id
}, {
$pull: {
members: this.session.auth.id
}
}, {
returnOriginal: false
}, (err, result) => {
if (err) {
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return setImmediate(processNext);
}
if (!result || !result.value) {
this.send({ verb: 'ERR_NOSUCHCHANNEL', params: channel, message: 'No such channel' });
return setImmediate(processNext);
}
let channelData = result.value;
let eventData = {
action: 'part',
channel: channelData.channel,
session: this.session.id.toString(),
channelId: channelData._id.toString(),
user: this.session.auth.id.toString(),
nick: this.getFormattedName()
};
this.send({ source: this.getFormattedName(), verb: 'PART', target: false, message: channelData.channel });
let subscriptionKey = [this.session.ns, '#', channelData._id].join('.');
// notify channel members
this.publish(subscriptionKey, eventData);
this.unsubscribe(subscriptionKey);
return setImmediate(processNext);
});
};
processNext();
}
command_PRIVMSG(tags, prefix, params, next) {
if (!this.session.user || !this.session.nick) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'PRIVMSG', message: 'You have not registered' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
@ -1634,7 +1672,7 @@ class IRCConnection extends EventEmitter {
command_MODE(tags, prefix, params, next) {
if (!this.session.user || !this.session.nick) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'JOIN', message: 'You have not registered' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
@ -1684,7 +1722,7 @@ class IRCConnection extends EventEmitter {
command_TOPIC(tags, prefix, params, next) {
if (!this.session.user || !this.session.nick) {
this.send({ verb: 'ERR_NOTREGISTERED', params: 'JOIN', message: 'You have not registered' });
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
@ -1778,6 +1816,25 @@ class IRCConnection extends EventEmitter {
});
}
}
command_OPER(tags, prefix, params, next) {
if (!this.session.user || !this.session.nick) {
this.send({ verb: 'ERR_NOTREGISTERED', message: 'You have not registered' });
return next();
}
if (!params.length) {
this.send({ verb: 'ERR_NEEDMOREPARAMS', params: 'OPER', message: 'Not enough parameters' });
return next();
}
if (!this.checkAuth()) {
return next();
}
this.send({ verb: 'ERR_NOOPERHOST', message: 'No O-lines for your host' });
return next();
}
}
module.exports = IRCConnection;