2018-03-09 20:44:13 +00:00
|
|
|
package fetch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"git.mascherari.press/cwtch/protocol"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
|
|
|
"github.com/s-rah/go-ricochet/wire/control"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CwtchPeerFetchChannel is the peer implementation of the im.cwtch.server.fetch
|
|
|
|
// channel.
|
|
|
|
type CwtchPeerFetchChannel struct {
|
|
|
|
channel *channels.Channel
|
|
|
|
Handler CwtchPeerFetchChannelHandler
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// CwtchPeerFetchChannelHandler should be implemented by peers to receive new messages.
|
2018-03-09 20:44:13 +00:00
|
|
|
type CwtchPeerFetchChannelHandler interface {
|
|
|
|
HandleGroupMessage(*protocol.GroupMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type string for this channel, e.g. "im.ricochet.server.fetch)
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) Type() string {
|
|
|
|
return "im.cwtch.server.fetch"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closed is called when the channel is closed for any reason.
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) Closed(err error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnlyClientCanOpen - for Cwtch server channels only client can open
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) OnlyClientCanOpen() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Singleton - for Cwtch channels there can only be one instance per direction
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) Singleton() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bidirectional - for Cwtch channels are not bidrectional
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) Bidirectional() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequiresAuthentication - Cwtch server channels require no auth.
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) RequiresAuthentication() string {
|
|
|
|
return "none"
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenInbound - cwtch server peer implementations shouldnever respond to inbound requests
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
|
|
|
|
return nil, errors.New("client does not receive inbound listen channels")
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenOutbound sets up a new cwtch fetch channel
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) OpenOutbound(channel *channels.Channel) ([]byte, error) {
|
|
|
|
cpfc.channel = channel
|
|
|
|
messageBuilder := new(utils.MessageBuilder)
|
|
|
|
return messageBuilder.OpenChannel(channel.ID, cpfc.Type()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenOutboundResult confirms a previous open channel request
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) OpenOutboundResult(err error, crm *Protocol_Data_Control.ChannelResult) {
|
|
|
|
if err == nil {
|
|
|
|
if crm.GetOpened() {
|
|
|
|
cpfc.channel.Pending = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Packet is called for each raw packet received on this channel.
|
|
|
|
func (cpfc *CwtchPeerFetchChannel) Packet(data []byte) {
|
|
|
|
csp := &protocol.CwtchServerPacket{}
|
|
|
|
err := proto.Unmarshal(data, csp)
|
|
|
|
if err == nil {
|
|
|
|
if csp.GetGroupMessages() != nil {
|
|
|
|
gms := csp.GetGroupMessages()
|
|
|
|
for _, gm := range gms {
|
|
|
|
cpfc.Handler.HandleGroupMessage(gm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// After a fetch we close the channel.
|
|
|
|
cpfc.channel.CloseChannel()
|
|
|
|
}
|