-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.go
78 lines (64 loc) · 1.47 KB
/
dictionary.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/osm/irc"
)
type dictionary struct {
dictionary map[string]string
foundMsg string
notFoundMsg string
}
var dictionaries map[string]dictionary
// initDictionaries initializes the dictionaries.
func (b *bot) initDictionaries() {
dictionaries = make(map[string]dictionary)
// Iterate over the dictionaries, read the dictionary into memory and
// store it in the global dictionary map.
for _, d := range b.IRC.Dictionaries {
file, err := ioutil.ReadFile(d.Dictionary)
if err != nil {
fmt.Fprintf(os.Stderr, "dictionary: cant open dictionary %s\n", d.Dictionary)
os.Exit(1)
}
var di map[string]string
err = json.Unmarshal(file, &di)
if err != nil {
fmt.Fprintf(os.Stderr, "dictionary: cant decode dictionary %s\n", d.Dictionary)
os.Exit(1)
}
dictionaries[d.Trigger] = dictionary{
dictionary: di,
foundMsg: d.FoundMsg,
notFoundMsg: d.NotFoundMsg,
}
}
}
func (b *bot) dictionaryHandler(m *irc.Message) {
a := b.parseAction(m).(*privmsgAction)
if !a.validChannel {
return
}
if b.shouldIgnore(m) {
return
}
entry, hasEntry := dictionaries[a.cmd]
if !hasEntry {
return
}
key := strings.Join(a.args, " ")
value, hasValue := entry.dictionary[key]
if !hasValue {
b.privmsgph(entry.notFoundMsg, map[string]string{
"<key>": key,
})
return
}
b.privmsgph(entry.foundMsg, map[string]string{
"<key>": key,
"<value>": value,
})
}