Skip to content

Instantly share code, notes, and snippets.

@six519
Created December 30, 2017 02:40
Show Gist options
  • Save six519/a35bb64ec95aa90b9e4e4c5c2374f29b to your computer and use it in GitHub Desktop.
Save six519/a35bb64ec95aa90b9e4e4c5c2374f29b to your computer and use it in GitHub Desktop.
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
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment