/* module not finished, DO NOT USE IT !!! */ /* insert into ircd_captcha (ip, time) values ("212.144.81.185", NULL); */ #define SHUN_MESSAGE "This Network is protected. Please fill the Form at http://dev.anope.de/php/pass.php to get access." #define SHUN_REASON "TEMPSHUNNED by services (ircd_captcha)" /*************************************************************************************************************************/ /******** DO NOT CHANGE BELOW THIS LINE UNLESS YOU KNOW WHAT ARE YOU DOING !!!!!!! **************************************/ /*************************************************************************************************************************/ #include "modules.h" #define DB_NAME "ircd_captcha" #define DB_SCHEMA "CREATE TABLE " DB_NAME " (\ ip VARCHAR( 30 ) NOT NULL ,\ time TIMESTAMP NOT NULL ,\ expire TIMESTAMP NULL ,\ PRIMARY KEY ( ip ) ,\ INDEX ( time )\ ) ENGINE = MYISAM " #define AUTHOR "Jens 'DukePyrolator' Voss " #define VERSION "0.1 beta" #define MODNAME "ircd_captcha" static MYSQL *mymysql; static MYSQL_RES *myres; static MYSQL_ROW myrow; char *myresult; int mydb_init(void); int do_query(const char *fmt, ... ); int my_nick(int argc, char **argv); // int purgedb(int argc, char *argv[]); int AnopeInit(int argc, char **argv) { // Command *c; EvtHook *hook = NULL; int status; #ifndef USE_MYSQL alog("[%s] MYSQL is not enabled. This module requires MYSQL !", MODNAME); alog("[%s] Module not loaded.", MODNAME); return MOD_STOP; #endif if (!do_mysql) { alog("[%s] MYSQL is not configured. Please edit your services.conf .", MODNAME); alog("[%s] Module not loaded.", MODNAME); return MOD_STOP; } /* activate mysql connection and stop if failed */ /* error messages are logged by mydb_init() */ if (mydb_init()) return MOD_STOP; hook = createEventHook(EVENT_NEWNICK, my_nick); status = moduleAddEventHook(hook); moduleAddAuthor(AUTHOR); moduleAddVersion(VERSION); return MOD_CONT; } /****************************************************************************************************/ /* main routine */ /****************************************************************************************************/ int my_nick(int argc, char **argv) { User *u; if (debug) alog("[%s] *** DEBUG *** new user connection %i : %s " , MODNAME, argc, argv[0]); u = finduser(argv[0]); if (debug) alog("[%s] *** DEBUG *** u->hostip = %s, u->mode = %i", MODNAME, u->hostip, u->mode); do_query("SELECT * FROM "DB_NAME" WHERE ip = '%s'", u->hostip); myres = mysql_store_result(mymysql); if (mysql_num_rows(myres) > 0) { /* any results in the database ? */ /* ip is already in the Database, user is allowed to connect */ /* no need to do additional checks */ mysql_free_result(myres); /* free allocated memory */ return MOD_CONT; } mysql_free_result(myres); /* free allocated memory */ /* send a notice to this user */ /* anope_cmd_notice(char *source, char *dest, const char *fmt, ...) */ notice_user(s_NickServ, u, SHUN_MESSAGE); /* tempshun the User */ send_cmd(ServerName, "TEMPSHUN +%s %s", u->nick, SHUN_REASON); return MOD_CONT; } /*****************************************************************************************************/ /* 1. connect to mysql by using the default values from services.conf) */ /* the module uses an own connection to the database */ /* 2. select the database */ /* 3. check if there are the right tables, if not (first use of this module) create them */ /* 4. returns 0 if all is ok */ /*****************************************************************************************************/ int mydb_init(void) { /* initializing mysql connection */ mymysql = mysql_init(NULL); if (MysqlSock) { if ((!mysql_real_connect(mymysql, MysqlHost, MysqlUser, MysqlPass, MysqlName, MysqlPort, MysqlSock, 0))) { alog("[%s] Cant connect to MySQL: %s\n", MODNAME, mysql_error(mysql)); alog("[%s] unloading...", MODNAME); return MOD_STOP; } } else { if ((!mysql_real_connect(mymysql, MysqlHost, MysqlUser, MysqlPass, MysqlName, MysqlPort, NULL, 0))) { alog("[%s] Cant connect to MySQL: %s\n", MODNAME, mysql_error(mysql)); alog("[%s] unloading...", MODNAME); return MOD_STOP; } } if (mysql_select_db(mymysql, MysqlName)) { alog("[%s] Cant connect to MySQL: %s\n", MODNAME, mysql_error(mysql)); alog("[%s] unloading...", MODNAME); return MOD_STOP; } /* checking for DB table and create it, if not found */ do_query("SHOW TABLES LIKE '" DB_NAME "';"); myres = mysql_store_result(mymysql); if (mysql_num_rows(myres) == 0) { /* table not found */ do_query(DB_SCHEMA); /* create table */ alog("[%s] Created table %s", MODNAME, DB_NAME); } mysql_free_result(myres); return 0; } /*************************************************************************************************/ /* executes a mysql query */ /* input: query (escaped string) */ /* output: nothing */ /*************************************************************************************************/ int do_query(const char *fmt, ... ) { char query[MAX_SQL_BUF]; int result, slen = 0; va_list args; *query = '\0'; va_start(args, fmt); slen = vsnprintf(query, MAX_SQL_BUF - 1, fmt, args); va_end(args); if (debug) alog("[%s] Query %s", MODNAME, query); mysql_ping(mymysql); /* checks for mysql connection and reconnect */ result = mysql_real_query(mymysql, query , slen); if (result) { log_perror(mysql_error(mymysql)); } return 0; }