aboutsummaryrefslogtreecommitdiff
path: root/telegram/handlers.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-04 01:17:36 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-04 01:17:36 +0300
commit8949290c529850e28000945550566fbd96c259cf (patch)
tree18160339b84e44305593947874c9a93d6f7b8640 /telegram/handlers.go
parent0c0c8e777ac52def4c867eaf4fddc626b08a1171 (diff)
Handle downloaded files
Diffstat (limited to 'telegram/handlers.go')
-rw-r--r--telegram/handlers.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/telegram/handlers.go b/telegram/handlers.go
index a2e45f7..5c421bb 100644
--- a/telegram/handlers.go
+++ b/telegram/handlers.go
@@ -1,7 +1,10 @@
package telegram
import (
+ "crypto/sha256"
"fmt"
+ "os"
+ "path/filepath"
"strconv"
"strings"
"sync"
@@ -75,6 +78,12 @@ func (c *Client) updateHandler() {
uhOh()
}
c.updateDeleteMessages(typedUpdate)
+ case client.TypeUpdateFile:
+ typedUpdate, ok := update.(*client.UpdateFile)
+ if !ok {
+ uhOh()
+ }
+ c.updateFile(typedUpdate)
default:
// log only handled types
continue
@@ -85,17 +94,20 @@ func (c *Client) updateHandler() {
}
}
+// new user discovered
func (c *Client) updateUser(update *client.UpdateUser) {
cache.users[update.User.Id] = update.User
show, status := userStatusToText(update.User.Status)
c.processStatusUpdate(update.User.Id, status, show)
}
+// user status changed
func (c *Client) updateUserStatus(update *client.UpdateUserStatus) {
show, status := userStatusToText(update.Status)
c.processStatusUpdate(update.UserId, status, show, gateway.SPImmed(false))
}
+// new chat discovered
func (c *Client) updateNewChat(update *client.UpdateNewChat) {
if update.Chat != nil && update.Chat.Photo != nil && update.Chat.Photo.Small != nil {
_, err := c.client.DownloadFile(&client.DownloadFileRequest{
@@ -135,6 +147,7 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
}
}
+// message received
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
// ignore self outgoing messages
if update.Message.IsOutgoing &&
@@ -186,6 +199,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
gateway.SendMessage(c.jid, strconv.Itoa(int(update.Message.ChatId)), text, c.xmpp)
}
+// message content updated
func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
if update.NewContent.MessageContentType() == client.TypeMessageText {
textContent := update.NewContent.(*client.MessageText)
@@ -194,9 +208,31 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
}
}
+// message(s) deleted
func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
if update.IsPermanent {
text := "✗ " + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
}
}
+
+// file downloaded
+func (c *Client) updateFile(update *client.UpdateFile) {
+ // not really
+ if !update.File.Local.IsDownloadingCompleted {
+ return
+ }
+
+ err := os.Symlink(
+ update.File.Local.Path,
+ fmt.Sprintf(
+ "%s/%s%s",
+ c.content.Path,
+ fmt.Sprintf("%x", sha256.Sum256([]byte(update.File.Remote.Id))),
+ filepath.Ext(update.File.Local.Path),
+ ),
+ )
+ if err != nil {
+ log.Errorf("Error creating symlink: %v", err)
+ }
+}