group message support - text WIP

This commit is contained in:
Martin/Geno 2019-09-06 04:34:13 +02:00 committed by genofire
parent 7e53697945
commit 93b7dc12f6
5 changed files with 42 additions and 9 deletions

View File

@ -29,6 +29,7 @@ func (c *Config) Start() error {
router.NewRoute().IQNamespaces(stanza.NSDiscoItems).HandlerFunc(c.handleDiscoItems)
router.HandleFunc("iq", c.handleIQ)
router.HandleFunc("message", c.handleMessage)
router.HandleFunc("presence", c.handleMessage)
c.xmpp, err = xmpp.NewComponent(xmpp.ComponentOptions{
Domain: c.Host,

View File

@ -31,6 +31,7 @@ func (c *Config) handleDiscoInfo(s xmpp.Sender, p stanza.Packet) {
{Var: stanza.NSMsgReceipts},
{Var: stanza.NSMsgChatMarkers},
{Var: stanza.NSMsgChatStateNotifications},
{Var: "http://jabber.org/protocol/muc"},
},
}
if discoInfo.Node == "" {
@ -98,15 +99,20 @@ func (c *Config) handleIQ(s xmpp.Sender, p stanza.Packet) {
}
func (c *Config) handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message)
attr := msg.Attrs
if !ok {
return
pr, ok := p.(stanza.Message)
attr = pr.Attrs
if !ok {
return
}
}
if c.XMPPDebug {
log.WithFields(map[string]interface{}{
"type": c.Type,
"from": s,
"to": msg.To,
"id": msg.Id,
"to": attr.To,
"id": attr.Id,
}).Debug(msg.XMPPFormat())
}
c.comp.Send(p)

View File

@ -26,8 +26,8 @@ func (t *Threema) getBot(jid *models.JID) *Bot {
if db := database.Read; db != nil && db.DB().Ping() == nil {
if err := db.Where(jid).First(jid); err.RecordNotFound() {
database.Write.Create(jid)
} else if err != nil {
log.Errorf("error getting jid %s from datatbase: %s", jid.String(), err.Error)
} else if err.Error != nil {
log.Errorf("error getting jid %s from database: %s", jid.String(), err.Error)
}
}
bot := &Bot{

View File

@ -67,6 +67,9 @@ func (t *Threema) Send(packet stanza.Packet) {
}
func (t *Threema) send(packet stanza.Packet) stanza.Packet {
switch p := packet.(type) {
case stanza.Presence:
log.Debug(p)
return nil
case stanza.Message:
from := models.ParseJID(p.Attrs.From)
to := models.ParseJID(p.Attrs.To)

View File

@ -1,7 +1,9 @@
package threema
import (
"encoding/base32"
"strconv"
"strings"
"github.com/bdlm/log"
"github.com/o3ma/o3"
@ -32,6 +34,18 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
Recipient: o3.NewIDString(to),
PubNick: a.ThreemaID.Nick,
}
var groupHeader *o3.GroupMessageHeader
if msg.Type == stanza.MessageTypeGroupchat {
toA := strings.SplitN(to, "-", 2)
gid, err := base32.StdEncoding.DecodeString(toA[1])
if err != nil {
return nil, err
}
groupHeader = &o3.GroupMessageHeader{
CreatorID: o3.NewIDString(toA[0]),
}
copy(groupHeader.GroupID[:], gid)
}
chatState := false
chatStateComposing := false
@ -102,12 +116,21 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
MessageHeader: header,
Body: msg.Body,
}
a.deliveredMSG[msg3ID] = msg.Id
a.readedMSG[msg3ID] = msg.Id
logger.WithFields(map[string]interface{}{
logger = logger.WithFields(map[string]interface{}{
"x_id": msg.Id,
"t_id": msg3ID,
"text": msg.Body,
}).Debug("send text")
})
if groupHeader != nil {
logger.Debug("send grouptext")
// TODO iterate of all occupants
return &o3.GroupTextMessage{
GroupMessageHeader: groupHeader,
TextMessage: msg3,
}, nil
}
a.deliveredMSG[msg3ID] = msg.Id
a.readedMSG[msg3ID] = msg.Id
logger.Debug("send text")
return msg3, nil
}