-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestRead.go
116 lines (94 loc) · 2.61 KB
/
testRead.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync/atomic"
"github.com/Tensai75/nntp"
)
var (
testReadCounter atomic.Int64
)
func (c *safeConn) testHead(messageId string) (*nntp.Article, error) {
// for reading error testing
/*
counter := testReadCounter.Add(1)
if counter%150 == 0 {
return nil, fmt.Errorf("Test Error")
}
*/
return readHeadFromFile(messageId)
}
func (c *safeConn) testBody(messageId string) (io.Reader, error) {
// for reading error testing
/*
counter := testReadCounter.Add(1)
if counter%150 == 0 {
return nil, fmt.Errorf("Test Error")
}
*/
return readBodyFromFile(messageId)
}
func (c *safeConn) testArticle(messageId string) (*nntp.Article, error) {
// for reading error testing
/*
counter := testReadCounter.Add(1)
if counter%150 == 0 {
return nil, fmt.Errorf("Test Error")
}
*/
return readArticleFromFile(messageId)
}
func readHead(conn *safeConn, messageId string) (*nntp.Article, error) {
if conf.Test != "" {
return conn.testHead(messageId)
}
return conn.Head(fmt.Sprintf("<%v>", messageId))
}
func readBody(conn *safeConn, messageId string) (io.Reader, error) {
if conf.Test != "" {
return conn.testBody(messageId)
}
return conn.Body(fmt.Sprintf("<%v>", messageId))
}
func readArticle(conn *safeConn, messageId string) (*nntp.Article, error) {
if conf.Test != "" {
return conn.testArticle(messageId)
}
return conn.Article(fmt.Sprintf("<%v>", messageId))
}
func readHeadFromFile(messageId string) (*nntp.Article, error) {
article, err := readArticleFromFile(messageId)
article.Body = nil
return article, err
}
func readBodyFromFile(messageId string) (io.Reader, error) {
article, err := readArticleFromFile(messageId)
return article.Body, err
}
func readArticleFromFile(messageId string) (*nntp.Article, error) {
var (
readFile []byte
article nntp.Article
body bytes.Buffer
)
if readFile, err = os.ReadFile(filepath.Join(conf.Test, messageId+".txt")); err != nil {
return nil, fmt.Errorf("Unable to load message <%v>", messageId)
}
expHeader := regexp.MustCompile(`(Subject|Date|From|Message-ID|Path|Newsgroups|X-Nxg): (.*)[ \n\r]`)
headers := expHeader.FindAllStringSubmatch(string(readFile), -1)
article.Header = make(map[string][]string)
for _, header := range headers {
headerName := strings.Trim(header[1], " \r\n")
value := strings.Trim(header[2], " \r\n")
article.Header[headerName] = append(article.Header[headerName], value)
}
expBody := regexp.MustCompile(`=ybegin[\w\W]+`)
body.Write([]byte(expBody.FindString(string(readFile))))
article.Body = &body
return &article, nil
}