]> git.r.bdr.sh - rbdr/mobius/commitdiff
Implement "Refuse private messages" client preference
authorJeff Halter <redacted>
Mon, 4 Jul 2022 03:48:05 +0000 (20:48 -0700)
committerJeff Halter <redacted>
Mon, 4 Jul 2022 03:48:05 +0000 (20:48 -0700)
hotline/transaction_handlers.go
hotline/transaction_handlers_test.go

index 46fe362bf687b1165e3854de3a3105af2e3f7d08..abe2ea8d2c72c01695f7ad474f4c0c63c8857b38 100644 (file)
@@ -321,14 +321,29 @@ func HandleSendInstantMsg(cc *ClientConn, t *Transaction) (res []Transaction, er
                reply.Fields = append(reply.Fields, NewField(fieldQuotingMsg, t.GetField(fieldQuotingMsg).Data))
        }
 
                reply.Fields = append(reply.Fields, NewField(fieldQuotingMsg, t.GetField(fieldQuotingMsg).Data))
        }
 
-       res = append(res, *reply)
-
        id, _ := byteToInt(ID.Data)
        otherClient, ok := cc.Server.Clients[uint16(id)]
        if !ok {
                return res, errors.New("invalid client ID")
        }
 
        id, _ := byteToInt(ID.Data)
        otherClient, ok := cc.Server.Clients[uint16(id)]
        if !ok {
                return res, errors.New("invalid client ID")
        }
 
+       // Check if target user has "Refuse private messages" flag
+       flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(otherClient.Flags)))
+       if flagBitmap.Bit(userFLagRefusePChat) == 1 {
+               res = append(res,
+                       *NewTransaction(
+                               tranServerMsg,
+                               cc.ID,
+                               NewField(fieldData, []byte(string(otherClient.UserName)+" does not accept private messages.")),
+                               NewField(fieldUserName, otherClient.UserName),
+                               NewField(fieldUserID, *otherClient.ID),
+                               NewField(fieldOptions, []byte{0, 2}),
+                       ),
+               )
+       } else {
+               res = append(res, *reply)
+       }
+
        // Respond with auto reply if other client has it enabled
        if len(otherClient.AutoReply) > 0 {
                res = append(res,
        // Respond with auto reply if other client has it enabled
        if len(otherClient.AutoReply) > 0 {
                res = append(res,
index e141c78dd29d0f52b19c0c4587a66a58cdec07bb..fd66feae5af7a240ac947c6d9039fed939d6d046 100644 (file)
@@ -2302,6 +2302,7 @@ func TestHandleSendInstantMsg(t *testing.T) {
                                                Clients: map[uint16]*ClientConn{
                                                        uint16(2): {
                                                                AutoReply: []byte(nil),
                                                Clients: map[uint16]*ClientConn{
                                                        uint16(2): {
                                                                AutoReply: []byte(nil),
+                                                               Flags:     []byte{0, 0},
                                                        },
                                                },
                                        },
                                                        },
                                                },
                                        },
@@ -2350,6 +2351,7 @@ func TestHandleSendInstantMsg(t *testing.T) {
                                        Server: &Server{
                                                Clients: map[uint16]*ClientConn{
                                                        uint16(2): {
                                        Server: &Server{
                                                Clients: map[uint16]*ClientConn{
                                                        uint16(2): {
+                                                               Flags:     []byte{0, 0},
                                                                ID:        &[]byte{0, 2},
                                                                UserName:  []byte("User2"),
                                                                AutoReply: []byte("autohai"),
                                                                ID:        &[]byte{0, 2},
                                                                UserName:  []byte("User2"),
                                                                AutoReply: []byte("autohai"),
@@ -2393,6 +2395,57 @@ func TestHandleSendInstantMsg(t *testing.T) {
                        },
                        wantErr: assert.NoError,
                },
                        },
                        wantErr: assert.NoError,
                },
+               {
+                       name: "when client 2 has refuse private messages enabled",
+                       args: args{
+                               cc: &ClientConn{
+                                       Account: &Account{
+                                               Access: func() accessBitmap {
+                                                       var bits accessBitmap
+                                                       bits.Set(accessSendPrivMsg)
+                                                       return bits
+                                               }(),
+                                       },
+                                       ID:       &[]byte{0, 1},
+                                       UserName: []byte("User1"),
+                                       Server: &Server{
+                                               Clients: map[uint16]*ClientConn{
+                                                       uint16(2): {
+                                                               Flags:    []byte{255, 255},
+                                                               ID:       &[]byte{0, 2},
+                                                               UserName: []byte("User2"),
+                                                       },
+                                               },
+                                       },
+                               },
+                               t: NewTransaction(
+                                       tranSendInstantMsg,
+                                       &[]byte{0, 1},
+                                       NewField(fieldData, []byte("hai")),
+                                       NewField(fieldUserID, []byte{0, 2}),
+                               ),
+                       },
+                       wantRes: []Transaction{
+                               *NewTransaction(
+                                       tranServerMsg,
+                                       &[]byte{0, 1},
+                                       NewField(fieldData, []byte("User2 does not accept private messages.")),
+                                       NewField(fieldUserName, []byte("User2")),
+                                       NewField(fieldUserID, []byte{0, 2}),
+                                       NewField(fieldOptions, []byte{0, 2}),
+                               ),
+                               {
+                                       clientID:  &[]byte{0, 1},
+                                       Flags:     0x00,
+                                       IsReply:   0x01,
+                                       Type:      []byte{0x0, 0x6c},
+                                       ID:        []byte{0, 0, 0, 0},
+                                       ErrorCode: []byte{0, 0, 0, 0},
+                                       Fields:    []Field(nil),
+                               },
+                       },
+                       wantErr: assert.NoError,
+               },
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {