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.NewRoute().IQNamespaces(stanza.NSDiscoItems).HandlerFunc(c.handleDiscoItems)
router.HandleFunc("iq", c.handleIQ) router.HandleFunc("iq", c.handleIQ)
router.HandleFunc("message", c.handleMessage) router.HandleFunc("message", c.handleMessage)
router.HandleFunc("presence", c.handleMessage)
c.xmpp, err = xmpp.NewComponent(xmpp.ComponentOptions{ c.xmpp, err = xmpp.NewComponent(xmpp.ComponentOptions{
Domain: c.Host, Domain: c.Host,

View File

@ -31,6 +31,7 @@ func (c *Config) handleDiscoInfo(s xmpp.Sender, p stanza.Packet) {
{Var: stanza.NSMsgReceipts}, {Var: stanza.NSMsgReceipts},
{Var: stanza.NSMsgChatMarkers}, {Var: stanza.NSMsgChatMarkers},
{Var: stanza.NSMsgChatStateNotifications}, {Var: stanza.NSMsgChatStateNotifications},
{Var: "http://jabber.org/protocol/muc"},
}, },
} }
if discoInfo.Node == "" { 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) { func (c *Config) handleMessage(s xmpp.Sender, p stanza.Packet) {
msg, ok := p.(stanza.Message) msg, ok := p.(stanza.Message)
attr := msg.Attrs
if !ok {
pr, ok := p.(stanza.Message)
attr = pr.Attrs
if !ok { if !ok {
return return
} }
}
if c.XMPPDebug { if c.XMPPDebug {
log.WithFields(map[string]interface{}{ log.WithFields(map[string]interface{}{
"type": c.Type, "type": c.Type,
"from": s, "from": s,
"to": msg.To, "to": attr.To,
"id": msg.Id, "id": attr.Id,
}).Debug(msg.XMPPFormat()) }).Debug(msg.XMPPFormat())
} }
c.comp.Send(p) 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 db := database.Read; db != nil && db.DB().Ping() == nil {
if err := db.Where(jid).First(jid); err.RecordNotFound() { if err := db.Where(jid).First(jid); err.RecordNotFound() {
database.Write.Create(jid) database.Write.Create(jid)
} else if err != nil { } else if err.Error != nil {
log.Errorf("error getting jid %s from datatbase: %s", jid.String(), err.Error) log.Errorf("error getting jid %s from database: %s", jid.String(), err.Error)
} }
} }
bot := &Bot{ bot := &Bot{

View File

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

View File

@ -1,7 +1,9 @@
package threema package threema
import ( import (
"encoding/base32"
"strconv" "strconv"
"strings"
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/o3ma/o3" "github.com/o3ma/o3"
@ -32,6 +34,18 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
Recipient: o3.NewIDString(to), Recipient: o3.NewIDString(to),
PubNick: a.ThreemaID.Nick, 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 chatState := false
chatStateComposing := false chatStateComposing := false
@ -102,12 +116,21 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
MessageHeader: header, MessageHeader: header,
Body: msg.Body, Body: msg.Body,
} }
a.deliveredMSG[msg3ID] = msg.Id logger = logger.WithFields(map[string]interface{}{
a.readedMSG[msg3ID] = msg.Id
logger.WithFields(map[string]interface{}{
"x_id": msg.Id, "x_id": msg.Id,
"t_id": msg3ID, "t_id": msg3ID,
"text": msg.Body, "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 return msg3, nil
} }