init
This commit is contained in:
commit
51b7e05596
11 changed files with 1214 additions and 0 deletions
53
modules/fortune/fortune.go
Normal file
53
modules/fortune/fortune.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue