|
|
|
@ -10,6 +10,7 @@ import (
|
|
|
|
|
"io"
|
|
|
|
|
"net"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"unicode"
|
|
|
|
@ -82,6 +83,8 @@ func (c *Client) handleMessage(m Envelope) error {
|
|
|
|
|
switch m.Kind {
|
|
|
|
|
case "meta":
|
|
|
|
|
return c.handleMeta(m.Body)
|
|
|
|
|
case "note":
|
|
|
|
|
return c.handleNote(m.Body)
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("received message of unsupported type: %v", m.Kind)
|
|
|
|
|
}
|
|
|
|
@ -97,6 +100,24 @@ func (c *Client) handleMeta(body json.RawMessage) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) handleNote(body json.RawMessage) error {
|
|
|
|
|
var ctxt []byte
|
|
|
|
|
if err := json.Unmarshal(body, &ctxt); err != nil {
|
|
|
|
|
return fmt.Errorf("unable to read note response: %v", err)
|
|
|
|
|
}
|
|
|
|
|
ptxt, err := rsa.DecryptPKCS1v15(rand.Reader, c.key, ctxt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("unable to decrypt note response: %v", err)
|
|
|
|
|
}
|
|
|
|
|
var note NoteData
|
|
|
|
|
if err := json.Unmarshal(ptxt, ¬e); err != nil {
|
|
|
|
|
return fmt.Errorf("unable to unmarshal note response: %v", err)
|
|
|
|
|
}
|
|
|
|
|
c.info("title: %s", note.Title)
|
|
|
|
|
c.info("body: %s", string(note.Body))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) handshake() error {
|
|
|
|
|
r := &Auth{Nick: c.nick, Key: c.key.PublicKey}
|
|
|
|
|
c.info("authenticating as %s", c.nick)
|
|
|
|
@ -198,6 +219,8 @@ func (c *Client) exec(line string) {
|
|
|
|
|
switch parts[0] {
|
|
|
|
|
case "notes/create":
|
|
|
|
|
c.createNote(parts[1:])
|
|
|
|
|
case "notes/get":
|
|
|
|
|
c.getNote(parts[1:])
|
|
|
|
|
default:
|
|
|
|
|
c.err("unrecognized client command: %s", parts[0])
|
|
|
|
|
}
|
|
|
|
@ -225,6 +248,22 @@ func (c *Client) createNote(args []string) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) getNote(args []string) {
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
|
c.err("ok notes/get takes exactly 1 argument")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
id, err := strconv.Atoi(args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.err("that doesn't look like an int: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := c.sendRequest(GetNoteRequest(id)); err != nil {
|
|
|
|
|
c.err("couldn't request note: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) encryptNote(title string, note []rune) (NoteRequest, error) {
|
|
|
|
|
obj := &NoteData{
|
|
|
|
|
Title: title,
|
|
|
|
|