diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..1ff2ca0 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,87 @@ +{ + // JSHint Default Configuration File (as on JSHint website) + // See http://jshint.com/docs/ for more details + + "maxerr" : 50, // {int} Maximum error before stopping + + // Enforcing + "bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.) + "camelcase" : false, // true: Identifiers must be in camelCase + "curly" : true, // true: Require {} for every new block or scope + "eqeqeq" : true, // true: Require triple equals (===) for comparison + "freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc. + "forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty() + "immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` + "indent" : 4, // {int} Number of spaces to use for indentation + "latedef" : false, // true: Require variables/functions to be defined before being used + "newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()` + "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` + "noempty" : true, // true: Prohibit use of empty blocks + "nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters. + "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) + "plusplus" : false, // true: Prohibit use of `++` & `--` + "quotmark" : false, // Quotation mark consistency: + // false : do nothing (default) + // true : ensure whatever is used is consistent + // "single" : require single quotes + // "double" : require double quotes + "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) + "unused" : true, // true: Require all defined variables be used + "strict" : true, // true: Requires all functions run in ES5 Strict Mode + "maxparams" : false, // {int} Max number of formal params allowed per function + "maxdepth" : false, // {int} Max depth of nested blocks (within functions) + "maxstatements" : false, // {int} Max number statements per function + "maxcomplexity" : false, // {int} Max cyclomatic complexity per function + "maxlen" : false, // {int} Max number of characters per line + + // Relaxing + "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) + "boss" : true, // true: Tolerate assignments where comparisons would be expected + "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. + "eqnull" : false, // true: Tolerate use of `== null` + "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) + "esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`) + "moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features) + // (ex: `for each`, multiple try/catch, function expression…) + "evil" : false, // true: Tolerate use of `eval` and `new Function()` + "expr" : true, // true: Tolerate `ExpressionStatement` as Programs + "funcscope" : false, // true: Tolerate defining variables inside control statements + "globalstrict" : true, // true: Allow global "use strict" (also enables 'strict') + "iterator" : false, // true: Tolerate using the `__iterator__` property + "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block + "laxbreak" : false, // true: Tolerate possibly unsafe line breakings + "laxcomma" : false, // true: Tolerate comma-first style coding + "loopfunc" : false, // true: Tolerate functions being defined in loops + "multistr" : false, // true: Tolerate multi-line strings + "noyield" : false, // true: Tolerate generator functions with no yield statement in them. + "notypeof" : false, // true: Tolerate invalid typeof operator values + "proto" : false, // true: Tolerate using the `__proto__` property + "scripturl" : false, // true: Tolerate script-targeted URLs + "shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` + "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation + "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` + "validthis" : false, // true: Tolerate using this in a non-constructor function + + // Environments + "browser" : false, // Web Browser (window, document, etc) + "browserify" : false, // Browserify (node.js code in the browser) + "couch" : false, // CouchDB + "devel" : true, // Development/debugging (alert, confirm, etc) + "dojo" : false, // Dojo Toolkit + "jasmine" : false, // Jasmine + "jquery" : false, // jQuery + "mocha" : false, // Mocha + "mootools" : false, // MooTools + "node" : true, // Node.js + "nonstandard" : false, // Widely adopted globals (escape, unescape, etc) + "prototypejs" : false, // Prototype and Scriptaculous + "qunit" : false, // QUnit + "rhino" : false, // Rhino + "shelljs" : false, // ShellJS + "worker" : false, // Web Workers + "wsh" : false, // Windows Scripting Host + "yui" : false, // Yahoo User Interface + + // Custom Globals + "globals" : {} // additional predefined global variables +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..0fed01d --- /dev/null +++ b/index.js @@ -0,0 +1,5 @@ +'use strict'; + +var Session = require('./lib/session'); + +module.exports = Session; diff --git a/lib/channel.js b/lib/channel.js new file mode 100644 index 0000000..820fa05 --- /dev/null +++ b/lib/channel.js @@ -0,0 +1,79 @@ +'use strict'; + +var _ = require('lodash'); + +var Event = require('./event'); + +var formatMessage = require('./util/format-message.js'); + +function Channel(session, name) { + this.session = session; + + var names = _.isArray(name) ? name : [name]; + + this.channelNames = names; + + this.handlers = []; + + _.bindAll(this, 'testMessage'); +} + +Channel.prototype = { + send: function() { + this.sendMessage.apply(this, arguments); + }, + + eachChannelName: function(cb) { + _.each(this.channelNames, cb, this); + }, + + sendMessage: function(msg) { + msg = formatMessage(msg); + + if (msg.channel) { + this.session.sendMessage(msg); + } else { + this.eachChannelName(function(channelName) { + var msgToSend = _.clone(msg); + + msgToSend.channel = channelName; + + this.session.sendMessage(msgToSend); + }); + } + }, + + on: function(selector, callback) { + if (callback) { + this.handlers.push(new Event(this, selector, callback)); + } else { + for (var key in selector) { + if (selector.hasOwnProperty(key)) { + this.on(key, selector[key]); + } + } + } + }, + + testMessage: function(message) { + for (var i = this.handlers.length - 1; i >= 0; i--) { + this.handlers[i].match(message); + } + }, + + setChannels: function() { + this.channels = {}; + + _.each(this.channelNames, function(channelName) { + var data = this.session.channelData(channelName); + this.channels[channelName] = data; + + this.session.onChannelMessage(channelName, this.testMessage); + }, this); + } +}; + +Object.defineProperties(Channel.prototype, { +}); + +module.exports = Channel; diff --git a/lib/event.js b/lib/event.js new file mode 100644 index 0000000..14ce86f --- /dev/null +++ b/lib/event.js @@ -0,0 +1,191 @@ +'use strict'; + +var Message = require('./message'); + +var regExRegEx = /^\/(.+)\/([gimy]*)$/; + +function Event(session, selector, callback) { + this.session = session; + this.selector = selector; + this.callback = callback; +} + +Event.prototype = { + get selector() { + return this._selector; + }, + + set selector(selector) { + var type; + + Object.defineProperty(this, '_selector', { + value: selector, + writeable: false, + configurable: true, + enumerable: false + }); + + var match; + + if (selector instanceof RegExp) { + type = 'regex'; + + Object.defineProperty(this, 'regex', { + value: selector, + writeable: false, + configurable: false, + enumerable: true + }); + } else { + match = selector.match(regExRegEx); + } + + if (match) { + type = 'regex'; + + Object.defineProperty(this, 'regex', { + value: new RegExp(match[1], match[2]), + writeable: false, + configurable: false, + enumerable: true + }); + } else if (selector[selector.length - 1] === '*') { + + if (selector[0] === '*') { + type = 'stringAnywhere'; + + Object.defineProperty(this, 'stringAnywhere', { + value: selector.slice(0, -1), + writeable: false, + configurable: false, + enumerable: true + }); + } else { + type = 'prefix'; + + Object.defineProperty(this, 'prefix', { + value: selector.slice(0, -1), + writeable: false, + configurable: false, + enumerable: true + }); + } + } else if (!type) { + type = 'text'; + } + + this.type = type; + }, + + get type() { + return this._type; + }, + + set type(type) { + Object.defineProperty(this, '_type', { + value: type, + writeable: false, + configurable: true, + enumerable: false + }); + + var matcher; + + switch (type) { + case 'text': + matcher = matchText; + break; + case 'prefix': + matcher = prefixMatch; + break; + case 'stringAnywhere': + matcher = stringAnyWhereMatch; + break; + case 'regex': + matcher = regexMatch; + break; + default: + matcher = function() {}; + } + + Object.defineProperty(this, 'match', { + value: matcher, + writeable: false, + configurable: true, + enumerable: true + }); + } +}; + +function matchText(message) { + /*jshint validthis:true */ + + if (!(message && message.text)) { + return; + } + + var matched = message.text === this.selector; + + if (matched) { + var msg = new Message(this.session, message); + this.callback(msg, this.selector); + } + + return matched; +} + +function prefixMatch(message) { + /*jshint validthis:true */ + + if (!(message && message.text)) { + return; + } + + var matched = message.text.indexOf(this.prefix) === 0; + + if (matched) { + var rest = message.text.slice(this.prefix.length); + + var msg = new Message(this.session, message); + this.callback(msg, this.prefix, rest); + } + + return matched; +} + +function stringAnyWhereMatch(message) { + /*jshint validthis:true */ + + if (!(message && message.text)) { + return; + } + + var matched = message.text.indexOf(this.stringAnywhere) >= 0; + + if (matched) { + var msg = new Message(this.session, message); + this.callback(msg, this.selector); + } + + return matched; +} + +function regexMatch(message) { + /*jshint validthis:true */ + + if (!(message && message.text)) { + return; + } + + var match = message.text.match(this.regex); + + if (match) { + var msg = new Message(this.session, message); + + this.callback(msg, match); + } + + return !!match; +} + +module.exports = Event; diff --git a/lib/message.js b/lib/message.js new file mode 100644 index 0000000..6b5f84c --- /dev/null +++ b/lib/message.js @@ -0,0 +1,61 @@ +'use strict'; + +var _ = require('lodash'); + +var formatMessage = require('./util/format-message.js'); + +function readOnlyEnumerable(obj, key, value) { + Object.defineProperty(obj, key, { + value: value, + writeable: false, + configurable: false, + enumerable: true + }); +} + +function Message(session, data) { + readOnlyEnumerable(this, 'data', data); + readOnlyEnumerable(this, 'session', session); +} + +Message.prototype = { + reply: function(str) { + var reply = formatMessage(str); + + reply.channel || (reply.channel = this.channelId); + + this.session.sendMessage(reply); + }, + + replyToSession: function(str) { + this.session.sendMessage(str); + }, + + get channelId() { + return this.data.channel; + }, + + get channelName() { + var id = this.channelId; + + var channel = _.find(this.rootSession.data.channels, function(channel) { + return channel.id === id; + }); + + return channel && channel.name; + }, + + get rootSession() { + if (this.session.testMessage) { + return this.session.session; + } else { + return this.session; + } + }, + + get text() { + return this.data.text; + } +}; + +module.exports = Message; diff --git a/lib/poll_channel.js b/lib/poll_channel.js new file mode 100644 index 0000000..18a435f --- /dev/null +++ b/lib/poll_channel.js @@ -0,0 +1,90 @@ +'use strict'; + +var _ = require('lodash'); +var request = require('request'); + +var Event = require('./event'); + +function buildURL(token, channel, oldest) { + var url = 'https://slack.com/api/channels.history?token='+token+'&channel='+channel; + + if (oldest) { + url += '&oldest='+oldest; + } + + return url; +} + +function now() { + return '' + (_.now()/1000); +} + +function Channel(token, channel) { + this.token = token; + this.channel = channel; + this.handlers = []; + this.channelTimes = {}; + + _.bindAll(this, 'handleMessageResponse', 'getMessages'); +} + +Channel.prototype.pollInterval = 2*1000; + +Channel.prototype.on = function(selector, callback) { + if (callback) { + this.handlers.push(new Event(selector, callback)); + } else { + for (var key in selector) { + if (selector.hasOwnProperty(key)) { + this.on(key, selector[key]); + } + } + } +}; + +Channel.prototype.testMessage = function(message) { + for (var i = this.handlers.length - 1; i >= 0; i--) { + this.handlers[i].match(message); + } +}; + +Channel.prototype.getMessages = function() { + this.channelTimes[this.channel] || (this.channelTimes[this.channel] = now()); + var url = buildURL(this.token, this.channel, this.channelTimes[this.channel]); + + request.get(url, this.handleMessageResponse); +}; + +Channel.prototype.handleMessageResponse = function(err, response, body) { + if (err) { + return; + } + + body = JSON.parse(body); + + if (body.messages.length) { + this.channelTimes[this.channel] = body.messages[0].ts; + + for (var i = body.messages.length - 1; i >= 0; i--) { + this.testMessage(body.messages[i]); + } + } + + this.queueRead(); +}; + +Channel.prototype.queueRead = function() { + return setTimeout(this.getMessages, this.pollInterval); +}; + +Channel.setup = function(token, channelId, events) { + var channel = new Channel(token, channelId); + + channel.on(events || {}); + + channel.getMessages(); + + return channel; +}; + +module.exports = Channel; diff --git a/lib/session.js b/lib/session.js new file mode 100644 index 0000000..dec610c --- /dev/null +++ b/lib/session.js @@ -0,0 +1,224 @@ +'use strict'; + +var __DEV__ = process.env.NODE_ENV === 'development'; + +var _ = require('lodash'); +var Slack = require('node-slackr'); +var request = require('request'); +var WebSocket = require('ws'); + +var Channel = require('./channel'); +var Event = require('./event'); + +var formatMessage = require('./util/format-message.js'); + +var sessions = {}; + +function log() { + if (__DEV__) { + console.log.apply(console.log, arguments); + } +} + +function Session(token, options) { + if (!options && !_.isString(token)) { + options = token; + token = options.token; + } + + options || (options = {}); + + if (sessions[token]) { + return sessions[token]; + } + + sessions[token] = this; + + var url = 'https://slack.com/api/rtm.start?token='+token; + + this.lastMessageId = 0; + + this.channels = []; + + this.messageHandlers = []; + + request.get(url, function(err, response, body) { + if (err) { + throw('error:', err); + } + + var data = JSON.parse(body); + + this.data = data; + }.bind(this)); + + this._channelMessagePings = {}; + + _.bindAll(this, 'gotMessage'); + + if (options.webhookClient) { + this.webhookClient = options.webhookClient; + } + + if (options.devChannel) { + this.devChannel = options.devChannel; + + if (__DEV__) { + return this.channel(options.devChannel); + } + } +} + +Session.prototype = { + channel: function(name) { + var channel = new Channel(this, name); + + this.channels.push(channel); + + return channel; + }, + + gotMessage: function(msg) { + var data = JSON.parse(msg); + + log('message recieved: ', msg); + + if (this['_handleType_' + data.type]) { + this['_handleType_' + data.type](data); + } + }, + + _handleType_message: function(data) { + if (this.devChannel) { + var channelId = this.channelData(this.devChannel).id; + + var isDevChannel = data.channel === channelId; + + if (__DEV__ && !isDevChannel) { + return; + } else if (!__DEV__ && isDevChannel) { + return; + } + } + + for (var i = this.messageHandlers.length - 1; i >= 0; i--) { + this.messageHandlers[i].match(data); + } + + _.each(this._channelMessagePings[data.channel], function(cb) { + cb(data); + }); + }, + + on: function(selector, callback) { + if (callback) { + this.messageHandlers.push(new Event(this, selector, callback)); + } else { + for (var key in selector) { + if (selector.hasOwnProperty(key)) { + this.on(key, selector[key]); + } + } + } + }, + + onChannelMessage: function(name, cb) { + var data = this.channelData(name); + + if (!data) { + console.error('Need to re-get channel list'); + return false; + } + + this._channelMessagePings[data.id] || (this._channelMessagePings[data.id] = []); + + this._channelMessagePings[data.id].push(cb); + }, + + channelData: function(name) { + var data; + + if (!this.data) { + return; + } + + if (name[0] === '#') { + name = name.slice(1); + + data = _.find(this.data.channels, function(c) { + return c.name === name; + }); + } else { + data = _.find(this.data.channels, function(c) { + return c.id === name; + }); + } + + return data; + }, + + sendMessage: function(msg) { + msg = formatMessage(msg); + + if (msg.attachments && this.webhookClient) { + this.webhookClient.notify(msg); + + log('message sent via client ', JSON.stringify(msg)); + } else { + this.send(msg); + + log('message sent via websocket ', JSON.stringify(msg)); + } + }, + + send: function(msg) { + this.lastMessageId += 1; + msg.id = this.lastMessageId; + var str = JSON.stringify(msg); + this.ws.send(str); + }, + + get name() { + if (this.data) { + return this.data.self.name; + } + }, + + get id() { + if (this.data) { + return this.data.self.id; + } + }, + + get data() { + return this._data; + }, + + set data(data) { + this._data = data; + + _(this.channels).invoke('setChannels'); + + this.ws = new WebSocket(data.url); + + this.ws.on('message', this.gotMessage); + }, + + get webhookClient() { + return this._webhookClient; + }, + + set webhookClient(client) { + if (client instanceof Slack) { + this._webhookClient = client; + } else { + var options = _.clone(client); + delete options.team; + delete options.token; + + this._webhookClient = new Slack(client.team, client.token, options); + } + } +}; + +module.exports = Session; diff --git a/lib/util/format-message.js b/lib/util/format-message.js new file mode 100644 index 0000000..3e5a5e1 --- /dev/null +++ b/lib/util/format-message.js @@ -0,0 +1,19 @@ +'use strict'; + +var _ = require('lodash'); + +module.exports = function formatMessage(msg) { + var newMsg; + + if (_.isString(msg)) { + newMsg = { + text: msg + }; + } else { + newMsg = msg; + } + + newMsg.type || (newMsg.type = 'message'); + + return newMsg; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..85d179f --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "slackr-bot", + "version": "0.0.1", + "description": "API for using the slack RTM interface", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "slack", + "slack bot" + ], + "repository": { + "type": "git", + "url": "git://github.com/tal/slackr-bot.git" + }, + "author": "Tal Atlas (http://tal.by/)", + "license": "MIT", + "dependencies": { + "node-slackr": "0.0.4", + "lodash": "^2.4.1", + "ws": "^0.6.3" + } +}