added cookie fun command :D (and the underlying getSetting/setSetting functions)
authorpk910 <philipp@zoelle1.de>
Tue, 15 Nov 2011 15:02:27 +0000 (16:02 +0100)
committerpk910 <philipp@zoelle1.de>
Tue, 15 Nov 2011 15:02:27 +0000 (16:02 +0100)
database.sql
database.upgrade.sql
src/cmd_funcmds.c
src/mysqlConn.c

index 1aebd1e08ceda4df7e1117376b039ed23e4a2e3e..4b1bb19685bb1d82b11b8899b7a226ca72360290 100644 (file)
@@ -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 ;
 
 -- --------------------------------------------------------
 
index 36c8b0faf7b761855bdb4c89d44b450bb06862dd..cb710fdc55e9a4a6ea12c77d0bbe2c26a65cbcbd 100644 (file)
@@ -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
index 860ac5fa503bf927d9beca1869c903cebe65b72f..7578f10a0a51b8736ee9f656af19386538dc6704 100644 (file)
@@ -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);
 }
index 4bf15f2c4417779d299256e03f0d7a2a50759ee0..ec270374c94f2f0640e41882bbd168013fb3a420 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #include "mysqlConn.h"
-#define DATABASE_VERSION "6"
+#define DATABASE_VERSION "7"
 
 struct used_result {
     MYSQL_RES *result;