Skip to content

Commit

Permalink
fix telegram sending links containing double underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Jan 27, 2024
1 parent afdb3ec commit eb32884
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

#pragma once

#define NCHAT_VERSION "4.35"
#define NCHAT_VERSION "4.36"
32 changes: 31 additions & 1 deletion lib/ncutil/src/strutil.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// strutil.cpp
//
// Copyright (c) 2020-2023 Kristofer Berggren
// Copyright (c) 2020-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
Expand Down Expand Up @@ -55,6 +55,36 @@ std::string StrUtil::Emojize(const std::string& p_Str, bool p_Pad /*= false*/)
return EmojiUtil::Emojize(p_Str, p_Pad);
}

std::string StrUtil::EscapeRawUrls(const std::string& p_Str)
{
std::string str = p_Str;
std::string rv;
std::regex rg("\\(?\\[?(http|https):\\/\\/([^\\s]+)");
std::smatch sm;
while (regex_search(str, sm, rg))
{
rv += sm.prefix().str();

std::string url = sm.str();
if (url.size() >= 2)
{
if ((url.front() == '(') || (url.front() == '['))
{
rv += url;
}
else
{
rv += "[" + url + "]";
}
}
str = sm.suffix();
}

rv += str;

return rv;
}

std::vector<std::string> StrUtil::ExtractUrlsFromStr(const std::string& p_Str)
{
std::string str = p_Str;
Expand Down
3 changes: 2 additions & 1 deletion lib/ncutil/src/strutil.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// strutil.h
//
// Copyright (c) 2020-2023 Kristofer Berggren
// Copyright (c) 2020-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
Expand All @@ -19,6 +19,7 @@ class StrUtil
static void DeleteToNextMatch(std::wstring& p_Str, int& p_Pos, int p_Offs, std::wstring p_Chars);
static void DeleteToPrevMatch(std::wstring& p_Str, int& p_Pos, int p_Offs, std::wstring p_Chars);
static std::string Emojize(const std::string& p_Str, bool p_Pad = false);
static std::string EscapeRawUrls(const std::string& p_Str);
static std::vector<std::string> ExtractUrlsFromStr(const std::string& p_Str);
static std::string GetPass();
static std::string GetPhoneNumber();
Expand Down
6 changes: 4 additions & 2 deletions lib/tgchat/src/tgchat.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tgchat.cpp
//
// Copyright (c) 2020-2023 Kristofer Berggren
// Copyright (c) 2020-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
Expand Down Expand Up @@ -2668,10 +2668,12 @@ td::td_api::object_ptr<td::td_api::formattedText> TgChat::Impl::GetFormattedText
static const int32_t markdownVersion = (m_Config.Get("markdown_version") == "1") ? 1 : 2;
if (markdownEnabled)
{
const std::string text = StrUtil::EscapeRawUrls(p_Text);

auto textParseMarkdown =
td::td_api::make_object<td::td_api::textParseModeMarkdown>(markdownVersion);
auto parseTextEntities =
td::td_api::make_object<td::td_api::parseTextEntities>(p_Text,
td::td_api::make_object<td::td_api::parseTextEntities>(text,
std::move(textParseMarkdown));
td::Client::Request parseRequest{ 1, std::move(parseTextEntities) };
auto parseResponse = td::Client::execute(std::move(parseRequest));
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NCHAT "1" "January 2024" "nchat v4.35" "User Commands"
.TH NCHAT "1" "January 2024" "nchat v4.36" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down

0 comments on commit eb32884

Please sign in to comment.