irc updates

This commit is contained in:
Andris Reinman 2017-09-19 09:33:21 +03:00
parent 6db8e096bc
commit 711baaf00b
2 changed files with 66 additions and 14 deletions

View file

@ -22,6 +22,13 @@ indexes:
name: show_new
key:
created: -1
- collection: users
type: users # index applies to users database
index:
name: users_namespace
key:
ns: 1
unameview: 1
# Indexes for the addresses collection
@ -344,5 +351,13 @@ indexes:
unique: true
key:
ns: 1
unickview: 1
nickview: 1
user: 1
- collection: channels
index:
name: irc_channels
unique: true
key:
ns: 1
channelview: 1

View file

@ -381,7 +381,7 @@ class IRCConnection extends EventEmitter {
if (!this.session.auth) {
this.send({
source: ':NickServ!NickServ@services.',
source: 'NickServ!NickServ@services.',
verb: 'NOTICE',
message: 'This server requires all users to be authenticated. Identify via /msg NickServ identify <password>'
});
@ -440,6 +440,7 @@ class IRCConnection extends EventEmitter {
}
}
this.session.nick = this.session.nick || userData.username;
this.session.ns = ns || 'root';
next(null, {
@ -453,14 +454,14 @@ class IRCConnection extends EventEmitter {
getNick(nick, next) {
let ns = this.session.ns;
let unickview = nick.toLowerCase().replace(/\./g, '');
let nickview = nick.toLowerCase().replace(/\./g, '');
let user = this.session.auth.id;
let verifyUser = done => {
if (unickview === this.session.auth.username.replace(/\./g, '')) {
if (nickview === this.session.auth.username.replace(/\./g, '')) {
return done();
}
db.users.collection('users').findOne({ unameview: unickview }, {
db.users.collection('users').findOne({ unameview: nickview }, {
fields: {
_id: true,
username: true
@ -479,7 +480,7 @@ class IRCConnection extends EventEmitter {
verifyUser(() => {
db.database.collection('nicks').insertOne({
ns,
unickview,
nickview,
nick,
user
}, (err, r) => {
@ -487,7 +488,7 @@ class IRCConnection extends EventEmitter {
if (err.code === 11000) {
return db.database.collection('nicks').findOne({
ns,
unickview
nickview
}, (err, nickData) => {
if (err) {
return next(err);
@ -511,7 +512,7 @@ class IRCConnection extends EventEmitter {
// try to remove old nicks
db.database.collection('nicks').deleteOne({
ns,
unickview: { $ne: unickview },
nickview: { $ne: nickview },
user
}, () => next(null, insertId));
});
@ -670,7 +671,7 @@ class IRCConnection extends EventEmitter {
}
let channel = params[0].trim();
if (channel.length < 2 || !/^[#&]/.test(channel) || /[#&\s]/.test(channel.substr(1))) {
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();
}
@ -679,9 +680,45 @@ class IRCConnection extends EventEmitter {
return next();
}
this.server.join(channel, this);
let channelview = channel.toLowerCase().replace(/\./g, '');
return next();
let tryCount = 0;
let tryGetChannel = () => {
db.database.collection('channels').findOne({
channelview,
ns: this.session.ns
}, (err, channelData) => {
if (err) {
this.send({ verb: 'ERR_FILEERROR', params: channel, message: err.message });
return next();
}
if (channelData) {
this.server.join(channel, this);
return next();
}
db.database.collection('channels').insertOne({
channel,
channelview,
ns: this.session.ns,
user: this.session.auth.id,
mode: []
}, 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.server.join(channel, this);
return next();
});
});
};
tryGetChannel();
}
command_PART(params, next) {
@ -826,7 +863,7 @@ class IRCConnection extends EventEmitter {
if (this.session.auth) {
this.send({
source: ':NickServ!NickServ@services.',
source: 'NickServ!NickServ@services.',
verb: 'NOTICE',
target: this.session.nick,
message: 'Already identified as ' + this.session.user
@ -850,7 +887,7 @@ class IRCConnection extends EventEmitter {
if (auth) {
this.session.auth = auth;
this.send({
source: ':NickServ!NickServ@services.',
source: 'NickServ!NickServ@services.',
verb: 'NOTICE',
target: this.session.nick,
message: 'You are now identified for ' + this.session.user
@ -858,7 +895,7 @@ class IRCConnection extends EventEmitter {
return this.verifyNickChange(false, next);
} else {
this.send({
source: ':NickServ!NickServ@services.',
source: 'NickServ!NickServ@services.',
verb: 'NOTICE',
target: this.session.nick,
message: 'Invalid password for ' + this.session.user