Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "fmt"
  6. "log"
  7. "net"
  8. "net/textproto"
  9. "strings"
  10. )
  11.  
  12. const (
  13. JOIN = "JOIN"
  14. NICK = "NICK"
  15. PART = "PART"
  16. PRIVMSG = "PRIVMSG"
  17. QUIT = "QUIT"
  18. USER = "USER"
  19. )
  20.  
  21. type Message struct {
  22. Prefix string
  23. Command string
  24. Params []string
  25. Trailing []string
  26. }
  27.  
  28. func (m *Message) Parse(line string) {
  29. prefixEnd, trailingStart := -1, len(line)
  30. m.Prefix, m.Command = "", ""
  31. if strings.HasPrefix(line, ":") {
  32. prefixEnd = strings.Index(line, " ")
  33. m.Prefix = line[1:prefixEnd]
  34. }
  35. trailingStart = strings.Index(line, " :")
  36. if trailingStart >= 0 {
  37. m.Trailing = strings.Fields(line[trailingStart+2:])
  38. } else {
  39. trailingStart = len(line) - 1
  40. }
  41. cmdAndParams := strings.Fields(line[(prefixEnd + 1) : trailingStart+1])
  42. if len(cmdAndParams) > 0 {
  43. m.Command = cmdAndParams[0]
  44. }
  45. if len(cmdAndParams) > 1 {
  46. m.Params = cmdAndParams[1:]
  47. }
  48. }
  49.  
  50. func NewMessage() *Message {
  51. m := new(Message)
  52. return m
  53. }
  54.  
  55. type Bot struct {
  56. Server string
  57. Port int
  58. Channels []string
  59. User string
  60. Nick string
  61. Password string
  62. Conn net.Conn
  63. Reader *textproto.Reader
  64. Writer *textproto.Writer
  65. Handlers map[string]string
  66. }
  67.  
  68. func (bot *Bot) Connect() error {
  69. conn, err := net.Dial("tcp", bot.url())
  70. if err != nil {
  71. return err
  72. }
  73. bot.Conn = conn
  74. bot.Reader = textproto.NewReader(bufio.NewReader(bot.Conn))
  75. bot.Writer = textproto.NewWriter(bufio.NewWriter(bot.Conn))
  76. bot.Writer.PrintfLine("%s %s 8 * :%s", USER, bot.User, bot.User)
  77. bot.Writer.PrintfLine("%s %s", NICK, bot.Nick)
  78. return nil
  79. }
  80.  
  81. func (bot *Bot) HandleActions() {
  82. message := NewMessage()
  83. for {
  84. line, err := bot.Reader.ReadLine()
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. fmt.Printf("%s\n", line)
  89. message.Parse(line)
  90. if response, ok := bot.Handlers[message.Trailing[0]]; ok {
  91. bot.Writer.PrintfLine("%s %s %s", PRIVMSG, message.Params[0], response)
  92. }
  93. }
  94. }
  95.  
  96. func (bot *Bot) Ident() {
  97. if bot.Password != "" {
  98. bot.Writer.PrintfLine("%s NickServ :identify %s %s", PRIVMSG, bot.Nick, bot.Password)
  99. }
  100. }
  101.  
  102. func (bot *Bot) Join() {
  103. for _, channel := range bot.Channels {
  104. bot.Writer.PrintfLine("%s %s", JOIN, channel)
  105. }
  106. }
  107.  
  108. func (bot *Bot) url() string {
  109. return fmt.Sprintf("%s:%d", bot.Server, bot.Port)
  110. }
  111.  
  112. func NewBot(server string, port int, channels []string, user, nick, password string) *Bot {
  113. bot := &Bot{
  114. Server: server,
  115. Port: port,
  116. Channels: channels,
  117. User: user,
  118. Nick: nick,
  119. Password: password,
  120. Handlers: make(map[string]string),
  121. }
  122. return bot
  123. }
  124.  
  125. func main() {
  126. server := "irc.freenode.net"
  127. port := 6667
  128. channels := []string{"#bottestchannel"}
  129. user := "bottestuser"
  130. nick := "bottestnick"
  131. password := ""
  132. ircbot := NewBot(server, port, channels, user, nick, password)
  133. ircbot.Handlers["!test"] = fmt.Sprintf("passed!")
  134. ircbot.Connect()
  135. ircbot.Join()
  136. ircbot.HandleActions()
  137. defer ircbot.Conn.Close()
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement