This commit is contained in:
Bohdan Horbeshko 2023-01-15 20:35:13 -05:00
parent 68e3581724
commit b3b53b6145
5 changed files with 52 additions and 11 deletions

View file

@ -39,6 +39,7 @@ type Session struct {
KeepOnline bool `yaml:":keeponline"`
RawMessages bool `yaml:":rawmessages"`
AsciiArrows bool `yaml:":asciiarrows"`
OOBMode bool `yaml:":oobmode"`
}
var configKeys = []string{
@ -46,6 +47,7 @@ var configKeys = []string{
"keeponline",
"rawmessages",
"asciiarrows",
"oobmode",
}
var sessionDB *SessionsYamlDB
@ -118,6 +120,8 @@ func (s *Session) Get(key string) (string, error) {
return fromBool(s.RawMessages), nil
case "asciiarrows":
return fromBool(s.AsciiArrows), nil
case "oobmode":
return fromBool(s.OOBMode), nil
}
return "", errors.New("Unknown session property")
@ -161,6 +165,13 @@ func (s *Session) Set(key string, value string) (string, error) {
}
s.AsciiArrows = b
return value, nil
case "oobmode":
b, err := toBool(value)
if err != nil {
return "", err
}
s.OOBMode = b
return value, nil
}
return "", errors.New("Unknown session property")

View file

@ -47,6 +47,7 @@ func TestSessionToMap(t *testing.T) {
session := Session{
Timezone: "klsf",
RawMessages: true,
OOBMode: true,
}
m := session.ToMap()
sample := map[string]string{
@ -54,6 +55,7 @@ func TestSessionToMap(t *testing.T) {
"keeponline": "false",
"rawmessages": "true",
"asciiarrows": "false",
"oobmode": "true",
}
if !reflect.DeepEqual(m, sample) {
t.Errorf("Map does not match the sample: %v", m)

View file

@ -350,10 +350,10 @@ func (c *Client) formatForward(fwd *client.MessageForwardInfo) string {
return "Unknown forward type"
}
func (c *Client) formatFile(file *client.File, compact bool) string {
func (c *Client) formatFile(file *client.File, compact bool) (string, string) {
log.Debugf("file: %#v", file)
if file == nil || file.Local == nil || file.Remote == nil {
return ""
return "", ""
}
gateway.StorageLock.Lock()
@ -367,7 +367,7 @@ func (c *Client) formatFile(file *client.File, compact bool) string {
_, err := os.Stat(src)
if err != nil {
log.Errorf("Cannot access source file: %v", err)
return ""
return "", ""
}
size64 := uint64(file.Size)
@ -385,7 +385,7 @@ func (c *Client) formatFile(file *client.File, compact bool) string {
log.Warn(err.Error())
} else {
log.Errorf("File moving error: %v", err)
return "<ERROR>"
return "<ERROR>", ""
}
}
gateway.CachedStorageSize += size64
@ -410,9 +410,9 @@ func (c *Client) formatFile(file *client.File, compact bool) string {
}
if compact {
return link
return link, link
} else {
return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link)
return fmt.Sprintf("%s (%v kbytes) | %s", filepath.Base(src), file.Size/1024, link), link
}
}
@ -749,7 +749,7 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File {
// ProcessIncomingMessage transfers a message to XMPP side and marks it as read on Telegram side
func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
var text string
var text, oob string
content := message.Content
if content != nil && content.MessageContentType() == client.TypeMessageChatChangePhoto {
chat, err := c.client.GetChat(&client.GetChatRequest{
@ -772,7 +772,10 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
preview = c.ensureDownloadFile(preview)
var prefix strings.Builder
prefix.WriteString(c.messageToPrefix(message, c.formatFile(preview, true), c.formatFile(file, false)))
previewName, _ := c.formatFile(preview, true)
fileName, link := c.formatFile(file, false)
prefix.WriteString(c.messageToPrefix(message, previewName, fileName))
oob = link
if text != "" {
// \n if it is groupchat and message is not empty
if chatId < 0 {
@ -786,6 +789,10 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
text = prefix.String()
}
if c.Session.OOBMode && oob != "" {
text = oob
}
}
// mark message as read
@ -795,7 +802,7 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
ForceRead: true,
})
// forward message to XMPP
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp)
gateway.SendMessageWithOOB(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp, oob)
}
// ProcessOutgoingMessage executes commands or sends messages to mapped chats

View file

@ -146,10 +146,13 @@ func TestFormatFile(t *testing.T) {
},
}
content := c.formatFile(&file, false)
content, link := c.formatFile(&file, false)
if content != ". (23 kbytes) | " {
t.Errorf("Wrong file label: %v", content)
}
if link != "" {
t.Errorf("Wrong file link: %v", link)
}
}
func TestFormatPreview(t *testing.T) {
@ -168,10 +171,13 @@ func TestFormatPreview(t *testing.T) {
},
}
content := c.formatFile(&file, true)
content, link := c.formatFile(&file, true)
if content != "" {
t.Errorf("Wrong preview label: %v", content)
}
if link != "" {
t.Errorf("Wrong preview link: %v", link)
}
}
func TestMessageToTextSticker(t *testing.T) {

View file

@ -28,6 +28,15 @@ var DirtySessions = false
// SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, component *xmpp.Component) {
sendMessageWrapper(to, from, body, component, "")
}
// SendMessageWithOOB creates and sends a message stanza with OOB URL
func SendMessageWithOOB(to string, from string, body string, component *xmpp.Component, oob string) {
sendMessageWrapper(to, from, body, component, oob)
}
func sendMessageWrapper(to string, from string, body string, component *xmpp.Component, oob string) {
componentJid := Jid.Full()
var logFrom string
@ -54,6 +63,12 @@ func SendMessage(to string, from string, body string, component *xmpp.Component)
Body: body,
}
if oob != "" {
message.Extensions = append(message.Extensions, stanza.OOB{
URL: oob,
})
}
sendMessage(&message, component)
}