-
Notifications
You must be signed in to change notification settings - Fork 1
/
nntp.go
394 lines (308 loc) · 8.43 KB
/
nntp.go
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
package nntp
import (
"errors"
"fmt"
"io"
"net/textproto"
"strconv"
"strings"
"time"
)
type Client struct {
connection *textproto.Conn
headerFormat *OverviewFormat
}
var ErrInvalidGreetingResponse = errors.New("invalid greeting response returned from server")
func NewFromConn(conn io.ReadWriteCloser) (*Client, error) {
c := &Client{
connection: textproto.NewConn(conn),
}
code, msg, err := c.connection.ReadCodeLine(0)
if err != nil {
return nil, fmt.Errorf("failed to read 'Service Ready' message: %w", err)
}
if code != 200 && code != 201 {
return nil, fmt.Errorf("%w: Allowed codes: 200, 201. Got: %s", ErrInvalidGreetingResponse, msg)
}
return c, nil
}
func (c *Client) Authenticate(username, password string) error {
id := c.connection.Next()
c.connection.StartRequest(id)
defer c.connection.EndRequest(id)
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
if err := c.connection.PrintfLine("AUTHINFO USER %s", username); err != nil {
return err
}
if _, _, err := c.connection.ReadCodeLine(381); err != nil {
return err
}
if err := c.connection.PrintfLine("AUTHINFO PASS %s", password); err != nil {
return err
}
if _, _, err := c.connection.ReadCodeLine(281); err != nil {
return err
}
return nil
}
func (c *Client) Quit() error {
id, err := c.connection.Cmd("QUIT")
if err != nil {
return err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
if _, _, err := c.connection.ReadCodeLine(205); err != nil {
return err
}
return nil
}
func (c *Client) Help() (string, error) {
id, err := c.connection.Cmd("HELP")
if err != nil {
return "", err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
if _, _, err := c.connection.ReadCodeLine(100); err != nil {
return "", err
}
lines, err := c.connection.ReadDotLines()
if err != nil {
return "", err
}
b := &strings.Builder{}
for _, l := range lines {
b.WriteString(l + "\n")
}
return b.String(), err
}
func (c *Client) Date() (time.Time, error) {
const nntpDateLayout = "20060102150405"
id, err := c.connection.Cmd("DATE")
if err != nil {
return time.Time{}, err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
_, s, err := c.connection.ReadCodeLine(111)
if err != nil {
return time.Time{}, err
}
date, err := time.ParseInLocation(nntpDateLayout, s, time.UTC)
if err != nil {
return time.Time{}, fmt.Errorf("failed to parse returned date: %w", err)
}
return date, nil
}
type NewsgroupStatus string
const (
// Posting is permitted
NewsgroupStatusPostingPermitted NewsgroupStatus = "y"
// Posting is not permitted
NewsgroupStatusPostingProhibited NewsgroupStatus = "n"
// Postings will be forwarded to the newsgroup moderator
NewsgroupStatusPostingModerated NewsgroupStatus = "m"
)
type NewsgroupOverview struct {
Name string
Low uint64
High uint64
Status NewsgroupStatus
}
func (c *Client) Newsgroups(since time.Time) ([]NewsgroupOverview, error) {
id, err := c.connection.Cmd("NEWGROUPS %s", since.UTC().Format("060102 150405 GMT"))
if err != nil {
return nil, err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
if _, _, err := c.connection.ReadCodeLine(231); err != nil {
return nil, err
}
lines, err := c.connection.ReadDotLines()
if err != nil {
return nil, err
}
groups := make([]NewsgroupOverview, len(lines))
for i := range lines {
groups[i], err = parseNewsgroupOverview(lines[i])
if err != nil {
return nil, fmt.Errorf("failed to parse newsgroup line '%s'. %w", lines[i], err)
}
}
return groups, nil
}
var (
ErrInvalidNewsgroupOverviewLineReturned = errors.New("invalid news group overview line returned. Line must consist of 4 parts separated by space")
ErrInvalidNewsGroupStatus = errors.New("invalid newsgroup status. Allowed: y,n,m")
)
func parseNewsgroupOverview(line string) (group NewsgroupOverview, err error) {
parts := strings.Split(line, " ")
if len(parts) != 4 {
return group, fmt.Errorf(
"%w: Got %d",
ErrInvalidNewsgroupOverviewLineReturned,
len(parts),
)
}
group.Name = parts[0]
switch strings.ToLower(parts[3]) {
case "y", "n", "m":
group.Status = NewsgroupStatus(strings.ToLower(parts[3]))
default:
return group, fmt.Errorf("%w: Got '%s'", ErrInvalidNewsGroupStatus, parts[3])
}
if group.High, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return group, fmt.Errorf("failed to parse high '%s': %w", parts[1], err)
}
if group.Low, err = strconv.ParseUint(parts[2], 10, 64); err != nil {
return group, fmt.Errorf("failed to parse low '%s': %w", parts[2], err)
}
return group, err
}
type NewsgroupDetail struct {
Name string
Low uint64
High uint64
Number uint64
}
var ErrInvalidNewsgroupLineReturned = errors.New("invalid news group line returned. Line must consist of 4 parts separated by space")
func (c *Client) Group(g string) (group NewsgroupDetail, err error) {
id, err := c.connection.Cmd("GROUP %s", g)
if err != nil {
return group, err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
_, line, err := c.connection.ReadCodeLine(211)
if err != nil {
return group, err
}
parts := strings.Split(line, " ")
if len(parts) != 4 {
return group, fmt.Errorf(
"%w: Got %d",
ErrInvalidNewsgroupLineReturned,
len(parts),
)
}
group.Name = parts[3]
if group.Number, err = strconv.ParseUint(parts[0], 10, 64); err != nil {
return group, fmt.Errorf("failed to parse number '%s': %w", parts[0], err)
}
if group.Low, err = strconv.ParseUint(parts[1], 10, 64); err != nil {
return group, fmt.Errorf("failed to parse low '%s': %w", parts[1], err)
}
if group.High, err = strconv.ParseUint(parts[2], 10, 64); err != nil {
return group, fmt.Errorf("failed to parse high '%s': %w", parts[2], err)
}
return group, err
}
func (c *Client) SetOverviewFormat(format *OverviewFormat) {
c.headerFormat = format
}
func (c *Client) InitializeOverviewFormat() error {
id, err := c.connection.Cmd("LIST OVERVIEW.FMT")
if err != nil {
return err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
if _, _, err = c.connection.ReadCodeLine(215); err != nil {
return err
}
lines, err := c.connection.ReadDotLines()
if err != nil {
return err
}
c.headerFormat = NewOverviewFormat(lines)
return nil
}
func (c *Client) Xover(r string) ([]Header, error) {
if c.headerFormat == nil {
if err := c.InitializeOverviewFormat(); err != nil {
return nil, fmt.Errorf("failed to initialize overview format: %w", err)
}
}
id, err := c.connection.Cmd("XOVER %s", r)
if err != nil {
return nil, err
}
c.connection.StartResponse(id)
defer c.connection.EndResponse(id)
if _, _, err = c.connection.ReadCodeLine(224); err != nil {
return nil, err
}
lines, err := c.connection.ReadDotLines()
if err != nil {
return nil, err
}
headers := make([]Header, len(lines))
for idx := range lines {
headers[idx], err = c.headerFormat.ParseXoverLine(lines[idx])
if err != nil {
return nil, fmt.Errorf("failed to parse line '%s': %w", lines[idx], err)
}
}
return headers, nil
}
func (c *Client) XoverChan(r string) (chan Header, chan error, error) {
if c.headerFormat == nil {
if err := c.InitializeOverviewFormat(); err != nil {
return nil, nil, fmt.Errorf("failed to initialize overview format: %w", err)
}
}
id, err := c.connection.Cmd("XOVER %s", r)
if err != nil {
return nil, nil, err
}
c.connection.StartResponse(id)
if _, _, err = c.connection.ReadCodeLine(224); err != nil {
c.connection.EndResponse(id)
return nil, nil, err
}
headerChan := make(chan Header, 1024)
errChan := make(chan error)
go func() {
defer c.connection.EndResponse(id)
defer close(headerChan)
defer close(errChan)
for {
line, err := c.connection.ReadLine()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
errChan <- err
return
}
// Dot by itself marks end; otherwise cut one dot.
if len(line) > 0 && line[0] == '.' {
if len(line) == 1 {
return
}
line = line[1:]
}
header, err := c.headerFormat.ParseXoverLine(line)
if err != nil {
errChan <- fmt.Errorf("failed to parse line '%s': %w", line, err)
continue
}
headerChan <- header
}
}()
return headerChan, errChan, nil
}
type Header struct {
MessageNumber uint64
Subject string
Author string
Date time.Time
MessageID string
References string
Bytes uint64
Lines uint64
Additional map[string]string
}