flokati/modules/fortune.go

48 lines
754 B
Go
Raw Normal View History

2016-01-10 17:45:21 +00:00
// vim:ts=4:sts=4:sw=4:noet:tw=72
2016-02-15 23:17:42 +00:00
package modules
2016-01-10 17:45:21 +00:00
import (
"bytes"
"fmt"
"os/exec"
"strings"
"github.com/sorcix/irc"
)
2016-02-15 23:17:42 +00:00
func init() {
MsgFuncs["fortune"] = fortuneHandleMessage
2016-01-10 17:45:21 +00:00
}
2016-02-15 23:17:42 +00:00
func fortuneHandleMessage(m *irc.Message) {
2016-01-10 17:45:21 +00:00
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)
2016-01-10 17:45:21 +00:00
}
}
}