From b78779dad0c78479654941ecb43a3d0a30e83689 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sun, 15 May 2022 12:59:54 -0400 Subject: [PATCH] Re-upload files to Telegram instead of exposing a XEP-0363 link --- telegram/handlers.go | 38 +++++++++++++++++++++++++ telegram/utils.go | 66 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/telegram/handlers.go b/telegram/handlers.go index 45f10fd..9b86ac7 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -2,6 +2,8 @@ package telegram import ( "fmt" + "os" + "path/filepath" "strconv" "strings" "sync" @@ -43,6 +45,16 @@ func (c *Client) getChatMessageLock(chatID int64) *sync.Mutex { return lock } +func (c *Client) cleanTempFile(path string) { + os.Remove(path) + + dir := filepath.Dir(path) + dirName := filepath.Base(dir) + if strings.HasPrefix(dirName, "telegabber-") { + os.Remove(dir) + } +} + func (c *Client) updateHandler() { listener := c.client.GetListener() defer listener.Close() @@ -111,6 +123,18 @@ func (c *Client) updateHandler() { uhOh() } c.updateAuthorizationState(typedUpdate) + case client.TypeUpdateMessageSendSucceeded: + typedUpdate, ok := update.(*client.UpdateMessageSendSucceeded) + if !ok { + uhOh() + } + c.updateMessageSendSucceeded(typedUpdate) + case client.TypeUpdateMessageSendFailed: + typedUpdate, ok := update.(*client.UpdateMessageSendFailed) + if !ok { + uhOh() + } + c.updateMessageSendFailed(typedUpdate) default: // log only handled types continue @@ -233,3 +257,17 @@ func (c *Client) updateAuthorizationState(update *client.UpdateAuthorizationStat c.forceClose() } } + +// clean uploaded files +func (c *Client) updateMessageSendSucceeded(update *client.UpdateMessageSendSucceeded) { + file, _ := c.contentToFile(update.Message.Content) + if file != nil && file.Local != nil { + c.cleanTempFile(file.Local.Path) + } +} +func (c *Client) updateMessageSendFailed(update *client.UpdateMessageSendFailed) { + file, _ := c.contentToFile(update.Message.Content) + if file != nil && file.Local != nil { + c.cleanTempFile(file.Local.Path) + } +} diff --git a/telegram/utils.go b/telegram/utils.go index f1f7f52..1248a54 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -5,6 +5,8 @@ import ( "fmt" "github.com/pkg/errors" "io" + "io/ioutil" + "net/http" "os" osUser "os/user" "path/filepath" @@ -832,10 +834,66 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str } // attach a file - var file *client.InputFileRemote + var file *client.InputFileLocal if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { - file = &client.InputFileRemote{ - Id: text, + response, err := http.Get(text) + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()), + c.xmpp, + ) + return nil + } + if response != nil && response.Body != nil { + defer response.Body.Close() + + if response.StatusCode != 200 { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Received status code %v", response.StatusCode), + c.xmpp, + ) + return nil + } + + tempDir, err := ioutil.TempDir("", "telegabber-*") + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()), + c.xmpp, + ) + return nil + } + tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text))) + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to create a temporary file: %s", err.Error()), + c.xmpp, + ) + return nil + } + + _, err = io.Copy(tempFile, response.Body) + if err != nil { + gateway.SendMessage( + returnJid, + strconv.FormatInt(chatID, 10), + fmt.Sprintf("Failed to write a temporary file: %s", err.Error()), + c.xmpp, + ) + return nil + } + + file = &client.InputFileLocal{ + Path: tempFile.Name(), + } } } @@ -844,6 +902,8 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str newlinePos := strings.Index(text, newlineChar) if newlinePos != -1 { text = text[newlinePos+1:] + } else { + text = "" } }