Commit eaf21885b8b931e81efbde06220cbbf56126d149

Parents: d564bf8646f5fdd3a095a885bf5eaf5a8748fa3e

From: Moritz Poldrack <git@moritz.sh>
Date: Sat Jun 11 15:32:58 2022 +0700

added NEWGROUPS command

		

Stats

README.md +2/-1
list.go +56/-0
reader.go +28/-0
reader_test.go +9/-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
diff --git a/README.md b/README.md
index ac50bdeb6d82053985e5ff082149ea3c9c8ca1cd..d5d1516f2711580c75195fa6b0781de13e87d4b0 100644
--- a/README.md
+++ b/README.md
@@ -35,10 +35,11 @@ | DATE              | READER        | 🗸           | RFC3977  |
 | GROUP             | READER        | 🗸           | RFC3977  |
 | LAST              | READER        |            | RFC3977  |
 | LISTGROUP         | READER        | 🗸           | RFC3977  |
-| NEWGROUPS         | READER        |            | RFC3977  |
+| NEWGROUPS²        | READER        | ?           | RFC3977  |
 | NEXT              | READER        |            | RFC3977  |
 
 ¹) if you happen to know a provider using SASL, please let me know
+²) untested, because my provider does not follow standards
 
 ## Development
 
diff --git a/list.go b/list.go
new file mode 100644
index 0000000000000000000000000000000000000000..398f450d90d0ec521131d99f0ef70898e8aaae54
--- /dev/null
+++ b/list.go
@@ -0,0 +1,56 @@
+package nntp
+
+import (
+	"strconv"
+	"strings"
+)
+
+type GroupOverview struct {
+	Name          string
+	HighWaterMark ArticleNumber
+	LowWaterMark  ArticleNumber
+	Status        GroupPosting
+}
+
+type GroupPosting int
+
+const (
+	GroupPostingAllowed GroupPosting = iota
+	GroupPostingForbidden
+	GroupPostingModerated
+)
+
+func ParseGroupOverview(description string) GroupOverview {
+	segments := strings.SplitN(description, " ", 4)
+
+	gO := GroupOverview{
+		Name: segments[0],
+	}
+
+	if len(segments) >= 2 {
+		num, err := strconv.ParseUint(segments[1], 10, 64)
+		if err == nil {
+			gO.HighWaterMark = ArticleNumber(num)
+		}
+	}
+
+	if len(segments) >= 3 {
+		num, err := strconv.ParseUint(segments[2], 10, 64)
+		if err == nil {
+			gO.LowWaterMark = ArticleNumber(num)
+		}
+	}
+
+	status := GroupPostingForbidden
+	if len(segments) >= 4 {
+		switch segments[3] {
+		case "y":
+			status = GroupPostingAllowed
+		case "m":
+			status = GroupPostingModerated
+		}
+	}
+	gO.Status = status
+
+	return gO
+}
diff --git a/reader.go b/reader.go
index 792d22535ed1034a6c8392626454c033c5c1bf74..f9b3f6ebf0c0385638623297d367aeb9c9b2d1ef 100644
--- a/reader.go
+++ b/reader.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -149,3 +150,30 @@ 	default:
 		return nil, ErrUnexpectedResponse
 	}
 }
+
+func (c *Conn) NewGroups(ctx context.Context, since time.Time) ([]GroupOverview, error) {
+	if c.caps&CapReader == 0 {
+		return nil, ErrCapabilityNotSupported
+	}
+
+	resp, err := c.Cmd(ctx, "NEWGROUPS %s GMT", since.In(time.FixedZone("GMT", 0)).Format("20060102 150405"))
+	if err != nil {
+		return nil, fmt.Errorf("failed to list new group since %s: %w", since.Format(time.RFC3339), err)
+	}
+
+	var res []GroupOverview
+	for _, row := range strings.Split(string(resp.Body), "\r\n") {
+		res = append(res, ParseGroupOverview(row))
+	}
+
+	switch resp.Status.Code {
+	case StatusArticleNumbersFollow:
+		return res, 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 55404d67665056818381f74b4a64868cc090a9fa..468f84520a2bc94279cf1898f85a90201fd3a680 100644
--- a/reader_test.go
+++ b/reader_test.go
@@ -388,3 +388,12 @@ 		}
 		b.Error("list 1 differs from list 3")
 	}
 }
+
+func TestNewGroups(t *testing.T) {
+	if NewsServerSecure == "" || NewsServerUser == "" || NewsServerPassword == "" {
+		t.Log("secure server address, username, and password required in variables_test.go")
+		t.SkipNow()
+	}
+
+	t.Skip("¯\\_(ツ)_/¯ my provider doesn't support it… Sorry.")
+}