diff --git a/app/app.go b/app/app.go index 45238b2..e5fc379 100644 --- a/app/app.go +++ b/app/app.go @@ -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) } diff --git a/app/cli/main.go b/app/cli/main.go index 6852011..8fe41e8 100644 --- a/app/cli/main.go +++ b/app/cli/main.go @@ -87,16 +87,15 @@ 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 { - if group.Owner == "self" { - s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer}) - } - } - return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true) } + s = []prompt.Suggest{} + groups := app.Peer.Profile.Groups + for _, group := range groups { + if group.Owner == "self" { + s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer}) + } + } + return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true) } if strings.HasPrefix(w, "accept-invite") { @@ -362,7 +361,7 @@ func main() { if maxLatency < latency { maxLatency = latency } - totalMessages += 1 + totalMessages++ } } diff --git a/server/app/main.go b/server/app/main.go index f84d50a..c1eb3cd 100644 --- a/server/app/main.go +++ b/server/app/main.go @@ -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) } diff --git a/server/fetch/server_fetch_channel.go b/server/fetch/server_fetch_channel.go index 9119343..bcc0228 100644 --- a/server/fetch/server_fetch_channel.go +++ b/server/fetch/server_fetch_channel.go @@ -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 diff --git a/server/listen/server_listen_channel.go b/server/listen/server_listen_channel.go index c6940db..79074f6 100644 --- a/server/listen/server_listen_channel.go +++ b/server/listen/server_listen_channel.go @@ -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, diff --git a/server/send/server_send_channel.go b/server/send/server_send_channel.go index 01459bc..506fe70 100644 --- a/server/send/server_send_channel.go +++ b/server/send/server_send_channel.go @@ -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) } diff --git a/server/server.go b/server/server.go index 212eceb..70ac609 100644 --- a/server/server.go +++ b/server/server.go @@ -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) diff --git a/server/server_instance.go b/server/server_instance.go index 50a8cd1..542034b 100644 --- a/server/server_instance.go +++ b/server/server_instance.go @@ -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) { diff --git a/storage/message_store.go b/storage/message_store.go index a701861..2a91eac 100644 --- a/storage/message_store.go +++ b/storage/message_store.go @@ -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)