Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "fmt"
- "log"
- "net"
- "net/textproto"
- "strings"
- )
- const (
- JOIN = "JOIN"
- NICK = "NICK"
- PART = "PART"
- PRIVMSG = "PRIVMSG"
- QUIT = "QUIT"
- USER = "USER"
- )
- type Message struct {
- Prefix string
- Command string
- Params []string
- Trailing []string
- }
- func (m *Message) Parse(line string) {
- prefixEnd, trailingStart := -1, len(line)
- m.Prefix, m.Command = "", ""
- if strings.HasPrefix(line, ":") {
- prefixEnd = strings.Index(line, " ")
- m.Prefix = line[1:prefixEnd]
- }
- trailingStart = strings.Index(line, " :")
- if trailingStart >= 0 {
- m.Trailing = strings.Fields(line[trailingStart+2:])
- } else {
- trailingStart = len(line) - 1
- }
- cmdAndParams := strings.Fields(line[(prefixEnd + 1) : trailingStart+1])
- if len(cmdAndParams) > 0 {
- m.Command = cmdAndParams[0]
- }
- if len(cmdAndParams) > 1 {
- m.Params = cmdAndParams[1:]
- }
- }
- func NewMessage() *Message {
- m := new(Message)
- return m
- }
- type Bot struct {
- Server string
- Port int
- Channels []string
- User string
- Nick string
- Password string
- Conn net.Conn
- Reader *textproto.Reader
- Writer *textproto.Writer
- Handlers map[string]string
- }
- func (bot *Bot) Connect() error {
- conn, err := net.Dial("tcp", bot.url())
- if err != nil {
- return err
- }
- bot.Conn = conn
- bot.Reader = textproto.NewReader(bufio.NewReader(bot.Conn))
- bot.Writer = textproto.NewWriter(bufio.NewWriter(bot.Conn))
- bot.Writer.PrintfLine("%s %s 8 * :%s", USER, bot.User, bot.User)
- bot.Writer.PrintfLine("%s %s", NICK, bot.Nick)
- return nil
- }
- func (bot *Bot) HandleActions() {
- message := NewMessage()
- for {
- line, err := bot.Reader.ReadLine()
- if err != nil {
- log.Fatal(err)
- }
- fmt.Printf("%s\n", line)
- message.Parse(line)
- if response, ok := bot.Handlers[message.Trailing[0]]; ok {
- bot.Writer.PrintfLine("%s %s %s", PRIVMSG, message.Params[0], response)
- }
- }
- }
- func (bot *Bot) Ident() {
- if bot.Password != "" {
- bot.Writer.PrintfLine("%s NickServ :identify %s %s", PRIVMSG, bot.Nick, bot.Password)
- }
- }
- func (bot *Bot) Join() {
- for _, channel := range bot.Channels {
- bot.Writer.PrintfLine("%s %s", JOIN, channel)
- }
- }
- func (bot *Bot) url() string {
- return fmt.Sprintf("%s:%d", bot.Server, bot.Port)
- }
- func NewBot(server string, port int, channels []string, user, nick, password string) *Bot {
- bot := &Bot{
- Server: server,
- Port: port,
- Channels: channels,
- User: user,
- Nick: nick,
- Password: password,
- Handlers: make(map[string]string),
- }
- return bot
- }
- func main() {
- server := "irc.freenode.net"
- port := 6667
- channels := []string{"#bottestchannel"}
- user := "bottestuser"
- nick := "bottestnick"
- password := ""
- ircbot := NewBot(server, port, channels, user, nick, password)
- ircbot.Handlers["!test"] = fmt.Sprintf("passed!")
- ircbot.Connect()
- ircbot.Join()
- ircbot.HandleActions()
- defer ircbot.Conn.Close()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement