Sha256: fd4836fd1f49689b8d14d8d02eaa7b820a7738ddb1f5357be9885f9d6d83e0a4

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 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.method_missing func.to_sym, JSON.parse(args, symbolize_names: true)
    end
    dbus_signal :message, 'message_type:s, json:s'
    dbus_signal :event, 'event:s, json:s'
    dbus_signal :friend_request, 'json:s'
    dbus_signal :join_request, 'json:s'
    dbus_signal :invite_request, 'json:s'
    dbus_signal :all, 'json:s'
    dbus_signal :unknow, 'json:s'
  end
end

def cqhttp2dbus(c, obj)
  j = c.json
  obj.all j.to_json
  case j['post_type']
  when 'message' then obj.message j['message_type'], j.to_json
  when 'event' then obj.event j['event'], j.to_json
  when 'request'
    case j['request_type']
    when 'friend' then obj.friend_request j.to_json
    when 'group'
      case j['sub_type']
      when 'add' then obj.join_request j.to_json
      when 'invite' then obj.invite_request j.to_json
      else obj.unknow j.to_json
      end
    else obj.unknow j.to_json
    end
  else obj.unknow j.to_json
  end
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

2 entries across 2 versions & 1 rubygems

Version Path
CQHTTP-1.0.0 exe/cqhttp-dbus
CQHTTP-0.3.1 exe/cqhttp-dbus