You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1122 lines
41 KiB
C++

3 years ago
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <stdlib.h>
3 years ago
#include <stdint.h>
3 years ago
#include <string.h>
#include <stdio.h>
#include <time.h>
3 years ago
#include <regex>
#include <fmt/core.h>
#include <optional>
3 years ago
#include <strophe.h>
#include <weechat/weechat-plugin.h>
3 years ago
#include "plugin.hh"
#include "account.hh"
#include "omemo.hh"
#include "user.hh"
#include "channel.hh"
3 years ago
#include "input.hh"
#include "buffer.hh"
#include "pgp.hh"
#include "util.hh"
#include "xmpp/node.hh"
3 years ago
void weechat::channel::set_transport(enum weechat::channel::transport transport, int force)
3 years ago
{
if (force)
switch (transport)
{
case weechat::channel::transport::PLAIN:
omemo.enabled = 0;
pgp.enabled = 0;
3 years ago
break;
case weechat::channel::transport::OMEMO:
omemo.enabled = 1;
pgp.enabled = 0;
3 years ago
break;
case weechat::channel::transport::PGP:
omemo.enabled = 0;
pgp.enabled = 1;
3 years ago
break;
default:
break;
}
if (this->transport != transport)
3 years ago
{
this->transport = transport;
weechat_printf_date_tags(buffer, 0, NULL, "%s%sTransport: %s",
weechat_prefix("network"), weechat_color("gray"),
weechat::channel::transport_name(this->transport));
3 years ago
}
}
struct t_gui_buffer *weechat::channel::search_buffer(weechat::channel::chat_type type,
const char *name)
3 years ago
{
struct t_hdata *hdata_buffer;
struct t_gui_buffer *ptr_buffer;
3 years ago
const char *ptr_type, *ptr_account_name, *ptr_remote_jid;
3 years ago
hdata_buffer = weechat_hdata_get("buffer");
ptr_buffer = (struct t_gui_buffer*)weechat_hdata_get_list(hdata_buffer, "gui_buffers");
3 years ago
while (ptr_buffer)
{
if (weechat_buffer_get_pointer(ptr_buffer, "plugin") == weechat_plugin)
{
ptr_type = weechat_buffer_get_string(ptr_buffer, "localvar_type");
ptr_account_name = weechat_buffer_get_string(ptr_buffer,
3 years ago
"localvar_account");
ptr_remote_jid = weechat_buffer_get_string(ptr_buffer,
"localvar_remote_jid");
3 years ago
if (ptr_type && ptr_type[0]
&& ptr_account_name && ptr_account_name[0]
3 years ago
&& ptr_remote_jid && ptr_remote_jid[0]
&& ( (( (type == weechat::channel::chat_type::MUC))
3 years ago
&& (strcmp(ptr_type, "room") == 0))
|| (( (type == weechat::channel::chat_type::PM))
3 years ago
&& (strcmp(ptr_type, "private") == 0)))
&& (ptr_account_name == account.name)
3 years ago
&& (weechat_strcasecmp(ptr_remote_jid, name) == 0))
3 years ago
{
return ptr_buffer;
}
}
ptr_buffer = (struct t_gui_buffer*)weechat_hdata_move(hdata_buffer, ptr_buffer, 1);
3 years ago
}
return NULL;
}
struct t_gui_buffer *weechat::channel::create_buffer(weechat::channel::chat_type type,
const char *name)
3 years ago
{
struct t_gui_buffer *ptr_buffer;
int buffer_created;
3 years ago
const char *short_name = NULL, *localvar_remote_jid = NULL;
3 years ago
buffer_created = 0;
std::string buffer_name = fmt::format("{}.{}", account.name, name);
3 years ago
ptr_buffer = weechat::channel::search_buffer(type, name);
3 years ago
if (ptr_buffer)
{
weechat_nicklist_remove_all(ptr_buffer);
}
else
{
ptr_buffer = weechat_buffer_new(buffer_name.data(),
3 years ago
&input__data_cb, NULL, NULL,
&buffer__close_cb, NULL, NULL);
if (!ptr_buffer)
return NULL;
buffer_created = 1;
}
if (buffer_created)
{
char *res = (char*)strrchr(name, '/');
3 years ago
if (!weechat_buffer_get_integer(ptr_buffer, "short_name_is_set"))
3 years ago
weechat_buffer_set(ptr_buffer, "short_name",
res ? res + 1 : name);
3 years ago
}
else
{
3 years ago
short_name = weechat_buffer_get_string(ptr_buffer, "short_name");
localvar_remote_jid = weechat_buffer_get_string(ptr_buffer,
"localvar_remote_jid");
3 years ago
if (!short_name ||
3 years ago
(localvar_remote_jid && (strcmp(localvar_remote_jid, short_name) == 0)))
3 years ago
{
3 years ago
weechat_buffer_set(ptr_buffer, "short_name",
xmpp_jid_node(account.context, name));
3 years ago
}
}
if(!(account.nickname().size()))
account.nickname(xmpp_jid_node(account.context, account.jid().data()));
3 years ago
// Set notify level for buffer: "0" = never add to hotlist
// "1" = add for highlights only
// "2" = add for highlights and messages
// "3" = add for all messages.
weechat_buffer_set(ptr_buffer, "notify",
(type == weechat::channel::chat_type::PM) ? "3" : "2");
3 years ago
weechat_buffer_set(ptr_buffer, "localvar_set_type",
(type == weechat::channel::chat_type::PM) ? "private" : "channel");
3 years ago
weechat_buffer_set(ptr_buffer, "localvar_set_nick",
account.nickname().data());
weechat_buffer_set(ptr_buffer, "localvar_set_account", account.name.data());
3 years ago
weechat_buffer_set(ptr_buffer, "localvar_set_remote_jid", name);
3 years ago
weechat_buffer_set(ptr_buffer, "input_multiline", "1");
3 years ago
if (buffer_created)
{
3 years ago
(void) weechat_hook_signal_send("logger_backlog",
WEECHAT_HOOK_SIGNAL_POINTER,
ptr_buffer);
3 years ago
weechat_buffer_set(ptr_buffer, "input_get_unknown_commands", "1");
if (type != weechat::channel::chat_type::PM)
3 years ago
{
weechat_buffer_set(ptr_buffer, "nicklist", "1");
weechat_buffer_set(ptr_buffer, "nicklist_display_groups", "0");
weechat_buffer_set_pointer(ptr_buffer, "nicklist_callback",
(void*)&buffer__nickcmp_cb);
3 years ago
weechat_buffer_set_pointer(ptr_buffer, "nicklist_callback_pointer",
&account);
3 years ago
}
weechat_buffer_set(ptr_buffer, "highlight_words_add",
account.nickname().data());
3 years ago
weechat_buffer_set(ptr_buffer, "highlight_tags_restrict",
"message");
}
return ptr_buffer;
}
void weechat::channel::add_nicklist_groups()
3 years ago
{
if (type == weechat::channel::chat_type::PM)
3 years ago
return;
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 000, "~").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 001, "&").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 002, "@").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 003, "%").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 004, "+").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 005, "?").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 006, "!").data(),
3 years ago
"weechat.color.nicklist_group", 1);
weechat_nicklist_add_group(buffer, NULL, fmt::format("%03d|%s", 999, "...").data(),
3 years ago
"weechat.color.nicklist_group", 1);
}
weechat::channel::channel(weechat::account& account,
weechat::channel::chat_type type,
const char *id, const char *name) : id(id), name(name), type(type), account(account)
3 years ago
{
if (!id || !name || !name[0])
throw std::invalid_argument("channel()");
3 years ago
//if (weechat::channel::search(&account, id))
// throw std::invalid_argument("duplicate");
3 years ago
buffer = weechat::channel::create_buffer(type, name);
if (!buffer)
throw std::invalid_argument("buffer fail");
else if (type == weechat::channel::chat_type::PM)
3 years ago
{
auto muc_channel = account.channels.find(jid(account.context,
id).bare.data());
if (muc_channel != account.channels.end())
{
weechat_buffer_merge(buffer, muc_channel->second.buffer);
}
}
3 years ago
typing_hook_timer = weechat_hook_timer(1 * 1000, 0, 0,
&weechat::channel::typing_cb,
this, nullptr);
self_typing_hook_timer = weechat_hook_timer(1 * 1000, 0, 0,
&weechat::channel::self_typing_cb,
this, nullptr);
omemo.enabled = type == weechat::channel::chat_type::PM ? 1 : 0;
omemo.devicelist_requests = weechat_hashtable_new(64,
WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, nullptr, nullptr);
omemo.bundle_requests = weechat_hashtable_new(64,
WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, nullptr, nullptr);
3 years ago
add_nicklist_groups();
3 years ago
if (type != weechat::channel::chat_type::MUC)
3 years ago
{
time_t start = time(NULL);
struct tm *ago = gmtime(&start);
ago->tm_mday -= 7;
start = mktime(ago);
fetch_mam(nullptr, &start, nullptr, nullptr);
3 years ago
}
3 years ago
}
void weechat::channel::member_speaking_add_to_list(const char *nick, int highlight)
3 years ago
{
int size, to_remove, i;
struct t_weelist_item *ptr_item;
/* create list if it does not exist */
if (!members_speaking[highlight])
members_speaking[highlight] = weechat_list_new();
3 years ago
/* remove item if it was already in list */
ptr_item = weechat_list_casesearch(members_speaking[highlight], nick);
3 years ago
if (ptr_item)
weechat_list_remove(members_speaking[highlight], ptr_item);
3 years ago
/* add nick in list */
weechat_list_add(members_speaking[highlight], nick,
3 years ago
WEECHAT_LIST_POS_END, NULL);
/* reduce list size if it's too big */
size = weechat_list_size(members_speaking[highlight]);
3 years ago
if (size > CHANNEL_MEMBERS_SPEAKING_LIMIT)
{
to_remove = size - CHANNEL_MEMBERS_SPEAKING_LIMIT;
for (i = 0; i < to_remove; i++)
{
weechat_list_remove(
members_speaking[highlight],
weechat_list_get(members_speaking[highlight], 0));
3 years ago
}
}
}
void weechat::channel::member_speaking_add(const char *nick, int highlight)
3 years ago
{
if (highlight < 0)
highlight = 0;
if (highlight > 1)
highlight = 1;
if (highlight)
weechat::channel::member_speaking_add_to_list(nick, 1);
3 years ago
weechat::channel::member_speaking_add_to_list(nick, 0);
3 years ago
}
void weechat::channel::member_speaking_rename(const char *old_nick, const char *new_nick)
3 years ago
{
struct t_weelist_item *ptr_item;
int i;
for (i = 0; i < 2; i++)
{
if (members_speaking[i])
3 years ago
{
ptr_item = weechat_list_search(members_speaking[i], old_nick);
3 years ago
if (ptr_item)
weechat_list_set(ptr_item, new_nick);
}
}
}
void weechat::channel::member_speaking_rename_if_present(const char *nick)
3 years ago
{
struct t_weelist_item *ptr_item;
int i, j, list_size;
for (i = 0; i < 2; i++)
{
if (members_speaking[i])
3 years ago
{
list_size = weechat_list_size(members_speaking[i]);
3 years ago
for (j = 0; j < list_size; j++)
{
ptr_item = weechat_list_get(members_speaking[i], j);
if (ptr_item && (weechat_strcasecmp(weechat_list_string(ptr_item),
nick) == 0))
3 years ago
weechat_list_set(ptr_item, nick);
}
}
}
}
int weechat::channel::typing_cb(const void *pointer, void *data, int remaining_calls)
3 years ago
{
weechat::channel *channel;
3 years ago
const char *localvar;
unsigned typecount;
time_t now;
(void) data;
(void) remaining_calls;
if (!pointer)
return WEECHAT_RC_ERROR;
channel = (weechat::channel *)pointer;
3 years ago
now = time(NULL);
typecount = 0;
for (auto ptr_typing = channel->typings.begin();
ptr_typing != channel->typings.end(); ptr_typing++)
3 years ago
{
if (now - ptr_typing->ts > 5)
3 years ago
{
channel->typings.erase(ptr_typing);
3 years ago
}
typecount++;
}
localvar = weechat_buffer_get_string(channel->buffer, "localvar_typing");
if (!localvar || strncmp(localvar, typecount > 0 ? "1" : "0", 1) != 0)
weechat_buffer_set(channel->buffer, "localvar_set_typing",
3 years ago
typecount > 0 ? "1" : "0");
weechat_bar_item_update("typing");
return WEECHAT_RC_OK;
}
weechat::channel::typing *weechat::channel::typing_search(const char *id)
3 years ago
{
if (!id)
return nullptr;
3 years ago
for (auto& ptr_typing : typings)
3 years ago
{
if (weechat_strcasecmp(ptr_typing.id, id) == 0)
return &ptr_typing;
3 years ago
}
return nullptr;
3 years ago
}
int weechat::channel::add_typing(weechat::user *user)
3 years ago
{
weechat::channel::typing *new_typing;
3 years ago
int ret = 0;
3 years ago
new_typing = weechat::channel::typing_search(user->id);
3 years ago
if (!new_typing)
{
new_typing = new weechat::channel::typing();
3 years ago
new_typing->id = strdup(user->id);
new_typing->name = strdup(user->profile.display_name);
3 years ago
ret = 1;
3 years ago
}
new_typing->ts = time(nullptr);
3 years ago
weechat::channel::typing_cb(this, nullptr, 0);
3 years ago
return ret;
}
int weechat::channel::self_typing_cb(const void *pointer, void *data, int remaining_calls)
3 years ago
{
time_t now;
(void) data;
(void) remaining_calls;
if (!pointer)
return WEECHAT_RC_ERROR;
weechat::channel *channel = (weechat::channel *)pointer;
3 years ago
now = time(NULL);
for (auto ptr_typing = channel->self_typings.begin();
ptr_typing != channel->self_typings.end(); ptr_typing++)
3 years ago
{
if (now - ptr_typing->ts > 10)
3 years ago
{
channel->send_paused(ptr_typing->user);
channel->self_typings.erase(ptr_typing);
3 years ago
}
}
return WEECHAT_RC_OK;
}
weechat::channel::typing *weechat::channel::self_typing_search(weechat::user *user)
3 years ago
{
for (auto& ptr_typing : typings)
3 years ago
{
if (user == ptr_typing.user)
return &ptr_typing;
3 years ago
}
return nullptr;
3 years ago
}
int weechat::channel::add_self_typing(weechat::user *user)
3 years ago
{
weechat::channel::typing *new_typing;
3 years ago
int ret = 0;
new_typing = self_typing_search(user);
3 years ago
if (!new_typing)
{
new_typing = new weechat::channel::typing();
3 years ago
new_typing->user = user;
new_typing->name = user ? strdup(user->profile.display_name) : NULL;
ret = 1;
}
self_typing_cb(this, nullptr, 0);
3 years ago
return ret;
3 years ago
}
weechat::channel::~channel()
3 years ago
{
if (typing_hook_timer)
weechat_unhook(typing_hook_timer);
if (self_typing_hook_timer)
weechat_unhook(self_typing_hook_timer);
if (members_speaking[0])
weechat_list_free(members_speaking[0]);
if (members_speaking[1])
weechat_list_free(members_speaking[1]);
3 years ago
}
void weechat::channel::update_topic(const char* topic, const char* creator, int last_set)
3 years ago
{
if (this->topic.value)
::free(this->topic.value);
if (this->topic.creator)
::free(this->topic.creator);
this->topic.value = (topic) ? strdup(topic) : NULL;
this->topic.creator = (creator) ? strdup(creator) : NULL;
this->topic.last_set = last_set;
if (this->topic.value)
weechat_buffer_set(buffer, "title", topic);
3 years ago
else
weechat_buffer_set(buffer, "title", "");
3 years ago
}
void weechat::channel::update_name(const char* name)
{
3 years ago
if (name)
weechat_buffer_set(buffer, "short_name", name);
else
weechat_buffer_set(buffer, "short_name", "");
}
weechat::channel::member *weechat::channel::add_member(const char *id, const char *client)
3 years ago
{
weechat::channel::member *member;
weechat::user *user;
3 years ago
user = user::search(&account, id);
3 years ago
if (this->id == id && type == weechat::channel::chat_type::MUC)
3 years ago
{
weechat_printf_date_tags(buffer, 0, "log2", "%sMUC: %s",
weechat_prefix("network"), id);
return nullptr;
3 years ago
}
if (!(member = member_search(id)))
3 years ago
{
member = new weechat::channel::member();
3 years ago
member->id = strdup(id);
3 years ago
3 years ago
member->role = NULL;
member->affiliation = NULL;
}
else if (user)
user->nicklist_remove(&account, this);
3 years ago
if (user)
user->nicklist_add(&account, this);
else return member; // TODO: !!
char *jid_bare = xmpp_jid_bare(account.context, user->id);
char *jid_resource = xmpp_jid_resource(account.context, user->id);
if (weechat_strcasecmp(jid_bare, id) == 0
&& type == weechat::channel::chat_type::MUC)
weechat_printf_date_tags(buffer, 0, "xmpp_presence,enter,log4", "%s%s%s%s%s %s%s%s%s %s%s%s%s%s%s%s%s%s%s%s%s%s%s",
weechat_prefix("join"),
user->as_prefix_raw().data(),
3 years ago
client ? " (" : "",
client ? client : "",
client ? ")" : "",
user->profile.status ? "is " : "",
3 years ago
weechat_color("irc.color.message_join"),
3 years ago
user->profile.status ? user->profile.status : (user->profile.idle ? "idle" : "entered"),
3 years ago
weechat_color("reset"),
id,
3 years ago
user->profile.status_text ? " [" : "",
user->profile.status_text ? user->profile.status_text : "",
user->profile.status_text ? "]" : "",
weechat_color("yellow"), " as ", weechat_color("reset"),
user->profile.affiliation ? user->profile.affiliation : "",
user->profile.affiliation ? " " : "",
user->profile.role,
user->profile.pgp_id ? weechat_color("gray") : "",
user->profile.pgp_id ? " with PGP:" : "",
user->profile.pgp_id ? user->profile.pgp_id : "",
user->profile.pgp_id ? weechat_color("reset") : "");
else
weechat_printf_date_tags(buffer, 0, "xmpp_presence,enter,log4", "%s%s (%s) %s%s%s%s %s%s%s%s%s%s%s%s%s",
weechat_prefix("join"),
jid_resource ? user->as_prefix_raw().data() : "You",
jid_resource ? jid_resource : user->as_prefix_raw().data(),
3 years ago
user->profile.status ? "is " : "",
3 years ago
weechat_color("irc.color.message_join"),
3 years ago
user->profile.status ? user->profile.status : (user->profile.idle ? "idle" : "entered"),
3 years ago
weechat_color("reset"),
3 years ago
user->profile.idle ? "since " : "",
3 years ago
user->profile.idle ? user->profile.idle->data() : "",
3 years ago
user->profile.status_text ? " [" : "",
user->profile.status_text ? user->profile.status_text : "",
user->profile.status_text ? "]" : "",
user->profile.pgp_id || user->profile.omemo ? weechat_color("gray") : "",
user->profile.pgp_id || user->profile.omemo ? " with " : "",
user->profile.pgp_id ? "PGP:" : "",
3 years ago
user->profile.pgp_id ? user->profile.pgp_id : "",
user->profile.omemo && user->profile.pgp_id ? " and " : "",
user->profile.omemo ? "OMEMO" : "",
user->profile.pgp_id || user->profile.omemo ? weechat_color("reset") : "");
return member;
}
weechat::channel::member *weechat::channel::member_search(const char *id)
{
if (!id)
return nullptr;
for (auto& ptr_member : members)
{
if (weechat_strcasecmp(ptr_member.second.id, id) == 0)
return &ptr_member.second;
}
return nullptr;
}
weechat::channel::member *weechat::channel::remove_member(const char *id, const char *reason)
{
weechat::channel::member *member;
weechat::user *user;
user = user::search(&account, id);
3 years ago
if (user)
user->nicklist_remove(&account, this);
3 years ago
else return nullptr; // TODO !!
member = member_search(id);
char *jid_bare = xmpp_jid_bare(account.context, user->id);
char *jid_resource = xmpp_jid_resource(account.context, user->id);
if (weechat_strcasecmp(jid_bare, id) == 0
&& type == weechat::channel::chat_type::MUC)
weechat_printf_date_tags(buffer, 0, "xmpp_presence,leave,log4",
"%s%s %sleft%s %s %s%s%s",
weechat_prefix("quit"),
jid_resource,
3 years ago
weechat_color("irc.color.message_quit"),
weechat_color("reset"),
id,
3 years ago
reason ? "[" : "",
reason ? reason : "",
reason ? "]" : "");
else
weechat_printf_date_tags(buffer, 0, "xmpp_presence,leave,log4",
"%s%s (%s) %sleft%s %s %s%s%s",
weechat_prefix("quit"),
xmpp_jid_bare(account.context, user->id),
xmpp_jid_resource(account.context, user->id),
3 years ago
weechat_color("irc.color.message_quit"),
weechat_color("reset"),
id,
3 years ago
reason ? "[" : "",
reason ? reason : "",
reason ? "]" : "");
3 years ago
return member;
}
3 years ago
int weechat::channel::send_message(std::string to, std::string body,
std::optional<std::string> oob)
{
xmpp_stanza_t *message = xmpp_message_new(account.context,
type == weechat::channel::chat_type::MUC
? "groupchat" : "chat",
to.data(), NULL);
char *id = xmpp_uuid_gen(account.context);
xmpp_stanza_set_id(message, id);
xmpp_free(account.context, id);
xmpp_message_set_body(message, body.data());
if (oob)
{
xmpp_stanza_t *message__x = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__x, "x");
xmpp_stanza_set_ns(message__x, "jabber:x:oob");
xmpp_stanza_t *message__x__url = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__x__url, "url");
xmpp_stanza_t *message__x__url__text = xmpp_stanza_new(account.context);
xmpp_stanza_set_text(message__x__url__text, oob->data());
xmpp_stanza_add_child(message__x__url, message__x__url__text);
xmpp_stanza_release(message__x__url__text);
xmpp_stanza_add_child(message__x, message__x__url);
xmpp_stanza_release(message__x__url);
xmpp_stanza_add_child(message, message__x);
xmpp_stanza_release(message__x);
}
xmpp_stanza_t *message__active = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__active, "active");
xmpp_stanza_set_ns(message__active, "http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(message, message__active);
xmpp_stanza_release(message__active);
xmpp_stanza_t *message__request = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__request, "request");
xmpp_stanza_set_ns(message__request, "urn:xmpp:receipts");
xmpp_stanza_add_child(message, message__request);
xmpp_stanza_release(message__request);
xmpp_stanza_t *message__markable = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__markable, "markable");
xmpp_stanza_set_ns(message__markable, "urn:xmpp:chat-markers:0");
xmpp_stanza_add_child(message, message__markable);
xmpp_stanza_release(message__markable);
xmpp_stanza_t *message__store = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__store, "store");
xmpp_stanza_set_ns(message__store, "urn:xmpp:hints");
xmpp_stanza_add_child(message, message__store);
xmpp_stanza_release(message__store);
xmpp_send(account.connection, message);
xmpp_stanza_release(message);
if (type != weechat::channel::chat_type::MUC)
weechat_printf_date_tags(buffer, 0,
"xmpp_message,message,private,notify_none,self_msg,log1",
"%s\t%s",
user::search(&account, account.jid().data())->as_prefix_raw().data(),
3 years ago
body.data());
return WEECHAT_RC_OK;
}
int weechat::channel::send_message(const char *to, const char *body)
3 years ago
{
send_reads();
3 years ago
xmpp_stanza_t *message = xmpp_message_new(account.context,
type == weechat::channel::chat_type::MUC
3 years ago
? "groupchat" : "chat",
to, NULL);
3 years ago
char *id = xmpp_uuid_gen(account.context);
3 years ago
xmpp_stanza_set_id(message, id);
xmpp_free(account.context, id);
3 years ago
if (account.omemo && omemo.enabled)
{
xmpp_stanza_t *encrypted = account.omemo.encode(&account, to, body);
3 years ago
if (!encrypted)
{
weechat_printf_date_tags(buffer, 0, "notify_none", "%s%s",
weechat_prefix("error"), "OMEMO Encryption Error");
set_transport(weechat::channel::transport::PLAIN, 1);
3 years ago
xmpp_stanza_release(message);
return WEECHAT_RC_ERROR;
}
3 years ago
xmpp_stanza_add_child(message, encrypted);
xmpp_stanza_release(encrypted);
xmpp_stanza_t *message__encryption = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__encryption, "encryption");
xmpp_stanza_set_ns(message__encryption, "urn:xmpp:eme:0");
xmpp_stanza_set_attribute(message__encryption, "namespace",
"eu.siacs.conversations.axolotl");
xmpp_stanza_set_attribute(message__encryption, "name", "OMEMO");
xmpp_stanza_add_child(message, message__encryption);
xmpp_stanza_release(message__encryption);
xmpp_message_set_body(message, OMEMO_ADVICE);
set_transport(weechat::channel::transport::OMEMO, 0);
}
else if (pgp.enabled && !pgp.ids.empty())
3 years ago
{
xmpp_stanza_t *message__x = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__x, "x");
xmpp_stanza_set_ns(message__x, "jabber:x:encrypted");
xmpp_stanza_t *message__x__text = xmpp_stanza_new(account.context);
char *ciphertext = account.pgp.encrypt(buffer, account.pgp_keyid().data(), std::vector(pgp.ids.begin(), pgp.ids.end()), body);
3 years ago
if (ciphertext)
xmpp_stanza_set_text(message__x__text, ciphertext);
3 years ago
else
{
weechat_printf_date_tags(buffer, 0, "notify_none", "%s%s",
3 years ago
weechat_prefix("error"), "PGP Error");
set_transport(weechat::channel::transport::PLAIN, 1);
3 years ago
xmpp_stanza_release(message);
return WEECHAT_RC_ERROR;
}
::free(ciphertext);
3 years ago
xmpp_stanza_add_child(message__x, message__x__text);
xmpp_stanza_release(message__x__text);
xmpp_stanza_add_child(message, message__x);
xmpp_stanza_release(message__x);
xmpp_stanza_t *message__encryption = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__encryption, "encryption");
xmpp_stanza_set_ns(message__encryption, "urn:xmpp:eme:0");
xmpp_stanza_set_attribute(message__encryption, "namespace", "jabber:x:encryption");
xmpp_stanza_add_child(message, message__encryption);
xmpp_stanza_release(message__encryption);
xmpp_message_set_body(message, weechat::xmpp::PGP_ADVICE);
3 years ago
set_transport(weechat::channel::transport::PGP, 0);
3 years ago
}
3 years ago
else
3 years ago
{
3 years ago
xmpp_message_set_body(message, body);
set_transport(weechat::channel::transport::PLAIN, 0);
3 years ago
}
3 years ago
3 years ago
static const std::regex pattern("https?:[^ ]*");
std::cmatch match;
if (transport == weechat::channel::transport::PLAIN &&
3 years ago
std::regex_search(body, match, pattern)
&& match[0].matched && !match.prefix().length())
{
3 years ago
std::string url { &*match[0].first, static_cast<size_t>(match[0].length()) };
do {
struct t_hashtable *options = weechat_hashtable_new(8,
WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING,
NULL, NULL);
if (!options) { return WEECHAT_RC_ERROR; };
weechat_hashtable_set(options, "header", "1");
weechat_hashtable_set(options, "nobody", "1");
auto command = "url:" + url;
const int timeout = 30000;
struct message_task {
weechat::channel& channel;
std::string to;
std::string body;
std::string url;
};
auto *task = new message_task { *this, to, body, url };
auto callback = [](const void *pointer, void *,
const char *, int ret, const char *out, const char *err) {
auto task = static_cast<const message_task*>(pointer);
if (!task) return WEECHAT_RC_ERROR;
3 years ago
if (ret == 0)
{
const std::string_view prefix = "content-type: ";
std::istringstream ss(out ? out : "");
std::string line, mime;
while (std::getline(ss, line)) {
std::transform(line.begin(), line.end(), line.begin(),
[](char c) -> char { return std::tolower(c); });
if (line.starts_with(prefix)) {
mime = line.substr(prefix.size());
break;
}
}
if (mime.starts_with("image") || mime.starts_with("video"))
{
weechat_printf_date_tags(task->channel.buffer, 0,
"notify_none,no_log", "[oob]\t%s%s",
weechat_color("gray"), mime.data());
task->channel.send_message(task->to, task->body, { task->url });
}
else
{
weechat_printf_date_tags(task->channel.buffer, 0,
"notify_none,no_log", "[curl]\t%s%s",
weechat_color("red"), err);
task->channel.send_message(task->to.data(), task->body.data());
}
}
else
{
task->channel.send_message(task->to.data(), task->body.data());
}
delete task;
return WEECHAT_RC_OK;
};
struct t_hook *process_hook =
weechat_hook_process_hashtable(command.data(), options, timeout,
callback, task, nullptr);
weechat_hashtable_free(options);
(void) process_hook;
return WEECHAT_RC_OK;
} while(0);
}
xmpp_stanza_t *message__active = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__active, "active");
3 years ago
xmpp_stanza_set_ns(message__active, "http://jabber.org/protocol/chatstates");
3 years ago
xmpp_stanza_add_child(message, message__active);
xmpp_stanza_release(message__active);
xmpp_stanza_t *message__request = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__request, "request");
xmpp_stanza_set_ns(message__request, "urn:xmpp:receipts");
xmpp_stanza_add_child(message, message__request);
xmpp_stanza_release(message__request);
xmpp_stanza_t *message__markable = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__markable, "markable");
xmpp_stanza_set_ns(message__markable, "urn:xmpp:chat-markers:0");
xmpp_stanza_add_child(message, message__markable);
xmpp_stanza_release(message__markable);
xmpp_stanza_t *message__store = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__store, "store");
xmpp_stanza_set_ns(message__store, "urn:xmpp:hints");
xmpp_stanza_add_child(message, message__store);
xmpp_stanza_release(message__store);
xmpp_send(account.connection, message);
3 years ago
xmpp_stanza_release(message);
if (type != weechat::channel::chat_type::MUC)
weechat_printf_date_tags(buffer, 0,
3 years ago
"xmpp_message,message,private,notify_none,self_msg,log1",
3 years ago
"%s\t%s",
user::search(&account, account.jid().data())->as_prefix_raw().data(),
3 years ago
body);
3 years ago
return WEECHAT_RC_OK;
3 years ago
}
3 years ago
void weechat::channel::send_reads()
3 years ago
{
auto i = std::begin(unreads);
3 years ago
while (i != std::end(unreads))
{
auto* unread = &*i;
3 years ago
xmpp_stanza_t *message = xmpp_message_new(account.context, NULL,
id.data(), NULL);
3 years ago
xmpp_stanza_t *message__displayed = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__displayed, "displayed");
xmpp_stanza_set_ns(message__displayed, "urn:xmpp:chat-markers:0");
xmpp_stanza_set_id(message__displayed, unread->id);
if (unread->thread)
{
xmpp_stanza_t *message__thread = xmpp_stanza_new(account.context);
xmpp_stanza_set_name(message__thread, "thread");
3 years ago
xmpp_stanza_t *message__thread__text = xmpp_stanza_new(account.context);
xmpp_stanza_set_text(message__thread__text, unread->thread);
xmpp_stanza_add_child(message__thread, message__thread__text);
xmpp_stanza_release(message__thread__text);
3 years ago
xmpp_stanza_add_child(message, message__thread);
xmpp_stanza_release(message__thread);
}
3 years ago
xmpp_stanza_add_child(message, message__displayed);
xmpp_stanza_release(message__displayed);
3 years ago
xmpp_send(account.connection, message);
xmpp_stanza_release(message);
3 years ago
i = unreads.erase(i);
3 years ago
}
}
void weechat::channel::send_typing(weechat::user *user)
3 years ago
{
if (add_self_typing(user))
3 years ago
{
xmpp_stanza_t *message = xmpp_message_new(account.context,
type == weechat::channel::chat_type::MUC
3 years ago
? "groupchat" : "chat",
(user ? user->id : id).data(), NULL);
3 years ago
xmpp_stanza_t *message__composing = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__composing, "composing");
xmpp_stanza_set_ns(message__composing, "http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(message, message__composing);
xmpp_stanza_release(message__composing);
xmpp_send(account.connection, message);
3 years ago
xmpp_stanza_release(message);
}
}
void weechat::channel::send_paused(weechat::user *user)
3 years ago
{
xmpp_stanza_t *message = xmpp_message_new(account.context,
type == weechat::channel::chat_type::MUC
3 years ago
? "groupchat" : "chat",
(user ? user->id : id).data(), NULL);
3 years ago
xmpp_stanza_t *message__paused = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(message__paused, "paused");
xmpp_stanza_set_ns(message__paused, "http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(message, message__paused);
xmpp_stanza_release(message__paused);
xmpp_send(account.connection, message);
3 years ago
xmpp_stanza_release(message);
}
3 years ago
void weechat::channel::fetch_mam(const char *id, time_t *start, time_t *end, const char* after)
3 years ago
{
xmpp_stanza_t *iq = xmpp_iq_new(account.context, "set", "juliet1");
xmpp_stanza_set_id(iq, id ? id : xmpp_uuid_gen(account.context));
3 years ago
xmpp_stanza_t *query = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, "urn:xmpp:mam:2");
xmpp_stanza_t *x = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(x, "x");
xmpp_stanza_set_ns(x, "jabber:x:data");
xmpp_stanza_set_attribute(x, "type", "result");
xmpp_stanza_t *field, *value, *text;
{
field = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(field, "field");
xmpp_stanza_set_attribute(field, "var", "FORM_TYPE");
xmpp_stanza_set_attribute(field, "type", "hidden");
value = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(value, "value");
text = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_text(text, "urn:xmpp:mam:2");
xmpp_stanza_add_child(value, text);
xmpp_stanza_release(text);
xmpp_stanza_add_child(field, value);
xmpp_stanza_release(value);
xmpp_stanza_add_child(x, field);
xmpp_stanza_release(field);
}
{
field = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(field, "field");
xmpp_stanza_set_attribute(field, "var", "with");
value = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(value, "value");
text = xmpp_stanza_new(account.context);
xmpp_stanza_set_text(text, id);
3 years ago
xmpp_stanza_add_child(value, text);
xmpp_stanza_release(text);
xmpp_stanza_add_child(field, value);
xmpp_stanza_release(value);
xmpp_stanza_add_child(x, field);
xmpp_stanza_release(field);
}
if (start)
{
field = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(field, "field");
xmpp_stanza_set_attribute(field, "var", "start");
value = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(value, "value");
text = xmpp_stanza_new(account.context);
3 years ago
char time[256] = {0};
strftime(time, sizeof(time), "%Y-%m-%dT%H:%M:%SZ", gmtime(start));
xmpp_stanza_set_text(text, time);
xmpp_stanza_add_child(value, text);
xmpp_stanza_release(text);
xmpp_stanza_add_child(field, value);
xmpp_stanza_release(value);
xmpp_stanza_add_child(x, field);
xmpp_stanza_release(field);
}
if (end)
{
field = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(field, "field");
xmpp_stanza_set_attribute(field, "var", "end");
value = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(value, "value");
text = xmpp_stanza_new(account.context);
3 years ago
char time[256] = {0};
strftime(time, sizeof(time), "%Y-%m-%dT%H:%M:%SZ", gmtime(end));
xmpp_stanza_set_text(text, time);
xmpp_stanza_add_child(value, text);
xmpp_stanza_release(text);
xmpp_stanza_add_child(field, value);
xmpp_stanza_release(value);
xmpp_stanza_add_child(x, field);
xmpp_stanza_release(field);
}
xmpp_stanza_add_child(query, x);
xmpp_stanza_release(x);
3 years ago
if (after)
{
xmpp_stanza_t *set, *set__after, *set__after__text;
set = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(set, "set");
xmpp_stanza_set_ns(set, "http://jabber.org/protocol/rsm");
set__after = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_name(set__after, "after");
set__after__text = xmpp_stanza_new(account.context);
3 years ago
xmpp_stanza_set_text(set__after__text, after);
xmpp_stanza_add_child(set__after, set__after__text);
xmpp_stanza_release(set__after__text);
xmpp_stanza_add_child(set, set__after);
xmpp_stanza_release(set__after);
xmpp_stanza_add_child(query, set);
xmpp_stanza_release(set);
}
else
account.add_mam_query(id, xmpp_stanza_get_id(iq), { *start }, { *end });
3 years ago
3 years ago
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
xmpp_send(account.connection, iq);
3 years ago
xmpp_stanza_release(iq);
}