2020-05-25 23:21:04 +02:00
|
|
|
#include "purple-info.h"
|
2020-05-09 14:07:21 +02:00
|
|
|
#include "config.h"
|
2020-05-26 20:06:09 +02:00
|
|
|
#include "format.h"
|
2020-05-22 14:48:17 +02:00
|
|
|
#include <algorithm>
|
2020-05-26 20:06:09 +02:00
|
|
|
#include <math.h>
|
2020-05-09 14:07:21 +02:00
|
|
|
|
2020-06-11 00:04:29 +02:00
|
|
|
static char chatNameComponent[] = "id";
|
2020-08-13 13:34:23 +02:00
|
|
|
static char joinStringKey[] = "link";
|
2020-06-11 00:04:29 +02:00
|
|
|
static char nameKey[] = "name";
|
|
|
|
static char typeKey[] = "type";
|
|
|
|
|
|
|
|
const char *getChatNameComponent()
|
|
|
|
{
|
|
|
|
return chatNameComponent;
|
|
|
|
}
|
2020-05-09 14:07:21 +02:00
|
|
|
|
|
|
|
GList *getChatJoinInfo()
|
|
|
|
{
|
2020-05-12 15:21:16 +02:00
|
|
|
// First entry is the internal chat name used to look up conversations
|
2020-05-09 14:07:21 +02:00
|
|
|
struct proto_chat_entry *pce;
|
|
|
|
pce = g_new0 (struct proto_chat_entry, 1);
|
2020-05-22 14:48:17 +02:00
|
|
|
pce->label = _("Chat ID (don't change):");
|
2020-06-11 00:04:29 +02:00
|
|
|
pce->identifier = chatNameComponent;
|
2020-05-09 14:07:21 +02:00
|
|
|
pce->required = FALSE;
|
|
|
|
GList *info = g_list_append (NULL, pce);
|
|
|
|
|
|
|
|
pce = g_new0 (struct proto_chat_entry, 1);
|
2020-08-13 13:34:23 +02:00
|
|
|
pce->label = _("Join URL or name (empty if creating new):");
|
|
|
|
pce->identifier = joinStringKey;
|
2020-05-09 14:07:21 +02:00
|
|
|
pce->required = FALSE;
|
|
|
|
info = g_list_append (info, pce);
|
|
|
|
|
|
|
|
pce = g_new0 (struct proto_chat_entry, 1);
|
2020-05-22 14:48:17 +02:00
|
|
|
pce->label = _("Group name (if creating a group):");
|
|
|
|
pce->identifier = nameKey;
|
2020-05-09 14:07:21 +02:00
|
|
|
pce->required = FALSE;
|
|
|
|
info = g_list_append (info, pce);
|
|
|
|
|
2020-05-22 14:48:17 +02:00
|
|
|
pce = g_new0 (struct proto_chat_entry, 1);
|
|
|
|
pce->label = _("Group to create: 1=small 2=big 3=channel");
|
|
|
|
pce->identifier = typeKey;
|
|
|
|
pce->required = FALSE;
|
|
|
|
pce->is_int = TRUE;
|
|
|
|
static_assert((GROUP_TYPE_BASIC > 0) && (GROUP_TYPE_SUPER > 0) && (GROUP_TYPE_CHANNEL > 0), "positive please");
|
|
|
|
pce->min = std::min({GROUP_TYPE_BASIC, GROUP_TYPE_SUPER, GROUP_TYPE_CHANNEL});
|
|
|
|
pce->max = std::max({GROUP_TYPE_BASIC, GROUP_TYPE_SUPER, GROUP_TYPE_CHANNEL});
|
|
|
|
info = g_list_append (info, pce);
|
|
|
|
|
2020-05-09 14:07:21 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2020-06-11 00:04:29 +02:00
|
|
|
std::string getPurpleChatName(const td::td_api::chat &chat)
|
2020-05-09 14:07:21 +02:00
|
|
|
{
|
2020-05-09 19:53:06 +02:00
|
|
|
return "chat" + std::to_string(chat.id_);
|
2020-05-09 14:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GHashTable *getChatComponents(const td::td_api::chat &chat)
|
|
|
|
{
|
|
|
|
char name[32];
|
2020-05-16 14:13:34 +02:00
|
|
|
snprintf(name, sizeof(name)-1, "chat%" G_GINT64_FORMAT "", chat.id_);
|
2020-05-09 14:07:21 +02:00
|
|
|
name[sizeof(name)-1] = '\0';
|
|
|
|
|
|
|
|
GHashTable *table = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
|
2020-06-11 00:04:29 +02:00
|
|
|
g_hash_table_insert(table, chatNameComponent, g_strdup(name));
|
2020-05-09 14:07:21 +02:00
|
|
|
return table;
|
|
|
|
}
|
2020-05-10 12:05:55 +02:00
|
|
|
|
|
|
|
const char *getChatName(GHashTable *components)
|
|
|
|
{
|
2020-06-11 00:04:29 +02:00
|
|
|
return (const char *)g_hash_table_lookup(components, chatNameComponent);
|
2020-05-10 12:05:55 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 13:34:23 +02:00
|
|
|
const char *getChatJoinString(GHashTable *components)
|
2020-05-12 19:37:59 +02:00
|
|
|
{
|
2020-08-13 13:34:23 +02:00
|
|
|
return (const char *)g_hash_table_lookup(components, joinStringKey);
|
2020-05-12 19:37:59 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 14:48:17 +02:00
|
|
|
const char *getChatGroupName(GHashTable *components)
|
|
|
|
{
|
|
|
|
return (const char *)g_hash_table_lookup(components, nameKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
int getChatGroupType(GHashTable *components)
|
|
|
|
{
|
|
|
|
const char *s = static_cast<const char *>(g_hash_table_lookup(components, typeKey));
|
|
|
|
return s ? atoi(s) : 0;
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:05:55 +02:00
|
|
|
int64_t getTdlibChatId(const char *chatName)
|
|
|
|
{
|
|
|
|
if (chatName && !strncmp(chatName, "chat", 4)) {
|
2020-05-13 15:59:45 +12:00
|
|
|
int64_t id;
|
|
|
|
if (sscanf(chatName+4, "%" G_GINT64_FORMAT "", &id) == 1)
|
2020-05-10 12:05:55 +02:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-26 20:06:09 +02:00
|
|
|
|
|
|
|
unsigned getAutoDownloadLimitKb(PurpleAccount *account)
|
|
|
|
{
|
|
|
|
std::string dlLimitStr = purple_account_get_string(account, AccountOptions::AutoDownloadLimit,
|
|
|
|
AccountOptions::AutoDownloadLimitDefault);
|
|
|
|
for (size_t i = 0; i < dlLimitStr.size(); i++)
|
|
|
|
if (dlLimitStr[i] == ',')
|
|
|
|
dlLimitStr[i] = '.';
|
|
|
|
char *endptr;
|
|
|
|
float dlLimit = strtof(dlLimitStr.c_str(), &endptr);
|
|
|
|
|
|
|
|
if (*endptr != '\0') {
|
|
|
|
std::string message = formatMessage(_("Invalid auto-download limit '{}', resetting to default"), dlLimitStr);
|
|
|
|
purple_notify_warning(account, _("Download limit"), message.c_str(), NULL);
|
|
|
|
purple_account_set_string(account, AccountOptions::AutoDownloadLimit,
|
|
|
|
AccountOptions::AutoDownloadLimitDefault);
|
|
|
|
dlLimit = atof(AccountOptions::AutoDownloadLimitDefault);
|
|
|
|
} else if (!isfinite(dlLimit) || (dlLimit >= UINT_MAX/1024-1)) {
|
|
|
|
purple_account_set_string(account, AccountOptions::AutoDownloadLimit, "0");
|
|
|
|
dlLimit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return floorf(dlLimit*1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSizeWithinLimit(unsigned size, unsigned limit)
|
|
|
|
{
|
|
|
|
return (limit == 0) || (size <= limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignoreBigDownloads(PurpleAccount *account)
|
|
|
|
{
|
|
|
|
return !strcmp(purple_account_get_string(account, AccountOptions::BigDownloadHandling,
|
2020-05-31 15:55:28 +02:00
|
|
|
AccountOptions::BigDownloadHandlingDefault),
|
2020-05-26 20:06:09 +02:00
|
|
|
AccountOptions::BigDownloadHandlingDiscard);
|
|
|
|
}
|
2020-06-13 16:01:05 +02:00
|
|
|
|
|
|
|
PurpleTdClient *getTdClient(PurpleAccount *account)
|
|
|
|
{
|
|
|
|
PurpleConnection *connection = purple_account_get_connection(account);
|
|
|
|
if (connection)
|
|
|
|
return static_cast<PurpleTdClient *>(purple_connection_get_protocol_data(connection));
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-08-14 21:44:38 +02:00
|
|
|
|
|
|
|
const char *getUiName()
|
|
|
|
{
|
|
|
|
GHashTable *ui_info = purple_core_get_ui_info();
|
|
|
|
const char *name = static_cast<char *>(g_hash_table_lookup(ui_info, "name"));
|
|
|
|
return name ? name : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *AccountOptions::DownloadBehaviourDefault()
|
|
|
|
{
|
|
|
|
if (!strcasecmp(getUiName(), "pidgin"))
|
|
|
|
return AccountOptions::DownloadBehaviourHyperlink;
|
|
|
|
else
|
|
|
|
return AccountOptions::DownloadBehaviourStandard;
|
|
|
|
}
|