Commit d564bf8646f5fdd3a095a885bf5eaf5a8748fa3e

Parents: 2bae0164e7c0c3554b6f6c2a93fd5b35c82e0417

From: Moritz Poldrack <git@moritz.sh>
Date: Sat Jun 11 12:18:24 2022 +0700

added LISTGROUP command

		

Stats

README.md +1/-1
reader.go +50/-0
reader_test.go +166/-0
status.go +1/-0
testdata/ListGroupShortened +140/-0

Changeset

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
diff --git a/README.md b/README.md
index 4b43d3427f8fb6649b905350a1c2deddbce6ed93..ac50bdeb6d82053985e5ff082149ea3c9c8ca1cd 100644
--- a/README.md
+++ b/README.md
@@ -34,7 +34,7 @@ | BODY              | READER        | πŸ—Έ           | RFC3977  |
 | DATE              | READER        | πŸ—Έ           | RFC3977  |
 | GROUP             | READER        | πŸ—Έ           | RFC3977  |
 | LAST              | READER        | β˜“           | RFC3977  |
-| LISTGROUP         | READER        | β˜“           | RFC3977  |
+| LISTGROUP         | READER        | πŸ—Έ           | RFC3977  |
 | NEWGROUPS         | READER        | β˜“           | RFC3977  |
 | NEXT              | READER        | β˜“           | RFC3977  |
 
diff --git a/reader.go b/reader.go
index 92ff81210e4ce2100c498569010a2192004a5692..792d22535ed1034a6c8392626454c033c5c1bf74 100644
--- a/reader.go
+++ b/reader.go
@@ -3,6 +3,7 @@
 import (
 	"context"
 	"fmt"
+	"strconv"
 	"time"
 )
 
@@ -99,3 +100,52 @@ 	default:
 		return ErrUnexpectedResponse
 	}
 }
+
+func (c *Conn) ListGroup(ctx context.Context, group string, from uint64, to uint64) ([]uint64, error) {
+	if c.caps&CapReader == 0 {
+		return nil, ErrCapabilityNotSupported
+	}
+
+	var param string
+	if group != "" {
+		param += " " + group
+		if from != 0 && to != 0 {
+			param += fmt.Sprintf(" %d-%d", from, to)
+		}
+	}
+
+	resp, err := c.Cmd(ctx, "LISTGROUP"+param)
+	if err != nil {
+		return nil, fmt.Errorf("failed to list content of group '%s': %w", group, err)
+	}
+
+	var parsedList []uint64
+	var numbuf []byte
+	var num uint64
+	for _, bte := range resp.Body {
+		if bte >= 0x30 && bte < 0x3A {
+			numbuf = append(numbuf, bte)
+			continue
+		}
+		if len(numbuf) == 0 {
+			continue
+		}
+		num, err = strconv.ParseUint(string(numbuf), 10, 64)
+		if err != nil {
+			return nil, fmt.Errorf("failed to parse response: %w", err)
+		}
+		parsedList = append(parsedList, num)
+		numbuf = []byte{}
+	}
+
+	switch resp.Status.Code {
+	case StatusArticleNumbersFollow:
+		return parsedList, nil
+	case StatusNoSuchNewsgroup:
+		return nil, ErrNoSuchNewsgroup
+	case StatusNoNewsgroupSelected:
+		return nil, ErrNoNewsgroupSelected
+	default:
+		return nil, ErrUnexpectedResponse
+	}
+}
diff --git a/reader_test.go b/reader_test.go
index 15a138a691d1141d8276ba0d1ac879f4a6bcd885..55404d67665056818381f74b4a64868cc090a9fa 100644
--- a/reader_test.go
+++ b/reader_test.go
@@ -1,10 +1,17 @@
 package nntp_test
 
 import (
+	"bytes"
 	"context"
 	"errors"
+	"fmt"
+	"reflect"
+	"strconv"
 	"testing"
+	"text/scanner"
 	"time"
+
+	_ "embed"
 
 	"mpldr.codes/usenet/nntp"
 )
