2009-11-29 20:23:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-02-15 07:26:19 +00:00
|
|
|
"bufio"
|
2011-11-06 05:08:28 +00:00
|
|
|
"flag"
|
2009-12-17 21:30:18 +00:00
|
|
|
"fmt"
|
2013-02-15 07:26:19 +00:00
|
|
|
irc "github.com/fluffle/goirc/client"
|
2009-12-17 21:30:18 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
2011-11-06 05:08:28 +00:00
|
|
|
var host *string = flag.String("host", "irc.freenode.net", "IRC server")
|
|
|
|
var channel *string = flag.String("channel", "#go-nuts", "IRC channel")
|
|
|
|
|
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
|
2011-11-09 22:06:54 +00:00
|
|
|
c := irc.SimpleClient("GoTest", "gotest")
|
|
|
|
c.EnableStateTracking()
|
2009-11-29 20:23:15 +00:00
|
|
|
c.AddHandler("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)
|
|
|
|
c.AddHandler("disconnected",
|
|
|
|
func(conn *irc.Conn, line *irc.Line) { quit <- true })
|
|
|
|
|
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-15 07:26:19 +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
|
|
|
|
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 {
|
2011-10-06 20:28:01 +00:00
|
|
|
// connect to server
|
2011-11-06 05:08:28 +00:00
|
|
|
if err := c.Connect(*host); err != nil {
|
2011-10-06 20:28:01 +00:00
|
|
|
fmt.Printf("Connection error: %s\n", err)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|