Fix %!(MISSING) and other formatting issues

This commit is contained in:
Bohdan Horbeshko 2021-12-18 11:04:24 -05:00
parent ad6e92e6d6
commit f2f20e680b
9 changed files with 59 additions and 19 deletions

4
go.mod
View file

@ -10,8 +10,8 @@ require (
github.com/soheilhy/args v0.0.0-20150720134047-6bcf4c78e87e
github.com/zelenin/go-tdlib v0.1.0
gopkg.in/yaml.v2 v2.2.4
gosrc.io/xmpp v0.1.3
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1
)
replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c
replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.2.1-0.20211218153313-a8aadd78b65b
replace github.com/zelenin/go-tdlib => github.com/godcong/go-tdlib v0.4.4-0.20211203152853-64d22ab8d4ac

6
go.sum
View file

@ -10,6 +10,10 @@ github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c h1:LzcQyE+Gs+
github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c/go.mod h1:fWixaMaFvx8cxXcJVJ5kU9csMeD/JN8on7ybassU8rY=
github.com/bodqhrohro/go-xmpp v0.2.1-0.20191105232737-9abd5be0aa1b h1:9BLd/SNO4JJZLRl1Qb1v9mNivIlHuwHDe2c8hQvBxFA=
github.com/bodqhrohro/go-xmpp v0.2.1-0.20191105232737-9abd5be0aa1b/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211205194122-f8c4ecb59d8b h1:rTK55SNCBmssyRgNAweVwVVfuoRstI8RbL+8Ys/RzxE=
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211205194122-f8c4ecb59d8b/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211218153313-a8aadd78b65b h1:VDi8z3PzEDhQzazRRuv1fkv662DT3Mm/TY/Lni2Sgrc=
github.com/bodqhrohro/go-xmpp v0.2.1-0.20211218153313-a8aadd78b65b/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
github.com/chromedp/cdproto v0.0.0-20190614062957-d6d2f92b486d/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
github.com/chromedp/cdproto v0.0.0-20190621002710-8cbd498dd7a0/go.mod h1:S8mB5wY3vV+vRIzf39xDXsw3XKYewW9X6rW2aEmkrSw=
github.com/chromedp/cdproto v0.0.0-20190812224334-39ef923dcb8d/go.mod h1:0YChpVzuLJC5CPr+x3xkHN6Z8KOSXjNbL7qV8Wc4GW0=
@ -126,6 +130,8 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gosrc.io/xmpp v0.1.3 h1:VYP1bA35irlQ1ZAJqNhJOz8NSsSTkzQRhREfmuG1H80=
gosrc.io/xmpp v0.1.3/go.mod h1:fWixaMaFvx8cxXcJVJ5kU9csMeD/JN8on7ybassU8rY=
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1 h1:E3uJqX6ImJL9AFdjGbiW04jq8IQ+NcOK+JSiWq2TbRw=
gosrc.io/xmpp v0.5.2-0.20211214110136-5f99e1cd06e1/go.mod h1:L3NFMqYOxyLz3JGmgFyWf7r9htE91zVGiK40oW4RwdY=
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY=
mvdan.cc/sh v2.6.4+incompatible/go.mod h1:IeeQbZq+x2SUGBensq/jge5lLQbS3XT2ktyp3wrt4x8=

View file

