My Daily Gist | Ferdinand Silva


Simple IRC Bot (Go Programming Language Sample Code)

 
package main
import (
"fmt"
"net"
"bufio"
"strings"
"net/textproto"
)
func main() {
var server string = "irc.freenode.net"
var port string = "6667"
var ircNick string = "ferdinandsilva"
var ircChannel string = "ferdinandsilva"
conn, err := net.Dial("tcp", server + ":" + port)
if err != nil {
fmt.Println("Can't connect to ", server)
} else {
defer conn.Close()
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
needBreak := false
for {
if needBreak {
break
}
data, err := tp.ReadLine()
if err == nil {
fmt.Println(data)
switch {
case strings.Contains(data, "Checking Ident"):
fmt.Fprintf(conn, "NICK " + ircNick + "\r\n")
fmt.Fprintf(conn, "USER " + ircNick + " \"" + ircNick + ".com\" \"" + server + "\" :" + ircNick + " robot\r\n")
case strings.Contains(data, "This nickname is registered"):
fmt.Println("Nickname is registered, please change the ircNick value.")
needBreak = true
case strings.Contains(data, "Nickname is already in use"):
fmt.Println("Nickname is in use, please change the ircNick value.")
needBreak = true
case strings.Contains(data, "Erroneous Nickname"):
fmt.Println("Nickname is erroneous, please change the ircNick value.")
needBreak = true
case strings.Contains(data, "End of /MOTD command"):
fmt.Fprintf(conn, "JOIN #" + ircChannel + "\r\n")
case strings.Contains(data, "PING :"):
fmt.Fprintf(conn, strings.Replace(data, "PING", "PONG", 1) + "\r\n")
case strings.Contains(data, "JOIN #"):
fmt.Fprintf(conn, "PRIVMSG #" + ircChannel + " :Magandang araw sa'yo!!!\r\n")
}
} else {
fmt.Println("Disconnected to: ", server)
break
}
}
}
}
view raw main.go hosted with ❤ by GitHub