Adjust bot package to changes in irc package.

Plugins work now. Tests don't, yet.
configurable-file-paths
Robert Gerus 2015-11-29 15:50:31 +01:00
parent 30513fc0d5
commit bae1d8c427
14 changed files with 56 additions and 82 deletions

View File

@ -13,13 +13,13 @@ import (
// channeljoin joins configured IRC channels when IRC server confirms we're good
// to go.
func channeljoin(output chan irc.Message, msg irc.Message) {
func channeljoin(output func(irc.Message), msg irc.Message) {
for _, channel := range cfg.LookupStringSlice(msg.Context, "Channels") {
log.Println(msg.Context["Network"], "joining channel", channel)
output <- irc.Message{
output(irc.Message{
Command: "JOIN",
Params: []string{channel},
}
})
}
}

View File

@ -25,7 +25,7 @@ type checkinator struct {
Users []user
}
func at(output chan irc.Message, msg irc.Message) {
func at(output func(irc.Message), msg irc.Message) {
var rmsg string
var values checkinator
var now []string
@ -37,13 +37,13 @@ func at(output chan irc.Message, msg irc.Message) {
data, err := httpGet("https://at.hackerspace.pl/api")
if err != nil {
output <- reply(msg, fmt.Sprint("error:", err))
output(reply(msg, fmt.Sprint("error:", err)))
return
}
err = json.Unmarshal(data, &values)
if err != nil {
output <- reply(msg, fmt.Sprint("error:", err))
output(reply(msg, fmt.Sprint("error:", err)))
return
}
@ -76,7 +76,7 @@ func at(output chan irc.Message, msg irc.Message) {
rmsg += fmt.Sprintf("; unknown: %d", values.Unknown)
}
output <- reply(msg, rmsg)
output(reply(msg, rmsg))
}
func init() {

View File

@ -12,22 +12,22 @@ import (
"github.com/arachnist/gorepost/irc"
)
var callbacks = make(map[string]map[string]func(chan irc.Message, irc.Message))
var callbacks = make(map[string]map[string]func(func(irc.Message), irc.Message))
// addCallback registers callbacks that can be later dispatched by Dispatcher
func addCallback(command, name string, callback func(chan irc.Message, irc.Message)) {
func addCallback(command, name string, callback func(func(irc.Message), irc.Message)) {
log.Println("adding callback", command, name)
if _, ok := callbacks[command]; !ok {
callbacks[command] = make(map[string]func(chan irc.Message, irc.Message))
callbacks[command] = make(map[string]func(func(irc.Message), irc.Message))
}
callbacks[strings.ToUpper(command)][strings.ToUpper(name)] = callback
callbacks[strings.ToUpper(command)][name] = callback
}
// Dispatcher takes irc messages and dispatches them to registered callbacks.
//
// It will take an input message, check (based on message context), if the
// message should be dispatched, and passes it to registered callback.
func Dispatcher(output chan irc.Message, input irc.Message) {
func Dispatcher(output func(irc.Message), input irc.Message) {
if _, ok := cfg.LookupStringMap(input.Context, "Ignore")[input.Context["Source"]]; ok {
log.Println("Context:", input.Context, "Ignoring", input.Context["Source"])
return

View File

@ -8,7 +8,7 @@ import (
"github.com/arachnist/gorepost/irc"
)
func google(output chan irc.Message, msg irc.Message) {
func google(output func(irc.Message), msg irc.Message) {
if strings.Split(msg.Trailing, " ")[0] != ":g" {
return
}
@ -25,7 +25,7 @@ func google(output chan irc.Message, msg irc.Message) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
output <- reply(msg, "problem connecting to google")
output(reply(msg, "problem connecting to google"))
return
}
@ -40,12 +40,12 @@ func google(output chan irc.Message, msg irc.Message) {
}
}
if errJ := json.NewDecoder(resp.Body).Decode(&data); errJ != nil {
output <- reply(msg, "problem decoding google response")
output(reply(msg, "problem decoding google response"))
return
}
if len(data.ResponseData.Results) > 0 {
res := data.ResponseData.Results[0]
output <- reply(msg, res.TitleNoFormatting+" "+res.URL)
output(reply(msg, res.TitleNoFormatting+" "+res.URL))
}
}

View File

@ -31,18 +31,18 @@ func IsIdentified(msg irc.Message) bool {
return true
}
func identify(output chan irc.Message, msg irc.Message) {
func identify(output func(irc.Message), msg irc.Message) {
if strings.Split(msg.Trailing, " ")[0] != ":identify" {
return
}
output <- irc.Message{
output(irc.Message{
Command: "WHOIS",
Params: []string{msg.Prefix.Name},
}
})
}
func registerIdentification(output chan irc.Message, msg irc.Message) {
func registerIdentification(output func(irc.Message), msg irc.Message) {
net := msg.Context["Network"]
if identified == nil {
@ -56,13 +56,13 @@ func registerIdentification(output chan irc.Message, msg irc.Message) {
identified[net][msg.Params[1]] = msg.Params[2]
}
func listIdentified(output chan irc.Message, msg irc.Message) {
func listIdentified(output func(irc.Message), msg irc.Message) {
if strings.Split(msg.Trailing, " ")[0] != ":idlist" {
return
}
if cfg.LookupInt(msg.Context, "AccessLevel") < 10 {
output <- reply(msg, "access denied")
output(reply(msg, "access denied"))
return
}
@ -74,7 +74,7 @@ func listIdentified(output chan irc.Message, msg irc.Message) {
}
}
output <- reply(msg, strings.Join(r, "; "))
output(reply(msg, strings.Join(r, "; ")))
}
func init() {

View File

@ -8,11 +8,11 @@ import (
"github.com/arachnist/gorepost/irc"
)
func invite(output chan irc.Message, msg irc.Message) {
output <- irc.Message{
func invite(output func(irc.Message), msg irc.Message) {
output(irc.Message{
Command: "JOIN",
Params: []string{msg.Trailing},
}
})
}
func init() {

View File

@ -19,7 +19,7 @@ func redirectError(*http.Request, []*http.Request) error {
return errNotReally
}
func kotki(output chan irc.Message, msg irc.Message) {
func kotki(output func(irc.Message), msg irc.Message) {
if strings.Split(msg.Trailing, " ")[0] != ":kotki" {
return
}
@ -38,7 +38,7 @@ func kotki(output chan irc.Message, msg irc.Message) {
rurl, _ := resp.Location()
rmsg = rurl.String()
output <- reply(msg, rmsg)
output(reply(msg, rmsg))
}
func init() {

View File

@ -10,16 +10,12 @@ import (
"github.com/arachnist/gorepost/irc"
)
func ping(output chan irc.Message, msg irc.Message) {
func ping(output func(irc.Message), msg irc.Message) {
if strings.Split(msg.Trailing, " ")[0] != ":ping" {
return
}
output <- irc.Message{
Command: "PRIVMSG",
Params: []string{msg.Prefix.Name},
Trailing: "pingity pong",
}
output(reply(msg, "pingity pong"))
}
func init() {

View File

@ -13,7 +13,7 @@ import (
"github.com/arachnist/gorepost/irc"
)
func nickserv(output chan irc.Message, msg irc.Message) {
func nickserv(output func(irc.Message), msg irc.Message) {
if msg.Prefix.String() != cfg.LookupString(msg.Context, "NickServPrefix") {
log.Println("Context:", msg.Context, "Someone is spoofing nickserv!")
return
@ -35,14 +35,10 @@ func nickserv(output chan irc.Message, msg irc.Message) {
}
log.Println("Context:", msg.Context, "Identifying to nickserv!")
output <- irc.Message{
Command: "PRIVMSG",
Params: []string{msg.Prefix.Name},
Trailing: fmt.Sprintf("IDENTIFY %s", cfg.LookupString(msg.Context, "NickServPassword")),
}
output(reply(msg, fmt.Sprintf("IDENTIFY %s", cfg.LookupString(msg.Context, "NickServPassword"))))
}
func joinsecuredchannels(output chan irc.Message, msg irc.Message) {
func joinsecuredchannels(output func(irc.Message), msg irc.Message) {
if msg.Prefix.String() != cfg.LookupString(msg.Context, "NickServPrefix") {
log.Println("Context:", msg.Context, "Someone is spoofing nickserv!")
return
@ -70,10 +66,10 @@ func joinsecuredchannels(output chan irc.Message, msg irc.Message) {
for _, channel := range channels {
log.Println(msg.Context["Network"], "joining channel", channel)
output <- irc.Message{
output(irc.Message{
Command: "JOIN",
Params: []string{channel},
}
})
}
}

View File

@ -10,11 +10,11 @@ import (
// pingpong responds to server pings. IRC servers disconnect idle clients that
// don't respond to PINGs.
func pingpong(output chan irc.Message, msg irc.Message) {
output <- irc.Message{
func pingpong(output func(irc.Message), msg irc.Message) {
output(irc.Message{
Command: "PONG",
Trailing: msg.Trailing,
}
})
}
func init() {

View File

@ -109,21 +109,14 @@ var eventTests = []struct {
}
func TestPlugins(t *testing.T) {
output := make(chan irc.Message, 1)
quitCollector := make(chan struct{}, 1)
var r []irc.Message
var wg sync.WaitGroup
go func(quit chan struct{}, input chan irc.Message) {
for {
select {
case msg := <-input:
wg.Done()
r = append(r, msg)
case <-quit:
}
}
}(quitCollector, output)
// fake irc.Conn Sender replacement
output := func(msg irc.Message) {
wg.Done()
r = append(r, msg)
}
for _, e := range eventTests {
r = r[:0]
@ -142,8 +135,6 @@ func TestPlugins(t *testing.T) {
t.Fail()
}
}
quitCollector <- struct{}{}
}
func configLookupHelper(map[string]string) []string {

View File

@ -110,21 +110,14 @@ var eventTests = []struct {
}
func TestPlugins(t *testing.T) {
output := make(chan irc.Message, 1)
quitCollector := make(chan struct{}, 1)
var r []irc.Message
var wg sync.WaitGroup
go func(quit chan struct{}, input chan irc.Message) {
for {
select {
case msg := <-input:
wg.Done()
r = append(r, msg)
case <-quit:
}
}
}(quitCollector, output)
// fake irc.Conn Sender replacement
output := func(msg irc.Message) {
wg.Done()
r = append(r, msg)
}
for _, e := range eventTests {
r = r[:0]
@ -143,8 +136,6 @@ func TestPlugins(t *testing.T) {
t.Fail()
}
}
quitCollector <- struct{}{}
}
func configLookupHelper(map[string]string) []string {

View File

@ -27,7 +27,7 @@ type seenRecord struct {
Text string
}
func seenrecord(output chan irc.Message, msg irc.Message) {
func seenrecord(output func(irc.Message), msg irc.Message) {
if msg.Params == nil {
return
}
@ -52,7 +52,7 @@ func seenrecord(output chan irc.Message, msg irc.Message) {
}
}
func seen(output chan irc.Message, msg irc.Message) {
func seen(output func(irc.Message), msg irc.Message) {
var v seenRecord
var r string
@ -67,16 +67,16 @@ func seen(output chan irc.Message, msg irc.Message) {
b, err := k.GetBytes("seen/" + args[1])
if err == kt.ErrNotFound {
output <- reply(msg, cfg.LookupString(msg.Context, "NotSeenMessage"))
output(reply(msg, cfg.LookupString(msg.Context, "NotSeenMessage")))
return
} else if err != nil {
output <- reply(msg, fmt.Sprint("error getting record for", args[1], err))
output(reply(msg, fmt.Sprint("error getting record for", args[1], err)))
return
}
err = json.Unmarshal(b, &v)
if err != nil {
output <- reply(msg, fmt.Sprint("error unmarshaling record for", args[1], err))
output(reply(msg, fmt.Sprint("error unmarshaling record for", args[1], err)))
return
}
@ -93,7 +93,7 @@ func seen(output chan irc.Message, msg irc.Message) {
r += fmt.Sprint("saying: ", v.Text)
}
output <- reply(msg, r)
output(reply(msg, r))
}
func init() {

View File

@ -28,7 +28,7 @@ func getURLTitle(l string) string {
return string(trimTitle.ReplaceAll([]byte(title), []byte{' '})[:])
}
func linktitle(output chan irc.Message, msg irc.Message) {
func linktitle(output func(irc.Message), msg irc.Message) {
var r []string
for _, s := range strings.Split(msg.Trailing, " ") {
@ -51,7 +51,7 @@ func linktitle(output chan irc.Message, msg irc.Message) {
if len(r) > 0 {
t := cfg.LookupString(msg.Context, "LinkTitlePrefix") + strings.Join(r, cfg.LookupString(msg.Context, "LinkTitleDelimiter"))
output <- reply(msg, t)
output(reply(msg, t))
}
}