-
Notifications
You must be signed in to change notification settings - Fork 11
/
nntp.go
54 lines (47 loc) · 1.07 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
package main
import (
"fmt"
"strconv"
"sync"
"github.com/Tensai75/nntp"
)
var (
connectionGuard chan struct{}
connectionOnce sync.Once
)
func ConnectNNTP() (*nntp.Conn, error) {
connectionOnce.Do(func() {
connectionGuard = make(chan struct{}, conf.Server.Connections)
})
connectionGuard <- struct{}{} // will block if guard channel is already filled
var conn *nntp.Conn
var err error
if conf.Server.SSL {
conn, err = nntp.DialTLS("tcp", conf.Server.Host+":"+strconv.Itoa(conf.Server.Port), nil)
} else {
conn, err = nntp.Dial("tcp", conf.Server.Host+":"+strconv.Itoa(conf.Server.Port))
}
if err != nil {
fmt.Printf("Connection to usenet server failed: %v\n", err)
conn.Quit()
return nil, err
}
if err := conn.Authenticate(conf.Server.User, conf.Server.Password); err != nil {
conn.Quit()
fmt.Printf("Authentication with usenet server failed: %v\n", err)
return nil, err
}
return conn, nil
}
func DisconnectNNTP(conn *nntp.Conn) {
if conn != nil {
conn.Quit()
select {
case <-connectionGuard:
// go on
default:
// go on
}
}
conn = nil
}