diff --git a/lib/JabberBot.js b/lib/JabberBot.js index 25c0c74c0..b7b56a901 100644 --- a/lib/JabberBot.js +++ b/lib/JabberBot.js @@ -1,6 +1,6 @@ var Botkit = require(__dirname + '/CoreBot.js'); const Stanza = require('node-xmpp-client').Stanza; -const GroupManager = require('./JabberGroupManager.js') +const GroupManager = require('./JabberGroupManager.js'); function JabberBot(configuration) { // Create a core botkit bot @@ -9,15 +9,15 @@ function JabberBot(configuration) { function toUTCDateTimeString(date) { var yyyy = date.getUTCFullYear(); - var mm = date.getUTCMonth() < 9 ? "0" + (date.getUTCMonth() + 1) : (date.getUTCMonth() + 1); // getMonth() is zero-based - var dd = date.getUTCDate() < 10 ? "0" + date.getUTCDate() : date.getUTCDate(); - var hh = date.getUTCHours() < 10 ? "0" + date.getUTCHours() : date.getUTCHours(); - var min = date.getUTCMinutes() < 10 ? "0" + date.getUTCMinutes() : date.getUTCMinutes(); - var ss = date.getUTCSeconds() < 10 ? "0" + date.getUTCSeconds() : date.getUTCSeconds(); - return "".concat(yyyy).concat('-').concat(mm).concat('-').concat(dd).concat('T').concat(hh).concat(':').concat(min).concat(':').concat(ss); + var mm = date.getUTCMonth() < 9 ? '0' + (date.getUTCMonth() + 1) : (date.getUTCMonth() + 1); // getMonth() is zero-based + var dd = date.getUTCDate() < 10 ? '0' + date.getUTCDate() : date.getUTCDate(); + var hh = date.getUTCHours() < 10 ? '0' + date.getUTCHours() : date.getUTCHours(); + var min = date.getUTCMinutes() < 10 ? '0' + date.getUTCMinutes() : date.getUTCMinutes(); + var ss = date.getUTCSeconds() < 10 ? '0' + date.getUTCSeconds() : date.getUTCSeconds(); + return ''.concat(yyyy).concat('-').concat(mm).concat('-').concat(dd).concat('T').concat(hh).concat(':').concat(min).concat(':').concat(ss); }; - controller.middleware.format.use(function (bot, message, platform_message, next) { + controller.middleware.format.use(function(bot, message, platform_message, next) { // clone the incoming message for (var k in message) { platform_message[k] = message[k]; @@ -27,7 +27,7 @@ function JabberBot(configuration) { // customize the bot definition, which will be used when new connections // spawn! - controller.defineBot(function (botkit, config) { + controller.defineBot(function(botkit, config) { var xmpp = require('simple-xmpp'); var bot = { @@ -46,35 +46,35 @@ function JabberBot(configuration) { xmpp.conn.send(roster_stanza); } - xmpp.on('online', function (data) { + xmpp.on('online', function(data) { console.log(toUTCDateTimeString(new Date()) + ':Connected with JID: ' + data.jid.user); console.log('Yes, I\'m connected!'); request_roster(); // send whitespace to keep the connection alive // and prevent timeouts - setInterval(function () { + setInterval(function() { xmpp.conn.send(' '); }, 1800000); }); - xmpp.on('close', function () { + xmpp.on('close', function() { console.log(toUTCDateTimeString(new Date()) + ':connection has been closed!'); process.exit(); }); - xmpp.on('error', function (err) { - console.log(toUTCDateTimeString(new Date()) + ":" + err); + xmpp.on('error', function(err) { + console.log(toUTCDateTimeString(new Date()) + ':' + err); process.exit(); }); - xmpp.on('subscribe', function (from) { + xmpp.on('subscribe', function(from) { xmpp.acceptSubscription(from); console.log(toUTCDateTimeString(new Date()) + ':accept subscribe from:' + from); controller.trigger('subscribe', [bot, from]); }); - xmpp.on('unsubscribe', function (from) { + xmpp.on('unsubscribe', function(from) { console.log(toUTCDateTimeString(new Date()) + ':accept unsubscribe from:' + from); xmpp.acceptUnsubscription(from); }); @@ -83,11 +83,9 @@ function JabberBot(configuration) { return jid === bot.client_jid; } - function IsBotMentioned(message) - { + function IsBotMentioned(message) { let mention_jids = extractMentionJids(message); - if (mention_jids.find(findBotJid)) - { + if (mention_jids.find(findBotJid)) { return true; } return false; @@ -110,9 +108,8 @@ function JabberBot(configuration) { return mention_jids; } - controller.on('message_received', function (bot, message) { - if (message.group == false) - { + controller.on('message_received', function(bot, message) { + if (message.group == false) { if (message.user === bot.client_jid) { controller.trigger('self_message', [bot, message]); return false; @@ -120,17 +117,11 @@ function JabberBot(configuration) { controller.trigger('direct_message', [bot, message]); return false; } - } - else - { - if (IsBotMentioned(message)) - { - if (bot.client_jid == message.from_jid) - { + } else { + if (IsBotMentioned(message)) { + if (bot.client_jid == message.from_jid) { controller.trigger('self_message', [bot, message]); - } - else - { + } else { controller.trigger('direct_mention', [bot, message]); } return false; @@ -138,9 +129,8 @@ function JabberBot(configuration) { } }); - xmpp.on('stanza', function (stanza) { - if (stanza.is('message')) - { + xmpp.on('stanza', function(stanza) { + if (stanza.is('message')) { if (stanza.attrs.type == 'chat') { var body = stanza.getChild('body'); if (body) { @@ -156,9 +146,7 @@ function JabberBot(configuration) { xmpp_message.channel = 'chat', controller.ingest(bot, xmpp_message, null); } - } - else if (stanza.attrs.type == 'groupchat') - { + } else if (stanza.attrs.type == 'groupchat') { var body = stanza.getChild('body'); if (body) { let message = body.getText(); @@ -188,21 +176,18 @@ function JabberBot(configuration) { } }); - bot.startConversation = function (message, cb) { + bot.startConversation = function(message, cb) { botkit.startConversation(this, message, cb); }; - bot.createConversation = function (message, cb) { + bot.createConversation = function(message, cb) { botkit.createConversation(this, message, cb); }; - bot.send = function (message, cb) { - if (message.stanza) - { + bot.send = function(message, cb) { + if (message.stanza) { xmpp.conn.send(message.stanza); - } - else - { + } else { xmpp.send(message.user, message.text, message.group); } @@ -211,7 +196,7 @@ function JabberBot(configuration) { } }; - bot.reply = function (src, resp, cb) { + bot.reply = function(src, resp, cb) { var msg = {}; if (typeof (resp) == 'string') { @@ -226,7 +211,7 @@ function JabberBot(configuration) { bot.say(msg, cb); }; - bot.findConversation = function (message, cb) { + bot.findConversation = function(message, cb) { botkit.debug('CUSTOM FIND CONVO', message.user, message.channel); for (var t = 0; t < botkit.tasks.length; t++) { for (var c = 0; c < botkit.tasks[t].convos.length; c++) { diff --git a/lib/JabberGroupManager.js b/lib/JabberGroupManager.js index cbad23647..1544ec1c6 100644 --- a/lib/JabberGroupManager.js +++ b/lib/JabberGroupManager.js @@ -1,4 +1,4 @@ -const caps = require('node-xmpp-caps'); +const caps = require('node-xmpp-caps'); const Stanza = require('node-xmpp-client').Stanza; const MD5 = require('md5'); @@ -14,20 +14,18 @@ function JabberGroupManager(config, xmpp, bot, controller) { const bot_caps_node_addr = 'http://protocols.cisco.com/jabber-bot'; var bot_caps = createCapsNode(bot_caps_node_addr); - xmpp.on('online', function (data) { + xmpp.on('online', function(data) { publishCapabilities(); joinPresetRooms(); }); - xmpp.on('stanza', function (stanza) { + xmpp.on('stanza', function(stanza) { if (stanza.is('iq')) { handleCapabilityIq(stanza); handleRoomDiscoQueryIq(stanza); - } - else if (stanza.is('message')) { + } else if (stanza.is('message')) { handleInviteMessage(stanza); - } - else if (stanza.is('presence')) { + } else if (stanza.is('presence')) { handleMembershipPresence(stanza); } }); @@ -60,11 +58,11 @@ function JabberGroupManager(config, xmpp, bot, controller) { } } - controller.storage.teams.all(function (err, rooms) { + controller.storage.teams.all(function(err, rooms) { for (let i = 0; i < rooms.length; i++) { let room = rooms[i]; if (room.type === 'joined_persist_rooms') { - let room_id = room.room_id + "/" + bot.client_jid; + let room_id = room.room_id + '/' + bot.client_jid; let password = room.password; if (!joinedRoomsId.has(getBareRoomId(room_id))) { joinedRoomsPasswordMap.set(getBareRoomId(room_id), password); @@ -117,15 +115,13 @@ function JabberGroupManager(config, xmpp, bot, controller) { return; let features = query.getChildren('feature'); - for (let i = 0; i < features.length; i++) - { + for (let i = 0; i < features.length; i++) { let feature = features[i]; if (feature.attrs.var === 'persistent' || - feature.attrs.var === 'muc_persistent') - { + feature.attrs.var === 'muc_persistent') { joinedPersistRoomsId.add(getBareRoomId(room_id)); let password = joinedRoomsPasswordMap.get(getBareRoomId(room_id)); - saveJoinPersistRooms(getBareRoomId(room_id), password) + saveJoinPersistRooms(getBareRoomId(room_id), password); return; } } @@ -144,7 +140,7 @@ function JabberGroupManager(config, xmpp, bot, controller) { let password_node = muc_message.getChild('password'); if (password_node) password = password_node.getText(); - let room_id = stanza.attrs.from + "/" + bot.client_jid; + let room_id = stanza.attrs.from + '/' + bot.client_jid; if (!joinedRoomsId.has(getBareRoomId(room_id))) { joinedRoomsPasswordMap.set(getBareRoomId(room_id), password); @@ -179,8 +175,7 @@ function JabberGroupManager(config, xmpp, bot, controller) { saveLeavePersistRooms(getBareRoomId(room_id)); } } - } - else if (item.attrs.role === 'participant') { + } else if (item.attrs.role === 'participant') { joinedRoomsId.add(getBareRoomId(room_id)); sendRoomDiscoQueryIq(room_id, jid); @@ -193,15 +188,14 @@ function JabberGroupManager(config, xmpp, bot, controller) { let disco_id = 'room_disco1'; let node = to.split('@')[1]; let iq_stanza = new Stanza('iq', { 'id': disco_id, 'type': 'get', 'to': to, 'from': from }); - iq_stanza.c('query', { xmlns: 'http://jabber.org/protocol/disco#info', 'node' : node}); - xmpp.conn.send(iq_stanza); + iq_stanza.c('query', { xmlns: 'http://jabber.org/protocol/disco#info', 'node': node}); + xmpp.conn.send(iq_stanza); }; function saveJoinPersistRooms(room_id, room_password) { let id = MD5(room_id); - controller.storage.teams.get(id, function (err, room) { - if (!room) - { + controller.storage.teams.get(id, function(err, room) { + if (!room) { room = { id: id, }; @@ -209,21 +203,20 @@ function JabberGroupManager(config, xmpp, bot, controller) { room.room_id = room_id; room.password = room_password; room.type = 'joined_persist_rooms'; - controller.storage.teams.save(room, function (err, room) { + controller.storage.teams.save(room, function(err, room) { }); }); } function saveLeavePersistRooms(room_id) { let id = MD5(room_id); - controller.storage.teams.delete(id, function (err, room) { + controller.storage.teams.delete(id, function(err, room) { }); } - function getBareRoomId(room_id) - { + function getBareRoomId(room_id) { return room_id.split('/')[0]; } } -module.exports = JabberGroupManager; \ No newline at end of file +module.exports = JabberGroupManager;