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 +-- modules/core/bs_act.cpp | 17 +- modules/core/bs_assign.cpp | 19 +- modules/core/bs_badwords.cpp | 69 +- modules/core/bs_bot.cpp | 142 ++-- modules/core/bs_botlist.cpp | 11 +- modules/core/bs_help.cpp | 9 +- modules/core/bs_info.cpp | 38 +- modules/core/bs_kick.cpp | 195 +++--- modules/core/bs_say.cpp | 19 +- modules/core/bs_set.cpp | 77 +-- modules/core/bs_unassign.cpp | 13 +- modules/core/cs_access.cpp | 189 +++--- modules/core/cs_akick.cpp | 202 +++--- modules/core/cs_ban.cpp | 37 +- modules/core/cs_clear.cpp | 69 +- modules/core/cs_drop.cpp | 23 +- modules/core/cs_forbid.cpp | 28 +- modules/core/cs_getkey.cpp | 17 +- modules/core/cs_help.cpp | 17 +- modules/core/cs_info.cpp | 69 +- modules/core/cs_invite.cpp | 14 +- modules/core/cs_kick.cpp | 33 +- modules/core/cs_list.cpp | 83 +-- modules/core/cs_modes.cpp | 99 +-- modules/core/cs_register.cpp | 33 +- modules/core/cs_saset.cpp | 29 +- modules/core/cs_saset_noexpire.cpp | 14 +- modules/core/cs_set.cpp | 29 +- modules/core/cs_set_bantype.cpp | 22 +- modules/core/cs_set_description.cpp | 24 +- modules/core/cs_set_entrymsg.cpp | 24 +- modules/core/cs_set_founder.cpp | 23 +- modules/core/cs_set_keeptopic.cpp | 20 +- modules/core/cs_set_mlock.cpp | 37 +- modules/core/cs_set_opnotice.cpp | 20 +- modules/core/cs_set_peace.cpp | 20 +- modules/core/cs_set_persist.cpp | 24 +- modules/core/cs_set_private.cpp | 20 +- modules/core/cs_set_restricted.cpp | 20 +- modules/core/cs_set_secure.cpp | 20 +- modules/core/cs_set_securefounder.cpp | 20 +- modules/core/cs_set_secureops.cpp | 20 +- modules/core/cs_set_signkick.cpp | 30 +- modules/core/cs_set_successor.cpp | 25 +- modules/core/cs_set_topiclock.cpp | 20 +- modules/core/cs_set_xop.cpp | 28 +- modules/core/cs_status.cpp | 22 +- modules/core/cs_suspend.cpp | 56 +- modules/core/cs_topic.cpp | 26 +- modules/core/cs_unban.cpp | 12 +- modules/core/cs_xop.cpp | 106 +-- modules/core/db_plain.cpp | 431 ++++++------ modules/core/enc_md5.cpp | 26 +- modules/core/enc_none.cpp | 32 +- modules/core/enc_old.cpp | 27 +- modules/core/enc_sha1.cpp | 26 +- modules/core/enc_sha256.cpp | 48 +- modules/core/hs_del.cpp | 18 +- modules/core/hs_delall.cpp | 18 +- modules/core/hs_group.cpp | 12 +- modules/core/hs_help.cpp | 10 +- modules/core/hs_list.cpp | 26 +- modules/core/hs_off.cpp | 6 +- modules/core/hs_on.cpp | 14 +- modules/core/hs_set.cpp | 82 +-- modules/core/hs_setall.cpp | 78 +-- modules/core/ms_cancel.cpp | 21 +- modules/core/ms_check.cpp | 24 +- modules/core/ms_del.cpp | 21 +- modules/core/ms_help.cpp | 10 +- modules/core/ms_info.cpp | 56 +- modules/core/ms_list.cpp | 32 +- modules/core/ms_read.cpp | 30 +- modules/core/ms_rsend.cpp | 17 +- modules/core/ms_send.cpp | 15 +- modules/core/ms_sendall.cpp | 15 +- modules/core/ms_set.cpp | 73 +- modules/core/ms_staff.cpp | 13 +- modules/core/ns_access.cpp | 61 +- modules/core/ns_alist.cpp | 43 +- modules/core/ns_drop.cpp | 41 +- modules/core/ns_forbid.cpp | 31 +- modules/core/ns_getemail.cpp | 14 +- modules/core/ns_getpass.cpp | 26 +- modules/core/ns_ghost.cpp | 36 +- modules/core/ns_group.cpp | 158 ++--- modules/core/ns_help.cpp | 12 +- modules/core/ns_identify.cpp | 86 +-- modules/core/ns_info.cpp | 58 +- modules/core/ns_list.cpp | 97 ++- modules/core/ns_logout.cpp | 31 +- modules/core/ns_recover.cpp | 40 +- modules/core/ns_register.cpp | 98 ++- modules/core/ns_release.cpp | 30 +- modules/core/ns_resetpass.cpp | 40 +- modules/core/ns_saset.cpp | 79 +-- modules/core/ns_saset_noexpire.cpp | 20 +- modules/core/ns_sendpass.cpp | 30 +- modules/core/ns_set.cpp | 63 +- modules/core/ns_set_autoop.cpp | 50 +- modules/core/ns_set_email.cpp | 48 +- modules/core/ns_set_greet.cpp | 39 +- modules/core/ns_set_hide.cpp | 70 +- modules/core/ns_set_kill.cpp | 44 +- modules/core/ns_set_language.cpp | 34 +- modules/core/ns_set_message.cpp | 32 +- modules/core/ns_set_private.cpp | 32 +- modules/core/ns_set_secure.cpp | 32 +- modules/core/ns_status.cpp | 20 +- modules/core/ns_suspend.cpp | 98 ++- modules/core/ns_update.cpp | 12 +- modules/core/os_akill.cpp | 98 +-- modules/core/os_chankill.cpp | 43 +- modules/core/os_chanlist.cpp | 20 +- modules/core/os_clearmodes.cpp | 40 +- modules/core/os_defcon.cpp | 149 ++-- modules/core/os_global.cpp | 14 +- modules/core/os_help.cpp | 8 +- modules/core/os_ignore.cpp | 59 +- modules/core/os_jupe.cpp | 17 +- modules/core/os_kick.cpp | 18 +- modules/core/os_mode.cpp | 16 +- modules/core/os_modinfo.cpp | 32 +- modules/core/os_modlist.cpp | 22 +- modules/core/os_modload.cpp | 10 +- modules/core/os_modunload.cpp | 10 +- modules/core/os_news.cpp | 89 ++- modules/core/os_noop.cpp | 26 +- modules/core/os_oline.cpp | 22 +- modules/core/os_quit.cpp | 14 +- modules/core/os_reload.cpp | 12 +- modules/core/os_restart.cpp | 14 +- modules/core/os_session.cpp | 215 +++--- modules/core/os_set.cpp | 92 +-- modules/core/os_shutdown.cpp | 14 +- modules/core/os_snline.cpp | 104 +-- modules/core/os_sqline.cpp | 99 ++- modules/core/os_staff.cpp | 16 +- modules/core/os_stats.cpp | 28 +- modules/core/os_svsnick.cpp | 24 +- modules/core/os_szline.cpp | 100 +-- modules/core/os_umode.cpp | 20 +- modules/core/os_update.cpp | 6 +- modules/core/os_userlist.cpp | 23 +- modules/core/ss_main.cpp | 8 +- modules/extra/cs_appendtopic.cpp | 48 +- modules/extra/cs_enforce.cpp | 49 +- modules/extra/cs_set_misc.cpp | 74 +- modules/extra/cs_tban.cpp | 68 +- modules/extra/hs_request.cpp | 204 +++--- modules/extra/m_helpchan.cpp | 10 +- modules/extra/m_ssl.cpp | 16 +- modules/extra/mysql/db_mysql.h | 6 +- modules/extra/mysql/db_mysql_execute.cpp | 2 +- modules/extra/mysql/db_mysql_read.cpp | 20 +- modules/extra/mysql/db_mysql_write.cpp | 18 +- modules/extra/ns_maxemail.cpp | 32 +- modules/extra/ns_set_misc.cpp | 83 +-- modules/protocol/bahamut.cpp | 202 +++--- modules/protocol/inspircd11.cpp | 297 ++++---- modules/protocol/inspircd12.cpp | 349 +++++----- modules/protocol/inspircd20.cpp | 509 +++++++------- modules/protocol/ratbox.cpp | 300 ++++----- modules/protocol/unreal32.cpp | 372 +++++----- modules/socketengines/m_socketengine_epoll.cpp | 34 +- modules/socketengines/m_socketengine_select.cpp | 26 +- src/actions.cpp | 29 +- src/base64.cpp | 334 ++------- src/bots.cpp | 53 +- src/botserv.cpp | 205 +++--- src/channels.cpp | 414 +++++------- src/chanserv.cpp | 212 +++--- src/command.cpp | 14 +- src/commands.cpp | 39 +- src/compat.cpp | 58 -- src/config.cpp | 861 +++++++++++------------- src/configreader.cpp | 30 +- src/encrypt.cpp | 19 +- src/hashcomp.cpp | 93 +-- src/hostserv.cpp | 36 +- src/init.cpp | 73 +- src/ircd.cpp | 19 +- src/language.cpp | 8 +- src/log.cpp | 50 +- src/mail.cpp | 50 +- src/main.cpp | 74 +- src/memory.cpp | 46 -- src/memoserv.cpp | 71 +- src/messages.cpp | 99 ++- src/misc.cpp | 497 +++++--------- src/modes.cpp | 96 +-- src/module.cpp | 20 +- src/modulemanager.cpp | 65 +- src/modules.cpp | 50 +- src/nickalias.cpp | 35 +- src/nickcore.cpp | 30 +- src/nickserv.cpp | 164 ++--- src/operserv.cpp | 163 ++--- src/opertype.cpp | 16 +- src/process.cpp | 79 +-- src/protocol.cpp | 111 +-- src/regchannel.cpp | 84 +-- src/send.cpp | 75 +-- src/servers.cpp | 62 +- src/sessions.cpp | 139 ++-- src/sockets.cpp | 113 ++-- src/threadengines/threadengine_pthread.cpp | 2 +- src/threadengines/threadengine_win32.cpp | 2 +- src/timers.cpp | 4 +- src/tools/db-convert.h | 2 - src/users.cpp | 324 ++++----- src/wildcard.cpp | 12 +- src/win32/windows.cpp | 2 +- 232 files changed, 7416 insertions(+), 8903 deletions(-) create mode 100644 include/anope.h delete mode 100644 src/compat.cpp 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 PIDFilenam