matrix_dbus 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edcb4137d5d3a6ad01adf1773edfc059a0997c88
4
+ data.tar.gz: 4bba507c4d362d59504101b19f2ec1f8f5e0c57a
5
+ SHA512:
6
+ metadata.gz: fbffb6d3b7fb577e4e06543a99ed2741d02cb67adbe889a1d8cf0ecb9c38b405b0cf37046ec0338f28f2d6de8cf76dae0a92b30896a7333656786633d829bf68
7
+ data.tar.gz: ba3b91e64f7f438f7c1a65111ac7c18c252b4aca287e7ad328b13e395493a8a3eb2b905d2dd6c8b5126aca0dbff613d6d17416c276aad8c372853c346b9f3757
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in matrix_dbus.gemspec
6
+ gemspec
data/README.en.md ADDED
@@ -0,0 +1,33 @@
1
+ # matrix_dbus ([中文](/README.md))
2
+
3
+ The bridge between [matrix](https://matrix.org) and [QQ](https://im.qq.com).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'matrix_dbus'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install matrix_dbus
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/71e6fd52/matrix_dbus).
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # matrix_dbus ([English](/README.en.md))
2
+
3
+ [matrix](https://matrix.org) 和 [QQ](https://im.qq.com) 的桥。
4
+
5
+ ## 安装
6
+
7
+ 在你的应用的 `Gemfile` 加入这行:
8
+
9
+ ```ruby
10
+ gem 'matrix_dbus'
11
+ ```
12
+
13
+ 然后运行:
14
+
15
+ $ bundle
16
+
17
+ 或者你自己安装
18
+
19
+ $ gem install matrix_dbus
20
+
21
+ ## 使用
22
+
23
+ TODO: 写使用说明
24
+
25
+ ## 开发
26
+
27
+ 检出这个仓库后,运行 `bin/setup` 来安装依赖关系。然后,运行 `rake spec` 来运行测试。 您还可以运行 `bin/console` 启动交互式提示符(`irb`),让您进行实验。
28
+
29
+ 要将此 gem 安装到本机上,请运行 `bundle exec rake install`。 要释出新版本,请在 `version.rb` 中更新版本号,然后运行 `bundle exec rake release`,该版本将为该版本创建一个 git 标签,并推送 git 提交和标签,然后将 `.gem` 文件提交到 [rubygems.org](https://rubygems.org)。
30
+
31
+ ## 贡献
32
+
33
+ 欢迎来 [GitHub](https://github.com/71e6fd52/matrix_dbus) 上发起 issues 和 pull requests
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "matrix_dbus"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/matrix-dbus ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'matrix_dbus'
4
+ Thread.abort_on_exception = true
5
+
6
+ def send(obj, name, json)
7
+ obj.method(name).call json.to_json unless json.empty?
8
+ end
9
+
10
+ def matrix2dbus(c, obj)
11
+ j = c.json
12
+ obj.all j.to_json
13
+
14
+ %i[account_data to_device presence].each do |name|
15
+ send obj, name, j[name.to_s]['events']
16
+ end
17
+
18
+ send obj, :rooms, j['rooms']
19
+ %i[leave join invite].each do |name|
20
+ send obj, name, j['rooms'][name.to_s]
21
+ end
22
+
23
+ send obj, :device_lists, j['device_lists']
24
+ %i[changed left].each do |name|
25
+ send obj, name, j['device_lists'][name.to_s]
26
+ end
27
+ end
28
+
29
+ bus = DBus::SessionBus.instance
30
+ service = bus.request_service 'org.dastudio.matrix'
31
+ obj = MatrixDBus::Matrix2DBus.new '/org/dastudio/matrix'
32
+ service.export obj
33
+
34
+ obj.matrix.bind ->(c) { matrix2dbus c, obj }
35
+ obj.run
36
+
37
+ main = DBus::Main.new
38
+ main << bus
39
+ main.run
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+ require 'base64'
3
+ require 'rest-client'
4
+ require 'dbus'
5
+
6
+ require 'matrix_dbus/version'
7
+ require 'matrix_dbus/network'
8
+ require 'matrix_dbus/dbus'
9
+ require 'matrix_dbus/matrix'
@@ -0,0 +1,52 @@
1
+ #
2
+ module MatrixAPI
3
+ def get(url)
4
+ uri = @host + '/_matrix/client/r0' + url
5
+ puts 'GET URL:', uri if $VERBOSE
6
+ JSON.parse RestClient.get(uri).body
7
+ rescue RestClient::Exceptions::OpenTimeout
8
+ retry
9
+ rescue RestClient::BadGateway
10
+ retry
11
+ end
12
+
13
+ def post(url, body)
14
+ uri = @host + '/_matrix/client/r0' + url
15
+ puts 'POST URL:', uri if $VERBOSE
16
+ JSON.parse RestClient.post(
17
+ uri,
18
+ body,
19
+ content_type: :json,
20
+ accept: :json
21
+ ).body
22
+ rescue RestClient::Exceptions::OpenTimeout
23
+ retry
24
+ rescue RestClient::BadGateway
25
+ retry
26
+ end
27
+
28
+ def put(url, body)
29
+ uri = @host + '/_matrix/client/r0' + url
30
+ puts 'PUT URL:', uri if $VERBOSE
31
+ JSON.parse RestClient.put(
32
+ uri,
33
+ body.to_json,
34
+ content_type: :json,
35
+ accept: :json
36
+ ).body
37
+ rescue RestClient::Exceptions::OpenTimeout
38
+ retry
39
+ rescue RestClient::BadGateway
40
+ retry
41
+ end
42
+
43
+ def upload_file(file, token)
44
+ uri = @host + '/_matrix/media/r0/upload?access_token=' + token
45
+ puts 'POST URL:', uri if $VERBOSE
46
+ JSON.parse(RestClient.post(uri, file).body)['content_uri']
47
+ rescue RestClient::Exceptions::OpenTimeout
48
+ retry
49
+ rescue RestClient::BadGateway
50
+ retry
51
+ end
52
+ end
@@ -0,0 +1,63 @@
1
+ module MatrixDBus
2
+ # DBus
3
+ class Matrix2DBus < DBus::Object
4
+ attr_reader :matrix
5
+
6
+ def initialize(*args)
7
+ @matrix = MatrixDBus::Matrix.new
8
+ super
9
+ end
10
+
11
+ def run
12
+ Thread.new { @matrix.run }
13
+ end
14
+
15
+ dbus_interface 'org.dastudio.matrix' do
16
+ %i[post put].each do |way|
17
+ dbus_method way, 'in url:s, in args:s' do |url, args|
18
+ args = JSON.parse args
19
+ url = URI(url)
20
+ url.query = URI.encode_www_form access_token: @matrix.access_token
21
+ begin
22
+ @matrix.network.method(way).call url.to_s, args
23
+ rescue RestClient::Exception => e
24
+ puts e.response.body
25
+ end
26
+ end
27
+ end
28
+
29
+ %i[get delete].each do |way|
30
+ dbus_method way, 'in url:s, in args:s' do |url, args|
31
+ args = JSON.parse args
32
+ args[:access_token] = @matrix.access_token
33
+ begin
34
+ @matrix.network.method(way).call url, args
35
+ rescue RestClient::Exception => e
36
+ puts e.response.body
37
+ end
38
+ end
39
+ end
40
+
41
+ dbus_method :post_raw, 'in url:s, in body:s' do |url, body|
42
+ body = Base64.decode64 body
43
+ @matrix.network.post_raw url, body
44
+ end
45
+
46
+ %i[
47
+ all
48
+ account_data
49
+ to_device
50
+ presence
51
+ rooms
52
+ leave
53
+ join
54
+ invite
55
+ device_lists
56
+ changed
57
+ left
58
+ ].each do |name|
59
+ dbus_signal name, 'json:s'
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,113 @@
1
+ module MatrixDBus
2
+ #
3
+ class Matrix
4
+ attr_reader :json, :access_token, :network
5
+
6
+ def initialize(username: '', password: '', host: 'https://matrix.org:8448')
7
+ @username = username
8
+ @password = password
9
+ @run = true
10
+ @event_method = []
11
+ @config = File.join(ENV['HOME'], '.config', 'matrix-qq')
12
+ @network = Network.new(host + '/_matrix/client/r0', method(:error))
13
+ load_token
14
+ load_batch
15
+ end
16
+
17
+ def bind(func)
18
+ @event_method << func
19
+ end
20
+
21
+ def save_token
22
+ unless File.directory? @config
23
+ File.delete @config if File.exist? @config
24
+ Dir.mkdir @config
25
+ end
26
+ File.open File.join(@config, 'token'), 'w', 0o600 do |f|
27
+ f.puts @access_token
28
+ end
29
+ end
30
+
31
+ def load_token
32
+ login unless File.exist? File.join @config, 'token'
33
+ login unless File.size? File.join @config, 'token'
34
+ File.open File.join(@config, 'token'), 'r', 0o600 do |f|
35
+ @access_token = f.gets.chomp
36
+ end
37
+ end
38
+
39
+ def save_batch
40
+ puts @next_batch if $DEBUG
41
+ File.open File.join(@config, 'batch'), 'w' do |f|
42
+ f.puts @next_batch
43
+ end
44
+ end
45
+
46
+ def load_batch
47
+ sync unless File.exist? File.join(@config, 'batch')
48
+ File.open File.join(@config, 'batch') do |f|
49
+ @next_batch = f.gets.chomp
50
+ end
51
+ end
52
+
53
+ def check_login
54
+ json = @network.get '/login'
55
+ raise "can't login" unless json['flows'].find do |f|
56
+ f['type'] == 'm.login.password'
57
+ end
58
+ end
59
+
60
+ def login
61
+ check_login
62
+ raise 'Unknow username' if @username == ''
63
+ raise 'Unknow password' if @password == ''
64
+ json = @network.post \
65
+ '/login',
66
+ type: 'm.login.password',
67
+ user: @username,
68
+ password: @password
69
+ @access_token = json['access_token']
70
+ save_token
71
+ end
72
+
73
+ def init_batch
74
+ json = get "/sync?access_token=#{@access_token}"
75
+ @next_batch = json['next_batch']
76
+ save_batch
77
+ end
78
+
79
+ def sync
80
+ init_batch if @next_batch.nil?
81
+ @json = @network.get \
82
+ '/sync',
83
+ since: @next_batch,
84
+ timeout: 10_000,
85
+ access_token: @access_token
86
+ @next_batch = @json['next_batch']
87
+ save_batch
88
+ end
89
+
90
+ def error(res)
91
+ json = JSON.parse res.body
92
+ return json unless json.key? 'error'
93
+ case json['errcode']
94
+ when 'M_UNKNOWN_TOKEN' then login
95
+ else raise json['error']
96
+ end
97
+ end
98
+
99
+ def quit
100
+ @run = false
101
+ save_batch
102
+ end
103
+
104
+ def run
105
+ while @run
106
+ sync
107
+ save_batch
108
+ @event_method.each { |func| func.call self }
109
+ sleep 1
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,95 @@
1
+ module MatrixDBus
2
+ # get, post
3
+ #
4
+ # Example:
5
+ # get = Network.gen :get, 'http://localhost:5700'
6
+ # json = get.call '/get_login_info'
7
+ class Network
8
+ # gen lambda
9
+ #
10
+ # type: Symbol or String, 'get', 'form' or 'json;
11
+ # host: String: API address, like 'http://localhost:5700'
12
+ def initialize(host, error) # => Hash<Symble, Lambda>
13
+ @host = host
14
+ @error = error
15
+ end
16
+
17
+ # get url
18
+ #
19
+ # uri: String
20
+ # params (optional): Hash, url query
21
+ def get(uri, params = nil) # => Hash
22
+ uri = URI(@host + uri)
23
+ uri.query = URI.encode_www_form params if params
24
+ puts 'GET URL:', uri if $DEBUG
25
+ error RestClient.get(uri.to_s)
26
+ rescue RestClient::Exceptions::OpenTimeout
27
+ retry
28
+ rescue RestClient::BadGateway
29
+ retry
30
+ end
31
+
32
+ # post json to url
33
+ #
34
+ # uri: String
35
+ # body: Hash, post body
36
+ def post(uri, body)
37
+ uri = @host + uri
38
+ puts 'POST URL:', uri if $DEBUG
39
+ error RestClient.post(uri.to_s, body.to_json, content_type: :json)
40
+ rescue RestClient::Exceptions::OpenTimeout
41
+ retry
42
+ rescue RestClient::BadGateway
43
+ retry
44
+ end
45
+
46
+ # post raw to url
47
+ #
48
+ # uri: String
49
+ # body: Anything
50
+ def post_raw(uri, body)
51
+ uri = URI(@host) + URI(uri)
52
+ puts 'POST URL:', uri if $DEBUG
53
+ error RestClient.post(uri.to_s, body)
54
+ rescue RestClient::Exceptions::OpenTimeout
55
+ retry
56
+ rescue RestClient::BadGateway
57
+ retry
58
+ end
59
+
60
+ # put to url
61
+ #
62
+ # uri: String
63
+ # body: Hash, put body
64
+ def put(uri, body)
65
+ uri = @host + uri
66
+ puts 'PUT URL:', uri.to_s if $DEBUG
67
+ error RestClient.put(uri.to_s, body.to_json, content_type: :json)
68
+ rescue RestClient::Exceptions::OpenTimeout
69
+ retry
70
+ rescue RestClient::BadGateway
71
+ retry
72
+ end
73
+
74
+ # delete to url
75
+ #
76
+ # uri: String
77
+ # params (optional): Hash, url query
78
+ def delete(uri, params = nil) # => Hash
79
+ uri = URI(@host + uri)
80
+ puts 'DELETE URL:', uri if $DEBUG
81
+ uri.query = String.encode_www_form params if params
82
+ error RestClient.delete(uri.to_s)
83
+ rescue RestClient::Exceptions::OpenTimeout
84
+ retry
85
+ rescue RestClient::BadGateway
86
+ retry
87
+ end
88
+
89
+ private
90
+
91
+ def error(res)
92
+ @error.call res
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module MatrixDBus
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'matrix_dbus/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'matrix_dbus'
7
+ spec.version = MatrixDBus::VERSION
8
+ spec.authors = ['71e6fd52']
9
+ spec.email = ['DAStudio.71e6fd52@gmail.com']
10
+
11
+ spec.summary = 'matrix to D-Bus'
12
+ spec.homepage = 'https://github.com/71e6fd52/matrix_dbus'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.15'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+
25
+ spec.add_dependency 'ruby-dbus', '~> 0.13.0'
26
+ spec.add_dependency 'rest-client', '~> 2.0'
27
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matrix_dbus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - 71e6fd52
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ruby-dbus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.13.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.13.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ description:
84
+ email:
85
+ - DAStudio.71e6fd52@gmail.com
86
+ executables:
87
+ - matrix-dbus
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - README.en.md
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - exe/matrix-dbus
101
+ - lib/matrix_dbus.rb
102
+ - lib/matrix_dbus/api.rb
103
+ - lib/matrix_dbus/dbus.rb
104
+ - lib/matrix_dbus/matrix.rb
105
+ - lib/matrix_dbus/network.rb
106
+ - lib/matrix_dbus/version.rb
107
+ - matrix_dbus.gemspec
108
+ homepage: https://github.com/71e6fd52/matrix_dbus
109
+ licenses: []
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.13
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: matrix to D-Bus
131
+ test_files: []