separate functions

This commit is contained in:
notnull 2021-03-11 16:15:10 -05:00
parent 8626850020
commit 1f567be857

36
main.go
View File

@ -9,6 +9,24 @@ import (
"strings" "strings"
) )
func sendData(conn net.Conn, message string) {
log.Print(message + "\n")
fmt.Fprintf(conn, "%s\n", message)
}
func logon(conn net.Conn) {
sendData(conn, "NICK pathbbot")
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() { func main() {
conn, err := net.Dial("tcp", "pathb.net:6667") conn, err := net.Dial("tcp", "pathb.net:6667")
if err != nil { if err != nil {
@ -19,27 +37,21 @@ func main() {
for { for {
ln, err := reader.ReadString('\n') ln, err := reader.ReadString('\n')
ln = strings.Trim(ln, "\n") ln = strings.Trim(ln, "\n")
// this switch statement handles cases where the connection is terminated log.Println(ln)
switch { switch {
case err == io.EOF: case err == io.EOF:
log.Println("Reached EOF - closing this connection.\n") log.Println("Reached EOF - closing this connection.\n")
return return
case err != nil: case err != nil:
log.Println("error.") log.Println("error.\n")
return return
}
log.Println(ln)
switch {
case strings.Contains(ln, "Looking up your hostname"): case strings.Contains(ln, "Looking up your hostname"):
fmt.Fprintf(conn, "NICK pathbbot\r\n") logon(conn)
fmt.Fprintf(conn, "USER inhabitant 8 * : pathbwalker\r\n")
case strings.Contains(ln, "PING"): case strings.Contains(ln, "PING"):
log.Print("sending PONG") sendPong(conn, ln)
fmt.Fprintf(conn, strings.Replace(ln, "PING", "PONG", 1))
case strings.Contains(ln, "End of /MOTD command."): case strings.Contains(ln, "End of /MOTD command."):
log.Print("Joining #pathb") join(conn)
fmt.Fprintf(conn, "JOIN #pathb\n")
} }
} }