OSDN Git Service

name_hashは被る可能性があるので、primaryではなくindexを指定するようにした
[webchat/WebChat.git] / ipban.js
1 //IPBAN\83N\83\89\83X
2 module.exports = function()
3 {
4         var config = require("./configure.js");
5         var IpBanModel = require("./init").GetIpBanColletion;
6         var collection = {};
7         this.IsBaned = function(ip){
8                 return collection[ip] == "r";
9         }
10         this.IsBlockedToWrite = function(ip){
11                 return ip in collection;
12         }
13         this.GetText = function(){
14                 var text = "";
15                 for(var key in collection)
16                 {
17                         if(collection[key] == "")
18                                 text += key + "\r\n";
19                         else
20                                 text += key + ":" + collection[key] + "\r\n";
21                 }
22                 return text;
23         }
24         this.Update = function(text,callfunc){
25                 collection = {};
26                 var async = require("async");
27                 async.waterfall([
28                         function(next){
29                                 IpBanModel.drop().done(next);
30                         },
31                         function(result,next){
32                                 var items = new Array();
33                                 lines = text.split("\r\n");
34                                 for(var i = 0; i < lines.length; i++)
35                                 {
36                                         var token = lines[i].split(":");
37                                         var ip = token[0];
38                                         if(ip == "")
39                                                 continue;
40                                         if(token.length == 1)
41                                                 collection[ip] = "";
42                                         else
43                                                 collection[ip] = token[1];
44                                         items.push(new Array(ip,collection[ip]));
45                                 }
46                                 newIpBan = IpBanModel.build(items);
47                                 newIpBan.save().done(next);
48                         },
49                 ],callfunc);
50         }
51         function GetIpBanList(callfunc)
52         {
53                 var async = require("async");
54                 async.waterfall([
55                         function(next){
56                                 IpBanModel.findAll().done(next);
57                         },
58                         function(result,next){
59                                 for(var i = 0; i < result.length; i++)
60                                         collection[result[i].ip] = result[i].type;
61                                 next(null,null);
62                         },
63                 ],callfunc);
64         }
65         GetIpBanList();
66 }