2009-11-29 20:23:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-11-21 19:53:14 +00:00
|
|
|
irc "github.com/fluffle/goirc/client"
|
2009-12-17 21:30:18 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"bufio"
|
|
|
|
"strings"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2009-12-17 17:22:31 +00:00
|
|
|
// create new IRC connection
|
2009-12-17 21:30:18 +00:00
|
|
|
c := irc.New("GoTest", "gotest", "GoBot")
|
2010-11-03 23:53:14 +00:00
|
|
|
c.Debug = true
|
2009-11-29 20:23:15 +00:00
|
|
|
c.AddHandler("connected",
|
2009-12-17 21:30:18 +00:00
|
|
|
func(conn *irc.Conn, line *irc.Line) { conn.Join("#go-nuts") })
|
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)
|
|
|
|
c.AddHandler("disconnected",
|
|
|
|
func(conn *irc.Conn, line *irc.Line) { quit <- true })
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// connect to server
|
2010-11-03 23:53:14 +00:00
|
|
|
if err := c.Connect("irc.freenode.net"); err != nil {
|
2009-12-17 21:30:18 +00:00
|
|
|
fmt.Printf("Connection error: %s\n", err)
|
|
|
|
return
|
2009-11-29 20:23:15 +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)
|
|
|
|
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
|
|
|
|
c.Flood = true
|
|
|
|
} else if len(cmd) > 2 && cmd[2] == 'd' {
|
|
|
|
// disable flooding
|
|
|
|
c.Flood = false
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
select {
|
|
|
|
case err := <-c.Err:
|
2009-12-19 15:19:28 +00:00
|
|
|
fmt.Printf("goirc error: %s\n", err)
|
2011-07-22 00:08:42 +00:00
|
|
|
case <-quit:
|
|
|
|
if !reallyquit {
|
|
|
|
fmt.Println("Reconnecting...")
|
|
|
|
if err := c.Connect("irc.freenode.net"); err != nil {
|
|
|
|
fmt.Printf("Connection error: %s\n", err)
|
|
|
|
reallyquit = true
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
}
|