Class: SimpleXChat::ClientAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/simplex-chat.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_uri, connect: true, timeout_ms: 10_000, interval_ms: 100) ⇒ ClientAgent

Returns a new instance of ClientAgent.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simplex-chat.rb', line 18

def initialize(client_uri, connect: true, timeout_ms: 10_000, interval_ms: 100)
  @uri = client_uri
  @message_queue = Queue.new
  @chat_message_queue = Queue.new
  @socket = nil
  @handshake = nil

  # Helpers for handling requests to and messages from the SXC client
  @listener_thread = nil
  @corr_id = Concurrent::AtomicFixnum.new(1) # Correlation ID for mapping client responses to command waiters
  @command_waiters = Concurrent::Hash.new
  @timeout_ms = timeout_ms
  @interval_ms = interval_ms

  @logger = Logging.logger

  self.connect if connect

  @logger.debug("Initialized ClientAgent")
end

Instance Method Details

#api_auto_accept(is_enabled) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/simplex-chat.rb', line 307

def api_auto_accept is_enabled
  onoff = is_enabled && "on" || "off"

  resp = send_command "/auto_accept #{onoff}"
  check_response_type(resp, "userContactLinkUpdated")

  nil
end

#api_chats(chat_count = 20) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/simplex-chat.rb', line 356

def api_chats(
  chat_count=20 # if nil, will return all the chats
)
  param = chat_count != nil ? "#{chat_count}" : "all"
  cmd = "/chats #{param}"
  resp = send_command cmd
  check_response_type(resp, "chats")

  resp["chats"].map do |chat|
    chat_type = parse_chat_info_type chat["chatInfo"]["type"]
    next if chat_type == nil # WARN: Chat type "local" is currently ignored
    conversation = nil
    if chat_type == ChatType::GROUP
      conversation = chat["chatInfo"]["groupInfo"]["localDisplayName"]
    else
      conversation = chat["chatInfo"]["contact"]["localDisplayName"]
    end

    {
      :chat_type => chat_type,
      :conversation => conversation
    }
  end.filter { |x| x != nil }
end

#api_contactsObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/simplex-chat.rb', line 268

def api_contacts
  resp = send_command "/contacts"
  check_response_type(resp, "contactsList")

  contacts = resp["contacts"]
  contacts.map{ |c| {
    "id" => c["contactId"],
    "name" => c["localDisplayName"],
    "preferences" => c["profile"]["preferences"].map{|k, v| { k => v["allow"] == "yes"}}.reduce({}, :merge),
    "mergedPreferences" => c["mergedPreferences"].map{|k, v| {
      k => (v["enabled"]["forUser"] && v["enabled"]["forContact"])
    }}.reduce({}, :merge),
  }}
end

#api_create_user_addressObject



240
241
242
243
244
245
# File 'lib/simplex-chat.rb', line 240

def api_create_user_address
  resp = send_command '/address'
  check_response_type(resp, "userContactLinkCreated")

  resp["connReqContact"]
end

#api_get_user_addressObject



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/simplex-chat.rb', line 227

def api_get_user_address
  resp = send_command "/show_address"
  resp_type = resp["type"]

  # Check if user doesn't have an address      
  if resp_type == "chatCmdError" && resp.dig("chatError", "storeError", "type") == "userContactLinkNotFound"
    return nil
  end
  check_response_type(resp, "userContactLink")

  resp["contactLink"]["connReqContact"]
end

#api_groupsObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/simplex-chat.rb', line 283

def api_groups
  resp = send_command "/groups"
  check_response_type(resp, "groupsList")

  groups = resp["groups"]
  groups.map{ |entry| 
    group = entry[0]
    members = entry[1]

    {
      "id" => group["groupId"],
      "name" => group["localDisplayName"],
      "preferences" => group["fullGroupPreferences"].map{|k, v| { k => v["enable"] == "on" }}.reduce({}, :merge),
      "currentMembers" => members["currentMembers"],
      "invitedByContactId" => group.dig("membership", "invitedBy", "byContactId"),
      "invitedByGroupMemberId" => group.dig("membership", "invitedByGroupMemberId"),
      "memberName" => group["membership"]["localDisplayName"],
      "memberRole" => group["membership"]["memberRole"],
      "memberCategory" => group["membership"]["memberCategory"],
      "memberStatus" => group["membership"]["memberStatus"]
    }
  }
end

#api_kick_group_member(group, member) ⇒ Object



316
317
318
319
# File 'lib/simplex-chat.rb', line 316

def api_kick_group_member(group, member)
  resp = send_command "/remove #{group} #{member}"
  check_response_type(resp, "userDeletedMember")
end

#api_network(socks: nil, socks_mode: nil, smp_proxy: nil, smp_proxy_fallback: nil, timeout_secs: nil) ⇒ Object

Parameters for /network:

