Bohdan Horbeshko 1 year ago
parent 68e3581724
commit b3b53b6145

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

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

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

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

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

Loading…
Cancel
Save