Procházet zdrojové kódy

issue #89 - reflect chat message back to TOC sender

Mike před 1 rokem
rodič
revize
393af7d674
2 změnil soubory, kde provedl 122 přidání a 2 odebrání
  1. 31 2
      server/toc/cmd_client.go
  2. 91 0
      server/toc/cmd_client_test.go

+ 31 - 2
server/toc/cmd_client.go

@@ -628,11 +628,40 @@ func (s OSCARProxy) ChatSend(ctx context.Context, chatRegistry *ChatRegistry, cm
 		Channel:      wire.ICBMChannelMIME,
 		TLVRestBlock: block,
 	}
-	if _, err := s.ChatService.ChannelMsgToHost(ctx, me, wire.SNACFrame{}, snac); err != nil {
+	reply, err := s.ChatService.ChannelMsgToHost(ctx, me, wire.SNACFrame{}, snac)
+	if err != nil {
 		return s.runtimeErr(ctx, fmt.Errorf("ChatService.ChannelMsgToHost: %w", err))
 	}
 
-	return fmt.Sprintf("CHAT_IN:%d:%s:F:%s", chatID, me.DisplayScreenName(), msg)
+	if reply == nil {
+		return s.runtimeErr(ctx, errors.New("ChatService.ChannelMsgToHost: missing response "))
+	}
+
+	switch v := reply.Body.(type) {
+	case wire.SNAC_0x0E_0x06_ChatChannelMsgToClient:
+		msgInfo, ok := v.Bytes(wire.ChatTLVMessageInfo)
+		if !ok {
+			return s.runtimeErr(ctx, errors.New("ChatService.ChannelMsgToHost: missing wire.ChatTLVMessageInfo"))
+		}
+		reflectMsg, err := wire.UnmarshalChatMessageText(msgInfo)
+		if err != nil {
+			return s.runtimeErr(ctx, fmt.Errorf("wire.UnmarshalChatMessageText: %w", err))
+		}
+
+		senderInfo, ok := v.Bytes(wire.ChatTLVSenderInformation)
+		if !ok {
+			return s.runtimeErr(ctx, errors.New("ChatService.ChannelMsgToHost: missing wire.ChatTLVSenderInformation"))
+		}
+
+		var userInfo wire.TLVUserInfo
+		if err := wire.UnmarshalBE(&userInfo, bytes.NewReader(senderInfo)); err != nil {
+			return s.runtimeErr(ctx, fmt.Errorf("wire.UnmarshalBE: %w", err))
+		}
+
+		return fmt.Sprintf("CHAT_IN:%d:%s:F:%s", chatID, userInfo.ScreenName, reflectMsg)
+	default:
+		return s.runtimeErr(ctx, errors.New("ChatService.ChannelMsgToHost: unexpected response"))
+	}
 }
 
 // Evil handles the toc_evil TOC command.

+ 91 - 0
server/toc/cmd_client_test.go

@@ -1068,6 +1068,23 @@ func TestOSCARProxy_ChatSend(t *testing.T) {
 									},
 								},
 							},
+							result: &wire.SNACMessage{
+								Body: wire.SNAC_0x0E_0x06_ChatChannelMsgToClient{
+									Channel: wire.ICBMChannelMIME,
+									TLVRestBlock: wire.TLVRestBlock{
+										TLVList: wire.TLVList{
+											wire.NewTLVBE(wire.ChatTLVSenderInformation,
+												newTestSession("me").TLVUserInfo()),
+											wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
+											wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
+												TLVList: wire.TLVList{
+													wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
+												},
+											}),
+										},
+									},
+								},
+							},
 						},
 					},
 				},
@@ -1110,6 +1127,80 @@ func TestOSCARProxy_ChatSend(t *testing.T) {
 			},
 			wantMsg: cmdInternalSvcErr,
 		},
+		{
+			name:     "send chat message, receive nil response from chat svc",
+			me:       newTestSession("me"),
+			givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
+			givenChatRegistry: func() *ChatRegistry {
+				reg := NewChatRegistry()
+				reg.RegisterSess(0, newTestSession("me"))
+				return reg
+			}(),
+			mockParams: mockParams{
+				chatParams: chatParams{
+					channelMsgToHostParamsChat: channelMsgToHostParamsChat{
+						{
+							sender: state.NewIdentScreenName("me"),
+							inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
+								Channel: wire.ICBMChannelMIME,
+								TLVRestBlock: wire.TLVRestBlock{
+									TLVList: wire.TLVList{
+										wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
+										wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
+										wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
+										wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
+											TLVList: wire.TLVList{
+												wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
+											},
+										}),
+									},
+								},
+							},
+							result: nil,
+						},
+					},
+				},
+			},
+			wantMsg: cmdInternalSvcErr,
+		},
+		{
+			name:     "send chat message, receive unexpected response from chat svc",
+			me:       newTestSession("me"),
+			givenCmd: []byte(`toc_chat_send 0 "Hello world!"`),
+			givenChatRegistry: func() *ChatRegistry {
+				reg := NewChatRegistry()
+				reg.RegisterSess(0, newTestSession("me"))
+				return reg
+			}(),
+			mockParams: mockParams{
+				chatParams: chatParams{
+					channelMsgToHostParamsChat: channelMsgToHostParamsChat{
+						{
+							sender: state.NewIdentScreenName("me"),
+							inBody: wire.SNAC_0x0E_0x05_ChatChannelMsgToHost{
+								Channel: wire.ICBMChannelMIME,
+								TLVRestBlock: wire.TLVRestBlock{
+									TLVList: wire.TLVList{
+										wire.NewTLVBE(wire.ChatTLVEnableReflectionFlag, uint8(1)),
+										wire.NewTLVBE(wire.ChatTLVSenderInformation, newTestSession("me").TLVUserInfo()),
+										wire.NewTLVBE(wire.ChatTLVPublicWhisperFlag, []byte{}),
+										wire.NewTLVBE(wire.ChatTLVMessageInfo, wire.TLVRestBlock{
+											TLVList: wire.TLVList{
+												wire.NewTLVBE(wire.ChatTLVMessageInfoText, "Hello world!"),
+											},
+										}),
+									},
+								},
+							},
+							result: &wire.SNACMessage{
+								Body: wire.SNACError{},
+							},
+						},
+					},
+				},
+			},
+			wantMsg: cmdInternalSvcErr,
+		},
 		{
 			name:     "chat room ID with invalid format",
 			givenCmd: []byte(`toc_chat_send zero "Hello world!"`),