yaic 0.1.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.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.claude/ralph/bin/dump-pid.sh +3 -0
  3. data/.claude/ralph/bin/kill-claude +6 -0
  4. data/.claude/ralph/bin/start-ralph +44 -0
  5. data/.claude/ralph/bin/stop-hook.sh +9 -0
  6. data/.claude/ralph/bin/stop-ralph +17 -0
  7. data/.claude/ralph/prompt.md +216 -0
  8. data/.claude/settings.json +26 -0
  9. data/.gitmodules +3 -0
  10. data/.ruby-version +1 -1
  11. data/CHANGELOG.md +9 -0
  12. data/CLAUDE.md +70 -0
  13. data/README.md +117 -17
  14. data/Rakefile +8 -0
  15. data/devenv.lock +2 -2
  16. data/devenv.nix +1 -0
  17. data/docs/agents/data-model.md +150 -0
  18. data/docs/agents/ralph/features/01-message-parsing.md.done +160 -0
  19. data/docs/agents/ralph/features/01-tcpsocket-refactor.md.done +109 -0
  20. data/docs/agents/ralph/features/02-connection-socket.md.done +138 -0
  21. data/docs/agents/ralph/features/02-simplified-client-api.md.done +306 -0
  22. data/docs/agents/ralph/features/03-registration.md.done +147 -0
  23. data/docs/agents/ralph/features/04-ping-pong.md.done +109 -0
  24. data/docs/agents/ralph/features/05-event-system.md.done +167 -0
  25. data/docs/agents/ralph/features/06-privmsg-notice.md.done +163 -0
  26. data/docs/agents/ralph/features/07-join-part.md.done +190 -0
  27. data/docs/agents/ralph/features/08-quit.md.done +118 -0
  28. data/docs/agents/ralph/features/09-nick-change.md.done +109 -0
  29. data/docs/agents/ralph/features/10-topic.md.done +145 -0
  30. data/docs/agents/ralph/features/11-kick.md.done +122 -0
  31. data/docs/agents/ralph/features/12-names.md.done +124 -0
  32. data/docs/agents/ralph/features/13-mode.md.done +174 -0
  33. data/docs/agents/ralph/features/14-who-whois.md.done +188 -0
  34. data/docs/agents/ralph/features/15-client-api.md.done +180 -0
  35. data/docs/agents/ralph/features/16-ssl-test-infrastructure.md.done +50 -0
  36. data/docs/agents/ralph/features/17-github-actions-ci.md.done +70 -0
  37. data/docs/agents/ralph/features/18-brakeman-security-scanning.md.done +67 -0
  38. data/docs/agents/ralph/features/19-fix-qa.md.done +73 -0
  39. data/docs/agents/ralph/features/20-test-optimization.md.done +70 -0
  40. data/docs/agents/ralph/features/21-test-parallelization.md.done +56 -0
  41. data/docs/agents/ralph/features/22-wait-until-pattern.md.done +90 -0
  42. data/docs/agents/ralph/features/23-ping-test-optimization.md.done +46 -0
  43. data/docs/agents/ralph/features/24-blocking-who-whois.md.done +159 -0
  44. data/docs/agents/ralph/features/25-verbose-mode.md.done +166 -0
  45. data/docs/agents/ralph/plans/test-optimization-plan.md +172 -0
  46. data/docs/agents/ralph/progress.md +731 -0
  47. data/docs/agents/todo.md +5 -0
  48. data/lib/yaic/channel.rb +22 -0
  49. data/lib/yaic/client.rb +982 -0
  50. data/lib/yaic/event.rb +35 -0
  51. data/lib/yaic/message.rb +119 -0
  52. data/lib/yaic/message_splitter.rb +51 -0
  53. data/lib/yaic/registration.rb +17 -0
  54. data/lib/yaic/socket.rb +152 -0
  55. data/lib/yaic/source.rb +39 -0
  56. data/lib/yaic/version.rb +1 -1
  57. data/lib/yaic/who_result.rb +17 -0
  58. data/lib/yaic/whois_result.rb +20 -0
  59. data/lib/yaic.rb +14 -1
  60. metadata +53 -3
