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