2016-02-28 02:52:20 +00:00
|
|
|
package goricochet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/s-rah/go-ricochet/auth"
|
|
|
|
"github.com/s-rah/go-ricochet/chat"
|
|
|
|
"github.com/s-rah/go-ricochet/contact"
|
|
|
|
"github.com/s-rah/go-ricochet/control"
|
2016-06-27 01:56:23 +00:00
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
2016-02-28 02:52:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MessageBuilder allows a client to construct specific data packets for the
|
|
|
|
// ricochet protocol.
|
|
|
|
type MessageBuilder struct {
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// OpenChannel contructs a message which will request to open a channel for
|
2016-02-28 02:52:20 +00:00
|
|
|
// chat on the given channelID.
|
2016-06-27 01:56:23 +00:00
|
|
|
func (mb *MessageBuilder) OpenChannel(channelID int32, channelType string) ([]byte, error) {
|
2016-02-28 02:52:20 +00:00
|
|
|
oc := &Protocol_Data_Control.OpenChannel{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
2016-06-27 01:56:23 +00:00
|
|
|
ChannelType: proto.String(channelType),
|
2016-02-28 02:52:20 +00:00
|
|
|
}
|
|
|
|
pc := &Protocol_Data_Control.Packet{
|
|
|
|
OpenChannel: oc,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
2016-02-29 00:18:25 +00:00
|
|
|
// AckOpenChannel constructs a message to acknowledge a previous open channel operation.
|
2016-06-27 01:56:23 +00:00
|
|
|
func (mb *MessageBuilder) AckOpenChannel(channelID int32) ([]byte, error) {
|
2016-02-29 00:18:25 +00:00
|
|
|
cr := &Protocol_Data_Control.ChannelResult{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
2016-06-27 01:56:23 +00:00
|
|
|
Opened: proto.Bool(true),
|
2016-02-29 00:18:25 +00:00
|
|
|
}
|
|
|
|
pc := &Protocol_Data_Control.Packet{
|
|
|
|
ChannelResult: cr,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// RejectOpenChannel constructs a channel result message, stating the channel failed to open and a reason
|
|
|
|
func (mb *MessageBuilder) RejectOpenChannel(channelID int32, error string) ([]byte, error) {
|
|
|
|
|
|
|
|
errorNum := Protocol_Data_Control.ChannelResult_CommonError_value[error]
|
|
|
|
commonError := Protocol_Data_Control.ChannelResult_CommonError(errorNum)
|
|
|
|
|
|
|
|
cr := &Protocol_Data_Control.ChannelResult{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
|
|
|
Opened: proto.Bool(false),
|
|
|
|
CommonError: &commonError,
|
|
|
|
}
|
|
|
|
pc := &Protocol_Data_Control.Packet{
|
|
|
|
ChannelResult: cr,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfirmAuthChannel constructs a message to acknowledge a previous open channel operation.
|
|
|
|
func (mb *MessageBuilder) ConfirmAuthChannel(channelID int32, serverCookie [16]byte) ([]byte, error) {
|
|
|
|
cr := &Protocol_Data_Control.ChannelResult{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
|
|
|
Opened: proto.Bool(true),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := proto.SetExtension(cr, Protocol_Data_AuthHiddenService.E_ServerCookie, serverCookie[:])
|
|
|
|
utils.CheckError(err)
|
|
|
|
|
|
|
|
pc := &Protocol_Data_Control.Packet{
|
|
|
|
ChannelResult: cr,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
2016-02-28 02:52:20 +00:00
|
|
|
// OpenContactRequestChannel contructs a message which will reuqest to open a channel for
|
|
|
|
// a contact request on the given channelID, with the given nick and message.
|
|
|
|
func (mb *MessageBuilder) OpenContactRequestChannel(channelID int32, nick string, message string) ([]byte, error) {
|
|
|
|
// Construct a Contact Request Channel
|
|
|
|
oc := &Protocol_Data_Control.OpenChannel{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
|
|
|
ChannelType: proto.String("im.ricochet.contact.request"),
|
|
|
|
}
|
|
|
|
|
|
|
|
contactRequest := &Protocol_Data_ContactRequest.ContactRequest{
|
|
|
|
Nickname: proto.String(nick),
|
|
|
|
MessageText: proto.String(message),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := proto.SetExtension(oc, Protocol_Data_ContactRequest.E_ContactRequest, contactRequest)
|
2016-06-27 01:56:23 +00:00
|
|
|
utils.CheckError(err)
|
|
|
|
|
|
|
|
pc := &Protocol_Data_Control.Packet{
|
|
|
|
OpenChannel: oc,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
2016-02-28 02:52:20 +00:00
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// ReplyToContactRequestOnResponse constructs a message to acknowledge contact request
|
|
|
|
func (mb *MessageBuilder) ReplyToContactRequestOnResponse(channelID int32, status string) ([]byte, error) {
|
|
|
|
cr := &Protocol_Data_Control.ChannelResult{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
|
|
|
Opened: proto.Bool(true),
|
2016-02-28 02:52:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
statusNum := Protocol_Data_ContactRequest.Response_Status_value[status]
|
|
|
|
responseStatus := Protocol_Data_ContactRequest.Response_Status(statusNum)
|
|
|
|
contactRequest := &Protocol_Data_ContactRequest.Response{
|
|
|
|
Status: &responseStatus,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := proto.SetExtension(cr, Protocol_Data_ContactRequest.E_Response, contactRequest)
|
|
|
|
utils.CheckError(err)
|
|
|
|
|
2016-02-28 02:52:20 +00:00
|
|
|
pc := &Protocol_Data_Control.Packet{
|
2016-06-27 01:56:23 +00:00
|
|
|
ChannelResult: cr,
|
2016-02-28 02:52:20 +00:00
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// ReplyToContactRequest constructs a message to acknowledge a contact request
|
|
|
|
func (mb *MessageBuilder) ReplyToContactRequest(channelID int32, status string) ([]byte, error) {
|
|
|
|
statusNum := Protocol_Data_ContactRequest.Response_Status_value[status]
|
|
|
|
responseStatus := Protocol_Data_ContactRequest.Response_Status(statusNum)
|
|
|
|
contactRequest := &Protocol_Data_ContactRequest.Response{
|
|
|
|
Status: &responseStatus,
|
|
|
|
}
|
|
|
|
return proto.Marshal(contactRequest)
|
|
|
|
}
|
|
|
|
|
2016-02-28 02:52:20 +00:00
|
|
|
// OpenAuthenticationChannel constructs a message which will reuqest to open a channel for
|
|
|
|
// authentication on the given channelID, with the given cookie
|
|
|
|
func (mb *MessageBuilder) OpenAuthenticationChannel(channelID int32, clientCookie [16]byte) ([]byte, error) {
|
|
|
|
oc := &Protocol_Data_Control.OpenChannel{
|
|
|
|
ChannelIdentifier: proto.Int32(channelID),
|
|
|
|
ChannelType: proto.String("im.ricochet.auth.hidden-service"),
|
|
|
|
}
|
|
|
|
err := proto.SetExtension(oc, Protocol_Data_AuthHiddenService.E_ClientCookie, clientCookie[:])
|
2016-06-27 01:56:23 +00:00
|
|
|
utils.CheckError(err)
|
|
|
|
|
2016-02-28 02:52:20 +00:00
|
|
|
pc := &Protocol_Data_Control.Packet{
|
|
|
|
OpenChannel: oc,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:56:23 +00:00
|
|
|
// Proof constructs a proof message with the given public key and signature.
|
|
|
|
func (mb *MessageBuilder) Proof(publicKeyBytes []byte, signatureBytes []byte) ([]byte, error) {
|
|
|
|
proof := &Protocol_Data_AuthHiddenService.Proof{
|
|
|
|
PublicKey: publicKeyBytes,
|
|
|
|
Signature: signatureBytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
ahsPacket := &Protocol_Data_AuthHiddenService.Packet{
|
|
|
|
Proof: proof,
|
|
|
|
Result: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Marshal(ahsPacket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthResult constructs a response to a Proof
|
|
|
|
func (mb *MessageBuilder) AuthResult(accepted bool, isKnownContact bool) ([]byte, error) {
|
|
|
|
// Construct a Result Message
|
|
|
|
result := &Protocol_Data_AuthHiddenService.Result{
|
|
|
|
Accepted: proto.Bool(accepted),
|
|
|
|
IsKnownContact: proto.Bool(isKnownContact),
|
|
|
|
}
|
|
|
|
|
|
|
|
ahsPacket := &Protocol_Data_AuthHiddenService.Packet{
|
|
|
|
Proof: nil,
|
|
|
|
Result: result,
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Marshal(ahsPacket)
|
|
|
|
}
|
|
|
|
|
2016-02-28 02:52:20 +00:00
|
|
|
// ChatMessage constructs a chat message with the given content.
|
2016-06-27 01:56:23 +00:00
|
|
|
func (mb *MessageBuilder) ChatMessage(message string, messageID int32) ([]byte, error) {
|
2016-02-28 02:52:20 +00:00
|
|
|
cm := &Protocol_Data_Chat.ChatMessage{
|
2016-06-27 01:56:23 +00:00
|
|
|
MessageId: proto.Uint32(uint32(messageID)),
|
2016-02-28 02:52:20 +00:00
|
|
|
MessageText: proto.String(message),
|
|
|
|
}
|
|
|
|
chatPacket := &Protocol_Data_Chat.Packet{
|
|
|
|
ChatMessage: cm,
|
|
|
|
}
|
|
|
|
return proto.Marshal(chatPacket)
|
|
|
|
}
|
2016-02-29 00:18:25 +00:00
|
|
|
|
|
|
|
// AckChatMessage constructs a chat message acknowledgement.
|
|
|
|
func (mb *MessageBuilder) AckChatMessage(messageID int32) ([]byte, error) {
|
|
|
|
cr := &Protocol_Data_Chat.ChatAcknowledge{
|
|
|
|
MessageId: proto.Uint32(uint32(messageID)),
|
|
|
|
Accepted: proto.Bool(true),
|
|
|
|
}
|
|
|
|
pc := &Protocol_Data_Chat.Packet{
|
|
|
|
ChatAcknowledge: cr,
|
|
|
|
}
|
|
|
|
return proto.Marshal(pc)
|
|
|
|
}
|