diff --git a/src/plugin/action/index.sjs b/src/plugin/action/index.sjs index 5ca19e6..50eadad 100644 --- a/src/plugin/action/index.sjs +++ b/src/plugin/action/index.sjs @@ -47,28 +47,7 @@ module.exports = ActionPlugin = { rawf("NOTICE %s :%s", target, body); } - function join (channel) { - return new Promise(function (resolve, reject) { - var unsubscribe = function () { - logger.debug("Join response or timeout occured."); - client.off('join', onJoin); - }; - - var onJoin = function (join) { - if (join.nickname !== nickname() || join.channel !== channel) { - return; - } - - unsubscribe(); - logger.debug("Resolving with join body."); - resolve(join); - }; - - client.on('join', onJoin); - - rawf("JOIN :%s", channel); - }); - } + const join = require('./join')(client, rawf); function part (channel, reason) { diff --git a/src/plugin/action/join.sjs b/src/plugin/action/join.sjs index 3b8110d..c81d7c8 100644 --- a/src/plugin/action/join.sjs +++ b/src/plugin/action/join.sjs @@ -1,30 +1,87 @@ -/* -module.exports = function (client, action_module) { - const server = action_module.imports.server; +const format = require("util").format; +const Promise = require("bluebird"); - return function join (channel) { - const deferred = Q.defer(); - const rawf = action_module.exports.rawf; - const result = {}; +module.exports = function (client, rawf) { + return function (channel) { + return new Promise(function (resolve, reject) { + if (channel === undefined || channel === "") { + reject(new Error("No channel given to join action.")); + return; + } - const unsubscribe = function () { - messageHandler.off('join', onJoin); - }; + const response = { + names: [] + }; - const onJoin = function (join) { - if (join.nickname !== client.nickname() || join.channel !== channel) { - return; + // Only listen to events for the channel we care about. + const forChannel = function (handler) { + return function (message) { + if (message.channel === channel) { + handler(message); + } + } } - result = join; - deferred.resolve(result); - }; + // const onJoin = forChannel(function (join) { + // // Nothing? + // }); + + const onTopic = forChannel(function (topic) { + response.topic = topic.topic; + }); + + const onTopicWhoTime = forChannel(function (topicWhoTime) { + response.topicChange = { + who: topicWhoTime.who, + timestamp: topicWhoTime.timestamp + }; + }); + + const onNames = forChannel(function (names) { + response.names = response.names.concat(names); + }); + + const onNamesEnd = forChannel(function (endofnames) { + unsubscribe(); + Promise.resolve(response); + }); + + const onJoinError = forChannel(function (err) { + unsubscribe(); + Promise.reject(err); + }); + + const handlers = { + //join: onJoin, + rpl_topic: onTopic, + rpl_topicwhotime: onTopicWhoTime, + rpl_namreply: onNames, + rpl_endofnames: onNamesEnd, + err_nosuchchannel: onJoinError, + err_unavailresource: onJoinError, + err_channelisfull: onJoinError, + err_toomanychannels: onJoinError, + err_inviteonlychan: onJoinError, + err_bannedfromchan: onJoinError, + err_badchannelkey: onJoinError, + err_needreggednick: onJoinError, + err_operonly: onJoinError + }; + + client.on(handlers); - messageHandler.on('join', onJoin); + // Assume failure in an hour. + setTimeout(function () { + unsubscribe(); + client.error(format("Attempt to join %s failed.", channel)); + }, 3600 * 1000); - rawf("JOIN :%s", channel); + const unsubscribe = function () { + client.debug("PluginAction", format("Unsubscribing events for JOIN %s", channel)); + client.off(handlers); + }; - return deferred.promise; - } -}; -*/ \ No newline at end of file + rawf("JOIN :%s", channel); + }); + }; +}; \ No newline at end of file