flokati/modules/fortune.go

46 lines
753 B
Go
Raw Permalink 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"
)
2016-02-15 23:17:42 +00:00
func init() {
MsgFuncs["fortune"] = fortuneHandleMessage
2016-01-10 17:45:21 +00:00
}
2017-06-28 21:28:39 +00:00
func fortuneHandleMessage(m *Message) {
tok := strings.Split(m.Text, " ")
2016-01-10 17:45:21 +00:00
if len(tok) < 1 {
return
}
switch tok[0] {
case "!fortune":
2017-06-28 21:28:39 +00:00
fortune(m.Channel)
2016-01-10 17:45:21 +00:00
default:
}
}
2017-06-28 21:28:39 +00:00
func fortune(channel string) {
2016-01-10 17:45:21 +00:00
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 != "" {
2017-06-28 21:28:39 +00:00
SayCh <- fmt.Sprintf("%s\n%s", channel, l)
2016-01-10 17:45:21 +00:00
}
}
}