diff options
author | Adam <Adam@anope.org> | 2011-09-25 04:19:15 -0400 |
---|---|---|
committer | Adam <Adam@anope.org> | 2011-09-25 04:19:15 -0400 |
commit | 1f2399de364c09adcce4193895cd362d80ffdfc5 (patch) | |
tree | 5f40fc531f22c174b6e10bb7bc12842a4a21d30b /include | |
parent | 43201ead9575a74e350710bc191f4ac67366aca7 (diff) |
Added a new database format and sqlite support. Also moved db-convert to a module.
Diffstat (limited to 'include')
-rw-r--r-- | include/access.h | 3 | ||||
-rw-r--r-- | include/account.h | 14 | ||||
-rw-r--r-- | include/bots.h | 5 | ||||
-rw-r--r-- | include/extensible.h | 160 | ||||
-rw-r--r-- | include/hashcomp.h | 24 | ||||
-rw-r--r-- | include/modules.h | 91 | ||||
-rw-r--r-- | include/oper.h | 8 | ||||
-rw-r--r-- | include/regchannel.h | 49 | ||||
-rw-r--r-- | include/serialize.h | 160 | ||||
-rw-r--r-- | include/services.h | 83 |
10 files changed, 301 insertions, 296 deletions
diff --git a/include/access.h b/include/access.h index 229a16f4b..d86539d8f 100644 --- a/include/access.h +++ b/include/access.h @@ -41,6 +41,9 @@ class CoreExport ChanAccess time_t last_seen; time_t created; + virtual Anope::string serialize_name() = 0; + virtual SerializableBase::serialized_data serialize() = 0; + ChanAccess(AccessProvider *p); virtual ~ChanAccess(); virtual bool Matches(User *u, NickCore *nc) = 0; diff --git a/include/account.h b/include/account.h index 6c6295f5c..3cfb803f8 100644 --- a/include/account.h +++ b/include/account.h @@ -93,9 +93,7 @@ const Anope::string NickCoreFlagStrings[] = { "MEMO_MAIL", "HIDE_STATUS", "SUSPENDED", "AUTOOP", "FORBIDDEN", "UNCONFIRMED", "" }; -class NickCore; - -class CoreExport NickAlias : public Extensible, public Flags<NickNameFlag, NS_END> +class CoreExport NickAlias : public Extensible, public Flags<NickNameFlag, NS_END>, public Serializable<NickAlias> { public: /** Default constructor @@ -118,6 +116,9 @@ class CoreExport NickAlias : public Extensible, public Flags<NickNameFlag, NS_EN NickCore *nc; /* I'm an alias of this */ HostInfo hostinfo; + serialized_data serialize(); + static void unserialize(serialized_data &); + /** Release a nick * See the comment in users.cpp */ @@ -131,7 +132,7 @@ class CoreExport NickAlias : public Extensible, public Flags<NickNameFlag, NS_EN void OnCancel(User *u); }; -class CoreExport NickCore : public Extensible, public Flags<NickCoreFlag, NI_END> +class CoreExport NickCore : public Extensible, public Flags<NickCoreFlag, NI_END>, public Serializable<NickCore> { public: /** Default constructor @@ -153,14 +154,17 @@ class CoreExport NickCore : public Extensible, public Flags<NickCoreFlag, NI_END std::vector<Anope::string> access; /* Access list, vector of strings */ std::vector<Anope::string> cert; /* ssl certificate list, vector of strings */ MemoInfo memos; - uint16 channelcount; /* Number of channels currently registered */ Oper *o; /* Unsaved data */ + uint16 channelcount; /* Number of channels currently registered */ time_t lastmail; /* Last time this nick record got a mail */ std::list<NickAlias *> aliases; /* List of aliases */ + serialized_data serialize(); + static void unserialize(serialized_data &); + /** Checks whether this account is a services oper or not. * @return True if this account is a services oper, false otherwise. */ diff --git a/include/bots.h b/include/bots.h index 20daeddcf..c23a354fc 100644 --- a/include/bots.h +++ b/include/bots.h @@ -32,7 +32,7 @@ enum BotFlag static const Anope::string BotFlagString[] = { "BEGIN", "CORE", "PRIVATE", "CONF", "" }; -class CoreExport BotInfo : public User, public Flags<BotFlag, BI_END> +class CoreExport BotInfo : public User, public Flags<BotFlag, BI_END>, public Serializable<BotInfo> { public: uint32 chancount; @@ -57,6 +57,9 @@ class CoreExport BotInfo : public User, public Flags<BotFlag, BI_END> */ virtual ~BotInfo(); + serialized_data serialize(); + static void unserialize(serialized_data &); + void GenerateUID(); /** Change the nickname for the bot. diff --git a/include/extensible.h b/include/extensible.h index 2d7fb5509..c5de2dae4 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -7,61 +7,28 @@ #ifndef EXTENSIBLE_H #define EXTENSIBLE_H +#include "anope.h" #include "hashcomp.h" -/** Dummy base class we use to cast everything to/from - */ -class ExtensibleItemBase +class ExtensibleItem { public: - ExtensibleItemBase() { } - virtual ~ExtensibleItemBase() { } -}; - -/** Class used to represent an extensible item that doesn't hold a pointer - */ -template<typename T> class ExtensibleItemRegular : public ExtensibleItemBase -{ - protected: - T Item; - - public: - ExtensibleItemRegular(T item) : Item(item) { } - virtual ~ExtensibleItemRegular() { } - T &GetItem() { return Item; } + ExtensibleItem(); + virtual ~ExtensibleItem(); + virtual void OnDelete(); }; -/** Class used to represent an extensible item that holds a pointer - */ -template<typename T> class ExtensibleItemPointer : public ExtensibleItemBase +class ExtensibleString : public Anope::string, public ExtensibleItem { - protected: - T *Item; - public: - ExtensibleItemPointer(T *item) : Item(item) { } - virtual ~ExtensibleItemPointer() { delete Item; } - T *GetItem() { return Item; } -}; - -/** Class used to represent an extensible item that holds a pointer to an arrray - */ -template<typename T> class ExtensibleItemPointerArray : public ExtensibleItemBase -{ - protected: - T *Item; - - public: - ExtensibleItemPointerArray(T *item) : Item(item) { } - virtual ~ExtensibleItemPointerArray() { delete [] Item; } - T *GetItem() { return Item; } + ExtensibleString(const Anope::string &s) : Anope::string(s), ExtensibleItem() { } }; class CoreExport Extensible : public Base { private: - typedef std::map<Anope::string, ExtensibleItemBase *> extensible_map; - extensible_map Extension_Items; + typedef Anope::map<ExtensibleItem *> extensible_map; + extensible_map extension_items; public: /** Default constructor, does nothing @@ -73,9 +40,10 @@ class CoreExport Extensible : public Base */ virtual ~Extensible() { - for (extensible_map::iterator it = Extension_Items.begin(), it_end = Extension_Items.end(); it != it_end; ++it) - delete it->second; - Extension_Items.clear(); + for (extensible_map::iterator it = extension_items.begin(), it_end = extension_items.end(); it != it_end; ++it) + if (it->second) + it->second->OnDelete(); + extension_items.clear(); } /** Extend an Extensible class. @@ -89,26 +57,10 @@ class CoreExport Extensible : public Base * * @return Returns true on success, false if otherwise */ - void Extend(const Anope::string &key, ExtensibleItemBase *p) + void Extend(const Anope::string &key, ExtensibleItem *p) { this->Shrink(key); - this->Extension_Items.insert(std::make_pair(key, p)); - } - - /** Extend an Extensible class. - * - * @param key The key parameter is an arbitary string which identifies the extension data - * - * You must provide a key to store the data as via the parameter 'key', this single-parameter - * version takes no 'data' parameter, this is used purely for boolean values. - * The key will be inserted into the map with a NULL 'data' pointer. If the key already exists - * then you may not insert it twice, Extensible::Extend will return false in this case. - * - * @return Returns true on success, false if otherwise - */ - void Extend(const Anope::string &key) - { - this->Extend(key, new ExtensibleItemPointer<char *>(NULL)); + this->extension_items[key] = p; } /** Shrink an Extensible class. @@ -121,91 +73,43 @@ class CoreExport Extensible : public Base */ bool Shrink(const Anope::string &key) { - extensible_map::iterator it = this->Extension_Items.find(key); - if (it != this->Extension_Items.end()) + extensible_map::iterator it = this->extension_items.find(key); + if (it != this->extension_items.end()) { - delete it->second; + if (it->second != NULL) + it->second->OnDelete(); /* map::size_type map::erase( const key_type& key ); * returns the number of elements removed, std::map * is single-associative so this should only be 0 or 1 */ - return this->Extension_Items.erase(key); - } - - return false; - } - - /** Get an extension item that is not a pointer. - * - * @param key The key parameter is an arbitary string which identifies the extension data - * @param p If you provide a non-existent key, this value will be 0. Otherwise a copy to the item you requested will be placed in this templated parameter. - * @return Returns true if the item was found and false if it was nor, regardless of wether 'p' is NULL. This allows you to store NULL values in Extensible. - */ - template<typename T> bool GetExtRegular(const Anope::string &key, T &p) - { - extensible_map::iterator it = this->Extension_Items.find(key); - - if (it != this->Extension_Items.end()) - { - p = debug_cast<ExtensibleItemRegular<T> *>(it->second)->GetItem(); - return true; + return this->extension_items.erase(key) > 0; } return false; } - /** Get an extension item that is a pointer. - * - * @param key The key parameter is an arbitary string which identifies the extension data - * * @param p If you provide a non-existent key, this value will be NULL. Otherwise a pointer to the item you requested will be placed in this templated parameter. - * @return Returns true if the item was found and false if it was nor, regardless of wether 'p' is NULL. This allows you to store NULL values in Extensible. - */ - template<typename T> bool GetExtPointer(const Anope::string &key, T *&p) - { - extensible_map::iterator it = this->Extension_Items.find(key); - - if (it != this->Extension_Items.end()) - { - p = debug_cast<ExtensibleItemPointer<T> *>(it->second)->GetItem(); - return true; - } - - p = NULL; - return false; - } - - /** Get an extension item that is a pointer to an array + /** Get an extension item. * * @param key The key parameter is an arbitary string which identifies the extension data - * @param p If you provide a non-existent key, this value will be NULL. Otherwise a pointer to the item you requested will be placed in this templated parameter. - * @return Returns true if the item was found and false if it was nor, regardless of wether 'p' is NULL. This allows you to store NULL values in Extensible. + * @return The item found */ - template<typename T> bool GetExtArray(const Anope::string &key, T *&p) + template<typename T> T GetExt(const Anope::string &key) { - extensible_map::iterator it = this->Extension_Items.find(key); + extensible_map::iterator it = this->extension_items.find(key); + if (it != this->extension_items.end()) + return debug_cast<T>(it->second); - if (it != this->Extension_Items.end()) - { - p = debug_cast<ExtensibleItemPointerArray<T> *>(it->second)->GetItem(); - return true; - } - - p = NULL; - return false; + return NULL; } - /** Get an extension item. + /** Check if an extension item exists. * * @param key The key parameter is an arbitary string which identifies the extension data - * @return Returns true if the item was found and false if it was not. - * - * This single-parameter version only checks if the key exists, it does nothing with - * the 'data' field and is probably only useful in conjunction with the single-parameter - * version of Extend(). + * @return True if the item was found. */ - bool GetExt(const Anope::string &key) + bool HasExt(const Anope::string &key) { - return this->Extension_Items.find(key) != this->Extension_Items.end(); + return this->extension_items.count(key) > 0; } /** Get a list of all extension items names. @@ -215,7 +119,7 @@ class CoreExport Extensible : public Base */ void GetExtList(std::deque<Anope::string> &list) { - for (extensible_map::iterator it = Extension_Items.begin(), it_end = Extension_Items.end(); it != it_end; ++it) + for (extensible_map::iterator it = extension_items.begin(), it_end = extension_items.end(); it != it_end; ++it) list.push_back(it->first); } }; diff --git a/include/hashcomp.h b/include/hashcomp.h index 9c1f0d2dc..c36211cde 100644 --- a/include/hashcomp.h +++ b/include/hashcomp.h @@ -316,30 +316,6 @@ namespace std }; } -/* Define operators for using >> and << with irc::string to an ostream on an istream. */ -/* This was endless fun. No. Really. */ -/* It was also the first core change Ommeh made, if anyone cares */ - -/** Operator >> for irc::string - */ -inline std::istream &operator>>(std::istream &is, irc::string &str) -{ - std::string tmp; - is >> tmp; - str = tmp.c_str(); - return is; -} - -/** Operator >> for ci::string - */ -inline std::istream &operator>>(std::istream &is, ci::string &str) -{ - std::string tmp; - is >> tmp; - str = tmp.c_str(); - return is; -} - /* Define operators for + and == with irc::string to std::string for easy assignment * and comparison * diff --git a/include/modules.h b/include/modules.h index d35c1f2ba..00e48250b 100644 --- a/include/modules.h +++ b/include/modules.h @@ -342,11 +342,6 @@ class CoreExport Module : public Extensible */ virtual void OnPostCommand(CommandSource &source, Command *command, const std::vector<Anope::string> ¶ms) { } - /** Called after the core has finished loading the databases, but before - * we connect to the server - */ - virtual void OnPostLoadDatabases() { } - /** Called when the databases are saved * @return EVENT_CONTINUE to let other modules continue saving, EVENT_STOP to stop */ @@ -485,73 +480,6 @@ class CoreExport Module : public Extensible */ virtual void OnServerDisconnect() { } - /** Called when the flatfile dbs are being written - * @param Write A callback to the function used to insert a line into the database - */ - virtual void OnDatabaseWrite(void (*Write)(const Anope::string &)) { } - - /** Called when a line is read from the database - * @param params The params from the database - * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to stop processing - */ - virtual EventReturn OnDatabaseRead(const std::vector<Anope::string> ¶ms) { return EVENT_CONTINUE; } - - /** Called when nickcore metadata is read from the database - * @param nc The nickcore - * @param key The metadata key - * @param params The params from the database - * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to stop processing - */ - virtual EventReturn OnDatabaseReadMetadata(NickCore *nc, const Anope::string &key, const std::vector<Anope::string> ¶ms) { return EVENT_CONTINUE; } - - /** Called when nickcore metadata is read from the database - * @param na The nickalias - * @param key The metadata key - * @param params The params from the database - * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to stop processing - */ - virtual EventReturn OnDatabaseReadMetadata(NickAlias *na, const Anope::string &key, const std::vector<Anope::string> ¶ms) { return EVENT_CONTINUE; } - - /** Called when botinfo metadata is read from the database - * @param bi The botinfo - * @param key The metadata key - * @param params The params from the database - * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to stop processing - */ - virtual EventReturn OnDatabaseReadMetadata(BotInfo *bi, const Anope::string &key, const std::vector<Anope::string> ¶ms) { return EVENT_CONTINUE; } - - /** Called when chaninfo metadata is read from the database - * @param ci The chaninfo - * @param key The metadata key - * @param params The params from the database - * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to stop processing - */ - virtual EventReturn OnDatabaseReadMetadata(ChannelInfo *ci, const Anope::string &key, const std::vector<Anope::string> ¶ms) { return EVENT_CONTINUE; } - - /** Called when we are writing metadata for a nickcore - * @param WriteMetata A callback function used to insert the metadata - * @param nc The nickcore - */ - virtual void OnDatabaseWriteMetadata(void (*WriteMetadata)(const Anope::string &, const Anope::string &), NickCore *nc) { } - - /** Called when we are wrting metadata for a nickalias - * @param WriteMetata A callback function used to insert the metadata - * @param na The nick alias - */ - virtual void OnDatabaseWriteMetadata(void (*WriteMetadata)(const Anope::string &, const Anope::string &), NickAlias *na) { } - - /** Called when we are writing metadata for a botinfo - * @param WriteMetata A callback function used to insert the metadata - * @param bi The botinfo - */ - virtual void OnDatabaseWriteMetadata(void (*WriteMetadata)(const Anope::string &, const Anope::string &), BotInfo *bi) { } - - /** Called when are are writing metadata for a channelinfo - * @param WriteMetata A callback function used to insert the metadata - * @param bi The channelinfo - */ - virtual void OnDatabaseWriteMetadata(void (*WriteMetadata)(const Anope::string &, const Anope::string &), ChannelInfo *ci) { } - /** Called when services restart */ virtual void OnRestart() { } @@ -673,11 +601,6 @@ class CoreExport Module : public Extensible */ virtual void OnChanDrop(const Anope::string &chname) { } - /** Called when a channel is forbidden - * @param ci The channel - */ - virtual void OnChanForbidden(ChannelInfo *ci) { } - /** Called when a channel is registered * @param ci The channel */ @@ -979,10 +902,10 @@ class CoreExport Module : public Extensible /** Called when a mode is about to be unlocked * @param ci The channel the mode is being unlocked from - * @param mode The mode being unlocked + * @param lock The mode lock * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to deny the mlock. */ - virtual EventReturn OnUnMLock(ChannelInfo *ci, ChannelMode *mode, const Anope::string ¶m) { return EVENT_CONTINUE; } + virtual EventReturn OnUnMLock(ChannelInfo *ci, ModeLock *lock) { return EVENT_CONTINUE; } /** Called after a module is loaded * @param u The user loading the module, can be NULL @@ -1042,7 +965,7 @@ enum Implementation I_OnNickUpdate, /* ChanServ */ - I_OnChanForbidden, I_OnChanSuspend, I_OnChanDrop, I_OnPreChanExpire, I_OnChanExpire, I_OnAccessAdd, + I_OnChanSuspend, I_OnChanDrop, I_OnPreChanExpire, I_OnChanExpire, I_OnAccessAdd, I_OnAccessDel, I_OnAccessClear, I_OnLevelChange, I_OnChanRegistered, I_OnChanUnsuspend, I_OnCreateChan, I_OnDelChan, I_OnChannelCreate, I_OnChannelDelete, I_OnAkickAdd, I_OnAkickDel, I_OnCheckKick, I_OnChanInfo, I_OnFindChan, I_OnCheckPriv, I_OnGroupCheckPriv, @@ -1066,8 +989,7 @@ enum Implementation I_OnAddXLine, I_OnDelXLine, I_IsServicesOper, /* Database */ - I_OnPostLoadDatabases, I_OnSaveDatabase, I_OnLoadDatabase, - I_OnDatabaseWrite, I_OnDatabaseRead, I_OnDatabaseReadMetadata, I_OnDatabaseWriteMetadata, + I_OnSaveDatabase, I_OnLoadDatabase, /* Modules */ I_OnModuleLoad, I_OnModuleUnload, @@ -1235,6 +1157,11 @@ class service_reference : public dynamic_reference<T> { } + inline void operator=(const Anope::string &n) + { + this->name = n; + } + operator bool() { if (this->invalid) diff --git a/include/oper.h b/include/oper.h index 8c587794b..fb046bdbe 100644 --- a/include/oper.h +++ b/include/oper.h @@ -11,14 +11,17 @@ class XLineManager; -class CoreExport XLine +class CoreExport XLine : public Serializable<XLine> { + protected: + XLine(); public: Anope::string Mask; Anope::string By; time_t Created; time_t Expires; Anope::string Reason; + Anope::string Manager; XLine(const Anope::string &mask, const Anope::string &reason = ""); @@ -28,6 +31,9 @@ class CoreExport XLine Anope::string GetUser() const; Anope::string GetHost() const; sockaddrs GetIP() const; + + serialized_data serialize(); + static void unserialize(serialized_data &data); }; class CoreExport XLineManager : public Service<XLineManager> diff --git a/include/regchannel.h b/include/regchannel.h index 017b93afe..680be5b44 100644 --- a/include/regchannel.h +++ b/include/regchannel.h @@ -60,6 +60,30 @@ const Anope::string ChannelInfoFlagStrings[] = { "SIGNKICK", "SIGNKICK_LEVEL", "SUSPENDED", "PERSIST", "" }; +/** Flags for badwords + */ +enum BadWordType +{ + /* Always kicks if the word is said */ + BW_ANY, + /* User must way the entire word */ + BW_SINGLE, + /* The word has to start with the badword */ + BW_START, + /* The word has to end with the badword */ + BW_END +}; + +/* Structure used to contain bad words. */ +struct BadWord : Serializable<BadWord> +{ + Anope::string word; + BadWordType type; + + serialized_data serialize(); + static void unserialize(serialized_data &); +}; + /** Flags for auto kick */ enum AutoKickFlag @@ -71,7 +95,7 @@ enum AutoKickFlag const Anope::string AutoKickFlagString[] = { "AK_ISNICK", "" }; /* AutoKick data. */ -class AutoKick : public Flags<AutoKickFlag> +class AutoKick : public Flags<AutoKickFlag>, public Serializable<AutoKick> { public: AutoKick() : Flags<AutoKickFlag>(AutoKickFlagString) { } @@ -83,20 +107,29 @@ class AutoKick : public Flags<AutoKickFlag> Anope::string creator; time_t addtime; time_t last_used; + + serialized_data serialize(); + static void unserialize(serialized_data &); }; -struct ModeLock +struct ModeLock : Serializable<ModeLock> { + ModeLock() { } + public: + ChannelInfo *ci; bool set; ChannelModeName name; Anope::string param; Anope::string setter; time_t created; - ModeLock(bool s, ChannelModeName n, const Anope::string &p, const Anope::string &se = "", time_t c = Anope::CurTime) : set(s), name(n), param(p), setter(se), created(c) { } + ModeLock(ChannelInfo *ch, bool s, ChannelModeName n, const Anope::string &p, const Anope::string &se = "", time_t c = Anope::CurTime) : ci(ch), set(s), name(n), param(p), setter(se), created(c) { } + + serialized_data serialize(); + static void unserialize(serialized_data &); }; -struct LogSetting +struct LogSetting : Serializable<LogSetting> { /* Our service name of the command */ Anope::string service_name; @@ -107,9 +140,12 @@ struct LogSetting Anope::string method, extra; Anope::string creator; time_t created; + + serialized_data serialize(); + static void unserialize(serialized_data &); }; -class CoreExport ChannelInfo : public Extensible, public Flags<ChannelInfoFlag, CI_END> +class CoreExport ChannelInfo : public Extensible, public Flags<ChannelInfoFlag, CI_END>, public Serializable<ChannelInfo> { private: NickCore *founder; /* Channel founder */ @@ -163,6 +199,9 @@ class CoreExport ChannelInfo : public Extensible, public Flags<ChannelInfoFlag, int16 floodlines, floodsecs; /* For FLOOD kicker */ int16 repeattimes; /* For REPEAT kicker */ + serialized_data serialize(); + static void unserialize(serialized_data &); + /** Change the founder of the channek * @params nc The new founder */ diff --git a/include/serialize.h b/include/serialize.h new file mode 100644 index 000000000..e4c6bce1f --- /dev/null +++ b/include/serialize.h @@ -0,0 +1,160 @@ +#ifndef SERIALIZE_H +#define SERIALIZE_H + +namespace Serialize +{ + enum DataType + { + DT_TEXT, + DT_INT + }; + + class stringstream : public std::stringstream + { + private: + DataType type; + bool key; + unsigned max; + + public: + stringstream() : std::stringstream(), type(DT_TEXT), key(false), max(0) { } + stringstream(const stringstream &ss) : std::stringstream(ss.str()), type(DT_TEXT), key(false), max(0) { } + Anope::string astr() const { return this->str(); } + template<typename T> std::istream &operator>>(T &val) + { + std::istringstream is(this->str()); + is >> val; + return *this; + } + std::istream &operator>>(Anope::string &val) { return *this >> val.str(); } + stringstream &setType(DataType t) + { + this->type = t; + return *this; + } + DataType getType() const + { + return this->type; + } + stringstream &setKey() + { + this->key = true; + return *this; + } + bool getKey() const + { + return this->key; + } + stringstream &setMax(unsigned m) + { + this->max = m; + return *this; + } + unsigned getMax() const + { + return this->max; + } + }; +} + +class SerializableBase; + +extern std::vector<SerializableBase *> serialized_types; +extern std::list<SerializableBase *> serialized_items; +extern void RegisterTypes(); + +class SerializableBase +{ + public: + typedef std::map<Anope::string, Serialize::stringstream> serialized_data; + + virtual Anope::string serialize_name() = 0; + virtual serialized_data serialize() = 0; + virtual void alloc(serialized_data &) = 0; +}; + +template<typename Type> class Serializable : public SerializableBase +{ + public: + static class SerializableAllocator : public SerializableBase + { + Anope::string name; + + public: + SerializableAllocator() + { + } + + ~SerializableAllocator() + { + Unregister(); + } + + void Register(const Anope::string &n, int pos = -1) + { + this->name = n; + serialized_types.insert(serialized_types.begin() + (pos < 0 ? serialized_types.size() : pos), this); + } + + void Unregister() + { + std::vector<SerializableBase *>::iterator it = std::find(serialized_types.begin(), serialized_types.end(), this); + if (it != serialized_types.end()) + serialized_types.erase(it); + } + + Anope::string serialize_name() + { + if (this->name.empty()) + throw CoreException(); + return this->name; + } + + serialized_data serialize() + { + throw CoreException(); + } + + void alloc(serialized_data &data) + { + Type::unserialize(data); + } + } Alloc; + + private: + std::list<SerializableBase *>::iterator s_iter; + + protected: + Serializable() + { + serialized_items.push_front(this); + this->s_iter = serialized_items.begin(); + } + + Serializable(const Serializable &) + { + serialized_items.push_front(this); + this->s_iter = serialized_items.begin(); + } + + ~Serializable() + { + if (!serialized_items.empty()) + serialized_items.erase(this->s_iter); + } + + public: + Anope::string serialize_name() + { + return Alloc.serialize_name(); + } + + void alloc(serialized_data &) + { + throw CoreException(); + } +}; + +template<typename T> typename Serializable<T>::SerializableAllocator Serializable<T>::Alloc; + +#endif // SERIALIZE_H diff --git a/include/services.h b/include/services.h index 77b9e9cd1..e863ab8d1 100644 --- a/include/services.h +++ b/include/services.h @@ -227,21 +227,6 @@ class ModuleException : public CoreException virtual ~ModuleException() throw() { } }; -class DatabaseException : public CoreException -{ - public: - /** This constructor can be used to specify an error message before throwing. - * @param mmessage The exception - */ - DatabaseException(const Anope::string &message) : CoreException(message, "A database module") { } - - /** Destructor - * @throws Nothing - */ - virtual ~DatabaseException() throw() { } -}; - - /** Debug cast to be used instead of dynamic_cast, this uses dynamic_cast * for debug builds and static_cast on releass builds to speed up the program * because dynamic_cast relies on RTTI. @@ -313,7 +298,29 @@ template<typename T, size_t Size = 32> class Flags Flag_Values.reset(); } - std::vector<Anope::string> ToString() + Anope::string ToString() + { + std::vector<Anope::string> v = ToVector(); + Anope::string flag_buf; + for (unsigned i = 0; i < v.size(); ++i) + flag_buf += v[i] + " "; + flag_buf.trim(); + return flag_buf; + } + + void FromString(const Anope::string &str) + { + spacesepstream sep(str); + Anope::string buf; + std::vector<Anope::string> v; + + while (sep.GetToken(buf)) + v.push_back(buf); + + FromVector(v); + } + + std::vector<Anope::string> ToVector() { std::vector<Anope::string> ret; for (unsigned i = 0; this->Flag_Strings && !this->Flag_Strings[i].empty(); ++i) @@ -322,7 +329,7 @@ template<typename T, size_t Size = 32> class Flags return ret; } - void FromString(const std::vector<Anope::string> &strings) + void FromVector(const std::vector<Anope::string> &strings) { for (unsigned i = 0; this->Flag_Strings && !this->Flag_Strings[i].empty(); ++i) for (unsigned j = 0; j < strings.size(); ++j) @@ -478,6 +485,7 @@ class Entry; #include "threadengine.h" #include "opertype.h" #include "modes.h" +#include "serialize.h" /*************************************************************************/ @@ -527,10 +535,14 @@ const Anope::string MemoFlagStrings[] = { /* Memo info structures. Since both nicknames and channels can have memos, * we encapsulate memo data in a MemoList to make it easier to handle. */ -class CoreExport Memo : public Flags<MemoFlag> +class CoreExport Memo : public Flags<MemoFlag>, public Serializable<Memo> { public: - Memo(); + Memo(); + serialized_data serialize(); + static void unserialize(serialized_data &); + + Anope::string owner; time_t time; /* When it was sent */ Anope::string sender; Anope::string text; @@ -555,15 +567,7 @@ struct Session unsigned hits; /* Number of subsequent kills for a host */ }; -struct Exception -{ - Anope::string mask; /* Hosts to which this exception applies */ - unsigned limit; /* Session limit for exception */ - Anope::string who; /* Nick of person who added the exception */ - Anope::string reason; /* Reason for exception's addition */ - time_t time; /* When this exception was added */ - time_t expires; /* Time when it expires. 0 == no expiry */ -}; +struct Exception; /*************************************************************************/ @@ -611,28 +615,7 @@ class CoreExport HostInfo /** Retrieve when the vhost was crated * @return the time it was created */ - const time_t GetTime() const; -}; - -/** Flags for badwords - */ -enum BadWordType -{ - /* Always kicks if the word is said */ - BW_ANY, - /* User must way the entire word */ - BW_SINGLE, - /* The word has to start with the badword */ - BW_START, - /* The word has to end with the badword */ - BW_END -}; - -/* Structure used to contain bad words. */ -struct BadWord -{ - Anope::string word; - BadWordType type; + time_t GetTime() const; }; /* BotServ SET flags */ |