summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgeniusdex geniusdex@31f1291d-b8d6-0310-a050-a5561fc1590b <geniusdex geniusdex@31f1291d-b8d6-0310-a050-a5561fc1590b@5417fbe8-f217-4b02-8779-1006273d7864>2006-12-09 22:35:44 +0000
committergeniusdex geniusdex@31f1291d-b8d6-0310-a050-a5561fc1590b <geniusdex geniusdex@31f1291d-b8d6-0310-a050-a5561fc1590b@5417fbe8-f217-4b02-8779-1006273d7864>2006-12-09 22:35:44 +0000
commit6e4112460817c1a099f2143b5e5d18a23c1be621 (patch)
tree7ed50fbba3e2779228c1ddf2811f74ce98e79344 /src
parent888fe64c6001c09622162d873375dd638c20f85f (diff)
BUILD : 1.7.17 (1198) BUGS : NOTES : Reviewed and updated all of the MySQL code...
git-svn-id: svn://svn.anope.org/anope/trunk@1198 31f1291d-b8d6-0310-a050-a5561fc1590b git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@917 5417fbe8-f217-4b02-8779-1006273d7864
Diffstat (limited to 'src')
-rw-r--r--src/botserv.c5
-rw-r--r--src/chanserv.c46
-rw-r--r--src/hostserv.c5
-rw-r--r--src/mysql.c2109
-rw-r--r--src/news.c7
-rw-r--r--src/nickserv.c14
-rw-r--r--src/operserv.c14
-rw-r--r--src/rdb.c202
-rw-r--r--src/sessions.c3
9 files changed, 1263 insertions, 1142 deletions
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_mysql_quote(ci->email);
+ q_lasttopic = db_mysql_quote(ci->last_topic);
+ q_lasttopicsetter = db_mysql_quote(ci->last_topic_setter);
+ q_forbidby = db_mysql_quote(ci->forbidby);
+ q_forbidreason = db_mysql_quote(ci->forbidreason);
+ q_mlock_key = db_mysql_quote(ci->mlock_key);
+ q_mlock_flood = db_mysql_quote(ci->mlock_flood);
+ q_mlock_redirect = db_mysql_quote(ci->mlock_redirect);
+ q_entrymsg = db_mysql_quote(ci->entry_message);
if (ci->bi) {
- cibotnick = db_mysql_quote(ci->bi->nick);
+ q_botnick = db_mysql_quote(ci->bi->nick);
} else {
- cibotnick = db_mysql_quote("");
+ q_botnick = db_mysql_quote("");
}
- efounderpass = db_mysql_secure(cifounderpass);
- free(cifounderpass);
+ e_pass = db_mysql_secure(q_pass);
+ free(q_pass);
/* Let's take care of the core itself */
- snprintf(sqlcmd, MAX_SQL_BUF,
- "UPDATE anope_cs_info SET founder='%s',successor='%s',founderpass=%s,"
- "descr='%s',url='%s',email='%s',time_registered='%d',last_used='%d',"
- "last_topic='%s',last_topic_setter='%s',last_topic_time='%d',flags='%d',"
- "forbidby='%s',forbidreason='%s',bantype='%d',accesscount='%d',"
- "akickcount='%d',mlock_on='%d',mlock_off='%d',mlock_limit='%d',"
- "mlock_key='%s',mlock_flood='%s',mlock_redirect='%s',entry_message='%s',"
- "memomax='%d',botnick='%s',botflags='%d',bwcount='%d',"
- "capsmin='%d',capspercent='%d',floodlines='%d',floodsecs='%d',"
- "repeattimes='%d',active='1' WHERE name='%s'",
- cifoundernick,
- cisuccessornick,
- efounderpass, cidesc, ciurl, ciemail,
- (int) ci->time_registered, (int) ci->last_used,
- cilasttopic, cilasttopicsetter,
- (int) ci->last_topic_time, (int) ci->flags,
- ciforbidby, ciforbidreason, (int) ci->bantype,
- (int) ci->accesscount, (int) ci->akickcount,
- (int) ci->mlock_on, (int) ci->mlock_off,
- (int) ci->mlock_limit, cimlock_key,
- cimlock_flood,
- cimlock_redirect,
- cientrymsg,
- (int) ci->memos.memomax,
- cibotnick,
- (int) ci->botflags,
- (int) ci->bwcount,
- (int) ci->capsmin,
- (int) ci->capspercent,
- (int) ci->floodlines,
- (int) ci->floodsecs, (int) ci->repeattimes, ciname);
- 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_cs_info SET founder = '%s', successor = '%s', "
+ "founderpass = %s, descr = '%s', url = '%s', email = '%s', "
+ "time_registered = %d, last_used = %d, last_topic = '%s', "
+ "last_topic_setter = '%s', last_topic_time = %d, flags = %d, "
+ "forbidby = '%s', forbidreason = '%s', bantype = %d, "
+ "accesscount = %d, akickcount = %d, mlock_on = %d, "
+ "mlock_off = %d, mlock_limit = %d, mlock_key = '%s', "
+ "mlock_flood = '%s', mlock_redirect = '%s', entry_message = '%s', "
+ "memomax = %d, botnick = '%s', botflags = %d, bwcount = %d, "
+ "capsmin = %d, capspercent = %d, floodlines = %d, floodsecs = %d, "
+ "repeattimes = %d, active = 1 WHERE name = '%s'", q_founder,
+ q_successor, e_pass, q_desc, q_url, q_email,
+ (int) ci->time_registered, (int) ci->last_used, q_lasttopic,
+ q_lasttopicsetter, (int) ci->last_topic_time, (int) ci->flags,
+ q_forbidby, q_forbidreason, (int) ci->bantype,
+ (int) ci->accesscount, (int) ci->akickcount, (int) ci->mlock_on,
+ (int) ci->mlock_off, (int) ci->mlock_limit, q_mlock_key,
+ q_mlock_flood, q_mlock_redirect, q_entrymsg,
+ (int) ci->memos.memomax, q_botnick, (int) ci->botflags,
+ (int) ci->bwcount, (int) ci->capsmin, (int) ci->capspercent,
+ (int) ci->floodlines, (int) ci->floodsecs, (int) ci->repeattimes,
+ q_name);
/* 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 DELAYED INTO anope_cs_info (name,founder,successor,founderpass,"
- "descr,url,email,time_registered,last_used,last_topic,last_topic_setter"
- ",last_topic_time,flags,forbidby,forbidreason,bantype,accesscount,akickcount"
- ",mlock_on,mlock_off,mlock_limit,mlock_key,mlock_flood,mlock_redirect,"
- "entry_message,botnick,botflags,bwcount,capsmin,capspercent,floodlines,"
- "floodsecs,repeattimes,active) VALUES ('%s','%s','%s',%s,'%s','%s','%s'"
- ",'%d','%d','%s','%s','%d','%d','%s','%s','%d','%d','%d','%d','%d','%d',"
- "'%s','%s','%s','%s','%s','%d','%d','%d','%d','%d','%d','%d','1')",
- ciname,
- cifoundernick,
- cisuccessornick,
- efounderpass, cidesc, ciurl, ciemail,
- (int) ci->time_registered, (int) ci->last_used,
- cilasttopic, cilasttopicsetter,
- (int) ci->last_topic_time, (int) ci->flags,
- ciforbidby, ciforbidreason, (int) ci->bantype,
- (int) ci->accesscount, (int) ci->akickcount,
- (int) ci->mlock_on, (int) ci->mlock_off,
- (int) ci->mlock_limit, cimlock_key,
- cimlock_flood,
- cimlock_redirect,
- cientrymsg,
- cibotnick,
- (int) ci->botflags,
- (int) ci->bwcount,
- (int) ci->capsmin,
- (int) ci->capspercent,
- (int) ci->floodlines,
- (int) ci->floodsecs, (int) ci->repeattimes);
- 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_cs_info (name, founder, successor, "
+ "founderpass, descr, url, email, time_registered, last_used, "
+ "last_topic, last_topic_setter, last_topic_time, flags, forbidby, "
+ "forbidreason, bantype, accesscount, akickcount, mlock_on, "
+ "mlock_off, mlock_limit, mlock_key, mlock_flood, mlock_redirect, "
+ "entry_message, botnick, botflags, bwcount, capsmin, capspercent, "
+ "floodlines, floodsecs, repeattimes, active) VALUES "
+ "('%s', '%s', '%s', %s, '%s', '%s', '%s', %d, %d, "
+ "'%s', '%s', %d, %d, '%s', '%s', %d, %d, %d, %d, "
+ "%d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, "
+ "%d, %d, %d, 1)", q_name, q_founder, q_successor, e_pass,
+ q_desc, q_url, q_email, (int) ci->time_registered,
+ (int) ci->last_used, q_lasttopic, q_lasttopicsetter,
+ (int) ci->last_topic_time, (int) ci->flags, q_forbidby,
+ q_forbidreason, (int) ci->bantype, (int) ci->accesscount,
+ (int) ci->akickcount, (int) ci->mlock_on, (int) ci->mlock_off,
+ (int) ci->mlock_limit, q_mlock_key, q_mlock_flood,
+ q_mlock_redirect, q_entrymsg, q_botnick, (int) ci->botflags,
+ (int) ci->bwcount, (int) ci->capsmin, (int) ci->capspercent,
+ (int) ci->floodlines, (int) ci->floodsecs,
+ (int) ci->repeattimes);
}
/* Memos */
- memos = ci->memos.memos;
- for (j = 0; j < ci->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','CHAN')",
- ciname, 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");
+ for (i = 0; i < ci->memos.memocount; i++) {
+ q_sender = db_mysql_quote(ci->memos.memos[i].sender);
+ q_text = db_mysql_quote(ci->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 = 'CHAN'", q_name,
+ ci->memos.memos[i].number, ci->memos.memos[i].flags,
+ (int) ci->memos.memos[i].time, q_sender, q_text,
+ ci->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', 'CHAN', 1)", q_name,
+ ci->memos.memos[i].number,
+ ci->memos.memos[i].flags,
+ (int) ci->memos.memos[i].time, q_sender, q_text);
+
+ /* See comment at db_mysql_save_ns_core */
+ ci->memos.memos[i].id = mysql_insert_id(mysql);
}
- free(msender);
- free(mtext);
+
+ free(q_sender);
+