-//RoomInfomationCollecionクラス\r
-function RoomInfomationCollection()\r
-{\r
- var config = require("./configure.js");\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
- port : config.db_port,\r
- database : config.db_name,\r
- });\r
- var collection = {};\r
- this.Get = function(rno){\r
- return collection[rno];\r
- }\r
- this.IsContains = function(rno){\r
- return rno in collection;\r
- };\r
- this.GetMessage = function(){\r
- var retval = new Array();\r
- for(var rno in collection)\r
- {\r
- item={};\r
- item.applyflag = !$rooms.Get(rno).IsVolatile();\r
- item.password = collection[rno].password;\r
- if(item.password == null)\r
- item.password = "";\r
- item.hiddenlog = collection[rno].hiddenlog;\r
- retval.push(item);\r
- }\r
- return retval;\r
- };\r
- this.GetKeys = function(){\r
- var retval = {};\r
- for(var rno in collection)\r
- {\r
- retval[rno] = {};\r
- }\r
- return retval;\r
- }\r
- this.Update = function(data,callfunc){\r
- Clear();\r
- var async = require("async");\r
- async.waterfall([\r
- function(next){\r
- pool.query("TRUNCATE TABLE rooms",null,next);\r
- },\r
- function(result,next){\r
- var util = require("util");\r
- console.log(util.inspect(data));\r
- var items = new Array();\r
- var config = data.config;\r
- for(var i = 0; i < config.length; i++)\r
- {\r
- var rno = Number(config[i].applyflag);\r
- if(isNaN(rno))\r
- continue;\r
- var password,romonly;\r
- if(typeof(config[rno].password)=="undefined")\r
- password = null;\r
- else if(config[rno].password == "")\r
- password = null;\r
- else\r
- password = config[rno].password;\r
- if(typeof(config[rno].hiddenlog)=="undefined")\r
- romonly = false;\r
- else\r
- romonly = config[rno].hiddenlog == "romonly";\r
-\r
- Add(rno,password,romonly);\r
- items.push(new Array(rno,password,romonly));\r
- }\r
- pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);\r
- }\r
- ],callfunc);\r
- }\r
- function GetRoomList(callback){\r
- Clear();\r
- var async = require("async");\r
- async.waterfall([\r
- function(next){\r
- pool.query("SELECT * FROM rooms",null,next);\r
- },\r
- function(result,next){\r
- for(var i = 0; i < result.length; i++)\r
- {\r
- //MySQLではTINYINTが使われている\r
- Add(result[i].number,result[i].password,result[i].hiddenlog != 0);\r
- }\r
- next(null,null);\r
- }\r
- ],callback);\r
- }\r
- function Clear(){\r
- collection = {};\r
- var config = require("./configure.js");\r
- for(var i = 0; i < config.max_room_number; i++)\r
- Add(i,null,null);\r
- };\r
- function Add(rno,pass,hiddenlogflag){\r
- collection[rno] = new RoomInfomation(pass,hiddenlogflag);\r
- if(pass != null)\r
- collection[rno].owner = $system_name;\r
- };\r
- var $gc_interval_id = setInterval(function(){\r
- for(var rno in this.rom_list)\r
- collection[rno].GCRomList();\r
- },$gc_time_interval);\r
- GetRoomList();\r
-}\r
-\r
-//RoomInfomationクラス\r
-function RoomInfomation(pass,hiddenlogflag)\r
-{\r
- this.password = pass;\r
- this.rom_list = {};\r
- this.authed_list = {};\r
- this.owner = null;\r
- this.time = null;\r
- this.hiddenlog = hiddenlogflag;\r
- this.GetConfig = function(){\r
- var roomconfig = {};\r
- if(this.IsVolatile() == false)\r
- {\r
- if(this.IsFixedPassword())\r
- roomconfig.type = 2;\r
- else if(this.IsHiddenLogFromRom())\r
- roomconfig.type = 3;\r
- else\r
- roomconfig.type = 1;\r
- roomconfig.IsOwned = !this.IsFirstAuth();\r
- }else{\r
- roomconfig.type = 0;\r
- }\r
- return roomconfig;\r
- }\r
- this.IsVolatile = function(){\r
- return this.owner == null &&\r
- this.password == null &&\r
- this.time == null &&\r
- this.hiddenlog == null;\r
- }\r
- this.GetRomCount = function(){\r
- var count = 0;\r
- for(var key in this.rom_list)\r
- count++;\r
- return count;\r
- };\r
- this.AddRom = function(ip){\r
- var date = new Date();\r
- this.rom_list[ip] = {time:date.getTime()};\r
- };\r
- this.RemoveRom = function(ip){\r
- delete this.rom_list[ip];\r
- };\r
- this.Reset = function(owner){\r
- var date = new Date();\r
- var time = date.getTime();\r
- this.password = null;\r
- this.authed_list = {};\r
- this.owner = owner;\r
- this.time = time;\r
- };\r
- this.IsFirstAuth = function(){\r
- return this.owner == null;\r
- };\r
- this.IsAuthed = function(name){\r
- return name == this.owner ||\r
- name in this.authed_list;\r
- };\r
- this.IsHiddenLogFromRom = function(){\r
- return this.hiddenlog;\r
- };\r
- this.IsFixedPassword = function(){\r
- return this.owner == $system_name;\r
- };\r
- this.IsOwner = function(name){\r
- return this.owner == name;\r
- };\r
- this.IsTimeout = function(){\r
- var date = new Date();\r
- var current_time = date.getTime();\r
- return !this.IsFixedPassword() &&\r
- current_time - this.time >= $reset_password_diff;\r
- };\r
- this.RemoveAuth = function(name)\r
- {\r
- delete this.authed_list[name];\r
- };\r
- this.Auth = function(name,password){\r
- if(this.password != password)\r
- return false;\r
- var date = new Date();\r
- var time = date.getTime();\r
- this.time = time;\r
- this.authed_list[name] = "";\r
- return true;\r
- };\r
- this.SetPassword = function(owner,password){\r
- if(owner == this.owner &&\r
- !this.IsFixedPassword() &&\r
- !this.IsHiddenLogFromRom())\r
- {\r
- var date = new Date();\r
- this.time = date.getTime();\r
- this.password = password;\r
- return true;\r
- }\r
- return false;\r
- };\r
- this.GCRomList = function(){\r
- var date = new Date();\r
- var current_time = date.getTime();\r
- for(var ip in this.rom_list)\r
- {\r
- if(current_time - this.rom_list[ip].time >= $gc_time_interval)\r
- delete this.rom_list[ip];\r
- }\r
- };\r
-}\r
-\r
-//IPBANクラス\r
-function IpBanCollecion()\r
-{\r
- var config = require("./configure.js");\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
- port : config.db_port,\r
- database : config.db_name,\r
- });\r
- var collection = {};\r
- this.IsBaned = function(ip){\r
- return collection[ip] == "r";\r
- }\r
- this.IsBlockedToWrite = function(ip){\r
- return ip in collection;\r
- }\r
- this.GetText = function(){\r
- var text = "";\r
- for(var key in collection)\r
- {\r
- if(collection[key] == "")\r
- text += key + "\r\n";\r
- else\r
- text += key + ":" + collection[key] + "\r\n";\r
- }\r
- return text;\r
- }\r
- this.Update = function(text,callfunc){\r
- collection = {};\r
- var async = require("async");\r
- async.waterfall([\r
- function(next){\r
- pool.query("TRUNCATE TABLE ipbanlist",null,next);\r
- },\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
- 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
- var async = require("async");\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
-\r
-var ipbanlist = new IpBanCollecion();\r
-var $rooms = new RoomInfomationCollection();\r
-\r
-createLogDirectory();\r
-\r