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