From a950ed8cabfeca55c909f02412b1788bd386dcff Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 15 Aug 2010 01:45:38 -0400 Subject: Rewrote the MySQL API to use threads. This acts similar to before, but is faster. Removed db_mysql_execute for now. --- CMakeLists.txt | 1 + data/CMakeLists.txt | 2 +- data/example.conf | 4 +- data/mysql/tables.sql | 354 ++++++ data/tables.sql | 386 ------ docs/MYSQL | 27 - include/modules.h | 119 +- include/services.h | 1 + include/sockets.h | 24 + modules/CMakeLists.txt | 1 - modules/extra/db_mysql.cpp | 1550 +++++++++++++++++++++++ modules/extra/m_mysql.cpp | 414 ++++++ modules/extra/mysql/db_mysql.h | 212 ---- modules/extra/mysql/db_mysql_execute.cpp | 159 --- modules/extra/mysql/db_mysql_read.cpp | 618 --------- modules/extra/mysql/db_mysql_write.cpp | 1040 --------------- modules/extra/sql.h | 82 ++ modules/socketengines/m_socketengine_epoll.cpp | 8 +- modules/socketengines/m_socketengine_select.cpp | 2 + src/CMakeLists.txt | 14 +- src/bin/mydbgen | 5 +- src/module.cpp | 11 + src/modulemanager.cpp | 34 + src/modules.cpp | 16 + src/socketengines/socketengine_eventfd.cpp | 47 + src/socketengines/socketengine_pipe.cpp | 50 + src/socketengines/socketengine_win32.cpp | 76 ++ src/sockets.cpp | 7 + 28 files changed, 2805 insertions(+), 2459 deletions(-) create mode 100644 data/mysql/tables.sql delete mode 100644 data/tables.sql delete mode 100644 docs/MYSQL create mode 100644 modules/extra/db_mysql.cpp create mode 100644 modules/extra/m_mysql.cpp delete mode 100644 modules/extra/mysql/db_mysql.h delete mode 100644 modules/extra/mysql/db_mysql_execute.cpp delete mode 100644 modules/extra/mysql/db_mysql_read.cpp delete mode 100644 modules/extra/mysql/db_mysql_write.cpp create mode 100644 modules/extra/sql.h create mode 100644 src/socketengines/socketengine_eventfd.cpp create mode 100644 src/socketengines/socketengine_pipe.cpp create mode 100644 src/socketengines/socketengine_win32.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 64d533dcf..30810405b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -330,6 +330,7 @@ check_function_exists(strlcat HAVE_STRLCAT) check_function_exists(strlcpy HAVE_STRLCPY) check_function_exists(umask HAVE_UMASK) check_function_exists(backtrace HAVE_BACKTRACE) +check_function_exists(eventfd HAVE_EVENTFD) # Check for the existance of the following types check_type_size(uint8_t UINT8_T) diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index 63d55d4cd..461ea287a 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -1,6 +1,6 @@ # Only install example.chk and example.conf from this directory # NOTE: I would've had this just find all files in the directory, but that would include files not needed (like this file) -set(DATA example.chk example.conf tables.sql) +set(DATA example.chk example.conf mysql/tables.sql) install(FILES ${DATA} DESTINATION data ) diff --git a/data/example.conf b/data/example.conf index 1a9ee7ff3..c580399fb 100644 --- a/data/example.conf +++ b/data/example.conf @@ -292,9 +292,7 @@ options * * Supported: * - db_plain - * - db_mysql_write - * - db_mysql_read - * - db_mysql_execute + * - db_mysql */ database = "db_plain" diff --git a/data/mysql/tables.sql b/data/mysql/tables.sql new file mode 100644 index 000000000..6b9c41b89 --- /dev/null +++ b/data/mysql/tables.sql @@ -0,0 +1,354 @@ +SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; + +-- +-- Table structure for table `anope_bs_badwords` +-- + +DROP TABLE IF EXISTS `anope_bs_badwords`; +CREATE TABLE IF NOT EXISTS `anope_bs_badwords` ( + `channel` varchar(255) NOT NULL default '', + `word` varchar(255) NOT NULL, + `type` varchar(50) NOT NULL, + UNIQUE KEY `channel` (`channel`,`word`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_bs_core` +-- + +DROP TABLE IF EXISTS `anope_bs_core`; +CREATE TABLE IF NOT EXISTS `anope_bs_core` ( + `nick` varchar(255) NOT NULL default '', + `user` varchar(255) NOT NULL default '', + `host` text NOT NULL, + `rname` text NOT NULL, + `flags` text NOT NULL, + `created` int(10) unsigned NOT NULL default '0', + `chancount` int(11) NOT NULL default '0', + PRIMARY KEY (`nick`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_bs_info_metadata` +-- + +DROP TABLE IF EXISTS `anope_bs_info_metadata`; +CREATE TABLE IF NOT EXISTS `anope_bs_info_metadata` ( + `botname` varchar(255) NOT NULL default '', + `name` varchar(255) NOT NULL default '', + `value` text NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_cs_access` +-- + +DROP TABLE IF EXISTS `anope_cs_access`; +CREATE TABLE IF NOT EXISTS `anope_cs_access` ( + `level` int(11) NOT NULL default '0', + `display` varchar(255) NOT NULL default '', + `channel` varchar(255) NOT NULL default '', + `last_seen` int(10) unsigned NOT NULL default '0', + `creator` varchar(255) NOT NULL default '', + UNIQUE KEY `channel` (`channel`,`display`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_cs_akick` +-- + +DROP TABLE IF EXISTS `anope_cs_akick`; +CREATE TABLE IF NOT EXISTS `anope_cs_akick` ( + `channel` varchar(255) NOT NULL default '', + `flags` varchar(255) NOT NULL default '', + `mask` varchar(255) NOT NULL default '', + `reason` text NOT NULL, + `creator` varchar(255) NOT NULL default '', + `created` int(10) unsigned NOT NULL default '0', + `last_used` int(10) unsigned NOT NULL default '0', + UNIQUE KEY `channel` (`channel`,`mask`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_cs_info` +-- + +DROP TABLE IF EXISTS `anope_cs_info`; +CREATE TABLE IF NOT EXISTS `anope_cs_info` ( + `name` varchar(255) NOT NULL default '', + `founder` text NOT NULL, + `successor` text NOT NULL, + `descr` text NOT NULL, + `time_registered` int(10) unsigned NOT NULL default '0', + `last_used` int(10) unsigned NOT NULL default '0', + `last_topic` text NOT NULL, + `last_topic_setter` text NOT NULL, + `last_topic_time` int(10) unsigned NOT NULL default '0', + `flags` text NOT NULL, + `forbidby` text NOT NULL, + `forbidreason` text NOT NULL, + `bantype` smallint(6) NOT NULL default '0', + `mlock_on` text NOT NULL, + `mlock_off` text NOT NULL, + `mlock_params` text NOT NULL, + `entry_message` text NOT NULL, + `memomax` smallint(5) unsigned NOT NULL default '0', + `botnick` varchar(255) NOT NULL default '', + `botflags` text NOT NULL, + `capsmin` smallint(6) NOT NULL default '0', + `capspercent` smallint(6) NOT NULL default '0', + `floodlines` smallint(6) NOT NULL default '0', + `floodsecs` smallint(6) NOT NULL default '0', + `repeattimes` smallint(6) NOT NULL default '0', + PRIMARY KEY (`name`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_cs_info_metadata` +-- + +DROP TABLE IF EXISTS `anope_cs_info_metadata`; +CREATE TABLE IF NOT EXISTS `anope_cs_info_metadata` ( + `channel` varchar(255) NOT NULL default '', + `name` varchar(255) NOT NULL default '', + `value` text NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_cs_levels` +-- + +DROP TABLE IF EXISTS `anope_cs_levels`; +CREATE TABLE IF NOT EXISTS `anope_cs_levels` ( + `channel` varchar(255) NOT NULL default '', + `position` int(11) NOT NULL default '0', + `level` int(11) NOT NULL default '0', + UNIQUE KEY `channel` (`channel`,`position`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_cs_ttb` +-- + +DROP TABLE IF EXISTS `anope_cs_ttb`; +CREATE TABLE IF NOT EXISTS `anope_cs_ttb` ( + `channel` varchar(255) NOT NULL default '', + `ttb_id` int(11) NOT NULL default '0', + `value` int(11) NOT NULL default '0', + UNIQUE KEY `channel` (`channel`,`ttb_id`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_extra` +-- + +DROP TABLE IF EXISTS `anope_extra`; +CREATE TABLE IF NOT EXISTS `anope_extra` ( + `data` text NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_info` +-- + +DROP TABLE IF EXISTS `anope_info`; +CREATE TABLE IF NOT EXISTS `anope_info` ( + `version` int(11) default NULL, + `date` datetime default NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ms_info` +-- + +DROP TABLE IF EXISTS `anope_ms_info`; +CREATE TABLE IF NOT EXISTS `anope_ms_info` ( + `receiver` varchar(255) NOT NULL, + `number` int(11) NOT NULL default '0', + `flags` int(11) NOT NULL default '0', + `time` int(10) unsigned NOT NULL default '0', + `sender` text NOT NULL, + `text` blob NOT NULL, + `serv` enum('NICK','CHAN') NOT NULL default 'NICK' +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ns_access` +-- + +DROP TABLE IF EXISTS `anope_ns_access`; +CREATE TABLE IF NOT EXISTS `anope_ns_access` ( + `display` varchar(255) NOT NULL default '', + `access` varchar(160) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ns_alias` +-- + +DROP TABLE IF EXISTS `anope_ns_alias`; +CREATE TABLE IF NOT EXISTS `anope_ns_alias` ( + `nick` varchar(255) NOT NULL default '', + `last_quit` text NOT NULL, + `last_realname` text NOT NULL, + `last_usermask` text NOT NULL, + `time_registered` int(10) unsigned NOT NULL default '0', + `last_seen` int(10) unsigned NOT NULL default '0', + `flags` text NOT NULL, + `display` varchar(255) NOT NULL default '', + PRIMARY KEY (`nick`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ns_alias_metadata` +-- + +DROP TABLE IF EXISTS `anope_ns_alias_metadata`; +CREATE TABLE IF NOT EXISTS `anope_ns_alias_metadata` ( + `nick` varchar(255) NOT NULL default '', + `name` varchar(255) NOT NULL default '', + `value` text NOT NULL, + PRIMARY KEY (`name`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ns_core` +-- + +DROP TABLE IF EXISTS `anope_ns_core`; +CREATE TABLE IF NOT EXISTS `anope_ns_core` ( + `display` varchar(255) NOT NULL default '', + `pass` text NOT NULL, + `email` text NOT NULL, + `greet` text NOT NULL, + `flags` text NOT NULL, + `language` smallint(5) unsigned NOT NULL default '0', + `channelcount` smallint(5) unsigned NOT NULL default '0', + `memomax` smallint(5) unsigned NOT NULL default '0', + PRIMARY KEY (`display`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ns_core_metadata` +-- + +DROP TABLE IF EXISTS `anope_ns_core_metadata`; +CREATE TABLE IF NOT EXISTS `anope_ns_core_metadata` ( + `nick` varchar(255) NOT NULL default '', + `name` varchar(255) NOT NULL default '', + `value` text NOT NULL, + PRIMARY KEY (`name`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_ns_request` +-- + +DROP TABLE IF EXISTS `anope_ns_request`; +CREATE TABLE IF NOT EXISTS `anope_ns_request` ( + `nick` varchar(255) NOT NULL default '', + `passcode` text NOT NULL, + `password` text NOT NULL, + `email` text NOT NULL, + `requested` int(10) unsigned NOT NULL default '0', + PRIMARY KEY (`nick`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_os_akills` +-- + +DROP TABLE IF EXISTS `anope_os_akills`; +CREATE TABLE IF NOT EXISTS `anope_os_akills` ( + `user` varchar(255) NOT NULL, + `host` varchar(255) NOT NULL, + `xby` text NOT NULL, + `reason` text NOT NULL, + `seton` int(10) unsigned NOT NULL default '0', + `expire` int(10) unsigned NOT NULL default '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_os_core` +-- + +DROP TABLE IF EXISTS `anope_os_core`; +CREATE TABLE IF NOT EXISTS `anope_os_core` ( + `maxusercnt` int(11) NOT NULL default '0', + `maxusertime` int(10) unsigned NOT NULL default '0', + `akills_count` int(11) NOT NULL default '0', + `snlines_count` int(11) NOT NULL default '0', + `sqlines_count` int(11) NOT NULL default '0', + `szlines_count` int(11) NOT NULL default '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_os_exceptions` +-- + +DROP TABLE IF EXISTS `anope_os_exceptions`; +CREATE TABLE IF NOT EXISTS `anope_os_exceptions` ( + `mask` varchar(255) NOT NULL, + `slimit` int(11) NOT NULL default '0', + `who` text NOT NULL, + `reason` text NOT NULL, + `time` int(10) unsigned NOT NULL default '0', + `expires` int(10) unsigned NOT NULL default '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +-- -------------------------------------------------------- + +-- +-- Table structure for table `anope_os_xlines` +-- + +DROP TABLE IF EXISTS `anope_os_xlines`; +CREATE TABLE IF NOT EXISTS `anope_os_xlines` ( + `type` varchar(20) NOT NULL, + `mask` varchar(255) NOT NULL, + `xby` text NOT NULL, + `reason` text NOT NULL, + `seton` int(10) unsigned NOT NULL default '0', + `expire` int(10) unsigned NOT NULL default '0' +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + diff --git a/data/tables.sql b/data/tables.sql deleted file mode 100644 index deeef41cc..000000000 --- a/data/tables.sql +++ /dev/null @@ -1,386 +0,0 @@ - --- If you need to create your db, uncomment the following lines. --- --- CREATE DATABASE anope; --- USE anope; - - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_extra' --- - -DROP TABLE IF EXISTS anope_extra; -CREATE TABLE anope_extra ( - data text NOT NULL default '' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_commands' --- - -DROP TABLE IF EXISTS anope_commands; -CREATE TABLE anope_commands ( - nick varchar(255) NOT NULL default '', - service varchar(255) NOT NULL default '', - command text NOT NULL default '' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_ns_core' --- - -DROP TABLE IF EXISTS anope_ns_core; -CREATE TABLE anope_ns_core ( - display varchar(255) NOT NULL default '', - pass text NOT NULL, - email text NOT NULL default '', - greet text NOT NULL default '', - icq int(10) unsigned NOT NULL default '0', - url text NOT NULL default '', - flags text NOT NULL default '', - language smallint(5) unsigned NOT NULL default '0', - channelcount smallint(5) unsigned NOT NULL default '0', - memomax smallint(5) unsigned NOT NULL default '0', - PRIMARY KEY (display) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- --- Table structure for table 'anope_ns_core_metadata' --- - -DROP TABLE IF EXISTS anope_ns_core_metadata; -CREATE TABLE anope_ns_core_metadata ( - nick varchar(255) NOT NULL default '', - name varchar(255) NOT NULL default '', - value text NOT NULL default '', - PRIMARY KEY (name) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- --------------------------------------------------------- --- --- Table structure for table 'anope_ns_alias' --- - -DROP TABLE IF EXISTS anope_ns_alias; -CREATE TABLE anope_ns_alias ( - nick varchar(255) NOT NULL default '', - last_quit text NOT NULL, - last_realname text NOT NULL, - last_usermask text NOT NULL, - time_registered int(10) unsigned NOT NULL default '0', - last_seen int(10) unsigned NOT NULL default '0', - flags text NOT NULL default '', - display varchar(255) NOT NULL default '', - PRIMARY KEY (nick) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- --- Table structure for table 'anope_ns_alias_metadata' --- - -DROP TABLE IF EXISTS anope_ns_alias_metadata; -CREATE TABLE anope_ns_alias_metadata ( - nick varchar(255) NOT NULL default '', - name varchar(255) NOT NULL default '', - value text NOT NULL default '', - PRIMARY KEY (name) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_ns_access' --- - -DROP TABLE IF EXISTS anope_ns_access; -CREATE TABLE anope_ns_access ( - display varchar(255) NOT NULL default '', - access varchar(160) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- --------------------------------------------------------- - --- --- Table structure for table 'anope_ns_request' --- - -DROP TABLE IF EXISTS anope_ns_request; -CREATE TABLE anope_ns_request ( - nick varchar(255) NOT NULL default '', - passcode text NOT NULL, - password text NOT NULL, - email text NOT NULL, - requested int(10) unsigned NOT NULL default '0', - PRIMARY KEY (nick) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_cs_access' --- - -DROP TABLE IF EXISTS anope_cs_access; -CREATE TABLE anope_cs_access ( - level int(11) NOT NULL default '0', - display varchar(255) NOT NULL default '', - channel varchar(255) NOT NULL default '', - last_seen int(10) unsigned NOT NULL default '0', - creator varchar(255) NOT NULL default '', - UNIQUE KEY (channel,display) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_cs_akick' --- - -DROP TABLE IF EXISTS anope_cs_akick; -CREATE TABLE anope_cs_akick ( - channel varchar(255) NOT NULL default '', - flags varchar(255) NOT NULL default '', - mask varchar(255) NOT NULL default '', - reason text NOT NULL default '', - creator varchar(255) NOT NULL default '', - created int(10) unsigned NOT NULL default '0', - last_used int(10) unsigned NOT NULL default '0', - UNIQUE KEY channel (channel, mask) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_bs_badwords' --- - -DROP TABLE IF EXISTS anope_bs_badwords; -CREATE TABLE anope_bs_badwords ( - channel varchar(255) NOT NULL default '', - word varchar(255) NOT NULL, - type varchar(50) NOT NULL, - UNIQUE KEY channel (channel,word) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_cs_info' --- - -DROP TABLE IF EXISTS anope_cs_info; -CREATE TABLE anope_cs_info ( - name varchar(255) NOT NULL default '', - founder text NOT NULL, - successor text NOT NULL, - descr text NOT NULL, - url text NOT NULL, - email text NOT NULL, - time_registered int(10) unsigned NOT NULL default '0', - last_used int(10) unsigned NOT NULL default '0', - last_topic text NOT NULL, - last_topic_setter text NOT NULL, - last_topic_time int(10) unsigned NOT NULL default '0', - flags text NOT NULL default '', - forbidby text NOT NULL, - forbidreason text NOT NULL, - bantype smallint(6) NOT NULL default '0', - mlock_on text NOT NULL default '', - mlock_off text NOT NULL default '', - mlock_params text NOT NULL default '', - entry_message text NOT NULL, - memomax smallint(5) unsigned NOT NULL default '0', - botnick varchar(255) NOT NULL default '', - botflags text NOT NULL default '', - capsmin smallint(6) NOT NULL default '0', - capspercent smallint(6) NOT NULL default '0', - floodlines smallint(6) NOT NULL default '0', - floodsecs smallint(6) NOT NULL default '0', - repeattimes smallint(6) NOT NULL default '0', - PRIMARY KEY (name) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_cs_info_metadata' --- - -DROP TABLE IF EXISTS anope_cs_info_metadata; -CREATE TABLE anope_cs_info_metadata ( - channel varchar(255) NOT NULL default '', - name varchar(255) NOT NULL default '', - value text NOT NULL default '' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_cs_levels' --- - -DROP TABLE IF EXISTS anope_cs_levels; -CREATE TABLE anope_cs_levels ( - channel varchar(255) NOT NULL default '', - position int(11) NOT NULL default '0', - level int(11) NOT NULL default '0', - UNIQUE KEY channel (channel,position) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_cs_ttb' --- - -DROP TABLE IF EXISTS anope_cs_ttb; -CREATE TABLE anope_cs_ttb ( - channel varchar(255) NOT NULL default '', - ttb_id int(11) NOT NULL default '0', - value int(11) NOT NULL default '0', - UNIQUE KEY channel (channel,ttb_id) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_bs_core' --- - -DROP TABLE IF EXISTS anope_bs_core; -CREATE TABLE anope_bs_core ( - nick varchar(255) NOT NULL default '', - user varchar(255) NOT NULL default '', - host text NOT NULL default '', - rname text NOT NULL default '', - flags text NOT NULL default '', - created int(10) unsigned NOT NULL default '0', - chancount int(11) NOT NULL default '0', - PRIMARY KEY (nick) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_bs_info_metadata' --- - -DROP TABLE IF EXISTS anope_bs_info_metadata; -CREATE TABLE anope_bs_info_metadata ( - botname varchar(255) NOT NULL default '', - name varchar(255) NOT NULL default '', - value text NOT NULL default '' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_ms_info' --- - -DROP TABLE IF EXISTS anope_ms_info; -CREATE TABLE anope_ms_info ( - receiver varchar(255) NOT NULL, - number int(11) NOT NULL default '0', - flags int(11) NOT NULL default '0', - time int(10) unsigned NOT NULL default '0', - sender text NOT NULL, - text blob NOT NULL, - serv enum('NICK','CHAN') NOT NULL default 'NICK' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_os_akills' --- - -DROP TABLE IF EXISTS anope_os_akills; -CREATE TABLE anope_os_akills ( - user varchar(255) NOT NULL, - host varchar(255) NOT NULL, - xby text NOT NULL, - reason text NOT NULL, - seton int(10) unsigned NOT NULL default '0', - expire int(10) unsigned NOT NULL default '0' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_os_core' --- - -DROP TABLE IF EXISTS anope_os_core; -CREATE TABLE anope_os_core ( - maxusercnt int(11) NOT NULL default '0', - maxusertime int(10) unsigned NOT NULL default '0', - akills_count int(11) NOT NULL default '0', - snlines_count int(11) NOT NULL default '0', - sqlines_count int(11) NOT NULL default '0', - szlines_count int(11) NOT NULL default '0' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_os_exceptions' --- - -DROP TABLE IF EXISTS anope_os_exceptions; -CREATE TABLE anope_os_exceptions ( - mask varchar(255) NOT NULL, - slimit int(11) NOT NULL default '0', - who text NOT NULL, - reason text NOT NULL, - time int(10) unsigned NOT NULL default '0', - expires int(10) unsigned NOT NULL default '0' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_os_xlines' --- - -DROP TABLE IF EXISTS anope_os_xlines; -CREATE TABLE anope_os_xlines ( - type varchar(20) NOT NULL, - mask varchar(255) NOT NULL, - xby text NOT NULL, - reason text NOT NULL, - seton int(10) unsigned NOT NULL default '0', - expire int(10) unsigned NOT NULL default '0' -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_metadata' --- - -DROP TABLE IF EXISTS anope_metadata; -CREATE TABLE anope_metadata ( - name varchar(255) NOT NULL default '', - value text NOT NULL default '', - PRIMARY KEY (name) -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - --- -------------------------------------------------------- - --- --- Table structure for table 'anope_info' --- - -DROP TABLE IF EXISTS anope_info; -CREATE TABLE anope_info ( - version int(11) default NULL, - date datetime default NULL -) ENGINE=MyISAM DEFAULT CHARSET=latin1; - diff --git a/docs/MYSQL b/docs/MYSQL deleted file mode 100644 index a72482107..000000000 --- a/docs/MYSQL +++ /dev/null @@ -1,27 +0,0 @@ -Anope MySQL Support -------------------- - -MySQL support was readded in version 1.9.2 In the form of three modules. - -db_mysql_read - Allows you to load your databases from MySQL. -db_mysql_write - Allows live updating of SQL tables whenever something is executed in Anope. -db_mysql_execute - Allows executing of Anope commands via SQL. - -To execute commands via SQL, you must insert the command into the anope_commands table, an example is as follows: - -INSERT INTO `anope_commands` (nick, service, command) VALUES('Adam', 'NickServ', 'REGISTER qwerty Adam@anope.org'); - -By default, every 60 seconds Anope checks this table for commands to execute. When Anope sees a new command to execute, it checks the following. - -If the nick given is -SQLUser, then the command gets executed by a special fake user within Anope called -SQLUser. -SQLUser has every permission and command available, there are no permission checks at all for this user, it can do anything. - -If the nick is not -SQLUser, it checks to see if it is a registered nick. If it is, it sees if there are any users online using the NickCore of that nick. - If there is a user online with that core the command gets executed as if that user executed it, and the reply goes to that user. - If there isn't a user online with the core, it creates a fake user as the nick given to it, and gives to it the permissions the user would have if they were online and identified. -If the nick is not registered, it checks to see if there is a user currently on that nick. If there is, it executes the command as that user, and the reply goes to that user. - If the nick is not registered and no one is using the nick, it creates a fake user of the nick given to it, and gives it regular nonidentified user access. - -Currently there is no way to check to see if a command was executed successfully within Anope from SQL (even if there was, the possibly update delay would be a problem). - -IMPORTANT: When using db_mysql_write when you already have a database, you need to do an initial import of the data to SQL (as db_mysql_write only updates it when it is changed, it never actually mass-dumps all of your data into SQL). To do this, start Anope and execute /OperServ SQLSYNC. - diff --git a/include/modules.h b/include/modules.h index 627e04383..307c83d87 100644 --- a/include/modules.h +++ b/include/modules.h @@ -160,9 +160,6 @@ extern CoreExport message_map MessageMap; class Module; extern CoreExport std::list Modules; -/*************************************************************************/ -/* Structure for information about a *Serv command. */ - class Version { private: @@ -1109,11 +1106,16 @@ enum Implementation I_END }; +class Service; /** Used to manage modules. */ class CoreExport ModuleManager { + private: + /** A map of service providers + */ + static std::map ServiceProviders; public: /** Event handler hooks. * This needs to be public to be used by FOREACH_MOD and friends. @@ -1202,6 +1204,24 @@ class CoreExport ModuleManager */ static void UnloadAll(bool unload_proto); + /** Register a service + * @param s The service + * @return true if it was successfully registeed, else false (service name colision) + */ + static bool RegisterService(Service *s); + + /** Unregister a service + * @param s The service + * @return true if it was unregistered successfully + */ + static bool UnregisterService(Service *s); + + /** Get a service + * @param name The service name + * @return The services, or NULL + */ + static Service *GetService(const Anope::string &name); + private: /** Call the module_delete function to safely delete the module * @param m the module to delete @@ -1229,6 +1249,99 @@ class CallBack : public Timer } }; +class Service +{ + public: + Module *owner; + Anope::string name; + + Service(Module *o, const Anope::string &n); + + virtual ~Service(); +}; + +class dynamic_reference_base +{ + public: + dynamic_reference_base(); + + virtual ~dynamic_reference_base(); +}; + +extern std::list dyn_references; + +template +class dynamic_reference : public dynamic_reference_base +{ + protected: + T *ref; + public: + dynamic_reference() : dynamic_reference_base(), ref(NULL) { } + + dynamic_reference(T *obj) : dynamic_reference_base(), ref(obj) { } + + virtual ~dynamic_reference() { } + + virtual operator bool() + { + return this->ref; + } + + virtual inline T *operator->() + { + return this->ref; + } + + void Invalidate() + { + this->ref = NULL; + } + + static void Invalidate(T *obj) + { + for (std::list::iterator it = dyn_references.begin(), it_end = dyn_references.end(); it != it_end;) + { + dynamic_reference *d = static_cast *>(*it); + ++it; + + if (d && d->ref == obj) + { + d->Invalidate(); + } + } + } +}; + +template +class service_reference : public dynamic_reference +{ + Module *owner; + Anope::string name; + + public: + service_reference(Module *o, const Anope::string &n) : dynamic_reference(), owner(o), name(n) + { + } + + ~service_reference() + { + } + + operator bool() + { + if (!this->ref) + this->ref = static_cast(ModuleManager::GetService(this->name)); + return this->ref; + } + + inline T *operator->() + { + if (!this->ref) + this->ref = static_cast(ModuleManager::GetService(this->name)); + return this->ref; + } +}; + struct Message { Anope::string name; diff --git a/include/services.h b/include/services.h index 1a81d6e96..15670c8ac 100644 --- a/include/services.h +++ b/include/services.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include diff --git a/include/sockets.h b/include/sockets.h index 78ed5f4af..6eeb3bf3d 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -81,6 +81,10 @@ class CoreExport Socket : public Flags /* Type this socket is */ SocketType Type; + /** Empty constructor, used for things such as the pipe socket + */ + Socket(); + /** Default constructor * @param nsock The socket to use, 0 if we need to create our own * @param nIPv6 true if using ipv6 @@ -149,6 +153,26 @@ class CoreExport Socket : public Flags void Write(const Anope::string &message); }; +class CoreExport Pipe : public Socket +{ + private: + int WritePipe; + + int RecvInternal(char *buf, size_t sz) const; + + int SendInternal(const Anope::string &buf) const; + public: + Pipe(); + + bool ProcessRead(); + + bool Read(const Anope::string &); + + void Notify(); + + virtual void OnNotify(); +}; + class CoreExport ClientSocket : public Socket { protected: diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index 8a90f3a3a..21ca74244 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -86,7 +86,6 @@ foreach(MODULE_FOLDER ${MODULES_FOLDERS}) # Get a list of ALL files and directories within this modules directory file(GLOB SUBMODULE_DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${MODULE_FOLDER}/*") remove_item_from_list(SUBMODULE_DIRS "CMakeFiles") - remove_item_from_list(SUBMODULE_DIRS "extra/mysql") # XXX till sql is fixed foreach(SUBDIR ${SUBMODULE_DIRS}) if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}") diff --git a/modules/extra/db_mysql.cpp b/modules/extra/db_mysql.cpp new file mode 100644 index 000000000..e93abba10 --- /dev/null +++ b/modules/extra/db_mysql.cpp @@ -0,0 +1,1550 @@ +#include "module.h" +#include "sql.h" + +struct NickAliasFlagInfo +{ + Anope::string Name; + NickNameFlag Flag; +}; + +NickAliasFlagInfo NickAliasFlags[] = { + {"FORBIDDEN", NS_FORBIDDEN}, + {"NOEXPIRE", NS_NO_EXPIRE}, + {"", static_cast(-1)} +}; + +struct NickCoreFlagInfo +{ + Anope::string Name; + NickCoreFlag Flag; +}; + +NickCoreFlagInfo NickCoreFlags[] = { + {"KILLPROTECT", NI_KILLPROTECT}, + {"SECURE", NI_SECURE}, + {"MSG", NI_MSG}, + {"MEMO_HARDMAX", NI_MEMO_HARDMAX}, + {"MEMO_SIGNON", NI_MEMO_SIGNON}, + {"MEMO_RECEIVE", NI_MEMO_RECEIVE}, + {"PRIVATE", NI_PRIVATE}, + {"HIDE_EMAIL", NI_HIDE_EMAIL}, + {"HIDE_MASK", NI_HIDE_MASK}, + {"HIDE_QUIT", NI_HIDE_QUIT}, + {"KILL_QUICK", NI_KILL_QUICK}, + {"KILL_IMMED", NI_KILL_IMMED}, + {"MEMO_MAIL", NI_MEMO_MAIL}, + {"HIDE_STATUS", NI_HIDE_STATUS}, + {"SUSPENDED", NI_SUSPENDED}, + {"AUTOOP", NI_AUTOOP}, + {"FORBIDDEN", NI_FORBIDDEN}, + {"", static_cast(-1)} +}; + +struct BotServFlagInfo +{ + Anope::string Name; + BotFlag Flag; +}; + +BotServFlagInfo BotServFlags[] = { + {"PRIVATE", BI_PRIVATE}, + {"", static_cast(-1)} +}; + +struct BotFlagInfo +{ + Anope::string Name; + BotServFlag Flag; +}; + +BotFlagInfo BotFlags[] = { + {"DONTKICKOPS", BS_DONTKICKOPS}, + {"DONTKICKVOICES", BS_DONTKICKVOICES}, + {"FANTASY", BS_FANTASY}, + {"SYMBIOSIS", BS_SYMBIOSIS}, + {"GREET", BS_GREET}, + {"NOBOT", BS_NOBOT}, + {"KICK_BOLDS", BS_KICK_BOLDS}, + {"KICK_COLORS", BS_KICK_COLORS}, + {"KICK_REVERSES", BS_KICK_REVERSES}, + {"KICK_UNDERLINES", BS_KICK_UNDERLINES}, + {"KICK_BADWORDS", BS_KICK_BADWORDS}, + {"KICK_CAPS", BS_KICK_CAPS}, + {"KICK_FLOOD", BS_KICK_FLOOD}, + {"KICK_REPEAT", BS_KICK_REPEAT}, + {"KICK_ITALICS", BS_KICK_ITALICS}, + {"", static_cast(-1)} +}; + +struct ChannelFlagInfo +{ + Anope::string Name; + ChannelInfoFlag Flag; +}; + +ChannelFlagInfo ChannelFlags[] = { + {"KEEPTOPIC", CI_KEEPTOPIC}, + {"SECUREOPS", CI_SECUREOPS}, + {"PRIVATE", CI_PRIVATE}, + {"TOPICLOCK", CI_TOPICLOCK}, + {"RESTRICTED", CI_RESTRICTED}, + {"PEACE", CI_PEACE}, + {"SECURE", CI_SECURE}, + {"FORBIDDEN", CI_FORBIDDEN}, + {"NO_EXPIRE", CI_NO_EXPIRE}, + {"MEMO_HARDMAX", CI_MEMO_HARDMAX}, + {"OPNOTICE", CI_OPNOTICE}, + {"SECUREFOUNDER", CI_SECUREFOUNDER}, + {"SIGNKICK", CI_SIGNKICK}, + {"SIGNKICK_LEVEL", CI_SIGNKICK_LEVEL}, + {"XOP", CI_XOP}, + {"SUSPENDED", CI_SUSPENDED}, + {"PERSIST", CI_PERSIST}, + {"", static_cast(-1)} +}; + +struct MemoFlagInfo +{ + Anope::string Name; + MemoFlag Flag; +}; + +MemoFlagInfo MemoFlags[] = { + {"UNREAD", MF_UNREAD}, + {"RECEIPT", MF_RECEIPT}, + {"NOTIFYS", MF_NOTIFYS}, + {"", static_cast(-1)} +}; + +static std::vector MakeVector(const Anope::string &buf) +{ + Anope::string s; + spacesepstream sep(buf); + std::vector params; + + while (sep.GetToken(s)) + { + if (s[0] == ':') + { + s.erase(s.begin()); + if (!s.empty() && !sep.StreamEnd()) + params.push_back(s + " " + sep.GetRemaining()); + else if (!s.empty()) + params.push_back(s); + } + else + params.push_back(s); + } + + return params; +} + +static Anope::string BuildFlagsList(ChannelInfo *ci) +{ + Anope::string ret; + + for (int i = 0; ChannelFlags[i].Flag != -1; ++i) + if (ci->HasFlag(ChannelFlags[i].Flag)) + ret += " " + ChannelFlags[i].Name; + + if (!ret.empty()) + ret.erase(ret.begin()); + + return ret; +} + +static Anope::string BuildFlagsList(NickAlias *na) +{ + Anope::string ret; + + for (int i = 0; NickAliasFlags[i].Flag != -1; ++i) + if (na->HasFlag(NickAliasFlags[i].Flag)) + ret += " " + NickAliasFlags[i].Name; + + if (!ret.empty()) + ret.erase(ret.begin()); + + return ret; +} + +static Anope::string BuildFlagsList(NickCore *nc) +{ + Anope::string ret; + + for (int i = 0; NickCoreFlags[i].Flag != -1; ++i) + if (nc->HasFlag(NickCoreFlags[i].Flag)) + ret += " " + NickCoreFlags[i].Name; + + if (!ret.empty()) + ret.erase(ret.begin()); + + return ret; +} + +static Anope::string BuildFlagsList(Memo *m) +{ + Anope::string ret; + + for (int i = 0; MemoFlags[i].Flag != -1; ++i) + if (m->HasFlag(MemoFlags[i].Flag)) + ret += " " + MemoFlags[i].Name; + + if (!ret.empty()) + ret.erase(ret.begin()); + + return ret; +} + +static Anope::string MakeMLock(ChannelInfo *ci, bool status) +{ + Anope::string ret; + + for (std::list::iterator it = ModeManager::Modes.begin(), it_end = ModeManager::Modes.end(); it != it_end; ++it) + { + if ((*it)->Class == MC_CHANNEL) + { + ChannelMode *cm = debug_cast(*it); + + if (ci->HasMLock(cm->Name, status)) + ret += " " + cm->NameAsString; + } + } + + if (!ret.empty()) + ret.erase(ret.begin()); + + return ret; +} + +static inline Anope::string GetMLockOn(ChannelInfo *ci) +{ + return MakeMLock(ci, true); +} + +static inline Anope::string GetMLockOff(ChannelInfo *ci) +{ + return MakeMLock(ci, false); +} + +static Anope::string GetMLockParams(ChannelInfo *ci) +{ + Anope::string ret; + + for (std::list::iterator it = ModeManager::Modes.begin(), it_end = ModeManager::Modes.end(); it != it_end; ++it) + { + if ((*it)->Class == MC_CHANNEL) + { + ChannelMode *cm = debug_cast(*it); + + Anope::string param; + if (ci->GetParam(cm->Name, param)) + ret += " " + cm->NameAsString + " " + param; + } + } + + if (!ret.empty()) + ret.erase(ret.begin()); + + return ret; +} + +static Anope::string GetBotFlags(Flags& Flags) +{ + Anope::string buf; + + for (int i = 0; BotFlags[i].Flag != -1; ++i) + if (Flags.HasFlag(BotFlags[i].Flag)) + buf += " " + BotFlags[i].Name; + + if (!buf.empty()) + buf.erase(buf.begin()); + + return buf; +} + +static Anope::string GetBotServFlags(BotInfo *bi) +{ + Anope::string buf; + + for (int i = 0; BotServFlags[i].Flag != -1; ++i) + if (bi->HasFlag(BotServFlags[i].Flag)) + buf += " " + BotServFlags[i].Name; + + if (!buf.empty()) + buf.erase(buf.begin());; + + return buf; +} + +static NickAlias *CurNick = NULL; +static NickCore *CurCore = NULL; +static ChannelInfo *CurChannel = NULL; +static BotInfo *CurBot = NULL; + +static void Write(const Anope::string &data); +static void WriteNickMetadata(const Anope::string &key, const Anope::string &data); +static void WriteCoreMetadata(const Anope::string &key, const Anope::string &data); +static void WriteChannelMetadata(const Anope::string &key, const Anope::string &data); +static void WriteBotMetadata(const Anope::string &key, const Anope::string &data); + +class CommandSQLSync : public Command +{ + public: + CommandSQLSync() : Command("SQLSYNC", 0, 0, "operserv/sqlsync") + { + } + + CommandReturn Execute(User *u, const std::vector ¶ms); + + bool OnHelp(User *u, const Anope::string &subcommand) + { + notice_help(Config.s_OperServ, u, OPER_HELP_SYNC); + return true; + } + + void OnServHelp(User *u) + { + notice_lang(Config.s_OperServ, u, OPER_HELP_CMD_SQLSYNC); + } +}; + +class MySQLInterface : public SQLInterface +{ + public: + MySQLInterface(Module *o) : SQLInterface(o) { } + + void OnResult(const SQLResult &r); + + void OnError(const SQLResult &r); +}; + +class DBMySQL; +static DBMySQL *me; +class DBMySQL : public Module +{ + private: + CommandSQLSync commandsqlsync; + MySQLInterface interface; + service_reference SQL; + + public: + time_t lastwarn; + bool ro; + + void RunQuery(const Anope::string &query) + { + if (SQL) + { + if (readonly && this->ro) + { + readonly = this->ro = false; + ircdproto->SendGlobops(OperServ, "Found SQL again, going out of readonly mode..."); + } + + SQL->Run(&interface, query); + } + else + { + time_t now = time(NULL); + + if (now - Config.UpdateTimeout > lastwarn) + { + ircdproto->SendGlobops(OperServ, "Unable to locate SQL reference, is m_mysql loaded? Going to readonly..."); + readonly = this->ro = true; + this->lastwarn = now; + } + } + } + + const Anope::string Escape(const Anope::string &query) + { + return SQL ? SQL->Escape(query) : query; + } + + DBMySQL(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator), interface(this), SQL(this, "mysql/main") + { + me = this; + + this->lastwarn = 0; + this->ro = false; + + Implementation i[] = { + I_OnLoadDatabase, I_OnServerConnect + }; + ModuleManager::Attach(i, this, 2); + + this->AddCommand(OperServ, &commandsqlsync); + + if (uplink_server) + OnServerConnect(); + } + + void OnServerConnect() + { + Implementation i[] = { + /* Misc */ + I_OnSaveDatabase, I_OnPostCommand, + /* NickServ */ + I_OnNickAddAccess, I_OnNickEraseAccess, I_OnNickClearAccess, + I_OnDelCore, I_OnNickForbidden, I_OnNickGroup, I_OnMakeNickRequest, + I_OnDelNickRequest, I_OnNickRegister, I_OnChangeCoreDisplay, + I_OnNickSuspended, + /* ChanServ */ + I_OnAccessAdd, I_OnAccessDel, I_OnAccessChange, I_OnAccessClear, I_OnLevelChange, + I_OnChanForbidden, I_OnDelChan, I_OnChanRegistered, I_OnChanSuspend, + I_OnAkickAdd, I_OnAkickDel, + /* BotServ */ + I_OnBotCreate, I_OnBotChange, I_OnBotDelete, + I_OnBotAssign, I_OnBotUnAssign, + I_OnBadWordAdd, I_OnBadWordDel, + /* MemoServ */ + I_OnMemoSend, I_OnMemoDel, + /* OperServ */ + I_OnAddAkill, I_OnDelAkill, I_OnExceptionAdd, I_OnExceptionDel, + I_OnAddXLine, I_OnDelXLine + }; + ModuleManager::Attach(i, this, 39); + } + + EventReturn OnLoadDatabase() + { + if (!SQL) + { + Alog() << "Error, unable to find service reference for SQL, is m_mysql loaded and configured properly?"; + return EVENT_CONTINUE; + } + + SQLResult r = SQL->RunQuery("SELECT * FROM `anope_ns_core`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickCore *nc = new NickCore(r.Get(i, "display")); + nc->pass = r.Get(i, "pass"); + nc->email = r.Get(i, "email"); + nc->greet = r.Get(i, "greet"); + + spacesepstream sep(r.Get(i, "flags")); + Anope::string buf; + while (sep.GetToken(buf)) + { + for (int j = 0; NickCoreFlags[j].Flag != -1; ++j) + { + if (NickCoreFlags[j].Name == buf) + { + nc->SetFlag(NickCoreFlags[j].Flag); + break; + } + } + } + + nc->language = r.Get(i, "language").is_number_only() ? convertTo(r.Get(i, "language")) : LANG_EN_US; + nc->channelcount = r.Get(i, "channelcount").is_number_only() ? convertTo(r.Get(i, "channelcount")) : 0; + nc->memos.memomax = r.Get(i, "memomax").is_number_only() ? convertTo(r.Get(i, "memomax")) : 20; + } + + r = SQL->RunQuery("SELECT * FROM `anope_ns_access`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickCore *nc = findcore(r.Get(i, "display")); + if (!nc) + { + Alog() << "MySQL: Got NickCore access entry for nonexistant core " << r.Get(i, "display"); + continue; + } + + nc->AddAccess(r.Get(i, "access")); + } + + r = SQL->RunQuery("SELECT * FROM `anope_ns_core_metadata`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickCore *nc = findcore(r.Get(i, "display")); + if (!nc) + { + Alog() << "MySQL: Got NickCore access entry for nonexistant core " << r.Get(i, "display"); + continue; + } + + EventReturn MOD_RESULT;; + std::vector Params = MakeVector(r.Get(i, "value")); + FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(nc, r.Get(i, "name"), Params)); + } + + r = SQL->RunQuery("SELECT * FROM `anope_ns_alias`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickCore *nc = findcore(r.Get(i, "display")); + if (!nc) + { + Alog() << "MySQL: Got NickAlias for nick " << r.Get(i, "nick") << " with nonexistant core " << r.Get(i, "display"); + continue; + } + + NickAlias *na = new NickAlias(r.Get(i, "nick"), nc); + na->last_quit = r.Get(i, "last_quit"); + na->last_realname = r.Get(i, "last_realname"); + na->last_usermask = r.Get(i, "last_usermask"); + na->time_registered = r.Get(i, "time_registered").is_number_only() ? convertTo(r.Get(i, "time_registered")) : time(NULL); + na->last_seen = r.Get(i, "last_seen").is_number_only() ? convertTo(r.Get(i, "last_seen")) : time(NULL); + + spacesepstream sep(r.Get(i, "flags")); + Anope::string buf; + while (sep.GetToken(buf)) + { + for (int j = 0; NickAliasFlags[j].Flag != -1; ++j) + { + if (NickAliasFlags[j].Name == buf) + { + na->SetFlag(NickAliasFlags[j].Flag); + break; + } + } + } + } + + r = SQL->RunQuery("SELECT * FROM `anope_ns_alias_metadata`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickAlias *na = findnick(r.Get(i, "nick")); + if (!na) + { + Alog() << "MySQL: Got metadata for nonexistant nick " << r.Get(i, "nick"); + continue; + } + + EventReturn MOD_RESULT; + std::vector Params = MakeVector(r.Get(i, "value")); + FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(na, na->nick, Params)); + } + + r = SQL->RunQuery("SELECT * FROM `anope_bs_core`"); + for (int i = 0; i < r.Rows(); ++i) + { + BotInfo *bi = findbot(r.Get(i, "nick")); + if (!bi) + bi = new BotInfo(r.Get(i, "nick")); + bi->SetIdent(r.Get(i, "user")); + bi->host = r.Get(i, "host"); + bi->realname = r.Get(i, "rname"); + bi->created = r.Get(i, "created").is_number_only() ? convertTo(r.Get(i, "creeated")) : time(NULL); + + spacesepstream sep(r.Get(i, "flags")); + Anope::string buf; + while (sep.GetToken(buf)) + { + for (int j = 0; BotServFlags[j].Flag != -1; ++j) + { + if (buf == BotServFlags[j].Name) + { + bi->SetFlag(BotServFlags[j].Flag); + break; + } + } + } + } + + r = SQL->RunQuery("SELECT * FROM `anope_bs_info_metadata`"); + for (int i = 0; i < r.Rows(); ++i) + { + BotInfo *bi = findbot(r.Get(i, "botname")); + if (!bi) + { + Alog() << "MySQL: BotInfo metadata for nonexistant bot " << r.Get(i, "botname"); + continue; + } + + EventReturn MOD_RESULT; + std::vector Params = MakeVector(r.Get(i, "value")); + FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(bi, bi->nick, Params)); + } + + r = SQL->RunQuery("SELECT * FROM `anope_cs_info`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickCore *nc = NULL; + + if (!r.Get(i, "founder").empty()) + { + nc = findcore(r.Get(i, "founder")); + if (!nc) + { + Alog() << "MySQL: Channel " << r.Get(i, "name") << " with nonexistant founder " << r.Get(i, "founder"); + continue; + } + + ChannelInfo *ci = new ChannelInfo(r.Get(i, "name")); + ci->founder = nc; + if (!r.Get(i, "successor").empty()) + ci->successor = findcore(r.Get(i, "successor")); + ci->desc = r.Get(i, "descr"); + ci->time_registered = r.Get(i, "time_registered").is_number_only() ? convertTo(r.Get(i, "time_registered")) : time(NULL); + ci->last_used = r.Get(i, "last_used").is_number_only() ? convertTo(r.Get(i, "last_used")) : time(NULL); + ci->last_topic = r.Get(i, "last_topic"); + ci->last_topic_setter = r.Get(i, "last_topic_setter"); + ci->last_topic_time = r.Get(i, "last_topic_time").is_number_only() ? convertTo(r.Get(i, "last_topic_time")) : time(NULL); + ci->forbidby = r.Get(i, "forbidby"); + ci->forbidreason = r.Get(i, "forbidreason"); + ci->bantype = r.Get(i, "bantype").is_number_only() ? convertTo(r.Get(i, "bantype")) : 2; + ci->entry_message = r.Get(i, "entry_message"); + ci->memos.memomax = r.Get(i, "memomax").is_number_only() ? convertTo(r.Get(i, "memomax")) : 20; + ci->capsmin = r.Get(i, "capsmin").is_number_only() ? convertTo(r.Get(i, "capsmin")) : 0; + ci->capspercent = r.Get(i, "capspercent").is_number_only() ? convertTo(r.Get(i, "capspercent")) : 0; + ci->floodlines = r.Get(i, "floodlines").is_number_only() ? convertTo(r.Get(i, "floodlines")) : 0; + ci->floodsecs = r.Get(i, "floodsecs").is_number_only() ? convertTo(r.Get(i, "floodsecs")) : 0; + ci->repeattimes = r.Get(i, "repeattimes").is_number_only() ? convertTo(r.Get(i, "repeattimes")) : 0; + ci->bi = findbot(r.Get(i, "botnick")); + if (ci->bi && !r.Get(i, "botflags").empty()) + { + spacesepstream sep(r.Get(i, "botflags")); + Anope::string buf; + while (sep.GetToken(buf)) + { + for (int j = 0; BotFlags[j].Flag != -1; ++j) + { + if (buf == BotFlags[j].Name) + { + ci->botflags.SetFlag(BotFlags[j].Flag); + break; + } + } + } + } + + if (!r.Get(i, "mlock_on").empty()) + { + std::vector modes; + + spacesepstream sep(r.Get(i, "mlock_on")); + Anope::string buf; + while (sep.GetToken(buf)) + modes.push_back(buf); + + ci->Extend("db_mlock_modes_on", new ExtensibleItemRegular >(modes)); + } + if (!r.Get(i, "mlock_off").empty()) + { + std::vector modes; + + spacesepstream sep(r.Get(i, "mlock_off")); + Anope::string buf; + while (sep.GetToken(buf)) + modes.push_back(buf); + + ci->Extend("db_mlock_modes_off", new ExtensibleItemRegular >(modes)); + } + if (!r.Get(i, "mlock_params").empty()) + { + std::vector > mlp; + + spacesepstream sep(r.Get(i, "mlock_params")); + Anope::string buf, buf2; + while (sep.GetToken(buf) && sep.GetToken(buf2)) + mlp.push_back(std::make_pair(Anope::string(buf), Anope::string(buf2))); + + ci->Extend("db_mlp", new ExtensibleItemRegular > >(mlp)); + } + + if (!r.Get(i, "flags").empty()) + { + spacesepstream sep(r.Get(i, "flags")); + Anope::string buf; + + while (sep.GetToken(buf)) + { + for (int j = 0; ChannelFlags[j].Flag != -1; ++j) + { + if (buf == ChannelFlags[j].Name) + { + ci->SetFlag(ChannelFlags[j].Flag); + break; + } + } + } + } + } + } + + r = SQL->RunQuery("SELECT * FROM `anope_cs_ttb`"); + for (int i = 0; i < r.Rows(); ++i) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "channel")); + if (!ci) + { + Alog() << "MySQL: Channel ttb for nonexistant channel " << r.Get(i, "channel"); + continue; + } + + ci->ttb[atoi(r.Get(i, "ttb_id").c_str())] = atoi(r.Get(i, "value").c_str()); + } + + r = SQL->RunQuery("SELECT * FROM `anope_bs_badwords`"); + for (int i = 0; i < r.Rows(); ++i) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "channel")); + if (!ci) + { + Alog() << "MySQL: Channel badwords entry for nonexistant channel " << r.Get(i, "channel"); + continue; + } + + BadWordType BWTYPE = BW_ANY; + if (r.Get(i, "type").equals_cs("SINGLE")) + BWTYPE = BW_SINGLE; + else if (r.Get(i, "type").equals_cs("START")) + BWTYPE = BW_START; + else if (r.Get(i, "type").equals_cs("END")) + BWTYPE = BW_END; + ci->AddBadWord(r.Get(i, "word"), BWTYPE); + } + + r = SQL->RunQuery("SELECT * FROM `anope_cs_access`"); + for (int i = 0; i < r.Rows(); ++i) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "channel")); + if (!ci) + { + Alog() << "MySQL: Channel access entry for nonexistant channel " << r.Get(i, "channel"); + continue; + } + + NickCore *nc = findcore(r.Get(i, "display")); + if (!nc) + { + Alog() << "MySQL: Channel access entry for " << ci->name << " with nonexistant nick " << r.Get(i, "display"); + continue; + } + + ci->AddAccess(nc, atoi(r.Get(i, "level").c_str()), r.Get(i, "creator"), (r.Get(i, "last_seen").is_number_only() ? convertTo(r.Get(i, "last_seen")) : time(NULL))); + } + + r = SQL->RunQuery("SELECT * FROM `anope_cs_akick`"); + for (int i = 0; i < r.Rows(); ++i) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "channel")); + if (!ci) + { + Alog() << "MySQL: Channel access entry for nonexistant channel " << r.Get(i, "channel"); + continue; + } + + NickCore *nc = NULL; + spacesepstream sep(r.Get(i, "flags")); + Anope::string flag, mask; + bool stuck = false; + while (sep.GetToken(flag)) + { + if (flag.equals_cs("ISNICK")) + nc = findcore(r.Get(i, "mask")); + else if (flag.equals_cs("STUCK")) + stuck = true; + + AutoKick *ak; + if (nc) + ak = ci->AddAkick(r.Get(i, "creator"), nc, r.Get(i, "reason"), atol(r.Get(i, "created").c_str()), atol(r.Get(i, "last_used").c_str())); + else + ak = ci->AddAkick(r.Get(i, "creator"), r.Get(i, "mask"), r.Get(i, "reason"), atol(r.Get(i, "created").c_str()), atol(r.Get(i, "last_used").c_str())); + if (stuck) + ak->SetFlag(AK_STUCK); + if (nc) + ak->SetFlag(AK_ISNICK); + } + } + + r = SQL->RunQuery("SELECT * FROM `anope_cs_levels`"); + for (int i = 0; i < r.Rows(); ++i) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "channel")); + if (!ci) + { + Alog() << "MySQL: Channel level entry for nonexistant channel " << r.Get(i, "channel"); + continue; + } + + ci->levels[atoi(r.Get(i, "position").c_str())] = atoi(r.Get(i, "level").c_str()); + } + + r = SQL->RunQuery("SELECT * FROM `anope_cs_info_metadata"); + for (int i = 0; i < r.Rows(); ++i) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "channel")); + if (!ci) + { + Alog() << "MySQL: Channel metadata for nonexistant channel " << r.Get(i, "channel"); + continue; + } + + EventReturn MOD_RESULT; + std::vector Params = MakeVector(r.Get(i, "value")); + FOREACH_RESULT(I_OnDatabaseReadMetadata, OnDatabaseReadMetadata(ci, ci->name, Params)); + } + + r = SQL->RunQuery("SELECT * FROM `anope_ns_request`"); + for (int i = 0; i < r.Rows(); ++i) + { + NickRequest *nr = new NickRequest(r.Get(i, "nick")); + nr->password = r.Get(i, "passcode"); + nr->password = r.Get(i, "password"); + nr->email = r.Get(i, "email"); + nr->requested = r.Get(i, "requested").is_number_only() ? convertTo(r.Get(i, "requested")) : time(NULL); + } + + r = SQL->RunQuery("SELECT * FROM `anope_ms_info`"); + for (int i = 0; i < r.Rows(); ++i) + { + MemoInfo *mi = NULL; + if (r.Get(i, "serv").equals_cs("NICK")) + { + NickCore *nc = findcore(r.Get(i, "receiver")); + if (nc) + mi = &nc->memos; + } + else if (r.Get(i, "serv").equals_cs("CHAN")) + { + ChannelInfo *ci = cs_findchan(r.Get(i, "receiver")); + if (ci) + mi = &ci->memos; + } + if (mi) + { + Memo *m = new Memo(); + mi->memos.push_back(m); + m->sender = r.Get(i, "sender"); + if (mi->memos.size() > 1) + { + m->number = mi->memos[mi->memos.size() - 1]->number + 1; + if (m->number < 1) + { + for (unsigned j = 0; j < mi->memos.size(); ++j) + mi->memos[j]->number = j + 1; + } + } + else + m->number = 1; + m->time = r.Get(i, "time").is_number_only() ? convertTo(r.Get(i, "time")) : time(NULL); + m->text = r.Get(i, "text"); + + if (!r.Get(i, "flags").empty()) + { + spacesepstream sep(r.Get(i, "flags")); + Anope::string buf; + while (sep.GetToken(buf)) + { + for (unsigned j = 0; MemoFlags[j].Flag != -1; ++j) + { + if (MemoFlags[j].Name == buf) + { + m->SetFlag(MemoFlags[j].Flag); + break; + } + } + } + } + } + } + + if (SQLine) + { + r = SQL->RunQuery("SELECT * FROM `anope_os_akills`"); + for (int i = 0; i < r.Rows(); ++i) + { + Anope::string user = r.Get(i, "user"); + Anope::string host = r.Get(i, "host"); + Anope::string by = r.Get(i, "by"); + Anope::string reason = r.Get(i, "reason"); + time_t seton = r.Get(i, "seton").is_number_only() ? convertTo(r.Get(i, "seton")) : time(NULL); + time_t expires = r.Get(i, "expires").is_number_only() ? convertTo(r.Get(i, "expires")) : time(NULL); + + XLine *x = SGLine->Add(NULL, NULL, user + "@" + host, expires, reason); + if (x) + { + x->By = by; + x->Created = seton; + } + } + } + + r = SQL->RunQuery("SELECT * FROM `anope_os_xlines`"); + for (int i = 0; i < r.Rows(); ++i) + { + Anope::string mask = r.Get(i, "mask"); + Anope::string by = r.Get(i, "xby"); + Anope::string reason = r.Get(i, "reason"); + time_t seton = r.Get(i, "seton").is_number_only() ? convertTo(r.Get(i, "seton")) : time(NULL); + time_t expires = r.Get(i, "expires").is_number_only() ? convertTo(r.Get(i, "expires")) : time(NULL); + + XLine *x = NULL; + if (SNLine && r.Get(i, "type").equals_cs("SNLINE")) + SNLine->Add(NULL, NULL, mask, expires, reason); + else if (SQLine && r.Get(i, "type").equals_cs("SQLINE")) + SQLine->Add(NULL, NULL, mask, expires, reason); + else if (SZLine && r.Get(i, "type").equals_cs("SZLINE")) + SZLine->Add(NULL, NULL, mask, expires, reason); + if (x) + { + x->By = by; + x->Created = seton; + } + } + + r = SQL->RunQuery("SELECT * FROM `anope_extra`"); + for (int i = 0; i < r.Rows(); ++i) + { + std::vector params = MakeVector(r.Get(i, "data")); + EventReturn MOD_RESULT; + FOREACH_RESULT(I_OnDatabaseRead, OnDatabaseRead(params)); + } + + return EVENT_STOP; + } + + EventReturn OnSaveDatabase() + { + this->RunQuery("TRUNCATE TABLE `anope_os_core`"); + + this->RunQuery("INSERT DELAYED INTO `anope_os_core` (maxusercnt, maxusertime, akills_count, sglines_count, sqlines_count, szlines_count) VALUES( " + stringify(maxusercnt) + ", " + stringify(maxusertime) + ", " + stringify(SGLine ? SGLine->GetCount() : 0) + ", " + stringify(SQLine ? SQLine->GetCount() : 0) + ", " + stringify(SNLine ? SNLine->GetCount() : 0) + ", " + stringify(SZLine ? SZLine->GetCount() : 0) + ")"); + for (nickcore_map::const_iterator it = NickCoreList.begin(), it_end = NickCoreList.end(); it != it_end; ++it) + { + CurCore = it->second; + FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteCoreMetadata, CurCore)); + } + + for (nickalias_map::const_iterator it = NickAliasList.begin(), it_end = NickAliasList.end(); it != it_end; ++it) + { + CurNick = it->second; + FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteNickMetadata, CurNick)); + } + + for (registered_channel_map::const_iterator it = RegisteredChannelList.begin(), it_end = RegisteredChannelList.end(); it != it_end; ++it) + { + CurChannel = it->second; + FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteChannelMetadata, CurChannel)); + } + + for (botinfo_map::const_iterator it = BotListByNick.begin(), it_end = BotListByNick.end(); it != it_end; ++it) + { + CurBot = it->second; + FOREACH_MOD(I_OnDatabaseWriteMetadata, OnDatabaseWriteMetadata(WriteBotMetadata, CurBot)); + + /* This is for the core bots, bots added by users are already handled by an event */ + this->RunQuery("INSERT DELAYED INTO `anope_bs_core` (nick, user, host, rname, flags, created, chancount) VALUES('" + this->Escape(CurBot->nick) + "', '" + this->Escape(CurBot->GetIdent()) + "', '" + this->Escape(CurBot->host) + "', '" + this->Escape(CurBot->realname) + "', '" + GetBotServFlags(CurBot) + "', " + CurBot->created + ", " + CurBot->chancount + ") ON DUPLICATE KEY UPDATE nick=VALUES(nick), user=VALUES(user), host=VALUES(host), rname=VALUES(rname), flags=VALUES(flags), created=VALUES(created), chancount=VALUES(created)"); + } + + FOREACH_MOD(I_OnDatabaseWrite, OnDatabaseWrite(Write)); + + return EVENT_CONTINUE; + } + + void OnPostCommand(User *u, const Anope::string &service, const Anope::string &command, const std::vector ¶ms) + { + if (service == Config.s_NickServ) + { + if (u->Account() && ((command.equals_ci("SET") && !params.empty()) || (command.equals_ci("SASET") && u->Account()->HasCommand("nickserv/saset") && params.size() > 1))) + { + Anope::string cmd = (command == "SET" ? params[0] : params[1]); + NickCore *nc = (command == "SET" ? u->Account() : findcore(params[0])); + if (!nc) + return; + if (cmd.equals_ci("PASSWORD") && params.size() > 1) + { + this->RunQuery("UPDATE `anope_ns_core` SET `pass` = '" + this->Escape(nc->pass) + "' WHERE `display` = '" + this->Escape(nc->display) + "'"); + } + else if (cmd.equals_ci("LANGUAGE") && params.size() > 1) + { + this->RunQuery("UPDATE `anope_ns_core` SET `language` = '" + this->Escape(nc->language) + "' WHERE `display` = '" + this->Escape(nc->display) + "'"); + } + else if (cmd.equals_ci("EMAIL")) + { + this->RunQuery("UPDATE `anope_ns_core` SET `email` = '" + this->Escape(nc->email) + "' WHERE `display` = '" + this->Escape(nc->display) + "'"); + } + else if (cmd.equals_ci("GREET")) + { + this->RunQuery("UPDATE `anope_ns_core` SET `greet` = '" + this->Escape(nc->greet) + " WHERE `display` = '" + this->Escape(nc->display) + "'"); + } + else if (cmd.equals_ci("KILL") || cmd.equals_ci("SECURE") || cmd.equals_ci("PRIVATE") || cmd.equals_ci("MSG") || cmd.equals_ci("HIDE") || cmd.equals_ci("AUTOOP")) + { + this->RunQuery("UPDATE `anope_ns_core` SET `flags` = '" + BuildFlagsList(nc) + "' WHERE `display` = '" + this->Escape(nc->display) + "'"); + } + } + } + else if (service == Config.s_ChanServ) + { + if (command.equals_ci("SET") && u->Account() && params.size() > 1) + { + ChannelInfo *ci = cs_findchan(params[0]); + if (!ci) + return; + if (!u->Account()->HasPriv("chanserv/set") && check_access(u, ci, CA_SET)) + return; + if (params[1].equals_ci("FOUNDER") && ci->founder) + { + this->RunQuery("UPDATE `anope_cs_info` SET `founder` = '" + this->Escape(ci->founder->display) + "' WHERE `name` = '" + this->Escape(ci->name) + "'"); + } + else if (params[1].equals_ci("SUCCESSOR")) + { + this->RunQuery("UPDATE `anope_cs_info` SET `successor` = '" + this->Escape(ci->successor ? ci->successor->display : "") + "' WHERE `name` = '" + this->Escape(ci->name) + "'"); + } + else if (params[1].equals_ci("DESC")) + { + this->RunQuery("UPDATE `anope_cs_info` SET `descr` = '" + this->Escape(ci->desc) + "' WHERE `name` = '" + this->Escape(ci->name) + "'"); + } + else if (params[1].equals_ci("ENTRYMSG")) + { + this->RunQuery("UPDATE `anope_cs_info` SET `entry_message` = '" + this->Escape(ci->entry_message) + "' WHERE `name` = '" + this->Escape(ci->name) + "'"); + } + else if (params[1].equals_ci("MLOCK")) + { + this->RunQuery("UPDATE `anope_cs_info` SET `mlock_on` = '" + GetMLockOn(ci) + "' WHERE `name` = " + this->Escape(ci->name) + "'"); + this->RunQuery("UPDATE `anope_cs_info` SET `mlock_off` = '" + GetMLockOff(ci) + "' WHERE `name` = " + this->Escape(ci->name) + "'"); + this->RunQuery("UPDATE `anope_cs_info` SET `mlock_params` = '" + GetMLockParams(ci) + "' WHERE `name` = " + this->Escape(ci->name) + "'"); + } + else if (params[1].equals_ci("BANTYPE")) + { + this->RunQuery("UPDATE `anope_cs_info` SET `bantype` = " + stringify(ci->bantype) + " WHERE `name` = '" + this->Escape(ci->name) + "'"); + } + else if (params[1].equals_ci("KEEPTOPIC") || params[1].equals_ci("TOPICLOCK") || params[1].equals_ci("PRIVATE") || params[1].equals_ci("SECUREOPS") || params[1].equals_ci("SECUREFOUNDER") || params[1].equals_ci("RESTRICTED") || params[1].equals_ci("S