diff options
| -rw-r--r-- | Changes | 1 | ||||
| -rw-r--r-- | Changes.mysql | 18 | ||||
| -rw-r--r-- | data/tables.sql | 30 | ||||
| -rw-r--r-- | include/extern.h | 8 | ||||
| -rw-r--r-- | include/services.h | 3 | ||||
| -rw-r--r-- | src/botserv.c | 5 | ||||
| -rw-r--r-- | src/chanserv.c | 46 | ||||
| -rw-r--r-- | src/hostserv.c | 5 | ||||
| -rw-r--r-- | src/mysql.c | 2109 | ||||
| -rw-r--r-- | src/news.c | 7 | ||||
| -rw-r--r-- | src/nickserv.c | 14 | ||||
| -rw-r--r-- | src/operserv.c | 14 | ||||
| -rw-r--r-- | src/rdb.c | 202 | ||||
| -rw-r--r-- | src/sessions.c | 3 | ||||
| -rw-r--r-- | version.log | 6 |
15 files changed, 1309 insertions, 1162 deletions
@@ -6,6 +6,7 @@ Anope Version S V N 10/21 F svspart sends svspart instead of eob. [#617] 11/02 F hs_request no longer returns MOD_STOP. [ #00] 11/02 F anoperc sends correct signalls. [#623] +12/09 F Reviewed and updated all of the MySQL code. [ #00] Anope Version 1.7.17 -------------------- diff --git a/Changes.mysql b/Changes.mysql index dba26af58..b14a801b9 100644 --- a/Changes.mysql +++ b/Changes.mysql @@ -1,6 +1,22 @@ Anope Version S V N -------------------- -- NONE +- DROP TABLE IF EXISTS `anope_os_status` +- ALTER TABLE `anope_bs_core` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_cs_access` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_cs_akicks` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_cs_badwords` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_cs_levels` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_cs_ttb` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_hs_core` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_ms_info` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_ns_access` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_ns_request` CHANGE `active` `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_os_akills` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_os_exceptions` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_os_news` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_os_sglines` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_os_sqlines` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL +- ALTER TABLE `anope_os_szlines` ADD `active` TINYINT( 1 ) DEFAULT '1' NOT NULL Anope Version 1.7.17 -------------------- diff --git a/data/tables.sql b/data/tables.sql index ed8b2b60c..546c95287 100644 --- a/data/tables.sql +++ b/data/tables.sql @@ -27,6 +27,7 @@ CREATE TABLE anope_bs_core ( flags int(11) NOT NULL default '0', created int(11) NOT NULL default '0', chancount int(11) NOT NULL default '0', + active int(1) NOT NULL default '1', PRIMARY KEY (bs_id), UNIQUE KEY nick (nick), KEY nick_index (nick(10)) @@ -44,6 +45,7 @@ CREATE TABLE anope_cs_access ( display varchar(255) NOT NULL default '', channel varchar(255) NOT NULL default '', last_seen int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (ca_id) ) TYPE=MyISAM; @@ -60,6 +62,7 @@ CREATE TABLE anope_cs_akicks ( reason text NOT NULL, creator text NOT NULL, addtime int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (ck_id) ) TYPE=MyISAM; @@ -73,6 +76,7 @@ CREATE TABLE anope_cs_badwords ( channel varchar(255) NOT NULL default '', word text NOT NULL, type int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (cw_id) ) TYPE=MyISAM; @@ -133,6 +137,7 @@ 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', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (cl_id) ) TYPE=MyISAM; @@ -145,6 +150,7 @@ 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', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (ct_id) ) TYPE=MyISAM; @@ -160,6 +166,7 @@ CREATE TABLE anope_hs_core ( vhost text NOT NULL, creator text NOT NULL, time int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (bs_id), UNIQUE KEY nick (nick), KEY nick_index (nick(10)) @@ -179,6 +186,7 @@ CREATE TABLE anope_ms_info ( sender text NOT NULL, text blob NOT NULL, serv enum('NICK','CHAN') NOT NULL default 'NICK', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (nm_id) ) TYPE=MyISAM; @@ -191,6 +199,7 @@ CREATE TABLE anope_ns_access ( na_id int(11) NOT NULL auto_increment, display varchar(255) NOT NULL default '', access text NOT NULL, + active tinyint(1) NOT NULL default '1', PRIMARY KEY (na_id) ) TYPE=MyISAM; @@ -253,7 +262,7 @@ CREATE TABLE anope_ns_request ( password tinyblob NOT NULL, email text NOT NULL, requested int(11) NOT NULL default '0', - active int(1) NOT NULL default '1', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (nr_id), UNIQUE KEY nick (nick), KEY nick_index (nick(10)) @@ -272,6 +281,7 @@ CREATE TABLE anope_os_akills ( reason text NOT NULL, seton int(11) NOT NULL default '0', expire int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (ok_id) ) TYPE=MyISAM; @@ -304,6 +314,7 @@ CREATE TABLE anope_os_exceptions ( reason text NOT NULL, time int(11) NOT NULL default '0', expires int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (oe_id) ) TYPE=MyISAM; @@ -319,6 +330,7 @@ CREATE TABLE anope_os_news ( ntext text NOT NULL, who text NOT NULL, time int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (on_id) ) TYPE=MyISAM; @@ -334,6 +346,7 @@ CREATE TABLE anope_os_sglines ( reason text NOT NULL, seton int(11) NOT NULL default '0', expire int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (og_id) ) TYPE=MyISAM; @@ -349,23 +362,11 @@ CREATE TABLE anope_os_sqlines ( reason text NOT NULL, seton int(11) NOT NULL default '0', expire int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (og_id) ) TYPE=MyISAM; -- --- Table structure for table `anope_os_status` --- - -DROP TABLE IF EXISTS anope_os_status; -CREATE TABLE anope_os_status ( - os_id int(11) NOT NULL auto_increment, - host text NOT NULL, - status int(11) NOT NULL default '0', - used int(11) NOT NULL default '0', - PRIMARY KEY (os_id) -) TYPE=MyISAM; - --- -- Table structure for table `anope_os_szlines` -- @@ -377,5 +378,6 @@ CREATE TABLE anope_os_szlines ( reason text NOT NULL, seton int(11) NOT NULL default '0', expire int(11) NOT NULL default '0', + active tinyint(1) NOT NULL default '1', PRIMARY KEY (og_id) ) TYPE=MyISAM; diff --git a/include/extern.h b/include/extern.h index 873a4146e..a064e3678 100644 --- a/include/extern.h +++ b/include/extern.h @@ -495,13 +495,13 @@ E int rdb_open(); E int rdb_close(); E char *rdb_quote(char *str); E int rdb_tag_table(char *table); -E int rdb_clear_table(char *table); +E int rdb_tag_table_where(char *table, char *clause); +E int rdb_empty_table(char *table); +E int rdb_clean_table(char *table); +E int rdb_clean_table_where(char *table, char *clause); E int rdb_scrub_table(char *table, char *clause); E int rdb_direct_query(char *query); E int rdb_ns_set_display(char *newnick, char *oldnick); -E int rdb_cs_set_founder(char *channel, char *founder); -E int rdb_cs_deluser(char *nick); -E int rdb_cs_delchan(ChannelInfo * ci); E void rdb_save_ns_core(NickCore * nc); E void rdb_save_ns_alias(NickAlias * na); E void rdb_save_ns_req(NickRequest * nr); diff --git a/include/services.h b/include/services.h index 9e83ca158..2d7c36d83 100644 --- a/include/services.h +++ b/include/services.h @@ -467,6 +467,9 @@ struct memo_ { char sender[NICKMAX]; char *text; ModuleData *moduleData; /* Module saved data attached to the Memo */ +#ifdef USE_MYSQL + uint32 id; /* Database ID; see mysql.c */ +#endif }; typedef struct { diff --git a/src/botserv.c b/src/botserv.c index c73b6c7f9..2a7ea4f60 100644 --- a/src/botserv.c +++ b/src/botserv.c @@ -540,13 +540,16 @@ void save_bs_rdb_dbase(void) if (!rdb_open()) return; - rdb_clear_table("anope_bs_core"); + rdb_tag_table("anope_bs_core"); for (i = 0; i < 256; i++) { for (bi = botlists[i]; bi; bi = bi->next) { rdb_save_bs_core(bi); } } + + rdb_clean_table("anope_bs_core"); + rdb_close(); #endif } diff --git a/src/chanserv.c b/src/chanserv.c index 908cb1e69..32c8a33cf 100644 --- a/src/chanserv.c +++ b/src/chanserv.c @@ -999,12 +999,12 @@ void save_cs_rdb_dbase(void) return; rdb_tag_table("anope_cs_info"); - rdb_scrub_table("anope_ms_info", "serv='CHAN'"); - rdb_clear_table("anope_cs_access"); - rdb_clear_table("anope_cs_levels"); - rdb_clear_table("anope_cs_akicks"); - rdb_clear_table("anope_cs_badwords"); - rdb_clear_table("anope_cs_ttb"); + rdb_tag_table("anope_cs_access"); + rdb_tag_table("anope_cs_levels"); + rdb_tag_table("anope_cs_akicks"); + rdb_tag_table("anope_cs_badwords"); + rdb_tag_table("anope_cs_ttb"); + rdb_tag_table_where("anope_ms_info", "serv='CHAN'"); for (i = 0; i < 256; i++) { for (ci = chanlists[i]; ci; ci = ci->next) { @@ -1012,7 +1012,14 @@ void save_cs_rdb_dbase(void) } /* for (chanlists[i]) */ } /* for (i) */ - rdb_scrub_table("anope_cs_info", "active='0'"); + rdb_clean_table("anope_cs_info"); + rdb_clean_table("anope_cs_access"); + rdb_clean_table("anope_cs_levels"); + rdb_clean_table("anope_cs_akicks"); + rdb_clean_table("anope_cs_badwords"); + rdb_clean_table("anope_cs_ttb"); + rdb_clean_table_where("anope_ms_info", "serv='CHAN'"); + rdb_close(); #endif } @@ -1712,12 +1719,6 @@ void cs_remove_nick(const NickCore * nc) ci->founder = nc2; ci->successor = NULL; nc2->channelcount++; -#ifdef USE_RDB - if (rdb_open()) { - rdb_cs_set_founder(ci->name, nc2->display); - rdb_close(); - } -#endif } } else { alog("%s: Deleting channel %s owned by deleted nick %s", s_ChanServ, ci->name, nc->display); @@ -1762,12 +1763,6 @@ void cs_remove_nick(const NickCore * nc) } } } -#ifdef USE_RDB - if (rdb_open()) { - rdb_cs_deluser(nc->display); - rdb_close(); - } -#endif } /*************************************************************************/ @@ -1942,18 +1937,7 @@ int delchan(ChannelInfo * ci) if (debug >= 2) { alog("debug: delchan() Bot has been removed moving on"); } -#ifdef USE_RDB - if (debug >= 2) { - alog("debug: delchan() rdb updating"); - } - if (rdb_open()) { - rdb_cs_delchan(ci); - rdb_close(); - } - if (debug >= 2) { - alog("debug: delchan() rdb done"); - } -#endif + if (debug >= 2) { alog("debug: delchan() founder cleanup"); } diff --git a/src/hostserv.c b/src/hostserv.c index d8c8af96b..365a5d091 100644 --- a/src/hostserv.c +++ b/src/hostserv.c @@ -506,13 +506,16 @@ void save_hs_rdb_dbase(void) if (!rdb_open()) return; - rdb_clear_table("anope_hs_core"); + rdb_tag_table("anope_hs_core"); current = head; while (current != NULL) { rdb_save_hs_core(current); current = current->next; } + + rdb_clean_table("anope_hs_core"); + rdb_close(); #endif } diff --git a/src/mysql.c b/src/mysql.c index a7f84b06e..19db1e419 100644 --- a/src/mysql.c +++ b/src/mysql.c @@ -22,10 +22,13 @@ MYSQL_RES *mysql_res; /* MySQL Result */ MYSQL_FIELD *mysql_fields; /* MySQL Fields */ MYSQL_ROW mysql_row; /* MySQL Row */ -int mysql_is_connected = 0; /* Are we currently connected? */ +int mysql_is_connected = 0; /* Are we currently connected? */ /*************************************************************************/ +/* Throw a mysql error into the logs. If severity is MYSQL_ERROR, we + * also exit Anope... + */ void db_mysql_error(int severity, char *msg) { static char buf[512]; @@ -50,6 +53,7 @@ void db_mysql_error(int severity, char *msg) /*************************************************************************/ +/* Initialize the MySQL code */ int db_mysql_init() { @@ -84,19 +88,25 @@ int db_mysql_init() /*************************************************************************/ +/* Open a connection to the mysql database. Return 0 on failure, or + * 0 on success. If this succeeds, we're guaranteed of a working + * mysql connection (unless something unexpected happens ofcourse...) + */ int db_mysql_open() { /* If MySQL is disabled, return 0 */ if (!do_mysql) return 0; - - /* If we're already connected, return 1 */ - if (mysql_is_connected) - return 1; - + + /* If we're already connected, return 1 */ + if (mysql_is_connected) + return 1; + mysql = mysql_init(NULL); - if (mysql == NULL) + if (mysql == NULL) { db_mysql_error(MYSQL_WARNING, "Unable to create mysql object"); + return 0; + } if (!MysqlPort) MysqlPort = MYSQL_DEFAULT_PORT; @@ -118,15 +128,19 @@ int db_mysql_open() return 0; } } - - mysql_is_connected = 1; - + + mysql_is_connected = 1; + return 1; } /*************************************************************************/ +/* Perform a MySQL query. Return 0 if the query succeeded, or something + * else, like 1, if it failed. Before returning failure, re-try the + * query a few times and die if it fails after a few tries. + */ int db_mysql_query(char *sql) { int lcv; @@ -137,14 +151,14 @@ int db_mysql_query(char *sql) } if (debug) { - alog("debug: MySQL: %s", sql); + alog("debug: MySQL: %s", sql); } /* Try as many times as configured in MysqlRetries */ for (lcv = 0; lcv < MysqlRetries; lcv++) { if (db_mysql_open() && (!mysql_query(mysql, sql))) { - + /* Success is 0 */ return 0; @@ -168,15 +182,17 @@ int db_mysql_query(char *sql) /*************************************************************************/ +/* Quote a string to be safely included in a query. The result of this + * function is allocated; it MUST be freed by the caller. + */ char *db_mysql_quote(char *sql) { int slen; char *quoted; - if (!sql) { + if (!sql) return sstrdup(""); - } slen = strlen(sql); quoted = malloc((1 + (slen * 2)) * sizeof(char)); @@ -188,49 +204,40 @@ char *db_mysql_quote(char *sql) /*************************************************************************/ -/* I don't like using res here, maybe we can pass it as a param? */ +/* Close the MySQL database connection. */ int db_mysql_close() { - if (mysql_res) - mysql_free_result(mysql_res); mysql_close(mysql); - - mysql_is_connected = 0; - + + mysql_is_connected = 0; + return 1; } /*************************************************************************/ -/* - * NickServ Specific Secion - */ - -/*************************************************************************/ -void db_mysql_save_ns_req(NickRequest * nr) +/* Try to execute a query and issue a warning when failed */ +void db_mysql_try(const char *fmt, ...) { - char *qnick, *qpasscode, *qpassword, *qemail; - char sqlcmd[MAX_SQL_BUF]; - - qnick = db_mysql_quote(nr->nick); - qpasscode = db_mysql_quote(nr->passcode); - qpassword = db_mysql_quote(nr->password); - qemail = db_mysql_quote(nr->email); - - snprintf(sqlcmd, MAX_SQL_BUF, - "REPLACE anope_ns_request (nick,passcode,password,email,requested,active)" - " VALUES ('%s','%s','%s','%s','%d','1')", - qnick, qpasscode, qpassword, qemail, (int) nr->requested); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); + va_list args; + static char sql[MAX_SQL_BUF]; + + va_start(args, fmt); + vsnprintf(sql, MAX_SQL_BUF, fmt, args); + va_end(args); + + if (db_mysql_query(sql)) { + log_perror("Can't create sql query: %s", sql); + db_mysql_error(MYSQL_ERROR, "query"); } - free(qnick); - free(qpasscode); - free(qpassword); - free(qemail); } +/*************************************************************************/ + +/* Return a string to insert into a SQL query. The string will, once + * evaluated by MySQL, result in a the given pass encoded in the encryption + * type selected for MysqlSecure + */ char *db_mysql_secure(char *pass) { char epass[BUFSIZE]; @@ -238,12 +245,12 @@ char *db_mysql_secure(char *pass) /* Initialize the buffer. Bug #86 */ memset(epass, '\0', BUFSIZE); - memset(tmp_pass, 0, PASSMAX); + memset(tmp_pass, '\0', PASSMAX); /* We couldnt decrypt the pass... */ - if(enc_decrypt(pass,tmp_pass,PASSMAX)!=1) { + if (enc_decrypt(pass, tmp_pass, PASSMAX) != 1) { snprintf(epass, sizeof(epass), "'%s'", pass); - } else { /* if we could decrypt the pass */ + } else { /* if we could decrypt the pass */ if (!pass) { snprintf(epass, sizeof(epass), "''"); } else if ((!MysqlSecure) || (strcmp(MysqlSecure, "") == 0)) { @@ -257,138 +264,199 @@ char *db_mysql_secure(char *pass) } else { snprintf(epass, sizeof(epass), "ENCODE('%s','%s')", tmp_pass, MysqlSecure); - } + } } return sstrdup(epass); } /*************************************************************************/ + +/* + * NickServ Specific Secion + */ + +/*************************************************************************/ + +/* Save the given NickRequest into the database + * These tables are tagged and will be cleaned: + * - anope_ns_request + */ +void db_mysql_save_ns_req(NickRequest * nr) +{ + char *q_nick, *q_passcode, *q_password, *q_email; + + q_nick = db_mysql_quote(nr->nick); + q_passcode = db_mysql_quote(nr->passcode); + q_password = db_mysql_quote(nr->password); + q_email = db_mysql_quote(nr->email); + + db_mysql_try + ("UPDATE anope_ns_request SET passcode = '%s', password = '%s', " + "email = '%s', requested = %d, active = 1 WHERE nick = '%s'", + q_passcode, q_password, q_email, (int) nr->requested, q_nick); + + if (mysql_affected_rows(mysql) == 0) { + db_mysql_try + ("INSERT DELAYED INTO anope_ns_request (nick, passcode, password, email, " + "requested, active) VALUES ('%s', '%s', '%s', '%s', %d, 1)", + q_nick, q_passcode, q_password, q_email, (int) nr->requested); + } + + free(q_nick); + free(q_passcode); + free(q_password); + free(q_email); +} + +/*************************************************************************/ + +/* Save the given NickCore into the database + * Also save the access list and memo's for this user + * These tables are tagged and will be cleaned: + * - anope_ns_core + * - anope_ns_alias + * - anope_ns_access + * - anope_ms_info (serv='NICK') + */ void db_mysql_save_ns_core(NickCore * nc) { - char sqlcmd[MAX_SQL_BUF]; - int j; + int i; char **access; Memo *memos; - char *cnick, *cpass, *epass, *cemail, *cgreet, *curl, *caccess, - *msender, *mtext; + char *q_display, *q_pass, *epass, *q_email, *q_greet, *q_url, + *q_access, *q_sender, *q_text; - cnick = db_mysql_quote(nc->display); - cpass = db_mysql_quote(nc->pass); - cemail = db_mysql_quote(nc->email); - cgreet = db_mysql_quote(nc->greet); - curl = db_mysql_quote(nc->url); + q_display = db_mysql_quote(nc->display); + q_pass = db_mysql_quote(nc->pass); + q_email = db_mysql_quote(nc->email); + q_greet = db_mysql_quote(nc->greet); + q_url = db_mysql_quote(nc->url); - epass = db_mysql_secure(cpass); - free(cpass); + epass = db_mysql_secure(q_pass); + free(q_pass); /* Let's take care of the core itself */ /* Update the existing records */ - snprintf(sqlcmd, MAX_SQL_BUF, - "UPDATE anope_ns_core SET pass=%s,email='%s',greet='%s',icq='%d',url='%s',flags='%d'," - "language='%d',accesscount='%d',memocount='%d',memomax='%d',channelcount='%d'" - ",channelmax='%d',active='1' WHERE display='%s'", - epass, cemail, cgreet, nc->icq, curl, nc->flags, - nc->language, nc->accesscount, nc->memos.memocount, - nc->memos.memomax, nc->channelcount, nc->channelmax, cnick); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); - } + db_mysql_try + ("UPDATE anope_ns_core SET pass = %s, email = '%s', greet = '%s', " + "icq = %d, url = '%s', flags = %d, language = %d, accesscount = %d, " + "memocount = %d, memomax = %d, channelcount = %d, channelmax = %d, " + "active = 1 WHERE display = '%s'", epass, q_email, q_greet, + nc->icq, q_url, nc->flags, nc->language, nc->accesscount, + nc->memos.memocount, nc->memos.memomax, nc->channelcount, + nc->channelmax, q_display); - /* need to write a wrapper for mysql_affected_rows */ /* Our previous UPDATE affected no rows, therefore this is a new record */ - if ((int) mysql_affected_rows(mysql) <= 0) { - - /* Let's take care of the core itself */ - snprintf(sqlcmd, MAX_SQL_BUF, - "INSERT DELAYED INTO anope_ns_core (display,pass,email,greet,icq,url,flags," - "language,accesscount,memocount,memomax,channelcount,channelmax,active)" - " VALUES ('%s',%s,'%s','%s','%d','%s','%d','%d','%d','%d','%d','%d','%d','1')", - cnick, epass, cemail, cgreet, nc->icq, curl, nc->flags, - nc->language, nc->accesscount, nc->memos.memocount, - nc->memos.memomax, nc->channelcount, nc->channelmax); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); - } + if (mysql_affected_rows(mysql) == 0) { + db_mysql_try + ("INSERT DELAYED INTO anope_ns_core (display, pass, email, greet, icq, " + "url, flags, language, accesscount, memocount, memomax, " + "channelcount, channelmax, active) VALUES ('%s', %s, '%s', " + "'%s', %d, '%s', %d, %d, %d, %d, %d, %d, %d, 1)", q_display, + epass, q_email, q_greet, nc->icq, q_url, nc->flags, + nc->language, nc->accesscount, nc->memos.memocount, + nc->memos.memomax, nc->channelcount, nc->channelmax); } /* Now let's do the access */ - for (j = 0, access = nc->access; j < nc->accesscount; j++, access++) { - caccess = db_mysql_quote(*access); - snprintf(sqlcmd, MAX_SQL_BUF, - "INSERT DELAYED INTO anope_ns_access (display,access) VALUES ('%s','%s')", - cnick, caccess); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); + for (i = 0; i < nc->accesscount; i++) { + q_access = db_mysql_quote(nc->access[i]); + + db_mysql_try + ("UPDATE anope_ns_access SET access = '%s' WHERE display = '%s'", + q_access, q_display); + + if (mysql_affected_rows(mysql) == 0) { + db_mysql_try + ("INSERT DELAYED INTO anope_ns_access (display, access) " + "VALUES ('%s','%s')", q_display, q_access); } - free(caccess); + + free(q_access); } - /* And... memos */ - memos = nc->memos.memos; - for (j = 0; j < nc->memos.memocount; j++, memos++) { - msender = db_mysql_quote(memos->sender); - mtext = db_mysql_quote(memos->text); - snprintf(sqlcmd, MAX_SQL_BUF, - "INSERT DELAYED INTO anope_ms_info (receiver,number,flags,time,sender,text,serv)" - " VALUES ('%s','%d','%d','%d','%s','%s','NICK')", - cnick, memos->number, memos->flags, - (int) memos->time, msender, mtext); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); + /* Memos */ + for (i = 0; i < nc->memos.memocount; i++) { + q_sender = db_mysql_quote(nc->memos.memos[i].sender); + q_text = db_mysql_quote(nc->memos.memos[i].text); + + db_mysql_try + ("UPDATE anope_ms_info SET receiver = '%s', number = %d, " + "flags = %d, time = %d, sender = '%s', text = '%s', " + "active = 1 WHERE nm_id = %d AND serv = 'NICK'", q_display, + nc->memos.memos[i].number, nc->memos.memos[i].flags, + (int) nc->memos.memos[i].time, q_sender, q_text, + nc->memos.memos[i].id); + if (mysql_affected_rows(mysql) == 0) { + db_mysql_try("INSERT INTO anope_ms_info (receiver, number, " + "flags, time, sender, text, serv, active) VALUES ('%s', " + "%d, %d, %d, '%s', '%s', 'NICK', 1)", q_display, + nc->memos.memos[i].number, + nc->memos.memos[i].flags, + (int) nc->memos.memos[i].time, q_sender, q_text); + + /* This is to make sure we can UPDATE memos instead of TRUNCATE + * the table each time and then INSERT them all again. Ideally + * everything in core would have it's dbase-id stored, but that's + * something for phase 3. -GD + */ + nc->memos.memos[i].id = mysql_insert_id(mysql); } - free(msender); - free(mtext); + + free(q_sender); + free(q_text); } - free(cnick); + free(q_display); free(epass); - free(cemail); - free(cgreet); - free(curl); + free(q_email); + free(q_greet); + free(q_url); } /*************************************************************************/ + +/* Save the given NickAlias into the database + * These tables are tagged and will be cleaned: + * - anope_ns_core + * - anope_ns_alias + * - anope_ns_access + * - anope_ms_info (serv='NICK') + */ void db_mysql_save_ns_alias(NickAlias * na) { - char sqlcmd[MAX_SQL_BUF]; - char *nnick, *nlmask, *nlrname, *nlquit, *nncnick; - nnick = db_mysql_quote(na->nick); - nlmask = db_mysql_quote(na->last_usermask); - nlrname = db_mysql_quote(na->last_realname); - nlquit = db_mysql_quote(na->last_quit); - nncnick = db_mysql_quote(na->nc->display); - snprintf(sqlcmd, MAX_SQL_BUF, - "UPDATE anope_ns_alias SET last_usermask='%s',last_realname='%s',last_quit='%s',time_registered='%d',last_seen='%d',status='%d',display='%s',active='1' WHERE nick='%s'", - nlmask, nlrname, nlquit, (int) na->time_registered, - (int) na->last_seen, (int) na->status, nncnick, nnick); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); - } - /* Our previous UPDATE affected no rows, therefore this is a new record */ - if ((int) mysql_affected_rows(mysql) <= 0) { - snprintf(sqlcmd, MAX_SQL_BUF, - "INSERT INTO anope_ns_alias (nick,last_usermask,last_realname,last_quit,time_registered,last_seen,status,display,active) VALUES ('%s','%s','%s','%s','%d','%d','%d','%s','1')", - nnick, nlmask, nlrname, nlquit, (int) na->time_registered, - (int) na->last_seen, (int) na->status, nncnick); - if (db_mysql_query(sqlcmd)) { - log_perror("Can't create sql query: %s", sqlcmd); - db_mysql_error(MYSQL_WARNING, "query"); - } - } + char *q_nick, *q_lastmask, *q_lastrname, *q_lastquit, *q_display; + + q_nick = db_mysql_quote(na->nick); + q_lastmask = db_mysql_quote(na->last_usermask); + q_lastrname = db_mysql_quote(na->last_realname); + q_lastquit = db_mysql_quote(na->last_quit); + q_display = db_mysql_quote(na->nc->display); - free(nnick); - free(nlmask); - free(nlrname); - free(nlquit); - free(nncnick); + db_mysql_try + ("UPDATE anope_ns_alias SET last_usermask = '%s', last_realname = '%s', " + "last_quit = '%s', time_registered = %d, last_seen = %d, status = %d, " + "display = '%s', active = 1 WHERE nick = '%s'", q_lastmask, + q_lastrname, q_lastquit, (int) na->time_registered, + (int) na->last_seen, (int) na->status, q_display, q_nick); - return; + /* Our previous UPDATE affected no rows, therefore this is a new record */ + if (mysql_affected_rows(mysql) == 0) { + db_mysql_try + ("INSERT DELAYED INTO anope_ns_alias (nick, last_usermask, last_realname, " + "last_quit, time_registered, last_seen, status, display, active) " + "VALUES ('%s', '%s', '%s', '%s', %d, %d, %d, '%s', 1)", + q_nick, q_lastmask, q_lastrname, q_lastquit, + (int) na->time_registered, (int) na->last_seen, + (int) na->status, q_display); + } + + free(q_nick); + free(q_lastmask); + free(q_lastrname); + free(q_lastquit); + free(q_display); } /*************************************************************************/ @@ -398,247 +466,282 @@ void db_mysql_save_ns_alias(NickAlias * na) */ /*************************************************************************/ + +/* Save the given ChannelInfo into the database + * Also save the access list, levels, akicks, badwords, ttb, and memo's for this channel + * These tables are tagged and will be cleaned: + * - anope_cs_info + * - anope_cs_access + * - anope_cs_levels + * - anope_cs_akicks + * - anope_cs_badwords + * - anope_cs_ttb + * - anope_ms_info (serv='CHAN') + */ void db_mysql_save_cs_info(ChannelInfo * ci) { - char sqlcmd[MAX_SQL_BUF]; - int j, position; + int i; Memo *memos; - char *ciname, *cifoundernick, *cisuccessornick, *cifounderpass, - *cidesc, *ciurl, *ciemail, *cilasttopic, *cilasttopicsetter, - *ciforbidby, *ciforbidreason, *cimlock_key, *cimlock_flood, - *cimlock_redirect, *cientrymsg, *cibotnick, *msender, *mtext, - *ciaccessdisp, *ciakickdisp, *ciakickreason, *ciakickcreator, - *cbadwords, *efounderpass; - - ciname = db_mysql_quote(ci->name); + char *q_name; + char *q_founder; + char *q_successor; + char *q_pass; + char *e_pass; + char *q_desc; + char *q_url; + char *q_email; + char *q_lasttopic; + char *q_lasttopicsetter; + char *q_forbidby; + char *q_forbidreason; + char *q_mlock_key; + char *q_mlock_flood; + char *q_mlock_redirect; + char *q_entrymsg; + char *q_botnick; + char *q_sender; + char *q_text; + char *q_accessdisp; + char *q_akickdisp; + char *q_akickreason; + char *q_akickcreator; + char *q_badwords; + + q_name = db_mysql_quote(ci->name); if (ci->founder) { - cifoundernick = db_mysql_quote(ci->founder->display); + q_founder = db_mysql_quote(ci->founder->display); } else { - cifoundernick = db_mysql_quote(""); + q_founder = db_mysql_quote(""); } if (ci->successor) { - cisuccessornick = db_mysql_quote(ci->successor->display); + q_successor = db_mysql_quote(ci->successor->display); } else { - cisuccessornick = db_mysql_quote(""); - } - cifounderpass = db_mysql_quote(ci->founderpass); - cidesc = db_mysql_quote(ci->desc); - ciurl = db_mysql_quote(ci->url); - ciemail = db_mysql_quote(ci->email); - cilasttopic = db_mysql_quote(ci->last_topic); - cilasttopicsetter = db_mysql_quote(ci->last_topic_setter); - ciforbidby = db_mysql_quote(ci->forbidby); - ciforbidreason = db_mysql_quote(ci->forbidreason); - cimlock_key = db_mysql_quote(ci->mlock_key); - cimlock_flood = db_mysql_quote(ci->mlock_flood); - cimlock_redirect = db_mysql_quote(ci->mlock_redirect); - cientrymsg = db_mysql_quote(ci->entry_message); + q_successor = db_mysql_quote(""); + } + q_pass = db_mysql_quote(ci->founderpass); + q_desc = db_mysql_quote(ci->desc); + q_url = db_mysql_quote(ci->url); + q_email = db_mys |