data/lib/yaic/event.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yaic
4
+ class Event
5
+ attr_reader :type, :message
6
+
7
+ def initialize(type:, message: nil, **attributes)
8
+ @type = type
9
+ @message = message
10
+ @attributes = attributes
11
+ end
12
+
13
+ def [](key)
14
+ @attributes[key]
15
+ end
16
+
17
+ def to_h
18
+ @attributes.dup
19
+ end
20
+
21
+ private
22
+
23
+ def respond_to_missing?(method_name, include_private = false)
24
+ @attributes.key?(method_name) || super
25
+ end
26
+
27
+ def method_missing(method_name, *args)
28
+ if @attributes.key?(method_name) && args.empty?
29
+ @attributes[method_name]
30
+ else
31
+ super
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "strscan"
4
+
5
+ module Yaic
6
+ class Message
7
+ attr_reader :tags, :source, :command, :params, :raw
8
+
9
+ def initialize(tags: {}, source: nil, command: nil, params: [])
10
+ @tags = tags
11
+ @source = source
12
+ @command = command
13
+ @params = params
14
+ @raw = nil
15
+ end
16
+
17
+ def self.parse(str)
18
+ return nil if str.nil? || str.empty?
19
+
20
+ str = str.b.force_encoding("UTF-8")
21
+ str = str.encode("UTF-8", "ISO-8859-1", invalid: :replace, undef: :replace) unless str.valid_encoding?
22
+
23
+ stripped = str.chomp("\r\n").chomp("\n")
24
+ return nil if stripped.empty?
25
+
26
+ scanner = StringScanner.new(stripped)
27
+
28
+ tags = parse_tags(scanner)
29
+ source = parse_source(scanner)
30
+ command = parse_command(scanner)
31
+ params = parse_params(scanner)
32
+
33
+ msg = new(tags: tags, source: source, command: command, params: params)
34
+ msg.instance_variable_set(:@raw, str)
35
+ msg
36
+ end
37
+
38
+ def to_s
39
+ parts = []
40
+ parts << @command
41
+
42
+ @params.each_with_index do |param, idx|
43
+ is_last = idx == @params.length - 1
44
+ parts << if is_last && needs_trailing_prefix?(param, @params.length)
45
+ ":#{param}"
46
+ else
47
+ param
48
+ end
49
+ end
50
+
51
+ "#{parts.join(" ")}\r\n"
52
+ end
53
+
54
+ private
55
+
56
+ def needs_trailing_prefix?(param, param_count)
57
+ param.empty? || param.include?(" ") || param.start_with?(":") || param_count > 1
58
+ end
59
+
60
+ class << self
61
+ private
62
+
63
+ def parse_tags(scanner)
64
+ tags = {}
65
+
66
+ if scanner.peek(1) == "@"
67
+ scanner.getch
68
+ tag_str = scanner.scan(/[^ ]+/)
69
+ skip_spaces(scanner)
70
+
71
+ tag_str&.split(";")&.each do |tag|
72
+ key, value = tag.split("=", 2)
73
+ tags[key] = value || ""
74
+ end
75
+ end
76
+
77
+ tags
78
+ end
79
+
80
+ def parse_source(scanner)
81
+ return nil unless scanner.peek(1) == ":"
82
+
83
+ scanner.getch
84
+ source_str = scanner.scan(/[^ ]+/)
85
+ skip_spaces(scanner)
86
+
87
+ Source.parse(source_str)
88
+ end
89
+
90
+ def parse_command(scanner)
91
+ cmd = scanner.scan(/[A-Za-z]+|\d{3}/)
92
+ skip_spaces(scanner)
93
+ cmd
94
+ end
95
+
96
+ def parse_params(scanner)
97
+ params = []
98
+
99
+ until scanner.eos?
100
+ if scanner.peek(1) == ":"
101
+ scanner.getch
102
+ params << scanner.rest
103
+ break
104
+ else
105
+ param = scanner.scan(/[^ ]+/)
106
+ params << param if param
107
+ skip_spaces(scanner)
108
+ end
109
+ end
110
+
111
+ params
112
+ end
113
+
114
+ def skip_spaces(scanner)
115
+ scanner.skip(/ +/)
116
+ end
117
+ end
118
+ end
119
+ 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
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yaic
4
+ module Registration
5
+ def self.pass_message(password)
6
+ Message.new(command: "PASS", params: [password])
7
+ end
8
+
9
+ def self.nick_message(nickname)
10
+ Message.new(command: "NICK", params: [nickname])
11
+ end
12
+
13
+ def self.user_message(username, realname)
14
+ Message.new(command: "USER", params: [username, "0", "*", realname])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "socket"
4
+ require "openssl"
5
+ require "io/wait"
6
+
7
+ module Yaic
8
+ class Socket
9
+ CONNECTION_ERRORS = [IOError, SystemCallError, OpenSSL::SSL::SSLError].freeze
10
+ DEFAULT_WRITE_TIMEOUT = 5
11
+
12
+ def state
13
+ @monitor.synchronize { @state }
14
+ end
15
+
16
+ def initialize(host, port, ssl: false, verify_ssl: true, connect_timeout: 30, write_timeout: DEFAULT_WRITE_TIMEOUT)
17
+ @host = host
18
+ @port = port
19
+ @ssl = ssl
20
+ @verify_mode = verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
21
+ @connect_timeout = connect_timeout
22
+ @write_timeout = write_timeout
23
+
24
+ @socket = nil
25
+ @read_buffer = String.new(encoding: Encoding::ASCII_8BIT)
26
+ @state = :disconnected
27
+ @monitor = Monitor.new
28
+ end
29
+
30
+ def connect
31
+ tcp_socket = TCPSocket.new(@host, @port, connect_timeout: @connect_timeout)
32
+ tcp_socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_KEEPALIVE, true)
33
+
34
+ @monitor.synchronize do
35
+ @socket = @ssl ? wrap_ssl(tcp_socket) : tcp_socket
36
+ @state = :connecting
37
+ end
38
+ rescue Errno::ETIMEDOUT, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, IO::TimeoutError, SocketError => e
39
+ raise Yaic::ConnectionError, e.message
40
+ end
41
+
42
+ def disconnect
43
+ @monitor.synchronize do
44
+ return if @state == :disconnected
45
+
46
+ begin
47
+ @socket&.close
48
+ rescue
49
+ nil
50
+ end
51
+ @socket = nil
52
+ @read_buffer.clear
53
+ @state = :disconnected
54
+ end
55
+ end
56
+
57
+ def read
58
+ @monitor.synchronize do
59
+ return nil if @socket.nil?
60
+
61
+ begin
62
+ data = @socket.read_nonblock(4096)
63
+ buffer_data(data)
64
+ extract_message
65
+ rescue IO::WaitReadable, IO::WaitWritable
66
+ extract_message
67
+ rescue *CONNECTION_ERRORS => e
68
+ extract_message || raise(ConnectionError, describe(e))
69
+ end
70
+ end
71
+ end
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
+
86
+ def write(message)
87
+ @monitor.synchronize do
88
+ raise ConnectionError, "Not connected" if @socket.nil?
89
+
90
+ data = message.to_s.b
91
+ data << "\r\n" unless data.end_with?("\r\n", "\n")
92
+ write_fully(data)
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def wrap_ssl(tcp_socket)
99
+ context = OpenSSL::SSL::SSLContext.new
100
+ context.verify_mode = @verify_mode
101
+
102
+ ssl_socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, context)
103
+ ssl_socket.hostname = @host
104
+ ssl_socket.sync = true
105
+ ssl_socket.connect
106
+ ssl_socket
107
+ end
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
+
142
+ def buffer_data(data)
143
+ @read_buffer << data
144
+ end
145
+
146
+ def extract_message
147
+ if (idx = @read_buffer.index("\n"))
148
+ @read_buffer.slice!(0..idx)
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yaic
4
+ class Source
5
+ attr_reader :nick, :user, :host, :raw
6
+
7
+ def initialize(nick: nil, user: nil, host: nil, raw: nil)
8
+ @nick = nick
9
+ @user = user
10
+ @host = host
11
+ @raw = raw
12
+ end
13
+
14
+ def self.parse(str)
15
+ return nil if str.nil? || str.empty?
16
+
17
+ nick = nil
18
+ user = nil
19
+ host = nil
20
+
21
+ if str.include?("!")
22
+ nick, rest = str.split("!", 2)
23
+ if rest.include?("@")
24
+ user, host = rest.split("@", 2)
25
+ else
26
+ user = rest
27
+ end
28
+ elsif str.include?("@")
29
+ nick, host = str.split("@", 2)
30
+ elsif str.include?(".")
31
+ host = str
32
+ else
33
+ nick = str
34
+ end
35
+
36
+ new(nick: nick, user: user, host: host, raw: str)
37
+ end
38
+ end
39
+ end
data/lib/yaic/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yaic
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yaic
4
+ class WhoResult
5
+ attr_reader :channel, :user, :host, :server, :nick, :away, :realname
6
+
7
+ def initialize(channel:, user:, host:, server:, nick:, away:, realname:)
8
+ @channel = channel
9
+ @user = user
10
+ @host = host
11
+ @server = server
12
+ @nick = nick
13
+ @away = away
14
+ @realname = realname
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yaic
4
+ class WhoisResult
5
+ attr_accessor :nick, :user, :host, :realname, :channels, :server, :idle, :signon, :account, :away
6
+
7
+ def initialize(nick:)
8
+ @nick = nick
9
+ @user = nil
10
+ @host = nil
11
+ @realname = nil
12
+ @channels = []
13
+ @server = nil
14
+ @idle = nil
15
+ @signon = nil
16
+ @account = nil
17
+ @away = nil
18
+ end
19
+ end
20
+ end
data/lib/yaic.rb CHANGED
@@ -1,8 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "monitor"
4
+
3
5
  require_relative "yaic/version"
