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.

141 lines
3.3 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, 0)
  29. }
  30. // Log joins and leaves
  31. case "packet.join":
  32. {
  33. if client.Nick() == event.Nick {
  34. bot.addChannel(event.Arg(0))
  35. }
  36. log.Println("(JOIN)", event.Nick, "joined", event.Arg(0))
  37. }
  38. case "packet.part":
  39. {
  40. log.Println("(PART)", event.Nick, "left", event.Arg(0))
  41. }
  42. case "packet.quit":
  43. {
  44. log.Println("(QUIT)", event.Nick, "quit")
  45. }
  46. case "ctcp.action", "packet.privmsg":
  47. {
  48. // The echo-message polyfill will return to sender.
  49. if event.Nick == bot.client.Nick() {
  50. break
  51. }
  52. // Only continue with channel messages, but log the others.
  53. channel := event.ChannelTarget()
  54. if channel == nil {
  55. if event.Verb() == "action" {
  56. log.Printf("Message: * %s %s", event.Nick, event.Text)
  57. } else {
  58. log.Printf("Message: <%s> %s", event.Nick, event.Text)
  59. }
  60. break
  61. }
  62. // Parse account, just in case it'll be useful one day.
  63. account := ""
  64. if user, ok := bot.client.FindUser(event.Nick); ok {
  65. account = user.Account
  66. }
  67. // Get the kind of psot this is.
  68. kind := ""
  69. if strings.HasPrefix(event.Nick, "=") || strings.HasPrefix(event.Nick, "\x1F=") {
  70. kind = "scene"
  71. } else if strings.ToLower(event.Verb()) == "action" {
  72. kind = "action"
  73. } else {
  74. kind = "text"
  75. }
  76. // Format NPC posts' nick and text.
  77. nick := event.Nick
  78. text := event.Text
  79. if strings.HasPrefix(event.Nick, "\x1F") || kind == "scene" {
  80. nick = strings.Replace(event.Nick, "\x1F", "", 2)
  81. text = util.RemoveNPCAttribution(event.Text)
  82. }
  83. // Do not dispatch empty messages.
  84. if len(text) < 1 || len(nick) < 1 || len(kind) < 1 {
  85. log.Println("Ignoring empty message from:", event.Nick)
  86. break
  87. }
  88. // Dispatch it.
  89. bot.handlePost(channel.Name(), ChannelPost{
  90. Account: account,
  91. Kind: kind,
  92. Nick: nick,
  93. Time: event.Time,
  94. Text: text,
  95. })
  96. }
  97. // Log notices
  98. case "packet.notice":
  99. {
  100. log.Printf("Message: -%s- %s", event.Nick, event.Text)
  101. }
  102. // Log initial numerics for debugging
  103. case "packet.001", "packet.002", "packet.003", "packet.251", "packet.255", "packet.265", "packet.266", "packet.250", "packet.375", "packet.372", "packet.376":
  104. {
  105. log.Printf("(%s) %s\n", event.Verb(), event.Text)
  106. }
  107. case "packet.005", "packet.cap", "packet.254":
  108. {
  109. log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args[1:], " "), event.Text)
  110. }
  111. case "packet.error":
  112. {
  113. log.Printf("(%s) %s\n", event.Verb(), event.Text)
  114. os.Exit(1)
  115. }
  116. }
  117. // Always log error numerics.
  118. if len(event.Verb()) == 3 && event.Verb()[0] == '4' || event.Verb()[0] == '9' {
  119. log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text)
  120. }
  121. }