-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin_rantext.go
121 lines (97 loc) · 2.34 KB
/
plugin_rantext.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
package main
import (
"github.com/thoj/go-ircevent"
"os"
"fmt"
"bufio"
"math/rand"
"strings"
)
type Corpus struct {
Text []string
Wrapper string
}
type RantextCommand struct {
ArgCommand
*Corpus
}
func Rantext() (rantexts []CommandInterface) {
wrappers := map[string]string{
"jerkcity": "\x02%s",
"troll": "\x0304,08%s",
"sharon": "\x0302,00%s",
"thatcher": "\x0302%s",
"theo": "\x0304,01%s",
"toothbrush": "\x02\x0307%s",
}
sources := strings.Split(Config.Global("rantext_sources"), ",")
for _, source := range(sources) {
wrapper := wrappers[source]
if wrapper == "" {
wrapper = "%s"
}
corpus := getCorpus(source, wrapper)
rantexts = append(rantexts, DirectedRantext(source, corpus))
rantexts = append(rantexts, UndirectedRantext(source, corpus))
}
return
}
func DirectedRantext(source string, corpus *Corpus) RantextCommand {
instance := RantextCommand{
ArgCommand: ArgCommand{
Args: []string{source, "[subject]"},
},
Corpus: corpus,
}
return instance
}
func (r RantextCommand) GetDocs() string {
var theFmt string
if len(r.Args) == 2 {
theFmt = "`[subject]: %s`"
} else {
theFmt = "`%s`"
}
return fmt.Sprintf(theFmt, r.Corpus.Choice())
}
func UndirectedRantext(source string, corpus *Corpus) RantextCommand {
instance := RantextCommand{
ArgCommand: ArgCommand{
Args: []string{source},
},
Corpus: corpus,
}
return instance
}
func getCorpus(source, wrapper string) (*Corpus) {
pathFormat := os.Getenv("HOME") + "/.gofoot/rantext/%s.txt"
file, err := os.Open(fmt.Sprintf(pathFormat, source))
if err != nil {
fmt.Println("Could not open %s: %s", source, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
corpus := Corpus{}
for scanner.Scan() {
corpus.Text = append(corpus.Text, scanner.Text())
}
corpus.Wrapper = wrapper
return &corpus
}
func (corpus Corpus) Choice() string {
return strings.TrimSpace(corpus.Text[rand.Intn(len(corpus.Text))])
}
func (corpus Corpus) WrappedChoice() string {
return fmt.Sprintf(corpus.Wrapper, corpus.Choice())
}
func (c RantextCommand) Handle(e *irc.Event) {
subject := c.argsForCommand(e.Message)["subject"]
choice := c.Corpus.WrappedChoice()
var message string
if subject != "" {
message = fmt.Sprintf("%s: %s", subject, choice)
} else {
message = fmt.Sprintf("%s", choice)
}
Connection.Privmsg(getTarget(e), message)
}