matrix_qq 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/README.en.md +35 -0
- data/README.md +35 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/matrix_qq +39 -0
- data/lib/matrix_qq/matrix/dbus.rb +23 -0
- data/lib/matrix_qq/matrix/forward/group.rb +68 -0
- data/lib/matrix_qq/matrix/forward/main.rb +51 -0
- data/lib/matrix_qq/matrix/log.rb +63 -0
- data/lib/matrix_qq/matrix/matrix.rb +50 -0
- data/lib/matrix_qq/matrix/require.rb +9 -0
- data/lib/matrix_qq/matrix/send.rb +21 -0
- data/lib/matrix_qq/qq/dbus.rb +24 -0
- data/lib/matrix_qq/qq/forward_group/main.rb +37 -0
- data/lib/matrix_qq/qq/forward_group/matrix.rb +46 -0
- data/lib/matrix_qq/qq/log.rb +56 -0
- data/lib/matrix_qq/qq/qq.rb +54 -0
- data/lib/matrix_qq/qq/require.rb +9 -0
- data/lib/matrix_qq/qq/send.rb +14 -0
- data/lib/matrix_qq/version.rb +3 -0
- data/lib/matrix_qq.rb +8 -0
- data/matrix_qq.gemspec +28 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bfd437e1c4a5be72e79b771d123cd4f67bf666f6
|
4
|
+
data.tar.gz: 53fba0f651a4eb8eece55b86927b66cda41b6fac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1fdc303ec9fa296b7c6820d70d089dfdd9f167ceb424eac4b7c425b74a465084bcbf8bcad1f66681593bd151bb3c0616943d77257f9e9428457a7eabc4b478e8
|
7
|
+
data.tar.gz: f6df154c0a1cd4c55346b2a2ac1febe9926ce18919b624a5bfbd927c6ff2526ac6b23ba467e7cb362fbf688181b86bdbe69d42a841641d88bbf6e775a05f7050
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.en.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# matrix_qq ([中文](/README.md))
|
2
|
+
|
3
|
+
The bridge between [matrix](https://matrix.org) and D-Bus.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's `Gemfile`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'matrix_qq'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install matrix_qq
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
NOTICE: Be sure `matrix-dbus` and `cqhttp-dbus` running.
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/71e6fd52/matrix_qq).
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# matrix_qq ([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_qq'
|
11
|
+
```
|
12
|
+
|
13
|
+
然后运行:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
或者你自己安装
|
18
|
+
|
19
|
+
$ gem install matrix_qq
|
20
|
+
|
21
|
+
## 使用
|
22
|
+
|
23
|
+
TODO: 写使用说明
|
24
|
+
|
25
|
+
注意:请确保 `matrix-dbus` 和 `cqhttp-dbus` 正在运行。
|
26
|
+
|
27
|
+
## 开发
|
28
|
+
|
29
|
+
检出这个仓库后,运行 `bin/setup` 来安装依赖关系。然后,运行 `rake spec` 来运行测试。 您还可以运行 `bin/console` 启动交互式提示符(`irb`),让您进行实验。
|
30
|
+
|
31
|
+
要将此 gem 安装到本机上,请运行 `bundle exec rake install`。 要释出新版本,请在 `version.rb` 中更新版本号,然后运行 `bundle exec rake release`,该版本将为该版本创建一个 git 标签,并推送 git 提交和标签,然后将 `.gem` 文件提交到 [rubygems.org](https://rubygems.org)。
|
32
|
+
|
33
|
+
## 贡献
|
34
|
+
|
35
|
+
欢迎来 [GitHub](https://github.com/71e6fd52/matrix_qq) 上发起 issues 和 pull requests
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'matrix_qq'
|
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
data/exe/matrix_qq
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'matrix_qq'
|
4
|
+
|
5
|
+
config = File.join(ENV['HOME'], '.config', 'matrix-qq', 'config')
|
6
|
+
unless File.size? config
|
7
|
+
File.open config, 'w' do |f|
|
8
|
+
f.puts %(module MatrixQQ
|
9
|
+
Config = {
|
10
|
+
tunnel: {
|
11
|
+
# '#example:matrix.org' => {
|
12
|
+
# type: 'matrix',
|
13
|
+
# to: { '123456789' => 'group' }
|
14
|
+
# },
|
15
|
+
# '123456789' => {
|
16
|
+
# type: 'group',
|
17
|
+
# to: { '#example:matrix.org' => 'matrix' }
|
18
|
+
# },
|
19
|
+
# '10000' => {
|
20
|
+
# type: 'friend',
|
21
|
+
# to: { '!abcdef:matrix.org' => 'matrix' }
|
22
|
+
# }
|
23
|
+
}
|
24
|
+
}
|
25
|
+
end)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
load config
|
29
|
+
|
30
|
+
session_bus = DBus::SessionBus.instance
|
31
|
+
|
32
|
+
matrix = MatrixQQ::Matrix.new session_bus
|
33
|
+
qq = MatrixQQ::QQ.new session_bus
|
34
|
+
matrix.qq_dbus = qq.dbus
|
35
|
+
qq.matrix_dbus = matrix.dbus
|
36
|
+
|
37
|
+
main = DBus::Main.new
|
38
|
+
main << session_bus
|
39
|
+
main.run
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class Matrix
|
3
|
+
class DBus
|
4
|
+
attr_reader :obj
|
5
|
+
|
6
|
+
def initialize(dbus)
|
7
|
+
srv = dbus.service('org.dastudio.matrix')
|
8
|
+
@obj = srv.object '/org/dastudio/matrix'
|
9
|
+
@obj.default_iface = 'org.dastudio.matrix'
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to_missing?(name, include_private = false)
|
13
|
+
@obj.respond_to_missing? name, include_private
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *args)
|
17
|
+
super if args.size > 2
|
18
|
+
args << {} if args.size == 1
|
19
|
+
JSON.parse @obj.send(name, args[0], args[1].to_json).first
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class Matrix
|
3
|
+
class Forward
|
4
|
+
# send to qq group
|
5
|
+
class Group
|
6
|
+
def initialize(dbus, matrix, info, room)
|
7
|
+
@dbus = dbus
|
8
|
+
@info = info
|
9
|
+
@matrix = matrix
|
10
|
+
@room = room
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
msg = @info['content']
|
15
|
+
body = msg['body']
|
16
|
+
type = msg['msgtype']
|
17
|
+
sender = user @info['sender']
|
18
|
+
sender, body = user_bot body if user_bot? body
|
19
|
+
MatrixQQ::QQ::SendGroup.text \
|
20
|
+
@matrix, @room, format_matrix_message(body, sender, type)
|
21
|
+
end
|
22
|
+
|
23
|
+
def format_matrix_message(msg, name, type = 'm.text')
|
24
|
+
return "#{name} 发送了一条消息" if msg =~ /^-msg /
|
25
|
+
return '有人发送了一条消息' if msg =~ /^-all /
|
26
|
+
return if msg =~ /^- /
|
27
|
+
|
28
|
+
info = type.match(/^m\./).post_match
|
29
|
+
info ||= 'text'
|
30
|
+
message msg, name, info
|
31
|
+
end
|
32
|
+
|
33
|
+
def message(msg, name, type)
|
34
|
+
case type
|
35
|
+
when 'text'
|
36
|
+
m = msg.match(/^-name /)
|
37
|
+
m ? m.post_match : "[#{name}] #{msg}"
|
38
|
+
when 'notice' then "[#{name}] notice #{msg}"
|
39
|
+
when 'emote' then "#{name} #{msg}"
|
40
|
+
else "#{name} send a #{info}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def user(user)
|
45
|
+
@dbus.get("/profile/#{user}/displayname")['displayname']
|
46
|
+
end
|
47
|
+
|
48
|
+
def match_bot(message)
|
49
|
+
message.match(/^(\(.*?\))?\[(.*?)\]\s*/)
|
50
|
+
end
|
51
|
+
|
52
|
+
def user_bot?(message)
|
53
|
+
m = match_bot message
|
54
|
+
return true if m
|
55
|
+
false
|
56
|
+
end
|
57
|
+
|
58
|
+
def user_bot(message)
|
59
|
+
m = match_bot message
|
60
|
+
return unless m
|
61
|
+
[m[2], m.post_match]
|
62
|
+
end
|
63
|
+
end # Matrix
|
64
|
+
|
65
|
+
Forward.send_to['group'] << Group
|
66
|
+
end # ForwardGroup
|
67
|
+
end # QQ
|
68
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class Matrix
|
3
|
+
# send matrix massage to other
|
4
|
+
class Forward
|
5
|
+
class << self
|
6
|
+
attr_accessor :send_to
|
7
|
+
end
|
8
|
+
self.send_to = Hash.new { |h, k| h[k] = [] }
|
9
|
+
|
10
|
+
def initialize(dbus, qq, info)
|
11
|
+
@dbus = dbus
|
12
|
+
@info = info
|
13
|
+
@qq = qq
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
return unless @info.is_a? Hash
|
18
|
+
@info.each_pair do |room, value|
|
19
|
+
tunnel = Config[:tunnel][room]
|
20
|
+
next if tunnel.nil?
|
21
|
+
next unless tunnel[:type] == 'matrix'
|
22
|
+
each_event value['timeline']['events'], tunnel
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def each_event(events, tunnel)
|
27
|
+
events.each do |event|
|
28
|
+
next unless event['type'] == 'm.room.message'
|
29
|
+
next if exist @info['event_id']
|
30
|
+
tunnel[:to].each_pair do |to_room, type|
|
31
|
+
call_module(event, to_room, type)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def call_module(event, room, type)
|
37
|
+
Forward.send_to[type.to_s].each do |func|
|
38
|
+
puts "Start #{func.name}" if $VERBOSE
|
39
|
+
func.new(@dbus, @qq, event, room).run
|
40
|
+
puts "End #{func.name}" if $VERBOSE
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def exist(event_id)
|
45
|
+
MatrixQQ::Matrix::Send.ignore.delete(event_id)
|
46
|
+
end
|
47
|
+
end # Forward
|
48
|
+
|
49
|
+
Matrix.join << Forward
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class Matrix
|
3
|
+
# Log message to stdout
|
4
|
+
class Log
|
5
|
+
def initialize(dbus, _, info)
|
6
|
+
@dbus = dbus
|
7
|
+
@info = info
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
return unless @info.is_a? Hash
|
12
|
+
@info.each_pair do |room, events|
|
13
|
+
log_room room
|
14
|
+
events['timeline']['events'].each do |event|
|
15
|
+
next unless event['type'] == 'm.room.message'
|
16
|
+
log_message event['sender'], event['content']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def log_message(sender, content)
|
22
|
+
name = user sender
|
23
|
+
if content['msgtype'] == 'm.text'
|
24
|
+
message = content['body']
|
25
|
+
name, message = user_bot message if user_bot? message
|
26
|
+
else
|
27
|
+
message = content
|
28
|
+
end
|
29
|
+
puts "#{name}: #{message}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def log_room(room)
|
33
|
+
puts "#{self.room(room)['name']}:"
|
34
|
+
end
|
35
|
+
|
36
|
+
def room(room)
|
37
|
+
@dbus.get "/rooms/#{room}/state/m.room.name"
|
38
|
+
end
|
39
|
+
|
40
|
+
def user(user)
|
41
|
+
@dbus.get("/profile/#{user}/displayname")['displayname']
|
42
|
+
end
|
43
|
+
|
44
|
+
def match_bot(message)
|
45
|
+
message.match(/^(\(.*?\))?\[(.*?)\]\s*/)
|
46
|
+
end
|
47
|
+
|
48
|
+
def user_bot?(message)
|
49
|
+
m = match_bot message
|
50
|
+
return true if m
|
51
|
+
false
|
52
|
+
end
|
53
|
+
|
54
|
+
def user_bot(message)
|
55
|
+
m = match_bot message
|
56
|
+
return unless m
|
57
|
+
[m[2], m.post_match]
|
58
|
+
end
|
59
|
+
end # Log
|
60
|
+
|
61
|
+
Matrix.join << Log
|
62
|
+
end # Matrix
|
63
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class Matrix
|
3
|
+
SIGN = %i[
|
4
|
+
all
|
5
|
+
account_data
|
6
|
+
to_device
|
7
|
+
presence
|
8
|
+
rooms
|
9
|
+
leave
|
10
|
+
join
|
11
|
+
invite
|
12
|
+
device_lists
|
13
|
+
changed
|
14
|
+
left
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
class << self
|
18
|
+
SIGN.each { |i| attr_accessor i }
|
19
|
+
end
|
20
|
+
SIGN.each { |i| Matrix.send (i.to_s + '='), [] }
|
21
|
+
|
22
|
+
attr_reader :dbus, :info
|
23
|
+
attr_accessor :qq_dbus
|
24
|
+
|
25
|
+
def initialize(dbus)
|
26
|
+
@dbus = DBus.new dbus
|
27
|
+
reg
|
28
|
+
end
|
29
|
+
|
30
|
+
def reg
|
31
|
+
SIGN.each do |i|
|
32
|
+
@dbus.obj.on_signal i.to_s do |json|
|
33
|
+
parse json
|
34
|
+
info = @info
|
35
|
+
Matrix.send(i).each do |func|
|
36
|
+
puts "Start #{func.name}" if $VERBOSE
|
37
|
+
func.new(@dbus, @qq_dbus, info.dup).run
|
38
|
+
puts "End #{func.name}" if $VERBOSE
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def parse(json)
|
47
|
+
@info = JSON.parse json
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class Matrix
|
3
|
+
module Send
|
4
|
+
class << self
|
5
|
+
attr_accessor :ignore
|
6
|
+
end
|
7
|
+
self.ignore = []
|
8
|
+
|
9
|
+
def self.raw(dbus, room_id, event_type, body)
|
10
|
+
txn_id = SecureRandom.hex(32)
|
11
|
+
puts "send #{body}" if $VERBOSE
|
12
|
+
res = dbus.put "/rooms/#{room_id}/send/#{event_type}/#{txn_id}", body
|
13
|
+
ignore << (res['event_id'])
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.text(dbus, room_id, message)
|
17
|
+
raw dbus, room_id, 'm.room.message', msgtype: 'm.text', body: message
|
18
|
+
end
|
19
|
+
end # Send
|
20
|
+
end # Matrix
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class QQ
|
3
|
+
class DBus
|
4
|
+
attr_reader :obj
|
5
|
+
|
6
|
+
def initialize(dbus)
|
7
|
+
srv = dbus.service('org.dastudio.qq')
|
8
|
+
@obj = srv.object '/org/dastudio/qq'
|
9
|
+
@obj.default_iface = 'org.dastudio.cqhttp'
|
10
|
+
end
|
11
|
+
|
12
|
+
def respond_to_missing?(name, include_private = false)
|
13
|
+
@obj.respond_to_missing? name, include_private
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *args)
|
17
|
+
super if args.size > 1
|
18
|
+
args << {} if args.empty?
|
19
|
+
arg = args.first
|
20
|
+
JSON.parse @obj.call(name.to_s, arg.to_json).first
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class QQ
|
3
|
+
# send group massage to other
|
4
|
+
class ForwardGroup
|
5
|
+
class << self
|
6
|
+
attr_accessor :send_to
|
7
|
+
end
|
8
|
+
self.send_to = Hash.new { |h, k| h[k] = [] }
|
9
|
+
|
10
|
+
def initialize(dbus, matrix, info)
|
11
|
+
@dbus = dbus
|
12
|
+
@info = info
|
13
|
+
@matrix = matrix
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
return unless @info.is_a? Hash
|
18
|
+
tunnel = Config[:tunnel][@info['group_id'].to_s]
|
19
|
+
return if tunnel.nil?
|
20
|
+
return unless tunnel[:type] == 'group'
|
21
|
+
tunnel[:to].each_pair do |room, type|
|
22
|
+
call_module(room, type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def call_module(room, type)
|
27
|
+
ForwardGroup.send_to[type.to_s].each do |func|
|
28
|
+
puts "Start #{func.name}" if $VERBOSE
|
29
|
+
func.new(@dbus, @matrix, @info, room).run
|
30
|
+
puts "End #{func.name}" if $VERBOSE
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end # Forward
|
34
|
+
|
35
|
+
QQ.group << ForwardGroup
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class QQ
|
3
|
+
class ForwardGroup
|
4
|
+
# send to matrix
|
5
|
+
class Matrix
|
6
|
+
def initialize(dbus, matrix, info, room)
|
7
|
+
@dbus = dbus
|
8
|
+
@info = info
|
9
|
+
@matrix = matrix
|
10
|
+
@room = room
|
11
|
+
end
|
12
|
+
|
13
|
+
def run
|
14
|
+
msg = message @info['message']
|
15
|
+
sender = user @info['user_id'], @info['group_id']
|
16
|
+
MatrixQQ::Matrix::Send.text @matrix, @room, "[#{sender}] #{msg}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def message(messages)
|
20
|
+
messages.inject('') do |obj, msg|
|
21
|
+
obj +
|
22
|
+
case msg['type']
|
23
|
+
when 'text' then msg['data']['text']
|
24
|
+
when 'emoji' then [msg['data']['id'].to_i].pack 'U'
|
25
|
+
when 'face' then "[QQ 表情:#{msg['data']['id']}]"
|
26
|
+
when 'record' then '[语音]'
|
27
|
+
when 'image' then msg['data']['url']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def user(user, group_id = nil)
|
33
|
+
if group_id.nil?
|
34
|
+
return @dbus.get_stranger_info(user_id: user)['nickname']
|
35
|
+
end
|
36
|
+
info = @dbus.get_group_member_info(user_id: user, group_id: group_id)
|
37
|
+
info = info['data']
|
38
|
+
name = info['card']
|
39
|
+
name == '' ? info['nickname'] : name
|
40
|
+
end
|
41
|
+
end # Matrix
|
42
|
+
|
43
|
+
ForwardGroup.send_to['matrix'] << Matrix
|
44
|
+
end # ForwardGroup
|
45
|
+
end # QQ
|
46
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class QQ
|
3
|
+
# Log message to stdout
|
4
|
+
class Log
|
5
|
+
def initialize(dbus, _, info)
|
6
|
+
@dbus = dbus
|
7
|
+
@info = info
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
return unless @info.is_a? Hash
|
12
|
+
log_room @info['group_id'] if @info.key? 'group_id'
|
13
|
+
msg = message @info['message']
|
14
|
+
sender = user @info['user_id'], @info['group_id']
|
15
|
+
log_message sender, msg
|
16
|
+
end
|
17
|
+
|
18
|
+
def message(messages)
|
19
|
+
messages.inject('') do |obj, msg|
|
20
|
+
obj +
|
21
|
+
case msg['type']
|
22
|
+
when 'text' then msg['data']['text']
|
23
|
+
when 'emoji' then [msg['data']['id'].to_i].pack 'U'
|
24
|
+
when 'face' then "[QQ 表情:#{msg['data']['id']}]"
|
25
|
+
when 'record' then '[语音]'
|
26
|
+
when 'image' then msg['data']['url']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def log_message(name, message)
|
32
|
+
puts "#{name}: #{message}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def log_room(groupid)
|
36
|
+
puts "#{room(groupid)}:"
|
37
|
+
end
|
38
|
+
|
39
|
+
def room(groupid)
|
40
|
+
groupid
|
41
|
+
end
|
42
|
+
|
43
|
+
def user(user, group_id = nil)
|
44
|
+
if group_id.nil?
|
45
|
+
return @dbus.get_stranger_info(user_id: user)['nickname']
|
46
|
+
end
|
47
|
+
info = @dbus.get_group_member_info(user_id: user, group_id: group_id)
|
48
|
+
info = info['data']
|
49
|
+
name = info['card']
|
50
|
+
name == '' ? info['nickname'] : name
|
51
|
+
end
|
52
|
+
end # Log
|
53
|
+
|
54
|
+
QQ.message << Log
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class QQ
|
3
|
+
SIGN = %i[
|
4
|
+
message
|
5
|
+
private
|
6
|
+
group
|
7
|
+
get_group_list
|
8
|
+
event
|
9
|
+
group_upload
|
10
|
+
group_admin
|
11
|
+
group_decrease
|
12
|
+
group_increase
|
13
|
+
friend_add
|
14
|
+
friend_request
|
15
|
+
join_request
|
16
|
+
invite_request
|
17
|
+
all
|
18
|
+
unknow
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
class << self
|
22
|
+
SIGN.each { |i| attr_accessor i }
|
23
|
+
end
|
24
|
+
SIGN.each { |i| QQ.send (i.to_s + '='), [] }
|
25
|
+
|
26
|
+
attr_reader :dbus, :info
|
27
|
+
attr_accessor :matrix_dbus
|
28
|
+
|
29
|
+
def initialize(dbus)
|
30
|
+
@dbus = DBus.new dbus
|
31
|
+
reg
|
32
|
+
end
|
33
|
+
|
34
|
+
def reg
|
35
|
+
SIGN.each do |i|
|
36
|
+
@dbus.obj.on_signal i.to_s do |json|
|
37
|
+
parse json
|
38
|
+
info = @info
|
39
|
+
QQ.send(i).each do |func|
|
40
|
+
puts "Start #{func.name}" if $VERBOSE
|
41
|
+
func.new(@dbus, @matrix_dbus, info.dup).run
|
42
|
+
puts "End #{func.name}" if $VERBOSE
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def parse(json)
|
51
|
+
@info = JSON.parse json
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module MatrixQQ
|
2
|
+
class QQ
|
3
|
+
module SendGroup
|
4
|
+
def self.raw(dbus, room_id, body)
|
5
|
+
dbus.send_group_msg group_id: room_id, message: body
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.text(dbus, room_id, message)
|
9
|
+
return if message.nil?
|
10
|
+
raw dbus, room_id, [message]
|
11
|
+
end
|
12
|
+
end # Send
|
13
|
+
end # Matrix
|
14
|
+
end
|
data/lib/matrix_qq.rb
ADDED
data/matrix_qq.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'matrix_qq/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'matrix_qq'
|
7
|
+
spec.version = MatrixQQ::VERSION
|
8
|
+
spec.authors = ['71e6fd52']
|
9
|
+
spec.email = ['DAStudio.71e6fd52@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'bridge between matrix and QQ'
|
12
|
+
spec.homepage = 'https://github.com/71e6fd52/matrix_qq'
|
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.14.0'
|
26
|
+
spec.add_dependency 'matrix_dbus', '~> 1.0'
|
27
|
+
spec.add_dependency 'CQHTTP', '~> 2.2'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matrix_qq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- 71e6fd52
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-28 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.14.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.14.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: matrix_dbus
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: CQHTTP
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.2'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.2'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- DAStudio.71e6fd52@gmail.com
|
100
|
+
executables:
|
101
|
+
- matrix_qq
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".rubocop.yml"
|
108
|
+
- ".travis.yml"
|
109
|
+
- Gemfile
|
110
|
+
- README.en.md
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- exe/matrix_qq
|
116
|
+
- lib/matrix_qq.rb
|
117
|
+
- lib/matrix_qq/matrix/dbus.rb
|
118
|
+
- lib/matrix_qq/matrix/forward/group.rb
|
119
|
+
- lib/matrix_qq/matrix/forward/main.rb
|
120
|
+
- lib/matrix_qq/matrix/log.rb
|
121
|
+
- lib/matrix_qq/matrix/matrix.rb
|
122
|
+
- lib/matrix_qq/matrix/require.rb
|
123
|
+
- lib/matrix_qq/matrix/send.rb
|
124
|
+
- lib/matrix_qq/qq/dbus.rb
|
125
|
+
- lib/matrix_qq/qq/forward_group/main.rb
|
126
|
+
- lib/matrix_qq/qq/forward_group/matrix.rb
|
127
|
+
- lib/matrix_qq/qq/log.rb
|
128
|
+
- lib/matrix_qq/qq/qq.rb
|
129
|
+
- lib/matrix_qq/qq/require.rb
|
130
|
+
- lib/matrix_qq/qq/send.rb
|
131
|
+
- lib/matrix_qq/version.rb
|
132
|
+
- matrix_qq.gemspec
|
133
|
+
homepage: https://github.com/71e6fd52/matrix_qq
|
134
|
+
licenses: []
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 2.6.13
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: bridge between matrix and QQ
|
156
|
+
test_files: []
|