-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (123 loc) · 3.05 KB
/
Copy pathindex.js
File metadata and controls
144 lines (123 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var util = require('util');
var net = require('net');
var fs = require('fs');
var stream = require('stream');
var events = require('events');
var clientControllers = []; // populate clientController s ?
var nicknames = {}; // {username,hostname,realname}
var channels = [];
function Server(nodeID, configJSON){
if(!(this instanceof Server)){
return new Server(nodeID, configJSON, requestListner);
}
net.Server.call(this, {allowHalfOpen: true});
var nodeIDconf = configJSON.nodeID;
if(nodeIDconf !== nodeID)
throw new Error('error nodeID');
this.on('connection',connectionListner); // net event: connectionListner(socket)
this.on('clientError', function(err, conn){ // orig http event , conn is the [socket] connection
conn.destroy(err);
});
}
util.inherits(Server, net.Server);
exports.Server = Server;
exports.createServer = function(nodeID,configJSON){
return new Server(nodeID,configJSON);
};
function IrcState(){
this.initState = true;
}
function IrcParser(parseStream){
events.EventEmitter.call(this);
var self = this;
self.buffer = '';
parseStream.on('readable', function(){
var chunk = this.read(); // null or buffer
chunk = chunk || '';
chunk = chunk.toString();
var lineEndPos;
while( (lineEndPos = chunk.search(/\r\n/)) !== -1)
self.parseLine( self.buffer + chunk.slice(0,lineEndPos) ); // string + buffer = string
chunk = chunk.slice(lineEndPos+2);
self.buffer = chunk;
}
self.buffer += chunk;
});
}
var COMMANDS = [{
regex: /NICK/,
value: 'NICK'
},{
regex: /USER/,
value: 'USER'
},{
regex: /QUIT/,
value: 'QUIT'
},{
regex: /JOIN/,
value: 'JOIN'
},{
regex: /PART/,
value: 'PART'
},{
regex: /LIST/,
value: 'LIST'
},{
regex: /PRIVMSG/,
value: 'PRIVMSG'
},{
regex: /WHO/,
value: 'WHO'
}];
IrcParser.prototype.parseLine = function(msgLine){
var self = this;
var command;
command =
//self.emit('command',command,params)
//self.emit('parseError',..)
};
util.inherits(IrcParser,events.EventEmitter);
function ClientController(state){
this.state = state;
}
ClientController.prototype.runCommand = function(command, params){
switch(command){
case 'NICK':
break;
case 'USER':
break;
case 'QUIT':
break;
case 'JOIN':
break;
case 'PART':
break;
case 'LIST':
break;
case 'PRIVMSG':
break;
case 'WHO':
break;
default: // error
}
}
util.inherits(ClientController, events.EventEmitter);
function connectionListner(socket){
var state = new IrcState();
//var executor = new Executor(state);
var clientController = new ClientController(state);
clientController.on('error', function(){
//socket.write(error)
});
clientControllers.push(client); // do we need this?
socket.on('end',function(){
console.log('client end');
});
var parser = new IrcParser(socket);
parser.on('command',function(command,params){
client.runCommand(command,params);
});
parser.on('parseError', function(){
//socket.write(error)
});
}