• R/O
  • HTTP
  • SSH
  • HTTPS

WebChat: Commit

チャットシステム


Commit MetaInfo

Revisiona336b85c60cbccbcaab44f2947b3652477d72ad4 (tree)
Time2014-05-31 01:28:48
Authorkonekoneko <jbh03215@hotm...>
Commiterkonekoneko

Log Message

一部クラスを別ファイルに分離した

Change Summary

Incremental Difference

--- a/chat.js
+++ b/chat.js
@@ -10,6 +10,11 @@ $splited_log_file_name = "logfile%d_%s.txt" //分割後のファイル名(%dと%
1010
1111 var clients = new Array();
1212
13+var ipbanlist = new require("./ipban.js").IpBanCollecion();
14+var $rooms = new require("./room.js").new RoomInfomationCollection();
15+
16+createLogDirectory();
17+
1318 var sessionStore;
1419
1520 module.exports = function(app,server,express,session){
@@ -120,306 +125,6 @@ function removeLog(files,callback)
120125 });
121126 }
122127
123-//RoomInfomationCollecionクラス
124-function RoomInfomationCollection()
125-{
126- var config = require("./configure.js");
127- var MySQLPool = new require("./mysql_pool.js");
128- var pool = new MySQLPool({
129- host : config.db_host,
130- user : config.db_user,
131- password : config.db_password,
132- port : config.db_port,
133- database : config.db_name,
134- });
135- var collection = {};
136- this.Get = function(rno){
137- return collection[rno];
138- }
139- this.IsContains = function(rno){
140- return rno in collection;
141- };
142- this.GetMessage = function(){
143- var retval = new Array();
144- for(var rno in collection)
145- {
146- item={};
147- item.applyflag = !$rooms.Get(rno).IsVolatile();
148- item.password = collection[rno].password;
149- if(item.password == null)
150- item.password = "";
151- item.hiddenlog = collection[rno].hiddenlog;
152- retval.push(item);
153- }
154- return retval;
155- };
156- this.GetKeys = function(){
157- var retval = {};
158- for(var rno in collection)
159- {
160- retval[rno] = {};
161- }
162- return retval;
163- }
164- this.Update = function(data,callfunc){
165- Clear();
166- var async = require("async");
167- async.waterfall([
168- function(next){
169- pool.query("TRUNCATE TABLE rooms",null,next);
170- },
171- function(result,next){
172- var util = require("util");
173- console.log(util.inspect(data));
174- var items = new Array();
175- var config = data.config;
176- for(var i = 0; i < config.length; i++)
177- {
178- var rno = Number(config[i].applyflag);
179- if(isNaN(rno))
180- continue;
181- var password,romonly;
182- if(typeof(config[rno].password)=="undefined")
183- password = null;
184- else if(config[rno].password == "")
185- password = null;
186- else
187- password = config[rno].password;
188- if(typeof(config[rno].hiddenlog)=="undefined")
189- romonly = false;
190- else
191- romonly = config[rno].hiddenlog == "romonly";
192-
193- Add(rno,password,romonly);
194- items.push(new Array(rno,password,romonly));
195- }
196- pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
197- }
198- ],callfunc);
199- }
200- function GetRoomList(callback){
201- Clear();
202- var async = require("async");
203- async.waterfall([
204- function(next){
205- pool.query("SELECT * FROM rooms",null,next);
206- },
207- function(result,next){
208- for(var i = 0; i < result.length; i++)
209- {
210- //MySQLではTINYINTが使われている
211- Add(result[i].number,result[i].password,result[i].hiddenlog != 0);
212- }
213- next(null,null);
214- }
215- ],callback);
216- }
217- function Clear(){
218- collection = {};
219- var config = require("./configure.js");
220- for(var i = 0; i < config.max_room_number; i++)
221- Add(i,null,null);
222- };
223- function Add(rno,pass,hiddenlogflag){
224- collection[rno] = new RoomInfomation(pass,hiddenlogflag);
225- if(pass != null)
226- collection[rno].owner = $system_name;
227- };
228- var $gc_interval_id = setInterval(function(){
229- for(var rno in this.rom_list)
230- collection[rno].GCRomList();
231- },$gc_time_interval);
232- GetRoomList();
233-}
234-
235-//RoomInfomationクラス
236-function RoomInfomation(pass,hiddenlogflag)
237-{
238- this.password = pass;
239- this.rom_list = {};
240- this.authed_list = {};
241- this.owner = null;
242- this.time = null;
243- this.hiddenlog = hiddenlogflag;
244- this.GetConfig = function(){
245- var roomconfig = {};
246- if(this.IsVolatile() == false)
247- {
248- if(this.IsFixedPassword())
249- roomconfig.type = 2;
250- else if(this.IsHiddenLogFromRom())
251- roomconfig.type = 3;
252- else
253- roomconfig.type = 1;
254- roomconfig.IsOwned = !this.IsFirstAuth();
255- }else{
256- roomconfig.type = 0;
257- }
258- return roomconfig;
259- }
260- this.IsVolatile = function(){
261- return this.owner == null &&
262- this.password == null &&
263- this.time == null &&
264- this.hiddenlog == null;
265- }
266- this.GetRomCount = function(){
267- var count = 0;
268- for(var key in this.rom_list)
269- count++;
270- return count;
271- };
272- this.AddRom = function(ip){
273- var date = new Date();
274- this.rom_list[ip] = {time:date.getTime()};
275- };
276- this.RemoveRom = function(ip){
277- delete this.rom_list[ip];
278- };
279- this.Reset = function(owner){
280- var date = new Date();
281- var time = date.getTime();
282- this.password = null;
283- this.authed_list = {};
284- this.owner = owner;
285- this.time = time;
286- };
287- this.IsFirstAuth = function(){
288- return this.owner == null;
289- };
290- this.IsAuthed = function(name){
291- return name == this.owner ||
292- name in this.authed_list;
293- };
294- this.IsHiddenLogFromRom = function(){
295- return this.hiddenlog;
296- };
297- this.IsFixedPassword = function(){
298- return this.owner == $system_name;
299- };
300- this.IsOwner = function(name){
301- return this.owner == name;
302- };
303- this.IsTimeout = function(){
304- var date = new Date();
305- var current_time = date.getTime();
306- return !this.IsFixedPassword() &&
307- current_time - this.time >= $reset_password_diff;
308- };
309- this.RemoveAuth = function(name)
310- {
311- delete this.authed_list[name];
312- };
313- this.Auth = function(name,password){
314- if(this.password != password)
315- return false;
316- var date = new Date();
317- var time = date.getTime();
318- this.time = time;
319- this.authed_list[name] = "";
320- return true;
321- };
322- this.SetPassword = function(owner,password){
323- if(owner == this.owner &&
324- !this.IsFixedPassword() &&
325- !this.IsHiddenLogFromRom())
326- {
327- var date = new Date();
328- this.time = date.getTime();
329- this.password = password;
330- return true;
331- }
332- return false;
333- };
334- this.GCRomList = function(){
335- var date = new Date();
336- var current_time = date.getTime();
337- for(var ip in this.rom_list)
338- {
339- if(current_time - this.rom_list[ip].time >= $gc_time_interval)
340- delete this.rom_list[ip];
341- }
342- };
343-}
344-
345-//IPBANクラス
346-function IpBanCollecion()
347-{
348- var config = require("./configure.js");
349- var MySQLPool = new require("./mysql_pool.js");
350- var pool = new MySQLPool({
351- host : config.db_host,
352- user : config.db_user,
353- password : config.db_password,
354- port : config.db_port,
355- database : config.db_name,
356- });
357- var collection = {};
358- this.IsBaned = function(ip){
359- return collection[ip] == "r";
360- }
361- this.IsBlockedToWrite = function(ip){
362- return ip in collection;
363- }
364- this.GetText = function(){
365- var text = "";
366- for(var key in collection)
367- {
368- if(collection[key] == "")
369- text += key + "\r\n";
370- else
371- text += key + ":" + collection[key] + "\r\n";
372- }
373- return text;
374- }
375- this.Update = function(text,callfunc){
376- collection = {};
377- var async = require("async");
378- async.waterfall([
379- function(next){
380- pool.query("TRUNCATE TABLE ipbanlist",null,next);
381- },
382- function(result,next){
383- var items = new Array();
384- lines = text.split("\r\n");
385- for(var i = 0; i < lines.length; i++)
386- {
387- var token = lines[i].split(":");
388- var ip = token[0];
389- if(ip == "")
390- continue;
391- if(token.length == 1)
392- collection[ip] = "";
393- else
394- collection[ip] = token[1];
395- items.push(new Array(ip,collection[ip]));
396- }
397- pool.query("INSERT INTO ipbanlist VALUES ?",[items],next);
398- },
399- ],callfunc);
400- }
401- function GetIpBanList(callfunc)
402- {
403- var async = require("async");
404- async.waterfall([
405- function(next){
406- pool.query("SELECT * FROM ipbanlist",null,next);
407- },
408- function(result,next){
409- for(var i = 0; i < result.length; i++)
410- collection[result[i].ip] = result[i].type;
411- next(null,null);
412- },
413- ],callfunc);
414- }
415- GetIpBanList();
416-}
417-
418-var ipbanlist = new IpBanCollecion();
419-var $rooms = new RoomInfomationCollection();
420-
421-createLogDirectory();
422-
423128 function createLogDirectory()
424129 {
425130 var fs = require("fs");
--- /dev/null
+++ b/ipban.js
@@ -0,0 +1,72 @@
1+//IPBANクラス
2+module.exports.IpBanCollecion = 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+}
--- /dev/null
+++ b/room.js
@@ -0,0 +1,221 @@
1+//RoomInfomationCollecionクラス
2+module.exports.RoomInfomationCollection = 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.Get = function(rno){
15+ return collection[rno];
16+ }
17+ this.IsContains = function(rno){
18+ return rno in collection;
19+ };
20+ this.GetMessage = function(){
21+ var retval = new Array();
22+ for(var rno in collection)
23+ {
24+ item={};
25+ item.applyflag = !$rooms.Get(rno).IsVolatile();
26+ item.password = collection[rno].password;
27+ if(item.password == null)
28+ item.password = "";
29+ item.hiddenlog = collection[rno].hiddenlog;
30+ retval.push(item);
31+ }
32+ return retval;
33+ };
34+ this.GetKeys = function(){
35+ var retval = {};
36+ for(var rno in collection)
37+ {
38+ retval[rno] = {};
39+ }
40+ return retval;
41+ }
42+ this.Update = function(data,callfunc){
43+ Clear();
44+ var async = require("async");
45+ async.waterfall([
46+ function(next){
47+ pool.query("TRUNCATE TABLE rooms",null,next);
48+ },
49+ function(result,next){
50+ var util = require("util");
51+ console.log(util.inspect(data));
52+ var items = new Array();
53+ var config = data.config;
54+ for(var i = 0; i < config.length; i++)
55+ {
56+ var rno = Number(config[i].applyflag);
57+ if(isNaN(rno))
58+ continue;
59+ var password,romonly;
60+ if(typeof(config[rno].password)=="undefined")
61+ password = null;
62+ else if(config[rno].password == "")
63+ password = null;
64+ else
65+ password = config[rno].password;
66+ if(typeof(config[rno].hiddenlog)=="undefined")
67+ romonly = false;
68+ else
69+ romonly = config[rno].hiddenlog == "romonly";
70+
71+ Add(rno,password,romonly);
72+ items.push(new Array(rno,password,romonly));
73+ }
74+ pool.query("INSERT INTO rooms VALUES ?",[items],callfunc);
75+ }
76+ ],callfunc);
77+ }
78+ function GetRoomList(callback){
79+ Clear();
80+ var async = require("async");
81+ async.waterfall([
82+ function(next){
83+ pool.query("SELECT * FROM rooms",null,next);
84+ },
85+ function(result,next){
86+ for(var i = 0; i < result.length; i++)
87+ {
88+ //MySQLではTINYINTが使われている
89+ Add(result[i].number,result[i].password,result[i].hiddenlog != 0);
90+ }
91+ next(null,null);
92+ }
93+ ],callback);
94+ }
95+ function Clear(){
96+ collection = {};
97+ var config = require("./configure.js");
98+ for(var i = 0; i < config.max_room_number; i++)
99+ Add(i,null,null);
100+ };
101+ function Add(rno,pass,hiddenlogflag){
102+ collection[rno] = new RoomInfomation(pass,hiddenlogflag);
103+ if(pass != null)
104+ collection[rno].owner = $system_name;
105+ };
106+ var $gc_interval_id = setInterval(function(){
107+ for(var rno in this.rom_list)
108+ collection[rno].GCRomList();
109+ },$gc_time_interval);
110+ GetRoomList();
111+}
112+
113+//RoomInfomationクラス
114+function RoomInfomation(pass,hiddenlogflag)
115+{
116+ this.password = pass;
117+ this.rom_list = {};
118+ this.authed_list = {};
119+ this.owner = null;
120+ this.time = null;
121+ this.hiddenlog = hiddenlogflag;
122+ this.GetConfig = function(){
123+ var roomconfig = {};
124+ if(this.IsVolatile() == false)
125+ {
126+ if(this.IsFixedPassword())
127+ roomconfig.type = 2;
128+ else if(this.IsHiddenLogFromRom())
129+ roomconfig.type = 3;
130+ else
131+ roomconfig.type = 1;
132+ roomconfig.IsOwned = !this.IsFirstAuth();
133+ }else{
134+ roomconfig.type = 0;
135+ }
136+ return roomconfig;
137+ }
138+ this.IsVolatile = function(){
139+ return this.owner == null &&
140+ this.password == null &&
141+ this.time == null &&
142+ this.hiddenlog == null;
143+ }
144+ this.GetRomCount = function(){
145+ var count = 0;
146+ for(var key in this.rom_list)
147+ count++;
148+ return count;
149+ };
150+ this.AddRom = function(ip){
151+ var date = new Date();
152+ this.rom_list[ip] = {time:date.getTime()};
153+ };
154+ this.RemoveRom = function(ip){
155+ delete this.rom_list[ip];
156+ };
157+ this.Reset = function(owner){
158+ var date = new Date();
159+ var time = date.getTime();
160+ this.password = null;
161+ this.authed_list = {};
162+ this.owner = owner;
163+ this.time = time;
164+ };
165+ this.IsFirstAuth = function(){
166+ return this.owner == null;
167+ };
168+ this.IsAuthed = function(name){
169+ return name == this.owner ||
170+ name in this.authed_list;
171+ };
172+ this.IsHiddenLogFromRom = function(){
173+ return this.hiddenlog;
174+ };
175+ this.IsFixedPassword = function(){
176+ return this.owner == $system_name;
177+ };
178+ this.IsOwner = function(name){
179+ return this.owner == name;
180+ };
181+ this.IsTimeout = function(){
182+ var date = new Date();
183+ var current_time = date.getTime();
184+ return !this.IsFixedPassword() &&
185+ current_time - this.time >= $reset_password_diff;
186+ };
187+ this.RemoveAuth = function(name)
188+ {
189+ delete this.authed_list[name];
190+ };
191+ this.Auth = function(name,password){
192+ if(this.password != password)
193+ return false;
194+ var date = new Date();
195+ var time = date.getTime();
196+ this.time = time;
197+ this.authed_list[name] = "";
198+ return true;
199+ };
200+ this.SetPassword = function(owner,password){
201+ if(owner == this.owner &&
202+ !this.IsFixedPassword() &&
203+ !this.IsHiddenLogFromRom())
204+ {
205+ var date = new Date();
206+ this.time = date.getTime();
207+ this.password = password;
208+ return true;
209+ }
210+ return false;
211+ };
212+ this.GCRomList = function(){
213+ var date = new Date();
214+ var current_time = date.getTime();
215+ for(var ip in this.rom_list)
216+ {
217+ if(current_time - this.rom_list[ip].time >= $gc_time_interval)
218+ delete this.rom_list[ip];
219+ }
220+ };
221+}
Show on old repository browser