Class: MatrixQQ::Matrix::Forward::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/matrix_qq/matrix/forward/group.rb

Overview

send to qq group

Constant Summary collapse

Emoji =
[
  *(0x0080..0x02AF),
  *(0x0300..0x03FF),
  *(0x0600..0x06FF),
  *(0x0C00..0x0C7F),
  *(0x1DC0..0x1DFF),
  *(0x1E00..0x1EFF),
  *(0x2000..0x209F),
  *(0x20D0..0x214F),
  *(0x2190..0x23FF),
  *(0x2460..0x25FF),
  *(0x2600..0x27EF),
  *(0x2900..0x29FF),
  *(0x2B00..0x2BFF),
  *(0x2C60..0x2C7F),
  *(0x2E00..0x2E7F),
  *(0x3000..0x303F),
  *(0xA490..0xA4CF),
  *(0xE000..0xF8FF),
  *(0xFE00..0xFE0F),
  *(0xFE30..0xFE4F),
  *(0x1F000..0x1F02F),
  *(0x1F0A0..0x1F0FF),
  *(0x1F100..0x1F64F),
  *(0x1F680..0x1F6FF),
  *(0x1F910..0x1F96B),
  *(0x1F980..0x1F9E0)
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(dbus, matrix, info, room) ⇒ Group

Returns a new instance of Group.



35
36
37
38
39
40
# File 'lib/matrix_qq/matrix/forward/group.rb', line 35

def initialize(dbus, matrix, info, room)
  @dbus = dbus
  @info = info
  @matrix = matrix
  @room = room
end

Instance Method Details

#emoji(msg) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/matrix_qq/matrix/forward/group.rb', line 52

def emoji(msg)
  return { type: 'text', data: { text: msg } } if (msg & Emoji).empty?
  msg.each_codepoint.inject([]) do |obj, code|
    obj <<
      if Emoji.include? code
        { type: 'emoji', data: { id: code.to_s } }
      else
        { type: 'text', data: { text: [code].pack('U') } }
      end
  end
end

#format_matrix_message(msg, name, type = 'm.text') ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/matrix_qq/matrix/forward/group.rb', line 64

def format_matrix_message(msg, name, type = 'm.text')
  room = "{#{@info['send_room']}} " if @info['print_room']
  return "#{room}#{name} 发送了一条消息" if msg =~ /^-msg /
  return '有人发送了一条消息' if msg =~ /^-all /
  return if msg =~ /^- /

  info = type.match(/^m\./).post_match
  info ||= 'text'
  message msg, name, info, room
end

#match_bot(message) ⇒ Object



90
91
92
# File 'lib/matrix_qq/matrix/forward/group.rb', line 90

def match_bot(message)
  message.match(/^(\(.*?\))?\[(.*?)\]\s*/)
end

#message(msg, name, type, room = '') ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/matrix_qq/matrix/forward/group.rb', line 75

def message(msg, name, type, room = '')
  case type
  when 'text'
    m = msg.match(/^-name /)
    m ? m.post_match : "#{room}[#{name}] #{msg}"
  when 'notice' then "#{room}[#{name}] notice #{msg}"
  when 'emote' then "#{room}#{name} #{msg}"
  else "#{room}#{name} send a #{info}"
  end
end

#runObject



42
43
44
45
46
47
48
49
50
# File 'lib/matrix_qq/matrix/forward/group.rb', line 42

def run
  msg = @info['content']
  body = msg['body']
  type = msg['msgtype']
  sender = user @info['sender']
  sender, body = user_bot body if user_bot? body
  message = format_matrix_message(body, sender, type)
  MatrixQQ::QQ::SendGroup.array @matrix, @room, emoji(message)
end

#user(user) ⇒ Object



86
87
88
# File 'lib/matrix_qq/matrix/forward/group.rb', line 86

def user(user)
  @dbus.get("/profile/#{user}/displayname")['displayname']
end

#user_bot(message) ⇒ Object



100
101
102
103
104
# File 'lib/matrix_qq/matrix/forward/group.rb', line 100

def user_bot(message)
  m = match_bot message
  return unless m
  [m[2], m.post_match]
end

#user_bot?(message) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/matrix_qq/matrix/forward/group.rb', line 94

def user_bot?(message)
  m = match_bot message
  return true if m
  false
end