From a124a3783d8e8136b63f188bbd292c391811df54 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Mon, 2 Nov 2015 13:40:15 +0100 Subject: core_channel Implement invite (un)serialization --- include/modules/invite.h | 7 +++++++ src/coremods/core_channel/invite.cpp | 31 +++++++++++++++++++++++++++++++ src/coremods/core_channel/invite.h | 18 +++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/include/modules/invite.h b/include/modules/invite.h index ab907e970..e53d5202f 100644 --- a/include/modules/invite.h +++ b/include/modules/invite.h @@ -100,6 +100,13 @@ class Invite::Invite : public insp::intrusive_list_node, publ */ bool IsTimed() const { return (expiretimer != NULL); } + /** Serialize this object + * @param format Serialization format + * @param show_chans True to include channel in the output, false to include the nick/uuid + * @param out Output will be appended to this string + */ + void Serialize(SerializeFormat format, bool show_chans, std::string& out); + friend class APIImpl; private: diff --git a/src/coremods/core_channel/invite.cpp b/src/coremods/core_channel/invite.cpp index 253c571ca..7ac662edc 100644 --- a/src/coremods/core_channel/invite.cpp +++ b/src/coremods/core_channel/invite.cpp @@ -38,6 +38,11 @@ void RemoveInvite(Invite::Invite* inv, bool remove_user, bool remove_chan) apiimpl->Destruct(inv, remove_user, remove_chan); } +void UnserializeInvite(LocalUser* user, const std::string& str) +{ + apiimpl->Unserialize(user, str); +} + Invite::APIBase::APIBase(Module* parent) : DataProvider(parent, "core_channel_invite") { @@ -150,6 +155,17 @@ const Invite::List* Invite::APIImpl::GetList(LocalUser* user) return NULL; } +void Invite::APIImpl::Unserialize(LocalUser* user, const std::string& value) +{ + irc::spacesepstream ss(value); + for (std::string channame, exptime; (ss.GetToken(channame) && ss.GetToken(exptime)); ) + { + Channel* chan = ServerInstance->FindChan(channame); + if (chan) + Create(user, chan, ConvToInt(exptime)); + } +} + Invite::Invite::Invite(LocalUser* u, Channel* c) : user(u) , chan(c) @@ -163,6 +179,21 @@ Invite::Invite::~Invite() ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Invite::~ %p", (void*) this); } +void Invite::Invite::Serialize(SerializeFormat format, bool show_chans, std::string& out) +{ + if (show_chans) + out.append(this->chan->name); + else + out.append((format == FORMAT_USER) ? user->nick : user->uuid); + out.push_back(' '); + + if (expiretimer) + out.append(ConvToStr(expiretimer->GetTrigger())); + else + out.push_back('0'); + out.push_back(' '); +} + InviteExpireTimer::InviteExpireTimer(Invite::Invite* invite, time_t timeout) : Timer(timeout) , inv(invite) diff --git a/src/coremods/core_channel/invite.h b/src/coremods/core_channel/invite.h index f11ff9043..2a99ec2df 100644 --- a/src/coremods/core_channel/invite.h +++ b/src/coremods/core_channel/invite.h @@ -40,6 +40,7 @@ namespace Invite } extern void RemoveInvite(Invite::Invite* inv, bool remove_user, bool remove_chan); +extern void UnserializeInvite(LocalUser* user, const std::string& value); template class Invite::ExtItem : public ExtensionItem @@ -84,11 +85,25 @@ class Invite::ExtItem : public ExtensionItem std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE { - return std::string(); + if (format == FORMAT_NETWORK) + return std::string(); + + std::string ret; + Store* store = static_cast*>(item); + for (typename insp::intrusive_list::iterator i = store->invites.begin(); i != store->invites.end(); ++i) + { + Invite* inv = *i; + inv->Serialize(format, (ExtType == ExtensionItem::EXT_USER), ret); + } + if (!ret.empty()) + ret.erase(ret.length()-1); + return ret; } void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE { + if ((ExtType != ExtensionItem::EXT_CHANNEL) && (format != FORMAT_NETWORK)) + UnserializeInvite(static_cast(container), value); } }; @@ -108,4 +123,5 @@ class Invite::APIImpl : public APIBase void RemoveAll(LocalUser* user) { userext.unset(user); } void RemoveAll(Channel* chan) { chanext.unset(chan); } void Destruct(Invite* inv, bool remove_chan = true, bool remove_user = true); + void Unserialize(LocalUser* user, const std::string& value); }; -- cgit v1.2.3