Added IRC message parser, used to generalize code
This commit is contained in:
parent
c13a58437b
commit
4b848c812d
74
main.go
74
main.go
@ -14,6 +14,7 @@ var nick = "pathbbot"
|
|||||||
func sendData(conn net.Conn, message string) {
|
func sendData(conn net.Conn, message string) {
|
||||||
fmt.Fprintf(conn, "%s\n", message)
|
fmt.Fprintf(conn, "%s\n", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func logon(conn net.Conn) {
|
func logon(conn net.Conn) {
|
||||||
sendData(conn, "NICK "+nick)
|
sendData(conn, "NICK "+nick)
|
||||||
sendData(conn, "User inhabitant 8 * : pathbwalker")
|
sendData(conn, "User inhabitant 8 * : pathbwalker")
|
||||||
@ -25,7 +26,6 @@ func sendPong(conn net.Conn, ping string) {
|
|||||||
|
|
||||||
func join(conn net.Conn) {
|
func join(conn net.Conn) {
|
||||||
sendData(conn, "JOIN #pathb")
|
sendData(conn, "JOIN #pathb")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -35,26 +35,80 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
logon(conn)
|
logon(conn)
|
||||||
for {
|
for {
|
||||||
ln, err := reader.ReadString('\n')
|
ln, err := reader.ReadString('\n')
|
||||||
ln = strings.Trim(ln, "\n")
|
fmt.Print(ln)
|
||||||
log.Println(ln)
|
|
||||||
|
|
||||||
switch {
|
var _, command, _, message = parseIRCMessage(ln)
|
||||||
case err == io.EOF:
|
|
||||||
|
switch err {
|
||||||
|
case io.EOF:
|
||||||
log.Println("Reached EOF - closing this connection.\n")
|
log.Println("Reached EOF - closing this connection.\n")
|
||||||
return
|
return
|
||||||
case err != nil:
|
case nil: // Do nothing
|
||||||
|
break
|
||||||
|
default:
|
||||||
log.Println("error.\n")
|
log.Println("error.\n")
|
||||||
return
|
return
|
||||||
case strings.Contains(ln, "PING"):
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case command == "PING":
|
||||||
sendPong(conn, ln)
|
sendPong(conn, ln)
|
||||||
case strings.Contains(ln, "001 "+nick+" :Welcome"):
|
case command == "001":
|
||||||
join(conn)
|
join(conn)
|
||||||
case strings.Contains(ln, "@arise"):
|
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. ")
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user