session.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package server
  2. import (
  3. "errors"
  4. "sync"
  5. "time"
  6. "github.com/mkaminski/goaim/oscar"
  7. )
  8. var ErrSignedOff = errors.New("user signed off")
  9. type ChatRoom struct {
  10. CreateTime time.Time
  11. DetailLevel uint8
  12. Exchange uint16
  13. Cookie string
  14. InstanceNumber uint16
  15. Name string
  16. SessionManager
  17. }
  18. func (c ChatRoom) TLVList() []oscar.TLV {
  19. return []oscar.TLV{
  20. oscar.NewTLV(0x00c9, uint16(15)),
  21. oscar.NewTLV(0x00ca, uint32(c.CreateTime.Unix())),
  22. oscar.NewTLV(0x00d1, uint16(1024)),
  23. oscar.NewTLV(0x00d2, uint16(100)),
  24. oscar.NewTLV(0x00d5, uint8(2)),
  25. oscar.NewTLV(0x006a, c.Name),
  26. oscar.NewTLV(0x00d3, c.Name),
  27. }
  28. }
  29. type ChatRegistry struct {
  30. store map[string]ChatRoom
  31. mapMutex sync.RWMutex
  32. }
  33. func NewChatRegistry() *ChatRegistry {
  34. return &ChatRegistry{
  35. store: make(map[string]ChatRoom),
  36. }
  37. }
  38. func (c *ChatRegistry) Register(room ChatRoom) {
  39. c.mapMutex.Lock()
  40. defer c.mapMutex.Unlock()
  41. c.store[room.Cookie] = room
  42. }
  43. func (c *ChatRegistry) Retrieve(chatID string) (ChatRoom, error) {
  44. c.mapMutex.RLock()
  45. defer c.mapMutex.RUnlock()
  46. sm, found := c.store[chatID]
  47. if !found {
  48. return sm, errors.New("unable to find session manager for chat")
  49. }
  50. return sm, nil
  51. }
  52. func (c *ChatRegistry) MaybeRemoveRoom(chatID string) {
  53. c.mapMutex.Lock()
  54. defer c.mapMutex.Unlock()
  55. room, found := c.store[chatID]
  56. if found && room.Empty() {
  57. delete(c.store, chatID)
  58. }
  59. }