2009-11-29 20:23:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2009-12-17 21:47:33 +00:00
|
|
|
"irc"
|
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")
|
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
|
|
|
|
|
|
|
// connect to server
|
2009-12-17 21:14:01 +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 {
|
|
|
|
in <- s[0:len(s)-1]
|
|
|
|
}
|
|
|
|
}
|
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-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)])
|
|
|
|
case cmd[1] == 'd':
|
|
|
|
fmt.Printf(c.String())
|
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
|
|
|
|
|
|
|
// stall here waiting for asplode on error channel
|
|
|
|
for {
|
2009-12-19 15:19:28 +00:00
|
|
|
for err := range c.Err {
|
|
|
|
fmt.Printf("goirc error: %s\n", err)
|
|
|
|
}
|
|
|
|
if reallyquit {
|
2009-12-17 21:30:18 +00:00
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-12-19 15:19:28 +00:00
|
|
|
fmt.Println("Reconnecting...")
|
|
|
|
if err := c.Connect("irc.freenode.net", ""); err != nil {
|
|
|
|
fmt.Printf("Connection error: %s\n", err)
|
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
}
|