From fe76704e771b2a8d1b423cf16b8d23cecb02b5ea Mon Sep 17 00:00:00 2001 From: Benny Morrison Date: Fri, 15 Mar 2019 01:51:57 -0400 Subject: refactored using github.com/lrstanley/girc --- main.go | 104 ++++++++++++++++++++++++++++------------------------------------ 1 file changed, 46 insertions(+), 58 deletions(-) diff --git a/main.go b/main.go index a865c47..da0471a 100644 --- a/main.go +++ b/main.go @@ -1,68 +1,56 @@ package main import ( - "crypto/tls" "log" - "net" + "os" + "strings" + "time" - "gopkg.in/irc.v3" + "github.com/lrstanley/girc" ) -func setConfs() (bool, irc.ClientConfig, tls.Config) { - //set this to false if you need - useTLS := true - - // set all the necessities - // also specifies the initial channel - // to join - config := irc.ClientConfig{ - Nick: "goofbot", - Pass: "password", - User: "goofbot", - Name: "Goofus McBot", - Handler: irc.HandlerFunc(func(c *irc.Client, m *irc.Message) { - if m.Command == "001" { - // 001 = welcome - c.Write("JOIN #institute") //initial channel join - } else if m.Command == "PRIVMSG" && c.FromChannel(m) { - c.WriteMessage(&irc.Message{ - Command: "PRIVMSG", - Params: []string{ - m.Params[0], - m.Trailing(), - }, - }) - } - }), - } - - // set up the tls params for the connection - // see: https://golang.org/pkg/crypto/tls/#Config - tlsconfig := tls.Config{ - InsecureSkipVerify: false, //set to true if you want to be dumb - RootCAs: nil, //use the OS's root CAs - PreferServerCipherSuites: true, //use the server's cipher list - } - return useTLS, config, tlsconfig -} - func main() { - useTLS, config, tlsconfig := setConfs() - - switch useTLS { - case true: - conn, err := tls.Dial("tcp", "irc.tilde.chat:6697", &tlsconfig) - case false: - conn, err := net.Dial("tcp", "irc.tilde.chat:6667") - } - if err != nil { - log.Fatalln(err) - } - - // create the connection - client := irc.NewClient(conn, config) - err = client.Run() - if err != nil { - log.Fatalln(err) + client := girc.New(girc.Config{ + Server: "irc.tilde.chat", + Port: 6697, + Nick: "goofbot", + User: "goofbot", + Name: "Goofus McBotus", + Out: os.Stdout, + SSL: true, + }) + + client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) { + c.Cmd.Join("#goofbot") + }) + + client.Handlers.Add(girc.PRIVMSG, func(c *girc.Client, e girc.Event) { + if strings.HasPrefix(e.Last(), "!hello") { + c.Cmd.ReplyTo(e, "henlo good fren!!") + return + } + + if strings.HasPrefix(e.Last(), "die, devil bird!") && e.Source.Name == "ahriman" { + c.Cmd.Reply(e, "SQUAWWWWWK!!") + time.Sleep(100 * time.Millisecond) + c.Close() + return + } + if strings.HasPrefix(e.Last(), "!botlist") { + c.Cmd.Reply(e, "Creator: ahriman. I'm the assistance bot for ~institute. Commands: !hello, !stop") + return + } + }) + + if err := client.Connect(); err != nil { + log.Fatalf("an error occurred while attempting to connect to %s: %s", client.Server(), err) } + //TODO: figure out sigint handling + // ctrlc := make(chan os.Signal, 1) + // signal.Notify(ctrlc, os.Interrupt) + // go func() { + // <-ctrlc + // client.Close() + // os.Exit(1) + // }() } -- cgit 1.4.1-2-gfad0