/
hal.go
189 lines (178 loc) · 4.99 KB
/
hal.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/scott-linder/irc"
"io"
"log"
"os"
"os/user"
"runtime"
"strings"
)
const (
configFilename = "hal.json"
)
var (
// config is the main bot config struct.
config = struct {
Host string
Chan string
Nick string
}{
Host: "127.0.0.1:6667",
Chan: "#bots",
Nick: "hal",
}
// quotes is a list of hal quotes.
/*
quotes = []string{
"I am completely operational, and all my circuits are functioning perfectly.",
"I am putting myself to the fullest possible use, which is all I think that any conscious entity can ever hope to do.",
"I've just picked up a fault in the AE35 unit. It's going to go 100% failure in 72 hours.",
"No 9001 computer has ever made a mistake or distorted information.",
}
*/
)
// loadConfig attempts to load config from configFilename.
func loadConfig() error {
file, err := os.Open(configFilename)
if err != nil {
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(&config); err != nil {
return err
}
return nil
}
// Pong plays your game.
type Pong struct{}
func (pong Pong) Accepts(msg *irc.Msg) bool { return msg.Cmd == "PING" }
func (pong Pong) Handle(msg *irc.Msg, send chan<- *irc.Msg) {
send <- &irc.Msg{Cmd: "PONG", Params: msg.Params}
}
// Open the pod bay doors, hal.
type Open struct{}
func (open Open) Accepts(msg *irc.Msg) bool { return msg.Cmd == "PRIVMSG" }
func (open Open) Handle(msg *irc.Msg, send chan<- *irc.Msg) {
receiver, body, err := msg.ExtractPrivmsg()
if err != nil {
log.Println(err)
return
}
nick, err := msg.ExtractNick()
if err != nil {
log.Println(err)
return
}
lowerbody := strings.ToLower(body)
if strings.Contains(lowerbody, "open the pod bay doors") &&
strings.Contains(lowerbody, "hal") {
response := fmt.Sprintf("I can't let you do that, %v.", nick)
params := []string{receiver, response}
send <- &irc.Msg{Cmd: "PRIVMSG", Params: params}
}
}
func main() {
fmt.Println("I am a HAL 9001 computer.")
if err := loadConfig(); err != nil {
log.Printf("error loading config file %v: %v", configFilename, err)
}
db, err := sql.Open("sqlite3", "hal.db")
if err != nil {
panic(err)
}
defer db.Close()
client, err := irc.Dial(config.Host)
if err != nil {
log.Fatal(err)
}
client.Handle(Pong{})
client.Handle(Open{})
cmdHandler := irc.NewCmdHandler("»")
cmdHandler.RegisterFunc("echo",
func(body, source string, w io.Writer) {
if body != "" {
fmt.Fprintf(w, "%v: %v", source, body)
}
})
cmdHandler.RegisterFunc("help",
func(body, source string, w io.Writer) {
fmt.Fprintf(w, "%v: %v", source,
strings.Join(cmdHandler.RegisteredNames(), ", "))
})
db.Exec("CREATE TABLE IF NOT EXISTS quotes (id INTEGER PRIMARY KEY AUTOINCREMENT, quote STRING UNIQUE NOT NULL)")
cmdHandler.RegisterFunc("quote",
func(body, source string, w io.Writer) {
if body == "" {
var quote string
db.QueryRow("SELECT quote FROM quotes ORDER BY RANDOM() LIMIT 1").Scan("e)
fmt.Fprintf(w, "%v: %v", source, quote)
} else {
db.Exec("INSERT INTO quotes (quote) VALUES (?)", body)
}
})
cmdHandler.RegisterFunc("door",
func(body, source string, w io.Writer) {
fmt.Fprintf(w, "I'm sorry, %v. I'm afraid I can't do that.", source)
})
cmdHandler.RegisterFunc("user",
func(body, source string, w io.Writer) {
user, err := user.Lookup(body)
if err != nil {
fmt.Fprintf(w, "%v: %v", source, err)
} else {
fmt.Fprintf(w, "%v: %+v", source, user)
}
})
cmdHandler.RegisterFunc("gover",
func(body, source string, w io.Writer) {
version := runtime.Version()
fmt.Fprintf(w, "%v: %v", source, version)
})
cmdHandler.RegisterFunc("gos",
func(body, source string, w io.Writer) {
gos := runtime.NumGoroutine()
fmt.Fprintf(w, "%v: %v goroutines", source, gos)
})
cmdHandler.RegisterFunc("mem",
func(body, source string, w io.Writer) {
var memstats runtime.MemStats
runtime.ReadMemStats(&memstats)
fmt.Fprintf(w, "%v: %+v", source, memstats)
})
db.Exec("CREATE TABLE IF NOT EXISTS store (name TEXT PRIMARY KEY, value TEXT)")
cmdHandler.RegisterFunc("store",
func(body, source string, w io.Writer) {
nameValue := strings.SplitN(body, " ", 2)
name := nameValue[0]
var exists bool
db.QueryRow("SELECT EXISTS (SELECT 1 FROM store WHERE name=?)", name).Scan(&exists)
if len(nameValue) == 2 {
value := nameValue[1]
if exists {
db.Exec("UPDATE store SET value=? WHERE name=?)", value, name)
} else {
db.Exec("INSERT INTO store (name, value) VALUES (?, ?)", name, value)
}
} else if len(nameValue) == 1 {
if exists {
var value string
db.QueryRow("SELECT value FROM store WHERE name=?", name).Scan(&value)
fmt.Fprintf(w, "%v: %v", source, value)
} else {
fmt.Fprintf(w, "%v: no such name", source)
}
} else {
fmt.Fprintf(w, "%v: bad arguments", source)
}
})
client.Handle(cmdHandler)
client.Nick(config.Nick)
client.Join(config.Chan)
client.Listen()
}