locate.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package handler
  2. import (
  3. "context"
  4. "github.com/mkaminski/goaim/oscar"
  5. "github.com/mkaminski/goaim/state"
  6. )
  7. func NewLocateService(messageRelayer MessageRelayer, feedbagManager FeedbagManager, profileManager ProfileManager) LocateService {
  8. return LocateService{
  9. sessionManager: messageRelayer,
  10. feedbagManager: feedbagManager,
  11. profileManager: profileManager,
  12. }
  13. }
  14. type LocateService struct {
  15. sessionManager MessageRelayer
  16. feedbagManager FeedbagManager
  17. profileManager ProfileManager
  18. }
  19. func (s LocateService) RightsQueryHandler(_ context.Context, inFrame oscar.SNACFrame) oscar.SNACMessage {
  20. return oscar.SNACMessage{
  21. Frame: oscar.SNACFrame{
  22. FoodGroup: oscar.Locate,
  23. SubGroup: oscar.LocateRightsReply,
  24. RequestID: inFrame.RequestID,
  25. },
  26. Body: oscar.SNAC_0x02_0x03_LocateRightsReply{
  27. TLVRestBlock: oscar.TLVRestBlock{
  28. TLVList: oscar.TLVList{
  29. oscar.NewTLV(0x01, uint16(1000)),
  30. oscar.NewTLV(0x02, uint16(1000)),
  31. oscar.NewTLV(0x03, uint16(1000)),
  32. oscar.NewTLV(0x04, uint16(1000)),
  33. oscar.NewTLV(0x05, uint16(1000)),
  34. },
  35. },
  36. },
  37. }
  38. }
  39. func (s LocateService) SetInfoHandler(ctx context.Context, sess *state.Session, inBody oscar.SNAC_0x02_0x04_LocateSetInfo) error {
  40. // update profile
  41. if profile, hasProfile := inBody.GetString(oscar.LocateTLVTagsInfoSigData); hasProfile {
  42. if err := s.profileManager.UpsertProfile(sess.ScreenName(), profile); err != nil {
  43. return err
  44. }
  45. }
  46. // broadcast away message change to buddies
  47. if awayMsg, hasAwayMsg := inBody.GetString(oscar.LocateTLVTagsInfoUnavailableData); hasAwayMsg {
  48. sess.SetAwayMessage(awayMsg)
  49. if err := broadcastArrival(ctx, sess, s.sessionManager, s.feedbagManager); err != nil {
  50. return err
  51. }
  52. }
  53. return nil
  54. }
  55. func (s LocateService) UserInfoQuery2Handler(ctx context.Context, sess *state.Session, inFrame oscar.SNACFrame, inBody oscar.SNAC_0x02_0x15_LocateUserInfoQuery2) (oscar.SNACMessage, error) {
  56. blocked, err := s.feedbagManager.Blocked(sess.ScreenName(), inBody.ScreenName)
  57. switch {
  58. case err != nil:
  59. return oscar.SNACMessage{}, err
  60. case blocked != state.BlockedNo:
  61. return oscar.SNACMessage{
  62. Frame: oscar.SNACFrame{
  63. FoodGroup: oscar.Locate,
  64. SubGroup: oscar.LocateErr,
  65. RequestID: inFrame.RequestID,
  66. },
  67. Body: oscar.SNACError{
  68. Code: oscar.ErrorCodeNotLoggedOn,
  69. },
  70. }, nil
  71. }
  72. buddySess := s.sessionManager.RetrieveByScreenName(inBody.ScreenName)
  73. if buddySess == nil {
  74. return oscar.SNACMessage{
  75. Frame: oscar.SNACFrame{
  76. FoodGroup: oscar.Locate,
  77. SubGroup: oscar.LocateErr,
  78. RequestID: inFrame.RequestID,
  79. },
  80. Body: oscar.SNACError{
  81. Code: oscar.ErrorCodeNotLoggedOn,
  82. },
  83. }, nil
  84. }
  85. var list oscar.TLVList
  86. if inBody.RequestProfile() {
  87. profile, err := s.profileManager.RetrieveProfile(inBody.ScreenName)
  88. if err != nil {
  89. return oscar.SNACMessage{}, err
  90. }
  91. list.AddTLVList([]oscar.TLV{
  92. oscar.NewTLV(oscar.LocateTLVTagsInfoSigMime, `text/aolrtf; charset="us-ascii"`),
  93. oscar.NewTLV(oscar.LocateTLVTagsInfoSigData, profile),
  94. })
  95. }
  96. if inBody.RequestAwayMessage() {
  97. list.AddTLVList([]oscar.TLV{
  98. oscar.NewTLV(oscar.LocateTLVTagsInfoUnavailableMime, `text/aolrtf; charset="us-ascii"`),
  99. oscar.NewTLV(oscar.LocateTLVTagsInfoUnavailableData, buddySess.AwayMessage()),
  100. })
  101. }
  102. return oscar.SNACMessage{
  103. Frame: oscar.SNACFrame{
  104. FoodGroup: oscar.Locate,
  105. SubGroup: oscar.LocateUserInfoReply,
  106. RequestID: inFrame.RequestID,
  107. },
  108. Body: oscar.SNAC_0x02_0x06_LocateUserInfoReply{
  109. TLVUserInfo: buddySess.TLVUserInfo(),
  110. LocateInfo: oscar.TLVRestBlock{
  111. TLVList: list,
  112. },
  113. },
  114. }, nil
  115. }
  116. func (s LocateService) SetDirInfoHandler(_ context.Context) oscar.SNACMessage {
  117. return oscar.SNACMessage{
  118. Frame: oscar.SNACFrame{
  119. FoodGroup: oscar.Locate,
  120. SubGroup: oscar.LocateSetDirReply,
  121. },
  122. Body: oscar.SNAC_0x02_0x0A_LocateSetDirReply{
  123. Result: 1,
  124. },
  125. }
  126. }
  127. func (s LocateService) SetKeywordInfoHandler(_ context.Context, inFrame oscar.SNACFrame) oscar.SNACMessage {
  128. return oscar.SNACMessage{
  129. Frame: oscar.SNACFrame{
  130. FoodGroup: oscar.Locate,
  131. SubGroup: oscar.LocateSetKeywordReply,
  132. RequestID: inFrame.RequestID,
  133. },
  134. Body: oscar.SNAC_0x02_0x10_LocateSetKeywordReply{
  135. Unknown: 1,
  136. },
  137. }
  138. }