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.

126 lines
2.8 KiB

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