goircbot/main.go

115 lines
2.9 KiB
Go

package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"strings"
)
var nick = "pathbbot"
func sendData(conn net.Conn, message string) {
fmt.Fprintf(conn, "%s\n", message)
}
func logon(conn net.Conn) {
sendData(conn, "NICK "+nick)
sendData(conn, "User inhabitant 8 * : pathbwalker")
}
func sendPong(conn net.Conn, ping string) {
sendData(conn, strings.Replace(ping, "PING", "PONG", 1))
}
func join(conn net.Conn) {
sendData(conn, "JOIN #pathb")
}
func main() {
conn, err := net.Dial("tcp", "irc.anarchyplanet.org:6667")
if err != nil {
log.Print(err)
}
reader := bufio.NewReader(conn)
logon(conn)
for {
ln, err := reader.ReadString('\n')
fmt.Print(ln)
var _, command, _, message = parseIRCMessage(ln)
switch err {
case io.EOF:
log.Println("Reached EOF - closing this connection.\n")
return
case nil: // Do nothing
break
default:
log.Println("error.\n")
return
}
switch {
case command == "PING":
sendPong(conn, ln)
case command == "001":
join(conn)
case command == "PRIVMSG" && message == "@arise":
sendData(conn, "PRIVMSG #pathb :What the fuck did you just fucking say about me, you little bitch? I'll have you know I graduated top of my class in the Navy Seals, and I've been involved in numerous secret raids on Al-Quaeda, and I have over 300 confirmed kills. I am trained in gorilla warfare and I'm the top sniper in the entire US armed forces. You are nothing to me but just another target. I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth, mark my fucking words.")
}
}
}
/***
* Parses a recieved IRC message into its components.
*
* Returns:
* prefix string - optional prefix, empty string if not
* command string - command
* params []string - params, separated into a slice. If final param form is present,
* it is returned with its preceeding ':'
* text string - if final param form is present, return final param string without
* preceeding ':'
*/
func parseIRCMessage(message string) (prefix, command string, params []string, text string) {
var lineReader = bufio.NewReader(strings.NewReader(strings.Trim(message, "\n")))
// Optionally scan prefix into prefix
if message[0] == ':' {
fmt.Fscanf(lineReader, ":%s ", &prefix)
}
// Scan first non prefix string into command
fmt.Fscanf(lineReader, "%s ", &command)
// Scan rest of line into args
for lineReader.Buffered() > 0 {
var param string
// Peek the first character, if it's a ':' this is the last parameter,
// which includes spaces
if char, _ := lineReader.Peek(1); char[0] == ':' {
var paramBytes, _ = io.ReadAll(lineReader)
param = string(paramBytes)
// Text is defined as the last parameter sans ':', if the special
// Last parameter exists
text = strings.TrimPrefix(param, ":")
} else {
var paramBytes, _ = lineReader.ReadString(' ')
param = string(paramBytes)
}
params = append(params, param)
}
return prefix, command, params, text
}