aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Kavlie <akavlie@gmail.com>2011-11-09 23:22:20 -0700
committerAaron Kavlie <akavlie@gmail.com>2011-11-09 23:22:20 -0700
commita72859ea9917de8eae1b80aff98b8269c2ef5b45 (patch)
treebd002301cc8cbb6f2bd8c9739b044be866e5c346
parent1a9a6884275be070821373c5690f6f881d62c286 (diff)
downloadreevo-webirc-a72859ea9917de8eae1b80aff98b8269c2ef5b45.tar.gz
reevo-webirc-a72859ea9917de8eae1b80aff98b8269c2ef5b45.zip
Add URL parsing for messages.
-rw-r--r--app.js39
-rw-r--r--index.html2
-rw-r--r--js/util.js7
3 files changed, 38 insertions, 10 deletions
diff --git a/app.js b/app.js
index 25f556a..a9f8311 100644
--- a/app.js
+++ b/app.js
@@ -12,10 +12,18 @@ $(function() {
12 defaults: { 12 defaults: {
13 // expected properties: 13 // expected properties:
14 // - sender 14 // - sender
15 // - text 15 // - raw
16 'type': 'message' 16 'type': 'message'
17 }, 17 },
18 18
19 initialize: function() {
20 this.set({text: this.parse( irc.util.escapeHTML(this.get('raw')) )});
21 },
22
23 parse: function(text) {
24 return this._linkify(text);
25 },
26
19 // Set output text for status messages 27 // Set output text for status messages
20 setText: function() { 28 setText: function() {
21 var text = ''; 29 var text = '';
@@ -31,6 +39,21 @@ $(function() {
31 break; 39 break;
32 } 40 }
33 this.set({text: text}); 41 this.set({text: text});
42 },
43
44 // Find and link URLs
45 _linkify: function(text) {
46 // see http://daringfireball.net/2010/07/improved_regex_for_matching_urls
47 var re = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
48 var parsed = text.replace(re, function(url) {
49 // turn into a link
50 var href = url;
51 if (url.indexOf('http') !== 0) {
52 href = 'http://' + url;
53 }
54 return '<a href="' + href + '" target="_blank">' + url + '</a>';
55 });
56 return parsed;
34 } 57 }
35 }); 58 });
36 59
@@ -346,7 +369,7 @@ $(function() {
346 revised = command; 369 revised = command;
347 break; 370 break;
348 } 371 }
349 return irc.utils.swapCommand(command, revised, text); 372 return irc.util.swapCommand(command, revised, text);
350 }, 373 },
351 374
352 sendInput: function(e) { 375 sendInput: function(e) {
@@ -362,7 +385,7 @@ $(function() {
362 var msgParts = parsed.split(' '); 385 var msgParts = parsed.split(' ');
363 if (msgParts[0].toLowerCase() === 'privmsg') { 386 if (msgParts[0].toLowerCase() === 'privmsg') {
364 pm = frames.getByName(msgParts[1]) || new Frame({type: 'pm', name: msg.nick}); 387 pm = frames.getByName(msgParts[1]) || new Frame({type: 'pm', name: msg.nick});
365 pm.stream.add({sender: msg.nick, text: msg.text}) 388 pm.stream.add({sender: msg.nick, raw: msg.text})
366 frames.add(pm); 389 frames.add(pm);
367 } 390 }
368 } else { 391 } else {
@@ -370,7 +393,7 @@ $(function() {
370 target: frame.get('name'), 393 target: frame.get('name'),
371 message: input 394 message: input
372 }); 395 });
373 frame.stream.add({sender: irc.me.get('nick'), text: input}); 396 frame.stream.add({sender: irc.me.get('nick'), raw: input});
374 } 397 }
375 398
376 this.input.val(''); 399 this.input.val('');
@@ -473,20 +496,20 @@ $(function() {
473 msg.to !== 'status') return; 496 msg.to !== 'status') return;
474 frame = frames.getByName(msg.to); 497 frame = frames.getByName(msg.to);
475 if (frame) { 498 if (frame) {
476 frame.stream.add({sender: msg.from, text: msg.text}); 499 frame.stream.add({sender: msg.from, raw: msg.text});
477 } 500 }
478 }); 501 });
479 502
480 socket.on('pm', function(msg) { 503 socket.on('pm', function(msg) {
481 pm = frames.getByName(msg.nick) || new Frame({type: 'pm', name: msg.nick}); 504 pm = frames.getByName(msg.nick) || new Frame({type: 'pm', name: msg.nick});
482 pm.stream.add({sender: msg.nick, text: msg.text}) 505 pm.stream.add({sender: msg.nick, raw: msg.text})
483 frames.add(pm); 506 frames.add(pm);
484 }) 507 })
485 508
486 // Message of the Day event (on joining a server) 509 // Message of the Day event (on joining a server)
487 socket.on('motd', function(data) { 510 socket.on('motd', function(data) {
488 data.motd.split('\n').forEach(function(line) { 511 data.motd.split('\n').forEach(function(line) {
489 frames.getByName('status').stream.add({sender: '', text: line}); 512 frames.getByName('status').stream.add({sender: '', raw: line});
490 }); 513 });
491 }); 514 });
492 515
@@ -560,7 +583,7 @@ $(function() {
560 console.log(data.message); 583 console.log(data.message);
561 frame = frames.getActive(); 584 frame = frames.getActive();
562 error = humanizeError(data.message); 585 error = humanizeError(data.message);
563 frame.stream.add({text: error, type: 'error'}) 586 frame.stream.add({type: 'error', raw: error});
564 }); 587 });
565 588
566}); \ No newline at end of file 589}); \ No newline at end of file
diff --git a/index.html b/index.html
index 2899b43..e908e0b 100644
--- a/index.html
+++ b/index.html
@@ -64,7 +64,7 @@
64 <!-- Mustache templates --> 64 <!-- Mustache templates -->
65 <script id="message-tmpl" type="text/html"> 65 <script id="message-tmpl" type="text/html">
66 {{#sender}}<span class="sender">{{sender}}</span>{{/sender}} 66 {{#sender}}<span class="sender">{{sender}}</span>{{/sender}}
67 <span class="text {{^sender}}no-sender{{/sender}}">{{text}}</span> 67 <span class="text {{^sender}}no-sender{{/sender}}">{{{text}}</span>
68 </script> 68 </script>
69 69
70 <script id="tab-tmpl" type="text/html"> 70 <script id="tab-tmpl" type="text/html">
diff --git a/js/util.js b/js/util.js
index 6082c1a..9bb429a 100644
--- a/js/util.js
+++ b/js/util.js
@@ -2,6 +2,7 @@
2(function(b){function c(){}for(var d="assert,clear,count,debug,dir,dirxml,error,exception,firebug,group,groupCollapsed,groupEnd,info,log,memoryProfile,memoryProfileEnd,profile,profileEnd,table,time,timeEnd,timeStamp,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try 2(function(b){function c(){}for(var d="assert,clear,count,debug,dir,dirxml,error,exception,firebug,group,groupCollapsed,groupEnd,info,log,memoryProfile,memoryProfileEnd,profile,profileEnd,table,time,timeEnd,timeStamp,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
3{console.log();return window.console;}catch(err){return window.console={};}})()); 3{console.log();return window.console;}catch(err){return window.console={};}})());
4 4
5
5// POLYFILLS 6// POLYFILLS
6// ========= 7// =========
7 8
@@ -74,14 +75,18 @@ if ( !Array.prototype.forEach ) {
74// ================= 75// =================
75 76
76window.irc = (function(module) { 77window.irc = (function(module) {
77 module.utils = { 78 module.util = {
78 // Replaces oldString with newString at beginning of text 79 // Replaces oldString with newString at beginning of text
79 swapCommand: function(oldString, newString, text) { 80 swapCommand: function(oldString, newString, text) {
80 if (text.substring(0, oldString.length) === oldString) 81 if (text.substring(0, oldString.length) === oldString)
81 return newString + text.substring(oldString.length, text.length); 82 return newString + text.substring(oldString.length, text.length);
82 else 83 else
83 throw 'String "' + oldString + '" not found at beginning of text'; 84 throw 'String "' + oldString + '" not found at beginning of text';
85 },
86 escapeHTML: function(text) {
87 return text.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
84 } 88 }
89
85 } 90 }
86 91
87 return module 92 return module