forked from cwtch.im/cwtch
More golint fixing
This commit is contained in:
parent
8349ff1b3c
commit
447b1b30fb
|
@ -6,10 +6,12 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
// Application is a facade over a CwtchPeer that provides some wrapping logic.
|
||||
type Application struct {
|
||||
Peer *peer.CwtchPeer
|
||||
}
|
||||
|
||||
// NewProfile creates a new CwtchPeer with a given name.
|
||||
func (app *Application) NewProfile(name string, filename string) error {
|
||||
profile := peer.NewCwtchPeer(name)
|
||||
app.Peer = profile
|
||||
|
@ -25,6 +27,7 @@ func (app *Application) NewProfile(name string, filename string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// SetProfile loads an existing profile from the given filename.
|
||||
func (app *Application) SetProfile(filename string) error {
|
||||
profile, err := peer.LoadCwtchPeer(filename)
|
||||
app.Peer = profile
|
||||
|
@ -39,6 +42,7 @@ func (app *Application) SetProfile(filename string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// PeerRequest attempts to setup peer relationship with the given onion address.
|
||||
func (app *Application) PeerRequest(onion string) {
|
||||
app.Peer.PeerWithOnion(onion)
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ func completer(d prompt.Document) []prompt.Suggest {
|
|||
s = append(s, prompt.Suggest{Text: contact.Onion, Description: contact.Name})
|
||||
}
|
||||
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
|
||||
} else {
|
||||
}
|
||||
s = []prompt.Suggest{}
|
||||
groups := app.Peer.Profile.Groups
|
||||
for _, group := range groups {
|
||||
|
@ -97,7 +97,6 @@ func completer(d prompt.Document) []prompt.Suggest {
|
|||
}
|
||||
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(w, "accept-invite") {
|
||||
s = []prompt.Suggest{}
|
||||
|
@ -362,7 +361,7 @@ func main() {
|
|||
if maxLatency < latency {
|
||||
maxLatency = latency
|
||||
}
|
||||
totalMessages += 1
|
||||
totalMessages++
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,11 +14,11 @@ func checkAndGenPrivateKey(privateKeyFile string) {
|
|||
if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) {
|
||||
log.Printf("no private key found!")
|
||||
log.Printf("generating new private key...")
|
||||
pk, pk_err := utils.GeneratePrivateKey()
|
||||
if pk_err != nil {
|
||||
pk, err := utils.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
log.Fatalf("error generating new private key: %v\n", err)
|
||||
}
|
||||
err := ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
|
||||
err = ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
|
||||
if err != nil {
|
||||
log.Fatalf("error writing new private key to file %s: %v\n", privateKeyFile, err)
|
||||
}
|
||||
|
|
|
@ -9,17 +9,14 @@ import (
|
|||
"github.com/s-rah/go-ricochet/wire/control"
|
||||
)
|
||||
|
||||
// CwtchChannel implements the ChannelHandler interface for a channel of
|
||||
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
||||
//
|
||||
// CwtchChannel implements protocol-level sanity and state validation, but
|
||||
// does not handle or acknowledge Cwtch messages. The application must provide
|
||||
// a CwtchChannelHandler implementation to handle Cwtch events.
|
||||
// CwtchServerFetchChannel implements the ChannelHandler interface for a channel of
|
||||
// type "im.cwtch.server.fetch" - this implementation only handles server side logic.
|
||||
type CwtchServerFetchChannel struct {
|
||||
Handler CwtchServerFetchHandler
|
||||
channel *channels.Channel
|
||||
}
|
||||
|
||||
// CwtchServerFetchHandler defines the interface for interacting with this Channel
|
||||
type CwtchServerFetchHandler interface {
|
||||
HandleFetchRequest() []*protocol.GroupMessage
|
||||
}
|
||||
|
@ -78,7 +75,7 @@ func (cc *CwtchServerFetchChannel) OpenOutboundResult(err error, crm *Protocol_D
|
|||
// NOTE: Should never be called
|
||||
}
|
||||
|
||||
// SendGroupMessage
|
||||
// SendGroupMessages sends a batch of group messages to the client.
|
||||
func (cc *CwtchServerFetchChannel) SendGroupMessages(gm []*protocol.GroupMessage) {
|
||||
csp := &protocol.CwtchServerPacket{}
|
||||
csp.GroupMessages = gm
|
||||
|
|
|
@ -9,12 +9,8 @@ import (
|
|||
"github.com/s-rah/go-ricochet/wire/control"
|
||||
)
|
||||
|
||||
// CwtchChannel implements the ChannelHandler interface for a channel of
|
||||
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
||||
//
|
||||
// CwtchChannel implements protocol-level sanity and state validation, but
|
||||
// does not handle or acknowledge Cwtch messages. The application must provide
|
||||
// a CwtchChannelHandler implementation to handle Cwtch events.
|
||||
// CwtchServerListenChannel implements the ChannelHandler interface for a channel of
|
||||
// type "im.cwtch.server.listen" - this implementation only handles server side logic.
|
||||
type CwtchServerListenChannel struct {
|
||||
channel *channels.Channel
|
||||
}
|
||||
|
@ -73,7 +69,7 @@ func (cc *CwtchServerListenChannel) OpenOutboundResult(err error, crm *Protocol_
|
|||
// NOTE: Should never be called
|
||||
}
|
||||
|
||||
// SendGroupMessage
|
||||
// SendGroupMessage sends a single group message to the peer
|
||||
func (cc *CwtchServerListenChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
||||
csp := &protocol.CwtchServerPacket{
|
||||
GroupMessage: gm,
|
||||
|
|
|
@ -10,12 +10,8 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
// CwtchChannel implements the ChannelHandler interface for a channel of
|
||||
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
||||
//
|
||||
// CwtchChannel implements protocol-level sanity and state validation, but
|
||||
// does not handle or acknowledge Cwtch messages. The application must provide
|
||||
// a CwtchChannelHandler implementation to handle Cwtch events.
|
||||
// CwtchServerSendChannel implements the ChannelHandler interface for a channel of
|
||||
// type "im.cwtch.server.send - this implementation only handles server-side logic.
|
||||
type CwtchServerSendChannel struct {
|
||||
// Methods of Handler are called for Cwtch events on this channel
|
||||
Handler CwtchServerSendChannelHandler
|
||||
|
@ -23,8 +19,7 @@ type CwtchServerSendChannel struct {
|
|||
spamguard spam.Guard
|
||||
}
|
||||
|
||||
// CwtchChannelHandler is implemented by an application type to receive
|
||||
// events from a CwtchChannel.
|
||||
// CwtchServerSendChannelHandler defines the interface needed to interact with this channel
|
||||
type CwtchServerSendChannelHandler interface {
|
||||
HandleGroupMessage(*protocol.GroupMessage)
|
||||
}
|
||||
|
|
|
@ -11,9 +11,13 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
// Server encapsulates a complete, compliant Cwtch server.
|
||||
type Server struct {
|
||||
}
|
||||
|
||||
// Run s
|
||||
// tarts a server with the given privateKey
|
||||
// TODO: surface errors
|
||||
func (s *Server) Run(privateKeyFile string) {
|
||||
cwtchserver := new(application.RicochetApplication)
|
||||
|
||||
|
|
|
@ -8,23 +8,26 @@ import (
|
|||
"github.com/s-rah/go-ricochet/channels"
|
||||
)
|
||||
|
||||
// Instance encapsulates the Ricochet application.
|
||||
type Instance struct {
|
||||
rai *application.ApplicationInstance
|
||||
ra *application.RicochetApplication
|
||||
msi storage.MessageStoreInterface
|
||||
}
|
||||
|
||||
// Init sets up a Server Instance
|
||||
func (si *Instance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
|
||||
si.rai = rai
|
||||
si.ra = ra
|
||||
si.msi = msi
|
||||
}
|
||||
|
||||
// HandleFetchRequest returns a list of all messages in the servers buffer
|
||||
func (si *Instance) HandleFetchRequest() []*protocol.GroupMessage {
|
||||
return si.msi.FetchMessages()
|
||||
}
|
||||
|
||||
// HandleGroupMessage
|
||||
// HandleGroupMessage takes ina group message and distributes it to all listening peers
|
||||
func (si *Instance) HandleGroupMessage(gm *protocol.GroupMessage) {
|
||||
si.msi.AddMessage(*gm)
|
||||
go si.ra.Broadcast(func(rai *application.ApplicationInstance) {
|
||||
|
|
|
@ -10,22 +10,26 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
// MessageStoreInterface defines an interface to interact with a store of cwtch messages.
|
||||
type MessageStoreInterface interface {
|
||||
AddMessage(protocol.GroupMessage)
|
||||
FetchMessages() []*protocol.GroupMessage
|
||||
}
|
||||
|
||||
// MessageStore is a file-backed implementation of MessageStoreInterface
|
||||
type MessageStore struct {
|
||||
file *os.File
|
||||
lock sync.Mutex
|
||||
messages []*protocol.GroupMessage
|
||||
}
|
||||
|
||||
// Close closes the message store and underlying resources.
|
||||
func (ms *MessageStore) Close() {
|
||||
ms.messages = nil
|
||||
ms.file.Close()
|
||||
}
|
||||
|
||||
// Init sets up a MessageStore backed by filename
|
||||
func (ms *MessageStore) Init(filename string) {
|
||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
|
@ -51,6 +55,7 @@ func (ms *MessageStore) Init(filename string) {
|
|||
|
||||
}
|
||||
|
||||
// FetchMessages returns all messages from the backing file.
|
||||
func (ms *MessageStore) FetchMessages() (messages []*protocol.GroupMessage) {
|
||||
messages = make([]*protocol.GroupMessage, len(ms.messages))
|
||||
ms.lock.Lock()
|
||||
|
@ -59,6 +64,7 @@ func (ms *MessageStore) FetchMessages() (messages []*protocol.GroupMessage) {
|
|||
return
|
||||
}
|
||||
|
||||
// AddMessage adds a GroupMessage to the store
|
||||
func (ms *MessageStore) AddMessage(gm protocol.GroupMessage) {
|
||||
ms.lock.Lock()
|
||||
ms.messages = append(ms.messages, &gm)
|
||||
|
|
Loading…
Reference in New Issue