|
|
|
@ -1,9 +1,25 @@ |
|
|
|
|
import { Meteor } from 'meteor/meteor'; |
|
|
|
|
import Queue from 'queue-fifo'; |
|
|
|
|
import moment from 'moment'; |
|
|
|
|
import _ from 'underscore'; |
|
|
|
|
|
|
|
|
|
import * as peerCommandHandlers from './peerHandlers'; |
|
|
|
|
import * as localCommandHandlers from './localHandlers'; |
|
|
|
|
import { callbacks } from '../../../callbacks'; |
|
|
|
|
import * as servers from '../servers'; |
|
|
|
|
import { Settings } from '../../../models/server'; |
|
|
|
|
|
|
|
|
|
let removed = false; |
|
|
|
|
const updateLastPing = _.throttle(Meteor.bindEnvironment(() => { |
|
|
|
|
if (removed) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Settings.upsert({ _id: 'IRC_Bridge_Last_Ping' }, { |
|
|
|
|
$set: { |
|
|
|
|
value: new Date(), |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}), 1000 * 10); |
|
|
|
|
|
|
|
|
|
class Bridge { |
|
|
|
|
constructor(config) { |
|
|
|
@ -27,7 +43,21 @@ class Bridge { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
init() { |
|
|
|
|
this.initTime = new Date(); |
|
|
|
|
removed = false; |
|
|
|
|
this.loggedInUsers = []; |
|
|
|
|
|
|
|
|
|
const lastPing = Settings.findOneById('IRC_Bridge_Last_Ping'); |
|
|
|
|
if (lastPing) { |
|
|
|
|
if (Math.abs(moment(lastPing.value).diff()) < 1000 * 30) { |
|
|
|
|
this.log('Not trying to connect.'); |
|
|
|
|
this.remove(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.log('Connecting.'); |
|
|
|
|
updateLastPing(); |
|
|
|
|
this.server.register(); |
|
|
|
|
|
|
|
|
|
this.server.on('registered', () => { |
|
|
|
@ -41,6 +71,13 @@ class Bridge { |
|
|
|
|
this.server.disconnect(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
remove() { |
|
|
|
|
this.log('Removing current connection.'); |
|
|
|
|
removed = true; |
|
|
|
|
this.server = null; |
|
|
|
|
this.removeLocalHandlers(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Log helper |
|
|
|
|
*/ |
|
|
|
@ -64,6 +101,19 @@ class Bridge { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async runQueue() { |
|
|
|
|
if (!this.server) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const lastResetTime = Settings.findOneById('IRC_Bridge_Reset_Time'); |
|
|
|
|
if (lastResetTime && lastResetTime.value > this.initTime) { |
|
|
|
|
this.stop(); |
|
|
|
|
this.remove(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateLastPing(); |
|
|
|
|
|
|
|
|
|
// If it is empty, skip and keep the queue going
|
|
|
|
|
if (this.queue.isEmpty()) { |
|
|
|
|
return setTimeout(this.runQueue.bind(this), this.queueTimeout); |
|
|
|
@ -75,21 +125,26 @@ class Bridge { |
|
|
|
|
this.logQueue(`Processing "${ item.command }" command from "${ item.from }"`); |
|
|
|
|
|
|
|
|
|
// Handle the command accordingly
|
|
|
|
|
switch (item.from) { |
|
|
|
|
case 'local': |
|
|
|
|
if (!localCommandHandlers[item.command]) { |
|
|
|
|
throw new Error(`Could not find handler for local:${ item.command }`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await localCommandHandlers[item.command].apply(this, item.parameters); |
|
|
|
|
break; |
|
|
|
|
case 'peer': |
|
|
|
|
if (!peerCommandHandlers[item.command]) { |
|
|
|
|
throw new Error(`Could not find handler for peer:${ item.command }`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await peerCommandHandlers[item.command].apply(this, item.parameters); |
|
|
|
|
break; |
|
|
|
|
try { |
|
|
|
|
// Handle the command accordingly
|
|
|
|
|
switch (item.from) { |
|
|
|
|
case 'local': |
|
|
|
|
if (!localCommandHandlers[item.command]) { |
|
|
|
|
throw new Error(`Could not find handler for local:${ item.command }`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await localCommandHandlers[item.command].apply(this, item.parameters); |
|
|
|
|
break; |
|
|
|
|
case 'peer': |
|
|
|
|
if (!peerCommandHandlers[item.command]) { |
|
|
|
|
throw new Error(`Could not find handler for peer:${ item.command }`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
await peerCommandHandlers[item.command].apply(this, item.parameters); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
this.logQueue(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Keep the queue going
|
|
|
|
@ -132,6 +187,17 @@ class Bridge { |
|
|
|
|
callbacks.add('afterLogoutCleanUp', this.onMessageReceived.bind(this, 'local', 'onLogout'), callbacks.priority.LOW, 'irc-on-logout'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
removeLocalHandlers() { |
|
|
|
|
callbacks.remove('afterValidateLogin', 'irc-on-login'); |
|
|
|
|
callbacks.remove('afterCreateUser', 'irc-on-create-user'); |
|
|
|
|
callbacks.remove('afterCreateChannel', 'irc-on-create-channel'); |
|
|
|
|
callbacks.remove('afterCreateRoom', 'irc-on-create-room'); |
|
|
|
|
callbacks.remove('afterJoinRoom', 'irc-on-join-room'); |
|
|
|
|
callbacks.remove('afterLeaveRoom', 'irc-on-leave-room'); |
|
|
|
|
callbacks.remove('afterSaveMessage', 'irc-on-save-message'); |
|
|
|
|
callbacks.remove('afterLogoutCleanUp', 'irc-on-logout'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sendCommand(command, parameters) { |
|
|
|
|
this.server.emit('onReceiveFromLocal', command, parameters); |
|
|
|
|
} |
|
|
|
|