From 4b848c812dee16dd241aafd458edd37eec150baa Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 14 Jul 2021 22:42:11 +0000 Subject: [PATCH] Added IRC message parser, used to generalize code --- main.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index fe8a891..c23aaf9 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ 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") @@ -25,7 +26,6 @@ func sendPong(conn net.Conn, ping string) { func join(conn net.Conn) { sendData(conn, "JOIN #pathb") - } func main() { @@ -35,26 +35,80 @@ func main() { } reader := bufio.NewReader(conn) + logon(conn) for { ln, err := reader.ReadString('\n') - ln = strings.Trim(ln, "\n") - log.Println(ln) + fmt.Print(ln) - switch { - case err == io.EOF: + var _, command, _, message = parseIRCMessage(ln) + + switch err { + case io.EOF: log.Println("Reached EOF - closing this connection.\n") return - case err != nil: + case nil: // Do nothing + break + default: log.Println("error.\n") return - case strings.Contains(ln, "PING"): + } + + switch { + case command == "PING": sendPong(conn, ln) - case strings.Contains(ln, "001 "+nick+" :Welcome"): + case command == "001": join(conn) - case strings.Contains(ln, "@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. ") + 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 +}