Convert XEP-0461 replies to Telegram
This commit is contained in:
parent
7bd165a738
commit
90807b2d9e
|
@ -15,7 +15,7 @@ import (
|
|||
goxmpp "gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
var version string = "1.4.0"
|
||||
var version string = "1.5.0-dev"
|
||||
var commit string
|
||||
|
||||
var sm *goxmpp.StreamManager
|
||||
|
|
|
@ -488,7 +488,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
return "Last message is empty", true
|
||||
}
|
||||
|
||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "")
|
||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", 0)
|
||||
|
||||
if content != nil {
|
||||
c.client.EditMessageText(&client.EditMessageTextRequest{
|
||||
|
@ -505,7 +505,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
return "Not enough arguments", true
|
||||
}
|
||||
|
||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "")
|
||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", 0)
|
||||
|
||||
if content != nil {
|
||||
_, err := c.client.SendMessage(&client.SendMessageRequest{
|
||||
|
@ -584,7 +584,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
|||
}
|
||||
}
|
||||
|
||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 1), "")
|
||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 1), "", 0)
|
||||
|
||||
if content != nil {
|
||||
_, err := c.client.SendMessage(&client.SendMessageRequest{
|
||||
|
|
|
@ -857,7 +857,7 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
|
|||
}
|
||||
|
||||
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
|
||||
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string) client.InputMessageContent {
|
||||
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, replyId int64) client.InputMessageContent {
|
||||
if !c.Online() {
|
||||
// we're offline
|
||||
return nil
|
||||
|
@ -879,10 +879,14 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|||
|
||||
// quotations
|
||||
var reply int64
|
||||
if replyId == 0 {
|
||||
replySlice := replyRegex.FindStringSubmatch(text)
|
||||
if len(replySlice) > 1 {
|
||||
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
|
||||
}
|
||||
} else {
|
||||
reply = replyId
|
||||
}
|
||||
|
||||
// attach a file
|
||||
var file *client.InputFileLocal
|
||||
|
@ -949,7 +953,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|||
}
|
||||
|
||||
// remove first line from text
|
||||
if file != nil || reply != 0 {
|
||||
if file != nil || (reply != 0 && replyId == 0) {
|
||||
newlinePos := strings.Index(text, newlineChar)
|
||||
if newlinePos != -1 {
|
||||
text = text[newlinePos+1:]
|
||||
|
|
|
@ -123,20 +123,20 @@ type Reply struct {
|
|||
type Fallback struct {
|
||||
XMLName xml.Name `xml:"urn:xmpp:fallback:0 fallback"`
|
||||
For string `xml:"for,attr"`
|
||||
Body []FallbackBody
|
||||
Subject []FallbackSubject
|
||||
Body []FallbackBody `xml:"urn:xmpp:fallback:0 body"`
|
||||
Subject []FallbackSubject `xml:"urn:xmpp:fallback:0 subject"`
|
||||
}
|
||||
|
||||
// FallbackBody is from XEP-0428
|
||||
type FallbackBody struct {
|
||||
XMLName xml.Name `xml:"body"`
|
||||
XMLName xml.Name `xml:"urn:xmpp:fallback:0 body"`
|
||||
Start string `xml:"start,attr"`
|
||||
End string `xml:"end,attr"`
|
||||
}
|
||||
|
||||
// FallbackSubject is from XEP-0428
|
||||
type FallbackSubject struct {
|
||||
XMLName xml.Name `xml:"subject"`
|
||||
XMLName xml.Name `xml:"urn:xmpp:fallback:0 subject"`
|
||||
Start string `xml:"start,attr"`
|
||||
End string `xml:"end,attr"`
|
||||
}
|
||||
|
|
|
@ -99,7 +99,46 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
|
|||
|
||||
toID, ok := toToID(msg.To)
|
||||
if ok {
|
||||
session.ProcessOutgoingMessage(toID, msg.Body, msg.From)
|
||||
var reply extensions.Reply
|
||||
var fallback extensions.Fallback
|
||||
msg.Get(&reply)
|
||||
msg.Get(&fallback)
|
||||
log.Debugf("reply: %#v", reply)
|
||||
log.Debugf("fallback: %#v", fallback)
|
||||
|
||||
var replyId int64
|
||||
var err error
|
||||
text := msg.Body
|
||||
if len(reply.Id) > 0 {
|
||||
id := reply.Id
|
||||
if id[0] == 'e' {
|
||||
id = id[1:]
|
||||
}
|
||||
replyId, err = strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
log.Warn(errors.Wrap(err, "Failed to parse message ID!"))
|
||||
}
|
||||
|
||||
if replyId != 0 && fallback.For == "urn:xmpp:reply:0" && len(fallback.Body) > 0 {
|
||||
body := fallback.Body[0]
|
||||
var start, end int64
|
||||
start, err = strconv.ParseInt(body.Start, 10, 64)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"start": body.Start,
|
||||
}).Warn(errors.Wrap(err, "Failed to parse fallback start!"))
|
||||
}
|
||||
end, err = strconv.ParseInt(body.End, 10, 64)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"end": body.End,
|
||||
}).Warn(errors.Wrap(err, "Failed to parse fallback end!"))
|
||||
}
|
||||
text = text[:start] + text[end:]
|
||||
}
|
||||
}
|
||||
|
||||
session.ProcessOutgoingMessage(toID, text, msg.From, replyId)
|
||||
return
|
||||
} else {
|
||||
toJid, err := stanza.NewJid(msg.To)
|
||||
|
|
Loading…
Reference in a new issue