From e30f973e3c695c652d80d3b01c760d3da71b8542 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 5 Jun 2018 18:07:03 +1200 Subject: [PATCH] transform telegram quoted-messages into DC-style inline replies --- NTFServer.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/NTFServer.go b/NTFServer.go index a912c29..1edc1ec 100644 --- a/NTFServer.go +++ b/NTFServer.go @@ -476,15 +476,37 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error { } else { // Actual chat message + sendMsg := update.Message.Text + + // Was it a reply to another message? If so, format it as " OrigMessage // update text" + if update.Message.ReplyToMessage != nil && len(update.Message.ReplyToMessage.Text) > 0 && update.Message.ReplyToMessage.From != nil { + origSenderId := int64(update.Message.ReplyToMessage.From.ID) + + if origSenderNick, ok := this.config.KnownUsers[origSenderId]; ok { + // Quoted messages from another hub user + sendMsg = fmt.Sprintf(`"<%s> %s" // %s`, origSenderNick, update.Message.ReplyToMessage.Text, sendMsg) + + } else if origSenderId == int64(this.bot.Self.ID) { + // Quoted messages from the hublink bot + sendMsg = fmt.Sprintf(`"%s" // %s`, update.Message.ReplyToMessage.Text, sendMsg) // the username is already present in the plaintext + + } else { + // No idea what was quoted. But it really was quoting something + // Paste it as-is but with some info in the logs for future cleanup + log.Printf("Quoting message '%#v' with unknown source", update.Message.ReplyToMessage) + sendMsg = fmt.Sprintf(`"%s" // %s`, update.Message.ReplyToMessage.Text, sendMsg) + } + + } // Also add it to the coalesce buffer so that we don't replay it from someone else's NMDC connection - this.Coalesce(hubNick, update.Message.Text) + this.Coalesce(hubNick, sendMsg) // Submit to NMDC - err := conn.SayPublic(update.Message.Text) + err := conn.SayPublic(sendMsg) if err != nil { - log.Printf("Failed to deliver message '%s': %s", update.Message.Text, err.Error()) - this.GroupChatSayHTML(fmt.Sprintf("Couldn't sync message '%s' because: %s", html.EscapeString(update.Message.Text), html.EscapeString(err.Error()))) + log.Printf("Failed to deliver message '%s': %s", sendMsg, err.Error()) + this.GroupChatSayHTML(fmt.Sprintf("Couldn't sync message '%s' because: %s", html.EscapeString(sendMsg), html.EscapeString(err.Error()))) } } }