Sha256: b604a6e788ddc537e2ef5a5b65c04cf00453bdcbb13a8c648ebd4672def7880d

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

#!/usr/bin/env ruby

require 'CQHTTP'
require 'dbus'
Thread.abort_on_exception = true

# DBus
class CQHTTP2DBus < DBus::Object
  def initialize(*args)
    @cqhttp = CQHTTP::API.new
    super
  end
  dbus_interface 'org.dastudio.cqhttp' do
    dbus_method :call, 'in func:s, in args:s' do |func, args|
      @cqhttp.send func, JSON.parse(args, symbolize_names: true)
    end

    %i[
      message
      private
      group
      get_group_list
      event
      group_upload
      group_admin
      group_decrease
      group_increase
      friend_add
      friend_request
      join_request
      invite_request
      all
      unknow
    ].each do |i|
      dbus_signal i, 'json:s'
    end
  end
end

# case signal
class Case
  def initialize(info, obj)
    @info = info
    @obj = obj
    @json = gen_json
  end

  def gen_json
    JSON.pretty_generate @info
  end

  def send_json(name)
    @obj.send name, @json
  end

  def unknow
    @obj.send_json :unknow
  end

  def run
    send_json :all
    case @info['post_type']
    when 'message' then message
    when 'event' then event
    when 'request' then request
    else unknow
    end
  end

  def message
    send_json :message
    send_json @info['message_type']
  end

  def event
    send_json :event
    send_json @info['event']
  end

  def request
    case @info['request_type']
    when 'friend' then send_json :friend_request
    when 'group'
      case j['sub_type']
      when 'add' then send_json :join_request
      when 'invite' then send_json :invite_request
      else unknow
      end
    else unknow
    end
  end
end

def cqhttp2dbus(c, obj)
  j = c.json
  Case.new(j, obj).run
end

bus = DBus::SessionBus.instance
service = bus.request_service 'org.dastudio.qq'
obj = CQHTTP2DBus.new '/org/dastudio/qq'
service.export obj

cqhttp = CQHTTP::Service.new
cqhttp.bind ->(c) { cqhttp2dbus c, obj }

Thread.new { cqhttp.run }

main = DBus::Main.new
main << bus
main.run

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
CQHTTP-2.1.0 exe/cqhttp-dbus