@@ -222,3 +229,162 @@ 			}
 		})
 	}
 }
+
+func TestListGroup(t *testing.T) {
+	if NewsServerSecure == "" || NewsServerUser == "" || NewsServerPassword == "" {
+		t.Log("secure server address, username, and password required in variables_test.go")
+		t.SkipNow()
+	}
+	if testing.Short() {
+		t.Skip("skipping test in short mode")
+	}
+	t.Parallel()
+	tests := []struct {
+		name          string
+		group         string
+		expectedError error
+		minEntries    int
+	}{
+		/*{ // eweka does not returns no newsgroup selected
+			name:          "invalid-group",
+			group:         "alt.binaries.fritzelschnitzelritzel",
+			expectedError: nntp.ErrNoSuchNewsgroup,
+		},*/
+		{
+			name:          "existing-group",
+			group:         "alt.binaries.dvdr.german",
+			expectedError: nil,
+			minEntries:    11835721,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			c, err := nntp.Dial(NewsServerSecure)
+			if err != nil {
+				t.Skipf("connection to '%s' failed: %v", NewsServerSecure, err)
+			}
+			defer c.Close(context.Background())
+
+			ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+			defer cancel()
+			err = c.LoginUserPass(ctx, NewsServerUser, NewsServerPassword)
+			if err != nil {
+				t.Skipf("login failed: %v", err)
+			}
+
+			ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
+			defer cancel()
+			list, err := c.ListGroup(ctx, test.group, 0, 0)
+			if !errors.Is(err, test.expectedError) {
+				t.Errorf("unexpected result:\n\tgot:      %v\n\texpected: %v\n", err, test.expectedError)
+			}
+			if len(list) < test.minEntries {
+				t.Errorf("less entries than expected: %d < %d", len(list), test.minEntries)
+			}
+		})
+	}
+}
+
+//go:embed testdata/ListGroupShortened
+var groupList []byte
+
+func BenchmarkListGroupListParse(b *testing.B) {
+	var parsedList1 []uint64
+	b.Run("scanner.Scanner", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			var parsedList []uint64
+			var s scanner.Scanner
+			s.Init(bytes.NewReader(groupList))
+			var num uint64
+			var err error
+			for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
+				num, err = strconv.ParseUint(s.TokenText(), 10, 64)
+				if err != nil {
+					b.Fail()
+				}
+				parsedList = append(parsedList, num)
+			}
+			parsedList1 = parsedList
+		}
+	})
+
+	var parsedList2 []uint64
+	b.Run("bytes.Split", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			var parsedList []uint64
+			var num uint64
+			var err error
+			for _, seq := range bytes.Split(groupList, []byte{'\r', '\n'}) {
+				if len(seq) == 0 {
+					continue
+				}
+				num, err = strconv.ParseUint(string(seq), 10, 64)
+				if err != nil {
+					fmt.Printf("%s\n%v\n%v\n", seq, seq, err)
+					b.FailNow()
+				}
+				parsedList = append(parsedList, num)
+			}
+			parsedList2 = parsedList
+		}
+	})
+
+	var parsedList3 []uint64
+	b.Run("byteslice-iter", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			var parsedList []uint64
+			var numbuf []byte
+			var num uint64
+			var err error
+			for _, bte := range groupList {
+				if bte >= 0x30 && bte < 0x3A {
+					numbuf = append(numbuf, bte)
+					continue
+				}
+				if len(numbuf) == 0 {
+					continue
+				}
+				num, err = strconv.ParseUint(string(numbuf), 10, 64)
+				if err != nil {
+					b.FailNow()
+				}
+				parsedList = append(parsedList, num)
+				numbuf = []byte{}
+			}
+			parsedList3 = parsedList
+		}
+	})
+
+	if !reflect.DeepEqual(parsedList1, parsedList2) {
+		minlen := len(parsedList1)
+		if len(parsedList1) != len(parsedList2) {
+			b.Logf("%d != %d", len(parsedList1), len(parsedList2))
+			if len(parsedList2) < minlen {
+				minlen = len(parsedList2)
+			}
+		}
+		for i := 0; i < minlen; i++ {
+			if parsedList1[i] != parsedList2[i] {
+				b.Logf("index %d: %d != %d", i, parsedList1[i], parsedList2[i])
+			}
+		}
+		b.Error("list 1 differs from list 2")
+	}
+
+	if !reflect.DeepEqual(parsedList1, parsedList3) {
+		minlen := len(parsedList1)
+		if len(parsedList1) != len(parsedList3) {
+			b.Logf("%d != %d", len(parsedList1), len(parsedList3))
+			if len(parsedList3) < minlen {
+				minlen = len(parsedList3)
+			}
+		}
+		for i := 0; i < minlen; i++ {
+			if parsedList1[i] != parsedList3[i] {
+				b.Logf("index %d: %d != %d", i, parsedList1[i], parsedList3[i])
+			}
+		}
+		b.Error("list 1 differs from list 3")
+	}
+}
diff --git a/status.go b/status.go
index 1ab9a2bcf7ce1467e8b262c3e3b0fe887ea413ed..2a9df0409a6171d16bb0452bbcd4d35803226ef6 100644
--- a/status.go
+++ b/status.go
@@ -9,6 +9,7 @@ 	StatusServiceAvailable            = 200
 	StatusServiceNoPosting            = 201
 	StatusConnectionClosing           = 205
 	StatusGroupSelected               = 211
