f36703fa69e4a6b32c895215e3bf5bd97620d1d5
[KiwiIRC.git] / js / util.js
1 function randomString(string_length) {
2 var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
3 var randomstring = '';
4 for (var i=0; i<string_length; i++) {
5 var rnum = Math.floor(Math.random() * chars.length);
6 randomstring += chars.substring(rnum,rnum+1);
7 }
8 return randomstring;
9 }
10
11 String.prototype.trim = function() {
12 return this.replace(/^\s+|\s+$/g, "");
13 };
14
15 String.prototype.lpad = function(length, character){
16 var padding = "";
17 for(var i=0; i<length; i++) padding += character;
18 return (padding + this).slice (-length)
19 };
20
21
22
23 /*
24 PLUGINS
25 Each function in each object is looped through and ran. The resulting text
26 is expected to be returned.
27 */
28 var plugins={};
29 plugins.privmsg = [
30 {
31 name: "html_safe",
32 onprivmsg: function(inp, tabview){
33 return $('<div/>').text(inp).html();
34 }
35 },
36
37 {
38 name: "activity",
39 onprivmsg: function(inp, tabview){
40 if(front.cur_channel.name.toLowerCase() != front.tabviews[tabview.toLowerCase()].name){
41 front.tabviews[tabview].activity();
42 }
43 }
44 },
45
46 {
47 name: "highlight",
48 onprivmsg: function(inp, tabview){
49 if(inp.toLowerCase().indexOf(gateway.nick.toLowerCase()) > -1){
50 if(front.cur_channel.name.toLowerCase() != front.tabviews[tabview.toLowerCase()].name){
51 front.tabviews[tabview].highlight();
52 }
53 if(front.isChannel(front.tabviews[tabview].name))
54 inp = '<span style="color:red;">'+inp+'</span>';
55 }
56
57 if(
58 !front.isChannel(front.tabviews[tabview].name) && front.tabviews[tabview].name != "server"
59 && front.cur_channel.name.toLowerCase() != front.tabviews[tabview.toLowerCase()].name
60 ){
61 front.tabviews[tabview].highlight();
62 }
63 return inp;
64 }
65 },
66
67 /*
68 {
69 name: "images",
70 onprivmsg: function(text){
71 if( !text ) return text;
72 //alert("-"+text+"-");
73 text = text.replace(/^((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?(\.jpg|\.jpeg|\.gif|\.bmp|\.png)$/gi,function(url){
74 var img = '<img src="'+url+'" height="50" width="50" />';
75 return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ img +'</a>';
76 });
77
78 return text;
79 }
80 },
81
82 */
83
84
85 {
86 //Following method taken from: http://snipplr.com/view/13533/convert-text-urls-into-links/
87 name: "linkify_plain",
88 onprivmsg: function(text){
89 if( !text ) return text;
90
91 text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
92 nice = url;
93 if(url.match('^https?:\/\/')){
94 //nice = nice.replace(/^https?:\/\//i,'')
95 }else{
96 url = 'http://'+url;
97 }
98
99 return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice +'</a>';
100 });
101
102 return text;
103 }
104 }
105 ];