Commit 27d49fd8496c045f9c176d63b0efc40d3394adcf

Parents: 2eef2f29849789b1430d9a006d8ee08634e653c7

From: Moritz Poldrack <git@moritz.sh>
Date: Fri Jun 10 21:23:43 2022 +0700

added HEAD retrieval

		

Stats

README.md +1/-1
mandatory.go +41/-0
mandatory_test.go +115/-0
message-identifier.go +32/-0
status.go +23/-14

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
diff --git a/README.md b/README.md
index 49dfd6e1cfd26fae0d36f7615147324dcac347a4..e0a6f58d05ad45bf73e94816d588defe449e3a93 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@
 | Command           | Capability    | Implemented | Standard |
 |-------------------|---------------|-------------|----------|
 | CAPABILITIES      | mandatory     | 🗸           | RFC3977  |
-| HEAD              | mandatory     | ☓           | RFC3977  |
+| HEAD              | mandatory     | 🗸           | RFC3977  |
 | HELP              | mandatory     | ☓           | RFC3977  |
 | QUIT              | mandatory     | ☓           | RFC3977  |
 | STAT              | mandatory     | ☓           | RFC3977  |
diff --git a/mandatory.go b/mandatory.go
new file mode 100644
index 0000000000000000000000000000000000000000..399b18a24b8100693c7c37913a8f7aacc3fd7abd
--- /dev/null
+++ b/mandatory.go
@@ -0,0 +1,41 @@
+package nntp
+
+import (
+	"bufio"
+	"bytes"
+	"context"
+	"fmt"
+	"net/textproto"
+)
+
+func (c *Conn) Head(ctx context.Context, msg MessageIdentifier) (map[string][]string, error) {
+	resp, err := c.Cmd(ctx, "HEAD %s", msg.msgID())
+	if err != nil {
+		return nil, fmt.Errorf("failed to retrieve header for article '%s': %w", msg.msgID(), err)
+	}
+
+	var mimeHeader map[string][]string
+	if resp.Body != nil {
+		bufReader := bufio.NewReader(bytes.NewReader(append(resp.Body[1:], '\r', '\n')))
+		tpreader := textproto.NewReader(bufReader)
+		mimeHeader, err = tpreader.ReadMIMEHeader()
+		if err != nil {
+			return nil, fmt.Errorf("unable to parse headers: %v", err)
+		}
+	}
+
+	switch resp.Status.Code {
+	case StatusHeadersFollow:
+		return mimeHeader, nil
+	case StatusNoNewsgroupSelected:
+		return nil, ErrNoNewsgroupSelected
+	case StatusCurrentArticleNumberInvalid:
+		return nil, ErrCurrentArticleNumberInvalid
+	case StatusNoArticleWithGivenNumber:
+		return nil, fmt.Errorf("%w '%s'", ErrNoArticleWithGivenNumber, msg.msgID())
+	case StatusNoArticleWithGivenMessageID:
+		return nil, fmt.Errorf("%w '%s'", ErrNoArticleWithGivenMessageID, msg.msgID())
+	default:
+		return nil, ErrUnexpectedResponse
+	}
+}
diff --git a/mandatory_test.go b/mandatory_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..323f73ebb377a264b383680b87470432e2199acd
--- /dev/null
+++ b/mandatory_test.go
@@ -0,0 +1,115 @@
+package nntp_test
+
+import (
+	"context"
+	"errors"
+	"testing"
+	"time"
+
+	"mpldr.codes/usenet/nntp"
+)
+
+func TestHead(t *testing.T) {
+	if NewsServerSecure == "" || NewsServerUser == "" || NewsServerPassword == "" {
+		t.Log("secure server address, username, and password required in variables_test.go")
+		t.SkipNow()
+	}
+
+	tests := []struct {
+		name          string
+		message       nntp.MessageIdentifier
+		expectedError error
+		preCommand    func(*nntp.Conn) error
+	}{
+		/*{ // Eweka send 430 instead
+			name:          "no-group-selected",
+			message:       nntp.CurrentMessage,
+			expectedError: nntp.ErrNoNewsgroupSelected,
+			preCommand: func(c *nntp.Conn) error {
+				return c.Group("alt.binaries.test")
+			},
+		},*/
+		{
+			name:          "article-number",
+			message:       nntp.ArticleNumber(5666767574),
+			expectedError: nil,
+			preCommand: func(c *nntp.Conn) error {
+				ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+				defer cancel()
+				return c.Group(ctx, "alt.binaries.test")
+			},
+		},
+		/*{ // Eweka sends 430 instead
+			name:          "invalid-number",
+			message:       nntp.ArticleNumber(2),
+			expectedError: nntp.ErrNoArticleWithGivenNumber,
+			preCommand: func(c *nntp.Conn) error {
+				return c.Group("alt.binaries.test")
+			},
+		},*/
+		{
+			name:          "invalid-id",
+			message:       nntp.MessageID("billy@bub"),
+			expectedError: nntp.ErrNoArticleWithGivenMessageID,
+			preCommand: func(c *nntp.Conn) error {
+				ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+				defer cancel()
+				return c.Group(ctx, "alt.binaries.test")
+			},
+		},
+		{
+			name:          "message-id",
+			message:       nntp.MessageID("64dae59a2ab54b328181a29e35462fb5@ngPost"),
+			expectedError: nil,
+			preCommand: func(c *nntp.Conn) error {
+				ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+				defer cancel()
+				return c.Group(ctx, "alt.binaries.test")
+			},
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			c, err := nntp.Dial(NewsServerPlain, nntp.OptionUnencrypted)
+			if err != nil {
+				t.Skipf("connection to '%s' failed: %v", NewsServerSecure, err)
+			}
+
+			ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+			defer cancel()
+			err = c.LoginUserPass(ctx, NewsServerUser, NewsServerPassword)
+			if err != nil {
+				t.Skipf("failed to authenticate: %v", err)
+			}
+
+			err = test.preCommand(c)
+			if err != nil {
+				t.Skipf("failed preparation: %v", err)
+			}
+
+			ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
+			defer cancel()
+			header, err := c.Head(ctx, test.message)
+			if !errors.Is(err, test.expectedError) {
+				t.Errorf("unexpected result:\n\tgot:      %v\n\texpected: %v\n", err, test.expectedError)
+			}
+
+			if test.expectedError != nil {
+				return
+			}
+
+			if len(header["From"]) == 0 {
+				t.Errorf("From: not set")
+			}
+
+			if len(header["Message-Id"]) == 0 {
+				t.Errorf("Message-ID: not set")
+			}
+
+			if t.Failed() {
+				// t.Logf("Headers: %#v", header)
+			}
+		})
+	}
+}
diff --git a/message-identifier.go b/message-identifier.go
new file mode 100644
index 0000000000000000000000000000000000000000..af985d3adea286035098761708b138cc63ace093
--- /dev/null
+++ b/message-identifier.go
@@ -0,0 +1,32 @@
+package nntp
+
+import "fmt"
+
+// MessageID represents a message's globally unique identifier.
+type MessageID string
+
+func (m MessageID) msgID() string {
+	return fmt.Sprintf("<%s>", m)
+}
+
+// ArticleNumber represents a message's index in a selected group.
+type ArticleNumber uint64
+
+func (i ArticleNumber) msgID() string {
+	return fmt.Sprint(i)
+}
+
+type current struct{}
+
+func (c current) msgID() string {
+	return ""
+}
+
+// MessageIdentifier is a common interface for types representing message
+// identifiers
+type MessageIdentifier interface {
+	msgID() string
+}
+
+// CurrentMessage represents the message the Article Pointer is currently at.
+var CurrentMessage = current{}
diff --git a/status.go b/status.go
index 1a0b54b4fd9e92f2f3c382dd966653f66450d539..5d6880a5fb8eefa37e51b6cc2ad26d3917807691 100644
--- a/status.go
+++ b/status.go
@@ -3,21 +3,30 @@
 import "errors"
 
 const (
-	StatusServiceAvailable       = 200
-	StatusServiceNoPosting       = 201
-	StatusGroupSelected          = 211
-	StatusAuthAccepted           = 281
-	StatusAuthAcceptedWithData   = 283
-	StatusAuthPassRequired       = 381
-	StatusAuthSASLContinue       = 383
-	StatusTemporarilyUnavailable = 400
-	StatusAuthFailed             = 481
-	StatusAuthInvalid            = 482
-	StatusNoSuchNewsgroup        = 411
-	StatusPermanentlyUnavailable = 502
+	StatusServiceAvailable            = 200
+	StatusServiceNoPosting            = 201
+	StatusGroupSelected               = 211
+	StatusHeadersFollow               = 221
+	StatusAuthAccepted                = 281
+	StatusAuthAcceptedWithData        = 283
+	StatusAuthPassRequired            = 381
+	StatusAuthSASLContinue            = 383
+	StatusTemporarilyUnavailable      = 400
+	StatusNoSuchNewsgroup             = 411
+	StatusNoNewsgroupSelected         = 412
+	StatusCurrentArticleNumberInvalid = 420
+	StatusNoArticleWithGivenNumber    = 423
+	StatusNoArticleWithGivenMessageID = 430
+	StatusAuthFailed                  = 481
+	StatusAuthInvalid                 = 482
+	StatusPermanentlyUnavailable      = 502
 )
 
 var (
-	ErrNoSuchNewsgroup    = errors.New("no such newsgroup")
-	ErrUnexpectedResponse = errors.New("received unexpected response code")
+	ErrCurrentArticleNumberInvalid = errors.New("current article number invalid")
+	ErrNoArticleWithGivenNumber    = errors.New("no article with that number")
+	ErrNoArticleWithGivenMessageID = errors.New("no article with that message-ID")
+	ErrNoSuchNewsgroup             = errors.New("no such newsgroup")
+	ErrNoNewsgroupSelected         = errors.New("no newsgroup selected")
+	ErrUnexpectedResponse          = errors.New("received unexpected response code")
 )