OSDN Git Service

IPBANLISTをMYSQLのテーブルで定義した
[webchat/WebChat.git] / chat.js
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