From 0ed3ef9dee2cb8f4b0c37da218a2fd1eee8c31f6 Mon Sep 17 00:00:00 2001 From: danieldg Date: Sun, 13 Sep 2009 20:32:03 +0000 Subject: Add SerializeFormat for easier metadata formatting git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11704 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/extensible.h | 46 ++++++++++++++++++++++++++------- include/modules.h | 7 ----- src/base.cpp | 25 +++++++++++++++--- src/modules.cpp | 1 - src/modules/m_callerid.cpp | 11 ++++---- src/modules/m_check.cpp | 13 +--------- src/modules/m_invisible.cpp | 2 +- src/modules/m_spanningtree/main.cpp | 11 -------- src/modules/m_spanningtree/main.h | 1 - src/modules/m_spanningtree/netburst.cpp | 4 +-- src/modules/m_sslinfo.cpp | 4 +-- 11 files changed, 69 insertions(+), 56 deletions(-) diff --git a/include/extensible.h b/include/extensible.h index 0a33470b5..3f5998231 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -1,6 +1,18 @@ class Extensible; class Module; +enum SerializeFormat +{ + /** Shown to a human (does not need to be unserializable) */ + FORMAT_USER, + /** Passed internally to this process (i.e. for /RELOADMODULE) */ + FORMAT_INTERNAL, + /** Passed to other servers on the network (i.e. METADATA s2s command) */ + FORMAT_NETWORK, + /** Stored on disk (i.e. permchannel database) */ + FORMAT_PERSIST +}; + /** Class represnting an extension of some object */ class CoreExport ExtensionItem @@ -9,10 +21,19 @@ class CoreExport ExtensionItem const std::string key; Module* const owner; ExtensionItem(const std::string& key, Module* owner); - /** Serialize this item into a string */ - virtual std::string serialize(Module* requestor, const Extensible* container, void* item) = 0; - /** Convert the string form back into an item */ - virtual void unserialize(Module* requestor, Extensible* container, const std::string& value) = 0; + /** Serialize this item into a string + * + * @param format The format to serialize to + * @param container The object containing this item + * @param item The item itself + */ + virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item) = 0; + /** Convert the string form back into an item + * @param format The format to serialize from (not FORMAT_USER) + * @param container The object that this item applies to + * @param value The return from a serialize() call that was run elsewhere with this key + */ + virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value) = 0; /** Free the item */ virtual void free(void* item) = 0; @@ -71,9 +92,8 @@ class CoreExport LocalExtItem : public ExtensionItem { public: LocalExtItem(const std::string& key, Module* owner); - // this is deliberately NOT virtual; don't subclass LocalExtItem if you want to sync data! - std::string serialize(Module* requestor, const Extensible* container, void* item); - void unserialize(Module* requestor, Extensible* container, const std::string& value); + virtual std::string serialize(SerializeFormat format, const Extensible* container, void* item); + virtual void unserialize(SerializeFormat format, Extensible* container, const std::string& value); virtual void free(void* item) = 0; }; @@ -126,12 +146,18 @@ class CoreExport SimpleExtItem : public LocalExtItem } }; -typedef SimpleExtItem LocalStringExt; +class CoreExport LocalStringExt : public SimpleExtItem +{ + public: + LocalStringExt(const std::string& key, Module* owner); + std::string serialize(SerializeFormat format, const Extensible* container, void* item); +}; class CoreExport LocalIntExt : public LocalExtItem { public: LocalIntExt(const std::string& key, Module* owner); + std::string serialize(SerializeFormat format, const Extensible* container, void* item); intptr_t get(const Extensible* container); intptr_t set(Extensible* container, intptr_t value); void free(void* item); @@ -142,8 +168,8 @@ class CoreExport StringExtItem : public ExtensionItem public: StringExtItem(const std::string& key, Module* owner); std::string* get(const Extensible* container); - std::string serialize(Module* requestor, const Extensible* container, void* item); - void unserialize(Module* requestor, Extensible* container, const std::string& value); + std::string serialize(SerializeFormat format, const Extensible* container, void* item); + void unserialize(SerializeFormat format, Extensible* container, const std::string& value); void set(Extensible* container, const std::string& value); void unset(Extensible* container); void free(void* item); diff --git a/include/modules.h b/include/modules.h index 4e960ec64..06c0f1dd5 100644 --- a/include/modules.h +++ b/include/modules.h @@ -857,13 +857,6 @@ class CoreExport Module : public Extensible */ virtual void ProtoSendMetaData(void* opaque, Extensible* target, const std::string &extname, const std::string &extdata); - /** - * Implemented by all modules that implement ProtoSendMetaData. - * Translates the item into a string format suitable for sending to other servers. - * Currently, this just translates nicks to their UID and channels to their name - */ - virtual std::string ProtoTranslate(Extensible* item); - /** Called after every WALLOPS command. * @param user The user sending the WALLOPS * @param text The content of the WALLOPS message diff --git a/src/base.cpp b/src/base.cpp index 4d0c17077..1e4f746af 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -147,19 +147,36 @@ LocalExtItem::LocalExtItem(const std::string& Key, Module* mod) : ExtensionItem( { } -std::string LocalExtItem::serialize(Module* requestor, const Extensible* container, void* item) +std::string LocalExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) { return ""; } -void LocalExtItem::unserialize(Module* requestor, Extensible* container, const std::string& value) +void LocalExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value) { } +LocalStringExt::LocalStringExt(const std::string& Key, Module* Owner) + : SimpleExtItem(Key, Owner) { } + +std::string LocalStringExt::serialize(SerializeFormat format, const Extensible* container, void* item) +{ + if (item && format == FORMAT_USER) + return *static_cast(item); + return ""; +} + LocalIntExt::LocalIntExt(const std::string& Key, Module* mod) : LocalExtItem(Key, mod) { } +std::string LocalIntExt::serialize(SerializeFormat format, const Extensible* container, void* item) +{ + if (format != FORMAT_USER) + return ""; + return ConvToStr(reinterpret_cast(item)); +} + intptr_t LocalIntExt::get(const Extensible* container) { return reinterpret_cast(get_raw(container)); @@ -186,12 +203,12 @@ std::string* StringExtItem::get(const Extensible* container) return static_cast(get_raw(container)); } -std::string StringExtItem::serialize(Module* requestor, const Extensible* container, void* item) +std::string StringExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) { return item ? *static_cast(item) : ""; } -void StringExtItem::unserialize(Module* requestor, Extensible* container, const std::string& value) +void StringExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value) { if (value.empty()) unset(container); diff --git a/src/modules.cpp b/src/modules.cpp index 9664e817f..32d75f60e 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -176,7 +176,6 @@ void Module::OnSyncNetwork(Module*, void*) { } void Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector&, const std::vector&) { } void Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { } void Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { } -std::string Module::ProtoTranslate(Extensible*) { return "?"; } void Module::OnWallops(User*, const std::string&) { } void Module::OnChangeHost(User*, const std::string&) { } void Module::OnChangeName(User*, const std::string&) { } diff --git a/src/modules/m_callerid.cpp b/src/modules/m_callerid.cpp index b84ee2c9c..dab0d3d43 100644 --- a/src/modules/m_callerid.cpp +++ b/src/modules/m_callerid.cpp @@ -56,14 +56,15 @@ class callerid_data : public classbase } } - std::string ToString(Module* proto) const + std::string ToString(SerializeFormat format) const { std::ostringstream oss; oss << lastnotify; for (std::set::const_iterator i = accepting.begin(); i != accepting.end(); ++i) { + User* u = *i; // Encode UIDs. - oss << "," << proto->ProtoTranslate(*i); + oss << "," << (format == FORMAT_USER ? u->nick : u->uuid); } oss << std::ends; return oss.str(); @@ -77,13 +78,13 @@ struct CallerIDExtInfo : public ExtensionItem { } - std::string serialize(Module* requestor, const Extensible* container, void* item) + std::string serialize(SerializeFormat format, const Extensible* container, void* item) { callerid_data* dat = static_cast(item); - return dat->ToString(requestor); + return dat->ToString(format); } - void unserialize(Module* requestor, Extensible* container, const std::string& value) + void unserialize(SerializeFormat format, Extensible* container, const std::string& value) { callerid_data* dat = new callerid_data(value); set_raw(container, dat); diff --git a/src/modules/m_check.cpp b/src/modules/m_check.cpp index 33f950b4c..847e9d5c9 100644 --- a/src/modules/m_check.cpp +++ b/src/modules/m_check.cpp @@ -41,7 +41,7 @@ class CommandCheck : public Command ExtensionItem* item = Extensible::GetItem(i->first); std::string value; if (item) - value = item->serialize(creator, ext, i->second); + value = item->serialize(FORMAT_USER, ext, i->second); if (value.empty()) dumpkeys << " " << i->first; else @@ -211,17 +211,6 @@ class ModuleCheck : public Module { return Version("CHECK command, view user/channel details", VF_VENDOR|VF_OPTCOMMON); } - - std::string ProtoTranslate(Extensible* item) - { - User* u = dynamic_cast(item); - Channel* c = dynamic_cast(item); - if (u) - return u->nick; - if (c) - return c->name; - return "?"; - } }; MODULE_INIT(ModuleCheck) diff --git a/src/modules/m_invisible.cpp b/src/modules/m_invisible.cpp index ae54fa96b..068e6d666 100644 --- a/src/modules/m_invisible.cpp +++ b/src/modules/m_invisible.cpp @@ -53,7 +53,7 @@ class InvisibleMode : public ModeHandler snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost().c_str(), adding ? "PART" : "JOIN", (*f)->name.c_str()); std::string out = tb; - std::string n = this->ServerInstance->Modes->ModeString(dest, (*f)); + std::string n = ServerInstance->Modes->ModeString(dest, (*f)); for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++) { diff --git a/src/modules/m_spanningtree/main.cpp b/src/modules/m_spanningtree/main.cpp index 3877daf42..ee8c82c8b 100644 --- a/src/modules/m_spanningtree/main.cpp +++ b/src/modules/m_spanningtree/main.cpp @@ -932,17 +932,6 @@ void ModuleSpanningTree::ProtoSendMetaData(void* opaque, Extensible* target, con s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata); } -std::string ModuleSpanningTree::ProtoTranslate(Extensible* item) -{ - User* u = dynamic_cast(item); - Channel* c = dynamic_cast(item); - if (u) - return u->uuid; - if (c) - return c->name; - return "*"; -} - void ModuleSpanningTree::OnEvent(Event* event) { if ((event->GetEventID() == "send_encap") || (event->GetEventID() == "send_metadata") || (event->GetEventID() == "send_topic") || (event->GetEventID() == "send_mode") || (event->GetEventID() == "send_mode_explicit") || (event->GetEventID() == "send_opers") diff --git a/src/modules/m_spanningtree/main.h b/src/modules/m_spanningtree/main.h index 1d3ec9d39..0144eb4a3 100644 --- a/src/modules/m_spanningtree/main.h +++ b/src/modules/m_spanningtree/main.h @@ -187,7 +187,6 @@ class ModuleSpanningTree : public Module ModResult OnSetAway(User* user, const std::string &awaymsg); void ProtoSendMode(void* opaque, TargetTypeFlags target_type, void* target, const std::vector &modeline, const std::vector &translate); void ProtoSendMetaData(void* opaque, Extensible* target, const std::string &extname, const std::string &extdata); - std::string ProtoTranslate(Extensible* item); void OnEvent(Event* event); void OnLoadModule(Module* mod,const std::string &name); void OnUnloadModule(Module* mod,const std::string &name); diff --git a/src/modules/m_spanningtree/netburst.cpp b/src/modules/m_spanningtree/netburst.cpp index 3ed85ea1e..ada10e6a1 100644 --- a/src/modules/m_spanningtree/netburst.cpp +++ b/src/modules/m_spanningtree/netburst.cpp @@ -225,7 +225,7 @@ void TreeSocket::SendChannelModes(TreeServer* Current) ExtensionItem* item = Extensible::GetItem(i->first); std::string value; if (item) - value = item->serialize(Utils->Creator, c->second, i->second); + value = item->serialize(FORMAT_NETWORK, c->second, i->second); if (!value.empty()) Utils->Creator->ProtoSendMetaData(this, c->second, i->first, value); } @@ -276,7 +276,7 @@ void TreeSocket::SendUsers(TreeServer* Current) ExtensionItem* item = Extensible::GetItem(i->first); std::string value; if (item) - value = item->serialize(Utils->Creator, u->second, i->second); + value = item->serialize(FORMAT_NETWORK, u->second, i->second); if (!value.empty()) Utils->Creator->ProtoSendMetaData(this, u->second, i->first, value); } diff --git a/src/modules/m_sslinfo.cpp b/src/modules/m_sslinfo.cpp index bf02ed879..952fcf4a1 100644 --- a/src/modules/m_sslinfo.cpp +++ b/src/modules/m_sslinfo.cpp @@ -29,12 +29,12 @@ class SSLCertExt : public ExtensionItem { delete old; } - std::string serialize(Module* requestor, const Extensible* container, void* item) + std::string serialize(SerializeFormat format, const Extensible* container, void* item) { return static_cast(item)->GetMetaLine(); } - void unserialize(Module* requestor, Extensible* container, const std::string& value) + void unserialize(SerializeFormat format, Extensible* container, const std::string& value) { ssl_cert* cert = new ssl_cert; set(container, cert); -- cgit v1.2.3