Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

channelreg.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // deleteChannelNoMutex deletes a given channel from our store.
  49. func (server *Server) deleteChannelNoMutex(tx *buntdb.Tx, channelKey string) {
  50. tx.Delete(fmt.Sprintf(keyChannelExists, channelKey))
  51. server.registeredChannels[channelKey] = nil
  52. }
  53. // loadChannelNoMutex loads a channel from the store.
  54. func (server *Server) loadChannelNoMutex(tx *buntdb.Tx, channelKey string) *RegisteredChannel {
  55. // return loaded chan if it already exists
  56. if server.registeredChannels[channelKey] != nil {
  57. return server.registeredChannels[channelKey]
  58. }
  59. _, err := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
  60. if err == buntdb.ErrNotFound {
  61. // chan does not already exist, return
  62. return nil
  63. }
  64. // channel exists, load it
  65. name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
  66. regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
  67. regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
  68. founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
  69. topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
  70. topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
  71. topicSetTime, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
  72. topicSetTimeInt, _ := strconv.ParseInt(topicSetTime, 10, 64)
  73. banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
  74. exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
  75. invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
  76. var banlist []string
  77. _ = json.Unmarshal([]byte(banlistString), &banlist)
  78. var exceptlist []string
  79. _ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
  80. var invitelist []string
  81. _ = json.Unmarshal([]byte(invitelistString), &invitelist)
  82. chanInfo := RegisteredChannel{
  83. Name: name,
  84. RegisteredAt: time.Unix(regTimeInt, 0),
  85. Founder: founder,
  86. Topic: topic,
  87. TopicSetBy: topicSetBy,
  88. TopicSetTime: time.Unix(topicSetTimeInt, 0),
  89. Banlist: banlist,
  90. Exceptlist: exceptlist,
  91. Invitelist: invitelist,
  92. }
  93. server.registeredChannels[channelKey] = &chanInfo
  94. return &chanInfo
  95. }
  96. // saveChannelNoMutex saves a channel to the store.
  97. func (server *Server) saveChannelNoMutex(tx *buntdb.Tx, channelKey string, channelInfo RegisteredChannel) {
  98. tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
  99. tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
  100. tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.Unix(), 10), nil)
  101. tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
  102. tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
  103. tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
  104. tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), strconv.FormatInt(channelInfo.TopicSetTime.Unix(), 10), nil)
  105. banlistString, _ := json.Marshal(channelInfo.Banlist)
  106. tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
  107. exceptlistString, _ := json.Marshal(channelInfo.Exceptlist)
  108. tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
  109. invitelistString, _ := json.Marshal(channelInfo.Invitelist)
  110. tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
  111. server.registeredChannels[channelKey] = &channelInfo
  112. }