package telegram import ( "crypto/sha1" "crypto/sha256" "fmt" "github.com/pkg/errors" "io" "os" "path/filepath" "regexp" "strconv" "strings" "time" "dev.narayana.im/narayana/telegabber/telegram/cache" "dev.narayana.im/narayana/telegabber/telegram/formatter" "dev.narayana.im/narayana/telegabber/xmpp/gateway" log "github.com/sirupsen/logrus" "github.com/soheilhy/args" "github.com/zelenin/go-tdlib/client" ) var errOffline = errors.New("TDlib instance is offline") var spaceRegex = regexp.MustCompile(`\s+`) var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n") const newlineChar string = "\n" // GetContactByUsername resolves username to user id retrieves user and chat information func (c *Client) GetContactByUsername(username string) (*client.Chat, *client.User, error) { if !c.Online() { return nil, nil, errOffline } var chat *client.Chat var err error var userID int64 if strings.HasPrefix(username, "@") { chat, err = c.client.SearchPublicChat(&client.SearchPublicChatRequest{ Username: username, }) if err != nil { return nil, nil, err } userID = chat.Id } else { userID, err = strconv.ParseInt(username, 10, 64) if err != nil { return nil, nil, err } } return c.GetContactByID(userID, chat) } // GetContactByID gets user and chat information from cache (or tries to retrieve it, if missing) func (c *Client) GetContactByID(id int64, chat *client.Chat) (*client.Chat, *client.User, error) { if !c.Online() || id == 0 { return nil, nil, errOffline } var user *client.User var cacheChat *client.Chat var ok bool var err error user, ok = c.cache.GetUser(id) if !ok && id > 0 { user, err = c.client.GetUser(&client.GetUserRequest{ UserId: id, }) if err == nil { c.cache.SetUser(id, user) } } cacheChat, ok = c.cache.GetChat(id) if !ok { if chat == nil { cacheChat, err = c.client.GetChat(&client.GetChatRequest{ ChatId: id, }) if err != nil { // error is irrelevant if the user was found successfully if user != nil { return nil, user, nil } return nil, nil, err } c.cache.SetChat(id, cacheChat) } else { c.cache.SetChat(id, chat) } } if chat == nil { chat = cacheChat } return chat, user, nil } func (c *Client) userStatusToText(status client.UserStatus, chatID int64) (string, string, string) { var show, textStatus, presenceType string switch status.UserStatusType() { case client.TypeUserStatusOnline: onlineStatus, _ := status.(*client.UserStatusOnline) c.DelayedStatusesLock.Lock() c.DelayedStatuses[chatID] = &DelayedStatus{ TimestampOnline: time.Now().Unix(), TimestampExpired: int64(onlineStatus.Expires), } c.DelayedStatusesLock.Unlock() textStatus = "Online" case client.TypeUserStatusRecently: show, textStatus = "dnd", "Last seen recently" c.DelayedStatusesLock.Lock() delete(c.DelayedStatuses, chatID) c.DelayedStatusesLock.Unlock() case client.TypeUserStatusLastWeek: show, textStatus = "xa", "Last seen last week" case client.TypeUserStatusLastMonth: show, textStatus = "xa", "Last seen last month" case client.TypeUserStatusEmpty: presenceType, textStatus = "unavailable", "Last seen a long time ago" case client.TypeUserStatusOffline: offlineStatus, _ := status.(*client.UserStatusOffline) // this will stop working in 2038 O\ wasOnline := int64(offlineStatus.WasOnline) elapsed := time.Now().Unix() - wasOnline if elapsed < 3600 { show = "away" } else { show = "xa" } textStatus = c.LastSeenStatus(wasOnline) c.DelayedStatusesLock.Lock() delete(c.DelayedStatuses, chatID) c.DelayedStatusesLock.Unlock() } return show, textStatus, presenceType } // LastSeenStatus formats a timestamp to a "Last seen at" string func (c *Client) LastSeenStatus(timestamp int64) string { return time.Unix(int64(timestamp), 0). In(c.Session.TimezoneToLocation()). Format("Last seen at 15:04 02/01/2006") } // ProcessStatusUpdate sets contact status func (c *Client) ProcessStatusUpdate(chatID int64, status string, show string, oldArgs ...args.V) error { if !c.Online() { return nil } log.WithFields(log.Fields{ "chat_id": chatID, }).Info("Status update for") chat, user, err := c.GetContactByID(chatID, nil) if err != nil { return err } var photo string if chat != nil && chat.Photo != nil { path := chat.Photo.Small.Local.Path file, err := os.Open(path) if err == nil { defer file.Close() hash := sha1.New() _, err = io.Copy(hash, file) if err == nil { photo = fmt.Sprintf("%x", hash.Sum(nil)) } else { log.Errorf("Error calculating hash: %v", path) } } else if path != "" { log.Errorf("Photo does not exist: %v", path) } } var presenceType string if gateway.SPType.IsSet(oldArgs) { presenceType = gateway.SPType.Get(oldArgs) } cachedStatus, ok := c.cache.GetStatus(chatID) if status == "" { if ok { show, status = cachedStatus.XMPP, cachedStatus.Description } else if user != nil && user.Status != nil { show, status, presenceType = c.userStatusToText(user.Status, chatID) } else { show, status = "chat", chat.Title } } c.cache.SetStatus(chatID, show, status) newArgs := []args.V { gateway.SPFrom(strconv.FormatInt(chatID, 10)), gateway.SPShow(show), gateway.SPStatus(status), gateway.SPPhoto(photo), gateway.SPResource(c.resource), gateway.SPImmed(gateway.SPImmed.Get(oldArgs)), } if presenceType != "" { newArgs = append(newArgs, gateway.SPType(presenceType)) } return gateway.SendPresence( c.xmpp, c.jid, newArgs..., ) } func (c *Client) formatContact(chatID int64) string { if chatID == 0 { return "" } chat, user, err := c.GetContactByID(chatID, nil) if err != nil { return "unknown contact: " + err.Error() } var str string if chat != nil { str = fmt.Sprintf("%s (%v)", chat.Title, chat.Id) } else if user != nil { username := user.Username if username == "" { username = strconv.FormatInt(user.Id, 10) } str = fmt.Sprintf("%s %s (%v)", user.FirstName, user.LastName, username) } else { str = strconv.FormatInt(chatID, 10) } str = spaceRegex.ReplaceAllString(str, " ") return str } func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, message *client.Message) string { var err error if message == nil { message, err = c.client.GetMessage(&client.GetMessageRequest{ ChatId: chatID, MessageId: messageID, }) if err != nil { return fmt.Sprintf("", err.Error()) } } if message == nil { return "" } var str strings.Builder // add messageid and sender var senderId int64 if message.SenderId != nil { switch message.SenderId.MessageSenderType() { case client.TypeMessageSenderUser: senderUser, _ := message.SenderId.(*client.MessageSenderUser) senderId = senderUser.UserId case client.TypeMessageSenderChat: senderChat, _ := message.SenderId.(*client.MessageSenderChat) senderId = senderChat.ChatId } } str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(senderId))) // add date if !preview { str.WriteString( time.Unix(int64(message.Date), 0). In(c.Session.TimezoneToLocation()). Format("02 Jan 2006 15:04:05 | "), ) } // text message var text string if message.Content != nil { text = c.messageToText(message, preview) } if text != "" { if !preview { str.WriteString(text) } else { newlinePos := strings.Index(text, newlineChar) if newlinePos == -1 { str.WriteString(text) } else { str.WriteString(text[0:newlinePos]) } } } return str.String() } func (c *Client) formatForward(fwd *client.MessageForwardInfo) string { switch fwd.Origin.MessageForwardOriginType() { case client.TypeMessageForwardOriginUser: originUser := fwd.Origin.(*client.MessageForwardOriginUser) return c.formatContact(originUser.SenderUserId) case client.TypeMessageForwardOriginChat: originChat := fwd.Origin.(*client.MessageForwardOriginChat) var signature string if originChat.AuthorSignature != "" { signature = fmt.Sprintf(" (%s)", originChat.AuthorSignature) } return c.formatContact(originChat.SenderChatId)+signature case client.TypeMessageForwardOriginHiddenUser: originUser := fwd.Origin.(*client.MessageForwardOriginHiddenUser) return originUser.SenderName case client.TypeMessageForwardOriginChannel: channel := fwd.Origin.(*client.MessageForwardOriginChannel) var signature string if channel.AuthorSignature != "" { signature = fmt.Sprintf(" (%s)", channel.AuthorSignature) } return c.formatContact(channel.ChatId)+signature case client.TypeMessageForwardOriginMessageImport: originImport := fwd.Origin.(*client.MessageForwardOriginMessageImport) return originImport.SenderName } return "Unknown forward type" } func (c *Client) formatContent(file *client.File, filename string) string { if file == nil { return "" } return fmt.Sprintf( "%s (%v kbytes) | %s", filename, file.Size/1024, c.formatFilePath(c.content.Link, file.Remote.UniqueId, filepath.Ext(file.Local.Path)), ) } func (c *Client) formatFilePath(basedir string, id string, ext string) string { return fmt.Sprintf("%s/%x%s", basedir, sha256.Sum256([]byte(id)), ext) } func (c *Client) formatBantime(hours int64) int32 { var until int32 if hours > 0 { until = int32(time.Now().Unix() + hours*3600) } return until } func (c *Client) formatLocation(location *client.Location) string { return fmt.Sprintf( "coordinates: %v,%v | https://www.google.com/maps/search/%v,%v/", location.Latitude, location.Longitude, location.Latitude, location.Longitude, ) } func (c *Client) messageToText(message *client.Message, preview bool) string { if message.Content == nil { log.Warnf("Unknown message: %#v", message) return "" } markupFunction := formatter.EntityToXEP0393 switch message.Content.MessageContentType() { case client.TypeMessageSticker: sticker, _ := message.Content.(*client.MessageSticker) return sticker.Sticker.Emoji case client.TypeMessageAnimatedEmoji: animatedEmoji, _ := message.Content.(*client.MessageAnimatedEmoji) return animatedEmoji.Emoji case client.TypeMessageBasicGroupChatCreate, client.TypeMessageSupergroupChatCreate: return "has created chat" case client.TypeMessageChatJoinByLink: return "joined chat via invite link" case client.TypeMessageChatAddMembers: addMembers, _ := message.Content.(*client.MessageChatAddMembers) text := "invited " if len(addMembers.MemberUserIds) > 0 { text += c.formatContact(addMembers.MemberUserIds[0]) } return text case client.TypeMessageChatDeleteMember: deleteMember, _ := message.Content.(*client.MessageChatDeleteMember) return "kicked " + c.formatContact(deleteMember.UserId) case client.TypeMessagePinMessage: pinMessage, _ := message.Content.(*client.MessagePinMessage) return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, preview, nil) case client.TypeMessageChatChangeTitle: changeTitle, _ := message.Content.(*client.MessageChatChangeTitle) return "chat title set to: " + changeTitle.Title case client.TypeMessageLocation: location, _ := message.Content.(*client.MessageLocation) return c.formatLocation(location.Location) case client.TypeMessageVenue: venue, _ := message.Content.(*client.MessageVenue) if preview { return venue.Venue.Title } else { return fmt.Sprintf( "*%s*\n%s\n%s", venue.Venue.Title, venue.Venue.Address, c.formatLocation(venue.Venue.Location), ) } case client.TypeMessagePhoto: photo, _ := message.Content.(*client.MessagePhoto) if preview { return photo.Caption.Text } else { return formatter.Format( photo.Caption.Text, formatter.SortEntities(photo.Caption.Entities), markupFunction, ) } case client.TypeMessageAudio: audio, _ := message.Content.(*client.MessageAudio) if preview { return audio.Caption.Text } else { return formatter.Format( audio.Caption.Text, formatter.SortEntities(audio.Caption.Entities), markupFunction, ) } case client.TypeMessageVideo: video, _ := message.Content.(*client.MessageVideo) if preview { return video.Caption.Text } else { return formatter.Format( video.Caption.Text, formatter.SortEntities(video.Caption.Entities), markupFunction, ) } case client.TypeMessageDocument: document, _ := message.Content.(*client.MessageDocument) if preview { return document.Caption.Text } else { return formatter.Format( document.Caption.Text, formatter.SortEntities(document.Caption.Entities), markupFunction, ) } case client.TypeMessageText: text, _ := message.Content.(*client.MessageText) if preview { return text.Text.Text } else { return formatter.Format( text.Text.Text, formatter.SortEntities(text.Text.Entities), markupFunction, ) } case client.TypeMessageVoiceNote: voice, _ := message.Content.(*client.MessageVoiceNote) if preview { return voice.Caption.Text } else { return formatter.Format( voice.Caption.Text, formatter.SortEntities(voice.Caption.Entities), markupFunction, ) } case client.TypeMessageVideoNote: return "" case client.TypeMessageAnimation: animation, _ := message.Content.(*client.MessageAnimation) if preview { return animation.Caption.Text } else { return formatter.Format( animation.Caption.Text, formatter.SortEntities(animation.Caption.Entities), markupFunction, ) } case client.TypeMessageContact: contact, _ := message.Content.(*client.MessageContact) if preview { return contact.Contact.FirstName + " " + contact.Contact.LastName } else { var jid string if contact.Contact.UserId != 0 { jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare()) } return fmt.Sprintf( "*%s %s*\n%s\n%s\n%s", contact.Contact.FirstName, contact.Contact.LastName, contact.Contact.PhoneNumber, contact.Contact.Vcard, jid, ) } case client.TypeMessageDice: dice, _ := message.Content.(*client.MessageDice) return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value) case client.TypeMessagePoll: poll, _ := message.Content.(*client.MessagePoll) if preview { return poll.Poll.Question } else { rows := []string{} rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question)) for _, option := range poll.Poll.Options { var tick string if option.IsChosen { tick = "x" } else { tick = " " } rows = append(rows, fmt.Sprintf( "[%s] %s | %v%% | %v vote", tick, option.Text, option.VotePercentage, option.VoterCount, )) } return strings.Join(rows, "\n") } } return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType()) } func (c *Client) contentToFilename(content client.MessageContent) (*client.File, string) { if content == nil { return nil, "" } switch content.MessageContentType() { case client.TypeMessageSticker: sticker, _ := content.(*client.MessageSticker) return sticker.Sticker.Sticker, "sticker.webp" case client.TypeMessageVoiceNote: voice, _ := content.(*client.MessageVoiceNote) return voice.VoiceNote.Voice, fmt.Sprintf("voice note (%v s.).oga", voice.VoiceNote.Duration) case client.TypeMessageVideoNote: video, _ := content.(*client.MessageVideoNote) return video.VideoNote.Video, fmt.Sprintf("video note (%v s.).mp4", video.VideoNote.Duration) case client.TypeMessageAnimation: animation, _ := content.(*client.MessageAnimation) return animation.Animation.Animation, "animation.mp4" case client.TypeMessagePhoto: photo, _ := content.(*client.MessagePhoto) sizes := photo.Photo.Sizes if len(sizes) >= 1 { file := sizes[len(sizes)-1].Photo return file, strconv.FormatInt(int64(file.Id), 10) + ".jpg" } return nil, "" case client.TypeMessageAudio: audio, _ := content.(*client.MessageAudio) return audio.Audio.Audio, filepath.Base(audio.Audio.Audio.Local.Path) case client.TypeMessageVideo: video, _ := content.(*client.MessageVideo) return video.Video.Video, filepath.Base(video.Video.Video.Local.Path) case client.TypeMessageDocument: document, _ := content.(*client.MessageDocument) return document.Document.Document, filepath.Base(document.Document.Document.Local.Path) } return nil, "" } func (c *Client) messageToPrefix(message *client.Message, fileString string) string { prefix := []string{} // message direction var directionChar string if message.IsOutgoing { directionChar = "⇾ " } else { directionChar = "⇽ " } prefix = append(prefix, directionChar+strconv.FormatInt(message.Id, 10)) // show sender in group chats if message.ChatId < 0 && message.SenderId != nil { var senderId int64 switch message.SenderId.MessageSenderType() { case client.TypeMessageSenderUser: senderUser, _ := message.SenderId.(*client.MessageSenderUser) senderId = senderUser.UserId case client.TypeMessageSenderChat: senderChat, _ := message.SenderId.(*client.MessageSenderChat) senderId = senderChat.ChatId } prefix = append(prefix, c.formatContact(senderId)) } // reply to if message.ReplyToMessageId != 0 { prefix = append(prefix, "reply: "+c.formatMessage(message.ChatId, message.ReplyToMessageId, true, nil)) } if message.ForwardInfo != nil { prefix = append(prefix, "fwd: "+c.formatForward(message.ForwardInfo)) } // file if fileString != "" { prefix = append(prefix, "file: "+fileString) } return strings.Join(prefix, " | ") } // ProcessOutgoingMessage executes commands or sends messages to mapped chats func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string) client.InputMessageContent { if !c.Online() { // we're offline return nil } if returnJid != "" && strings.HasPrefix(text, "/") { // try to execute commands response, isCommand := c.ProcessChatCommand(chatID, text) if response != "" { gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp) } // do not send on success if isCommand { return nil } } log.Warnf("Sending message to chat %v", chatID) // quotations var reply int64 replySlice := replyRegex.FindStringSubmatch(text) if len(replySlice) > 1 { reply, _ = strconv.ParseInt(replySlice[1], 10, 64) } // attach a file var file *client.InputFileRemote if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { file = &client.InputFileRemote{ Id: text, } } // remove first line from text if file != nil || reply != 0 { newlinePos := strings.Index(text, newlineChar) if newlinePos != -1 { text = text[newlinePos+1:] } } formattedText := &client.FormattedText{ Text: text, } var message client.InputMessageContent if file != nil { // we can try to send a document message = &client.InputMessageDocument{ Document: file, Caption: formattedText, } } else { // compile our message message = &client.InputMessageText{ Text: formattedText, } } if chatID != 0 { _, err := c.client.SendMessage(&client.SendMessageRequest{ ChatId: chatID, ReplyToMessageId: reply, InputMessageContent: message, }) if err != nil { gateway.SendMessage( returnJid, strconv.FormatInt(chatID, 10), fmt.Sprintf("Not sent: %s", err.Error()), c.xmpp, ) } return nil } else { return message } } // StatusesRange proxies the following function from unexported cache func (c *Client) StatusesRange() chan *cache.Status { return c.cache.StatusesRange() } func (c *Client) addResource(resource string) { if resource == "" { return } c.locks.resourcesLock.Lock() defer c.locks.resourcesLock.Unlock() c.resources[resource] = true } func (c *Client) deleteResource(resource string) { c.locks.resourcesLock.Lock() defer c.locks.resourcesLock.Unlock() if _, ok := c.resources[resource]; ok { delete(c.resources, resource) } } // resend statuses to (to another resource, for example) func (c *Client) roster(resource string) { if _, ok := c.resources[resource]; ok { return // we know it } log.Warnf("Sending roster for %v", resource) for _, chat := range c.cache.ChatsKeys() { c.ProcessStatusUpdate(chat, "", "") } gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login)) c.addResource(resource) } // get last messages from specified chat func (c *Client) getLastMessages(id int64, query string, from int64, count int32) (*client.Messages, error) { return c.client.SearchChatMessages(&client.SearchChatMessagesRequest{ ChatId: id, Query: query, SenderId: &client.MessageSenderUser{UserId: from}, Filter: &client.SearchMessagesFilterEmpty{}, Limit: count, }) } // DownloadFile actually obtains a file by id given by TDlib func (c *Client) DownloadFile(id int32, priority int32, synchronous bool) (*client.File, error) { return c.client.DownloadFile(&client.DownloadFileRequest{ FileId: id, Priority: priority, Synchronous: synchronous, }) } // subscribe to a Telegram ID func (c *Client) subscribeToID(id int64, chat *client.Chat) { var args []args.V args = append(args, gateway.SPFrom(strconv.FormatInt(id, 10))) args = append(args, gateway.SPType("subscribe")) if chat == nil { chat, _, _ = c.GetContactByID(id, nil) } if chat != nil { args = append(args, gateway.SPNickname(chat.Title)) } gateway.SendPresence( c.xmpp, c.jid, args..., ) }