- socks: on/off/<[ipv4]:port>
- socks-mode: always/onion
- smp-proxy: always/unknown/unprotected/never
- smp-proxy-fallback: no/protected/yes
- timeout: <seconds>


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/simplex-chat.rb', line 327

def api_network(socks: nil, socks_mode: nil, smp_proxy: nil, smp_proxy_fallback: nil, timeout_secs: nil)
  args = {
    "socks" => socks,
    "socks-mode" => socks_mode,
    "smp-proxy" => smp_proxy,
    "smp-proxy-fallback" => smp_proxy_fallback,
    "timeout" => timeout_secs
  }
  command = '/network'
  args.each do |param, value|
    next if value == nil
    command += " #{param}=#{value}"
  end
  resp = send_command command
  check_response_type(resp, "networkConfig")

  resp["networkConfig"]
end

#api_profileObject



217
218
219
220
221
222
223
224
225
# File 'lib/simplex-chat.rb', line 217

def api_profile
  resp = send_command '/profile'
  check_response_type(resp, "userProfile")

  {
    "name" => resp["user"]["profile"]["displayName"],
    "preferences" => resp["user"]["fullPreferences"].map{|k, v| { k => v["allow"] == "yes"}}.reduce({}, :merge)
  }
end

#api_reaction(chat_type, chat_id, message_item_id, add: true, emoji: '👍') ⇒ Object

