diff options
Diffstat (limited to 'xmpp')
-rw-r--r-- | xmpp/extensions/extensions.go | 36 | ||||
-rw-r--r-- | xmpp/gateway/gateway.go | 55 | ||||
-rw-r--r-- | xmpp/handlers.go | 4 |
3 files changed, 79 insertions, 16 deletions
diff --git a/xmpp/extensions/extensions.go b/xmpp/extensions/extensions.go index 3a2f998..679b428 100644 --- a/xmpp/extensions/extensions.go +++ b/xmpp/extensions/extensions.go @@ -234,6 +234,20 @@ type PresenceXMucUserStatus struct { Code uint16 `xml:"code,attr"` } +// MessageDelay is from XEP-0203 +type MessageDelay struct { + XMLName xml.Name `xml:"urn:xmpp:delay delay"` + From string `xml:"from,attr"` + Stamp string `xml:"stamp,attr"` +} + +// MessageDelayLegacy is from XEP-0203 +type MessageDelayLegacy struct { + XMLName xml.Name `xml:"jabber:x:delay x"` + From string `xml:"from,attr"` + Stamp string `xml:"stamp,attr"` +} + // Namespace is a namespace! func (c PresenceNickExtension) Namespace() string { return c.XMLName.Space @@ -304,6 +318,16 @@ func (c PresenceXMucUserExtension) Namespace() string { return c.XMLName.Space } +// Namespace is a namespace! +func (c MessageDelay) Namespace() string { + return c.XMLName.Space +} + +// Namespace is a namespace! +func (c MessageDelayLegacy) Namespace() string { + return c.XMLName.Space +} + // Name is a packet name func (ClientMessage) Name() string { return "message" @@ -394,4 +418,16 @@ func init() { "http://jabber.org/protocol/muc#user", "x", }, PresenceXMucUserExtension{}) + + // message delay + stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{ + "urn:xmpp:delay", + "delay", + }, MessageDelay{}) + + // legacy message delay + stanza.TypeRegistry.MapExtension(stanza.PKTMessage, xml.Name{ + "jabber:x:delay", + "x", + }, MessageDelayLegacy{}) } diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go index d6b7826..074d2d0 100644 --- a/xmpp/gateway/gateway.go +++ b/xmpp/gateway/gateway.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" "strings" "sync" + "time" "dev.narayana.im/narayana/telegabber/badger" "dev.narayana.im/narayana/telegabber/xmpp/extensions" @@ -42,26 +43,26 @@ var DirtySessions = false var MessageOutgoingPermissionVersion = 0 // SendMessage creates and sends a message stanza -func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, isCarbon bool) { - sendMessageWrapper(to, from, body, id, component, reply, "", isCarbon) +func SendMessage(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, timestamp int64, isCarbon, isGroupchat bool) { + sendMessageWrapper(to, from, body, id, component, reply, timestamp, "", isCarbon, isGroupchat) } // SendServiceMessage creates and sends a simple message stanza from transport func SendServiceMessage(to string, body string, component *xmpp.Component) { - sendMessageWrapper(to, "", body, "", component, nil, "", false) + sendMessageWrapper(to, "", body, "", component, nil, 0, "", false, false) } // SendTextMessage creates and sends a simple message stanza func SendTextMessage(to string, from string, body string, component *xmpp.Component) { - sendMessageWrapper(to, from, body, "", component, nil, "", false) + sendMessageWrapper(to, from, body, "", component, nil, 0, "", false, false) } // SendMessageWithOOB creates and sends a message stanza with OOB URL -func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob string, isCarbon bool) { - sendMessageWrapper(to, from, body, id, component, reply, oob, isCarbon) +func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, timestamp int64, oob string, isCarbon bool, isGroupchat bool) { + sendMessageWrapper(to, from, body, id, component, reply, timestamp, oob, isCarbon, isGroupchat) } -func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, oob string, isCarbon bool) { +func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, reply *Reply, timestamp int64, oob string, isCarbon bool, isGroupchat bool) { toJid, err := stanza.NewJid(to) if err != nil { log.WithFields(log.Fields{ @@ -76,12 +77,17 @@ func sendMessageWrapper(to string, from string, body string, id string, componen var logFrom string var messageFrom string var messageTo string - if from == "" { - logFrom = componentJid - messageFrom = componentJid - } else { + if isGroupchat { logFrom = from - messageFrom = from + "@" + componentJid + messageFrom = from + } else { + if from == "" { + logFrom = componentJid + messageFrom = componentJid + } else { + logFrom = from + messageFrom = from + "@" + componentJid + } } if isCarbon { messageTo = messageFrom @@ -95,11 +101,18 @@ func sendMessageWrapper(to string, from string, body string, id string, componen "to": to, }).Warn("Got message") + var messageType stanza.StanzaType + if isGroupchat { + messageType = stanza.MessageTypeGroupchat + } else { + messageType = stanza.MessageTypeChat + } + message := stanza.Message{ Attrs: stanza.Attrs{ From: messageFrom, To: messageTo, - Type: "chat", + Type: messageType, Id: id, }, Body: body, @@ -122,13 +135,27 @@ func sendMessageWrapper(to string, from string, body string, id string, componen if !isCarbon && toJid.Resource != "" { message.Extensions = append(message.Extensions, stanza.HintNoCopy{}) } + if timestamp != 0 { + var delayFrom string + if isGroupchat { + delayFrom, _, _ = SplitJID(from) + } + message.Extensions = append(message.Extensions, extensions.MessageDelay{ + From: delayFrom, + Stamp: time.Unix(timestamp, 0).UTC().Format(time.RFC3339), + }) + message.Extensions = append(message.Extensions, extensions.MessageDelayLegacy{ + From: delayFrom, + Stamp: time.Unix(timestamp, 0).UTC().Format("20060102T15:04:05"), + }) + } if isCarbon { carbonMessage := extensions.ClientMessage{ Attrs: stanza.Attrs{ From: bareTo, To: to, - Type: "chat", + Type: messageType, }, } carbonMessage.Extensions = append(carbonMessage.Extensions, extensions.CarbonSent{ diff --git a/xmpp/handlers.go b/xmpp/handlers.go index a7e3c55..bb2064a 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -440,7 +440,7 @@ func handleMUCPresence(s xmpp.Sender, p stanza.Presence) { return } - fromBare, _, ok := gateway.SplitJID(p.From) + fromBare, fromResource, ok := gateway.SplitJID(p.From) if !ok { presenceReplySetError(reply, 400) return @@ -458,7 +458,7 @@ func handleMUCPresence(s xmpp.Sender, p stanza.Presence) { return } - session.SendMUCStatuses(chatId) + session.JoinMUC(chatId, fromResource) } } } |