2009-11-29 20:23:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-16 03:24:10 +00:00
|
|
|
"bufio"
|
2011-11-06 05:08:28 +00:00
|
|
|
"flag"
|
2009-12-17 21:30:18 +00:00
|
|
|
"fmt"
|
2013-02-16 03:24:10 +00:00
|
|
|
irc "github.com/fluffle/goirc/client"
|
2013-02-17 07:50:42 +00:00
|
|
|
"math/rand"
|
2009-12-17 21:30:18 +00:00
|
|
|
"os"
|
2013-02-17 07:50:42 +00:00
|
|
|
"strconv"
|
2009-12-17 21:30:18 +00:00
|
|
|
"strings"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
2013-02-17 07:50:42 +00:00
|
|
|
var host *string = flag.String("host", "irc.synirc.net", "IRC server")
|
2011-11-06 05:08:28 +00:00
|
|
|
var channel *string = flag.String("channel", "#go-nuts", "IRC channel")
|
2013-02-17 07:50:42 +00:00
|
|
|
var nick *string = flag.String("nick", "Septapus", "Nick")
|
|
|
|
var ident *string = flag.String("ident", "Septapus", "Ident")
|
|
|
|
var name *string = flag.String("name", "Septapus v9", "Name")
|
2011-11-06 05:08:28 +00:00
|
|
|
|
2009-11-29 20:23:15 +00:00
|
|
|
func main() {
|
2011-11-06 05:08:28 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// create new IRC connection
|
2013-02-18 03:07:44 +00:00
|
|
|
c := irc.SimpleClient(*nick, *ident, *name)
|
2011-11-09 22:06:54 +00:00
|
|
|
c.EnableStateTracking()
|
2013-02-16 03:24:10 +00:00
|
|
|
c.HandleFunc(irc.CONNECTED,
|
2011-11-06 05:08:28 +00:00
|
|
|
func(conn *irc.Conn, line *irc.Line) { conn.Join(*channel) })
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
// Set up a handler to notify of disconnect events.
|
|
|
|
quit := make(chan bool)
|
2013-02-16 03:24:10 +00:00
|
|
|
c.HandleFunc(irc.DISCONNECTED,
|
2011-07-22 00:08:42 +00:00
|
|
|
func(conn *irc.Conn, line *irc.Line) { quit <- true })
|
|
|
|
|
2013-02-17 07:50:42 +00:00
|
|
|
// Set up some simple commands, !bark and !roll.
|
2013-02-17 08:09:16 +00:00
|
|
|
// The !roll command will also get the "!help roll" command also.
|
2013-02-17 07:50:42 +00:00
|
|
|
c.SimpleCommandFunc("bark", func(conn *irc.Conn, line *irc.Line) { conn.Privmsg(line.Target(), "Woof Woof") })
|
|
|
|
c.SimpleCommandHelpFunc("roll", `Rolls a d6, "roll <n>" to roll n dice at once.`, func(conn *irc.Conn, line *irc.Line) {
|
|
|
|
count := 1
|
|
|
|
fields := strings.Fields(line.Message())
|
|
|
|
if len(fields) > 1 {
|
|
|
|
var err error
|
|
|
|
if count, err = strconv.Atoi(fields[len(fields)-1]); err != nil {
|
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total := 0
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
total += rand.Intn(6) + 1
|
|
|
|
}
|
|
|
|
conn.Privmsg(line.Target(), fmt.Sprintf("%d", total))
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set up some commands that are triggered by a regex in a message.
|
2013-02-17 08:09:16 +00:00
|
|
|
// It is important to see that UrlRegex could actually respond to some
|
|
|
|
// of the Url's that YouTubeRegex listens to, because of this we put the
|
|
|
|
// YouTube command at a higher priority, this way it will take precedence.
|
2013-02-17 07:50:42 +00:00
|
|
|
c.CommandFunc(irc.YouTubeRegex, irc.YouTubeFunc, 10)
|
|
|
|
c.CommandFunc(irc.UrlRegex, irc.UrlFunc, 0)
|
2013-02-17 03:57:54 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// set up a goroutine to read commands from stdin
|
2009-12-17 21:30:18 +00:00
|
|
|
in := make(chan string, 4)
|
|
|
|
reallyquit := false
|
2009-12-17 17:22:31 +00:00
|
|
|
go func() {
|
2009-12-17 21:30:18 +00:00
|
|
|
con := bufio.NewReader(os.Stdin)
|
2009-12-17 17:22:31 +00:00
|
|
|
for {
|
2009-12-17 21:30:18 +00:00
|
|
|
s, err := con.ReadString('\n')
|
2009-12-17 17:22:31 +00:00
|
|
|
if err != nil {
|
|
|
|
// wha?, maybe ctrl-D...
|
2009-12-17 21:30:18 +00:00
|
|
|
close(in)
|
2013-02-16 03:24:10 +00:00
|
|
|
reallyquit = true
|
|
|
|
c.Quit("")
|
2009-12-17 21:30:18 +00:00
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
// no point in sending empty lines down the channel
|
|
|
|
if len(s) > 2 {
|
2011-09-12 22:25:09 +00:00
|
|
|
in <- s[0 : len(s)-1]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
}()
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
// set up a goroutine to do parsey things with the stuff from stdin
|
|
|
|
go func() {
|
2009-12-19 15:19:28 +00:00
|
|
|
for cmd := range in {
|
2009-12-17 17:22:31 +00:00
|
|
|
if cmd[0] == ':' {
|
|
|
|
switch idx := strings.Index(cmd, " "); {
|
2009-12-19 18:36:12 +00:00
|
|
|
case cmd[1] == 'd':
|
|
|
|
fmt.Printf(c.String())
|
|
|
|
case cmd[1] == 'f':
|
|
|
|
if len(cmd) > 2 && cmd[2] == 'e' {
|
|
|
|
// enable flooding
|
2013-02-18 01:53:17 +00:00
|
|
|
c.Config().Flood = true
|
2009-12-19 18:36:12 +00:00
|
|
|
} else if len(cmd) > 2 && cmd[2] == 'd' {
|
|
|
|
// disable flooding
|
2013-02-18 01:53:17 +00:00
|
|
|
c.Config().Flood = false
|
2009-12-19 18:36:12 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
c.Privmsg("#", "flood test!")
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
case idx == -1:
|
|
|
|
continue
|
|
|
|
case cmd[1] == 'q':
|
|
|
|
reallyquit = true
|
|
|
|
c.Quit(cmd[idx+1 : len(cmd)])
|
|
|
|
case cmd[1] == 'j':
|
|
|
|
c.Join(cmd[idx+1 : len(cmd)])
|
|
|
|
case cmd[1] == 'p':
|
|
|
|
c.Part(cmd[idx+1 : len(cmd)])
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Raw(cmd)
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
}()
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
for !reallyquit {
|
2011-10-06 20:28:01 +00:00
|
|
|
// connect to server
|
2013-02-18 03:07:44 +00:00
|
|
|
<<<<<<< HEAD
|
2011-11-06 05:08:28 +00:00
|
|
|
if err := c.Connect(*host); err != nil {
|
2013-02-17 07:50:42 +00:00
|
|
|
fmt.Printf("Error %v", err)
|
2013-02-18 03:07:44 +00:00
|
|
|
=======
|
2013-02-18 01:53:17 +00:00
|
|
|
if err := c.ConnectTo(*host); err != nil {
|
2011-10-06 20:28:01 +00:00
|
|
|
fmt.Printf("Connection error: %s\n", err)
|
2013-02-18 03:07:44 +00:00
|
|
|
>>>>>>> fluffle/master
|
2011-10-06 20:28:01 +00:00
|
|
|
return
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2011-10-06 20:28:01 +00:00
|
|
|
// wait on quit channel
|
|
|
|
<-quit
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
}
|