|
|
@ -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": "<challenge> : 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": "<device_id> : Asks the bot to start a decimal SAS verification transaction with the sender's specified device.", |
|
|
|
"sas_decimal_code": "<device_id> <sas1> <sas2> <sas3> : Sends the device's generated decimal SAS code for the bot to verify, " + |
|
|
|
"after a \"!sas_verify_me\" command.", |
|
|
|
"request_my_room_key": "<device_id> <sender_key> <session_id> : 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": "<device_id> <sender_key> <session_id> : 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,7 +148,13 @@ 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 { |
|
|
|
if s.inRoom(roomID) { |
|
|
|
if len(arguments) != 1 { |
|
|
|
return mevt.MessageEventContent{ |
|
|
|
MsgType: mevt.MsgText, |
|
|
|
Body: "sas_verify_me " + helpMsgs["sas_verify_me"], |
|
|
|
}, nil |
|
|
|
} else { |
|
|
|
deviceID := id.DeviceID(arguments[0]) |
|
|
|
transaction, err := botClient.StartSASVerification(userID, deviceID) |
|
|
|
if err != nil { |
|
|
@ -135,13 +169,20 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { |
|
|
|
Body: fmt.Sprintf("Started SAS verification with user %v device %v: transaction %v", userID, deviceID, transaction), |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
Path: []string{"sas_decimal_code"}, |
|
|
|
Command: func(roomID id.RoomID, userID id.UserID, arguments []string) (interface{}, error) { |
|
|
|
if s.inRoom(roomID) && len(arguments) == 4 { |
|
|
|
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++ { |
|
|
@ -150,7 +191,7 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { |
|
|
|
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), |
|
|
|
Body: fmt.Sprintf("Error reading SAS code: %v", err), |
|
|
|
}, nil |
|
|
|
} |
|
|
|
decimalSAS[i] = uint(sasCode) |
|
|
@ -161,13 +202,20 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { |
|
|
|
Body: fmt.Sprintf("Read SAS code from user %v device %v: %v", userID, deviceID, decimalSAS), |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
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 { |
|
|
|
if s.inRoom(roomID) { |
|
|
|
if len(arguments) != 3 { |
|
|
|
return mevt.MessageEventContent{ |
|
|
|
MsgType: mevt.MsgText, |
|
|
|
Body: "request_my_room_key " + helpMsgs["request_my_room_key"], |
|
|
|
}, nil |
|
|
|
} else { |
|
|
|
deviceID := id.DeviceID(arguments[0]) |
|
|
|
senderKey := id.SenderKey(arguments[1]) |
|
|
|
sessionID := id.SessionID(arguments[2]) |
|
|
@ -208,13 +256,20 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { |
|
|
|
Body: fmt.Sprintf("Sent room key request for session %v to device %v", sessionID, deviceID), |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
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 { |
|
|
|
if s.inRoom(roomID) { |
|
|
|
if len(arguments) != 3 { |
|
|
|
return mevt.MessageEventContent{ |
|
|
|
MsgType: mevt.MsgText, |
|
|
|
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]) |
|
|
@ -236,6 +291,7 @@ func (s *Service) Commands(cli types.MatrixClient) []types.Command { |
|
|
|
Body: fmt.Sprintf("Forwarded room key for session %v to device %v", sessionID, deviceID), |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, nil |
|
|
|
}, |
|
|
|
}, |
|
|
|