triggers.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/PuerkitoBio/goquery"
  6. "github.com/whyrusleeping/hellabot"
  7. )
  8. var masters = []string{"hardcoded", "nicks", "are", "bad"}
  9. func byMaster(mes *hbot.Message) bool {
  10. for _, s := range masters {
  11. if mes.From == s {
  12. return true
  13. }
  14. }
  15. return false
  16. }
  17. var OpPeople = &hbot.Trigger{
  18. func(mes *hbot.Message) bool {
  19. return mes.Content == "!opme" && byMaster(mes)
  20. },
  21. func(irc *hbot.IrcCon, mes *hbot.Message) bool {
  22. irc.ChMode(mes.To, mes.From, "+o")
  23. return false
  24. },
  25. }
  26. var FollowInvite = &hbot.Trigger{
  27. func(msg *hbot.Message) bool {
  28. return msg.Command == "INVITE" && byMaster(msg)
  29. },
  30. func(irc *hbot.IrcCon, msg *hbot.Message) bool {
  31. irc.Join(msg.Content)
  32. return false
  33. },
  34. }
  35. var TopScore = &hbot.Trigger{
  36. func(msg *hbot.Message) bool {
  37. return msg.Content == "!top" && byMaster(msg)
  38. },
  39. func(irc *hbot.IrcCon, msg *hbot.Message) bool {
  40. ch, ok := irc.Channels[msg.To]
  41. if !ok {
  42. fmt.Println("Error: Channel not registered.")
  43. return false
  44. }
  45. top, err := getTop(5)
  46. if err != nil {
  47. ch.Say("getTop() Error:" + err.Error())
  48. return false
  49. }
  50. for _, t := range top {
  51. ch.Say(t)
  52. }
  53. return false
  54. },
  55. }
  56. func getTop(n int) ([]string, error) {
  57. top := make([]string, n)
  58. doc, err := goquery.NewDocument("http://dctf.defcamp.ro/ranks")
  59. if err != nil {
  60. return nil, err
  61. }
  62. rec := make(chan string)
  63. go func() {
  64. doc.Find(".table-hover > tbody > tr").Each(func(i int, s *goquery.Selection) {
  65. if i > n-1 {
  66. return
  67. }
  68. var (
  69. team = strings.TrimSpace(s.Find(".fa-users").Parent().Text())
  70. points = strings.TrimSpace(s.Find(".fa-flag").Parent().Text())
  71. )
  72. rec <- fmt.Sprintf("#%2d %6s %s\n", i+1, points, team)
  73. })
  74. }()
  75. for idx := 0; idx < n; idx++ {
  76. top[idx] = <-rec
  77. }
  78. close(rec)
  79. return top, nil
  80. }