6
+ require_relative "yaic/source"
7
+ require_relative "yaic/message"
8
+ require_relative "yaic/socket"
9
+ require_relative "yaic/registration"
10
+ require_relative "yaic/event"
11
+ require_relative "yaic/channel"
12
+ require_relative "yaic/who_result"
13
+ require_relative "yaic/whois_result"
14
+ require_relative "yaic/message_splitter"
15
+ require_relative "yaic/client"
4
16
 
5
17
  module Yaic
6
18
  class Error < StandardError; end
7
- # Your code goes here...
19
+ class TimeoutError < Error; end
20
+ class ConnectionError < Error; end
8
21
  end
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.1.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-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  email:
13
13
  - joe@dupuis.io
@@ -15,17 +15,67 @@ executables: []
15
15
  extensions: []
16
16
  extra_rdoc_files: []
17
17
  files:
18
+ - ".claude/ralph/bin/dump-pid.sh"
19
+ - ".claude/ralph/bin/kill-claude"
20
+ - ".claude/ralph/bin/start-ralph"
21
+ - ".claude/ralph/bin/stop-hook.sh"
22
+ - ".claude/ralph/bin/stop-ralph"
23
+ - ".claude/ralph/prompt.md"
24
+ - ".claude/settings.json"
18
25
  - ".dockerignore"
