Last active
June 18, 2017 14:24
-
-
Save DanielOaks/cbbc957e8dba39f59d9e to your computer and use it in GitHub Desktop.
Tiny IRC bot in Go, just to get started with the language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// written by Daniel Oaks <daniel@danieloaks.net> | |
// released under the CC0 Public Domain license | |
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"github.com/docopt/docopt-go" | |
"github.com/goshuirc/eventmgr" | |
"github.com/goshuirc/irc-go/client" | |
"github.com/goshuirc/irc-go/ircfmt" | |
"github.com/goshuirc/irc-go/ircmap" | |
) | |
// rawHandler prints raw messages to and from the server. | |
func rawHandler(event string, info eventmgr.InfoMap) { | |
sc := info["server"].(*gircclient.ServerConnection) | |
direction := info["direction"].(string) | |
line := info["data"].(string) | |
var arrow string | |
if direction == "in" { | |
arrow = "<- " | |
} else { | |
arrow = " ->" | |
} | |
fmt.Println(sc.Name, arrow, ircfmt.Escape(strings.Trim(line, "\r\n"))) | |
} | |
// eventHandler prints actual events to and from the server. | |
func eventHandler(event string, info eventmgr.InfoMap) { | |
sc := info["server"].(*gircclient.ServerConnection) | |
direction := info["direction"].(string) | |
var arrow string | |
if direction == "in" { | |
arrow = "<- " | |
} else { | |
arrow = " ->" | |
} | |
fmt.Println(sc.Name, arrow, info["tags"], info["prefix"], info["command"], info["params"]) | |
} | |
// privmsgHandler handles incoming PRIVMSGs. | |
func privmsgHandler(event string, info eventmgr.InfoMap) { | |
sc := info["server"].(*gircclient.ServerConnection) | |
target, _ := ircmap.Casefold(sc.Casemapping, info["params"].([]string)[0]) | |
fmt.Println(sc.Name, target, "<", info["prefix"].(string), ">", ircfmt.Escape(info["params"].([]string)[1])) | |
} | |
func main() { | |
version := "tinybot 0.0.1" | |
usage := `tinybot. | |
Usage: | |
tinybot connect <host> [--ssl] <nick> | |
tinybot -h | --help | |
tinybot --version | |
Options: | |
<host> Host to connect to (example: localhost:6667). | |
<nick> Nickname to connect with. | |
--ssl Connect securely using TLS. | |
-h --help Show this screen. | |
--version Show version.` | |
arguments, _ := docopt.Parse(usage, nil, true, version, false) | |
if arguments["connect"].(bool) { | |
fmt.Println("Connecting to", arguments["<host>"], "as", arguments["<nick>"]) | |
// create reactor and server | |
reactor := gircclient.NewReactor() | |
server := reactor.CreateServer("test") | |
// set info | |
server.InitialNick = arguments["<nick>"].(string) | |
server.InitialUser = server.InitialNick | |
// attach events | |
server.RegisterEvent("in", "PRIVMSG", privmsgHandler, 0) | |
server.RegisterEvent("in", "raw", rawHandler, 0) | |
server.RegisterEvent("out", "raw", rawHandler, 0) | |
// server.RegisterEvent("in", "all", eventHandler, 0) | |
// server.RegisterEvent("out", "all", eventHandler, 0) | |
// actually connect | |
err := server.Connect(arguments["<host>"].(string), arguments["--ssl"].(bool), nil) | |
if err != nil { | |
fmt.Println("Connection error:", err) | |
os.Exit(1) | |
} | |
server.ReceiveLoop() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment