Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
linter formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Brown committed Feb 14, 2018
1 parent 35f7811 commit 3b8bc38
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 76 deletions.
83 changes: 34 additions & 49 deletions lib/JabberBot.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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];
Expand All @@ -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 = {
Expand All @@ -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);
});
Expand All @@ -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;
Expand All @@ -110,37 +108,29 @@ 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;
} else {
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;
}
}
});

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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}

Expand All @@ -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') {
Expand All @@ -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++) {
Expand Down
47 changes: 20 additions & 27 deletions lib/JabberGroupManager.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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);
}
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -193,37 +188,35 @@ 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,
};
}
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;
module.exports = JabberGroupManager;

0 comments on commit 3b8bc38

Please sign in to comment.