From ae38212c1ce829c783edf971081c90137abb49a0 Mon Sep 17 00:00:00 2001 From: Naram Qashat Date: Sun, 25 Jul 2010 21:58:20 -0400 Subject: Epic commit to replace most of the strings in Anope with a single Anope::string class, plus some other little fixes here and there. If you follow 1.9.x development and are testing things, THIS is one of those things that NEEDS testing. --- include/account.h | 62 ++++----- include/anope.h | 363 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/bots.h | 16 +-- include/channels.h | 47 ++++--- include/commands.h | 26 ++-- include/config.h | 296 +++++++++++++++++++++-------------------- include/extensible.h | 33 ++--- include/extern.h | 273 ++++++++++++++++---------------------- include/hashcomp.h | 121 ++--------------- include/mail.h | 20 +-- include/modes.h | 68 +++++----- include/modules.h | 126 +++++++++--------- include/operserv.h | 44 +++---- include/opertype.h | 18 +-- include/regchannel.h | 46 +++---- include/servers.h | 30 ++--- include/services.h | 360 ++++++++++++++++++++++---------------------------- include/sockets.h | 34 ++--- include/users.h | 82 ++++++------ 19 files changed, 1136 insertions(+), 929 deletions(-) create mode 100644 include/anope.h (limited to 'include') diff --git a/include/account.h b/include/account.h index 82ccb3871..11fcc6767 100644 --- a/include/account.h +++ b/include/account.h @@ -1,13 +1,15 @@ #ifndef ACCOUNT_H #define ACCOUNT_H +#include "anope.h" + class NickAlias; class NickCore; class NickRequest; -typedef unordered_map_namespace::unordered_map nickalias_map; -typedef unordered_map_namespace::unordered_map nickcore_map; -typedef unordered_map_namespace::unordered_map nickrequest_map; +typedef unordered_map_namespace::unordered_map nickalias_map; +typedef unordered_map_namespace::unordered_map nickcore_map; +typedef unordered_map_namespace::unordered_map nickrequest_map; extern CoreExport nickalias_map NickAliasList; extern CoreExport nickcore_map NickCoreList; @@ -86,14 +88,14 @@ enum NickCoreFlag class CoreExport NickRequest : public Extensible { public: - NickRequest(const std::string &nickname); + NickRequest(const Anope::string &nickname); ~NickRequest(); - char *nick; - std::string passcode; - std::string password; - char *email; + Anope::string nick; + Anope::string passcode; + Anope::string password; + Anope::string email; time_t requested; time_t lastmail; /* Unsaved */ }; @@ -107,19 +109,19 @@ class CoreExport NickAlias : public Extensible, public Flags { public: - /** Default constructor + /** Default constructor * @param display The display nick */ - NickCore(const std::string &nickdisplay); + NickCore(const Anope::string &nickdisplay); /** Default destructor */ @@ -149,12 +151,12 @@ class CoreExport NickCore : public Extensible, public Flags Users; - char *display; /* How the nick is displayed */ - std::string pass; /* Password of the nicks */ - char *email; /* E-mail associated to the nick */ - char *greet; /* Greet associated to the nick */ - uint16 language; /* Language selected by nickname owner (LANG_*) */ - std::vector access; /* Access list, vector of strings */ + Anope::string display; /* How the nick is displayed */ + Anope::string pass; /* Password of the nicks */ + Anope::string email; /* E-mail associated to the nick */ + Anope::string greet; /* Greet associated to the nick */ + uint16 language; /* Language selected by nickname owner (LANG_*) */ + std::vector access; /* Access list, vector of strings */ MemoInfo memos; uint16 channelcount; /* Number of channels currently registered */ @@ -168,7 +170,7 @@ class CoreExport NickCore : public Extensible, public Flags +#include +#include "hashcomp.h" + +struct Message; + +namespace Anope +{ + /** + * A wrapper string class around all the other string classes, this class will + * allow us to only require one type of string everywhere that can be converted + * at any time to a specific type of string. + */ + class string + { + private: + /** + * The actual string is stored in an std::string as it can be converted to + * ci::string, irc::string, or a C-style string at any time. + */ + std::string _string; + public: + /** + * Extras. + */ + typedef std::string::iterator iterator; + typedef std::string::const_iterator const_iterator; + typedef std::string::reverse_iterator reverse_iterator; + typedef std::string::const_reverse_iterator const_reverse_iterator; + typedef std::string::size_type size_type; + static const size_type npos = static_cast(-1); + + /** + * Constructors that can take in any type of string. + */ + string() : _string("") { } + string(char chr) : _string() { _string = chr; } + string(size_type n, char chr) : _string(n, chr) { } + string(const char *_str) : _string(_str) { } + string(const std::string &_str) : _string(_str) { } + string(const ci::string &_str) : _string(_str.c_str()) { } + string(const irc::string &_str) : _string(_str.c_str()) { } + string(const string &_str, size_type pos = 0, size_type n = npos) : _string(_str._string, pos, n) { } + template string(InputIterator first, InputIterator last) : _string(first, last) { } + + /** + * Assignment operators, so any type of string can be assigned to this class. + */ + inline string &operator=(char chr) { this->_string = chr; return *this; } + inline string &operator=(const char *_str) { this->_string = _str; return *this; } + inline string &operator=(const std::string &_str) { this->_string = _str; return *this; } + inline string &operator=(const ci::string &_str) { this->_string = _str.c_str(); return *this; } + inline string &operator=(const irc::string &_str) { this->_string = _str.c_str(); return *this; } + inline string &operator=(const string &_str) { if (this != &_str) this->_string = _str._string; return *this; } + + /** + * Equality operators, to compare to any type of string. + */ + inline bool operator==(const char *_str) const { return this->_string == _str; } + inline bool operator==(const std::string &_str) const { return this->_string == _str; } + inline bool operator==(const ci::string &_str) const { return ci::string(this->_string.c_str()) == _str; } + inline bool operator==(const irc::string &_str) const { return irc::string(this->_string.c_str()) == _str; } + inline bool operator==(const string &_str) const { return this->_string == _str._string; } + + inline bool equals_cs(const char *_str) const { return this->_string == _str; } + inline bool equals_cs(const std::string &_str) const { return this->_string == _str; } + inline bool equals_cs(const ci::string &_str) const { return this->_string == _str.c_str(); } + inline bool equals_cs(const irc::string &_str) const { return this->_string == _str.c_str(); } + inline bool equals_cs(const string &_str) const { return this->_string == _str._string; } + + inline bool equals_ci(const char *_str) const { return ci::string(this->_string.c_str()) == _str; } + inline bool equals_ci(const std::string &_str) const { return ci::string(this->_string.c_str()) == _str.c_str(); } + inline bool equals_ci(const ci::string &_str) const { return _str == this->_string.c_str(); } + inline bool equals_ci(const irc::string &_str) const { return ci::string(this->_string.c_str()) == _str.c_str(); } + inline bool equals_ci(const string &_str) const { return ci::string(this->_string.c_str()) == _str._string.c_str(); } + + inline bool equals_irc(const char *_str) const { return irc::string(this->_string.c_str()) == _str; } + inline bool equals_irc(const std::string &_str) const { return irc::string(this->_string.c_str()) == _str.c_str(); } + inline bool equals_irc(const ci::string &_str) const { return irc::string(this->_string.c_str()) == _str.c_str(); } + inline bool equals_irc(const irc::string &_str) const { return _str == this->_string.c_str(); } + inline bool equals_irc(const string &_str) const { return irc::string(this->_string.c_str()) == _str._string.c_str(); } + + /** + * Inequality operators, exact opposites of the above. + */ + inline bool operator!=(const char *_str) const { return !operator==(_str); } + inline bool operator!=(const std::string &_str) const { return !operator==(_str); } + inline bool operator!=(const ci::string &_str) const { return !operator==(_str); } + inline bool operator!=(const irc::string &_str) const { return !operator==(_str); } + inline bool operator!=(const string &_str) const { return !operator==(_str); } + + /** + * Compound addition operators, overloaded to do concatenation. + */ + inline string &operator+=(char chr) { this->_string += chr; return *this; } + inline string &operator+=(const char *_str) { this->_string += _str; return *this; } + inline string &operator+=(const std::string &_str) { this->_string += _str; return *this; } + inline string &operator+=(const ci::string &_str) { this->_string += _str.c_str(); return *this; } + inline string &operator+=(const irc::string &_str) { this->_string += _str.c_str(); return *this; } + inline string &operator+=(const string &_str) { if (this != &_str) this->_string += _str._string; return *this; } + + /** + * Addition operators, overloaded to do concatenation. + */ + inline const string operator+(char chr) const { return string(*this) += chr; } + inline const string operator+(const char *_str) const { return string(*this) += _str; } + inline const string operator+(const std::string &_str) const { return string(*this) += _str; } + inline const string operator+(const ci::string &_str) const { return string(*this) += _str; } + inline const string operator+(const irc::string &_str) const { return string(*this) += _str; } + inline const string operator+(const string &_str) const { return string(*this) += _str; } + + friend const string operator+(char chr, const string &str); + friend const string operator+(const char *_str, const string &str); + friend const string operator+(const std::string &_str, const string &str); + friend const string operator+(const ci::string &_str, const string &str); + friend const string operator+(const irc::string &_str, const string &str); + + /** + * Less-than operator. + */ + inline bool operator<(const string &_str) const { return this->_string < _str._string; } + + /** + * The following functions return the various types of strings. + */ + inline const char *c_str() const { return this->_string.c_str(); } + inline std::string &str() { return this->_string; } + inline const std::string &str() const { return this->_string; } + inline ci::string ci_str() const { return ci::string(this->_string.c_str()); } + inline irc::string irc_str() const { return irc::string(this->_string.c_str()); } + + /** + * Returns if the string is empty or not. + */ + inline bool empty() const { return this->_string.empty(); } + + /** + * Returns the string's length. + */ + inline size_type length() const { return this->_string.length(); } + + /** + * Erases characters from the string. + */ + inline iterator erase(const iterator &i) { return this->_string.erase(i); } + inline iterator erase(const iterator &first, const iterator &last) { return this->_string.erase(first, last); } + inline void erase(size_type pos = 0, size_type n = std::string::npos) { this->_string.erase(pos, n); } + + /** + * Clears the string. + */ + inline void clear() { this->_string.clear(); } + + /** + * Find substrings of the string. + */ + inline size_type find(const string &_str, size_type pos = 0) const { return this->_string.find(_str._string, pos); } + inline size_type find(char chr, size_type pos = 0) const { return this->_string.find(chr, pos); } + inline size_type find_ci(const string &_str, size_type pos = 0) const { return ci::string(this->_string.c_str()).find(ci::string(_str._string.c_str()), pos); } + inline size_type find_ci(char chr, size_type pos = 0) const { return ci::string(this->_string.c_str()).find(chr, pos); } + + inline size_type rfind(const string &_str, size_type pos = npos) const { return this->_string.rfind(_str._string, pos); } + inline size_type rfind(char chr, size_type pos = npos) const { return this->_string.rfind(chr, pos); } + inline size_type rfind_ci(const string &_str, size_type pos = npos) const { return ci::string(this->_string.c_str()).rfind(ci::string(_str._string.c_str()), pos); } + inline size_type rfind_ci(char chr, size_type pos = npos) const { return ci::string(this->_string.c_str()).rfind(chr, pos); } + + inline size_type find_first_of(const string &_str, size_type pos = 0) const { return this->_string.find_first_of(_str._string, pos); } + inline size_type find_first_of_ci(const string &_str, size_type pos = 0) const { return ci::string(this->_string.c_str()).find_first_of(ci::string(_str._string.c_str()), pos); } + + inline size_type find_first_not_of(const string &_str, size_type pos = 0) const { return this->_string.find_first_not_of(_str._string, pos); } + inline size_type find_first_not_of_ci(const string &_str, size_type pos = 0) const { return ci::string(this->_string.c_str()).find_first_not_of(ci::string(_str._string.c_str()), pos); } + + inline size_type find_last_of(const string &_str, size_type pos = npos) const { return this->_string.find_last_of(_str._string, pos); } + inline size_type find_last_of_ci(const string &_str, size_type pos = npos) const { return ci::string(this->_string.c_str()).find_last_of(ci::string(_str._string.c_str()), pos); } + + inline size_type find_last_not_of(const string &_str, size_type pos = npos) const { return this->_string.find_last_not_of(_str._string, pos); } + inline size_type find_last_not_of_ci(const string &_str, size_type pos = npos) const { return ci::string(this->_string.c_str()).find_last_not_of(ci::string(_str._string.c_str()), pos); } + + /** + * Determine if string consists of only numbers. + */ + inline bool is_number_only() const { return this->find_first_not_of("0123456789.-") == npos; } + + /** + * Replace parts of the string. + */ + inline string replace(size_type pos, size_type n, const string &_str) { return string(this->_string.replace(pos, n, _str._string)); } + inline string replace(size_type pos, size_type n, const string &_str, size_type pos1, size_type n1) { return string(this->_string.replace(pos, n, _str._string, pos1, n1)); } + inline string replace(size_type pos, size_type n, size_type n1, char chr) { return string(this->_string.replace(pos, n, n1, chr)); } + inline string replace(iterator first, iterator last, const string &_str) { return string(this->_string.replace(first, last, _str._string)); } + inline string replace(iterator first, iterator last, size_type n, char chr) { return string(this->_string.replace(first, last, n, chr)); } + template inline string replace(iterator first, iterator last, InputIterator f, InputIterator l) { return string(this->_string.replace(first, last, f, l)); } + inline string replace_all_cs(const string &_orig, const string &_repl) + { + Anope::string new_string = *this; + size_type pos = new_string.find(_orig), orig_length = _orig.length(); + while (pos != npos) + { + new_string.replace(pos, pos + orig_length, _repl); + pos = new_string.find(_orig, pos + orig_length); + } + return new_string; + } + inline string replace_all_ci(const string &_orig, const string &_repl) + { + Anope::string new_string = *this; + size_type pos = new_string.find_ci(_orig), orig_length = _orig.length(); + while (pos != npos) + { + new_string.replace(pos, pos + orig_length, _repl); + pos = new_string.find_ci(_orig, pos + orig_length); + } + return new_string; + } + + /** + * Get a substring of the string. + */ + inline string substr(size_type pos = 0, size_type n = npos) const { return string(this->_string.substr(pos, n)); } + + /** + * Iterators to the string. + */ + inline iterator begin() { return this->_string.begin(); } + inline const_iterator begin() const { return this->_string.begin(); } + inline iterator end() { return this->_string.end(); } + inline const_iterator end() const { return this->_string.end(); } + inline reverse_iterator rbegin() { return this->_string.rbegin(); } + inline const_reverse_iterator rbegin() const { return this->_string.rbegin(); } + inline reverse_iterator rend() { return this->_string.rend(); } + inline const_reverse_iterator rend() const { return this->_string.rend(); } + + /** + * Subscript operator, to access individual characters of the string. + */ + inline char &operator[](size_type n) { return this->_string[n]; } + inline const char &operator[](size_type n) const { return this->_string[n]; } + + /** + * Stream insertion operator, must be friend because they cannot be inside the class. + */ + friend std::ostream &operator<<(std::ostream &os, const string &_str); + }; + + inline std::ostream &operator<<(std::ostream &os, const string &_str) { return os << _str._string; } + + inline const string operator+(char chr, const string &str) { string tmp(chr); tmp += str; return tmp; } + inline const string operator+(const char *_str, const string &str) { string tmp(_str); tmp += str; return tmp; } + inline const string operator+(const std::string &_str, const string &str) { string tmp(_str); tmp += str; return tmp; } + inline const string operator+(const ci::string &_str, const string &str) { string tmp(_str); tmp += str; return tmp; } + inline const string operator+(const irc::string &_str, const string &str) { string tmp(_str); tmp += str; return tmp; } + + static const char *const compiled = __TIME__ " " __DATE__; + + extern string Version(); + + extern string Build(); + + /** Check whether two strings match. + * @param str The string to check against the pattern (e.g. foobar) + * @param mask The pattern to check (e.g. foo*bar) + * @param case_sensitive Whether or not the match is case sensitive, default false. + */ + extern bool Match(const Anope::string &str, const Anope::string &mask, bool case_sensitive = false); + + /** Add a message to Anope + * @param name The message name as sent by the IRCd + * @param func A callback function that will be called when this message is received + * @return The new message object + */ + extern Message *AddMessage(const string &name, int (*func)(const string &source, int ac, const char **av)); + + /** Deletes a message from Anope + * XXX Im not sure what will happen if this function is called indirectly from message function pointed to by this message.. must check + * @param m The message + * @return true if the message was found and deleted, else false + */ + extern bool DelMessage(Message *m); + + /** Returns a list of pointers to message handlers + * @param The message name as sent by the IRCd + * @return a vector with pointers to the messagehandlers (you can bind more than one handler to a message) + */ + extern std::vector FindMessage(const string &name); +} + +/** sepstream allows for splitting token seperated lists. + * Each successive call to sepstream::GetToken() returns + * the next token, until none remain, at which point the method returns + * an empty string. + */ +class CoreExport sepstream +{ + private: + /** Original string. + */ + Anope::string tokens; + /** Last position of a seperator token + */ + Anope::string::iterator last_starting_position; + /** Current string position + */ + Anope::string::iterator n; + /** Seperator value + */ + char sep; + public: + /** Create a sepstream and fill it with the provided data + */ + sepstream(const Anope::string &source, char seperator); + virtual ~sepstream() { } + + /** Fetch the next token from the stream + * @param token The next token from the stream is placed here + * @return True if tokens still remain, false if there are none left + */ + virtual bool GetToken(Anope::string &token); + + /** Fetch the entire remaining stream, without tokenizing + * @return The remaining part of the stream + */ + virtual const Anope::string GetRemaining(); + + /** Returns true if the end of the stream has been reached + * @return True if the end of the stream has been reached, otherwise false + */ + virtual bool StreamEnd(); +}; + +/** A derived form of sepstream, which seperates on commas + */ +class commasepstream : public sepstream +{ + public: + /** Initialize with comma seperator + */ + commasepstream(const Anope::string &source) : sepstream(source, ',') { } +}; + +/** A derived form of sepstream, which seperates on spaces + */ +class spacesepstream : public sepstream +{ + public: + /** Initialize with space seperator + */ + spacesepstream(const Anope::string &source) : sepstream(source, ' ') { } +}; + +#endif // ANOPE_H diff --git a/include/bots.h b/include/bots.h index cd95467b5..3d35f569e 100644 --- a/include/bots.h +++ b/include/bots.h @@ -12,8 +12,8 @@ class BotInfo; -typedef unordered_map_namespace::unordered_map botinfo_map; -typedef unordered_map_namespace::unordered_map botinfo_uid_map; +typedef unordered_map_namespace::unordered_map botinfo_map; +typedef unordered_map_namespace::unordered_map botinfo_uid_map; extern CoreExport botinfo_map BotListByNick; extern CoreExport botinfo_uid_map BotListByUID; @@ -32,8 +32,8 @@ enum BotFlag class CoreExport BotInfo : public User, public Flags { public: - time_t created; /* Birth date ;) */ - time_t lastmsg; /* Last time we said something */ + time_t created; /* Birth date ;) */ + time_t lastmsg; /* Last time we said something */ CommandMap Commands; /* Commands on this bot */ /** Create a new bot. @@ -42,7 +42,7 @@ class CoreExport BotInfo : public User, public Flags * @param host The hostname to give the bot. * @param real The realname to give the bot. */ - BotInfo(const std::string &nick, const std::string &user = "", const std::string &host = "", const std::string &real = ""); + BotInfo(const Anope::string &nick, const Anope::string &user = "", const Anope::string &host = "", const Anope::string &real = ""); /** Destroy a bot, clearing up appropriately. */ @@ -51,7 +51,7 @@ class CoreExport BotInfo : public User, public Flags /** Change the nickname for the bot. * @param newnick The nick to change to */ - void SetNewNick(const std::string &newnick); + void SetNewNick(const Anope::string &newnick); /** Rejoins all channels that this bot is assigned to. * Used on /kill, rename, etc. @@ -71,8 +71,8 @@ class CoreExport BotInfo : public User, public Flags void UnAssign(User *u, ChannelInfo *ci); void Join(Channel *c); - void Join(const std::string &chname); - void Part(Channel *c, const std::string &reason = ""); + void Join(const Anope::string &chname); + void Part(Channel *c, const Anope::string &reason = ""); }; #endif // BOTS_H diff --git a/include/channels.h b/include/channels.h index 7cdc32de3..8bf98e4b9 100644 --- a/include/channels.h +++ b/include/channels.h @@ -9,20 +9,25 @@ #ifndef CHANNELS_H #define CHANNELS_H -typedef unordered_map_namespace::unordered_map channel_map; +typedef unordered_map_namespace::unordered_map channel_map; extern CoreExport channel_map ChannelList; struct UserData { UserData() { - lastline = NULL; + Clear(); + } + + virtual ~UserData() { } + + void Clear() + { last_use = last_start = time(NULL); lines = times = 0; + lastline.clear(); } - virtual ~UserData() { delete [] lastline; } - /* Data validity */ time_t last_use; @@ -31,7 +36,7 @@ struct UserData time_t last_start; /* for repeat kicker */ - char *lastline; + Anope::string lastline; int16 times; }; @@ -60,7 +65,7 @@ class CoreExport Channel : public Extensible, public Flags Params; + std::map Params; /* Modes set on the channel */ Flags modes; @@ -70,17 +75,17 @@ class CoreExport Channel : public Extensible, public Flags CommandMap; +typedef std::map CommandMap; /** The return value from commands. */ @@ -28,10 +28,10 @@ enum CommandReturn MOD_STOP }; -extern CoreExport Command *FindCommand(BotInfo *bi, const ci::string &cmd); -extern CoreExport void mod_help_cmd(BotInfo *bi, User *u, const ci::string &cmd); -extern CoreExport void mod_run_cmd(BotInfo *bi, User *u, const std::string &message); -extern CoreExport void mod_run_cmd(BotInfo *bi, User *u, Command *c, const ci::string &command, const ci::string &message); +extern CoreExport Command *FindCommand(BotInfo *bi, const Anope::string &cmd); +extern CoreExport void mod_help_cmd(BotInfo *bi, User *u, const Anope::string &cmd); +extern CoreExport void mod_run_cmd(BotInfo *bi, User *u, const Anope::string &message); +extern CoreExport void mod_run_cmd(BotInfo *bi, User *u, Command *c, const Anope::string &command, const Anope::string &message); enum CommandFlag { @@ -53,9 +53,9 @@ class CoreExport Command : public Flags /* Minimum parameters required to use this command */ size_t MinParams; /* Command name */ - ci::string name; + Anope::string name; /* Permission needed to use this comand */ - ci::string permission; + Anope::string permission; /* Module which owns us */ Module *module; @@ -68,14 +68,14 @@ class CoreExport Command : public Flags * @param max_params The maximum number of parameters the parser will create, after max_params, all will be combined into the last argument. * NOTE: If max_params is not set (default), there is no limit to the max number of params. */ - Command(const ci::string &sname, size_t min_params, size_t max_params = 0, const ci::string &spermission = ""); + Command(const Anope::string &sname, size_t min_params, size_t max_params = 0, const Anope::string &spermission = ""); virtual ~Command(); /** Execute this command. * @param u The user executing the command. */ - virtual CommandReturn Execute(User *u, const std::vector &); + virtual CommandReturn Execute(User *u, const std::vector &); /** Called when HELP is requsted for the client this command is on. * @param u The user requesting help @@ -87,18 +87,18 @@ class CoreExport Command : public Flags * @param subcommand The subcommand the user is requesting help on, or an empty string. (e.g. /ns help set foo bar lol gives a subcommand of "FOO BAR LOL") * @return true if help was provided to the user, false otherwise. */ - virtual bool OnHelp(User *u, const ci::string &subcommand); + virtual bool OnHelp(User *u, const Anope::string &subcommand); /** Requested when the user provides bad syntax to this command (not enough params, etc). * @param u The user executing the command. * @param subcommand The subcommand the user tried to use */ - virtual void OnSyntaxError(User *u, const ci::string &subcommand); + virtual void OnSyntaxError(User *u, const Anope::string &subcommand); /** Set which command permission (e.g. chanserv/forbid) is required for this command. * @param reststr The permission required to successfully execute this command */ - void SetPermission(const ci::string &reststr); + void SetPermission(const Anope::string &reststr); /** Add a subcommand to this command * @param c The command @@ -108,7 +108,7 @@ class CoreExport Command : public Flags /** Delete a subcommand from this command * @param cname The subcommand name */ - virtual bool DelSubcommand(const ci::string &cname); + virtual bool DelSubcommand(const Anope::string &cname); }; #endif // COMMANDS_H diff --git a/include/config.h b/include/config.h index 34315e3c8..ef7445041 100644 --- a/include/config.h +++ b/include/config.h @@ -8,9 +8,11 @@ #include #include +#include "anope.h" + /** A configuration key and value pair */ -typedef std::pair KeyVal; +typedef std::pair KeyVal; /** A list of related configuration keys and values */ @@ -18,7 +20,7 @@ typedef std::vector KeyValList; /** An entire config file, built up of KeyValLists */ -typedef std::multimap ConfigDataHash; +typedef std::multimap ConfigDataHash; // Required forward definitions class ServerConfig; @@ -32,8 +34,9 @@ enum ConfigDataType DT_UINTEGER, // Unsigned Integer DT_LUINTEGER, // Long Unsigned Integer DT_CHARPTR, // Char pointer - DT_STRING, // std::string + DT_CSSTRING, // std::string DT_CISTRING, // ci::string + DT_STRING, // Anope::string DT_BOOLEAN, // Boolean DT_HOSTNAME, // Hostname syntax DT_NOSPACES, // No spaces @@ -54,7 +57,7 @@ class ValueItem { private: /** Actual data */ - std::string v; + Anope::string v; public: /** Initialize with an int */ ValueItem(int); @@ -64,8 +67,10 @@ class ValueItem ValueItem(const char *); /** Initialize with an std::string */ ValueItem(const std::string &); - /** Initialize with an std::string */ + /** Initialize with a ci::string */ ValueItem(const ci::string &); + /** Initialize with an Anope::string */ + ValueItem(const Anope::string &); /** Initialize with a long */ ValueItem(long); /** Change value to a char pointer */ @@ -76,16 +81,20 @@ class ValueItem void Set(const std::string &); /** Change value to a ci::string */ void Set(const ci::string &); + /** Change value to an Anope::string */ + void Set(const Anope::string &); /** Change value to an int */ void Set(int); /** Get value as an int */ int GetInteger(); /** Get value as a string */ const char *GetString() const; - /** Get value as a string */ - inline const std::string &GetValue() const { return v; } + /** Get value as an std::string */ + inline const std::string GetCSValue() const { return v.str(); } /** Get value as a ci::string */ - inline const ci::string GetCIValue() const { return v.c_str(); } + inline const ci::string GetCIValue() const { return v.ci_str(); } + /** Get value as a ci::string */ + inline const Anope::string &GetValue() const { return v; } /** Get value as a bool */ bool GetBool(); }; @@ -240,6 +249,47 @@ template<> class ValueContainer : public ValueContainerBase } }; +/** This a specific version of ValueContainer to handle Anope::string specially + */ +template<> class ValueContainer : public ValueContainerBase +{ + private: + /** Contained item */ + Anope::string *val; + public: + /** Initialize with nothing */ + ValueContainer() : ValueContainerBase(), val(NULL) { } + /** Initialize with an std::string */ + ValueContainer(Anope::string *Val) : ValueContainerBase(), val(Val) { } + /** Initialize with a copy */ + ValueContainer(const ValueContainer &Val) : ValueContainerBase(), val(Val.val) { } + ValueContainer &operator=(const ValueContainer &Val) + { + val = Val.val; + return *this; + } + /** Change value to given std::string */ + void Set(const std::string &newval) + { + *val = newval; + } + /** Change value to given ci::string */ + void Set(const ci::string &newval) + { + *val = newval; + } + /** Change value to given Anope::string */ + void Set(const Anope::string &newval) + { + *val = newval; + } + /** Change value to given char pointer */ + void Set(const char *newval) + { + *val = newval; + } +}; + /** A specialization of ValueContainer to hold a pointer to a bool */ typedef ValueContainer ValueContainerBool; @@ -272,37 +322,42 @@ typedef ValueContainer ValueContainerTime; /** A specialization of ValueContainer to hold a pointer to * an std::string */ -typedef ValueContainer ValueContainerString; +typedef ValueContainer ValueContainerCSString; /** A specialization of ValueContainer to hold a pointer to * an ci::string */ typedef ValueContainer ValueContainerCIString; +/** A specialization of ValueContainer to hold a pointer to + * an Anope::string + */ +typedef ValueContainer ValueContainerString; + /** A set of ValueItems used by multi-value validator functions */ typedef std::deque ValueList; /** A callback for validating a single value */ -typedef bool (*Validator)(ServerConfig *, const char *, const char *, ValueItem &); +typedef bool (*Validator)(ServerConfig *, const Anope::string &, const Anope::string &, ValueItem &); /** A callback for validating multiple value entries */ -typedef bool (*MultiValidator)(ServerConfig *, const char *, const char **, ValueList &, int *, bool); +typedef bool (*MultiValidator)(ServerConfig *, const Anope::string &, const Anope::string *, ValueList &, int *, bool); /** A callback indicating the end of a group of entries */ -typedef bool (*MultiNotify)(ServerConfig *, const char *, bool); +typedef bool (*MultiNotify)(ServerConfig *, const Anope::string &, bool); /** Holds a core configuration item and its callbacks */ struct InitialConfig { /** Tag name */ - const char *tag; + const Anope::string tag; /** Value name */ - const char *value; + const Anope::string value; /** Default, if not defined */ - const char *default_value; + const Anope::string default_value; /** Value containers */ ValueContainerBase *val; /** Data types */ @@ -317,11 +372,11 @@ struct InitialConfig struct MultiConfig { /** Tag name */ - const char *tag; + const Anope::string tag; /** One or more items within tag */ - const char *items[17]; + const Anope::string items[17]; /** One or more defaults for items within tags */ - const char *items_default[17]; + const Anope::string items_default[17]; /** One or more data types */ int datatype[17]; /** Initialization function */ @@ -348,7 +403,7 @@ class ServerConfig std::vector include_stack; /** Check that there is only one of each configuration item */ - bool CheckOnce(const char *); + bool CheckOnce(const Anope::string &); public: std::ostringstream errstr; ConfigDataHash newconfig; @@ -373,98 +428,63 @@ class ServerConfig * this connection as SNOTICEs. * If the parameter is NULL, the messages are spooled to all connections via WriteOpers as SNOTICEs. */ - void ReportConfigError(const std::string &, bool); + void ReportConfigError(const Anope::string &, bool); /** Load 'filename' into 'target', with the new config parser everything is parsed into * tag/key/value at load-time rather than at read-value time. */ - bool LoadConf(ConfigDataHash &, const char *, std::ostringstream &); - /** Load 'filename' into 'target', with the new config parser everything is parsed into - * tag/key/value at load-time rather than at read-value time. - */ - bool LoadConf(ConfigDataHash &, const std::string &, std::ostringstream &); - /** Load 'filename' into 'target', with the new config parser everything is parsed into - * tag/key/value at load-time rather than at read-value time. - */ - bool LoadConf(ConfigDataHash &, const ci::string &, std::ostringstream &); + bool LoadConf(ConfigDataHash &, const Anope::string &, std::ostringstream &); // Both these return true if the value existed or false otherwise /** Writes 'length' chars into 'result' as a string */ - bool ConfValue(ConfigDataHash &, const char *, const char *, int, char *, int, bool = false); - /** Writes 'length' chars into 'result' as a string - */ - bool ConfValue(ConfigDataHash &, const char *, const char *, const char *, int, char *, int, bool = false); + bool ConfValue(ConfigDataHash &, const Anope::string &, const Anope::string &, int, Anope::string &, bool = false); /** Writes 'length' chars into 'result' as a string */ - bool ConfValue(ConfigDataHash &, const ci::string &, const ci::string &, int, ci::string &, bool = false); - /** Writes 'length' chars into 'result' as a string - */ - bool ConfValue(ConfigDataHash &, const ci::string &, const ci::string &, const ci::string &, int, ci::string &, bool = false); - /** Tries to convert the value to an integer and write it to 'result' - */ - bool ConfValueInteger(ConfigDataHash &, const char *, const char *, int, int &); + bool ConfValue(ConfigDataHash &, const Anope::string &, const Anope::string &, const Anope::string &, int, Anope::string &, bool = false); /** Tries to convert the value to an integer and write it to 'result' */ - bool ConfValueInteger(ConfigDataHash &, const char *, const char *, const char *, int, int &); + bool ConfValueInteger(ConfigDataHash &, const Anope::string &, const Anope::string &, int, int &); /** Tries to convert the value to an integer and write it to 'result' */ - bool ConfValueInteger(ConfigDataHash &, const ci::string &, const ci::string &, int, int &); - /** Tries to convert the value to an integer and write it to 'result' - */ - bool ConfValueInteger(ConfigDataHash &, const ci::string &, const ci::string &, const ci::string &, int, int &); - /** Returns true if the value exists and has a true value, false otherwise - */ - bool ConfValueBool(ConfigDataHash &, const char *, const char *, int); + bool ConfValueInteger(ConfigDataHash &, const Anope::string &, const Anope::string &, const Anope::string &, int, int &); /** Returns true if the value exists and has a true value, false otherwise */ - bool ConfValueBool(ConfigDataHash &, const char *, const char *, const char *, int); + bool ConfValueBool(ConfigDataHash &, const Anope::string &, const Anope::string &, int); /** Returns true if the value exists and has a true value, false otherwise */ - bool ConfValueBool(ConfigDataHash &, const ci::string &, const ci::string &, int); - /** Returns true if the value exists and has a true value, false otherwise - */ - bool ConfValueBool(ConfigDataHash &, const ci::string &, const ci::string &, const ci::string &, int); - /** Returns the number of occurences of tag in the config file - */ - int ConfValueEnum(ConfigDataHash &, const char *); + bool ConfValueBool(ConfigDataHash &, const Anope::string &, const Anope::string &, const Anope::string &, int); /** Returns the number of occurences of tag in the config file */ - int ConfValueEnum(ConfigDataHash &, const std::string &); - /** Returns the number of occurences of tag in the config file - */ - int ConfValueEnum(ConfigDataHash &, const ci::string &); - /** Returns the numbers of vars inside the index'th 'tag in the config file - */ - int ConfVarEnum(ConfigDataHash &, const char *, int); + int ConfValueEnum(ConfigDataHash &, const Anope::string &); /** Returns the numbers of vars inside the index'th 'tag in the config file */ - int ConfVarEnum(ConfigDataHash &, const ci::string &, int); - void ValidateHostname(const char *, const ci::string &, const ci::string &); - void ValidateIP(const char *p, const ci::string &, const ci::string &, bool); - void ValidateNoSpaces(const char *, const ci::string &, const ci::string &); + int ConfVarEnum(ConfigDataHash &, const Anope::string &, int); + void ValidateHostname(const Anope::string &, const Anope::string &, const Anope::string &); + void ValidateIP(const Anope::string &p, const Anope::string &, const Anope::string &, bool); + void ValidateNoSpaces(const Anope::string &, const Anope::string &, const Anope::string &); /** Below here is a list of variables which contain the config files values */ /* IRCd module in use */ - char *IRCDModule; + Anope::string IRCDModule; /* Host to connect to **/ - char *LocalHost; + Anope::string LocalHost; /* List of uplink servers to try and connect to */ std::list Uplinks; /* Our server name */ - char *ServerName; + Anope::string ServerName; /* Our servers description */ - char *ServerDesc; + Anope::string ServerDesc; /* The username/ident of services clients */ - char *ServiceUser; + Anope::string ServiceUser; /* The hostname if services clients */ - char *ServiceHost; + Anope::string ServiceHost; /* Log channel */ - char *LogChannel; + Anope::string LogChannel; /* Name of the network were on */ - char *NetworkName; + Anope::string NetworkName; /* The max legnth of nicks */ unsigned NickLen; /* Max length of idents */ @@ -476,39 +496,39 @@ class ServerConfig unsigned PassLen; /* NickServ Name */ - char *s_NickServ; + Anope::string s_NickServ; /* ChanServ Name */ - char *s_ChanServ; + Anope::string s_ChanServ; /* MemoServ Name */ - char *s_MemoServ; + Anope::string s_MemoServ; /* BotServ Name */ - char *s_BotServ; + Anope::string s_BotServ; /* OperServ name */ - char *s_OperServ; + Anope::string s_OperServ; /* Global name */ - char *s_GlobalNoticer; + Anope::string s_GlobalNoticer; /* NickServs realname */ - char *desc_NickServ; + Anope::string desc_NickServ; /* ChanServ realname */ - char *desc_ChanServ; - /* MemoServ relname */ - char *desc_MemoServ; + Anope::string desc_ChanServ; + /* MemoServ realname */ + Anope::string desc_MemoServ; /* BotServ realname */ - char *desc_BotServ; + Anope::string desc_BotServ; /* OperServ realname */ - char *desc_OperServ; + Anope::string desc_OperServ; /* Global realname */ - char *desc_GlobalNoticer; + Anope::string desc_GlobalNoticer; /* HostServ Name */ - char *s_HostServ; + Anope::string s_HostServ; /* HostServ realname */ - char *desc_HostServ; + Anope::string desc_HostServ; /* Filename for the PID file */ - char *PIDFilename; + Anope::string PIDFilename; /* MOTD filename */ - char *MOTDFilename; + Anope::string MOTDFilename; /* True if its ok to not be able to save backs */ bool NoBackupOkay; @@ -548,9 +568,9 @@ class ServerConfig /* Max number if news items allowed in the list */ unsigned NewsCount; /* Default mlock modes */ - ci::string MLock; + Anope::string MLock; /* Default botmodes on channels, defaults to ao */ - ci::string BotModes; + Anope::string BotModes; /* How many times to try and reconnect to the uplink before giving up */ unsigned MaxRetries; /* How long to wait between connection attempts */ @@ -561,9 +581,9 @@ class ServerConfig /* Services can use email */ bool UseMail; /* Path to the sendmail executable */ - char *SendMailPath; + Anope::string SendMailPath; /* Address to send from */ - char *SendFrom; + Anope::string SendFrom; /* Only opers can have services send mail */ bool RestrictMail; /* Delay between sending mail */ @@ -572,7 +592,7 @@ class ServerConfig bool DontQuoteAddresses; /* Prefix of guest nicks when a user gets forced off of a nick */ - char *NSGuestNickPrefix; + Anope::string NSGuestNickPrefix; /* Allow users to set kill immed on */ bool NSAllowKillImmed; /* Don't allow nicks to use /ns group to regroup nicks */ @@ -597,9 +617,9 @@ class ServerConfig /* Max number of allowed strings on the access list */ unsigned NSAccessMax; /* Enforcer client user name */ - char *NSEnforcerUser; + Anope::string NSEnforcerUser; /* Enforcer client hostname */ - char *NSEnforcerHost; + Anope::string NSEnforcerHost; /* How long before recovered nicks are released */ time_t NSReleaseTimeout; /* /nickserv list is oper only */ @@ -630,7 +650,7 @@ class ServerConfig /* Max number of entries allowed on autokick lists */ unsigned CSAutokickMax; /* Default autokick reason */ - char *CSAutokickReason; + Anope::string CSAutokickReason; /* Time ChanServ should stay in the channel to hold it to keep users from getting in */ time_t CSInhabit; /* ChanServ's LIST command is oper only */ @@ -664,7 +684,7 @@ class ServerConfig /* Case sensitive badwords matching */ bool BSCaseSensitive; /* Char to use for the fantasy char, eg ! */ - char *BSFantasyCharacter; + Anope::string BSFantasyCharacter; /* Only show /stats o to opers */ bool HideStatsO; @@ -675,9 +695,9 @@ class ServerConfig /* Dont allow users to register nicks with oper names in them */ bool RestrictOperNicks; /* Message to send when shutting down */ - char *GlobalOnCycleMessage; + Anope::string GlobalOnCycleMessage; /* Message to send when starting up */ - char *GlobalOnCycleUP; + Anope::string GlobalOnCycleUP; /* Super admin is allowed */ bool SuperAdmin; /* Log things said through ACT/SAY */ @@ -758,30 +778,30 @@ class ServerConfig /* How long session akills should last */ time_t SessionAutoKillExpiry; /* Reason to use for session kills */ - char *SessionLimitExceeded; + Anope::string SessionLimitExceeded; /* Optional second reason */ - char *SessionLimitDetailsLoc; + Anope::string SessionLimitDetailsLoc; /* OperServ requires you to be an operator */ bool OSOpersOnly; /* List of modules to autoload */ - std::list ModulesAutoLoad; + std::list ModulesAutoLoad; /* Encryption modules */ - std::list EncModuleList; + std::list EncModuleList; /* Database modules */ - std::list DBModuleList; + std::list DBModuleList; /* HostServ Core Modules */ - std::list HostServCoreModules; + std::list HostServCoreModules; /* MemoServ Core Modules */ - std::list MemoServCoreModules; + std::list MemoServCoreModules; /* BotServ Core Modules */ - std::list BotServCoreModules; + std::list BotServCoreModules; /* OperServ Core Modules */ - std::list OperServCoreModules; + std::list OperServCoreModules; /* NickServ Core Modules */ - std::list NickServCoreModules; + std::list NickServCoreModules; /* ChanServ Core Modules */ - std::list ChanServCoreModules; + std::list ChanServCoreModules; /* Default defcon level */ int DefConLevel; @@ -792,37 +812,35 @@ class ServerConfig /* How long to add akills for defcon */ time_t DefConAKILL; /* Chan modes for defcon */ - char *DefConChanModes; + Anope::string DefConChanModes; /* Should we global on defcon */ bool GlobalOnDefcon; /* Should we send DefconMessage aswell? */ bool GlobalOnDefconMore; /* Message to send when defcon is off */ - char *DefConOffMessage; + Anope::string DefConOffMessage; /* Message to send when defcon is on*/ - char *DefconMessage; + Anope::string DefconMessage; /* Reason to akill clients for defcon */ - char *DefConAkillReason; + Anope::string DefConAkillReason; /* The socket engine in use */ - ci::string SocketEngine; + Anope::string SocketEngine; /* User keys to use for generating random hashes for pass codes etc */ - long unsigned int UserKey1; - long unsigned int UserKey2; - long unsigned int UserKey3; + unsigned long UserKey1; + unsigned long UserKey2; + unsigned long UserKey3; /* Numeric */ - char *Numeric; + Anope::string Numeric; /* Array of ulined servers */ - char **Ulines; - /* Number of ulines */ - int NumUlines; + std::list Ulines; /* List of available opertypes */ std::list MyOperTypes; /* List of pairs of opers and their opertype from the config */ - std::list > Opers; + std::list > Opers; }; /** This class can be used on its own to represent an exception, or derived to represent a module-specific exception. @@ -836,12 +854,10 @@ class ConfigException : public CoreException public: /** Default constructor, just uses the error mesage 'Config threw an exception'. */ - ConfigException() : CoreException("Config threw an exception", "Config Parser") {} + ConfigException() : CoreException("Config threw an exception", "Config Parser") { } /** This constructor can be used to specify an error message before throwing. */ - ConfigException(const char *message) : CoreException(message, "Config Parser") {} - ConfigException(const std::string &message) : CoreException(message, "Config Parser") {} - ConfigException(const ci::string &message) : CoreException(message, "Config Parser") {} + ConfigException(const Anope::string &message) : CoreException(message, "Config Parser") { } /** This destructor solves world hunger, cancels the world debt, and causes the world to end. * Actually no, it does nothing. Never mind. * @throws Nothing! @@ -890,7 +906,7 @@ class CoreExport ConfigReader /** Overloaded constructor. * This constructor initialises the ConfigReader class to read a user-specified config file */ - ConfigReader(const std::string &); + ConfigReader(const Anope::string &); /** Default destructor. * This method destroys the ConfigReader class. */ @@ -899,26 +915,26 @@ class CoreExport ConfigReader * This method retrieves a value from the config file. Where multiple copies of the tag * exist in the config file, index indicates which of the values to retrieve. */ - std::string ReadValue(const std::string &, const std::string &, int, bool = false); + Anope::string ReadValue(const Anope::string &, const Anope::string &, int, bool = false); /** Retrieves a value from the config file. * This method retrieves a value from the config file. Where multiple copies of the tag * exist in the config file, index indicates which of the values to retrieve. If the * tag is not found the default value is returned instead. */ - std::string ReadValue(const std::string &, const std::string &, const std::string &, int, bool = false); + Anope::string ReadValue(const Anope::string &, const Anope::string &, const Anope::string &, int, bool = false); /** Retrieves a boolean value from the config file. * This method retrieves a boolean value from the config file. Where multiple copies of the tag * exist in the config file, index indicates which of the values to retrieve. The values "1", "yes" * and "true" in the config file count as true to ReadFlag, and any other value counts as false. */ - bool ReadFlag(const std::string &, const std::string &, int); + bool ReadFlag(const Anope::string &, const Anope::string &, int); /** Retrieves a boolean value from the config file. * This method retrieves a boolean value from the config file. Where multiple copies of the tag * exist in the config file, index indicates which of the values to retrieve. The values "1", "yes" * and "true" in the config file count as true to ReadFlag, and any other value counts as false. * If the tag is not found, the default value is used instead. */ - bool ReadFlag(const std::string &, const std::string &, const std::string &, int); + bool ReadFlag(const Anope::string &, const Anope::string &, const Anope::string &, int); /** Retrieves an integer value from the config file. * This method retrieves an integer value from the config file. Where multiple copies of the tag * exist in the config file, index indicates which of the values to retrieve. Any invalid integer @@ -928,7 +944,7 @@ class CoreExport ConfigReader * will return CONF_INT_NEGATIVE. Note that need_positive is not suitable to get an unsigned int - you * should cast the result to achieve that effect. */ - int ReadInteger(const std::string &, const std::string &, int, bool); + int ReadInteger(const Anope::string &, const Anope::string &, int, bool); /** Retrieves an integer value from the config file. * This method retrieves an integer value from the config file. Where multiple copies of the tag * exist in the config file, index indicates which of the values to retrieve. Any invalid integer @@ -937,7 +953,7 @@ class CoreExport ConfigReader * If a signed number is placed into a tag which is specified unsigned, 0 will be returned and GetError() * will return CONF_NOT_UNSIGNED. If the tag is not found, the default value is used instead. */ - int ReadInteger(const std::string &, const std::string &, const std::string &, int, bool); + int ReadInteger(const Anope::string &, const Anope::string &, const Anope::string &, int, bool); /** Returns the last error to occur. * Valid errors can be found by looking in modules.h. Any nonzero value indicates an error condition. * A call to GetError() resets the error flag back to 0. @@ -949,7 +965,7 @@ class CoreExport ConfigReader * used with the index value of ConfigReader::ReadValue to loop through all copies of a * multiple instance tag. */ - int Enumerate(const std::string &); + int Enumerate(const Anope::string &); /** Returns true if a config file is valid. * This method is partially implemented and will only return false if the config * file does not exist or could not be opened. @@ -967,7 +983,7 @@ class CoreExport ConfigReader * function would return 2. Spaces and newlines both qualify as valid seperators * between values. */ - int EnumerateValues(const std::string &, int); + int EnumerateValues(const Anope::string &, int); }; #endif // CONFIG_H diff --git a/include/extensible.h b/include/extensible.h index d835dd865..523a25b6d 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -7,6 +7,8 @@ #ifndef EXTENSIBLE_H #define EXTENSIBLE_H +#include "hashcomp.h" + /** Dummy base class we use to cast everything to/from */ class ExtensibleItemBase @@ -58,7 +60,8 @@ template class ExtensibleItemPointerArray : public ExtensibleItemBas class CoreExport Extensible { private: - std::map Extension_Items; + typedef std::map extensible_map; + extensible_map Extension_Items; public: /** Default constructor, does nothing @@ -70,7 +73,7 @@ class CoreExport Extensible */ virtual ~Extensible() { - for (std::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) delete it->second; Extension_Items.clear(); } @@ -86,7 +89,7 @@ class CoreExport Extensible * * @return Returns true on success, false if otherwise */ - bool Extend(const std::string &key, ExtensibleItemBase *p) + bool Extend(const Anope::string &key, ExtensibleItemBase *p) { bool Ret = this->Extension_Items.insert(std::make_pair(key, p)).second; if (!Ret) @@ -105,7 +108,7 @@ class CoreExport Extensible * * @return R