Sha256: 70ea2cfca93948496a30d2ab20764732c0b3257b8171dbfc54680213a669c704

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

module MatrixQQ
  class Matrix
    # Log message to stdout
    class Log
      def initialize(dbus, _, info)
        @dbus = dbus
        @info = info
      end

      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

      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

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

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

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

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

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

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

    Matrix.join << Log
  end # Matrix
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
matrix_qq-0.5.4 lib/matrix_qq/matrix/log.rb
matrix_qq-0.5.3 lib/matrix_qq/matrix/log.rb
matrix_qq-0.5.2 lib/matrix_qq/matrix/log.rb
matrix_qq-0.5.1 lib/matrix_qq/matrix/log.rb
matrix_qq-0.5.0 lib/matrix_qq/matrix/log.rb
matrix_qq-0.4.0 lib/matrix_qq/matrix/log.rb
matrix_qq-0.3.3 lib/matrix_qq/matrix/log.rb
matrix_qq-0.3.2 lib/matrix_qq/matrix/log.rb
matrix_qq-0.3.1 lib/matrix_qq/matrix/log.rb
matrix_qq-0.3.0 lib/matrix_qq/matrix/log.rb
matrix_qq-0.2.1 lib/matrix_qq/matrix/log.rb
matrix_qq-0.2.0 lib/matrix_qq/matrix/log.rb
matrix_qq-0.1.1 lib/matrix_qq/matrix/log.rb