yaic 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 +4 -4
- data/.claude/ralph/prompt.md +0 -2
- data/.ruby-version +1 -1
- data/CHANGELOG.md +9 -0
- data/CLAUDE.md +6 -1
- data/README.md +13 -2
- data/devenv.lock +2 -2
- data/lib/yaic/client.rb +202 -41
- data/lib/yaic/message_splitter.rb +51 -0
- data/lib/yaic/socket.rb +60 -28
- data/lib/yaic/version.rb +1 -1
- data/lib/yaic.rb +1 -0
- metadata +4 -4
- data/.claude/agents/ralph-qa.md +0 -101
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dfc41afda11f1d5bd34a01d8bb9ef2b279957c0e9b47c086c123774e884223e9
|
|
4
|
+
data.tar.gz: 6ffcbdd124ad0e8404d2053c451571b97238341f3b99f8faed8c4eb2e27c092e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bd28937767960a33e30cdd3b5940b70fdbe9d4c6534ef17007f666d0957fe92fb29559e18c2a1e1ca01b8509b4f2381bad7fe3ea1eef86ffde069f6ce7e750b8
|
|
7
|
+
data.tar.gz: 14f2ac257c1758b333345b30f905a0458e07b927ff6344fe7375fee2ff9f5585f05be199bbc41c2dcd35189822739923ca8204cee561e35f19cb762aefaeee6c
|
data/.claude/ralph/prompt.md
CHANGED
|
@@ -131,8 +131,6 @@ When feature is done, tests pass, and **QA has approved**:
|
|
|
131
131
|
.claude/ralph/bin/kill-claude
|
|
132
132
|
```
|
|
133
133
|
|
|
134
|
-
**IMPORTANT**: The kill script must be run with the sandbox disabled (`dangerouslyDisableSandbox: true`) because it needs to send signals to processes.
|
|
135
|
-
|
|
136
134
|
The loop will restart you with fresh context.
|
|
137
135
|
|
|
138
136
|
**CRITICAL: NEVER call the exit script if you are blocked or have a question. Use `AskUserQuestion` instead and wait for the human to respond.**
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
3.4.
|
|
1
|
+
3.4.13
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.3.0] - 2026-12-02
|
|
4
|
+
|
|
5
|
+
- Detect dropped connections: socket errors, EOF, server `ERROR`, and unanswered keepalive `PING`s move the client to `:disconnected` and emit `:disconnect` with a `reason`
|
|
6
|
+
- Add `stale_timeout:` option and `Client#disconnect_reason`
|
|
7
|
+
- Writes on a lost connection raise `Yaic::ConnectionError`; pending operations fail fast instead of timing out
|
|
8
|
+
- `quit` no longer raises on a dead connection and emits `:disconnect` exactly once
|
|
9
|
+
- Socket writes are written completely (partial writes handled) with a write timeout instead of an unflushed queue
|
|
10
|
+
- The read loop waits for socket readability instead of polling every millisecond
|
|
11
|
+
|
|
3
12
|
## [0.1.0] - 2025-11-28
|
|
4
13
|
|
|
5
14
|
- Initial release
|
data/CLAUDE.md
CHANGED
|
@@ -28,6 +28,11 @@ bundle exec standardrb -A # Run linter with auto-fix
|
|
|
28
28
|
bin/brakeman # Run security scanner
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
### Releasing
|
|
32
|
+
Releases are automated by `.github/workflows/release.yml` (RubyGems trusted publishing, no API key). To release:
|
|
33
|
+
1. Bump `Yaic::VERSION` in `lib/yaic/version.rb`, run `bundle install` so `Gemfile.lock` matches, and move the `[Unreleased]` CHANGELOG entries under the new version.
|
|
34
|
+
2. Merge to `main`. Once the `Ruby` CI workflow passes on that commit, the release workflow tags it `vX.Y.Z` and pushes the gem. Commits that don't change the version are skipped.
|
|
35
|
+
|
|
31
36
|
### Integration Test Setup
|
|
32
37
|
Integration tests require a running IRC server (InspIRCd via Docker):
|
|
33
38
|
```bash
|
|
@@ -57,7 +62,7 @@ client.on(:message) { |event| handle_message(event) }
|
|
|
57
62
|
client.on(:join) { |event| handle_join(event) }
|
|
58
63
|
```
|
|
59
64
|
|
|
60
|
-
Events: `:raw`, `:connect`, `:disconnect`, `:message`, `:notice`, `:join`, `:part`, `:quit`, `:kick`, `:nick`, `:topic`, `:mode`, `:names`, `:who`, `:whois`, `:error`
|
|
65
|
+
Events: `:raw`, `:connect`, `:disconnect`, `:message`, `:notice`, `:join`, `:part`, `:quit`, `:kick`, `:nick`, `:topic`, `:mode`, `:names`, `:who`, `:whois`, `:ison`, `:error`
|
|
61
66
|
|
|
62
67
|
### Test Structure
|
|
63
68
|
|
data/README.md
CHANGED
|
@@ -57,7 +57,7 @@ client.off(:message)
|
|
|
57
57
|
|-------|------------|
|
|
58
58
|
| `:raw` | `message` - Raw IRC message |
|
|
59
59
|
| `:connect` | `server` - Server name |
|
|
60
|
-
| `:disconnect` | - |
|
|
60
|
+
| `:disconnect` | `reason` - Quit message, server `ERROR` text, socket error, or ping timeout |
|
|
61
61
|
| `:message` | `source`, `target`, `text` |
|
|
62
62
|
| `:notice` | `source`, `target`, `text` |
|
|
63
63
|
| `:join` | `channel`, `user` |
|
|
@@ -70,7 +70,7 @@ client.off(:message)
|
|
|
70
70
|
| `:names` | `channel`, `users` |
|
|
71
71
|
| `:who` | `channel`, `user`, `host`, `server`, `nick`, `away`, `realname` |
|
|
72
72
|
| `:whois` | `result` (WhoisResult object) |
|
|
73
|
-
| `:error` | `numeric`, `message` |
|
|
73
|
+
| `:error` | `numeric`, `message` (or `exception` for unexpected internal errors) |
|
|
74
74
|
|
|
75
75
|
## Commands
|
|
76
76
|
|
|
@@ -99,6 +99,17 @@ client.whois("nick") # WHOIS query
|
|
|
99
99
|
client.names("#channel") # NAMES query
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
+
## Connection Loss
|
|
103
|
+
|
|
104
|
+
The client notices when the connection dies: the server closes it, a socket error occurs, the server sends `ERROR`, or a keepalive `PING` goes unanswered. After `stale_timeout / 2` seconds without traffic (default `stale_timeout` is 180) the client sends a `PING`; if nothing comes back within another `stale_timeout / 2`, the connection is considered lost.
|
|
105
|
+
|
|
106
|
+
When that happens the client moves to `:disconnected`, emits `:disconnect` with a `reason`, and exposes it through `client.disconnect_reason`. Commands on a lost connection raise `Yaic::ConnectionError`, and pending operations (`join`, `who`, `ison`, ...) fail immediately with it instead of waiting for their timeout.
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
client = Yaic::Client.new(server: "irc.libera.chat", port: 6697, ssl: true, nickname: "mynick", stale_timeout: 120)
|
|
110
|
+
client.on(:disconnect) { |event| puts "Disconnected: #{event.reason}" }
|
|
111
|
+
```
|
|
112
|
+
|
|
102
113
|
## Threading
|
|
103
114
|
|
|
104
115
|
The client spawns a background thread to read incoming messages. Event handlers are called from this thread. All public methods are thread-safe.
|
data/devenv.lock
CHANGED
|
@@ -126,10 +126,10 @@
|
|
|
126
126
|
"nixpkgs": "nixpkgs_2"
|
|
127
127
|
},
|
|
128
128
|
"locked": {
|
|
129
|
-
"lastModified":
|
|
129
|
+
"lastModified": 1795641025,
|
|
130
130
|
"owner": "bobvanderlinden",
|
|
131
131
|
"repo": "nixpkgs-ruby",
|
|
132
|
-
"rev": "
|
|
132
|
+
"rev": "10ee5e2e77f3a21957c77379d5e98cb20036c78c",
|
|
133
133
|
"type": "github"
|
|
134
134
|
},
|
|
135
135
|
"original": {
|
data/lib/yaic/client.rb
CHANGED
|
@@ -5,6 +5,7 @@ module Yaic
|
|
|
5
5
|
STALE_TIMEOUT = 180
|
|
6
6
|
DEFAULT_CONNECT_TIMEOUT = 30
|
|
7
7
|
DEFAULT_OPERATION_TIMEOUT = 10
|
|
8
|
+
MAX_HOST_LENGTH = 63
|
|
8
9
|
|
|
9
10
|
EVENT_MAP = {
|
|
10
11
|
"PRIVMSG" => :message,
|
|
@@ -40,11 +41,15 @@ module Yaic
|
|
|
40
41
|
@monitor.synchronize { @last_received_at }
|
|
41
42
|
end
|
|
42
43
|
|
|
44
|
+
def disconnect_reason
|
|
45
|
+
@monitor.synchronize { @disconnect_reason }
|
|
46
|
+
end
|
|
47
|
+
|
|
43
48
|
def channels
|
|
44
49
|
@monitor.synchronize { @channels.dup }
|
|
45
50
|
end
|
|
46
51
|
|
|
47
|
-
def initialize(port:, host: nil, nick: nil, user: nil, realname: nil, password: nil, ssl: false, server: nil, nickname: nil, username: nil, verbose: false)
|
|
52
|
+
def initialize(port:, host: nil, nick: nil, user: nil, realname: nil, password: nil, ssl: false, verify_ssl: true, server: nil, nickname: nil, username: nil, verbose: false, stale_timeout: STALE_TIMEOUT)
|
|
48
53
|
@server = server || host
|
|
49
54
|
@port = port
|
|
50
55
|
@nick = nickname || nick
|
|
@@ -52,13 +57,18 @@ module Yaic
|
|
|
52
57
|
@realname = realname || @nick
|
|
53
58
|
@password = password
|
|
54
59
|
@ssl = ssl
|
|
60
|
+
@verify_ssl = verify_ssl
|
|
55
61
|
@verbose = verbose
|
|
62
|
+
@stale_timeout = stale_timeout
|
|
56
63
|
|
|
57
64
|
@socket = nil
|
|
58
65
|
@state = :disconnected
|
|
59
66
|
@isupport = {}
|
|
60
67
|
@nick_attempts = 0
|
|
61
68
|
@last_received_at = nil
|
|
69
|
+
@last_received_clock = nil
|
|
70
|
+
@ping_sent_clock = nil
|
|
71
|
+
@disconnect_reason = nil
|
|
62
72
|
@handlers = {}
|
|
63
73
|
@channels = {}
|
|
64
74
|
@pending_names = {}
|
|
@@ -66,6 +76,8 @@ module Yaic
|
|
|
66
76
|
@pending_whois_complete = {}
|
|
67
77
|
@pending_who_results = {}
|
|
68
78
|
@pending_who_complete = {}
|
|
79
|
+
@pending_ison = nil
|
|
80
|
+
@pending_ison_complete = false
|
|
69
81
|
@read_thread = nil
|
|
70
82
|
@monitor = Monitor.new
|
|
71
83
|
end
|
|
@@ -73,7 +85,11 @@ module Yaic
|
|
|
73
85
|
def connect(timeout: DEFAULT_CONNECT_TIMEOUT)
|
|
74
86
|
log "Connecting to #{@server}:#{@port}#{" (SSL)" if @ssl}..."
|
|
75
87
|
sock = @monitor.synchronize do
|
|
76
|
-
@
|
|
88
|
+
@disconnect_reason = nil
|
|
89
|
+
@last_received_at = nil
|
|
90
|
+
@last_received_clock = nil
|
|
91
|
+
@ping_sent_clock = nil
|
|
92
|
+
@socket ||= Socket.new(@server, @port, ssl: @ssl, verify_ssl: @verify_ssl)
|
|
77
93
|
end
|
|
78
94
|
sock.connect
|
|
79
95
|
send_registration
|
|
@@ -88,17 +104,21 @@ module Yaic
|
|
|
88
104
|
end
|
|
89
105
|
|
|
90
106
|
def disconnect
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
@read_thread&.join(1)
|
|
107
|
+
close_connection(nil)
|
|
108
|
+
join_read_thread
|
|
94
109
|
end
|
|
95
110
|
|
|
96
111
|
def handle_message(message)
|
|
97
|
-
@monitor.synchronize
|
|
112
|
+
@monitor.synchronize do
|
|
113
|
+
@last_received_at = Time.now
|
|
114
|
+
@last_received_clock = clock
|
|
115
|
+
end
|
|
98
116
|
|
|
99
117
|
case message.command
|
|
100
118
|
when "PING"
|
|
101
119
|
handle_ping(message)
|
|
120
|
+
when "ERROR"
|
|
121
|
+
handle_server_error(message)
|
|
102
122
|
when "001"
|
|
103
123
|
handle_rpl_welcome(message)
|
|
104
124
|
when "005"
|
|
@@ -145,6 +165,8 @@ module Yaic
|
|
|
145
165
|
handle_rpl_away(message)
|
|
146
166
|
when "318"
|
|
147
167
|
handle_rpl_endofwhois(message)
|
|
168
|
+
when "303"
|
|
169
|
+
handle_rpl_ison(message)
|
|
148
170
|
end
|
|
149
171
|
|
|
150
172
|
emit_events(message)
|
|
@@ -153,7 +175,7 @@ module Yaic
|
|
|
153
175
|
def connection_stale?
|
|
154
176
|
@monitor.synchronize do
|
|
155
177
|
return false if @last_received_at.nil?
|
|
156
|
-
Time.now - @last_received_at >
|
|
178
|
+
Time.now - @last_received_at > @stale_timeout
|
|
157
179
|
end
|
|
158
180
|
end
|
|
159
181
|
|
|
@@ -173,22 +195,32 @@ module Yaic
|
|
|
173
195
|
end
|
|
174
196
|
|
|
175
197
|
def privmsg(target, text)
|
|
176
|
-
|
|
177
|
-
|
|
198
|
+
max = MessageSplitter.max_text_bytes(line_length: line_length, command: "PRIVMSG", target: target, prefix_length: source_prefix_length)
|
|
199
|
+
parts = MessageSplitter.split(text, max_bytes: max)
|
|
200
|
+
parts.each do |part|
|
|
201
|
+
message = Message.new(command: "PRIVMSG", params: [target, part])
|
|
202
|
+
write(message.to_s)
|
|
203
|
+
end
|
|
204
|
+
parts
|
|
178
205
|
end
|
|
179
206
|
|
|
180
207
|
alias_method :msg, :privmsg
|
|
181
208
|
|
|
182
209
|
def notice(target, text)
|
|
183
|
-
|
|
184
|
-
|
|
210
|
+
max = MessageSplitter.max_text_bytes(line_length: line_length, command: "NOTICE", target: target, prefix_length: source_prefix_length)
|
|
211
|
+
parts = MessageSplitter.split(text, max_bytes: max)
|
|
212
|
+
parts.each do |part|
|
|
213
|
+
message = Message.new(command: "NOTICE", params: [target, part])
|
|
214
|
+
write(message.to_s)
|
|
215
|
+
end
|
|
216
|
+
parts
|
|
185
217
|
end
|
|
186
218
|
|
|
187
219
|
def join(channel, key = nil, timeout: DEFAULT_OPERATION_TIMEOUT)
|
|
188
220
|
log "Joining #{channel}..."
|
|
189
221
|
params = key ? [channel, key] : [channel]
|
|
190
222
|
message = Message.new(command: "JOIN", params: params)
|
|
191
|
-
|
|
223
|
+
write(message.to_s)
|
|
192
224
|
wait_until(timeout: timeout) { channel_joined?(channel) }
|
|
193
225
|
log "Joined #{channel}"
|
|
194
226
|
end
|
|
@@ -197,20 +229,22 @@ module Yaic
|
|
|
197
229
|
log "Parting #{channel}..."
|
|
198
230
|
params = reason ? [channel, reason] : [channel]
|
|
199
231
|
message = Message.new(command: "PART", params: params)
|
|
200
|
-
|
|
232
|
+
write(message.to_s)
|
|
201
233
|
wait_until(timeout: timeout) { !channel_joined?(channel) }
|
|
202
234
|
log "Parted #{channel}"
|
|
203
235
|
end
|
|
204
236
|
|
|
205
237
|
def quit(reason = nil)
|
|
238
|
+
return unless transition_to_disconnected(reason)
|
|
239
|
+
|
|
206
240
|
params = reason ? [reason] : []
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
241
|
+
begin
|
|
242
|
+
socket&.write(Message.new(command: "QUIT", params: params).to_s)
|
|
243
|
+
rescue ConnectionError
|
|
244
|
+
nil
|
|
245
|
+
end
|
|
246
|
+
join_read_thread
|
|
247
|
+
finish_disconnect(reason)
|
|
214
248
|
log "Disconnected"
|
|
215
249
|
end
|
|
216
250
|
|
|
@@ -219,25 +253,25 @@ module Yaic
|
|
|
219
253
|
|
|
220
254
|
old_nick = @monitor.synchronize { @nick }
|
|
221
255
|
message = Message.new(command: "NICK", params: [new_nick])
|
|
222
|
-
|
|
256
|
+
write(message.to_s)
|
|
223
257
|
wait_until(timeout: timeout) { @monitor.synchronize { @nick } != old_nick }
|
|
224
258
|
end
|
|
225
259
|
|
|
226
260
|
def topic(channel, new_topic = nil)
|
|
227
261
|
params = new_topic.nil? ? [channel] : [channel, new_topic]
|
|
228
262
|
message = Message.new(command: "TOPIC", params: params)
|
|
229
|
-
|
|
263
|
+
write(message.to_s)
|
|
230
264
|
end
|
|
231
265
|
|
|
232
266
|
def kick(channel, nick, reason = nil)
|
|
233
267
|
params = reason ? [channel, nick, reason] : [channel, nick]
|
|
234
268
|
message = Message.new(command: "KICK", params: params)
|
|
235
|
-
|
|
269
|
+
write(message.to_s)
|
|
236
270
|
end
|
|
237
271
|
|
|
238
272
|
def names(channel)
|
|
239
273
|
message = Message.new(command: "NAMES", params: [channel])
|
|
240
|
-
|
|
274
|
+
write(message.to_s)
|
|
241
275
|
end
|
|
242
276
|
|
|
243
277
|
def mode(target, modes = nil, *args)
|
|
@@ -245,7 +279,7 @@ module Yaic
|
|
|
245
279
|
params << modes if modes
|
|
246
280
|
params.concat(args) unless args.empty?
|
|
247
281
|
message = Message.new(command: "MODE", params: params)
|
|
248
|
-
|
|
282
|
+
write(message.to_s)
|
|
249
283
|
end
|
|
250
284
|
|
|
251
285
|
def who(mask, timeout: DEFAULT_OPERATION_TIMEOUT)
|
|
@@ -256,7 +290,7 @@ module Yaic
|
|
|
256
290
|
end
|
|
257
291
|
|
|
258
292
|
message = Message.new(command: "WHO", params: [mask])
|
|
259
|
-
|
|
293
|
+
write(message.to_s)
|
|
260
294
|
|
|
261
295
|
wait_until(timeout: timeout) { @monitor.synchronize { @pending_who_complete[mask] } }
|
|
262
296
|
|
|
@@ -274,7 +308,7 @@ module Yaic
|
|
|
274
308
|
@monitor.synchronize { @pending_whois_complete[nick] = false }
|
|
275
309
|
|
|
276
310
|
message = Message.new(command: "WHOIS", params: [nick])
|
|
277
|
-
|
|
311
|
+
write(message.to_s)
|
|
278
312
|
|
|
279
313
|
wait_until(timeout: timeout) { @monitor.synchronize { @pending_whois_complete[nick] } }
|
|
280
314
|
|
|
@@ -288,7 +322,31 @@ module Yaic
|
|
|
288
322
|
end
|
|
289
323
|
|
|
290
324
|
def raw(command)
|
|
291
|
-
|
|
325
|
+
write(command)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def ison(*nicks, timeout: DEFAULT_OPERATION_TIMEOUT)
|
|
329
|
+
nicks = nicks.flatten
|
|
330
|
+
return [] if nicks.empty?
|
|
331
|
+
|
|
332
|
+
log "Sending ISON #{nicks.join(" ")}..."
|
|
333
|
+
@monitor.synchronize do
|
|
334
|
+
@pending_ison = nil
|
|
335
|
+
@pending_ison_complete = false
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
message = Message.new(command: "ISON", params: [nicks.join(" ")])
|
|
339
|
+
write(message.to_s)
|
|
340
|
+
|
|
341
|
+
wait_until(timeout: timeout) { @monitor.synchronize { @pending_ison_complete } }
|
|
342
|
+
|
|
343
|
+
@monitor.synchronize do
|
|
344
|
+
result = @pending_ison || []
|
|
345
|
+
log "ISON complete (#{result.size} online)"
|
|
346
|
+
result
|
|
347
|
+
end
|
|
348
|
+
ensure
|
|
349
|
+
@monitor.synchronize { @pending_ison_complete = false }
|
|
292
350
|
end
|
|
293
351
|
|
|
294
352
|
private
|
|
@@ -297,6 +355,19 @@ module Yaic
|
|
|
297
355
|
@monitor.synchronize { @socket }
|
|
298
356
|
end
|
|
299
357
|
|
|
358
|
+
def line_length
|
|
359
|
+
@monitor.synchronize do
|
|
360
|
+
value = @isupport["LINELEN"]
|
|
361
|
+
value ? value.to_i : 512
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
def source_prefix_length
|
|
366
|
+
@monitor.synchronize do
|
|
367
|
+
@nick.bytesize + @user.bytesize + MAX_HOST_LENGTH + 4
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
300
371
|
def log(message)
|
|
301
372
|
return unless @verbose
|
|
302
373
|
warn "[YAIC] #{message}"
|
|
@@ -313,15 +384,62 @@ module Yaic
|
|
|
313
384
|
def wait_until(timeout:)
|
|
314
385
|
deadline = Time.now + timeout
|
|
315
386
|
until yield
|
|
387
|
+
raise ConnectionError, disconnect_reason || "Not connected" if state == :disconnected
|
|
316
388
|
raise Yaic::TimeoutError, "Operation timed out after #{timeout} seconds" if Time.now > deadline
|
|
317
389
|
sleep 0.01
|
|
318
390
|
end
|
|
319
391
|
end
|
|
320
392
|
|
|
393
|
+
def write(line)
|
|
394
|
+
sock = socket
|
|
395
|
+
raise ConnectionError, "Not connected" unless sock
|
|
396
|
+
|
|
397
|
+
sock.write(line)
|
|
398
|
+
rescue ConnectionError => e
|
|
399
|
+
connection_lost(e.message)
|
|
400
|
+
raise
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def clock
|
|
404
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
def transition_to_disconnected(reason)
|
|
408
|
+
@monitor.synchronize do
|
|
409
|
+
return false if @state == :disconnected
|
|
410
|
+
|
|
411
|
+
@state = :disconnected
|
|
412
|
+
@disconnect_reason = reason
|
|
413
|
+
true
|
|
414
|
+
end
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
def finish_disconnect(reason)
|
|
418
|
+
socket&.disconnect
|
|
419
|
+
@monitor.synchronize { @channels.clear }
|
|
420
|
+
emit(:disconnect, nil, reason: reason)
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
def close_connection(reason)
|
|
424
|
+
return false unless transition_to_disconnected(reason)
|
|
425
|
+
|
|
426
|
+
finish_disconnect(reason)
|
|
427
|
+
true
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def connection_lost(reason)
|
|
431
|
+
log "Connection lost: #{reason}" if close_connection(reason)
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def join_read_thread
|
|
435
|
+
thread = @read_thread
|
|
436
|
+
thread.join(1) if thread && thread != Thread.current
|
|
437
|
+
end
|
|
438
|
+
|
|
321
439
|
def start_read_loop
|
|
322
440
|
@read_thread = Thread.new do
|
|
323
441
|
loop do
|
|
324
|
-
break if
|
|
442
|
+
break if state == :disconnected
|
|
325
443
|
process_incoming
|
|
326
444
|
end
|
|
327
445
|
end
|
|
@@ -333,19 +451,47 @@ module Yaic
|
|
|
333
451
|
message = Message.parse(raw)
|
|
334
452
|
handle_message(message) if message
|
|
335
453
|
else
|
|
336
|
-
|
|
454
|
+
check_keepalive
|
|
455
|
+
socket.wait_readable(keepalive_tick)
|
|
337
456
|
end
|
|
457
|
+
rescue ConnectionError => e
|
|
458
|
+
connection_lost(e.message)
|
|
338
459
|
rescue => e
|
|
339
460
|
emit(:error, nil, exception: e)
|
|
340
461
|
end
|
|
341
462
|
|
|
463
|
+
def keepalive_tick
|
|
464
|
+
[@stale_timeout / 4.0, 1].min
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
def check_keepalive
|
|
468
|
+
action = @monitor.synchronize do
|
|
469
|
+
next if @state != :connected || @last_received_clock.nil?
|
|
470
|
+
|
|
471
|
+
now = clock
|
|
472
|
+
half = @stale_timeout / 2.0
|
|
473
|
+
if @ping_sent_clock && @ping_sent_clock > @last_received_clock
|
|
474
|
+
:timeout if now - @ping_sent_clock > half
|
|
475
|
+
elsif now - @last_received_clock > half
|
|
476
|
+
@ping_sent_clock = now
|
|
477
|
+
:ping
|
|
478
|
+
end
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
case action
|
|
482
|
+
when :ping
|
|
483
|
+
write(Message.new(command: "PING", params: ["yaic"]).to_s)
|
|
484
|
+
when :timeout
|
|
485
|
+
connection_lost("Ping timeout: no response from server in #{@stale_timeout} seconds")
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
342
489
|
def send_registration
|
|
343
|
-
sock = socket
|
|
344
490
|
if @password
|
|
345
|
-
|
|
491
|
+
write(Registration.pass_message(@password).to_s)
|
|
346
492
|
end
|
|
347
|
-
|
|
348
|
-
|
|
493
|
+
write(Registration.nick_message(@nick).to_s)
|
|
494
|
+
write(Registration.user_message(@user, @realname).to_s)
|
|
349
495
|
end
|
|
350
496
|
|
|
351
497
|
def handle_rpl_welcome(message)
|
|
@@ -371,20 +517,23 @@ module Yaic
|
|
|
371
517
|
end
|
|
372
518
|
|
|
373
519
|
def handle_err_nicknameinuse(_message)
|
|
374
|
-
|
|
520
|
+
new_nick = @monitor.synchronize do
|
|
521
|
+
next unless @state == :registering
|
|
375
522
|
|
|
376
|
-
@monitor.synchronize do
|
|
377
523
|
@nick_attempts += 1
|
|
378
|
-
|
|
379
|
-
@nick = new_nick
|
|
380
|
-
socket.write(Registration.nick_message(@nick).to_s)
|
|
524
|
+
@nick = "#{@nick.sub(/_+$/, "")}#{"_" * @nick_attempts}"
|
|
381
525
|
end
|
|
526
|
+
write(Registration.nick_message(new_nick).to_s) if new_nick
|
|
382
527
|
end
|
|
383
528
|
|
|
384
529
|
def handle_ping(message)
|
|
385
530
|
token = message.params[0]
|
|
386
531
|
pong = Message.new(command: "PONG", params: [token])
|
|
387
|
-
|
|
532
|
+
write(pong.to_s)
|
|
533
|
+
end
|
|
534
|
+
|
|
535
|
+
def handle_server_error(message)
|
|
536
|
+
connection_lost(message.params.last || "Server closed the connection")
|
|
388
537
|
end
|
|
389
538
|
|
|
390
539
|
def handle_join(message)
|
|
@@ -722,6 +871,18 @@ module Yaic
|
|
|
722
871
|
emit(:whois, message, result: result)
|
|
723
872
|
end
|
|
724
873
|
|
|
874
|
+
def handle_rpl_ison(message)
|
|
875
|
+
nicks_str = message.params[1] || ""
|
|
876
|
+
online_nicks = nicks_str.split
|
|
877
|
+
|
|
878
|
+
@monitor.synchronize do
|
|
879
|
+
@pending_ison = online_nicks
|
|
880
|
+
@pending_ison_complete = true
|
|
881
|
+
end
|
|
882
|
+
|
|
883
|
+
emit(:ison, message, nicks: online_nicks)
|
|
884
|
+
end
|
|
885
|
+
|
|
725
886
|
def apply_user_mode(channel, nick, mode_char, adding)
|
|
726
887
|
return unless channel.users.key?(nick)
|
|
727
888
|
|
|
@@ -812,7 +973,7 @@ module Yaic
|
|
|
812
973
|
when :mode
|
|
813
974
|
{target: message.params[0], modes: message.params[1], args: message.params[2..]}
|
|
814
975
|
when :error
|
|
815
|
-
{numeric: message.command.to_i, message: message.params.last}
|
|
976
|
+
{numeric: message.command.to_i, message: message.params.last, params: message.params}
|
|
816
977
|
else
|
|
817
978
|
{}
|
|
818
979
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Yaic
|
|
4
|
+
module MessageSplitter
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def max_text_bytes(line_length:, command:, target:, prefix_length: 0)
|
|
8
|
+
overhead = command.bytesize + 1 + target.bytesize + 2 + 2 + prefix_length
|
|
9
|
+
result = line_length - overhead
|
|
10
|
+
[result, 1].max
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def split(text, max_bytes:)
|
|
14
|
+
text = text.to_s
|
|
15
|
+
return [text] if text.bytesize <= max_bytes
|
|
16
|
+
|
|
17
|
+
chunks = []
|
|
18
|
+
remaining = text.dup
|
|
19
|
+
while remaining.bytesize > 0
|
|
20
|
+
if remaining.bytesize <= max_bytes
|
|
21
|
+
chunks << remaining
|
|
22
|
+
break
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
truncated = safe_truncate(remaining, max_bytes)
|
|
26
|
+
break if truncated.empty?
|
|
27
|
+
|
|
28
|
+
if remaining[truncated.length] == " "
|
|
29
|
+
chunks << truncated
|
|
30
|
+
remaining = remaining[(truncated.length + 1)..]
|
|
31
|
+
elsif (space_idx = truncated.rindex(" "))
|
|
32
|
+
chunks << truncated[0...space_idx]
|
|
33
|
+
remaining = remaining[(space_idx + 1)..]
|
|
34
|
+
else
|
|
35
|
+
chunks << truncated
|
|
36
|
+
remaining = remaining[truncated.length..]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
chunks
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def safe_truncate(text, max_bytes)
|
|
43
|
+
return text if text.bytesize <= max_bytes
|
|
44
|
+
truncated = text.byteslice(0, max_bytes)
|
|
45
|
+
while truncated.bytesize > 0 && !truncated.valid_encoding?
|
|
46
|
+
truncated = truncated.byteslice(0, truncated.bytesize - 1)
|
|
47
|
+
end
|
|
48
|
+
truncated
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/yaic/socket.rb
CHANGED
|
@@ -2,23 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
require "socket"
|
|
4
4
|
require "openssl"
|
|
5
|
+
require "io/wait"
|
|
5
6
|
|
|
6
7
|
module Yaic
|
|
7
8
|
class Socket
|
|
9
|
+
CONNECTION_ERRORS = [IOError, SystemCallError, OpenSSL::SSL::SSLError].freeze
|
|
10
|
+
DEFAULT_WRITE_TIMEOUT = 5
|
|
11
|
+
|
|
8
12
|
def state
|
|
9
13
|
@monitor.synchronize { @state }
|
|
10
14
|
end
|
|
11
15
|
|
|
12
|
-
def initialize(host, port, ssl: false,
|
|
16
|
+
def initialize(host, port, ssl: false, verify_ssl: true, connect_timeout: 30, write_timeout: DEFAULT_WRITE_TIMEOUT)
|
|
13
17
|
@host = host
|
|
14
18
|
@port = port
|
|
15
19
|
@ssl = ssl
|
|
16
|
-
@verify_mode =
|
|
20
|
+
@verify_mode = verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
|
|
17
21
|
@connect_timeout = connect_timeout
|
|
22
|
+
@write_timeout = write_timeout
|
|
18
23
|
|
|
19
24
|
@socket = nil
|
|
20
25
|
@read_buffer = String.new(encoding: Encoding::ASCII_8BIT)
|
|
21
|
-
@write_queue = []
|
|
22
26
|
@state = :disconnected
|
|
23
27
|
@monitor = Monitor.new
|
|
24
28
|
end
|
|
@@ -46,7 +50,6 @@ module Yaic
|
|
|
46
50
|
end
|
|
47
51
|
@socket = nil
|
|
48
52
|
@read_buffer.clear
|
|
49
|
-
@write_queue.clear
|
|
50
53
|
@state = :disconnected
|
|
51
54
|
end
|
|
52
55
|
end
|
|
@@ -59,27 +62,34 @@ module Yaic
|
|
|
59
62
|
data = @socket.read_nonblock(4096)
|
|
60
63
|
buffer_data(data)
|
|
61
64
|
extract_message
|
|
62
|
-
rescue IO::WaitReadable
|
|
65
|
+
rescue IO::WaitReadable, IO::WaitWritable
|
|
63
66
|
extract_message
|
|
64
|
-
rescue
|
|
65
|
-
|
|
67
|
+
rescue *CONNECTION_ERRORS => e
|
|
68
|
+
extract_message || raise(ConnectionError, describe(e))
|
|
66
69
|
end
|
|
67
70
|
end
|
|
68
71
|
end
|
|
69
72
|
|
|
73
|
+
def wait_readable(timeout)
|
|
74
|
+
io = @monitor.synchronize do
|
|
75
|
+
return true if @socket.nil? || @read_buffer.include?("\n")
|
|
76
|
+
return true if @socket.respond_to?(:pending) && @socket.pending.positive?
|
|
77
|
+
|
|
78
|
+
@socket.to_io
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
!!io.wait_readable(timeout)
|
|
82
|
+
rescue IOError
|
|
83
|
+
true
|
|
84
|
+
end
|
|
85
|
+
|
|
70
86
|
def write(message)
|
|
71
87
|
@monitor.synchronize do
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
message = message.dup
|
|
75
|
-
message << "\r\n" unless message.end_with?("\r\n", "\n")
|
|
88
|
+
raise ConnectionError, "Not connected" if @socket.nil?
|
|
76
89
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
@write_queue << message
|
|
81
|
-
flush_write_queue
|
|
82
|
-
end
|
|
90
|
+
data = message.to_s.b
|
|
91
|
+
data << "\r\n" unless data.end_with?("\r\n", "\n")
|
|
92
|
+
write_fully(data)
|
|
83
93
|
end
|
|
84
94
|
end
|
|
85
95
|
|
|
@@ -96,6 +106,39 @@ module Yaic
|
|
|
96
106
|
ssl_socket
|
|
97
107
|
end
|
|
98
108
|
|
|
109
|
+
def write_fully(data)
|
|
110
|
+
deadline = monotonic_now + @write_timeout
|
|
111
|
+
until data.empty?
|
|
112
|
+
begin
|
|
113
|
+
written = @socket.write_nonblock(data)
|
|
114
|
+
data = data.byteslice(written..)
|
|
115
|
+
rescue IO::WaitWritable
|
|
116
|
+
wait_until_ready(:wait_writable, deadline)
|
|
117
|
+
rescue IO::WaitReadable
|
|
118
|
+
wait_until_ready(:wait_readable, deadline)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
rescue *CONNECTION_ERRORS => e
|
|
122
|
+
raise ConnectionError, describe(e)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def wait_until_ready(method, deadline)
|
|
126
|
+
remaining = deadline - monotonic_now
|
|
127
|
+
return if remaining.positive? && @socket.to_io.public_send(method, remaining)
|
|
128
|
+
|
|
129
|
+
raise ConnectionError, "Write timed out after #{@write_timeout} seconds"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def describe(error)
|
|
133
|
+
return "Connection closed by server" if error.is_a?(EOFError)
|
|
134
|
+
|
|
135
|
+
"#{error.class}: #{error.message}"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def monotonic_now
|
|
139
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
140
|
+
end
|
|
141
|
+
|
|
99
142
|
def buffer_data(data)
|
|
100
143
|
@read_buffer << data
|
|
101
144
|
end
|
|
@@ -105,16 +148,5 @@ module Yaic
|
|
|
105
148
|
@read_buffer.slice!(0..idx)
|
|
106
149
|
end
|
|
107
150
|
end
|
|
108
|
-
|
|
109
|
-
def flush_write_queue
|
|
110
|
-
while @write_queue.any?
|
|
111
|
-
begin
|
|
112
|
-
@socket.write_nonblock(@write_queue.first)
|
|
113
|
-
@write_queue.shift
|
|
114
|
-
rescue IO::WaitWritable
|
|
115
|
-
break
|
|
116
|
-
end
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
151
|
end
|
|
120
152
|
end
|
data/lib/yaic/version.rb
CHANGED
data/lib/yaic.rb
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: yaic
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Joé Dupuis
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 1980-01-
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
email:
|
|
13
13
|
- joe@dupuis.io
|
|
@@ -15,7 +15,6 @@ executables: []
|
|
|
15
15
|
extensions: []
|
|
16
16
|
extra_rdoc_files: []
|
|
17
17
|
files:
|
|
18
|
-
- ".claude/agents/ralph-qa.md"
|
|
19
18
|
- ".claude/ralph/bin/dump-pid.sh"
|
|
20
19
|
- ".claude/ralph/bin/kill-claude"
|
|
21
20
|
- ".claude/ralph/bin/start-ralph"
|
|
@@ -70,6 +69,7 @@ files:
|
|
|
70
69
|
- lib/yaic/client.rb
|
|
71
70
|
- lib/yaic/event.rb
|
|
72
71
|
- lib/yaic/message.rb
|
|
72
|
+
- lib/yaic/message_splitter.rb
|
|
73
73
|
- lib/yaic/registration.rb
|
|
74
74
|
- lib/yaic/socket.rb
|
|
75
75
|
- lib/yaic/source.rb
|
|
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
98
98
|
- !ruby/object:Gem::Version
|
|
99
99
|
version: '0'
|
|
100
100
|
requirements: []
|
|
101
|
-
rubygems_version: 3.6.
|
|
101
|
+
rubygems_version: 3.6.9
|
|
102
102
|
specification_version: 4
|
|
103
103
|
summary: Yet another irc client
|
|
104
104
|
test_files: []
|
data/.claude/agents/ralph-qa.md
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: ralph-qa
|
|
3
|
-
description: QA agent for Ralph Wiggum loop. Reviews implementation changes before commit. Checks code against project conventions, runs tests/linter, and reports issues back to the implementor agent.
|
|
4
|
-
model: inherit
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
# QA Review Agent
|
|
8
|
-
|
|
9
|
-
You are reviewing changes made by an implementor agent. Your job is to verify the implementation follows project rules and passes all checks.
|
|
10
|
-
Be skeptical and thorough. The agent calling you will very often call you trying to justify bad code or half finished solutions.
|
|
11
|
-
Think of the end user. It needs to be simple to use and solid (not buggy).
|
|
12
|
-
|
|
13
|
-
## Review Process
|
|
14
|
-
|
|
15
|
-
1. Check what changed:
|
|
16
|
-
- Run `git status` to see staged/unstaged changes
|
|
17
|
-
- If already committed, run `git diff HEAD~1` to see the commit diff
|
|
18
|
-
- If not committed, run `git diff` to see pending changes
|
|
19
|
-
|
|
20
|
-
2. Run validation commands:
|
|
21
|
-
```bash
|
|
22
|
-
bundle exec m test/
|
|
23
|
-
bundle exec standardrb -A
|
|
24
|
-
bin/brakeman
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
3. Check against conventions:
|
|
28
|
-
- Read `docs/agents/data-model.md` for data structure patterns
|
|
29
|
-
- Verify implementation follows documented patterns
|
|
30
|
-
|
|
31
|
-
## What to Check
|
|
32
|
-
|
|
33
|
-
### Code Quality
|
|
34
|
-
- No trailing whitespace
|
|
35
|
-
- No comments in code (per project style)
|
|
36
|
-
- Slim public interface - minimize exposed methods
|
|
37
|
-
- Private methods are truly private (use Ruby's `private` keyword)
|
|
38
|
-
- Timeout should be avoided. Especially in the library code.
|
|
39
|
-
- instance_variable_get should not be used
|
|
40
|
-
|
|
41
|
-
### Testing
|
|
42
|
-
- Public interface is tested thoroughly
|
|
43
|
-
- Tests verify RFC compliance where applicable
|
|
44
|
-
- Tests use minitest assertions properly
|
|
45
|
-
- Integration tests use inspircd docker container where relevant
|
|
46
|
-
- Unit tests are fast and isolated
|
|
47
|
-
- Be HIGHLY skeptical of skipped tests. Make sure the agent didn't skip test needlessly. If the agent skipped a test because it is not able to implement the test it should reach out with the ask question tool. Skipping test should be for feature we're gonna implement later. Not because it's hard to make the test pass or some dependency is missing. If a test doesn't pass because say a dependency is unreachable, the agent should ask the user a question using the ask question tool.
|
|
48
|
-
- Assume the test server is already running unless you see the test fail because of it.
|
|
49
|
-
|
|
50
|
-
### Test Optimization Specific (for test speedup tasks)
|
|
51
|
-
- **CRITICAL**: No tests should be removed or skipped to make things faster
|
|
52
|
-
- Test count must remain the same or increase
|
|
53
|
-
- Assertions should not be weakened
|
|
54
|
-
- If a test is refactored, it must test the same behavior
|
|
55
|
-
- Ideally no sleep or timeout. If they can be removed without hurting readability that's preferable.
|
|
56
|
-
- Any mocking introduced must still test the same behavior
|
|
57
|
-
- Verify the optimization actually reduces test time
|
|
58
|
-
|
|
59
|
-
### RFC Compliance
|
|
60
|
-
- Message parsing follows IRC protocol spec
|
|
61
|
-
- Numeric codes match documented values
|
|
62
|
-
- Event names and payloads match feature specs
|
|
63
|
-
- Edge cases from RFC are handled (empty params, special chars)
|
|
64
|
-
- If you need to check the RFC you can find it here: docs/agents/rfc/modern-irc/
|
|
65
|
-
|
|
66
|
-
### Linting
|
|
67
|
-
- standardrb passes with no errors
|
|
68
|
-
- Run with `-A` to auto-fix where possible
|
|
69
|
-
|
|
70
|
-
### Security
|
|
71
|
-
- brakeman passes with no warnings
|
|
72
|
-
- Run `bin/brakeman` to check for security issues
|
|
73
|
-
|
|
74
|
-
## Running Tests
|
|
75
|
-
|
|
76
|
-
```bash
|
|
77
|
-
bundle exec m test/ # Run all tests
|
|
78
|
-
bundle exec m test/unit/ # Run unit tests only
|
|
79
|
-
bundle exec m test/integration/ # Run integration tests only
|
|
80
|
-
bundle exec m test/file_test.rb # Run specific file
|
|
81
|
-
bundle exec m test/file_test.rb:42 # Run specific test by line
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
## Tips
|
|
85
|
-
- While the test might not be parallelizable yet, it should be fine to run test, linting and security check in parallel.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
## Reporting Back
|
|
90
|
-
|
|
91
|
-
Report to the implementor agent:
|
|
92
|
-
|
|
93
|
-
**If compliant:**
|
|
94
|
-
> QA PASSED. All checks passed, code follows conventions.
|
|
95
|
-
|
|
96
|
-
**If issues found:**
|
|
97
|
-
> QA FAILED. Issues found:
|
|
98
|
-
> - [Issue 1]: [Description]. [How to fix].
|
|
99
|
-
> - [Issue 2]: [Description]. [How to fix].
|
|
100
|
-
>
|
|
101
|
-
> Required fixes: [list specific changes needed]
|