All files / lib JabberBot.js

2.86% Statements 4/140
0% Branches 0/55
0% Functions 0/22
2.86% Lines 4/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 2471x 1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     1x  
var Botkit = require(__dirname + '/CoreBot.js');
const Stanza = require('node-xmpp-client').Stanza;
const GroupManager = require('./JabberGroupManager.js');
 
function JabberBot(configuration) {
    // Create a core botkit bot
    var controller = Botkit(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);
    };
 
    controller.middleware.format.use(function(bot, message, platform_message, next) {
        // clone the incoming message
        for (var k in message) {
            platform_message[k] = message[k];
        }
        next();
    });
 
    // customize the bot definition, which will be used when new connections
    // spawn!
    controller.defineBot(function(botkit, config) {
        var xmpp = require('simple-xmpp');
 
        var bot = {
            type: 'xmpp',
            botkit: botkit,
            config: config || {},
            client_jid: config.client.jid,
            utterances: botkit.utterances,
        };
 
        GroupManager(config, xmpp, bot, controller);
 
        function request_roster() {
            let roster_stanza = new Stanza('iq', { 'from': config.client, 'type': 'get'});
            roster_stanza.c('query', { xmlns: 'jabber:iq:roster'});
            xmpp.conn.send(roster_stanza);
        }
 
        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() {
                xmpp.conn.send(' ');
            }, 1800000);
        });
 
        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);
            process.exit();
        });
 
        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) {
            console.log(toUTCDateTimeString(new Date()) + ':accept unsubscribe from:' + from);
            xmpp.acceptUnsubscription(from);
        });
 
        function findBotJid(jid) {
            return jid === bot.client_jid;
        }
 
        function matchBotJid(jid_left, jid_right) {
            return jid_left.toLowerCase() === jid_right.toLowerCase();
        }
 
        function IsBotMentioned(message) {
            let mention_jids = extractMentionJids(message);
            if (mention_jids.find(findBotJid)) {
                return true;
            }
            return false;
        }
 
        function extractMentionJids(message) {
            let direct_mention_reg = /href="xmpp:\s?(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+)\s?"/ig;
            let email_reg = /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+/i;
            let match = message.stanza.toString().match(direct_mention_reg);
            let mention_jids = [];
            if (match) {
                for (let i = 0; i < match.length; i++) {
                    let jid_match = match[i].match(email_reg);
                    if (jid_match) {
                        let jid = jid_match[0];
                        mention_jids.push(jid);
                    }
                }
            }
            return mention_jids;
        }
 
        controller.on('message_received', function(bot, message) {
            if (message.group == false) {
                if (matchBotJid(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 (matchBotJid(bot.client_jid, message.from_jid)) {
                        controller.trigger('self_message', [bot, message]);
                    } else {
                        controller.trigger('direct_mention', [bot, message]);
                    }
                    return false;
                }
            }
        });
 
        xmpp.on('stanza', function(stanza) {
            if (stanza.is('message')) {
                if (stanza.attrs.type == 'chat') {
                    var body = stanza.getChild('body');
                    if (body) {
                        var message = body.getText();
                        var from = stanza.attrs.from;
                        var id = from.split('/')[0];
 
                        var xmpp_message = {};
                        xmpp_message.user = from;
                        xmpp_message.text = message;
                        xmpp_message.group = false;
                        xmpp_message.stanza = stanza;
                        xmpp_message.channel = 'chat',
                        controller.ingest(bot, xmpp_message, null);
                    }
                } else if (stanza.attrs.type == 'groupchat') {
                    var body = stanza.getChild('body');
                    if (body) {
                        let message = body.getText();
                        let from = stanza.attrs.from;
                        let from_split = from.split('/');
                        let conference = from_split[0];
                        let from_jid = null;
                        if (from_split.length > 1)
                            from_jid = from_split[1];
                        if (!from_jid)
                            return;
 
                        let history_reg = /xmlns="http:\/\/www.jabber.com\/protocol\/muc#history"/i;
                        if (history_reg.test(stanza.toString()))
                            return false;
 
                        var xmpp_message = {};
                        xmpp_message.user = conference;
                        xmpp_message.text = message;
                        xmpp_message.group = true;
                        xmpp_message.channel = 'groupchat';
                        xmpp_message.from_jid = from_jid;
                        xmpp_message.stanza = stanza;
                        controller.ingest(bot, xmpp_message, null);
                    }
                }
            }
        });
 
        bot.startConversation = function(message, cb) {
            botkit.startConversation(this, message, cb);
        };
 
        bot.createConversation = function(message, cb) {
            botkit.createConversation(this, message, cb);
        };
 
        bot.send = function(message, cb) {
            if (message.stanza) {
                xmpp.conn.send(message.stanza);
            } else {
                xmpp.send(message.user, message.text, message.group);
            }
 
            if (cb) {
                cb();
            }
        };
 
        bot.reply = function(src, resp, cb) {
            var msg = {};
 
            if (typeof (resp) == 'string') {
                msg.text = resp;
            } else {
                msg = resp;
            }
            msg.user = src.user;
            msg.channel = src.channel;
            msg.group = src.group;
 
            bot.say(msg, 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++) {
                    if (
                        botkit.tasks[t].convos[c].isActive() &&
                        botkit.tasks[t].convos[c].source_message.user == message.user &&
                        botkit.tasks[t].convos[c].source_message.channel == message.channel
                    ) {
                        botkit.debug('FOUND EXISTING CONVO!');
                        cb(botkit.tasks[t].convos[c]);
                        return;
                    }
                }
            }
 
            cb();
        };
 
        xmpp.connect(config.client);
        return bot;
    });
 
    controller.startTicking();
 
    return controller;
}
 
module.exports = JabberBot;