From a336b85c60cbccbcaab44f2947b3652477d72ad4 Mon Sep 17 00:00:00 2001 From: konekoneko Date: Sat, 31 May 2014 01:28:48 +0900 Subject: [PATCH] =?utf8?q?=E4=B8=80=E9=83=A8=E3=82=AF=E3=83=A9=E3=82=B9?= =?utf8?q?=E3=82=92=E5=88=A5=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AB?= =?utf8?q?=E5=88=86=E9=9B=A2=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- chat.js | 305 ++------------------------------------------------------------- ipban.js | 72 +++++++++++++++ room.js | 221 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+), 300 deletions(-) create mode 100644 ipban.js create mode 100644 room.js diff --git a/chat.js b/chat.js index dbdd35e..9e3d294 100644 --- a/chat.js +++ b/chat.js @@ -10,6 +10,11 @@ $splited_log_file_name = "logfile%d_%s.txt" //分割後のファイル名(%dと% var clients = new Array(); +var ipbanlist = new require("./ipban.js").IpBanCollecion(); +var $rooms = new require("./room.js").new RoomInfomationCollection(); + +createLogDirectory(); + var sessionStore; module.exports = function(app,server,express,session){ @@ -120,306 +125,6 @@ function removeLog(files,callback) }); } -//RoomInfomationCollecionクラス -function RoomInfomationCollection() -{ - var config = require("./configure.js"); - var MySQLPool = new require("./mysql_pool.js"); - var pool = new MySQLPool({ - host : config.db_host, - user : config.db_user, - password : config.db_password, - port : config.db_port, - database : config.db_name, - }); - var collection = {}; - this.Get = function(rno){ - return collection[rno]; - } - this.IsContains = function(rno){ - return rno in collection; - }; - this.GetMessage = function(){ - var retval = new Array(); - for(var rno in collection) - { - item={}; - item.applyflag = !$rooms.Get(rno).IsVolatile(); - item.password = collection[rno].password; - if(item.password == null) - item.password = ""; - item.hiddenlog = collection[rno].hiddenlog; - retval.push(item); - } - return retval; - }; - this.GetKeys = function(){ - var retval = {}; - for(var rno in collection) - { - retval[rno] = {}; - } - return retval; - } - this.Update = function(data,callfunc){ - Clear(); - var async = require("async"); - async.waterfall([ - function(next){ - pool.query("TRUNCATE TABLE rooms",null,next); - }, - function(result,next){ - var util = require("util"); - console.log(util.inspect(data)); - var items = new Array(); - var config = data.config; - for(var i = 0; i < config.length; i++) - { - var rno = Number(config[i].applyflag); - if(isNaN(rno)) - continue; - var password,romonly; - if(typeof(config[rno].password)=="undefined") - password = null; - else if(config[rno].password == "") - password = null; - else - password = config[rno].password; - if(typeof(config[rno].hiddenlog)=="undefined") - romonly = false; - else - romonly = config[rno].hiddenlog == "romonly"; - - Add(rno,password,romonly); - items.push(new Array(rno,password,romonly)); - } - pool.query("INSERT INTO rooms VALUES ?",[items],callfunc); - } - ],callfunc); - } - function GetRoomList(callback){ - Clear(); - var async = require("async"); - async.waterfall([ - function(next){ - pool.query("SELECT * FROM rooms",null,next); - }, - function(result,next){ - for(var i = 0; i < result.length; i++) - { - //MySQLではTINYINTが使われている - Add(result[i].number,result[i].password,result[i].hiddenlog != 0); - } - next(null,null); - } - ],callback); - } - function Clear(){ - collection = {}; - var config = require("./configure.js"); - for(var i = 0; i < config.max_room_number; i++) - Add(i,null,null); - }; - function Add(rno,pass,hiddenlogflag){ - collection[rno] = new RoomInfomation(pass,hiddenlogflag); - if(pass != null) - collection[rno].owner = $system_name; - }; - var $gc_interval_id = setInterval(function(){ - for(var rno in this.rom_list) - collection[rno].GCRomList(); - },$gc_time_interval); - GetRoomList(); -} - -//RoomInfomationクラス -function RoomInfomation(pass,hiddenlogflag) -{ - this.password = pass; - this.rom_list = {}; - this.authed_list = {}; - this.owner = null; - this.time = null; - this.hiddenlog = hiddenlogflag; - this.GetConfig = function(){ - var roomconfig = {}; - if(this.IsVolatile() == false) - { - if(this.IsFixedPassword()) - roomconfig.type = 2; - else if(this.IsHiddenLogFromRom()) - roomconfig.type = 3; - else - roomconfig.type = 1; - roomconfig.IsOwned = !this.IsFirstAuth(); - }else{ - roomconfig.type = 0; - } - return roomconfig; - } - this.IsVolatile = function(){ - return this.owner == null && - this.password == null && - this.time == null && - this.hiddenlog == null; - } - this.GetRomCount = function(){ - var count = 0; - for(var key in this.rom_list) - count++; - return count; - }; - this.AddRom = function(ip){ - var date = new Date(); - this.rom_list[ip] = {time:date.getTime()}; - }; - this.RemoveRom = function(ip){ - delete this.rom_list[ip]; - }; - this.Reset = function(owner){ - var date = new Date(); - var time = date.getTime(); - this.password = null; - this.authed_list = {}; - this.owner = owner; - this.time = time; - }; - this.IsFirstAuth = function(){ - return this.owner == null; - }; - this.IsAuthed = function(name){ - return name == this.owner || - name in this.authed_list; - }; - this.IsHiddenLogFromRom = function(){ - return this.hiddenlog; - }; - this.IsFixedPassword = function(){ - return this.owner == $system_name; - }; - this.IsOwner = function(name){ - return this.owner == name; - }; - this.IsTimeout = function(){ - var date = new Date(); - var current_time = date.getTime(); - return !this.IsFixedPassword() && - current_time - this.time >= $reset_password_diff; - }; - this.RemoveAuth = function(name) - { - delete this.authed_list[name]; - }; - this.Auth = function(name,password){ - if(this.password != password) - return false; - var date = new Date(); - var time = date.getTime(); - this.time = time; - this.authed_list[name] = ""; - return true; - }; - this.SetPassword = function(owner,password){ - if(owner == this.owner && - !this.IsFixedPassword() && - !this.IsHiddenLogFromRom()) - { - var date = new Date(); - this.time = date.getTime(); - this.password = password; - return true; - } - return false; - }; - this.GCRomList = function(){ - var date = new Date(); - var current_time = date.getTime(); - for(var ip in this.rom_list) - { - if(current_time - this.rom_list[ip].time >= $gc_time_interval) - delete this.rom_list[ip]; - } - }; -} - -//IPBANクラス -function IpBanCollecion() -{ - var config = require("./configure.js"); - var MySQLPool = new require("./mysql_pool.js"); - var pool = new MySQLPool({ - host : config.db_host, - user : config.db_user, - password : config.db_password, - port : config.db_port, - database : config.db_name, - }); - var collection = {}; - this.IsBaned = function(ip){ - return collection[ip] == "r"; - } - this.IsBlockedToWrite = function(ip){ - return ip in collection; - } - this.GetText = function(){ - var text = ""; - for(var key in collection) - { - if(collection[key] == "") - text += key + "\r\n"; - else - text += key + ":" + collection[key] + "\r\n"; - } - return text; - } - this.Update = function(text,callfunc){ - collection = {}; - var async = require("async"); - async.waterfall([ - function(next){ - pool.query("TRUNCATE TABLE ipbanlist",null,next); - }, - function(result,next){ - var items = new Array(); - lines = text.split("\r\n"); - for(var i = 0; i < lines.length; i++) - { - var token = lines[i].split(":"); - var ip = token[0]; - if(ip == "") - continue; - if(token.length == 1) - collection[ip] = ""; - else - collection[ip] = token[1]; - items.push(new Array(ip,collection[ip])); - } - pool.query("INSERT INTO ipbanlist VALUES ?",[items],next); - }, - ],callfunc); - } - function GetIpBanList(callfunc) - { - var async = require("async"); - async.waterfall([ - function(next){ - pool.query("SELECT * FROM ipbanlist",null,next); - }, - function(result,next){ - for(var i = 0; i < result.length; i++) - collection[result[i].ip] = result[i].type; - next(null,null); - }, - ],callfunc); - } - GetIpBanList(); -} - -var ipbanlist = new IpBanCollecion(); -var $rooms = new RoomInfomationCollection(); - -createLogDirectory(); - function createLogDirectory() { var fs = require("fs"); diff --git a/ipban.js b/ipban.js new file mode 100644 index 0000000..5f2eca9 --- /dev/null +++ b/ipban.js @@ -0,0 +1,72 @@ +//IPBANƒNƒ‰ƒX +module.exports.IpBanCollecion = function() +{ + var config = require("./configure.js"); + var MySQLPool = new require("./mysql_pool.js"); + var pool = new MySQLPool({ + host : config.db_host, + user : config.db_user, + password : config.db_password, + port : config.db_port, + database : config.db_name, + }); + var collection = {}; + this.IsBaned = function(ip){ + return collection[ip] == "r"; + } + this.IsBlockedToWrite = function(ip){ + return ip in collection; + } + this.GetText = function(){ + var text = ""; + for(var key in collection) + { + if(collection[key] == "") + text += key + "\r\n"; + else + text += key + ":" + collection[key] + "\r\n"; + } + return text; + } + this.Update = function(text,callfunc){ + collection = {}; + var async = require("async"); + async.waterfall([ + function(next){ + pool.query("TRUNCATE TABLE ipbanlist",null,next); + }, + function(result,next){ + var items = new Array(); + lines = text.split("\r\n"); + for(var i = 0; i < lines.length; i++) + { + var token = lines[i].split(":"); + var ip = token[0]; + if(ip == "") + continue; + if(token.length == 1) + collection[ip] = ""; + else + collection[ip] = token[1]; + items.push(new Array(ip,collection[ip])); + } + pool.query("INSERT INTO ipbanlist VALUES ?",[items],next); + }, + ],callfunc); + } + function GetIpBanList(callfunc) + { + var async = require("async"); + async.waterfall([ + function(next){ + pool.query("SELECT * FROM ipbanlist",null,next); + }, + function(result,next){ + for(var i = 0; i < result.length; i++) + collection[result[i].ip] = result[i].type; + next(null,null); + }, + ],callfunc); + } + GetIpBanList(); +} diff --git a/room.js b/room.js new file mode 100644 index 0000000..c295da5 --- /dev/null +++ b/room.js @@ -0,0 +1,221 @@ +//RoomInfomationCollecionƒNƒ‰ƒX +module.exports.RoomInfomationCollection = function() +{ + var config = require("./configure.js"); + var MySQLPool = new require("./mysql_pool.js"); + var pool = new MySQLPool({ + host : config.db_host, + user : config.db_user, + password : config.db_password, + port : config.db_port, + database : config.db_name, + }); + var collection = {}; + this.Get = function(rno){ + return collection[rno]; + } + this.IsContains = function(rno){ + return rno in collection; + }; + this.GetMessage = function(){ + var retval = new Array(); + for(var rno in collection) + { + item={}; + item.applyflag = !$rooms.Get(rno).IsVolatile(); + item.password = collection[rno].password; + if(item.password == null) + item.password = ""; + item.hiddenlog = collection[rno].hiddenlog; + retval.push(item); + } + return retval; + }; + this.GetKeys = function(){ + var retval = {}; + for(var rno in collection) + { + retval[rno] = {}; + } + return retval; + } + this.Update = function(data,callfunc){ + Clear(); + var async = require("async"); + async.waterfall([ + function(next){ + pool.query("TRUNCATE TABLE rooms",null,next); + }, + function(result,next){ + var util = require("util"); + console.log(util.inspect(data)); + var items = new Array(); + var config = data.config; + for(var i = 0; i < config.length; i++) + { + var rno = Number(config[i].applyflag); + if(isNaN(rno)) + continue; + var password,romonly; + if(typeof(config[rno].password)=="undefined") + password = null; + else if(config[rno].password == "") + password = null; + else + password = config[rno].password; + if(typeof(config[rno].hiddenlog)=="undefined") + romonly = false; + else + romonly = config[rno].hiddenlog == "romonly"; + + Add(rno,password,romonly); + items.push(new Array(rno,password,romonly)); + } + pool.query("INSERT INTO rooms VALUES ?",[items],callfunc); + } + ],callfunc); + } + function GetRoomList(callback){ + Clear(); + var async = require("async"); + async.waterfall([ + function(next){ + pool.query("SELECT * FROM rooms",null,next); + }, + function(result,next){ + for(var i = 0; i < result.length; i++) + { + //MySQL‚Å‚ÍTINYINT‚ªŽg‚í‚ê‚Ä‚¢‚é + Add(result[i].number,result[i].password,result[i].hiddenlog != 0); + } + next(null,null); + } + ],callback); + } + function Clear(){ + collection = {}; + var config = require("./configure.js"); + for(var i = 0; i < config.max_room_number; i++) + Add(i,null,null); + }; + function Add(rno,pass,hiddenlogflag){ + collection[rno] = new RoomInfomation(pass,hiddenlogflag); + if(pass != null) + collection[rno].owner = $system_name; + }; + var $gc_interval_id = setInterval(function(){ + for(var rno in this.rom_list) + collection[rno].GCRomList(); + },$gc_time_interval); + GetRoomList(); +} + +//RoomInfomationƒNƒ‰ƒX +function RoomInfomation(pass,hiddenlogflag) +{ + this.password = pass; + this.rom_list = {}; + this.authed_list = {}; + this.owner = null; + this.time = null; + this.hiddenlog = hiddenlogflag; + this.GetConfig = function(){ + var roomconfig = {}; + if(this.IsVolatile() == false) + { + if(this.IsFixedPassword()) + roomconfig.type = 2; + else if(this.IsHiddenLogFromRom()) + roomconfig.type = 3; + else + roomconfig.type = 1; + roomconfig.IsOwned = !this.IsFirstAuth(); + }else{ + roomconfig.type = 0; + } + return roomconfig; + } + this.IsVolatile = function(){ + return this.owner == null && + this.password == null && + this.time == null && + this.hiddenlog == null; + } + this.GetRomCount = function(){ + var count = 0; + for(var key in this.rom_list) + count++; + return count; + }; + this.AddRom = function(ip){ + var date = new Date(); + this.rom_list[ip] = {time:date.getTime()}; + }; + this.RemoveRom = function(ip){ + delete this.rom_list[ip]; + }; + this.Reset = function(owner){ + var date = new Date(); + var time = date.getTime(); + this.password = null; + this.authed_list = {}; + this.owner = owner; + this.time = time; + }; + this.IsFirstAuth = function(){ + return this.owner == null; + }; + this.IsAuthed = function(name){ + return name == this.owner || + name in this.authed_list; + }; + this.IsHiddenLogFromRom = function(){ + return this.hiddenlog; + }; + this.IsFixedPassword = function(){ + return this.owner == $system_name; + }; + this.IsOwner = function(name){ + return this.owner == name; + }; + this.IsTimeout = function(){ + var date = new Date(); + var current_time = date.getTime(); + return !this.IsFixedPassword() && + current_time - this.time >= $reset_password_diff; + }; + this.RemoveAuth = function(name) + { + delete this.authed_list[name]; + }; + this.Auth = function(name,password){ + if(this.password != password) + return false; + var date = new Date(); + var time = date.getTime(); + this.time = time; + this.authed_list[name] = ""; + return true; + }; + this.SetPassword = function(owner,password){ + if(owner == this.owner && + !this.IsFixedPassword() && + !this.IsHiddenLogFromRom()) + { + var date = new Date(); + this.time = date.getTime(); + this.password = password; + return true; + } + return false; + }; + this.GCRomList = function(){ + var date = new Date(); + var current_time = date.getTime(); + for(var ip in this.rom_list) + { + if(current_time - this.rom_list[ip].time >= $gc_time_interval) + delete this.rom_list[ip]; + } + }; +} -- 2.11.0