| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package server
- import (
- "errors"
- "github.com/google/uuid"
- "github.com/mkaminski/goaim/oscar"
- "io"
- "time"
- )
- type ChatNavHandler interface {
- CreateRoomHandler(sess *Session, cr *ChatRegistry, newChatRoom ChatRoomFactory, snacPayloadIn oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate) (XMessage, error)
- RequestChatRightsHandler() XMessage
- RequestRoomInfoHandler(cr *ChatRegistry, snacPayloadIn oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo) (XMessage, error)
- }
- func NewChatNavRouter() ChatNavRouter {
- return ChatNavRouter{
- ChatNavHandler: ChatNavService{},
- }
- }
- type ChatNavRouter struct {
- ChatNavHandler
- }
- func (rt *ChatNavRouter) RouteChatNav(sess *Session, cr *ChatRegistry, SNACFrame oscar.SnacFrame, r io.Reader, w io.Writer, sequence *uint32) error {
- switch SNACFrame.SubGroup {
- case oscar.ChatNavRequestChatRights:
- outSNAC := rt.RequestChatRightsHandler()
- return writeOutSNAC(SNACFrame, outSNAC.snacFrame, outSNAC.snacOut, sequence, w)
- case oscar.ChatNavRequestRoomInfo:
- inSNAC := oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo{}
- if err := oscar.Unmarshal(&inSNAC, r); err != nil {
- return err
- }
- outSNAC, err := rt.RequestRoomInfoHandler(cr, inSNAC)
- if err != nil {
- return err
- }
- return writeOutSNAC(SNACFrame, outSNAC.snacFrame, outSNAC.snacOut, sequence, w)
- case oscar.ChatNavCreateRoom:
- snacPayloadIn := oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{}
- if err := oscar.Unmarshal(&snacPayloadIn, r); err != nil {
- return err
- }
- outSNAC, err := rt.CreateRoomHandler(sess, cr, NewChatRoom, snacPayloadIn)
- if err != nil {
- return err
- }
- return writeOutSNAC(SNACFrame, outSNAC.snacFrame, outSNAC.snacOut, sequence, w)
- default:
- return ErrUnsupportedSubGroup
- }
- }
- type ChatNavService struct {
- }
- type ChatCookie struct {
- Cookie []byte `len_prefix:"uint16"`
- SessID string `len_prefix:"uint16"`
- }
- func (s ChatNavService) RequestChatRightsHandler() XMessage {
- return XMessage{
- snacFrame: oscar.SnacFrame{
- FoodGroup: oscar.CHAT_NAV,
- SubGroup: oscar.ChatNavNavInfo,
- },
- snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: 0x02,
- Val: uint8(10),
- },
- {
- TType: 0x03,
- Val: oscar.SNAC_0x0D_0x09_TLVExchangeInfo{
- Identifier: 4,
- TLVBlock: oscar.TLVBlock{
- TLVList: oscar.TLVList{
- {
- TType: 0x0002,
- Val: uint16(0x0010),
- },
- {
- TType: 0x00c9,
- Val: uint16(15),
- },
- {
- TType: 0x00d3,
- Val: "default Exchange",
- },
- {
- TType: 0x00d5,
- Val: uint8(2),
- },
- {
- TType: 0xd6,
- Val: "us-ascii",
- },
- {
- TType: 0xd7,
- Val: "en",
- },
- {
- TType: 0xd8,
- Val: "us-ascii",
- },
- {
- TType: 0xd9,
- Val: "en",
- },
- },
- },
- },
- },
- },
- },
- },
- }
- }
- func NewChatRoom() ChatRoom {
- return ChatRoom{
- Cookie: uuid.New().String(),
- CreateTime: time.Now(),
- SessionManager: NewSessionManager(),
- }
- }
- type ChatRoomFactory func() ChatRoom
- func (s ChatNavService) CreateRoomHandler(sess *Session, cr *ChatRegistry, newChatRoom ChatRoomFactory, snacPayloadIn oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate) (XMessage, error) {
- name, hasName := snacPayloadIn.GetString(oscar.ChatTLVRoomName)
- if !hasName {
- return XMessage{}, errors.New("unable to find chat name")
- }
- room := newChatRoom()
- room.DetailLevel = snacPayloadIn.DetailLevel
- room.Exchange = snacPayloadIn.Exchange
- room.InstanceNumber = snacPayloadIn.InstanceNumber
- room.Name = name
- cr.Register(room)
- // add user to chat room
- room.NewSessionWithSN(sess.ID, sess.ScreenName)
- return XMessage{
- snacFrame: oscar.SnacFrame{
- FoodGroup: oscar.CHAT_NAV,
- SubGroup: oscar.ChatNavNavInfo,
- },
- snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: oscar.ChatNavTLVRoomInfo,
- Val: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
- Exchange: snacPayloadIn.Exchange,
- Cookie: room.Cookie,
- InstanceNumber: snacPayloadIn.InstanceNumber,
- DetailLevel: snacPayloadIn.DetailLevel,
- TLVBlock: oscar.TLVBlock{
- TLVList: room.TLVList(),
- },
- },
- },
- },
- },
- },
- }, nil
- }
- func (s ChatNavService) RequestRoomInfoHandler(cr *ChatRegistry, snacPayloadIn oscar.SNAC_0x0D_0x04_ChatNavRequestRoomInfo) (XMessage, error) {
- room, err := cr.Retrieve(string(snacPayloadIn.Cookie))
- if err != nil {
- return XMessage{}, err
- }
- return XMessage{
- snacFrame: oscar.SnacFrame{
- FoodGroup: oscar.CHAT_NAV,
- SubGroup: oscar.ChatNavNavInfo,
- },
- snacOut: oscar.SNAC_0x0D_0x09_ChatNavNavInfo{
- TLVRestBlock: oscar.TLVRestBlock{
- TLVList: oscar.TLVList{
- {
- TType: 0x04,
- Val: oscar.SNAC_0x0E_0x02_ChatRoomInfoUpdate{
- Exchange: 4,
- Cookie: room.Cookie,
- InstanceNumber: 100,
- DetailLevel: 2,
- TLVBlock: oscar.TLVBlock{
- TLVList: room.TLVList(),
- },
- },
- },
- },
- },
- },
- }, nil
- }
|