TODO: Add ‘/_reaction members` support, either on this

function or in a separate one


383
384
385
386
387
388
389
390
391
392
# File 'lib/simplex-chat.rb', line 383

def api_reaction(chat_type, chat_id, message_item_id, add: true, emoji: '👍')
  onoff = add ? "on" : "off"
  param_obj = {
    "type" => "emoji",
    "emoji" => emoji
  }
  cmd = "/_reaction #{chat_type}#{chat_id} #{message_item_id} #{onoff} #{param_obj.to_json}"
  resp = send_command cmd
  check_response_type(resp, "chatItemReaction")
end

#api_send_file(chat_type, receiver, file_path) ⇒ Object



261
262
263
264
265
266
# File 'lib/simplex-chat.rb', line 261

def api_send_file(chat_type, receiver, file_path)
  resp = send_command "/file #{chat_type}#{receiver} #{file_path}"
  check_response_type(resp, "newChatItems")

  resp["chatItems"]
end

#api_send_image(chat_type, receiver, file_path) ⇒ Object



254
255
256
257
258
259
# File 'lib/simplex-chat.rb', line 254

def api_send_image(chat_type, receiver, file_path)
  resp = send_command "/image #{chat_type}#{receiver} #{file_path}"
  check_response_type(resp, "newChatItems")

  resp["chatItems"]
end

#api_send_text_message(chat_type, receiver, message) ⇒ Object



247
248
249
250
251
252
# File 'lib/simplex-chat.rb', line 247

def api_send_text_message(chat_type, receiver, message)
  resp = send_command "#{chat_type}#{receiver} #{message}"
  check_response_type(resp, "newChatItems")

  resp["chatItems"]
end

#api_tail(chat_type: nil, conversation: nil, message_count: nil) ⇒ Object



346
347
348
349
350
351
352
353
354
# File 'lib/simplex-chat.rb', line 346

def api_tail(chat_type: nil, conversation: nil, message_count: nil)
  cmd = "/tail"
  cmd += " #{chat_type}#{conversation}" if chat_type != nil && conversation != nil
  cmd += " #{message_count}" if message_count != nil
  resp = send_command cmd
  check_response_type(resp, "chatItems")

  resp["chatItems"].map{|chat_item| parse_chat_item chat_item}
end

#api_versionObject



210
211
212
213
214
215
# File 'lib/simplex-chat.rb', line 210

def api_version
  resp = send_command '/version'
  check_response_type(resp, "versionInfo")

  resp["versionInfo"]["version"]
end

#connectObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/simplex-chat.rb', line 39

def connect
  @logger.debug("Connecting to: '#{@uri}'...")
  @socket = TCPSocket.new @uri.host, @uri.port
  @handshake = WebSocket::Handshake::Client.new(url: @uri.to_s)

  # Do websocket handshake
  @logger.debug("Doing handshake with: '#{@uri}'...")
  @socket.write @handshake.to_s
  resp = HTTPResponse.read_new(Net::BufferedIO.new(@socket))

  @listener_thread = Thread.new do
    frame = WebSocket::Frame::Incoming::Client.new(version: @handshake.version)
    loop do
      begin
        buf = @socket.read_nonblock 4096
        frame << buf
        obj = frame.next
        next if obj == nil
        @logger.debug("New message (raw): #{obj}")

        msg = JSON.parse obj.to_s
        # @logger.debug("New message: #{msg}")
        # @logger.debug("Command waiters: #{@command_waiters}")

        corr_id = msg["corrId"]
        resp = msg["resp"]
        single_use_queue = @command_waiters[corr_id]
        if corr_id != nil && single_use_queue != nil
          single_use_queue = @command_waiters[corr_id]
          single_use_queue.push(resp)
          @logger.debug("Message sent to waiter with corrId '#{corr_id}'")
        else
          @message_queue.push resp
          @logger.debug("Message put on message queue (number of messages in queue: #{@message_queue.size})")
        end
      rescue IO::WaitReadable
        IO.select([@socket])
        retry
      rescue IO::WaitWritable
        IO.select(nil, [@socket])
        retry
      rescue => e
        # TODO: Verify if this way of stopping the execution
        #       is graceful enough after implementing reconnects
        @logger.error "Unhandled exception caught: #{e}"
        @message_queue.close
        raise e
      end
    end
  end

  @logger.info("Successfully connected ClientAgent to: #{@uri}")
end

#disconnectObject



157
158
159
160
161
162
# File 'lib/simplex-chat.rb', line 157

def disconnect
  @listener_thread.terminate
  @socket.close
  @message_queue.clear
  @chat_message_queue.clear
end

#next_chat_message(max_backlog_secs: 15.0) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/simplex-chat.rb', line 99

def next_chat_message(
  max_backlog_secs: 15.0 # if nil, it will process any incoming messages, including old ones
)
  # NOTE: There can be more than one message per
  #       client message. Because of that, we use
  #       a chat message queue to insert one or
  #       more messages at a time, but poll just
  #       one at a time
  # NOTE: We use non-blocking pop for thread safety
  begin
    chat_msg = @chat_message_queue.pop(true)
    return chat_msg
  rescue ThreadError
    @logger.debug("Chat message queue is empty, waiting for new messages...")
  end
  
  loop do
    msg = next_message
    break if msg == nil
    next if not ["chatItemUpdated", "newChatItems"].include?(msg["type"])

    # Handle one or more chat messages in a single client message
    new_chat_messages = nil
    if msg["type"] == "chatItemUpdated"
      new_chat_messages = [msg["chatItem"]]
    else
      new_chat_messages = msg["chatItems"]
    end

    new_chat_messages.each do |chat_item|
      chat_message = parse_chat_item chat_item

      time_diff = Time.now - chat_message[:msg_timestamp]
      if max_backlog_secs != nil && time_diff > max_backlog_secs
        @logger.debug("Skipped message (time diff: #{time_diff}, max allowed: #{max_backlog_secs}): #{chat_message}")
        next
      end

      @chat_message_queue.push chat_message
      @logger.debug("Chat message pushed to queue: #{chat_message}")
    end

    # NOTE: Even after parsing the messages, the
    #       chat message queue can be empty because
    #       all the messages are too old, so we have
    #       to check again
    begin
      chat_msg = @chat_message_queue.pop(true)
      @logger.debug("Chat message popped from queue")
      return chat_msg
    rescue ThreadError
      @logger.debug("Chat message queue is still empty, waiting for new messages...")
    end
  end

  nil
end

#next_messageObject



93
94
95
96
97
# File 'lib/simplex-chat.rb', line 93

def next_message
  msg = @message_queue.pop
  @logger.debug("Message retrieved from queue (number of messages in queue: #{@message_queue.size})")
  msg
end

#send_command(cmd, timeout_ms: @timeout_ms, interval_ms: @interval_ms) ⇒ Object

Sends a raw command to the SimpleX Chat client



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/simplex-chat.rb', line 165

def send_command(cmd, timeout_ms: @timeout_ms, interval_ms: @interval_ms)
  corr_id = next_corr_id
  obj = {
    "corrId" => corr_id,
    "cmd" => cmd
  }
  json = obj.to_json
  frame = WebSocket::Frame::Outgoing::Client.new(version: @handshake.version, data: json, type: :text)

  # The listener thread will send the message
  # that matches the corrId to this single
  # use queue instead of the global message queue,
  # and this function will poll it to wait for the
  # command response
  single_use_queue = SizedQueue.new 1
  @command_waiters[corr_id] = single_use_queue
  @logger.debug("Created command waiter for command ##{corr_id}")

  @logger.debug("Sending command ##{corr_id}: #{json.to_s}")
  @socket.write frame.to_s

  @logger.debug("Waiting response for command ##{corr_id}...")
  msg = nil
  iterations = timeout_ms / interval_ms
  iterations.times do
    begin
      msg = single_use_queue.pop(true)
      break
    rescue ThreadError
      sleep(interval_ms / 1000.0)
    end
  end

  if msg == nil
    raise SendCommandError.new(json.to_s)
  end

  @logger.debug("Command ##{corr_id} finished successfully with response: #{msg}")

  msg
ensure
  @command_waiters.delete corr_id
  @logger.debug("Cleaned up command waiter ##{corr_id}")
end