forked from lkiesow/nodejs-irc-webclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc_client.html
290 lines (257 loc) · 8.88 KB
/
irc_client.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!DOCTYPE html>
<html>
<head>
<title>JS IRC Client</title>
</head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<style type="text/css">
ul#channel li {
cursor: pointer;
}
div {
padding: 0px;
margin: 0px;
}
ul {
font-size: smaller;
display: block;
list-style-type: none;
}
div#ui ul {
height: 500px;
overflow-y: scroll;
overflow-x: hidden;
margin: 0px;
}
div#userlist {
width: 18%;
height: 100%;
background-color: #bbbbbb;
display: table-cell;
overflow-y: scroll;
overflow-x: hidden;
}
</style>
<script type="text/javascript">
var current_channel = 'server';
function parse_msg( text ) {
var data = text.split( " ", 4 );
if ( data.length == 4 && data[1] == 'PRIVMSG' ) {
var user = text.split( '!~' )[0].replace( ':', '' );
var message = text.replace( /^:[^:]*:/, '' ).replace( /[\n\r]/g, '' );
var receiver = null;
/* The person we want to answer (channel or user). */
if ( data[2].charAt(0) == '#' ) {
receiver = data[2]; // channel
} else {
receiver = user; // user
}
return {
'valid' : 1,
'user' : user,
'channel' : data[2],
'receiver' : receiver,
'message' : message
}
} else {
// not a valid message
return { 'valid' : 0 }
}
}
//var socket = io.connect( window.location.host );
var socket = io.connect( window.location.host );
$(document).ready( function () {
$("#ui").hide();
$('#connect').css( 'padding-top',
( ( $(window).innerHeight() - $('#connect').outerHeight() ) / 2 ) + 'px' );
$("#connect-btn").click( function(e) {
e.preventDefault();
/* UI things… */
$("#connect").hide();
$("#ui").show();
$('#top-box').height( $(window).innerHeight() - $('#bottom-box').outerHeight() - 25 );
$('#top-box ul').height( $('#top-box').innerHeight() - 2 );
/* Connect to IRC. */
socket.send("CONNECT "+ $("#server").val());
socket.send("NICK "+ $("#nick").val() +"\n");
socket.send("USER "+ $("#nick").val() + ' '
+ $("#server").val().split(":")[0]
+ " blubb :"+$("#nick").val() +"\n");
/* Add handler to input form. */
$('#bottom-box form').submit( function() {
var msg = $('#msginput').val();
$('#msginput').val('');
/* Check for commands. */
/* /join */
if ( msg.match( /^\/join .+/i ) ) {
socket.send( 'JOIN ' + msg.split(' ')[1] + "\r\n" );
/* /nick */
} else if ( msg.match( /^\/nick .+/i ) ) {
if (msg.split(' ')[1].match(/^([a-zA-Z0-9]|[-[]\\`\^{}])+$/))
{
socket.send('NICK ' + msg.split(' ')[1] + "\r\n" );
$("#nick").val(msg.split(' ')[1]); //save nick for recnnects...
}
else
{
showMsg("Error: Your Nickname contains illegal letters.", current_channel);
}
/* /help */
} else if ( msg.match( /^\/help.*/i ) ) {
showHelp();
/* /connect */
} else if ( msg.match( /^\/connect .+/i ) ) {
socket.send("CONNECT "+ msg.split(' ')[1]);
socket.send("NICK "+ $("#nick").val() +"\n");
socket.send("USER "+ $("#nick").val() + ' '
+ $("#server").val().split(":")[0]
+ " blubb :"+$("#nick").val() +"\n");
/* /part */
} else if ( msg.match( /^\/part( .+)?$/i ) ) {
if(msg.split(' ')[1]) //channel given
socket.send('PART ' + msg.split(' ')[1] + "\r\n" );
else //no channel given -> use current channel
socket.send('PART ' + current_channel + "\r\n" );
/* /query */
} else if ( msg.match( /^\/query .+/i ) ) {
var username = msg.split(' ')[1];
if ($('#'+username).length == 0)
{
switch_channel( username );
$('#channel').append( '<li onclick="switch_channel($(this).text() );" id="'
+ username + '">' + username + '</li>' );
showMsg('Private conversation with '+username, current_channel);
}
/* /msg */
} else if ( msg.match( /^\/msg .+ .+/i ) ) {
var username = msg.split(' ')[1];
var message = msg.substr(msg.substr(5).indexOf(' ')+6);
socket.send("PRIVMSG "+username+" :"+message+"\r\n");
showMsg($("#nick").val()+ " -> "+ username +" : " + message, "server");
showMsg($("#nick").val()+ " : " + message, username);
/* unknown command */
} else if ( msg.match( /^\/.*/i ) ) {
showMsg( "Error: Unknown command or too few arguments. Type /help for help.", current_channel );
} else {
/* No command. Send as message to current channel */
socket.send("PRIVMSG "+current_channel+" :"+msg+"\r\n");
showMsg($("#nick").val()+" : " + msg, current_channel);
}
return false;
} );
});
});
function showMsg( msg, channel ){
msg = $('<div/>').text(msg).html(); // escape msg
$("#messages").append( '<li '
+ ( ( channel != current_channel ) ? 'style="display:none;"' : '' ) +
' class="' + channel.replace( /^#/, '' ) + '">' + msg + "</li>").ready(function(){
var scrollbox = document.getElementById('messages');
scrollbox.scrollTop = scrollbox.scrollHeight;
});
}
function switch_channel( c ) {
$('#messages li').css( 'display', 'none' );
$('li.' + c.replace( /^#/, '' ) ).css( 'display', 'block' );
current_channel = c;
}
function showHelp(){
showMsg("*************************************", current_channel);
showMsg("Available commands:", current_channel);
showMsg("/join <channel> : Joins the given Channel", current_channel);
showMsg("/nick <nickname> : Changes the nick name to the given one", current_channel);
showMsg("/part [<channel>] : Leaves the given channel or the current one if no channel is given", current_channel);
showMsg("/connect <hostname>:<port> : Connects to the given server (closes any open connection)", current_channel);
showMsg("/query <user> : Opens a private chat with the given user", current_channel);
showMsg("/msg <user> <message> : Sends a message to the given user without opening a new chat.", current_channel);
showMsg("*************************************" , current_channel);
}
socket.on('message', function (data) {
var lines = data.split( "\r\n" );
for ( var i = 0; i < lines.length; i++ ) {
// play ping pong
if ( lines[i].match( /^PING.*/ ) ) {
socket.send( 'PONG ' + lines[i].split(' ')[1] + '\r\n' );
} else if ( lines[i] != '' ) {
var msg = parse_msg( lines[i] );
if ( msg.valid ) {
// check if channel exists. If not -> add it as a new one (query)
if ($('#'+msg.receiver.replace(/^#/, '')).length == 0)
$('#channel').append( '<li onclick="switch_channel($(this).text() );" id="'
+ msg.receiver + '">' + msg.receiver + '</li>' );
showMsg( msg.user + ' : ' + msg.message, msg.receiver );
} else {
msg = lines[i].split(' ');
if ( msg.length > 2 && msg[1] == 'JOIN' ) {
if(msg[0].slice(1, msg[0].indexOf('!'))== $('#nick').val())
{
// own JOIN message -> open channel
switch_channel( msg[2] );
$('#channel').append( '<li onclick="switch_channel( $(this).text() );" id="'
+ msg[2].replace( /^#/, '' ) + '">' + msg[2] + '</li>' );
}
else
{
// TODO: other user's JOIN -> update user list
}
} else if ( msg.length > 2 && msg[1] == 'PART' ) {
if(msg[0].slice(1, msg[0].indexOf('!')) == $('#nick').val())
{
// own PART message -> leave channel
switch_channel( 'server' );
$(msg[2]).remove();
}
else
{
//TODO: other user's PART message -> update user lists of affected channels
}
} else if ( msg.length > 4 && msg[1] == '332' ) { /* Channel motd */
showMsg( lines[i].replace( /^:[^:]*:/, '' ), msg[3] );
} else if ( msg.length > 4 && msg[1] == '353' ) { /* User in channel */
showMsg( 'User in channel: ' + lines[i].split(':')[2], msg[4] );
} else if ( msg.length > 2 && msg[1] == 'MODE' ) { /* Changed user mode. */
showMsg( lines[i].replace( /^.* MODE /, 'Mode ' ) + ' by ' +
msg[0].replace( /^:/, '' ).replace( /!.*$/, '' ), msg[2]);
} else {
showMsg( lines[i], 'server' );
}
}
}
}
});
//TODO: add a cool user list for the channels
</script>
<body style="padding: 0px; margin:0px;">
<div id="connect" style="text-align: center;">
<form>
<label for="server">Server: </label>
<input type="text" id="server" required value="irc.freenode.net:6667" /><br />
<input type="text" id="nick" required placeholder="Your nickname here" />
<input type="submit" id="connect-btn" value="connect" />
</form>
</div>
<div id="ui" style="width: 100%; ">
<div id="top-box" style="display: table; width: 100%;">
<div style="display: table-row; width: 100%;">
<div style="display: table-cell;">
<ul id="messages">
<li>Welcome to IRC webclient</li>
</ul>
</div>
<div id="userlist">
<ul id="channel">
<li id="server" onclick="switch_channel( $(this).text() );">server</li>
</ul>
</div>
</div>
</div>
<div id="bottom-box">
<form action="#">
<input type="text" id="msginput" style="width: 82%" />
<input type="submit" id="postmsg" />
</form>
</div>
</div>
</body>
</html>