Skip to content

Commit 87b92a3

Browse files
committed
Server Side Documentation for #41
1 parent f140a34 commit 87b92a3

6 files changed

+54
-26
lines changed

docs/Extensions.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ In the future we would like to expand quite a few sections in PrashFour IRC Clie
1111
6. Username typeahead: Currently we are using Bootstrap's typeahead() which always displays the results BELOW the input field (atleast in our CSS where we have absolute positioning), and to work around this we position it relatively a fixed position above, which never makes it look just "right". We could have implemented javascript to make it look right however we'd like to have more adaptive typeahead solution than Bootstrap provides (multiple words, not just the first word, command support, etc)
1212
7. Easter Eggs: I mean, who doesn't love easter eggs?
1313
8. A more advanced /list. Currently we're relying on a "message" view to display the list of things. This restricts the way we can display the data visually. We could have created a new view hoever we did not want to clutter how our views work at the moment.
14-
9. ROUTER Support: We did not have enough time to implement a router, that is, a #hash based url for each location on the app. Even though there is only one REAL page ("/"), there are other dynamically generated pages, that should be /#/channel/name /#/pm/user, etc. An individual URL for each different window on the navbar (chat window either public or private, or LIST or STATUS)
14+
9. ROUTER Support: We did not have enough time to implement a router, that is, a #hash based url for each location on the app. Even though there is only one REAL page ("/"), there are other dynamically generated pages, that should be /#/channel/name /#/pm/user, etc. An individual URL for each different window on the navbar (chat window either public or private, or LIST or STATUS)
15+
10. SSL support that isn't broken: We have the options to select SSL and "self-signed certificates", unfortunately when we tested it with irc.tawx.net +6697, we couldn't connect...even though this is just as simple as passing the SSL parameters to node-irc, we couldn't diagnose why this didn't work...so it'd be good to fix this, as non-SSL IRC makes us sad.

docs/Files.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Files
1111

1212
/views/
1313
======
14-
* index.jade: Loads the javascript files used to manipulate the DOM (Backbone) and access to the templates
14+
* index.jade: Loads the javascript files used to manipulate the DOM (Backbone) and access to the templates (mainly client.js, which has the others via connect-assets)
1515
* template.jade: Contains all the templates used to populate the visuals on the client side
1616

1717
/

lib/ircsocketconnect.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var irc = require('irc');
22
var mongoose = require('mongoose');
33
var config = require('../config').config;
44

5-
// establish models
5+
// establish models from mongoose model
66
var User = mongoose.model('User');
77
var Message = mongoose.model('Message');
88

@@ -29,8 +29,8 @@ var IRCSocketConnect = function (hostname, port, ssl, selfSigned, nick, realName
2929
autoRejoin: rejoin,
3030
autoConnect: true, //Setting autoConnect to false prevents the Client from connecting on instantiation. You will need to call connect() on the client instance:
3131
channels: channels, //Starting channels, for this will be null usually (no client fields yet)
32-
secure: ssl,
33-
selfSigned: selfSigned,
32+
secure: ssl, //SORT OF BROKEN
33+
selfSigned: selfSigned, //SORT OF BROKEN
3434
certExpired: false,
3535
floodProtection: true,
3636
floodProtectionDelay: 1000,
@@ -45,7 +45,7 @@ var IRCSocketConnect = function (hostname, port, ssl, selfSigned, nick, realName
4545
}
4646

4747
this.nodeircInstance = new irc.Client(hostname, nick, connectOptions);
48-
this.keepAlive = keepAlive;
48+
this.keepAlive = keepAlive; //Used when logged in user wants to restore active connection
4949

5050
//All the events that need to be handled and forwarded to socket.io
5151
//from http://node-irc.readthedocs.org/en/latest/
@@ -85,20 +85,19 @@ var IRCSocketConnect = function (hostname, port, ssl, selfSigned, nick, realName
8585
args[arg] = callbackArgs[index]; //taking them, and formatting them the exact way node-irc does, but in an object that can be sent via sockets
8686
});
8787

88-
// emit the event (may be more than 1 socket per person)
88+
// emit the event
8989
for (var i = 0; i < that.sockets.length; i++) {
9090
that.sockets[i].emit(event, args);
9191
}
92-
93-
//Besides sending to client, do server side stuff
9492

93+
//If it is a message, we want to log that
9594
if (event == 'message') {
9695
if (that.username) {
9796
var channelOrUser;
9897
if (args.to[0] != '#') //PM workaround
99-
channelOrUser = args.from.toLowerCase();
98+
channelOrUser = args.from.toLowerCase(); //PM, which means to is ME, so we want them
10099
else
101-
channelOrUser = args.to.toLowerCase();
100+
channelOrUser = args.to.toLowerCase(); //Normal message, should be #channel
102101

103102
// log this message
104103
that.logMessage(channelOrUser, args.from, args.text);
@@ -108,7 +107,7 @@ var IRCSocketConnect = function (hostname, port, ssl, selfSigned, nick, realName
108107
});
109108
};
110109

