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.

channelreg.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. "encoding/json"
  10. "github.com/tidwall/buntdb"
  11. )
  12. const (
  13. keyChannelExists = "channel.exists %s"
  14. keyChannelName = "channel.name %s" // stores the 'preferred name' of the channel, not casemapped
  15. keyChannelRegTime = "channel.registered.time %s"
  16. keyChannelFounder = "channel.founder %s"
  17. keyChannelTopic = "channel.topic %s"
  18. keyChannelTopicSetBy = "channel.topic.setby %s"
  19. keyChannelTopicSetTime = "channel.topic.settime %s"
  20. keyChannelBanlist = "channel.banlist %s"
  21. keyChannelExceptlist = "channel.exceptlist %s"
  22. keyChannelInvitelist = "channel.invitelist %s"
  23. )
  24. var (
  25. errChanExists = errors.New("Channel already exists")
  26. )
  27. // RegisteredChannel holds details about a given registered channel.
  28. type RegisteredChannel struct {
  29. // Name of the channel.
  30. Name string
  31. // RegisteredAt represents the time that the channel was registered.
  32. RegisteredAt time.Time
  33. // Founder indicates the founder of the channel.
  34. Founder string
  35. // Topic represents the channel topic.
  36. Topic string
  37. // TopicSetBy represents the host that set the topic.
  38. TopicSetBy string
  39. // TopicSetTime represents the time the topic was set.
  40. TopicSetTime time.Time
  41. // Banlist represents the bans set on the channel.
  42. Banlist []string
  43. // Exceptlist represents the exceptions set on the channel.
  44. Exceptlist []string
  45. // Invitelist represents the invite exceptions set on the channel.
  46. Invitelist []string
  47. }
  48. // loadChannelNoMutex loads a channel from the store.
  49. func (server *Server) loadChannelNoMutex(tx *buntdb.Tx, channelKey string) *RegisteredChannel {
  50. // return loaded chan if it already exists
  51. if server.registeredChannels[channelKey] != nil {
  52. return server.registeredChannels[channelKey]
  53. }
  54. _, err := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  55. if err == buntdb.ErrNotFound {
  56. // chan does not already exist, return
  57. return nil
  58. }
  59. // channel exists, load it
  60. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  61. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  62. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  63. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  64. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  65. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  66. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  67. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  68. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  69. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  70. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  71. var banlist []string
  72. _ = json.Unmarshal([]byte(banlistString), &banlist)
  73. var exceptlist []string
  74. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  75. var invitelist []string
  76. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  77. chanInfo := RegisteredChannel{
  78. Name: name,
  79. RegisteredAt: time.Unix(regTimeInt, 0),
  80. Founder: founder,
  81. Topic: topic,
  82. TopicSetBy: topicSetBy,
  83. TopicSetTime: time.Unix(topicSetTimeInt, 0),
  84. Banlist: banlist,
  85. Exceptlist: exceptlist,
  86. Invitelist: invitelist,
  87. }
  88. server.registeredChannels[channelKey] = &chanInfo
  89. return &chanInfo
  90. }
  91. // saveChannelNoMutex saves a channel to the store.
  92. func (server *Server) saveChannelNoMutex(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel) {
  93. tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
  94. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  95. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  96. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  97. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  98. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  99. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  100. banlistString, _ := json.Marshal(channelInfo.Banlist)
  101. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  102. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  103. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  104. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  105. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  106. server.registeredChannels[channelKey] = &channelInfo
  107. }