From b2f54202a988b61074ac339db586144382a42bbf Mon Sep 17 00:00:00 2001 From: Nikos Filippakis Date: Wed, 5 Aug 2020 12:52:25 +0200 Subject: [PATCH] Add help messages for cryptotest cmds Signed-off-by: Nikos Filippakis --- go.sum | 2 + services/cryptotest/cryptotest.go | 228 +++++++++++++++++++----------- 2 files changed, 144 insertions(+), 86 deletions(-) diff --git a/go.sum b/go.sum index 75005e6..e79dd50 100644 --- a/go.sum +++ b/go.sum @@ -272,5 +272,7 @@ maunium.net/go/mautrix v0.5.5 h1:e0Pql1FdxoNUudx2oXo1gZHMrqIh5MC72cdXEPIrYLA= maunium.net/go/mautrix v0.5.5/go.mod h1:FLbMANzwqlsX2Fgm7SDe+E4I3wSa4UxJRKqS5wGkCwA= maunium.net/go/mautrix v0.6.0 h1:V32l4aygKk2XcH3fi8Yd0pFeSyYZJNRIvr8vdA2GtC8= maunium.net/go/mautrix v0.6.0/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo= +maunium.net/go/mautrix v0.7.0 h1:9Wxs5S4Wl4S99dbBwfLZYAe/sP7VKaFikw9Ocf88kfk= +maunium.net/go/mautrix v0.7.0/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo= maunium.net/go/mauview v0.1.1/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o= maunium.net/go/tcell v0.2.0/go.mod h1:9Apcb3lNNS6C6lCqKT9UFp7BTRzHXfWE+/tgufsAMho= diff --git a/services/cryptotest/cryptotest.go b/services/cryptotest/cryptotest.go index f94ec67..59754b3 100644 --- a/services/cryptotest/cryptotest.go +++ b/services/cryptotest/cryptotest.go @@ -48,19 +48,42 @@ func (s *Service) handleEventMessage(source mautrix.EventSource, evt *mevt.Event } // Commands supported: -// !crypto_response random_string -// Responds with a notice of "some message". -// TODO details here -// TODO each cmd when called incorrectly (wrong # of args) should also show a msg +// !crypto_help Displays a help string +// !crypto_challenge Sets a challenge for a room which clients should reply to with !crypto_response +// !crypto_response Used by the client to repeat the room challenge +// !crypto_new_session Invalidates the bot's current outgoing session +// !sas_verify_me Asks the bot to verify the sender +// !sas_decimal_code Sends the sender's SAS code to the bot for verification +// !request_my_room_key Asks the bot to request a room key from the sender +// !forward_me_room_key Asks the bot to forward a room key to the sender +// This service can be used for testing other clients by writing the commands above in a room where this service is enabled. func (s *Service) Commands(cli types.MatrixClient) []types.Command { botClient := cli.(*clients.BotClient) + helpMsgs := map[string]string{ + "crypto_help": ": Displays the help message", + "crypto_challenge": "[prefix] : The bot sets a random challenge for the room and echoes it. " + + "The client tested should respond with \"!crypto_response challenge\"." + + "Alternatively the prefix that the challenge will be echoed with can be set.", + "crypto_response": " : Should repeat the crypto_challenge's challenge code.", + "crypto_new_session": ": Asks the bot to invalidate its current outgoing group session and create a new one.", + "sas_verify_me": " : Asks the bot to start a decimal SAS verification transaction with the sender's specified device.", + "sas_decimal_code": " : Sends the device's generated decimal SAS code for the bot to verify, " + + "after a \"!sas_verify_me\" command.", + "request_my_room_key": " : Asks the bot to request the room key for the current room " + + "and given sender key and session ID from the sender's given device.", + "forward_me_room_key": " : Asks the bot to send the room key for the current room " + + "and given sender key and session ID to the sender's given device.", + } return []types.Command{ { Path: []string{"crypto_help"}, Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { if s.inRoom(roomID) { - // TODO help msg - return mevt.MessageEventContent{MsgType: mevt.MsgText, Body: "help"}, nil + helpTxt := "Supported crypto test methods:\n\n" + for cmd, helpMsg := range helpMsgs { + helpTxt += fmt.Sprintf("!%v %v\n\n", cmd, helpMsg) + } + return mevt.MessageEventContent{MsgType: mevt.MsgText, Body: helpTxt}, nil } return nil, nil }, @@ -85,7 +108,12 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { Path: []string{"crypto_response"}, Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { if s.inRoom(roomID) { - if len(arguments) > 0 && arguments[0] == expectedString[roomID] { + if len(arguments) != 1 { + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: "!crypto_response " + helpMsgs["crypto_response"], + }, nil + } else if arguments[0] == expectedString[roomID] { return mevt.MessageEventContent{ MsgType: mevt.MsgText, Body: fmt.Sprintf("Correct response received from %v", userID.String()), @@ -120,20 +148,27 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { { Path: []string{"sas_verify_me"}, Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { - if s.inRoom(roomID) && len(arguments) > 0 { - deviceID := id.DeviceID(arguments[0]) - transaction, err := botClient.StartSASVerification(userID, deviceID) - if err != nil { - log.WithFields(log.Fields{"user_id": userID, "device_id": deviceID}).WithError(err).Error("Error starting SAS verification") + if s.inRoom(roomID) { + if len(arguments) != 1 { return mevt.MessageEventContent{ MsgType: mevt.MsgText, - Body: fmt.Sprintf("Error starting SAS verification: %v", err), + Body: "sas_verify_me " + helpMsgs["sas_verify_me"], + }, nil + } else { + deviceID := id.DeviceID(arguments[0]) + transaction, err := botClient.StartSASVerification(userID, deviceID) + if err != nil { + log.WithFields(log.Fields{"user_id": userID, "device_id": deviceID}).WithError(err).Error("Error starting SAS verification") + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Error starting SAS verification: %v", err), + }, nil + } + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Started SAS verification with user %v device %v: transaction %v", userID, deviceID, transaction), }, nil } - return mevt.MessageEventContent{ - MsgType: mevt.MsgText, - Body: fmt.Sprintf("Started SAS verification with user %v device %v: transaction %v", userID, deviceID, transaction), - }, nil } return nil, nil }, @@ -141,25 +176,32 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { { Path: []string{"sas_decimal_code"}, Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { - if s.inRoom(roomID) && len(arguments) == 4 { - deviceID := id.DeviceID(arguments[0]) - var decimalSAS crypto.DecimalSASData - for i := 0; i < 3; i++ { - sasCode, err := strconv.Atoi(arguments[i+1]) - if err != nil { - log.WithFields(log.Fields{"user_id": userID, "device_id": deviceID}).WithError(err).Error("Error reading SAS code") - return mevt.MessageEventContent{ - MsgType: mevt.MsgText, - Body: fmt.Sprintf("Error reading SAS cdoe: %v", err), - }, nil + if s.inRoom(roomID) { + if len(arguments) != 4 { + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: "sas_decimal_code " + helpMsgs["sas_decimal_code"], + }, nil + } else { + deviceID := id.DeviceID(arguments[0]) + var decimalSAS crypto.DecimalSASData + for i := 0; i < 3; i++ { + sasCode, err := strconv.Atoi(arguments[i+1]) + if err != nil { + log.WithFields(log.Fields{"user_id": userID, "device_id": deviceID}).WithError(err).Error("Error reading SAS code") + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Error reading SAS code: %v", err), + }, nil + } + decimalSAS[i] = uint(sasCode) } - decimalSAS[i] = uint(sasCode) + botClient.SubmitDecimalSAS(userID, deviceID, decimalSAS) + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Read SAS code from user %v device %v: %v", userID, deviceID, decimalSAS), + }, nil } - botClient.SubmitDecimalSAS(userID, deviceID, decimalSAS) - return mevt.MessageEventContent{ - MsgType: mevt.MsgText, - Body: fmt.Sprintf("Read SAS code from user %v device %v: %v", userID, deviceID, decimalSAS), - }, nil } return nil, nil }, @@ -167,46 +209,53 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { { Path: []string{"request_my_room_key"}, Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { - if s.inRoom(roomID) && len(arguments) == 3 { - deviceID := id.DeviceID(arguments[0]) - senderKey := id.SenderKey(arguments[1]) - sessionID := id.SessionID(arguments[2]) - receivedChan, err := botClient.SendRoomKeyRequest(userID, deviceID, roomID, senderKey, sessionID, time.Minute) - if err != nil { - log.WithFields(log.Fields{ - "user_id": userID, - "device_id": deviceID, - "sender_key": senderKey, - "session_id": sessionID, - }).WithError(err).Error("Error requesting room key") + if s.inRoom(roomID) { + if len(arguments) != 3 { return mevt.MessageEventContent{ MsgType: mevt.MsgText, - Body: fmt.Sprintf("Error requesting room key for session %v: %v", sessionID, err), + Body: "request_my_room_key " + helpMsgs["request_my_room_key"], }, nil - } - go func() { - var result string - received := <-receivedChan - if received { - result = "Key received successfully!" - } else { - result = "Key was not received in the time limit" - } - content := mevt.MessageEventContent{ - MsgType: mevt.MsgText, - Body: fmt.Sprintf("Room key request for session %v result: %v", sessionID, result), - } - if _, err := botClient.SendMessageEvent(roomID, mevt.EventMessage, content); err != nil { + } else { + deviceID := id.DeviceID(arguments[0]) + senderKey := id.SenderKey(arguments[1]) + sessionID := id.SessionID(arguments[2]) + receivedChan, err := botClient.SendRoomKeyRequest(userID, deviceID, roomID, senderKey, sessionID, time.Minute) + if err != nil { log.WithFields(log.Fields{ - "room_id": roomID, - "content": content, - }).WithError(err).Error("Failed to send room key request result to room") + "user_id": userID, + "device_id": deviceID, + "sender_key": senderKey, + "session_id": sessionID, + }).WithError(err).Error("Error requesting room key") + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Error requesting room key for session %v: %v", sessionID, err), + }, nil } - }() - return mevt.MessageEventContent{ - MsgType: mevt.MsgText, - Body: fmt.Sprintf("Sent room key request for session %v to device %v", sessionID, deviceID), - }, nil + go func() { + var result string + received := <-receivedChan + if received { + result = "Key received successfully!" + } else { + result = "Key was not received in the time limit" + } + content := mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Room key request for session %v result: %v", sessionID, result), + } + if _, err := botClient.SendMessageEvent(roomID, mevt.EventMessage, content); err != nil { + log.WithFields(log.Fields{ + "room_id": roomID, + "content": content, + }).WithError(err).Error("Failed to send room key request result to room") + } + }() + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Sent room key request for session %v to device %v", sessionID, deviceID), + }, nil + } } return nil, nil }, @@ -214,27 +263,34 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { { Path: []string{"forward_me_room_key"}, Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { - if s.inRoom(roomID) && len(arguments) == 3 { - deviceID := id.DeviceID(arguments[0]) - senderKey := id.SenderKey(arguments[1]) - sessionID := id.SessionID(arguments[2]) - err := botClient.ForwardRoomKeyToDevice(userID, deviceID, roomID, senderKey, sessionID) - if err != nil { - log.WithFields(log.Fields{ - "user_id": userID, - "device_id": deviceID, - "sender_key": senderKey, - "session_id": sessionID, - }).WithError(err).Error("Error forwarding room key") + if s.inRoom(roomID) { + if len(arguments) != 3 { return mevt.MessageEventContent{ MsgType: mevt.MsgText, - Body: fmt.Sprintf("Error forwarding room key for session %v: %v", sessionID, err), + Body: "forward_me_room_key " + helpMsgs["forward_me_room_key"], + }, nil + } else { + deviceID := id.DeviceID(arguments[0]) + senderKey := id.SenderKey(arguments[1]) + sessionID := id.SessionID(arguments[2]) + err := botClient.ForwardRoomKeyToDevice(userID, deviceID, roomID, senderKey, sessionID) + if err != nil { + log.WithFields(log.Fields{ + "user_id": userID, + "device_id": deviceID, + "sender_key": senderKey, + "session_id": sessionID, + }).WithError(err).Error("Error forwarding room key") + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Error forwarding room key for session %v: %v", sessionID, err), + }, nil + } + return mevt.MessageEventContent{ + MsgType: mevt.MsgText, + Body: fmt.Sprintf("Forwarded room key for session %v to device %v", sessionID, deviceID), }, nil } - return mevt.MessageEventContent{ - MsgType: mevt.MsgText, - Body: fmt.Sprintf("Forwarded room key for session %v to device %v", sessionID, deviceID), - }, nil } return nil, nil },