111-
//Attach the handlers to node-irc
110+
//Attach event handlers for the stuff node-irc spews out
112111
for (var event in this.events) {
113112
this.callbackCreator(event, this.events[event]);
114113
}
@@ -117,12 +116,15 @@ var IRCSocketConnect = function (hostname, port, ssl, selfSigned, nick, realName
117116
// properties and methods
118117
IRCSocketConnect.prototype = {
119118
associateUser: function(username) {
119+
//Attach the users username to this instance
120120
this.username = username;
121121
},
122122
connect: function() {
123+
//Calls node-irc's connect
123124
this.nodeircInstance.connect();
124125
},
125126
disconnect: function() {
127+
//Calls node-irc's disconnect
126128
this.nodeircInstance.disconnect();
127129
},
128130
addSocket: function(socket) {
@@ -136,18 +138,19 @@ IRCSocketConnect.prototype = {
136138
}
137139
},
138140
logMessage: function(channel, fromUser, theMessage) {
141+
//If we're logged in
139142
if (this.username) {
143+
//Save the message
140144
var message = new Message({channel: channel.toLowerCase(), server: this.server.toLowerCase(), ofuser: this.username, user: fromUser, message: theMessage});
141145
message.save();
142146

143147
// keep log size in check (only need so much backlog for each user
148+
//Remove the excess messages (can increase pretty big for a zillion users so we limit it via config.js)
144149
Message.count({}, function(err, count) {
145150
if (count > config.max_log) {
146151
//Get the oldest messages
147-
var query = Message.find({}).limit(count - config.max).sort({date: 1});
148-
149-
//Remove them
150-
query.exec(function (err, records) {
152+
153+
Message.find({}, null, {sort: {date: 1}, limit: count - config.max}, function (err, records) {
151154
records.forEach(function(record){
152155
record.remove();
153156
});

lib/mongooseschema.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ module.exports = function() {
2323
//Users (Logging In System)
2424
//Messages (backlog for the associated ofuser)
2525

26+
//lastSession is sesseionKey+IP
2627
var Users = new Schema({
2728
username: { type: String , index: {unique: true}},
2829
password: String,
29-
lastSession: {type: String, default: ""}
30+
lastSession: {type: String, default: "TECHNICALLYTHISSHOULDNEVERSTAYLIKETHISHOWEVERWHOKNOWSSOIWILLJUSTPUMPITWITHLETTERS"}
3031
});
3132

3233
var Messages = new Schema({

lib/prashfour.js

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ app.configure(function(){
6464
app.set('mongoose_auth', config.mongoose_auth);
6565
});
6666

67+
//Single page app, all we need is '/'
6768
app.get('/', function(req, res){
6869
res.render('index.jade', {port: app.get('port')});
6970
});

lib/sockethandler.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,27 @@ module.exports = function(socket, allConnections) {
1414
socket.emit("latencyPONG", {});
1515
});
1616

17+
//Checks the state of the database
18+
//http://mongoosejs.com/docs/api.html#connection_Connection-readyState
1719
socket.on('isDatabaseConnected', function(){
1820
socket.emit('isDatabaseConnected', {state: mongoose.connection.readyState});
1921
});
2022

2123
socket.on('register', function(data) {
2224
//Duplicate Username Check
2325
User.findOne({username: data.username}, function(err, userAlreadyExists) {
26+
//No need to check any other details, if it matches username, it is a duplicate
2427
if (userAlreadyExists) {
2528
socket.emit('register_error', {message: 'Username already exists'});
2629
}
2730
else {
31+
//Generate a session ID
2832
var sessID = Math.random().toString(36);
33+
//Store it in the DB concatenated with the IP
2934
var user = new User({username: data.username, password: passwordHash.generate(data.password), lastSession: sessID + socket.handshake.address.address});
3035
user.save(); //Save user in database
3136
loggedInUser = user;
32-
socket.emit('register_success', {session: sessID});
37+
socket.emit('register_success', {session: sessID}); //Pass sessID to client
3338
}
3439
});
3540
});
@@ -68,6 +73,7 @@ module.exports = function(socket, allConnections) {
6873
});
6974
});
7075

76+
//Called when the client has a sessID and calls loginBySession
7177
socket.on('loginBySession', function (data) {
7278
User.findOne({lastSession: data.session + socket.handshake.address.address}, function(err, matchingUser) {
7379
//Found a user
@@ -89,52 +95,62 @@ module.exports = function(socket, allConnections) {
8995
});
9096
});
9197

98+
//When the client calls connect
9299
socket.on('connect', function(data) {
93100
var connection;
101+
102+
//If loggedIn, return the connection that exists
94103
if (loggedInUser) {
95104
connection = allConnections[loggedInUser.username];
96105
}
106+
107+
//Create a new connection with the data specified if it wasn't
97108
if (connection === undefined) {
98109
connection = new IRCSocketConnect(data.server, data.port, data.secure, data.selfSigned, data.nick, data.realName, data.password, data.rejoin, data.encoding, data.keepAlive);
99110

100111
// save this connection
112+
// associate if they are logged in
101113
if (loggedInUser) {
102114
// bind this socket to the proper IRC instance
103115
connection.associateUser(loggedInUser.username);
104116
allConnections[loggedInUser.username] = connection;
105117
}
106118
}
107119
else {
108-
if (!connection.keepAlive) {
109-
connection.connect();
110-
}
120+
//Notify the client of the previous connection, with the channel, server, and nick associated with the connection
111121
socket.emit('previous_connection', {
112122
nick: connection.nodeircInstance.nick,
113123
server: connection.nodeircInstance.opt.server,
114124
channels: connection.nodeircInstance.chans
115125
});
116126
}
117127

118-
// register this socket with our user's IRC connection (multiple sockets means multiple windows!)
128+
// register this socket with our user's IRC connection
119129
connection.addSocket(socket);
120130

121131
// Socket events sent FROM the front-end
132+
//Join #chan or "chan" -> #chan
122133
socket.removeAllListeners('join');
123134
socket.on('join', function(name) {
124135
if (name[0] != '#') {
125136
name = '#' + name;
126137
}
138+
//Calls join
127139
connection.nodeircInstance.join(name);
128140
});
129141

142+
//Part channel
143+
//Part #chan or "chan" -> #chan
130144
socket.removeAllListeners('part');
131145
socket.on('part', function(name) {
132146
if (name[0] != '#') {
133147
name = '#' + name;
134148
}
149+
//Calls part
135150
connection.nodeircInstance.part(name);
136151
});
137152

153+
//Clients sends a message
138154
socket.removeAllListeners('say');
139155
socket.on('say', function(data) {
140156
//send it
@@ -146,6 +162,7 @@ module.exports = function(socket, allConnections) {
146162
}
147163
});
148164

165+
//ACTION command
149166
socket.removeAllListeners('action');
150167
socket.on('action', function(data) {
151168
connection.nodeircInstance.action(data.target, data.message);
@@ -156,8 +173,10 @@ module.exports = function(socket, allConnections) {
156173
);
157174
});
158175

176+
//CHANGE of TOPIC via Client
159177
socket.removeAllListeners('topic');
160178
socket.on('topic', function(data) {
179+
//No handling if they have the permissions, assumes they are OP (will error if not, but won't report to client)
161180
connection.nodeircInstance.send('TOPIC ', data.name, data.topic);
162181
});
163182

@@ -172,6 +191,7 @@ module.exports = function(socket, allConnections) {
172191
connection.nodeircInstance.opt.nick = data.newNick;
173192
});
174193

194+
//WHOIS call
175195
socket.removeAllListeners('whois');
176196
socket.on('whois', function(data) {
177197
//Do a WHOIS on NICK
@@ -207,13 +227,15 @@ module.exports = function(socket, allConnections) {
207227
allConnections[loggedInUser.username] = undefined;
208228
loggedInUser = null;
209229
}
210-
230+
231+
//Manually disconnected via client end, tell client to go back to main menu via reset
211232
socket.emit('reset');
212233
});
213234

214235
socket.removeAllListeners('getOldMessages');
215236
socket.on('getOldMessages', function(data) {
216237
if (loggedInUser) {
238+
//Query to get messages in a channel on a certain network of a certain username
217239
Message.find({channel: data.channelName.toLowerCase(), server: connection.server.toLowerCase(), ofuser: loggedInUser.username}, null, {skip: data.skip, sort: {date: -1}, limit: data.amount}, function (err, docs) {
218240
if (docs && docs.length > 0) {
219241
var returnData = {
@@ -226,12 +248,12 @@ module.exports = function(socket, allConnections) {
226248
}
227249
});
228250

251+
//List for the server
229252
socket.removeAllListeners('list');
230253
socket.on('list', function (data) {
231-
connection.nodeircInstance.list(); //takes an array of arguments
254+
//LONG RUNNING
255+
connection.nodeircInstance.list(); //takes an array of arguments, but we don't pass it (not needed)
232256
});
233257

234-
235-
236258
});
237259
}

0 commit comments

Comments
 (0)