@ -17,10 +17,10 @@ type Insertion struct {
// from the start or from the end
type InsertionStack []*Insertion
var boldRunes = []rune("**")
var boldRunesMarkdown = []rune("**")
var boldRunesXEP0393 = []rune("*")
var italicRunes = []rune("_")
var codeRunes = []rune("\n```\n")
var urlRuneL = []rune("[")
// rebalance pumps all the values at given offset to current stack (growing
// from start) from given stack (growing from end); should be called
@ -80,7 +80,7 @@ func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) (*Insertion,
func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
switch entity.Type.TextEntityTypeType() {
case client.TypeTextEntityTypeBold:
return markupBraces(entity, boldRunes, boldRunes)
return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown)
case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes)
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
@ -90,13 +90,35 @@ func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
case client.TypeTextEntityTypeTextUrl:
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
return markupBraces(entity, urlRuneL, []rune("]("+textURL.Url+")"))
return markupBraces(entity, []rune("["), []rune("]("+textURL.Url+")"))
}
return nil, nil
}
// Format traverses an already sorted list of entities and wraps the text in Markdown
// EntityToXEP0393 generates the wrapping XEP-0393 tags
func EntityToXEP0393(entity *client.TextEntity) (*Insertion, *Insertion) {
switch entity.Type.TextEntityTypeType() {
case client.TypeTextEntityTypeBold:
return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393)
case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes)
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
return markupBraces(entity, codeRunes, codeRunes)
case client.TypeTextEntityTypePreCode:
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
// TODO: inline code support (non-standard too)
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
case client.TypeTextEntityTypeTextUrl:
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)
// non-standard, Pidgin-specific
return markupBraces(entity, []rune{}, []rune(" <"+textURL.Url+">"))
}
return nil, nil
}
// Format traverses an already sorted list of entities and wraps the text in a markup
func Format(
sourceText string,
entities []*client.TextEntity,

View file

@ -237,7 +237,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
// message content updated
func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
markupFunction := formatter.EntityToMarkdown
markupFunction := formatter.EntityToXEP0393
if update.NewContent.MessageContentType() == client.TypeMessageText {
textContent := update.NewContent.(*client.MessageText)
text := fmt.Sprintf("✎ %v | %s", update.MessageID, formatter.Format(

View file

@ -292,7 +292,7 @@ func (c *Client) messageToText(message *client.Message) string {
return "<BIG EMOJI>"
}
markupFunction := formatter.EntityToMarkdown
markupFunction := formatter.EntityToXEP0393
switch message.Content.MessageContentType() {
case client.TypeMessageSticker:
sticker, _ := message.Content.(*client.MessageSticker)

View file

@ -11,6 +11,7 @@ import (
log "github.com/sirupsen/logrus"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
var tgConf config.TelegramConfig
@ -22,7 +23,7 @@ var db *persistence.SessionsYamlDB
func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, *xmpp.Component, error) {
var err error
gateway.Jid, err = xmpp.NewJid(conf.Jid)
gateway.Jid, err = stanza.NewJid(conf.Jid)
if err != nil {
return nil, nil, err
}
@ -30,8 +31,11 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
tgConf = tc
options := xmpp.ComponentOptions{
TransportConfiguration: xmpp.TransportConfiguration{
Address: conf.Host + ":" + conf.Port,
Domain: conf.Jid,
},
Domain: conf.Jid,
Secret: conf.Password,
Name: "telegabber",
}
@ -41,7 +45,9 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
router.HandleFunc("presence", HandlePresence)
router.HandleFunc("message", HandleMessage)
component, err := xmpp.NewComponent(options, router)
component, err := xmpp.NewComponent(options, router, func(err error) {
log.Error(err)
})
if err != nil {
return nil, nil, err
}

View file

@ -32,6 +32,7 @@ type IqVcardTemp struct {
N IqVcardN
Tel IqVcardTel
Photo IqVcardPhoto
ResultSet *stanza.ResultSet `xml:"set,omitempty"`
}
// IqVcardFn is vCard/FN
@ -118,6 +119,11 @@ func (c IqVcardTemp) Namespace() string {
return c.XMLName.Space
}
// Namespace is a namespace!
func (c IqVcardTemp) GetSet() *stanza.ResultSet {
return c.ResultSet
}
func init() {
// presence nick
stanza.TypeRegistry.MapExtension(stanza.PKTPresence, xml.Name{

View file

@ -16,7 +16,7 @@ import (
var Queue = make(map[string]*stanza.Presence)
// Jid stores the component's JID object
var Jid *xmpp.Jid
var Jid *stanza.Jid
// SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, component *xmpp.Component) {

View file

@ -24,7 +24,7 @@ func logPacketType(p stanza.Packet) {
// HandleIq processes an incoming XMPP iq
func HandleIq(s xmpp.Sender, p stanza.Packet) {
iq, ok := p.(stanza.IQ)
iq, ok := p.(*stanza.IQ)
if !ok {
logPacketType(p)
return
@ -60,7 +60,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
}).Warn("Message")
log.Debugf("%#v", msg)
fromJid, err := xmpp.NewJid(msg.From)
fromJid, err := stanza.NewJid(msg.From)
if err != nil {
log.Error("Invalid from JID!")
return
@ -155,7 +155,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
log.Debugf("%#v", p)
// create session
fromJid, err := xmpp.NewJid(p.From)
fromJid, err := stanza.NewJid(p.From)
if err != nil {
log.Error("Invalid from JID!")
return
@ -196,13 +196,13 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
}
}
func handleGetVcardTempIq(s xmpp.Sender, iq stanza.IQ) {
func handleGetVcardTempIq(s xmpp.Sender, iq *stanza.IQ) {
log.WithFields(log.Fields{
"from": iq.From,
"to": iq.To,
}).Warn("VCard request")
fromJid, err := xmpp.NewJid(iq.From)
fromJid, err := stanza.NewJid(iq.From)
if err != nil {
log.Error("Invalid from JID!")
return
@ -274,5 +274,5 @@ func handleGetVcardTempIq(s xmpp.Sender, iq stanza.IQ) {
return
}
_ = gateway.ResumableSend(component, answer)
_ = gateway.ResumableSend(component, &answer)
}