Guest User

Untitled

a guest
Sep 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net/textproto"
  6. "regexp"
  7. "strings"
  8. "os"
  9. )
  10.  
  11.  
  12. type PrivMsg struct {
  13. nick, channel, text string
  14. }
  15.  
  16.  
  17. var (
  18. conn *textproto.Conn
  19. err os.Error
  20.  
  21. ping = regexp.MustCompile("^PING :([a-zA-Z0-9\\.]+)$")
  22. motd = regexp.MustCompile(":End of /MOTD command\\.$")
  23. privmsg = regexp.MustCompile("^:([a-zA-Z0-9`_\\-]+)![a-zA-Z0-9/\\\\\\.\\-]+@[a-zA-Z0-9/\\\\\\.\\-]+ PRIVMSG (#[a-zA-Z0-9]+) :(.*)$")
  24. )
  25.  
  26.  
  27. func talk(channel, msg string) {
  28. conn.Cmd("PRIVMSG " + channel + " :" + msg)
  29. }
  30.  
  31.  
  32. func handlePing(auth string) {
  33. conn.Cmd("PONG :" + auth)
  34. fmt.Printf("PONG :%s\n", auth)
  35. }
  36.  
  37.  
  38. func handlePrivmsg(pm *PrivMsg) {
  39. if strings.Contains(pm.text, "MrGoBot") {
  40. talk(pm.channel, "Hello, " + pm.nick + "!")
  41. }
  42. }
  43.  
  44.  
  45. func handleMotd() {
  46. conn.Cmd("JOIN #GoBot")
  47. fmt.Println("JOIN #GoBot")
  48. }
  49.  
  50.  
  51. func parseLine(line string) {
  52. // Channel activity
  53. if match := privmsg.FindStringSubmatch(line); match != nil {
  54. pm := new(PrivMsg)
  55. pm.nick, pm.channel, pm.text = match[1], match[2], match[3]
  56. handlePrivmsg(pm)
  57. return
  58. }
  59.  
  60. // Server PING
  61. if match := ping.FindStringSubmatch(line); match != nil {
  62. handlePing(match[1])
  63. return
  64. }
  65.  
  66. // End of MOTD (successful login to IRC server)
  67. if match := motd.FindString(line); match != "" {
  68. handleMotd()
  69. return
  70. }
  71. }
  72.  
  73.  
  74. func main() {
  75. conn, err = textproto.Dial("tcp", "chat.freenode.net:6667")
  76. if err != nil {
  77. fmt.Printf("%s", err)
  78. return
  79. }
  80.  
  81. conn.Cmd("NICK MrGoBot\n\rUSER mrgobot 8 * :MrGoBot")
  82.  
  83. for {
  84. text, err := conn.ReadLine()
  85. if err != nil {
  86. fmt.Printf("%s", err)
  87. return
  88. }
  89.  
  90. go parseLine(text)
  91.  
  92. fmt.Println(text)
  93. }
  94. }
Add Comment
Please, Sign In to add comment