127 lines
9.2 KiB
C++
127 lines
9.2 KiB
C++
#pragma once
|
|
#include "gateway/source_name_fold.hpp"
|
|
#include "gateway/chat.hpp"
|
|
#include "gateway/guild_bank.hpp"
|
|
#include "gateway/legacy_stream.hpp"
|
|
#include <algorithm>
|
|
namespace gateway {
|
|
inline bool native_guild_management_opcode(uint32_t op){return op==0x2f0000 || op==0x2f0001 || op==0x2f0004 || op==0x2f0013 || op==0x2f0008 || op==0x2f0005 || op==0x2f0015 || op==0x2f0016 || op==0x4400f7 || op==0x2f0009 || op==0x2f0006;}
|
|
// /gkick by name (owner 2026-11-26): Tortoise sends one SMSG_GUILD_ROSTER capped at 0x8000 bytes (online first, then
|
|
// offline by rank; no paging), so large guilds' low-rank offline members never reach the native roster and the
|
|
// native Uninvite (roster GUID) cannot name them. The 1.12 client sends the typed name; so does this:
|
|
// Tortoise CMSG_GUILD_REMOVE 0x08E {cstring name}. Tortoise normalises the name and checks membership, rights,
|
|
// guild master and rank order, answering with its own SMSG_GUILD_COMMAND_RESULT.
|
|
inline LegacyPacket source_guild_remove_by_name(std::string_view name){
|
|
if(name.empty() || name.size()>48 || std::any_of(name.begin(),name.end(),[](unsigned char c){return c<=32 || c==127;}))
|
|
throw Error("guild remove name bound");
|
|
Bytes out;cstring(out,std::string(name));return {0x8e,std::move(out)};
|
|
}
|
|
// Opt-in native70334 candidate: original API/known opcode profile and Trinity
|
|
// writer grammar; Source identity/rights remain authoritative. No DB access.
|
|
inline LegacyPacket source_guild_management(uint32_t opcode,std::span<const uint8_t> body,
|
|
const GuildInfo& info,const GuildRoster& roster,uint64_t self,uint16_t realm,uint32_t self_rank){
|
|
if(!info.id || !self || !realm || roster.source_rank_rights.empty() || self_rank>=roster.source_rank_rights.size())throw Error("guild management context absent");
|
|
const auto me=std::find_if(roster.members.begin(),roster.members.end(),[&](const auto& m){return m.guid==self && m.rank==self_rank;});
|
|
if(me==roster.members.end())throw Error("guild management self absent");
|
|
const auto permission=roster.source_rank_rights[self_rank];
|
|
auto text=[](const std::string& value,size_t bound,bool nonempty){
|
|
if((nonempty && value.empty()) || value.size()>bound || std::any_of(value.begin(),value.end(),[](unsigned char c){return c<32 || c==127;}))throw Error("guild management text bound");
|
|
};
|
|
Reader r(body);Bytes out;
|
|
if(opcode==0x2f0015 || opcode==0x2f0016){
|
|
const bool motd=opcode==0x2f0015;
|
|
if(!(permission&(motd?0x1000u:0x10000u)))throw Error("Source guild text edit rights");
|
|
// Exact original70334 writers9c3d20/9c3fd0: byte length11bits,
|
|
// flush five zero padding bits, then text. Source CString has no NUL.
|
|
const auto first=r.le(1),second=r.le(1);const auto bits=(first<<8)|second;
|
|
if(bits&31)throw Error("guild text padding");
|
|
auto bytes=r.take(bits>>5);r.end();
|
|
if(bytes.size()>(motd?128u:500u)||std::find(bytes.begin(),bytes.end(),0)!=bytes.end())throw Error("Source guild text bound");
|
|
append(out,bytes);le(out,0,1);return {motd?0x91u:0x2fcu,std::move(out)};
|
|
}
|
|
if(opcode==0x4400f7){
|
|
// GuildSetGuildMaster {bits9 name length, name} (TC GuildPackets.cpp:918) -> Tortoise CMSG_GUILD_LEADER
|
|
// 0x90 {cstring}: only the Source leader may transfer, to another member of the current own roster.
|
|
if(self_rank!=0)throw Error("only the Source guild master may transfer leadership");
|
|
const auto first=r.le(1),second=r.le(1);if(second&0x7f)throw Error("guild master name padding");
|
|
const auto length=(first<<1)|(second>>7);auto bytes=r.take(length);r.end();
|
|
std::string name(bytes.begin(),bytes.end());text(name,48,true);
|
|
// Tortoise normalizePlayerName folds case before its lookup; match the roster the same way, send its name.
|
|
const auto wanted=tortoise_fold_name(name);if(!wanted)throw Error("new guild master name encoding");
|
|
const auto member=std::find_if(roster.members.begin(),roster.members.end(),[&](const auto& m){return tortoise_fold_name(m.name)==wanted;});
|
|
if(member==roster.members.end() || member->guid==self)throw Error("new guild master outside current own roster");
|
|
cstring(out,member->name);return {0x90,std::move(out)};
|
|
}
|
|
if(opcode==0x2f0009){
|
|
// #91 GuildDelete (/gdisband, empty) -> Tortoise CMSG_GUILD_DISBAND 0x08F (empty). Tortoise checks the
|
|
// guild master itself and answers with its own guild events.
|
|
r.end();if(self_rank!=0)throw Error("only the Source guild master may disband");
|
|
return {0x8f,std::move(out)};
|
|
}
|
|
if(opcode==0x2f0006){
|
|
// #91 GuildDeleteRank {int32 RankOrder} -> Tortoise CMSG_GUILD_DEL_RANK 0x233 (empty): Tortoise only removes
|
|
// its lowest rank (and refuses while members hold it), so only that rank is translated.
|
|
const auto order=r.le(4);r.end();
|
|
if(self_rank!=0 || roster.source_rank_rights.size()<=5 || order+1!=roster.source_rank_rights.size())
|
|
throw Error("Source guild deletes only its lowest rank (guild master, more than five ranks)");
|
|
return {0x233,std::move(out)};
|
|
}
|
|
if(opcode==0x2f0005){
|
|
if(self_rank!=0 || roster.source_rank_rights.size()>=10)throw Error("Source guild rank creation rights/count");
|
|
for(size_t i=0;i<info.ranks.size();++i)
|
|
if((i<roster.source_rank_rights.size())!=!info.ranks[i].empty())throw Error("Source rank name/rights snapshot differs");
|
|
const auto length=r.le(1);if(length&1)throw Error("guild add rank name padding");
|
|
// Original70334 writer: 7-bit byte length, int32 RankOrder, name.
|
|
// Original reference HandleGuildAddRank ignores RankOrder and appends,
|
|
// exactly as Source HandleGuildAddRank/CreateRank does.
|
|
(void)r.le(4);
|
|
auto bytes=r.take(length>>1);r.end();std::string name(bytes.begin(),bytes.end());text(name,25,true);
|
|
cstring(out,name);return {0x232,std::move(out)};
|
|
}
|
|
if(opcode==0x2f0008){
|
|
if(self_rank!=0)throw Error("only Source guild master may edit rank permissions");
|
|
auto rank=r.le(1),order=r.le(4),flags=r.le(4),gold=r.le(4);
|
|
if(rank>=roster.source_rank_rights.size() || order!=rank || gold!=(rank==0?0xffffffffu:0u))
|
|
throw Error("guild rank mutation identity/bank hold");
|
|
for(unsigned i=0;i<16;++i)if(r.le(4)!=0)throw Error("unimplemented guild bank rank mutation");
|
|
const auto length=r.le(1);if(length&1)throw Error("guild rank name padding");
|
|
auto bytes=r.take(length>>1);std::string name(bytes.begin(),bytes.end());text(name,25,true);
|
|
const auto old_flags=r.le(4);r.end();
|
|
const auto old_source=roster.source_rank_rights[rank];
|
|
const auto expected=forever_guild_rank_flags(rank,old_source);
|
|
constexpr uint32_t shared=0x1u|0x2u|0x4u|0x8u|0x10u|0x20u|0x80u|0x100u|0x1000u|0x2000u|0x4000u|0x8000u|0x10000u;
|
|
if(old_flags!=expected || ((flags^expected)&~shared) || (rank==0 && flags!=expected))
|
|
throw Error("stale or unsupported guild rank permissions");
|
|
if(info.ranks[rank].empty())throw Error("guild rank names absent");
|
|
// Source permits renaming its leader rank, but forces its rights ALL.
|
|
// Accept only unchanged native flags/limits for rank0; never translate
|
|
// this into a bank update or a guildmaster permission reduction.
|
|
le(out,rank,4);le(out,rank==0?old_source:((old_source&~shared)|(flags&shared)|0x40u),4);cstring(out,name);
|
|
return {0x231,std::move(out)};
|
|
}
|
|
const auto guid=unpack_guid(r,16);
|
|
const auto member=std::find_if(roster.members.begin(),roster.members.end(),[&](const auto& m){auto expected=forever_player_guid(m.guid,realm);return std::equal(guid.begin(),guid.end(),expected.begin());});
|
|
if(member==roster.members.end())throw Error("guild management target outside current own roster");
|
|
text(member->name,48,true);cstring(out,member->name);
|
|
if(opcode==0x2f0004){
|
|
// CMSG_GUILD_OFFICER_REMOVE_MEMBER {ObjectGuid Removee} (/gkick, roster Remove) -> Tortoise CMSG_GUILD_REMOVE
|
|
// 0x08E {cstring name}. Tortoise checks GR_RIGHT_REMOVE, guild master and rank order and answers with its own
|
|
// SMSG_GUILD_COMMAND_RESULT, so only the identity is resolved here (offline members included).
|
|
r.end();if(member->guid==self)throw Error("guild remove targets self (use leave)");
|
|
return {0x8eu,std::move(out)};
|
|
}
|
|
if(opcode==0x2f0000 || opcode==0x2f0001){
|
|
r.end();const bool promote=opcode==0x2f0000;
|
|
if(member->guid==self || !(permission&(promote?0x80u:0x100u)) ||
|
|
(promote?self_rank+1>=member->rank:self_rank>=member->rank || member->rank+1>=roster.source_rank_rights.size()))throw Error("Source guild promotion/demotion rights or hierarchy");
|
|
return {promote?0x8bu:0x8cu,std::move(out)};
|
|
}
|
|
if(opcode!=0x2f0013)throw Error("unsupported guild management action");
|
|
const auto length=r.le(1),bits=r.le(1);if(bits&0x7f)throw Error("guild note padding");
|
|
const bool public_note=bits&0x80;
|
|
if(!(permission&(public_note?0x2000u:0x8000u)))throw Error("Source guild note rights absent");
|
|
auto bytes=r.take(length);r.end();std::string note(bytes.begin(),bytes.end());text(note,31,false);cstring(out,note);
|
|
return {public_note?0x234u:0x235u,std::move(out)};
|
|
}
|
|
}
|