Created
November 23, 2014 09:28
-
-
Save fuzzy/c09b813b92d63e15a42e to your computer and use it in GitHub Desktop.
a basic irc bot, that is pretty crappy, but it's the first thing I've done in go, yay
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"time" | |
"github.com/husio/go-irc" | |
) | |
// globals that make me ill, move to configs asap | |
var ( | |
address = flag.String("address", "irc.freenode.net:6667", "IRC server address") | |
nick = flag.String("nick", "GehenBot", "User nick") | |
name = flag.String("name", "GehenBot", "User name") | |
verbose = flag.Bool("verbose", false, "Print all messages to stdout") | |
evPipe = make(chan IrcEvent) | |
c, err = irc.Connect(*address) | |
) | |
// IRC Convenience | |
func Say(channel string, message string) { | |
c.Send("PRIVMSG %s :%s\n", channel, message) | |
} | |
func Msg(sendTo string, message string) { | |
Say(sendTo, message) | |
} | |
// command implimentations | |
func Help(event IrcEvent) { | |
var msgTo string | |
if event.params[0] == *nick { | |
msgTo = event.fromNick | |
} else { | |
msgTo = event.params[0] | |
} | |
Say(msgTo, "!<CMD> <args> ...") | |
Say(msgTo, "help tell") | |
} | |
func Tell(event IrcEvent) { | |
Say(event.args[0], strings.Join(event.args[1:], " ")) | |
} | |
// Event Handling | |
type IrcEvent struct { | |
ircCommand string | |
fromNick string | |
fromHost string | |
command string | |
params []string | |
args []string | |
} | |
func EventHandler() { | |
for { | |
event := <- evPipe | |
fmt.Printf( | |
"\nIrcCommand: %s\nFromNick: %s\nFromHost: %s\nCommand: %s\nParams: %s\nArgs: %s\n\n", | |
event.ircCommand, event.fromNick, event.fromHost, event.command, event.params, event.args, | |
) | |
if event.command == "help" { | |
Help(event) | |
} else if event.command == "tell" { | |
Tell(event) | |
} | |
} | |
} | |
func ParseEvent(message irc.Message) bool { | |
evt := IrcEvent{ | |
ircCommand: message.Command(), | |
fromNick: strings.Split(message.Prefix(), "!")[0], | |
fromHost: strings.Split(message.Prefix(), "!")[1], | |
command: strings.Split(message.Trailing(), " ")[0][1:], | |
params: message.Params(), | |
args: strings.Split(message.Trailing(), " ")[1:], | |
} | |
// HandleEvent(evt) | |
evPipe <- evt | |
return true | |
} | |
// main program entry | |
func main() { | |
go EventHandler() | |
flag.Parse() | |
if err != nil { | |
log.Fatalln(err) | |
} | |
c.Send("NICK %s", *nick) | |
c.Send("USER %s * * :...", *name) | |
time.Sleep(time.Millisecond * 10) | |
for _, name := range flag.Args() { | |
if !strings.HasPrefix(name, "#") { | |
name = "#" + name | |
} | |
c.Send("JOIN %s", name) | |
} | |
// read data from stdin and send it through the wire | |
go func() { | |
reader := bufio.NewReader(os.Stdin) | |
for { | |
line, err := reader.ReadString('\n') | |
if err != nil { | |
log.Fatalln(err) | |
} | |
line = strings.TrimSpace(line) | |
if len(line) == 0 { | |
continue | |
} | |
c.Send(line) | |
} | |
}() | |
fmt.Printf("\nGehenBot v0.0.1 by Mike 'Fuzzy' Partin <[email protected]>\n\n") | |
// handle incomming messages | |
for { | |
message, err := c.ReadMessage() | |
if err != nil { | |
log.Fatalln(err) | |
return | |
} | |
if message.Command() == "PING" { | |
c.Send("PONG %s", message.Trailing()) | |
} | |
if message.Command() == "PRIVMSG" { | |
text := message.Trailing() | |
if text[0] == '!' { | |
ParseEvent(message) | |
} | |
} | |
if *verbose { | |
log.Println(message) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment