Add GetMessage

parent 21ea946c
package client
import (
"errors"
"time"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/mailproxy"
"github.com/katzenpost/mailproxy/config"
......@@ -64,3 +67,32 @@ func (c Client) Send(recipient, msg string) error {
c.proxy.SetRecipient(recipient, identityKey.PublicKey())
return c.proxy.SendMessage(c.address, recipient, []byte(msg))
}
type Message struct {
Sender string
Payload string
}
func (c Client) GetMessage(timeout int64) (Message, error) {
if timeout == 0 {
ev := <-c.eventSink
return c.handleEvent(ev)
}
select {
case ev := <-c.eventSink:
return c.handleEvent(ev)
case <-time.After(time.Second * time.Duration(timeout)):
return Message{}, errors.New("Timeout")
}
}
func (c Client) handleEvent(ev event.Event) (Message, error) {
switch ev.(type) {
case *event.MessageReceivedEvent:
msg, err := c.proxy.ReceivePop(c.address)
return Message{msg.SenderID, string(msg.Payload)}, err
default:
return Message{}, errors.New("Another event arrived")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment