summaryrefslogtreecommitdiffstats
path: root/webchat
diff options
context:
space:
mode:
authorlassulus <lassulus@googlemail.com>2013-11-12 04:08:38 +0100
committerlassulus <lassulus@googlemail.com>2013-11-12 04:08:38 +0100
commitb749c216ae84f02330da7396135be11259ec12d9 (patch)
treeb9a6f98f3f2b4f4c5ee60d36e1d11acdf57da267 /webchat
parent721b4fb1b32ee47f158fb3b9a1ad0747ff579044 (diff)
webchat: fix nicklist
Diffstat (limited to 'webchat')
-rw-r--r--webchat/index.js24
-rw-r--r--webchat/public/client.js22
-rw-r--r--webchat/public/functions.js8
3 files changed, 37 insertions, 17 deletions
diff --git a/webchat/index.js b/webchat/index.js
index 3bd92198..9569fc80 100644
--- a/webchat/index.js
+++ b/webchat/index.js
@@ -6,6 +6,7 @@ var connect = require('connect');
var irc = require('irc');
var make_sockjs_server_connection_transport = require('./sockjs_server_connection_transport.js')
var RPC = require('./public/rpc.js');
+var irc_nicks = []
function pluck (key) {
return function (object) {
@@ -21,9 +22,9 @@ clients.broadcast = function (method, params) {
}
var irc_reconnect = function() { //reconnt to irc
- console.log("reconnecting due to pingtimeout")
- irc_client.disconnect()
- irc_client.connect()
+ console.log("would reconnect now")
+// irc_client.disconnect()
+// irc_client.connect()
}
var pingTimeoutDelay = 3*60*1000
@@ -59,21 +60,27 @@ irc_client.on('message#krebs', function(from, message) {
irc_client.on('names#krebs', function(nicks) {
Object.keys(nicks).forEach(function (nick) {
+ irc_nicks.push(nick)
clients.broadcast('join', {type: 'irc', nick: nick})
})
})
irc_client.on('join#krebs', function(nick, msg) {
if (nick !== 'kweb'){
+ irc_nicks.push(nick)
clients.broadcast('join', {type: 'irc', nick: nick})
}
})
irc_client.on('part#krebs', function(nick, rs, msg) {
- clients.broadcast('quit', {type: 'irc', nick: nick})
+ clients.broadcast('part', {type: 'irc', nick: nick})
});
irc_client.on('error', function (error) {
+ irc_nicks.forEach( function(nick) {
+ client.rpc.send('part', {type: 'irc', nick: nick})
+ irc_nicks.splice(irc_nicks.indexOf(nick))
+ })
console.log('irc-client error', error)
})
@@ -89,6 +96,7 @@ echo.on('connection', function (connection) {
client.rpc.register('msg', {msg: 'string'}, function (params, callback) {
callback(null)
clients.broadcast('msg', {type: 'web', nick: client.nick, msg: params.msg})
+ irc_client.say('#krebs', client.nick + ' → ' + params.msg)
})
client.rpc.register('nick', {nick: 'string'}, function (params, callback) {
if (!!~clients.map(pluck('nick')).indexOf(params.nick)) {
@@ -100,11 +108,17 @@ echo.on('connection', function (connection) {
client.nick = params.nick
callback(null)
clients.broadcast('nick', {type: 'web', newnick: client.nick, oldnick: oldnick})
+ irc_client.say('#krebs', oldnick + ' is now known as ' + client.nick)
}
})
connection.on('close', function() { //propagate if client quits the page
clients.splice(clients.indexOf(client));
clients.broadcast('part', {type: 'web', nick: client.nick})
+ irc_client.say('#krebs', client.nick + ' has parted')
+ })
+ //send the irc nicklist to the new joined client
+ irc_nicks.forEach( function(nick) {
+ client.rpc.send('join', {type: 'irc', nick: nick})
})
//send nicklist to newly joined client
clients.map(pluck('nick')).forEach(function (nick) {
@@ -114,6 +128,8 @@ echo.on('connection', function (connection) {
clients.push(client)
//send all including the new client the join
clients.broadcast('join', {type: 'web', nick: client.nick})
+ //send join to irc
+ irc_client.say('#krebs', client.nick + ' has joined')
})
var app = connect()
diff --git a/webchat/public/client.js b/webchat/public/client.js
index d7a5ecab..54ccfe34 100644
--- a/webchat/public/client.js
+++ b/webchat/public/client.js
@@ -3,16 +3,16 @@ settings.sock = new SockJS('/echo');
settings.waiting_callbacks = {}
var transport = make_sockjs_client_transport(settings.sock)
-var rpc = new RPC(transport)
+settings.rpc = new RPC(transport)
-rpc.register('msg', {type: 'string', nick: 'string', msg: 'string'}, function(params, callback) {
+settings.rpc.register('msg', {type: 'string', nick: 'string', msg: 'string'}, function(params, callback) {
var safe_message = $('<div/>').text(params.msg).html();
safe_message = replaceURLWithHTMLLinks(safe_message);
var safe_from = $('<div/>').text(params.nick).html();
chatboxAppend(safe_from, safe_message, 'web_msg')
return callback(null)
})
-rpc.register('nick', {type: 'string', newnick: 'string', oldnick: 'string'}, function(params, callback) {
+settings.rpc.register('nick', {type: 'string', newnick: 'string', oldnick: 'string'}, function(params, callback) {
var safe_oldnick = $('<div/>').text(params.oldnick).html();
var safe_newnick = $('<div/>').text(params.newnick).html();
var safe_type = $('<div/>').text(params.type).html();
@@ -25,20 +25,20 @@ rpc.register('nick', {type: 'string', newnick: 'string', oldnick: 'string'}, fun
chatboxAppend(safe_oldnick, 'is now known as ' + safe_newnick, 'nick');
return callback(null)
})
-rpc.register('your_nick', {nick: 'string'}, function(params, callback) {
+settings.rpc.register('your_nick', {nick: 'string'}, function(params, callback) {
var safe_nick = $('<div/>').text(params.nick).html();
settings.nick = safe_nick
$('#nick').html(settings.nick)
return callback(null)
})
-rpc.register('join', {type: 'string', nick: 'string'}, function(params, callback) {
+settings.rpc.register('join', {type: 'string', nick: 'string'}, function(params, callback) {
var safe_nick = $('<div/>').text(params.nick).html();
var safe_type = $('<div/>').text(params.type).html();
$('#nicklist').append('<div class="'+safe_type+'_name">' + safe_nick + '</div>') ;
chatboxAppend(safe_nick, 'has joined');
return callback(null)
})
-rpc.register('part', {type: 'string', nick: 'string'}, function(params, callback) {
+settings.rpc.register('part', {type: 'string', nick: 'string'}, function(params, callback) {
var safe_nick = $('<div/>').text(params.nick).html();
var safe_type = $('<div/>').text(params.type).html();
$(getNicklistElement(safe_nick,safe_type)).remove();
@@ -60,14 +60,10 @@ $(function() {
e.stopPropagation();
e.stopImmediatePropagation();
var input = ($('#input').val());
- $('#input').val('');
+ $('#input').val('')
- var match = /^\/([a-z]+)(?:\s+(.*\S))?\s*$/.exec(input)
- if (match) {
- return rpc.send(match[1], match[2])
- } else {
- return rpc.send('msg', input)
- }
+ var command = inputParser(input)
+ return (commands[command.method] || commands.badcommand)(settings, command.params)
}
});
diff --git a/webchat/public/functions.js b/webchat/public/functions.js
index 318d0865..781fafce 100644
--- a/webchat/public/functions.js
+++ b/webchat/public/functions.js
@@ -1,3 +1,11 @@
+function inputParser (str) {
+ var match = /^\/([a-z]+)(?:\s+(.*\S))?\s*$/.exec(str)
+ if (match) {
+ return { method: match[1], params: match[2] }
+ } else {
+ return { method: 'msg', params: str }
+ }
+}
function replaceURLWithHTMLLinks (text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;