|
|
@ -44,6 +44,8 @@ type Client struct {
|
|
|
|
line []rune
|
|
|
|
line []rune
|
|
|
|
prev *terminal.State
|
|
|
|
prev *terminal.State
|
|
|
|
keyStore map[string]rsa.PublicKey
|
|
|
|
keyStore map[string]rsa.PublicKey
|
|
|
|
|
|
|
|
requestCount int
|
|
|
|
|
|
|
|
outstanding map[int]chan Envelope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// establishes a connection to the server
|
|
|
|
// establishes a connection to the server
|
|
|
@ -83,27 +85,14 @@ func (c *Client) handleMessages() {
|
|
|
|
|
|
|
|
|
|
|
|
// handle a message received from the server
|
|
|
|
// handle a message received from the server
|
|
|
|
func (c *Client) handleMessage(m Envelope) error {
|
|
|
|
func (c *Client) handleMessage(m Envelope) error {
|
|
|
|
switch m.Kind {
|
|
|
|
res, ok := c.outstanding[m.Id]
|
|
|
|
case "meta":
|
|
|
|
if !ok {
|
|
|
|
return c.handleMeta(m.Body)
|
|
|
|
c.info("%v", m)
|
|
|
|
case "note":
|
|
|
|
c.err("received message corresponding to no known request id: %d", m.Id)
|
|
|
|
return c.handleNote(m.Body)
|
|
|
|
return fmt.Errorf("no such id: %d", m.Id)
|
|
|
|
case "list-notes":
|
|
|
|
}
|
|
|
|
return c.handleListNotes(m.Body)
|
|
|
|
res <- m
|
|
|
|
case "key-response":
|
|
|
|
close(res)
|
|
|
|
return c.handleKeyResponse(m.Body)
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return fmt.Errorf("received message of unsupported type: %v", m.Kind)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// handles a meta message; that is, a message that is shown to the user
|
|
|
|
|
|
|
|
func (c *Client) handleMeta(body json.RawMessage) error {
|
|
|
|
|
|
|
|
var meta Meta
|
|
|
|
|
|
|
|
if err := json.Unmarshal(body, &meta); err != nil {
|
|
|
|
|
|
|
|
return fmt.Errorf("unable to unmarshal meta message: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
c.info("message from server: %v", meta)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -173,23 +162,28 @@ func (c *Client) handleListNotes(raw json.RawMessage) error {
|
|
|
|
func (c *Client) handshake() error {
|
|
|
|
func (c *Client) handshake() error {
|
|
|
|
r := &Auth{Nick: c.nick, Key: c.key.PublicKey}
|
|
|
|
r := &Auth{Nick: c.nick, Key: c.key.PublicKey}
|
|
|
|
c.info("authenticating as %s", c.nick)
|
|
|
|
c.info("authenticating as %s", c.nick)
|
|
|
|
return c.sendRequest(r)
|
|
|
|
_, err := c.sendRequest(r)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Client) sendRequest(r request) error {
|
|
|
|
func (c *Client) sendRequest(r request) (chan Envelope, error) {
|
|
|
|
e, err := wrapRequest(r)
|
|
|
|
e, err := wrapRequest(c.requestCount, r)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(e)
|
|
|
|
b, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res := make(chan Envelope, 1)
|
|
|
|
|
|
|
|
c.outstanding[c.requestCount] = res
|
|
|
|
|
|
|
|
c.requestCount++
|
|
|
|
c.info("sending json request: %s", b)
|
|
|
|
c.info("sending json request: %s", b)
|
|
|
|
if _, err := c.conn.Write(b); err != nil {
|
|
|
|
if _, err := c.conn.Write(b); err != nil {
|
|
|
|
return err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Client) info(template string, args ...interface{}) {
|
|
|
|
func (c *Client) info(template string, args ...interface{}) {
|
|
|
@ -315,7 +309,7 @@ func (c *Client) createNote(args []string) {
|
|
|
|
c.err("%v", err)
|
|
|
|
c.err("%v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.sendRequest(note); err != nil {
|
|
|
|
if _, err := c.sendRequest(note); err != nil {
|
|
|
|
c.err("error sending note: %v", err)
|
|
|
|
c.err("error sending note: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -330,15 +324,23 @@ func (c *Client) getNote(args []string) {
|
|
|
|
c.err("that doesn't look like an int: %v", err)
|
|
|
|
c.err("that doesn't look like an int: %v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := c.sendRequest(GetNoteRequest(id)); err != nil {
|
|
|
|
res, err := c.sendRequest(GetNoteRequest(id))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.err("couldn't request note: %v", err)
|
|
|
|
c.err("couldn't request note: %v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e := <-res
|
|
|
|
|
|
|
|
c.handleNote(e.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Client) listNotes(args []string) {
|
|
|
|
func (c *Client) listNotes(args []string) {
|
|
|
|
r := &ListNotes{N: 10}
|
|
|
|
r := &ListNotes{N: 10}
|
|
|
|
c.sendRequest(r)
|
|
|
|
res, err := c.sendRequest(r)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
c.err("%v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
e := <-res
|
|
|
|
|
|
|
|
c.handleListNotes(e.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Client) encryptNote(title string, message []rune) (*EncryptedNote, error) {
|
|
|
|
func (c *Client) encryptNote(title string, message []rune) (*EncryptedNote, error) {
|
|
|
@ -390,10 +392,13 @@ func (c *Client) getKey(args []string) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req := KeyRequest(args[0])
|
|
|
|
req := KeyRequest(args[0])
|
|
|
|
if err := c.sendRequest(req); err != nil {
|
|
|
|
res, err := c.sendRequest(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.err("couldn't send key request: %v", err)
|
|
|
|
c.err("couldn't send key request: %v", err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e := <-res
|
|
|
|
|
|
|
|
c.handleKeyResponse(e.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *Client) saveKey(nick string, key rsa.PublicKey) {
|
|
|
|
func (c *Client) saveKey(nick string, key rsa.PublicKey) {
|
|
|
@ -577,6 +582,8 @@ func connect() {
|
|
|
|
nick: options.nick,
|
|
|
|
nick: options.nick,
|
|
|
|
done: make(chan interface{}),
|
|
|
|
done: make(chan interface{}),
|
|
|
|
line: make([]rune, 0, 32),
|
|
|
|
line: make([]rune, 0, 32),
|
|
|
|
|
|
|
|
keyStore: make(map[string]rsa.PublicKey, 8),
|
|
|
|
|
|
|
|
outstanding: make(map[int]chan Envelope),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client.run()
|
|
|
|
client.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|