From de4e38ad80dc9365a31e0226635e3cdf26b21577 Mon Sep 17 00:00:00 2001 From: pk910 Date: Tue, 15 Nov 2011 16:02:27 +0100 Subject: [PATCH] added cookie fun command :D (and the underlying getSetting/setSetting functions) --- database.sql | 9 +++-- database.upgrade.sql | 7 ++++ src/cmd_funcmds.c | 94 ++++++++++++++++++++++++++++++++++++++------ src/mysqlConn.c | 2 +- 4 files changed, 97 insertions(+), 15 deletions(-) diff --git a/database.sql b/database.sql index 1aebd1e..4b1bb19 100644 --- a/database.sql +++ b/database.sql @@ -217,11 +217,14 @@ CREATE TABLE IF NOT EXISTS `funcmd` ( CREATE TABLE IF NOT EXISTS `fundata` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cid` int(11) NOT NULL, - `uid` int(11) NOT NULL, + `user` varchar(50) NOT NULL, `name` varchar(50) NOT NULL, `value` text NOT NULL, - PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='neonserv v3'; + PRIMARY KEY (`id`), + KEY `cid` (`cid`), + KEY `user` (`user`), + KEY `name` (`name`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='neonserv v3' AUTO_INCREMENT=1 ; -- -------------------------------------------------------- diff --git a/database.upgrade.sql b/database.upgrade.sql index 36c8b0f..cb710fd 100644 --- a/database.upgrade.sql +++ b/database.upgrade.sql @@ -76,3 +76,10 @@ UNIQUE ( ) ENGINE = MYISAM ; -- version: 6 + +ALTER TABLE `fundata` CHANGE `uid` `user` VARCHAR( 50 ) NOT NULL; +ALTER TABLE `fundata` ADD INDEX ( `cid` ); +ALTER TABLE `fundata` ADD INDEX ( `user` ); +ALTER TABLE `fundata` ADD INDEX ( `name` ); + +-- version: 7 diff --git a/src/cmd_funcmds.c b/src/cmd_funcmds.c index 860ac5f..7578f10 100644 --- a/src/cmd_funcmds.c +++ b/src/cmd_funcmds.c @@ -30,6 +30,7 @@ static const struct default_language_entry msgtab[] = { {"FUN_DICE_NUM", "I do not understand $b%s$b. Please use a single number above 1."}, /* {ARGS: "bla"} */ {"FUN_8BALL", "$b%s$b: %s"}, /* {ARGS: "TestUser", "Not a chance."} */ {"FUN_8BALL_REPLIES", "Not a chance.|In your dreams.|Absolutely!|Could be, could be.|No!"}, + {"FUN_COOKIE", "gives %1%s a very big chocolate cookie. (%1$s got %2$d cookies - %3$d in this channel)"}, /* {ARGS: "TestUser", 20, 50} */ {NULL, NULL} }; @@ -66,19 +67,28 @@ current_funcmd.chan = chan; \ current_funcmd.send_notice = 0; \ } -static void funcmd_reply(const char *text, ...) { +#define REPLYTYPE_NORMAL 0 +#define REPLYTYPE_ACTION 1 +static void funcmd_reply(const char *text, int type, ...) { + if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return; const char *reply_format = get_language_string(current_funcmd.user, text); if(reply_format) text = reply_format; char formatBuf[MAXLEN]; - if(current_funcmd.send_notice) - sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text); - else - sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text); + if(current_funcmd.send_notice) { + if(type == REPLYTYPE_ACTION) + sprintf(formatBuf, "NOTICE %s :%s %s", current_funcmd.user->nick, current_funcmd.client->user->nick, text); + else + sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text); + } else { + if(type == REPLYTYPE_ACTION) + sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", current_funcmd.chan->name, text); + else + sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text); + } va_list arg_list; char sendBuf[MAXLEN]; int pos; - if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return; sendBuf[0] = '\0'; va_start(arg_list, text); pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list); @@ -89,14 +99,57 @@ static void funcmd_reply(const char *text, ...) { write_socket(current_funcmd.client, sendBuf, pos+1); } +static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) { + char *uname = ""; + int cid = 0; + MYSQL_RES *res; + MYSQL_ROW row; + if(user) { + uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*"); + } + if(chan) { + loadChannelSettings(chan); + if(chan->flags & CHANFLAG_CHAN_REGISTERED) + cid = chan->channel_id; + } + printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting)); + res = mysql_use(); + if ((row = mysql_fetch_row(res)) != NULL) { + return row[0]; + } else + return NULL; +} + +static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) { + char *uname = ""; + int cid = 0; + MYSQL_RES *res; + MYSQL_ROW row; + if(user) { + uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*"); + } + if(chan) { + loadChannelSettings(chan); + if(chan->flags & CHANFLAG_CHAN_REGISTERED) + cid = chan->channel_id; + } + printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting)); + res = mysql_use(); + if ((row = mysql_fetch_row(res)) != NULL) { + if(strcmp(row[1], value)) + printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]); + } else + printf_mysql_query("INSERT INTO `fundata` (`user`, `cid`, `name`, `value`) VALUES ('%s', '%d', '%s', '%s')", escape_string(uname), cid, escape_string(setting), escape_string(value)); +} + CMD_BIND(funcmd_ping) { FUNCMD_HEADER; - funcmd_reply("\002%s\002: Pong!", user->nick); + funcmd_reply("\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick); } CMD_BIND(funcmd_pong) { FUNCMD_HEADER; - funcmd_reply("\002%s\002: Ping!", user->nick); + funcmd_reply("\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick); } CMD_BIND(funcmd_dice) { @@ -104,9 +157,9 @@ CMD_BIND(funcmd_dice) { int max = atoi(argv[0]); if(max > 1) { int val = (rand() % max) + 1; - funcmd_reply("FUN_DICE", user->nick, val, max); + funcmd_reply("FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max); } else - funcmd_reply("FUN_DICE_NUM", argv[0]); + funcmd_reply("FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]); } CMD_BIND(funcmd_8ball) { @@ -138,6 +191,25 @@ CMD_BIND(funcmd_8ball) { } } if(creply) { - funcmd_reply("FUN_8BALL", user->nick, creply); + funcmd_reply("FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply); + } +} + +CMD_BIND(funcmd_cookie) { + FUNCMD_HEADER; + if(argc) { + if(!(user = getUserByNick(argv[0]))) + reply(current_funcmd.client, current_funcmd.user, "NS_USER_UNKNOWN", argv[0]); } + char *tmp; + int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0); + int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0); + user_count++; + total_count++; + char buf[10]; + sprintf(buf, "%d", user_count); + setSetting(user, chan, "cookies", buf); + sprintf(buf, "%d", total_count); + setSetting(user, NULL, "cookies", buf); + funcmd_reply("FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count); } diff --git a/src/mysqlConn.c b/src/mysqlConn.c index 4bf15f2..ec27037 100644 --- a/src/mysqlConn.c +++ b/src/mysqlConn.c @@ -16,7 +16,7 @@ */ #include "mysqlConn.h" -#define DATABASE_VERSION "6" +#define DATABASE_VERSION "7" struct used_result { MYSQL_RES *result; -- 2.20.1