-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgospot.go
99 lines (87 loc) · 2.07 KB
/
gospot.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
package main
import (
"net"
"os"
"fmt"
"bufio"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "Go Spot Go"
app.Usage = "Index your usenet with Go"
app.Flags = []cli.Flag {
cli.StringFlag{
Name: "login",
Value: "",
Usage: "username for your news server",
},
cli.StringFlag{
Name: "password",
Value: "",
Usage: "password for your news server",
},
cli.StringFlag{
Name: "ip",
Value: "news.astraweb.com",
Usage: "address of your news server",
},
cli.StringFlag{
Name: "port",
Value: "119",
Usage: "port number of your news server",
},
}
app.Commands = []cli.Command{
{
Name: "groups",
Usage: "list the newsgroups, then quit",
Action: func(c *cli.Context) {
conn := login(c)
defer conn.Close()
conn.Write([]byte("list\n"))
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading groups error:", err)
}
},
},
}
app.Run(os.Args)
}
func login(c *cli.Context) (conn *net.TCPConn) {
ip, port := c.GlobalString("ip"), c.GlobalString("port")
newsAddr, err := net.ResolveTCPAddr("tcp", ip + ":" + port)
if err != nil {
println("Could not resolve", ip, "on", port)
os.Exit(1)
}
conn, err = net.DialTCP("tcp", nil, newsAddr)
if err != nil {
println("Could not connect to", ip, "on", port)
os.Exit(1)
}
connbuf := bufio.NewReader(conn)
str, err := connbuf.ReadString('\n')
login, password := c.GlobalString("login"), c.GlobalString("password")
conn.Write([]byte("authinfo\n"))
str, err = connbuf.ReadString('\n')
conn.Write([]byte("authinfo user " + login + "\n"))
str, err = connbuf.ReadString('\n')
if err != nil || str != "381 PASS required\r\n" {
fmt.Println("Unable to send username:", login)
fmt.Println(str)
os.Exit(1)
}
conn.Write([]byte("authinfo pass " + password + "\n"))
str, err = connbuf.ReadString('\n')
if err != nil || str != "281 Ok\r\n" {
fmt.Println("Unable to login")
fmt.Println(str)
os.Exit(1)
}
return conn
}