This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
106 lines (92 loc) · 1.98 KB
/
main.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
package main
import (
"encoding/json"
"io/ioutil"
"os"
"runtime"
"strings"
"unicode"
"github.com/fatih/color"
"github.com/legolord208/stdutil"
)
var colorError = color.New(color.FgRed, color.Bold)
const rulesFile = ".ar_rules"
type rule struct {
Msg string
Exact bool
Reply string
From []string
NotFrom []string
InChannel []string
NotInChannel []string
}
var rules = make([]rule, 0)
func loadRules() {
content, err := ioutil.ReadFile(rulesFile)
if err != nil {
if !os.IsNotExist(err) {
stdutil.PrintErr("Couldn't read rules file", err)
}
} else {
err = json.Unmarshal(content, &rules)
if err != nil {
stdutil.PrintErr("Couldn't parse rules file", err)
exit()
}
warn := false
for i, rule := range rules {
msg := ""
for _, c := range rule.Msg {
if unicode.IsUpper(c) {
warn = true
c = unicode.ToLower(c)
} else if c == '\n' {
warn = true
c = ' '
}
msg += string(c)
}
rule.Msg = strings.TrimSpace(msg)
rules[i] = rule
}
if warn {
stdutil.PrintErr("Very funny. You can edit. We get it.", nil)
return
}
}
}
func saveRules() bool {
content, err := json.MarshalIndent(rules, "", "\t")
if err != nil {
stdutil.PrintErr("Couldn't generate rules file", err)
} else {
err = ioutil.WriteFile(rulesFile, content, 0666)
if err != nil {
stdutil.PrintErr("Couldn't write rules file", err)
} else {
return true
}
}
return false
}
func main() {
args := os.Args[1:]
stdutil.EventPrePrintError = append(stdutil.EventPrePrintError, func(full string, msg string, err error) bool {
color.Unset()
colorError.Set()
return false
})
stdutil.EventPostPrintError = append(stdutil.EventPostPrintError, func(full string, msg string, err error) {
color.Unset()
})
if len(args) < 1 {
doEdit()
} else if strings.EqualFold(args[0], "run") {
doRun(args[1:])
} else {
stdutil.PrintErr("Invalid mode '"+args[0]+"'.", nil)
}
}
func exit() {
runtime.Goexit()
}