aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2023-01-16 04:35:13 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2023-01-16 04:35:13 +0300
commitb3b53b6145c4b8e31d134942507c3ce0226182a4 (patch)
tree4f0753727317859504c26dd5bd4f338991db9082
parent68e3581724f04366c114f267b3264075bc635061 (diff)
OOB
-rw-r--r--persistence/sessions.go11
-rw-r--r--persistence/sessions_test.go2
-rw-r--r--telegram/utils.go25
-rw-r--r--telegram/utils_test.go10
-rw-r--r--xmpp/gateway/gateway.go15
5 files changed, 52 insertions, 11 deletions
diff --git a/persistence/sessions.go b/persistence/sessions.go
index bc71fd6..ed95f60 100644
--- a/persistence/sessions.go
+++ b/persistence/sessions.go
@@ -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")
diff --git a/persistence/sessions_test.go b/persistence/sessions_test.go
index 76f71f9..c8e3077 100644
--- a/persistence/sessions_test.go
+++ b/persistence/sessions_test.go
@@ -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)
diff --git a/telegram/utils.go b/telegram/utils.go
index 17e9861..1545939 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -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
diff --git a/telegram/utils_test.go b/telegram/utils_test.go
index f0140ae..bfd6c49 100644
--- a/telegram/utils_test.go
+++ b/telegram/utils_test.go
@@ -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) {
diff --git a/xmpp/gateway/gateway.go b/xmpp/gateway/gateway.go
index d4620e6..f4ec690 100644
--- a/xmpp/gateway/gateway.go
+++ b/xmpp/gateway/gateway.go
@@ -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)
}