2021-12-24 12:41:16 -05:00
|
|
|
package services
|
2021-12-18 16:48:13 -05:00
|
|
|
|
|
|
|
import (
|
2021-12-19 01:14:25 -05:00
|
|
|
"aim-oscar/aimerror"
|
2021-12-18 16:48:13 -05:00
|
|
|
"aim-oscar/models"
|
|
|
|
"aim-oscar/oscar"
|
|
|
|
"aim-oscar/util"
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
)
|
|
|
|
|
2021-12-24 12:41:16 -05:00
|
|
|
var ServiceVersions map[uint16]uint16
|
2021-12-18 16:48:13 -05:00
|
|
|
|
|
|
|
func init() {
|
2021-12-24 12:41:16 -05:00
|
|
|
ServiceVersions = make(map[uint16]uint16)
|
|
|
|
ServiceVersions[1] = 3
|
|
|
|
ServiceVersions[2] = 1
|
|
|
|
ServiceVersions[3] = 1
|
|
|
|
ServiceVersions[4] = 1
|
|
|
|
ServiceVersions[17] = 1
|
2021-12-18 16:48:13 -05:00
|
|
|
}
|
|
|
|
|
2021-12-19 01:14:25 -05:00
|
|
|
type GenericServiceControls struct {
|
2021-12-24 12:41:16 -05:00
|
|
|
OnlineCh chan *models.User
|
|
|
|
ServerHostname string
|
2021-12-19 01:14:25 -05:00
|
|
|
}
|
2021-12-18 16:48:13 -05:00
|
|
|
|
2021-12-19 01:14:25 -05:00
|
|
|
func (g *GenericServiceControls) HandleSNAC(ctx context.Context, db *bun.DB, snac *oscar.SNAC) (context.Context, error) {
|
2021-12-18 16:48:13 -05:00
|
|
|
session, _ := oscar.SessionFromContext(ctx)
|
|
|
|
|
|
|
|
switch snac.Header.Subtype {
|
2021-12-19 01:14:25 -05:00
|
|
|
|
|
|
|
// Client is ONLINE and READY
|
|
|
|
case 0x02:
|
|
|
|
user := models.UserFromContext(ctx)
|
|
|
|
if user != nil {
|
2021-12-19 17:17:47 -05:00
|
|
|
user.Status = models.UserStatusOnline
|
2021-12-19 01:14:25 -05:00
|
|
|
if err := user.Update(ctx, db); err != nil {
|
|
|
|
return ctx, errors.Wrap(err, "could not set user as active")
|
|
|
|
}
|
|
|
|
|
|
|
|
g.OnlineCh <- user
|
|
|
|
|
|
|
|
// Find all of the buddies that are online and tell the user
|
|
|
|
var buddies []*models.Buddy
|
|
|
|
err := db.NewSelect().Model(&buddies).Where("with_uin = ?", user.UIN).Relation("Source").Scan(context.Background(), &buddies)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, errors.Wrapf(err, "could not find user's buddies: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, buddy := range buddies {
|
2021-12-19 17:17:47 -05:00
|
|
|
if buddy.Source.Status != models.UserStatusOnline {
|
2021-12-19 01:14:25 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
onlineSnac := oscar.NewSNAC(3, 0xb)
|
2022-01-16 14:43:48 -05:00
|
|
|
onlineSnac.Data.WriteLPString(buddy.Source.ScreenName)
|
2021-12-19 01:14:25 -05:00
|
|
|
onlineSnac.Data.WriteUint16(0) // TODO: user warning level
|
|
|
|
|
|
|
|
tlvs := []*oscar.TLV{
|
2021-12-24 13:38:22 -05:00
|
|
|
oscar.NewTLV(1, util.Word(0)), // TODO: user class
|
|
|
|
oscar.NewTLV(0x06, util.Dword(uint32(user.Status))), // TODO: User Status
|
|
|
|
// oscar.NewTLV(0x0a, util.Dword(binary.BigEndian.Uint32([]byte(g.ServerHostname)))), // todo: External IP of the client?
|
2021-12-19 01:14:25 -05:00
|
|
|
oscar.NewTLV(0x0f, util.Dword(uint32(time.Since(user.LastActivityAt).Seconds()))), // Idle Time
|
|
|
|
oscar.NewTLV(0x03, util.Dword(uint32(time.Now().Unix()))), // Client Signon Time
|
|
|
|
oscar.NewTLV(0x05, util.Dword(uint32(user.CreatedAt.Unix()))), // Member since
|
|
|
|
}
|
|
|
|
|
|
|
|
onlineSnac.Data.WriteUint16(uint16(len(tlvs)))
|
|
|
|
for _, tlv := range tlvs {
|
|
|
|
onlineSnac.Data.WriteBinary(tlv)
|
|
|
|
}
|
|
|
|
|
|
|
|
onlineFlap := oscar.NewFLAP(2)
|
|
|
|
onlineFlap.Data.WriteBinary(onlineSnac)
|
|
|
|
if err := session.Send(onlineFlap); err != nil {
|
2022-01-16 14:43:48 -05:00
|
|
|
return ctx, errors.Wrapf(err, "could not tell %s that %s is online", buddy.Source.ScreenName, buddy.Target.ScreenName)
|
2021-12-19 01:14:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.NewContextWithUser(ctx, user), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
|
2021-12-18 16:48:13 -05:00
|
|
|
// Client wants to know the rate limits for all services
|
|
|
|
case 0x06:
|
|
|
|
rateSnac := oscar.NewSNAC(1, 7)
|
|
|
|
rateSnac.Data.WriteUint16(1) // one rate class
|
|
|
|
|
|
|
|
// Define a Rate Class
|
|
|
|
rc := oscar.Buffer{}
|
|
|
|
rc.WriteUint16(1) // ID
|
|
|
|
rc.WriteUint32(80) // Window Size
|
|
|
|
rc.WriteUint32(2500) // Clear level
|
|
|
|
rc.WriteUint32(2000) // Alert level
|
|
|
|
rc.WriteUint32(1500) // Limit level
|
|
|
|
rc.WriteUint32(800) // Disconnect level
|
|
|
|
rc.WriteUint32(3400) // Current level (fake)
|
|
|
|
rc.WriteUint32(6000) // Max level
|
|
|
|
rc.WriteUint32(0) // Last time ?
|
|
|
|
rc.WriteUint8(0) // Current state ?
|
|
|
|
rateSnac.Data.Write(rc.Bytes())
|
|
|
|
|
|
|
|
// Define a Rate Group
|
|
|
|
rg := oscar.Buffer{}
|
|
|
|
rg.WriteUint16(1) // ID
|
|
|
|
|
|
|
|
// TODO: make actual rate groups instead of this hack. I can't tell which subtypes are supported so
|
|
|
|
// make it set rate limits for everything family for all subtypes under 0x21.
|
2021-12-24 12:41:16 -05:00
|
|
|
rg.WriteUint16(uint16(len(ServiceVersions)) * 0x21) // Number of rate groups
|
|
|
|
for family := range ServiceVersions {
|
2021-12-18 16:48:13 -05:00
|
|
|
for subtype := 0; subtype < 0x21; subtype++ {
|
|
|
|
rg.WriteUint16(family)
|
|
|
|
rg.WriteUint16(uint16(subtype))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rateSnac.Data.Write(rg.Bytes())
|
|
|
|
|
|
|
|
rateFlap := oscar.NewFLAP(2)
|
|
|
|
rateFlap.Data.WriteBinary(rateSnac)
|
|
|
|
return ctx, session.Send(rateFlap)
|
|
|
|
|
|
|
|
// Client wants their own online information
|
|
|
|
case 0x0e:
|
|
|
|
user := models.UserFromContext(ctx)
|
|
|
|
if user == nil {
|
2021-12-19 01:14:25 -05:00
|
|
|
return ctx, aimerror.NoUserInSession
|
2021-12-18 16:48:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
onlineSnac := oscar.NewSNAC(1, 0xf)
|
2022-01-16 14:43:48 -05:00
|
|
|
onlineSnac.Data.WriteUint8(uint8(len(user.ScreenName)))
|
|
|
|
onlineSnac.Data.WriteString(user.ScreenName)
|
2021-12-18 16:48:13 -05:00
|
|
|
onlineSnac.Data.WriteUint16(0) // warning level
|
|
|
|
|
2021-12-19 17:17:47 -05:00
|
|
|
user.Status = models.UserStatusOnline
|
2021-12-18 17:10:00 -05:00
|
|
|
if err := user.Update(ctx, db); err != nil {
|
|
|
|
return ctx, errors.Wrap(err, "could not set user as active")
|
|
|
|
}
|
|
|
|
|
2021-12-18 16:48:13 -05:00
|
|
|
tlvs := []*oscar.TLV{
|
2021-12-24 13:38:22 -05:00
|
|
|
oscar.NewTLV(0x01, util.Dword(0)), // User Class
|
|
|
|
oscar.NewTLV(0x06, util.Dword(uint32(user.Status))), // TODO: User Status
|
|
|
|
// oscar.NewTLV(0x0a, util.Dword(binary.BigEndian.Uint32([]byte(g.ServerHostname)))), // External IP of the client?
|
2021-12-18 17:10:00 -05:00
|
|
|
oscar.NewTLV(0x0f, util.Dword(uint32(time.Since(user.LastActivityAt).Seconds()))), // Idle Time
|
|
|
|
oscar.NewTLV(0x03, util.Dword(uint32(time.Now().Unix()))), // Client Signon Time
|
|
|
|
oscar.NewTLV(0x01e, util.Dword(0x0)), // Unknown value
|
|
|
|
oscar.NewTLV(0x05, util.Dword(uint32(user.CreatedAt.Unix()))), // Member since
|
2021-12-18 16:48:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
onlineSnac.Data.WriteUint16(uint16(len(tlvs)))
|
|
|
|
for _, tlv := range tlvs {
|
|
|
|
onlineSnac.Data.WriteBinary(tlv)
|
|
|
|
}
|
|
|
|
|
|
|
|
onlineFlap := oscar.NewFLAP(2)
|
|
|
|
onlineFlap.Data.WriteBinary(onlineSnac)
|
2021-12-19 01:14:25 -05:00
|
|
|
return models.NewContextWithUser(ctx, user), session.Send(onlineFlap)
|
2021-12-18 16:48:13 -05:00
|
|
|
|
2021-12-24 12:41:16 -05:00
|
|
|
// Client wants to know the ServiceVersions of all of the services offered
|
2021-12-18 16:48:13 -05:00
|
|
|
case 0x17:
|
|
|
|
versionsSnac := oscar.NewSNAC(1, 0x18)
|
2021-12-24 12:41:16 -05:00
|
|
|
for family, version := range ServiceVersions {
|
2021-12-18 16:48:13 -05:00
|
|
|
versionsSnac.Data.WriteUint16(family)
|
|
|
|
versionsSnac.Data.WriteUint16(version)
|
|
|
|
}
|
|
|
|
versionsFlap := oscar.NewFLAP(2)
|
|
|
|
versionsFlap.Data.WriteBinary(versionsSnac)
|
|
|
|
return ctx, session.Send(versionsFlap)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
}
|