Class: MatrixQQ::Matrix::Log

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

Overview

Log message to stdout

Instance Method Summary collapse

Constructor Details

#initialize(dbus, _, info) ⇒ Log

Returns a new instance of Log.



5
6
7
8
# File 'lib/matrix_qq/matrix/log.rb', line 5

def initialize(dbus, _, info)
  @dbus = dbus
  @info = info
end

Instance Method Details

#log_message(sender, content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/matrix_qq/matrix/log.rb', line 21

def log_message(sender, content)
  name = user sender
  if content['msgtype'] == 'm.text'
    message = content['body']
    name, message = user_bot message if user_bot? message
  else
    message = content
  end
  puts "#{name}: #{message}"
end

#log_room(room) ⇒ Object



32
33
34
# File 'lib/matrix_qq/matrix/log.rb', line 32

def log_room(room)
  puts "#{self.room(room)['name']}:"
end

#match_bot(message) ⇒ Object



44
45
46
# File 'lib/matrix_qq/matrix/log.rb', line 44

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

#room(room) ⇒ Object



36
37
38
# File 'lib/matrix_qq/matrix/log.rb', line 36

def room(room)
  @dbus.get "/rooms/#{room}/state/m.room.name"
end

#runObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/matrix_qq/matrix/log.rb', line 10

def run
  return unless @info.is_a? Hash
  @info.each_pair do |room, events|
    log_room room
    events['timeline']['events'].each do |event|
      next unless event['type'] == 'm.room.message'
      log_message event['sender'], event['content']
    end
  end
end

#user(user) ⇒ Object



40
41
42
# File 'lib/matrix_qq/matrix/log.rb', line 40

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

#user_bot(message) ⇒ Object



54
55
56
57
58
# File 'lib/matrix_qq/matrix/log.rb', line 54

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

#user_bot?(message) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/matrix_qq/matrix/log.rb', line 48

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