threema refactory
This commit is contained in:
parent
e80741392d
commit
5a53c04c6e
|
@ -1,7 +1,3 @@
|
||||||
[submodule "vendor/github.com/o3ma/o3"]
|
[submodule "vendor/github.com/o3ma/o3"]
|
||||||
path = vendor/github.com/o3ma/o3
|
path = vendor/github.com/o3ma/o3
|
||||||
url = https://github.com/genofire/o3.git
|
url = https://github.com/genofire/o3.git
|
||||||
[submodule "vendor/gosrc.io/xmpp"]
|
|
||||||
path = vendor/gosrc.io/xmpp
|
|
||||||
url = https://github.com/genofire/go-xmpp.git
|
|
||||||
branch = patch-1
|
|
||||||
|
|
|
@ -19,13 +19,14 @@ func (a *Account) receiver(out chan<- stanza.Packet) {
|
||||||
out <- xMSG
|
out <- xMSG
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sender := receivedMessage.Msg.Sender().String()
|
header := receivedMessage.Msg.Header()
|
||||||
|
sender := header.Sender.String()
|
||||||
if string(a.TID) == sender {
|
if string(a.TID) == sender {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if p, err := a.receiving(receivedMessage.Msg); err != nil {
|
if p, err := a.receiving(receivedMessage.Msg); err != nil {
|
||||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
|
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
|
||||||
xMSG.Body = fmt.Sprintf("error on decoding message: %s\n%v", err, receivedMessage.Msg.Serialize())
|
xMSG.Body = fmt.Sprintf("error on decoding message: %s\n%v", err, receivedMessage.Msg)
|
||||||
out <- xMSG
|
out <- xMSG
|
||||||
} else if p != nil {
|
} else if p != nil {
|
||||||
out <- p
|
out <- p
|
||||||
|
@ -40,77 +41,77 @@ func requestExtensions(xMSG *stanza.Message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
|
func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
|
||||||
|
header := receivedMessage.Header()
|
||||||
|
sender := header.Sender.String()
|
||||||
logger := log.WithFields(map[string]interface{}{
|
logger := log.WithFields(map[string]interface{}{
|
||||||
"from": receivedMessage.Sender().String(),
|
"from": header.Sender.String(),
|
||||||
"to": a.XMPP.String(),
|
"to": a.XMPP.String(),
|
||||||
})
|
})
|
||||||
switch msg := receivedMessage.(type) {
|
switch msg := receivedMessage.(type) {
|
||||||
case o3.TextMessage:
|
case o3.TextMessage:
|
||||||
sender := msg.Sender().String()
|
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)})
|
||||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)})
|
xMSG.Body = msg.Body
|
||||||
xMSG.Body = msg.Text()
|
|
||||||
requestExtensions(&xMSG)
|
requestExtensions(&xMSG)
|
||||||
logger.WithField("text", xMSG.Body).Debug("send text")
|
logger.WithField("text", xMSG.Body).Debug("send text")
|
||||||
return xMSG, nil
|
return xMSG, nil
|
||||||
|
/*
|
||||||
|
case o3.AudioMessage:
|
||||||
|
if a.threema.httpUploadPath == "" {
|
||||||
|
return nil, errors.New("no place to store files at transport configurated")
|
||||||
|
}
|
||||||
|
data, err := msg.GetAudioData(a.Session)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("unable to read data from message: %s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
xMSG, err := a.FileToXMPP(sender.String(), header.ID, "mp3", data)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("unable to create data from message: %s", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
xMSG.Type = "chat"
|
||||||
|
requestExtensions(&xMSG)
|
||||||
|
logger.WithField("url", xMSG.Body).Debug("send audio")
|
||||||
|
return xMSG, nil
|
||||||
|
|
||||||
case o3.AudioMessage:
|
case o3.ImageMessage:
|
||||||
if a.threema.httpUploadPath == "" {
|
if a.threema.httpUploadPath == "" {
|
||||||
return nil, errors.New("no place to store files at transport configurated")
|
return nil, errors.New("no place to store files at transport configurated")
|
||||||
}
|
}
|
||||||
data, err := msg.GetAudioData(a.Session)
|
data, err := msg.GetImageData(a.Session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf("unable to read data from message: %s", err)
|
logger.Warnf("unable to read data from message: %s", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
xMSG, err := a.FileToXMPP(msg.Sender().String(), msg.ID(), "mp3", data)
|
xMSG, err := a.FileToXMPP(sender.String(), header.ID, "jpg", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf("unable to create data from message: %s", err)
|
logger.Warnf("unable to create data from message: %s", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
xMSG.Type = "chat"
|
xMSG.Type = "chat"
|
||||||
requestExtensions(&xMSG)
|
requestExtensions(&xMSG)
|
||||||
logger.WithField("url", xMSG.Body).Debug("send audio")
|
logger.WithField("url", xMSG.Body).Debug("send image")
|
||||||
return xMSG, nil
|
return xMSG, nil
|
||||||
|
*/
|
||||||
case o3.ImageMessage:
|
|
||||||
if a.threema.httpUploadPath == "" {
|
|
||||||
return nil, errors.New("no place to store files at transport configurated")
|
|
||||||
}
|
|
||||||
data, err := msg.GetImageData(a.Session)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warnf("unable to read data from message: %s", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
xMSG, err := a.FileToXMPP(msg.Sender().String(), msg.ID(), "jpg", data)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warnf("unable to create data from message: %s", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
xMSG.Type = "chat"
|
|
||||||
requestExtensions(&xMSG)
|
|
||||||
logger.WithField("url", xMSG.Body).Debug("send image")
|
|
||||||
return xMSG, nil
|
|
||||||
|
|
||||||
case o3.DeliveryReceiptMessage:
|
case o3.DeliveryReceiptMessage:
|
||||||
msgID := msg.MsgID()
|
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
|
||||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String()})
|
|
||||||
state := ""
|
state := ""
|
||||||
|
|
||||||
if msg.Status() == o3.MSGDELIVERED {
|
if msg.Status == o3.MSGDELIVERED {
|
||||||
state = "delivered"
|
state = "delivered"
|
||||||
if id, ok := a.deliveredMSG[msgID]; ok {
|
if id, ok := a.deliveredMSG[msg.MessageID]; ok {
|
||||||
xMSG.Extensions = append(xMSG.Extensions, stanza.ReceiptReceived{ID: id})
|
xMSG.Extensions = append(xMSG.Extensions, stanza.ReceiptReceived{ID: id})
|
||||||
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkReceived{ID: id})
|
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkReceived{ID: id})
|
||||||
delete(a.deliveredMSG, msgID)
|
delete(a.deliveredMSG, msg.MessageID)
|
||||||
} else {
|
} else {
|
||||||
logger.Warnf("found not id in cache to announce received on xmpp side")
|
logger.Warnf("found not id in cache to announce received on xmpp side")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if msg.Status() == o3.MSGREAD {
|
if msg.Status == o3.MSGREAD {
|
||||||
state = "displayed"
|
state = "displayed"
|
||||||
if id, ok := a.readedMSG[msgID]; ok {
|
if id, ok := a.readedMSG[msg.MessageID]; ok {
|
||||||
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkDisplayed{ID: id})
|
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkDisplayed{ID: id})
|
||||||
delete(a.readedMSG, msgID)
|
delete(a.readedMSG, msg.MessageID)
|
||||||
} else {
|
} else {
|
||||||
logger.Warnf("found not id in cache to announce readed on xmpp side")
|
logger.Warnf("found not id in cache to announce readed on xmpp side")
|
||||||
}
|
}
|
||||||
|
@ -122,7 +123,7 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case o3.TypingNotificationMessage:
|
case o3.TypingNotificationMessage:
|
||||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)})
|
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)})
|
||||||
if msg.OnOff != 0 {
|
if msg.OnOff != 0 {
|
||||||
logger.Debug("composing")
|
logger.Debug("composing")
|
||||||
xMSG.Extensions = append(xMSG.Extensions, stanza.StateComposing{})
|
xMSG.Extensions = append(xMSG.Extensions, stanza.StateComposing{})
|
||||||
|
|
|
@ -23,6 +23,7 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
||||||
"from": a.XMPP.String(),
|
"from": a.XMPP.String(),
|
||||||
"to": to,
|
"to": to,
|
||||||
})
|
})
|
||||||
|
msg3To := o3.NewIDString(to)
|
||||||
|
|
||||||
chatState := false
|
chatState := false
|
||||||
chatStateComposing := false
|
chatStateComposing := false
|
||||||
|
@ -65,7 +66,13 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
||||||
if msgStateRead {
|
if msgStateRead {
|
||||||
msgType = o3.MSGREAD
|
msgType = o3.MSGREAD
|
||||||
}
|
}
|
||||||
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
|
drm := o3.DeliveryReceiptMessage{
|
||||||
|
MessageHeader: &o3.MessageHeader{
|
||||||
|
ID: id,
|
||||||
|
Recipient: msg3To,
|
||||||
|
},
|
||||||
|
Status: msgType,
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -86,17 +93,21 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
msg3ID := o3.NewMsgID()
|
||||||
|
|
||||||
// send text message
|
// send text message
|
||||||
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
|
msg3 := o3.TextMessage{
|
||||||
if err != nil {
|
MessageHeader: &o3.MessageHeader{
|
||||||
return nil, err
|
ID: msg3ID,
|
||||||
|
Recipient: msg3To,
|
||||||
|
},
|
||||||
|
Body: msg.Body,
|
||||||
}
|
}
|
||||||
a.deliveredMSG[msg3.ID()] = msg.Id
|
a.deliveredMSG[msg3ID] = msg.Id
|
||||||
a.readedMSG[msg3.ID()] = msg.Id
|
a.readedMSG[msg3ID] = msg.Id
|
||||||
logger.WithFields(map[string]interface{}{
|
logger.WithFields(map[string]interface{}{
|
||||||
"x_id": msg.Id,
|
"x_id": msg.Id,
|
||||||
"t_id": msg3.ID(),
|
"t_id": msg3ID,
|
||||||
"text": msg.Body,
|
"text": msg.Body,
|
||||||
}).Debug("send text")
|
}).Debug("send text")
|
||||||
return msg3, nil
|
return msg3, nil
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 6f04471a270e0b968bfa499b4d5d6d640141afb9
|
Subproject commit 76d505644f017b91642e7800309ffba08cfbc285
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit ca13581329dd9a42961025a6b8e935b7a59a08de
|
|
Reference in New Issue