71 lines
5.1 KiB
C++
71 lines
5.1 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#include "contact_name_translation.hpp"
|
|
#include "gateway/chat.hpp"
|
|
#include "gateway/source_team.hpp"
|
|
#include "gateway/turtle_login_profile.hpp"
|
|
#include <algorithm>
|
|
using namespace gateway;
|
|
void NativeContactNames::sync(const NativeSocialTranslation& social,uint64_t owner,uint16_t realm){
|
|
if(!owner||owner>UINT32_MAX||!realm||realm>8191)throw Error("Contact owner bound");
|
|
if(owner_&&(owner_!=owner||realm_!=realm))throw Error("Contact authority crossed World owner");
|
|
owner_=owner;realm_=realm;auto ids=social.contact_name_ids();if(ids.size()>75)throw Error("Contact grant bound");
|
|
for(auto id:mail_senders_)if(ids.size()<75)ids.insert(id);
|
|
for(auto it=grants_.begin();it!=grants_.end();)if(!ids.contains(it->first)){names_.erase(it->first);attempted_.erase(it->first);it=grants_.erase(it);}else ++it;
|
|
for(auto id:ids)if(!grants_.contains(id)){if(next_generation_==UINT64_MAX)throw Error("Contact generation exhausted");grants_[id]=++next_generation_;}
|
|
// Sent queries survive role loss as tombstones until their real reply.
|
|
}
|
|
void NativeContactNames::mail_senders(std::set<uint64_t> ids,const NativeSocialTranslation& social,uint64_t owner,uint16_t realm){
|
|
if(ids.size()>100)throw Error("Mail sender grant bound");
|
|
mail_senders_=std::move(ids);
|
|
sync(social,owner,realm);
|
|
}
|
|
ContactNameProjection NativeContactNames::projection(const Character& p,const NativeSocialTranslation& social,const RosterRealm& realm) const {
|
|
if(realm.realm_id!=realm_||!realm.virtual_address||!turtle_source_roster_pair(p.race,p.cls))throw SocialHold("Contact metadata unavailable in current Source profile");
|
|
validate_social_name(p.name);ContactNameProjection out;out.guid=forever_player_guid(p.guid,realm_);out.virtual_address=realm.virtual_address;
|
|
out.name=p.name;out.race=p.race;out.sex=p.gender;out.cls=p.cls;
|
|
const auto team = source_team_for_race(p.race);
|
|
if (!team) throw SocialHold("Unknown Source contact team");
|
|
out.pvp_faction = *team == SourceTeam::Alliance ? 1 : 0;
|
|
if(auto f=social.source_friend(p.guid);f&&f->status){if(f->level>255)throw SocialHold("Contact level exceeds original name field");out.level=uint8_t(f->level);}
|
|
return out;
|
|
}
|
|
NativeContactNames::Prepared NativeContactNames::client(std::span<const uint8_t> body,const NativeSocialTranslation& social,const RosterRealm& realm) const {
|
|
Prepared out;out.owner=owner_;out.realm=realm_;if(!owner_||realm.realm_id!=realm_)throw Error("Contact names lack World authority");
|
|
for(const auto& guid:native_contact_name_query(body)){
|
|
uint64_t id=0;for(const auto& [candidate,generation]:grants_){(void)generation;if(forever_player_guid(candidate,realm_)==guid){id=candidate;break;}}
|
|
if(!id){out.held=true;continue;}
|
|
if(auto known=names_.find(id);known!=names_.end()){
|
|
try{out.native.push_back(projection(known->second,social,realm));}catch(const SocialHold&){out.held=true;}continue;
|
|
}
|
|
if(pending_.contains(id)){out.held=true;continue;}
|
|
if(pending_.size()+out.source.size()>=75){out.held=true;continue;}
|
|
Bytes query;le(query,id,8);out.source.push_back({0x50,std::move(query)});out.generations[id]=grants_.at(id);
|
|
}return out;
|
|
}
|
|
std::optional<NativeContactNames::Prepared> NativeContactNames::dependency(const RosterRealm& realm) const {
|
|
if(!owner_)return std::nullopt;
|
|
if(realm.realm_id!=realm_||!realm.virtual_address)throw Error("Contact dependency lacks current realm authority");
|
|
if(pending_.size()>=75)return std::nullopt;
|
|
for(const auto& [id,generation]:grants_){
|
|
if(names_.contains(id)||pending_.contains(id))continue;
|
|
auto attempted=attempted_.find(id);
|
|
if(attempted!=attempted_.end()&&attempted->second==generation)continue;
|
|
Prepared out;out.owner=owner_;out.realm=realm_;out.generations[id]=generation;
|
|
Bytes query;le(query,id,8);out.source.push_back({0x50,std::move(query)});
|
|
return out;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
bool NativeContactNames::current(const Prepared& p) const {
|
|
if(p.owner!=owner_||p.realm!=realm_)return false;
|
|
for(auto [id,generation]:p.generations){auto grant=grants_.find(id);if(grant==grants_.end()||grant->second!=generation||pending_.contains(id))return false;}return true;
|
|
}
|
|
void NativeContactNames::sent(const Prepared& p){if(!current(p))throw Error("Contact-name prepared authority retired");for(auto [id,generation]:p.generations){pending_[id]={owner_,generation,realm_};attempted_[id]=generation;}}
|
|
std::optional<ContactNameProjection> NativeContactNames::receive(std::span<const uint8_t> body,const NativeSocialTranslation& social,const RosterRealm& realm){
|
|
if(body.size()<8)return std::nullopt;
|
|
Reader r(body);auto id=r.le(8);auto pending=pending_.find(id);if(pending==pending_.end())return std::nullopt;
|
|
const auto token=pending->second;pending_.erase(pending);auto grant=grants_.find(id);
|
|
if(token.owner!=owner_||token.realm!=realm_||grant==grants_.end()||grant->second!=token.generation)return std::nullopt;
|
|
try{auto player=turtle_chat_player(body);auto out=projection(player,social,realm);names_[id]=std::move(player);return out;}catch(const Error&){return std::nullopt;}
|
|
}
|