From 86fdbaf45c1223e87f249d6c119f7aa8bb5f012a Mon Sep 17 00:00:00 2001 From: David A Roberts Date: Fri, 4 Dec 2015 18:57:45 +1000 Subject: [PATCH] Initial commit --- .gitignore | 1 + package.json | 21 +++++++++++++++++ src/matrix.coffee | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 src/matrix.coffee diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/package.json b/package.json new file mode 100644 index 0000000..4d67ba1 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "hubot-matrix", + "version": "1.0.0", + "description": "Matrix adapter for hubot", + "main": "src/matrix.coffee", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "David A Roberts", + "license": "MIT", + "dependencies": { + "parent-require": "^1.0.0", + "matrix-js-sdk": ">=0.3.0" + }, + "peerDependencies": { + "hubot": ">=2.0" + }, + "devDependencies": { + "coffee-script": ">=1.2.0" + } +} diff --git a/src/matrix.coffee b/src/matrix.coffee new file mode 100644 index 0000000..3b0b380 --- /dev/null +++ b/src/matrix.coffee @@ -0,0 +1,59 @@ +try + {Robot,Adapter,TextMessage,User} = require 'hubot' +catch + prequire = require('parent-require') + {Robot,Adapter,TextMessage,User} = prequire 'hubot' + +sdk = require 'matrix-js-sdk' + +class Matrix extends Adapter + constructor: -> + super + @robot.logger.info "Constructor" + + send: (envelope, strings...) -> + for str in strings + @robot.logger.info "Sending to #{envelope.room}: #{str}" + @client.sendTextMessage envelope.room, str + + reply: (envelope, strings...) -> + for str in strings + @send envelope, "#{envelope.user.name}: #{str}" + + run: -> + @robot.logger.info "Run #{@robot.name}" + client = sdk.createClient('https://matrix.org') + client.login 'm.login.password', { + user: @robot.name + password: process.env.HUBOT_MATRIX_PASSWORD + }, (err, data) => + if err + @robot.logger.error err + return + @user_id = data.user_id + @access_token = data.access_token + @robot.logger.info "Logged in #{@user_id}" + @client = sdk.createClient + baseUrl: 'https://matrix.org' + accessToken: @access_token + userId: @user_id + @client.on 'syncComplete', => + @robot.logger.info "Synced #{@client.getRooms().length} rooms" + @emit 'connected' + @client.on 'Room.timeline', (event, room, toStartOfTimeline) => + if event.getType() == 'm.room.message' and toStartOfTimeline == false + message = event.getContent().body + name = event.getSender() + user = @robot.brain.userForId name + user.room = room.roomId + if user.name != @user_id + @robot.logger.info "Received message: '#{message}' in room: #{user.room}, from: #{user.name}." + @receive new TextMessage user, message + @client.on 'RoomMember.membership', (event, member) => + if member.membership == 'invite' and member.userId == @user_id + @client.joinRoom(member.roomId).done => + @robot.logger.info "Auto-joined #{member.roomId}" + @client.startClient 0 + +exports.use = (robot) -> + new Matrix robot