Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net"
  6.     "bufio"
  7.     "strings"
  8.     "os"
  9. )
  10.  
  11. func send(conn net.Conn, message string) {
  12.     fmt.Println("> ", message)
  13.     fmt.Fprintf(conn, message)
  14. }
  15.  
  16. func read(conn net.Conn) string {
  17.     fmt.Println("DBG: reading from server...")
  18.     response, err := bufio.NewReader(conn).ReadString('\n')
  19.     if err != nil {
  20.         fmt.Println("oopsies!  Error reading from server! Bailing out")
  21.         os.Exit(1)
  22.     }
  23.     fmt.Println("< ", response)
  24.  
  25.     return response
  26. }
  27.  
  28. func privmsg(conn net.Conn, channel string, msg string) {
  29.     send(conn, "PRIVMSG "+channel+" "+":"+msg+"\r\n")
  30. }
  31.  
  32. func handle_privmsg(conn net.Conn, words []string) {
  33.     //:prgnick!prg@64552A1E:E6FF1A33:696E1BF9:IP PRIVMSG #testchan :thought it would be harder 
  34.     //userblock := words[0]
  35.     channel := words[2]
  36.     msg := strings.Join(words[3:], " ")
  37.  
  38.     if(strings.Contains(strings.ToUpper(msg), "PING")) {
  39.         privmsg(conn, channel, "Pong, mother fuckers..")
  40.     }
  41. }
  42.  
  43.  
  44. func main() {
  45.     fmt.Println("This is pre-alpha software.  USE AT YOUR OWN RISK!")
  46.     fmt.Println("Bot initializing...")
  47.  
  48.     const nick = "super_testbot123"
  49.     const user = nick
  50.     const fullname = "Test bot"
  51.     const server = "irc.whatever.net"
  52.     const port = "6667"
  53.  
  54.     conn, err := net.Dial("tcp", server + ":" + port)
  55.     if err != nil {
  56.         fmt.Println("Oopsies! Failed to connect to the server!")
  57.     }
  58.     read(conn)
  59.     nick_msg := "NICK " + nick + "\r\n"
  60.     send(conn, nick_msg)
  61.     usr_msg := "USER " + user + " * * :" + fullname + "\r\n"
  62.     send(conn, usr_msg)
  63.     for {
  64.         in_msg := read(conn)
  65.         words := strings.Split(in_msg, " ")
  66.         if(words[0] == "PING") {
  67.             pong_msg := "PONG " + strings.Split(in_msg, " ")[1] + "\r\n"
  68.             send(conn, pong_msg)
  69.         }
  70.         if(words[1] == "PRIVMSG") {
  71.             handle_privmsg(conn, words)
  72.         }
  73.     }
  74.  
  75.  
  76.     fmt.Println("Terminating gobot!")
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement