flokati/modules/fortune.go

46 lines
753 B
Go

// vim:ts=4:sts=4:sw=4:noet:tw=72
package modules
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
func init() {
MsgFuncs["fortune"] = fortuneHandleMessage
}
func fortuneHandleMessage(m *Message) {
tok := strings.Split(m.Text, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!fortune":
fortune(m.Channel)
default:
}
}
func fortune(channel string) {
cmd := exec.Command("/usr/games/fortune", "/flokatirc/fortunes/")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
println(err)
return
}
s := out.String()
s = strings.Replace(s, "\r", "", -1)
s = strings.Replace(s, "\t", " ", -1)
for _, l := range strings.Split(s, "\n") {
if l != "" {
SayCh <- fmt.Sprintf("%s\n%s", channel, l)
}
}
}