+	StatusArticleNumbersFollow        = 211
 	StatusArticleFollows              = 220
 	StatusBodyFollows                 = 222
 	StatusHeadersFollow               = 221
diff --git a/testdata/ListGroupShortened b/testdata/ListGroupShortened
new file mode 100644
index 0000000000000000000000000000000000000000..5ac25325b8ceb15a6981b462f27cab6c4ffcac04
--- /dev/null
+++ b/testdata/ListGroupShortened
@@ -0,0 +1,140 @@
+11853711
+11853712
+11853713
+11853714
+11853715
+11853716
+11853717
+11853718
+11853719
+11853720
+11853721
+11853722
+11853723
+11853724
+11853725
+11853726
+11853727
+11853728
+11853729
+11853730
+11853731
+11853732
+11853733
+11853734
+11853735
+11853736
+11853737
+11853738
+11853739
+11853740
+11853741
+11853742
+11853743
+11853744
+11853745
+11853746
+11853747
+11853748
+11853749
+11853750
+11853751
+11853752
+11853753
+11853754
+11853755
+11853756
+11853757
+11853758
+11853759
+11853760
+11853761
+11853762
+11853763
+11853764
+11853765
+11853766
+11853767
+11853768
+11853769
+11853770
+11853771
+11853772
+11853773
+11853774
+11853775
+11853776
+11853777
+11853778
+11853779
+11853780
+11853781
+11853782
+11853783
+11853784
+11853785
+11853786
+11853787
+11853788
+11853789
+11853790
+11853791
+11853792
+11853793
+11853794
+11853795
+11853796
+11853797
+11853798
+11853799
+11853800
+11853801
+11853802
+11853803
+11853804
+11853805
+11853806
+11853807
+11853808
+11853809
+11853810
+11853811
+11853812
+11853813
+11853814
+11853815
+11853816
+11853817
+11853818
+11853819
+11853820
+11853821
+11853822
+11853823
+11853824
+11853825
+11853826
+11853827
+11853828
+11853829
+11853830
+11853831
+11853832
+11853833
+11853834
+11853835
+11853836
+11853837
+11853838
+11853839
+11853840
+11853841
+11853842
+11853843
+11853844
+11853845
+11853846
+11853847
+11853848
+11853849
+11853850