simplex-chat 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1dde8b2fbd228f72ec1b9cb4f95269401aa2fa68774b38fdbef43d8eec2b7d63
4
- data.tar.gz: 1c54b1064c53ceeb06ae4b0f68715e773e96753208ec87b7ad445e2863649969
3
+ metadata.gz: 3f038bfb3c5cc7308a7c53d4a19e4152cfcc4e3f54088093d66b1947d2816f7c
4
+ data.tar.gz: 4e4fa59d7fa8c29225b64bf7856ce9dec9a5551df0038158e4c53e4a60c25b7c
5
5
  SHA512:
6
- metadata.gz: 669c992a7cbab4e903581bae8def4f6a4dc6e269cc5041e8e0dd5901bc62e8d162aaf4aa95ab0f266932619e34c48d10400107dfd84d4a8cc60d64c51a40711a
7
- data.tar.gz: f6c49bb438a61eb4015cd94e5f29197214b36a7eb3d18f5a8bee05ecc2d7cbcf284f10d792cb3b0aad8aa9f3c41be36a78875b5c38b13961770aa835b829823c
6
+ metadata.gz: c3d3091b7fdbc2ff66ed648dc27729860fd79b2434dbb3bbe3f3ec7473042975a8a8aa981b853e302259137aa7b88b1ad5233f4459f9ec1d09aab7d8eadfd165
7
+ data.tar.gz: c690575e7ad589694966f0ed69f3e62b2ce7d9e562cd419b49bed1bc8bcfb07d5dfefc43fed5b2905ceca1d9520d6c5a90153a67ebc52df853a3f3886ea5fe07
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleXChat
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/simplex-chat.rb CHANGED
@@ -28,10 +28,18 @@ module SimpleXChat
28
28
  CONTACT_REQUEST = '<@'
29
29
  end
30
30
 
31
+ module GroupMemberRole
32
+ AUTHOR = 'author' # reserved and unused as of now, but added anyways
33
+ OWNER = 'owner'
34
+ ADMIN = 'admin'
35
+ MEMBER = 'member'
36
+ OBSERVER = 'observer'
37
+ end
38
+
31
39
  class ClientAgent
32
40
  attr_accessor :on_message
33
41
 
34
- def initialize client_uri, connect: true, log_level: Logger::INFO
42
+ def initialize client_uri, connect: true, log_level: Logger::INFO, timeout_ms: 30_000, interval_ms: 100
35
43
  @uri = client_uri
36
44
  @message_queue = SizedQueue.new 4096
37
45
  @chat_message_queue = Queue.new
@@ -42,6 +50,8 @@ module SimpleXChat
42
50
  @listener_thread = nil
43
51
  @corr_id = Concurrent::AtomicFixnum.new(1) # Correlation ID for mapping client responses to command waiters
44
52
  @command_waiters = Concurrent::Hash.new
53
+ @timeout_ms = timeout_ms
54
+ @interval_ms = interval_ms
45
55
 
46
56
  @logger = Logger.new($stderr)
47
57
  @logger.level = log_level
@@ -50,9 +60,7 @@ module SimpleXChat
50
60
  "| [#{severity}] | #{datetime} | (#{progname}) :: #{msg}\n"
51
61
  }
52
62
 
53
- if connect
54
- self.connect
55
- end
63
+ self.connect if connect
56
64
 
57
65
  @logger.debug("Initialized ClientAgent")
58
66
  end
@@ -144,8 +152,13 @@ module SimpleXChat
144
152
  group = nil
145
153
  sender = nil
146
154
  contact = nil
155
+ contact_role = nil
147
156
  if chat_type == ChatType::GROUP
148
- contact = chat_item["chatItem"]["chatDir"]["groupMember"]["localDisplayName"]
157
+ # NOTE: The group can "send messages" without a contact
158
+ # For example, when a member is removed, the group
159
+ # sends a message about his removal, with no contact
160
+ contact = chat_item.dig "chatItem", "chatDir", "groupMember", "localDisplayName"
161
+ contact_role = chat_item.dig "chatItem", "chatDir", "groupMember", "memberRole"
149
162
  group = chat_item["chatInfo"]["groupInfo"]["localDisplayName"]
150
163
  sender = group
151
164
  else
@@ -159,6 +172,7 @@ module SimpleXChat
159
172
  chat_message = {
160
173
  :chat_type => chat_type,
161
174
  :sender => sender,
175
+ :contact_role => contact_role,
162
176
  :contact => contact,
163
177
  :group => group,
164
178
  :msg_text => msg_text,
@@ -182,7 +196,7 @@ module SimpleXChat
182
196
  end
183
197
 
184
198
  # Sends a raw command to the SimpleX Chat client
185
- def send_command(cmd)
199
+ def send_command(cmd, timeout_ms: @timeout_ms, interval_ms: @interval_ms)
186
200
  corr_id = next_corr_id
187
201
  obj = {
188
202
  "corrId" => corr_id,
@@ -202,12 +216,16 @@ module SimpleXChat
202
216
  @socket.write frame.to_s
203
217
 
204
218
  msg = nil
205
- 100.times do
219
+ iterations = timeout_ms / interval_ms
220
+ iterations.times do
206
221
  begin
207
222
  msg = single_use_queue.pop(true)
208
223
  break
209
224
  rescue ThreadError
210
- sleep 0.1
225
+ sleep(interval_ms / 1000.0)
226
+ ensure
227
+ # Clean up command_waiters
228
+ @command_waiters.delete corr_id
211
229
  end
212
230
  end
213
231
 
@@ -308,6 +326,48 @@ module SimpleXChat
308
326
  }
309
327
  end
310
328
 
329
+ def api_auto_accept is_enabled
330
+ onoff = is_enabled && "on" || "off"
331
+
332
+ resp = send_command "/auto_accept #{onoff}"
333
+ resp_type = resp["type"]
334
+ raise "Unexpected response: #{resp_type}" if resp_type != "userContactLinkUpdated"
335
+
336
+ nil
337
+ end
338
+
339
+ def api_kick_group_member(group, member)
340
+ resp = send_command "/remove #{group} #{member}"
341
+ resp_type = resp["type"]
342
+ raise "Unexpected response: #{resp_type}" unless resp_type == "userDeletedMember"
343
+ end
344
+
345
+ # Parameters for /network:
346
+ # - socks: on/off/<[ipv4]:port>
347
+ # - socks-mode: always/onion
348
+ # - smp-proxy: always/unknown/unprotected/never
349
+ # - smp-proxy-fallback: no/protected/yes
350
+ # - timeout: <seconds>
351
+ def api_network(socks: nil, socks_mode: nil, smp_proxy: nil, smp_proxy_fallback: nil, timeout_secs: nil)
352
+ args = {
353
+ "socks" => socks,
354
+ "socks-mode" => socks_mode,
355
+ "smp-proxy" => smp_proxy,
356
+ "smp-proxy-fallback" => smp_proxy_fallback,
357
+ "timeout" => timeout_secs
358
+ }
359
+ command = '/network'
360
+ args.each do |param, value|
361
+ next if value == nil
362
+ command += " #{param}=#{value}"
363
+ end
364
+ resp = send_command command
365
+ resp_type = resp["type"]
366
+ raise "Unexpected response: #{resp_type}" if resp_type != "networkConfig"
367
+
368
+ resp["networkConfig"]
369
+ end
370
+
311
371
  private
312
372
 
313
373
  def next_corr_id
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplex-chat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rdbo
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-23 00:00:00.000000000 Z
10
+ date: 2025-03-01 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: websocket