From: Jeff Halter Date: Sat, 25 Jun 2022 01:45:06 +0000 (-0700) Subject: Implement "Can use any name" permission X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/ea5d8c51c2ebfc3d9d26c1ba7123c35b49efcda6?hp=-c Implement "Can use any name" permission --- ea5d8c51c2ebfc3d9d26c1ba7123c35b49efcda6 diff --git a/hotline/access.go b/hotline/access.go index f2e904d..1bee72c 100644 --- a/hotline/access.go +++ b/hotline/access.go @@ -33,7 +33,7 @@ const ( accessCannotBeDiscon = 23 accessGetClientInfo = 24 accessUploadAnywhere = 25 - // accessAnyName = 26 + accessAnyName = 26 // accessNoAgreement = 27 accessSetFileComment = 28 accessSetFolderComment = 29 diff --git a/hotline/field.go b/hotline/field.go index 3076017..aef790d 100644 --- a/hotline/field.go +++ b/hotline/field.go @@ -19,7 +19,7 @@ const fieldUserAccess = 110 // const fieldUserAlias = 111 TODO: implement const fieldUserFlags = 112 -const fieldOptions = 113 // Server message (1) or admin message (any other value) +const fieldOptions = 113 const fieldChatID = 114 const fieldChatSubject = 115 const fieldWaitingCount = 116 diff --git a/hotline/server.go b/hotline/server.go index ec60527..e5a7b8e 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -627,7 +627,11 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser } if clientLogin.GetField(fieldUserName).Data != nil { - c.UserName = clientLogin.GetField(fieldUserName).Data + if c.Authorize(accessAnyName) { + c.UserName = clientLogin.GetField(fieldUserName).Data + } else { + c.UserName = []byte(c.Account.Name) + } } if clientLogin.GetField(fieldUserIconID).Data != nil { diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index dda2dce..6389ed2 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -908,7 +908,15 @@ func HandleGetUserNameList(cc *ClientConn, t *Transaction) (res []Transaction, e func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err error) { cc.Agreed = true - cc.UserName = t.GetField(fieldUserName).Data + + if t.GetField(fieldUserName).Data != nil { + if cc.Authorize(accessAnyName) { + cc.UserName = t.GetField(fieldUserName).Data + } else { + cc.UserName = []byte(cc.Account.Name) + } + } + *cc.Icon = t.GetField(fieldUserIconID).Data cc.logger = cc.logger.With("name", string(cc.UserName)) @@ -938,7 +946,7 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er cc.AutoReply = []byte{} } - for _, t := range cc.notifyOthers( + trans := cc.notifyOthers( *NewTransaction( tranNotifyChangeUser, nil, NewField(fieldUserName, cc.UserName), @@ -946,12 +954,11 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er NewField(fieldUserIconID, *cc.Icon), NewField(fieldUserFlags, *cc.Flags), ), - ) { - cc.Server.outbox <- t - } + ) + res = append(res, trans...) if cc.Server.Config.BannerFile != "" { - cc.Server.outbox <- *NewTransaction(tranServerBanner, cc.ID, NewField(fieldBannerType, []byte("JPEG"))) + res = append(res, *NewTransaction(tranServerBanner, cc.ID, NewField(fieldBannerType, []byte("JPEG")))) } res = append(res, cc.NewReply(t)) diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index 06782a1..5136c7d 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -2810,3 +2810,80 @@ None. }) } } + +func TestHandleTranAgreed(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "normal request flow", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() *[]byte { + var bits accessBitmap + bits.Set(accessDisconUser) + bits.Set(accessAnyName) + access := bits[:] + return &access + }()}, + Icon: &[]byte{0, 1}, + Flags: &[]byte{0, 1}, + Version: &[]byte{0, 1}, + ID: &[]byte{0, 1}, + logger: NewTestLogger(), + Server: &Server{ + Config: &Config{ + BannerFile: "banner.jpg", + }, + }, + }, + t: NewTransaction( + tranAgreed, nil, + NewField(fieldUserName, []byte("username")), + NewField(fieldUserIconID, []byte{0, 1}), + NewField(fieldOptions, []byte{0, 0}), + ), + }, + wantRes: []Transaction{ + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x00, + Type: []byte{0, 0x7a}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{ + NewField(fieldBannerType, []byte("JPEG")), + }, + }, + { + clientID: &[]byte{0, 1}, + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x79}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + Fields: []Field{}, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleTranAgreed(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleTranAgreed(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +}