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.

292 lines
7.8 KiB

  1. package bot
  2. import (
  3. "context"
  4. "log"
  5. "strings"
  6. "time"
  7. "git.aiterp.net/gisle/irc"
  8. "git.aiterp.net/rpdata/logbot3/internal/config"
  9. "git.aiterp.net/rpdata/logbot3/internal/models/channels"
  10. "git.aiterp.net/rpdata/logbot3/internal/models/logs"
  11. "git.aiterp.net/rpdata/logbot3/internal/models/posts"
  12. )
  13. // A Channel represents a handler for a single channel that takes care of the logging and such.
  14. type Channel struct {
  15. ctx context.Context
  16. cancel context.CancelFunc
  17. ch chan ChannelPost
  18. lastPostSessionID string
  19. lastPostTime time.Time
  20. name string
  21. parentCtx context.Context
  22. parent *Bot
  23. client *irc.Client
  24. }
  25. func newChannel(parent *Bot, name string, client *irc.Client) *Channel {
  26. ctx, cancel := context.WithCancel(context.Background())
  27. return &Channel{
  28. name: name,
  29. parent: parent,
  30. parentCtx: parent.ctx,
  31. client: client,
  32. ch: make(chan ChannelPost, 8),
  33. ctx: ctx,
  34. cancel: cancel,
  35. }
  36. }
  37. func (channel *Channel) loop() {
  38. queue := make([]ChannelPost, 0, 8)
  39. minutely := time.NewTicker(time.Minute)
  40. defer channel.cancel()
  41. defer minutely.Stop()
  42. // In case of a crash, it should take ownership of sessions when rejoining.
  43. session, err := logs.FindOpen(channel.parentCtx, channel.name)
  44. if err == nil {
  45. minsUntilDeadline := 1 + int(((time.Hour * 2) - time.Since(session.LatestTime())).Minutes())
  46. if minsUntilDeadline < 1 {
  47. minsUntilDeadline = 1
  48. }
  49. channel.client.Sayf(channel.name, "Session https://aiterp.net/logs/%s will be closed in %d min if there are no new posts.", session.ID, minsUntilDeadline)
  50. channel.lastPostSessionID = session.ID
  51. channel.lastPostTime = session.LatestTime()
  52. }
  53. // If the bot is op, run the commands from the configuration.
  54. go func() {
  55. time.Sleep(time.Second)
  56. commands := config.Get().Commands.OnJoinOp
  57. target := channel.client.Channel(channel.name)
  58. if len(commands) > 0 && target != nil {
  59. me, ok := target.UserList().User(channel.client.Nick())
  60. if ok && strings.ContainsRune(me.Modes, 'o') {
  61. channel.parent.runCommands(commands, target, map[string]string{
  62. "chan": channel.name,
  63. })
  64. }
  65. }
  66. }()
  67. for {
  68. select {
  69. case post := <-channel.ch:
  70. {
  71. log.Printf("Received %s post from %s", post.Kind, post.Nick)
  72. // Handle bot commands, or add to queue otherwise.
  73. if cmd := post.botCommand(); cmd != nil {
  74. switch cmd.Verb {
  75. case "end", "ends", "endsession":
  76. {
  77. session, err := logs.FindOpen(channel.parentCtx, channel.name)
  78. if err == logs.ErrNoneOpen {
  79. channel.client.Say(channel.name, "No open session found. Mission accomplished, I guess?")
  80. break
  81. } else if err != nil {
  82. log.Println("Could not find session:", err)
  83. break
  84. }
  85. _, err = logs.SetOpen(channel.parentCtx, session, false)
  86. if err != nil {
  87. channel.client.Say(channel.name, "Something went wrong when closing the log, please use the website instead.")
  88. log.Println("Could not set open:", err)
  89. }
  90. }
  91. case "tag", "event":
  92. {
  93. if cmd.Text == "" {
  94. channel.client.Say(channel.name, "Usage: !tag <Event Name>")
  95. }
  96. session, err := logs.FindOpen(channel.parentCtx, channel.name)
  97. if err == logs.ErrNoneOpen {
  98. channel.client.Say(channel.name, "No open session found. You can edit closed logs on the website.")
  99. break
  100. } else if err != nil {
  101. log.Println("Could not find session:", err)
  102. break
  103. }
  104. _, err = logs.SetEventName(channel.parentCtx, session, cmd.Text)
  105. if err != nil {
  106. channel.client.Say(channel.name, "Something went wrong when setting the event name, please use the website instead.")
  107. log.Println("Could not set event name:", err)
  108. }
  109. }
  110. }
  111. } else {
  112. queue = append(queue, post)
  113. if post.Time.After(channel.lastPostTime) {
  114. channel.lastPostTime = post.Time
  115. }
  116. }
  117. // Stop here if there's nothing to post.
  118. if len(queue) == 0 {
  119. break
  120. }
  121. // Buffer up posts close to one another.
  122. deadline := time.After(time.Second * 3)
  123. buffering := true
  124. for buffering {
  125. select {
  126. case post := <-channel.ch:
  127. {
  128. queue = append(queue, post)
  129. }
  130. case <-deadline:
  131. {
  132. buffering = false
  133. }
  134. }
  135. }
  136. // Select session.
  137. session, err := logs.FindOpen(channel.parentCtx, channel.name)
  138. if err == logs.ErrNoneOpen {
  139. eventName := ""
  140. channelData, err := channels.Find(channel.parentCtx, channel.name)
  141. if err == nil {
  142. eventName = channelData.EventName
  143. }
  144. session, err = logs.Add(channel.parentCtx, channel.name, queue[0].Time, true, eventName)
  145. if err != nil {
  146. channel.client.Say(channel.name, "This unit failed to open session: "+err.Error())
  147. log.Println("Failed to open session:", err)
  148. }
  149. } else if err != nil {
  150. channel.client.Say(channel.name, "This unit is unable to check active sessions: "+err.Error())
  151. break
  152. }
  153. log.Println("Selected session:", session.ID)
  154. // Remember which session was last posted to.
  155. channel.lastPostSessionID = session.ID
  156. // Post posts
  157. lastSuccess := -1
  158. for i, channelPost := range queue {
  159. post, err := posts.Add(channel.parentCtx, session, channelPost.Time, channelPost.Kind, channelPost.Nick, channelPost.Text)
  160. if err != nil {
  161. log.Println("Failed to post:", err)
  162. break
  163. }
  164. summary := ""
  165. for _, ru := range post.Text {
  166. summary += string(ru)
  167. if len(summary) > 30 {
  168. summary += "..."
  169. break
  170. }
  171. }
  172. log.Printf("Posted (id=%s, kind=%s, nick=%s, delay=%s): %s", post.ID, post.Kind, post.Nick, time.Since(post.Time), summary)
  173. lastSuccess = i
  174. }
  175. if lastSuccess >= 0 {
  176. copy(queue, queue[lastSuccess:])
  177. queue = queue[:len(queue)-(lastSuccess+1)]
  178. }
  179. }
  180. case now := <-minutely.C:
  181. if !channel.lastPostTime.IsZero() && now.Sub(channel.lastPostTime) > (time.Hour*2) {
  182. session, err := logs.FindOpen(channel.parentCtx, channel.name)
  183. if err == logs.ErrNoneOpen {
  184. log.Println(channel.name, "Log already closed.")
  185. channel.lastPostTime = time.Time{}
  186. channel.lastPostSessionID = ""
  187. break
  188. } else if err != nil {
  189. log.Println(channel.name, "Could not find session:", err)
  190. break
  191. }
  192. if session.ID != channel.lastPostSessionID {
  193. log.Println("Aborted auto-close in", channel.name, "due to session change.")
  194. channel.lastPostTime = time.Time{}
  195. channel.lastPostSessionID = ""
  196. break
  197. }
  198. if now.Sub(session.LatestTime()) < (time.Hour * 2) {
  199. log.Println("Aborted auto-close in", channel.name, "due to session being more recent.")
  200. channel.lastPostTime = session.LatestTime()
  201. break
  202. }
  203. _, err = logs.SetOpen(channel.parentCtx, session, false)
  204. if err != nil {
  205. log.Println("Could not set open:", err)
  206. break
  207. }
  208. channel.client.Sayf(channel.name, "Log session closed after 2+ hours of inactivity. See log at https://aiterp.net/logs/%s", channel.lastPostSessionID)
  209. channel.lastPostTime = time.Time{}
  210. channel.lastPostSessionID = ""
  211. }
  212. case <-channel.parentCtx.Done():
  213. {
  214. // Time to pack up shop.
  215. return
  216. }
  217. }
  218. }
  219. }
  220. func (channel *Channel) wait(ctx context.Context) error {
  221. select {
  222. case <-ctx.Done():
  223. return ctx.Err()
  224. case <-channel.ctx.Done():
  225. return nil
  226. }
  227. }
  228. // A ChannelPost is a post made to a channel.
  229. type ChannelPost struct {
  230. Kind string
  231. Time time.Time
  232. Nick string
  233. Text string
  234. Account string
  235. }
  236. func (post *ChannelPost) botCommand() *botCommand {
  237. if !strings.HasPrefix(post.Text, "!") {
  238. return nil
  239. }
  240. split := strings.SplitN(post.Text, " ", 2)
  241. if len(split) == 1 {
  242. return &botCommand{Verb: strings.ToLower(split[0][1:]), Text: ""}
  243. }
  244. return &botCommand{Verb: strings.ToLower(split[0][1:]), Text: split[1]}
  245. }
  246. type botCommand struct {
  247. Verb string
  248. Text string
  249. }