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.

174 lines
5.2 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>
#include <stdint.h>
3 years ago
#include <string.h>
#include <stdio.h>
#include <strophe.h>
#include <weechat/weechat-plugin.h>
3 years ago
#include "plugin.hh"
#include "account.hh"
#include "user.hh"
#include "channel.hh"
3 years ago
std::string weechat::user::get_colour()
3 years ago
{
return weechat::user::get_colour(this->profile.display_name);
3 years ago
}
std::string weechat::user::get_colour(const char *name)
3 years ago
{
return weechat_info_get("nick_color", name);
3 years ago
}
std::string weechat::user::get_colour_for_nicklist()
3 years ago
{
return weechat::user::get_colour_for_nicklist(this->profile.display_name);
}
std::string weechat::user::get_colour_for_nicklist(const char *name)
{
return weechat_info_get("nick_color_name", name);
}
std::string weechat::user::as_prefix_raw()
{
return weechat::user::as_prefix_raw(this->profile.display_name);
}
3 years ago
std::string weechat::user::as_prefix_raw(const char *name)
{
static char result[2048];
3 years ago
3 years ago
snprintf(result, sizeof(result), "%s%s%s",
3 years ago
weechat_info_get("nick_color", name),
3 years ago
name, weechat_color("reset"));
3 years ago
return result;
}
std::string weechat::user::as_prefix()
3 years ago
{
return weechat::user::as_prefix(this->profile.display_name);
}
3 years ago
std::string weechat::user::as_prefix(const char *name)
{
static char result[2048];
3 years ago
snprintf(result, sizeof(result), "%s%s\t",
weechat::user::get_colour(name).data(), name);
3 years ago
return result;
}
weechat::user *weechat::user::bot_search(weechat::account *account,
const char *pgp_id)
3 years ago
{
3 years ago
if (!account || !pgp_id)
return nullptr;
3 years ago
for (auto& ptr_user : account->users)
3 years ago
{
if (ptr_user.second.profile.pgp_id &&
ptr_user.second.profile.pgp_id == pgp_id)
return &ptr_user.second;
3 years ago
}
return nullptr;
3 years ago
}
weechat::user *weechat::user::search(weechat::account *account,
const char *id)
3 years ago
{
if (!account || !id)
return nullptr;
3 years ago
if (auto user = account->users.find(id); user != account->users.end())
return &user->second;
3 years ago
return nullptr;
3 years ago
}
void weechat::user::nicklist_add(weechat::account *account,
weechat::channel *channel)
3 years ago
{
struct t_gui_nick_group *ptr_group;
struct t_gui_buffer *ptr_buffer;
char *name = channel ? this->profile.display_name : this->id;
if (channel && weechat_strcasecmp(xmpp_jid_bare(account->context, name),
channel->id.data()) == 0)
name = xmpp_jid_resource(account->context, name);
3 years ago
ptr_buffer = channel ? channel->buffer : account->buffer;
3 years ago
char *group = (char*)"...";
if (this->profile.affiliation ? this->profile.affiliation == std::string("outcast") : false)
3 years ago
group = (char*)"!";
if (this->profile.role ? this->profile.role == std::string("visitor") : false)
3 years ago
group = (char*)"?";
if (this->profile.role ? this->profile.role == std::string("participant") : false)
3 years ago
group = (char*)"+";
if (this->profile.affiliation ? this->profile.affiliation == std::string("member") : false)
3 years ago
group = (char*)"%";
if (this->profile.role ? this->profile.role == std::string("moderator") : false)
3 years ago
group = (char*)"@";
if (this->profile.affiliation ? this->profile.affiliation == std::string("admin") : false)
3 years ago
group = (char*)"&";
if (this->profile.affiliation ? this->profile.affiliation == std::string("owner") : false)
3 years ago
group = (char*)"~";
ptr_group = weechat_nicklist_search_group(ptr_buffer, nullptr, group);
3 years ago
weechat_nicklist_add_nick(ptr_buffer, ptr_group,
name,
this->is_away ?
3 years ago
"weechat.color.nicklist_away" :
get_colour_for_nicklist().data(),
3 years ago
group,
3 years ago
"bar_fg",
1);
}
void weechat::user::nicklist_remove(weechat::account *account,
weechat::channel *channel)
{
3 years ago
struct t_gui_nick *ptr_nick;
struct t_gui_buffer *ptr_buffer;
char *name = this->profile.display_name;
if (channel && weechat_strcasecmp(xmpp_jid_bare(account->context, name),
channel->id.data()) == 0)
name = xmpp_jid_resource(account->context, name);
ptr_buffer = channel ? channel->buffer : account->buffer;
if (name && (ptr_nick = weechat_nicklist_search_nick(ptr_buffer, nullptr, name)))
3 years ago
weechat_nicklist_remove_nick(ptr_buffer, ptr_nick);
}
weechat::user::user(weechat::account *account, weechat::channel *channel,
const char *id, const char *display_name)
3 years ago
{
3 years ago
if (!account || !id)
3 years ago
{
throw nullptr;
3 years ago
}
if (account->users.empty() && channel)
channel->add_nicklist_groups();
3 years ago
weechat::user *ptr_user = user::search(account, id);
3 years ago
if (ptr_user)
{
throw nullptr;
3 years ago
}
this->id = strdup(id);
3 years ago
this->profile.display_name = display_name ?
3 years ago
strdup(display_name) : strdup("");
3 years ago
nicklist_add(account, nullptr);
3 years ago
}