shiny_nntp 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +115 -0
- data/Rakefile +12 -0
- data/exe/shiny_nntp +3 -0
- data/lib/shiny_nntp/article.rb +46 -0
- data/lib/shiny_nntp/connection.rb +85 -0
- data/lib/shiny_nntp/group.rb +174 -0
- data/lib/shiny_nntp/protocol.rb +66 -0
- data/lib/shiny_nntp/rapid_yenc.rb +81 -0
- data/lib/shiny_nntp/session.rb +90 -0
- data/lib/shiny_nntp/ssl_connection.rb +21 -0
- data/lib/shiny_nntp/version.rb +5 -0
- data/lib/shiny_nntp/yenc.rb +352 -0
- data/lib/shiny_nntp.rb +21 -0
- data/sig/shiny_nntp.rbs +4 -0
- metadata +96 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 93e32fca1e13808216d28fc0009d9ad30274cbd3f1501aca7869de8a849a762f
|
|
4
|
+
data.tar.gz: 76da511bd0a070ce35eef686d2527a2f1093d00c800ffb130b5aa42d440029d0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 456f6bdbaba4ae0e7a49caf75f44de4de6ebdadefe47087b3e6d15baff776f5da74b39709d53e3ef38a17534997917bd2cb8391e8221fba1b6fd0a91c44c6e9d
|
|
7
|
+
data.tar.gz: 505e9974ee46d956e2bb1a58d9dad2e0da35bb02854ac89a3da08a87358fce77b7d8f04ae03846569474c0be59e56c9b85630254457744e38c98af56e5c96b48
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"shiny_nntp" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["roblane09@gmail.com"](mailto:"roblane09@gmail.com").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rob-lane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# ShinyNntp
|
|
2
|
+
|
|
3
|
+
A simple NNTP client for Ruby. It keeps one connection, sends a small set of commands, and reads the responses. It does not cache groups, overview lines, or article bodies. Every call goes to the server.
|
|
4
|
+
|
|
5
|
+
Command support is limited to the server greeting, `CAPABILITIES`, `AUTHINFO USER`, `AUTHINFO PASS`, `LIST`, `GROUP`, `BODY`, `OVER`, `XOVER`, and `QUIT`. Anything else raises `ShinyNntp::ProtocolError` when the response code is not the one that command requires.
|
|
6
|
+
|
|
7
|
+
Article numbers belong to whichever group the session selected last. After a later `session.group`, a numbered `body`, `article`, `over`, or `xover` on an earlier group reads from the newly selected group. A message-id does not. Connections are plain TCP. This client does not open a TLS socket.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
NNTP commands are pure Ruby. Decoding a yEnc body loads [rapidyenc](https://github.com/animetosho/rapidyenc) through FFI. Build that shared library and set `RAPID_YENC_LIB` to its path before decoding. The variable is read on the first decode.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
git clone https://github.com/animetosho/rapidyenc.git
|
|
15
|
+
cd rapidyenc
|
|
16
|
+
mkdir build
|
|
17
|
+
cd build
|
|
18
|
+
cmake ..
|
|
19
|
+
cmake --build . --config Release
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
CMake writes `librapidyenc.dylib` on macOS and `librapidyenc.so` on Linux into that `build` directory.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
export RAPID_YENC_LIB=/absolute/path/to/rapidyenc/build/librapidyenc.dylib
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Add the gem to the application Gemfile:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
gem "shiny_nntp", git: "https://gitlab.com/rlane/shiny_nntp.git"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then run `bundle install`.
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
Open a connection, then a session. `Session.new` reads the greeting (200 or 201) before any command is sent.
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
require "shiny_nntp"
|
|
42
|
+
|
|
43
|
+
connection = ShinyNntp::Connection.new(uri: "news.example.com", port: 119)
|
|
44
|
+
session = ShinyNntp::Session.new(connection)
|
|
45
|
+
|
|
46
|
+
session.posting_allowed? # true when the greeting is 200
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`port` defaults to 119.
|
|
50
|
+
|
|
51
|
+
List what the server implements, authenticate when it requires a user, and list group names. `capabilities` returns the capability lines. `list_groups` returns names from `LIST`.
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
session.capabilities
|
|
55
|
+
# => ["VERSION 2", "READER", "AUTHINFO USER", "OVER"]
|
|
56
|
+
|
|
57
|
+
session.auth("username", "password")
|
|
58
|
+
|
|
59
|
+
session.list_groups
|
|
60
|
+
# => ["comp.lang.ruby", "alt.test"]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`auth` sends `AUTHINFO USER`. It sends `AUTHINFO PASS` only when the server answers 381. Pass `nil` for the password when the server accepts the user alone.
|
|
64
|
+
|
|
65
|
+
Select a group to explore it. `article_count` is the server's estimate. Not every number from `min_id` to `max_id` is an article the server still has.
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
group = session.group("comp.lang.ruby")
|
|
69
|
+
group.name # => "comp.lang.ruby"
|
|
70
|
+
group.article_count # => 42
|
|
71
|
+
group.min_id # => 7
|
|
72
|
+
group.max_id # => 99
|
|
73
|
+
session.current_group # the Group selected last
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Use `over` when `capabilities` includes `OVER`. Use `xover` when the server only offers the older command. Both take an article number, an open range, a closed range, or a message-id, and return `Article` objects sorted by article number. Those articles carry the overview fields and do not include a body. An empty range returns `[]`.
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
headers = group.over(group.min_id..(group.min_id + 20))
|
|
80
|
+
headers.each do |article|
|
|
81
|
+
puts "#{article.number} #{article.subject} #{article.from} #{article.message_id}"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
group.over(group.min_id) # one article number
|
|
85
|
+
group.over(group.min_id..) # that number through the end of the group
|
|
86
|
+
group.over("#{group.min_id}-") # the same open range, written as text
|
|
87
|
+
group.xover(group.min_id..group.max_id)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Each overview article has `number`, `subject`, `from`, `date`, `message_id`, `references`, `bytes`, `line_count`, and `extra` for any further columns. An empty header is `""`. A missing byte or line count is `nil`.
|
|
91
|
+
|
|
92
|
+
Read a body with `article` or `body`. `article` returns an `Article` whose `lines` are the body with dot-stuffing already removed. `body` returns that line array. A missing number or message-id returns `nil`.
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
article = group.article(group.min_id)
|
|
96
|
+
article.lines.each { |line| puts line }
|
|
97
|
+
|
|
98
|
+
group.body(article.message_id) # looks the article up by message-id
|
|
99
|
+
group.body("<id@example.com>")
|
|
100
|
+
|
|
101
|
+
session.quit
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`quit` sends `QUIT` and closes the socket.
|
|
105
|
+
|
|
106
|
+
A yEnc body can be decoded after `RAPID_YENC_LIB` is set. `decode_yenc` returns one part. `ShinyNntp::Yenc.combine` places parts back into the original file and returns those bytes. A bad or incomplete part raises `ShinyNntp::YencError`.
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
part = group.article(number).decode_yenc
|
|
110
|
+
bytes = ShinyNntp::Yenc.combine([part])
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Development
|
|
114
|
+
|
|
115
|
+
From a checkout of this repository, run `bin/setup`, then `bundle exec rspec`. Specs that decode yEnc load the library named by `RAPID_YENC_LIB`. The license is MIT.
|
data/Rakefile
ADDED
data/exe/shiny_nntp
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ShinyNntp
|
|
4
|
+
# An article body fetched with BODY, or one overview line from OVER.
|
|
5
|
+
#
|
|
6
|
+
# +lines+ is the body as an array of strings when the article was
|
|
7
|
+
# fetched with BODY. Dot-stuffing has already been removed, and the
|
|
8
|
+
# terminating "." line is not included. OVER does not fetch a body, so
|
|
9
|
+
# +lines+ is nil. The overview fields are the first eight OVER columns:
|
|
10
|
+
# article number, Subject, From, Date, Message-ID, References, :bytes,
|
|
11
|
+
# and :lines. Further columns are kept in +extra+. A yEnc body can be
|
|
12
|
+
# decoded with #decode_yenc.
|
|
13
|
+
class Article
|
|
14
|
+
attr_reader :number, :message_id, :lines, :subject, :from, :date, :references, :bytes, :line_count, :extra
|
|
15
|
+
|
|
16
|
+
def initialize(number:, message_id:, lines: nil, **fields)
|
|
17
|
+
@number = number
|
|
18
|
+
@message_id = message_id
|
|
19
|
+
@lines = lines
|
|
20
|
+
@subject = fields[:subject]
|
|
21
|
+
@from = fields[:from]
|
|
22
|
+
@date = fields[:date]
|
|
23
|
+
@references = fields[:references]
|
|
24
|
+
@bytes = fields[:bytes]
|
|
25
|
+
@line_count = fields[:line_count]
|
|
26
|
+
@extra = fields.fetch(:extra, [])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def each_line(&block)
|
|
30
|
+
return enum_for(:each_line) unless block
|
|
31
|
+
|
|
32
|
+
Array(lines).each(&block)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# True when the body contains a yEnc =ybegin header.
|
|
36
|
+
def yenc?
|
|
37
|
+
Array(lines).any? { |line| line.start_with?("=ybegin ") }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Decodes this body as one yEnc part. Raises YencError when the body
|
|
41
|
+
# is not a valid yEnc article.
|
|
42
|
+
def decode_yenc
|
|
43
|
+
Yenc.decode_article(lines)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "socket"
|
|
4
|
+
|
|
5
|
+
module ShinyNntp
|
|
6
|
+
Status = Data.define(:code, :message) do
|
|
7
|
+
def to_s
|
|
8
|
+
"#{code} #{message}"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Holds a connection to an NNTP server.
|
|
13
|
+
class Connection
|
|
14
|
+
attr_reader :socket, :status
|
|
15
|
+
|
|
16
|
+
def initialize(options = {}, **keywords)
|
|
17
|
+
options = options.merge(keywords)
|
|
18
|
+
@status = nil
|
|
19
|
+
raise ArgumentError, "Either :uri or :socket must be provided" if options[:uri].nil? && options[:socket].nil?
|
|
20
|
+
|
|
21
|
+
@socket = options[:socket] || build_socket(options[:uri], options)
|
|
22
|
+
raise ArgumentError, "Failed to build socket" if @socket.nil?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def query(query, **args)
|
|
26
|
+
command = form_message("QUERY", query, **args)
|
|
27
|
+
send_message(command)
|
|
28
|
+
status = get_status
|
|
29
|
+
raise Error, status.message if (400..499).include?(status.code)
|
|
30
|
+
|
|
31
|
+
data = get_block_data
|
|
32
|
+
yield status, data if block_given?
|
|
33
|
+
{ status: status, data: data }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def command(command, **args)
|
|
37
|
+
command = form_message(command, **args)
|
|
38
|
+
send_message(command)
|
|
39
|
+
get_status
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def get_status
|
|
43
|
+
code, message = get_line.split(" ", 2)
|
|
44
|
+
@status = Status.new(code.to_i, message)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def quit
|
|
48
|
+
command("QUIT")
|
|
49
|
+
@socket.close
|
|
50
|
+
@status = nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
protected
|
|
54
|
+
|
|
55
|
+
def get_line
|
|
56
|
+
line = @socket.gets
|
|
57
|
+
line.chomp!
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def form_message(command, **args)
|
|
61
|
+
message = command.to_s.upcase
|
|
62
|
+
message += " #{args.join(" ")}" if args.any?
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def send_message(message)
|
|
66
|
+
@socket.puts(message)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def build_socket(uri, options)
|
|
70
|
+
port = options.fetch(:port, 119)
|
|
71
|
+
socket_factory = options.fetch(:socket_factory, TCPSocket)
|
|
72
|
+
socket_factory.new(uri, port)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def get_block_data
|
|
76
|
+
data = []
|
|
77
|
+
line = get_line
|
|
78
|
+
until line == "."
|
|
79
|
+
data << line
|
|
80
|
+
line = get_line
|
|
81
|
+
end
|
|
82
|
+
data
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "protocol"
|
|
4
|
+
require_relative "article"
|
|
5
|
+
|
|
6
|
+
module ShinyNntp
|
|
7
|
+
# A newsgroup selected with GROUP.
|
|
8
|
+
#
|
|
9
|
+
# +article_count+ is the estimate from the 211 response. Not every id
|
|
10
|
+
# from +min_id+ to +max_id+ is an article the server still has.
|
|
11
|
+
#
|
|
12
|
+
# An article number is read from whichever group the session selected
|
|
13
|
+
# last, so +body+ and +article+ with a number follow a later GROUP.
|
|
14
|
+
# A message-id is read from the server's article store and does not
|
|
15
|
+
# change the selected group.
|
|
16
|
+
class Group
|
|
17
|
+
include Protocol
|
|
18
|
+
|
|
19
|
+
attr_reader :name, :article_count, :min_id, :max_id, :connection
|
|
20
|
+
|
|
21
|
+
def initialize(name:, article_count:, min_id:, max_id:, connection:)
|
|
22
|
+
@name = name
|
|
23
|
+
@article_count = article_count
|
|
24
|
+
@min_id = min_id
|
|
25
|
+
@max_id = max_id
|
|
26
|
+
@connection = connection
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Sends BODY for an article number or a message-id.
|
|
30
|
+
#
|
|
31
|
+
# A message-id may be "<id@host>" or "id@host". Returns the body as an
|
|
32
|
+
# array of strings when the server answers 222. Yields each unstuffed
|
|
33
|
+
# line when a block is given. Returns nil for 423 (no article with
|
|
34
|
+
# that number) or 430 (no article with that message-id).
|
|
35
|
+
def body(id, &block)
|
|
36
|
+
article(id, &block)&.lines
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Same retrieval as #body, returned as an Article.
|
|
40
|
+
# Returns nil for 423 or 430.
|
|
41
|
+
#
|
|
42
|
+
# The number on the Article is the number in the session's selected
|
|
43
|
+
# group. It is 0 when a message-id is not in that group.
|
|
44
|
+
def article(id)
|
|
45
|
+
token, missing = body_argument(id)
|
|
46
|
+
status = expect_code(send_command("BODY #{token}"), 222, missing)
|
|
47
|
+
return nil if status.code == missing
|
|
48
|
+
|
|
49
|
+
number, message_id = parse_article(status)
|
|
50
|
+
lines = read_multiline { |line| yield line if block_given? }
|
|
51
|
+
Article.new(number: number, message_id: message_id, lines: lines)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Sends OVER for a message-id or an article range.
|
|
55
|
+
#
|
|
56
|
+
# A range is an article number (+12+), everything from a number
|
|
57
|
+
# (+12..+ or +"12-"+), or an inclusive span (+12..40+ or +"12-40"+).
|
|
58
|
+
# A message-id may be "<id@host>" or "id@host". Number and range
|
|
59
|
+
# forms are read from the session's selected group. A message-id is
|
|
60
|
+
# not, and does not change that selection.
|
|
61
|
+
#
|
|
62
|
+
# Returns one Article per overview line, sorted by article number.
|
|
63
|
+
# An empty array is returned for 423 (nothing in the range) and 430
|
|
64
|
+
# (no such message-id).
|
|
65
|
+
def over(selector)
|
|
66
|
+
fetch_overview("OVER", selector)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Sends XOVER, the RFC 2980 form of OVER. The selector and overview
|
|
70
|
+
# lines are handled the same way. An empty array is returned for 420
|
|
71
|
+
# (no articles in the range).
|
|
72
|
+
def xover(selector)
|
|
73
|
+
fetch_overview("XOVER", selector)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
# Decimal text is an article number (423 when missing). Anything else
|
|
79
|
+
# is a message-id, written in angle brackets (430 when missing).
|
|
80
|
+
def body_argument(id)
|
|
81
|
+
text = id.to_s
|
|
82
|
+
return [text, 423] if text.match?(/\A\d+\z/)
|
|
83
|
+
|
|
84
|
+
[message_id_token(text), 430]
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def fetch_overview(command, selector)
|
|
88
|
+
token = over_argument(selector)
|
|
89
|
+
missing = command == "XOVER" ? 420 : overview_missing(token)
|
|
90
|
+
status = expect_code(send_command("#{command} #{token}"), 224, missing)
|
|
91
|
+
return [] if status.code == missing
|
|
92
|
+
|
|
93
|
+
read_multiline.map { |line| overview_article(line) }.sort_by(&:number)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def overview_missing(token)
|
|
97
|
+
token.start_with?("<") ? 430 : 423
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def over_argument(selector)
|
|
101
|
+
return format_over_range(selector) if selector.is_a?(Range)
|
|
102
|
+
|
|
103
|
+
text = selector.to_s
|
|
104
|
+
return text if text.match?(/\A\d+(?:-\d*)?\z/)
|
|
105
|
+
|
|
106
|
+
message_id_token(text)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def format_over_range(range)
|
|
110
|
+
raise ArgumentError, "overview range is inclusive" if range.exclude_end?
|
|
111
|
+
raise ArgumentError, "overview range needs a start article number" if range.begin.nil?
|
|
112
|
+
|
|
113
|
+
start = Integer(range.begin)
|
|
114
|
+
return "#{start}-" if range.end.nil?
|
|
115
|
+
|
|
116
|
+
"#{start}-#{Integer(range.end)}"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def message_id_token(text)
|
|
120
|
+
text.start_with?("<") ? text : "<#{text}>"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def overview_article(line)
|
|
124
|
+
fields = overview_fields(line)
|
|
125
|
+
Article.new(**overview_headers(fields), **overview_counts(fields))
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def overview_fields(line)
|
|
129
|
+
fields = line.split("\t", -1)
|
|
130
|
+
raise ProtocolError, "malformed overview line: #{line.inspect}" unless fields[0]&.match?(/\A\d+\z/)
|
|
131
|
+
|
|
132
|
+
fields.fill("", fields.length...8)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def overview_headers(fields)
|
|
136
|
+
{
|
|
137
|
+
number: fields[0].to_i,
|
|
138
|
+
subject: fields[1],
|
|
139
|
+
from: fields[2],
|
|
140
|
+
date: fields[3],
|
|
141
|
+
message_id: fields[4],
|
|
142
|
+
references: fields[5]
|
|
143
|
+
}
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def overview_counts(fields)
|
|
147
|
+
{
|
|
148
|
+
bytes: overview_count(fields[6]),
|
|
149
|
+
line_count: overview_count(fields[7]),
|
|
150
|
+
extra: overview_extra(fields)
|
|
151
|
+
}
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def overview_extra(fields)
|
|
155
|
+
extra = fields.drop(8)
|
|
156
|
+
extra.pop while !extra.empty? && extra.last.empty?
|
|
157
|
+
extra
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def overview_count(value)
|
|
161
|
+
return nil if value.empty?
|
|
162
|
+
raise ProtocolError, "malformed overview count: #{value.inspect}" unless value.match?(/\A\d+\z/)
|
|
163
|
+
|
|
164
|
+
value.to_i
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def parse_article(status)
|
|
168
|
+
match = /\A(\d+) (<[^>\s]+>)/.match(status.message.to_s)
|
|
169
|
+
raise ProtocolError, "malformed BODY response: #{status.message.inspect}" unless match
|
|
170
|
+
|
|
171
|
+
[match[1].to_i, match[2]]
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "connection"
|
|
4
|
+
|
|
5
|
+
module ShinyNntp
|
|
6
|
+
# Wire format shared by Session and Group.
|
|
7
|
+
#
|
|
8
|
+
# Commands are single lines. CR, LF, and NUL are rejected so a value
|
|
9
|
+
# cannot end the line early; a bare LF is enough for many servers to
|
|
10
|
+
# run a second command. Every response line must end with CR LF. The
|
|
11
|
+
# status code is the first three digits.
|
|
12
|
+
module Protocol
|
|
13
|
+
CRLF = "\r\n"
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def send_command(command)
|
|
18
|
+
text = command.to_s
|
|
19
|
+
raise ProtocolError, "command contains CR, LF, or NUL" if text.match?(/[\r\n\0]/n)
|
|
20
|
+
|
|
21
|
+
io = connection.socket
|
|
22
|
+
io.write("#{text}#{CRLF}")
|
|
23
|
+
io.flush if io.respond_to?(:flush)
|
|
24
|
+
read_status
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def read_status
|
|
28
|
+
line = read_line
|
|
29
|
+
match = /\A([1-5][0-9]{2})(?: (.*))?\z/n.match(line)
|
|
30
|
+
raise ProtocolError, "invalid status line: #{line.inspect}" unless match
|
|
31
|
+
|
|
32
|
+
Status.new(match[1].to_i, match[2])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def read_line
|
|
36
|
+
line = connection.socket.gets
|
|
37
|
+
raise ProtocolError, "response ended before CR LF" if line.nil?
|
|
38
|
+
raise ProtocolError, "response line must end with CR LF" unless line.end_with?(CRLF)
|
|
39
|
+
|
|
40
|
+
line.delete_suffix(CRLF)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Reads a multi-line data block. A line that is exactly "." ends the
|
|
44
|
+
# block and is not returned. A line that starts with ".." has the
|
|
45
|
+
# first dot removed (RFC 3977 dot-stuffing).
|
|
46
|
+
def read_multiline
|
|
47
|
+
lines = []
|
|
48
|
+
loop do
|
|
49
|
+
line = read_line
|
|
50
|
+
break if line == "."
|
|
51
|
+
|
|
52
|
+
line = line.delete_prefix(".") if line.start_with?("..")
|
|
53
|
+
yield line if block_given?
|
|
54
|
+
lines << line
|
|
55
|
+
end
|
|
56
|
+
lines
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def expect_code(status, *codes)
|
|
60
|
+
return status if codes.include?(status.code)
|
|
61
|
+
|
|
62
|
+
detail = [status.code, status.message].compact.join(" ")
|
|
63
|
+
raise ProtocolError, "expected #{codes.join(" or ")} response, got #{detail}"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ffi"
|
|
4
|
+
|
|
5
|
+
module ShinyNntp
|
|
6
|
+
# FFI binding for the rapidyenc C library.
|
|
7
|
+
#
|
|
8
|
+
# +RAPID_YENC_LIB+ is the path to librapidyenc. The library is opened
|
|
9
|
+
# on the first decode, and rapidyenc_decode_init is called once.
|
|
10
|
+
module RapidYenc
|
|
11
|
+
# SIMD kernels may read a few bytes past the buffer while matching
|
|
12
|
+
# special characters. Those bytes are not decoded.
|
|
13
|
+
DECODE_PADDING = 64
|
|
14
|
+
|
|
15
|
+
# Opens librapidyenc and binds the decode functions.
|
|
16
|
+
module Native
|
|
17
|
+
extend FFI::Library
|
|
18
|
+
|
|
19
|
+
@load_mutex = Mutex.new
|
|
20
|
+
@loaded = false
|
|
21
|
+
|
|
22
|
+
def self.load!
|
|
23
|
+
return if @loaded
|
|
24
|
+
|
|
25
|
+
@load_mutex.synchronize { open_library unless @loaded }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.open_library
|
|
29
|
+
attach(library_path)
|
|
30
|
+
rapidyenc_decode_init
|
|
31
|
+
@loaded = true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.library_path
|
|
35
|
+
path = ENV.fetch("RAPID_YENC_LIB", nil)
|
|
36
|
+
raise YencError, "RAPID_YENC_LIB must be the path to librapidyenc" if path.nil? || path.empty?
|
|
37
|
+
|
|
38
|
+
path
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.attach(path)
|
|
42
|
+
ffi_lib path
|
|
43
|
+
attach_function :rapidyenc_decode_init, [], :void
|
|
44
|
+
attach_function :rapidyenc_decode_ex, %i[int pointer pointer size_t pointer], :size_t
|
|
45
|
+
rescue LoadError => e
|
|
46
|
+
raise YencError, "could not load rapidyenc from #{path}: #{e.message}"
|
|
47
|
+
end
|
|
48
|
+
private_class_method :open_library, :library_path, :attach
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Input: the encoded payload of one yEnc part, already NNTP dot-unstuffed.
|
|
52
|
+
# Output: decoded bytes as an ASCII-8BIT Ruby String.
|
|
53
|
+
#
|
|
54
|
+
# is_raw is 0 because dot-stuffing has already been removed. Line
|
|
55
|
+
# endings in the payload are skipped by the decoder.
|
|
56
|
+
def self.decode_payload(payload)
|
|
57
|
+
Native.load!
|
|
58
|
+
payload = payload.to_s.b
|
|
59
|
+
return "".b if payload.empty?
|
|
60
|
+
|
|
61
|
+
decode_into(payload)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.decode_into(payload)
|
|
65
|
+
buffer = FFI::MemoryPointer.new(:uint8, payload.bytesize + DECODE_PADDING)
|
|
66
|
+
read_buffer(buffer, payload)
|
|
67
|
+
ensure
|
|
68
|
+
buffer&.free
|
|
69
|
+
end
|
|
70
|
+
private_class_method :decode_into
|
|
71
|
+
|
|
72
|
+
def self.read_buffer(buffer, payload)
|
|
73
|
+
buffer.put_bytes(0, payload)
|
|
74
|
+
written = Native.rapidyenc_decode_ex(0, buffer, buffer, payload.bytesize, nil)
|
|
75
|
+
raise YencError, "rapidyenc decode overran the payload" if written > payload.bytesize
|
|
76
|
+
|
|
77
|
+
buffer.get_bytes(0, written).b
|
|
78
|
+
end
|
|
79
|
+
private_class_method :read_buffer
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "protocol"
|
|
4
|
+
require_relative "group"
|
|
5
|
+
|
|
6
|
+
module ShinyNntp
|
|
7
|
+
# An NNTP session on a connection.
|
|
8
|
+
#
|
|
9
|
+
# The server greeting (RFC 3977 section 5.1) is read when the session
|
|
10
|
+
# is created, before any command is sent. Commands are written to the
|
|
11
|
+
# connection socket. A response code other than the one the command
|
|
12
|
+
# requires raises ProtocolError.
|
|
13
|
+
class Session
|
|
14
|
+
include Protocol
|
|
15
|
+
|
|
16
|
+
attr_reader :connection, :current_group
|
|
17
|
+
|
|
18
|
+
def initialize(connection)
|
|
19
|
+
@connection = connection
|
|
20
|
+
@current_group = nil
|
|
21
|
+
@greeting = expect_code(read_status, 200, 201)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def posting_allowed?
|
|
25
|
+
@greeting.code == 200
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# CAPABILITIES. Expects 101, then a multi-line capability list.
|
|
29
|
+
def capabilities
|
|
30
|
+
expect_code(send_command("CAPABILITIES"), 101)
|
|
31
|
+
read_multiline
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# AUTHINFO USER, and AUTHINFO PASS when the server asks for one.
|
|
35
|
+
#
|
|
36
|
+
# 281 means the user is authenticated (password was not required, or
|
|
37
|
+
# the password was accepted). 381 means a password is required.
|
|
38
|
+
def auth(username, password = nil)
|
|
39
|
+
status = expect_code(send_command("AUTHINFO USER #{username}"), 281, 381)
|
|
40
|
+
return true if status.code == 281
|
|
41
|
+
|
|
42
|
+
raise ProtocolError, "password required" if password.nil?
|
|
43
|
+
|
|
44
|
+
expect_code(send_command("AUTHINFO PASS #{password}"), 281)
|
|
45
|
+
true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# LIST (active groups). Expects 215, then one group per line.
|
|
49
|
+
# Returns the group names.
|
|
50
|
+
def list_groups
|
|
51
|
+
expect_code(send_command("LIST"), 215)
|
|
52
|
+
read_multiline.map { |line| line.split(" ", 2).first }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# GROUP <name>. Expects 211 and selects that group on the session.
|
|
56
|
+
# Returns a Group that can fetch article bodies on this connection.
|
|
57
|
+
def group(name)
|
|
58
|
+
status = expect_code(send_command("GROUP #{name}"), 211)
|
|
59
|
+
selected = parse_group(status)
|
|
60
|
+
@current_group = Group.new(
|
|
61
|
+
name: selected[:name],
|
|
62
|
+
article_count: selected[:article_count],
|
|
63
|
+
min_id: selected[:min_id],
|
|
64
|
+
max_id: selected[:max_id],
|
|
65
|
+
connection: connection
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# QUIT. Expects 205, then closes the connection socket.
|
|
70
|
+
def quit
|
|
71
|
+
expect_code(send_command("QUIT"), 205)
|
|
72
|
+
connection.socket.close
|
|
73
|
+
true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def parse_group(status)
|
|
79
|
+
match = /\A(\d+) (\d+) (\d+) (\S+)\z/.match(status.message.to_s)
|
|
80
|
+
raise ProtocolError, "malformed GROUP response: #{status.message.inspect}" unless match
|
|
81
|
+
|
|
82
|
+
{
|
|
83
|
+
article_count: match[1].to_i,
|
|
84
|
+
min_id: match[2].to_i,
|
|
85
|
+
max_id: match[3].to_i,
|
|
86
|
+
name: match[4]
|
|
87
|
+
}
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require_relative 'connection'
|
|
2
|
+
require 'openssl'
|
|
3
|
+
require 'socket'
|
|
4
|
+
|
|
5
|
+
module ShinyNntp
|
|
6
|
+
module TLSConnection
|
|
7
|
+
def self.wrap(tcp_connection, hostname: ,context: default_context)
|
|
8
|
+
OpenSSL::SSL::SSLSocket.new(tcp_connection, context).tap do |ssl|
|
|
9
|
+
ssl.hostname = hostname
|
|
10
|
+
ssl.sync_close = true
|
|
11
|
+
ssl.connect
|
|
12
|
+
ssl.post_connection_check(hostname)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.default_context
|
|
17
|
+
OpenSSL::SSL::SSLContext.new.tap do |ctx|
|
|
18
|
+
ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER) # uses system CA store
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zlib"
|
|
4
|
+
require_relative "rapid_yenc"
|
|
5
|
+
|
|
6
|
+
module ShinyNntp
|
|
7
|
+
# Decodes yEnc article bodies (yEncode draft 1.3).
|
|
8
|
+
#
|
|
9
|
+
# +lines+ are an article body with NNTP dot-stuffing already removed,
|
|
10
|
+
# which is what Group#article returns. Text outside the =ybegin / =yend
|
|
11
|
+
# block is ignored. A multipart article is one part: =ypart gives the
|
|
12
|
+
# 1-based inclusive byte range of that part in the original file.
|
|
13
|
+
# Yenc.combine writes parts back into that file.
|
|
14
|
+
module Yenc
|
|
15
|
+
Part = Data.define(
|
|
16
|
+
:name,
|
|
17
|
+
:file_size,
|
|
18
|
+
:line_length,
|
|
19
|
+
:data,
|
|
20
|
+
:part,
|
|
21
|
+
:total,
|
|
22
|
+
:begin_byte,
|
|
23
|
+
:end_byte,
|
|
24
|
+
:crc32,
|
|
25
|
+
:pcrc32
|
|
26
|
+
) do
|
|
27
|
+
def multipart?
|
|
28
|
+
!part.nil?
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.decode_article(lines)
|
|
33
|
+
Decoder.new(lines).part
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.combine(parts)
|
|
37
|
+
Combiner.new(parts).file
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# key=value fields from a =ybegin, =ypart, or =yend line.
|
|
41
|
+
# +name+ consumes the rest of the line, including spaces.
|
|
42
|
+
class Keywords
|
|
43
|
+
FIELD = /\A([A-Za-z][A-Za-z0-9]*)=(.*)\z/
|
|
44
|
+
CRC = /\A[0-9a-fA-F]{1,8}\z/
|
|
45
|
+
|
|
46
|
+
def self.parse(line, keyword)
|
|
47
|
+
new(line, keyword)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.verify_crc(label, expected, data)
|
|
51
|
+
return if expected.nil?
|
|
52
|
+
|
|
53
|
+
actual = Zlib.crc32(data)
|
|
54
|
+
return if actual == expected
|
|
55
|
+
|
|
56
|
+
raise YencError, mismatch(label, expected, actual)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.mismatch(label, expected, actual)
|
|
60
|
+
format(
|
|
61
|
+
"yEnc %<label>s mismatch: expected %<expected>08x, computed %<actual>08x",
|
|
62
|
+
label: label, expected: expected, actual: actual
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def initialize(line, keyword)
|
|
67
|
+
body = line.sub(/\A=#{Regexp.escape(keyword)}[ \t]+/, "")
|
|
68
|
+
raise YencError, "invalid yEnc #{keyword} line" if body == line
|
|
69
|
+
|
|
70
|
+
@fields = {}
|
|
71
|
+
consume(body)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
attr_reader :fields
|
|
75
|
+
|
|
76
|
+
def required_integer(key)
|
|
77
|
+
raise YencError, "yEnc #{key} is missing" unless @fields.key?(key)
|
|
78
|
+
|
|
79
|
+
integer(key)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def optional_integer(key)
|
|
83
|
+
return nil unless @fields.key?(key)
|
|
84
|
+
|
|
85
|
+
integer(key)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def optional_crc(key)
|
|
89
|
+
return nil unless @fields.key?(key)
|
|
90
|
+
|
|
91
|
+
value = @fields[key]
|
|
92
|
+
raise YencError, "yEnc #{key} is invalid" unless CRC.match?(value)
|
|
93
|
+
|
|
94
|
+
value.to_i(16)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def consume(body)
|
|
100
|
+
match = FIELD.match(body)
|
|
101
|
+
return unless match
|
|
102
|
+
return store_name(match[2]) if match[1] == "name"
|
|
103
|
+
|
|
104
|
+
value, remainder = match[2].split(/[ \t]+/, 2)
|
|
105
|
+
return if value.nil? || value.empty?
|
|
106
|
+
|
|
107
|
+
@fields[match[1]] = value
|
|
108
|
+
consume(remainder.to_s)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def store_name(rest)
|
|
112
|
+
@fields["name"] = rest.strip
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def integer(key)
|
|
116
|
+
value = @fields[key]
|
|
117
|
+
raise YencError, "yEnc #{key} is invalid" unless /\A\d+\z/.match?(value)
|
|
118
|
+
|
|
119
|
+
value.to_i
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
private_constant :Keywords
|
|
123
|
+
|
|
124
|
+
# One =ybegin ... =yend block from an article body.
|
|
125
|
+
class Decoder
|
|
126
|
+
def initialize(lines)
|
|
127
|
+
@lines = Array(lines).map { |line| line.to_s.b.sub(/(?:\r\n|\r|\n)\z/, "") }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def part
|
|
131
|
+
prepare
|
|
132
|
+
data = decode_payload
|
|
133
|
+
assemble(data)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def prepare
|
|
139
|
+
read_header
|
|
140
|
+
read_bounds
|
|
141
|
+
read_trailer
|
|
142
|
+
check_declared_size
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def read_header
|
|
146
|
+
@header_at = @lines.index { |line| line.start_with?("=ybegin ") }
|
|
147
|
+
raise YencError, "article does not contain a yEnc header" unless @header_at
|
|
148
|
+
|
|
149
|
+
@header = Keywords.parse(@lines.fetch(@header_at), "ybegin")
|
|
150
|
+
@part_number = @header.optional_integer("part")
|
|
151
|
+
@total = @header.optional_integer("total")
|
|
152
|
+
@file_size = @header.required_integer("size")
|
|
153
|
+
@line_length = @header.required_integer("line")
|
|
154
|
+
@name = @header.fields["name"]
|
|
155
|
+
validate_header
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def validate_header
|
|
159
|
+
raise YencError, "yEnc header is missing name" if @name.nil? || @name.empty?
|
|
160
|
+
raise YencError, "yEnc line is invalid" if @line_length < 1
|
|
161
|
+
|
|
162
|
+
validate_part_numbers
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def validate_part_numbers
|
|
166
|
+
raise YencError, "yEnc part is invalid" if @part_number&.zero?
|
|
167
|
+
|
|
168
|
+
validate_total
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def validate_total
|
|
172
|
+
raise YencError, "yEnc total is invalid" if @total&.zero?
|
|
173
|
+
raise YencError, "yEnc total is invalid" if @total && @part_number && @total < @part_number
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def read_bounds
|
|
177
|
+
if @part_number
|
|
178
|
+
read_ypart(@header_at + 1)
|
|
179
|
+
else
|
|
180
|
+
@cursor = @header_at + 1
|
|
181
|
+
@begin_byte = 1
|
|
182
|
+
@end_byte = @file_size
|
|
183
|
+
end
|
|
184
|
+
@part_size = @end_byte - @begin_byte + 1
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def read_ypart(index)
|
|
188
|
+
line = @lines[index]
|
|
189
|
+
raise YencError, "multipart yEnc article is missing =ypart" unless line&.start_with?("=ypart ")
|
|
190
|
+
|
|
191
|
+
ypart = Keywords.parse(line, "ypart")
|
|
192
|
+
@begin_byte = ypart.required_integer("begin")
|
|
193
|
+
@end_byte = ypart.required_integer("end")
|
|
194
|
+
invalid = @begin_byte < 1 || @end_byte < @begin_byte || @end_byte > @file_size
|
|
195
|
+
raise YencError, "yEnc part range is invalid" if invalid
|
|
196
|
+
|
|
197
|
+
@cursor = index + 1
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def read_trailer
|
|
201
|
+
offset = @lines[@cursor..]&.index { |line| line.start_with?("=yend ") }
|
|
202
|
+
raise YencError, "yEnc article is missing =yend" unless offset
|
|
203
|
+
|
|
204
|
+
@trailer_at = @cursor + offset
|
|
205
|
+
@trailer = Keywords.parse(@lines.fetch(@trailer_at), "yend")
|
|
206
|
+
check_trailer_part
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def check_trailer_part
|
|
210
|
+
trailer_part = @trailer.optional_integer("part")
|
|
211
|
+
if @part_number
|
|
212
|
+
raise YencError, "yEnc trailer is missing part" if trailer_part.nil?
|
|
213
|
+
raise YencError, "yEnc part number mismatch" unless trailer_part == @part_number
|
|
214
|
+
elsif trailer_part
|
|
215
|
+
raise YencError, "yEnc part number mismatch"
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def check_declared_size
|
|
220
|
+
trailer_size = @trailer.required_integer("size")
|
|
221
|
+
unless trailer_size == @part_size
|
|
222
|
+
raise YencError, "yEnc size mismatch: expected #{@part_size} bytes, trailer says #{trailer_size}"
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
@pcrc32 = @trailer.optional_crc("pcrc32")
|
|
226
|
+
@crc32 = @trailer.optional_crc("crc32")
|
|
227
|
+
raise YencError, "yEnc trailer is missing pcrc32" if @part_number && @pcrc32.nil?
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def decode_payload
|
|
231
|
+
encoded = @lines[@cursor...@trailer_at].join("\r\n".b)
|
|
232
|
+
data = RapidYenc.decode_payload(encoded)
|
|
233
|
+
return data if data.bytesize == @part_size
|
|
234
|
+
|
|
235
|
+
raise YencError, "yEnc size mismatch: expected #{@part_size} bytes, decoded #{data.bytesize}"
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def assemble(data)
|
|
239
|
+
Keywords.verify_crc("pcrc32", @pcrc32, data)
|
|
240
|
+
Keywords.verify_crc("crc32", @crc32, data) if @crc32 && whole_file?
|
|
241
|
+
Part.new(name: @name, file_size: @file_size, line_length: @line_length, data: data,
|
|
242
|
+
part: @part_number, total: @total, begin_byte: @begin_byte, end_byte: @end_byte,
|
|
243
|
+
crc32: @crc32, pcrc32: @pcrc32)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def whole_file?
|
|
247
|
+
@part_number.nil? || (@part_size == @file_size && @begin_byte == 1)
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
private_constant :Decoder
|
|
251
|
+
|
|
252
|
+
# Places decoded parts at their =ypart offsets.
|
|
253
|
+
class Combiner
|
|
254
|
+
def initialize(parts)
|
|
255
|
+
@parts = Array(parts)
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def file
|
|
259
|
+
raise YencError, "no yEnc parts to combine" if @parts.empty?
|
|
260
|
+
|
|
261
|
+
ensure_same_file
|
|
262
|
+
output = ("\0" * @file_size).b
|
|
263
|
+
ranges = []
|
|
264
|
+
@parts.each { |part| place(output, part, ranges) }
|
|
265
|
+
raise YencError, "yEnc file is missing parts" unless covered?(ranges)
|
|
266
|
+
|
|
267
|
+
verify_file_crc(output)
|
|
268
|
+
output
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
private
|
|
272
|
+
|
|
273
|
+
def ensure_same_file
|
|
274
|
+
@name = @parts.first.name
|
|
275
|
+
@file_size = @parts.first.file_size
|
|
276
|
+
return if @parts.all? { |part| part.name == @name && part.file_size == @file_size }
|
|
277
|
+
|
|
278
|
+
raise YencError, "yEnc parts are not from the same file"
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def place(output, part, ranges)
|
|
282
|
+
offset, chunk = located(output, part)
|
|
283
|
+
reject_disagreement(output, chunk, offset, ranges)
|
|
284
|
+
write_chunk(output, chunk, offset, ranges)
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def located(output, part)
|
|
288
|
+
offset = part.begin_byte - 1
|
|
289
|
+
chunk = part.data.b
|
|
290
|
+
check_fit(output, part, offset, chunk)
|
|
291
|
+
[offset, chunk]
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def check_fit(output, part, offset, chunk)
|
|
295
|
+
expected = part.end_byte - part.begin_byte + 1
|
|
296
|
+
fits = offset >= 0 && expected == chunk.bytesize && offset + chunk.bytesize <= output.bytesize
|
|
297
|
+
raise YencError, "yEnc part does not fit in the file" unless fits
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def write_chunk(output, chunk, offset, ranges)
|
|
301
|
+
length = chunk.bytesize
|
|
302
|
+
output[offset, length] = chunk unless length.zero?
|
|
303
|
+
ranges << [offset, offset + length]
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def reject_disagreement(output, chunk, offset, ranges)
|
|
307
|
+
finish = offset + chunk.bytesize
|
|
308
|
+
ranges.each do |start, stop|
|
|
309
|
+
overlap_start = [start, offset].max
|
|
310
|
+
overlap_end = [stop, finish].min
|
|
311
|
+
next if overlap_start >= overlap_end
|
|
312
|
+
|
|
313
|
+
compare_overlap(output, chunk, offset, overlap_start, overlap_end)
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def compare_overlap(output, chunk, offset, overlap_start, overlap_end)
|
|
318
|
+
width = overlap_end - overlap_start
|
|
319
|
+
existing = output.byteslice(overlap_start, width)
|
|
320
|
+
incoming = chunk.byteslice(overlap_start - offset, width)
|
|
321
|
+
raise YencError, "yEnc parts overlap and disagree" unless existing == incoming
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def covered?(ranges)
|
|
325
|
+
merged = merge(ranges.reject { |start, finish| start == finish })
|
|
326
|
+
return merged.empty? if @file_size.zero?
|
|
327
|
+
|
|
328
|
+
merged == [[0, @file_size]]
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def merge(ranges)
|
|
332
|
+
merged = []
|
|
333
|
+
ranges.sort.each do |start, finish|
|
|
334
|
+
if merged.empty? || start > merged.last[1]
|
|
335
|
+
merged << [start, finish]
|
|
336
|
+
elsif finish > merged.last[1]
|
|
337
|
+
merged.last[1] = finish
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
merged
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def verify_file_crc(output)
|
|
344
|
+
crcs = @parts.filter_map(&:crc32).uniq
|
|
345
|
+
raise YencError, "yEnc parts disagree on crc32" if crcs.size > 1
|
|
346
|
+
|
|
347
|
+
Keywords.verify_crc("crc32", crcs.first, output)
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
private_constant :Combiner
|
|
351
|
+
end
|
|
352
|
+
end
|
data/lib/shiny_nntp.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "shiny_nntp/version"
|
|
4
|
+
require_relative "shiny_nntp/connection"
|
|
5
|
+
|
|
6
|
+
module ShinyNntp
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
|
|
9
|
+
# Raised when a command or response does not follow the NNTP wire rules,
|
|
10
|
+
# or when a command comes back with a response code it was not expecting.
|
|
11
|
+
class ProtocolError < StandardError; end
|
|
12
|
+
|
|
13
|
+
# Raised when a yEnc article is missing, truncated, or fails a size or CRC check.
|
|
14
|
+
class YencError < Error; end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
require_relative "shiny_nntp/protocol"
|
|
18
|
+
require_relative "shiny_nntp/article"
|
|
19
|
+
require_relative "shiny_nntp/yenc"
|
|
20
|
+
require_relative "shiny_nntp/group"
|
|
21
|
+
require_relative "shiny_nntp/session"
|
data/sig/shiny_nntp.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: shiny_nntp
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- rob-lane
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: ffi
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
-
|
|
17
|
+
- ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 1.17.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
-
|
|
25
|
+
- ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: 1.17.0
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: pry-byebug
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
-
|
|
33
|
+
- ">="
|
|
34
|
+
- !ruby/object:Gem::Version
|
|
35
|
+
version: 3.10.1
|
|
36
|
+
type: :development
|
|
37
|
+
prerelease: false
|
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
-
|
|
41
|
+
- ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 3.10.1
|
|
44
|
+
description: Ruby NNTP client library. Supports SSL/TLS and STARTTLS. Depends on FFI and the RapidYenc C library.
|
|
45
|
+
email:
|
|
46
|
+
- rob@shiny.systems
|
|
47
|
+
executables:
|
|
48
|
+
- shiny_nntp
|
|
49
|
+
extensions: []
|
|
50
|
+
extra_rdoc_files: []
|
|
51
|
+
files:
|
|
52
|
+
- CHANGELOG.md
|
|
53
|
+
- CODE_OF_CONDUCT.md
|
|
54
|
+
- LICENSE.txt
|
|
55
|
+
- README.md
|
|
56
|
+
- Rakefile
|
|
57
|
+
- exe/shiny_nntp
|
|
58
|
+
- lib/shiny_nntp.rb
|
|
59
|
+
- lib/shiny_nntp/article.rb
|
|
60
|
+
- lib/shiny_nntp/connection.rb
|
|
61
|
+
- lib/shiny_nntp/group.rb
|
|
62
|
+
- lib/shiny_nntp/protocol.rb
|
|
63
|
+
- lib/shiny_nntp/rapid_yenc.rb
|
|
64
|
+
- lib/shiny_nntp/session.rb
|
|
65
|
+
- lib/shiny_nntp/ssl_connection.rb
|
|
66
|
+
- lib/shiny_nntp/version.rb
|
|
67
|
+
- lib/shiny_nntp/yenc.rb
|
|
68
|
+
- sig/shiny_nntp.rbs
|
|
69
|
+
homepage: "https://gitlab.com/rlane/shiny_nntp"
|
|
70
|
+
licenses:
|
|
71
|
+
- MIT
|
|
72
|
+
metadata:
|
|
73
|
+
allowed_push_host: "https://rubygems.org"
|
|
74
|
+
homepage_uri: "https://gitlab.com/rlane/shiny_nntp"
|
|
75
|
+
source_code_uri: "https://gitlab.com/rlane/shiny_nntp/"
|
|
76
|
+
changelog_uri: "https://gitlab.com/rlane/shiny_nntp/-/blob/main/CHANGELOG.md"
|
|
77
|
+
rdoc_options: []
|
|
78
|
+
require_paths:
|
|
79
|
+
- lib
|
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
-
|
|
83
|
+
- ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: 3.2.0
|
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
-
|
|
89
|
+
- ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: "0"
|
|
92
|
+
requirements: []
|
|
93
|
+
rubygems_version: 4.1.0
|
|
94
|
+
specification_version: 4
|
|
95
|
+
summary: Ruby NNTP client library.
|
|
96
|
+
test_files: []
|