aboutsummaryrefslogtreecommitdiff
path: root/telegram/handlers.go
diff options
context:
space:
mode:
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r--telegram/handlers.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go
index 5ff5dad..d2d5b61 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -2,6 +2,7 @@ package telegram
import (
"strconv"
+ "strings"
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
@@ -38,6 +39,12 @@ func (c *Client) updateHandler() {
uhOh()
}
c.updateNewChat(typedUpdate)
+ case client.TypeUpdateNewMessage:
+ typedUpdate, ok := update.(*client.UpdateNewMessage)
+ if !ok {
+ uhOh()
+ }
+ c.updateNewMessage(typedUpdate)
default:
// log only handled types
continue
@@ -97,3 +104,54 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
c.processStatusUpdate(int32(update.Chat.Id), update.Chat.Title, "chat")
}
}
+
+func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
+ // ignore self outgoing messages
+ if update.Message.IsOutgoing &&
+ update.Message.SendingState != nil &&
+ update.Message.SendingState.MessageSendingStateType() == client.TypeMessageSendingStatePending {
+ return
+ }
+
+ log.WithFields(log.Fields{
+ "chat_id": update.Message.ChatId,
+ }).Warn("New message from chat")
+
+ text := c.messageToText(update.Message)
+ file, filename := c.contentToFilename(update.Message.Content)
+
+ // download file(s)
+ if file != nil && !file.Local.IsDownloadingCompleted {
+ c.client.DownloadFile(&client.DownloadFileRequest{
+ FileId: file.Id,
+ Priority: 32,
+ Synchronous: true,
+ })
+ }
+ // OTR support (I do not know why would you need it, seriously)
+ if !strings.HasPrefix(text, "?OTR") {
+ var prefix strings.Builder
+ prefix.WriteString(c.messageToPrefix(update.Message, c.formatContent(file, filename)))
+ if text != "" {
+ // \n if it is groupchat and message is not empty
+ if update.Message.ChatId < 0 {
+ prefix.WriteString("\n")
+ } else if update.Message.ChatId > 0 {
+ prefix.WriteString(" | ")
+ }
+
+ prefix.WriteString(text)
+ }
+
+ text = prefix.String()
+ }
+
+ // mark message as read
+ c.client.ViewMessages(&client.ViewMessagesRequest{
+ ChatId: update.Message.ChatId,
+ MessageIds: []int64{update.Message.Id},
+ ForceRead: true,
+ })
+ // forward message to XMPP
+ gateway.SendMessage(c.jid, strconv.Itoa(int(update.Message.ChatId)), text, c.xmpp)
+}