goirc/client.go

103 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
"bufio"
2011-11-06 05:08:28 +00:00
"flag"
"fmt"
"os"
"strings"
2016-09-12 19:55:10 +00:00
irc "github.com/fluffle/goirc/client"
)
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")
func main() {
2011-11-06 05:08:28 +00:00
flag.Parse()
// create new IRC connection
2013-02-18 01:53:17 +00:00
c := irc.SimpleClient("GoTest", "gotest")
c.EnableStateTracking()
2013-02-16 11:04:06 +00:00
c.HandleFunc("connected",
2011-11-06 05:08:28 +00:00
func(conn *irc.Conn, line *irc.Line) { conn.Join(*channel) })
// Set up a handler to notify of disconnect events.
quit := make(chan bool)
2013-02-16 11:04:06 +00:00
c.HandleFunc("disconnected",
func(conn *irc.Conn, line *irc.Line) { quit <- true })
// set up a goroutine to read commands from stdin
in := make(chan string, 4)
reallyquit := false
go func() {
con := bufio.NewReader(os.Stdin)
for {
s, err := con.ReadString('\n')
if err != nil {
// wha?, maybe ctrl-D...
close(in)
break
}
// no point in sending empty lines down the channel
if len(s) > 2 {
in <- s[0 : len(s)-1]
}
}
}()
// set up a goroutine to do parsey things with the stuff from stdin
go func() {
for cmd := range in {
if cmd[0] == ':' {
switch idx := strings.Index(cmd, " "); {
case cmd[1] == 'd':
fmt.Printf(c.String())
case cmd[1] == 'n':
parts := strings.Split(cmd, " ")
username := strings.TrimSpace(parts[1])
channelname := strings.TrimSpace(parts[2])
_, userIsOn := c.StateTracker().IsOn(channelname, username)
fmt.Printf("Checking if %s is in %s Online: %t\n", username, channelname, userIsOn)
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
} else if len(cmd) > 2 && cmd[2] == 'd' {
// disable flooding
2013-02-18 01:53:17 +00:00
c.Config().Flood = false
}
for i := 0; i < 20; i++ {
c.Privmsg("#", "flood test!")
}
case idx == -1:
continue
case cmd[1] == 'q':
reallyquit = true
c.Quit(cmd[idx+1 : len(cmd)])
2016-09-12 19:55:10 +00:00
case cmd[1] == 's':
reallyquit = true
2016-09-16 18:43:45 +00:00
c.Close()
case cmd[1] == 'j':
c.Join(cmd[idx+1 : len(cmd)])
case cmd[1] == 'p':
c.Part(cmd[idx+1 : len(cmd)])
}
} else {
c.Raw(cmd)
}
}
}()
for !reallyquit {
2011-10-06 20:28:01 +00:00
// connect to server
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)
return
}
2011-10-06 20:28:01 +00:00
// wait on quit channel
<-quit
}
}