forked from fluffle/goirc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rbot.go
163 lines (146 loc) · 3.6 KB
/
rbot.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
package main
import (
irc "github.com/fluffle/goirc/client"
"fmt"
"os"
"strings"
"time"
"net"
"bufio"
"crypto/tls"
"crypto/rand"
"goconfig"
)
const confFile = "rbot.conf"
var trigger string
var sections []string
var conf *config.Config
func main() {
readConf()
trigger = readConfString("DEFAULT", "trigger")
readAuth()
identdport, _ := conf.String("DEFAULT", "identdport")
if identdport != "" && identdport != "0" {
go identd(identdport)
}
sections = conf.Sections()
for _, s := range sections {
if strings.Index(s, " ") == -1 && s != "DEFAULT" {
// found a network
go connect(s)
}
}
select { }
}
func connect(network string) {
if !readConfBool(network, "autoconnect") {
return
}
server := readConfString(network, "server")
nick := readConfString(network, "nick")
user := readConfString(network, "user")
nickserv, _ := conf.String(network, "nickserv")
c := irc.New(nick, user, user)
c.Network = network
c.SSL = readConfBool(network, "ssl")
if c.SSL {
// we don't care about certificate validity
c.SSLConfig = &tls.Config{Rand: rand.Reader, Time: time.Nanoseconds}
}
c.AddHandler("connected",
func(conn *irc.Conn, line *irc.Line) {
fmt.Printf("Connected to %s!\n", conn.Host)
if len(nickserv) > 0 {
conn.Privmsg("NickServ", "IDENTIFY " + nickserv)
} else {
autojoin(conn)
}
})
c.AddHandler("privmsg", handlePrivmsg)
c.AddHandler("mode", handleMode)
c.AddHandler("join", handleJoin)
c.AddHandler("invite", handleInvite)
for {
fmt.Printf("Connecting to %s...\n", server)
if err := c.Connect(server); err != nil {
fmt.Printf("Connection error: %s\n", err)
}
for c.Connected {
if err := <-c.Err; err != nil {
fmt.Printf("goirc error: %s", err)
}
}
time.Sleep(30000000000) // 30 seconds
}
}
func autojoin(conn *irc.Conn) {
for _, s := range sections {
split := strings.SplitN(s, " ", 2)
if len(split) == 2 && split[0] == conn.Network {
// found a channel
if readConfBool(s, "autojoin") {
fmt.Printf("Joining %s on %s\n", split[1], conn.Network)
conn.Join(split[1])
}
if annstream, _ := conf.Bool(s, "stream"); annstream {
announceJustinTv(conn, split[1])
}
}
}
}
func readConf() {
var err os.Error
conf, err = config.ReadDefault("rbot.conf")
if (err != nil) {
fmt.Printf("Config error: %s\n", err)
os.Exit(1)
}
}
func readConfString(section, option string) string {
value, err := conf.String(section, option)
if err != nil {
panic(fmt.Sprintf("Config error: %s", err))
}
return value
}
func readConfBool(section, option string) bool {
value, err := conf.Bool(section, option)
if err != nil {
panic(fmt.Sprintf("Config error: %s", err))
}
return value
}
func updateConf(section, option, value string) {
conf.AddOption(section, option, value)
if err := conf.WriteFile(confFile, 0644, ""); err != nil {
panic("Error while writing to " + confFile)
}
}
func identd(port string) {
identd, err := net.Listen("tcp", "0.0.0.0:" + port)
if err != nil {
fmt.Println("Failed to start identd on port", port)
return
}
defer identd.Close()
fmt.Println("Started identd on port", port)
for {
conn, err := identd.Accept()
if err != nil {
fmt.Println("Failed to accept identd connection")
continue
}
io := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
line, err := io.Reader.ReadString('\n')
if err != nil || len(line) < 2 {
conn.Close()
fmt.Println("Failed to read identd request")
continue
}
line = line[:len(line) - 2]
line = fmt.Sprintf("%s : ERROR : NO-USER\r\n", line)
io.Writer.WriteString(line)
time.Sleep(1000000000) // 1 second
conn.Close()
}
}