flokati/modules/fortune.go

51 lines
823 B
Go

// vim:ts=4:sts=4:sw=4:noet:tw=72
package modules
import (
"bytes"
"fmt"
"os/exec"
"strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc"
)
func init() {
MsgHandlers["fortune"] = fortuneHandleMessage
xlog.Info("Fortune module initialized")
}
func fortuneHandleMessage(m *irc.Message) {
tok := strings.Split(m.Trailing, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!fortune":
fortune()
default:
}
}
func fortune() {
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("*\n%s", l)
}
}
}