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) + } +}