decode.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package oscarclient
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/mk6i/open-oscar-server/wire"
  7. )
  8. // snacDecoders unmarshal the bodies of the SNACs that in-process clients
  9. // handle, keyed by food group and subgroup.
  10. var snacDecoders = map[[2]uint16]func(r io.Reader) (any, error){
  11. {wire.OService, wire.OServiceHostOnline}: decodeBody[wire.SNAC_0x01_0x03_OServiceHostOnline],
  12. {wire.OService, wire.OServiceServiceResponse}: decodeBody[wire.SNAC_0x01_0x05_OServiceServiceResponse],
  13. {wire.OService, wire.OServiceRateParamChange}: decodeBody[wire.SNAC_0x01_0x0A_OServiceRateParamsChange],
  14. {wire.OService, wire.OServiceUserInfoUpdate}: decodeBody[wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate],
  15. {wire.OService, wire.OServiceEvilNotification}: decodeBody[wire.SNAC_0x01_0x10_OServiceEvilNotification],
  16. {wire.Locate, wire.LocateUserInfoReply}: decodeBody[wire.SNAC_0x02_0x06_LocateUserInfoReply],
  17. {wire.Buddy, wire.BuddyArrived}: decodeBody[wire.SNAC_0x03_0x0B_BuddyArrived],
  18. {wire.Buddy, wire.BuddyDeparted}: decodeBody[wire.SNAC_0x03_0x0C_BuddyDeparted],
  19. {wire.ICBM, wire.ICBMChannelMsgToClient}: decodeBody[wire.SNAC_0x04_0x07_ICBMChannelMsgToClient],
  20. {wire.ICBM, wire.ICBMEvilReply}: decodeBody[wire.SNAC_0x04_0x09_ICBMEvilReply],
  21. {wire.ICBM, wire.ICBMClientEvent}: decodeBody[wire.SNAC_0x04_0x14_ICBMClientEvent],
  22. {wire.Admin, wire.AdminInfoChangeReply}: decodeBody[wire.SNAC_0x07_0x05_AdminChangeReply],
  23. {wire.ChatNav, wire.ChatNavNavInfo}: decodeBody[wire.SNAC_0x0D_0x09_ChatNavNavInfo],
  24. {wire.Chat, wire.ChatUsersJoined}: decodeBody[wire.SNAC_0x0E_0x03_ChatUsersJoined],
  25. {wire.Chat, wire.ChatUsersLeft}: decodeBody[wire.SNAC_0x0E_0x04_ChatUsersLeft],
  26. {wire.Chat, wire.ChatChannelMsgToClient}: decodeBody[wire.SNAC_0x0E_0x06_ChatChannelMsgToClient],
  27. {wire.Feedbag, wire.FeedbagReply}: decodeBody[wire.SNAC_0x13_0x06_FeedbagReply],
  28. {wire.Feedbag, wire.FeedbagInsertItem}: decodeBody[wire.SNAC_0x13_0x08_FeedbagInsertItem],
  29. {wire.Feedbag, wire.FeedbagUpdateItem}: decodeBody[wire.SNAC_0x13_0x09_FeedbagUpdateItem],
  30. {wire.Feedbag, wire.FeedbagDeleteItem}: decodeBody[wire.SNAC_0x13_0x0A_FeedbagDeleteItem],
  31. {wire.Feedbag, wire.FeedbagStatus}: decodeBody[wire.SNAC_0x13_0x0E_FeedbagStatus],
  32. {wire.BUCP, wire.BUCPLoginResponse}: decodeBody[wire.SNAC_0x17_0x03_BUCPLoginResponse],
  33. }
  34. // snacErrorSubGroup is the subgroup of the error SNAC in every food group.
  35. const snacErrorSubGroup = 0x0001
  36. // decodeSNAC unmarshals a SNAC received from the OSCAR server. A SNAC that no
  37. // in-process client handles is returned with a nil body.
  38. func decodeSNAC(payload []byte) (wire.SNACMessage, error) {
  39. r := bytes.NewReader(payload)
  40. msg := wire.SNACMessage{}
  41. if err := wire.UnmarshalBE(&msg.Frame, r); err != nil {
  42. return msg, fmt.Errorf("unmarshal SNAC frame: %w", err)
  43. }
  44. decode := snacDecoders[[2]uint16{msg.Frame.FoodGroup, msg.Frame.SubGroup}]
  45. if msg.Frame.SubGroup == snacErrorSubGroup {
  46. decode = decodeBody[wire.SNACError]
  47. }
  48. if decode == nil {
  49. return msg, nil
  50. }
  51. body, err := decode(r)
  52. if err != nil {
  53. return msg, fmt.Errorf("unmarshal %s: %w",
  54. wire.SubGroupName(msg.Frame.FoodGroup, msg.Frame.SubGroup), err)
  55. }
  56. msg.Body = body
  57. return msg, nil
  58. }
  59. func decodeBody[T any](r io.Reader) (any, error) {
  60. var body T
  61. err := wire.UnmarshalBE(&body, r)
  62. return body, err
  63. }