26
+ - ".gitmodules"
19
27
  - ".ruby-version"
20
28
  - CHANGELOG.md
29
+ - CLAUDE.md
21
30
  - LICENSE.txt
22
31
  - README.md
23
32
  - Rakefile
24
33
  - devenv.lock
25
34
  - devenv.nix
26
35
  - devenv.yaml
36
+ - docs/agents/data-model.md
37
+ - docs/agents/ralph/features/01-message-parsing.md.done
38
+ - docs/agents/ralph/features/01-tcpsocket-refactor.md.done
39
+ - docs/agents/ralph/features/02-connection-socket.md.done
40
+ - docs/agents/ralph/features/02-simplified-client-api.md.done
41
+ - docs/agents/ralph/features/03-registration.md.done
42
+ - docs/agents/ralph/features/04-ping-pong.md.done
43
+ - docs/agents/ralph/features/05-event-system.md.done
44
+ - docs/agents/ralph/features/06-privmsg-notice.md.done
45
+ - docs/agents/ralph/features/07-join-part.md.done
46
+ - docs/agents/ralph/features/08-quit.md.done
47
+ - docs/agents/ralph/features/09-nick-change.md.done
48
+ - docs/agents/ralph/features/10-topic.md.done
49
+ - docs/agents/ralph/features/11-kick.md.done
50
+ - docs/agents/ralph/features/12-names.md.done
51
+ - docs/agents/ralph/features/13-mode.md.done
52
+ - docs/agents/ralph/features/14-who-whois.md.done
53
+ - docs/agents/ralph/features/15-client-api.md.done
54
+ - docs/agents/ralph/features/16-ssl-test-infrastructure.md.done
55
+ - docs/agents/ralph/features/17-github-actions-ci.md.done
56
+ - docs/agents/ralph/features/18-brakeman-security-scanning.md.done
57
+ - docs/agents/ralph/features/19-fix-qa.md.done
58
+ - docs/agents/ralph/features/20-test-optimization.md.done
59
+ - docs/agents/ralph/features/21-test-parallelization.md.done
60
+ - docs/agents/ralph/features/22-wait-until-pattern.md.done
61
+ - docs/agents/ralph/features/23-ping-test-optimization.md.done
62
+ - docs/agents/ralph/features/24-blocking-who-whois.md.done
63
+ - docs/agents/ralph/features/25-verbose-mode.md.done
64
+ - docs/agents/ralph/plans/test-optimization-plan.md
65
+ - docs/agents/ralph/progress.md
66
+ - docs/agents/todo.md
27
67
  - lib/yaic.rb
68
+ - lib/yaic/channel.rb
69
+ - lib/yaic/client.rb
70
+ - lib/yaic/event.rb
71
+ - lib/yaic/message.rb
72
+ - lib/yaic/message_splitter.rb
73
+ - lib/yaic/registration.rb
74
+ - lib/yaic/socket.rb
75
+ - lib/yaic/source.rb
28
76
  - lib/yaic/version.rb
77
+ - lib/yaic/who_result.rb
78
+ - lib/yaic/whois_result.rb
29
79
  - sig/yaic.rbs
30
80
  homepage: https://github.com/JoeDupuis/yaic
31
81
  licenses:
@@ -48,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
98
  - !ruby/object:Gem::Version
49
99
  version: '0'
50
100
  requirements: []
51
- rubygems_version: 3.6.2
101
+ rubygems_version: 3.6.9
52
102
  specification_version: 4
53
103
  summary: Yet another irc client
54
104
  test_files: []