+++ /dev/null
-/* cmd_funcmds.c - NeonServ v5.3
- * Copyright (C) 2011-2012 Philipp Kreil (pk910)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "cmd_funcmds.h"
-#include "modcmd.h"
-#include "mysqlConn.h"
-#include "IRCParser.h"
-#include "ClientSocket.h"
-#include "UserNode.h"
-#include "ChanNode.h"
-#include "lang.h"
-#include "tools.h"
-#include "DBHelper.h"
-
-static const struct default_language_entry msgtab[] = {
- {"FUN_DICE", "$b%s$b: A $b%d$b shows on the %d-sided die."}, /* {ARGS: "TestUser", 5, 6} */
- {"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 has got %2$d cookies until now (%3$d in this channel)."}, /* {ARGS: "TestUser", 20, 50} */
- {NULL, NULL}
-};
-
-void init_funcmds() {
- register_default_language_table(msgtab);
- srand(time(NULL));
-}
-
-struct current_funcmd_header {
- struct ClientSocket *client;
- struct UserNode *user;
- struct ChanNode *chan;
- char send_notice;
-};
-
-static struct current_funcmd_header current_funcmd;
-
-#define FUNCMD_HEADER \
-current_funcmd.client = getTextBot(); \
-current_funcmd.user = user; \
-current_funcmd.chan = chan; \
-{\
- MYSQL_RES *res; \
- MYSQL_ROW row; \
- printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
- res = mysql_use(); \
- row = mysql_fetch_row(res); \
- if(!row || !strcmp(row[0], "0")) { \
- reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
- return; \
- } else if(!strcmp(row[0], "1")) \
- current_funcmd.send_notice = 1; \
- else \
- current_funcmd.send_notice = 0; \
-}
-
-#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) {
- 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;
- sendBuf[0] = '\0';
- va_start(arg_list, type);
- pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
- va_end(arg_list);
- if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
- sendBuf[pos] = '\n';
- sendBuf[pos+1] = '\0';
- 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!", REPLYTYPE_NORMAL, user->nick);
-}
-
-CMD_BIND(funcmd_pong) {
- FUNCMD_HEADER;
- funcmd_reply("\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
-}
-
-CMD_BIND(funcmd_dice) {
- FUNCMD_HEADER;
- int max = atoi(argv[0]);
- if(max > 1) {
- int val = (rand() % max) + 1;
- funcmd_reply("FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
- } else
- funcmd_reply("FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
-}
-
-CMD_BIND(funcmd_8ball) {
- FUNCMD_HEADER;
- char *message = merge_argv(argv, 0, argc);
- const char *const_replies = get_language_string(current_funcmd.user, "FUN_8BALL_REPLIES");
- char replies[MAXLEN];
- int i, reply_count = 1;
- for(i = 0; const_replies[i]; i++) {
- if(const_replies[i] == '|')
- reply_count++;
- replies[i] = const_replies[i];
- }
- replies[i] = '\0';
- unsigned int crc32_val = (crc32(message)) % reply_count;
- char *creply = (crc32_val == 0 ? replies : NULL);
- reply_count = 0;
- for(i = 0; replies[i]; i++) {
- if(replies[i] == '|') {
- if(creply) {
- replies[i] = '\0';
- break;
- } else {
- reply_count++;
- if(reply_count == crc32_val) {
- creply = &replies[i+1];
- }
- }
- }
- }
- if(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]);
- return;
- }
- }
- 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);
-}