flokati/modules/fortune/fortune.go

54 lines
808 B
Go

// vim:ts=4:sts=4:sw=4:noet:tw=72
package fortune
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
"github.com/sorcix/irc"
)
var (
sayCh chan string
)
func Init(ch chan string) {
log.Println("Initializing fortune module")
sayCh = ch
}
func HandleMessage(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)
}
}
}