OSDN Git Service

IPBANLISTをMYSQLのテーブルで定義した
authorkonekoneko <jbh03215@hotmail.co.jp>
Fri, 16 Nov 2012 15:11:45 +0000 (00:11 +0900)
committerkonekoneko <jbh03215@hotmail.co.jp>
Fri, 16 Nov 2012 15:11:45 +0000 (00:11 +0900)
chat.js
init.sql

diff --git a/chat.js b/chat.js
index 5b68ad5..30e57da 100644 (file)
--- a/chat.js
+++ b/chat.js
@@ -365,6 +365,13 @@ function RoomInfomation(pass,hiddenlogflag)
 //IPBANクラス\r
 function IpBanCollecion()\r
 {\r
+       var MySQLPool = new require("./mysql_pool.js");\r
+       var pool = new MySQLPool({\r
+                               host     : config.db_host,\r
+                               user     : config.db_user,\r
+                               password : config.db_password,\r
+                               database : "configure",\r
+                       });\r
        var collection = {};\r
        this.IsBaned = function(ip){\r
                return collection[ip] == "r";\r
@@ -384,49 +391,42 @@ function IpBanCollecion()
                return text;\r
        }\r
        this.Update = function(text,callfunc){\r
+               collection = {};\r
                async.waterfall([\r
-                       function(callback){\r
-                               fs.open($ip_ban_list_file_name,"w",callback);\r
-                       },\r
-                       function(fd,callback){\r
-                               var buf = new Buffer(text);\r
-                               fs.write(fd,buf,0,Buffer.byteLength(text),null,function(){\r
-                                       callback(null,fd);\r
-                               });\r
+                       function(next){\r
+                               pool.query("TRUNCATE TABLE ipbanlist",null,next);\r
                        },\r
-                       function(fd,callback){\r
-                               fs.close(fd,function(){\r
-                                       GetIpBanList(callfunc);\r
-                               });\r
-                       }\r
-               ]);\r
-       }\r
-       function GetIpBanList(callback)\r
-       {\r
-               collection = {};\r
-               fs.exists($ip_ban_list_file_name,function(exists){\r
-                       if(exists == false)\r
-                       {\r
-                               if(typeof(callback) == "function")\r
-                                       callback();\r
-                               return;\r
-                       }\r
-                       var stream = fs.createReadStream($ip_ban_list_file_name);\r
-                       new lazy(stream)\r
-                               .lines\r
-                               .forEach(function(line){\r
-                                       var token = line.toString().replace(/(\r|\n|\r\n)/gm, "").split(":");\r
+                       function(result,next){\r
+                               var items = new Array();\r
+                               lines = text.split("\r\n");\r
+                               for(var i = 0; i < lines.length; i++)\r
+                               {\r
+                                       var token = lines[i].split(":");\r
                                        var ip = token[0];\r
+                                       if(ip == "")\r
+                                               continue;\r
                                        if(token.length == 1)\r
                                                collection[ip] = "";\r
                                        else\r
                                                collection[ip] = token[1];\r
-                               })\r
-                               .join(function(){\r
-                                       if(typeof(callback) == "function")\r
-                                               callback();\r
-                               });\r
-               });\r
+                                       items.push(new Array(ip,collection[ip]));\r
+                               }\r
+                               pool.query("INSERT INTO ipbanlist VALUES ?",[items],next);\r
+                       },\r
+               ],callfunc);\r
+       }\r
+       function GetIpBanList(callfunc)\r
+       {\r
+               async.waterfall([\r
+                       function(next){\r
+                               pool.query("SELECT * FROM ipbanlist",null,next);\r
+                       },\r
+                       function(result,next){\r
+                               for(var i = 0; i < result.length; i++)\r
+                                       collection[result[i].ip] = result[i].type;\r
+                               next(null,null);\r
+                       },\r
+               ],callfunc);\r
        }\r
        GetIpBanList();\r
 }\r
index 06bf100..42e084e 100644 (file)
--- a/init.sql
+++ b/init.sql
@@ -1,12 +1,12 @@
-CREATE DATABASE IF NOT EXISTS profile;
 grant select,
        insert,
        delete,
        update on *.* to user identified by 'user';
 flush privileges;
+CREATE DATABASE IF NOT EXISTS profile;
 use profile;
-drop table list;
-create table list(name VARCHAR(64) NOT NULL,
+DROP TABLE IF EXISTS list;
+CREATE TABLE list(name VARCHAR(64) NOT NULL,
        age SMALLINT  UNSIGNED DEFAULT 0,
        height SMALLINT  UNSIGNED DEFAULT 0,
        weight SMALLINT  UNSIGNED DEFAULT 0,
@@ -16,3 +16,8 @@ create table list(name VARCHAR(64) NOT NULL,
        lastmodified DATETIME,
        etc TEXT,
        PRIMARY KEY(name));
+CREATE DATABASE IF NOT EXISTS configure;
+use configure;
+DROP TABLE IF EXISTS ipbanlist;
+CREATE TABLE ipbanlist(ip VARCHAR(64) NOT NULL,
+       type CHAR(1));