diff --git a/component/receiver.go b/component/receiver.go index 3456bf4..f567108 100644 --- a/component/receiver.go +++ b/component/receiver.go @@ -12,7 +12,7 @@ func (c *Config) receiver() { log.WithField("type", c.Type).Panicf("connection closed%s", err) return } - p, back := c.receive(packet) + p, back := c.receiving(packet) if p == nil { continue } @@ -24,7 +24,7 @@ func (c *Config) receiver() { } } -func (c *Config) receive(packet xmpp.Packet) (xmpp.Packet, bool) { +func (c *Config) receiving(packet xmpp.Packet) (xmpp.Packet, bool) { logger := log.WithField("type", c.Type) switch p := packet.(type) { diff --git a/component/receiver_test.go b/component/receiver_test.go index 3fcd236..5c83681 100644 --- a/component/receiver_test.go +++ b/component/receiver_test.go @@ -13,20 +13,20 @@ func TestReceive(t *testing.T) { c := Config{Host: "example.org", Type: "monkeyservice"} // ignoring packet - p, back := c.receive(xmpp.Handshake{}) + p, _ := c.receiving(xmpp.Handshake{}) assert.Nil(p) // receive presence - p, back = c.receive(xmpp.Presence{}) + p, _ = c.receiving(xmpp.Presence{}) assert.Nil(p) // message - p, back = c.receive(xmpp.Message{}) + p, back := c.receiving(xmpp.Message{}) assert.False(back) assert.NotNil(p) // unsupported iq - p, back = c.receive(xmpp.IQ{Payload: []xmpp.IQPayload{ + p, back = c.receiving(xmpp.IQ{Payload: []xmpp.IQPayload{ &xmpp.Err{}, }}) assert.True(back) @@ -36,7 +36,7 @@ func TestReceive(t *testing.T) { assert.Equal("feature-not-implemented", iq.Error.Reason) // iq disco info - p, back = c.receive(xmpp.IQ{ + p, back = c.receiving(xmpp.IQ{ PacketAttrs: xmpp.PacketAttrs{Type: "get"}, Payload: []xmpp.IQPayload{ &xmpp.DiscoInfo{}, @@ -50,7 +50,7 @@ func TestReceive(t *testing.T) { assert.Equal("monkeyservice", dinfo.Identity.Name) // iq disco items - p, back = c.receive(xmpp.IQ{ + p, back = c.receiving(xmpp.IQ{ PacketAttrs: xmpp.PacketAttrs{Type: "get"}, Payload: []xmpp.IQPayload{ &xmpp.DiscoItems{}, diff --git a/component/send.go b/component/send.go index f3108d9..21a5179 100644 --- a/component/send.go +++ b/component/send.go @@ -7,13 +7,13 @@ import ( func (c *Config) sender(packets chan xmpp.Packet) { for packet := range packets { - if p := c.send(packet); p != nil { + if p := c.sending(packet); p != nil { c.xmpp.Send(p) } } } -func (c *Config) send(packet xmpp.Packet) xmpp.Packet { +func (c *Config) sending(packet xmpp.Packet) xmpp.Packet { logger := log.WithField("type", c.Type) switch p := packet.(type) { case xmpp.Message: diff --git a/component/send_test.go b/component/send_test.go index 585e73e..7abbd6a 100644 --- a/component/send_test.go +++ b/component/send_test.go @@ -13,17 +13,17 @@ func TestSend(t *testing.T) { c := Config{Host: "example.org"} // ignoring packet - p := c.send(xmpp.IQ{}) + p := c.sending(xmpp.IQ{}) assert.Nil(p) // send by component host - p = c.send(xmpp.Message{}) + p = c.sending(xmpp.Message{}) assert.NotNil(p) msg := p.(xmpp.Message) assert.Equal("example.org", msg.PacketAttrs.From) // send by a user of component - p = c.send(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}}) + p = c.sending(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}}) assert.NotNil(p) msg = p.(xmpp.Message) assert.Equal("threemaid@example.org", msg.PacketAttrs.From) diff --git a/component/threema/account.go b/component/threema/account.go index d749a68..a3ecfdf 100644 --- a/component/threema/account.go +++ b/component/threema/account.go @@ -14,7 +14,7 @@ type Account struct { models.AccountThreema Session o3.SessionContext send chan<- o3.Message - recieve <-chan o3.ReceivedMsg + receive <-chan o3.ReceivedMsg deliveredMSG map[uint64]string readedMSG map[uint64]string } @@ -45,7 +45,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) { a := &Account{AccountThreema: account} a.Session = o3.NewSessionContext(tid) - a.send, a.recieve, err = a.Session.Run() + a.send, a.receive, err = a.Session.Run() if err != nil { return nil, err @@ -55,7 +55,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) { a.deliveredMSG = make(map[uint64]string) a.readedMSG = make(map[uint64]string) - go a.reciever(t.out) + go a.receiver(t.out) t.accountJID[jid.String()] = a return a, nil diff --git a/component/threema/main.go b/component/threema/main.go index 5a549ce..2621481 100644 --- a/component/threema/main.go +++ b/component/threema/main.go @@ -55,7 +55,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet { if to.IsDomain() { if from == nil { - log.Warn("recieve message without sender") + log.Warn("receive message without sender") return nil } msg := xmpp.NewMessage("chat", "", from.String(), "", "en") diff --git a/component/threema/recieve.go b/component/threema/recieve.go deleted file mode 100644 index d2fc136..0000000 --- a/component/threema/recieve.go +++ /dev/null @@ -1,62 +0,0 @@ -package threema - -import ( - "strconv" - - "github.com/bdlm/log" - "github.com/o3ma/o3" - "gosrc.io/xmpp" -) - -func (a *Account) reciever(out chan<- xmpp.Packet) { - for receivedMessage := range a.recieve { - if p := a.handle(receivedMessage); p != nil { - out <- p - } - } -} -func (a *Account) handle(receivedMessage o3.ReceivedMsg) xmpp.Packet { - if receivedMessage.Err != nil { - log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err) - return nil - } - switch msg := receivedMessage.Msg.(type) { - case o3.TextMessage: - sender := msg.Sender().String() - if string(a.TID) == sender { - return nil - } - xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), strconv.FormatUint(msg.ID(), 10), "en") - xMSG.Body = msg.Text() - xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptRequest{}) - xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerMarkable{}) - return xMSG - - case o3.DeliveryReceiptMessage: - msgID := msg.MsgID() - xMSG := xmpp.NewMessage("chat", msg.Sender().String(), a.XMPP.String(), "", "en") - - if msg.Status() == o3.MSGDELIVERED { - if id, ok := a.deliveredMSG[msgID]; ok { - xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptReceived{Id: id}) - xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerReceived{Id: id}) - delete(a.deliveredMSG, msgID) - } else { - log.Warnf("found not id in cache to announce received on xmpp side") - } - } - if msg.Status() == o3.MSGREAD { - if id, ok := a.readedMSG[msgID]; ok { - xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerDisplayed{Id: id}) - delete(a.readedMSG, msgID) - } else { - log.Warnf("found not id in cache to announce readed on xmpp side") - } - } - - if len(xMSG.Extensions) > 0 { - return xMSG - } - } - return nil -} diff --git a/component/threema/recieve_test.go b/component/threema/recieve_test.go deleted file mode 100644 index 16a9566..0000000 --- a/component/threema/recieve_test.go +++ /dev/null @@ -1,131 +0,0 @@ -package threema - -import ( - "errors" - "testing" - - "github.com/o3ma/o3" - "github.com/stretchr/testify/assert" - "gosrc.io/xmpp" -) - -const threemaID = "87654321" - -var threemaIDByte o3.IDString - -func init() { - threemaIDByte = o3.NewIDString(threemaID) -} - -func createDummyAccount() Account { - a := Account{ - deliveredMSG: make(map[uint64]string), - readedMSG: make(map[uint64]string), - } - a.TID = make([]byte, len(threemaIDByte)) - copy(a.TID, threemaIDByte[:]) - - return a -} - -func TestRecieve(t *testing.T) { - assert := assert.New(t) - - a := createDummyAccount() - - // handle/skip error - p := a.handle(o3.ReceivedMsg{ - Err: errors.New("dummy"), - }) - assert.Nil(p) - - // nothing to handle - p = a.handle(o3.ReceivedMsg{}) - assert.Nil(p) -} - -func TestRecieveText(t *testing.T) { - assert := assert.New(t) - - a := createDummyAccount() - - // handle text - session := o3.SessionContext{ - ID: o3.ThreemaID{ - ID: o3.NewIDString("12345678"), - Nick: o3.NewPubNick("user"), - }, - } - txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah") - assert.NoError(err) - p := a.handle(o3.ReceivedMsg{ - Msg: txtMsg, - }) - xMSG, ok := p.(xmpp.Message) - assert.True(ok) - assert.Equal("Oojoh0Ah", xMSG.Body) - - // handle/skip text to own id - session = o3.SessionContext{ - ID: o3.ThreemaID{ - ID: threemaIDByte, - Nick: o3.NewPubNick("user"), - }, - } - txtMsg, err = o3.NewTextMessage(&session, threemaID, "Aesh8shu") - assert.NoError(err) - p = a.handle(o3.ReceivedMsg{ - Msg: txtMsg, - }) - assert.Nil(p) -} - -func TestRecieveDeliveryReceipt(t *testing.T) { - assert := assert.New(t) - - a := createDummyAccount() - - // handle delivered - session := o3.SessionContext{ - ID: o3.ThreemaID{ - ID: o3.NewIDString("12345678"), - Nick: o3.NewPubNick("user"), - }, - } - msgID := o3.NewMsgID() - a.deliveredMSG[msgID] = "im4aeseeh1IbaQui" - a.readedMSG[msgID] = "im4aeseeh1IbaQui" - - drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED) - assert.NoError(err) - p := a.handle(o3.ReceivedMsg{ - Msg: drm, - }) - xMSG, ok := p.(xmpp.Message) - assert.True(ok) - rr := xMSG.Extensions[0].(xmpp.ReceiptReceived) - assert.Equal("im4aeseeh1IbaQui", rr.Id) - - // handle delivered -> not in cache - p = a.handle(o3.ReceivedMsg{ - Msg: drm, - }) - assert.Nil(p) - - // handle readed - drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD) - assert.NoError(err) - p = a.handle(o3.ReceivedMsg{ - Msg: drm, - }) - xMSG, ok = p.(xmpp.Message) - assert.True(ok) - cmdd := xMSG.Extensions[0].(xmpp.ChatMarkerDisplayed) - assert.Equal("im4aeseeh1IbaQui", cmdd.Id) - - // handle delivered -> not in cache - p = a.handle(o3.ReceivedMsg{ - Msg: drm, - }) - assert.Nil(p) -} diff --git a/component/threema/send.go b/component/threema/send.go index f6b66aa..0c95c74 100644 --- a/component/threema/send.go +++ b/component/threema/send.go @@ -9,6 +9,17 @@ import ( ) func (a *Account) Send(to string, msg xmpp.Message) error { + m, err := a.sending(to, msg) + if err != nil { + return err + } + if m != nil { + a.send <- m + } + return nil +} +func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) { + // handle delivered / readed msgID := "" readed := false for _, el := range msg.Extensions { @@ -25,7 +36,7 @@ func (a *Account) Send(to string, msg xmpp.Message) error { if msgID != "" { id, err := strconv.ParseUint(msgID, 10, 64) if err != nil { - return err + return nil, err } msgType := o3.MSGDELIVERED if readed { @@ -33,23 +44,22 @@ func (a *Account) Send(to string, msg xmpp.Message) error { } drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType) if err != nil { - return err + return nil, err } - a.send <- drm log.WithFields(map[string]interface{}{ "tid": to, "msg_id": id, "type": msgType, }).Debug("update status of threema message") - return nil + return drm, nil } + // send text message msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body) if err != nil { - return err + return nil, err } a.deliveredMSG[msg3.ID()] = msg.Id a.readedMSG[msg3.ID()] = msg.Id - a.send <- msg3 - return nil + return msg3, nil } diff --git a/component/threema/send_test.go b/component/threema/send_test.go new file mode 100644 index 0000000..7e09ca1 --- /dev/null +++ b/component/threema/send_test.go @@ -0,0 +1,83 @@ +package threema + +import ( + "testing" + + "github.com/o3ma/o3" + "github.com/stretchr/testify/assert" + "gosrc.io/xmpp" +) + +func TestAccountSend(t *testing.T) { + assert := assert.New(t) + + send := make(chan o3.Message) + a := Account{ + Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}), + send: send, + deliveredMSG: make(map[uint64]string), + readedMSG: make(map[uint64]string), + } + + go func() { + a.Send("a", xmpp.Message{ + PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, + Body: "ohz8kai0ohNgohth", + }) + }() + p := <-send + assert.NotNil(p) + msg := p.(o3.TextMessage) + assert.Contains(msg.Text(), "ohz8kai0ohNgohth") + + // test error + err := a.Send("a", xmpp.Message{ + PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, + Extensions: []xmpp.MsgExtension{ + &xmpp.ReceiptReceived{Id: "blub"}, + }, + }) + assert.Error(err) +} + +func TestAccountSendingDeliviery(t *testing.T) { + assert := assert.New(t) + + a := Account{ + Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}), + } + + // test error - threema send only int ids + msg, err := a.sending("a", xmpp.Message{ + PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, + Extensions: []xmpp.MsgExtension{ + &xmpp.ReceiptReceived{Id: "blub"}, + }, + }) + assert.Error(err) + assert.Nil(msg) + + // test delivered + msg, err = a.sending("a", xmpp.Message{ + PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, + Extensions: []xmpp.MsgExtension{ + &xmpp.ChatMarkerReceived{Id: "3"}, + }, + }) + assert.NoError(err) + drm, ok := msg.(o3.DeliveryReceiptMessage) + assert.True(ok) + assert.Equal(o3.MSGDELIVERED, drm.Status()) + + // test read + msg, err = a.sending("a", xmpp.Message{ + PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, + Extensions: []xmpp.MsgExtension{ + &xmpp.ChatMarkerDisplayed{Id: "5"}, + }, + }) + assert.NoError(err) + drm, ok = msg.(o3.DeliveryReceiptMessage) + assert.True(ok) + assert.Equal(o3.MSGREAD, drm.Status()) +}