Class: MatrixDBus::Matrix

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username: '', password: '', host: 'https://matrix.org:8448') ⇒ Matrix

Returns a new instance of Matrix.



6
7
8
9
10
11
12
13
14
15
# File 'lib/matrix_dbus/matrix.rb', line 6

def initialize(username: '', password: '', host: 'https://matrix.org:8448')
  @username = username
  @password = password
  @run = true
  @event_method = []
  @config = File.join(ENV['HOME'], '.config', 'matrix-qq')
  @network = Network.new(host + '/_matrix/client/r0', method(:error))
  load_token
  load_batch
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



4
5
6
# File 'lib/matrix_dbus/matrix.rb', line 4

def access_token
  @access_token
end

#jsonObject (readonly)

Returns the value of attribute json.



4
5
6
# File 'lib/matrix_dbus/matrix.rb', line 4

def json
  @json
end

#networkObject (readonly)

Returns the value of attribute network.



4
5
6
# File 'lib/matrix_dbus/matrix.rb', line 4

def network
  @network
end

Instance Method Details

#bind(func) ⇒ Object



17
18
19
# File 'lib/matrix_dbus/matrix.rb', line 17

def bind(func)
  @event_method << func
end

#check_loginObject



53
54
55
56
57
58
# File 'lib/matrix_dbus/matrix.rb', line 53

def 
  json = @network.get '/login'
  raise "can't login" unless json['flows'].find do |f|
    f['type'] == 'm.login.password'
  end
end

#error(res) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/matrix_dbus/matrix.rb', line 90

def error(res)
  json = JSON.parse res.body
  return json unless json.key? 'error'
  case json['errcode']
  when 'M_UNKNOWN_TOKEN' then 
  else raise json['error']
  end
end

#init_batchObject



73
74
75
76
77
# File 'lib/matrix_dbus/matrix.rb', line 73

def init_batch
  json = get "/sync?access_token=#{@access_token}"
  @next_batch = json['next_batch']
  save_batch
end

#load_batchObject



46
47
48
49
50
51
# File 'lib/matrix_dbus/matrix.rb', line 46

def load_batch
  sync unless File.exist? File.join(@config, 'batch')
  File.open File.join(@config, 'batch') do |f|
    @next_batch = f.gets.chomp
  end
end

#load_tokenObject



31
32
33
34
35
36
37
# File 'lib/matrix_dbus/matrix.rb', line 31

def load_token
   unless File.exist? File.join @config, 'token'
   unless File.size? File.join @config, 'token'
  File.open File.join(@config, 'token'), 'r', 0o600 do |f|
    @access_token = f.gets.chomp
  end
end

#loginObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/matrix_dbus/matrix.rb', line 60

def 
  
  raise 'Unknow username' if @username == ''
  raise 'Unknow password' if @password == ''
  json = @network.post \
    '/login',
    type: 'm.login.password',
    user: @username,
    password: @password
  @access_token = json['access_token']
  save_token
end

#quitObject



99
100
101
102
# File 'lib/matrix_dbus/matrix.rb', line 99

def quit
  @run = false
  save_batch
end

#runObject



104
105
106
107
108
109
110
111
# File 'lib/matrix_dbus/matrix.rb', line 104

def run
  while @run
    sync
    save_batch
    @event_method.each { |func| func.call self }
    sleep 1
  end
end

#save_batchObject



39
40
41
42
43
44
# File 'lib/matrix_dbus/matrix.rb', line 39

def save_batch
  puts @next_batch if $DEBUG
  File.open File.join(@config, 'batch'), 'w' do |f|
    f.puts @next_batch
  end
end

#save_tokenObject



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

def save_token
  unless File.directory? @config
    File.delete @config if File.exist? @config
    Dir.mkdir @config
  end
  File.open File.join(@config, 'token'), 'w', 0o600 do |f|
    f.puts @access_token
  end
end

#syncObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/matrix_dbus/matrix.rb', line 79

def sync
  init_batch if @next_batch.nil?
  @json = @network.get \
    '/sync',
    since: @next_batch,
    timeout: 10_000,
    access_token: @access_token
  @next_batch = @json['next_batch']
  save_batch
end