kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

commit ae82f1d3c307db1cc3bd9bb0321224d46d53ab54
parent 3fbe627e0a441b9e7a8314df7651cfdcaa78df62
Author: sin <sin@2f30.org>
Date:   Thu, 18 Apr 2013 11:34:26 +0100

kunt: Add radio + games functionality

Diffstat:
Msrc/irc/events.go | 9+++------
Msrc/kunt/kunt.go | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
2 files changed, 58 insertions(+), 24 deletions(-)

diff --git a/src/irc/events.go b/src/irc/events.go @@ -10,12 +10,9 @@ type IrcEvent struct { } func (i *IrcContext) AddEventHandler(ev IrcEvent) int { - if i.evnum+1 < len(i.ev) { - i.ev[i.evnum] = ev - i.evnum++ - return 0 - } - return -1 + i.ev = append(i.ev, ev) + i.evnum++ + return 0 } func (i *IrcContext) DelEventHandler(ev *IrcEvent) int { diff --git a/src/kunt/kunt.go b/src/kunt/kunt.go @@ -5,10 +5,12 @@ import ( "flag" "fmt" "fsdb" + "games" "io/ioutil" "irc" "log" "math/rand" + "net/http" "os" "os/exec" "strings" @@ -16,8 +18,8 @@ import ( ) type kuntCtx struct { - stime time.Time - ircCtx *irc.IrcContext + stime time.Time + ircCtx *irc.IrcContext } func resolveYoutubeTitle(link string) (title string, ret bool) { @@ -77,6 +79,7 @@ func cmdHelp(msg irc.IrcMessage) { "!counturls -> Print the number of entries in the url db", "!uptime -> Show uptime", "!wisdom -> Invoke fortune(6)", + "!radio -> Print now playing song", "!src -> Fetch kunt's source code", "!TODO -> Print the TODO list", "!version -> Show version", @@ -105,17 +108,14 @@ func cmdRandQuote(msg irc.IrcMessage) { } func cmdAddQuote(msg irc.IrcMessage) { - if len(msg.Params) != 2 { - kunt.ircCtx.SendPrivmsg(msg.Params[0], "Missing parameter for !addquote\r\n") + s := msg.Params[1] + hm := strings.Fields(s) + if len(hm) < 2 { + kunt.ircCtx.SendPrivmsg(msg.Params[0], "Missing parameter for !addquote") return } - - s := "" - for i, v := range msg.Params { - if i > 1 { - s += v - } - } + s = s[11:] + s = strings.TrimSpace(s) buf := []byte(s) quote, err := quoteDb.Put(-1, buf) if err != nil { @@ -151,12 +151,14 @@ func cmdRandUrl(msg irc.IrcMessage) { } func cmdAddUrl(msg irc.IrcMessage) { - if len(msg.Params) != 2 { - kunt.ircCtx.SendPrivmsg(msg.Params[0], "Wrong formatting for !addurl") + s := msg.Params[1] + as := strings.Fields(s) + if len(as) < 2 { + kunt.ircCtx.SendPrivmsg(msg.Params[0], "Missing parameter for !addurl") return } - - s := strings.TrimSpace(msg.Params[1]) + s = s[9:] + s = strings.TrimSpace(s) s += "\n" buf := []byte(s) url, err := urlDb.Put(-1, buf) @@ -175,6 +177,18 @@ func cmdCountUrls(msg irc.IrcMessage) { kunt.ircCtx.SendPrivmsg(msg.Params[0], r) } +func cmdRadio(msg irc.IrcMessage) { + np, e := http.Get("http://radio.2f30.org:8000/npsong.xsl") + if e != nil { + kunt.ircCtx.SendPrivmsg(msg.Params[0], "Is radio broken?") + } else { + defer np.Body.Close() + song, _ := ioutil.ReadAll(np.Body) + r := fmt.Sprintf("[Radio] %s", song) + kunt.ircCtx.SendPrivmsg(msg.Params[0], r) + } +} + func cmdUptime(msg irc.IrcMessage) { etime := time.Now() r := fmt.Sprintf("%v", etime.Sub(kunt.stime)) @@ -204,16 +218,37 @@ func cmdTodo(msg irc.IrcMessage) { } } +func cmdGame(msg irc.IrcMessage) { + s := msg.Params[1] + as := strings.Fields(s) + if len(as) < 2 { + kunt.ircCtx.SendPrivmsg(msg.Params[0], "Missing parameter for !game") + return + } + s = s[6:] + s = strings.TrimSpace(s) + switch s { + case "list": + for _, str := range geng.List() { + kunt.ircCtx.SendPrivmsg(msg.Params[0], "[game] "+str) + } + return + } + gchan := make(chan irc.IrcMessage) + geng.New(s, kunt.ircCtx, &gchan) + gchan <- irc.IrcMessage{Prefix: "", Cmd: "SETNICK", Params: []string{"kunt"}} +} + var sslon = flag.Bool("s", false, "SSL support") var quoteDb *fsdb.Fsdb var urlDb *fsdb.Fsdb var kunt kuntCtx - -func handlePrivmsg(msg irc.IrcMessage) { -} +var geng games.GameEnger func main() { + kunt.stime = time.Now() + geng = games.NewGameEng() log.SetPrefix("kunt: ") flag.Parse() @@ -244,9 +279,11 @@ func main() { "!randurl": cmdRandUrl, "!addurl": cmdAddUrl, "!counturls": cmdCountUrls, + "!radio": cmdRadio, "!uptime": cmdUptime, "!src": cmdSrc, "!TODO": cmdTodo, + "!game": cmdGame, } cfg := irc.IrcConfig{