The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package bot
  2. import (
  3. "log"
  4. "os"
  5. "strings"
  6. "github.com/gissleh/irc"
  7. "git.aiterp.net/rpdata/logbot3/internal/config"
  8. "git.aiterp.net/rpdata/logbot3/internal/util"
  9. )
  10. func handler(event *irc.Event, client *irc.Client) {
  11. bot, ok := client.Value(botKey).(*Bot)
  12. if !ok {
  13. return
  14. }
  15. switch event.Name() {
  16. // Handle bot loop
  17. case "hook.ready":
  18. {
  19. log.Println("Client is ready!")
  20. go bot.loop()
  21. bot.runCommands(config.Get().Commands.OnReady, bot.client.Status(), nil)
  22. }
  23. case "client.disconnect":
  24. {
  25. log.Println("Client disconnected!")
  26. bot.stopLoop()
  27. conf := config.Get().Server
  28. go bot.Connect(conf.Address, conf.SSL, 100)
  29. }
  30. // Log Connections
  31. case "client.connecting":
  32. {
  33. log.Println("Client is connecting...")
  34. }
  35. case "client.connect":
  36. {
  37. log.Println("Client connected!")
  38. }
  39. // Log joins and leaves
  40. case "packet.join":
  41. {
  42. if client.Nick() == event.Nick {
  43. bot.addChannel(event.Arg(0))
  44. }
  45. log.Println("(JOIN)", event.Nick, "joined", event.Arg(0))
  46. }
  47. case "packet.part":
  48. {
  49. log.Println("(PART)", event.Nick, "left", event.Arg(0))
  50. }
  51. case "packet.quit":
  52. {
  53. log.Println("(QUIT)", event.Nick, "quit")
  54. }
  55. case "ctcp.action", "packet.privmsg":
  56. {
  57. // The echo-message polyfill will return to sender.
  58. if event.Nick == bot.client.Nick() {
  59. break
  60. }
  61. // Only continue with channel messages, but log the others.
  62. channel := event.ChannelTarget()
  63. if channel == nil {
  64. if event.Verb() == "action" {
  65. log.Printf("Message: * %s %s", event.Nick, event.Text)
  66. } else {
  67. log.Printf("Message: <%s> %s", event.Nick, event.Text)
  68. }
  69. break
  70. }
  71. // Parse account, just in case it'll be useful one day.
  72. account := ""
  73. if user, ok := bot.client.FindUser(event.Nick); ok {
  74. account = user.Account
  75. }
  76. // Get the kind of psot this is.
  77. kind := ""
  78. if strings.HasPrefix(event.Nick, "=") || strings.HasPrefix(event.Nick, "\x1F=") {
  79. kind = "scene"
  80. } else if strings.ToLower(event.Verb()) == "action" {
  81. kind = "action"
  82. } else {
  83. kind = "text"
  84. }
  85. // Format NPC posts' nick and text.
  86. nick := event.Nick
  87. text := event.Text
  88. if strings.HasPrefix(event.Nick, "\x1F") || kind == "scene" {
  89. nick = strings.Replace(event.Nick, "\x1F", "", 2)
  90. text = util.RemoveNPCAttribution(event.Text)
  91. }
  92. // Do not dispatch empty messages.
  93. if len(text) < 1 || len(nick) < 1 || len(kind) < 1 {
  94. log.Println("Ignoring empty message from:", event.Nick)
  95. break
  96. }
  97. // Dispatch it.
  98. bot.handlePost(channel.Name(), ChannelPost{
  99. Account: account,
  100. Kind: kind,
  101. Nick: nick,
  102. Time: event.Time,
  103. Text: text,
  104. })
  105. }
  106. // Log notices
  107. case "packet.notice":
  108. {
  109. log.Printf("Message: -%s- %s", event.Nick, event.Text)
  110. }
  111. // Log initial numerics for debugging
  112. case "packet.001", "packet.002", "packet.003", "packet.251", "packet.255", "packet.265", "packet.266", "packet.250", "packet.375", "packet.372", "packet.376":
  113. {
  114. log.Printf("(%s) %s\n", event.Verb(), event.Text)
  115. }
  116. case "packet.005", "packet.cap", "packet.254":
  117. {
  118. log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args[1:], " "), event.Text)
  119. }
  120. case "packet.error":
  121. {
  122. log.Printf("(%s) %s\n", event.Verb(), event.Text)
  123. os.Exit(1)
  124. }
  125. }
  126. if event.Kind() == "error" {
  127. log.Printf("Client Error (%s) %s\n", event.Verb(), event.Text)
  128. }
  129. // Always log error numerics.
  130. if len(event.Verb()) == 3 && event.Verb()[0] == '4' || event.Verb()[0] == '9' {
  131. log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text)
  132. }
  133. }