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.

103 lines
1.9 KiB

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/gisle/irc"
  6. "git.aiterp.net/rpdata/logbot3/internal/config"
  7. )
  8. func handler(event *irc.Event, client *irc.Client) {
  9. bot, ok := client.Value(botKey).(*Bot)
  10. if !ok {
  11. return
  12. }
  13. switch event.Name() {
  14. // Handle bot loop
  15. case "hook.ready":
  16. {
  17. log.Println("Client is ready!")
  18. go bot.loop()
  19. }
  20. case "client.disconnect":
  21. {
  22. log.Println("Client disconnected!")
  23. bot.stopLoop()
  24. conf := config.Get().Server
  25. go bot.Connect(conf.Address, conf.SSL, 0)
  26. }
  27. // Log joins and leaves
  28. case "packet.join":
  29. {
  30. if client.Nick() == event.Nick {
  31. bot.addChannel(event.Arg(0))
  32. }
  33. log.Println("(JOIN)", event.Nick, "joined", event.Arg(0))
  34. }
  35. case "packet.part":
  36. {
  37. log.Println("(PART)", event.Nick, "left", event.Arg(0))
  38. }
  39. case "packet.quit":
  40. {
  41. log.Println("(QUIT)", event.Nick, "quit")
  42. }
  43. case "ctcp.action", "packet.privmsg":
  44. {
  45. if event.Nick == bot.client.Nick() {
  46. break
  47. }
  48. channel := event.ChannelTarget()
  49. if channel == nil {
  50. break
  51. }
  52. account := ""
  53. if user, ok := bot.client.FindUser(event.Nick); ok {
  54. account = user.Account
  55. }
  56. kind := ""
  57. if event.Verb() == "action" {
  58. kind = "action"
  59. } else {
  60. if strings.HasPrefix(event.Nick, "=") {
  61. kind = "scene"
  62. } else {
  63. kind = "text"
  64. }
  65. }
  66. post := ChannelPost{
  67. Account: account,
  68. Kind: kind,
  69. Nick: event.Nick,
  70. Time: event.Time,
  71. Text: event.Text,
  72. }
  73. bot.handlePost(channel.Name(), post)
  74. }
  75. // Log initial numerics for debugging's sake
  76. case "packet.001", "packet.002", "packet.003", "packet.251", "packet.255", "packet.265", "packet.266", "packet.250", "packet.375", "packet.372", "packet.376":
  77. {
  78. log.Printf("(%s) %s\n", event.Verb(), event.Text)
  79. }
  80. case "packet.005", "packet.254":
  81. {
  82. log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text)
  83. }
  84. }
  85. }
  86. func init() {
  87. irc.Handle(handler)
  88. }