/
rusty.go
211 lines (185 loc) · 4.57 KB
/
rusty.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
package main
import (
"crypto/tls"
"encoding/json"
//"flag"
"fmt"
"io/ioutil"
//"log"
"os"
"github.com/codegangsta/cli"
irc "github.com/fluffle/goirc/client"
//"github.com/fluffle/goirc/logging/glog"
"github.com/williballenthin/govt"
)
var CONFIG_FILE string
var RC RustyConfig
func GetFlags() []cli.Flag {
var flags = []cli.Flag{
// IRC config
cli.StringFlag{
Name: "host, H",
Usage: "hostname of the IRC server",
Value: "irc.freenode.net",
},
cli.IntFlag{
Name: "port, P",
Usage: "port of the IRC server",
Value: 6697,
},
cli.StringFlag{
Name: "nick, n",
Usage: "nick of the bot",
Value: "rusty-r0b0t",
},
cli.StringFlag{
Name: "channel, c",
Usage: "channel to join",
Value: "#inventropy",
},
cli.BoolTFlag{
Name: "insecure, N",
Usage: "disable SSL",
},
// General
cli.BoolFlag{
Name: "verbose, V",
Usage: "verbose output",
},
}
return flags
}
func SetupForRun(c *cli.Context) error {
LoadConfig(c)
RegisterHandlers()
return nil
}
func SetupApp(args []string) {
app := cli.NewApp()
app.Name = "rusty"
app.Usage = "Ol' rusty"
app.Version = "0.1a"
app.Commands = []cli.Command{
cli.Command{
Name: "run",
Action: Connect,
Usage: "Run ol' Rusty!",
Flags: GetFlags(),
Before: SetupForRun,
},
cli.Command{
Name: "test",
Action: func(c *cli.Context) {
fmt.Printf(".")
},
Usage: "For testing purposes",
Flags: GetFlags(),
Before: SetupForRun,
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, C",
Usage: "location of your configuration file",
Value: GetConfigPath(),
Destination: &CONFIG_FILE,
},
}
app.Run(args)
}
func GetConfigPath() string {
temp_name := os.ExpandEnv("$HOME/.config/rusty-robot/rusty.rc")
if _, err := os.Stat(temp_name); err != nil {
temp_name = "/dev/null"
}
return temp_name
}
func LoadConfig(ctx *cli.Context) {
var contents []byte
if _, err := os.Stat(CONFIG_FILE); err != nil && CONFIG_FILE != "/dev/null" {
fmt.Printf("Unable to find config file. Resorting to defaults.\n")
} else {
contents = handle_err(ioutil.ReadFile(CONFIG_FILE)).([]byte)
err := json.Unmarshal(contents, &RC)
if err != nil && CONFIG_FILE != "/dev/null" {
fmt.Printf("Error reading config file (%s): %v.\n", CONFIG_FILE, err)
}
}
// Override config file values with CLI options
if &RC.Host == nil || ctx.IsSet("host") {
RC.Host = ctx.String("host")
}
if &RC.Port == nil || ctx.IsSet("port") {
RC.Port = ctx.Int("port")
}
if &RC.Nick == nil || ctx.IsSet("nick") {
RC.Nick = ctx.String("nick")
}
if &RC.Channel == nil || ctx.IsSet("channel") {
RC.Channel = ctx.String("channel")
}
if &RC.SSL == nil {
RC.SSL = ctx.BoolT("insecure")
}
if &RC.Verbose == nil {
RC.Verbose = ctx.Bool("verbose")
}
RC.VTClient = govt.Client{Apikey: RC.VTAPIKey, Url: "https://www.virustotal.com/vtapi/v2/"}
}
func Connect(ctx *cli.Context) {
fmt.Printf("Connecting to %s...\n", RC.Host)
// create new IRC connection
cfg := irc.NewConfig(RC.Nick, "rusty")
cfg.SSL = RC.SSL
if cfg.SSL {
cfg.SSLConfig = &tls.Config{
MinVersion: 0,
MaxVersion: 0,
InsecureSkipVerify: false,
ServerName: RC.Host,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
}
cfg.Server = fmt.Sprintf("%s:%d", RC.Host, RC.Port)
cfg.NewNick = func(n string) string { return n + "_" }
c := irc.Client(cfg)
c.EnableStateTracking()
c.HandleFunc(irc.CONNECTED,
func(conn *irc.Conn, line *irc.Line) {
fmt.Printf("Connected to %s\n", RC.Host)
conn.Join(RC.Channel)
})
// Set up a handler to notify of disconnect events.
quit := make(chan bool)
c.HandleFunc(irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) { quit <- true })
c.HandleFunc(irc.JOIN, func(conn *irc.Conn, line *irc.Line) {
if line.Nick == conn.Config().Me.String() && line.Args[0] == RC.Channel {
fmt.Printf("Joined channel %s\n", RC.Channel)
}
})
c.HandleFunc(irc.PRIVMSG, handle_privmsg)
reallyquit := false
for !reallyquit {
// connect to server
if err := c.ConnectTo(RC.Host); err != nil {
fmt.Printf("Connection error: %s\n", err)
return
}
// wait on quit channel
<-quit
}
}
func main() {
SetupApp(os.Args)
}