OSDN Git Service

cd71581ba72e723ea68918ab584b34c7f17b21ff
[webchat/WebChat.git] / ipban.js
1 //IPBAN\83N\83\89\83X
2 module.exports = function()
3 {
4         var config = require("./configure.js");
5         var MySQLPool = new require("./mysql_pool.js");
6         var pool = new MySQLPool({
7                                 host     : config.db_host,
8                                 user     : config.db_user,
9                                 password : config.db_password,
10                                 port     : config.db_port,
11                                 database : config.db_name,
12                         });
13         var collection = {};
14         this.IsBaned = function(ip){
15                 return collection[ip] == "r";
16         }
17         this.IsBlockedToWrite = function(ip){
18                 return ip in collection;
19         }
20         this.GetText = function(){
21                 var text = "";
22                 for(var key in collection)
23                 {
24                         if(collection[key] == "")
25                                 text += key + "\r\n";
26                         else
27                                 text += key + ":" + collection[key] + "\r\n";
28                 }
29                 return text;
30         }
31         this.Update = function(text,callfunc){
32                 collection = {};
33                 var async = require("async");
34                 async.waterfall([
35                         function(next){
36                                 pool.query("TRUNCATE TABLE ipbanlist",null,next);
37                         },
38                         function(result,next){
39                                 var items = new Array();
40                                 lines = text.split("\r\n");
41                                 for(var i = 0; i < lines.length; i++)
42                                 {
43                                         var token = lines[i].split(":");
44                                         var ip = token[0];
45                                         if(ip == "")
46                                                 continue;
47                                         if(token.length == 1)
48                                                 collection[ip] = "";
49                                         else
50                                                 collection[ip] = token[1];
51                                         items.push(new Array(ip,collection[ip]));
52                                 }
53                                 pool.query("INSERT INTO ipbanlist VALUES ?",[items],next);
54                         },
55                 ],callfunc);
56         }
57         function GetIpBanList(callfunc)
58         {
59                 var async = require("async");
60                 async.waterfall([
61                         function(next){
62                                 pool.query("SELECT * FROM ipbanlist",null,next);
63                         },
64                         function(result,next){
65                                 for(var i = 0; i < result.length; i++)
66                                         collection[result[i].ip] = result[i].type;
67                                 next(null,null);
68                         },
69                 ],callfunc);
70         }
71         GetIpBanList();
72 }